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