fb_mmc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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_ANDROID_BOOT_IMAGE
  113. /**
  114. * Read Android boot image header from boot partition.
  115. *
  116. * @param[in] dev_desc MMC device descriptor
  117. * @param[in] info Boot partition info
  118. * @param[out] hdr Where to store read boot image header
  119. *
  120. * @return Boot image header sectors count or 0 on error
  121. */
  122. static lbaint_t fb_mmc_get_boot_header(struct blk_desc *dev_desc,
  123. disk_partition_t *info,
  124. struct andr_img_hdr *hdr,
  125. char *response)
  126. {
  127. ulong sector_size; /* boot partition sector size */
  128. lbaint_t hdr_sectors; /* boot image header sectors count */
  129. int res;
  130. /* Calculate boot image sectors count */
  131. sector_size = info->blksz;
  132. hdr_sectors = DIV_ROUND_UP(sizeof(struct andr_img_hdr), sector_size);
  133. if (hdr_sectors == 0) {
  134. pr_err("invalid number of boot sectors: 0\n");
  135. fastboot_fail("invalid number of boot sectors: 0", response);
  136. return 0;
  137. }
  138. /* Read the boot image header */
  139. res = blk_dread(dev_desc, info->start, hdr_sectors, (void *)hdr);
  140. if (res != hdr_sectors) {
  141. pr_err("cannot read header from boot partition\n");
  142. fastboot_fail("cannot read header from boot partition",
  143. response);
  144. return 0;
  145. }
  146. /* Check boot header magic string */
  147. res = android_image_check_header(hdr);
  148. if (res != 0) {
  149. pr_err("bad boot image magic\n");
  150. fastboot_fail("boot partition not initialized", response);
  151. return 0;
  152. }
  153. return hdr_sectors;
  154. }
  155. /**
  156. * Write downloaded zImage to boot partition and repack it properly.
  157. *
  158. * @param dev_desc MMC device descriptor
  159. * @param download_buffer Address to fastboot buffer with zImage in it
  160. * @param download_bytes Size of fastboot buffer, in bytes
  161. *
  162. * @return 0 on success or -1 on error
  163. */
  164. static int fb_mmc_update_zimage(struct blk_desc *dev_desc,
  165. void *download_buffer,
  166. u32 download_bytes,
  167. char *response)
  168. {
  169. uintptr_t hdr_addr; /* boot image header address */
  170. struct andr_img_hdr *hdr; /* boot image header */
  171. lbaint_t hdr_sectors; /* boot image header sectors */
  172. u8 *ramdisk_buffer;
  173. u32 ramdisk_sector_start;
  174. u32 ramdisk_sectors;
  175. u32 kernel_sector_start;
  176. u32 kernel_sectors;
  177. u32 sectors_per_page;
  178. disk_partition_t info;
  179. int res;
  180. puts("Flashing zImage\n");
  181. /* Get boot partition info */
  182. res = part_get_info_by_name(dev_desc, BOOT_PARTITION_NAME, &info);
  183. if (res < 0) {
  184. pr_err("cannot find boot partition\n");
  185. fastboot_fail("cannot find boot partition", response);
  186. return -1;
  187. }
  188. /* Put boot image header in fastboot buffer after downloaded zImage */
  189. hdr_addr = (uintptr_t)download_buffer + ALIGN(download_bytes, PAGE_SIZE);
  190. hdr = (struct andr_img_hdr *)hdr_addr;
  191. /* Read boot image header */
  192. hdr_sectors = fb_mmc_get_boot_header(dev_desc, &info, hdr, response);
  193. if (hdr_sectors == 0) {
  194. pr_err("unable to read boot image header\n");
  195. fastboot_fail("unable to read boot image header", response);
  196. return -1;
  197. }
  198. /* Check if boot image has second stage in it (we don't support it) */
  199. if (hdr->second_size > 0) {
  200. pr_err("moving second stage is not supported yet\n");
  201. fastboot_fail("moving second stage is not supported yet",
  202. response);
  203. return -1;
  204. }
  205. /* Extract ramdisk location */
  206. sectors_per_page = hdr->page_size / info.blksz;
  207. ramdisk_sector_start = info.start + sectors_per_page;
  208. ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
  209. sectors_per_page;
  210. ramdisk_sectors = DIV_ROUND_UP(hdr->ramdisk_size, hdr->page_size) *
  211. sectors_per_page;
  212. /* Read ramdisk and put it in fastboot buffer after boot image header */
  213. ramdisk_buffer = (u8 *)hdr + (hdr_sectors * info.blksz);
  214. res = blk_dread(dev_desc, ramdisk_sector_start, ramdisk_sectors,
  215. ramdisk_buffer);
  216. if (res != ramdisk_sectors) {
  217. pr_err("cannot read ramdisk from boot partition\n");
  218. fastboot_fail("cannot read ramdisk from boot partition",
  219. response);
  220. return -1;
  221. }
  222. /* Write new kernel size to boot image header */
  223. hdr->kernel_size = download_bytes;
  224. res = blk_dwrite(dev_desc, info.start, hdr_sectors, (void *)hdr);
  225. if (res == 0) {
  226. pr_err("cannot writeback boot image header\n");
  227. fastboot_fail("cannot write back boot image header", response);
  228. return -1;
  229. }
  230. /* Write the new downloaded kernel */
  231. kernel_sector_start = info.start + sectors_per_page;
  232. kernel_sectors = DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
  233. sectors_per_page;
  234. res = blk_dwrite(dev_desc, kernel_sector_start, kernel_sectors,
  235. download_buffer);
  236. if (res == 0) {
  237. pr_err("cannot write new kernel\n");
  238. fastboot_fail("cannot write new kernel", response);
  239. return -1;
  240. }
  241. /* Write the saved ramdisk back */
  242. ramdisk_sector_start = info.start + sectors_per_page;
  243. ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
  244. sectors_per_page;
  245. res = blk_dwrite(dev_desc, ramdisk_sector_start, ramdisk_sectors,
  246. ramdisk_buffer);
  247. if (res == 0) {
  248. pr_err("cannot write back original ramdisk\n");
  249. fastboot_fail("cannot write back original ramdisk", response);
  250. return -1;
  251. }
  252. puts("........ zImage was updated in boot partition\n");
  253. fastboot_okay(NULL, response);
  254. return 0;
  255. }
  256. #endif
  257. /**
  258. * fastboot_mmc_get_part_info() - Lookup eMMC partion by name
  259. *
  260. * @part_name: Named partition to lookup
  261. * @dev_desc: Pointer to returned blk_desc pointer
  262. * @part_info: Pointer to returned disk_partition_t
  263. * @response: Pointer to fastboot response buffer
  264. */
  265. int fastboot_mmc_get_part_info(const char *part_name,
  266. struct blk_desc **dev_desc,
  267. disk_partition_t *part_info, char *response)
  268. {
  269. int r;
  270. *dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
  271. if (!*dev_desc) {
  272. fastboot_fail("block device not found", response);
  273. return -ENOENT;
  274. }
  275. if (!part_name || !strcmp(part_name, "")) {
  276. fastboot_fail("partition not given", response);
  277. return -ENOENT;
  278. }
  279. r = part_get_info_by_name_or_alias(*dev_desc, part_name, part_info);
  280. if (r < 0) {
  281. fastboot_fail("partition not found", response);
  282. return r;
  283. }
  284. return r;
  285. }
  286. /**
  287. * fastboot_mmc_flash_write() - Write image to eMMC for fastboot
  288. *
  289. * @cmd: Named partition to write image to
  290. * @download_buffer: Pointer to image data
  291. * @download_bytes: Size of image data
  292. * @response: Pointer to fastboot response buffer
  293. */
  294. void fastboot_mmc_flash_write(const char *cmd, void *download_buffer,
  295. u32 download_bytes, char *response)
  296. {
  297. struct blk_desc *dev_desc;
  298. disk_partition_t info;
  299. dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
  300. if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
  301. pr_err("invalid mmc device\n");
  302. fastboot_fail("invalid mmc device", response);
  303. return;
  304. }
  305. #if CONFIG_IS_ENABLED(EFI_PARTITION)
  306. if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
  307. printf("%s: updating MBR, Primary and Backup GPT(s)\n",
  308. __func__);
  309. if (is_valid_gpt_buf(dev_desc, download_buffer)) {
  310. printf("%s: invalid GPT - refusing to write to flash\n",
  311. __func__);
  312. fastboot_fail("invalid GPT partition", response);
  313. return;
  314. }
  315. if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
  316. printf("%s: writing GPT partitions failed\n", __func__);
  317. fastboot_fail("writing GPT partitions failed",
  318. response);
  319. return;
  320. }
  321. printf("........ success\n");
  322. fastboot_okay(NULL, response);
  323. return;
  324. }
  325. #endif
  326. #if CONFIG_IS_ENABLED(DOS_PARTITION)
  327. if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) {
  328. printf("%s: updating MBR\n", __func__);
  329. if (is_valid_dos_buf(download_buffer)) {
  330. printf("%s: invalid MBR - refusing to write to flash\n",
  331. __func__);
  332. fastboot_fail("invalid MBR partition", response);
  333. return;
  334. }
  335. if (write_mbr_partition(dev_desc, download_buffer)) {
  336. printf("%s: writing MBR partition failed\n", __func__);
  337. fastboot_fail("writing MBR partition failed",
  338. response);
  339. return;
  340. }
  341. printf("........ success\n");
  342. fastboot_okay(NULL, response);
  343. return;
  344. }
  345. #endif
  346. #ifdef CONFIG_ANDROID_BOOT_IMAGE
  347. if (strncasecmp(cmd, "zimage", 6) == 0) {
  348. fb_mmc_update_zimage(dev_desc, download_buffer,
  349. download_bytes, response);
  350. return;
  351. }
  352. #endif
  353. if (part_get_info_by_name_or_alias(dev_desc, cmd, &info) < 0) {
  354. pr_err("cannot find partition: '%s'\n", cmd);
  355. fastboot_fail("cannot find partition", response);
  356. return;
  357. }
  358. if (is_sparse_image(download_buffer)) {
  359. struct fb_mmc_sparse sparse_priv;
  360. struct sparse_storage sparse;
  361. int err;
  362. sparse_priv.dev_desc = dev_desc;
  363. sparse.blksz = info.blksz;
  364. sparse.start = info.start;
  365. sparse.size = info.size;
  366. sparse.write = fb_mmc_sparse_write;
  367. sparse.reserve = fb_mmc_sparse_reserve;
  368. sparse.mssg = fastboot_fail;
  369. printf("Flashing sparse image at offset " LBAFU "\n",
  370. sparse.start);
  371. sparse.priv = &sparse_priv;
  372. err = write_sparse_image(&sparse, cmd, download_buffer,
  373. response);
  374. if (!err)
  375. fastboot_okay(NULL, response);
  376. } else {
  377. write_raw_image(dev_desc, &info, cmd, download_buffer,
  378. download_bytes, response);
  379. }
  380. }
  381. /**
  382. * fastboot_mmc_flash_erase() - Erase eMMC for fastboot
  383. *
  384. * @cmd: Named partition to erase
  385. * @response: Pointer to fastboot response buffer
  386. */
  387. void fastboot_mmc_erase(const char *cmd, char *response)
  388. {
  389. int ret;
  390. struct blk_desc *dev_desc;
  391. disk_partition_t info;
  392. lbaint_t blks, blks_start, blks_size, grp_size;
  393. struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
  394. if (mmc == NULL) {
  395. pr_err("invalid mmc device\n");
  396. fastboot_fail("invalid mmc device", response);
  397. return;
  398. }
  399. dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
  400. if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
  401. pr_err("invalid mmc device\n");
  402. fastboot_fail("invalid mmc device", response);
  403. return;
  404. }
  405. ret = part_get_info_by_name_or_alias(dev_desc, cmd, &info);
  406. if (ret < 0) {
  407. pr_err("cannot find partition: '%s'\n", cmd);
  408. fastboot_fail("cannot find partition", response);
  409. return;
  410. }
  411. /* Align blocks to erase group size to avoid erasing other partitions */
  412. grp_size = mmc->erase_grp_size;
  413. blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
  414. if (info.size >= grp_size)
  415. blks_size = (info.size - (blks_start - info.start)) &
  416. (~(grp_size - 1));
  417. else
  418. blks_size = 0;
  419. printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
  420. blks_start, blks_start + blks_size);
  421. blks = fb_mmc_blk_write(dev_desc, blks_start, blks_size, NULL);
  422. if (blks != blks_size) {
  423. pr_err("failed erasing from device %d\n", dev_desc->devnum);
  424. fastboot_fail("failed erasing from device", response);
  425. return;
  426. }
  427. printf("........ erased " LBAFU " bytes from '%s'\n",
  428. blks_size * info.blksz, cmd);
  429. fastboot_okay(NULL, response);
  430. }