fb_mmc.c 16 KB

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