boot_api.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * Copyright (c) 2017-2023, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #ifndef BOOT_API_H
  7. #define BOOT_API_H
  8. #include <stdint.h>
  9. #include <stdio.h>
  10. /*
  11. * Possible value of boot context field 'auth_status'
  12. */
  13. #if STM32MP13
  14. /* No authentication done */
  15. #define BOOT_API_CTX_AUTH_NO 0x7CFDD351U
  16. /* Authentication done and failed */
  17. #define BOOT_API_CTX_AUTH_FAILED 0x51330884U
  18. /* Authentication done and success */
  19. #define BOOT_API_CTX_AUTH_SUCCESS 0x67E8CAE1U
  20. #endif
  21. #if STM32MP15
  22. /* No authentication done */
  23. #define BOOT_API_CTX_AUTH_NO 0x0U
  24. /* Authentication done and failed */
  25. #define BOOT_API_CTX_AUTH_FAILED 0x1U
  26. /* Authentication done and succeeded */
  27. #define BOOT_API_CTX_AUTH_SUCCESS 0x2U
  28. #endif
  29. /*
  30. * Possible value of boot context field 'boot_interface_sel'
  31. */
  32. /* Value of field 'boot_interface_sel' when no boot occurred */
  33. #define BOOT_API_CTX_BOOT_INTERFACE_SEL_NO 0x0U
  34. /* Boot occurred on SD */
  35. #define BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD 0x1U
  36. /* Boot occurred on EMMC */
  37. #define BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC 0x2U
  38. /* Boot occurred on FMC */
  39. #define BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC 0x3U
  40. /* Boot occurred on QSPI NOR */
  41. #define BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_SPI 0x4U
  42. /* Boot occurred on UART */
  43. #define BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART 0x5U
  44. /* Boot occurred on USB */
  45. #define BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB 0x6U
  46. /* Boot occurred on QSPI NAND */
  47. #define BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_SPI 0x7U
  48. /**
  49. * @brief Possible value of boot context field 'EmmcXferStatus'
  50. */
  51. /*
  52. * Possible value of boot context field 'emmc_xfer_status'
  53. */
  54. #define BOOT_API_CTX_EMMC_XFER_STATUS_NOT_STARTED 0x0U
  55. #define BOOT_API_CTX_EMMC_XFER_STATUS_DATAEND_DETECTED 0x1U
  56. #define BOOT_API_CTX_EMMC_XFER_STATUS_XFER_OVERALL_TIMEOUT_DETECTED 0x2U
  57. #define BOOT_API_CTX_EMMC_XFER_STATUS_XFER_DATA_TIMEOUT 0x3U
  58. /*
  59. * Possible value of boot context field 'emmc_error_status'
  60. */
  61. #define BOOT_API_CTX_EMMC_ERROR_STATUS_NONE 0x0U
  62. #define BOOT_API_CTX_EMMC_ERROR_STATUS_CMD_TIMEOUT 0x1U
  63. #define BOOT_API_CTX_EMMC_ERROR_STATUS_ACK_TIMEOUT 0x2U
  64. #define BOOT_API_CTX_EMMC_ERROR_STATUS_DATA_CRC_FAIL 0x3U
  65. #define BOOT_API_CTX_EMMC_ERROR_STATUS_NOT_ENOUGH_BOOT_DATA_RX 0x4U
  66. #define BOOT_API_CTX_EMMC_ERROR_STATUS_HEADER_NOT_FOUND 0x5U
  67. #define BOOT_API_CTX_EMMC_ERROR_STATUS_HEADER_SIZE_ZERO 0x6U
  68. #define BOOT_API_CTX_EMMC_ERROR_STATUS_IMAGE_NOT_COMPLETE 0x7U
  69. #define BOOT_API_CTX_EMMC_ERROR_STATUS_ACK_ERROR 0x8U
  70. /* Image Header related definitions */
  71. /* Definition of header version */
  72. #if STM32MP13
  73. #define BOOT_API_HEADER_VERSION 0x00020000U
  74. #endif
  75. #if STM32MP15
  76. #define BOOT_API_HEADER_VERSION 0x00010000U
  77. #endif
  78. /*
  79. * Magic number used to detect header in memory
  80. * Its value must be 'S' 'T' 'M' 0x32, i.e 0x324D5453 as field
  81. * 'bootapi_image_header_t.magic'
  82. * This identifies the start of a boot image.
  83. */
  84. #define BOOT_API_IMAGE_HEADER_MAGIC_NB 0x324D5453U
  85. /* Definitions related to Authentication used in image header structure */
  86. #define BOOT_API_ECDSA_PUB_KEY_LEN_IN_BYTES 64
  87. #define BOOT_API_ECDSA_SIGNATURE_LEN_IN_BYTES 64
  88. #define BOOT_API_SHA256_DIGEST_SIZE_IN_BYTES 32
  89. /* Possible values of the field 'boot_api_image_header_t.ecc_algo_type' */
  90. #define BOOT_API_ECDSA_ALGO_TYPE_P256NIST 1
  91. #define BOOT_API_ECDSA_ALGO_TYPE_BRAINPOOL256 2
  92. /*
  93. * Extension headers related definitions
  94. */
  95. /* 'bootapi_image_header_t.extension_flag' used for authentication feature */
  96. #define BOOT_API_AUTHENTICATION_EXTENSION_BIT BIT(0)
  97. /* 'bootapi_image_header_t.extension_flag' used for FSBL decryption feature */
  98. #define BOOT_API_FSBL_DECRYPTION_EXTENSION_BIT BIT(1)
  99. /* 'bootapi_image_header_t.extension_flag' used for padding header feature */
  100. #define BOOT_API_PADDING_EXTENSION_BIT BIT(31)
  101. /*
  102. * mask of bits of field 'bootapi_image_header_t.extension_flag'
  103. * used for extension headers
  104. */
  105. #define BOOT_API_ALL_EXTENSIONS_MASK \
  106. (BOOT_API_AUTHENTICATION_EXTENSION_BIT | \
  107. BOOT_API_FSBL_DECRYPTION_EXTENSION_BIT | \
  108. BOOT_API_PADDING_EXTENSION_BIT)
  109. /*
  110. * Magic number of FSBL decryption extension header
  111. * The value shall gives the four bytes 'S','T',0x00,0x01 in memory
  112. */
  113. #define BOOT_API_FSBL_DECRYPTION_HEADER_MAGIC_NB 0x01005453U
  114. /*
  115. * Magic number of PKH revocation extension header
  116. * The value shall gives the four bytes 'S','T',0x00,0x02 in memory
  117. */
  118. #define BOOT_API_AUTHENTICATION_HEADER_MAGIC_NB 0x02005453U
  119. /* Max number of ECDSA public key hash in table */
  120. #define BOOT_API_AUTHENTICATION_NB_PKH_MAX 8U
  121. /* ECDSA public key hash table size in bytes */
  122. #define BOOT_API_AUTHENTICATION_TABLE_SIZE_BYTES \
  123. (BOOT_API_AUTHENTICATION_NB_PKH_MAX * \
  124. BOOT_API_SHA256_DIGEST_SIZE_IN_BYTES)
  125. /*
  126. * Magic number of padding extension header
  127. * The value shall gives the four bytes 'S','T',0xFF,0xFF in memory
  128. */
  129. #define BOOT_API_PADDING_HEADER_MAGIC_NB 0xFFFF5453U
  130. /*
  131. * Cores secure magic numbers
  132. * Constant to be stored in bakcup register
  133. * BOOT_API_MAGIC_NUMBER_TAMP_BCK_REG_IDX
  134. */
  135. #define BOOT_API_A7_CORE0_MAGIC_NUMBER 0xCA7FACE0U
  136. #define BOOT_API_A7_CORE1_MAGIC_NUMBER 0xCA7FACE1U
  137. /*
  138. * TAMP_BCK4R register index
  139. * This register is used to write a Magic Number in order to restart
  140. * Cortex A7 Core 1 and make it execute @ branch address from TAMP_BCK5R
  141. */
  142. #define BOOT_API_CORE1_MAGIC_NUMBER_TAMP_BCK_REG_IDX 4U
  143. /*
  144. * TAMP_BCK5R register index
  145. * This register is used to contain the branch address of
  146. * Cortex A7 Core 1 when restarted by a TAMP_BCK4R magic number writing
  147. */
  148. #define BOOT_API_CORE1_BRANCH_ADDRESS_TAMP_BCK_REG_IDX 5U
  149. /*
  150. * Possible value of boot context field 'hse_clock_value_in_hz'
  151. */
  152. #define BOOT_API_CTX_HSE_CLOCK_VALUE_UNDEFINED 0U
  153. #define BOOT_API_CTX_HSE_CLOCK_VALUE_24_MHZ 24000000U
  154. #define BOOT_API_CTX_HSE_CLOCK_VALUE_25_MHZ 25000000U
  155. #define BOOT_API_CTX_HSE_CLOCK_VALUE_26_MHZ 26000000U
  156. /*
  157. * Possible value of boot context field 'boot_partition_used_toboot'
  158. */
  159. #define BOOT_API_CTX_BOOT_PARTITION_UNDEFINED 0U
  160. /* Used FSBL1 to boot */
  161. #define BOOT_API_CTX_BOOT_PARTITION_FSBL1 1U
  162. /* Used FSBL2 to boot */
  163. #define BOOT_API_CTX_BOOT_PARTITION_FSBL2 2U
  164. /* OTP_CFG0 */
  165. #define BOOT_API_OTP_MODE_WORD_NB 0
  166. /* Closed = OTP_CFG0[6] */
  167. #define BOOT_API_OTP_MODE_CLOSED_BIT_POS 6
  168. #define BOOT_API_RETURN_OK 0x77U
  169. /*
  170. * Boot Context related definitions
  171. */
  172. /*
  173. * Boot core boot configuration structure
  174. * Specifies all items of the cold boot configuration
  175. * Memory and peripheral part.
  176. */
  177. typedef struct {
  178. /*
  179. * Boot interface used to boot : take values from defines
  180. * BOOT_API_CTX_BOOT_INTERFACE_SEL_XXX above
  181. */
  182. uint16_t boot_interface_selected;
  183. uint16_t boot_interface_instance;
  184. #if STM32MP13
  185. uint32_t reserved1[12];
  186. #endif
  187. #if STM32MP15
  188. uint32_t reserved1[13];
  189. #endif
  190. uint32_t otp_afmux_values[3];
  191. uint32_t reserved[3];
  192. #if STM32MP15
  193. uint32_t reserved2[2];
  194. #endif
  195. uint32_t auth_status;
  196. #if STM32MP15
  197. /*
  198. * Pointers to bootROM External Secure Services
  199. * - ECDSA verify signature
  200. */
  201. uint32_t reserved3;
  202. uint32_t (*bootrom_ecdsa_verify_signature)(uint8_t *hash_in,
  203. uint8_t *pubkey_in,
  204. uint8_t *signature,
  205. uint32_t ecc_algo);
  206. uint32_t reserved4;
  207. #endif
  208. /*
  209. * Information specific to an SD boot
  210. * Updated each time an SD boot is at least attempted,
  211. * even if not successful
  212. * Note : This is useful to understand why an SD boot failed
  213. * in particular
  214. */
  215. uint32_t sd_err_internal_timeout_cnt;
  216. uint32_t sd_err_dcrc_fail_cnt;
  217. uint32_t sd_err_dtimeout_cnt;
  218. uint32_t sd_err_ctimeout_cnt;
  219. uint32_t sd_err_ccrc_fail_cnt;
  220. uint32_t sd_overall_retry_cnt;
  221. /*
  222. * Information specific to an eMMC boot
  223. * Updated each time an eMMC boot is at least attempted,
  224. * even if not successful
  225. * Note : This is useful to understand why an eMMC boot failed
  226. * in particular
  227. */
  228. uint32_t emmc_xfer_status;
  229. uint32_t emmc_error_status;
  230. uint32_t emmc_nbbytes_rxcopied_tosysram_download_area;
  231. uint32_t hse_clock_value_in_hz;
  232. /*
  233. * Boot partition :
  234. * ie FSBL partition on which the boot was successful
  235. */
  236. uint32_t boot_partition_used_toboot;
  237. } __packed boot_api_context_t;
  238. /*
  239. * Image Header related definitions
  240. */
  241. /*
  242. * Structure used to define the common Header format used for FSBL, xloader,
  243. * ... and in particular used by bootROM for FSBL header readout.
  244. * FSBL header size is 256 Bytes = 0x100
  245. */
  246. typedef struct {
  247. /* BOOT_API_IMAGE_HEADER_MAGIC_NB */
  248. uint32_t magic;
  249. uint8_t image_signature[BOOT_API_ECDSA_SIGNATURE_LEN_IN_BYTES];
  250. /*
  251. * Checksum of payload
  252. * 32-bit sum all payload bytes considered as 8 bit unsigned
  253. * numbers, discarding any overflow bits.
  254. * Use to check UART/USB downloaded image integrity when signature
  255. * is not used
  256. */
  257. uint32_t payload_checksum;
  258. /* Image header version : should have value BOOT_API_HEADER_VERSION */
  259. uint32_t header_version;
  260. /* Image length in bytes */
  261. uint32_t image_length;
  262. /*
  263. * Image Entry point address : should be in the SYSRAM area
  264. * and at least within the download area range
  265. */
  266. uint32_t image_entry_point;
  267. /* Reserved */
  268. uint32_t reserved1;
  269. /*
  270. * Image load address : not used by bootROM but to be consistent
  271. * with header format for other packages (xloader, ...)
  272. */
  273. uint32_t load_address;
  274. /* Reserved */
  275. uint32_t reserved2;
  276. /* Image version to be compared by bootROM with monotonic
  277. * counter value in OTP_CFG4 prior executing the downloaded image
  278. */
  279. uint32_t image_version;
  280. #if STM32MP13
  281. /*
  282. * Extension flags :
  283. *
  284. * Bit 0 : Authentication extension header
  285. * value 0 : No signature check request
  286. * Bit 1 : Encryption extension header
  287. * Bit 2 : Padding extension header
  288. */
  289. uint32_t extension_flags;
  290. /* Length in bytes of all extension headers */
  291. uint32_t extension_headers_length;
  292. /* Add binary type information */
  293. uint32_t binary_type;
  294. /* Pad up to 128 byte total size */
  295. uint8_t pad[16];
  296. /* Followed by extension header */
  297. uint8_t ext_header[];
  298. #endif
  299. #if STM32MP15
  300. /*
  301. * Option flags:
  302. * Bit 0 : No signature check request : 'No_sig_check'
  303. * value 1 : for No signature check request
  304. * value 0 : No request to bypass the signature check
  305. * Note : No signature check is never allowed on a Secured chip
  306. */
  307. uint32_t option_flags;
  308. /*
  309. * Type of ECC algorithm to use :
  310. * value 1 : for P-256 NIST algorithm
  311. * value 2 : for Brainpool 256 algorithm
  312. * See definitions 'BOOT_API_ECDSA_ALGO_TYPE_XXX' above.
  313. */
  314. uint32_t ecc_algo_type;
  315. /*
  316. * OEM ECC Public Key (aka Root pubk) provided in header on 512 bits.
  317. * The SHA-256 hash of the OEM ECC pubk must match the one stored
  318. * in OTP cells.
  319. */
  320. uint8_t ecc_pubk[BOOT_API_ECDSA_PUB_KEY_LEN_IN_BYTES];
  321. /* Pad up to 256 byte total size */
  322. uint8_t pad[83];
  323. /* Add binary type information */
  324. uint8_t binary_type;
  325. #endif
  326. } __packed boot_api_image_header_t;
  327. typedef uint8_t boot_api_sha256_t[BOOT_API_SHA256_DIGEST_SIZE_IN_BYTES];
  328. typedef struct {
  329. /* Extension header type:
  330. * BOOT_API_FSBL_DECRYPTION_HEADER_MAGIC_NB or
  331. * BOOT_API_AUTHENTICATION_HEADER_MAGIC_NB
  332. * BOOT_API_PADDING_HEADER_MAGIC_NB
  333. */
  334. uint32_t type;
  335. /* Extension header len in byte */
  336. uint32_t len;
  337. /* parameters of this extension */
  338. uint8_t params[];
  339. } __packed boot_extension_header_t;
  340. typedef struct {
  341. /* Idx of ECDSA public key to be used in table */
  342. uint32_t pk_idx;
  343. /* Number of ECDSA public key in table */
  344. uint32_t nb_pk;
  345. /*
  346. * Type of ECC algorithm to use :
  347. * value 1 : for P-256 NIST algorithm
  348. * value 2 : for Brainpool 256 algorithm
  349. * See definitions 'BOOT_API_ECDSA_ALGO_TYPE_XXX' above.
  350. */
  351. uint32_t ecc_algo_type;
  352. /* ECDSA public key to be used to check signature. */
  353. uint8_t ecc_pubk[BOOT_API_ECDSA_PUB_KEY_LEN_IN_BYTES];
  354. /* table of Hash of Algo+ECDSA public key */
  355. boot_api_sha256_t pk_hashes[];
  356. } __packed boot_ext_header_params_authentication_t;
  357. typedef struct {
  358. /* Size of encryption key (128 or 256) */
  359. uint32_t key_size;
  360. uint32_t derivation_cont;
  361. /* 128 msb bits of plain payload SHA256 */
  362. uint32_t hash[4];
  363. } __packed boot_ext_header_params_encrypted_fsbl_t;
  364. #endif /* BOOT_API_H */