Back to home page

OSCL-LXR

 
 

    


0001 ===================
0002 Fallback mechanisms
0003 ===================
0004 
0005 A fallback mechanism is supported to allow to overcome failures to do a direct
0006 filesystem lookup on the root filesystem or when the firmware simply cannot be
0007 installed for practical reasons on the root filesystem. The kernel
0008 configuration options related to supporting the firmware fallback mechanism are:
0009 
0010   * CONFIG_FW_LOADER_USER_HELPER: enables building the firmware fallback
0011     mechanism. Most distributions enable this option today. If enabled but
0012     CONFIG_FW_LOADER_USER_HELPER_FALLBACK is disabled, only the custom fallback
0013     mechanism is available and for the request_firmware_nowait() call.
0014   * CONFIG_FW_LOADER_USER_HELPER_FALLBACK: force enables each request to
0015     enable the kobject uevent fallback mechanism on all firmware API calls
0016     except request_firmware_direct(). Most distributions disable this option
0017     today. The call request_firmware_nowait() allows for one alternative
0018     fallback mechanism: if this kconfig option is enabled and your second
0019     argument to request_firmware_nowait(), uevent, is set to false you are
0020     informing the kernel that you have a custom fallback mechanism and it will
0021     manually load the firmware. Read below for more details.
0022 
0023 Note that this means when having this configuration:
0024 
0025 CONFIG_FW_LOADER_USER_HELPER=y
0026 CONFIG_FW_LOADER_USER_HELPER_FALLBACK=n
0027 
0028 the kobject uevent fallback mechanism will never take effect even
0029 for request_firmware_nowait() when uevent is set to true.
0030 
0031 Justifying the firmware fallback mechanism
0032 ==========================================
0033 
0034 Direct filesystem lookups may fail for a variety of reasons. Known reasons for
0035 this are worth itemizing and documenting as it justifies the need for the
0036 fallback mechanism:
0037 
0038 * Race against access with the root filesystem upon bootup.
0039 
0040 * Races upon resume from suspend. This is resolved by the firmware cache, but
0041   the firmware cache is only supported if you use uevents, and its not
0042   supported for request_firmware_into_buf().
0043 
0044 * Firmware is not accessible through typical means:
0045 
0046         * It cannot be installed into the root filesystem
0047         * The firmware provides very unique device specific data tailored for
0048           the unit gathered with local information. An example is calibration
0049           data for WiFi chipsets for mobile devices. This calibration data is
0050           not common to all units, but tailored per unit.  Such information may
0051           be installed on a separate flash partition other than where the root
0052           filesystem is provided.
0053 
0054 Types of fallback mechanisms
0055 ============================
0056 
0057 There are really two fallback mechanisms available using one shared sysfs
0058 interface as a loading facility:
0059 
0060 * Kobject uevent fallback mechanism
0061 * Custom fallback mechanism
0062 
0063 First lets document the shared sysfs loading facility.
0064 
0065 Firmware sysfs loading facility
0066 ===============================
0067 
0068 In order to help device drivers upload firmware using a fallback mechanism
0069 the firmware infrastructure creates a sysfs interface to enable userspace
0070 to load and indicate when firmware is ready. The sysfs directory is created
0071 via fw_create_instance(). This call creates a new struct device named after
0072 the firmware requested, and establishes it in the device hierarchy by
0073 associating the device used to make the request as the device's parent.
0074 The sysfs directory's file attributes are defined and controlled through
0075 the new device's class (firmware_class) and group (fw_dev_attr_groups).
0076 This is actually where the original firmware_class module name came from,
0077 given that originally the only firmware loading mechanism available was the
0078 mechanism we now use as a fallback mechanism, which registers a struct class
0079 firmware_class. Because the attributes exposed are part of the module name, the
0080 module name firmware_class cannot be renamed in the future, to ensure backward
0081 compatibility with old userspace.
0082 
0083 To load firmware using the sysfs interface we expose a loading indicator,
0084 and a file upload firmware into:
0085 
0086   * /sys/$DEVPATH/loading
0087   * /sys/$DEVPATH/data
0088 
0089 To upload firmware you will echo 1 onto the loading file to indicate
0090 you are loading firmware. You then write the firmware into the data file,
0091 and you notify the kernel the firmware is ready by echo'ing 0 onto
0092 the loading file.
0093 
0094 The firmware device used to help load firmware using sysfs is only created if
0095 direct firmware loading fails and if the fallback mechanism is enabled for your
0096 firmware request, this is set up with :c:func:`firmware_fallback_sysfs`. It is
0097 important to re-iterate that no device is created if a direct filesystem lookup
0098 succeeded.
0099 
0100 Using::
0101 
0102         echo 1 > /sys/$DEVPATH/loading
0103 
0104 Will clean any previous partial load at once and make the firmware API
0105 return an error. When loading firmware the firmware_class grows a buffer
0106 for the firmware in PAGE_SIZE increments to hold the image as it comes in.
0107 
0108 firmware_data_read() and firmware_loading_show() are just provided for the
0109 test_firmware driver for testing, they are not called in normal use or
0110 expected to be used regularly by userspace.
0111 
0112 firmware_fallback_sysfs
0113 -----------------------
0114 .. kernel-doc:: drivers/base/firmware_loader/fallback.c
0115    :functions: firmware_fallback_sysfs
0116 
0117 Firmware kobject uevent fallback mechanism
0118 ==========================================
0119 
0120 Since a device is created for the sysfs interface to help load firmware as a
0121 fallback mechanism userspace can be informed of the addition of the device by
0122 relying on kobject uevents. The addition of the device into the device
0123 hierarchy means the fallback mechanism for firmware loading has been initiated.
0124 For details of implementation refer to fw_load_sysfs_fallback(), in particular
0125 on the use of dev_set_uevent_suppress() and kobject_uevent().
0126 
0127 The kernel's kobject uevent mechanism is implemented in lib/kobject_uevent.c,
0128 it issues uevents to userspace. As a supplement to kobject uevents Linux
0129 distributions could also enable CONFIG_UEVENT_HELPER_PATH, which makes use of
0130 core kernel's usermode helper (UMH) functionality to call out to a userspace
0131 helper for kobject uevents. In practice though no standard distribution has
0132 ever used the CONFIG_UEVENT_HELPER_PATH. If CONFIG_UEVENT_HELPER_PATH is
0133 enabled this binary would be called each time kobject_uevent_env() gets called
0134 in the kernel for each kobject uevent triggered.
0135 
0136 Different implementations have been supported in userspace to take advantage of
0137 this fallback mechanism. When firmware loading was only possible using the
0138 sysfs mechanism the userspace component "hotplug" provided the functionality of
0139 monitoring for kobject events. Historically this was superseded be systemd's
0140 udev, however firmware loading support was removed from udev as of systemd
0141 commit be2ea723b1d0 ("udev: remove userspace firmware loading support")
0142 as of v217 on August, 2014. This means most Linux distributions today are
0143 not using or taking advantage of the firmware fallback mechanism provided
0144 by kobject uevents. This is specially exacerbated due to the fact that most
0145 distributions today disable CONFIG_FW_LOADER_USER_HELPER_FALLBACK.
0146 
0147 Refer to do_firmware_uevent() for details of the kobject event variables
0148 setup. The variables currently passed to userspace with a "kobject add"
0149 event are:
0150 
0151 * FIRMWARE=firmware name
0152 * TIMEOUT=timeout value
0153 * ASYNC=whether or not the API request was asynchronous
0154 
0155 By default DEVPATH is set by the internal kernel kobject infrastructure.
0156 Below is an example simple kobject uevent script::
0157 
0158         # Both $DEVPATH and $FIRMWARE are already provided in the environment.
0159         MY_FW_DIR=/lib/firmware/
0160         echo 1 > /sys/$DEVPATH/loading
0161         cat $MY_FW_DIR/$FIRMWARE > /sys/$DEVPATH/data
0162         echo 0 > /sys/$DEVPATH/loading
0163 
0164 Firmware custom fallback mechanism
0165 ==================================
0166 
0167 Users of the request_firmware_nowait() call have yet another option available
0168 at their disposal: rely on the sysfs fallback mechanism but request that no
0169 kobject uevents be issued to userspace. The original logic behind this
0170 was that utilities other than udev might be required to lookup firmware
0171 in non-traditional paths -- paths outside of the listing documented in the
0172 section 'Direct filesystem lookup'. This option is not available to any of
0173 the other API calls as uevents are always forced for them.
0174 
0175 Since uevents are only meaningful if the fallback mechanism is enabled
0176 in your kernel it would seem odd to enable uevents with kernels that do not
0177 have the fallback mechanism enabled in their kernels. Unfortunately we also
0178 rely on the uevent flag which can be disabled by request_firmware_nowait() to
0179 also setup the firmware cache for firmware requests. As documented above,
0180 the firmware cache is only set up if uevent is enabled for an API call.
0181 Although this can disable the firmware cache for request_firmware_nowait()
0182 calls, users of this API should not use it for the purposes of disabling
0183 the cache as that was not the original purpose of the flag. Not setting
0184 the uevent flag means you want to opt-in for the firmware fallback mechanism
0185 but you want to suppress kobject uevents, as you have a custom solution which
0186 will monitor for your device addition into the device hierarchy somehow and
0187 load firmware for you through a custom path.
0188 
0189 Firmware fallback timeout
0190 =========================
0191 
0192 The firmware fallback mechanism has a timeout. If firmware is not loaded
0193 onto the sysfs interface by the timeout value an error is sent to the
0194 driver. By default the timeout is set to 60 seconds if uevents are
0195 desirable, otherwise MAX_JIFFY_OFFSET is used (max timeout possible).
0196 The logic behind using MAX_JIFFY_OFFSET for non-uevents is that a custom
0197 solution will have as much time as it needs to load firmware.
0198 
0199 You can customize the firmware timeout by echo'ing your desired timeout into
0200 the following file:
0201 
0202 * /sys/class/firmware/timeout
0203 
0204 If you echo 0 into it means MAX_JIFFY_OFFSET will be used. The data type
0205 for the timeout is an int.
0206 
0207 EFI embedded firmware fallback mechanism
0208 ========================================
0209 
0210 On some devices the system's EFI code / ROM may contain an embedded copy
0211 of firmware for some of the system's integrated peripheral devices and
0212 the peripheral's Linux device-driver needs to access this firmware.
0213 
0214 Device drivers which need such firmware can use the
0215 firmware_request_platform() function for this, note that this is a
0216 separate fallback mechanism from the other fallback mechanisms and
0217 this does not use the sysfs interface.
0218 
0219 A device driver which needs this can describe the firmware it needs
0220 using an efi_embedded_fw_desc struct:
0221 
0222 .. kernel-doc:: include/linux/efi_embedded_fw.h
0223    :functions: efi_embedded_fw_desc
0224 
0225 The EFI embedded-fw code works by scanning all EFI_BOOT_SERVICES_CODE memory
0226 segments for an eight byte sequence matching prefix; if the prefix is found it
0227 then does a sha256 over length bytes and if that matches makes a copy of length
0228 bytes and adds that to its list with found firmwares.
0229 
0230 To avoid doing this somewhat expensive scan on all systems, dmi matching is
0231 used. Drivers are expected to export a dmi_system_id array, with each entries'
0232 driver_data pointing to an efi_embedded_fw_desc.
0233 
0234 To register this array with the efi-embedded-fw code, a driver needs to:
0235 
0236 1. Always be builtin to the kernel or store the dmi_system_id array in a
0237    separate object file which always gets builtin.
0238 
0239 2. Add an extern declaration for the dmi_system_id array to
0240    include/linux/efi_embedded_fw.h.
0241 
0242 3. Add the dmi_system_id array to the embedded_fw_table in
0243    drivers/firmware/efi/embedded-firmware.c wrapped in a #ifdef testing that
0244    the driver is being builtin.
0245 
0246 4. Add "select EFI_EMBEDDED_FIRMWARE if EFI_STUB" to its Kconfig entry.
0247 
0248 The firmware_request_platform() function will always first try to load firmware
0249 with the specified name directly from the disk, so the EFI embedded-fw can
0250 always be overridden by placing a file under /lib/firmware.
0251 
0252 Note that:
0253 
0254 1. The code scanning for EFI embedded-firmware runs near the end
0255    of start_kernel(), just before calling rest_init(). For normal drivers and
0256    subsystems using subsys_initcall() to register themselves this does not
0257    matter. This means that code running earlier cannot use EFI
0258    embedded-firmware.
0259 
0260 2. At the moment the EFI embedded-fw code assumes that firmwares always start at
0261    an offset which is a multiple of 8 bytes, if this is not true for your case
0262    send in a patch to fix this.
0263 
0264 3. At the moment the EFI embedded-fw code only works on x86 because other archs
0265    free EFI_BOOT_SERVICES_CODE before the EFI embedded-fw code gets a chance to
0266    scan it.
0267 
0268 4. The current brute-force scanning of EFI_BOOT_SERVICES_CODE is an ad-hoc
0269    brute-force solution. There has been discussion to use the UEFI Platform
0270    Initialization (PI) spec's Firmware Volume protocol. This has been rejected
0271    because the FV Protocol relies on *internal* interfaces of the PI spec, and:
0272    1. The PI spec does not define peripheral firmware at all
0273    2. The internal interfaces of the PI spec do not guarantee any backward
0274    compatibility. Any implementation details in FV may be subject to change,
0275    and may vary system to system. Supporting the FV Protocol would be
0276    difficult as it is purposely ambiguous.
0277 
0278 Example how to check for and extract embedded firmware
0279 ------------------------------------------------------
0280 
0281 To check for, for example Silead touchscreen controller embedded firmware,
0282 do the following:
0283 
0284 1. Boot the system with efi=debug on the kernel commandline
0285 
0286 2. cp /sys/kernel/debug/efi/boot_services_code? to your home dir
0287 
0288 3. Open the boot_services_code? files in a hex-editor, search for the
0289    magic prefix for Silead firmware: F0 00 00 00 02 00 00 00, this gives you
0290    the beginning address of the firmware inside the boot_services_code? file.
0291 
0292 4. The firmware has a specific pattern, it starts with a 8 byte page-address,
0293    typically F0 00 00 00 02 00 00 00 for the first page followed by 32-bit
0294    word-address + 32-bit value pairs. With the word-address incrementing 4
0295    bytes (1 word) for each pair until a page is complete. A complete page is
0296    followed by a new page-address, followed by more word + value pairs. This
0297    leads to a very distinct pattern. Scroll down until this pattern stops,
0298    this gives you the end of the firmware inside the boot_services_code? file.
0299 
0300 5. "dd if=boot_services_code? of=firmware bs=1 skip=<begin-addr> count=<len>"
0301    will extract the firmware for you. Inspect the firmware file in a
0302    hexeditor to make sure you got the dd parameters correct.
0303 
0304 6. Copy it to /lib/firmware under the expected name to test it.
0305 
0306 7. If the extracted firmware works, you can use the found info to fill an
0307    efi_embedded_fw_desc struct to describe it, run "sha256sum firmware"
0308    to get the sha256sum to put in the sha256 field.