boot_api.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Copyright (c) 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. * Exported constants
  12. */
  13. /*
  14. * Boot Context related definitions
  15. */
  16. /*
  17. * Possible value of boot context field 'auth_status'
  18. */
  19. /* No authentication done */
  20. #define BOOT_API_CTX_AUTH_NO 0x0U
  21. /* Authentication done and failed */
  22. #define BOOT_API_CTX_AUTH_FAILED 0x1U
  23. /* Authentication done and succeeded */
  24. #define BOOT_API_CTX_AUTH_SUCCESS 0x2U
  25. /*
  26. * Possible value of boot context field 'boot_interface_sel'
  27. */
  28. /* Value of field 'boot_interface_sel' when no boot occurred */
  29. #define BOOT_API_CTX_BOOT_INTERFACE_SEL_NO 0x0U
  30. /* Boot occurred on SD */
  31. #define BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD 0x1U
  32. /* Boot occurred on EMMC */
  33. #define BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC 0x2U
  34. /* Boot occurred on FMC */
  35. #define BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC 0x3U
  36. /* Boot occurred on OSPI NOR */
  37. #define BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_SPI 0x4U
  38. /* Boot occurred on UART */
  39. #define BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART 0x5U
  40. /* Boot occurred on USB */
  41. #define BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB 0x6U
  42. /* Boot occurred on OSPI NAND */
  43. #define BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_SPI 0x7U
  44. /* Boot occurred on HyperFlash QSPI */
  45. #define BOOT_API_CTX_BOOT_INTERFACE_SEL_HYPERFLASH_OSPI 0x8U
  46. /*
  47. * Possible value of boot context field 'emmc_xfer_status'
  48. */
  49. #define BOOT_API_CTX_EMMC_XFER_STATUS_NOT_STARTED 0x0U
  50. #define BOOT_API_CTX_EMMC_XFER_STATUS_DATAEND_DETECTED 0x1U
  51. #define BOOT_API_CTX_EMMC_XFER_STATUS_XFER_DATA_TIMEOUT 0x2U
  52. /*
  53. * Possible value of boot context field 'emmc_error_status'
  54. */
  55. #define BOOT_API_CTX_EMMC_ERROR_STATUS_NONE 0x0U
  56. #define BOOT_API_CTX_EMMC_ERROR_STATUS_CMD_TIMEOUT 0x1U
  57. #define BOOT_API_CTX_EMMC_ERROR_STATUS_ACK_TIMEOUT 0x2U
  58. #define BOOT_API_CTX_EMMC_ERROR_STATUS_DATA_CRC_FAIL 0x3U
  59. #define BOOT_API_CTX_EMMC_ERROR_STATUS_NOT_ENOUGH_BOOT_DATA_RX 0x4U
  60. #define BOOT_API_CTX_EMMC_ERROR_STATUS_HEADER_NOT_FOUND 0x5U
  61. #define BOOT_API_CTX_EMMC_ERROR_STATUS_HEADER_SIZE_ZERO 0x6U
  62. #define BOOT_API_CTX_EMMC_ERROR_STATUS_IMAGE_NOT_COMPLETE 0x7U
  63. #define BOOT_API_CTX_EMMC_ERROR_STATUS_ACK_ERROR 0x8U
  64. /* Definitions relative to 'p_rom_version_info->platform_type_ver' field */
  65. #define BOOT_API_CTX_ROM_VERSION_PLAT_VER_IC_EMU_FPGA 0xAA
  66. #define BOOT_API_CTX_ROM_VERSION_PLAT_VER_FPGA_ONLY 0xBB
  67. /* Image Header related definitions */
  68. /* Definition of header version */
  69. #define BOOT_API_HEADER_VERSION 0x00020200U
  70. /*
  71. * Magic number used to detect header in memory
  72. * Its value must be 'S' 'T' 'M' 0x32, i.e 0x324D5453 as field
  73. * 'bootapi_image_header_t.magic'
  74. * This identifies the start of a boot image.
  75. */
  76. #define BOOT_API_IMAGE_HEADER_MAGIC_NB 0x324D5453U
  77. /* Definitions related to Authentication used in image header structure */
  78. #define BOOT_API_ECDSA_PUB_KEY_LEN_IN_BYTES 64
  79. #define BOOT_API_ECDSA_SIGNATURE_LEN_IN_BYTES 64
  80. #define BOOT_API_SHA256_DIGEST_SIZE_IN_BYTES 32
  81. /* Possible values of the field 'boot_api_image_header_t.ecc_algo_type' */
  82. #define BOOT_API_ECDSA_ALGO_TYPE_P256NIST 1
  83. #define BOOT_API_ECDSA_ALGO_TYPE_BRAINPOOL256 2
  84. /*
  85. * Extension headers related definitions
  86. */
  87. /* 'bootapi_image_header_t.extension_flag' used for authentication feature */
  88. #define BOOT_API_AUTHENTICATION_EXTENSION_BIT BIT(0)
  89. /* 'bootapi_image_header_t.extension_flag' used for FSBL decryption feature */
  90. #define BOOT_API_FSBL_DECRYPTION_EXTENSION_BIT BIT(1)
  91. /* 'bootapi_image_header_t.extension_flag' used for padding header feature */
  92. #define BOOT_API_PADDING_EXTENSION_BIT BIT(31)
  93. /*
  94. * mask of bits of field 'bootapi_image_header_t.extension_flag'
  95. * used for extension headers
  96. */
  97. #define BOOT_API_ALL_EXTENSIONS_MASK \
  98. (BOOT_API_AUTHENTICATION_EXTENSION_BIT | \
  99. BOOT_API_FSBL_DECRYPTION_EXTENSION_BIT | \
  100. BOOT_API_PADDING_EXTENSION_BIT)
  101. /*
  102. * Magic number of FSBL decryption extension header
  103. * The value shall gives the four bytes 'S','T',0x00,0x01 in memory
  104. */
  105. #define BOOT_API_FSBL_DECRYPTION_HEADER_MAGIC_NB 0x01005453U
  106. /*
  107. * Magic number of PKH revocation extension header
  108. * The value shall gives the four bytes 'S','T',0x00,0x02 in memory
  109. */
  110. #define BOOT_API_AUTHENTICATION_HEADER_MAGIC_NB 0x02005453U
  111. /* Max number of ECDSA public key hash in table */
  112. #define BOOT_API_AUTHENTICATION_NB_PKH_MAX 8U
  113. /* ECDSA public key hash table size in bytes */
  114. #define BOOT_API_AUTHENTICATION_TABLE_SIZE_BYTES \
  115. (BOOT_API_AUTHENTICATION_NB_PKH_MAX * \
  116. BOOT_API_SHA256_DIGEST_SIZE_IN_BYTES)
  117. /*
  118. * Magic number of padding extension header
  119. * The value shall gives the four bytes 'S','T',0xFF,0xFF in memory
  120. */
  121. #define BOOT_API_PADDING_HEADER_MAGIC_NB 0xFFFF5453U
  122. /*
  123. * Related to binaryType
  124. * 0x00: U-Boot
  125. * 0x10-0x1F: TF-A
  126. * 0x20-0X2F: OPTEE
  127. * 0x30: CM33 image
  128. */
  129. #define BOOT_API_IMAGE_TYPE_UBOOT 0x0
  130. #define BOOT_API_IMAGE_TYPE_M33 0x30
  131. /*
  132. * Cores secure magic numbers
  133. * Constant to be stored in bakcup register
  134. * BOOT_API_MAGIC_NUMBER_TAMP_BCK_REG_IDX
  135. */
  136. #define BOOT_API_A35_CORE0_MAGIC_NUMBER 0xCA7FACE0U
  137. #define BOOT_API_A35_CORE1_MAGIC_NUMBER 0xCA7FACE1U
  138. /*
  139. * TAMP_BCK9R register index
  140. * This register is used to write a Magic Number in order to restart
  141. * Cortex A35 Core 1 and make it execute @ branch address from TAMP_BCK5R
  142. */
  143. #define BOOT_API_CORE1_MAGIC_NUMBER_TAMP_BCK_REG_IDX 9U
  144. /*
  145. * TAMP_BCK10R register index
  146. * This register is used to contain the branch address of
  147. * Cortex A35 Core 1 when restarted by a TAMP_BCK4R magic number writing
  148. */
  149. #define BOOT_API_CORE1_BRANCH_ADDRESS_TAMP_BCK_REG_IDX 10U
  150. /*
  151. * Possible value of boot context field 'hse_clock_value_in_hz'
  152. */
  153. #define BOOT_API_CTX_HSE_CLOCK_VALUE_UNDEFINED 0U
  154. #define BOOT_API_CTX_HSE_CLOCK_VALUE_19_2_MHZ 19200000U
  155. #define BOOT_API_CTX_HSE_CLOCK_VALUE_24_MHZ 24000000U
  156. #define BOOT_API_CTX_HSE_CLOCK_VALUE_25_MHZ 25000000U
  157. #define BOOT_API_CTX_HSE_CLOCK_VALUE_26_MHZ 26000000U
  158. #define BOOT_API_CTX_HSE_CLOCK_VALUE_40_MHZ 40000000U
  159. #define BOOT_API_CTX_HSE_CLOCK_VALUE_48_MHZ 48000000U
  160. /*
  161. * Possible value of boot context field 'boot_partition_used_toboot'
  162. */
  163. #define BOOT_API_CTX_BOOT_PARTITION_UNDEFINED 0U
  164. /* Used FSBL1 to boot */
  165. #define BOOT_API_CTX_BOOT_PARTITION_FSBL1 1U
  166. /* Used FSBL2 to boot */
  167. #define BOOT_API_CTX_BOOT_PARTITION_FSBL2 2U
  168. #define BOOT_API_RETURN_OK 0x66U
  169. /*
  170. * Possible values of boot context field
  171. * 'ssp_config_ptr_in->ssp_cmd'
  172. */
  173. /* 'K' 'B' 'U' 'P' -.> 'PUBK' */
  174. #define BOOT_API_CTX_SSP_CMD_CALC_CHIP_PUBK 0x4B425550
  175. /*
  176. * Exported types
  177. */
  178. /*
  179. * bootROM version information structure definition
  180. * Total size = 24 bytes = 6 uint32_t
  181. */
  182. typedef struct {
  183. /* Chip Version */
  184. uint32_t chip_ver;
  185. /* Cut version within a fixed chip version */
  186. uint32_t cut_ver;
  187. /* Version of ROM Mask within a fixed cut version */
  188. uint32_t rom_mask_ver;
  189. /* Internal Version of bootROM code */
  190. uint32_t bootrom_ver;
  191. /* Version of bootROM adapted */
  192. uint32_t for_chip_design_rtl_ver;
  193. /* Restriction on compiled platform when it applies */
  194. uint32_t platform_type_ver;
  195. } boot_api_rom_version_info_t;
  196. /*
  197. * Boot Context related definitions
  198. */
  199. /*
  200. * Boot core boot configuration structure
  201. * Specifies all items of the secure boot configuration
  202. * Memory and peripheral part.
  203. */
  204. typedef struct {
  205. /* Boot partition: ie FSBL partition on which the boot was successful */
  206. uint32_t boot_partition_used_toboot;
  207. uint32_t reserved1[3];
  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 reserved[4];
  232. /*
  233. * Boot interface used to boot : take values from defines
  234. * BOOT_API_CTX_BOOT_INTERFACE_SEL_XXX above
  235. */
  236. uint16_t boot_interface_selected;
  237. uint16_t boot_interface_instance;
  238. uint32_t hse_clock_value_in_hz;
  239. uint32_t nand_fsbl_first_block;
  240. /*
  241. * Returned authentication status : take values from defines
  242. * BOOT_API_CTX_AUTH_XXX above
  243. */
  244. uint32_t auth_status;
  245. /* Pointer on ROM constant containing ROM information */
  246. const boot_api_rom_version_info_t *p_rom_version_info;
  247. } __packed boot_api_context_t;
  248. /*
  249. * Image Header related definitions
  250. */
  251. /*
  252. * Structure used to define the common Header format used for FSBL, xloader,
  253. * ... and in particular used by bootROM for FSBL header readout.
  254. * FSBL header size is 256 Bytes = 0x100
  255. */
  256. typedef struct {
  257. /* BOOT_API_IMAGE_HEADER_MAGIC_NB */
  258. uint32_t magic;
  259. uint8_t image_signature[BOOT_API_ECDSA_SIGNATURE_LEN_IN_BYTES];
  260. /*
  261. * Checksum of payload
  262. * 32-bit sum all payload bytes considered as 8 bit unsigned
  263. * numbers, discarding any overflow bits.
  264. * Use to check UART/USB downloaded image integrity when signature
  265. * is not used
  266. */
  267. uint32_t payload_checksum;
  268. /* Image header version : should have value BOOT_API_HEADER_VERSION */
  269. uint32_t header_version;
  270. /* Image length in bytes */
  271. uint32_t image_length;
  272. /*
  273. * Image Entry point address : should be in the SYSRAM area
  274. * and at least within the download area range
  275. */
  276. uint32_t image_entry_point;
  277. /* Reserved */
  278. uint32_t reserved1;
  279. /*
  280. * Image load address : not used by bootROM but to be consistent
  281. * with header format for other packages (xloader, ...)
  282. */
  283. uint32_t load_address;
  284. /* Reserved */
  285. uint32_t reserved2;
  286. /* Image version to be compared by bootROM with FSBL_A or FSBL_M version
  287. * counter value in OTP prior executing the downloaded image
  288. */
  289. uint32_t image_version;
  290. /*
  291. * Extension flags :
  292. *
  293. * Bit 0 : Authentication extension header
  294. * value 0 : No signature check request
  295. * Bit 1 : Encryption extension header
  296. * Bit 2 : Padding extension header
  297. */
  298. uint32_t extension_flags;
  299. /* Length in bytes of all extension headers */
  300. uint32_t extension_headers_length;
  301. /* Add binary type information */
  302. uint32_t binary_type;
  303. /* Pad up to 128 byte total size */
  304. uint8_t pad[16];
  305. /* Followed by extension header */
  306. uint8_t ext_header[];
  307. } __packed boot_api_image_header_t;
  308. typedef uint8_t boot_api_sha256_t[BOOT_API_SHA256_DIGEST_SIZE_IN_BYTES];
  309. typedef struct {
  310. /* Extension header type:
  311. * BOOT_API_FSBL_DECRYPTION_HEADER_MAGIC_NB or
  312. * BOOT_API_AUTHENTICATION_HEADER_MAGIC_NB
  313. * BOOT_API_PADDING_HEADER_MAGIC_NB
  314. */
  315. uint32_t type;
  316. /* Extension header len in byte */
  317. uint32_t len;
  318. /* parameters of this extension */
  319. uint8_t params[];
  320. } __packed boot_extension_header_t;
  321. typedef struct {
  322. /* Idx of ECDSA public key to be used in table */
  323. uint32_t pk_idx;
  324. /* Number of ECDSA public key in table */
  325. uint32_t nb_pk;
  326. /*
  327. * Type of ECC algorithm to use :
  328. * value 1 : for P-256 NIST algorithm
  329. * value 2 : for Brainpool 256 algorithm
  330. * See definitions 'BOOT_API_ECDSA_ALGO_TYPE_XXX' above.
  331. */
  332. uint32_t ecc_algo_type;
  333. /* ECDSA public key to be used to check signature. */
  334. uint8_t ecc_pubk[BOOT_API_ECDSA_PUB_KEY_LEN_IN_BYTES];
  335. /* table of Hash of Algo+ECDSA public key */
  336. boot_api_sha256_t pk_hashes[];
  337. } __packed boot_ext_header_params_authentication_t;
  338. typedef struct {
  339. /* Size of encryption key (128 or 256) */
  340. uint32_t key_size;
  341. uint32_t derivation_cont;
  342. /* 128 msb bits of plain payload SHA256 */
  343. uint32_t hash[4];
  344. } __packed boot_ext_header_params_encrypted_fsbl_t;
  345. #endif /* BOOT_API_H */