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