cmds.c 13 KB

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