fb_mmc.c 12 KB

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