xref: /freebsd/share/man/man9/firmware.9 (revision 2a6b4327c288b6a121f131f7ef09b871b9a14f96)
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 August 2, 2008
27.Dt FIRMWARE 9
28.Os
29.Sh NAME
30.Nm firmware_register ,
31.Nm firmware_unregister ,
32.Nm firmware_get ,
33.Nm firmware_put
34.Nd firmware image loading and management
35.Sh SYNOPSIS
36.In sys/param.h
37.In sys/systm.h
38.In sys/linker.h
39.In sys/firmware.h
40.Bd -literal
41struct firmware {
42	const char	*name;		/* system-wide name */
43	const void	*data;		/* location of image */
44	size_t		datasize;	/* size of image in bytes */
45	unsigned int	version;	/* version of the image */
46};
47.Ed
48.Ft "const struct firmware *"
49.Fo firmware_register
50.Fa "const char *imagename"
51.Fa "const void *data"
52.Fa "size_t datasize"
53.Fa "unsigned int version"
54.Fa "const struct firmware *parent"
55.Fc
56.Ft int
57.Fn firmware_unregister "const char *imagename"
58.Ft "const struct firmware *"
59.Fn firmware_get "const char *imagename"
60.Ft void
61.Fn firmware_put "const struct firmware *fp" "int flags"
62.Sh DESCRIPTION
63The
64.Nm firmware
65abstraction provides a convenient interface for loading
66.Nm firmware images
67into the kernel, and for accessing such images from kernel components.
68.Pp
69A
70.Nm firmware image
71(or
72.Nm image
73for brevity)
74is an opaque block of data residing in kernel memory.
75It is associated to a unique
76.Nm imagename
77which constitutes a search key, and to an integer
78.Nm version
79number, which is also an opaque piece of information for the
80firmware subsystem.
81.Pp
82An image is registered with the
83.Nm firmware
84subsystem by calling the function
85.Fn firmware_register ,
86and unregistered by calling
87.Fn firmware_unregister .
88These functions are usually (but not exclusively) called by
89specially crafted kernel modules that contain the firmware image.
90The modules can be statically compiled in the kernel, or loaded by
91.Nm /boot/loader ,
92manually at runtime, or on demand by the firmware subsystem.
93.Pp
94.Nm Clients
95of the firmware subsystem can request access to a given image
96by calling the function
97.Fn firmware_get
98with the
99.Nm imagename
100they want as an argument. If a matching image is not already registered,
101the firmware subsystem will try to load it using the
102mechanisms specified below (typically, a kernel module
103with
104.Nm
105the same name
106as the image).
107.Sh API DESCRIPTION
108The kernel
109.Nm
110firmware API
111is made of the following functions:
112.Pp
113.Fn firmware_register
114registers with the kernel an image of size
115.Nm datasize
116located at address
117.Nm data ,
118under the name
119.Nm imagename .
120.Pp
121The function returns NULL on error (e.g. because an
122image with the same name already exists, or the image
123table is full), or a
124.Ft const struct firmware *
125pointer to the image requested.
126.Pp
127.Fn firmware_unregister
128tries to unregister the firmware image
129.Nm imagename
130from the system. The function is successful and returns 0
131if there are no pending references to the image, otherwise
132it does not unregister the image and returns EBUSY.
133.Pp
134.Fn firmware_get
135returns the requested firmware image.
136If the image is not yet registered with the system,
137the function tries to load it.
138This involves the linker subsystem and disk access, so
139.Fn firmware_get
140must not be called with any locks (except for
141.Va Giant ) .
142Note also that if the firmware image is loaded from a filesystem
143it must already be mounted.
144In particular this means that it may be necessary to defer requests
145from a driver attach method unless it is known the root filesystem is
146already mounted.
147.Pp
148On success,
149.Fn firmware_get
150returns a pointer to the image description and increases the reference count
151for this image. On failure, the function returns NULL.
152.Pp
153.Fn firmware_put
154drops a reference to a firmware image.
155The
156.Fa flags
157argument may be set to
158.Dv FIRMWARE_UNLOAD
159to indicate that
160firmware_put is free to reclaim resources associated with
161the firmware image if this is the last reference.
162By default a firmware image will be deferred to a
163.Xr taskqueue 9
164thread so the call may be done while holding a lock.
165In certain cases, such as on driver detach, this cannot be allowed.
166.Sh FIRMWARE LOADING MECHANISMS
167As mentioned before, any component of the system can register
168firmware images at any time by simply calling
169.Fn firmware_register .
170.Pp
171This is typically done when a module containing
172a firmware image is given control,
173whether compiled in, or preloaded by
174.Nm /boot/loader ,
175or manually loaded with
176.Xr kldload 8 .
177However, a system can implement additional mechanisms to bring
178these images in memory before calling
179.Fn firmware_register .
180.Pp
181When
182.Fn firmware_get
183does not find the requested image, it tries to load it using
184one of the available loading mechanisms.
185At the moment, there is only one, namely
186.Nm Loadable kernel modules :
187.Pp
188A firmware image named
189.Nm foo
190is looked up by trying to load the module named
191.Nm foo.ko ,
192using the facilities described in
193.Xr kld 4 .
194In particular, images are looked up in the directories specified
195by the sysctl variable
196.Nm kern.module_path
197which on most systems defaults to
198.Nm /boot/kernel;/boot/modules .
199.Pp
200Note that in case a module contains multiple images,
201the caller should first request a
202.Fn firmware_get
203for the first image contained in the module, followed by requests
204for the other images.
205.Sh BUILDING FIRMWARE LOADABLE MODULES
206A firmware module is built by embedding the
207.Nm firmware image
208into a suitable loadable kernel module that calls
209.Fn firmware_register
210on loading, and
211.Fn firmware_unregister
212on unloading.
213.Pp
214Various system scripts and makefiles let you build a module
215by simply writing a Makefile with the following entries:
216.Bd -literal
217
218        KMOD=   imagename
219        FIRMWS= image_file:imagename[:version]
220        .include <bsd.kmod.mk>
221
222.Ed
223where KMOD is the basename of the module; FIRMWS is a list of
224colon-separated tuples indicating the image_file's to be embedded
225in the module, the imagename and version of each firmware image.
226.Pp
227If you need to embed firmware images into a system, you should write
228appropriate entries in the <files.arch> file, e.g. this example is
229from
230.Nm sys/arm/xscale/ixp425/files.ixp425 :
231.Bd -literal
232ixp425_npe_fw.c                         optional npe_fw                 \\
233        compile-with    "${AWK} -f $S/tools/fw_stub.awk			\\
234			IxNpeMicrocode.dat:npe_fw -mnpe -c${.TARGET}"	\\
235        no-implicit-rule before-depend local                            \\
236        clean           "ixp425_npe_fw.c"
237#
238# NB: ld encodes the path in the binary symbols generated for the
239#     firmware image so link the file to the object directory to
240#     get known values for reference in the _fw.c file.
241#
242IxNpeMicrocode.fwo  optional npe_fw					\\
243        dependency      "IxNpeMicrocode.dat"				\\
244        compile-with    "${LD} -b binary -d -warn-common		\\
245			    -r -d -o ${.TARGET} IxNpeMicrocode.dat"	\\
246        no-implicit-rule                                                \\
247        clean           "IxNpeMicrocode.fwo"
248IxNpeMicrocode.dat                      optional npe_fw                 \\
249        dependency      ".PHONY"                                        \\
250        compile-with    "uudecode < $S/contrib/dev/npe/IxNpeMicrocode.dat.uu" \\
251        no-obj no-implicit-rule                                         \\
252        clean           "IxNpeMicrocode.dat"
253.Ed
254.Pp
255Note that generating the firmware modules in this way requires
256the availability of the following tools:
257.Xr awk 1 ,
258.Xr make 1 ,
259the compiler and the linker.
260.Sh SEE ALSO
261.Xr kld 4 ,
262.Xr module 9
263.Pp
264.Pa /usr/share/examples/kld/firmware
265.Sh HISTORY
266The
267.Nm firmware
268system was introduced in
269.Fx 6.1 .
270.Sh AUTHORS
271This manual page was written by
272.An Max Laier Aq mlaier@FreeBSD.org .
273