fallback-mechanisms.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. ===================
  2. Fallback mechanisms
  3. ===================
  4. A fallback mechanism is supported to allow to overcome failures to do a direct
  5. filesystem lookup on the root filesystem or when the firmware simply cannot be
  6. installed for practical reasons on the root filesystem. The kernel
  7. configuration options related to supporting the firmware fallback mechanism are:
  8. * CONFIG_FW_LOADER_USER_HELPER: enables building the firmware fallback
  9. mechanism. Most distributions enable this option today. If enabled but
  10. CONFIG_FW_LOADER_USER_HELPER_FALLBACK is disabled, only the custom fallback
  11. mechanism is available and for the request_firmware_nowait() call.
  12. * CONFIG_FW_LOADER_USER_HELPER_FALLBACK: force enables each request to
  13. enable the kobject uevent fallback mechanism on all firmware API calls
  14. except request_firmware_direct(). Most distributions disable this option
  15. today. The call request_firmware_nowait() allows for one alternative
  16. fallback mechanism: if this kconfig option is enabled and your second
  17. argument to request_firmware_nowait(), uevent, is set to false you are
  18. informing the kernel that you have a custom fallback mechanism and it will
  19. manually load the firmware. Read below for more details.
  20. Note that this means when having this configuration:
  21. CONFIG_FW_LOADER_USER_HELPER=y
  22. CONFIG_FW_LOADER_USER_HELPER_FALLBACK=n
  23. the kobject uevent fallback mechanism will never take effect even
  24. for request_firmware_nowait() when uevent is set to true.
  25. Justifying the firmware fallback mechanism
  26. ==========================================
  27. Direct filesystem lookups may fail for a variety of reasons. Known reasons for
  28. this are worth itemizing and documenting as it justifies the need for the
  29. fallback mechanism:
  30. * Race against access with the root filesystem upon bootup.
  31. * Races upon resume from suspend. This is resolved by the firmware cache, but
  32. the firmware cache is only supported if you use uevents, and its not
  33. supported for request_firmware_into_buf().
  34. * Firmware is not accessible through typical means:
  35. * It cannot be installed into the root filesystem
  36. * The firmware provides very unique device specific data tailored for
  37. the unit gathered with local information. An example is calibration
  38. data for WiFi chipsets for mobile devices. This calibration data is
  39. not common to all units, but tailored per unit. Such information may
  40. be installed on a separate flash partition other than where the root
  41. filesystem is provided.
  42. Types of fallback mechanisms
  43. ============================
  44. There are really two fallback mechanisms available using one shared sysfs
  45. interface as a loading facility:
  46. * Kobject uevent fallback mechanism
  47. * Custom fallback mechanism
  48. First lets document the shared sysfs loading facility.
  49. Firmware sysfs loading facility
  50. ===============================
  51. In order to help device drivers upload firmware using a fallback mechanism
  52. the firmware infrastructure creates a sysfs interface to enable userspace
  53. to load and indicate when firmware is ready. The sysfs directory is created
  54. via fw_create_instance(). This call creates a new struct device named after
  55. the firmware requested, and establishes it in the device hierarchy by
  56. associating the device used to make the request as the device's parent.
  57. The sysfs directory's file attributes are defined and controlled through
  58. the new device's class (firmware_class) and group (fw_dev_attr_groups).
  59. This is actually where the original firmware_class module name came from,
  60. given that originally the only firmware loading mechanism available was the
  61. mechanism we now use as a fallback mechanism, which registers a struct class
  62. firmware_class. Because the attributes exposed are part of the module name, the
  63. module name firmware_class cannot be renamed in the future, to ensure backward
  64. compatibility with old userspace.
  65. To load firmware using the sysfs interface we expose a loading indicator,
  66. and a file upload firmware into:
  67. * /sys/$DEVPATH/loading
  68. * /sys/$DEVPATH/data
  69. To upload firmware you will echo 1 onto the loading file to indicate
  70. you are loading firmware. You then write the firmware into the data file,
  71. and you notify the kernel the firmware is ready by echo'ing 0 onto
  72. the loading file.
  73. The firmware device used to help load firmware using sysfs is only created if
  74. direct firmware loading fails and if the fallback mechanism is enabled for your
  75. firmware request, this is set up with :c:func:`firmware_fallback_sysfs`. It is
  76. important to re-iterate that no device is created if a direct filesystem lookup
  77. succeeded.
  78. Using::
  79. echo 1 > /sys/$DEVPATH/loading
  80. Will clean any previous partial load at once and make the firmware API
  81. return an error. When loading firmware the firmware_class grows a buffer
  82. for the firmware in PAGE_SIZE increments to hold the image as it comes in.
  83. firmware_data_read() and firmware_loading_show() are just provided for the
  84. test_firmware driver for testing, they are not called in normal use or
  85. expected to be used regularly by userspace.
  86. firmware_fallback_sysfs
  87. -----------------------
  88. .. kernel-doc:: drivers/base/firmware_loader/fallback.c
  89. :functions: firmware_fallback_sysfs
  90. Firmware kobject uevent fallback mechanism
  91. ==========================================
  92. Since a device is created for the sysfs interface to help load firmware as a
  93. fallback mechanism userspace can be informed of the addition of the device by
  94. relying on kobject uevents. The addition of the device into the device
  95. hierarchy means the fallback mechanism for firmware loading has been initiated.
  96. For details of implementation refer to fw_load_sysfs_fallback(), in particular
  97. on the use of dev_set_uevent_suppress() and kobject_uevent().
  98. The kernel's kobject uevent mechanism is implemented in lib/kobject_uevent.c,
  99. it issues uevents to userspace. As a supplement to kobject uevents Linux
  100. distributions could also enable CONFIG_UEVENT_HELPER_PATH, which makes use of
  101. core kernel's usermode helper (UMH) functionality to call out to a userspace
  102. helper for kobject uevents. In practice though no standard distribution has
  103. ever used the CONFIG_UEVENT_HELPER_PATH. If CONFIG_UEVENT_HELPER_PATH is
  104. enabled this binary would be called each time kobject_uevent_env() gets called
  105. in the kernel for each kobject uevent triggered.
  106. Different implementations have been supported in userspace to take advantage of
  107. this fallback mechanism. When firmware loading was only possible using the
  108. sysfs mechanism the userspace component "hotplug" provided the functionality of
  109. monitoring for kobject events. Historically this was superseded be systemd's
  110. udev, however firmware loading support was removed from udev as of systemd
  111. commit be2ea723b1d0 ("udev: remove userspace firmware loading support")
  112. as of v217 on August, 2014. This means most Linux distributions today are
  113. not using or taking advantage of the firmware fallback mechanism provided
  114. by kobject uevents. This is specially exacerbated due to the fact that most
  115. distributions today disable CONFIG_FW_LOADER_USER_HELPER_FALLBACK.
  116. Refer to do_firmware_uevent() for details of the kobject event variables
  117. setup. The variables currently passed to userspace with a "kobject add"
  118. event are:
  119. * FIRMWARE=firmware name
  120. * TIMEOUT=timeout value
  121. * ASYNC=whether or not the API request was asynchronous
  122. By default DEVPATH is set by the internal kernel kobject infrastructure.
  123. Below is an example simple kobject uevent script::
  124. # Both $DEVPATH and $FIRMWARE are already provided in the environment.
  125. MY_FW_DIR=/lib/firmware/
  126. echo 1 > /sys/$DEVPATH/loading
  127. cat $MY_FW_DIR/$FIRMWARE > /sys/$DEVPATH/data
  128. echo 0 > /sys/$DEVPATH/loading
  129. Firmware custom fallback mechanism
  130. ==================================
  131. Users of the request_firmware_nowait() call have yet another option available
  132. at their disposal: rely on the sysfs fallback mechanism but request that no
  133. kobject uevents be issued to userspace. The original logic behind this
  134. was that utilities other than udev might be required to lookup firmware
  135. in non-traditional paths -- paths outside of the listing documented in the
  136. section 'Direct filesystem lookup'. This option is not available to any of
  137. the other API calls as uevents are always forced for them.
  138. Since uevents are only meaningful if the fallback mechanism is enabled
  139. in your kernel it would seem odd to enable uevents with kernels that do not
  140. have the fallback mechanism enabled in their kernels. Unfortunately we also
  141. rely on the uevent flag which can be disabled by request_firmware_nowait() to
  142. also setup the firmware cache for firmware requests. As documented above,
  143. the firmware cache is only set up if uevent is enabled for an API call.
  144. Although this can disable the firmware cache for request_firmware_nowait()
  145. calls, users of this API should not use it for the purposes of disabling
  146. the cache as that was not the original purpose of the flag. Not setting
  147. the uevent flag means you want to opt-in for the firmware fallback mechanism
  148. but you want to suppress kobject uevents, as you have a custom solution which
  149. will monitor for your device addition into the device hierarchy somehow and
  150. load firmware for you through a custom path.
  151. Firmware fallback timeout
  152. =========================
  153. The firmware fallback mechanism has a timeout. If firmware is not loaded
  154. onto the sysfs interface by the timeout value an error is sent to the
  155. driver. By default the timeout is set to 60 seconds if uevents are
  156. desirable, otherwise MAX_JIFFY_OFFSET is used (max timeout possible).
  157. The logic behind using MAX_JIFFY_OFFSET for non-uevents is that a custom
  158. solution will have as much time as it needs to load firmware.
  159. You can customize the firmware timeout by echo'ing your desired timeout into
  160. the following file:
  161. * /sys/class/firmware/timeout
  162. If you echo 0 into it means MAX_JIFFY_OFFSET will be used. The data type
  163. for the timeout is an int.
  164. EFI embedded firmware fallback mechanism
  165. ========================================
  166. On some devices the system's EFI code / ROM may contain an embedded copy
  167. of firmware for some of the system's integrated peripheral devices and
  168. the peripheral's Linux device-driver needs to access this firmware.
  169. Device drivers which need such firmware can use the
  170. firmware_request_platform() function for this, note that this is a
  171. separate fallback mechanism from the other fallback mechanisms and
  172. this does not use the sysfs interface.
  173. A device driver which needs this can describe the firmware it needs
  174. using an efi_embedded_fw_desc struct:
  175. .. kernel-doc:: include/linux/efi_embedded_fw.h
  176. :functions: efi_embedded_fw_desc
  177. The EFI embedded-fw code works by scanning all EFI_BOOT_SERVICES_CODE memory
  178. segments for an eight byte sequence matching prefix; if the prefix is found it
  179. then does a sha256 over length bytes and if that matches makes a copy of length
  180. bytes and adds that to its list with found firmwares.
  181. To avoid doing this somewhat expensive scan on all systems, dmi matching is
  182. used. Drivers are expected to export a dmi_system_id array, with each entries'
  183. driver_data pointing to an efi_embedded_fw_desc.
  184. To register this array with the efi-embedded-fw code, a driver needs to:
  185. 1. Always be builtin to the kernel or store the dmi_system_id array in a
  186. separate object file which always gets builtin.
  187. 2. Add an extern declaration for the dmi_system_id array to
  188. include/linux/efi_embedded_fw.h.
  189. 3. Add the dmi_system_id array to the embedded_fw_table in
  190. drivers/firmware/efi/embedded-firmware.c wrapped in a #ifdef testing that
  191. the driver is being builtin.
  192. 4. Add "select EFI_EMBEDDED_FIRMWARE if EFI_STUB" to its Kconfig entry.
  193. The firmware_request_platform() function will always first try to load firmware
  194. with the specified name directly from the disk, so the EFI embedded-fw can
  195. always be overridden by placing a file under /lib/firmware.
  196. Note that:
  197. 1. The code scanning for EFI embedded-firmware runs near the end
  198. of start_kernel(), just before calling rest_init(). For normal drivers and
  199. subsystems using subsys_initcall() to register themselves this does not
  200. matter. This means that code running earlier cannot use EFI
  201. embedded-firmware.
  202. 2. At the moment the EFI embedded-fw code assumes that firmwares always start at
  203. an offset which is a multiple of 8 bytes, if this is not true for your case
  204. send in a patch to fix this.
  205. 3. At the moment the EFI embedded-fw code only works on x86 because other archs
  206. free EFI_BOOT_SERVICES_CODE before the EFI embedded-fw code gets a chance to
  207. scan it.
  208. 4. The current brute-force scanning of EFI_BOOT_SERVICES_CODE is an ad-hoc
  209. brute-force solution. There has been discussion to use the UEFI Platform
  210. Initialization (PI) spec's Firmware Volume protocol. This has been rejected
  211. because the FV Protocol relies on *internal* interfaces of the PI spec, and:
  212. 1. The PI spec does not define peripheral firmware at all
  213. 2. The internal interfaces of the PI spec do not guarantee any backward
  214. compatibility. Any implementation details in FV may be subject to change,
  215. and may vary system to system. Supporting the FV Protocol would be
  216. difficult as it is purposely ambiguous.
  217. Example how to check for and extract embedded firmware
  218. ------------------------------------------------------
  219. To check for, for example Silead touchscreen controller embedded firmware,
  220. do the following:
  221. 1. Boot the system with efi=debug on the kernel commandline
  222. 2. cp /sys/kernel/debug/efi/boot_services_code? to your home dir
  223. 3. Open the boot_services_code? files in a hex-editor, search for the
  224. magic prefix for Silead firmware: F0 00 00 00 02 00 00 00, this gives you
  225. the beginning address of the firmware inside the boot_services_code? file.
  226. 4. The firmware has a specific pattern, it starts with a 8 byte page-address,
  227. typically F0 00 00 00 02 00 00 00 for the first page followed by 32-bit
  228. word-address + 32-bit value pairs. With the word-address incrementing 4
  229. bytes (1 word) for each pair until a page is complete. A complete page is
  230. followed by a new page-address, followed by more word + value pairs. This
  231. leads to a very distinct pattern. Scroll down until this pattern stops,
  232. this gives you the end of the firmware inside the boot_services_code? file.
  233. 5. "dd if=boot_services_code? of=firmware bs=1 skip=<begin-addr> count=<len>"
  234. will extract the firmware for you. Inspect the firmware file in a
  235. hexeditor to make sure you got the dd parameters correct.
  236. 6. Copy it to /lib/firmware under the expected name to test it.
  237. 7. If the extracted firmware works, you can use the found info to fill an
  238. efi_embedded_fw_desc struct to describe it, run "sha256sum firmware"
  239. to get the sha256sum to put in the sha256 field.