16aec1278SMax Laier.\" Copyright (c) 2006 Max Laier <mlaier@FreeBSD.org> 26aec1278SMax Laier.\" All rights reserved. 36aec1278SMax Laier.\" 46aec1278SMax Laier.\" Redistribution and use in source and binary forms, with or without 56aec1278SMax Laier.\" modification, are permitted provided that the following conditions 66aec1278SMax Laier.\" are met: 76aec1278SMax Laier.\" 1. Redistributions of source code must retain the above copyright 86aec1278SMax Laier.\" notice, this list of conditions and the following disclaimer. 96aec1278SMax Laier.\" 2. Redistributions in binary form must reproduce the above copyright 106aec1278SMax Laier.\" notice, this list of conditions and the following disclaimer in the 116aec1278SMax Laier.\" documentation and/or other materials provided with the distribution. 126aec1278SMax Laier.\" 136aec1278SMax Laier.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR 146aec1278SMax Laier.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 156aec1278SMax Laier.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 166aec1278SMax Laier.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT, 176aec1278SMax Laier.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 186aec1278SMax Laier.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 196aec1278SMax Laier.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 206aec1278SMax Laier.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 216aec1278SMax Laier.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 226aec1278SMax Laier.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 236aec1278SMax Laier.\" 246aec1278SMax Laier.\" $FreeBSD$ 256aec1278SMax Laier.\" 26*6f65b505SBjoern A. Zeeb.Dd January 27, 2021 276aec1278SMax Laier.Dt FIRMWARE 9 28aa12cea2SUlrich Spörlein.Os 296aec1278SMax Laier.Sh NAME 306aec1278SMax Laier.Nm firmware_register , 316aec1278SMax Laier.Nm firmware_unregister , 326aec1278SMax Laier.Nm firmware_get , 33*6f65b505SBjoern A. Zeeb.Nm firmware_get_flags , 346aec1278SMax Laier.Nm firmware_put 356aec1278SMax Laier.Nd firmware image loading and management 366aec1278SMax Laier.Sh SYNOPSIS 376aec1278SMax Laier.In sys/param.h 38dad5bb64SMax Laier.In sys/systm.h 396aec1278SMax Laier.In sys/linker.h 406aec1278SMax Laier.In sys/firmware.h 416aec1278SMax Laier.Bd -literal 426aec1278SMax Laierstruct firmware { 436aec1278SMax Laier const char *name; /* system-wide name */ 446aec1278SMax Laier const void *data; /* location of image */ 456aec1278SMax Laier size_t datasize; /* size of image in bytes */ 466aec1278SMax Laier unsigned int version; /* version of the image */ 476aec1278SMax Laier}; 486aec1278SMax Laier.Ed 49ddb53abcSLuigi Rizzo.Ft "const struct firmware *" 506aec1278SMax Laier.Fo firmware_register 516aec1278SMax Laier.Fa "const char *imagename" 526aec1278SMax Laier.Fa "const void *data" 536aec1278SMax Laier.Fa "size_t datasize" 546aec1278SMax Laier.Fa "unsigned int version" 55ddb53abcSLuigi Rizzo.Fa "const struct firmware *parent" 566aec1278SMax Laier.Fc 576aec1278SMax Laier.Ft int 586aec1278SMax Laier.Fn firmware_unregister "const char *imagename" 59ddb53abcSLuigi Rizzo.Ft "const struct firmware *" 606aec1278SMax Laier.Fn firmware_get "const char *imagename" 61*6f65b505SBjoern A. Zeeb.Ft "const struct firmware *" 62*6f65b505SBjoern A. Zeeb.Fn firmware_get_flags "const char *imagename" "uint32_t flags" 636aec1278SMax Laier.Ft void 64ddb53abcSLuigi Rizzo.Fn firmware_put "const struct firmware *fp" "int flags" 656aec1278SMax Laier.Sh DESCRIPTION 66bd84dd2fSRuslan ErmilovThe 67bd84dd2fSRuslan Ermilov.Nm firmware 6897070dd5SLuigi Rizzoabstraction provides a convenient interface for loading 6997070dd5SLuigi Rizzo.Nm firmware images 7097070dd5SLuigi Rizzointo the kernel, and for accessing such images from kernel components. 716aec1278SMax Laier.Pp 7297070dd5SLuigi RizzoA 7397070dd5SLuigi Rizzo.Nm firmware image 7497070dd5SLuigi Rizzo(or 7597070dd5SLuigi Rizzo.Nm image 7697070dd5SLuigi Rizzofor brevity) 7797070dd5SLuigi Rizzois an opaque block of data residing in kernel memory. 7897070dd5SLuigi RizzoIt is associated to a unique 7997070dd5SLuigi Rizzo.Nm imagename 8097070dd5SLuigi Rizzowhich constitutes a search key, and to an integer 8197070dd5SLuigi Rizzo.Nm version 8297070dd5SLuigi Rizzonumber, which is also an opaque piece of information for the 8397070dd5SLuigi Rizzofirmware subsystem. 8497070dd5SLuigi Rizzo.Pp 8597070dd5SLuigi RizzoAn image is registered with the 86bd84dd2fSRuslan Ermilov.Nm firmware 8797070dd5SLuigi Rizzosubsystem by calling the function 8897070dd5SLuigi Rizzo.Fn firmware_register , 8997070dd5SLuigi Rizzoand unregistered by calling 9097070dd5SLuigi Rizzo.Fn firmware_unregister . 91ddb53abcSLuigi RizzoThese functions are usually (but not exclusively) called by 9297070dd5SLuigi Rizzospecially crafted kernel modules that contain the firmware image. 9397070dd5SLuigi RizzoThe modules can be statically compiled in the kernel, or loaded by 9497070dd5SLuigi Rizzo.Nm /boot/loader , 9597070dd5SLuigi Rizzomanually at runtime, or on demand by the firmware subsystem. 966aec1278SMax Laier.Pp 9797070dd5SLuigi Rizzo.Nm Clients 9897070dd5SLuigi Rizzoof the firmware subsystem can request access to a given image 9997070dd5SLuigi Rizzoby calling the function 10097070dd5SLuigi Rizzo.Fn firmware_get 10197070dd5SLuigi Rizzowith the 10297070dd5SLuigi Rizzo.Nm imagename 103*6f65b505SBjoern A. Zeebthey want as an argument, or by calling 104*6f65b505SBjoern A. Zeeb.Fn firmware_get_flags 105*6f65b505SBjoern A. Zeebwith the 106*6f65b505SBjoern A. Zeeb.Nm imagename 107*6f65b505SBjoern A. Zeeband 108*6f65b505SBjoern A. Zeeb.Nm flags 109*6f65b505SBjoern A. Zeebthey want as an arguments. 1107851d429SEdward Tomasz NapieralaIf a matching image is not already registered, 11197070dd5SLuigi Rizzothe firmware subsystem will try to load it using the 11297070dd5SLuigi Rizzomechanisms specified below (typically, a kernel module 11397070dd5SLuigi Rizzowith 11473bbeaa5SGlen Barber.Nm 11573bbeaa5SGlen Barberthe same name 11697070dd5SLuigi Rizzoas the image). 11797070dd5SLuigi Rizzo.Sh API DESCRIPTION 11897070dd5SLuigi RizzoThe kernel 11973bbeaa5SGlen Barber.Nm 12073bbeaa5SGlen Barberfirmware API 12197070dd5SLuigi Rizzois made of the following functions: 12297070dd5SLuigi Rizzo.Pp 12397070dd5SLuigi Rizzo.Fn firmware_register 12497070dd5SLuigi Rizzoregisters with the kernel an image of size 12597070dd5SLuigi Rizzo.Nm datasize 12697070dd5SLuigi Rizzolocated at address 12797070dd5SLuigi Rizzo.Nm data , 12897070dd5SLuigi Rizzounder the name 12997070dd5SLuigi Rizzo.Nm imagename . 13097070dd5SLuigi Rizzo.Pp 13197070dd5SLuigi RizzoThe function returns NULL on error (e.g. because an 13297070dd5SLuigi Rizzoimage with the same name already exists, or the image 13397070dd5SLuigi Rizzotable is full), or a 13497070dd5SLuigi Rizzo.Ft const struct firmware * 13597070dd5SLuigi Rizzopointer to the image requested. 13697070dd5SLuigi Rizzo.Pp 1376aec1278SMax Laier.Fn firmware_unregister 13897070dd5SLuigi Rizzotries to unregister the firmware image 13997070dd5SLuigi Rizzo.Nm imagename 1407851d429SEdward Tomasz Napieralafrom the system. 1417851d429SEdward Tomasz NapieralaThe function is successful and returns 0 14297070dd5SLuigi Rizzoif there are no pending references to the image, otherwise 14397070dd5SLuigi Rizzoit does not unregister the image and returns EBUSY. 1446aec1278SMax Laier.Pp 1456aec1278SMax Laier.Fn firmware_get 146*6f65b505SBjoern A. Zeeband 147*6f65b505SBjoern A. Zeeb.Fn firmware_get_flags 148*6f65b505SBjoern A. Zeebreturn the requested firmware image. 149*6f65b505SBjoern A. ZeebThe 150*6f65b505SBjoern A. Zeeb.Fa flags 151*6f65b505SBjoern A. Zeebargument may be set to 152*6f65b505SBjoern A. Zeeb.Dv FIRMWARE_GET_NOWARN 153*6f65b505SBjoern A. Zeebto indicate that errors on firmware load or registration should 154*6f65b505SBjoern A. Zeebonly be logged in case of 155*6f65b505SBjoern A. Zeeb.Nm booverbose . 156bd84dd2fSRuslan ErmilovIf the image is not yet registered with the system, 157*6f65b505SBjoern A. Zeebthe functions try to load it. 15897070dd5SLuigi RizzoThis involves the linker subsystem and disk access, so 1596aec1278SMax Laier.Fn firmware_get 160*6f65b505SBjoern A. Zeebor 161*6f65b505SBjoern A. Zeeb.Fn firmware_get_flags 162bd84dd2fSRuslan Ermilovmust not be called with any locks (except for 163bd84dd2fSRuslan Ermilov.Va Giant ) . 1646e0186d5SSam LefflerNote also that if the firmware image is loaded from a filesystem 1656e0186d5SSam Lefflerit must already be mounted. 1666e0186d5SSam LefflerIn particular this means that it may be necessary to defer requests 1676e0186d5SSam Lefflerfrom a driver attach method unless it is known the root filesystem is 1686e0186d5SSam Leffleralready mounted. 16997070dd5SLuigi Rizzo.Pp 170bd84dd2fSRuslan ErmilovOn success, 1716aec1278SMax Laier.Fn firmware_get 172*6f65b505SBjoern A. Zeeband 173*6f65b505SBjoern A. Zeeb.Fn firmware_get_flags 174*6f65b505SBjoern A. Zeebreturn a pointer to the image description and increase the reference count 1757851d429SEdward Tomasz Napieralafor this image. 176*6f65b505SBjoern A. ZeebOn failure, the functions return NULL. 1776aec1278SMax Laier.Pp 1786aec1278SMax Laier.Fn firmware_put 17997070dd5SLuigi Rizzodrops a reference to a firmware image. 180bd84dd2fSRuslan ErmilovThe 181bd84dd2fSRuslan Ermilov.Fa flags 182bd84dd2fSRuslan Ermilovargument may be set to 1836aec1278SMax Laier.Dv FIRMWARE_UNLOAD 18497070dd5SLuigi Rizzoto indicate that 18597070dd5SLuigi Rizzofirmware_put is free to reclaim resources associated with 18697070dd5SLuigi Rizzothe firmware image if this is the last reference. 1876e0186d5SSam LefflerBy default a firmware image will be deferred to a 1886e0186d5SSam Leffler.Xr taskqueue 9 1896e0186d5SSam Lefflerthread so the call may be done while holding a lock. 1906e0186d5SSam LefflerIn certain cases, such as on driver detach, this cannot be allowed. 19197070dd5SLuigi Rizzo.Sh FIRMWARE LOADING MECHANISMS 19297070dd5SLuigi RizzoAs mentioned before, any component of the system can register 19397070dd5SLuigi Rizzofirmware images at any time by simply calling 19497070dd5SLuigi Rizzo.Fn firmware_register . 1956aec1278SMax Laier.Pp 19697070dd5SLuigi RizzoThis is typically done when a module containing 19797070dd5SLuigi Rizzoa firmware image is given control, 19897070dd5SLuigi Rizzowhether compiled in, or preloaded by 19997070dd5SLuigi Rizzo.Nm /boot/loader , 20097070dd5SLuigi Rizzoor manually loaded with 20197070dd5SLuigi Rizzo.Xr kldload 8 . 20297070dd5SLuigi RizzoHowever, a system can implement additional mechanisms to bring 20397070dd5SLuigi Rizzothese images in memory before calling 20497070dd5SLuigi Rizzo.Fn firmware_register . 20597070dd5SLuigi Rizzo.Pp 20697070dd5SLuigi RizzoWhen 20797070dd5SLuigi Rizzo.Fn firmware_get 208*6f65b505SBjoern A. Zeebor 209*6f65b505SBjoern A. Zeeb.Fn firmware_get_flags 21097070dd5SLuigi Rizzodoes not find the requested image, it tries to load it using 21197070dd5SLuigi Rizzoone of the available loading mechanisms. 21297070dd5SLuigi RizzoAt the moment, there is only one, namely 213*6f65b505SBjoern A. Zeeb.Nm Loadable kernel modules . 21497070dd5SLuigi Rizzo.Pp 21597070dd5SLuigi RizzoA firmware image named 21697070dd5SLuigi Rizzo.Nm foo 21797070dd5SLuigi Rizzois looked up by trying to load the module named 21897070dd5SLuigi Rizzo.Nm foo.ko , 21997070dd5SLuigi Rizzousing the facilities described in 22097070dd5SLuigi Rizzo.Xr kld 4 . 22197070dd5SLuigi RizzoIn particular, images are looked up in the directories specified 22297070dd5SLuigi Rizzoby the sysctl variable 22397070dd5SLuigi Rizzo.Nm kern.module_path 22497070dd5SLuigi Rizzowhich on most systems defaults to 22597070dd5SLuigi Rizzo.Nm /boot/kernel;/boot/modules . 22697070dd5SLuigi Rizzo.Pp 22797070dd5SLuigi RizzoNote that in case a module contains multiple images, 22897070dd5SLuigi Rizzothe caller should first request a 22997070dd5SLuigi Rizzo.Fn firmware_get 230*6f65b505SBjoern A. Zeebor 231*6f65b505SBjoern A. Zeeb.Fn firmware_get_flags 23297070dd5SLuigi Rizzofor the first image contained in the module, followed by requests 23397070dd5SLuigi Rizzofor the other images. 23497070dd5SLuigi Rizzo.Sh BUILDING FIRMWARE LOADABLE MODULES 23597070dd5SLuigi RizzoA firmware module is built by embedding the 23697070dd5SLuigi Rizzo.Nm firmware image 23797070dd5SLuigi Rizzointo a suitable loadable kernel module that calls 23897070dd5SLuigi Rizzo.Fn firmware_register 23997070dd5SLuigi Rizzoon loading, and 24097070dd5SLuigi Rizzo.Fn firmware_unregister 24197070dd5SLuigi Rizzoon unloading. 24297070dd5SLuigi Rizzo.Pp 24397070dd5SLuigi RizzoVarious system scripts and makefiles let you build a module 24497070dd5SLuigi Rizzoby simply writing a Makefile with the following entries: 24597070dd5SLuigi Rizzo.Bd -literal 24697070dd5SLuigi Rizzo 24797070dd5SLuigi Rizzo KMOD= imagename 24897070dd5SLuigi Rizzo FIRMWS= image_file:imagename[:version] 24997070dd5SLuigi Rizzo .include <bsd.kmod.mk> 25097070dd5SLuigi Rizzo 25197070dd5SLuigi Rizzo.Ed 25297070dd5SLuigi Rizzowhere KMOD is the basename of the module; FIRMWS is a list of 25397070dd5SLuigi Rizzocolon-separated tuples indicating the image_file's to be embedded 25497070dd5SLuigi Rizzoin the module, the imagename and version of each firmware image. 25597070dd5SLuigi Rizzo.Pp 25697070dd5SLuigi RizzoIf you need to embed firmware images into a system, you should write 25797070dd5SLuigi Rizzoappropriate entries in the <files.arch> file, e.g. this example is 25897070dd5SLuigi Rizzofrom 25997070dd5SLuigi Rizzo.Nm sys/arm/xscale/ixp425/files.ixp425 : 26097070dd5SLuigi Rizzo.Bd -literal 26197070dd5SLuigi Rizzoixp425_npe_fw.c optional npe_fw \\ 26297070dd5SLuigi Rizzo compile-with "${AWK} -f $S/tools/fw_stub.awk \\ 26397070dd5SLuigi Rizzo IxNpeMicrocode.dat:npe_fw -mnpe -c${.TARGET}" \\ 26497070dd5SLuigi Rizzo no-implicit-rule before-depend local \\ 26597070dd5SLuigi Rizzo clean "ixp425_npe_fw.c" 26697070dd5SLuigi Rizzo# 26797070dd5SLuigi Rizzo# NB: ld encodes the path in the binary symbols generated for the 26897070dd5SLuigi Rizzo# firmware image so link the file to the object directory to 26997070dd5SLuigi Rizzo# get known values for reference in the _fw.c file. 27097070dd5SLuigi Rizzo# 27197070dd5SLuigi RizzoIxNpeMicrocode.fwo optional npe_fw \\ 27297070dd5SLuigi Rizzo dependency "IxNpeMicrocode.dat" \\ 27397070dd5SLuigi Rizzo compile-with "${LD} -b binary -d -warn-common \\ 27497070dd5SLuigi Rizzo -r -d -o ${.TARGET} IxNpeMicrocode.dat" \\ 27597070dd5SLuigi Rizzo no-implicit-rule \\ 27697070dd5SLuigi Rizzo clean "IxNpeMicrocode.fwo" 27797070dd5SLuigi Rizzo.Ed 27897070dd5SLuigi Rizzo.Pp 27922ae5464SEd MasteFirmware was previously committed to the source tree as uuencoded files, 28022ae5464SEd Mastebut this is no longer required; the binary firmware file should be committed 28122ae5464SEd Masteto the tree as provided by the vendor. 28222ae5464SEd Maste.Pp 28397070dd5SLuigi RizzoNote that generating the firmware modules in this way requires 28497070dd5SLuigi Rizzothe availability of the following tools: 2852a6b4327SJohn-Mark Gurney.Xr awk 1 , 2862a6b4327SJohn-Mark Gurney.Xr make 1 , 28797070dd5SLuigi Rizzothe compiler and the linker. 28897070dd5SLuigi Rizzo.Sh SEE ALSO 289030c03b5SSergey Kandaurov.Xr kld 4 , 290030c03b5SSergey Kandaurov.Xr module 9 29197070dd5SLuigi Rizzo.Pp 29297070dd5SLuigi Rizzo.Pa /usr/share/examples/kld/firmware 2936aec1278SMax Laier.Sh HISTORY 294bd84dd2fSRuslan ErmilovThe 295bd84dd2fSRuslan Ermilov.Nm firmware 296bd84dd2fSRuslan Ermilovsystem was introduced in 297999b97c5SHiroki Sato.Fx 6.1 . 2986aec1278SMax Laier.Sh AUTHORS 2996aec1278SMax LaierThis manual page was written by 3008a7314fcSBaptiste Daroussin.An Max Laier Aq Mt mlaier@FreeBSD.org . 301