xref: /freebsd/sys/kern/subr_firmware.c (revision 3a3afbec)
16aec1278SMax Laier /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
38a36da99SPedro F. Giffuni  *
46c6eaea6SSam Leffler  * Copyright (c) 2005-2008, Sam Leffler <sam@errno.com>
56aec1278SMax Laier  * All rights reserved.
66aec1278SMax Laier  *
76aec1278SMax Laier  * Redistribution and use in source and binary forms, with or without
86aec1278SMax Laier  * modification, are permitted provided that the following conditions
96aec1278SMax Laier  * are met:
106aec1278SMax Laier  * 1. Redistributions of source code must retain the above copyright
116aec1278SMax Laier  *    notice unmodified, this list of conditions, and the following
126aec1278SMax Laier  *    disclaimer.
136aec1278SMax Laier  * 2. Redistributions in binary form must reproduce the above copyright
146aec1278SMax Laier  *    notice, this list of conditions and the following disclaimer in the
156aec1278SMax Laier  *    documentation and/or other materials provided with the distribution.
166aec1278SMax Laier  *
176aec1278SMax Laier  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
186aec1278SMax Laier  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
196aec1278SMax Laier  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
206aec1278SMax Laier  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
216aec1278SMax Laier  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
226aec1278SMax Laier  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236aec1278SMax Laier  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246aec1278SMax Laier  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256aec1278SMax Laier  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
266aec1278SMax Laier  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276aec1278SMax Laier  */
286aec1278SMax Laier 
296aec1278SMax Laier #include <sys/param.h>
306aec1278SMax Laier #include <sys/errno.h>
314b62b42aSWarner Losh #include <sys/eventhandler.h>
32c7b1e980SWarner Losh #include <sys/fcntl.h>
336aec1278SMax Laier #include <sys/firmware.h>
344b62b42aSWarner Losh #include <sys/kernel.h>
354b62b42aSWarner Losh #include <sys/linker.h>
364b62b42aSWarner Losh #include <sys/lock.h>
374b62b42aSWarner Losh #include <sys/malloc.h>
384b62b42aSWarner Losh #include <sys/module.h>
394b62b42aSWarner Losh #include <sys/mutex.h>
40c7b1e980SWarner Losh #include <sys/namei.h>
41acd3428bSRobert Watson #include <sys/priv.h>
426aec1278SMax Laier #include <sys/proc.h>
434b62b42aSWarner Losh #include <sys/queue.h>
44c7b1e980SWarner Losh #include <sys/sbuf.h>
45c7b1e980SWarner Losh #include <sys/sysctl.h>
464b62b42aSWarner Losh #include <sys/systm.h>
474b62b42aSWarner Losh #include <sys/taskqueue.h>
486c6eaea6SSam Leffler 
496c6eaea6SSam Leffler #include <sys/filedesc.h>
506c6eaea6SSam Leffler #include <sys/vnode.h>
516aec1278SMax Laier 
5233d54970SLuigi Rizzo /*
5333d54970SLuigi Rizzo  * Loadable firmware support. See sys/sys/firmware.h and firmware(9)
5433d54970SLuigi Rizzo  * form more details on the subsystem.
5533d54970SLuigi Rizzo  *
5633d54970SLuigi Rizzo  * 'struct firmware' is the user-visible part of the firmware table.
574f8ad92fSMark Johnston  * Additional internal information is stored in a 'struct priv_fw',
584f8ad92fSMark Johnston  * which embeds the public firmware structure.
5933d54970SLuigi Rizzo  */
6033d54970SLuigi Rizzo 
6133d54970SLuigi Rizzo /*
6233d54970SLuigi Rizzo  * fw.name != NULL when an image is registered; file != NULL for
6333d54970SLuigi Rizzo  * autoloaded images whose handling has not been completed.
6433d54970SLuigi Rizzo  *
6533d54970SLuigi Rizzo  * The state of a slot evolves as follows:
6633d54970SLuigi Rizzo  *	firmware_register	-->  fw.name = image_name
6733d54970SLuigi Rizzo  *	(autoloaded image)	-->  file = module reference
6833d54970SLuigi Rizzo  *	firmware_unregister	-->  fw.name = NULL
6933d54970SLuigi Rizzo  *	(unloadentry complete)	-->  file = NULL
7033d54970SLuigi Rizzo  *
7133d54970SLuigi Rizzo  * In order for the above to work, the 'file' field must remain
7233d54970SLuigi Rizzo  * unchanged in firmware_unregister().
7333d54970SLuigi Rizzo  *
7433d54970SLuigi Rizzo  * Images residing in the same module are linked to each other
7533d54970SLuigi Rizzo  * through the 'parent' argument of firmware_register().
7633d54970SLuigi Rizzo  * One image (typically, one with the same name as the module to let
7733d54970SLuigi Rizzo  * the autoloading mechanism work) is considered the parent image for
7833d54970SLuigi Rizzo  * all other images in the same module. Children affect the refcount
7933d54970SLuigi Rizzo  * on the parent image preventing improper unloading of the image itself.
8033d54970SLuigi Rizzo  */
8133d54970SLuigi Rizzo 
8233d54970SLuigi Rizzo struct priv_fw {
8333d54970SLuigi Rizzo 	int		refcnt;		/* reference count */
844f8ad92fSMark Johnston 	LIST_ENTRY(priv_fw) link;	/* table linkage */
8533d54970SLuigi Rizzo 
8633d54970SLuigi Rizzo 	/*
8733d54970SLuigi Rizzo 	 * parent entry, see above. Set on firmware_register(),
8833d54970SLuigi Rizzo 	 * cleared on firmware_unregister().
8933d54970SLuigi Rizzo 	 */
9033d54970SLuigi Rizzo 	struct priv_fw	*parent;
9133d54970SLuigi Rizzo 
92c7b1e980SWarner Losh 	int 		flags;
93c7b1e980SWarner Losh #define FW_BINARY	0x080	/* Firmware directly loaded, file == NULL */
94c7b1e980SWarner Losh #define FW_UNLOAD	0x100	/* record FIRMWARE_UNLOAD requests */
9533d54970SLuigi Rizzo 
9633d54970SLuigi Rizzo 	/*
9733d54970SLuigi Rizzo 	 * 'file' is private info managed by the autoload/unload code.
9833d54970SLuigi Rizzo 	 * Set at the end of firmware_get(), cleared only in the
996c6eaea6SSam Leffler 	 * firmware_unload_task, so the latter can depend on its value even
10033d54970SLuigi Rizzo 	 * while the lock is not held.
10133d54970SLuigi Rizzo 	 */
10233d54970SLuigi Rizzo 	linker_file_t   file;	/* module file, if autoloaded */
10333d54970SLuigi Rizzo 
10433d54970SLuigi Rizzo 	/*
10533d54970SLuigi Rizzo 	 * 'fw' is the externally visible image information.
10633d54970SLuigi Rizzo 	 * We do not make it the first field in priv_fw, to avoid the
10733d54970SLuigi Rizzo 	 * temptation of casting pointers to each other.
10833d54970SLuigi Rizzo 	 * Use PRIV_FW(fw) to get a pointer to the cointainer of fw.
10933d54970SLuigi Rizzo 	 * Beware, PRIV_FW does not work for a NULL pointer.
11033d54970SLuigi Rizzo 	 */
11133d54970SLuigi Rizzo 	struct firmware	fw;	/* externally visible information */
11233d54970SLuigi Rizzo };
11333d54970SLuigi Rizzo 
11433d54970SLuigi Rizzo /*
11533d54970SLuigi Rizzo  * PRIV_FW returns the pointer to the container of struct firmware *x.
11633d54970SLuigi Rizzo  * Cast to intptr_t to override the 'const' attribute of x
11733d54970SLuigi Rizzo  */
11833d54970SLuigi Rizzo #define PRIV_FW(x)	((struct priv_fw *)		\
11933d54970SLuigi Rizzo 	((intptr_t)(x) - offsetof(struct priv_fw, fw)) )
12033d54970SLuigi Rizzo 
12133d54970SLuigi Rizzo /*
1224f8ad92fSMark Johnston  * Global firmware image registry.
12333d54970SLuigi Rizzo  */
1244f8ad92fSMark Johnston static LIST_HEAD(, priv_fw) firmware_table;
12533d54970SLuigi Rizzo 
12633d54970SLuigi Rizzo /*
1276c6eaea6SSam Leffler  * Firmware module operations are handled in a separate task as they
128c7b1e980SWarner Losh  * might sleep and they require directory context to do i/o. We also
129c7b1e980SWarner Losh  * use this when loading binaries directly.
13033d54970SLuigi Rizzo  */
1316c6eaea6SSam Leffler static struct taskqueue *firmware_tq;
1326c6eaea6SSam Leffler static struct task firmware_unload_task;
13333d54970SLuigi Rizzo 
13433d54970SLuigi Rizzo /*
13533d54970SLuigi Rizzo  * This mutex protects accesses to the firmware table.
13633d54970SLuigi Rizzo  */
1376c6eaea6SSam Leffler static struct mtx firmware_mtx;
1386aec1278SMax Laier MTX_SYSINIT(firmware, &firmware_mtx, "firmware table", MTX_DEF);
1396aec1278SMax Laier 
1404f8ad92fSMark Johnston static MALLOC_DEFINE(M_FIRMWARE, "firmware", "device firmware images");
1414f8ad92fSMark Johnston 
142c7b1e980SWarner Losh static uint64_t firmware_max_size = 8u << 20; /* Default to 8MB cap */
143c7b1e980SWarner Losh SYSCTL_U64(_debug, OID_AUTO, firmware_max_size,
144c7b1e980SWarner Losh     CTLFLAG_RWTUN, &firmware_max_size, 0,
145c7b1e980SWarner Losh     "Max size permitted for a firmware file.");
146c7b1e980SWarner Losh 
1476aec1278SMax Laier /*
14833d54970SLuigi Rizzo  * Helper function to lookup a name.
14933d54970SLuigi Rizzo  * As a side effect, it sets the pointer to a free slot, if any.
15033d54970SLuigi Rizzo  * This way we can concentrate most of the registry scanning in
15133d54970SLuigi Rizzo  * this function, which makes it easier to replace the registry
15233d54970SLuigi Rizzo  * with some other data structure.
15333d54970SLuigi Rizzo  */
15433d54970SLuigi Rizzo static struct priv_fw *
lookup(const char * name)1554f8ad92fSMark Johnston lookup(const char *name)
15633d54970SLuigi Rizzo {
1574f8ad92fSMark Johnston 	struct priv_fw *fp;
15833d54970SLuigi Rizzo 
1594f8ad92fSMark Johnston 	mtx_assert(&firmware_mtx, MA_OWNED);
1604f8ad92fSMark Johnston 
1614f8ad92fSMark Johnston 	LIST_FOREACH(fp, &firmware_table, link) {
16233d54970SLuigi Rizzo 		if (fp->fw.name != NULL && strcasecmp(name, fp->fw.name) == 0)
16333d54970SLuigi Rizzo 			break;
164479905a1SWarner Losh 
165479905a1SWarner Losh 		/*
166479905a1SWarner Losh 		 * If the name looks like an absolute path, also try to match
167479905a1SWarner Losh 		 * the last part of the string to the requested firmware if it
168479905a1SWarner Losh 		 * matches the trailing components.  This allows us to load
169479905a1SWarner Losh 		 * /boot/firmware/abc/bca2233_fw.bin and match it against
170479905a1SWarner Losh 		 * requests for bca2233_fw.bin or abc/bca2233_fw.bin.
171479905a1SWarner Losh 		 */
172479905a1SWarner Losh 		if (*fp->fw.name == '/' && strlen(fp->fw.name) > strlen(name)) {
173479905a1SWarner Losh 			const char *p = fp->fw.name + strlen(fp->fw.name) - strlen(name);
174479905a1SWarner Losh 			if (p[-1] == '/' && strcasecmp(name, p) == 0)
175479905a1SWarner Losh 				break;
176479905a1SWarner Losh 		}
17733d54970SLuigi Rizzo 	}
1784f8ad92fSMark Johnston 	return (fp);
17933d54970SLuigi Rizzo }
18033d54970SLuigi Rizzo 
18133d54970SLuigi Rizzo /*
1826aec1278SMax Laier  * Register a firmware image with the specified name.  The
1836aec1278SMax Laier  * image name must not already be registered.  If this is a
1846aec1278SMax Laier  * subimage then parent refers to a previously registered
1856aec1278SMax Laier  * image that this should be associated with.
1866aec1278SMax Laier  */
18733d54970SLuigi Rizzo const struct firmware *
firmware_register(const char * imagename,const void * data,size_t datasize,unsigned int version,const struct firmware * parent)1886aec1278SMax Laier firmware_register(const char *imagename, const void *data, size_t datasize,
18933d54970SLuigi Rizzo     unsigned int version, const struct firmware *parent)
1906aec1278SMax Laier {
1914f8ad92fSMark Johnston 	struct priv_fw *frp;
1924f8ad92fSMark Johnston 	char *name;
1936aec1278SMax Laier 
1946aec1278SMax Laier 	mtx_lock(&firmware_mtx);
1954f8ad92fSMark Johnston 	frp = lookup(imagename);
1964f8ad92fSMark Johnston 	if (frp != NULL) {
1976aec1278SMax Laier 		mtx_unlock(&firmware_mtx);
1986aec1278SMax Laier 		printf("%s: image %s already registered!\n",
1996aec1278SMax Laier 		    __func__, imagename);
2004f8ad92fSMark Johnston 		return (NULL);
2016aec1278SMax Laier 	}
2026aec1278SMax Laier 	mtx_unlock(&firmware_mtx);
2034f8ad92fSMark Johnston 
2044f8ad92fSMark Johnston 	frp = malloc(sizeof(*frp), M_FIRMWARE, M_WAITOK | M_ZERO);
2054f8ad92fSMark Johnston 	name = strdup(imagename, M_FIRMWARE);
2064f8ad92fSMark Johnston 
2074f8ad92fSMark Johnston 	mtx_lock(&firmware_mtx);
2084f8ad92fSMark Johnston 	if (lookup(imagename) != NULL) {
2094f8ad92fSMark Johnston 		/* We lost a race. */
2104f8ad92fSMark Johnston 		mtx_unlock(&firmware_mtx);
2114f8ad92fSMark Johnston 		free(name, M_FIRMWARE);
2124f8ad92fSMark Johnston 		free(frp, M_FIRMWARE);
2134f8ad92fSMark Johnston 		return (NULL);
2146aec1278SMax Laier 	}
2154f8ad92fSMark Johnston 	frp->fw.name = name;
21633d54970SLuigi Rizzo 	frp->fw.data = data;
21733d54970SLuigi Rizzo 	frp->fw.datasize = datasize;
21833d54970SLuigi Rizzo 	frp->fw.version = version;
219059d10c7SNavdeep Parhar 	if (parent != NULL)
22033d54970SLuigi Rizzo 		frp->parent = PRIV_FW(parent);
2214f8ad92fSMark Johnston 	LIST_INSERT_HEAD(&firmware_table, frp, link);
2226aec1278SMax Laier 	mtx_unlock(&firmware_mtx);
22338d4db19SMax Laier 	if (bootverbose)
22438d4db19SMax Laier 		printf("firmware: '%s' version %u: %zu bytes loaded at %p\n",
22538d4db19SMax Laier 		    imagename, version, datasize, data);
2264f8ad92fSMark Johnston 	return (&frp->fw);
2276aec1278SMax Laier }
2286aec1278SMax Laier 
2296aec1278SMax Laier /*
2306aec1278SMax Laier  * Unregister/remove a firmware image.  If there are outstanding
2316aec1278SMax Laier  * references an error is returned and the image is not removed
2326aec1278SMax Laier  * from the registry.
2336aec1278SMax Laier  */
2346aec1278SMax Laier int
firmware_unregister(const char * imagename)2356aec1278SMax Laier firmware_unregister(const char *imagename)
2366aec1278SMax Laier {
23733d54970SLuigi Rizzo 	struct priv_fw *fp;
23833d54970SLuigi Rizzo 	int err;
2396aec1278SMax Laier 
2406aec1278SMax Laier 	mtx_lock(&firmware_mtx);
2414f8ad92fSMark Johnston 	fp = lookup(imagename);
24233d54970SLuigi Rizzo 	if (fp == NULL) {
2436aec1278SMax Laier 		/*
24433d54970SLuigi Rizzo 		 * It is ok for the lookup to fail; this can happen
2456aec1278SMax Laier 		 * when a module is unloaded on last reference and the
24628323addSBryan Drewery 		 * module unload handler unregister's each of its
2476aec1278SMax Laier 		 * firmware images.
2486aec1278SMax Laier 		 */
24933d54970SLuigi Rizzo 		err = 0;
25033d54970SLuigi Rizzo 	} else if (fp->refcnt != 0) {	/* cannot unregister */
25133d54970SLuigi Rizzo 		err = EBUSY;
25233d54970SLuigi Rizzo 	} else {
2534f8ad92fSMark Johnston 		LIST_REMOVE(fp, link);
2544f8ad92fSMark Johnston 		free(__DECONST(char *, fp->fw.name), M_FIRMWARE);
2554f8ad92fSMark Johnston 		free(fp, M_FIRMWARE);
25633d54970SLuigi Rizzo 		err = 0;
2576aec1278SMax Laier 	}
2586aec1278SMax Laier 	mtx_unlock(&firmware_mtx);
2594f8ad92fSMark Johnston 	return (err);
2606aec1278SMax Laier }
2616aec1278SMax Laier 
2626f65b505SBjoern A. Zeeb struct fw_loadimage {
2636f65b505SBjoern A. Zeeb 	const char	*imagename;
2646f65b505SBjoern A. Zeeb 	uint32_t	flags;
2656f65b505SBjoern A. Zeeb };
2666f65b505SBjoern A. Zeeb 
267c7b1e980SWarner Losh static const char *fw_path = "/boot/firmware/";
268c7b1e980SWarner Losh 
269c7b1e980SWarner Losh static void
try_binary_file(const char * imagename,uint32_t flags)270c7b1e980SWarner Losh try_binary_file(const char *imagename, uint32_t flags)
271c7b1e980SWarner Losh {
272c7b1e980SWarner Losh 	struct nameidata nd;
273c7b1e980SWarner Losh 	struct thread *td = curthread;
274c7b1e980SWarner Losh 	struct ucred *cred = td ? td->td_ucred : NULL;
275c7b1e980SWarner Losh 	struct sbuf *sb;
276c7b1e980SWarner Losh 	struct priv_fw *fp;
277c7b1e980SWarner Losh 	const char *fn;
278c7b1e980SWarner Losh 	struct vattr vattr;
279c7b1e980SWarner Losh 	void *data = NULL;
280c7b1e980SWarner Losh 	const struct firmware *fw;
2813a3afbecSWarner Losh 	int oflags;
282c7b1e980SWarner Losh 	size_t resid;
283c7b1e980SWarner Losh 	int error;
284c7b1e980SWarner Losh 	bool warn = flags & FIRMWARE_GET_NOWARN;
285c7b1e980SWarner Losh 
286c7b1e980SWarner Losh 	/*
287c7b1e980SWarner Losh 	 * XXX TODO: Loop over some path instead of a single element path.
288c7b1e980SWarner Losh 	 * and fetch this path from the 'firmware_path' kenv the loader sets.
289c7b1e980SWarner Losh 	 */
290c7b1e980SWarner Losh 	sb = sbuf_new_auto();
291c7b1e980SWarner Losh 	sbuf_printf(sb, "%s%s", fw_path, imagename);
292c7b1e980SWarner Losh 	sbuf_finish(sb);
293c7b1e980SWarner Losh 	fn = sbuf_data(sb);
294c7b1e980SWarner Losh 	if (bootverbose)
295c7b1e980SWarner Losh 		printf("Trying to load binary firmware from %s\n", fn);
296c7b1e980SWarner Losh 
297c7b1e980SWarner Losh 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, fn);
2983a3afbecSWarner Losh 	oflags = FREAD;
2993a3afbecSWarner Losh 	error = vn_open(&nd, &oflags, 0, NULL);
300c7b1e980SWarner Losh 	if (error)
301c7b1e980SWarner Losh 		goto err;
302c7b1e980SWarner Losh 	NDFREE_PNBUF(&nd);
303c7b1e980SWarner Losh 	if (nd.ni_vp->v_type != VREG)
304c7b1e980SWarner Losh 		goto err2;
305c7b1e980SWarner Losh 	error = VOP_GETATTR(nd.ni_vp, &vattr, cred);
306c7b1e980SWarner Losh 	if (error)
307c7b1e980SWarner Losh 		goto err2;
308c7b1e980SWarner Losh 
309c7b1e980SWarner Losh 	/*
310c7b1e980SWarner Losh 	 * Limit this to something sane, 8MB by default.
311c7b1e980SWarner Losh 	 */
312c7b1e980SWarner Losh 	if (vattr.va_size > firmware_max_size) {
3133a3afbecSWarner Losh 		printf("Firmware %s is too big: %lld bytes, %ld bytes max.\n",
3143a3afbecSWarner Losh 		    fn, (long long)vattr.va_size, (long)firmware_max_size);
315c7b1e980SWarner Losh 		goto err2;
316c7b1e980SWarner Losh 	}
317c7b1e980SWarner Losh 	data = malloc(vattr.va_size, M_FIRMWARE, M_WAITOK);
318c7b1e980SWarner Losh 	error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)data, vattr.va_size, 0,
319c7b1e980SWarner Losh 	    UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &resid, td);
320c7b1e980SWarner Losh 	/* XXX make data read only? */
321c7b1e980SWarner Losh 	VOP_UNLOCK(nd.ni_vp);
322c7b1e980SWarner Losh 	vn_close(nd.ni_vp, FREAD, cred, td);
323c7b1e980SWarner Losh 	nd.ni_vp = NULL;
324c7b1e980SWarner Losh 	if (error != 0 || resid != 0)
325c7b1e980SWarner Losh 		goto err;
326c7b1e980SWarner Losh 	fw = firmware_register(fn, data, vattr.va_size, 0, NULL);
327c7b1e980SWarner Losh 	if (fw == NULL)
328c7b1e980SWarner Losh 		goto err;
329c7b1e980SWarner Losh 	fp = PRIV_FW(fw);
330c7b1e980SWarner Losh 	fp->flags |= FW_BINARY;
331c7b1e980SWarner Losh 	if (bootverbose)
332c7b1e980SWarner Losh 		printf("%s: Loaded binary firmware using %s\n", imagename, fn);
333c7b1e980SWarner Losh 	sbuf_delete(sb);
334c7b1e980SWarner Losh 	return;
335c7b1e980SWarner Losh 
336c7b1e980SWarner Losh err2: /* cleanup in vn_open through vn_close */
337c7b1e980SWarner Losh 	VOP_UNLOCK(nd.ni_vp);
338c7b1e980SWarner Losh 	vn_close(nd.ni_vp, FREAD, cred, td);
339c7b1e980SWarner Losh err:
340c7b1e980SWarner Losh 	free(data, M_FIRMWARE);
341c7b1e980SWarner Losh 	if (bootverbose || warn)
342c7b1e980SWarner Losh 		printf("%s: could not load binary firmware %s either\n", imagename, fn);
343c7b1e980SWarner Losh 	sbuf_delete(sb);
344c7b1e980SWarner Losh }
345c7b1e980SWarner Losh 
3466c6eaea6SSam Leffler static void
loadimage(void * arg,int npending __unused)3476f65b505SBjoern A. Zeeb loadimage(void *arg, int npending __unused)
3486c6eaea6SSam Leffler {
3496f65b505SBjoern A. Zeeb 	struct fw_loadimage *fwli = arg;
3506c6eaea6SSam Leffler 	struct priv_fw *fp;
3516c6eaea6SSam Leffler 	linker_file_t result;
3526c6eaea6SSam Leffler 	int error;
3536c6eaea6SSam Leffler 
3546f65b505SBjoern A. Zeeb 	error = linker_reference_module(fwli->imagename, NULL, &result);
3556c6eaea6SSam Leffler 	if (error != 0) {
3566f65b505SBjoern A. Zeeb 		if (bootverbose || (fwli->flags & FIRMWARE_GET_NOWARN) == 0)
3576c6eaea6SSam Leffler 			printf("%s: could not load firmware image, error %d\n",
3586f65b505SBjoern A. Zeeb 			    fwli->imagename, error);
359c7b1e980SWarner Losh 		try_binary_file(fwli->imagename, fwli->flags);
3604f8ad92fSMark Johnston 		mtx_lock(&firmware_mtx);
3616c6eaea6SSam Leffler 		goto done;
3626c6eaea6SSam Leffler 	}
3636c6eaea6SSam Leffler 
3646c6eaea6SSam Leffler 	mtx_lock(&firmware_mtx);
3656f65b505SBjoern A. Zeeb 	fp = lookup(fwli->imagename);
3666c6eaea6SSam Leffler 	if (fp == NULL || fp->file != NULL) {
3676c6eaea6SSam Leffler 		mtx_unlock(&firmware_mtx);
3686c6eaea6SSam Leffler 		if (fp == NULL)
3696c6eaea6SSam Leffler 			printf("%s: firmware image loaded, "
3706f65b505SBjoern A. Zeeb 			    "but did not register\n", fwli->imagename);
3716f65b505SBjoern A. Zeeb 		(void) linker_release_module(fwli->imagename, NULL, NULL);
3724f8ad92fSMark Johnston 		mtx_lock(&firmware_mtx);
3736c6eaea6SSam Leffler 		goto done;
3746c6eaea6SSam Leffler 	}
3756c6eaea6SSam Leffler 	fp->file = result;	/* record the module identity */
3766c6eaea6SSam Leffler done:
3776f65b505SBjoern A. Zeeb 	wakeup_one(arg);
3784f8ad92fSMark Johnston 	mtx_unlock(&firmware_mtx);
3796c6eaea6SSam Leffler }
3806c6eaea6SSam Leffler 
3816aec1278SMax Laier /*
3826aec1278SMax Laier  * Lookup and potentially load the specified firmware image.
38333d54970SLuigi Rizzo  * If the firmware is not found in the registry, try to load a kernel
38433d54970SLuigi Rizzo  * module named as the image name.
38533d54970SLuigi Rizzo  * If the firmware is located, a reference is returned. The caller must
38633d54970SLuigi Rizzo  * release this reference for the image to be eligible for removal/unload.
3876aec1278SMax Laier  */
38833d54970SLuigi Rizzo const struct firmware *
firmware_get_flags(const char * imagename,uint32_t flags)3896f65b505SBjoern A. Zeeb firmware_get_flags(const char *imagename, uint32_t flags)
3906aec1278SMax Laier {
3916c6eaea6SSam Leffler 	struct task fwload_task;
3926aec1278SMax Laier 	struct thread *td;
39333d54970SLuigi Rizzo 	struct priv_fw *fp;
3946aec1278SMax Laier 
3956aec1278SMax Laier 	mtx_lock(&firmware_mtx);
3964f8ad92fSMark Johnston 	fp = lookup(imagename);
39733d54970SLuigi Rizzo 	if (fp != NULL)
39833d54970SLuigi Rizzo 		goto found;
3996aec1278SMax Laier 	/*
40033d54970SLuigi Rizzo 	 * Image not present, try to load the module holding it.
4016aec1278SMax Laier 	 */
4026aec1278SMax Laier 	td = curthread;
403acd3428bSRobert Watson 	if (priv_check(td, PRIV_FIRMWARE_LOAD) != 0 ||
404acd3428bSRobert Watson 	    securelevel_gt(td->td_ucred, 0) != 0) {
4056c6eaea6SSam Leffler 		mtx_unlock(&firmware_mtx);
4066aec1278SMax Laier 		printf("%s: insufficient privileges to "
4076aec1278SMax Laier 		    "load firmware image %s\n", __func__, imagename);
4086aec1278SMax Laier 		return NULL;
4096aec1278SMax Laier 	}
410450ec4edSIan Dowse 	/*
4116c6eaea6SSam Leffler 	 * Defer load to a thread with known context.  linker_reference_module
4126c6eaea6SSam Leffler 	 * may do filesystem i/o which requires root & current dirs, etc.
4136c6eaea6SSam Leffler 	 * Also we must not hold any mtx's over this call which is problematic.
414450ec4edSIan Dowse 	 */
415528fb798SAndrew Gallatin 	if (!cold) {
4166f65b505SBjoern A. Zeeb 		struct fw_loadimage fwli;
4176f65b505SBjoern A. Zeeb 
4186f65b505SBjoern A. Zeeb 		fwli.imagename = imagename;
4196f65b505SBjoern A. Zeeb 		fwli.flags = flags;
4206f65b505SBjoern A. Zeeb 		TASK_INIT(&fwload_task, 0, loadimage, (void *)&fwli);
4216c6eaea6SSam Leffler 		taskqueue_enqueue(firmware_tq, &fwload_task);
4226f65b505SBjoern A. Zeeb 		PHOLD(curproc);
4236f65b505SBjoern A. Zeeb 		msleep((void *)&fwli, &firmware_mtx, 0, "fwload", 0);
4246f65b505SBjoern A. Zeeb 		PRELE(curproc);
425528fb798SAndrew Gallatin 	}
4266c6eaea6SSam Leffler 	/*
4276c6eaea6SSam Leffler 	 * After attempting to load the module, see if the image is registered.
4286c6eaea6SSam Leffler 	 */
4294f8ad92fSMark Johnston 	fp = lookup(imagename);
43033d54970SLuigi Rizzo 	if (fp == NULL) {
4316aec1278SMax Laier 		mtx_unlock(&firmware_mtx);
43233d54970SLuigi Rizzo 		return NULL;
43333d54970SLuigi Rizzo 	}
43433d54970SLuigi Rizzo found:				/* common exit point on success */
435059d10c7SNavdeep Parhar 	if (fp->refcnt == 0 && fp->parent != NULL)
436059d10c7SNavdeep Parhar 		fp->parent->refcnt++;
43733d54970SLuigi Rizzo 	fp->refcnt++;
43833d54970SLuigi Rizzo 	mtx_unlock(&firmware_mtx);
43933d54970SLuigi Rizzo 	return &fp->fw;
4406aec1278SMax Laier }
4416aec1278SMax Laier 
4426f65b505SBjoern A. Zeeb const struct firmware *
firmware_get(const char * imagename)4436f65b505SBjoern A. Zeeb firmware_get(const char *imagename)
4446f65b505SBjoern A. Zeeb {
4456f65b505SBjoern A. Zeeb 
4466f65b505SBjoern A. Zeeb 	return (firmware_get_flags(imagename, 0));
4476f65b505SBjoern A. Zeeb }
4486f65b505SBjoern A. Zeeb 
4496aec1278SMax Laier /*
45033d54970SLuigi Rizzo  * Release a reference to a firmware image returned by firmware_get.
45133d54970SLuigi Rizzo  * The caller may specify, with the FIRMWARE_UNLOAD flag, its desire
45233d54970SLuigi Rizzo  * to release the resource, but the flag is only advisory.
45333d54970SLuigi Rizzo  *
45433d54970SLuigi Rizzo  * If this is the last reference to the firmware image, and this is an
4556c6eaea6SSam Leffler  * autoloaded module, wake up the firmware_unload_task to figure out
4566c6eaea6SSam Leffler  * what to do with the associated module.
4576aec1278SMax Laier  */
4586aec1278SMax Laier void
firmware_put(const struct firmware * p,int flags)45933d54970SLuigi Rizzo firmware_put(const struct firmware *p, int flags)
4606aec1278SMax Laier {
46133d54970SLuigi Rizzo 	struct priv_fw *fp = PRIV_FW(p);
46233d54970SLuigi Rizzo 
4636aec1278SMax Laier 	mtx_lock(&firmware_mtx);
4646aec1278SMax Laier 	fp->refcnt--;
465eb1030c4SIan Dowse 	if (fp->refcnt == 0) {
466059d10c7SNavdeep Parhar 		if (fp->parent != NULL)
467059d10c7SNavdeep Parhar 			fp->parent->refcnt--;
46833d54970SLuigi Rizzo 		if (flags & FIRMWARE_UNLOAD)
46933d54970SLuigi Rizzo 			fp->flags |= FW_UNLOAD;
4706aec1278SMax Laier 		if (fp->file)
4716c6eaea6SSam Leffler 			taskqueue_enqueue(firmware_tq, &firmware_unload_task);
47233d54970SLuigi Rizzo 	}
47333d54970SLuigi Rizzo 	mtx_unlock(&firmware_mtx);
47433d54970SLuigi Rizzo }
47533d54970SLuigi Rizzo 
47633d54970SLuigi Rizzo /*
4776c6eaea6SSam Leffler  * Setup directory state for the firmware_tq thread so we can do i/o.
4786c6eaea6SSam Leffler  */
4796c6eaea6SSam Leffler static void
set_rootvnode(void * arg,int npending)4806c6eaea6SSam Leffler set_rootvnode(void *arg, int npending)
4816c6eaea6SSam Leffler {
4826c6eaea6SSam Leffler 
4838a08cec1SMateusz Guzik 	pwd_ensure_dirs();
48473254c9eSSam Leffler 	free(arg, M_TEMP);
4856c6eaea6SSam Leffler }
4866c6eaea6SSam Leffler 
4876c6eaea6SSam Leffler /*
4886c6eaea6SSam Leffler  * Event handler called on mounting of /; bounce a task
4896c6eaea6SSam Leffler  * into the task queue thread to setup it's directories.
4906c6eaea6SSam Leffler  */
4916c6eaea6SSam Leffler static void
firmware_mountroot(void * arg)4926c6eaea6SSam Leffler firmware_mountroot(void *arg)
4936c6eaea6SSam Leffler {
49473254c9eSSam Leffler 	struct task *setroot_task;
4956c6eaea6SSam Leffler 
49673254c9eSSam Leffler 	setroot_task = malloc(sizeof(struct task), M_TEMP, M_NOWAIT);
49773254c9eSSam Leffler 	if (setroot_task != NULL) {
49873254c9eSSam Leffler 		TASK_INIT(setroot_task, 0, set_rootvnode, setroot_task);
49973254c9eSSam Leffler 		taskqueue_enqueue(firmware_tq, setroot_task);
50073254c9eSSam Leffler 	} else
50173254c9eSSam Leffler 		printf("%s: no memory for task!\n", __func__);
5026c6eaea6SSam Leffler }
5036c6eaea6SSam Leffler EVENTHANDLER_DEFINE(mountroot, firmware_mountroot, NULL, 0);
5046c6eaea6SSam Leffler 
5056c6eaea6SSam Leffler /*
50633d54970SLuigi Rizzo  * The body of the task in charge of unloading autoloaded modules
50733d54970SLuigi Rizzo  * that are not needed anymore.
50833d54970SLuigi Rizzo  * Images can be cross-linked so we may need to make multiple passes,
50933d54970SLuigi Rizzo  * but the time we spend in the loop is bounded because we clear entries
51033d54970SLuigi Rizzo  * as we touch them.
51133d54970SLuigi Rizzo  */
51233d54970SLuigi Rizzo static void
unloadentry(void * unused1,int unused2)51333d54970SLuigi Rizzo unloadentry(void *unused1, int unused2)
51433d54970SLuigi Rizzo {
515c7b1e980SWarner Losh 	struct priv_fw *fp, *tmp;
51633d54970SLuigi Rizzo 
51733d54970SLuigi Rizzo 	mtx_lock(&firmware_mtx);
5184f8ad92fSMark Johnston restart:
519c7b1e980SWarner Losh 	LIST_FOREACH_SAFE(fp, &firmware_table, link, tmp) {
520c7b1e980SWarner Losh 		if (((fp->flags & FW_BINARY) == 0 && fp->file == NULL) ||
521c7b1e980SWarner Losh 		    fp->refcnt != 0 || (fp->flags & FW_UNLOAD) == 0)
52233d54970SLuigi Rizzo 			continue;
52333d54970SLuigi Rizzo 
52433d54970SLuigi Rizzo 		/*
525c7b1e980SWarner Losh 		 * If we directly loaded the firmware, then we just need to
526c7b1e980SWarner Losh 		 * remove the entry from the list and free the entry and go to
527c7b1e980SWarner Losh 		 * the next one.  There's no need for the indirection of the kld
528c7b1e980SWarner Losh 		 * module case, we free memory and go to the next one.
529c7b1e980SWarner Losh 		 */
530c7b1e980SWarner Losh 		if ((fp->flags & FW_BINARY) != 0) {
531c7b1e980SWarner Losh 			LIST_REMOVE(fp, link);
532c7b1e980SWarner Losh 			free(__DECONST(char *, fp->fw.data), M_FIRMWARE);
533c7b1e980SWarner Losh 			free(__DECONST(char *, fp->fw.name), M_FIRMWARE);
534c7b1e980SWarner Losh 			free(fp, M_FIRMWARE);
535c7b1e980SWarner Losh 			continue;
536c7b1e980SWarner Losh 		}
537c7b1e980SWarner Losh 
538c7b1e980SWarner Losh 		/*
539c7b1e980SWarner Losh 		 * Found an entry.  This is the kld case, so we have a more
540c7b1e980SWarner Losh 		 * complex dance.  Now:
5414f8ad92fSMark Johnston 		 * 1. make sure we scan the table again
54233d54970SLuigi Rizzo 		 * 2. clear FW_UNLOAD so we don't try this entry again.
54333d54970SLuigi Rizzo 		 * 3. release the lock while trying to unload the module.
54433d54970SLuigi Rizzo 		 */
54533d54970SLuigi Rizzo 		fp->flags &= ~FW_UNLOAD;	/* do not try again */
54633d54970SLuigi Rizzo 
54733d54970SLuigi Rizzo 		/*
54833d54970SLuigi Rizzo 		 * We rely on the module to call firmware_unregister()
5494f8ad92fSMark Johnston 		 * on unload to actually free the entry.
55033d54970SLuigi Rizzo 		 */
5514f8ad92fSMark Johnston 		mtx_unlock(&firmware_mtx);
5526776747aSKonstantin Belousov 		(void)linker_release_module(NULL, NULL, fp->file);
5534f8ad92fSMark Johnston 		mtx_lock(&firmware_mtx);
55446cac10bSAndrew Gallatin 
55546cac10bSAndrew Gallatin 		/*
55646cac10bSAndrew Gallatin 		 * When we dropped the lock, another thread could have
55746cac10bSAndrew Gallatin 		 * removed an element, so we must restart the scan.
55846cac10bSAndrew Gallatin 		 */
5594f8ad92fSMark Johnston 		goto restart;
56033d54970SLuigi Rizzo 	}
5616aec1278SMax Laier 	mtx_unlock(&firmware_mtx);
5626aec1278SMax Laier }
5636aec1278SMax Laier 
5646aec1278SMax Laier /*
565479905a1SWarner Losh  * Find all the binary firmware that was loaded in the boot loader via load -t
566479905a1SWarner Losh  * firmware foo.  There is only one firmware per file, it's the whole file, and
567479905a1SWarner Losh  * there's no meaningful version passed in, so pass 0 for that.  If version is
568479905a1SWarner Losh  * needed by the consumer (and not just arbitrarily defined), the .ko version
569479905a1SWarner Losh  * must be used instead.
570479905a1SWarner Losh  */
571479905a1SWarner Losh static void
firmware_binary_files(void)572479905a1SWarner Losh firmware_binary_files(void)
573479905a1SWarner Losh {
574479905a1SWarner Losh 	caddr_t file;
575479905a1SWarner Losh 	char *name;
576479905a1SWarner Losh 	const char *type;
577479905a1SWarner Losh 	const void *addr;
578479905a1SWarner Losh 	size_t size;
579479905a1SWarner Losh 	unsigned int version = 0;
580479905a1SWarner Losh 	const struct firmware *fw;
581479905a1SWarner Losh 	struct priv_fw *fp;
582479905a1SWarner Losh 
583479905a1SWarner Losh 	file = 0;
584479905a1SWarner Losh 	for (;;) {
585479905a1SWarner Losh 		file = preload_search_next_name(file);
586479905a1SWarner Losh 		if (file == 0)
587479905a1SWarner Losh 			break;
588479905a1SWarner Losh 		type = (const char *)preload_search_info(file, MODINFO_TYPE);
589479905a1SWarner Losh 		if (type == NULL || strcmp(type, "firmware") != 0)
590479905a1SWarner Losh 			continue;
591479905a1SWarner Losh 		name = preload_search_info(file, MODINFO_NAME);
592479905a1SWarner Losh 		addr = preload_fetch_addr(file);
593479905a1SWarner Losh 		size = preload_fetch_size(file);
594479905a1SWarner Losh 		fw = firmware_register(name, addr, size, version, NULL);
595479905a1SWarner Losh 		fp = PRIV_FW(fw);
596479905a1SWarner Losh 		fp->refcnt++;	/* Hold an extra reference so we never unload */
597479905a1SWarner Losh 	}
598479905a1SWarner Losh }
599479905a1SWarner Losh 
600479905a1SWarner Losh /*
6016aec1278SMax Laier  * Module glue.
6026aec1278SMax Laier  */
6036aec1278SMax Laier static int
firmware_modevent(module_t mod,int type,void * unused)6046aec1278SMax Laier firmware_modevent(module_t mod, int type, void *unused)
6056aec1278SMax Laier {
60633d54970SLuigi Rizzo 	struct priv_fw *fp;
6074f8ad92fSMark Johnston 	int err;
608eb1030c4SIan Dowse 
6094f8ad92fSMark Johnston 	err = 0;
6106aec1278SMax Laier 	switch (type) {
6116aec1278SMax Laier 	case MOD_LOAD:
6126c6eaea6SSam Leffler 		TASK_INIT(&firmware_unload_task, 0, unloadentry, NULL);
6136c6eaea6SSam Leffler 		firmware_tq = taskqueue_create("taskqueue_firmware", M_WAITOK,
6146c6eaea6SSam Leffler 		    taskqueue_thread_enqueue, &firmware_tq);
6156c6eaea6SSam Leffler 		/* NB: use our own loop routine that sets up context */
6166c6eaea6SSam Leffler 		(void) taskqueue_start_threads(&firmware_tq, 1, PWAIT,
6176c6eaea6SSam Leffler 		    "firmware taskq");
618479905a1SWarner Losh 		firmware_binary_files();
6196c6eaea6SSam Leffler 		if (rootvnode != NULL) {
6206c6eaea6SSam Leffler 			/*
6216c6eaea6SSam Leffler 			 * Root is already mounted so we won't get an event;
6226c6eaea6SSam Leffler 			 * simulate one here.
6236c6eaea6SSam Leffler 			 */
6246c6eaea6SSam Leffler 			firmware_mountroot(NULL);
6256c6eaea6SSam Leffler 		}
6264f8ad92fSMark Johnston 		break;
62733d54970SLuigi Rizzo 
6286aec1278SMax Laier 	case MOD_UNLOAD:
62933d54970SLuigi Rizzo 		/* request all autoloaded modules to be released */
63033d54970SLuigi Rizzo 		mtx_lock(&firmware_mtx);
6314f8ad92fSMark Johnston 		LIST_FOREACH(fp, &firmware_table, link)
632c2ede4b3SMartin Blapp 			fp->flags |= FW_UNLOAD;
63333d54970SLuigi Rizzo 		mtx_unlock(&firmware_mtx);
6346c6eaea6SSam Leffler 		taskqueue_enqueue(firmware_tq, &firmware_unload_task);
6356c6eaea6SSam Leffler 		taskqueue_drain(firmware_tq, &firmware_unload_task);
6364f8ad92fSMark Johnston 
6374f8ad92fSMark Johnston 		LIST_FOREACH(fp, &firmware_table, link) {
63833d54970SLuigi Rizzo 			if (fp->fw.name != NULL) {
6394f8ad92fSMark Johnston 				printf("%s: image %s still active, %d refs\n",
6404f8ad92fSMark Johnston 				    __func__, fp->fw.name, fp->refcnt);
64133d54970SLuigi Rizzo 				err = EINVAL;
64233d54970SLuigi Rizzo 			}
64333d54970SLuigi Rizzo 		}
6446c6eaea6SSam Leffler 		if (err == 0)
6456c6eaea6SSam Leffler 			taskqueue_free(firmware_tq);
6464f8ad92fSMark Johnston 		break;
6474f8ad92fSMark Johnston 
6484f8ad92fSMark Johnston 	default:
6494f8ad92fSMark Johnston 		err = EOPNOTSUPP;
6504f8ad92fSMark Johnston 		break;
6516aec1278SMax Laier 	}
6524f8ad92fSMark Johnston 	return (err);
6536aec1278SMax Laier }
6546aec1278SMax Laier 
6556aec1278SMax Laier static moduledata_t firmware_mod = {
6566aec1278SMax Laier 	"firmware",
6576aec1278SMax Laier 	firmware_modevent,
6584592c621SWarner Losh 	NULL
6596aec1278SMax Laier };
6606aec1278SMax Laier DECLARE_MODULE(firmware, firmware_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
6616aec1278SMax Laier MODULE_VERSION(firmware, 1);
662