bubt.c 21 KB

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