firmware-update.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. Firmware Update (FWU)
  2. =====================
  3. This document describes the design of the various Firmware Update (FWU)
  4. mechanisms available in TF-A.
  5. 1. PSA Firmware Update (PSA FWU)
  6. 2. TBBR Firmware Update (TBBR FWU)
  7. PSA Firmware Update implements the specification of the same name (Arm document
  8. IHI 0093), which defines a standard firmware interface for installing firmware
  9. updates.
  10. On the other hand, TBBR Firmware Update only covers firmware recovery. Arguably,
  11. its name is somewhat misleading but the TBBR specification and terminology
  12. predates PSA FWU. Both mechanisms are complementary in the sense that PSA FWU
  13. assumes that the device has a backup or recovery capability in the event of a
  14. failed update, which can be fulfilled with TBBR FWU implementation.
  15. .. _PSA Firmware Update:
  16. PSA Firmware Update (PSA FWU)
  17. -----------------------------
  18. Introduction
  19. ~~~~~~~~~~~~
  20. The `PSA FW update specification`_ defines the concepts of ``Firmware Update
  21. Client`` and ``Firmware Update Agent``.
  22. The new firmware images are provided by the ``Client`` to the ``Update Agent``
  23. to flash them in non-volatile storage.
  24. A common system design will place the ``Update Agent`` in the Secure-world
  25. while the ``Client`` executes in the Normal-world.
  26. The `PSA FW update specification`_ provides ABIs meant for a Normal-world
  27. entity aka ``Client`` to transmit the firmware images to the ``Update Agent``.
  28. Scope
  29. ~~~~~
  30. The design of the ``Client`` and ``Update Agent`` is out of scope of this
  31. document.
  32. This document mainly covers ``Platform Boot`` details i.e. the role of
  33. the second stage Bootloader after FWU has been done by ``Client`` and
  34. ``Update Agent``.
  35. Overview
  36. ~~~~~~~~
  37. There are active and update banks in the non-volatile storage identified
  38. by the ``active_index`` and the ``update_index`` respectively.
  39. An active bank stores running firmware, whereas an update bank contains
  40. firmware updates.
  41. Once Firmwares are updated in the update bank of the non-volatile
  42. storage, then ``Update Agent`` marks the update bank as the active bank,
  43. and write updated FWU metadata in non-volatile storage.
  44. On subsequent reboot, the second stage Bootloader (BL2) performs the
  45. following actions:
  46. - Read FWU metadata in memory
  47. - Retrieve the image specification (offset and length) of updated images
  48. present in non-volatile storage with the help of FWU metadata
  49. - Set these image specification in the corresponding I/O policies of the
  50. updated images using the FWU platform functions
  51. ``plat_fwu_set_images_source()`` and ``plat_fwu_set_metadata_image_source()``,
  52. please refer :ref:`Porting Guide`
  53. - Use these I/O policies to read the images from this address into the memory
  54. By default, the platform uses the active bank of non-volatile storage to boot
  55. the images in ``trial state``. If images pass through the authentication check
  56. and also if the system successfully booted the Normal-world image then
  57. ``Update Agent`` marks this update as accepted after further sanitisation
  58. checking at Normal-world.
  59. The second stage Bootloader (BL2) avoids upgrading the platform NV-counter until
  60. it's been confirmed that given update is accepted.
  61. The following sequence diagram shows platform-boot flow:
  62. .. image:: ../resources/diagrams/PSA-FWU.png
  63. If the platform fails to boot from active bank due to any reasons such
  64. as authentication failure or non-fuctionality of Normal-world software then the
  65. watchdog will reset to give a chance to the platform to fix the issue. This
  66. boot failure & reset sequence might be repeated up to ``trial state`` times.
  67. After that, the platform can decide to boot from the ``previous_active_index``
  68. bank.
  69. If the images still does not boot successfully from the ``previous_active_index``
  70. bank (e.g. due to ageing effect of non-volatile storage) then the platform can
  71. choose firmware recovery mechanism :ref:`TBBR Firmware Update` to bring system
  72. back to life.
  73. .. _TBBR Firmware Update:
  74. TBBR Firmware Update (TBBR FWU)
  75. -------------------------------
  76. Introduction
  77. ~~~~~~~~~~~~
  78. This technique enables authenticated firmware to update firmware images from
  79. external interfaces such as USB, UART, SD-eMMC, NAND, NOR or Ethernet to SoC
  80. Non-Volatile memories such as NAND Flash, LPDDR2-NVM or any memory determined
  81. by the platform.
  82. This feature functions even when the current firmware in the system is corrupt
  83. or missing; it therefore may be used as a recovery mode. It may also be
  84. complemented by other, higher level firmware update software.
  85. FWU implements a specific part of the Trusted Board Boot Requirements (TBBR)
  86. specification, Arm DEN0006C-1. It should be used in conjunction with the
  87. :ref:`Trusted Board Boot` design document, which describes the image
  88. authentication parts of the Trusted Firmware-A (TF-A) TBBR implementation.
  89. It can be used as a last resort when all firmware updates that are carried out
  90. as part of the :ref:`PSA Firmware Update` procedure have failed to function.
  91. Scope
  92. ~~~~~
  93. This document describes the secure world FWU design. It is beyond its scope to
  94. describe how normal world FWU images should operate. To implement normal world
  95. FWU images, please refer to the "Non-Trusted Firmware Updater" requirements in
  96. the TBBR.
  97. Overview
  98. ~~~~~~~~
  99. The FWU boot flow is primarily mediated by BL1. Since BL1 executes in ROM, and
  100. it is usually desirable to minimize the amount of ROM code, the design allows
  101. some parts of FWU to be implemented in other secure and normal world images.
  102. Platform code may choose which parts are implemented in which images but the
  103. general expectation is:
  104. - BL1 handles:
  105. - Detection and initiation of the FWU boot flow.
  106. - Copying images from non-secure to secure memory
  107. - FWU image authentication
  108. - Context switching between the normal and secure world during the FWU
  109. process.
  110. - Other secure world FWU images handle platform initialization required by
  111. the FWU process.
  112. - Normal world FWU images handle loading of firmware images from external
  113. interfaces to non-secure memory.
  114. The primary requirements of the FWU feature are:
  115. #. Export a BL1 SMC interface to interoperate with other FWU images executing
  116. at other Exception Levels.
  117. #. Export a platform interface to provide FWU common code with the information
  118. it needs, and to enable platform specific FWU functionality. See the
  119. :ref:`Porting Guide` for details of this interface.
  120. TF-A uses abbreviated image terminology for FWU images like for other TF-A
  121. images. See the :ref:`Image Terminology` document for an explanation of these
  122. terms.
  123. The following diagram shows the FWU boot flow for Arm development platforms.
  124. Arm CSS platforms like Juno have a System Control Processor (SCP), and these
  125. use all defined FWU images. Other platforms may use a subset of these.
  126. |Flow Diagram|
  127. Image Identification
  128. ~~~~~~~~~~~~~~~~~~~~
  129. Each FWU image and certificate is identified by a unique ID, defined by the
  130. platform, which BL1 uses to fetch an image descriptor (``image_desc_t``) via a
  131. call to ``bl1_plat_get_image_desc()``. The same ID is also used to prepare the
  132. Chain of Trust (Refer to the :ref:`Authentication Framework & Chain of Trust`
  133. document for more information).
  134. The image descriptor includes the following information:
  135. - Executable or non-executable image. This indicates whether the normal world
  136. is permitted to request execution of a secure world FWU image (after
  137. authentication). Secure world certificates and non-AP images are examples
  138. of non-executable images.
  139. - Secure or non-secure image. This indicates whether the image is
  140. authenticated/executed in secure or non-secure memory.
  141. - Image base address and size.
  142. - Image entry point configuration (an ``entry_point_info_t``).
  143. - FWU image state.
  144. BL1 uses the FWU image descriptors to:
  145. - Validate the arguments of FWU SMCs
  146. - Manage the state of the FWU process
  147. - Initialize the execution state of the next FWU image.
  148. FWU State Machine
  149. ~~~~~~~~~~~~~~~~~
  150. BL1 maintains state for each FWU image during FWU execution. FWU images at lower
  151. Exception Levels raise SMCs to invoke FWU functionality in BL1, which causes
  152. BL1 to update its FWU image state. The BL1 image states and valid state
  153. transitions are shown in the diagram below. Note that secure images have a more
  154. complex state machine than non-secure images.
  155. |FWU state machine|
  156. The following is a brief description of the supported states:
  157. - RESET: This is the initial state of every image at the start of FWU.
  158. Authentication failure also leads to this state. A secure
  159. image may yield to this state if it has completed execution.
  160. It can also be reached by using ``FWU_SMC_IMAGE_RESET``.
  161. - COPYING: This is the state of a secure image while BL1 is copying it
  162. in blocks from non-secure to secure memory.
  163. - COPIED: This is the state of a secure image when BL1 has completed
  164. copying it to secure memory.
  165. - AUTHENTICATED: This is the state of an image when BL1 has successfully
  166. authenticated it.
  167. - EXECUTED: This is the state of a secure, executable image when BL1 has
  168. passed execution control to it.
  169. - INTERRUPTED: This is the state of a secure, executable image after it has
  170. requested BL1 to resume normal world execution.
  171. BL1 SMC Interface
  172. ~~~~~~~~~~~~~~~~~
  173. BL1_SMC_CALL_COUNT
  174. ^^^^^^^^^^^^^^^^^^
  175. ::
  176. Arguments:
  177. uint32_t function ID : 0x0
  178. Return:
  179. uint32_t
  180. This SMC returns the number of SMCs supported by BL1.
  181. BL1_SMC_UID
  182. ^^^^^^^^^^^
  183. ::
  184. Arguments:
  185. uint32_t function ID : 0x1
  186. Return:
  187. UUID : 32 bits in each of w0-w3 (or r0-r3 for AArch32 callers)
  188. This SMC returns the 128-bit `Universally Unique Identifier`_ for the
  189. BL1 SMC service.
  190. BL1_SMC_VERSION
  191. ^^^^^^^^^^^^^^^
  192. ::
  193. Argument:
  194. uint32_t function ID : 0x3
  195. Return:
  196. uint32_t : Bits [31:16] Major Version
  197. Bits [15:0] Minor Version
  198. This SMC returns the current version of the BL1 SMC service.
  199. BL1_SMC_RUN_IMAGE
  200. ^^^^^^^^^^^^^^^^^
  201. ::
  202. Arguments:
  203. uint32_t function ID : 0x4
  204. entry_point_info_t *ep_info
  205. Return:
  206. void
  207. Pre-conditions:
  208. if (normal world caller) synchronous exception
  209. if (ep_info not EL3) synchronous exception
  210. This SMC passes execution control to an EL3 image described by the provided
  211. ``entry_point_info_t`` structure. In the normal TF-A boot flow, BL2 invokes
  212. this SMC for BL1 to pass execution control to BL31.
  213. FWU_SMC_IMAGE_COPY
  214. ^^^^^^^^^^^^^^^^^^
  215. ::
  216. Arguments:
  217. uint32_t function ID : 0x10
  218. unsigned int image_id
  219. uintptr_t image_addr
  220. unsigned int block_size
  221. unsigned int image_size
  222. Return:
  223. int : 0 (Success)
  224. : -ENOMEM
  225. : -EPERM
  226. Pre-conditions:
  227. if (image_id is invalid) return -EPERM
  228. if (image_id is non-secure image) return -EPERM
  229. if (image_id state is not (RESET or COPYING)) return -EPERM
  230. if (secure world caller) return -EPERM
  231. if (image_addr + block_size overflows) return -ENOMEM
  232. if (image destination address + image_size overflows) return -ENOMEM
  233. if (source block is in secure memory) return -ENOMEM
  234. if (source block is not mapped into BL1) return -ENOMEM
  235. if (image_size > free secure memory) return -ENOMEM
  236. if (image overlaps another image) return -EPERM
  237. This SMC copies the secure image indicated by ``image_id`` from non-secure memory
  238. to secure memory for later authentication. The image may be copied in a single
  239. block or multiple blocks. In either case, the total size of the image must be
  240. provided in ``image_size`` when invoking this SMC for the first time for each
  241. image; it is ignored in subsequent calls (if any) for the same image.
  242. The ``image_addr`` and ``block_size`` specify the source memory block to copy from.
  243. The destination address is provided by the platform code.
  244. If ``block_size`` is greater than the amount of remaining bytes to copy for this
  245. image then the former is truncated to the latter. The copy operation is then
  246. considered as complete and the FWU state machine transitions to the "COPIED"
  247. state. If there is still more to copy, the FWU state machine stays in or
  248. transitions to the COPYING state (depending on the previous state).
  249. When using multiple blocks, the source blocks do not necessarily need to be in
  250. contiguous memory.
  251. Once the SMC is handled, BL1 returns from exception to the normal world caller.
  252. FWU_SMC_IMAGE_AUTH
  253. ^^^^^^^^^^^^^^^^^^
  254. ::
  255. Arguments:
  256. uint32_t function ID : 0x11
  257. unsigned int image_id
  258. uintptr_t image_addr
  259. unsigned int image_size
  260. Return:
  261. int : 0 (Success)
  262. : -ENOMEM
  263. : -EPERM
  264. : -EAUTH
  265. Pre-conditions:
  266. if (image_id is invalid) return -EPERM
  267. if (secure world caller)
  268. if (image_id state is not RESET) return -EPERM
  269. if (image_addr/image_size is not mapped into BL1) return -ENOMEM
  270. else // normal world caller
  271. if (image_id is secure image)
  272. if (image_id state is not COPIED) return -EPERM
  273. else // image_id is non-secure image
  274. if (image_id state is not RESET) return -EPERM
  275. if (image_addr/image_size is in secure memory) return -ENOMEM
  276. if (image_addr/image_size not mapped into BL1) return -ENOMEM
  277. This SMC authenticates the image specified by ``image_id``. If the image is in the
  278. RESET state, BL1 authenticates the image in place using the provided
  279. ``image_addr`` and ``image_size``. If the image is a secure image in the COPIED
  280. state, BL1 authenticates the image from the secure memory that BL1 previously
  281. copied the image into.
  282. BL1 returns from exception to the caller. If authentication succeeds then BL1
  283. sets the image state to AUTHENTICATED. If authentication fails then BL1 returns
  284. the -EAUTH error and sets the image state back to RESET.
  285. FWU_SMC_IMAGE_EXECUTE
  286. ^^^^^^^^^^^^^^^^^^^^^
  287. ::
  288. Arguments:
  289. uint32_t function ID : 0x12
  290. unsigned int image_id
  291. Return:
  292. int : 0 (Success)
  293. : -EPERM
  294. Pre-conditions:
  295. if (image_id is invalid) return -EPERM
  296. if (secure world caller) return -EPERM
  297. if (image_id is non-secure image) return -EPERM
  298. if (image_id is non-executable image) return -EPERM
  299. if (image_id state is not AUTHENTICATED) return -EPERM
  300. This SMC initiates execution of a previously authenticated image specified by
  301. ``image_id``, in the other security world to the caller. The current
  302. implementation only supports normal world callers initiating execution of a
  303. secure world image.
  304. BL1 saves the normal world caller's context, sets the secure image state to
  305. EXECUTED, and returns from exception to the secure image.
  306. FWU_SMC_IMAGE_RESUME
  307. ^^^^^^^^^^^^^^^^^^^^
  308. ::
  309. Arguments:
  310. uint32_t function ID : 0x13
  311. register_t image_param
  312. Return:
  313. register_t : image_param (Success)
  314. : -EPERM
  315. Pre-conditions:
  316. if (normal world caller and no INTERRUPTED secure image) return -EPERM
  317. This SMC resumes execution in the other security world while there is a secure
  318. image in the EXECUTED/INTERRUPTED state.
  319. For normal world callers, BL1 sets the previously interrupted secure image state
  320. to EXECUTED. For secure world callers, BL1 sets the previously executing secure
  321. image state to INTERRUPTED. In either case, BL1 saves the calling world's
  322. context, restores the resuming world's context and returns from exception into
  323. the resuming world. If the call is successful then the caller provided
  324. ``image_param`` is returned to the resumed world, otherwise an error code is
  325. returned to the caller.
  326. FWU_SMC_SEC_IMAGE_DONE
  327. ^^^^^^^^^^^^^^^^^^^^^^
  328. ::
  329. Arguments:
  330. uint32_t function ID : 0x14
  331. Return:
  332. int : 0 (Success)
  333. : -EPERM
  334. Pre-conditions:
  335. if (normal world caller) return -EPERM
  336. This SMC indicates completion of a previously executing secure image.
  337. BL1 sets the previously executing secure image state to the RESET state,
  338. restores the normal world context and returns from exception into the normal
  339. world.
  340. FWU_SMC_UPDATE_DONE
  341. ^^^^^^^^^^^^^^^^^^^
  342. ::
  343. Arguments:
  344. uint32_t function ID : 0x15
  345. register_t client_cookie
  346. Return:
  347. N/A
  348. This SMC completes the firmware update process. BL1 calls the platform specific
  349. function ``bl1_plat_fwu_done``, passing the optional argument ``client_cookie`` as
  350. a ``void *``. The SMC does not return.
  351. FWU_SMC_IMAGE_RESET
  352. ^^^^^^^^^^^^^^^^^^^
  353. ::
  354. Arguments:
  355. uint32_t function ID : 0x16
  356. unsigned int image_id
  357. Return:
  358. int : 0 (Success)
  359. : -EPERM
  360. Pre-conditions:
  361. if (secure world caller) return -EPERM
  362. if (image in EXECUTED) return -EPERM
  363. This SMC sets the state of an image to RESET and zeroes the memory used by it.
  364. This is only allowed if the image is not being executed.
  365. --------------
  366. *Copyright (c) 2015-2022, Arm Limited and Contributors. All rights reserved.*
  367. .. _Universally Unique Identifier: https://tools.ietf.org/rfc/rfc4122.txt
  368. .. |Flow Diagram| image:: ../resources/diagrams/fwu_flow.png
  369. .. |FWU state machine| image:: ../resources/diagrams/fwu_states.png
  370. .. _PSA FW update specification: https://developer.arm.com/documentation/den0118/latest/