FIRMWARE(9)

πŸ’Ύ FIRMWARE(9) β€” the kernel dev's manual, bestie edition ✨

okay so this is the kernel developer's manual page for FIRMWARE(9) β€” the whole firmware image loading + management situation. if you write kernel code and you've ever been like "where do the firmware blobs even come from 😭", this is your page. let's go 🫢


🌸 NAME 🌸

firmware_register, firmware_unregister, firmware_get, firmware_put β€” firmware image loading and management. that's the whole friend group right there πŸ’–


✨ SYNOPSIS ✨

first, the includes, because you literally cannot do anything without them:

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/linker.h>
#include <sys/firmware.h>

and here's the struct, the moment, the main character herself πŸ‘‘

struct firmware {
        const char      *name;          /* system-wide name */
        const void      *data;          /* location of image */
        size_t          datasize;       /* size of image in bytes */
        unsigned int    version;        /* version of the image */
};

the function signatures, verbatim, no notes:

const struct firmware *
firmware_register(const char *imagename, const void *data,
    size_t datasize, unsigned int version,
    const struct firmware *parent);

int
firmware_unregister(const char *imagename);

const struct firmware *
firmware_get(const char *imagename);

void
firmware_put(const struct firmware *fp, int flags);

πŸ¦‹ DESCRIPTION πŸ¦‹

so the firmware abstraction is basically a convenient interface for loading firmware images into the kernel and for letting kernel components access them. that's the vibe. it's giving "glue layer, but make it elegant".

