fb_mmc.c 19 KB

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