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