a firmware image (or just image, for brevity, we're casual here) is an opaque block of data living in kernel memory. it's associated with a unique imagename, which is the search key, and with an integer version number β€” and the version is also just an opaque piece of info for the firmware subsystem. no one's reading it, it's just there for the aesthetic. πŸ’…

an image gets registered with the firmware subsystem by calling firmware_register(), and unregistered by calling firmware_unregister(). these functions are usually β€” but not exclusively β€” called by specially crafted kernel modules that contain the firmware image. those modules can be:

  • statically compiled into the kernel,
  • loaded by /boot/loader,
  • loaded manually at runtime,
  • or loaded on demand by the firmware subsystem.

flexible queen behavior tbh 🌈

clients of the firmware subsystem request access to a given image by calling firmware_get() with the imagename they want as an argument. if a matching image isn't already registered, the firmware subsystem will try to load it using the mechanisms described below β€” typically, a kernel module with firmware_register under the same name as the image. iykyk 🧚


πŸ‘‘ API DESCRIPTION πŸ‘‘

the kernel firmware_register firmware API is made of these functions:

  • firmware_register() β€” registers with the kernel an image of size datasize located at address data, under the name imagename.
  • firmware_unregister() β€” tries to unregister the firmware image imagename from the system. the function is successful and returns 0 if there are no pending references to the image; otherwise it does not unregister the image and returns EBUSY.
  • firmware_get() β€” returns the requested firmware image. if the image isn't yet registered with the system, the function tries to load it. this involves the linker subsystem and disk access, so firmware_get() must not be called with any locks (except for Giant). also note that if the firmware image is loaded from a filesystem, that filesystem must already be mounted. in particular, this means it may be necessary to defer requests from a driver attach method unless it's known that the root filesystem is already mounted.
  • firmware_put() β€” drops a reference to a firmware image.

and the return-value tea β˜•:

firmware_register() returns NULL on error (e.g. because an image with the same name already exists, or the image table is full), or a const struct firmware * pointer to the image requested.

firmware_get() on success returns a pointer to the image description and increases the reference count for this image. on failure, the function returns NULL.

for firmware_put(), the flags argument may be set to FIRMWARE_UNLOAD to indicate that firmware_put is free to reclaim resources associated with the firmware image if this is the last reference. by default a firmware image will be deferred to a taskqueue(9) thread so the call may be done while holding a lock. in certain cases, such as on driver detach, this cannot be allowed. 😀

🚨 bestie listen: no locks except Giant when you call firmware_get(). it touches the linker and the disk. respect the constraint or suffer.

πŸ”₯ FIRMWARE LOADING MECHANISMS πŸ”₯

as mentioned before, any component of the system can register firmware images at any time by simply calling firmware_register(). so empowering honestly πŸ’œ

this is typically done when a module containing a firmware image is given control β€” whether compiled in, or preloaded by /boot/loader, or manually loaded with kldload(8). however, a system can implement additional mechanisms to bring these images into memory before calling firmware_register().

when firmware_get() doesn't find the requested image, it tries to load it using one of the available loading mechanisms. at the moment, there is only one, namely loadable kernel modules:

  • a firmware image named foo is looked up by trying to load the module named foo.ko, using the facilities described in kld(4). in particular, images are looked up in the directories specified by the sysctl variable kern.module_path, which on most systems defaults to /boot/kernel;/boot/modules.
⚠️ heads up: in case a module contains multiple images, the caller should first request a firmware_get() for the first image contained in the module, followed by requests for the other images. order matters, don't freestyle it πŸ™ˆ

πŸŽ€ BUILDING FIRMWARE LOADABLE MODULES πŸŽ€

a firmware module is built by embedding the firmware image into a suitable loadable kernel module that calls firmware_register() on loading, and firmware_unregister() on unloading. iconic lifecycle πŸ’«

various system scripts and makefiles let you build a module by simply writing a Makefile with the following entries:

KMOD=   imagename
FIRMWS= image_file:imagename[:version]
.include <bsd.kmod.mk>

where:

  • KMOD is the basename of the module;
  • FIRMWS is a list of colon-separated tuples indicating the image_file's to be embedded in the module, the imagename and version of each firmware image.

if you need to embed firmware images into a system, you should write appropriate entries in the <files.arch> file, e.g. this example is from sys/arm/xscale/ixp425/files.ixp425:

ixp425_npe_fw.c                         optional npe_fw                 \
        compile-with    "${AWK} -f $S/tools/fw_stub.awk                 \
                        IxNpeMicrocode.dat:npe_fw -mnpe -c${.TARGET}"   \
        no-implicit-rule before-depend local                            \
        clean           "ixp425_npe_fw.c"
#
# NB: ld encodes the path in the binary symbols generated for the
#     firmware image so link the file to the object directory to
#     get known values for reference in the _fw.c file.
#
IxNpeMicrocode.fwo  optional npe_fw                                     \
        dependency      "IxNpeMicrocode.dat"                            \
        compile-with    "${LD} -b binary -d -warn-common                \
                            -r -d -o ${.TARGET} IxNpeMicrocode.dat"     \
        no-implicit-rule                                                \
        clean           "IxNpeMicrocode.fwo"
IxNpeMicrocode.dat                      optional npe_fw                 \
        dependency      ".PHONY"                                        \
        compile-with    "uudecode < $S/contrib/dev/npe/IxNpeMicrocode.dat.uu" \
        no-obj no-implicit-rule                                         \
        clean           "IxNpeMicrocode.dat"

note that generating the firmware modules in this way requires the availability of the following tools: awk(1), make(1), the compiler and the linker. the whole squad 🫢


🌈 SEE ALSO 🌈


⭐ HISTORY ⭐

the firmware system was introduced in FreeBSD 6.1. a classic, an elder, we respect her πŸ’…


πŸ’– AUTHORS πŸ’–

this manual page was written by Max Laier <mlaier@FreeBSD.org>. thank you king πŸ‘‘


and that's the page!! this was the Debian flavor of FIRMWARE(9), dated August 2, 2008 β€” so if you're reading this in a group chat from the future, yes, the firmware subsystem is older than you and it's still slaying. go register some images bestie, you got this πŸ¦‹βœ¨πŸ’œ

πŸ“¦ where this page comes from