fsl_validate.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright 2015 Freescale Semiconductor, Inc.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #ifndef _FSL_VALIDATE_H_
  7. #define _FSL_VALIDATE_H_
  8. #include <fsl_sec.h>
  9. #include <fsl_sec_mon.h>
  10. #include <command.h>
  11. #include <linux/types.h>
  12. #define WORD_SIZE 4
  13. /* Minimum and maximum size of RSA signature length in bits */
  14. #define KEY_SIZE 4096
  15. #define KEY_SIZE_BYTES (KEY_SIZE/8)
  16. #define KEY_SIZE_WORDS (KEY_SIZE_BYTES/(WORD_SIZE))
  17. extern struct jobring jr;
  18. /* Barker code size in bytes */
  19. #define ESBC_BARKER_LEN 4 /* barker code length in ESBC uboot client */
  20. /* header */
  21. /* No-error return values */
  22. #define ESBC_VALID_HDR 0 /* header is valid */
  23. /* Maximum number of SG entries allowed */
  24. #define MAX_SG_ENTRIES 8
  25. /* Different Header Struct for LS-CH3 */
  26. #ifdef CONFIG_ESBC_HDR_LS
  27. struct fsl_secboot_img_hdr {
  28. u8 barker[ESBC_BARKER_LEN]; /* barker code */
  29. u32 srk_tbl_off;
  30. struct {
  31. u8 num_srk;
  32. u8 srk_sel;
  33. u8 reserve;
  34. } len_kr;
  35. u8 ie_flag;
  36. u32 uid_flag;
  37. u32 psign; /* signature offset */
  38. u32 sign_len; /* length of the signature in bytes */
  39. u64 pimg64; /* 64 bit pointer to ESBC Image */
  40. u32 img_size; /* ESBC client image size in bytes */
  41. u32 ie_key_sel;
  42. u32 fsl_uid_0;
  43. u32 fsl_uid_1;
  44. u32 oem_uid_0;
  45. u32 oem_uid_1;
  46. u32 oem_uid_2;
  47. u32 oem_uid_3;
  48. u32 oem_uid_4;
  49. u32 reserved1[3];
  50. };
  51. #ifdef CONFIG_KEY_REVOCATION
  52. /* Srk table and key revocation check */
  53. #define UNREVOCABLE_KEY 8
  54. #define ALIGN_REVOC_KEY 7
  55. #define MAX_KEY_ENTRIES 8
  56. #endif
  57. #if defined(CONFIG_FSL_ISBC_KEY_EXT)
  58. #define IE_FLAG_MASK 0x1
  59. #define SCRATCH_IE_LOW_ADR 13
  60. #define SCRATCH_IE_HIGH_ADR 14
  61. #endif
  62. #else /* CONFIG_ESBC_HDR_LS */
  63. /*
  64. * ESBC uboot client header structure.
  65. * The struct contain the following fields
  66. * barker code
  67. * public key offset
  68. * pub key length
  69. * signature offset
  70. * length of the signature
  71. * ptr to SG table
  72. * no of entries in SG table
  73. * esbc ptr
  74. * size of esbc
  75. * esbc entry point
  76. * Scatter gather flag
  77. * UID flag
  78. * FSL UID
  79. * OEM UID
  80. * Here, pub key is modulus concatenated with exponent
  81. * of equal length
  82. */
  83. struct fsl_secboot_img_hdr {
  84. u8 barker[ESBC_BARKER_LEN]; /* barker code */
  85. union {
  86. u32 pkey; /* public key offset */
  87. #ifdef CONFIG_KEY_REVOCATION
  88. u32 srk_tbl_off;
  89. #endif
  90. };
  91. union {
  92. u32 key_len; /* pub key length in bytes */
  93. #ifdef CONFIG_KEY_REVOCATION
  94. struct {
  95. u32 srk_table_flag:8;
  96. u32 srk_sel:8;
  97. u32 num_srk:16;
  98. } len_kr;
  99. #endif
  100. };
  101. u32 psign; /* signature offset */
  102. u32 sign_len; /* length of the signature in bytes */
  103. union {
  104. u32 psgtable; /* ptr to SG table */
  105. #ifndef CONFIG_ESBC_ADDR_64BIT
  106. u32 pimg; /* ptr to ESBC client image */
  107. #endif
  108. };
  109. union {
  110. u32 sg_entries; /* no of entries in SG table */
  111. u32 img_size; /* ESBC client image size in bytes */
  112. };
  113. u32 img_start; /* ESBC client entry point */
  114. u32 sg_flag; /* Scatter gather flag */
  115. u32 uid_flag;
  116. u32 fsl_uid_0;
  117. u32 oem_uid_0;
  118. u32 reserved1[2];
  119. u32 fsl_uid_1;
  120. u32 oem_uid_1;
  121. union {
  122. u32 reserved2[2];
  123. #ifdef CONFIG_ESBC_ADDR_64BIT
  124. u64 pimg64; /* 64 bit pointer to ESBC Image */
  125. #endif
  126. };
  127. u32 ie_flag;
  128. u32 ie_key_sel;
  129. };
  130. #ifdef CONFIG_KEY_REVOCATION
  131. /* Srk table and key revocation check */
  132. #define SRK_FLAG 0x01
  133. #define UNREVOCABLE_KEY 4
  134. #define ALIGN_REVOC_KEY 3
  135. #define MAX_KEY_ENTRIES 4
  136. #endif
  137. #if defined(CONFIG_FSL_ISBC_KEY_EXT)
  138. #define IE_FLAG_MASK 0xFFFFFFFF
  139. #endif
  140. #endif /* CONFIG_ESBC_HDR_LS */
  141. #if defined(CONFIG_FSL_ISBC_KEY_EXT)
  142. struct ie_key_table {
  143. u32 key_len;
  144. u8 pkey[2 * KEY_SIZE_BYTES];
  145. };
  146. struct ie_key_info {
  147. uint32_t key_revok;
  148. uint32_t num_keys;
  149. struct ie_key_table ie_key_tbl[32];
  150. };
  151. #endif
  152. #ifdef CONFIG_KEY_REVOCATION
  153. struct srk_table {
  154. u32 key_len;
  155. u8 pkey[2 * KEY_SIZE_BYTES];
  156. };
  157. #endif
  158. /*
  159. * SG table.
  160. */
  161. #if defined(CONFIG_FSL_TRUST_ARCH_v1) && defined(CONFIG_FSL_CORENET)
  162. /*
  163. * This struct contains the following fields
  164. * length of the segment
  165. * source address
  166. */
  167. struct fsl_secboot_sg_table {
  168. u32 len; /* length of the segment in bytes */
  169. u32 src_addr; /* ptr to the data segment */
  170. };
  171. #else
  172. /*
  173. * This struct contains the following fields
  174. * length of the segment
  175. * Destination Target ID
  176. * source address
  177. * destination address
  178. */
  179. struct fsl_secboot_sg_table {
  180. u32 len;
  181. u32 trgt_id;
  182. u32 src_addr;
  183. u32 dst_addr;
  184. };
  185. #endif
  186. /* ESBC global structure.
  187. * Data to be used across verification of different images.
  188. * Stores follwoing Data:
  189. * IE Table
  190. */
  191. struct fsl_secboot_glb {
  192. #if defined(CONFIG_FSL_ISBC_KEY_EXT)
  193. uintptr_t ie_addr;
  194. struct ie_key_info ie_tbl;
  195. #endif
  196. };
  197. /*
  198. * ESBC private structure.
  199. * Private structure used by ESBC to store following fields
  200. * ESBC client key
  201. * ESBC client key hash
  202. * ESBC client Signature
  203. * Encoded hash recovered from signature
  204. * Encoded hash of ESBC client header plus ESBC client image
  205. */
  206. struct fsl_secboot_img_priv {
  207. uint32_t hdr_location;
  208. uintptr_t ie_addr;
  209. u32 key_len;
  210. struct fsl_secboot_img_hdr hdr;
  211. u8 img_key[2 * KEY_SIZE_BYTES]; /* ESBC client key */
  212. u8 img_key_hash[32]; /* ESBC client key hash */
  213. #ifdef CONFIG_KEY_REVOCATION
  214. struct srk_table srk_tbl[MAX_KEY_ENTRIES];
  215. #endif
  216. u8 img_sign[KEY_SIZE_BYTES]; /* ESBC client signature */
  217. u8 img_encoded_hash[KEY_SIZE_BYTES]; /* EM wrt RSA PKCSv1.5 */
  218. /* Includes hash recovered after
  219. * signature verification
  220. */
  221. u8 img_encoded_hash_second[KEY_SIZE_BYTES];/* EM' wrt RSA PKCSv1.5 */
  222. /* Includes hash of
  223. * ESBC client header plus
  224. * ESBC client image
  225. */
  226. struct fsl_secboot_sg_table sgtbl[MAX_SG_ENTRIES]; /* SG table */
  227. uintptr_t ehdrloc; /* ESBC Header location */
  228. uintptr_t *img_addr_ptr; /* ESBC Image Location */
  229. uint32_t img_size; /* ESBC Image Size */
  230. };
  231. int do_esbc_halt(cmd_tbl_t *cmdtp, int flag, int argc,
  232. char * const argv[]);
  233. int fsl_secboot_validate(uintptr_t haddr, char *arg_hash_str,
  234. uintptr_t *img_addr_ptr);
  235. int fsl_secboot_blob_encap(cmd_tbl_t *cmdtp, int flag, int argc,
  236. char * const argv[]);
  237. int fsl_secboot_blob_decap(cmd_tbl_t *cmdtp, int flag, int argc,
  238. char * const argv[]);
  239. int fsl_check_boot_mode_secure(void);
  240. int fsl_setenv_chain_of_trust(void);
  241. /*
  242. * This function is used to validate the main U-boot binary from
  243. * SPL just before passing control to it using QorIQ Trust
  244. * Architecture header (appended to U-boot image).
  245. */
  246. void spl_validate_uboot(uint32_t hdr_addr, uintptr_t img_addr);
  247. #endif