FIRMWARE(9)

πŸ“¦
version 15.1 Β· 2 versions
, in freebsd 15.1, nixpkgs unstable

FIRMWARE(9) β€” Kernel Developer's Manual.

okay bestie, you clicked into the firmware image loading and management page, and honestly? it's giving kernel magic βœ¨πŸ’…

🌸 NAME 🌸

firmware_register, firmware_unregister, firmware_get, firmware_get_flags, firmware_put β€” the whole firmware image loading and management crew. They handle loading firmware images and managing them. πŸ’–

✨ SYNOPSIS ✨

Here's the incantation, bestie β€” copy it exact, no freestyling:

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

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 */
};

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);

const struct firmware *
firmware_get_flags(const char *imagename, uint32_t flags);

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

πŸ¦‹ DESCRIPTION πŸ¦‹

The firmware abstraction is like the VIP connector for getting firmware images into the kernel and letting kernel components access them. Convenient? Obsessed. πŸ’…

A firmware image β€” or just image, for brevity β€” is an opaque block of data chilling in kernel memory. It's tied to a unique imagename, which is the search key, and to an integer version number, which is also an opaque piece of info for the firmware subsystem. No cap.

An image gets registered with the firmware subsystem by calling firmware_register(), and unregistered by calling firmware_unregister(). These are usually β€” but not exclusively β€” called by specially crafted kernel modules that contain the firmware image. Those modules can be statically compiled in the kernel, or loaded by /boot/loader, manually at runtime, or on demand by the firmware subsystem. main character module energy, honestly.

Firmware binary files may also be loaded directly rather than embedded into kernel modules. slay.

Clients of the firmware subsystem can request access to a given image by calling firmware_get() with the imagename they want as an argument, or by calling firmware_get_flags() with the imagename and flags they want as arguments. If a matching image isn't already registered, the firmware subsystem will try to load it using the mechanisms specified below β€” typically, a kernel module with firmware_register the same name as the image. 🫢

πŸ”₯ API DESCRIPTION πŸ”₯

The kernel firmware_register firmware API is made of these functions, bestie:

  • firmware_register() β€” registers with the kernel an image of size datasize located at address data, under the name imagename.

The function 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_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. rude, but fair.
  • firmware_get() and firmware_get_flags() β€” return the requested firmware image. The flags argument may be set to FIRMWARE_GET_NOWARN to indicate that errors on firmware load or registration should only be logged in case of booverbose. If the image is not yet registered with the system, the functions try to load it.
🚨 bestie, lock warning: this involves the linker subsystem and disk access, so firmware_get() or firmware_get_flags() must not be called with any locks (except for Giant). Also note that if the firmware image is loaded from a filesystem it must already be mounted. In particular this means that it may be necessary to defer requests from a driver attach method unless it is known the root filesystem is already mounted. iykyk.

On success, firmware_get() and firmware_get_flags() return a pointer to the image description and increase the reference count for this image. On failure, the functions return NULL. 😭

  • firmware_put() β€” drops a reference to a firmware image. 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. periodt πŸ’…

🌈 FIRMWARE LOADING VIA MODULES 🌈

As mentioned before, any component of the system can register firmware images at any time by simply calling firmware_register(). iconic behavior.

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() or firmware_get_flags() does not 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. one mechanism, but make it fashion.

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. πŸŽ€

Note that in case a module contains multiple images, the caller should first request a firmware_get() or firmware_get_flags() for the first image contained in the module, followed by requests for the other images. order matters, bestie.

πŸŽ€ 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. ate πŸ’–

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> or <files> file, e.g. this example is from sys/conf/files:

       iwn1000fw.c                     optional iwn1000fw | iwnfw              \
               compile-with    "${AWK} -f $S/tools/fw_stub.awk iwn1000.fw:iwn1000fw -miwn1000fw -c${.TARGET}" \
               no-ctfconvert no-implicit-rule before-depend local              \
               clean           "iwn1000fw.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.
       #
       iwn1000fw.fwo                   optional iwn1000fw | iwnfw              \
               dependency      "iwn1000.fw"                                    \
               compile-with    "${NORMAL_FWO}"                                 \
               no-implicit-rule                                                \
               clean           "iwn1000fw.fwo"

Firmware was previously committed to the source tree as uuencoded files, but this is no longer required; the binary firmware file should be committed to the tree as provided by the vendor. vendor-provided and unbothered ✨

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. πŸ”§

πŸ‘‘ LOADING BINARY FIRMWARE FILES πŸ‘‘

πŸ“ Binary Firmware Format

Binary firmware files can also be loaded, either from /boot/loader, or when firmware_get cannot find the registered firmware from a kernel module. Binary firmware files are raw binary files that the creator of the firmware made. They offer an easier way to load firmware, but one that lacks the full flexibility and generality of kernel modules with the following restrictions: read the fine print, bestie πŸ‘€

  • Binary firmware files only hold one set of firmware.
  • They do not offer kernel module dependencies to ensure they are loaded automatically by the boot loader.
  • They cannot be compiled into the kernel.
  • The imagename is identical to the full path name used to load the module.
  • The version number is assumed to be zero.

πŸŒ€ Loading from /boot/loader

Binary firmware files may be loaded either from the command line with load -t firmware /boot/firmware/filename or using the loader.conf(5) mechanism to load modules with a type of firmware. For example:

wififw_load="YES"
wififw_name="/boot/firmware/wifi2034_fw.bin"
wififw_type="firmware"

πŸ’œ On demand loading from firmware_get

If no kernel module with an embedded firmware image named imagename is loaded, then imagename will be appended to the module path (by default /boot/firmware/) and if that file exists, it will be loaded and registered using firmware_register using the full path to the filename as imagename. on-demand? we love convenience πŸ’…

⭐ Searching for imagename

firmware_get uses the following algorithm to find firmware images:

  • If an existing firmware image is registered for imagename, that image is returned.
  • If imagename matches the trailing subpath of a registered image with a full path, that image is returned.
  • The kernel linker searches for a kernel module named imagename. If a kernel module is found, it is loaded, and the list of registered firmware images is searched again. If a match is found, the matching image is returned.
  • The kernel searches for a file named imagename in the firmware image path (by default /boot/firmware/). If that file exists and can be read, its contents are registered as a firmware image with the full path as the imagename and that firmware is returned. Currently, there is an 8MB limit on the size of the firmware image. This can be changed by the sysctl variable debug.max_firmware_size. 8MB is the vibe check πŸ”₯

πŸ‘€ SEE ALSO πŸ‘€

πŸ’« HISTORY πŸ’«

The firmware system was introduced in FreeBSD 6.1. Binary firmware loading was introduced in FreeBSD 15.0. generational glow-up ✨

🫢 AUTHORS 🫢

This manual page was written by Max Laier <mlaier@FreeBSD.org>. respect the author, bestie πŸ’–


xoxo from Debian, January 25, 2024 πŸ’–πŸ¦‹

πŸ“¦ where this page comes from