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