]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/man/man9/firmware.9
zfs: merge openzfs/zfs@4694131a0 (master) into main
[FreeBSD/FreeBSD.git] / share / man / man9 / firmware.9
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
42 struct 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
66 The
67 .Nm firmware
68 abstraction provides a convenient interface for loading
69 .Nm firmware images
70 into the kernel, and for accessing such images from kernel components.
71 .Pp
72 A
73 .Nm firmware image
74 (or
75 .Nm image
76 for brevity)
77 is an opaque block of data residing in kernel memory.
78 It is associated to a unique
79 .Nm imagename
80 which constitutes a search key, and to an integer
81 .Nm version
82 number, which is also an opaque piece of information for the
83 firmware subsystem.
84 .Pp
85 An image is registered with the
86 .Nm firmware
87 subsystem by calling the function
88 .Fn firmware_register ,
89 and unregistered by calling
90 .Fn firmware_unregister .
91 These functions are usually (but not exclusively) called by
92 specially crafted kernel modules that contain the firmware image.
93 The modules can be statically compiled in the kernel, or loaded by
94 .Nm /boot/loader ,
95 manually at runtime, or on demand by the firmware subsystem.
96 .Pp
97 .Nm Clients
98 of the firmware subsystem can request access to a given image
99 by calling the function
100 .Fn firmware_get
101 with the
102 .Nm imagename
103 they want as an argument, or by calling
104 .Fn firmware_get_flags
105 with the
106 .Nm imagename
107 and
108 .Nm flags
109 they want as an arguments.
110 If a matching image is not already registered,
111 the firmware subsystem will try to load it using the
112 mechanisms specified below (typically, a kernel module
113 with
114 .Nm
115 the same name
116 as the image).
117 .Sh API DESCRIPTION
118 The kernel
119 .Nm
120 firmware API
121 is made of the following functions:
122 .Pp
123 .Fn firmware_register
124 registers with the kernel an image of size
125 .Nm datasize
126 located at address
127 .Nm data ,
128 under the name
129 .Nm imagename .
130 .Pp
131 The function returns NULL on error (e.g. because an
132 image with the same name already exists, or the image
133 table is full), or a
134 .Ft const struct firmware *
135 pointer to the image requested.
136 .Pp
137 .Fn firmware_unregister
138 tries to unregister the firmware image
139 .Nm imagename
140 from the system.
141 The function is successful and returns 0
142 if there are no pending references to the image, otherwise
143 it does not unregister the image and returns EBUSY.
144 .Pp
145 .Fn firmware_get
146 and
147 .Fn firmware_get_flags
148 return the requested firmware image.
149 The
150 .Fa flags
151 argument may be set to
152 .Dv FIRMWARE_GET_NOWARN
153 to indicate that errors on firmware load or registration should
154 only be logged in case of
155 .Nm booverbose .
156 If the image is not yet registered with the system,
157 the functions try to load it.
158 This involves the linker subsystem and disk access, so
159 .Fn firmware_get
160 or
161 .Fn firmware_get_flags
162 must not be called with any locks (except for
163 .Va Giant ) .
164 Note also that if the firmware image is loaded from a filesystem
165 it must already be mounted.
166 In particular this means that it may be necessary to defer requests
167 from a driver attach method unless it is known the root filesystem is
168 already mounted.
169 .Pp
170 On success,
171 .Fn firmware_get
172 and
173 .Fn firmware_get_flags
174 return a pointer to the image description and increase the reference count
175 for this image.
176 On failure, the functions return NULL.
177 .Pp
178 .Fn firmware_put
179 drops a reference to a firmware image.
180 The
181 .Fa flags
182 argument may be set to
183 .Dv FIRMWARE_UNLOAD
184 to indicate that
185 firmware_put is free to reclaim resources associated with
186 the firmware image if this is the last reference.
187 By default a firmware image will be deferred to a
188 .Xr taskqueue 9
189 thread so the call may be done while holding a lock.
190 In certain cases, such as on driver detach, this cannot be allowed.
191 .Sh FIRMWARE LOADING MECHANISMS
192 As mentioned before, any component of the system can register
193 firmware images at any time by simply calling
194 .Fn firmware_register .
195 .Pp
196 This is typically done when a module containing
197 a firmware image is given control,
198 whether compiled in, or preloaded by
199 .Nm /boot/loader ,
200 or manually loaded with
201 .Xr kldload 8 .
202 However, a system can implement additional mechanisms to bring
203 these images in memory before calling
204 .Fn firmware_register .
205 .Pp
206 When
207 .Fn firmware_get
208 or
209 .Fn firmware_get_flags
210 does not find the requested image, it tries to load it using
211 one of the available loading mechanisms.
212 At the moment, there is only one, namely
213 .Nm Loadable kernel modules .
214 .Pp
215 A firmware image named
216 .Nm foo
217 is looked up by trying to load the module named
218 .Nm foo.ko ,
219 using the facilities described in
220 .Xr kld 4 .
221 In particular, images are looked up in the directories specified
222 by the sysctl variable
223 .Nm kern.module_path
224 which on most systems defaults to
225 .Nm /boot/kernel;/boot/modules .
226 .Pp
227 Note that in case a module contains multiple images,
228 the caller should first request a
229 .Fn firmware_get
230 or
231 .Fn firmware_get_flags
232 for the first image contained in the module, followed by requests
233 for the other images.
234 .Sh BUILDING FIRMWARE LOADABLE MODULES
235 A firmware module is built by embedding the
236 .Nm firmware image
237 into a suitable loadable kernel module that calls
238 .Fn firmware_register
239 on loading, and
240 .Fn firmware_unregister
241 on unloading.
242 .Pp
243 Various system scripts and makefiles let you build a module
244 by 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
252 where KMOD is the basename of the module; FIRMWS is a list of
253 colon-separated tuples indicating the image_file's to be embedded
254 in the module, the imagename and version of each firmware image.
255 .Pp
256 If you need to embed firmware images into a system, you should write
257 appropriate entries in the <files.arch> file, e.g. this example is
258 from
259 .Nm sys/arm/xscale/ixp425/files.ixp425 :
260 .Bd -literal
261 ixp425_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 #
271 IxNpeMicrocode.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
279 Firmware was previously committed to the source tree as uuencoded files,
280 but this is no longer required; the binary firmware file should be committed
281 to the tree as provided by the vendor.
282 .Pp
283 Note that generating the firmware modules in this way requires
284 the availability of the following tools:
285 .Xr awk 1 ,
286 .Xr make 1 ,
287 the 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
294 The
295 .Nm firmware
296 system was introduced in
297 .Fx 6.1 .
298 .Sh AUTHORS
299 This manual page was written by
300 .An Max Laier Aq Mt mlaier@FreeBSD.org .