cmds.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2018 Xilinx, Inc.
  4. */
  5. #include <common.h>
  6. #include <asm/io.h>
  7. #include <asm/arch/hardware.h>
  8. #include <asm/arch/sys_proto.h>
  9. #include <malloc.h>
  10. #include <u-boot/md5.h>
  11. #include <u-boot/rsa.h>
  12. #include <u-boot/rsa-mod-exp.h>
  13. #include <u-boot/sha256.h>
  14. #include <zynqpl.h>
  15. #include <fpga.h>
  16. #include <zynq_bootimg.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. #ifdef CONFIG_CMD_ZYNQ_RSA
  19. #define ZYNQ_EFUSE_RSA_ENABLE_MASK 0x400
  20. #define ZYNQ_ATTRIBUTE_PL_IMAGE_MASK 0x20
  21. #define ZYNQ_ATTRIBUTE_CHECKSUM_TYPE_MASK 0x7000
  22. #define ZYNQ_ATTRIBUTE_RSA_PRESENT_MASK 0x8000
  23. #define ZYNQ_ATTRIBUTE_RSA_PART_OWNER_MASK 0x30000
  24. #define ZYNQ_RSA_MODULAR_SIZE 256
  25. #define ZYNQ_RSA_MODULAR_EXT_SIZE 256
  26. #define ZYNQ_RSA_EXPO_SIZE 64
  27. #define ZYNQ_RSA_SPK_SIGNATURE_SIZE 256
  28. #define ZYNQ_RSA_PARTITION_SIGNATURE_SIZE 256
  29. #define ZYNQ_RSA_SIGNATURE_SIZE 0x6C0
  30. #define ZYNQ_RSA_HEADER_SIZE 4
  31. #define ZYNQ_RSA_MAGIC_WORD_SIZE 60
  32. #define ZYNQ_RSA_PART_OWNER_UBOOT 1
  33. #define ZYNQ_RSA_ALIGN_PPK_START 64
  34. #define WORD_LENGTH_SHIFT 2
  35. static u8 *ppkmodular;
  36. static u8 *ppkmodularex;
  37. struct zynq_rsa_public_key {
  38. uint len; /* Length of modulus[] in number of u32 */
  39. u32 n0inv; /* -1 / modulus[0] mod 2^32 */
  40. u32 *modulus; /* modulus as little endian array */
  41. u32 *rr; /* R^2 as little endian array */
  42. };
  43. static struct zynq_rsa_public_key public_key;
  44. static struct partition_hdr part_hdr[ZYNQ_MAX_PARTITION_NUMBER];
  45. /*
  46. * Extract the primary public key components from already autheticated FSBL
  47. */
  48. static void zynq_extract_ppk(u32 fsbl_len)
  49. {
  50. u32 padsize;
  51. u8 *ppkptr;
  52. debug("%s\n", __func__);
  53. /*
  54. * Extract the authenticated PPK from OCM i.e at end of the FSBL
  55. */
  56. ppkptr = (u8 *)(fsbl_len + ZYNQ_OCM_BASEADDR);
  57. padsize = ((u32)ppkptr % ZYNQ_RSA_ALIGN_PPK_START);
  58. if (padsize)
  59. ppkptr += (ZYNQ_RSA_ALIGN_PPK_START - padsize);
  60. ppkptr += ZYNQ_RSA_HEADER_SIZE;
  61. ppkptr += ZYNQ_RSA_MAGIC_WORD_SIZE;
  62. ppkmodular = (u8 *)ppkptr;
  63. ppkptr += ZYNQ_RSA_MODULAR_SIZE;
  64. ppkmodularex = (u8 *)ppkptr;
  65. ppkptr += ZYNQ_RSA_MODULAR_EXT_SIZE;
  66. }
  67. /*
  68. * Calculate the inverse(-1 / modulus[0] mod 2^32 ) for the PPK
  69. */
  70. static u32 zynq_calc_inv(void)
  71. {
  72. u32 modulus = public_key.modulus[0];
  73. u32 tmp = BIT(1);
  74. u32 inverse;
  75. inverse = modulus & BIT(0);
  76. while (tmp) {
  77. inverse *= 2 - modulus * inverse;
  78. tmp *= tmp;
  79. }
  80. return ~(inverse - 1);
  81. }
  82. /*
  83. * Recreate the signature by padding the bytes and verify with hash value
  84. */
  85. static int zynq_pad_and_check(u8 *signature, u8 *hash)
  86. {
  87. u8 padding[] = {0x30, 0x31, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48,
  88. 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04,
  89. 0x20};
  90. u8 *pad_ptr = signature + 256;
  91. u32 pad = 202;
  92. u32 ii;
  93. /*
  94. * Re-Create PKCS#1v1.5 Padding
  95. * MSB ----------------------------------------------------LSB
  96. * 0x0 || 0x1 || 0xFF(for 202 bytes) || 0x0 || T_padding || SHA256 Hash
  97. */
  98. if (*--pad_ptr != 0 || *--pad_ptr != 1)
  99. return -1;
  100. for (ii = 0; ii < pad; ii++) {
  101. if (*--pad_ptr != 0xFF)
  102. return -1;
  103. }
  104. if (*--pad_ptr != 0)
  105. return -1;
  106. for (ii = 0; ii < sizeof(padding); ii++) {
  107. if (*--pad_ptr != padding[ii])
  108. return -1;
  109. }
  110. for (ii = 0; ii < 32; ii++) {
  111. if (*--pad_ptr != hash[ii])
  112. return -1;
  113. }
  114. return 0;
  115. }
  116. /*
  117. * Verify and extract the hash value from signature using the public key
  118. * and compare it with calculated hash value.
  119. */
  120. static int zynq_rsa_verify_key(const struct zynq_rsa_public_key *key,
  121. const u8 *sig, const u32 sig_len, const u8 *hash)
  122. {
  123. int status;
  124. void *buf;
  125. if (!key || !sig || !hash)
  126. return -1;
  127. if (sig_len != (key->len * sizeof(u32))) {
  128. printf("Signature is of incorrect length %d\n", sig_len);
  129. return -1;
  130. }
  131. /* Sanity check for stack size */
  132. if (sig_len > ZYNQ_RSA_SPK_SIGNATURE_SIZE) {
  133. printf("Signature length %u exceeds maximum %d\n", sig_len,
  134. ZYNQ_RSA_SPK_SIGNATURE_SIZE);
  135. return -1;
  136. }
  137. buf = malloc(sig_len);
  138. if (!buf)
  139. return -1;
  140. memcpy(buf, sig, sig_len);
  141. status = zynq_pow_mod((u32 *)key, (u32 *)buf);
  142. if (status == -1) {
  143. free(buf);
  144. return status;
  145. }
  146. status = zynq_pad_and_check((u8 *)buf, (u8 *)hash);
  147. free(buf);
  148. return status;
  149. }
  150. /*
  151. * Authenticate the partition
  152. */
  153. static int zynq_authenticate_part(u8 *buffer, u32 size)
  154. {
  155. u8 hash_signature[32];
  156. u8 *spk_modular;
  157. u8 *spk_modular_ex;
  158. u8 *signature_ptr;
  159. u32 status;
  160. debug("%s\n", __func__);
  161. signature_ptr = (u8 *)(buffer + size - ZYNQ_RSA_SIGNATURE_SIZE);
  162. signature_ptr += ZYNQ_RSA_HEADER_SIZE;
  163. signature_ptr += ZYNQ_RSA_MAGIC_WORD_SIZE;
  164. ppkmodular = (u8 *)signature_ptr;
  165. signature_ptr += ZYNQ_RSA_MODULAR_SIZE;
  166. ppkmodularex = signature_ptr;
  167. signature_ptr += ZYNQ_RSA_MODULAR_EXT_SIZE;
  168. signature_ptr += ZYNQ_RSA_EXPO_SIZE;
  169. sha256_csum_wd((const unsigned char *)signature_ptr,
  170. (ZYNQ_RSA_MODULAR_EXT_SIZE + ZYNQ_RSA_EXPO_SIZE +
  171. ZYNQ_RSA_MODULAR_SIZE),
  172. (unsigned char *)hash_signature, 0x1000);
  173. spk_modular = (u8 *)signature_ptr;
  174. signature_ptr += ZYNQ_RSA_MODULAR_SIZE;
  175. spk_modular_ex = (u8 *)signature_ptr;
  176. signature_ptr += ZYNQ_RSA_MODULAR_EXT_SIZE;
  177. signature_ptr += ZYNQ_RSA_EXPO_SIZE;
  178. public_key.len = ZYNQ_RSA_MODULAR_SIZE / sizeof(u32);
  179. public_key.modulus = (u32 *)ppkmodular;
  180. public_key.rr = (u32 *)ppkmodularex;
  181. public_key.n0inv = zynq_calc_inv();
  182. status = zynq_rsa_verify_key(&public_key, signature_ptr,
  183. ZYNQ_RSA_SPK_SIGNATURE_SIZE,
  184. hash_signature);
  185. if (status)
  186. return status;
  187. signature_ptr += ZYNQ_RSA_SPK_SIGNATURE_SIZE;
  188. sha256_csum_wd((const unsigned char *)buffer,
  189. (size - ZYNQ_RSA_PARTITION_SIGNATURE_SIZE),
  190. (unsigned char *)hash_signature, 0x1000);
  191. public_key.len = ZYNQ_RSA_MODULAR_SIZE / sizeof(u32);
  192. public_key.modulus = (u32 *)spk_modular;
  193. public_key.rr = (u32 *)spk_modular_ex;
  194. public_key.n0inv = zynq_calc_inv();
  195. return zynq_rsa_verify_key(&public_key, (u8 *)signature_ptr,
  196. ZYNQ_RSA_PARTITION_SIGNATURE_SIZE,
  197. (u8 *)hash_signature);
  198. }
  199. /*
  200. * Parses the partition header and verfies the authenticated and
  201. * encrypted image.
  202. */
  203. static int zynq_verify_image(u32 src_ptr)
  204. {
  205. u32 silicon_ver, image_base_addr, status;
  206. u32 partition_num = 0;
  207. u32 efuseval, srcaddr, size, fsbl_len;
  208. struct partition_hdr *hdr_ptr;
  209. u32 part_data_len, part_img_len, part_attr;
  210. u32 part_load_addr, part_dst_addr, part_chksum_offset;
  211. u32 part_start_addr, part_total_size, partitioncount;
  212. bool encrypt_part_flag = false;
  213. bool part_chksum_flag = false;
  214. bool signed_part_flag = false;
  215. image_base_addr = src_ptr;
  216. silicon_ver = zynq_get_silicon_version();
  217. /* RSA not supported in silicon versions 1.0 and 2.0 */
  218. if (silicon_ver == 0 || silicon_ver == 1)
  219. return -1;
  220. zynq_get_partition_info(image_base_addr, &fsbl_len,
  221. &part_hdr[0]);
  222. /* Extract ppk if efuse was blown Otherwise return error */
  223. efuseval = readl(&efuse_base->status);
  224. if (!(efuseval & ZYNQ_EFUSE_RSA_ENABLE_MASK))
  225. return -1;
  226. zynq_extract_ppk(fsbl_len);
  227. partitioncount = zynq_get_part_count(&part_hdr[0]);
  228. /*
  229. * As the first two partitions are related to fsbl,
  230. * we can ignore those two in bootimage and the below
  231. * code doesn't need to validate it as fsbl is already
  232. * done by now
  233. */
  234. if (partitioncount <= 2 ||
  235. partitioncount > ZYNQ_MAX_PARTITION_NUMBER)
  236. return -1;
  237. while (partition_num < partitioncount) {
  238. if (((part_hdr[partition_num].partitionattr &
  239. ZYNQ_ATTRIBUTE_RSA_PART_OWNER_MASK) >> 16) !=
  240. ZYNQ_RSA_PART_OWNER_UBOOT) {
  241. printf("UBOOT is not Owner for partition %d\n",
  242. partition_num);
  243. partition_num++;
  244. continue;
  245. }
  246. hdr_ptr = &part_hdr[partition_num];
  247. status = zynq_validate_hdr(hdr_ptr);
  248. if (status)
  249. return status;
  250. part_data_len = hdr_ptr->datawordlen;
  251. part_img_len = hdr_ptr->imagewordlen;
  252. part_attr = hdr_ptr->partitionattr;
  253. part_load_addr = hdr_ptr->loadaddr;
  254. part_chksum_offset = hdr_ptr->checksumoffset;
  255. part_start_addr = hdr_ptr->partitionstart;
  256. part_total_size = hdr_ptr->partitionwordlen;
  257. if (part_data_len != part_img_len) {
  258. debug("Encrypted\n");
  259. encrypt_part_flag = true;
  260. }
  261. if (part_attr & ZYNQ_ATTRIBUTE_CHECKSUM_TYPE_MASK)
  262. part_chksum_flag = true;
  263. if (part_attr & ZYNQ_ATTRIBUTE_RSA_PRESENT_MASK) {
  264. debug("RSA Signed\n");
  265. signed_part_flag = true;
  266. size = part_total_size << WORD_LENGTH_SHIFT;
  267. } else {
  268. size = part_img_len;
  269. }
  270. if (!signed_part_flag && !part_chksum_flag) {
  271. printf("Partition not signed & no chksum\n");
  272. partition_num++;
  273. continue;
  274. }
  275. srcaddr = image_base_addr +
  276. (part_start_addr << WORD_LENGTH_SHIFT);
  277. /*
  278. * This validation is just for PS DDR.
  279. * TODO: Update this for PL DDR check as well.
  280. */
  281. if (part_load_addr < gd->bd->bi_dram[0].start &&
  282. ((part_load_addr + part_data_len) >
  283. (gd->bd->bi_dram[0].start +
  284. gd->bd->bi_dram[0].size))) {
  285. printf("INVALID_LOAD_ADDRESS_FAIL\n");
  286. return -1;
  287. }
  288. if (part_attr & ZYNQ_ATTRIBUTE_PL_IMAGE_MASK)
  289. part_load_addr = srcaddr;
  290. else
  291. memcpy((u32 *)part_load_addr, (u32 *)srcaddr,
  292. size);
  293. if (part_chksum_flag) {
  294. part_chksum_offset = image_base_addr +
  295. (part_chksum_offset <<
  296. WORD_LENGTH_SHIFT);
  297. status = zynq_validate_partition(part_load_addr,
  298. (part_total_size <<
  299. WORD_LENGTH_SHIFT),
  300. part_chksum_offset);
  301. if (status != 0) {
  302. printf("PART_CHKSUM_FAIL\n");
  303. return -1;
  304. }
  305. debug("Partition Validation Done\n");
  306. }
  307. if (signed_part_flag) {
  308. status = zynq_authenticate_part((u8 *)part_load_addr,
  309. size);
  310. if (status != 0) {
  311. printf("AUTHENTICATION_FAIL\n");
  312. return -1;
  313. }
  314. debug("Authentication Done\n");
  315. }
  316. if (encrypt_part_flag) {
  317. debug("DECRYPTION\n");
  318. part_dst_addr = part_load_addr;
  319. if (part_attr & ZYNQ_ATTRIBUTE_PL_IMAGE_MASK) {
  320. partition_num++;
  321. continue;
  322. }
  323. status = zynq_decrypt_load(part_load_addr,
  324. part_img_len,
  325. part_dst_addr,
  326. part_data_len);
  327. if (status != 0) {
  328. printf("DECRYPTION_FAIL\n");
  329. return -1;
  330. }
  331. }
  332. partition_num++;
  333. }
  334. return 0;
  335. }
  336. static int do_zynq_rsa(cmd_tbl_t *cmdtp, int flag, int argc,
  337. char * const argv[])
  338. {
  339. u32 src_ptr;
  340. char *endp;
  341. src_ptr = simple_strtoul(argv[2], &endp, 16);
  342. if (*argv[2] == 0 || *endp != 0)
  343. return CMD_RET_USAGE;
  344. if (zynq_verify_image(src_ptr))
  345. return CMD_RET_FAILURE;
  346. return CMD_RET_SUCCESS;
  347. }
  348. #endif
  349. #ifdef CONFIG_CMD_ZYNQ_AES
  350. static int zynq_decrypt_image(cmd_tbl_t *cmdtp, int flag, int argc,
  351. char * const argv[])
  352. {
  353. char *endp;
  354. u32 srcaddr, srclen, dstaddr, dstlen;
  355. int status;
  356. srcaddr = simple_strtoul(argv[2], &endp, 16);
  357. if (*argv[2] == 0 || *endp != 0)
  358. return CMD_RET_USAGE;
  359. srclen = simple_strtoul(argv[3], &endp, 16);
  360. if (*argv[3] == 0 || *endp != 0)
  361. return CMD_RET_USAGE;
  362. dstaddr = simple_strtoul(argv[4], &endp, 16);
  363. if (*argv[4] == 0 || *endp != 0)
  364. return CMD_RET_USAGE;
  365. dstlen = simple_strtoul(argv[5], &endp, 16);
  366. if (*argv[5] == 0 || *endp != 0)
  367. return CMD_RET_USAGE;
  368. /*
  369. * Roundup source and destination lengths to
  370. * word size
  371. */
  372. if (srclen % 4)
  373. srclen = roundup(srclen, 4);
  374. if (dstlen % 4)
  375. dstlen = roundup(dstlen, 4);
  376. status = zynq_decrypt_load(srcaddr, srclen >> 2, dstaddr, dstlen >> 2);
  377. if (status != 0)
  378. return CMD_RET_FAILURE;
  379. return CMD_RET_SUCCESS;
  380. }
  381. #endif
  382. static cmd_tbl_t zynq_commands[] = {
  383. #ifdef CONFIG_CMD_ZYNQ_RSA
  384. U_BOOT_CMD_MKENT(rsa, 3, 1, do_zynq_rsa, "", ""),
  385. #endif
  386. #ifdef CONFIG_CMD_ZYNQ_AES
  387. U_BOOT_CMD_MKENT(aes, 6, 1, zynq_decrypt_image, "", ""),
  388. #endif
  389. };
  390. static int do_zynq(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  391. {
  392. cmd_tbl_t *zynq_cmd;
  393. int ret;
  394. if (!ARRAY_SIZE(zynq_commands)) {
  395. puts("No zynq specific command enabled\n");
  396. return CMD_RET_USAGE;
  397. }
  398. if (argc < 2)
  399. return CMD_RET_USAGE;
  400. zynq_cmd = find_cmd_tbl(argv[1], zynq_commands,
  401. ARRAY_SIZE(zynq_commands));
  402. if (!zynq_cmd || argc != zynq_cmd->maxargs)
  403. return CMD_RET_USAGE;
  404. ret = zynq_cmd->cmd(zynq_cmd, flag, argc, argv);
  405. return cmd_process_error(zynq_cmd, ret);
  406. }
  407. static char zynq_help_text[] =
  408. ""
  409. #ifdef CONFIG_CMD_ZYNQ_RSA
  410. "rsa <baseaddr> - Verifies the authenticated and encrypted\n"
  411. " zynq images and loads them back to load\n"
  412. " addresses as specified in BOOT image(BOOT.BIN)\n"
  413. #endif
  414. #ifdef CONFIG_CMD_ZYNQ_AES
  415. "aes <srcaddr> <srclen> <dstaddr> <dstlen>\n"
  416. " - Decrypts the encrypted image present in source\n"
  417. " address and places the decrypted image at\n"
  418. " destination address\n"
  419. #endif
  420. ;
  421. U_BOOT_CMD(zynq, 6, 0, do_zynq,
  422. "Zynq specific commands", zynq_help_text
  423. );