fb_mmc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2014 Broadcom Corporation.
  4. */
  5. #include <config.h>
  6. #include <common.h>
  7. #include <blk.h>
  8. #include <env.h>
  9. #include <fastboot.h>
  10. #include <fastboot-internal.h>
  11. #include <fb_mmc.h>
  12. #include <flash.h>
  13. #include <image-sparse.h>
  14. #include <image.h>
  15. #include <log.h>
  16. #include <part.h>
  17. #include <mmc.h>
  18. #include <div64.h>
  19. #include <linux/compat.h>
  20. #include <android_image.h>
  21. #ifdef CONFIG_FASTBOOT_STARFIVE_MAX_BLK_WRITE
  22. #define FASTBOOT_MAX_BLK_WRITE CONFIG_FASTBOOT_STARFIVE_MAX_BLK_WRITE /* = 8192 */
  23. #else
  24. #define FASTBOOT_MAX_BLK_WRITE 16384
  25. #endif
  26. #define BOOT_PARTITION_NAME "boot"
  27. struct fb_mmc_sparse {
  28. struct blk_desc *dev_desc;
  29. };
  30. static int raw_part_get_info_by_name(struct blk_desc *dev_desc,
  31. const char *name,
  32. struct disk_partition *info)
  33. {
  34. /* strlen("fastboot_raw_partition_") + PART_NAME_LEN + 1 */
  35. char env_desc_name[23 + PART_NAME_LEN + 1];
  36. char *raw_part_desc;
  37. const char *argv[2];
  38. const char **parg = argv;
  39. /* check for raw partition descriptor */
  40. strcpy(env_desc_name, "fastboot_raw_partition_");
  41. strlcat(env_desc_name, name, PART_NAME_LEN);
  42. raw_part_desc = strdup(env_get(env_desc_name));
  43. if (raw_part_desc == NULL)
  44. return -ENODEV;
  45. /*
  46. * parse partition descriptor
  47. *
  48. * <lba_start> <lba_size> [mmcpart <num>]
  49. */
  50. for (; parg < argv + sizeof(argv) / sizeof(*argv); ++parg) {
  51. *parg = strsep(&raw_part_desc, " ");
  52. if (*parg == NULL) {
  53. pr_err("Invalid number of arguments.\n");
  54. return -ENODEV;
  55. }
  56. }
  57. info->start = simple_strtoul(argv[0], NULL, 0);
  58. info->size = simple_strtoul(argv[1], NULL, 0);
  59. info->blksz = dev_desc->blksz;
  60. strlcpy((char *)info->name, name, PART_NAME_LEN);
  61. if (raw_part_desc) {
  62. if (strcmp(strsep(&raw_part_desc, " "), "mmcpart") == 0) {
  63. ulong mmcpart = simple_strtoul(raw_part_desc, NULL, 0);
  64. int ret = blk_dselect_hwpart(dev_desc, mmcpart);
  65. if (ret)
  66. return ret;
  67. }
  68. }
  69. return 0;
  70. }
  71. static int do_get_part_info(struct blk_desc **dev_desc, const char *name,
  72. struct disk_partition *info)
  73. {
  74. int ret;
  75. /* First try partition names on the default device */
  76. *dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
  77. if (*dev_desc) {
  78. ret = part_get_info_by_name(*dev_desc, name, info);
  79. if (ret >= 0)
  80. return ret;
  81. /* Then try raw partitions */
  82. ret = raw_part_get_info_by_name(*dev_desc, name, info);
  83. if (ret >= 0)
  84. return ret;
  85. }
  86. /* Then try dev.hwpart:part */
  87. ret = part_get_info_by_dev_and_name_or_num("mmc", name, dev_desc,
  88. info, true);
  89. return ret;
  90. }
  91. static int part_get_info_by_name_or_alias(struct blk_desc **dev_desc,
  92. const char *name,
  93. struct disk_partition *info)
  94. {
  95. int ret;
  96. ret = do_get_part_info(dev_desc, name, info);
  97. if (ret < 0) {
  98. /* strlen("fastboot_partition_alias_") + PART_NAME_LEN + 1 */
  99. char env_alias_name[25 + PART_NAME_LEN + 1];
  100. char *aliased_part_name;
  101. /* check for alias */
  102. strcpy(env_alias_name, "fastboot_partition_alias_");
  103. strlcat(env_alias_name, name, PART_NAME_LEN);
  104. aliased_part_name = env_get(env_alias_name);
  105. if (aliased_part_name != NULL)
  106. ret = do_get_part_info(dev_desc, aliased_part_name,
  107. info);
  108. }
  109. return ret;
  110. }
  111. /**
  112. * fb_mmc_blk_write() - Write/erase MMC in chunks of FASTBOOT_MAX_BLK_WRITE
  113. *
  114. * @block_dev: Pointer to block device
  115. * @start: First block to write/erase
  116. * @blkcnt: Count of blocks
  117. * @buffer: Pointer to data buffer for write or NULL for erase
  118. */
  119. static lbaint_t fb_mmc_blk_write(struct blk_desc *block_dev, lbaint_t start,
  120. lbaint_t blkcnt, const void *buffer)
  121. {
  122. lbaint_t blk = start;
  123. lbaint_t blks_written;
  124. lbaint_t cur_blkcnt;
  125. lbaint_t blks = 0;
  126. int i;
  127. for (i = 0; i < blkcnt; i += FASTBOOT_MAX_BLK_WRITE) {
  128. cur_blkcnt = min((int)blkcnt - i, FASTBOOT_MAX_BLK_WRITE);
  129. if (buffer) {
  130. if (fastboot_progress_callback)
  131. fastboot_progress_callback("writing");
  132. blks_written = blk_dwrite(block_dev, blk, cur_blkcnt,
  133. buffer + (i * block_dev->blksz));
  134. } else {
  135. if (fastboot_progress_callback)
  136. fastboot_progress_callback("erasing");
  137. blks_written = blk_derase(block_dev, blk, cur_blkcnt);
  138. }
  139. blk += blks_written;
  140. blks += blks_written;
  141. }
  142. return blks;
  143. }
  144. static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info,
  145. lbaint_t blk, lbaint_t blkcnt, const void *buffer)
  146. {
  147. struct fb_mmc_sparse *sparse = info->priv;
  148. struct blk_desc *dev_desc = sparse->dev_desc;
  149. return fb_mmc_blk_write(dev_desc, blk, blkcnt, buffer);
  150. }
  151. static lbaint_t fb_mmc_sparse_reserve(struct sparse_storage *info,
  152. lbaint_t blk, lbaint_t blkcnt)
  153. {
  154. return blkcnt;
  155. }
  156. static void write_raw_image(struct blk_desc *dev_desc,
  157. struct disk_partition *info, const char *part_name,
  158. void *buffer, u32 download_bytes, char *response)
  159. {
  160. lbaint_t blkcnt;
  161. lbaint_t blks;
  162. /* determine number of blocks to write */
  163. blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
  164. blkcnt = lldiv(blkcnt, info->blksz);
  165. if (blkcnt > info->size) {
  166. pr_err("too large for partition: '%s'\n", part_name);
  167. fastboot_fail("too large for partition", response);
  168. return;
  169. }
  170. puts("Flashing Raw Image\n");
  171. blks = fb_mmc_blk_write(dev_desc, info->start, blkcnt, buffer);
  172. if (blks != blkcnt) {
  173. pr_err("failed writing to device %d\n", dev_desc->devnum);
  174. fastboot_fail("failed writing to device", response);
  175. return;
  176. }
  177. printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
  178. part_name);
  179. fastboot_okay(NULL, response);
  180. }
  181. #if defined(CONFIG_FASTBOOT_MMC_BOOT_SUPPORT) || \
  182. defined(CONFIG_FASTBOOT_MMC_USER_SUPPORT)
  183. static int fb_mmc_erase_mmc_hwpart(struct blk_desc *dev_desc)
  184. {
  185. lbaint_t blks;
  186. debug("Start Erasing mmc hwpart[%u]...\n", dev_desc->hwpart);
  187. blks = fb_mmc_blk_write(dev_desc, 0, dev_desc->lba, NULL);
  188. if (blks != dev_desc->lba) {
  189. pr_err("Failed to erase mmc hwpart[%u]\n", dev_desc->hwpart);
  190. return 1;
  191. }
  192. printf("........ erased %lu bytes from mmc hwpart[%u]\n",
  193. dev_desc->lba * dev_desc->blksz, dev_desc->hwpart);
  194. return 0;
  195. }
  196. #endif
  197. #ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT
  198. static void fb_mmc_boot_ops(struct blk_desc *dev_desc, void *buffer,
  199. int hwpart, u32 buff_sz, char *response)
  200. {
  201. lbaint_t blkcnt;
  202. lbaint_t blks;
  203. unsigned long blksz;
  204. // To operate on EMMC_BOOT1/2 (mmc0boot0/1) we first change the hwpart
  205. if (blk_dselect_hwpart(dev_desc, hwpart)) {
  206. pr_err("Failed to select hwpart\n");
  207. fastboot_fail("Failed to select hwpart", response);
  208. return;
  209. }
  210. if (buffer) { /* flash */
  211. /* determine number of blocks to write */
  212. blksz = dev_desc->blksz;
  213. blkcnt = ((buff_sz + (blksz - 1)) & ~(blksz - 1));
  214. blkcnt = lldiv(blkcnt, blksz);
  215. if (blkcnt > dev_desc->lba) {
  216. pr_err("Image size too large\n");
  217. fastboot_fail("Image size too large", response);
  218. return;
  219. }
  220. debug("Start Flashing Image to EMMC_BOOT%d...\n", hwpart);
  221. blks = fb_mmc_blk_write(dev_desc, 0, blkcnt, buffer);
  222. if (blks != blkcnt) {
  223. pr_err("Failed to write EMMC_BOOT%d\n", hwpart);
  224. fastboot_fail("Failed to write EMMC_BOOT part",
  225. response);
  226. return;
  227. }
  228. printf("........ wrote %lu bytes to EMMC_BOOT%d\n",
  229. blkcnt * blksz, hwpart);
  230. } else { /* erase */
  231. if (fb_mmc_erase_mmc_hwpart(dev_desc)) {
  232. pr_err("Failed to erase EMMC_BOOT%d\n", hwpart);
  233. fastboot_fail("Failed to erase EMMC_BOOT part",
  234. response);
  235. return;
  236. }
  237. }
  238. fastboot_okay(NULL, response);
  239. }
  240. #endif
  241. #ifdef CONFIG_ANDROID_BOOT_IMAGE
  242. /**
  243. * Read Android boot image header from boot partition.
  244. *
  245. * @param[in] dev_desc MMC device descriptor
  246. * @param[in] info Boot partition info
  247. * @param[out] hdr Where to store read boot image header
  248. *
  249. * @return Boot image header sectors count or 0 on error
  250. */
  251. static lbaint_t fb_mmc_get_boot_header(struct blk_desc *dev_desc,
  252. struct disk_partition *info,
  253. struct andr_img_hdr *hdr,
  254. char *response)
  255. {
  256. ulong sector_size; /* boot partition sector size */
  257. lbaint_t hdr_sectors; /* boot image header sectors count */
  258. int res;
  259. /* Calculate boot image sectors count */
  260. sector_size = info->blksz;
  261. hdr_sectors = DIV_ROUND_UP(sizeof(struct andr_img_hdr), sector_size);
  262. if (hdr_sectors == 0) {
  263. pr_err("invalid number of boot sectors: 0\n");
  264. fastboot_fail("invalid number of boot sectors: 0", response);
  265. return 0;
  266. }
  267. /* Read the boot image header */
  268. res = blk_dread(dev_desc, info->start, hdr_sectors, (void *)hdr);
  269. if (res != hdr_sectors) {
  270. pr_err("cannot read header from boot partition\n");
  271. fastboot_fail("cannot read header from boot partition",
  272. response);
  273. return 0;
  274. }
  275. /* Check boot header magic string */
  276. res = android_image_check_header(hdr);
  277. if (res != 0) {
  278. pr_err("bad boot image magic\n");
  279. fastboot_fail("boot partition not initialized", response);
  280. return 0;
  281. }
  282. return hdr_sectors;
  283. }
  284. /**
  285. * Write downloaded zImage to boot partition and repack it properly.
  286. *
  287. * @param dev_desc MMC device descriptor
  288. * @param download_buffer Address to fastboot buffer with zImage in it
  289. * @param download_bytes Size of fastboot buffer, in bytes
  290. *
  291. * @return 0 on success or -1 on error
  292. */
  293. static int fb_mmc_update_zimage(struct blk_desc *dev_desc,
  294. void *download_buffer,
  295. u32 download_bytes,
  296. char *response)
  297. {
  298. uintptr_t hdr_addr; /* boot image header address */
  299. struct andr_img_hdr *hdr; /* boot image header */
  300. lbaint_t hdr_sectors; /* boot image header sectors */
  301. u8 *ramdisk_buffer;
  302. u32 ramdisk_sector_start;
  303. u32 ramdisk_sectors;
  304. u32 kernel_sector_start;
  305. u32 kernel_sectors;
  306. u32 sectors_per_page;
  307. struct disk_partition info;
  308. int res;
  309. puts("Flashing zImage\n");
  310. /* Get boot partition info */
  311. res = part_get_info_by_name(dev_desc, BOOT_PARTITION_NAME, &info);
  312. if (res < 0) {
  313. pr_err("cannot find boot partition\n");
  314. fastboot_fail("cannot find boot partition", response);
  315. return -1;
  316. }
  317. /* Put boot image header in fastboot buffer after downloaded zImage */
  318. hdr_addr = (uintptr_t)download_buffer + ALIGN(download_bytes, PAGE_SIZE);
  319. hdr = (struct andr_img_hdr *)hdr_addr;
  320. /* Read boot image header */
  321. hdr_sectors = fb_mmc_get_boot_header(dev_desc, &info, hdr, response);
  322. if (hdr_sectors == 0) {
  323. pr_err("unable to read boot image header\n");
  324. fastboot_fail("unable to read boot image header", response);
  325. return -1;
  326. }
  327. /* Check if boot image has second stage in it (we don't support it) */
  328. if (hdr->second_size > 0) {
  329. pr_err("moving second stage is not supported yet\n");
  330. fastboot_fail("moving second stage is not supported yet",
  331. response);
  332. return -1;
  333. }
  334. /* Extract ramdisk location */
  335. sectors_per_page = hdr->page_size / info.blksz;
  336. ramdisk_sector_start = info.start + sectors_per_page;
  337. ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
  338. sectors_per_page;
  339. ramdisk_sectors = DIV_ROUND_UP(hdr->ramdisk_size, hdr->page_size) *
  340. sectors_per_page;
  341. /* Read ramdisk and put it in fastboot buffer after boot image header */
  342. ramdisk_buffer = (u8 *)hdr + (hdr_sectors * info.blksz);
  343. res = blk_dread(dev_desc, ramdisk_sector_start, ramdisk_sectors,
  344. ramdisk_buffer);
  345. if (res != ramdisk_sectors) {
  346. pr_err("cannot read ramdisk from boot partition\n");
  347. fastboot_fail("cannot read ramdisk from boot partition",
  348. response);
  349. return -1;
  350. }
  351. /* Write new kernel size to boot image header */
  352. hdr->kernel_size = download_bytes;
  353. res = blk_dwrite(dev_desc, info.start, hdr_sectors, (void *)hdr);
  354. if (res == 0) {
  355. pr_err("cannot writeback boot image header\n");
  356. fastboot_fail("cannot write back boot image header", response);
  357. return -1;
  358. }
  359. /* Write the new downloaded kernel */
  360. kernel_sector_start = info.start + sectors_per_page;
  361. kernel_sectors = DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
  362. sectors_per_page;
  363. res = blk_dwrite(dev_desc, kernel_sector_start, kernel_sectors,
  364. download_buffer);
  365. if (res == 0) {
  366. pr_err("cannot write new kernel\n");
  367. fastboot_fail("cannot write new kernel", response);
  368. return -1;
  369. }
  370. /* Write the saved ramdisk back */
  371. ramdisk_sector_start = info.start + sectors_per_page;
  372. ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
  373. sectors_per_page;
  374. res = blk_dwrite(dev_desc, ramdisk_sector_start, ramdisk_sectors,
  375. ramdisk_buffer);
  376. if (res == 0) {
  377. pr_err("cannot write back original ramdisk\n");
  378. fastboot_fail("cannot write back original ramdisk", response);
  379. return -1;
  380. }
  381. puts("........ zImage was updated in boot partition\n");
  382. fastboot_okay(NULL, response);
  383. return 0;
  384. }
  385. #endif
  386. /**
  387. * fastboot_mmc_get_part_info() - Lookup eMMC partion by name
  388. *
  389. * @part_name: Named partition to lookup
  390. * @dev_desc: Pointer to returned blk_desc pointer
  391. * @part_info: Pointer to returned struct disk_partition
  392. * @response: Pointer to fastboot response buffer
  393. */
  394. int fastboot_mmc_get_part_info(const char *part_name,
  395. struct blk_desc **dev_desc,
  396. struct disk_partition *part_info, char *response)
  397. {
  398. int ret;
  399. if (!part_name || !strcmp(part_name, "")) {
  400. fastboot_fail("partition not given", response);
  401. return -ENOENT;
  402. }
  403. ret = part_get_info_by_name_or_alias(dev_desc, part_name, part_info);
  404. if (ret < 0) {
  405. switch (ret) {
  406. case -ENOSYS:
  407. case -EINVAL:
  408. fastboot_fail("invalid partition or device", response);
  409. break;
  410. case -ENODEV:
  411. fastboot_fail("no such device", response);
  412. break;
  413. case -ENOENT:
  414. fastboot_fail("no such partition", response);
  415. break;
  416. case -EPROTONOSUPPORT:
  417. fastboot_fail("unknown partition table type", response);
  418. break;
  419. default:
  420. fastboot_fail("unanticipated error", response);
  421. break;
  422. }
  423. }
  424. return ret;
  425. }
  426. static struct blk_desc *fastboot_mmc_get_dev(char *response)
  427. {
  428. struct blk_desc *ret = blk_get_dev("mmc",
  429. CONFIG_FASTBOOT_FLASH_MMC_DEV);
  430. if (!ret || ret->type == DEV_TYPE_UNKNOWN) {
  431. pr_err("invalid mmc device\n");
  432. fastboot_fail("invalid mmc device", response);
  433. return NULL;
  434. }
  435. return ret;
  436. }
  437. /**
  438. * fastboot_mmc_flash_write() - Write image to eMMC for fastboot
  439. *
  440. * @cmd: Named partition to write image to
  441. * @download_buffer: Pointer to image data
  442. * @download_bytes: Size of image data
  443. * @response: Pointer to fastboot response buffer
  444. */
  445. void fastboot_mmc_flash_write(const char *cmd, void *download_buffer,
  446. u32 download_bytes, char *response)
  447. {
  448. struct blk_desc *dev_desc;
  449. struct disk_partition info = {0};
  450. char *fastboot_env;
  451. #ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT
  452. if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) {
  453. dev_desc = fastboot_mmc_get_dev(response);
  454. if (dev_desc)
  455. fb_mmc_boot_ops(dev_desc, download_buffer, 1,
  456. download_bytes, response);
  457. return;
  458. }
  459. if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT2_NAME) == 0) {
  460. dev_desc = fastboot_mmc_get_dev(response);
  461. if (dev_desc)
  462. fb_mmc_boot_ops(dev_desc, download_buffer, 2,
  463. download_bytes, response);
  464. return;
  465. }
  466. #endif
  467. #if CONFIG_IS_ENABLED(EFI_PARTITION)
  468. if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
  469. dev_desc = fastboot_mmc_get_dev(response);
  470. if (!dev_desc)
  471. return;
  472. printf("%s: updating MBR, Primary and Backup GPT(s)\n",
  473. __func__);
  474. if (is_valid_gpt_buf(dev_desc, download_buffer)) {
  475. printf("%s: invalid GPT - refusing to write to flash\n",
  476. __func__);
  477. fastboot_fail("invalid GPT partition", response);
  478. return;
  479. }
  480. if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
  481. printf("%s: writing GPT partitions failed\n", __func__);
  482. fastboot_fail("writing GPT partitions failed",
  483. response);
  484. return;
  485. }
  486. part_init(dev_desc);
  487. printf("........ success\n");
  488. fastboot_okay(NULL, response);
  489. return;
  490. }
  491. #endif
  492. #if CONFIG_IS_ENABLED(DOS_PARTITION)
  493. if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) {
  494. dev_desc = fastboot_mmc_get_dev(response);
  495. if (!dev_desc)
  496. return;
  497. printf("%s: updating MBR\n", __func__);
  498. if (is_valid_dos_buf(download_buffer)) {
  499. printf("%s: invalid MBR - refusing to write to flash\n",
  500. __func__);
  501. fastboot_fail("invalid MBR partition", response);
  502. return;
  503. }
  504. if (write_mbr_sector(dev_desc, download_buffer)) {
  505. printf("%s: writing MBR partition failed\n", __func__);
  506. fastboot_fail("writing MBR partition failed",
  507. response);
  508. return;
  509. }
  510. part_init(dev_desc);
  511. printf("........ success\n");
  512. fastboot_okay(NULL, response);
  513. return;
  514. }
  515. #endif
  516. #ifdef CONFIG_ANDROID_BOOT_IMAGE
  517. if (strncasecmp(cmd, "zimage", 6) == 0) {
  518. dev_desc = fastboot_mmc_get_dev(response);
  519. if (dev_desc)
  520. fb_mmc_update_zimage(dev_desc, download_buffer,
  521. download_bytes, response);
  522. return;
  523. }
  524. #endif
  525. #if CONFIG_IS_ENABLED(FASTBOOT_MMC_USER_SUPPORT)
  526. if (strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) {
  527. dev_desc = fastboot_mmc_get_dev(response);
  528. if (!dev_desc)
  529. return;
  530. strlcpy((char *)&info.name, cmd, sizeof(info.name));
  531. info.size = dev_desc->lba;
  532. info.blksz = dev_desc->blksz;
  533. }
  534. #endif
  535. /* Fastboot for StarFive JH7110 SoC */
  536. fastboot_env = env_get("fb_sf_flag");
  537. if (fastboot_env) {
  538. dev_desc = blk_get_dev("mmc", 0);
  539. if (!dev_desc)
  540. return;
  541. if (strcmp(cmd, "all") == 0) {
  542. strlcpy((char *)&info.name, cmd, sizeof(info.name));
  543. info.start = 0x0;
  544. info.size = 0x80000000;
  545. info.blksz = 0x200;
  546. } else if (strcmp(cmd, "part") == 0) {
  547. strlcpy((char *)&info.name, cmd, sizeof(info.name));
  548. info.start = 0x0;
  549. info.size = 0x1000;
  550. info.blksz = 0x200;
  551. } else if (strcmp(cmd, "spl") == 0) {
  552. strlcpy((char *)&info.name, cmd, sizeof(info.name));
  553. info.start = 0x1000;
  554. info.size = 0x1000;
  555. info.blksz = 0x200;
  556. } else if (strcmp(cmd, "uboot") == 0) {
  557. strlcpy((char *)&info.name, cmd, sizeof(info.name));
  558. info.start = 0x2000;
  559. info.size = 0x2000;
  560. info.blksz = 0x200;
  561. } else if (strcmp(cmd, "image") == 0) {
  562. fastboot_env = env_get("fb_sf_image_addr");
  563. if (fastboot_env) {
  564. strlcpy((char *)&info.name, cmd, sizeof(info.name));
  565. info.start = simple_strtoul(fastboot_env, NULL, 16);
  566. info.size = 0x92000;
  567. info.blksz = 0x200;
  568. }
  569. } else if (strcmp(cmd, "root") == 0) {
  570. fastboot_env = env_get("fb_sf_root_addr");
  571. if (fastboot_env) {
  572. strlcpy((char *)&info.name, cmd, sizeof(info.name));
  573. info.start = simple_strtoul(fastboot_env, NULL, 16);
  574. info.size = 0x80000000;
  575. info.blksz = 0x200;
  576. }
  577. }
  578. }
  579. if (!info.name[0] &&
  580. fastboot_mmc_get_part_info(cmd, &dev_desc, &info, response) < 0)
  581. return;
  582. if (is_sparse_image(download_buffer)) {
  583. struct fb_mmc_sparse sparse_priv;
  584. struct sparse_storage sparse;
  585. int err;
  586. sparse_priv.dev_desc = dev_desc;
  587. sparse.blksz = info.blksz;
  588. sparse.start = info.start;
  589. sparse.size = info.size;
  590. sparse.write = fb_mmc_sparse_write;
  591. sparse.reserve = fb_mmc_sparse_reserve;
  592. sparse.mssg = fastboot_fail;
  593. printf("Flashing sparse image at offset " LBAFU "\n",
  594. sparse.start);
  595. sparse.priv = &sparse_priv;
  596. err = write_sparse_image(&sparse, cmd, download_buffer,
  597. response);
  598. if (!err)
  599. fastboot_okay(NULL, response);
  600. } else {
  601. write_raw_image(dev_desc, &info, cmd, download_buffer,
  602. download_bytes, response);
  603. }
  604. }
  605. /**
  606. * fastboot_mmc_flash_erase() - Erase eMMC for fastboot
  607. *
  608. * @cmd: Named partition to erase
  609. * @response: Pointer to fastboot response buffer
  610. */
  611. void fastboot_mmc_erase(const char *cmd, char *response)
  612. {
  613. struct blk_desc *dev_desc;
  614. struct disk_partition info;
  615. lbaint_t blks, blks_start, blks_size, grp_size;
  616. struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
  617. #ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT
  618. if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) {
  619. /* erase EMMC boot1 */
  620. dev_desc = fastboot_mmc_get_dev(response);
  621. if (dev_desc)
  622. fb_mmc_boot_ops(dev_desc, NULL, 1, 0, response);
  623. return;
  624. }
  625. if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT2_NAME) == 0) {
  626. /* erase EMMC boot2 */
  627. dev_desc = fastboot_mmc_get_dev(response);
  628. if (dev_desc)
  629. fb_mmc_boot_ops(dev_desc, NULL, 2, 0, response);
  630. return;
  631. }
  632. #endif
  633. #ifdef CONFIG_FASTBOOT_MMC_USER_SUPPORT
  634. if (strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) {
  635. /* erase EMMC userdata */
  636. dev_desc = fastboot_mmc_get_dev(response);
  637. if (!dev_desc)
  638. return;
  639. if (fb_mmc_erase_mmc_hwpart(dev_desc))
  640. fastboot_fail("Failed to erase EMMC_USER", response);
  641. else
  642. fastboot_okay(NULL, response);
  643. return;
  644. }
  645. #endif
  646. if (fastboot_mmc_get_part_info(cmd, &dev_desc, &info, response) < 0)
  647. return;
  648. /* Align blocks to erase group size to avoid erasing other partitions */
  649. grp_size = mmc->erase_grp_size;
  650. blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
  651. if (info.size >= grp_size)
  652. blks_size = (info.size - (blks_start - info.start)) &
  653. (~(grp_size - 1));
  654. else
  655. blks_size = 0;
  656. printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
  657. blks_start, blks_start + blks_size);
  658. blks = fb_mmc_blk_write(dev_desc, blks_start, blks_size, NULL);
  659. if (blks != blks_size) {
  660. pr_err("failed erasing from device %d\n", dev_desc->devnum);
  661. fastboot_fail("failed erasing from device", response);
  662. return;
  663. }
  664. printf("........ erased " LBAFU " bytes from '%s'\n",
  665. blks_size * info.blksz, cmd);
  666. fastboot_okay(NULL, response);
  667. }