In Linux, firmware refers to "firmware", which is a program executed by the hardware device itself and is generally stored in the device flash. In a Linux system, the device driver is in the kernel state, while the firmware file is in the user state. Therefore, a safe, stable and reliable mechanism is needed to ensure that the device driver successfully loads the firmware file.
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
What is linux firmware
Firmware (firmware) is a program executed by the hardware device itself. Firmware is generally stored in the device flash. For cost and convenience reasons, the running program of the hardware device is usually packaged into a firmware file in a specific format, stored in the terminal system, and the hardware device is upgraded through the terminal system.
During the Linux kernel development process, developers debug peripheral driver devices, such as touch, charging, linear motors, storage, WIFI devices, etc. There are also situations where firmware needs to be updated. In a Linux system, the device driver is in the kernel state, while the firmware file is in the user state. Therefore, a safe, stable and reliable mechanism is needed to ensure that the device driver successfully loads the firmware file.
In order to solve the problem of device drivers stably loading user-mode firmware files from the kernel mode, the Linux system provides a firmware subsystem.
Linux firmware subsystem process introduction
The Linux firmware subsystem is implemented based on the sysfs and uevent mechanisms.
After the driver calls the firmware system function interface to apply for firmware, the firmware subsystem uses the firmware compilation kernel to obtain the firmware; if the acquisition fails, it uses the firmware cache to obtain the firmware; if it still fails to obtain, Use the default path kernel to directly search to obtain the firmware. If the acquisition still fails, report the uevent message to the init process. The init process receives the uevent message and filters out the messages whose subsystem type is firmware. The init process searches for the firmware based on the firmware information pointed to in the uevent message, and writes the obtained firmware content from the user state to the kernel state through the file node interface provided by sysfs, so that the driver can obtain the data of the firmware file.
Linux firmware system provides a variety of methods to obtain firmware files in different scenarios.
1) The method of directly compiling into the kernel;
2) The method of firmware caching;
3) The method of directly specifying the path according to the kernel:
4) Assist processing through the init process;
Linux firmware subsystem flow chart
Linux firmware subsystem main function interface
Main function interface:
The main types of application firmware interfaces are divided into synchronous and asynchronous.
Usually the process of applying for firmware is time-consuming, and the process of processing firmware upgrade is time-consuming, so it can be implemented using an asynchronous function interface, or first create a work queue in the driver and call the synchronous function interface to implement it.
Among them:
The kernel applies for firmware files by calling the request_firmware function.
After the kernel obtains the firmware file, it calls release_firmware to release the related memory.
Among them:
The request_firmware_direct interface only searches for firmware in the path specified by the kernel and does not use uevent Mechanism to obtain firmware.
The request_firmware_nowait interface obtains firmware through an asynchronous work queue, which can not block the driver probe time.
Linux firmware subsystem implementation process
request_firmware implementation Process
The request_firmware function sets different flag bits by calling the _request_firmware_prepare function to achieve different differential functions.
_request_firmware_prepare function:
After turning on the CONFIG_FW_LOADER macro switch, first determine whether the firmware file is compiled into the kernel by calling the fw_get_builtin_firmware function .
Then call the fw_lookup_and_allocate_buf function to determine whether the linked list in the global fw_cache structure has recorded the name of the currently requested firmware. If the name of the currently requested firmware does not exist, the corresponding memory space is dynamically allocated and the name of the currently requested firmware is added to the linked list in the global fw_cache structure.
fw_get_filesystem_firmware function
Mainly searches for firmware files through the default path provided by the kernel and calls the kernel_read_file_from_path function. If the firmware file is not found, the flag bit FW_OPT_USERHELPER is used to determine whether the USER_HELPER mode is enabled.
Among them:
The default path in the Firmware system is as follows:
The default path can be added through the kernel command line. Pass the module_param_string interface to the variable path to customize the new path.
USER_HELPER mode
This function is only supported after the kernel turns on CONFIG_FW_LOADER_USER_HELPER. The main function is to report uevent messages to the init process through the kernel, obtain firmware information through the init process, and write it to the underlying sysfs node.
fw_load_from_user_helper function:
First call the fw_create_instance function to create the device device, class file and attribute file, and allocate the firmware_priv structure.
Next, a directory will be created under /sys/class/firmware, which uses the device name as its directory name.
This directory contains three attributes:
loading:
is set to 1: This attribute is started by setting 1 in the user space responsible for loading firmware;
is set to 0: when the loading process is completed;
is set to -1: the firmware loading process will be terminated.
#data:
is used to receive firmware data. After loading is set, the user space process writes the firmware to this attribute.
device: The symbolic link of the corresponding entry under
/sys/devices.
timeout:
The default maximum timeout for applying firmware through uevent is 60S, and the upper layer write timeout is supported.
_request_firmware_load function:
First disable uevent reporting, add the device by calling the device_add function, and trigger the calling of the firmware_uevent function. Among them, fill in the information format reported by uevent, including the name of the firmware, timeout period, and whether it is asynchronous.
The next step is to enable the uevent reporting function, call the kobject_uevent function at the same time, and report the add action type to the upper layer ueventd.
Then call the fw_state_wait_timeout function and wait for the processing of the upper layer ueventd within the preset timeout period.
If the timeout reaches or wakes up after receiving the completion amount, the previously applied memory will be released, and memory information such as device and class will be released.
ueventd related firmware processing process
Ueventd is an important module in the init process. It mainly handles selinux, dev device creation, monitoring kernel reporting of uevent messages, and firmware loading. etc. content.
FirmwareHandler processing flow:
The HandleUevent method in FirmwareHandler mainly handles the interaction process between firmware loading and underlying nodes.
First determine whether the subsystem type of the uevent message is the firmware field before processing. This type will only be reported by the firmware module in the kernel.
HandleUevent mainly creates different sub-threads through a main thread, and processes firmware requests from different drivers from the kernel in parallel.
ProcessFirmwareEvent function
First, it loops to determine whether the firmware file retrieved in the path supported by ueventd exists; if it exists, write the underlying loading attribute file as 1 , and at the same time copy the obtained firmware file and write it to the underlying data file. After completion, 0 is written to the underlying loading attribute file.
At this point, the kernel has obtained the firmware file information written by the user space.
Among them:
ueventd supports the path to search for firmware by default:
from the firmware_directory specified in the ueventd.rc file.
Related recommendations: "Linux Video Tutorial"
The above is the detailed content of what is linux firmware. For more information, please follow other related articles on the PHP Chinese website!