2006-01-29 02:52:42 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2005, Sam Leffler <sam@errno.com>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice unmodified, this list of conditions, and the following
|
|
|
|
* disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/taskqueue.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/linker.h>
|
|
|
|
#include <sys/firmware.h>
|
2006-11-06 13:42:10 +00:00
|
|
|
#include <sys/priv.h>
|
2006-01-29 02:52:42 +00:00
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/module.h>
|
|
|
|
|
|
|
|
#define FIRMWARE_MAX 30
|
|
|
|
static struct firmware firmware_table[FIRMWARE_MAX];
|
|
|
|
struct task firmware_task;
|
|
|
|
struct mtx firmware_mtx;
|
|
|
|
MTX_SYSINIT(firmware, &firmware_mtx, "firmware table", MTX_DEF);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Register a firmware image with the specified name. The
|
|
|
|
* image name must not already be registered. If this is a
|
|
|
|
* subimage then parent refers to a previously registered
|
|
|
|
* image that this should be associated with.
|
|
|
|
*/
|
|
|
|
struct firmware *
|
|
|
|
firmware_register(const char *imagename, const void *data, size_t datasize,
|
|
|
|
unsigned int version, struct firmware *parent)
|
|
|
|
{
|
|
|
|
struct firmware *frp = NULL;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
mtx_lock(&firmware_mtx);
|
|
|
|
for (i = 0; i < FIRMWARE_MAX; i++) {
|
|
|
|
struct firmware *fp = &firmware_table[i];
|
|
|
|
|
|
|
|
if (fp->name == NULL) {
|
|
|
|
if (frp == NULL)
|
|
|
|
frp = fp;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strcasecmp(imagename, fp->name) == 0) {
|
|
|
|
mtx_unlock(&firmware_mtx);
|
|
|
|
printf("%s: image %s already registered!\n",
|
|
|
|
__func__, imagename);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (frp == NULL) {
|
|
|
|
mtx_unlock(&firmware_mtx);
|
|
|
|
printf("%s: cannot register image %s, firmware table full!\n",
|
|
|
|
__func__, imagename);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
frp->name = imagename;
|
|
|
|
frp->data = data;
|
|
|
|
frp->datasize = datasize;
|
|
|
|
frp->version = version;
|
|
|
|
frp->refcnt = 0;
|
2006-06-10 17:04:07 +00:00
|
|
|
frp->flags = 0;
|
2006-01-29 02:52:42 +00:00
|
|
|
if (parent != NULL)
|
|
|
|
parent->refcnt++;
|
|
|
|
frp->parent = parent;
|
|
|
|
frp->file = NULL;
|
|
|
|
mtx_unlock(&firmware_mtx);
|
|
|
|
return frp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-06-10 17:04:07 +00:00
|
|
|
clearentry(struct firmware *fp)
|
2006-01-29 02:52:42 +00:00
|
|
|
{
|
|
|
|
KASSERT(fp->refcnt == 0, ("image %s refcnt %u", fp->name, fp->refcnt));
|
2006-06-10 17:04:07 +00:00
|
|
|
fp->name = NULL;
|
|
|
|
fp->file = NULL;
|
2006-01-29 02:52:42 +00:00
|
|
|
fp->data = NULL;
|
|
|
|
fp->datasize = 0;
|
|
|
|
fp->version = 0;
|
2006-06-10 17:04:07 +00:00
|
|
|
fp->flags = 0;
|
2006-01-29 02:52:42 +00:00
|
|
|
if (fp->parent != NULL) { /* release parent reference */
|
|
|
|
fp->parent->refcnt--;
|
|
|
|
fp->parent = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct firmware *
|
|
|
|
lookup(const char *name)
|
|
|
|
{
|
2006-06-13 21:34:12 +00:00
|
|
|
struct firmware *fp;
|
2006-01-29 02:52:42 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < FIRMWARE_MAX; i++) {
|
2006-06-13 21:34:12 +00:00
|
|
|
fp = &firmware_table[i];
|
2006-01-29 02:52:42 +00:00
|
|
|
if (fp->name != NULL && strcasecmp(name, fp->name) == 0)
|
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unregister/remove a firmware image. If there are outstanding
|
|
|
|
* references an error is returned and the image is not removed
|
|
|
|
* from the registry.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
firmware_unregister(const char *imagename)
|
|
|
|
{
|
|
|
|
struct firmware *fp;
|
|
|
|
int refcnt = 0;
|
|
|
|
|
|
|
|
mtx_lock(&firmware_mtx);
|
|
|
|
/*
|
|
|
|
* NB: it is ok for the lookup to fail; this can happen
|
|
|
|
* when a module is unloaded on last reference and the
|
|
|
|
* module unload handler unregister's each of it's
|
|
|
|
* firmware images.
|
|
|
|
*/
|
|
|
|
fp = lookup(imagename);
|
|
|
|
if (fp != NULL) {
|
|
|
|
refcnt = fp->refcnt;
|
|
|
|
if (refcnt == 0)
|
2006-06-10 17:04:07 +00:00
|
|
|
clearentry(fp);
|
2006-01-29 02:52:42 +00:00
|
|
|
}
|
|
|
|
mtx_unlock(&firmware_mtx);
|
|
|
|
return (refcnt != 0 ? EBUSY : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lookup and potentially load the specified firmware image.
|
|
|
|
* If the firmware is not found in the registry attempt to
|
|
|
|
* load a kernel module with the image name. If the firmware
|
|
|
|
* is located a reference is returned. The caller must release
|
|
|
|
* this reference for the image to be eligible for removal/unload.
|
|
|
|
*/
|
|
|
|
struct firmware *
|
|
|
|
firmware_get(const char *imagename)
|
|
|
|
{
|
|
|
|
struct thread *td;
|
|
|
|
struct firmware *fp;
|
|
|
|
linker_file_t result;
|
|
|
|
int requested_load = 0;
|
|
|
|
|
|
|
|
again:
|
|
|
|
mtx_lock(&firmware_mtx);
|
|
|
|
fp = lookup(imagename);
|
|
|
|
if (fp != NULL) {
|
|
|
|
if (requested_load)
|
|
|
|
fp->file = result;
|
|
|
|
fp->refcnt++;
|
|
|
|
mtx_unlock(&firmware_mtx);
|
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Image not present, try to load the module holding it
|
|
|
|
* or if we already tried give up.
|
|
|
|
*/
|
|
|
|
mtx_unlock(&firmware_mtx);
|
|
|
|
if (requested_load) {
|
|
|
|
printf("%s: failed to load firmware image %s\n",
|
|
|
|
__func__, imagename);
|
2007-01-27 19:52:08 +00:00
|
|
|
(void) linker_release_module(imagename, NULL, NULL);
|
2006-01-29 02:52:42 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
td = curthread;
|
2006-11-06 13:42:10 +00:00
|
|
|
if (priv_check(td, PRIV_FIRMWARE_LOAD) != 0 ||
|
|
|
|
securelevel_gt(td->td_ucred, 0) != 0) {
|
2006-01-29 02:52:42 +00:00
|
|
|
printf("%s: insufficient privileges to "
|
|
|
|
"load firmware image %s\n", __func__, imagename);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
(void) linker_reference_module(imagename, NULL, &result);
|
|
|
|
requested_load = 1;
|
|
|
|
goto again; /* sort of an Algol-style for loop */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
unloadentry(void *unused1, int unused2)
|
|
|
|
{
|
|
|
|
struct firmware *fp;
|
2006-06-10 17:04:07 +00:00
|
|
|
linker_file_t file;
|
2006-06-25 12:36:21 +00:00
|
|
|
int i, err;
|
2006-01-29 02:52:42 +00:00
|
|
|
|
|
|
|
mtx_lock(&firmware_mtx);
|
2006-06-10 17:04:07 +00:00
|
|
|
for (;;) {
|
|
|
|
/* Look for an unwanted entry that we explicitly loaded. */
|
|
|
|
for (i = 0; i < FIRMWARE_MAX; i++) {
|
|
|
|
fp = &firmware_table[i];
|
|
|
|
if (fp->name != NULL && fp->file != NULL &&
|
|
|
|
fp->refcnt == 0 &&
|
|
|
|
(fp->flags & FIRMWAREFLAG_KEEPKLDREF) == 0)
|
|
|
|
break;
|
|
|
|
fp = NULL;
|
|
|
|
}
|
|
|
|
if (fp == NULL)
|
|
|
|
break;
|
|
|
|
file = fp->file;
|
|
|
|
/* No longer explicitly loaded. */
|
|
|
|
fp->file = NULL;
|
2006-01-29 02:52:42 +00:00
|
|
|
mtx_unlock(&firmware_mtx);
|
|
|
|
|
2006-06-25 12:36:21 +00:00
|
|
|
err = linker_release_module(NULL, NULL, file);
|
2006-01-29 02:52:42 +00:00
|
|
|
|
|
|
|
mtx_lock(&firmware_mtx);
|
2006-06-25 12:36:21 +00:00
|
|
|
if (err) {
|
|
|
|
/*
|
|
|
|
* If linker_release_module() failed then we still
|
|
|
|
* hold a reference on the module so it should not be
|
|
|
|
* possible for it to go away or be re-registered.
|
|
|
|
*/
|
|
|
|
KASSERT(fp->file == NULL,
|
|
|
|
("firmware entry reused while referenced!"));
|
|
|
|
fp->file = file;
|
|
|
|
}
|
2006-01-29 02:52:42 +00:00
|
|
|
}
|
|
|
|
mtx_unlock(&firmware_mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Release a reference to a firmware image returned by
|
|
|
|
* firmware_get. The reference is released and if this is
|
|
|
|
* the last reference to the firmware image the associated
|
|
|
|
* module may be released/unloaded.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
firmware_put(struct firmware *fp, int flags)
|
|
|
|
{
|
|
|
|
mtx_lock(&firmware_mtx);
|
|
|
|
fp->refcnt--;
|
2006-06-10 17:04:07 +00:00
|
|
|
if (fp->refcnt == 0) {
|
|
|
|
if ((flags & FIRMWARE_UNLOAD) == 0)
|
|
|
|
fp->flags |= FIRMWAREFLAG_KEEPKLDREF;
|
|
|
|
}
|
2006-01-29 02:52:42 +00:00
|
|
|
if (fp->file)
|
|
|
|
taskqueue_enqueue(taskqueue_thread, &firmware_task);
|
|
|
|
mtx_unlock(&firmware_mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Module glue.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
firmware_modevent(module_t mod, int type, void *unused)
|
|
|
|
{
|
2006-06-13 21:34:12 +00:00
|
|
|
struct firmware *fp;
|
2006-06-10 17:04:07 +00:00
|
|
|
int i;
|
|
|
|
|
2006-01-29 02:52:42 +00:00
|
|
|
switch (type) {
|
|
|
|
case MOD_LOAD:
|
|
|
|
TASK_INIT(&firmware_task, 0, unloadentry, NULL);
|
|
|
|
return 0;
|
|
|
|
case MOD_UNLOAD:
|
2006-06-10 17:04:07 +00:00
|
|
|
for (i = 0; i < FIRMWARE_MAX; i++) {
|
2006-06-13 21:34:12 +00:00
|
|
|
fp = &firmware_table[i];
|
2006-06-10 17:04:07 +00:00
|
|
|
fp->flags &= ~FIRMWAREFLAG_KEEPKLDREF;
|
|
|
|
}
|
|
|
|
taskqueue_enqueue(taskqueue_thread, &firmware_task);
|
2006-01-29 02:52:42 +00:00
|
|
|
taskqueue_drain(taskqueue_thread, &firmware_task);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static moduledata_t firmware_mod = {
|
|
|
|
"firmware",
|
|
|
|
firmware_modevent,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
DECLARE_MODULE(firmware, firmware_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
|
|
|
|
MODULE_VERSION(firmware, 1);
|