cmds.c 13 KB

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