xref: /rk3399_ARM-atf/plat/st/stm32mp1/include/boot_api.h (revision 9083fa11ead67272b94329e8f84257de6658620d)
14353bb20SYann Gautier /*
2fa92fef0SPatrick Delaunay  * Copyright (c) 2017-2021, STMicroelectronics - All Rights Reserved
34353bb20SYann Gautier  *
44353bb20SYann Gautier  * SPDX-License-Identifier: BSD-3-Clause
54353bb20SYann Gautier  */
64353bb20SYann Gautier 
7c3cf06f1SAntonio Nino Diaz #ifndef BOOT_API_H
8c3cf06f1SAntonio Nino Diaz #define BOOT_API_H
94353bb20SYann Gautier 
104353bb20SYann Gautier #include <stdint.h>
11aec7de41SYann Gautier #include <stdio.h>
124353bb20SYann Gautier 
134353bb20SYann Gautier /*
144bdb1a7aSLionel Debieve  * Possible value of boot context field 'auth_status'
154bdb1a7aSLionel Debieve  */
164bdb1a7aSLionel Debieve /* No authentication done */
174bdb1a7aSLionel Debieve #define BOOT_API_CTX_AUTH_NO					0x0U
184bdb1a7aSLionel Debieve /* Authentication done and failed */
194bdb1a7aSLionel Debieve #define BOOT_API_CTX_AUTH_FAILED				0x1U
204bdb1a7aSLionel Debieve /* Authentication done and succeeded */
214bdb1a7aSLionel Debieve #define BOOT_API_CTX_AUTH_SUCCESS				0x2U
224bdb1a7aSLionel Debieve 
234bdb1a7aSLionel Debieve /*
244353bb20SYann Gautier  * Possible value of boot context field 'boot_interface_sel'
254353bb20SYann Gautier  */
264353bb20SYann Gautier 
274353bb20SYann Gautier /* Value of field 'boot_interface_sel' when no boot occurred */
284353bb20SYann Gautier #define BOOT_API_CTX_BOOT_INTERFACE_SEL_NO			0x0U
294353bb20SYann Gautier 
304353bb20SYann Gautier /* Boot occurred on SD */
314353bb20SYann Gautier #define BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD		0x1U
324353bb20SYann Gautier 
334353bb20SYann Gautier /* Boot occurred on EMMC */
344353bb20SYann Gautier #define BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC		0x2U
354353bb20SYann Gautier 
3612e21dfdSLionel Debieve /* Boot occurred on FMC */
3712e21dfdSLionel Debieve #define BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC		0x3U
3812e21dfdSLionel Debieve 
39b1b218fbSLionel Debieve /* Boot occurred on QSPI NOR */
40b1b218fbSLionel Debieve #define BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_QSPI		0x4U
41b1b218fbSLionel Debieve 
42*9083fa11SPatrick Delaunay /* Boot occurred on UART */
43*9083fa11SPatrick Delaunay #define BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART		0x5U
44*9083fa11SPatrick Delaunay 
45fa92fef0SPatrick Delaunay /* Boot occurred on USB */
46fa92fef0SPatrick Delaunay #define BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB		0x6U
47fa92fef0SPatrick Delaunay 
4857044228SLionel Debieve /* Boot occurred on QSPI NAND */
4957044228SLionel Debieve #define BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_QSPI		0x7U
5057044228SLionel Debieve 
514353bb20SYann Gautier /**
524353bb20SYann Gautier  * @brief  Possible value of boot context field 'EmmcXferStatus'
534353bb20SYann Gautier  */
544353bb20SYann Gautier /*
554353bb20SYann Gautier  * Possible value of boot context field 'emmc_xfer_status'
564353bb20SYann Gautier  */
574353bb20SYann Gautier #define BOOT_API_CTX_EMMC_XFER_STATUS_NOT_STARTED			0x0U
584353bb20SYann Gautier #define BOOT_API_CTX_EMMC_XFER_STATUS_DATAEND_DETECTED			0x1U
594353bb20SYann Gautier #define BOOT_API_CTX_EMMC_XFER_STATUS_XFER_OVERALL_TIMEOUT_DETECTED	0x2U
604353bb20SYann Gautier #define BOOT_API_CTX_EMMC_XFER_STATUS_XFER_DATA_TIMEOUT			0x3U
614353bb20SYann Gautier 
624353bb20SYann Gautier /*
634353bb20SYann Gautier  * Possible value of boot context field 'emmc_error_status'
644353bb20SYann Gautier  */
654353bb20SYann Gautier #define BOOT_API_CTX_EMMC_ERROR_STATUS_NONE                     0x0U
664353bb20SYann Gautier #define BOOT_API_CTX_EMMC_ERROR_STATUS_CMD_TIMEOUT              0x1U
674353bb20SYann Gautier #define BOOT_API_CTX_EMMC_ERROR_STATUS_ACK_TIMEOUT              0x2U
684353bb20SYann Gautier #define BOOT_API_CTX_EMMC_ERROR_STATUS_DATA_CRC_FAIL            0x3U
694353bb20SYann Gautier #define BOOT_API_CTX_EMMC_ERROR_STATUS_NOT_ENOUGH_BOOT_DATA_RX  0x4U
704353bb20SYann Gautier #define BOOT_API_CTX_EMMC_ERROR_STATUS_HEADER_NOT_FOUND         0x5U
714353bb20SYann Gautier #define BOOT_API_CTX_EMMC_ERROR_STATUS_HEADER_SIZE_ZERO         0x6U
724353bb20SYann Gautier #define BOOT_API_CTX_EMMC_ERROR_STATUS_IMAGE_NOT_COMPLETE       0x7U
734353bb20SYann Gautier 
744353bb20SYann Gautier /* Image Header related definitions */
754353bb20SYann Gautier 
764353bb20SYann Gautier /* Definition of header version */
774353bb20SYann Gautier #define BOOT_API_HEADER_VERSION					0x00010000U
784353bb20SYann Gautier 
794353bb20SYann Gautier /*
804353bb20SYann Gautier  * Magic number used to detect header in memory
814353bb20SYann Gautier  * Its value must be 'S' 'T' 'M' 0x32, i.e 0x324D5453 as field
824353bb20SYann Gautier  * 'bootapi_image_header_t.magic'
834353bb20SYann Gautier  * This identifies the start of a boot image.
844353bb20SYann Gautier  */
854353bb20SYann Gautier #define BOOT_API_IMAGE_HEADER_MAGIC_NB				0x324D5453U
864353bb20SYann Gautier 
874353bb20SYann Gautier /* Definitions related to Authentication used in image header structure */
884353bb20SYann Gautier #define BOOT_API_ECDSA_PUB_KEY_LEN_IN_BYTES			64
894353bb20SYann Gautier #define BOOT_API_ECDSA_SIGNATURE_LEN_IN_BYTES			64
904353bb20SYann Gautier #define BOOT_API_SHA256_DIGEST_SIZE_IN_BYTES			32
914353bb20SYann Gautier 
924353bb20SYann Gautier /* Possible values of the field 'boot_api_image_header_t.ecc_algo_type' */
934353bb20SYann Gautier #define BOOT_API_ECDSA_ALGO_TYPE_P256NIST			1
944353bb20SYann Gautier #define BOOT_API_ECDSA_ALGO_TYPE_BRAINPOOL256			2
954353bb20SYann Gautier 
964353bb20SYann Gautier /*
974353bb20SYann Gautier  * Cores secure magic numbers
984353bb20SYann Gautier  * Constant to be stored in bakcup register
994353bb20SYann Gautier  * BOOT_API_MAGIC_NUMBER_TAMP_BCK_REG_IDX
1004353bb20SYann Gautier  */
1014353bb20SYann Gautier #define BOOT_API_A7_CORE0_MAGIC_NUMBER				0xCA7FACE0U
1024353bb20SYann Gautier #define BOOT_API_A7_CORE1_MAGIC_NUMBER				0xCA7FACE1U
1034353bb20SYann Gautier 
1044353bb20SYann Gautier /*
1054353bb20SYann Gautier  * TAMP_BCK4R register index
1064353bb20SYann Gautier  * This register is used to write a Magic Number in order to restart
1074353bb20SYann Gautier  * Cortex A7 Core 1 and make it execute @ branch address from TAMP_BCK5R
1084353bb20SYann Gautier  */
1094353bb20SYann Gautier #define BOOT_API_CORE1_MAGIC_NUMBER_TAMP_BCK_REG_IDX		4U
1104353bb20SYann Gautier 
1114353bb20SYann Gautier /*
1124353bb20SYann Gautier  * TAMP_BCK5R register index
1134353bb20SYann Gautier  * This register is used to contain the branch address of
1144353bb20SYann Gautier  * Cortex A7 Core 1 when restarted by a TAMP_BCK4R magic number writing
1154353bb20SYann Gautier  */
1164353bb20SYann Gautier #define BOOT_API_CORE1_BRANCH_ADDRESS_TAMP_BCK_REG_IDX		5U
1174353bb20SYann Gautier 
1184353bb20SYann Gautier /*
1194353bb20SYann Gautier  * Possible value of boot context field 'hse_clock_value_in_hz'
1204353bb20SYann Gautier  */
1214353bb20SYann Gautier #define BOOT_API_CTX_HSE_CLOCK_VALUE_UNDEFINED			0U
1224353bb20SYann Gautier #define BOOT_API_CTX_HSE_CLOCK_VALUE_24_MHZ			24000000U
1234353bb20SYann Gautier #define BOOT_API_CTX_HSE_CLOCK_VALUE_25_MHZ			25000000U
1244353bb20SYann Gautier #define BOOT_API_CTX_HSE_CLOCK_VALUE_26_MHZ			26000000U
1254353bb20SYann Gautier 
1264353bb20SYann Gautier /*
1274353bb20SYann Gautier  * Possible value of boot context field 'boot_partition_used_toboot'
1284353bb20SYann Gautier  */
1294353bb20SYann Gautier #define BOOT_API_CTX_BOOT_PARTITION_UNDEFINED			0U
1304353bb20SYann Gautier 
1314353bb20SYann Gautier /* Used FSBL1 to boot */
1324353bb20SYann Gautier #define BOOT_API_CTX_BOOT_PARTITION_FSBL1			1U
1334353bb20SYann Gautier 
1344353bb20SYann Gautier /* Used FSBL2 to boot */
1354353bb20SYann Gautier #define BOOT_API_CTX_BOOT_PARTITION_FSBL2			2U
1364353bb20SYann Gautier 
1374353bb20SYann Gautier /* OTP_CFG0 */
1384353bb20SYann Gautier #define BOOT_API_OTP_MODE_WORD_NB				0
1394353bb20SYann Gautier /* Closed = OTP_CFG0[6] */
1404353bb20SYann Gautier #define BOOT_API_OTP_MODE_CLOSED_BIT_POS			6
1414353bb20SYann Gautier 
142697d18aeSLionel Debieve #define BOOT_API_RETURN_OK					0x77U
1434bdb1a7aSLionel Debieve 
1444353bb20SYann Gautier /*
1454353bb20SYann Gautier  * Boot Context related definitions
1464353bb20SYann Gautier  */
1474353bb20SYann Gautier 
1484353bb20SYann Gautier /*
1494353bb20SYann Gautier  * Boot core boot configuration structure
1504353bb20SYann Gautier  * Specifies all items of the cold boot configuration
1514353bb20SYann Gautier  * Memory and peripheral part.
1524353bb20SYann Gautier  */
1534353bb20SYann Gautier typedef struct {
1544353bb20SYann Gautier 	/*
1554353bb20SYann Gautier 	 * Boot interface used to boot : take values from defines
1564353bb20SYann Gautier 	 * BOOT_API_CTX_BOOT_INTERFACE_SEL_XXX above
1574353bb20SYann Gautier 	 */
1584353bb20SYann Gautier 	uint16_t boot_interface_selected;
1594353bb20SYann Gautier 	uint16_t boot_interface_instance;
1604353bb20SYann Gautier 	uint32_t reserved1[13];
1614353bb20SYann Gautier 	uint32_t otp_afmux_values[3];
1624bdb1a7aSLionel Debieve 	uint32_t reserved[5];
1634bdb1a7aSLionel Debieve 	uint32_t auth_status;
1644bdb1a7aSLionel Debieve 
1654bdb1a7aSLionel Debieve 	/*
1664bdb1a7aSLionel Debieve 	 * Pointers to bootROM External Secure Services
1674bdb1a7aSLionel Debieve 	 * - ECDSA check key
1684bdb1a7aSLionel Debieve 	 * - ECDSA verify signature
1694bdb1a7aSLionel Debieve 	 * - ECDSA verify signature and go
1704bdb1a7aSLionel Debieve 	 */
1714bdb1a7aSLionel Debieve 	uint32_t (*bootrom_ecdsa_check_key)(uint8_t *pubkey_in,
1724bdb1a7aSLionel Debieve 					    uint8_t *pubkey_out);
1734bdb1a7aSLionel Debieve 	uint32_t (*bootrom_ecdsa_verify_signature)(uint8_t *hash_in,
1744bdb1a7aSLionel Debieve 						   uint8_t *pubkey_in,
1754bdb1a7aSLionel Debieve 						   uint8_t *signature,
1764bdb1a7aSLionel Debieve 						   uint32_t ecc_algo);
1774bdb1a7aSLionel Debieve 	uint32_t (*bootrom_ecdsa_verify_and_go)(uint8_t *hash_in,
1784bdb1a7aSLionel Debieve 						uint8_t *pub_key_in,
1794bdb1a7aSLionel Debieve 						uint8_t *signature,
1804bdb1a7aSLionel Debieve 						uint32_t ecc_algo,
1814bdb1a7aSLionel Debieve 						uint32_t *entry_in);
1824bdb1a7aSLionel Debieve 
1834353bb20SYann Gautier 	/*
1844353bb20SYann Gautier 	 * Information specific to an SD boot
1854353bb20SYann Gautier 	 * Updated each time an SD boot is at least attempted,
1864353bb20SYann Gautier 	 * even if not successful
1874353bb20SYann Gautier 	 * Note : This is useful to understand why an SD boot failed
1884353bb20SYann Gautier 	 * in particular
1894353bb20SYann Gautier 	 */
1904353bb20SYann Gautier 	uint32_t sd_err_internal_timeout_cnt;
1914353bb20SYann Gautier 	uint32_t sd_err_dcrc_fail_cnt;
1924353bb20SYann Gautier 	uint32_t sd_err_dtimeout_cnt;
1934353bb20SYann Gautier 	uint32_t sd_err_ctimeout_cnt;
1944353bb20SYann Gautier 	uint32_t sd_err_ccrc_fail_cnt;
1954353bb20SYann Gautier 	uint32_t sd_overall_retry_cnt;
1964353bb20SYann Gautier 	/*
1974353bb20SYann Gautier 	 * Information specific to an eMMC boot
1984353bb20SYann Gautier 	 * Updated each time an eMMC boot is at least attempted,
1994353bb20SYann Gautier 	 * even if not successful
2004353bb20SYann Gautier 	 * Note : This is useful to understand why an eMMC boot failed
2014353bb20SYann Gautier 	 * in particular
2024353bb20SYann Gautier 	 */
2034353bb20SYann Gautier 	uint32_t emmc_xfer_status;
2044353bb20SYann Gautier 	uint32_t emmc_error_status;
2054353bb20SYann Gautier 	uint32_t emmc_nbbytes_rxcopied_tosysram_download_area;
2064353bb20SYann Gautier 	uint32_t hse_clock_value_in_hz;
2074353bb20SYann Gautier 	/*
2084353bb20SYann Gautier 	 * Boot partition :
2094353bb20SYann Gautier 	 * ie FSBL partition on which the boot was successful
2104353bb20SYann Gautier 	 */
2114353bb20SYann Gautier 	uint32_t boot_partition_used_toboot;
2124353bb20SYann Gautier 
2134353bb20SYann Gautier } __packed boot_api_context_t;
2144353bb20SYann Gautier 
2154353bb20SYann Gautier /*
2164353bb20SYann Gautier  * Image Header related definitions
2174353bb20SYann Gautier  */
2184353bb20SYann Gautier 
2194353bb20SYann Gautier /*
2204353bb20SYann Gautier  * Structure used to define the common Header format used for FSBL, xloader,
2214353bb20SYann Gautier  * ... and in particular used by bootROM for FSBL header readout.
2224353bb20SYann Gautier  * FSBL header size is 256 Bytes = 0x100
2234353bb20SYann Gautier  */
2244353bb20SYann Gautier typedef struct {
2254353bb20SYann Gautier 	/* BOOT_API_IMAGE_HEADER_MAGIC_NB */
2264353bb20SYann Gautier 	uint32_t magic;
2274353bb20SYann Gautier 	uint8_t image_signature[BOOT_API_ECDSA_SIGNATURE_LEN_IN_BYTES];
2284353bb20SYann Gautier 	/*
2294353bb20SYann Gautier 	 * Checksum of payload
2304353bb20SYann Gautier 	 * 32-bit sum all all payload bytes considered as 8 bit unigned numbers,
2314353bb20SYann Gautier 	 * discarding any overflow bits.
2324353bb20SYann Gautier 	 * Use to check UART/USB downloaded image integrity when signature
2334353bb20SYann Gautier 	 * is not used (i.e bit 0 : 'No_sig_check' = 1 in option flags)
2344353bb20SYann Gautier 	 */
2354353bb20SYann Gautier 	uint32_t payload_checksum;
2364353bb20SYann Gautier 	/* Image header version : should have value BOOT_API_HEADER_VERSION */
2374353bb20SYann Gautier 	uint32_t header_version;
2384353bb20SYann Gautier 	/* Image length in bytes */
2394353bb20SYann Gautier 	uint32_t image_length;
2404353bb20SYann Gautier 	/*
2414353bb20SYann Gautier 	 * Image Entry point address : should be in the SYSRAM area
2424353bb20SYann Gautier 	 * and at least within the download area range
2434353bb20SYann Gautier 	 */
2444353bb20SYann Gautier 	uint32_t image_entry_point;
2454353bb20SYann Gautier 	/* Reserved */
2464353bb20SYann Gautier 	uint32_t reserved1;
2474353bb20SYann Gautier 	/*
2484353bb20SYann Gautier 	 * Image load address : not used by bootROM but to be consistent
2494353bb20SYann Gautier 	 * with header format for other packages (xloader, ...)
2504353bb20SYann Gautier 	 */
2514353bb20SYann Gautier 	uint32_t load_address;
2524353bb20SYann Gautier 	/* Reserved */
2534353bb20SYann Gautier 	uint32_t reserved2;
2544353bb20SYann Gautier 	/* Image version to be compared by bootROM with monotonic
2554353bb20SYann Gautier 	 * counter value in OTP_CFG4 prior executing the downloaded image
2564353bb20SYann Gautier 	 */
2574353bb20SYann Gautier 	uint32_t image_version;
2584353bb20SYann Gautier 	/*
2594353bb20SYann Gautier 	 * Option flags:
2604353bb20SYann Gautier 	 * Bit 0 : No signature check request : 'No_sig_check'
2614353bb20SYann Gautier 	 *      value 1 : for No signature check request
2624353bb20SYann Gautier 	 *      value 0 : No request to bypass the signature check
2634353bb20SYann Gautier 	 * Note : No signature check is never allowed on a Secured chip
2644353bb20SYann Gautier 	 */
2654353bb20SYann Gautier 	uint32_t option_flags;
2664353bb20SYann Gautier 	/*
2674353bb20SYann Gautier 	 * Type of ECC algorithm to use  :
2684353bb20SYann Gautier 	 * value 1 : for P-256 NIST algorithm
2694353bb20SYann Gautier 	 * value 2 : for Brainpool 256 algorithm
2704353bb20SYann Gautier 	 * See definitions 'BOOT_API_ECDSA_ALGO_TYPE_XXX' above.
2714353bb20SYann Gautier 	 */
2724353bb20SYann Gautier 	uint32_t ecc_algo_type;
2734353bb20SYann Gautier 	/*
2744353bb20SYann Gautier 	 * OEM ECC Public Key (aka Root pubk) provided in header on 512 bits.
2754353bb20SYann Gautier 	 * The SHA-256 hash of the OEM ECC pubk must match the one stored
2764353bb20SYann Gautier 	 * in OTP cells.
2774353bb20SYann Gautier 	 */
2784353bb20SYann Gautier 	uint8_t ecc_pubk[BOOT_API_ECDSA_PUB_KEY_LEN_IN_BYTES];
2794353bb20SYann Gautier 	/* Pad up to 256 byte total size */
280aec7de41SYann Gautier 	uint8_t pad[83];
281aec7de41SYann Gautier 	/* Add binary type information */
282aec7de41SYann Gautier 	uint8_t binary_type;
2834353bb20SYann Gautier } __packed boot_api_image_header_t;
2844353bb20SYann Gautier 
285c3cf06f1SAntonio Nino Diaz #endif /* BOOT_API_H */
286