blob: 1efbe2e607ca46d1cee923366ded16b1a8b1d289 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +02002/*
3 * Copyright (C) 2016 Marvell International Ltd.
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +02004 * https://spdx.org/licenses
5 */
6
7#include <config.h>
8#include <common.h>
9#include <command.h>
Simon Glass7b51b572019-08-01 09:46:52 -060010#include <env.h>
Simon Glass8e8ccfe2019-12-28 10:45:03 -070011#include <image.h>
Simon Glass90526e92020-05-10 11:39:56 -060012#include <net.h>
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +020013#include <vsprintf.h>
14#include <errno.h>
15#include <dm.h>
Pali Rohárf7b0bbc2022-08-23 14:52:24 +020016#include <fuse.h>
17#include <mach/efuse.h>
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +020018
19#include <spi_flash.h>
20#include <spi.h>
21#include <nand.h>
22#include <usb.h>
23#include <fs.h>
24#include <mmc.h>
Konstantin Porotchkine559ef12017-01-08 16:52:06 +020025#ifdef CONFIG_BLK
26#include <blk.h>
27#endif
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +020028#include <u-boot/sha1.h>
29#include <u-boot/sha256.h>
Pali Rohár93c13582022-07-26 16:11:58 +020030#include <u-boot/sha512.h>
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +020031
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +020032#if defined(CONFIG_ARMADA_8K)
33#define MAIN_HDR_MAGIC 0xB105B002
34
35struct mvebu_image_header {
36 u32 magic; /* 0-3 */
37 u32 prolog_size; /* 4-7 */
38 u32 prolog_checksum; /* 8-11 */
39 u32 boot_image_size; /* 12-15 */
40 u32 boot_image_checksum; /* 16-19 */
41 u32 rsrvd0; /* 20-23 */
42 u32 load_addr; /* 24-27 */
43 u32 exec_addr; /* 28-31 */
44 u8 uart_cfg; /* 32 */
45 u8 baudrate; /* 33 */
46 u8 ext_count; /* 34 */
47 u8 aux_flags; /* 35 */
48 u32 io_arg_0; /* 36-39 */
49 u32 io_arg_1; /* 40-43 */
50 u32 io_arg_2; /* 43-47 */
51 u32 io_arg_3; /* 48-51 */
52 u32 rsrvd1; /* 52-55 */
53 u32 rsrvd2; /* 56-59 */
54 u32 rsrvd3; /* 60-63 */
55};
56#elif defined(CONFIG_ARMADA_3700) /* A3700 */
57#define HASH_SUM_LEN 16
58#define IMAGE_VERSION_3_6_0 0x030600
59#define IMAGE_VERSION_3_5_0 0x030500
60
Pali Rohár5d2f7d32022-07-26 16:11:56 +020061struct tim_boot_flash_sign {
62 unsigned int id;
63 const char *name;
64};
65
66struct tim_boot_flash_sign tim_boot_flash_signs[] = {
67 { 0x454d4d08, "mmc" },
68 { 0x454d4d0b, "mmc" },
69 { 0x5350490a, "spi" },
70 { 0x5350491a, "nand" },
71 { 0x55415223, "uart" },
72 { 0x53415432, "sata" },
73 {},
74};
75
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +020076struct common_tim_data {
77 u32 version;
78 u32 identifier;
79 u32 trusted;
80 u32 issue_date;
81 u32 oem_unique_id;
82 u32 reserved[5]; /* Reserve 20 bytes */
83 u32 boot_flash_sign;
84 u32 num_images;
85 u32 num_keys;
86 u32 size_of_reserved;
87};
88
89struct mvebu_image_info {
90 u32 image_id;
91 u32 next_image_id;
92 u32 flash_entry_addr;
93 u32 load_addr;
94 u32 image_size;
95 u32 image_size_to_hash;
96 u32 hash_algorithm_id;
97 u32 hash[HASH_SUM_LEN]; /* Reserve 512 bits for the hash */
98 u32 partition_number;
99 u32 enc_algorithm_id;
100 u32 encrypt_start_offset;
101 u32 encrypt_size;
102};
Pali Rohár5d2f7d32022-07-26 16:11:56 +0200103#elif defined(CONFIG_ARMADA_32BIT)
Joel Johnson8ec6db72020-04-17 09:38:04 -0600104
Pali Rohár7af368f2021-10-22 12:41:10 +0200105/* Structure of the main header, version 1 (Armada 370/XP/375/38x/39x) */
Joel Johnson8ec6db72020-04-17 09:38:04 -0600106struct a38x_main_hdr_v1 {
107 u8 blockid; /* 0x0 */
108 u8 flags; /* 0x1 */
Pali Rohár33cdd9e2021-10-22 12:37:48 +0200109 u16 nandpagesize; /* 0x2-0x3 */
Joel Johnson8ec6db72020-04-17 09:38:04 -0600110 u32 blocksize; /* 0x4-0x7 */
111 u8 version; /* 0x8 */
112 u8 headersz_msb; /* 0x9 */
113 u16 headersz_lsb; /* 0xA-0xB */
114 u32 srcaddr; /* 0xC-0xF */
115 u32 destaddr; /* 0x10-0x13 */
116 u32 execaddr; /* 0x14-0x17 */
117 u8 options; /* 0x18 */
118 u8 nandblocksize; /* 0x19 */
119 u8 nandbadblklocation; /* 0x1A */
120 u8 reserved4; /* 0x1B */
121 u16 reserved5; /* 0x1C-0x1D */
122 u8 ext; /* 0x1E */
123 u8 checksum; /* 0x1F */
124};
Joel Johnson658854a2020-04-17 09:38:06 -0600125
Pali Rohárf7b0bbc2022-08-23 14:52:24 +0200126/*
127 * Header for the optional headers, version 1 (Armada 370/XP/375/38x/39x)
128 */
129struct a38x_opt_hdr_v1 {
130 u8 headertype;
131 u8 headersz_msb;
132 u16 headersz_lsb;
133 u8 data[0];
134};
135#define A38X_OPT_HDR_V1_SECURE_TYPE 0x1
136
Joel Johnson658854a2020-04-17 09:38:06 -0600137struct a38x_boot_mode {
138 unsigned int id;
139 const char *name;
140};
141
142/* The blockid header field values used to indicate boot device of image */
143struct a38x_boot_mode a38x_boot_modes[] = {
144 { 0x4D, "i2c" },
145 { 0x5A, "spi" },
146 { 0x69, "uart" },
147 { 0x78, "sata" },
148 { 0x8B, "nand" },
149 { 0x9C, "pex" },
150 { 0xAE, "mmc" },
151 {},
152};
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200153
Pali Rohár5d2f7d32022-07-26 16:11:56 +0200154#endif
155
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200156struct bubt_dev {
157 char name[8];
158 size_t (*read)(const char *file_name);
159 int (*write)(size_t image_size);
160 int (*active)(void);
161};
162
163static ulong get_load_addr(void)
164{
165 const char *addr_str;
166 unsigned long addr;
167
Simon Glass00caae62017-08-03 12:22:12 -0600168 addr_str = env_get("loadaddr");
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200169 if (addr_str)
Simon Glass7e5f4602021-07-24 09:03:29 -0600170 addr = hextoul(addr_str, NULL);
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200171 else
172 addr = CONFIG_SYS_LOAD_ADDR;
173
174 return addr;
175}
176
177/********************************************************************
178 * eMMC services
179 ********************************************************************/
Jean-Jacques Hiblotd6400c32018-01-04 15:23:32 +0100180#if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(MMC_WRITE)
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200181static int mmc_burn_image(size_t image_size)
182{
183 struct mmc *mmc;
184 lbaint_t start_lba;
185 lbaint_t blk_count;
186 ulong blk_written;
187 int err;
188 const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
Konstantin Porotchkine559ef12017-01-08 16:52:06 +0200189#ifdef CONFIG_BLK
190 struct blk_desc *blk_desc;
191#endif
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200192 mmc = find_mmc_device(mmc_dev_num);
193 if (!mmc) {
194 printf("No SD/MMC/eMMC card found\n");
195 return -ENOMEDIUM;
196 }
197
198 err = mmc_init(mmc);
199 if (err) {
200 printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
201 mmc_dev_num);
202 return err;
203 }
204
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200205 /* SD reserves LBA-0 for MBR and boots from LBA-1,
206 * MMC/eMMC boots from LBA-0
207 */
208 start_lba = IS_SD(mmc) ? 1 : 0;
Konstantin Porotchkine559ef12017-01-08 16:52:06 +0200209#ifdef CONFIG_BLK
210 blk_count = image_size / mmc->write_bl_len;
211 if (image_size % mmc->write_bl_len)
212 blk_count += 1;
213
214 blk_desc = mmc_get_blk_desc(mmc);
215 if (!blk_desc) {
216 printf("Error - failed to obtain block descriptor\n");
217 return -ENODEV;
218 }
219 blk_written = blk_dwrite(blk_desc, start_lba, blk_count,
220 (void *)get_load_addr());
221#else
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200222 blk_count = image_size / mmc->block_dev.blksz;
223 if (image_size % mmc->block_dev.blksz)
224 blk_count += 1;
225
226 blk_written = mmc->block_dev.block_write(mmc_dev_num,
Konstantin Porotchkine559ef12017-01-08 16:52:06 +0200227 start_lba, blk_count,
228 (void *)get_load_addr());
229#endif /* CONFIG_BLK */
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200230 if (blk_written != blk_count) {
231 printf("Error - written %#lx blocks\n", blk_written);
232 return -ENOSPC;
233 }
234 printf("Done!\n");
235
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200236 return 0;
237}
238
239static size_t mmc_read_file(const char *file_name)
240{
241 loff_t act_read = 0;
242 int rc;
243 struct mmc *mmc;
244 const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
245
246 mmc = find_mmc_device(mmc_dev_num);
247 if (!mmc) {
248 printf("No SD/MMC/eMMC card found\n");
249 return 0;
250 }
251
252 if (mmc_init(mmc)) {
253 printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
254 mmc_dev_num);
255 return 0;
256 }
257
258 /* Load from data partition (0) */
259 if (fs_set_blk_dev("mmc", "0", FS_TYPE_ANY)) {
260 printf("Error: MMC 0 not found\n");
261 return 0;
262 }
263
264 /* Perfrom file read */
265 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
266 if (rc)
267 return 0;
268
269 return act_read;
270}
271
272static int is_mmc_active(void)
273{
274 return 1;
275}
276#else /* CONFIG_DM_MMC */
277static int mmc_burn_image(size_t image_size)
278{
279 return -ENODEV;
280}
281
282static size_t mmc_read_file(const char *file_name)
283{
284 return 0;
285}
286
287static int is_mmc_active(void)
288{
289 return 0;
290}
291#endif /* CONFIG_DM_MMC */
292
293/********************************************************************
294 * SPI services
295 ********************************************************************/
296#ifdef CONFIG_SPI_FLASH
297static int spi_burn_image(size_t image_size)
298{
299 int ret;
300 struct spi_flash *flash;
301 u32 erase_bytes;
302
303 /* Probe the SPI bus to get the flash device */
Tom Rini7e6a6fd2021-12-11 14:55:48 -0500304 flash = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
305 CONFIG_SF_DEFAULT_CS,
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200306 CONFIG_SF_DEFAULT_SPEED,
307 CONFIG_SF_DEFAULT_MODE);
308 if (!flash) {
309 printf("Failed to probe SPI Flash\n");
310 return -ENOMEDIUM;
311 }
312
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200313 erase_bytes = image_size +
314 (flash->erase_size - image_size % flash->erase_size);
315 printf("Erasing %d bytes (%d blocks) at offset 0 ...",
316 erase_bytes, erase_bytes / flash->erase_size);
317 ret = spi_flash_erase(flash, 0, erase_bytes);
318 if (ret)
319 printf("Error!\n");
320 else
321 printf("Done!\n");
322
323 printf("Writing %d bytes from 0x%lx to offset 0 ...",
324 (int)image_size, get_load_addr());
325 ret = spi_flash_write(flash, 0, image_size, (void *)get_load_addr());
326 if (ret)
327 printf("Error!\n");
328 else
329 printf("Done!\n");
330
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200331 return ret;
332}
333
334static int is_spi_active(void)
335{
336 return 1;
337}
338
339#else /* CONFIG_SPI_FLASH */
340static int spi_burn_image(size_t image_size)
341{
342 return -ENODEV;
343}
344
345static int is_spi_active(void)
346{
347 return 0;
348}
349#endif /* CONFIG_SPI_FLASH */
350
351/********************************************************************
352 * NAND services
353 ********************************************************************/
354#ifdef CONFIG_CMD_NAND
355static int nand_burn_image(size_t image_size)
356{
Konstantin Porotchkinf2ca24d2017-03-28 18:16:56 +0300357 int ret;
358 uint32_t block_size;
Grygorii Strashko7021c7e2017-06-26 19:13:02 -0500359 struct mtd_info *mtd;
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200360
Grygorii Strashko7021c7e2017-06-26 19:13:02 -0500361 mtd = get_nand_dev_by_index(nand_curr_device);
362 if (!mtd) {
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200363 puts("\nno devices available\n");
364 return -ENOMEDIUM;
365 }
Grygorii Strashko7021c7e2017-06-26 19:13:02 -0500366 block_size = mtd->erasesize;
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200367
368 /* Align U-Boot size to currently used blocksize */
369 image_size = ((image_size + (block_size - 1)) & (~(block_size - 1)));
370
Joel Johnsonf41b85e2020-04-17 09:38:05 -0600371 /* Erase the U-Boot image space */
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200372 printf("Erasing 0x%x - 0x%x:...", 0, (int)image_size);
Grygorii Strashko7021c7e2017-06-26 19:13:02 -0500373 ret = nand_erase(mtd, 0, image_size);
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200374 if (ret) {
375 printf("Error!\n");
376 goto error;
377 }
378 printf("Done!\n");
379
380 /* Write the image to flash */
Konstantin Porotchkinf2ca24d2017-03-28 18:16:56 +0300381 printf("Writing %d bytes from 0x%lx to offset 0 ... ",
382 (int)image_size, get_load_addr());
Grygorii Strashko7021c7e2017-06-26 19:13:02 -0500383 ret = nand_write(mtd, 0, &image_size, (void *)get_load_addr());
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200384 if (ret)
385 printf("Error!\n");
386 else
387 printf("Done!\n");
388
389error:
390 return ret;
391}
392
393static int is_nand_active(void)
394{
395 return 1;
396}
397
398#else /* CONFIG_CMD_NAND */
399static int nand_burn_image(size_t image_size)
400{
401 return -ENODEV;
402}
403
404static int is_nand_active(void)
405{
406 return 0;
407}
408#endif /* CONFIG_CMD_NAND */
409
410/********************************************************************
411 * USB services
412 ********************************************************************/
413#if defined(CONFIG_USB_STORAGE) && defined(CONFIG_BLK)
414static size_t usb_read_file(const char *file_name)
415{
416 loff_t act_read = 0;
417 struct udevice *dev;
418 int rc;
419
420 usb_stop();
421
422 if (usb_init() < 0) {
423 printf("Error: usb_init failed\n");
424 return 0;
425 }
426
427 /* Try to recognize storage devices immediately */
Simon Glasse33a5c62022-08-11 19:34:59 -0600428 blk_first_device(UCLASS_USB, &dev);
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200429 if (!dev) {
430 printf("Error: USB storage device not found\n");
431 return 0;
432 }
433
434 /* Always load from usb 0 */
435 if (fs_set_blk_dev("usb", "0", FS_TYPE_ANY)) {
436 printf("Error: USB 0 not found\n");
437 return 0;
438 }
439
440 /* Perfrom file read */
441 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
442 if (rc)
443 return 0;
444
445 return act_read;
446}
447
448static int is_usb_active(void)
449{
450 return 1;
451}
452
453#else /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
454static size_t usb_read_file(const char *file_name)
455{
456 return 0;
457}
458
459static int is_usb_active(void)
460{
461 return 0;
462}
463#endif /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
464
465/********************************************************************
466 * Network services
467 ********************************************************************/
468#ifdef CONFIG_CMD_NET
469static size_t tftp_read_file(const char *file_name)
470{
Pali Rohár4b9521f2022-07-26 16:11:57 +0200471 int ret;
472
Simon Glassbb872dd2019-12-28 10:45:02 -0700473 /*
474 * update global variable image_load_addr before tftp file from network
475 */
476 image_load_addr = get_load_addr();
Pali Rohár4b9521f2022-07-26 16:11:57 +0200477 ret = net_loop(TFTPGET);
478 return ret > 0 ? ret : 0;
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200479}
480
481static int is_tftp_active(void)
482{
483 return 1;
484}
485
486#else
487static size_t tftp_read_file(const char *file_name)
488{
489 return 0;
490}
491
492static int is_tftp_active(void)
493{
494 return 0;
495}
496#endif /* CONFIG_CMD_NET */
497
498enum bubt_devices {
499 BUBT_DEV_NET = 0,
500 BUBT_DEV_USB,
501 BUBT_DEV_MMC,
502 BUBT_DEV_SPI,
503 BUBT_DEV_NAND,
504
505 BUBT_MAX_DEV
506};
507
508struct bubt_dev bubt_devs[BUBT_MAX_DEV] = {
509 {"tftp", tftp_read_file, NULL, is_tftp_active},
510 {"usb", usb_read_file, NULL, is_usb_active},
511 {"mmc", mmc_read_file, mmc_burn_image, is_mmc_active},
512 {"spi", NULL, spi_burn_image, is_spi_active},
513 {"nand", NULL, nand_burn_image, is_nand_active},
514};
515
516static int bubt_write_file(struct bubt_dev *dst, size_t image_size)
517{
518 if (!dst->write) {
519 printf("Error: Write not supported on device %s\n", dst->name);
520 return -ENOTSUPP;
521 }
522
523 return dst->write(image_size);
524}
525
526#if defined(CONFIG_ARMADA_8K)
527u32 do_checksum32(u32 *start, int32_t len)
528{
529 u32 sum = 0;
530 u32 *startp = start;
531
532 do {
533 sum += *startp;
534 startp++;
535 len -= 4;
536 } while (len > 0);
537
538 return sum;
539}
540
541static int check_image_header(void)
542{
543 struct mvebu_image_header *hdr =
544 (struct mvebu_image_header *)get_load_addr();
545 u32 header_len = hdr->prolog_size;
546 u32 checksum;
547 u32 checksum_ref = hdr->prolog_checksum;
548
549 /*
550 * For now compare checksum, and magic. Later we can
551 * verify more stuff on the header like interface type, etc
552 */
553 if (hdr->magic != MAIN_HDR_MAGIC) {
554 printf("ERROR: Bad MAGIC 0x%08x != 0x%08x\n",
555 hdr->magic, MAIN_HDR_MAGIC);
556 return -ENOEXEC;
557 }
558
559 /* The checksum value is discarded from checksum calculation */
560 hdr->prolog_checksum = 0;
561
562 checksum = do_checksum32((u32 *)hdr, header_len);
563 if (checksum != checksum_ref) {
564 printf("Error: Bad Image checksum. 0x%x != 0x%x\n",
565 checksum, checksum_ref);
566 return -ENOEXEC;
567 }
568
569 /* Restore the checksum before writing */
570 hdr->prolog_checksum = checksum_ref;
571 printf("Image checksum...OK!\n");
572
573 return 0;
574}
575#elif defined(CONFIG_ARMADA_3700) /* Armada 3700 */
576static int check_image_header(void)
577{
578 struct common_tim_data *hdr = (struct common_tim_data *)get_load_addr();
579 int image_num;
580 u8 hash_160_output[SHA1_SUM_LEN];
581 u8 hash_256_output[SHA256_SUM_LEN];
Pali Rohár93c13582022-07-26 16:11:58 +0200582 u8 hash_512_output[SHA512_SUM_LEN];
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200583 sha1_context hash1_text;
584 sha256_context hash256_text;
Pali Rohár93c13582022-07-26 16:11:58 +0200585 sha512_context hash512_text;
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200586 u8 *hash_output;
587 u32 hash_algorithm_id;
588 u32 image_size_to_hash;
589 u32 flash_entry_addr;
590 u32 *hash_value;
591 u32 internal_hash[HASH_SUM_LEN];
592 const u8 *buff;
593 u32 num_of_image = hdr->num_images;
594 u32 version = hdr->version;
595 u32 trusted = hdr->trusted;
596
597 /* bubt checksum validation only supports nontrusted images */
598 if (trusted == 1) {
599 printf("bypass image validation, ");
600 printf("only untrusted image is supported now\n");
601 return 0;
602 }
603 /* only supports image version 3.5 and 3.6 */
604 if (version != IMAGE_VERSION_3_5_0 && version != IMAGE_VERSION_3_6_0) {
605 printf("Error: Unsupported Image version = 0x%08x\n", version);
606 return -ENOEXEC;
607 }
608 /* validate images hash value */
609 for (image_num = 0; image_num < num_of_image; image_num++) {
610 struct mvebu_image_info *info =
611 (struct mvebu_image_info *)(get_load_addr() +
612 sizeof(struct common_tim_data) +
613 image_num * sizeof(struct mvebu_image_info));
614 hash_algorithm_id = info->hash_algorithm_id;
615 image_size_to_hash = info->image_size_to_hash;
616 flash_entry_addr = info->flash_entry_addr;
617 hash_value = info->hash;
618 buff = (const u8 *)(get_load_addr() + flash_entry_addr);
619
620 if (image_num == 0) {
621 /*
622 * The first image includes hash values in its content.
623 * For hash calculation, we need to save the original
624 * hash values to a local variable that will be
625 * copied back for comparsion and set all zeros to
626 * the orignal hash values for calculating new value.
627 * First image original format :
628 * x...x (datum1) x...x(orig. hash values) x...x(datum2)
629 * Replaced first image format :
630 * x...x (datum1) 0...0(hash values) x...x(datum2)
631 */
632 memcpy(internal_hash, hash_value,
633 sizeof(internal_hash));
634 memset(hash_value, 0, sizeof(internal_hash));
635 }
636 if (image_size_to_hash == 0) {
637 printf("Warning: Image_%d hash checksum is disabled, ",
638 image_num);
639 printf("skip the image validation.\n");
640 continue;
641 }
642 switch (hash_algorithm_id) {
643 case SHA1_SUM_LEN:
644 sha1_starts(&hash1_text);
645 sha1_update(&hash1_text, buff, image_size_to_hash);
646 sha1_finish(&hash1_text, hash_160_output);
647 hash_output = hash_160_output;
648 break;
649 case SHA256_SUM_LEN:
650 sha256_starts(&hash256_text);
651 sha256_update(&hash256_text, buff, image_size_to_hash);
652 sha256_finish(&hash256_text, hash_256_output);
653 hash_output = hash_256_output;
654 break;
Pali Rohár93c13582022-07-26 16:11:58 +0200655 case SHA512_SUM_LEN:
656 sha512_starts(&hash512_text);
657 sha512_update(&hash512_text, buff, image_size_to_hash);
658 sha512_finish(&hash512_text, hash_512_output);
659 hash_output = hash_512_output;
660 break;
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200661 default:
662 printf("Error: Unsupported hash_algorithm_id = %d\n",
663 hash_algorithm_id);
664 return -ENOEXEC;
665 }
666 if (image_num == 0)
667 memcpy(hash_value, internal_hash,
668 sizeof(internal_hash));
669 if (memcmp(hash_value, hash_output, hash_algorithm_id) != 0) {
670 printf("Error: Image_%d checksum is not correct\n",
671 image_num);
672 return -ENOEXEC;
673 }
674 }
675 printf("Image checksum...OK!\n");
676
677 return 0;
678}
Pali Rohár5d2f7d32022-07-26 16:11:56 +0200679#elif defined(CONFIG_ARMADA_32BIT)
Joel Johnson8ec6db72020-04-17 09:38:04 -0600680static size_t a38x_header_size(const struct a38x_main_hdr_v1 *h)
681{
682 if (h->version == 1)
683 return (h->headersz_msb << 16) | le16_to_cpu(h->headersz_lsb);
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200684
Joel Johnson8ec6db72020-04-17 09:38:04 -0600685 printf("Error: Invalid A38x image (header version 0x%x unknown)!\n",
686 h->version);
687 return 0;
688}
689
690static uint8_t image_checksum8(const void *start, size_t len)
691{
692 u8 csum = 0;
693 const u8 *p = start;
694
695 while (len) {
696 csum += *p;
697 ++p;
698 --len;
699 }
700
701 return csum;
702}
703
Pali Rohár5a065342022-08-23 14:52:23 +0200704static uint32_t image_checksum32(const void *start, size_t len)
705{
706 u32 csum = 0;
707 const u32 *p = start;
708
709 while (len) {
710 csum += *p;
711 ++p;
712 len -= sizeof(u32);
713 }
714
715 return csum;
716}
717
Joel Johnson8ec6db72020-04-17 09:38:04 -0600718static int check_image_header(void)
719{
720 u8 checksum;
Pali Rohár5a065342022-08-23 14:52:23 +0200721 u32 checksum32, exp_checksum32;
722 u32 offset, size;
Joel Johnson8ec6db72020-04-17 09:38:04 -0600723 const struct a38x_main_hdr_v1 *hdr =
724 (struct a38x_main_hdr_v1 *)get_load_addr();
725 const size_t image_size = a38x_header_size(hdr);
726
727 if (!image_size)
728 return -ENOEXEC;
729
730 checksum = image_checksum8(hdr, image_size);
731 checksum -= hdr->checksum;
732 if (checksum != hdr->checksum) {
Pali Rohár5a065342022-08-23 14:52:23 +0200733 printf("Error: Bad A38x image header checksum. 0x%x != 0x%x\n",
Joel Johnson8ec6db72020-04-17 09:38:04 -0600734 checksum, hdr->checksum);
735 return -ENOEXEC;
736 }
737
Pali Rohár5a065342022-08-23 14:52:23 +0200738 offset = le32_to_cpu(hdr->srcaddr);
739 size = le32_to_cpu(hdr->blocksize);
740
741 if (hdr->blockid == 0x78) { /* SATA id */
742 if (offset < 1) {
743 printf("Error: Bad A38x image srcaddr.\n");
744 return -ENOEXEC;
745 }
746 offset -= 1;
747 offset *= 512;
748 }
749
750 if (hdr->blockid == 0xAE) /* SDIO id */
751 offset *= 512;
752
753 if (offset % 4 != 0 || size < 4 || size % 4 != 0) {
754 printf("Error: Bad A38x image blocksize.\n");
755 return -ENOEXEC;
756 }
757
758 checksum32 = image_checksum32((u8 *)hdr + offset, size - 4);
759 exp_checksum32 = *(u32 *)((u8 *)hdr + offset + size - 4);
760 if (checksum32 != exp_checksum32) {
761 printf("Error: Bad A38x image data checksum. 0x%08x != 0x%08x\n",
762 checksum32, exp_checksum32);
763 return -ENOEXEC;
764 }
765
Joel Johnson8ec6db72020-04-17 09:38:04 -0600766 printf("Image checksum...OK!\n");
767 return 0;
768}
Pali Rohárf7b0bbc2022-08-23 14:52:24 +0200769
770#if defined(CONFIG_ARMADA_38X)
771static int a38x_image_is_secure(const struct a38x_main_hdr_v1 *hdr)
772{
773 u32 image_size = a38x_header_size(hdr);
774 struct a38x_opt_hdr_v1 *ohdr;
775 u32 ohdr_size;
776
777 if (hdr->version != 1)
778 return 0;
779
780 if (!hdr->ext)
781 return 0;
782
783 ohdr = (struct a38x_opt_hdr_v1 *)(hdr + 1);
784 do {
785 if (ohdr->headertype == A38X_OPT_HDR_V1_SECURE_TYPE)
786 return 1;
787
788 ohdr_size = (ohdr->headersz_msb << 16) | le16_to_cpu(ohdr->headersz_lsb);
789
790 if (!*((u8 *)ohdr + ohdr_size - 4))
791 break;
792
793 ohdr = (struct a38x_opt_hdr_v1 *)((u8 *)ohdr + ohdr_size);
794 if ((u8 *)ohdr >= (u8 *)hdr + image_size)
795 break;
796 } while (1);
797
798 return 0;
799}
800#endif
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200801#else /* Not ARMADA? */
802static int check_image_header(void)
803{
804 printf("bubt cmd does not support this SoC device or family!\n");
805 return -ENOEXEC;
806}
807#endif
808
Pali Rohárf7b0bbc2022-08-23 14:52:24 +0200809#if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_32BIT)
810static u64 fuse_read_u64(u32 bank)
811{
812 u32 val[2];
813 int ret;
814
815 ret = fuse_read(bank, 0, &val[0]);
816 if (ret < 0)
817 return -1;
818
819 ret = fuse_read(bank, 1, &val[1]);
820 if (ret < 0)
821 return -1;
822
823 return ((u64)val[1] << 32) | val[0];
824}
825#endif
826
827#if defined(CONFIG_ARMADA_3700)
828static inline u8 maj3(u8 val)
829{
830 /* return majority vote of 3 bits */
831 return ((val & 0x7) == 3 || (val & 0x7) > 4) ? 1 : 0;
832}
833#endif
834
Joel Johnson658854a2020-04-17 09:38:06 -0600835static int bubt_check_boot_mode(const struct bubt_dev *dst)
836{
Pali Rohár5d2f7d32022-07-26 16:11:56 +0200837#if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_32BIT)
Pali Rohárf7b0bbc2022-08-23 14:52:24 +0200838 int mode, secure_mode;
Pali Rohár5d2f7d32022-07-26 16:11:56 +0200839#if defined(CONFIG_ARMADA_3700)
840 const struct tim_boot_flash_sign *boot_modes = tim_boot_flash_signs;
841 const struct common_tim_data *hdr =
842 (struct common_tim_data *)get_load_addr();
843 u32 id = hdr->boot_flash_sign;
Pali Rohárf7b0bbc2022-08-23 14:52:24 +0200844 int is_secure = hdr->trusted != 0;
845 u64 otp_secure_bits = fuse_read_u64(1);
846 int otp_secure_boot = ((maj3(otp_secure_bits >> 0) << 0) |
847 (maj3(otp_secure_bits >> 4) << 1)) == 2;
848 unsigned int otp_boot_device = (maj3(otp_secure_bits >> 48) << 0) |
849 (maj3(otp_secure_bits >> 52) << 1) |
850 (maj3(otp_secure_bits >> 56) << 2) |
851 (maj3(otp_secure_bits >> 60) << 3);
Pali Rohár5d2f7d32022-07-26 16:11:56 +0200852#elif defined(CONFIG_ARMADA_32BIT)
853 const struct a38x_boot_mode *boot_modes = a38x_boot_modes;
854 const struct a38x_main_hdr_v1 *hdr =
855 (struct a38x_main_hdr_v1 *)get_load_addr();
856 u32 id = hdr->blockid;
Pali Rohárf7b0bbc2022-08-23 14:52:24 +0200857#if defined(CONFIG_ARMADA_38X)
858 int is_secure = a38x_image_is_secure(hdr);
859 u64 otp_secure_bits = fuse_read_u64(EFUSE_LINE_SECURE_BOOT);
860 int otp_secure_boot = otp_secure_bits & 0x1;
861 unsigned int otp_boot_device = (otp_secure_bits >> 8) & 0x7;
862#endif
Pali Rohár5d2f7d32022-07-26 16:11:56 +0200863#endif
Joel Johnson658854a2020-04-17 09:38:06 -0600864
Pali Rohár5d2f7d32022-07-26 16:11:56 +0200865 for (mode = 0; boot_modes[mode].name; mode++) {
866 if (boot_modes[mode].id == id)
867 break;
Joel Johnson658854a2020-04-17 09:38:06 -0600868 }
Pali Rohár5d2f7d32022-07-26 16:11:56 +0200869
870 if (!boot_modes[mode].name) {
871 printf("Error: unknown boot device in image header: 0x%x\n", id);
872 return -ENOEXEC;
873 }
874
Pali Rohárf7b0bbc2022-08-23 14:52:24 +0200875 if (strcmp(boot_modes[mode].name, dst->name) != 0) {
876 printf("Error: image meant to be booted from \"%s\", not \"%s\"!\n",
877 boot_modes[mode].name, dst->name);
878 return -ENOEXEC;
879 }
Pali Rohár5d2f7d32022-07-26 16:11:56 +0200880
Pali Rohárf7b0bbc2022-08-23 14:52:24 +0200881#if defined(CONFIG_ARMADA_38X) || defined(CONFIG_ARMADA_3700)
882 if (otp_secure_bits == (u64)-1) {
883 printf("Error: cannot read OTP secure bits\n");
884 return -ENOEXEC;
885 } else {
886 if (otp_secure_boot && !is_secure) {
887 printf("Error: secure boot is enabled in OTP but image does not have secure boot header!\n");
888 return -ENOEXEC;
889 } else if (!otp_secure_boot && is_secure) {
890#if defined(CONFIG_ARMADA_3700)
891 /*
892 * Armada 3700 BootROM rejects trusted image when secure boot is not enabled.
893 * Armada 385 BootROM accepts image with secure boot header also when secure boot is not enabled.
894 */
895 printf("Error: secure boot is disabled in OTP but image has secure boot header!\n");
896 return -ENOEXEC;
Pali Rohár5d2f7d32022-07-26 16:11:56 +0200897#endif
Pali Rohárf7b0bbc2022-08-23 14:52:24 +0200898 } else if (otp_boot_device && otp_boot_device != id) {
899 for (secure_mode = 0; boot_modes[secure_mode].name; secure_mode++) {
900 if (boot_modes[secure_mode].id == otp_boot_device)
901 break;
902 }
903 printf("Error: boot source is set to \"%s\" in OTP but image is for \"%s\"!\n",
904 boot_modes[secure_mode].name ?: "unknown", dst->name);
905 return -ENOEXEC;
906 }
907 }
908#endif
909#endif
910 return 0;
Joel Johnson658854a2020-04-17 09:38:06 -0600911}
912
913static int bubt_verify(const struct bubt_dev *dst)
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200914{
915 int err;
916
917 /* Check a correct image header exists */
918 err = check_image_header();
919 if (err) {
920 printf("Error: Image header verification failed\n");
921 return err;
922 }
923
Joel Johnson658854a2020-04-17 09:38:06 -0600924 err = bubt_check_boot_mode(dst);
925 if (err) {
926 printf("Error: Image boot mode verification failed\n");
927 return err;
928 }
929
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200930 return 0;
931}
932
933static int bubt_read_file(struct bubt_dev *src)
934{
935 size_t image_size;
936
937 if (!src->read) {
938 printf("Error: Read not supported on device \"%s\"\n",
939 src->name);
940 return 0;
941 }
942
943 image_size = src->read(net_boot_file_name);
944 if (image_size <= 0) {
945 printf("Error: Failed to read file %s from %s\n",
946 net_boot_file_name, src->name);
947 return 0;
948 }
949
950 return image_size;
951}
952
953static int bubt_is_dev_active(struct bubt_dev *dev)
954{
955 if (!dev->active) {
Joel Johnsonf41b85e2020-04-17 09:38:05 -0600956 printf("Device \"%s\" not supported by U-Boot image\n",
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200957 dev->name);
958 return 0;
959 }
960
961 if (!dev->active()) {
962 printf("Device \"%s\" is inactive\n", dev->name);
963 return 0;
964 }
965
966 return 1;
967}
968
969struct bubt_dev *find_bubt_dev(char *dev_name)
970{
971 int dev;
972
973 for (dev = 0; dev < BUBT_MAX_DEV; dev++) {
974 if (strcmp(bubt_devs[dev].name, dev_name) == 0)
975 return &bubt_devs[dev];
976 }
977
978 return 0;
979}
980
981#define DEFAULT_BUBT_SRC "tftp"
982
983#ifndef DEFAULT_BUBT_DST
984#ifdef CONFIG_MVEBU_SPI_BOOT
985#define DEFAULT_BUBT_DST "spi"
986#elif defined(CONFIG_MVEBU_NAND_BOOT)
987#define DEFAULT_BUBT_DST "nand"
988#elif defined(CONFIG_MVEBU_MMC_BOOT)
989#define DEFAULT_BUBT_DST "mmc"
Konstantin Porotchkin9f27bcc2021-03-17 18:53:43 +0200990#else
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200991#define DEFAULT_BUBT_DST "error"
992#endif
993#endif /* DEFAULT_BUBT_DST */
994
Simon Glass09140112020-05-10 11:40:03 -0600995int do_bubt_cmd(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +0200996{
997 struct bubt_dev *src, *dst;
998 size_t image_size;
999 char src_dev_name[8];
1000 char dst_dev_name[8];
1001 char *name;
1002 int err;
1003
1004 if (argc < 2)
1005 copy_filename(net_boot_file_name,
1006 CONFIG_MVEBU_UBOOT_DFLT_NAME,
1007 sizeof(net_boot_file_name));
1008 else
1009 copy_filename(net_boot_file_name, argv[1],
1010 sizeof(net_boot_file_name));
1011
1012 if (argc >= 3) {
1013 strncpy(dst_dev_name, argv[2], 8);
1014 } else {
1015 name = DEFAULT_BUBT_DST;
1016 strncpy(dst_dev_name, name, 8);
1017 }
1018
1019 if (argc >= 4)
1020 strncpy(src_dev_name, argv[3], 8);
1021 else
1022 strncpy(src_dev_name, DEFAULT_BUBT_SRC, 8);
1023
1024 /* Figure out the destination device */
1025 dst = find_bubt_dev(dst_dev_name);
1026 if (!dst) {
1027 printf("Error: Unknown destination \"%s\"\n", dst_dev_name);
Pali Rohár09b0e202022-07-26 16:11:59 +02001028 return 1;
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +02001029 }
1030
1031 if (!bubt_is_dev_active(dst))
Pali Rohár09b0e202022-07-26 16:11:59 +02001032 return 1;
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +02001033
1034 /* Figure out the source device */
1035 src = find_bubt_dev(src_dev_name);
1036 if (!src) {
1037 printf("Error: Unknown source \"%s\"\n", src_dev_name);
1038 return 1;
1039 }
1040
1041 if (!bubt_is_dev_active(src))
1042 return -ENODEV;
1043
Joel Johnsonf41b85e2020-04-17 09:38:05 -06001044 printf("Burning U-Boot image \"%s\" from \"%s\" to \"%s\"\n",
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +02001045 net_boot_file_name, src->name, dst->name);
1046
1047 image_size = bubt_read_file(src);
1048 if (!image_size)
Pali Rohár09b0e202022-07-26 16:11:59 +02001049 return 1;
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +02001050
Joel Johnson658854a2020-04-17 09:38:06 -06001051 err = bubt_verify(dst);
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +02001052 if (err)
Pali Rohár09b0e202022-07-26 16:11:59 +02001053 return 1;
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +02001054
1055 err = bubt_write_file(dst, image_size);
1056 if (err)
Pali Rohár09b0e202022-07-26 16:11:59 +02001057 return 1;
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +02001058
1059 return 0;
1060}
1061
1062U_BOOT_CMD(
1063 bubt, 4, 0, do_bubt_cmd,
1064 "Burn a u-boot image to flash",
1065 "[file-name] [destination [source]]\n"
Pali Rohár32771532021-01-27 11:56:02 +01001066 "\t-file-name The image file name to burn. Default = " CONFIG_MVEBU_UBOOT_DFLT_NAME "\n"
1067 "\t-destination Flash to burn to [spi, nand, mmc]. Default = " DEFAULT_BUBT_DST "\n"
1068 "\t-source The source to load image from [tftp, usb, mmc]. Default = " DEFAULT_BUBT_SRC "\n"
Konstantin Porotchkinfa61ef62016-12-08 12:22:28 +02001069 "Examples:\n"
1070 "\tbubt - Burn flash-image.bin from tftp to active boot device\n"
1071 "\tbubt flash-image-new.bin nand - Burn flash-image-new.bin from tftp to NAND flash\n"
1072 "\tbubt backup-flash-image.bin mmc usb - Burn backup-flash-image.bin from usb to MMC\n"
1073
1074);