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