2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005-2008, Sam Leffler <sam@errno.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/queue.h>
36 #include <sys/taskqueue.h>
37 #include <sys/systm.h>
39 #include <sys/mutex.h>
40 #include <sys/errno.h>
41 #include <sys/linker.h>
42 #include <sys/firmware.h>
45 #include <sys/module.h>
46 #include <sys/eventhandler.h>
48 #include <sys/filedesc.h>
49 #include <sys/vnode.h>
52 * Loadable firmware support. See sys/sys/firmware.h and firmware(9)
53 * form more details on the subsystem.
55 * 'struct firmware' is the user-visible part of the firmware table.
56 * Additional internal information is stored in a 'struct priv_fw'
57 * (currently a static array). A slot is in use if FW_INUSE is true:
60 #define FW_INUSE(p) ((p)->file != NULL || (p)->fw.name != NULL)
63 * fw.name != NULL when an image is registered; file != NULL for
64 * autoloaded images whose handling has not been completed.
66 * The state of a slot evolves as follows:
67 * firmware_register --> fw.name = image_name
68 * (autoloaded image) --> file = module reference
69 * firmware_unregister --> fw.name = NULL
70 * (unloadentry complete) --> file = NULL
72 * In order for the above to work, the 'file' field must remain
73 * unchanged in firmware_unregister().
75 * Images residing in the same module are linked to each other
76 * through the 'parent' argument of firmware_register().
77 * One image (typically, one with the same name as the module to let
78 * the autoloading mechanism work) is considered the parent image for
79 * all other images in the same module. Children affect the refcount
80 * on the parent image preventing improper unloading of the image itself.
84 int refcnt; /* reference count */
87 * parent entry, see above. Set on firmware_register(),
88 * cleared on firmware_unregister().
90 struct priv_fw *parent;
92 int flags; /* record FIRMWARE_UNLOAD requests */
93 #define FW_UNLOAD 0x100
96 * 'file' is private info managed by the autoload/unload code.
97 * Set at the end of firmware_get(), cleared only in the
98 * firmware_unload_task, so the latter can depend on its value even
99 * while the lock is not held.
101 linker_file_t file; /* module file, if autoloaded */
104 * 'fw' is the externally visible image information.
105 * We do not make it the first field in priv_fw, to avoid the
106 * temptation of casting pointers to each other.
107 * Use PRIV_FW(fw) to get a pointer to the cointainer of fw.
108 * Beware, PRIV_FW does not work for a NULL pointer.
110 struct firmware fw; /* externally visible information */
114 * PRIV_FW returns the pointer to the container of struct firmware *x.
115 * Cast to intptr_t to override the 'const' attribute of x
117 #define PRIV_FW(x) ((struct priv_fw *) \
118 ((intptr_t)(x) - offsetof(struct priv_fw, fw)) )
121 * At the moment we use a static array as backing store for the registry.
122 * Should we move to a dynamic structure, keep in mind that we cannot
123 * reallocate the array because pointers are held externally.
124 * A list may work, though.
126 #define FIRMWARE_MAX 50
127 static struct priv_fw firmware_table[FIRMWARE_MAX];
130 * Firmware module operations are handled in a separate task as they
131 * might sleep and they require directory context to do i/o.
133 static struct taskqueue *firmware_tq;
134 static struct task firmware_unload_task;
137 * This mutex protects accesses to the firmware table.
139 static struct mtx firmware_mtx;
140 MTX_SYSINIT(firmware, &firmware_mtx, "firmware table", MTX_DEF);
143 * Helper function to lookup a name.
144 * As a side effect, it sets the pointer to a free slot, if any.
145 * This way we can concentrate most of the registry scanning in
146 * this function, which makes it easier to replace the registry
147 * with some other data structure.
149 static struct priv_fw *
150 lookup(const char *name, struct priv_fw **empty_slot)
152 struct priv_fw *fp = NULL;
153 struct priv_fw *dummy;
156 if (empty_slot == NULL)
159 for (i = 0; i < FIRMWARE_MAX; i++) {
160 fp = &firmware_table[i];
161 if (fp->fw.name != NULL && strcasecmp(name, fp->fw.name) == 0)
163 else if (!FW_INUSE(fp))
166 return (i < FIRMWARE_MAX ) ? fp : NULL;
170 * Register a firmware image with the specified name. The
171 * image name must not already be registered. If this is a
172 * subimage then parent refers to a previously registered
173 * image that this should be associated with.
175 const struct firmware *
176 firmware_register(const char *imagename, const void *data, size_t datasize,
177 unsigned int version, const struct firmware *parent)
179 struct priv_fw *match, *frp;
182 str = strdup(imagename, M_TEMP);
184 mtx_lock(&firmware_mtx);
186 * Do a lookup to make sure the name is unique or find a free slot.
188 match = lookup(imagename, &frp);
190 mtx_unlock(&firmware_mtx);
191 printf("%s: image %s already registered!\n",
192 __func__, imagename);
197 mtx_unlock(&firmware_mtx);
198 printf("%s: cannot register image %s, firmware table full!\n",
199 __func__, imagename);
203 bzero(frp, sizeof(*frp)); /* start from a clean record */
206 frp->fw.datasize = datasize;
207 frp->fw.version = version;
209 frp->parent = PRIV_FW(parent);
210 mtx_unlock(&firmware_mtx);
212 printf("firmware: '%s' version %u: %zu bytes loaded at %p\n",
213 imagename, version, datasize, data);
218 * Unregister/remove a firmware image. If there are outstanding
219 * references an error is returned and the image is not removed
223 firmware_unregister(const char *imagename)
228 mtx_lock(&firmware_mtx);
229 fp = lookup(imagename, NULL);
232 * It is ok for the lookup to fail; this can happen
233 * when a module is unloaded on last reference and the
234 * module unload handler unregister's each of its
238 } else if (fp->refcnt != 0) { /* cannot unregister */
241 linker_file_t x = fp->file; /* save value */
244 * Clear the whole entry with bzero to make sure we
245 * do not forget anything. Then restore 'file' which is
246 * non-null for autoloaded images.
248 free((void *) (uintptr_t) fp->fw.name, M_TEMP);
249 bzero(fp, sizeof(struct priv_fw));
253 mtx_unlock(&firmware_mtx);
258 loadimage(void *arg, int npending)
260 struct thread *td = curthread;
261 char *imagename = arg;
263 linker_file_t result;
266 /* synchronize with the thread that dispatched us */
267 mtx_lock(&firmware_mtx);
268 mtx_unlock(&firmware_mtx);
270 if (td->td_proc->p_fd->fd_rdir == NULL) {
271 printf("%s: root not mounted yet, no way to load image\n",
275 error = linker_reference_module(imagename, NULL, &result);
277 printf("%s: could not load firmware image, error %d\n",
282 mtx_lock(&firmware_mtx);
283 fp = lookup(imagename, NULL);
284 if (fp == NULL || fp->file != NULL) {
285 mtx_unlock(&firmware_mtx);
287 printf("%s: firmware image loaded, "
288 "but did not register\n", imagename);
289 (void) linker_release_module(imagename, NULL, NULL);
292 fp->file = result; /* record the module identity */
293 mtx_unlock(&firmware_mtx);
295 wakeup_one(imagename); /* we're done */
299 * Lookup and potentially load the specified firmware image.
300 * If the firmware is not found in the registry, try to load a kernel
301 * module named as the image name.
302 * If the firmware is located, a reference is returned. The caller must
303 * release this reference for the image to be eligible for removal/unload.
305 const struct firmware *
306 firmware_get(const char *imagename)
308 struct task fwload_task;
312 mtx_lock(&firmware_mtx);
313 fp = lookup(imagename, NULL);
317 * Image not present, try to load the module holding it.
320 if (priv_check(td, PRIV_FIRMWARE_LOAD) != 0 ||
321 securelevel_gt(td->td_ucred, 0) != 0) {
322 mtx_unlock(&firmware_mtx);
323 printf("%s: insufficient privileges to "
324 "load firmware image %s\n", __func__, imagename);
328 * Defer load to a thread with known context. linker_reference_module
329 * may do filesystem i/o which requires root & current dirs, etc.
330 * Also we must not hold any mtx's over this call which is problematic.
333 TASK_INIT(&fwload_task, 0, loadimage, __DECONST(void *,
335 taskqueue_enqueue(firmware_tq, &fwload_task);
336 msleep(__DECONST(void *, imagename), &firmware_mtx, 0,
340 * After attempting to load the module, see if the image is registered.
342 fp = lookup(imagename, NULL);
344 mtx_unlock(&firmware_mtx);
347 found: /* common exit point on success */
348 if (fp->refcnt == 0 && fp->parent != NULL)
349 fp->parent->refcnt++;
351 mtx_unlock(&firmware_mtx);
356 * Release a reference to a firmware image returned by firmware_get.
357 * The caller may specify, with the FIRMWARE_UNLOAD flag, its desire
358 * to release the resource, but the flag is only advisory.
360 * If this is the last reference to the firmware image, and this is an
361 * autoloaded module, wake up the firmware_unload_task to figure out
362 * what to do with the associated module.
365 firmware_put(const struct firmware *p, int flags)
367 struct priv_fw *fp = PRIV_FW(p);
369 mtx_lock(&firmware_mtx);
371 if (fp->refcnt == 0) {
372 if (fp->parent != NULL)
373 fp->parent->refcnt--;
374 if (flags & FIRMWARE_UNLOAD)
375 fp->flags |= FW_UNLOAD;
377 taskqueue_enqueue(firmware_tq, &firmware_unload_task);
379 mtx_unlock(&firmware_mtx);
383 * Setup directory state for the firmware_tq thread so we can do i/o.
386 set_rootvnode(void *arg, int npending)
395 * Event handler called on mounting of /; bounce a task
396 * into the task queue thread to setup it's directories.
399 firmware_mountroot(void *arg)
401 struct task *setroot_task;
403 setroot_task = malloc(sizeof(struct task), M_TEMP, M_NOWAIT);
404 if (setroot_task != NULL) {
405 TASK_INIT(setroot_task, 0, set_rootvnode, setroot_task);
406 taskqueue_enqueue(firmware_tq, setroot_task);
408 printf("%s: no memory for task!\n", __func__);
410 EVENTHANDLER_DEFINE(mountroot, firmware_mountroot, NULL, 0);
413 * The body of the task in charge of unloading autoloaded modules
414 * that are not needed anymore.
415 * Images can be cross-linked so we may need to make multiple passes,
416 * but the time we spend in the loop is bounded because we clear entries
420 unloadentry(void *unused1, int unused2)
422 int limit = FIRMWARE_MAX;
423 int i; /* current cycle */
425 mtx_lock(&firmware_mtx);
427 * Scan the table. limit is set to make sure we make another
428 * full sweep after matching an entry that requires unloading.
430 for (i = 0; i < limit; i++) {
434 fp = &firmware_table[i % FIRMWARE_MAX];
435 if (fp->fw.name == NULL || fp->file == NULL ||
436 fp->refcnt != 0 || (fp->flags & FW_UNLOAD) == 0)
440 * Found an entry. Now:
441 * 1. bump up limit to make sure we make another full round;
442 * 2. clear FW_UNLOAD so we don't try this entry again.
443 * 3. release the lock while trying to unload the module.
444 * 'file' remains set so that the entry cannot be reused
445 * in the meantime (it also means that fp->file will
446 * not change while we release the lock).
448 limit = i + FIRMWARE_MAX; /* make another full round */
449 fp->flags &= ~FW_UNLOAD; /* do not try again */
451 mtx_unlock(&firmware_mtx);
452 err = linker_release_module(NULL, NULL, fp->file);
453 mtx_lock(&firmware_mtx);
456 * We rely on the module to call firmware_unregister()
457 * on unload to actually release the entry.
458 * If err = 0 we can drop our reference as the system
459 * accepted it. Otherwise unloading failed (e.g. the
460 * module itself gave an error) so our reference is
466 mtx_unlock(&firmware_mtx);
473 firmware_modevent(module_t mod, int type, void *unused)
480 TASK_INIT(&firmware_unload_task, 0, unloadentry, NULL);
481 firmware_tq = taskqueue_create("taskqueue_firmware", M_WAITOK,
482 taskqueue_thread_enqueue, &firmware_tq);
483 /* NB: use our own loop routine that sets up context */
484 (void) taskqueue_start_threads(&firmware_tq, 1, PWAIT,
486 if (rootvnode != NULL) {
488 * Root is already mounted so we won't get an event;
491 firmware_mountroot(NULL);
496 /* request all autoloaded modules to be released */
497 mtx_lock(&firmware_mtx);
498 for (i = 0; i < FIRMWARE_MAX; i++) {
499 fp = &firmware_table[i];
500 fp->flags |= FW_UNLOAD;
502 mtx_unlock(&firmware_mtx);
503 taskqueue_enqueue(firmware_tq, &firmware_unload_task);
504 taskqueue_drain(firmware_tq, &firmware_unload_task);
506 for (i = 0; i < FIRMWARE_MAX; i++) {
507 fp = &firmware_table[i];
508 if (fp->fw.name != NULL) {
509 printf("%s: image %p ref %d still active slot %d\n",
510 __func__, fp->fw.name,
516 taskqueue_free(firmware_tq);
522 static moduledata_t firmware_mod = {
527 DECLARE_MODULE(firmware, firmware_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
528 MODULE_VERSION(firmware, 1);