fb_mmc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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_") + PART_NAME_LEN + 1 */
  29. char env_alias_name[25 + PART_NAME_LEN + 1];
  30. char *aliased_part_name;
  31. /* check for alias */
  32. strcpy(env_alias_name, "fastboot_partition_alias_");
  33. strncat(env_alias_name, name, PART_NAME_LEN);
  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(const char *part_name,
  265. struct blk_desc **dev_desc,
  266. disk_partition_t *part_info, char *response)
  267. {
  268. int r;
  269. *dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
  270. if (!*dev_desc) {
  271. fastboot_fail("block device not found", response);
  272. return -ENOENT;
  273. }
  274. if (!part_name || !strcmp(part_name, "")) {
  275. fastboot_fail("partition not given", response);
  276. return -ENOENT;
  277. }
  278. r = part_get_info_by_name_or_alias(*dev_desc, part_name, part_info);
  279. if (r < 0) {
  280. fastboot_fail("partition not found", response);
  281. return r;
  282. }
  283. return r;
  284. }
  285. /**
  286. * fastboot_mmc_flash_write() - Write image to eMMC for fastboot
  287. *
  288. * @cmd: Named partition to write image to
  289. * @download_buffer: Pointer to image data
  290. * @download_bytes: Size of image data
  291. * @response: Pointer to fastboot response buffer
  292. */
  293. void fastboot_mmc_flash_write(const char *cmd, void *download_buffer,
  294. u32 download_bytes, char *response)
  295. {
  296. struct blk_desc *dev_desc;
  297. disk_partition_t info;
  298. dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
  299. if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
  300. pr_err("invalid mmc device\n");
  301. fastboot_fail("invalid mmc device", response);
  302. return;
  303. }
  304. #if CONFIG_IS_ENABLED(EFI_PARTITION)
  305. if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
  306. printf("%s: updating MBR, Primary and Backup GPT(s)\n",
  307. __func__);
  308. if (is_valid_gpt_buf(dev_desc, download_buffer)) {
  309. printf("%s: invalid GPT - refusing to write to flash\n",
  310. __func__);
  311. fastboot_fail("invalid GPT partition", response);
  312. return;
  313. }
  314. if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
  315. printf("%s: writing GPT partitions failed\n", __func__);
  316. fastboot_fail("writing GPT partitions failed",
  317. response);
  318. return;
  319. }
  320. printf("........ success\n");
  321. fastboot_okay(NULL, response);
  322. return;
  323. }
  324. #endif
  325. #if CONFIG_IS_ENABLED(DOS_PARTITION)
  326. if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) {
  327. printf("%s: updating MBR\n", __func__);
  328. if (is_valid_dos_buf(download_buffer)) {
  329. printf("%s: invalid MBR - refusing to write to flash\n",
  330. __func__);
  331. fastboot_fail("invalid MBR partition", response);
  332. return;
  333. }
  334. if (write_mbr_partition(dev_desc, download_buffer)) {
  335. printf("%s: writing MBR partition failed\n", __func__);
  336. fastboot_fail("writing MBR partition failed",
  337. response);
  338. return;
  339. }
  340. printf("........ success\n");
  341. fastboot_okay(NULL, response);
  342. return;
  343. }
  344. #endif
  345. #ifdef CONFIG_ANDROID_BOOT_IMAGE
  346. if (strncasecmp(cmd, "zimage", 6) == 0) {
  347. fb_mmc_update_zimage(dev_desc, download_buffer,
  348. download_bytes, response);
  349. return;
  350. }
  351. #endif
  352. if (part_get_info_by_name_or_alias(dev_desc, cmd, &info) < 0) {
  353. pr_err("cannot find partition: '%s'\n", cmd);
  354. fastboot_fail("cannot find partition", response);
  355. return;
  356. }
  357. if (is_sparse_image(download_buffer)) {
  358. struct fb_mmc_sparse sparse_priv;
  359. struct sparse_storage sparse;
  360. int err;
  361. sparse_priv.dev_desc = dev_desc;
  362. sparse.blksz = info.blksz;
  363. sparse.start = info.start;
  364. sparse.size = info.size;
  365. sparse.write = fb_mmc_sparse_write;
  366. sparse.reserve = fb_mmc_sparse_reserve;
  367. sparse.mssg = fastboot_fail;
  368. printf("Flashing sparse image at offset " LBAFU "\n",
  369. sparse.start);
  370. sparse.priv = &sparse_priv;
  371. err = write_sparse_image(&sparse, cmd, download_buffer,
  372. response);
  373. if (!err)
  374. fastboot_okay(NULL, response);
  375. } else {
  376. write_raw_image(dev_desc, &info, cmd, download_buffer,
  377. download_bytes, response);
  378. }
  379. }
  380. /**
  381. * fastboot_mmc_flash_erase() - Erase eMMC for fastboot
  382. *
  383. * @cmd: Named partition to erase
  384. * @response: Pointer to fastboot response buffer
  385. */
  386. void fastboot_mmc_erase(const char *cmd, char *response)
  387. {
  388. int ret;
  389. struct blk_desc *dev_desc;
  390. disk_partition_t info;
  391. lbaint_t blks, blks_start, blks_size, grp_size;
  392. struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
  393. if (mmc == NULL) {
  394. pr_err("invalid mmc device\n");
  395. fastboot_fail("invalid mmc device", response);
  396. return;
  397. }
  398. dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
  399. if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
  400. pr_err("invalid mmc device\n");
  401. fastboot_fail("invalid mmc device", response);
  402. return;
  403. }
  404. ret = part_get_info_by_name_or_alias(dev_desc, cmd, &info);
  405. if (ret < 0) {
  406. pr_err("cannot find partition: '%s'\n", cmd);
  407. fastboot_fail("cannot find partition", response);
  408. return;
  409. }
  410. /* Align blocks to erase group size to avoid erasing other partitions */
  411. grp_size = mmc->erase_grp_size;
  412. blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
  413. if (info.size >= grp_size)
  414. blks_size = (info.size - (blks_start - info.start)) &
  415. (~(grp_size - 1));
  416. else
  417. blks_size = 0;
  418. printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
  419. blks_start, blks_start + blks_size);
  420. blks = fb_mmc_blk_write(dev_desc, blks_start, blks_size, NULL);
  421. if (blks != blks_size) {
  422. pr_err("failed erasing from device %d\n", dev_desc->devnum);
  423. fastboot_fail("failed erasing from device", response);
  424. return;
  425. }
  426. printf("........ erased " LBAFU " bytes from '%s'\n",
  427. blks_size * info.blksz, cmd);
  428. fastboot_okay(NULL, response);
  429. }