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