1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Xilinx, Inc.
9 #include <asm/arch/hardware.h>
10 #include <asm/arch/sys_proto.h>
12 #include <u-boot/md5.h>
13 #include <u-boot/rsa.h>
14 #include <u-boot/rsa-mod-exp.h>
15 #include <u-boot/sha256.h>
18 #include <zynq_bootimg.h>
20 DECLARE_GLOBAL_DATA_PTR;
22 #ifdef CONFIG_CMD_ZYNQ_RSA
24 #define ZYNQ_EFUSE_RSA_ENABLE_MASK 0x400
25 #define ZYNQ_ATTRIBUTE_PL_IMAGE_MASK 0x20
26 #define ZYNQ_ATTRIBUTE_CHECKSUM_TYPE_MASK 0x7000
27 #define ZYNQ_ATTRIBUTE_RSA_PRESENT_MASK 0x8000
28 #define ZYNQ_ATTRIBUTE_RSA_PART_OWNER_MASK 0x30000
30 #define ZYNQ_RSA_MODULAR_SIZE 256
31 #define ZYNQ_RSA_MODULAR_EXT_SIZE 256
32 #define ZYNQ_RSA_EXPO_SIZE 64
33 #define ZYNQ_RSA_SPK_SIGNATURE_SIZE 256
34 #define ZYNQ_RSA_PARTITION_SIGNATURE_SIZE 256
35 #define ZYNQ_RSA_SIGNATURE_SIZE 0x6C0
36 #define ZYNQ_RSA_HEADER_SIZE 4
37 #define ZYNQ_RSA_MAGIC_WORD_SIZE 60
38 #define ZYNQ_RSA_PART_OWNER_UBOOT 1
39 #define ZYNQ_RSA_ALIGN_PPK_START 64
41 #define WORD_LENGTH_SHIFT 2
43 static u8 *ppkmodular;
44 static u8 *ppkmodularex;
46 struct zynq_rsa_public_key {
47 uint len; /* Length of modulus[] in number of u32 */
48 u32 n0inv; /* -1 / modulus[0] mod 2^32 */
49 u32 *modulus; /* modulus as little endian array */
50 u32 *rr; /* R^2 as little endian array */
53 static struct zynq_rsa_public_key public_key;
55 static struct partition_hdr part_hdr[ZYNQ_MAX_PARTITION_NUMBER];
58 * Extract the primary public key components from already autheticated FSBL
60 static void zynq_extract_ppk(u32 fsbl_len)
65 debug("%s\n", __func__);
68 * Extract the authenticated PPK from OCM i.e at end of the FSBL
70 ppkptr = (u8 *)(fsbl_len + ZYNQ_OCM_BASEADDR);
71 padsize = ((u32)ppkptr % ZYNQ_RSA_ALIGN_PPK_START);
73 ppkptr += (ZYNQ_RSA_ALIGN_PPK_START - padsize);
75 ppkptr += ZYNQ_RSA_HEADER_SIZE;
77 ppkptr += ZYNQ_RSA_MAGIC_WORD_SIZE;
79 ppkmodular = (u8 *)ppkptr;
80 ppkptr += ZYNQ_RSA_MODULAR_SIZE;
81 ppkmodularex = (u8 *)ppkptr;
82 ppkptr += ZYNQ_RSA_MODULAR_EXT_SIZE;
86 * Calculate the inverse(-1 / modulus[0] mod 2^32 ) for the PPK
88 static u32 zynq_calc_inv(void)
90 u32 modulus = public_key.modulus[0];
94 inverse = modulus & BIT(0);
97 inverse *= 2 - modulus * inverse;
101 return ~(inverse - 1);
105 * Recreate the signature by padding the bytes and verify with hash value
107 static int zynq_pad_and_check(u8 *signature, u8 *hash)
109 u8 padding[] = {0x30, 0x31, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48,
110 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04,
112 u8 *pad_ptr = signature + 256;
117 * Re-Create PKCS#1v1.5 Padding
118 * MSB ----------------------------------------------------LSB
119 * 0x0 || 0x1 || 0xFF(for 202 bytes) || 0x0 || T_padding || SHA256 Hash
121 if (*--pad_ptr != 0 || *--pad_ptr != 1)
124 for (ii = 0; ii < pad; ii++) {
125 if (*--pad_ptr != 0xFF)
132 for (ii = 0; ii < sizeof(padding); ii++) {
133 if (*--pad_ptr != padding[ii])
137 for (ii = 0; ii < 32; ii++) {
138 if (*--pad_ptr != hash[ii])
145 * Verify and extract the hash value from signature using the public key
146 * and compare it with calculated hash value.
148 static int zynq_rsa_verify_key(const struct zynq_rsa_public_key *key,
149 const u8 *sig, const u32 sig_len, const u8 *hash)
154 if (!key || !sig || !hash)
157 if (sig_len != (key->len * sizeof(u32))) {
158 printf("Signature is of incorrect length %d\n", sig_len);
162 /* Sanity check for stack size */
163 if (sig_len > ZYNQ_RSA_SPK_SIGNATURE_SIZE) {
164 printf("Signature length %u exceeds maximum %d\n", sig_len,
165 ZYNQ_RSA_SPK_SIGNATURE_SIZE);
169 buf = malloc(sig_len);
173 memcpy(buf, sig, sig_len);
175 status = zynq_pow_mod((u32 *)key, (u32 *)buf);
181 status = zynq_pad_and_check((u8 *)buf, (u8 *)hash);
188 * Authenticate the partition
190 static int zynq_authenticate_part(u8 *buffer, u32 size)
192 u8 hash_signature[32];
198 debug("%s\n", __func__);
200 signature_ptr = (u8 *)(buffer + size - ZYNQ_RSA_SIGNATURE_SIZE);
202 signature_ptr += ZYNQ_RSA_HEADER_SIZE;
204 signature_ptr += ZYNQ_RSA_MAGIC_WORD_SIZE;
206 ppkmodular = (u8 *)signature_ptr;
207 signature_ptr += ZYNQ_RSA_MODULAR_SIZE;
208 ppkmodularex = signature_ptr;
209 signature_ptr += ZYNQ_RSA_MODULAR_EXT_SIZE;
210 signature_ptr += ZYNQ_RSA_EXPO_SIZE;
212 sha256_csum_wd((const unsigned char *)signature_ptr,
213 (ZYNQ_RSA_MODULAR_EXT_SIZE + ZYNQ_RSA_EXPO_SIZE +
214 ZYNQ_RSA_MODULAR_SIZE),
215 (unsigned char *)hash_signature, 0x1000);
217 spk_modular = (u8 *)signature_ptr;
218 signature_ptr += ZYNQ_RSA_MODULAR_SIZE;
219 spk_modular_ex = (u8 *)signature_ptr;
220 signature_ptr += ZYNQ_RSA_MODULAR_EXT_SIZE;
221 signature_ptr += ZYNQ_RSA_EXPO_SIZE;
223 public_key.len = ZYNQ_RSA_MODULAR_SIZE / sizeof(u32);
224 public_key.modulus = (u32 *)ppkmodular;
225 public_key.rr = (u32 *)ppkmodularex;
226 public_key.n0inv = zynq_calc_inv();
228 status = zynq_rsa_verify_key(&public_key, signature_ptr,
229 ZYNQ_RSA_SPK_SIGNATURE_SIZE,
234 signature_ptr += ZYNQ_RSA_SPK_SIGNATURE_SIZE;
236 sha256_csum_wd((const unsigned char *)buffer,
237 (size - ZYNQ_RSA_PARTITION_SIGNATURE_SIZE),
238 (unsigned char *)hash_signature, 0x1000);
240 public_key.len = ZYNQ_RSA_MODULAR_SIZE / sizeof(u32);
241 public_key.modulus = (u32 *)spk_modular;
242 public_key.rr = (u32 *)spk_modular_ex;
243 public_key.n0inv = zynq_calc_inv();
245 return zynq_rsa_verify_key(&public_key, (u8 *)signature_ptr,
246 ZYNQ_RSA_PARTITION_SIGNATURE_SIZE,
247 (u8 *)hash_signature);
251 * Parses the partition header and verfies the authenticated and
254 static int zynq_verify_image(u32 src_ptr)
256 u32 silicon_ver, image_base_addr, status;
257 u32 partition_num = 0;
258 u32 efuseval, srcaddr, size, fsbl_len;
259 struct partition_hdr *hdr_ptr;
260 u32 part_data_len, part_img_len, part_attr;
261 u32 part_load_addr, part_dst_addr, part_chksum_offset;
262 u32 part_start_addr, part_total_size, partitioncount;
263 bool encrypt_part_flag = false;
264 bool part_chksum_flag = false;
265 bool signed_part_flag = false;
267 image_base_addr = src_ptr;
269 silicon_ver = zynq_get_silicon_version();
271 /* RSA not supported in silicon versions 1.0 and 2.0 */
272 if (silicon_ver == 0 || silicon_ver == 1)
275 zynq_get_partition_info(image_base_addr, &fsbl_len,
278 /* Extract ppk if efuse was blown Otherwise return error */
279 efuseval = readl(&efuse_base->status);
280 if (!(efuseval & ZYNQ_EFUSE_RSA_ENABLE_MASK))
283 zynq_extract_ppk(fsbl_len);
285 partitioncount = zynq_get_part_count(&part_hdr[0]);
288 * As the first two partitions are related to fsbl,
289 * we can ignore those two in bootimage and the below
290 * code doesn't need to validate it as fsbl is already
293 if (partitioncount <= 2 ||
294 partitioncount > ZYNQ_MAX_PARTITION_NUMBER)
297 while (partition_num < partitioncount) {
298 if (((part_hdr[partition_num].partitionattr &
299 ZYNQ_ATTRIBUTE_RSA_PART_OWNER_MASK) >> 16) !=
300 ZYNQ_RSA_PART_OWNER_UBOOT) {
301 printf("UBOOT is not Owner for partition %d\n",
306 hdr_ptr = &part_hdr[partition_num];
307 status = zynq_validate_hdr(hdr_ptr);
311 part_data_len = hdr_ptr->datawordlen;
312 part_img_len = hdr_ptr->imagewordlen;
313 part_attr = hdr_ptr->partitionattr;
314 part_load_addr = hdr_ptr->loadaddr;
315 part_chksum_offset = hdr_ptr->checksumoffset;
316 part_start_addr = hdr_ptr->partitionstart;
317 part_total_size = hdr_ptr->partitionwordlen;
319 if (part_data_len != part_img_len) {
320 debug("Encrypted\n");
321 encrypt_part_flag = true;
324 if (part_attr & ZYNQ_ATTRIBUTE_CHECKSUM_TYPE_MASK)
325 part_chksum_flag = true;
327 if (part_attr & ZYNQ_ATTRIBUTE_RSA_PRESENT_MASK) {
328 debug("RSA Signed\n");
329 signed_part_flag = true;
330 size = part_total_size << WORD_LENGTH_SHIFT;
335 if (!signed_part_flag && !part_chksum_flag) {
336 printf("Partition not signed & no chksum\n");
341 srcaddr = image_base_addr +
342 (part_start_addr << WORD_LENGTH_SHIFT);
345 * This validation is just for PS DDR.
346 * TODO: Update this for PL DDR check as well.
348 if (part_load_addr < gd->bd->bi_dram[0].start &&
349 ((part_load_addr + part_data_len) >
350 (gd->bd->bi_dram[0].start +
351 gd->bd->bi_dram[0].size))) {
352 printf("INVALID_LOAD_ADDRESS_FAIL\n");
356 if (part_attr & ZYNQ_ATTRIBUTE_PL_IMAGE_MASK)
357 part_load_addr = srcaddr;
359 memcpy((u32 *)part_load_addr, (u32 *)srcaddr,
362 if (part_chksum_flag) {
363 part_chksum_offset = image_base_addr +
364 (part_chksum_offset <<
366 status = zynq_validate_partition(part_load_addr,
371 printf("PART_CHKSUM_FAIL\n");
374 debug("Partition Validation Done\n");
377 if (signed_part_flag) {
378 status = zynq_authenticate_part((u8 *)part_load_addr,
381 printf("AUTHENTICATION_FAIL\n");
384 debug("Authentication Done\n");
387 if (encrypt_part_flag) {
388 debug("DECRYPTION\n");
390 part_dst_addr = part_load_addr;
392 if (part_attr & ZYNQ_ATTRIBUTE_PL_IMAGE_MASK) {
397 status = zynq_decrypt_load(part_load_addr,
402 printf("DECRYPTION_FAIL\n");
412 static int do_zynq_rsa(struct cmd_tbl *cmdtp, int flag, int argc,
418 if (argc != cmdtp->maxargs)
419 return CMD_RET_FAILURE;
421 src_ptr = simple_strtoul(argv[2], &endp, 16);
422 if (*argv[2] == 0 || *endp != 0)
423 return CMD_RET_USAGE;
425 if (zynq_verify_image(src_ptr))
426 return CMD_RET_FAILURE;
428 return CMD_RET_SUCCESS;
432 #ifdef CONFIG_CMD_ZYNQ_AES
433 static int zynq_decrypt_image(struct cmd_tbl *cmdtp, int flag, int argc,
437 u32 srcaddr, srclen, dstaddr, dstlen;
440 if (argc < 5 && argc > cmdtp->maxargs)
441 return CMD_RET_USAGE;
443 srcaddr = simple_strtoul(argv[2], &endp, 16);
444 if (*argv[2] == 0 || *endp != 0)
445 return CMD_RET_USAGE;
446 srclen = simple_strtoul(argv[3], &endp, 16);
447 if (*argv[3] == 0 || *endp != 0)
448 return CMD_RET_USAGE;
449 dstaddr = simple_strtoul(argv[4], &endp, 16);
450 if (*argv[4] == 0 || *endp != 0)
451 return CMD_RET_USAGE;
452 dstlen = simple_strtoul(argv[5], &endp, 16);
453 if (*argv[5] == 0 || *endp != 0)
454 return CMD_RET_USAGE;
457 * Roundup source and destination lengths to
461 srclen = roundup(srclen, 4);
463 dstlen = roundup(dstlen, 4);
465 status = zynq_decrypt_load(srcaddr, srclen >> 2, dstaddr, dstlen >> 2);
467 return CMD_RET_FAILURE;
469 return CMD_RET_SUCCESS;
473 static struct cmd_tbl zynq_commands[] = {
474 #ifdef CONFIG_CMD_ZYNQ_RSA
475 U_BOOT_CMD_MKENT(rsa, 3, 1, do_zynq_rsa, "", ""),
477 #ifdef CONFIG_CMD_ZYNQ_AES
478 U_BOOT_CMD_MKENT(aes, 6, 1, zynq_decrypt_image, "", ""),
482 static int do_zynq(struct cmd_tbl *cmdtp, int flag, int argc,
485 struct cmd_tbl *zynq_cmd;
488 if (!ARRAY_SIZE(zynq_commands)) {
489 puts("No zynq specific command enabled\n");
490 return CMD_RET_USAGE;
494 return CMD_RET_USAGE;
495 zynq_cmd = find_cmd_tbl(argv[1], zynq_commands,
496 ARRAY_SIZE(zynq_commands));
498 return CMD_RET_USAGE;
500 ret = zynq_cmd->cmd(zynq_cmd, flag, argc, argv);
502 return cmd_process_error(zynq_cmd, ret);
505 #ifdef CONFIG_SYS_LONGHELP
506 static char zynq_help_text[] =
508 #ifdef CONFIG_CMD_ZYNQ_RSA
509 "rsa <baseaddr> - Verifies the authenticated and encrypted\n"
510 " zynq images and loads them back to load\n"
511 " addresses as specified in BOOT image(BOOT.BIN)\n"
513 #ifdef CONFIG_CMD_ZYNQ_AES
514 "aes <srcaddr> <srclen> <dstaddr> <dstlen>\n"
515 " - Decrypts the encrypted image present in source\n"
516 " address and places the decrypted image at\n"
517 " destination address\n"
522 U_BOOT_CMD(zynq, 6, 0, do_zynq,
523 "Zynq specific commands", zynq_help_text