fb_mmc.c 11 KB

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