fb_mmc.c 18 KB

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