1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2014 Broadcom Corporation.
11 #include <fastboot-internal.h>
14 #include <image-sparse.h>
20 #include <linux/compat.h>
21 #include <android_image.h>
23 #define FASTBOOT_MAX_BLK_WRITE 16384
25 #define BOOT_PARTITION_NAME "boot"
27 struct fb_mmc_sparse {
28 struct blk_desc *dev_desc;
31 static int raw_part_get_info_by_name(struct blk_desc *dev_desc,
33 struct disk_partition *info)
35 /* strlen("fastboot_raw_partition_") + PART_NAME_LEN + 1 */
36 char env_desc_name[23 + PART_NAME_LEN + 1];
39 const char **parg = argv;
41 /* check for raw partition descriptor */
42 strcpy(env_desc_name, "fastboot_raw_partition_");
43 strlcat(env_desc_name, name, sizeof(env_desc_name));
44 raw_part_desc = strdup(env_get(env_desc_name));
45 if (raw_part_desc == NULL)
49 * parse partition descriptor
51 * <lba_start> <lba_size> [mmcpart <num>]
53 for (; parg < argv + sizeof(argv) / sizeof(*argv); ++parg) {
54 *parg = strsep(&raw_part_desc, " ");
56 pr_err("Invalid number of arguments.\n");
61 info->start = simple_strtoul(argv[0], NULL, 0);
62 info->size = simple_strtoul(argv[1], NULL, 0);
63 info->blksz = dev_desc->blksz;
64 strlcpy((char *)info->name, name, PART_NAME_LEN);
67 if (strcmp(strsep(&raw_part_desc, " "), "mmcpart") == 0) {
68 ulong mmcpart = simple_strtoul(raw_part_desc, NULL, 0);
69 int ret = blk_dselect_hwpart(dev_desc, mmcpart);
79 static int do_get_part_info(struct blk_desc **dev_desc, const char *name,
80 struct disk_partition *info)
84 /* First try partition names on the default device */
85 *dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
87 ret = part_get_info_by_name(*dev_desc, name, info);
91 /* Then try raw partitions */
92 ret = raw_part_get_info_by_name(*dev_desc, name, info);
97 /* Then try dev.hwpart:part */
98 ret = part_get_info_by_dev_and_name_or_num("mmc", name, dev_desc,
103 static int part_get_info_by_name_or_alias(struct blk_desc **dev_desc,
105 struct disk_partition *info)
107 /* strlen("fastboot_partition_alias_") + PART_NAME_LEN + 1 */
108 char env_alias_name[25 + PART_NAME_LEN + 1];
109 char *aliased_part_name;
111 /* check for alias */
112 strlcpy(env_alias_name, "fastboot_partition_alias_", sizeof(env_alias_name));
113 strlcat(env_alias_name, name, sizeof(env_alias_name));
114 aliased_part_name = env_get(env_alias_name);
115 if (aliased_part_name)
116 name = aliased_part_name;
118 return do_get_part_info(dev_desc, name, info);
122 * fb_mmc_blk_write() - Write/erase MMC in chunks of FASTBOOT_MAX_BLK_WRITE
124 * @block_dev: Pointer to block device
125 * @start: First block to write/erase
126 * @blkcnt: Count of blocks
127 * @buffer: Pointer to data buffer for write or NULL for erase
129 static lbaint_t fb_mmc_blk_write(struct blk_desc *block_dev, lbaint_t start,
130 lbaint_t blkcnt, const void *buffer)
132 lbaint_t blk = start;
133 lbaint_t blks_written;
138 for (i = 0; i < blkcnt; i += FASTBOOT_MAX_BLK_WRITE) {
139 cur_blkcnt = min((int)blkcnt - i, FASTBOOT_MAX_BLK_WRITE);
141 if (fastboot_progress_callback)
142 fastboot_progress_callback("writing");
143 blks_written = blk_dwrite(block_dev, blk, cur_blkcnt,
144 buffer + (i * block_dev->blksz));
146 if (fastboot_progress_callback)
147 fastboot_progress_callback("erasing");
148 blks_written = blk_derase(block_dev, blk, cur_blkcnt);
151 blks += blks_written;
156 static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info,
157 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
159 struct fb_mmc_sparse *sparse = info->priv;
160 struct blk_desc *dev_desc = sparse->dev_desc;
162 return fb_mmc_blk_write(dev_desc, blk, blkcnt, buffer);
165 static lbaint_t fb_mmc_sparse_reserve(struct sparse_storage *info,
166 lbaint_t blk, lbaint_t blkcnt)
171 static void write_raw_image(struct blk_desc *dev_desc,
172 struct disk_partition *info, const char *part_name,
173 void *buffer, u32 download_bytes, char *response)
178 /* determine number of blocks to write */
179 blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
180 blkcnt = lldiv(blkcnt, info->blksz);
182 if (blkcnt > info->size) {
183 pr_err("too large for partition: '%s'\n", part_name);
184 fastboot_fail("too large for partition", response);
188 puts("Flashing Raw Image\n");
190 blks = fb_mmc_blk_write(dev_desc, info->start, blkcnt, buffer);
192 if (blks != blkcnt) {
193 pr_err("failed writing to device %d\n", dev_desc->devnum);
194 fastboot_fail("failed writing to device", response);
198 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
200 fastboot_okay(NULL, response);
203 #if defined(CONFIG_FASTBOOT_MMC_BOOT_SUPPORT) || \
204 defined(CONFIG_FASTBOOT_MMC_USER_SUPPORT)
205 static int fb_mmc_erase_mmc_hwpart(struct blk_desc *dev_desc)
209 debug("Start Erasing mmc hwpart[%u]...\n", dev_desc->hwpart);
211 blks = fb_mmc_blk_write(dev_desc, 0, dev_desc->lba, NULL);
213 if (blks != dev_desc->lba) {
214 pr_err("Failed to erase mmc hwpart[%u]\n", dev_desc->hwpart);
218 printf("........ erased %lu bytes from mmc hwpart[%u]\n",
219 dev_desc->lba * dev_desc->blksz, dev_desc->hwpart);
225 #ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT
226 static void fb_mmc_boot_ops(struct blk_desc *dev_desc, void *buffer,
227 int hwpart, u32 buff_sz, char *response)
233 // To operate on EMMC_BOOT1/2 (mmc0boot0/1) we first change the hwpart
234 if (blk_dselect_hwpart(dev_desc, hwpart)) {
235 pr_err("Failed to select hwpart\n");
236 fastboot_fail("Failed to select hwpart", response);
240 if (buffer) { /* flash */
242 /* determine number of blocks to write */
243 blksz = dev_desc->blksz;
244 blkcnt = ((buff_sz + (blksz - 1)) & ~(blksz - 1));
245 blkcnt = lldiv(blkcnt, blksz);
247 if (blkcnt > dev_desc->lba) {
248 pr_err("Image size too large\n");
249 fastboot_fail("Image size too large", response);
253 debug("Start Flashing Image to EMMC_BOOT%d...\n", hwpart);
255 blks = fb_mmc_blk_write(dev_desc, 0, blkcnt, buffer);
257 if (blks != blkcnt) {
258 pr_err("Failed to write EMMC_BOOT%d\n", hwpart);
259 fastboot_fail("Failed to write EMMC_BOOT part",
264 printf("........ wrote %lu bytes to EMMC_BOOT%d\n",
265 blkcnt * blksz, hwpart);
267 if (fb_mmc_erase_mmc_hwpart(dev_desc)) {
268 pr_err("Failed to erase EMMC_BOOT%d\n", hwpart);
269 fastboot_fail("Failed to erase EMMC_BOOT part",
275 fastboot_okay(NULL, response);
279 #ifdef CONFIG_ANDROID_BOOT_IMAGE
281 * Read Android boot image header from boot partition.
283 * @param[in] dev_desc MMC device descriptor
284 * @param[in] info Boot partition info
285 * @param[out] hdr Where to store read boot image header
287 * Return: Boot image header sectors count or 0 on error
289 static lbaint_t fb_mmc_get_boot_header(struct blk_desc *dev_desc,
290 struct disk_partition *info,
291 struct andr_img_hdr *hdr,
294 ulong sector_size; /* boot partition sector size */
295 lbaint_t hdr_sectors; /* boot image header sectors count */
298 /* Calculate boot image sectors count */
299 sector_size = info->blksz;
300 hdr_sectors = DIV_ROUND_UP(sizeof(struct andr_img_hdr), sector_size);
301 if (hdr_sectors == 0) {
302 pr_err("invalid number of boot sectors: 0\n");
303 fastboot_fail("invalid number of boot sectors: 0", response);
307 /* Read the boot image header */
308 res = blk_dread(dev_desc, info->start, hdr_sectors, (void *)hdr);
309 if (res != hdr_sectors) {
310 pr_err("cannot read header from boot partition\n");
311 fastboot_fail("cannot read header from boot partition",
316 /* Check boot header magic string */
317 res = android_image_check_header(hdr);
319 pr_err("bad boot image magic\n");
320 fastboot_fail("boot partition not initialized", response);
328 * Write downloaded zImage to boot partition and repack it properly.
330 * @param dev_desc MMC device descriptor
331 * @param download_buffer Address to fastboot buffer with zImage in it
332 * @param download_bytes Size of fastboot buffer, in bytes
334 * Return: 0 on success or -1 on error
336 static int fb_mmc_update_zimage(struct blk_desc *dev_desc,
337 void *download_buffer,
341 uintptr_t hdr_addr; /* boot image header address */
342 struct andr_img_hdr *hdr; /* boot image header */
343 lbaint_t hdr_sectors; /* boot image header sectors */
345 u32 ramdisk_sector_start;
347 u32 kernel_sector_start;
349 u32 sectors_per_page;
350 struct disk_partition info;
353 puts("Flashing zImage\n");
355 /* Get boot partition info */
356 res = part_get_info_by_name(dev_desc, BOOT_PARTITION_NAME, &info);
358 pr_err("cannot find boot partition\n");
359 fastboot_fail("cannot find boot partition", response);
363 /* Put boot image header in fastboot buffer after downloaded zImage */
364 hdr_addr = (uintptr_t)download_buffer + ALIGN(download_bytes, PAGE_SIZE);
365 hdr = (struct andr_img_hdr *)hdr_addr;
367 /* Read boot image header */
368 hdr_sectors = fb_mmc_get_boot_header(dev_desc, &info, hdr, response);
369 if (hdr_sectors == 0) {
370 pr_err("unable to read boot image header\n");
371 fastboot_fail("unable to read boot image header", response);
375 /* Check if boot image has second stage in it (we don't support it) */
376 if (hdr->second_size > 0) {
377 pr_err("moving second stage is not supported yet\n");
378 fastboot_fail("moving second stage is not supported yet",
383 /* Extract ramdisk location */
384 sectors_per_page = hdr->page_size / info.blksz;
385 ramdisk_sector_start = info.start + sectors_per_page;
386 ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
388 ramdisk_sectors = DIV_ROUND_UP(hdr->ramdisk_size, hdr->page_size) *
391 /* Read ramdisk and put it in fastboot buffer after boot image header */
392 ramdisk_buffer = (u8 *)hdr + (hdr_sectors * info.blksz);
393 res = blk_dread(dev_desc, ramdisk_sector_start, ramdisk_sectors,
395 if (res != ramdisk_sectors) {
396 pr_err("cannot read ramdisk from boot partition\n");
397 fastboot_fail("cannot read ramdisk from boot partition",
402 /* Write new kernel size to boot image header */
403 hdr->kernel_size = download_bytes;
404 res = blk_dwrite(dev_desc, info.start, hdr_sectors, (void *)hdr);
406 pr_err("cannot writeback boot image header\n");
407 fastboot_fail("cannot write back boot image header", response);
411 /* Write the new downloaded kernel */
412 kernel_sector_start = info.start + sectors_per_page;
413 kernel_sectors = DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
415 res = blk_dwrite(dev_desc, kernel_sector_start, kernel_sectors,
418 pr_err("cannot write new kernel\n");
419 fastboot_fail("cannot write new kernel", response);
423 /* Write the saved ramdisk back */
424 ramdisk_sector_start = info.start + sectors_per_page;
425 ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
427 res = blk_dwrite(dev_desc, ramdisk_sector_start, ramdisk_sectors,
430 pr_err("cannot write back original ramdisk\n");
431 fastboot_fail("cannot write back original ramdisk", response);
435 puts("........ zImage was updated in boot partition\n");
436 fastboot_okay(NULL, response);
442 * fastboot_mmc_get_part_info() - Lookup eMMC partion by name
444 * @part_name: Named partition to lookup
445 * @dev_desc: Pointer to returned blk_desc pointer
446 * @part_info: Pointer to returned struct disk_partition
447 * @response: Pointer to fastboot response buffer
449 int fastboot_mmc_get_part_info(const char *part_name,
450 struct blk_desc **dev_desc,
451 struct disk_partition *part_info, char *response)
455 if (!part_name || !strcmp(part_name, "")) {
456 fastboot_fail("partition not given", response);
460 ret = part_get_info_by_name_or_alias(dev_desc, part_name, part_info);
465 fastboot_fail("invalid partition or device", response);
468 fastboot_fail("no such device", response);
471 fastboot_fail("no such partition", response);
473 case -EPROTONOSUPPORT:
474 fastboot_fail("unknown partition table type", response);
477 fastboot_fail("unanticipated error", response);
485 static struct blk_desc *fastboot_mmc_get_dev(char *response)
487 struct blk_desc *ret = blk_get_dev("mmc",
488 CONFIG_FASTBOOT_FLASH_MMC_DEV);
490 if (!ret || ret->type == DEV_TYPE_UNKNOWN) {
491 pr_err("invalid mmc device\n");
492 fastboot_fail("invalid mmc device", response);
499 * fastboot_mmc_flash_write() - Write image to eMMC for fastboot
501 * @cmd: Named partition to write image to
502 * @download_buffer: Pointer to image data
503 * @download_bytes: Size of image data
504 * @response: Pointer to fastboot response buffer
506 void fastboot_mmc_flash_write(const char *cmd, void *download_buffer,
507 u32 download_bytes, char *response)
509 struct blk_desc *dev_desc;
510 struct disk_partition info = {0};
512 #ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT
513 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) {
514 dev_desc = fastboot_mmc_get_dev(response);
516 fb_mmc_boot_ops(dev_desc, download_buffer, 1,
517 download_bytes, response);
520 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT2_NAME) == 0) {
521 dev_desc = fastboot_mmc_get_dev(response);
523 fb_mmc_boot_ops(dev_desc, download_buffer, 2,
524 download_bytes, response);
529 #if CONFIG_IS_ENABLED(EFI_PARTITION)
530 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
531 dev_desc = fastboot_mmc_get_dev(response);
535 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
537 if (is_valid_gpt_buf(dev_desc, download_buffer)) {
538 printf("%s: invalid GPT - refusing to write to flash\n",
540 fastboot_fail("invalid GPT partition", response);
543 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
544 printf("%s: writing GPT partitions failed\n", __func__);
545 fastboot_fail("writing GPT partitions failed",
550 printf("........ success\n");
551 fastboot_okay(NULL, response);
556 #if CONFIG_IS_ENABLED(DOS_PARTITION)
557 if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) {
558 dev_desc = fastboot_mmc_get_dev(response);
562 printf("%s: updating MBR\n", __func__);
563 if (is_valid_dos_buf(download_buffer)) {
564 printf("%s: invalid MBR - refusing to write to flash\n",
566 fastboot_fail("invalid MBR partition", response);
569 if (write_mbr_sector(dev_desc, download_buffer)) {
570 printf("%s: writing MBR partition failed\n", __func__);
571 fastboot_fail("writing MBR partition failed",
576 printf("........ success\n");
577 fastboot_okay(NULL, response);
582 #ifdef CONFIG_ANDROID_BOOT_IMAGE
583 if (strncasecmp(cmd, "zimage", 6) == 0) {
584 dev_desc = fastboot_mmc_get_dev(response);
586 fb_mmc_update_zimage(dev_desc, download_buffer,
587 download_bytes, response);
592 #if CONFIG_IS_ENABLED(FASTBOOT_MMC_USER_SUPPORT)
593 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) {
594 dev_desc = fastboot_mmc_get_dev(response);
598 strlcpy((char *)&info.name, cmd, sizeof(info.name));
599 info.size = dev_desc->lba;
600 info.blksz = dev_desc->blksz;
605 fastboot_mmc_get_part_info(cmd, &dev_desc, &info, response) < 0)
608 if (is_sparse_image(download_buffer)) {
609 struct fb_mmc_sparse sparse_priv;
610 struct sparse_storage sparse;
613 sparse_priv.dev_desc = dev_desc;
615 sparse.blksz = info.blksz;
616 sparse.start = info.start;
617 sparse.size = info.size;
618 sparse.write = fb_mmc_sparse_write;
619 sparse.reserve = fb_mmc_sparse_reserve;
620 sparse.mssg = fastboot_fail;
622 printf("Flashing sparse image at offset " LBAFU "\n",
625 sparse.priv = &sparse_priv;
626 err = write_sparse_image(&sparse, cmd, download_buffer,
629 fastboot_okay(NULL, response);
631 write_raw_image(dev_desc, &info, cmd, download_buffer,
632 download_bytes, response);
637 * fastboot_mmc_flash_erase() - Erase eMMC for fastboot
639 * @cmd: Named partition to erase
640 * @response: Pointer to fastboot response buffer
642 void fastboot_mmc_erase(const char *cmd, char *response)
644 struct blk_desc *dev_desc;
645 struct disk_partition info;
646 lbaint_t blks, blks_start, blks_size, grp_size;
647 struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
649 #ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT
650 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) {
651 /* erase EMMC boot1 */
652 dev_desc = fastboot_mmc_get_dev(response);
654 fb_mmc_boot_ops(dev_desc, NULL, 1, 0, response);
657 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT2_NAME) == 0) {
658 /* erase EMMC boot2 */
659 dev_desc = fastboot_mmc_get_dev(response);
661 fb_mmc_boot_ops(dev_desc, NULL, 2, 0, response);
666 #ifdef CONFIG_FASTBOOT_MMC_USER_SUPPORT
667 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) {
668 /* erase EMMC userdata */
669 dev_desc = fastboot_mmc_get_dev(response);
673 if (fb_mmc_erase_mmc_hwpart(dev_desc))
674 fastboot_fail("Failed to erase EMMC_USER", response);
676 fastboot_okay(NULL, response);
681 if (fastboot_mmc_get_part_info(cmd, &dev_desc, &info, response) < 0)
684 /* Align blocks to erase group size to avoid erasing other partitions */
685 grp_size = mmc->erase_grp_size;
686 blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
687 if (info.size >= grp_size)
688 blks_size = (info.size - (blks_start - info.start)) &
693 printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
694 blks_start, blks_start + blks_size);
696 blks = fb_mmc_blk_write(dev_desc, blks_start, blks_size, NULL);
698 if (blks != blks_size) {
699 pr_err("failed erasing from device %d\n", dev_desc->devnum);
700 fastboot_fail("failed erasing from device", response);
704 printf("........ erased " LBAFU " bytes from '%s'\n",
705 blks_size * info.blksz, cmd);
706 fastboot_okay(NULL, response);