xref: /freebsd/sys/kern/subr_firmware.c (revision 3c4ba5f55438f7afd4f4b0b56f88f2bb505fd6a6)
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  * which embeds the public firmware structure.
58  */
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 	LIST_ENTRY(priv_fw) link;	/* table linkage */
84 
85 	/*
86 	 * parent entry, see above. Set on firmware_register(),
87 	 * cleared on firmware_unregister().
88 	 */
89 	struct priv_fw	*parent;
90 
91 	int 		flags;	/* record FIRMWARE_UNLOAD requests */
92 #define FW_UNLOAD	0x100
93 
94 	/*
95 	 * 'file' is private info managed by the autoload/unload code.
96 	 * Set at the end of firmware_get(), cleared only in the
97 	 * firmware_unload_task, so the latter can depend on its value even
98 	 * while the lock is not held.
99 	 */
100 	linker_file_t   file;	/* module file, if autoloaded */
101 
102 	/*
103 	 * 'fw' is the externally visible image information.
104 	 * We do not make it the first field in priv_fw, to avoid the
105 	 * temptation of casting pointers to each other.
106 	 * Use PRIV_FW(fw) to get a pointer to the cointainer of fw.
107 	 * Beware, PRIV_FW does not work for a NULL pointer.
108 	 */
109 	struct firmware	fw;	/* externally visible information */
110 };
111 
112 /*
113  * PRIV_FW returns the pointer to the container of struct firmware *x.
114  * Cast to intptr_t to override the 'const' attribute of x
115  */
116 #define PRIV_FW(x)	((struct priv_fw *)		\
117 	((intptr_t)(x) - offsetof(struct priv_fw, fw)) )
118 
119 /*
120  * Global firmware image registry.
121  */
122 static LIST_HEAD(, priv_fw) firmware_table;
123 
124 /*
125  * Firmware module operations are handled in a separate task as they
126  * might sleep and they require directory context to do i/o.
127  */
128 static struct taskqueue *firmware_tq;
129 static struct task firmware_unload_task;
130 
131 /*
132  * This mutex protects accesses to the firmware table.
133  */
134 static struct mtx firmware_mtx;
135 MTX_SYSINIT(firmware, &firmware_mtx, "firmware table", MTX_DEF);
136 
137 static MALLOC_DEFINE(M_FIRMWARE, "firmware", "device firmware images");
138 
139 /*
140  * Helper function to lookup a name.
141  * As a side effect, it sets the pointer to a free slot, if any.
142  * This way we can concentrate most of the registry scanning in
143  * this function, which makes it easier to replace the registry
144  * with some other data structure.
145  */
146 static struct priv_fw *
147 lookup(const char *name)
148 {
149 	struct priv_fw *fp;
150 
151 	mtx_assert(&firmware_mtx, MA_OWNED);
152 
153 	LIST_FOREACH(fp, &firmware_table, link) {
154 		if (fp->fw.name != NULL && strcasecmp(name, fp->fw.name) == 0)
155 			break;
156 	}
157 	return (fp);
158 }
159 
160 /*
161  * Register a firmware image with the specified name.  The
162  * image name must not already be registered.  If this is a
163  * subimage then parent refers to a previously registered
164  * image that this should be associated with.
165  */
166 const struct firmware *
167 firmware_register(const char *imagename, const void *data, size_t datasize,
168     unsigned int version, const struct firmware *parent)
169 {
170 	struct priv_fw *frp;
171 	char *name;
172 
173 	mtx_lock(&firmware_mtx);
174 	frp = lookup(imagename);
175 	if (frp != NULL) {
176 		mtx_unlock(&firmware_mtx);
177 		printf("%s: image %s already registered!\n",
178 		    __func__, imagename);
179 		return (NULL);
180 	}
181 	mtx_unlock(&firmware_mtx);
182 
183 	frp = malloc(sizeof(*frp), M_FIRMWARE, M_WAITOK | M_ZERO);
184 	name = strdup(imagename, M_FIRMWARE);
185 
186 	mtx_lock(&firmware_mtx);
187 	if (lookup(imagename) != NULL) {
188 		/* We lost a race. */
189 		mtx_unlock(&firmware_mtx);
190 		free(name, M_FIRMWARE);
191 		free(frp, M_FIRMWARE);
192 		return (NULL);
193 	}
194 	frp->fw.name = name;
195 	frp->fw.data = data;
196 	frp->fw.datasize = datasize;
197 	frp->fw.version = version;
198 	if (parent != NULL)
199 		frp->parent = PRIV_FW(parent);
200 	LIST_INSERT_HEAD(&firmware_table, frp, link);
201 	mtx_unlock(&firmware_mtx);
202 	if (bootverbose)
203 		printf("firmware: '%s' version %u: %zu bytes loaded at %p\n",
204 		    imagename, version, datasize, data);
205 	return (&frp->fw);
206 }
207 
208 /*
209  * Unregister/remove a firmware image.  If there are outstanding
210  * references an error is returned and the image is not removed
211  * from the registry.
212  */
213 int
214 firmware_unregister(const char *imagename)
215 {
216 	struct priv_fw *fp;
217 	int err;
218 
219 	mtx_lock(&firmware_mtx);
220 	fp = lookup(imagename);
221 	if (fp == NULL) {
222 		/*
223 		 * It is ok for the lookup to fail; this can happen
224 		 * when a module is unloaded on last reference and the
225 		 * module unload handler unregister's each of its
226 		 * firmware images.
227 		 */
228 		err = 0;
229 	} else if (fp->refcnt != 0) {	/* cannot unregister */
230 		err = EBUSY;
231 	} else {
232 		LIST_REMOVE(fp, link);
233 		free(__DECONST(char *, fp->fw.name), M_FIRMWARE);
234 		free(fp, M_FIRMWARE);
235 		err = 0;
236 	}
237 	mtx_unlock(&firmware_mtx);
238 	return (err);
239 }
240 
241 struct fw_loadimage {
242 	const char	*imagename;
243 	uint32_t	flags;
244 };
245 
246 static void
247 loadimage(void *arg, int npending __unused)
248 {
249 	struct fw_loadimage *fwli = arg;
250 	struct priv_fw *fp;
251 	linker_file_t result;
252 	int error;
253 
254 	error = linker_reference_module(fwli->imagename, NULL, &result);
255 	if (error != 0) {
256 		if (bootverbose || (fwli->flags & FIRMWARE_GET_NOWARN) == 0)
257 			printf("%s: could not load firmware image, error %d\n",
258 			    fwli->imagename, error);
259 		mtx_lock(&firmware_mtx);
260 		goto done;
261 	}
262 
263 	mtx_lock(&firmware_mtx);
264 	fp = lookup(fwli->imagename);
265 	if (fp == NULL || fp->file != NULL) {
266 		mtx_unlock(&firmware_mtx);
267 		if (fp == NULL)
268 			printf("%s: firmware image loaded, "
269 			    "but did not register\n", fwli->imagename);
270 		(void) linker_release_module(fwli->imagename, NULL, NULL);
271 		mtx_lock(&firmware_mtx);
272 		goto done;
273 	}
274 	fp->file = result;	/* record the module identity */
275 done:
276 	wakeup_one(arg);
277 	mtx_unlock(&firmware_mtx);
278 }
279 
280 /*
281  * Lookup and potentially load the specified firmware image.
282  * If the firmware is not found in the registry, try to load a kernel
283  * module named as the image name.
284  * If the firmware is located, a reference is returned. The caller must
285  * release this reference for the image to be eligible for removal/unload.
286  */
287 const struct firmware *
288 firmware_get_flags(const char *imagename, uint32_t flags)
289 {
290 	struct task fwload_task;
291 	struct thread *td;
292 	struct priv_fw *fp;
293 
294 	mtx_lock(&firmware_mtx);
295 	fp = lookup(imagename);
296 	if (fp != NULL)
297 		goto found;
298 	/*
299 	 * Image not present, try to load the module holding it.
300 	 */
301 	td = curthread;
302 	if (priv_check(td, PRIV_FIRMWARE_LOAD) != 0 ||
303 	    securelevel_gt(td->td_ucred, 0) != 0) {
304 		mtx_unlock(&firmware_mtx);
305 		printf("%s: insufficient privileges to "
306 		    "load firmware image %s\n", __func__, imagename);
307 		return NULL;
308 	}
309 	/*
310 	 * Defer load to a thread with known context.  linker_reference_module
311 	 * may do filesystem i/o which requires root & current dirs, etc.
312 	 * Also we must not hold any mtx's over this call which is problematic.
313 	 */
314 	if (!cold) {
315 		struct fw_loadimage fwli;
316 
317 		fwli.imagename = imagename;
318 		fwli.flags = flags;
319 		TASK_INIT(&fwload_task, 0, loadimage, (void *)&fwli);
320 		taskqueue_enqueue(firmware_tq, &fwload_task);
321 		PHOLD(curproc);
322 		msleep((void *)&fwli, &firmware_mtx, 0, "fwload", 0);
323 		PRELE(curproc);
324 	}
325 	/*
326 	 * After attempting to load the module, see if the image is registered.
327 	 */
328 	fp = lookup(imagename);
329 	if (fp == NULL) {
330 		mtx_unlock(&firmware_mtx);
331 		return NULL;
332 	}
333 found:				/* common exit point on success */
334 	if (fp->refcnt == 0 && fp->parent != NULL)
335 		fp->parent->refcnt++;
336 	fp->refcnt++;
337 	mtx_unlock(&firmware_mtx);
338 	return &fp->fw;
339 }
340 
341 const struct firmware *
342 firmware_get(const char *imagename)
343 {
344 
345 	return (firmware_get_flags(imagename, 0));
346 }
347 
348 /*
349  * Release a reference to a firmware image returned by firmware_get.
350  * The caller may specify, with the FIRMWARE_UNLOAD flag, its desire
351  * to release the resource, but the flag is only advisory.
352  *
353  * If this is the last reference to the firmware image, and this is an
354  * autoloaded module, wake up the firmware_unload_task to figure out
355  * what to do with the associated module.
356  */
357 void
358 firmware_put(const struct firmware *p, int flags)
359 {
360 	struct priv_fw *fp = PRIV_FW(p);
361 
362 	mtx_lock(&firmware_mtx);
363 	fp->refcnt--;
364 	if (fp->refcnt == 0) {
365 		if (fp->parent != NULL)
366 			fp->parent->refcnt--;
367 		if (flags & FIRMWARE_UNLOAD)
368 			fp->flags |= FW_UNLOAD;
369 		if (fp->file)
370 			taskqueue_enqueue(firmware_tq, &firmware_unload_task);
371 	}
372 	mtx_unlock(&firmware_mtx);
373 }
374 
375 /*
376  * Setup directory state for the firmware_tq thread so we can do i/o.
377  */
378 static void
379 set_rootvnode(void *arg, int npending)
380 {
381 
382 	pwd_ensure_dirs();
383 	free(arg, M_TEMP);
384 }
385 
386 /*
387  * Event handler called on mounting of /; bounce a task
388  * into the task queue thread to setup it's directories.
389  */
390 static void
391 firmware_mountroot(void *arg)
392 {
393 	struct task *setroot_task;
394 
395 	setroot_task = malloc(sizeof(struct task), M_TEMP, M_NOWAIT);
396 	if (setroot_task != NULL) {
397 		TASK_INIT(setroot_task, 0, set_rootvnode, setroot_task);
398 		taskqueue_enqueue(firmware_tq, setroot_task);
399 	} else
400 		printf("%s: no memory for task!\n", __func__);
401 }
402 EVENTHANDLER_DEFINE(mountroot, firmware_mountroot, NULL, 0);
403 
404 /*
405  * The body of the task in charge of unloading autoloaded modules
406  * that are not needed anymore.
407  * Images can be cross-linked so we may need to make multiple passes,
408  * but the time we spend in the loop is bounded because we clear entries
409  * as we touch them.
410  */
411 static void
412 unloadentry(void *unused1, int unused2)
413 {
414 	struct priv_fw *fp;
415 
416 	mtx_lock(&firmware_mtx);
417 restart:
418 	LIST_FOREACH(fp, &firmware_table, link) {
419 		if (fp->file == NULL || fp->refcnt != 0 ||
420 		    (fp->flags & FW_UNLOAD) == 0)
421 			continue;
422 
423 		/*
424 		 * Found an entry. Now:
425 		 * 1. make sure we scan the table again
426 		 * 2. clear FW_UNLOAD so we don't try this entry again.
427 		 * 3. release the lock while trying to unload the module.
428 		 */
429 		fp->flags &= ~FW_UNLOAD;	/* do not try again */
430 
431 		/*
432 		 * We rely on the module to call firmware_unregister()
433 		 * on unload to actually free the entry.
434 		 */
435 		mtx_unlock(&firmware_mtx);
436 		(void)linker_release_module(NULL, NULL, fp->file);
437 		mtx_lock(&firmware_mtx);
438 
439 		/*
440 		 * When we dropped the lock, another thread could have
441 		 * removed an element, so we must restart the scan.
442 		 */
443 		goto restart;
444 	}
445 	mtx_unlock(&firmware_mtx);
446 }
447 
448 /*
449  * Module glue.
450  */
451 static int
452 firmware_modevent(module_t mod, int type, void *unused)
453 {
454 	struct priv_fw *fp;
455 	int err;
456 
457 	err = 0;
458 	switch (type) {
459 	case MOD_LOAD:
460 		TASK_INIT(&firmware_unload_task, 0, unloadentry, NULL);
461 		firmware_tq = taskqueue_create("taskqueue_firmware", M_WAITOK,
462 		    taskqueue_thread_enqueue, &firmware_tq);
463 		/* NB: use our own loop routine that sets up context */
464 		(void) taskqueue_start_threads(&firmware_tq, 1, PWAIT,
465 		    "firmware taskq");
466 		if (rootvnode != NULL) {
467 			/*
468 			 * Root is already mounted so we won't get an event;
469 			 * simulate one here.
470 			 */
471 			firmware_mountroot(NULL);
472 		}
473 		break;
474 
475 	case MOD_UNLOAD:
476 		/* request all autoloaded modules to be released */
477 		mtx_lock(&firmware_mtx);
478 		LIST_FOREACH(fp, &firmware_table, link)
479 			fp->flags |= FW_UNLOAD;
480 		mtx_unlock(&firmware_mtx);
481 		taskqueue_enqueue(firmware_tq, &firmware_unload_task);
482 		taskqueue_drain(firmware_tq, &firmware_unload_task);
483 
484 		LIST_FOREACH(fp, &firmware_table, link) {
485 			if (fp->fw.name != NULL) {
486 				printf("%s: image %s still active, %d refs\n",
487 				    __func__, fp->fw.name, fp->refcnt);
488 				err = EINVAL;
489 			}
490 		}
491 		if (err == 0)
492 			taskqueue_free(firmware_tq);
493 		break;
494 
495 	default:
496 		err = EOPNOTSUPP;
497 		break;
498 	}
499 	return (err);
500 }
501 
502 static moduledata_t firmware_mod = {
503 	"firmware",
504 	firmware_modevent,
505 	NULL
506 };
507 DECLARE_MODULE(firmware, firmware_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
508 MODULE_VERSION(firmware, 1);
509