fb_mmc.c 14 KB

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