FreeBSD kernel kern code
subr_firmware.c
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2005-2008, Sam Leffler <sam@errno.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice unmodified, this list of conditions, and the following
10  * disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$BSDSUniX$");
29 
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/malloc.h>
33 #include <sys/queue.h>
34 #include <sys/taskqueue.h>
35 #include <sys/systm.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/errno.h>
39 #include <sys/linker.h>
40 #include <sys/firmware.h>
41 #include <sys/priv.h>
42 #include <sys/proc.h>
43 #include <sys/module.h>
44 #include <sys/eventhandler.h>
45 
46 #include <sys/filedesc.h>
47 #include <sys/vnode.h>
48 
49 /*
50  * Loadable firmware support. See sys/sys/firmware.h and firmware(9)
51  * form more details on the subsystem.
52  *
53  * 'struct firmware' is the user-visible part of the firmware table.
54  * Additional internal information is stored in a 'struct priv_fw'
55  * (currently a static array). A slot is in use if FW_INUSE is true:
56  */
57 
58 #define FW_INUSE(p) ((p)->file != NULL || (p)->fw.name != NULL)
59 
60 /*
61  * fw.name != NULL when an image is registered; file != NULL for
62  * autoloaded images whose handling has not been completed.
63  *
64  * The state of a slot evolves as follows:
65  * firmware_register --> fw.name = image_name
66  * (autoloaded image) --> file = module reference
67  * firmware_unregister --> fw.name = NULL
68  * (unloadentry complete) --> file = NULL
69  *
70  * In order for the above to work, the 'file' field must remain
71  * unchanged in firmware_unregister().
72  *
73  * Images residing in the same module are linked to each other
74  * through the 'parent' argument of firmware_register().
75  * One image (typically, one with the same name as the module to let
76  * the autoloading mechanism work) is considered the parent image for
77  * all other images in the same module. Children affect the refcount
78  * on the parent image preventing improper unloading of the image itself.
79  */
80 
81 struct priv_fw {
82  int refcnt; /* reference count */
83 
84  /*
85  * parent entry, see above. Set on firmware_register(),
86  * cleared on firmware_unregister().
87  */
88  struct priv_fw *parent;
89 
90  int flags; /* record FIRMWARE_UNLOAD requests */
91 #define FW_UNLOAD 0x100
92 
93  /*
94  * 'file' is private info managed by the autoload/unload code.
95  * Set at the end of firmware_get(), cleared only in the
96  * firmware_unload_task, so the latter can depend on its value even
97  * while the lock is not held.
98  */
99  linker_file_t file; /* module file, if autoloaded */
100 
101  /*
102  * 'fw' is the externally visible image information.
103  * We do not make it the first field in priv_fw, to avoid the
104  * temptation of casting pointers to each other.
105  * Use PRIV_FW(fw) to get a pointer to the cointainer of fw.
106  * Beware, PRIV_FW does not work for a NULL pointer.
107  */
108  struct firmware fw; /* externally visible information */
109 };
110 
111 /*
112  * PRIV_FW returns the pointer to the container of struct firmware *x.
113  * Cast to intptr_t to override the 'const' attribute of x
114  */
115 #define PRIV_FW(x) ((struct priv_fw *) \
116  ((intptr_t)(x) - offsetof(struct priv_fw, fw)) )
117 
118 /*
119  * At the moment we use a static array as backing store for the registry.
120  * Should we move to a dynamic structure, keep in mind that we cannot
121  * reallocate the array because pointers are held externally.
122  * A list may work, though.
123  */
124 #define FIRMWARE_MAX 50
126 
127 /*
128  * Firmware module operations are handled in a separate task as they
129  * might sleep and they require directory context to do i/o.
130  */
131 static struct taskqueue *firmware_tq;
132 static struct task firmware_unload_task;
133 
134 /*
135  * This mutex protects accesses to the firmware table.
136  */
137 static struct mtx firmware_mtx;
138 MTX_SYSINIT(firmware, &firmware_mtx, "firmware table", MTX_DEF);
139 
140 /*
141  * Helper function to lookup a name.
142  * As a side effect, it sets the pointer to a free slot, if any.
143  * This way we can concentrate most of the registry scanning in
144  * this function, which makes it easier to replace the registry
145  * with some other data structure.
146  */
147 static struct priv_fw *
148 lookup(const char *name, struct priv_fw **empty_slot)
149 {
150  struct priv_fw *fp = NULL;
151  struct priv_fw *dummy;
152  int i;
153 
154  if (empty_slot == NULL)
155  empty_slot = &dummy;
156  *empty_slot = NULL;
157  for (i = 0; i < FIRMWARE_MAX; i++) {
158  fp = &firmware_table[i];
159  if (fp->fw.name != NULL && strcasecmp(name, fp->fw.name) == 0)
160  break;
161  else if (!FW_INUSE(fp))
162  *empty_slot = fp;
163  }
164  return (i < FIRMWARE_MAX ) ? fp : NULL;
165 }
166 
167 /*
168  * Register a firmware image with the specified name. The
169  * image name must not already be registered. If this is a
170  * subimage then parent refers to a previously registered
171  * image that this should be associated with.
172  */
173 const struct firmware *
174 firmware_register(const char *imagename, const void *data, size_t datasize,
175  unsigned int version, const struct firmware *parent)
176 {
177  struct priv_fw *match, *frp;
178 
179  mtx_lock(&firmware_mtx);
180  /*
181  * Do a lookup to make sure the name is unique or find a free slot.
182  */
183  match = lookup(imagename, &frp);
184  if (match != NULL) {
185  mtx_unlock(&firmware_mtx);
186  printf("%s: image %s already registered!\n",
187  __func__, imagename);
188  return NULL;
189  }
190  if (frp == NULL) {
191  mtx_unlock(&firmware_mtx);
192  printf("%s: cannot register image %s, firmware table full!\n",
193  __func__, imagename);
194  return NULL;
195  }
196  bzero(frp, sizeof(*frp)); /* start from a clean record */
197  frp->fw.name = imagename;
198  frp->fw.data = data;
199  frp->fw.datasize = datasize;
200  frp->fw.version = version;
201  if (parent != NULL)
202  frp->parent = PRIV_FW(parent);
203  mtx_unlock(&firmware_mtx);
204  if (bootverbose)
205  printf("firmware: '%s' version %u: %zu bytes loaded at %p\n",
206  imagename, version, datasize, data);
207  return &frp->fw;
208 }
209 
210 /*
211  * Unregister/remove a firmware image. If there are outstanding
212  * references an error is returned and the image is not removed
213  * from the registry.
214  */
215 int
216 firmware_unregister(const char *imagename)
217 {
218  struct priv_fw *fp;
219  int err;
220 
221  mtx_lock(&firmware_mtx);
222  fp = lookup(imagename, NULL);
223  if (fp == NULL) {
224  /*
225  * It is ok for the lookup to fail; this can happen
226  * when a module is unloaded on last reference and the
227  * module unload handler unregister's each of it's
228  * firmware images.
229  */
230  err = 0;
231  } else if (fp->refcnt != 0) { /* cannot unregister */
232  err = EBUSY;
233  } else {
234  linker_file_t x = fp->file; /* save value */
235 
236  /*
237  * Clear the whole entry with bzero to make sure we
238  * do not forget anything. Then restore 'file' which is
239  * non-null for autoloaded images.
240  */
241  bzero(fp, sizeof(struct priv_fw));
242  fp->file = x;
243  err = 0;
244  }
245  mtx_unlock(&firmware_mtx);
246  return err;
247 }
248 
249 static void
250 loadimage(void *arg, int npending)
251 {
252  struct thread *td = curthread;
253  char *imagename = arg;
254  struct priv_fw *fp;
255  linker_file_t result;
256  int error;
257 
258  /* synchronize with the thread that dispatched us */
259  mtx_lock(&firmware_mtx);
260  mtx_unlock(&firmware_mtx);
261 
262  if (td->td_proc->p_fd->fd_rdir == NULL) {
263  printf("%s: root not mounted yet, no way to load image\n",
264  imagename);
265  goto done;
266  }
267  error = linker_reference_module(imagename, NULL, &result);
268  if (error != 0) {
269  printf("%s: could not load firmware image, error %d\n",
270  imagename, error);
271  goto done;
272  }
273 
274  mtx_lock(&firmware_mtx);
275  fp = lookup(imagename, NULL);
276  if (fp == NULL || fp->file != NULL) {
277  mtx_unlock(&firmware_mtx);
278  if (fp == NULL)
279  printf("%s: firmware image loaded, "
280  "but did not register\n", imagename);
281  (void) linker_release_module(imagename, NULL, NULL);
282  goto done;
283  }
284  fp->file = result; /* record the module identity */
285  mtx_unlock(&firmware_mtx);
286 done:
287  wakeup_one(imagename); /* we're done */
288 }
289 
290 /*
291  * Lookup and potentially load the specified firmware image.
292  * If the firmware is not found in the registry, try to load a kernel
293  * module named as the image name.
294  * If the firmware is located, a reference is returned. The caller must
295  * release this reference for the image to be eligible for removal/unload.
296  */
297 const struct firmware *
298 firmware_get(const char *imagename)
299 {
300  struct task fwload_task;
301  struct thread *td;
302  struct priv_fw *fp;
303 
304  mtx_lock(&firmware_mtx);
305  fp = lookup(imagename, NULL);
306  if (fp != NULL)
307  goto found;
308  /*
309  * Image not present, try to load the module holding it.
310  */
311  td = curthread;
312  if (priv_check(td, PRIV_FIRMWARE_LOAD) != 0 ||
313  securelevel_gt(td->td_ucred, 0) != 0) {
314  mtx_unlock(&firmware_mtx);
315  printf("%s: insufficient privileges to "
316  "load firmware image %s\n", __func__, imagename);
317  return NULL;
318  }
319  /*
320  * Defer load to a thread with known context. linker_reference_module
321  * may do filesystem i/o which requires root & current dirs, etc.
322  * Also we must not hold any mtx's over this call which is problematic.
323  */
324  if (!cold) {
325  TASK_INIT(&fwload_task, 0, loadimage, __DECONST(void *,
326  imagename));
327  taskqueue_enqueue(firmware_tq, &fwload_task);
328  msleep(__DECONST(void *, imagename), &firmware_mtx, 0,
329  "fwload", 0);
330  }
331  /*
332  * After attempting to load the module, see if the image is registered.
333  */
334  fp = lookup(imagename, NULL);
335  if (fp == NULL) {
336  mtx_unlock(&firmware_mtx);
337  return NULL;
338  }
339 found: /* common exit point on success */
340  if (fp->refcnt == 0 && fp->parent != NULL)
341  fp->parent->refcnt++;
342  fp->refcnt++;
343  mtx_unlock(&firmware_mtx);
344  return &fp->fw;
345 }
346 
347 /*
348  * Release a reference to a firmware image returned by firmware_get.
349  * The caller may specify, with the FIRMWARE_UNLOAD flag, its desire
350  * to release the resource, but the flag is only advisory.
351  *
352  * If this is the last reference to the firmware image, and this is an
353  * autoloaded module, wake up the firmware_unload_task to figure out
354  * what to do with the associated module.
355  */
356 void
357 firmware_put(const struct firmware *p, int flags)
358 {
359  struct priv_fw *fp = PRIV_FW(p);
360 
361  mtx_lock(&firmware_mtx);
362  fp->refcnt--;
363  if (fp->refcnt == 0) {
364  if (fp->parent != NULL)
365  fp->parent->refcnt--;
366  if (flags & FIRMWARE_UNLOAD)
367  fp->flags |= FW_UNLOAD;
368  if (fp->file)
370  }
371  mtx_unlock(&firmware_mtx);
372 }
373 
374 /*
375  * Setup directory state for the firmware_tq thread so we can do i/o.
376  */
377 static void
378 set_rootvnode(void *arg, int npending)
379 {
380  struct thread *td = curthread;
381  struct proc *p = td->td_proc;
382 
383  FILEDESC_XLOCK(p->p_fd);
384  if (p->p_fd->fd_cdir == NULL) {
385  p->p_fd->fd_cdir = rootvnode;
386  VREF(rootvnode);
387  }
388  if (p->p_fd->fd_rdir == NULL) {
389  p->p_fd->fd_rdir = rootvnode;
390  VREF(rootvnode);
391  }
392  FILEDESC_XUNLOCK(p->p_fd);
393 
394  free(arg, M_TEMP);
395 }
396 
397 /*
398  * Event handler called on mounting of /; bounce a task
399  * into the task queue thread to setup it's directories.
400  */
401 static void
403 {
404  struct task *setroot_task;
405 
406  setroot_task = malloc(sizeof(struct task), M_TEMP, M_NOWAIT);
407  if (setroot_task != NULL) {
408  TASK_INIT(setroot_task, 0, set_rootvnode, setroot_task);
409  taskqueue_enqueue(firmware_tq, setroot_task);
410  } else
411  printf("%s: no memory for task!\n", __func__);
412 }
413 EVENTHANDLER_DEFINE(mountroot, firmware_mountroot, NULL, 0);
414 
415 /*
416  * The body of the task in charge of unloading autoloaded modules
417  * that are not needed anymore.
418  * Images can be cross-linked so we may need to make multiple passes,
419  * but the time we spend in the loop is bounded because we clear entries
420  * as we touch them.
421  */
422 static void
423 unloadentry(void *unused1, int unused2)
424 {
425  int limit = FIRMWARE_MAX;
426  int i; /* current cycle */
427 
428  mtx_lock(&firmware_mtx);
429  /*
430  * Scan the table. limit is set to make sure we make another
431  * full sweep after matching an entry that requires unloading.
432  */
433  for (i = 0; i < limit; i++) {
434  struct priv_fw *fp;
435  int err;
436 
437  fp = &firmware_table[i % FIRMWARE_MAX];
438  if (fp->fw.name == NULL || fp->file == NULL ||
439  fp->refcnt != 0 || (fp->flags & FW_UNLOAD) == 0)
440  continue;
441 
442  /*
443  * Found an entry. Now:
444  * 1. bump up limit to make sure we make another full round;
445  * 2. clear FW_UNLOAD so we don't try this entry again.
446  * 3. release the lock while trying to unload the module.
447  * 'file' remains set so that the entry cannot be reused
448  * in the meantime (it also means that fp->file will
449  * not change while we release the lock).
450  */
451  limit = i + FIRMWARE_MAX; /* make another full round */
452  fp->flags &= ~FW_UNLOAD; /* do not try again */
453 
454  mtx_unlock(&firmware_mtx);
455  err = linker_release_module(NULL, NULL, fp->file);
456  mtx_lock(&firmware_mtx);
457 
458  /*
459  * We rely on the module to call firmware_unregister()
460  * on unload to actually release the entry.
461  * If err = 0 we can drop our reference as the system
462  * accepted it. Otherwise unloading failed (e.g. the
463  * module itself gave an error) so our reference is
464  * still valid.
465  */
466  if (err == 0)
467  fp->file = NULL;
468  }
469  mtx_unlock(&firmware_mtx);
470 }
471 
472 /*
473  * Module glue.
474  */
475 static int
476 firmware_modevent(module_t mod, int type, void *unused)
477 {
478  struct priv_fw *fp;
479  int i, err;
480 
481  switch (type) {
482  case MOD_LOAD:
483  TASK_INIT(&firmware_unload_task, 0, unloadentry, NULL);
484  firmware_tq = taskqueue_create("taskqueue_firmware", M_WAITOK,
485  taskqueue_thread_enqueue, &firmware_tq);
486  /* NB: use our own loop routine that sets up context */
487  (void) taskqueue_start_threads(&firmware_tq, 1, PWAIT,
488  "firmware taskq");
489  if (rootvnode != NULL) {
490  /*
491  * Root is already mounted so we won't get an event;
492  * simulate one here.
493  */
494  firmware_mountroot(NULL);
495  }
496  return 0;
497 
498  case MOD_UNLOAD:
499  /* request all autoloaded modules to be released */
500  mtx_lock(&firmware_mtx);
501  for (i = 0; i < FIRMWARE_MAX; i++) {
502  fp = &firmware_table[i];
503  fp->flags |= FW_UNLOAD;
504  }
505  mtx_unlock(&firmware_mtx);
507  taskqueue_drain(firmware_tq, &firmware_unload_task);
508  err = 0;
509  for (i = 0; i < FIRMWARE_MAX; i++) {
510  fp = &firmware_table[i];
511  if (fp->fw.name != NULL) {
512  printf("%s: image %p ref %d still active slot %d\n",
513  __func__, fp->fw.name,
514  fp->refcnt, i);
515  err = EINVAL;
516  }
517  }
518  if (err == 0)
519  taskqueue_free(firmware_tq);
520  return err;
521  }
522  return EINVAL;
523 }
524 
525 static moduledata_t firmware_mod = {
526  "firmware",
528  NULL
529 };
530 DECLARE_MODULE(firmware, firmware_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
531 MODULE_VERSION(firmware, 1);
MTX_SYSINIT(firmware,&firmware_mtx,"firmware table", MTX_DEF)
#define FIRMWARE_MAX
void taskqueue_drain(struct taskqueue *queue, struct task *task)
int firmware_unregister(const char *imagename)
int bootverbose
Definition: init_main.c:107
void * malloc(unsigned long size, struct malloc_type *mtp, int flags)
Definition: kern_malloc.c:454
static void set_rootvnode(void *arg, int npending)
device_t parent
Definition: device_if.m:171
const char * name
Definition: kern_fail.c:97
static struct mtx firmware_mtx
MODULE_VERSION(firmware, 1)
static struct task firmware_unload_task
#define FW_INUSE(p)
Definition: subr_firmware.c:58
int * type
Definition: cpufreq_if.m:98
static moduledata_t firmware_mod
void wakeup_one(void *ident)
Definition: kern_synch.c:398
static struct priv_fw * lookup(const char *name, struct priv_fw **empty_slot)
int priv_check(struct thread *td, int priv)
Definition: kern_priv.c:170
#define FW_UNLOAD
Definition: subr_firmware.c:91
void firmware_put(const struct firmware *p, int flags)
static int firmware_modevent(module_t mod, int type, void *unused)
static void loadimage(void *arg, int npending)
static int dummy
static struct priv_fw firmware_table[FIRMWARE_MAX]
void taskqueue_free(struct taskqueue *queue)
struct priv_fw * parent
Definition: subr_firmware.c:88
DECLARE_MODULE(firmware, firmware_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST)
#define PRIV_FW(x)
struct taskqueue * taskqueue_create(const char *name, int mflags, taskqueue_enqueue_fn enqueue, void *context)
const struct firmware * firmware_get(const char *imagename)
struct firmware fw
int refcnt
Definition: subr_firmware.c:82
void free(void *addr, struct malloc_type *mtp)
Definition: kern_malloc.c:554
int printf(const char *fmt,...)
Definition: subr_prf.c:367
int linker_reference_module(const char *modname, struct mod_depend *verinfo, linker_file_t *result)
Definition: kern_linker.c:479
int taskqueue_enqueue(struct taskqueue *queue, struct task *task)
linker_file_t * result
Definition: linker_if.m:136
int linker_release_module(const char *modname, struct mod_depend *verinfo, linker_file_t lf)
Definition: kern_linker.c:499
static struct taskqueue * firmware_tq
__FBSDID("$BSDSUniX$")
linker_file_t file
Definition: subr_firmware.c:99
int taskqueue_start_threads(struct taskqueue **tqp, int count, int pri, const char *name,...)
int securelevel_gt(struct ucred *cr, int level)
Definition: kern_prot.c:1310
void taskqueue_thread_enqueue(void *context)
static void unloadentry(void *unused1, int unused2)
const struct firmware * firmware_register(const char *imagename, const void *data, size_t datasize, unsigned int version, const struct firmware *parent)
static void firmware_mountroot(void *arg)
EVENTHANDLER_DEFINE(mountroot, firmware_mountroot, NULL, 0)
struct vnode * rootvnode
Definition: vfs_mountroot.c:96