xref: /freebsd/share/man/man9/firmware.9 (revision 517e52b6c21ccff22c46df0dcd15c19baee3d86c)
1.\" Copyright (c) 2006 Max Laier <mlaier@FreeBSD.org>
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\"
13.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
14.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
17.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23.\"
24.\" $FreeBSD$
25.\"
26.Dd January 27, 2021
27.Dt FIRMWARE 9
28.Os
29.Sh NAME
30.Nm firmware_register ,
31.Nm firmware_unregister ,
32.Nm firmware_get ,
33.Nm firmware_get_flags ,
34.Nm firmware_put
35.Nd firmware image loading and management
36.Sh SYNOPSIS
37.In sys/param.h
38.In sys/systm.h
39.In sys/linker.h
40.In sys/firmware.h
41.Bd -literal
42struct firmware {
43	const char	*name;		/* system-wide name */
44	const void	*data;		/* location of image */
45	size_t		datasize;	/* size of image in bytes */
46	unsigned int	version;	/* version of the image */
47};
48.Ed
49.Ft "const struct firmware *"
50.Fo firmware_register
51.Fa "const char *imagename"
52.Fa "const void *data"
53.Fa "size_t datasize"
54.Fa "unsigned int version"
55.Fa "const struct firmware *parent"
56.Fc
57.Ft int
58.Fn firmware_unregister "const char *imagename"
59.Ft "const struct firmware *"
60.Fn firmware_get "const char *imagename"
61.Ft "const struct firmware *"
62.Fn firmware_get_flags "const char *imagename" "uint32_t flags"
63.Ft void
64.Fn firmware_put "const struct firmware *fp" "int flags"
65.Sh DESCRIPTION
66The
67.Nm firmware
68abstraction provides a convenient interface for loading
69.Nm firmware images
70into the kernel, and for accessing such images from kernel components.
71.Pp
72A
73.Nm firmware image
74(or
75.Nm image
76for brevity)
77is an opaque block of data residing in kernel memory.
78It is associated to a unique
79.Nm imagename
80which constitutes a search key, and to an integer
81.Nm version
82number, which is also an opaque piece of information for the
83firmware subsystem.
84.Pp
85An image is registered with the
86.Nm firmware
87subsystem by calling the function
88.Fn firmware_register ,
89and unregistered by calling
90.Fn firmware_unregister .
91These functions are usually (but not exclusively) called by
92specially crafted kernel modules that contain the firmware image.
93The modules can be statically compiled in the kernel, or loaded by
94.Nm /boot/loader ,
95manually at runtime, or on demand by the firmware subsystem.
96.Pp
97.Nm Clients
98of the firmware subsystem can request access to a given image
99by calling the function
100.Fn firmware_get
101with the
102.Nm imagename
103they want as an argument, or by calling
104.Fn firmware_get_flags
105with the
106.Nm imagename
107and
108.Nm flags
109they want as an arguments.
110If a matching image is not already registered,
111the firmware subsystem will try to load it using the
112mechanisms specified below (typically, a kernel module
113with
114.Nm
115the same name
116as the image).
117.Sh API DESCRIPTION
118The kernel
119.Nm
120firmware API
121is made of the following functions:
122.Pp
123.Fn firmware_register
124registers with the kernel an image of size
125.Nm datasize
126located at address
127.Nm data ,
128under the name
129.Nm imagename .
130.Pp
131The function returns NULL on error (e.g. because an
132image with the same name already exists, or the image
133table is full), or a
134.Ft const struct firmware *
135pointer to the image requested.
136.Pp
137.Fn firmware_unregister
138tries to unregister the firmware image
139.Nm imagename
140from the system.
141The function is successful and returns 0
142if there are no pending references to the image, otherwise
143it does not unregister the image and returns EBUSY.
144.Pp
145.Fn firmware_get
146and
147.Fn firmware_get_flags
148return the requested firmware image.
149The
150.Fa flags
151argument may be set to
152.Dv FIRMWARE_GET_NOWARN
153to indicate that errors on firmware load or registration should
154only be logged in case of
155.Nm booverbose .
156If the image is not yet registered with the system,
157the functions try to load it.
158This involves the linker subsystem and disk access, so
159.Fn firmware_get
160or
161.Fn firmware_get_flags
162must not be called with any locks (except for
163.Va Giant ) .
164Note also that if the firmware image is loaded from a filesystem
165it must already be mounted.
166In particular this means that it may be necessary to defer requests
167from a driver attach method unless it is known the root filesystem is
168already mounted.
169.Pp
170On success,
171.Fn firmware_get
172and
173.Fn firmware_get_flags
174return a pointer to the image description and increase the reference count
175for this image.
176On failure, the functions return NULL.
177.Pp
178.Fn firmware_put
179drops a reference to a firmware image.
180The
181.Fa flags
182argument may be set to
183.Dv FIRMWARE_UNLOAD
184to indicate that
185firmware_put is free to reclaim resources associated with
186the firmware image if this is the last reference.
187By default a firmware image will be deferred to a
188.Xr taskqueue 9
189thread so the call may be done while holding a lock.
190In certain cases, such as on driver detach, this cannot be allowed.
191.Sh FIRMWARE LOADING MECHANISMS
192As mentioned before, any component of the system can register
193firmware images at any time by simply calling
194.Fn firmware_register .
195.Pp
196This is typically done when a module containing
197a firmware image is given control,
198whether compiled in, or preloaded by
199.Nm /boot/loader ,
200or manually loaded with
201.Xr kldload 8 .
202However, a system can implement additional mechanisms to bring
203these images in memory before calling
204.Fn firmware_register .
205.Pp
206When
207.Fn firmware_get
208or
209.Fn firmware_get_flags
210does not find the requested image, it tries to load it using
211one of the available loading mechanisms.
212At the moment, there is only one, namely
213.Nm Loadable kernel modules .
214.Pp
215A firmware image named
216.Nm foo
217is looked up by trying to load the module named
218.Nm foo.ko ,
219using the facilities described in
220.Xr kld 4 .
221In particular, images are looked up in the directories specified
222by the sysctl variable
223.Nm kern.module_path
224which on most systems defaults to
225.Nm /boot/kernel;/boot/modules .
226.Pp
227Note that in case a module contains multiple images,
228the caller should first request a
229.Fn firmware_get
230or
231.Fn firmware_get_flags
232for the first image contained in the module, followed by requests
233for the other images.
234.Sh BUILDING FIRMWARE LOADABLE MODULES
235A firmware module is built by embedding the
236.Nm firmware image
237into a suitable loadable kernel module that calls
238.Fn firmware_register
239on loading, and
240.Fn firmware_unregister
241on unloading.
242.Pp
243Various system scripts and makefiles let you build a module
244by simply writing a Makefile with the following entries:
245.Bd -literal
246
247        KMOD=   imagename
248        FIRMWS= image_file:imagename[:version]
249        .include <bsd.kmod.mk>
250
251.Ed
252where KMOD is the basename of the module; FIRMWS is a list of
253colon-separated tuples indicating the image_file's to be embedded
254in the module, the imagename and version of each firmware image.
255.Pp
256If you need to embed firmware images into a system, you should write
257appropriate entries in the <files.arch> file, e.g. this example is
258from
259.Nm sys/arm/xscale/ixp425/files.ixp425 :
260.Bd -literal
261ixp425_npe_fw.c                         optional npe_fw                 \\
262        compile-with    "${AWK} -f $S/tools/fw_stub.awk			\\
263			IxNpeMicrocode.dat:npe_fw -mnpe -c${.TARGET}"	\\
264        no-implicit-rule before-depend local                            \\
265        clean           "ixp425_npe_fw.c"
266#
267# NB: ld encodes the path in the binary symbols generated for the
268#     firmware image so link the file to the object directory to
269#     get known values for reference in the _fw.c file.
270#
271IxNpeMicrocode.fwo  optional npe_fw					\\
272        dependency      "IxNpeMicrocode.dat"				\\
273        compile-with    "${LD} -b binary -d -warn-common		\\
274			    -r -d -o ${.TARGET} IxNpeMicrocode.dat"	\\
275        no-implicit-rule                                                \\
276        clean           "IxNpeMicrocode.fwo"
277.Ed
278.Pp
279Firmware was previously committed to the source tree as uuencoded files,
280but this is no longer required; the binary firmware file should be committed
281to the tree as provided by the vendor.
282.Pp
283Note that generating the firmware modules in this way requires
284the availability of the following tools:
285.Xr awk 1 ,
286.Xr make 1 ,
287the compiler and the linker.
288.Sh SEE ALSO
289.Xr kld 4 ,
290.Xr module 9
291.Pp
292.Pa /usr/share/examples/kld/firmware
293.Sh HISTORY
294The
295.Nm firmware
296system was introduced in
297.Fx 6.1 .
298.Sh AUTHORS
299This manual page was written by
300.An Max Laier Aq Mt mlaier@FreeBSD.org .
301