1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2014 Broadcom Corporation.
11 #include <fastboot-internal.h>
13 #include <image-sparse.h>
19 #include <linux/compat.h>
20 #include <android_image.h>
22 #define FASTBOOT_MAX_BLK_WRITE 16384
24 #define BOOT_PARTITION_NAME "boot"
26 struct fb_mmc_sparse {
27 struct blk_desc *dev_desc;
30 static int raw_part_get_info_by_name(struct blk_desc *dev_desc,
32 struct disk_partition *info)
34 /* strlen("fastboot_raw_partition_") + PART_NAME_LEN + 1 */
35 char env_desc_name[23 + PART_NAME_LEN + 1];
38 const char **parg = argv;
40 /* check for raw partition descriptor */
41 strcpy(env_desc_name, "fastboot_raw_partition_");
42 strlcat(env_desc_name, name, sizeof(env_desc_name));
43 raw_part_desc = strdup(env_get(env_desc_name));
44 if (raw_part_desc == NULL)
48 * parse partition descriptor
50 * <lba_start> <lba_size> [mmcpart <num>]
52 for (; parg < argv + sizeof(argv) / sizeof(*argv); ++parg) {
53 *parg = strsep(&raw_part_desc, " ");
55 pr_err("Invalid number of arguments.\n");
60 info->start = simple_strtoul(argv[0], NULL, 0);
61 info->size = simple_strtoul(argv[1], NULL, 0);
62 info->blksz = dev_desc->blksz;
63 strlcpy((char *)info->name, name, PART_NAME_LEN);
66 if (strcmp(strsep(&raw_part_desc, " "), "mmcpart") == 0) {
67 ulong mmcpart = simple_strtoul(raw_part_desc, NULL, 0);
68 int ret = blk_dselect_hwpart(dev_desc, mmcpart);
78 static int do_get_part_info(struct blk_desc **dev_desc, const char *name,
79 struct disk_partition *info)
83 /* First try partition names on the default device */
84 *dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
86 ret = part_get_info_by_name(*dev_desc, name, info);
90 /* Then try raw partitions */
91 ret = raw_part_get_info_by_name(*dev_desc, name, info);
96 /* Then try dev.hwpart:part */
97 ret = part_get_info_by_dev_and_name_or_num("mmc", name, dev_desc,
102 static int part_get_info_by_name_or_alias(struct blk_desc **dev_desc,
104 struct disk_partition *info)
106 /* strlen("fastboot_partition_alias_") + PART_NAME_LEN + 1 */
107 char env_alias_name[25 + PART_NAME_LEN + 1];
108 char *aliased_part_name;
110 /* check for alias */
111 strlcpy(env_alias_name, "fastboot_partition_alias_", sizeof(env_alias_name));
112 strlcat(env_alias_name, name, sizeof(env_alias_name));
113 aliased_part_name = env_get(env_alias_name);
114 if (aliased_part_name)
115 name = aliased_part_name;
117 return do_get_part_info(dev_desc, name, info);
121 * fb_mmc_blk_write() - Write/erase MMC in chunks of FASTBOOT_MAX_BLK_WRITE
123 * @block_dev: Pointer to block device
124 * @start: First block to write/erase
125 * @blkcnt: Count of blocks
126 * @buffer: Pointer to data buffer for write or NULL for erase
128 static lbaint_t fb_mmc_blk_write(struct blk_desc *block_dev, lbaint_t start,
129 lbaint_t blkcnt, const void *buffer)
131 lbaint_t blk = start;
132 lbaint_t blks_written;
137 for (i = 0; i < blkcnt; i += FASTBOOT_MAX_BLK_WRITE) {
138 cur_blkcnt = min((int)blkcnt - i, FASTBOOT_MAX_BLK_WRITE);
140 if (fastboot_progress_callback)
141 fastboot_progress_callback("writing");
142 blks_written = blk_dwrite(block_dev, blk, cur_blkcnt,
143 buffer + (i * block_dev->blksz));
145 if (fastboot_progress_callback)
146 fastboot_progress_callback("erasing");
147 blks_written = blk_derase(block_dev, blk, cur_blkcnt);
150 blks += blks_written;
155 static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info,
156 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
158 struct fb_mmc_sparse *sparse = info->priv;
159 struct blk_desc *dev_desc = sparse->dev_desc;
161 return fb_mmc_blk_write(dev_desc, blk, blkcnt, buffer);
164 static lbaint_t fb_mmc_sparse_reserve(struct sparse_storage *info,
165 lbaint_t blk, lbaint_t blkcnt)
170 static void write_raw_image(struct blk_desc *dev_desc,
171 struct disk_partition *info, const char *part_name,
172 void *buffer, u32 download_bytes, char *response)
177 /* determine number of blocks to write */
178 blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
179 blkcnt = lldiv(blkcnt, info->blksz);
181 if (blkcnt > info->size) {
182 pr_err("too large for partition: '%s'\n", part_name);
183 fastboot_fail("too large for partition", response);
187 puts("Flashing Raw Image\n");
189 blks = fb_mmc_blk_write(dev_desc, info->start, blkcnt, buffer);
191 if (blks != blkcnt) {
192 pr_err("failed writing to device %d\n", dev_desc->devnum);
193 fastboot_fail("failed writing to device", response);
197 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
199 fastboot_okay(NULL, response);
202 #if defined(CONFIG_FASTBOOT_MMC_BOOT_SUPPORT) || \
203 defined(CONFIG_FASTBOOT_MMC_USER_SUPPORT)
204 static int fb_mmc_erase_mmc_hwpart(struct blk_desc *dev_desc)
208 debug("Start Erasing mmc hwpart[%u]...\n", dev_desc->hwpart);
210 blks = fb_mmc_blk_write(dev_desc, 0, dev_desc->lba, NULL);
212 if (blks != dev_desc->lba) {
213 pr_err("Failed to erase mmc hwpart[%u]\n", dev_desc->hwpart);
217 printf("........ erased %lu bytes from mmc hwpart[%u]\n",
218 dev_desc->lba * dev_desc->blksz, dev_desc->hwpart);
224 #ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT
225 static void fb_mmc_boot_ops(struct blk_desc *dev_desc, void *buffer,
226 int hwpart, u32 buff_sz, char *response)
232 // To operate on EMMC_BOOT1/2 (mmc0boot0/1) we first change the hwpart
233 if (blk_dselect_hwpart(dev_desc, hwpart)) {
234 pr_err("Failed to select hwpart\n");
235 fastboot_fail("Failed to select hwpart", response);
239 if (buffer) { /* flash */
241 /* determine number of blocks to write */
242 blksz = dev_desc->blksz;
243 blkcnt = ((buff_sz + (blksz - 1)) & ~(blksz - 1));
244 blkcnt = lldiv(blkcnt, blksz);
246 if (blkcnt > dev_desc->lba) {
247 pr_err("Image size too large\n");
248 fastboot_fail("Image size too large", response);
252 debug("Start Flashing Image to EMMC_BOOT%d...\n", hwpart);
254 blks = fb_mmc_blk_write(dev_desc, 0, blkcnt, buffer);
256 if (blks != blkcnt) {
257 pr_err("Failed to write EMMC_BOOT%d\n", hwpart);
258 fastboot_fail("Failed to write EMMC_BOOT part",
263 printf("........ wrote %lu bytes to EMMC_BOOT%d\n",
264 blkcnt * blksz, hwpart);
266 if (fb_mmc_erase_mmc_hwpart(dev_desc)) {
267 pr_err("Failed to erase EMMC_BOOT%d\n", hwpart);
268 fastboot_fail("Failed to erase EMMC_BOOT part",
274 fastboot_okay(NULL, response);
278 #ifdef CONFIG_ANDROID_BOOT_IMAGE
280 * Read Android boot image header from boot partition.
282 * @param[in] dev_desc MMC device descriptor
283 * @param[in] info Boot partition info
284 * @param[out] hdr Where to store read boot image header
286 * Return: Boot image header sectors count or 0 on error
288 static lbaint_t fb_mmc_get_boot_header(struct blk_desc *dev_desc,
289 struct disk_partition *info,
290 struct andr_img_hdr *hdr,
293 ulong sector_size; /* boot partition sector size */
294 lbaint_t hdr_sectors; /* boot image header sectors count */
297 /* Calculate boot image sectors count */
298 sector_size = info->blksz;
299 hdr_sectors = DIV_ROUND_UP(sizeof(struct andr_img_hdr), sector_size);
300 if (hdr_sectors == 0) {
301 pr_err("invalid number of boot sectors: 0\n");
302 fastboot_fail("invalid number of boot sectors: 0", response);
306 /* Read the boot image header */
307 res = blk_dread(dev_desc, info->start, hdr_sectors, (void *)hdr);
308 if (res != hdr_sectors) {
309 pr_err("cannot read header from boot partition\n");
310 fastboot_fail("cannot read header from boot partition",
315 /* Check boot header magic string */
316 res = android_image_check_header(hdr);
318 pr_err("bad boot image magic\n");
319 fastboot_fail("boot partition not initialized", response);
327 * Write downloaded zImage to boot partition and repack it properly.
329 * @param dev_desc MMC device descriptor
330 * @param download_buffer Address to fastboot buffer with zImage in it
331 * @param download_bytes Size of fastboot buffer, in bytes
333 * Return: 0 on success or -1 on error
335 static int fb_mmc_update_zimage(struct blk_desc *dev_desc,
336 void *download_buffer,
340 uintptr_t hdr_addr; /* boot image header address */
341 struct andr_img_hdr *hdr; /* boot image header */
342 lbaint_t hdr_sectors; /* boot image header sectors */
344 u32 ramdisk_sector_start;
346 u32 kernel_sector_start;
348 u32 sectors_per_page;
349 struct disk_partition info;
352 puts("Flashing zImage\n");
354 /* Get boot partition info */
355 res = part_get_info_by_name(dev_desc, BOOT_PARTITION_NAME, &info);
357 pr_err("cannot find boot partition\n");
358 fastboot_fail("cannot find boot partition", response);
362 /* Put boot image header in fastboot buffer after downloaded zImage */
363 hdr_addr = (uintptr_t)download_buffer + ALIGN(download_bytes, PAGE_SIZE);
364 hdr = (struct andr_img_hdr *)hdr_addr;
366 /* Read boot image header */
367 hdr_sectors = fb_mmc_get_boot_header(dev_desc, &info, hdr, response);
368 if (hdr_sectors == 0) {
369 pr_err("unable to read boot image header\n");
370 fastboot_fail("unable to read boot image header", response);
374 /* Check if boot image has second stage in it (we don't support it) */
375 if (hdr->second_size > 0) {
376 pr_err("moving second stage is not supported yet\n");
377 fastboot_fail("moving second stage is not supported yet",
382 /* Extract ramdisk location */
383 sectors_per_page = hdr->page_size / info.blksz;
384 ramdisk_sector_start = info.start + sectors_per_page;
385 ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
387 ramdisk_sectors = DIV_ROUND_UP(hdr->ramdisk_size, hdr->page_size) *
390 /* Read ramdisk and put it in fastboot buffer after boot image header */
391 ramdisk_buffer = (u8 *)hdr + (hdr_sectors * info.blksz);
392 res = blk_dread(dev_desc, ramdisk_sector_start, ramdisk_sectors,
394 if (res != ramdisk_sectors) {
395 pr_err("cannot read ramdisk from boot partition\n");
396 fastboot_fail("cannot read ramdisk from boot partition",
401 /* Write new kernel size to boot image header */
402 hdr->kernel_size = download_bytes;
403 res = blk_dwrite(dev_desc, info.start, hdr_sectors, (void *)hdr);
405 pr_err("cannot writeback boot image header\n");
406 fastboot_fail("cannot write back boot image header", response);
410 /* Write the new downloaded kernel */
411 kernel_sector_start = info.start + sectors_per_page;
412 kernel_sectors = DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
414 res = blk_dwrite(dev_desc, kernel_sector_start, kernel_sectors,
417 pr_err("cannot write new kernel\n");
418 fastboot_fail("cannot write new kernel", response);
422 /* Write the saved ramdisk back */
423 ramdisk_sector_start = info.start + sectors_per_page;
424 ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
426 res = blk_dwrite(dev_desc, ramdisk_sector_start, ramdisk_sectors,
429 pr_err("cannot write back original ramdisk\n");
430 fastboot_fail("cannot write back original ramdisk", response);
434 puts("........ zImage was updated in boot partition\n");
435 fastboot_okay(NULL, response);
441 * fastboot_mmc_get_part_info() - Lookup eMMC partion by name
443 * @part_name: Named partition to lookup
444 * @dev_desc: Pointer to returned blk_desc pointer
445 * @part_info: Pointer to returned struct disk_partition
446 * @response: Pointer to fastboot response buffer
448 int fastboot_mmc_get_part_info(const char *part_name,
449 struct blk_desc **dev_desc,
450 struct disk_partition *part_info, char *response)
454 if (!part_name || !strcmp(part_name, "")) {
455 fastboot_fail("partition not given", response);
459 ret = part_get_info_by_name_or_alias(dev_desc, part_name, part_info);
464 fastboot_fail("invalid partition or device", response);
467 fastboot_fail("no such device", response);
470 fastboot_fail("no such partition", response);
472 case -EPROTONOSUPPORT:
473 fastboot_fail("unknown partition table type", response);
476 fastboot_fail("unanticipated error", response);
484 static struct blk_desc *fastboot_mmc_get_dev(char *response)
486 struct blk_desc *ret = blk_get_dev("mmc",
487 CONFIG_FASTBOOT_FLASH_MMC_DEV);
489 if (!ret || ret->type == DEV_TYPE_UNKNOWN) {
490 pr_err("invalid mmc device\n");
491 fastboot_fail("invalid mmc device", response);
498 * fastboot_mmc_flash_write() - Write image to eMMC for fastboot
500 * @cmd: Named partition to write image to
501 * @download_buffer: Pointer to image data
502 * @download_bytes: Size of image data
503 * @response: Pointer to fastboot response buffer
505 void fastboot_mmc_flash_write(const char *cmd, void *download_buffer,
506 u32 download_bytes, char *response)
508 struct blk_desc *dev_desc;
509 struct disk_partition info = {0};
511 #ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT
512 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) {
513 dev_desc = fastboot_mmc_get_dev(response);
515 fb_mmc_boot_ops(dev_desc, download_buffer, 1,
516 download_bytes, response);
519 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT2_NAME) == 0) {
520 dev_desc = fastboot_mmc_get_dev(response);
522 fb_mmc_boot_ops(dev_desc, download_buffer, 2,
523 download_bytes, response);
528 #if CONFIG_IS_ENABLED(EFI_PARTITION)
529 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
530 dev_desc = fastboot_mmc_get_dev(response);
534 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
536 if (is_valid_gpt_buf(dev_desc, download_buffer)) {
537 printf("%s: invalid GPT - refusing to write to flash\n",
539 fastboot_fail("invalid GPT partition", response);
542 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
543 printf("%s: writing GPT partitions failed\n", __func__);
544 fastboot_fail("writing GPT partitions failed",
549 printf("........ success\n");
550 fastboot_okay(NULL, response);
555 #if CONFIG_IS_ENABLED(DOS_PARTITION)
556 if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) {
557 dev_desc = fastboot_mmc_get_dev(response);
561 printf("%s: updating MBR\n", __func__);
562 if (is_valid_dos_buf(download_buffer)) {
563 printf("%s: invalid MBR - refusing to write to flash\n",
565 fastboot_fail("invalid MBR partition", response);
568 if (write_mbr_sector(dev_desc, download_buffer)) {
569 printf("%s: writing MBR partition failed\n", __func__);
570 fastboot_fail("writing MBR partition failed",
575 printf("........ success\n");
576 fastboot_okay(NULL, response);
581 #ifdef CONFIG_ANDROID_BOOT_IMAGE
582 if (strncasecmp(cmd, "zimage", 6) == 0) {
583 dev_desc = fastboot_mmc_get_dev(response);
585 fb_mmc_update_zimage(dev_desc, download_buffer,
586 download_bytes, response);
591 #if CONFIG_IS_ENABLED(FASTBOOT_MMC_USER_SUPPORT)
592 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) {
593 dev_desc = fastboot_mmc_get_dev(response);
597 strlcpy((char *)&info.name, cmd, sizeof(info.name));
598 info.size = dev_desc->lba;
599 info.blksz = dev_desc->blksz;
604 fastboot_mmc_get_part_info(cmd, &dev_desc, &info, response) < 0)
607 if (is_sparse_image(download_buffer)) {
608 struct fb_mmc_sparse sparse_priv;
609 struct sparse_storage sparse;
612 sparse_priv.dev_desc = dev_desc;
614 sparse.blksz = info.blksz;
615 sparse.start = info.start;
616 sparse.size = info.size;
617 sparse.write = fb_mmc_sparse_write;
618 sparse.reserve = fb_mmc_sparse_reserve;
619 sparse.mssg = fastboot_fail;
621 printf("Flashing sparse image at offset " LBAFU "\n",
624 sparse.priv = &sparse_priv;
625 err = write_sparse_image(&sparse, cmd, download_buffer,
628 fastboot_okay(NULL, response);
630 write_raw_image(dev_desc, &info, cmd, download_buffer,
631 download_bytes, response);
636 * fastboot_mmc_flash_erase() - Erase eMMC for fastboot
638 * @cmd: Named partition to erase
639 * @response: Pointer to fastboot response buffer
641 void fastboot_mmc_erase(const char *cmd, char *response)
643 struct blk_desc *dev_desc;
644 struct disk_partition info;
645 lbaint_t blks, blks_start, blks_size, grp_size;
646 struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
648 #ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT
649 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) {
650 /* erase EMMC boot1 */
651 dev_desc = fastboot_mmc_get_dev(response);
653 fb_mmc_boot_ops(dev_desc, NULL, 1, 0, response);
656 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT2_NAME) == 0) {
657 /* erase EMMC boot2 */
658 dev_desc = fastboot_mmc_get_dev(response);
660 fb_mmc_boot_ops(dev_desc, NULL, 2, 0, response);
665 #ifdef CONFIG_FASTBOOT_MMC_USER_SUPPORT
666 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) {
667 /* erase EMMC userdata */
668 dev_desc = fastboot_mmc_get_dev(response);
672 if (fb_mmc_erase_mmc_hwpart(dev_desc))
673 fastboot_fail("Failed to erase EMMC_USER", response);
675 fastboot_okay(NULL, response);
680 if (fastboot_mmc_get_part_info(cmd, &dev_desc, &info, response) < 0)
683 /* Align blocks to erase group size to avoid erasing other partitions */
684 grp_size = mmc->erase_grp_size;
685 blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
686 if (info.size >= grp_size)
687 blks_size = (info.size - (blks_start - info.start)) &
692 printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
693 blks_start, blks_start + blks_size);
695 blks = fb_mmc_blk_write(dev_desc, blks_start, blks_size, NULL);
697 if (blks != blks_size) {
698 pr_err("failed erasing from device %d\n", dev_desc->devnum);
699 fastboot_fail("failed erasing from device", response);
703 printf("........ erased " LBAFU " bytes from '%s'\n",
704 blks_size * info.blksz, cmd);
705 fastboot_okay(NULL, response);