xref: /freebsd/sys/kern/subr_firmware.c (revision c697fb7f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2005-2008, Sam Leffler <sam@errno.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
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.
16  *
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.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
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>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/errno.h>
41 #include <sys/linker.h>
42 #include <sys/firmware.h>
43 #include <sys/priv.h>
44 #include <sys/proc.h>
45 #include <sys/module.h>
46 #include <sys/eventhandler.h>
47 
48 #include <sys/filedesc.h>
49 #include <sys/vnode.h>
50 
51 /*
52  * Loadable firmware support. See sys/sys/firmware.h and firmware(9)
53  * form more details on the subsystem.
54  *
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:
58  */
59 
60 #define FW_INUSE(p)	((p)->file != NULL || (p)->fw.name != NULL)
61 
62 /*
63  * fw.name != NULL when an image is registered; file != NULL for
64  * autoloaded images whose handling has not been completed.
65  *
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
71  *
72  * In order for the above to work, the 'file' field must remain
73  * unchanged in firmware_unregister().
74  *
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.
81  */
82 
83 struct priv_fw {
84 	int		refcnt;		/* reference count */
85 
86 	/*
87 	 * parent entry, see above. Set on firmware_register(),
88 	 * cleared on firmware_unregister().
89 	 */
90 	struct priv_fw	*parent;
91 
92 	int 		flags;	/* record FIRMWARE_UNLOAD requests */
93 #define FW_UNLOAD	0x100
94 
95 	/*
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.
100 	 */
101 	linker_file_t   file;	/* module file, if autoloaded */
102 
103 	/*
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.
109 	 */
110 	struct firmware	fw;	/* externally visible information */
111 };
112 
113 /*
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
116  */
117 #define PRIV_FW(x)	((struct priv_fw *)		\
118 	((intptr_t)(x) - offsetof(struct priv_fw, fw)) )
119 
120 /*
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.
125  */
126 #define	FIRMWARE_MAX	50
127 static struct priv_fw firmware_table[FIRMWARE_MAX];
128 
129 /*
130  * Firmware module operations are handled in a separate task as they
131  * might sleep and they require directory context to do i/o.
132  */
133 static struct taskqueue *firmware_tq;
134 static struct task firmware_unload_task;
135 
136 /*
137  * This mutex protects accesses to the firmware table.
138  */
139 static struct mtx firmware_mtx;
140 MTX_SYSINIT(firmware, &firmware_mtx, "firmware table", MTX_DEF);
141 
142 /*
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.
148  */
149 static struct priv_fw *
150 lookup(const char *name, struct priv_fw **empty_slot)
151 {
152 	struct priv_fw *fp = NULL;
153 	struct priv_fw *dummy;
154 	int i;
155 
156 	if (empty_slot == NULL)
157 		empty_slot = &dummy;
158 	*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)
162 			break;
163 		else if (!FW_INUSE(fp))
164 			*empty_slot = fp;
165 	}
166 	return (i < FIRMWARE_MAX ) ? fp : NULL;
167 }
168 
169 /*
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.
174  */
175 const struct firmware *
176 firmware_register(const char *imagename, const void *data, size_t datasize,
177     unsigned int version, const struct firmware *parent)
178 {
179 	struct priv_fw *match, *frp;
180 	char *str;
181 
182 	str = strdup(imagename, M_TEMP);
183 
184 	mtx_lock(&firmware_mtx);
185 	/*
186 	 * Do a lookup to make sure the name is unique or find a free slot.
187 	 */
188 	match = lookup(imagename, &frp);
189 	if (match != NULL) {
190 		mtx_unlock(&firmware_mtx);
191 		printf("%s: image %s already registered!\n",
192 			__func__, imagename);
193 		free(str, M_TEMP);
194 		return NULL;
195 	}
196 	if (frp == NULL) {
197 		mtx_unlock(&firmware_mtx);
198 		printf("%s: cannot register image %s, firmware table full!\n",
199 		    __func__, imagename);
200 		free(str, M_TEMP);
201 		return NULL;
202 	}
203 	bzero(frp, sizeof(*frp));	/* start from a clean record */
204 	frp->fw.name = str;
205 	frp->fw.data = data;
206 	frp->fw.datasize = datasize;
207 	frp->fw.version = version;
208 	if (parent != NULL)
209 		frp->parent = PRIV_FW(parent);
210 	mtx_unlock(&firmware_mtx);
211 	if (bootverbose)
212 		printf("firmware: '%s' version %u: %zu bytes loaded at %p\n",
213 		    imagename, version, datasize, data);
214 	return &frp->fw;
215 }
216 
217 /*
218  * Unregister/remove a firmware image.  If there are outstanding
219  * references an error is returned and the image is not removed
220  * from the registry.
221  */
222 int
223 firmware_unregister(const char *imagename)
224 {
225 	struct priv_fw *fp;
226 	int err;
227 
228 	mtx_lock(&firmware_mtx);
229 	fp = lookup(imagename, NULL);
230 	if (fp == NULL) {
231 		/*
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
235 		 * firmware images.
236 		 */
237 		err = 0;
238 	} else if (fp->refcnt != 0) {	/* cannot unregister */
239 		err = EBUSY;
240 	} else {
241 		linker_file_t x = fp->file;	/* save value */
242 
243 		/*
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.
247 		 */
248 		free((void *) (uintptr_t) fp->fw.name, M_TEMP);
249 		bzero(fp, sizeof(struct priv_fw));
250 		fp->file = x;
251 		err = 0;
252 	}
253 	mtx_unlock(&firmware_mtx);
254 	return err;
255 }
256 
257 static void
258 loadimage(void *arg, int npending)
259 {
260 	char *imagename = arg;
261 	struct priv_fw *fp;
262 	linker_file_t result;
263 	int error;
264 
265 	/* synchronize with the thread that dispatched us */
266 	mtx_lock(&firmware_mtx);
267 	mtx_unlock(&firmware_mtx);
268 
269 	error = linker_reference_module(imagename, NULL, &result);
270 	if (error != 0) {
271 		printf("%s: could not load firmware image, error %d\n",
272 		    imagename, error);
273 		goto done;
274 	}
275 
276 	mtx_lock(&firmware_mtx);
277 	fp = lookup(imagename, NULL);
278 	if (fp == NULL || fp->file != NULL) {
279 		mtx_unlock(&firmware_mtx);
280 		if (fp == NULL)
281 			printf("%s: firmware image loaded, "
282 			    "but did not register\n", imagename);
283 		(void) linker_release_module(imagename, NULL, NULL);
284 		goto done;
285 	}
286 	fp->file = result;	/* record the module identity */
287 	mtx_unlock(&firmware_mtx);
288 done:
289 	wakeup_one(imagename);		/* we're done */
290 }
291 
292 /*
293  * Lookup and potentially load the specified firmware image.
294  * If the firmware is not found in the registry, try to load a kernel
295  * module named as the image name.
296  * If the firmware is located, a reference is returned. The caller must
297  * release this reference for the image to be eligible for removal/unload.
298  */
299 const struct firmware *
300 firmware_get(const char *imagename)
301 {
302 	struct task fwload_task;
303 	struct thread *td;
304 	struct priv_fw *fp;
305 
306 	mtx_lock(&firmware_mtx);
307 	fp = lookup(imagename, NULL);
308 	if (fp != NULL)
309 		goto found;
310 	/*
311 	 * Image not present, try to load the module holding it.
312 	 */
313 	td = curthread;
314 	if (priv_check(td, PRIV_FIRMWARE_LOAD) != 0 ||
315 	    securelevel_gt(td->td_ucred, 0) != 0) {
316 		mtx_unlock(&firmware_mtx);
317 		printf("%s: insufficient privileges to "
318 		    "load firmware image %s\n", __func__, imagename);
319 		return NULL;
320 	}
321 	/*
322 	 * Defer load to a thread with known context.  linker_reference_module
323 	 * may do filesystem i/o which requires root & current dirs, etc.
324 	 * Also we must not hold any mtx's over this call which is problematic.
325 	 */
326 	if (!cold) {
327 		TASK_INIT(&fwload_task, 0, loadimage, __DECONST(void *,
328 		    imagename));
329 		taskqueue_enqueue(firmware_tq, &fwload_task);
330 		msleep(__DECONST(void *, imagename), &firmware_mtx, 0,
331 		    "fwload", 0);
332 	}
333 	/*
334 	 * After attempting to load the module, see if the image is registered.
335 	 */
336 	fp = lookup(imagename, NULL);
337 	if (fp == NULL) {
338 		mtx_unlock(&firmware_mtx);
339 		return NULL;
340 	}
341 found:				/* common exit point on success */
342 	if (fp->refcnt == 0 && fp->parent != NULL)
343 		fp->parent->refcnt++;
344 	fp->refcnt++;
345 	mtx_unlock(&firmware_mtx);
346 	return &fp->fw;
347 }
348 
349 /*
350  * Release a reference to a firmware image returned by firmware_get.
351  * The caller may specify, with the FIRMWARE_UNLOAD flag, its desire
352  * to release the resource, but the flag is only advisory.
353  *
354  * If this is the last reference to the firmware image, and this is an
355  * autoloaded module, wake up the firmware_unload_task to figure out
356  * what to do with the associated module.
357  */
358 void
359 firmware_put(const struct firmware *p, int flags)
360 {
361 	struct priv_fw *fp = PRIV_FW(p);
362 
363 	mtx_lock(&firmware_mtx);
364 	fp->refcnt--;
365 	if (fp->refcnt == 0) {
366 		if (fp->parent != NULL)
367 			fp->parent->refcnt--;
368 		if (flags & FIRMWARE_UNLOAD)
369 			fp->flags |= FW_UNLOAD;
370 		if (fp->file)
371 			taskqueue_enqueue(firmware_tq, &firmware_unload_task);
372 	}
373 	mtx_unlock(&firmware_mtx);
374 }
375 
376 /*
377  * Setup directory state for the firmware_tq thread so we can do i/o.
378  */
379 static void
380 set_rootvnode(void *arg, int npending)
381 {
382 
383 	pwd_ensure_dirs();
384 
385 	free(arg, M_TEMP);
386 }
387 
388 /*
389  * Event handler called on mounting of /; bounce a task
390  * into the task queue thread to setup it's directories.
391  */
392 static void
393 firmware_mountroot(void *arg)
394 {
395 	struct task *setroot_task;
396 
397 	setroot_task = malloc(sizeof(struct task), M_TEMP, M_NOWAIT);
398 	if (setroot_task != NULL) {
399 		TASK_INIT(setroot_task, 0, set_rootvnode, setroot_task);
400 		taskqueue_enqueue(firmware_tq, setroot_task);
401 	} else
402 		printf("%s: no memory for task!\n", __func__);
403 }
404 EVENTHANDLER_DEFINE(mountroot, firmware_mountroot, NULL, 0);
405 
406 /*
407  * The body of the task in charge of unloading autoloaded modules
408  * that are not needed anymore.
409  * Images can be cross-linked so we may need to make multiple passes,
410  * but the time we spend in the loop is bounded because we clear entries
411  * as we touch them.
412  */
413 static void
414 unloadentry(void *unused1, int unused2)
415 {
416 	int limit = FIRMWARE_MAX;
417 	int i;	/* current cycle */
418 
419 	mtx_lock(&firmware_mtx);
420 	/*
421 	 * Scan the table. limit is set to make sure we make another
422 	 * full sweep after matching an entry that requires unloading.
423 	 */
424 	for (i = 0; i < limit; i++) {
425 		struct priv_fw *fp;
426 		int err;
427 
428 		fp = &firmware_table[i % FIRMWARE_MAX];
429 		if (fp->fw.name == NULL || fp->file == NULL ||
430 		    fp->refcnt != 0 || (fp->flags & FW_UNLOAD) == 0)
431 			continue;
432 
433 		/*
434 		 * Found an entry. Now:
435 		 * 1. bump up limit to make sure we make another full round;
436 		 * 2. clear FW_UNLOAD so we don't try this entry again.
437 		 * 3. release the lock while trying to unload the module.
438 		 * 'file' remains set so that the entry cannot be reused
439 		 * in the meantime (it also means that fp->file will
440 		 * not change while we release the lock).
441 		 */
442 		limit = i + FIRMWARE_MAX;	/* make another full round */
443 		fp->flags &= ~FW_UNLOAD;	/* do not try again */
444 
445 		mtx_unlock(&firmware_mtx);
446 		err = linker_release_module(NULL, NULL, fp->file);
447 		mtx_lock(&firmware_mtx);
448 
449 		/*
450 		 * We rely on the module to call firmware_unregister()
451 		 * on unload to actually release the entry.
452 		 * If err = 0 we can drop our reference as the system
453 		 * accepted it. Otherwise unloading failed (e.g. the
454 		 * module itself gave an error) so our reference is
455 		 * still valid.
456 		 */
457 		if (err == 0)
458 			fp->file = NULL;
459 	}
460 	mtx_unlock(&firmware_mtx);
461 }
462 
463 /*
464  * Module glue.
465  */
466 static int
467 firmware_modevent(module_t mod, int type, void *unused)
468 {
469 	struct priv_fw *fp;
470 	int i, err;
471 
472 	switch (type) {
473 	case MOD_LOAD:
474 		TASK_INIT(&firmware_unload_task, 0, unloadentry, NULL);
475 		firmware_tq = taskqueue_create("taskqueue_firmware", M_WAITOK,
476 		    taskqueue_thread_enqueue, &firmware_tq);
477 		/* NB: use our own loop routine that sets up context */
478 		(void) taskqueue_start_threads(&firmware_tq, 1, PWAIT,
479 		    "firmware taskq");
480 		if (rootvnode != NULL) {
481 			/*
482 			 * Root is already mounted so we won't get an event;
483 			 * simulate one here.
484 			 */
485 			firmware_mountroot(NULL);
486 		}
487 		return 0;
488 
489 	case MOD_UNLOAD:
490 		/* request all autoloaded modules to be released */
491 		mtx_lock(&firmware_mtx);
492 		for (i = 0; i < FIRMWARE_MAX; i++) {
493 			fp = &firmware_table[i];
494 			fp->flags |= FW_UNLOAD;
495 		}
496 		mtx_unlock(&firmware_mtx);
497 		taskqueue_enqueue(firmware_tq, &firmware_unload_task);
498 		taskqueue_drain(firmware_tq, &firmware_unload_task);
499 		err = 0;
500 		for (i = 0; i < FIRMWARE_MAX; i++) {
501 			fp = &firmware_table[i];
502 			if (fp->fw.name != NULL) {
503 				printf("%s: image %p ref %d still active slot %d\n",
504 					__func__, fp->fw.name,
505 					fp->refcnt,  i);
506 				err = EINVAL;
507 			}
508 		}
509 		if (err == 0)
510 			taskqueue_free(firmware_tq);
511 		return err;
512 	}
513 	return EINVAL;
514 }
515 
516 static moduledata_t firmware_mod = {
517 	"firmware",
518 	firmware_modevent,
519 	NULL
520 };
521 DECLARE_MODULE(firmware, firmware_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
522 MODULE_VERSION(firmware, 1);
523