Merge tag 'u-boot-nand-20221211' of https://source.denx.de/u-boot/custodians/u-boot...
[platform/kernel/u-boot.git] / cmd / mvebu / bubt.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2016 Marvell International Ltd.
4  * https://spdx.org/licenses
5  */
6
7 #include <config.h>
8 #include <common.h>
9 #include <command.h>
10 #include <env.h>
11 #include <image.h>
12 #include <net.h>
13 #include <vsprintf.h>
14 #include <errno.h>
15 #include <dm.h>
16 #include <fuse.h>
17 #include <mach/efuse.h>
18
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>
25 #ifdef CONFIG_BLK
26 #include <blk.h>
27 #endif
28 #include <u-boot/sha1.h>
29 #include <u-boot/sha256.h>
30 #include <u-boot/sha512.h>
31
32 #if defined(CONFIG_ARMADA_8K)
33 #define MAIN_HDR_MAGIC          0xB105B002
34
35 struct 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
61 struct tim_boot_flash_sign {
62         unsigned int id;
63         const char *name;
64 };
65
66 struct 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
76 struct 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
89 struct 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 };
103 #elif defined(CONFIG_ARMADA_32BIT)
104
105 /* Structure of the main header, version 1 (Armada 370/XP/375/38x/39x) */
106 struct a38x_main_hdr_v1 {
107         u8  blockid;               /* 0x0       */
108         u8  flags;                 /* 0x1       */
109         u16 nandpagesize;          /* 0x2-0x3   */
110         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 };
125
126 /*
127  * Header for the optional headers, version 1 (Armada 370/XP/375/38x/39x)
128  */
129 struct 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
137 struct 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 */
143 struct 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 };
153
154 #endif
155
156 struct 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
163 static ulong get_load_addr(void)
164 {
165         const char *addr_str;
166         unsigned long addr;
167
168         addr_str = env_get("loadaddr");
169         if (addr_str)
170                 addr = hextoul(addr_str, NULL);
171         else
172                 addr = CONFIG_SYS_LOAD_ADDR;
173
174         return addr;
175 }
176
177 /********************************************************************
178  *     eMMC services
179  ********************************************************************/
180 #if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(MMC_WRITE)
181 static 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;
189 #ifdef CONFIG_BLK
190         struct blk_desc *blk_desc;
191 #endif
192         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
205         /* 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;
209 #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
222         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,
227                                                  start_lba, blk_count,
228                                                  (void *)get_load_addr());
229 #endif /* CONFIG_BLK */
230         if (blk_written != blk_count) {
231                 printf("Error - written %#lx blocks\n", blk_written);
232                 return -ENOSPC;
233         }
234         printf("Done!\n");
235
236         return 0;
237 }
238
239 static 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
272 static int is_mmc_active(void)
273 {
274         return 1;
275 }
276 #else /* CONFIG_DM_MMC */
277 static int mmc_burn_image(size_t image_size)
278 {
279         return -ENODEV;
280 }
281
282 static size_t mmc_read_file(const char *file_name)
283 {
284         return 0;
285 }
286
287 static 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
297 static 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 */
304         flash = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
305                                 CONFIG_SF_DEFAULT_CS,
306                                 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
313         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
331         return ret;
332 }
333
334 static int is_spi_active(void)
335 {
336         return 1;
337 }
338
339 #else /* CONFIG_SPI_FLASH */
340 static int spi_burn_image(size_t image_size)
341 {
342         return -ENODEV;
343 }
344
345 static 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
355 static int nand_burn_image(size_t image_size)
356 {
357         int ret;
358         uint32_t block_size;
359         struct mtd_info *mtd;
360
361         mtd = get_nand_dev_by_index(nand_curr_device);
362         if (!mtd) {
363                 puts("\nno devices available\n");
364                 return -ENOMEDIUM;
365         }
366         block_size = mtd->erasesize;
367
368         /* Align U-Boot size to currently used blocksize */
369         image_size = ((image_size + (block_size - 1)) & (~(block_size - 1)));
370
371         /* Erase the U-Boot image space */
372         printf("Erasing 0x%x - 0x%x:...", 0, (int)image_size);
373         ret = nand_erase(mtd, 0, image_size);
374         if (ret) {
375                 printf("Error!\n");
376                 goto error;
377         }
378         printf("Done!\n");
379
380         /* Write the image to flash */
381         printf("Writing %d bytes from 0x%lx to offset 0 ... ",
382                (int)image_size, get_load_addr());
383         ret = nand_write(mtd, 0, &image_size, (void *)get_load_addr());
384         if (ret)
385                 printf("Error!\n");
386         else
387                 printf("Done!\n");
388
389 error:
390         return ret;
391 }
392
393 static int is_nand_active(void)
394 {
395         return 1;
396 }
397
398 #else /* CONFIG_CMD_NAND */
399 static int nand_burn_image(size_t image_size)
400 {
401         return -ENODEV;
402 }
403
404 static 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)
414 static 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 */
428         blk_first_device(UCLASS_USB, &dev);
429         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
448 static int is_usb_active(void)
449 {
450         return 1;
451 }
452
453 #else /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
454 static size_t usb_read_file(const char *file_name)
455 {
456         return 0;
457 }
458
459 static 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
469 static size_t tftp_read_file(const char *file_name)
470 {
471         int ret;
472
473         /*
474          * update global variable image_load_addr before tftp file from network
475          */
476         image_load_addr = get_load_addr();
477         ret = net_loop(TFTPGET);
478         return ret > 0 ? ret : 0;
479 }
480
481 static int is_tftp_active(void)
482 {
483         return 1;
484 }
485
486 #else
487 static size_t tftp_read_file(const char *file_name)
488 {
489         return 0;
490 }
491
492 static int is_tftp_active(void)
493 {
494         return 0;
495 }
496 #endif /* CONFIG_CMD_NET */
497
498 enum 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
508 struct 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
516 static 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)
527 u32 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
541 static 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 */
576 static 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];
582         u8 hash_512_output[SHA512_SUM_LEN];
583         sha1_context hash1_text;
584         sha256_context hash256_text;
585         sha512_context hash512_text;
586         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;
655                 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;
661                 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 }
679 #elif defined(CONFIG_ARMADA_32BIT)
680 static 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);
684
685         printf("Error: Invalid A38x image (header version 0x%x unknown)!\n",
686                h->version);
687         return 0;
688 }
689
690 static 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
704 static 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
718 static int check_image_header(void)
719 {
720         u8 checksum;
721         u32 checksum32, exp_checksum32;
722         u32 offset, size;
723         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) {
733                 printf("Error: Bad A38x image header checksum. 0x%x != 0x%x\n",
734                        checksum, hdr->checksum);
735                 return -ENOEXEC;
736         }
737
738         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
766         printf("Image checksum...OK!\n");
767         return 0;
768 }
769
770 #if defined(CONFIG_ARMADA_38X)
771 static 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
801 #else /* Not ARMADA? */
802 static 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
809 #if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_32BIT)
810 static 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)
828 static 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
835 static int bubt_check_boot_mode(const struct bubt_dev *dst)
836 {
837 #if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_32BIT)
838         int mode, secure_mode;
839 #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;
844         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);
852 #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;
857 #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
863 #endif
864
865         for (mode = 0; boot_modes[mode].name; mode++) {
866                 if (boot_modes[mode].id == id)
867                         break;
868         }
869
870         if (!boot_modes[mode].name) {
871                 printf("Error: unknown boot device in image header: 0x%x\n", id);
872                 return -ENOEXEC;
873         }
874
875         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         }
880
881 #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;
897 #endif
898                 } 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;
911 }
912
913 static int bubt_verify(const struct bubt_dev *dst)
914 {
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
924         err = bubt_check_boot_mode(dst);
925         if (err) {
926                 printf("Error: Image boot mode verification failed\n");
927                 return err;
928         }
929
930         return 0;
931 }
932
933 static 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
953 static int bubt_is_dev_active(struct bubt_dev *dev)
954 {
955         if (!dev->active) {
956                 printf("Device \"%s\" not supported by U-Boot image\n",
957                        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
969 struct 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"
990 #else
991 #define DEFAULT_BUBT_DST "error"
992 #endif
993 #endif /* DEFAULT_BUBT_DST */
994
995 int do_bubt_cmd(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
996 {
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);
1028                 return 1;
1029         }
1030
1031         if (!bubt_is_dev_active(dst))
1032                 return 1;
1033
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
1044         printf("Burning U-Boot image \"%s\" from \"%s\" to \"%s\"\n",
1045                net_boot_file_name, src->name, dst->name);
1046
1047         image_size = bubt_read_file(src);
1048         if (!image_size)
1049                 return 1;
1050
1051         err = bubt_verify(dst);
1052         if (err)
1053                 return 1;
1054
1055         err = bubt_write_file(dst, image_size);
1056         if (err)
1057                 return 1;
1058
1059         return 0;
1060 }
1061
1062 U_BOOT_CMD(
1063         bubt, 4, 0, do_bubt_cmd,
1064         "Burn a u-boot image to flash",
1065         "[file-name] [destination [source]]\n"
1066         "\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"
1069         "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 );