bubt.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. /*
  2. * Copyright (C) 2016 Marvell International Ltd.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0
  5. * https://spdx.org/licenses
  6. */
  7. #include <config.h>
  8. #include <common.h>
  9. #include <command.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 = getenv("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. #ifdef CONFIG_DM_MMC
  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 *nand;
  277. int dev = nand_curr_device;
  278. if ((dev < 0) || (dev >= CONFIG_SYS_MAX_NAND_DEVICE) ||
  279. (!nand_info[dev]->name)) {
  280. puts("\nno devices available\n");
  281. return -ENOMEDIUM;
  282. }
  283. nand = nand_info[dev];
  284. block_size = nand->erasesize;
  285. /* Align U-Boot size to currently used blocksize */
  286. image_size = ((image_size + (block_size - 1)) & (~(block_size - 1)));
  287. /* Erase the U-BOOT image space */
  288. printf("Erasing 0x%x - 0x%x:...", 0, (int)image_size);
  289. ret = nand_erase(nand, 0, image_size);
  290. if (ret) {
  291. printf("Error!\n");
  292. goto error;
  293. }
  294. printf("Done!\n");
  295. /* Write the image to flash */
  296. printf("Writing %d bytes from 0x%lx to offset 0 ... ",
  297. (int)image_size, get_load_addr());
  298. ret = nand_write(nand, 0, &image_size, (void *)get_load_addr());
  299. if (ret)
  300. printf("Error!\n");
  301. else
  302. printf("Done!\n");
  303. error:
  304. return ret;
  305. }
  306. static int is_nand_active(void)
  307. {
  308. return 1;
  309. }
  310. #else /* CONFIG_CMD_NAND */
  311. static int nand_burn_image(size_t image_size)
  312. {
  313. return -ENODEV;
  314. }
  315. static int is_nand_active(void)
  316. {
  317. return 0;
  318. }
  319. #endif /* CONFIG_CMD_NAND */
  320. /********************************************************************
  321. * USB services
  322. ********************************************************************/
  323. #if defined(CONFIG_USB_STORAGE) && defined(CONFIG_BLK)
  324. static size_t usb_read_file(const char *file_name)
  325. {
  326. loff_t act_read = 0;
  327. struct udevice *dev;
  328. int rc;
  329. usb_stop();
  330. if (usb_init() < 0) {
  331. printf("Error: usb_init failed\n");
  332. return 0;
  333. }
  334. /* Try to recognize storage devices immediately */
  335. blk_first_device(IF_TYPE_USB, &dev);
  336. if (!dev) {
  337. printf("Error: USB storage device not found\n");
  338. return 0;
  339. }
  340. /* Always load from usb 0 */
  341. if (fs_set_blk_dev("usb", "0", FS_TYPE_ANY)) {
  342. printf("Error: USB 0 not found\n");
  343. return 0;
  344. }
  345. /* Perfrom file read */
  346. rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
  347. if (rc)
  348. return 0;
  349. return act_read;
  350. }
  351. static int is_usb_active(void)
  352. {
  353. return 1;
  354. }
  355. #else /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
  356. static size_t usb_read_file(const char *file_name)
  357. {
  358. return 0;
  359. }
  360. static int is_usb_active(void)
  361. {
  362. return 0;
  363. }
  364. #endif /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
  365. /********************************************************************
  366. * Network services
  367. ********************************************************************/
  368. #ifdef CONFIG_CMD_NET
  369. static size_t tftp_read_file(const char *file_name)
  370. {
  371. /* update global variable load_addr before tftp file from network */
  372. load_addr = get_load_addr();
  373. return net_loop(TFTPGET);
  374. }
  375. static int is_tftp_active(void)
  376. {
  377. return 1;
  378. }
  379. #else
  380. static size_t tftp_read_file(const char *file_name)
  381. {
  382. return 0;
  383. }
  384. static int is_tftp_active(void)
  385. {
  386. return 0;
  387. }
  388. #endif /* CONFIG_CMD_NET */
  389. enum bubt_devices {
  390. BUBT_DEV_NET = 0,
  391. BUBT_DEV_USB,
  392. BUBT_DEV_MMC,
  393. BUBT_DEV_SPI,
  394. BUBT_DEV_NAND,
  395. BUBT_MAX_DEV
  396. };
  397. struct bubt_dev bubt_devs[BUBT_MAX_DEV] = {
  398. {"tftp", tftp_read_file, NULL, is_tftp_active},
  399. {"usb", usb_read_file, NULL, is_usb_active},
  400. {"mmc", mmc_read_file, mmc_burn_image, is_mmc_active},
  401. {"spi", NULL, spi_burn_image, is_spi_active},
  402. {"nand", NULL, nand_burn_image, is_nand_active},
  403. };
  404. static int bubt_write_file(struct bubt_dev *dst, size_t image_size)
  405. {
  406. if (!dst->write) {
  407. printf("Error: Write not supported on device %s\n", dst->name);
  408. return -ENOTSUPP;
  409. }
  410. return dst->write(image_size);
  411. }
  412. #if defined(CONFIG_ARMADA_8K)
  413. u32 do_checksum32(u32 *start, int32_t len)
  414. {
  415. u32 sum = 0;
  416. u32 *startp = start;
  417. do {
  418. sum += *startp;
  419. startp++;
  420. len -= 4;
  421. } while (len > 0);
  422. return sum;
  423. }
  424. static int check_image_header(void)
  425. {
  426. struct mvebu_image_header *hdr =
  427. (struct mvebu_image_header *)get_load_addr();
  428. u32 header_len = hdr->prolog_size;
  429. u32 checksum;
  430. u32 checksum_ref = hdr->prolog_checksum;
  431. /*
  432. * For now compare checksum, and magic. Later we can
  433. * verify more stuff on the header like interface type, etc
  434. */
  435. if (hdr->magic != MAIN_HDR_MAGIC) {
  436. printf("ERROR: Bad MAGIC 0x%08x != 0x%08x\n",
  437. hdr->magic, MAIN_HDR_MAGIC);
  438. return -ENOEXEC;
  439. }
  440. /* The checksum value is discarded from checksum calculation */
  441. hdr->prolog_checksum = 0;
  442. checksum = do_checksum32((u32 *)hdr, header_len);
  443. if (checksum != checksum_ref) {
  444. printf("Error: Bad Image checksum. 0x%x != 0x%x\n",
  445. checksum, checksum_ref);
  446. return -ENOEXEC;
  447. }
  448. /* Restore the checksum before writing */
  449. hdr->prolog_checksum = checksum_ref;
  450. printf("Image checksum...OK!\n");
  451. return 0;
  452. }
  453. #elif defined(CONFIG_ARMADA_3700) /* Armada 3700 */
  454. static int check_image_header(void)
  455. {
  456. struct common_tim_data *hdr = (struct common_tim_data *)get_load_addr();
  457. int image_num;
  458. u8 hash_160_output[SHA1_SUM_LEN];
  459. u8 hash_256_output[SHA256_SUM_LEN];
  460. sha1_context hash1_text;
  461. sha256_context hash256_text;
  462. u8 *hash_output;
  463. u32 hash_algorithm_id;
  464. u32 image_size_to_hash;
  465. u32 flash_entry_addr;
  466. u32 *hash_value;
  467. u32 internal_hash[HASH_SUM_LEN];
  468. const u8 *buff;
  469. u32 num_of_image = hdr->num_images;
  470. u32 version = hdr->version;
  471. u32 trusted = hdr->trusted;
  472. /* bubt checksum validation only supports nontrusted images */
  473. if (trusted == 1) {
  474. printf("bypass image validation, ");
  475. printf("only untrusted image is supported now\n");
  476. return 0;
  477. }
  478. /* only supports image version 3.5 and 3.6 */
  479. if (version != IMAGE_VERSION_3_5_0 && version != IMAGE_VERSION_3_6_0) {
  480. printf("Error: Unsupported Image version = 0x%08x\n", version);
  481. return -ENOEXEC;
  482. }
  483. /* validate images hash value */
  484. for (image_num = 0; image_num < num_of_image; image_num++) {
  485. struct mvebu_image_info *info =
  486. (struct mvebu_image_info *)(get_load_addr() +
  487. sizeof(struct common_tim_data) +
  488. image_num * sizeof(struct mvebu_image_info));
  489. hash_algorithm_id = info->hash_algorithm_id;
  490. image_size_to_hash = info->image_size_to_hash;
  491. flash_entry_addr = info->flash_entry_addr;
  492. hash_value = info->hash;
  493. buff = (const u8 *)(get_load_addr() + flash_entry_addr);
  494. if (image_num == 0) {
  495. /*
  496. * The first image includes hash values in its content.
  497. * For hash calculation, we need to save the original
  498. * hash values to a local variable that will be
  499. * copied back for comparsion and set all zeros to
  500. * the orignal hash values for calculating new value.
  501. * First image original format :
  502. * x...x (datum1) x...x(orig. hash values) x...x(datum2)
  503. * Replaced first image format :
  504. * x...x (datum1) 0...0(hash values) x...x(datum2)
  505. */
  506. memcpy(internal_hash, hash_value,
  507. sizeof(internal_hash));
  508. memset(hash_value, 0, sizeof(internal_hash));
  509. }
  510. if (image_size_to_hash == 0) {
  511. printf("Warning: Image_%d hash checksum is disabled, ",
  512. image_num);
  513. printf("skip the image validation.\n");
  514. continue;
  515. }
  516. switch (hash_algorithm_id) {
  517. case SHA1_SUM_LEN:
  518. sha1_starts(&hash1_text);
  519. sha1_update(&hash1_text, buff, image_size_to_hash);
  520. sha1_finish(&hash1_text, hash_160_output);
  521. hash_output = hash_160_output;
  522. break;
  523. case SHA256_SUM_LEN:
  524. sha256_starts(&hash256_text);
  525. sha256_update(&hash256_text, buff, image_size_to_hash);
  526. sha256_finish(&hash256_text, hash_256_output);
  527. hash_output = hash_256_output;
  528. break;
  529. default:
  530. printf("Error: Unsupported hash_algorithm_id = %d\n",
  531. hash_algorithm_id);
  532. return -ENOEXEC;
  533. }
  534. if (image_num == 0)
  535. memcpy(hash_value, internal_hash,
  536. sizeof(internal_hash));
  537. if (memcmp(hash_value, hash_output, hash_algorithm_id) != 0) {
  538. printf("Error: Image_%d checksum is not correct\n",
  539. image_num);
  540. return -ENOEXEC;
  541. }
  542. }
  543. printf("Image checksum...OK!\n");
  544. return 0;
  545. }
  546. #else /* Not ARMADA? */
  547. static int check_image_header(void)
  548. {
  549. printf("bubt cmd does not support this SoC device or family!\n");
  550. return -ENOEXEC;
  551. }
  552. #endif
  553. static int bubt_verify(size_t image_size)
  554. {
  555. int err;
  556. /* Check a correct image header exists */
  557. err = check_image_header();
  558. if (err) {
  559. printf("Error: Image header verification failed\n");
  560. return err;
  561. }
  562. return 0;
  563. }
  564. static int bubt_read_file(struct bubt_dev *src)
  565. {
  566. size_t image_size;
  567. if (!src->read) {
  568. printf("Error: Read not supported on device \"%s\"\n",
  569. src->name);
  570. return 0;
  571. }
  572. image_size = src->read(net_boot_file_name);
  573. if (image_size <= 0) {
  574. printf("Error: Failed to read file %s from %s\n",
  575. net_boot_file_name, src->name);
  576. return 0;
  577. }
  578. return image_size;
  579. }
  580. static int bubt_is_dev_active(struct bubt_dev *dev)
  581. {
  582. if (!dev->active) {
  583. printf("Device \"%s\" not supported by U-BOOT image\n",
  584. dev->name);
  585. return 0;
  586. }
  587. if (!dev->active()) {
  588. printf("Device \"%s\" is inactive\n", dev->name);
  589. return 0;
  590. }
  591. return 1;
  592. }
  593. struct bubt_dev *find_bubt_dev(char *dev_name)
  594. {
  595. int dev;
  596. for (dev = 0; dev < BUBT_MAX_DEV; dev++) {
  597. if (strcmp(bubt_devs[dev].name, dev_name) == 0)
  598. return &bubt_devs[dev];
  599. }
  600. return 0;
  601. }
  602. #define DEFAULT_BUBT_SRC "tftp"
  603. #ifndef DEFAULT_BUBT_DST
  604. #ifdef CONFIG_MVEBU_SPI_BOOT
  605. #define DEFAULT_BUBT_DST "spi"
  606. #elif defined(CONFIG_MVEBU_NAND_BOOT)
  607. #define DEFAULT_BUBT_DST "nand"
  608. #elif defined(CONFIG_MVEBU_MMC_BOOT)
  609. #define DEFAULT_BUBT_DST "mmc"
  610. else
  611. #define DEFAULT_BUBT_DST "error"
  612. #endif
  613. #endif /* DEFAULT_BUBT_DST */
  614. int do_bubt_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  615. {
  616. struct bubt_dev *src, *dst;
  617. size_t image_size;
  618. char src_dev_name[8];
  619. char dst_dev_name[8];
  620. char *name;
  621. int err;
  622. if (argc < 2)
  623. copy_filename(net_boot_file_name,
  624. CONFIG_MVEBU_UBOOT_DFLT_NAME,
  625. sizeof(net_boot_file_name));
  626. else
  627. copy_filename(net_boot_file_name, argv[1],
  628. sizeof(net_boot_file_name));
  629. if (argc >= 3) {
  630. strncpy(dst_dev_name, argv[2], 8);
  631. } else {
  632. name = DEFAULT_BUBT_DST;
  633. strncpy(dst_dev_name, name, 8);
  634. }
  635. if (argc >= 4)
  636. strncpy(src_dev_name, argv[3], 8);
  637. else
  638. strncpy(src_dev_name, DEFAULT_BUBT_SRC, 8);
  639. /* Figure out the destination device */
  640. dst = find_bubt_dev(dst_dev_name);
  641. if (!dst) {
  642. printf("Error: Unknown destination \"%s\"\n", dst_dev_name);
  643. return -EINVAL;
  644. }
  645. if (!bubt_is_dev_active(dst))
  646. return -ENODEV;
  647. /* Figure out the source device */
  648. src = find_bubt_dev(src_dev_name);
  649. if (!src) {
  650. printf("Error: Unknown source \"%s\"\n", src_dev_name);
  651. return 1;
  652. }
  653. if (!bubt_is_dev_active(src))
  654. return -ENODEV;
  655. printf("Burning U-BOOT image \"%s\" from \"%s\" to \"%s\"\n",
  656. net_boot_file_name, src->name, dst->name);
  657. image_size = bubt_read_file(src);
  658. if (!image_size)
  659. return -EIO;
  660. err = bubt_verify(image_size);
  661. if (err)
  662. return err;
  663. err = bubt_write_file(dst, image_size);
  664. if (err)
  665. return err;
  666. return 0;
  667. }
  668. U_BOOT_CMD(
  669. bubt, 4, 0, do_bubt_cmd,
  670. "Burn a u-boot image to flash",
  671. "[file-name] [destination [source]]\n"
  672. "\t-file-name The image file name to burn. Default = flash-image.bin\n"
  673. "\t-destination Flash to burn to [spi, nand, mmc]. Default = active boot device\n"
  674. "\t-source The source to load image from [tftp, usb, mmc]. Default = tftp\n"
  675. "Examples:\n"
  676. "\tbubt - Burn flash-image.bin from tftp to active boot device\n"
  677. "\tbubt flash-image-new.bin nand - Burn flash-image-new.bin from tftp to NAND flash\n"
  678. "\tbubt backup-flash-image.bin mmc usb - Burn backup-flash-image.bin from usb to MMC\n"
  679. );