1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Xilinx, Inc.
9 #include <asm/global_data.h>
11 #include <asm/arch/hardware.h>
12 #include <asm/arch/sys_proto.h>
14 #include <linux/bitops.h>
15 #include <u-boot/md5.h>
16 #include <u-boot/rsa.h>
17 #include <u-boot/rsa-mod-exp.h>
18 #include <u-boot/sha256.h>
21 #include <zynq_bootimg.h>
23 DECLARE_GLOBAL_DATA_PTR;
25 #ifdef CONFIG_CMD_ZYNQ_RSA
27 #define ZYNQ_EFUSE_RSA_ENABLE_MASK 0x400
28 #define ZYNQ_ATTRIBUTE_PL_IMAGE_MASK 0x20
29 #define ZYNQ_ATTRIBUTE_CHECKSUM_TYPE_MASK 0x7000
30 #define ZYNQ_ATTRIBUTE_RSA_PRESENT_MASK 0x8000
31 #define ZYNQ_ATTRIBUTE_RSA_PART_OWNER_MASK 0x30000
33 #define ZYNQ_RSA_MODULAR_SIZE 256
34 #define ZYNQ_RSA_MODULAR_EXT_SIZE 256
35 #define ZYNQ_RSA_EXPO_SIZE 64
36 #define ZYNQ_RSA_SPK_SIGNATURE_SIZE 256
37 #define ZYNQ_RSA_PARTITION_SIGNATURE_SIZE 256
38 #define ZYNQ_RSA_SIGNATURE_SIZE 0x6C0
39 #define ZYNQ_RSA_HEADER_SIZE 4
40 #define ZYNQ_RSA_MAGIC_WORD_SIZE 60
41 #define ZYNQ_RSA_PART_OWNER_UBOOT 1
42 #define ZYNQ_RSA_ALIGN_PPK_START 64
44 #define WORD_LENGTH_SHIFT 2
46 static u8 *ppkmodular;
47 static u8 *ppkmodularex;
49 struct zynq_rsa_public_key {
50 uint len; /* Length of modulus[] in number of u32 */
51 u32 n0inv; /* -1 / modulus[0] mod 2^32 */
52 u32 *modulus; /* modulus as little endian array */
53 u32 *rr; /* R^2 as little endian array */
56 static struct zynq_rsa_public_key public_key;
58 static struct partition_hdr part_hdr[ZYNQ_MAX_PARTITION_NUMBER];
61 * Extract the primary public key components from already autheticated FSBL
63 static void zynq_extract_ppk(u32 fsbl_len)
68 debug("%s\n", __func__);
71 * Extract the authenticated PPK from OCM i.e at end of the FSBL
73 ppkptr = (u8 *)(fsbl_len + ZYNQ_OCM_BASEADDR);
74 padsize = ((u32)ppkptr % ZYNQ_RSA_ALIGN_PPK_START);
76 ppkptr += (ZYNQ_RSA_ALIGN_PPK_START - padsize);
78 ppkptr += ZYNQ_RSA_HEADER_SIZE;
80 ppkptr += ZYNQ_RSA_MAGIC_WORD_SIZE;
82 ppkmodular = (u8 *)ppkptr;
83 ppkptr += ZYNQ_RSA_MODULAR_SIZE;
84 ppkmodularex = (u8 *)ppkptr;
85 ppkptr += ZYNQ_RSA_MODULAR_EXT_SIZE;
89 * Calculate the inverse(-1 / modulus[0] mod 2^32 ) for the PPK
91 static u32 zynq_calc_inv(void)
93 u32 modulus = public_key.modulus[0];
97 inverse = modulus & BIT(0);
100 inverse *= 2 - modulus * inverse;
104 return ~(inverse - 1);
108 * Recreate the signature by padding the bytes and verify with hash value
110 static int zynq_pad_and_check(u8 *signature, u8 *hash)
112 u8 padding[] = {0x30, 0x31, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48,
113 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04,
115 u8 *pad_ptr = signature + 256;
120 * Re-Create PKCS#1v1.5 Padding
121 * MSB ----------------------------------------------------LSB
122 * 0x0 || 0x1 || 0xFF(for 202 bytes) || 0x0 || T_padding || SHA256 Hash
124 if (*--pad_ptr != 0 || *--pad_ptr != 1)
127 for (ii = 0; ii < pad; ii++) {
128 if (*--pad_ptr != 0xFF)
135 for (ii = 0; ii < sizeof(padding); ii++) {
136 if (*--pad_ptr != padding[ii])
140 for (ii = 0; ii < 32; ii++) {
141 if (*--pad_ptr != hash[ii])
148 * Verify and extract the hash value from signature using the public key
149 * and compare it with calculated hash value.
151 static int zynq_rsa_verify_key(const struct zynq_rsa_public_key *key,
152 const u8 *sig, const u32 sig_len, const u8 *hash)
157 if (!key || !sig || !hash)
160 if (sig_len != (key->len * sizeof(u32))) {
161 printf("Signature is of incorrect length %d\n", sig_len);
165 /* Sanity check for stack size */
166 if (sig_len > ZYNQ_RSA_SPK_SIGNATURE_SIZE) {
167 printf("Signature length %u exceeds maximum %d\n", sig_len,
168 ZYNQ_RSA_SPK_SIGNATURE_SIZE);
172 buf = malloc(sig_len);
176 memcpy(buf, sig, sig_len);
178 status = zynq_pow_mod((u32 *)key, (u32 *)buf);
184 status = zynq_pad_and_check((u8 *)buf, (u8 *)hash);
191 * Authenticate the partition
193 static int zynq_authenticate_part(u8 *buffer, u32 size)
195 u8 hash_signature[32];
201 debug("%s\n", __func__);
203 signature_ptr = (u8 *)(buffer + size - ZYNQ_RSA_SIGNATURE_SIZE);
205 signature_ptr += ZYNQ_RSA_HEADER_SIZE;
207 signature_ptr += ZYNQ_RSA_MAGIC_WORD_SIZE;
209 ppkmodular = (u8 *)signature_ptr;
210 signature_ptr += ZYNQ_RSA_MODULAR_SIZE;
211 ppkmodularex = signature_ptr;
212 signature_ptr += ZYNQ_RSA_MODULAR_EXT_SIZE;
213 signature_ptr += ZYNQ_RSA_EXPO_SIZE;
215 sha256_csum_wd((const unsigned char *)signature_ptr,
216 (ZYNQ_RSA_MODULAR_EXT_SIZE + ZYNQ_RSA_EXPO_SIZE +
217 ZYNQ_RSA_MODULAR_SIZE),
218 (unsigned char *)hash_signature, 0x1000);
220 spk_modular = (u8 *)signature_ptr;
221 signature_ptr += ZYNQ_RSA_MODULAR_SIZE;
222 spk_modular_ex = (u8 *)signature_ptr;
223 signature_ptr += ZYNQ_RSA_MODULAR_EXT_SIZE;
224 signature_ptr += ZYNQ_RSA_EXPO_SIZE;
226 public_key.len = ZYNQ_RSA_MODULAR_SIZE / sizeof(u32);
227 public_key.modulus = (u32 *)ppkmodular;
228 public_key.rr = (u32 *)ppkmodularex;
229 public_key.n0inv = zynq_calc_inv();
231 status = zynq_rsa_verify_key(&public_key, signature_ptr,
232 ZYNQ_RSA_SPK_SIGNATURE_SIZE,
237 signature_ptr += ZYNQ_RSA_SPK_SIGNATURE_SIZE;
239 sha256_csum_wd((const unsigned char *)buffer,
240 (size - ZYNQ_RSA_PARTITION_SIGNATURE_SIZE),
241 (unsigned char *)hash_signature, 0x1000);
243 public_key.len = ZYNQ_RSA_MODULAR_SIZE / sizeof(u32);
244 public_key.modulus = (u32 *)spk_modular;
245 public_key.rr = (u32 *)spk_modular_ex;
246 public_key.n0inv = zynq_calc_inv();
248 return zynq_rsa_verify_key(&public_key, (u8 *)signature_ptr,
249 ZYNQ_RSA_PARTITION_SIGNATURE_SIZE,
250 (u8 *)hash_signature);
254 * Parses the partition header and verfies the authenticated and
257 static int zynq_verify_image(u32 src_ptr)
259 u32 silicon_ver, image_base_addr, status;
260 u32 partition_num = 0;
261 u32 efuseval, srcaddr, size, fsbl_len;
262 struct partition_hdr *hdr_ptr;
263 u32 part_data_len, part_img_len, part_attr;
264 u32 part_load_addr, part_dst_addr, part_chksum_offset;
265 u32 part_start_addr, part_total_size, partitioncount;
266 bool encrypt_part_flag = false;
267 bool part_chksum_flag = false;
268 bool signed_part_flag = false;
270 image_base_addr = src_ptr;
272 silicon_ver = zynq_get_silicon_version();
274 /* RSA not supported in silicon versions 1.0 and 2.0 */
275 if (silicon_ver == 0 || silicon_ver == 1)
278 zynq_get_partition_info(image_base_addr, &fsbl_len,
281 /* Extract ppk if efuse was blown Otherwise return error */
282 efuseval = readl(&efuse_base->status);
283 if (!(efuseval & ZYNQ_EFUSE_RSA_ENABLE_MASK))
286 zynq_extract_ppk(fsbl_len);
288 partitioncount = zynq_get_part_count(&part_hdr[0]);
291 * As the first two partitions are related to fsbl,
292 * we can ignore those two in bootimage and the below
293 * code doesn't need to validate it as fsbl is already
296 if (partitioncount <= 2 ||
297 partitioncount > ZYNQ_MAX_PARTITION_NUMBER)
300 while (partition_num < partitioncount) {
301 if (((part_hdr[partition_num].partitionattr &
302 ZYNQ_ATTRIBUTE_RSA_PART_OWNER_MASK) >> 16) !=
303 ZYNQ_RSA_PART_OWNER_UBOOT) {
304 printf("UBOOT is not Owner for partition %d\n",
309 hdr_ptr = &part_hdr[partition_num];
310 status = zynq_validate_hdr(hdr_ptr);
314 part_data_len = hdr_ptr->datawordlen;
315 part_img_len = hdr_ptr->imagewordlen;
316 part_attr = hdr_ptr->partitionattr;
317 part_load_addr = hdr_ptr->loadaddr;
318 part_chksum_offset = hdr_ptr->checksumoffset;
319 part_start_addr = hdr_ptr->partitionstart;
320 part_total_size = hdr_ptr->partitionwordlen;
322 if (part_data_len != part_img_len) {
323 debug("Encrypted\n");
324 encrypt_part_flag = true;
327 if (part_attr & ZYNQ_ATTRIBUTE_CHECKSUM_TYPE_MASK)
328 part_chksum_flag = true;
330 if (part_attr & ZYNQ_ATTRIBUTE_RSA_PRESENT_MASK) {
331 debug("RSA Signed\n");
332 signed_part_flag = true;
333 size = part_total_size << WORD_LENGTH_SHIFT;
338 if (!signed_part_flag && !part_chksum_flag) {
339 printf("Partition not signed & no chksum\n");
344 srcaddr = image_base_addr +
345 (part_start_addr << WORD_LENGTH_SHIFT);
348 * This validation is just for PS DDR.
349 * TODO: Update this for PL DDR check as well.
351 if (part_load_addr < gd->bd->bi_dram[0].start &&
352 ((part_load_addr + part_data_len) >
353 (gd->bd->bi_dram[0].start +
354 gd->bd->bi_dram[0].size))) {
355 printf("INVALID_LOAD_ADDRESS_FAIL\n");
359 if (part_attr & ZYNQ_ATTRIBUTE_PL_IMAGE_MASK)
360 part_load_addr = srcaddr;
362 memcpy((u32 *)part_load_addr, (u32 *)srcaddr,
365 if (part_chksum_flag) {
366 part_chksum_offset = image_base_addr +
367 (part_chksum_offset <<
369 status = zynq_validate_partition(part_load_addr,
374 printf("PART_CHKSUM_FAIL\n");
377 debug("Partition Validation Done\n");
380 if (signed_part_flag) {
381 status = zynq_authenticate_part((u8 *)part_load_addr,
384 printf("AUTHENTICATION_FAIL\n");
387 debug("Authentication Done\n");
390 if (encrypt_part_flag) {
391 debug("DECRYPTION\n");
393 part_dst_addr = part_load_addr;
395 if (part_attr & ZYNQ_ATTRIBUTE_PL_IMAGE_MASK) {
400 status = zynq_decrypt_load(part_load_addr,
406 printf("DECRYPTION_FAIL\n");
416 static int do_zynq_rsa(struct cmd_tbl *cmdtp, int flag, int argc,
422 if (argc != cmdtp->maxargs)
423 return CMD_RET_FAILURE;
425 src_ptr = simple_strtoul(argv[2], &endp, 16);
426 if (*argv[2] == 0 || *endp != 0)
427 return CMD_RET_USAGE;
429 if (zynq_verify_image(src_ptr))
430 return CMD_RET_FAILURE;
432 return CMD_RET_SUCCESS;
436 #ifdef CONFIG_CMD_ZYNQ_AES
437 static int zynq_decrypt_image(struct cmd_tbl *cmdtp, int flag, int argc,
441 u32 srcaddr, srclen, dstaddr, dstlen;
443 u8 imgtype = BIT_NONE;
445 if (argc < 5 && argc > cmdtp->maxargs)
446 return CMD_RET_USAGE;
449 if (!strcmp("load", argv[2]))
451 else if (!strcmp("loadp", argv[2]))
452 imgtype = BIT_PARTIAL;
454 return CMD_RET_USAGE;
456 srcaddr = simple_strtoul(argv[3], &endp, 16);
457 if (*argv[3] == 0 || *endp != 0)
458 return CMD_RET_USAGE;
459 srclen = simple_strtoul(argv[4], &endp, 16);
460 if (*argv[4] == 0 || *endp != 0)
461 return CMD_RET_USAGE;
463 dstaddr = 0xFFFFFFFF;
466 srcaddr = simple_strtoul(argv[2], &endp, 16);
467 if (*argv[2] == 0 || *endp != 0)
468 return CMD_RET_USAGE;
469 srclen = simple_strtoul(argv[3], &endp, 16);
470 if (*argv[3] == 0 || *endp != 0)
471 return CMD_RET_USAGE;
472 dstaddr = simple_strtoul(argv[4], &endp, 16);
473 if (*argv[4] == 0 || *endp != 0)
474 return CMD_RET_USAGE;
475 dstlen = simple_strtoul(argv[5], &endp, 16);
476 if (*argv[5] == 0 || *endp != 0)
477 return CMD_RET_USAGE;
481 * Roundup source and destination lengths to
485 srclen = roundup(srclen, 4);
487 dstlen = roundup(dstlen, 4);
489 status = zynq_decrypt_load(srcaddr, srclen >> 2, dstaddr,
490 dstlen >> 2, imgtype);
492 return CMD_RET_FAILURE;
494 return CMD_RET_SUCCESS;
498 static struct cmd_tbl zynq_commands[] = {
499 #ifdef CONFIG_CMD_ZYNQ_RSA
500 U_BOOT_CMD_MKENT(rsa, 3, 1, do_zynq_rsa, "", ""),
502 #ifdef CONFIG_CMD_ZYNQ_AES
503 U_BOOT_CMD_MKENT(aes, 6, 1, zynq_decrypt_image, "", ""),
507 static int do_zynq(struct cmd_tbl *cmdtp, int flag, int argc,
510 struct cmd_tbl *zynq_cmd;
513 if (!ARRAY_SIZE(zynq_commands)) {
514 puts("No zynq specific command enabled\n");
515 return CMD_RET_USAGE;
519 return CMD_RET_USAGE;
520 zynq_cmd = find_cmd_tbl(argv[1], zynq_commands,
521 ARRAY_SIZE(zynq_commands));
523 return CMD_RET_USAGE;
525 ret = zynq_cmd->cmd(zynq_cmd, flag, argc, argv);
527 return cmd_process_error(zynq_cmd, ret);
530 #ifdef CONFIG_SYS_LONGHELP
531 static char zynq_help_text[] =
533 #ifdef CONFIG_CMD_ZYNQ_RSA
534 "rsa <baseaddr> - Verifies the authenticated and encrypted\n"
535 " zynq images and loads them back to load\n"
536 " addresses as specified in BOOT image(BOOT.BIN)\n"
538 #ifdef CONFIG_CMD_ZYNQ_AES
539 "aes <srcaddr> <srclen> <dstaddr> <dstlen>\n"
540 " - Decrypts the encrypted image present in source\n"
541 " address and places the decrypted image at\n"
542 " destination address\n"
543 "aes load <srcaddr> <srclen>\n"
544 "aes loadp <srcaddr> <srclen>\n"
545 " if operation type is load or loadp, it loads the encrypted\n"
546 " full or partial bitstream on to PL respectively.\n"
551 U_BOOT_CMD(zynq, 6, 0, do_zynq,
552 "Zynq specific commands", zynq_help_text