bubt.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2016 Marvell International Ltd.
  4. * https://spdx.org/licenses
  5. */
  6. #include <config.h>
  7. #include <common.h>
  8. #include <command.h>
  9. #include <vsprintf.h>
  10. #include <errno.h>
  11. #include <dm.h>
  12. #include <spi_flash.h>
  13. #include <spi.h>
  14. #include <nand.h>
  15. #include <usb.h>
  16. #include <fs.h>
  17. #include <mmc.h>
  18. #ifdef CONFIG_BLK
  19. #include <blk.h>
  20. #endif
  21. #include <u-boot/sha1.h>
  22. #include <u-boot/sha256.h>
  23. #ifndef CONFIG_SYS_MMC_ENV_DEV
  24. #define CONFIG_SYS_MMC_ENV_DEV 0
  25. #endif
  26. #if defined(CONFIG_ARMADA_8K)
  27. #define MAIN_HDR_MAGIC 0xB105B002
  28. struct mvebu_image_header {
  29. u32 magic; /* 0-3 */
  30. u32 prolog_size; /* 4-7 */
  31. u32 prolog_checksum; /* 8-11 */
  32. u32 boot_image_size; /* 12-15 */
  33. u32 boot_image_checksum; /* 16-19 */
  34. u32 rsrvd0; /* 20-23 */
  35. u32 load_addr; /* 24-27 */
  36. u32 exec_addr; /* 28-31 */
  37. u8 uart_cfg; /* 32 */
  38. u8 baudrate; /* 33 */
  39. u8 ext_count; /* 34 */
  40. u8 aux_flags; /* 35 */
  41. u32 io_arg_0; /* 36-39 */
  42. u32 io_arg_1; /* 40-43 */
  43. u32 io_arg_2; /* 43-47 */
  44. u32 io_arg_3; /* 48-51 */
  45. u32 rsrvd1; /* 52-55 */
  46. u32 rsrvd2; /* 56-59 */
  47. u32 rsrvd3; /* 60-63 */
  48. };
  49. #elif defined(CONFIG_ARMADA_3700) /* A3700 */
  50. #define HASH_SUM_LEN 16
  51. #define IMAGE_VERSION_3_6_0 0x030600
  52. #define IMAGE_VERSION_3_5_0 0x030500
  53. struct common_tim_data {
  54. u32 version;
  55. u32 identifier;
  56. u32 trusted;
  57. u32 issue_date;
  58. u32 oem_unique_id;
  59. u32 reserved[5]; /* Reserve 20 bytes */
  60. u32 boot_flash_sign;
  61. u32 num_images;
  62. u32 num_keys;
  63. u32 size_of_reserved;
  64. };
  65. struct mvebu_image_info {
  66. u32 image_id;
  67. u32 next_image_id;
  68. u32 flash_entry_addr;
  69. u32 load_addr;
  70. u32 image_size;
  71. u32 image_size_to_hash;
  72. u32 hash_algorithm_id;
  73. u32 hash[HASH_SUM_LEN]; /* Reserve 512 bits for the hash */
  74. u32 partition_number;
  75. u32 enc_algorithm_id;
  76. u32 encrypt_start_offset;
  77. u32 encrypt_size;
  78. };
  79. #endif /* CONFIG_ARMADA_XXX */
  80. struct bubt_dev {
  81. char name[8];
  82. size_t (*read)(const char *file_name);
  83. int (*write)(size_t image_size);
  84. int (*active)(void);
  85. };
  86. static ulong get_load_addr(void)
  87. {
  88. const char *addr_str;
  89. unsigned long addr;
  90. addr_str = env_get("loadaddr");
  91. if (addr_str)
  92. addr = simple_strtoul(addr_str, NULL, 16);
  93. else
  94. addr = CONFIG_SYS_LOAD_ADDR;
  95. return addr;
  96. }
  97. /********************************************************************
  98. * eMMC services
  99. ********************************************************************/
  100. #if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(MMC_WRITE)
  101. static int mmc_burn_image(size_t image_size)
  102. {
  103. struct mmc *mmc;
  104. lbaint_t start_lba;
  105. lbaint_t blk_count;
  106. ulong blk_written;
  107. int err;
  108. const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
  109. #ifdef CONFIG_BLK
  110. struct blk_desc *blk_desc;
  111. #endif
  112. mmc = find_mmc_device(mmc_dev_num);
  113. if (!mmc) {
  114. printf("No SD/MMC/eMMC card found\n");
  115. return -ENOMEDIUM;
  116. }
  117. err = mmc_init(mmc);
  118. if (err) {
  119. printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
  120. mmc_dev_num);
  121. return err;
  122. }
  123. #ifdef CONFIG_SYS_MMC_ENV_PART
  124. if (mmc->part_num != CONFIG_SYS_MMC_ENV_PART) {
  125. err = mmc_switch_part(mmc_dev_num, CONFIG_SYS_MMC_ENV_PART);
  126. if (err) {
  127. printf("MMC partition switch failed\n");
  128. return err;
  129. }
  130. }
  131. #endif
  132. /* SD reserves LBA-0 for MBR and boots from LBA-1,
  133. * MMC/eMMC boots from LBA-0
  134. */
  135. start_lba = IS_SD(mmc) ? 1 : 0;
  136. #ifdef CONFIG_BLK
  137. blk_count = image_size / mmc->write_bl_len;
  138. if (image_size % mmc->write_bl_len)
  139. blk_count += 1;
  140. blk_desc = mmc_get_blk_desc(mmc);
  141. if (!blk_desc) {
  142. printf("Error - failed to obtain block descriptor\n");
  143. return -ENODEV;
  144. }
  145. blk_written = blk_dwrite(blk_desc, start_lba, blk_count,
  146. (void *)get_load_addr());
  147. #else
  148. blk_count = image_size / mmc->block_dev.blksz;
  149. if (image_size % mmc->block_dev.blksz)
  150. blk_count += 1;
  151. blk_written = mmc->block_dev.block_write(mmc_dev_num,
  152. start_lba, blk_count,
  153. (void *)get_load_addr());
  154. #endif /* CONFIG_BLK */
  155. if (blk_written != blk_count) {
  156. printf("Error - written %#lx blocks\n", blk_written);
  157. return -ENOSPC;
  158. }
  159. printf("Done!\n");
  160. #ifdef CONFIG_SYS_MMC_ENV_PART
  161. if (mmc->part_num != CONFIG_SYS_MMC_ENV_PART)
  162. mmc_switch_part(mmc_dev_num, mmc->part_num);
  163. #endif
  164. return 0;
  165. }
  166. static size_t mmc_read_file(const char *file_name)
  167. {
  168. loff_t act_read = 0;
  169. int rc;
  170. struct mmc *mmc;
  171. const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
  172. mmc = find_mmc_device(mmc_dev_num);
  173. if (!mmc) {
  174. printf("No SD/MMC/eMMC card found\n");
  175. return 0;
  176. }
  177. if (mmc_init(mmc)) {
  178. printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
  179. mmc_dev_num);
  180. return 0;
  181. }
  182. /* Load from data partition (0) */
  183. if (fs_set_blk_dev("mmc", "0", FS_TYPE_ANY)) {
  184. printf("Error: MMC 0 not found\n");
  185. return 0;
  186. }
  187. /* Perfrom file read */
  188. rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
  189. if (rc)
  190. return 0;
  191. return act_read;
  192. }
  193. static int is_mmc_active(void)
  194. {
  195. return 1;
  196. }
  197. #else /* CONFIG_DM_MMC */
  198. static int mmc_burn_image(size_t image_size)
  199. {
  200. return -ENODEV;
  201. }
  202. static size_t mmc_read_file(const char *file_name)
  203. {
  204. return 0;
  205. }
  206. static int is_mmc_active(void)
  207. {
  208. return 0;
  209. }
  210. #endif /* CONFIG_DM_MMC */
  211. /********************************************************************
  212. * SPI services
  213. ********************************************************************/
  214. #ifdef CONFIG_SPI_FLASH
  215. static int spi_burn_image(size_t image_size)
  216. {
  217. int ret;
  218. struct spi_flash *flash;
  219. u32 erase_bytes;
  220. /* Probe the SPI bus to get the flash device */
  221. flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
  222. CONFIG_ENV_SPI_CS,
  223. CONFIG_SF_DEFAULT_SPEED,
  224. CONFIG_SF_DEFAULT_MODE);
  225. if (!flash) {
  226. printf("Failed to probe SPI Flash\n");
  227. return -ENOMEDIUM;
  228. }
  229. #ifdef CONFIG_SPI_FLASH_PROTECTION
  230. spi_flash_protect(flash, 0);
  231. #endif
  232. erase_bytes = image_size +
  233. (flash->erase_size - image_size % flash->erase_size);
  234. printf("Erasing %d bytes (%d blocks) at offset 0 ...",
  235. erase_bytes, erase_bytes / flash->erase_size);
  236. ret = spi_flash_erase(flash, 0, erase_bytes);
  237. if (ret)
  238. printf("Error!\n");
  239. else
  240. printf("Done!\n");
  241. printf("Writing %d bytes from 0x%lx to offset 0 ...",
  242. (int)image_size, get_load_addr());
  243. ret = spi_flash_write(flash, 0, image_size, (void *)get_load_addr());
  244. if (ret)
  245. printf("Error!\n");
  246. else
  247. printf("Done!\n");
  248. #ifdef CONFIG_SPI_FLASH_PROTECTION
  249. spi_flash_protect(flash, 1);
  250. #endif
  251. return ret;
  252. }
  253. static int is_spi_active(void)
  254. {
  255. return 1;
  256. }
  257. #else /* CONFIG_SPI_FLASH */
  258. static int spi_burn_image(size_t image_size)
  259. {
  260. return -ENODEV;
  261. }
  262. static int is_spi_active(void)
  263. {
  264. return 0;
  265. }
  266. #endif /* CONFIG_SPI_FLASH */
  267. /********************************************************************
  268. * NAND services
  269. ********************************************************************/
  270. #ifdef CONFIG_CMD_NAND
  271. static int nand_burn_image(size_t image_size)
  272. {
  273. int ret;
  274. uint32_t block_size;
  275. struct mtd_info *mtd;
  276. mtd = get_nand_dev_by_index(nand_curr_device);
  277. if (!mtd) {
  278. puts("\nno devices available\n");
  279. return -ENOMEDIUM;
  280. }
  281. block_size = mtd->erasesize;
  282. /* Align U-Boot size to currently used blocksize */
  283. image_size = ((image_size + (block_size - 1)) & (~(block_size - 1)));
  284. /* Erase the U-BOOT image space */
  285. printf("Erasing 0x%x - 0x%x:...", 0, (int)image_size);
  286. ret = nand_erase(mtd, 0, image_size);
  287. if (ret) {
  288. printf("Error!\n");
  289. goto error;
  290. }
  291. printf("Done!\n");
  292. /* Write the image to flash */
  293. printf("Writing %d bytes from 0x%lx to offset 0 ... ",
  294. (int)image_size, get_load_addr());
  295. ret = nand_write(mtd, 0, &image_size, (void *)get_load_addr());
  296. if (ret)
  297. printf("Error!\n");
  298. else
  299. printf("Done!\n");
  300. error:
  301. return ret;
  302. }
  303. static int is_nand_active(void)
  304. {
  305. return 1;
  306. }
  307. #else /* CONFIG_CMD_NAND */
  308. static int nand_burn_image(size_t image_size)
  309. {
  310. return -ENODEV;
  311. }
  312. static int is_nand_active(void)
  313. {
  314. return 0;
  315. }
  316. #endif /* CONFIG_CMD_NAND */
  317. /********************************************************************
  318. * USB services
  319. ********************************************************************/
  320. #if defined(CONFIG_USB_STORAGE) && defined(CONFIG_BLK)
  321. static size_t usb_read_file(const char *file_name)
  322. {
  323. loff_t act_read = 0;
  324. struct udevice *dev;
  325. int rc;
  326. usb_stop();
  327. if (usb_init() < 0) {
  328. printf("Error: usb_init failed\n");
  329. return 0;
  330. }
  331. /* Try to recognize storage devices immediately */
  332. blk_first_device(IF_TYPE_USB, &dev);
  333. if (!dev) {
  334. printf("Error: USB storage device not found\n");
  335. return 0;
  336. }
  337. /* Always load from usb 0 */
  338. if (fs_set_blk_dev("usb", "0", FS_TYPE_ANY)) {
  339. printf("Error: USB 0 not found\n");
  340. return 0;
  341. }
  342. /* Perfrom file read */
  343. rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
  344. if (rc)
  345. return 0;
  346. return act_read;
  347. }
  348. static int is_usb_active(void)
  349. {
  350. return 1;
  351. }
  352. #else /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
  353. static size_t usb_read_file(const char *file_name)
  354. {
  355. return 0;
  356. }
  357. static int is_usb_active(void)
  358. {
  359. return 0;
  360. }
  361. #endif /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
  362. /********************************************************************
  363. * Network services
  364. ********************************************************************/
  365. #ifdef CONFIG_CMD_NET
  366. static size_t tftp_read_file(const char *file_name)
  367. {
  368. /* update global variable load_addr before tftp file from network */
  369. load_addr = get_load_addr();
  370. return net_loop(TFTPGET);
  371. }
  372. static int is_tftp_active(void)
  373. {
  374. return 1;
  375. }
  376. #else
  377. static size_t tftp_read_file(const char *file_name)
  378. {
  379. return 0;
  380. }
  381. static int is_tftp_active(void)
  382. {
  383. return 0;
  384. }
  385. #endif /* CONFIG_CMD_NET */
  386. enum bubt_devices {
  387. BUBT_DEV_NET = 0,
  388. BUBT_DEV_USB,
  389. BUBT_DEV_MMC,
  390. BUBT_DEV_SPI,
  391. BUBT_DEV_NAND,
  392. BUBT_MAX_DEV
  393. };
  394. struct bubt_dev bubt_devs[BUBT_MAX_DEV] = {
  395. {"tftp", tftp_read_file, NULL, is_tftp_active},
  396. {"usb", usb_read_file, NULL, is_usb_active},
  397. {"mmc", mmc_read_file, mmc_burn_image, is_mmc_active},
  398. {"spi", NULL, spi_burn_image, is_spi_active},
  399. {"nand", NULL, nand_burn_image, is_nand_active},
  400. };
  401. static int bubt_write_file(struct bubt_dev *dst, size_t image_size)
  402. {
  403. if (!dst->write) {
  404. printf("Error: Write not supported on device %s\n", dst->name);
  405. return -ENOTSUPP;
  406. }
  407. return dst->write(image_size);
  408. }
  409. #if defined(CONFIG_ARMADA_8K)
  410. u32 do_checksum32(u32 *start, int32_t len)
  411. {
  412. u32 sum = 0;
  413. u32 *startp = start;
  414. do {
  415. sum += *startp;
  416. startp++;
  417. len -= 4;
  418. } while (len > 0);
  419. return sum;
  420. }
  421. static int check_image_header(void)
  422. {
  423. struct mvebu_image_header *hdr =
  424. (struct mvebu_image_header *)get_load_addr();
  425. u32 header_len = hdr->prolog_size;
  426. u32 checksum;
  427. u32 checksum_ref = hdr->prolog_checksum;
  428. /*
  429. * For now compare checksum, and magic. Later we can
  430. * verify more stuff on the header like interface type, etc
  431. */
  432. if (hdr->magic != MAIN_HDR_MAGIC) {
  433. printf("ERROR: Bad MAGIC 0x%08x != 0x%08x\n",
  434. hdr->magic, MAIN_HDR_MAGIC);
  435. return -ENOEXEC;
  436. }
  437. /* The checksum value is discarded from checksum calculation */
  438. hdr->prolog_checksum = 0;
  439. checksum = do_checksum32((u32 *)hdr, header_len);
  440. if (checksum != checksum_ref) {
  441. printf("Error: Bad Image checksum. 0x%x != 0x%x\n",
  442. checksum, checksum_ref);
  443. return -ENOEXEC;
  444. }
  445. /* Restore the checksum before writing */
  446. hdr->prolog_checksum = checksum_ref;
  447. printf("Image checksum...OK!\n");
  448. return 0;
  449. }
  450. #elif defined(CONFIG_ARMADA_3700) /* Armada 3700 */
  451. static int check_image_header(void)
  452. {
  453. struct common_tim_data *hdr = (struct common_tim_data *)get_load_addr();
  454. int image_num;
  455. u8 hash_160_output[SHA1_SUM_LEN];
  456. u8 hash_256_output[SHA256_SUM_LEN];
  457. sha1_context hash1_text;
  458. sha256_context hash256_text;
  459. u8 *hash_output;
  460. u32 hash_algorithm_id;
  461. u32 image_size_to_hash;
  462. u32 flash_entry_addr;
  463. u32 *hash_value;
  464. u32 internal_hash[HASH_SUM_LEN];
  465. const u8 *buff;
  466. u32 num_of_image = hdr->num_images;
  467. u32 version = hdr->version;
  468. u32 trusted = hdr->trusted;
  469. /* bubt checksum validation only supports nontrusted images */
  470. if (trusted == 1) {
  471. printf("bypass image validation, ");
  472. printf("only untrusted image is supported now\n");
  473. return 0;
  474. }
  475. /* only supports image version 3.5 and 3.6 */
  476. if (version != IMAGE_VERSION_3_5_0 && version != IMAGE_VERSION_3_6_0) {
  477. printf("Error: Unsupported Image version = 0x%08x\n", version);
  478. return -ENOEXEC;
  479. }
  480. /* validate images hash value */
  481. for (image_num = 0; image_num < num_of_image; image_num++) {
  482. struct mvebu_image_info *info =
  483. (struct mvebu_image_info *)(get_load_addr() +
  484. sizeof(struct common_tim_data) +
  485. image_num * sizeof(struct mvebu_image_info));
  486. hash_algorithm_id = info->hash_algorithm_id;
  487. image_size_to_hash = info->image_size_to_hash;
  488. flash_entry_addr = info->flash_entry_addr;
  489. hash_value = info->hash;
  490. buff = (const u8 *)(get_load_addr() + flash_entry_addr);
  491. if (image_num == 0) {
  492. /*
  493. * The first image includes hash values in its content.
  494. * For hash calculation, we need to save the original
  495. * hash values to a local variable that will be
  496. * copied back for comparsion and set all zeros to
  497. * the orignal hash values for calculating new value.
  498. * First image original format :
  499. * x...x (datum1) x...x(orig. hash values) x...x(datum2)
  500. * Replaced first image format :
  501. * x...x (datum1) 0...0(hash values) x...x(datum2)
  502. */
  503. memcpy(internal_hash, hash_value,
  504. sizeof(internal_hash));
  505. memset(hash_value, 0, sizeof(internal_hash));
  506. }
  507. if (image_size_to_hash == 0) {
  508. printf("Warning: Image_%d hash checksum is disabled, ",
  509. image_num);
  510. printf("skip the image validation.\n");
  511. continue;
  512. }
  513. switch (hash_algorithm_id) {
  514. case SHA1_SUM_LEN:
  515. sha1_starts(&hash1_text);
  516. sha1_update(&hash1_text, buff, image_size_to_hash);
  517. sha1_finish(&hash1_text, hash_160_output);
  518. hash_output = hash_160_output;
  519. break;
  520. case SHA256_SUM_LEN:
  521. sha256_starts(&hash256_text);
  522. sha256_update(&hash256_text, buff, image_size_to_hash);
  523. sha256_finish(&hash256_text, hash_256_output);
  524. hash_output = hash_256_output;
  525. break;
  526. default:
  527. printf("Error: Unsupported hash_algorithm_id = %d\n",
  528. hash_algorithm_id);
  529. return -ENOEXEC;
  530. }
  531. if (image_num == 0)
  532. memcpy(hash_value, internal_hash,
  533. sizeof(internal_hash));
  534. if (memcmp(hash_value, hash_output, hash_algorithm_id) != 0) {
  535. printf("Error: Image_%d checksum is not correct\n",
  536. image_num);
  537. return -ENOEXEC;
  538. }
  539. }
  540. printf("Image checksum...OK!\n");
  541. return 0;
  542. }
  543. #else /* Not ARMADA? */
  544. static int check_image_header(void)
  545. {
  546. printf("bubt cmd does not support this SoC device or family!\n");
  547. return -ENOEXEC;
  548. }
  549. #endif
  550. static int bubt_verify(size_t image_size)
  551. {
  552. int err;
  553. /* Check a correct image header exists */
  554. err = check_image_header();
  555. if (err) {
  556. printf("Error: Image header verification failed\n");
  557. return err;
  558. }
  559. return 0;
  560. }
  561. static int bubt_read_file(struct bubt_dev *src)
  562. {
  563. size_t image_size;
  564. if (!src->read) {
  565. printf("Error: Read not supported on device \"%s\"\n",
  566. src->name);
  567. return 0;
  568. }
  569. image_size = src->read(net_boot_file_name);
  570. if (image_size <= 0) {
  571. printf("Error: Failed to read file %s from %s\n",
  572. net_boot_file_name, src->name);
  573. return 0;
  574. }
  575. return image_size;
  576. }
  577. static int bubt_is_dev_active(struct bubt_dev *dev)
  578. {
  579. if (!dev->active) {
  580. printf("Device \"%s\" not supported by U-BOOT image\n",
  581. dev->name);
  582. return 0;
  583. }
  584. if (!dev->active()) {
  585. printf("Device \"%s\" is inactive\n", dev->name);
  586. return 0;
  587. }
  588. return 1;
  589. }
  590. struct bubt_dev *find_bubt_dev(char *dev_name)
  591. {
  592. int dev;
  593. for (dev = 0; dev < BUBT_MAX_DEV; dev++) {
  594. if (strcmp(bubt_devs[dev].name, dev_name) == 0)
  595. return &bubt_devs[dev];
  596. }
  597. return 0;
  598. }
  599. #define DEFAULT_BUBT_SRC "tftp"
  600. #ifndef DEFAULT_BUBT_DST
  601. #ifdef CONFIG_MVEBU_SPI_BOOT
  602. #define DEFAULT_BUBT_DST "spi"
  603. #elif defined(CONFIG_MVEBU_NAND_BOOT)
  604. #define DEFAULT_BUBT_DST "nand"
  605. #elif defined(CONFIG_MVEBU_MMC_BOOT)
  606. #define DEFAULT_BUBT_DST "mmc"
  607. else
  608. #define DEFAULT_BUBT_DST "error"
  609. #endif
  610. #endif /* DEFAULT_BUBT_DST */
  611. int do_bubt_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  612. {
  613. struct bubt_dev *src, *dst;
  614. size_t image_size;
  615. char src_dev_name[8];
  616. char dst_dev_name[8];
  617. char *name;
  618. int err;
  619. if (argc < 2)
  620. copy_filename(net_boot_file_name,
  621. CONFIG_MVEBU_UBOOT_DFLT_NAME,
  622. sizeof(net_boot_file_name));
  623. else
  624. copy_filename(net_boot_file_name, argv[1],
  625. sizeof(net_boot_file_name));
  626. if (argc >= 3) {
  627. strncpy(dst_dev_name, argv[2], 8);
  628. } else {
  629. name = DEFAULT_BUBT_DST;
  630. strncpy(dst_dev_name, name, 8);
  631. }
  632. if (argc >= 4)
  633. strncpy(src_dev_name, argv[3], 8);
  634. else
  635. strncpy(src_dev_name, DEFAULT_BUBT_SRC, 8);
  636. /* Figure out the destination device */
  637. dst = find_bubt_dev(dst_dev_name);
  638. if (!dst) {
  639. printf("Error: Unknown destination \"%s\"\n", dst_dev_name);
  640. return -EINVAL;
  641. }
  642. if (!bubt_is_dev_active(dst))
  643. return -ENODEV;
  644. /* Figure out the source device */
  645. src = find_bubt_dev(src_dev_name);
  646. if (!src) {
  647. printf("Error: Unknown source \"%s\"\n", src_dev_name);
  648. return 1;
  649. }
  650. if (!bubt_is_dev_active(src))
  651. return -ENODEV;
  652. printf("Burning U-BOOT image \"%s\" from \"%s\" to \"%s\"\n",
  653. net_boot_file_name, src->name, dst->name);
  654. image_size = bubt_read_file(src);
  655. if (!image_size)
  656. return -EIO;
  657. err = bubt_verify(image_size);
  658. if (err)
  659. return err;
  660. err = bubt_write_file(dst, image_size);
  661. if (err)
  662. return err;
  663. return 0;
  664. }
  665. U_BOOT_CMD(
  666. bubt, 4, 0, do_bubt_cmd,
  667. "Burn a u-boot image to flash",
  668. "[file-name] [destination [source]]\n"
  669. "\t-file-name The image file name to burn. Default = flash-image.bin\n"
  670. "\t-destination Flash to burn to [spi, nand, mmc]. Default = active boot device\n"
  671. "\t-source The source to load image from [tftp, usb, mmc]. Default = tftp\n"
  672. "Examples:\n"
  673. "\tbubt - Burn flash-image.bin from tftp to active boot device\n"
  674. "\tbubt flash-image-new.bin nand - Burn flash-image-new.bin from tftp to NAND flash\n"
  675. "\tbubt backup-flash-image.bin mmc usb - Burn backup-flash-image.bin from usb to MMC\n"
  676. );