1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2014 Broadcom Corporation.
10 #include <fastboot-internal.h>
12 #include <image-sparse.h>
16 #include <linux/compat.h>
17 #include <android_image.h>
19 #define FASTBOOT_MAX_BLK_WRITE 16384
21 #define BOOT_PARTITION_NAME "boot"
23 struct fb_mmc_sparse {
24 struct blk_desc *dev_desc;
27 static int part_get_info_by_name_or_alias(struct blk_desc *dev_desc,
28 const char *name, disk_partition_t *info)
32 ret = part_get_info_by_name(dev_desc, name, info);
34 /* strlen("fastboot_partition_alias_") + PART_NAME_LEN + 1 */
35 char env_alias_name[25 + PART_NAME_LEN + 1];
36 char *aliased_part_name;
39 strcpy(env_alias_name, "fastboot_partition_alias_");
40 strncat(env_alias_name, name, PART_NAME_LEN);
41 aliased_part_name = env_get(env_alias_name);
42 if (aliased_part_name != NULL)
43 ret = part_get_info_by_name(dev_desc,
44 aliased_part_name, info);
50 * fb_mmc_blk_write() - Write/erase MMC in chunks of FASTBOOT_MAX_BLK_WRITE
52 * @block_dev: Pointer to block device
53 * @start: First block to write/erase
54 * @blkcnt: Count of blocks
55 * @buffer: Pointer to data buffer for write or NULL for erase
57 static lbaint_t fb_mmc_blk_write(struct blk_desc *block_dev, lbaint_t start,
58 lbaint_t blkcnt, const void *buffer)
61 lbaint_t blks_written;
66 for (i = 0; i < blkcnt; i += FASTBOOT_MAX_BLK_WRITE) {
67 cur_blkcnt = min((int)blkcnt - i, FASTBOOT_MAX_BLK_WRITE);
69 if (fastboot_progress_callback)
70 fastboot_progress_callback("writing");
71 blks_written = blk_dwrite(block_dev, blk, cur_blkcnt,
72 buffer + (i * block_dev->blksz));
74 if (fastboot_progress_callback)
75 fastboot_progress_callback("erasing");
76 blks_written = blk_derase(block_dev, blk, cur_blkcnt);
84 static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info,
85 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
87 struct fb_mmc_sparse *sparse = info->priv;
88 struct blk_desc *dev_desc = sparse->dev_desc;
90 return fb_mmc_blk_write(dev_desc, blk, blkcnt, buffer);
93 static lbaint_t fb_mmc_sparse_reserve(struct sparse_storage *info,
94 lbaint_t blk, lbaint_t blkcnt)
99 static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info,
100 const char *part_name, void *buffer,
101 u32 download_bytes, char *response)
106 /* determine number of blocks to write */
107 blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
108 blkcnt = lldiv(blkcnt, info->blksz);
110 if (blkcnt > info->size) {
111 pr_err("too large for partition: '%s'\n", part_name);
112 fastboot_fail("too large for partition", response);
116 puts("Flashing Raw Image\n");
118 blks = fb_mmc_blk_write(dev_desc, info->start, blkcnt, buffer);
120 if (blks != blkcnt) {
121 pr_err("failed writing to device %d\n", dev_desc->devnum);
122 fastboot_fail("failed writing to device", response);
126 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
128 fastboot_okay(NULL, response);
131 #ifdef CONFIG_ANDROID_BOOT_IMAGE
133 * Read Android boot image header from boot partition.
135 * @param[in] dev_desc MMC device descriptor
136 * @param[in] info Boot partition info
137 * @param[out] hdr Where to store read boot image header
139 * @return Boot image header sectors count or 0 on error
141 static lbaint_t fb_mmc_get_boot_header(struct blk_desc *dev_desc,
142 disk_partition_t *info,
143 struct andr_img_hdr *hdr,
146 ulong sector_size; /* boot partition sector size */
147 lbaint_t hdr_sectors; /* boot image header sectors count */
150 /* Calculate boot image sectors count */
151 sector_size = info->blksz;
152 hdr_sectors = DIV_ROUND_UP(sizeof(struct andr_img_hdr), sector_size);
153 if (hdr_sectors == 0) {
154 pr_err("invalid number of boot sectors: 0\n");
155 fastboot_fail("invalid number of boot sectors: 0", response);
159 /* Read the boot image header */
160 res = blk_dread(dev_desc, info->start, hdr_sectors, (void *)hdr);
161 if (res != hdr_sectors) {
162 pr_err("cannot read header from boot partition\n");
163 fastboot_fail("cannot read header from boot partition",
168 /* Check boot header magic string */
169 res = android_image_check_header(hdr);
171 pr_err("bad boot image magic\n");
172 fastboot_fail("boot partition not initialized", response);
180 * Write downloaded zImage to boot partition and repack it properly.
182 * @param dev_desc MMC device descriptor
183 * @param download_buffer Address to fastboot buffer with zImage in it
184 * @param download_bytes Size of fastboot buffer, in bytes
186 * @return 0 on success or -1 on error
188 static int fb_mmc_update_zimage(struct blk_desc *dev_desc,
189 void *download_buffer,
193 uintptr_t hdr_addr; /* boot image header address */
194 struct andr_img_hdr *hdr; /* boot image header */
195 lbaint_t hdr_sectors; /* boot image header sectors */
197 u32 ramdisk_sector_start;
199 u32 kernel_sector_start;
201 u32 sectors_per_page;
202 disk_partition_t info;
205 puts("Flashing zImage\n");
207 /* Get boot partition info */
208 res = part_get_info_by_name(dev_desc, BOOT_PARTITION_NAME, &info);
210 pr_err("cannot find boot partition\n");
211 fastboot_fail("cannot find boot partition", response);
215 /* Put boot image header in fastboot buffer after downloaded zImage */
216 hdr_addr = (uintptr_t)download_buffer + ALIGN(download_bytes, PAGE_SIZE);
217 hdr = (struct andr_img_hdr *)hdr_addr;
219 /* Read boot image header */
220 hdr_sectors = fb_mmc_get_boot_header(dev_desc, &info, hdr, response);
221 if (hdr_sectors == 0) {
222 pr_err("unable to read boot image header\n");
223 fastboot_fail("unable to read boot image header", response);
227 /* Check if boot image has second stage in it (we don't support it) */
228 if (hdr->second_size > 0) {
229 pr_err("moving second stage is not supported yet\n");
230 fastboot_fail("moving second stage is not supported yet",
235 /* Extract ramdisk location */
236 sectors_per_page = hdr->page_size / info.blksz;
237 ramdisk_sector_start = info.start + sectors_per_page;
238 ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
240 ramdisk_sectors = DIV_ROUND_UP(hdr->ramdisk_size, hdr->page_size) *
243 /* Read ramdisk and put it in fastboot buffer after boot image header */
244 ramdisk_buffer = (u8 *)hdr + (hdr_sectors * info.blksz);
245 res = blk_dread(dev_desc, ramdisk_sector_start, ramdisk_sectors,
247 if (res != ramdisk_sectors) {
248 pr_err("cannot read ramdisk from boot partition\n");
249 fastboot_fail("cannot read ramdisk from boot partition",
254 /* Write new kernel size to boot image header */
255 hdr->kernel_size = download_bytes;
256 res = blk_dwrite(dev_desc, info.start, hdr_sectors, (void *)hdr);
258 pr_err("cannot writeback boot image header\n");
259 fastboot_fail("cannot write back boot image header", response);
263 /* Write the new downloaded kernel */
264 kernel_sector_start = info.start + sectors_per_page;
265 kernel_sectors = DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
267 res = blk_dwrite(dev_desc, kernel_sector_start, kernel_sectors,
270 pr_err("cannot write new kernel\n");
271 fastboot_fail("cannot write new kernel", response);
275 /* Write the saved ramdisk back */
276 ramdisk_sector_start = info.start + sectors_per_page;
277 ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
279 res = blk_dwrite(dev_desc, ramdisk_sector_start, ramdisk_sectors,
282 pr_err("cannot write back original ramdisk\n");
283 fastboot_fail("cannot write back original ramdisk", response);
287 puts("........ zImage was updated in boot partition\n");
288 fastboot_okay(NULL, response);
294 * fastboot_mmc_get_part_info() - Lookup eMMC partion by name
296 * @part_name: Named partition to lookup
297 * @dev_desc: Pointer to returned blk_desc pointer
298 * @part_info: Pointer to returned disk_partition_t
299 * @response: Pointer to fastboot response buffer
301 int fastboot_mmc_get_part_info(const char *part_name,
302 struct blk_desc **dev_desc,
303 disk_partition_t *part_info, char *response)
307 *dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
309 fastboot_fail("block device not found", response);
312 if (!part_name || !strcmp(part_name, "")) {
313 fastboot_fail("partition not given", response);
317 r = part_get_info_by_name_or_alias(*dev_desc, part_name, part_info);
319 fastboot_fail("partition not found", response);
327 * fastboot_mmc_flash_write() - Write image to eMMC for fastboot
329 * @cmd: Named partition to write image to
330 * @download_buffer: Pointer to image data
331 * @download_bytes: Size of image data
332 * @response: Pointer to fastboot response buffer
334 void fastboot_mmc_flash_write(const char *cmd, void *download_buffer,
335 u32 download_bytes, char *response)
337 struct blk_desc *dev_desc;
338 disk_partition_t info;
340 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
341 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
342 pr_err("invalid mmc device\n");
343 fastboot_fail("invalid mmc device", response);
347 #if CONFIG_IS_ENABLED(EFI_PARTITION)
348 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
349 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
351 if (is_valid_gpt_buf(dev_desc, download_buffer)) {
352 printf("%s: invalid GPT - refusing to write to flash\n",
354 fastboot_fail("invalid GPT partition", response);
357 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
358 printf("%s: writing GPT partitions failed\n", __func__);
359 fastboot_fail("writing GPT partitions failed",
363 printf("........ success\n");
364 fastboot_okay(NULL, response);
369 #if CONFIG_IS_ENABLED(DOS_PARTITION)
370 if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) {
371 printf("%s: updating MBR\n", __func__);
372 if (is_valid_dos_buf(download_buffer)) {
373 printf("%s: invalid MBR - refusing to write to flash\n",
375 fastboot_fail("invalid MBR partition", response);
378 if (write_mbr_partition(dev_desc, download_buffer)) {
379 printf("%s: writing MBR partition failed\n", __func__);
380 fastboot_fail("writing MBR partition failed",
384 printf("........ success\n");
385 fastboot_okay(NULL, response);
390 #ifdef CONFIG_ANDROID_BOOT_IMAGE
391 if (strncasecmp(cmd, "zimage", 6) == 0) {
392 fb_mmc_update_zimage(dev_desc, download_buffer,
393 download_bytes, response);
398 if (part_get_info_by_name_or_alias(dev_desc, cmd, &info) < 0) {
399 pr_err("cannot find partition: '%s'\n", cmd);
400 fastboot_fail("cannot find partition", response);
404 if (is_sparse_image(download_buffer)) {
405 struct fb_mmc_sparse sparse_priv;
406 struct sparse_storage sparse;
409 sparse_priv.dev_desc = dev_desc;
411 sparse.blksz = info.blksz;
412 sparse.start = info.start;
413 sparse.size = info.size;
414 sparse.write = fb_mmc_sparse_write;
415 sparse.reserve = fb_mmc_sparse_reserve;
416 sparse.mssg = fastboot_fail;
418 printf("Flashing sparse image at offset " LBAFU "\n",
421 sparse.priv = &sparse_priv;
422 err = write_sparse_image(&sparse, cmd, download_buffer,
425 fastboot_okay(NULL, response);
427 write_raw_image(dev_desc, &info, cmd, download_buffer,
428 download_bytes, response);
433 * fastboot_mmc_flash_erase() - Erase eMMC for fastboot
435 * @cmd: Named partition to erase
436 * @response: Pointer to fastboot response buffer
438 void fastboot_mmc_erase(const char *cmd, char *response)
441 struct blk_desc *dev_desc;
442 disk_partition_t info;
443 lbaint_t blks, blks_start, blks_size, grp_size;
444 struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
447 pr_err("invalid mmc device\n");
448 fastboot_fail("invalid mmc device", response);
452 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
453 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
454 pr_err("invalid mmc device\n");
455 fastboot_fail("invalid mmc device", response);
459 ret = part_get_info_by_name_or_alias(dev_desc, cmd, &info);
461 pr_err("cannot find partition: '%s'\n", cmd);
462 fastboot_fail("cannot find partition", response);
466 /* Align blocks to erase group size to avoid erasing other partitions */
467 grp_size = mmc->erase_grp_size;
468 blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
469 if (info.size >= grp_size)
470 blks_size = (info.size - (blks_start - info.start)) &
475 printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
476 blks_start, blks_start + blks_size);
478 blks = fb_mmc_blk_write(dev_desc, blks_start, blks_size, NULL);
480 if (blks != blks_size) {
481 pr_err("failed erasing from device %d\n", dev_desc->devnum);
482 fastboot_fail("failed erasing from device", response);
486 printf("........ erased " LBAFU " bytes from '%s'\n",
487 blks_size * info.blksz, cmd);
488 fastboot_okay(NULL, response);