fb_mmc.c 16 KB

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