cmd: mvebu/bubt: Verify image type for all 32-bit Aramda SoCs and Armada 3700
[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 <flash.h>
12 #include <image.h>
13 #include <net.h>
14 #include <vsprintf.h>
15 #include <errno.h>
16 #include <dm.h>
17
18 #include <spi_flash.h>
19 #include <spi.h>
20 #include <nand.h>
21 #include <usb.h>
22 #include <fs.h>
23 #include <mmc.h>
24 #ifdef CONFIG_BLK
25 #include <blk.h>
26 #endif
27 #include <u-boot/sha1.h>
28 #include <u-boot/sha256.h>
29
30 #if defined(CONFIG_ARMADA_8K)
31 #define MAIN_HDR_MAGIC          0xB105B002
32
33 struct mvebu_image_header {
34         u32     magic;                  /*  0-3  */
35         u32     prolog_size;            /*  4-7  */
36         u32     prolog_checksum;        /*  8-11 */
37         u32     boot_image_size;        /* 12-15 */
38         u32     boot_image_checksum;    /* 16-19 */
39         u32     rsrvd0;                 /* 20-23 */
40         u32     load_addr;              /* 24-27 */
41         u32     exec_addr;              /* 28-31 */
42         u8      uart_cfg;               /*  32   */
43         u8      baudrate;               /*  33   */
44         u8      ext_count;              /*  34   */
45         u8      aux_flags;              /*  35   */
46         u32     io_arg_0;               /* 36-39 */
47         u32     io_arg_1;               /* 40-43 */
48         u32     io_arg_2;               /* 43-47 */
49         u32     io_arg_3;               /* 48-51 */
50         u32     rsrvd1;                 /* 52-55 */
51         u32     rsrvd2;                 /* 56-59 */
52         u32     rsrvd3;                 /* 60-63 */
53 };
54 #elif defined(CONFIG_ARMADA_3700)       /* A3700 */
55 #define HASH_SUM_LEN            16
56 #define IMAGE_VERSION_3_6_0     0x030600
57 #define IMAGE_VERSION_3_5_0     0x030500
58
59 struct tim_boot_flash_sign {
60         unsigned int id;
61         const char *name;
62 };
63
64 struct tim_boot_flash_sign tim_boot_flash_signs[] = {
65         { 0x454d4d08, "mmc"  },
66         { 0x454d4d0b, "mmc"  },
67         { 0x5350490a, "spi"  },
68         { 0x5350491a, "nand" },
69         { 0x55415223, "uart" },
70         { 0x53415432, "sata" },
71         {},
72 };
73
74 struct common_tim_data {
75         u32     version;
76         u32     identifier;
77         u32     trusted;
78         u32     issue_date;
79         u32     oem_unique_id;
80         u32     reserved[5];            /* Reserve 20 bytes */
81         u32     boot_flash_sign;
82         u32     num_images;
83         u32     num_keys;
84         u32     size_of_reserved;
85 };
86
87 struct mvebu_image_info {
88         u32     image_id;
89         u32     next_image_id;
90         u32     flash_entry_addr;
91         u32     load_addr;
92         u32     image_size;
93         u32     image_size_to_hash;
94         u32     hash_algorithm_id;
95         u32     hash[HASH_SUM_LEN];     /* Reserve 512 bits for the hash */
96         u32     partition_number;
97         u32     enc_algorithm_id;
98         u32     encrypt_start_offset;
99         u32     encrypt_size;
100 };
101 #elif defined(CONFIG_ARMADA_32BIT)
102
103 /* Structure of the main header, version 1 (Armada 370/XP/375/38x/39x) */
104 struct a38x_main_hdr_v1 {
105         u8  blockid;               /* 0x0       */
106         u8  flags;                 /* 0x1       */
107         u16 nandpagesize;          /* 0x2-0x3   */
108         u32 blocksize;             /* 0x4-0x7   */
109         u8  version;               /* 0x8       */
110         u8  headersz_msb;          /* 0x9       */
111         u16 headersz_lsb;          /* 0xA-0xB   */
112         u32 srcaddr;               /* 0xC-0xF   */
113         u32 destaddr;              /* 0x10-0x13 */
114         u32 execaddr;              /* 0x14-0x17 */
115         u8  options;               /* 0x18      */
116         u8  nandblocksize;         /* 0x19      */
117         u8  nandbadblklocation;    /* 0x1A      */
118         u8  reserved4;             /* 0x1B      */
119         u16 reserved5;             /* 0x1C-0x1D */
120         u8  ext;                   /* 0x1E      */
121         u8  checksum;              /* 0x1F      */
122 };
123
124 struct a38x_boot_mode {
125         unsigned int id;
126         const char *name;
127 };
128
129 /* The blockid header field values used to indicate boot device of image */
130 struct a38x_boot_mode a38x_boot_modes[] = {
131         { 0x4D, "i2c"  },
132         { 0x5A, "spi"  },
133         { 0x69, "uart" },
134         { 0x78, "sata" },
135         { 0x8B, "nand" },
136         { 0x9C, "pex"  },
137         { 0xAE, "mmc"  },
138         {},
139 };
140
141 #endif
142
143 struct bubt_dev {
144         char name[8];
145         size_t (*read)(const char *file_name);
146         int (*write)(size_t image_size);
147         int (*active)(void);
148 };
149
150 static ulong get_load_addr(void)
151 {
152         const char *addr_str;
153         unsigned long addr;
154
155         addr_str = env_get("loadaddr");
156         if (addr_str)
157                 addr = hextoul(addr_str, NULL);
158         else
159                 addr = CONFIG_SYS_LOAD_ADDR;
160
161         return addr;
162 }
163
164 /********************************************************************
165  *     eMMC services
166  ********************************************************************/
167 #if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(MMC_WRITE)
168 static int mmc_burn_image(size_t image_size)
169 {
170         struct mmc      *mmc;
171         lbaint_t        start_lba;
172         lbaint_t        blk_count;
173         ulong           blk_written;
174         int             err;
175         const u8        mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
176 #ifdef CONFIG_BLK
177         struct blk_desc *blk_desc;
178 #endif
179         mmc = find_mmc_device(mmc_dev_num);
180         if (!mmc) {
181                 printf("No SD/MMC/eMMC card found\n");
182                 return -ENOMEDIUM;
183         }
184
185         err = mmc_init(mmc);
186         if (err) {
187                 printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
188                        mmc_dev_num);
189                 return err;
190         }
191
192         /* SD reserves LBA-0 for MBR and boots from LBA-1,
193          * MMC/eMMC boots from LBA-0
194          */
195         start_lba = IS_SD(mmc) ? 1 : 0;
196 #ifdef CONFIG_BLK
197         blk_count = image_size / mmc->write_bl_len;
198         if (image_size % mmc->write_bl_len)
199                 blk_count += 1;
200
201         blk_desc = mmc_get_blk_desc(mmc);
202         if (!blk_desc) {
203                 printf("Error - failed to obtain block descriptor\n");
204                 return -ENODEV;
205         }
206         blk_written = blk_dwrite(blk_desc, start_lba, blk_count,
207                                  (void *)get_load_addr());
208 #else
209         blk_count = image_size / mmc->block_dev.blksz;
210         if (image_size % mmc->block_dev.blksz)
211                 blk_count += 1;
212
213         blk_written = mmc->block_dev.block_write(mmc_dev_num,
214                                                  start_lba, blk_count,
215                                                  (void *)get_load_addr());
216 #endif /* CONFIG_BLK */
217         if (blk_written != blk_count) {
218                 printf("Error - written %#lx blocks\n", blk_written);
219                 return -ENOSPC;
220         }
221         printf("Done!\n");
222
223         return 0;
224 }
225
226 static size_t mmc_read_file(const char *file_name)
227 {
228         loff_t          act_read = 0;
229         int             rc;
230         struct mmc      *mmc;
231         const u8        mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
232
233         mmc = find_mmc_device(mmc_dev_num);
234         if (!mmc) {
235                 printf("No SD/MMC/eMMC card found\n");
236                 return 0;
237         }
238
239         if (mmc_init(mmc)) {
240                 printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
241                        mmc_dev_num);
242                 return 0;
243         }
244
245         /* Load from data partition (0) */
246         if (fs_set_blk_dev("mmc", "0", FS_TYPE_ANY)) {
247                 printf("Error: MMC 0 not found\n");
248                 return 0;
249         }
250
251         /* Perfrom file read */
252         rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
253         if (rc)
254                 return 0;
255
256         return act_read;
257 }
258
259 static int is_mmc_active(void)
260 {
261         return 1;
262 }
263 #else /* CONFIG_DM_MMC */
264 static int mmc_burn_image(size_t image_size)
265 {
266         return -ENODEV;
267 }
268
269 static size_t mmc_read_file(const char *file_name)
270 {
271         return 0;
272 }
273
274 static int is_mmc_active(void)
275 {
276         return 0;
277 }
278 #endif /* CONFIG_DM_MMC */
279
280 /********************************************************************
281  *     SPI services
282  ********************************************************************/
283 #ifdef CONFIG_SPI_FLASH
284 static int spi_burn_image(size_t image_size)
285 {
286         int ret;
287         struct spi_flash *flash;
288         u32 erase_bytes;
289
290         /* Probe the SPI bus to get the flash device */
291         flash = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
292                                 CONFIG_SF_DEFAULT_CS,
293                                 CONFIG_SF_DEFAULT_SPEED,
294                                 CONFIG_SF_DEFAULT_MODE);
295         if (!flash) {
296                 printf("Failed to probe SPI Flash\n");
297                 return -ENOMEDIUM;
298         }
299
300         erase_bytes = image_size +
301                 (flash->erase_size - image_size % flash->erase_size);
302         printf("Erasing %d bytes (%d blocks) at offset 0 ...",
303                erase_bytes, erase_bytes / flash->erase_size);
304         ret = spi_flash_erase(flash, 0, erase_bytes);
305         if (ret)
306                 printf("Error!\n");
307         else
308                 printf("Done!\n");
309
310         printf("Writing %d bytes from 0x%lx to offset 0 ...",
311                (int)image_size, get_load_addr());
312         ret = spi_flash_write(flash, 0, image_size, (void *)get_load_addr());
313         if (ret)
314                 printf("Error!\n");
315         else
316                 printf("Done!\n");
317
318         return ret;
319 }
320
321 static int is_spi_active(void)
322 {
323         return 1;
324 }
325
326 #else /* CONFIG_SPI_FLASH */
327 static int spi_burn_image(size_t image_size)
328 {
329         return -ENODEV;
330 }
331
332 static int is_spi_active(void)
333 {
334         return 0;
335 }
336 #endif /* CONFIG_SPI_FLASH */
337
338 /********************************************************************
339  *     NAND services
340  ********************************************************************/
341 #ifdef CONFIG_CMD_NAND
342 static int nand_burn_image(size_t image_size)
343 {
344         int ret;
345         uint32_t block_size;
346         struct mtd_info *mtd;
347
348         mtd = get_nand_dev_by_index(nand_curr_device);
349         if (!mtd) {
350                 puts("\nno devices available\n");
351                 return -ENOMEDIUM;
352         }
353         block_size = mtd->erasesize;
354
355         /* Align U-Boot size to currently used blocksize */
356         image_size = ((image_size + (block_size - 1)) & (~(block_size - 1)));
357
358         /* Erase the U-Boot image space */
359         printf("Erasing 0x%x - 0x%x:...", 0, (int)image_size);
360         ret = nand_erase(mtd, 0, image_size);
361         if (ret) {
362                 printf("Error!\n");
363                 goto error;
364         }
365         printf("Done!\n");
366
367         /* Write the image to flash */
368         printf("Writing %d bytes from 0x%lx to offset 0 ... ",
369                (int)image_size, get_load_addr());
370         ret = nand_write(mtd, 0, &image_size, (void *)get_load_addr());
371         if (ret)
372                 printf("Error!\n");
373         else
374                 printf("Done!\n");
375
376 error:
377         return ret;
378 }
379
380 static int is_nand_active(void)
381 {
382         return 1;
383 }
384
385 #else /* CONFIG_CMD_NAND */
386 static int nand_burn_image(size_t image_size)
387 {
388         return -ENODEV;
389 }
390
391 static int is_nand_active(void)
392 {
393         return 0;
394 }
395 #endif /* CONFIG_CMD_NAND */
396
397 /********************************************************************
398  *     USB services
399  ********************************************************************/
400 #if defined(CONFIG_USB_STORAGE) && defined(CONFIG_BLK)
401 static size_t usb_read_file(const char *file_name)
402 {
403         loff_t act_read = 0;
404         struct udevice *dev;
405         int rc;
406
407         usb_stop();
408
409         if (usb_init() < 0) {
410                 printf("Error: usb_init failed\n");
411                 return 0;
412         }
413
414         /* Try to recognize storage devices immediately */
415         blk_first_device(IF_TYPE_USB, &dev);
416         if (!dev) {
417                 printf("Error: USB storage device not found\n");
418                 return 0;
419         }
420
421         /* Always load from usb 0 */
422         if (fs_set_blk_dev("usb", "0", FS_TYPE_ANY)) {
423                 printf("Error: USB 0 not found\n");
424                 return 0;
425         }
426
427         /* Perfrom file read */
428         rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
429         if (rc)
430                 return 0;
431
432         return act_read;
433 }
434
435 static int is_usb_active(void)
436 {
437         return 1;
438 }
439
440 #else /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
441 static size_t usb_read_file(const char *file_name)
442 {
443         return 0;
444 }
445
446 static int is_usb_active(void)
447 {
448         return 0;
449 }
450 #endif /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
451
452 /********************************************************************
453  *     Network services
454  ********************************************************************/
455 #ifdef CONFIG_CMD_NET
456 static size_t tftp_read_file(const char *file_name)
457 {
458         /*
459          * update global variable image_load_addr before tftp file from network
460          */
461         image_load_addr = get_load_addr();
462         return net_loop(TFTPGET);
463 }
464
465 static int is_tftp_active(void)
466 {
467         return 1;
468 }
469
470 #else
471 static size_t tftp_read_file(const char *file_name)
472 {
473         return 0;
474 }
475
476 static int is_tftp_active(void)
477 {
478         return 0;
479 }
480 #endif /* CONFIG_CMD_NET */
481
482 enum bubt_devices {
483         BUBT_DEV_NET = 0,
484         BUBT_DEV_USB,
485         BUBT_DEV_MMC,
486         BUBT_DEV_SPI,
487         BUBT_DEV_NAND,
488
489         BUBT_MAX_DEV
490 };
491
492 struct bubt_dev bubt_devs[BUBT_MAX_DEV] = {
493         {"tftp", tftp_read_file, NULL, is_tftp_active},
494         {"usb",  usb_read_file,  NULL, is_usb_active},
495         {"mmc",  mmc_read_file,  mmc_burn_image, is_mmc_active},
496         {"spi",  NULL, spi_burn_image,  is_spi_active},
497         {"nand", NULL, nand_burn_image, is_nand_active},
498 };
499
500 static int bubt_write_file(struct bubt_dev *dst, size_t image_size)
501 {
502         if (!dst->write) {
503                 printf("Error: Write not supported on device %s\n", dst->name);
504                 return -ENOTSUPP;
505         }
506
507         return dst->write(image_size);
508 }
509
510 #if defined(CONFIG_ARMADA_8K)
511 u32 do_checksum32(u32 *start, int32_t len)
512 {
513         u32 sum = 0;
514         u32 *startp = start;
515
516         do {
517                 sum += *startp;
518                 startp++;
519                 len -= 4;
520         } while (len > 0);
521
522         return sum;
523 }
524
525 static int check_image_header(void)
526 {
527         struct mvebu_image_header *hdr =
528                         (struct mvebu_image_header *)get_load_addr();
529         u32 header_len = hdr->prolog_size;
530         u32 checksum;
531         u32 checksum_ref = hdr->prolog_checksum;
532
533         /*
534          * For now compare checksum, and magic. Later we can
535          * verify more stuff on the header like interface type, etc
536          */
537         if (hdr->magic != MAIN_HDR_MAGIC) {
538                 printf("ERROR: Bad MAGIC 0x%08x != 0x%08x\n",
539                        hdr->magic, MAIN_HDR_MAGIC);
540                 return -ENOEXEC;
541         }
542
543         /* The checksum value is discarded from checksum calculation */
544         hdr->prolog_checksum = 0;
545
546         checksum = do_checksum32((u32 *)hdr, header_len);
547         if (checksum != checksum_ref) {
548                 printf("Error: Bad Image checksum. 0x%x != 0x%x\n",
549                        checksum, checksum_ref);
550                 return -ENOEXEC;
551         }
552
553         /* Restore the checksum before writing */
554         hdr->prolog_checksum = checksum_ref;
555         printf("Image checksum...OK!\n");
556
557         return 0;
558 }
559 #elif defined(CONFIG_ARMADA_3700) /* Armada 3700 */
560 static int check_image_header(void)
561 {
562         struct common_tim_data *hdr = (struct common_tim_data *)get_load_addr();
563         int image_num;
564         u8 hash_160_output[SHA1_SUM_LEN];
565         u8 hash_256_output[SHA256_SUM_LEN];
566         sha1_context hash1_text;
567         sha256_context hash256_text;
568         u8 *hash_output;
569         u32 hash_algorithm_id;
570         u32 image_size_to_hash;
571         u32 flash_entry_addr;
572         u32 *hash_value;
573         u32 internal_hash[HASH_SUM_LEN];
574         const u8 *buff;
575         u32 num_of_image = hdr->num_images;
576         u32 version = hdr->version;
577         u32 trusted = hdr->trusted;
578
579         /* bubt checksum validation only supports nontrusted images */
580         if (trusted == 1) {
581                 printf("bypass image validation, ");
582                 printf("only untrusted image is supported now\n");
583                 return 0;
584         }
585         /* only supports image version 3.5 and 3.6 */
586         if (version != IMAGE_VERSION_3_5_0 && version != IMAGE_VERSION_3_6_0) {
587                 printf("Error: Unsupported Image version = 0x%08x\n", version);
588                 return -ENOEXEC;
589         }
590         /* validate images hash value */
591         for (image_num = 0; image_num < num_of_image; image_num++) {
592                 struct mvebu_image_info *info =
593                                 (struct mvebu_image_info *)(get_load_addr() +
594                                 sizeof(struct common_tim_data) +
595                                 image_num * sizeof(struct mvebu_image_info));
596                 hash_algorithm_id = info->hash_algorithm_id;
597                 image_size_to_hash = info->image_size_to_hash;
598                 flash_entry_addr = info->flash_entry_addr;
599                 hash_value = info->hash;
600                 buff = (const u8 *)(get_load_addr() + flash_entry_addr);
601
602                 if (image_num == 0) {
603                         /*
604                          * The first image includes hash values in its content.
605                          * For hash calculation, we need to save the original
606                          * hash values to a local variable that will be
607                          * copied back for comparsion and set all zeros to
608                          * the orignal hash values for calculating new value.
609                          * First image original format :
610                          * x...x (datum1) x...x(orig. hash values) x...x(datum2)
611                          * Replaced first image format :
612                          * x...x (datum1) 0...0(hash values) x...x(datum2)
613                          */
614                         memcpy(internal_hash, hash_value,
615                                sizeof(internal_hash));
616                         memset(hash_value, 0, sizeof(internal_hash));
617                 }
618                 if (image_size_to_hash == 0) {
619                         printf("Warning: Image_%d hash checksum is disabled, ",
620                                image_num);
621                         printf("skip the image validation.\n");
622                         continue;
623                 }
624                 switch (hash_algorithm_id) {
625                 case SHA1_SUM_LEN:
626                         sha1_starts(&hash1_text);
627                         sha1_update(&hash1_text, buff, image_size_to_hash);
628                         sha1_finish(&hash1_text, hash_160_output);
629                         hash_output = hash_160_output;
630                         break;
631                 case SHA256_SUM_LEN:
632                         sha256_starts(&hash256_text);
633                         sha256_update(&hash256_text, buff, image_size_to_hash);
634                         sha256_finish(&hash256_text, hash_256_output);
635                         hash_output = hash_256_output;
636                         break;
637                 default:
638                         printf("Error: Unsupported hash_algorithm_id = %d\n",
639                                hash_algorithm_id);
640                         return -ENOEXEC;
641                 }
642                 if (image_num == 0)
643                         memcpy(hash_value, internal_hash,
644                                sizeof(internal_hash));
645                 if (memcmp(hash_value, hash_output, hash_algorithm_id) != 0) {
646                         printf("Error: Image_%d checksum is not correct\n",
647                                image_num);
648                         return -ENOEXEC;
649                 }
650         }
651         printf("Image checksum...OK!\n");
652
653         return 0;
654 }
655 #elif defined(CONFIG_ARMADA_32BIT)
656 static size_t a38x_header_size(const struct a38x_main_hdr_v1 *h)
657 {
658         if (h->version == 1)
659                 return (h->headersz_msb << 16) | le16_to_cpu(h->headersz_lsb);
660
661         printf("Error: Invalid A38x image (header version 0x%x unknown)!\n",
662                h->version);
663         return 0;
664 }
665
666 static uint8_t image_checksum8(const void *start, size_t len)
667 {
668         u8 csum = 0;
669         const u8 *p = start;
670
671         while (len) {
672                 csum += *p;
673                 ++p;
674                 --len;
675         }
676
677         return csum;
678 }
679
680 static int check_image_header(void)
681 {
682         u8 checksum;
683         const struct a38x_main_hdr_v1 *hdr =
684                 (struct a38x_main_hdr_v1 *)get_load_addr();
685         const size_t image_size = a38x_header_size(hdr);
686
687         if (!image_size)
688                 return -ENOEXEC;
689
690         checksum = image_checksum8(hdr, image_size);
691         checksum -= hdr->checksum;
692         if (checksum != hdr->checksum) {
693                 printf("Error: Bad A38x image checksum. 0x%x != 0x%x\n",
694                        checksum, hdr->checksum);
695                 return -ENOEXEC;
696         }
697
698         printf("Image checksum...OK!\n");
699         return 0;
700 }
701 #else /* Not ARMADA? */
702 static int check_image_header(void)
703 {
704         printf("bubt cmd does not support this SoC device or family!\n");
705         return -ENOEXEC;
706 }
707 #endif
708
709 static int bubt_check_boot_mode(const struct bubt_dev *dst)
710 {
711 #if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_32BIT)
712         int mode;
713 #if defined(CONFIG_ARMADA_3700)
714         const struct tim_boot_flash_sign *boot_modes = tim_boot_flash_signs;
715         const struct common_tim_data *hdr =
716                 (struct common_tim_data *)get_load_addr();
717         u32 id = hdr->boot_flash_sign;
718 #elif defined(CONFIG_ARMADA_32BIT)
719         const struct a38x_boot_mode *boot_modes = a38x_boot_modes;
720         const struct a38x_main_hdr_v1 *hdr =
721                 (struct a38x_main_hdr_v1 *)get_load_addr();
722         u32 id = hdr->blockid;
723 #endif
724
725         for (mode = 0; boot_modes[mode].name; mode++) {
726                 if (boot_modes[mode].id == id)
727                         break;
728         }
729
730         if (!boot_modes[mode].name) {
731                 printf("Error: unknown boot device in image header: 0x%x\n", id);
732                 return -ENOEXEC;
733         }
734
735         if (strcmp(boot_modes[mode].name, dst->name) == 0)
736                 return 0;
737
738         printf("Error: image meant to be booted from \"%s\", not \"%s\"!\n",
739                boot_modes[mode].name, dst->name);
740         return -ENOEXEC;
741 #else
742         return 0;
743 #endif
744 }
745
746 static int bubt_verify(const struct bubt_dev *dst)
747 {
748         int err;
749
750         /* Check a correct image header exists */
751         err = check_image_header();
752         if (err) {
753                 printf("Error: Image header verification failed\n");
754                 return err;
755         }
756
757         err = bubt_check_boot_mode(dst);
758         if (err) {
759                 printf("Error: Image boot mode verification failed\n");
760                 return err;
761         }
762
763         return 0;
764 }
765
766 static int bubt_read_file(struct bubt_dev *src)
767 {
768         size_t image_size;
769
770         if (!src->read) {
771                 printf("Error: Read not supported on device \"%s\"\n",
772                        src->name);
773                 return 0;
774         }
775
776         image_size = src->read(net_boot_file_name);
777         if (image_size <= 0) {
778                 printf("Error: Failed to read file %s from %s\n",
779                        net_boot_file_name, src->name);
780                 return 0;
781         }
782
783         return image_size;
784 }
785
786 static int bubt_is_dev_active(struct bubt_dev *dev)
787 {
788         if (!dev->active) {
789                 printf("Device \"%s\" not supported by U-Boot image\n",
790                        dev->name);
791                 return 0;
792         }
793
794         if (!dev->active()) {
795                 printf("Device \"%s\" is inactive\n", dev->name);
796                 return 0;
797         }
798
799         return 1;
800 }
801
802 struct bubt_dev *find_bubt_dev(char *dev_name)
803 {
804         int dev;
805
806         for (dev = 0; dev < BUBT_MAX_DEV; dev++) {
807                 if (strcmp(bubt_devs[dev].name, dev_name) == 0)
808                         return &bubt_devs[dev];
809         }
810
811         return 0;
812 }
813
814 #define DEFAULT_BUBT_SRC "tftp"
815
816 #ifndef DEFAULT_BUBT_DST
817 #ifdef CONFIG_MVEBU_SPI_BOOT
818 #define DEFAULT_BUBT_DST "spi"
819 #elif defined(CONFIG_MVEBU_NAND_BOOT)
820 #define DEFAULT_BUBT_DST "nand"
821 #elif defined(CONFIG_MVEBU_MMC_BOOT)
822 #define DEFAULT_BUBT_DST "mmc"
823 #else
824 #define DEFAULT_BUBT_DST "error"
825 #endif
826 #endif /* DEFAULT_BUBT_DST */
827
828 int do_bubt_cmd(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
829 {
830         struct bubt_dev *src, *dst;
831         size_t image_size;
832         char src_dev_name[8];
833         char dst_dev_name[8];
834         char *name;
835         int  err;
836
837         if (argc < 2)
838                 copy_filename(net_boot_file_name,
839                               CONFIG_MVEBU_UBOOT_DFLT_NAME,
840                               sizeof(net_boot_file_name));
841         else
842                 copy_filename(net_boot_file_name, argv[1],
843                               sizeof(net_boot_file_name));
844
845         if (argc >= 3) {
846                 strncpy(dst_dev_name, argv[2], 8);
847         } else {
848                 name = DEFAULT_BUBT_DST;
849                 strncpy(dst_dev_name, name, 8);
850         }
851
852         if (argc >= 4)
853                 strncpy(src_dev_name, argv[3], 8);
854         else
855                 strncpy(src_dev_name, DEFAULT_BUBT_SRC, 8);
856
857         /* Figure out the destination device */
858         dst = find_bubt_dev(dst_dev_name);
859         if (!dst) {
860                 printf("Error: Unknown destination \"%s\"\n", dst_dev_name);
861                 return -EINVAL;
862         }
863
864         if (!bubt_is_dev_active(dst))
865                 return -ENODEV;
866
867         /* Figure out the source device */
868         src = find_bubt_dev(src_dev_name);
869         if (!src) {
870                 printf("Error: Unknown source \"%s\"\n", src_dev_name);
871                 return 1;
872         }
873
874         if (!bubt_is_dev_active(src))
875                 return -ENODEV;
876
877         printf("Burning U-Boot image \"%s\" from \"%s\" to \"%s\"\n",
878                net_boot_file_name, src->name, dst->name);
879
880         image_size = bubt_read_file(src);
881         if (!image_size)
882                 return -EIO;
883
884         err = bubt_verify(dst);
885         if (err)
886                 return err;
887
888         err = bubt_write_file(dst, image_size);
889         if (err)
890                 return err;
891
892         return 0;
893 }
894
895 U_BOOT_CMD(
896         bubt, 4, 0, do_bubt_cmd,
897         "Burn a u-boot image to flash",
898         "[file-name] [destination [source]]\n"
899         "\t-file-name     The image file name to burn. Default = " CONFIG_MVEBU_UBOOT_DFLT_NAME "\n"
900         "\t-destination   Flash to burn to [spi, nand, mmc]. Default = " DEFAULT_BUBT_DST "\n"
901         "\t-source        The source to load image from [tftp, usb, mmc]. Default = " DEFAULT_BUBT_SRC "\n"
902         "Examples:\n"
903         "\tbubt - Burn flash-image.bin from tftp to active boot device\n"
904         "\tbubt flash-image-new.bin nand - Burn flash-image-new.bin from tftp to NAND flash\n"
905         "\tbubt backup-flash-image.bin mmc usb - Burn backup-flash-image.bin from usb to MMC\n"
906
907 );