bubt.c 18 KB

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