1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2014 Broadcom Corporation.
11 #include <image-sparse.h>
15 #include <linux/compat.h>
16 #include <android_image.h>
19 * FIXME: Ensure we always set these names via Kconfig once xxx_PARTITION is
22 #ifndef CONFIG_FASTBOOT_GPT_NAME
23 #define CONFIG_FASTBOOT_GPT_NAME "gpt"
27 #ifndef CONFIG_FASTBOOT_MBR_NAME
28 #define CONFIG_FASTBOOT_MBR_NAME "mbr"
31 #define BOOT_PARTITION_NAME "boot"
33 struct fb_mmc_sparse {
34 struct blk_desc *dev_desc;
37 static int part_get_info_by_name_or_alias(struct blk_desc *dev_desc,
38 const char *name, disk_partition_t *info)
42 ret = part_get_info_by_name(dev_desc, name, info);
44 /* strlen("fastboot_partition_alias_") + 32(part_name) + 1 */
45 char env_alias_name[25 + 32 + 1];
46 char *aliased_part_name;
49 strcpy(env_alias_name, "fastboot_partition_alias_");
50 strncat(env_alias_name, name, 32);
51 aliased_part_name = env_get(env_alias_name);
52 if (aliased_part_name != NULL)
53 ret = part_get_info_by_name(dev_desc,
54 aliased_part_name, info);
59 static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info,
60 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
62 struct fb_mmc_sparse *sparse = info->priv;
63 struct blk_desc *dev_desc = sparse->dev_desc;
65 return blk_dwrite(dev_desc, blk, blkcnt, buffer);
68 static lbaint_t fb_mmc_sparse_reserve(struct sparse_storage *info,
69 lbaint_t blk, lbaint_t blkcnt)
74 static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info,
75 const char *part_name, void *buffer,
76 unsigned int download_bytes)
81 /* determine number of blocks to write */
82 blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
83 blkcnt = lldiv(blkcnt, info->blksz);
85 if (blkcnt > info->size) {
86 pr_err("too large for partition: '%s'\n", part_name);
87 fastboot_fail("too large for partition");
91 puts("Flashing Raw Image\n");
93 blks = blk_dwrite(dev_desc, info->start, blkcnt, buffer);
95 pr_err("failed writing to device %d\n", dev_desc->devnum);
96 fastboot_fail("failed writing to device");
100 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
105 #ifdef CONFIG_ANDROID_BOOT_IMAGE
107 * Read Android boot image header from boot partition.
109 * @param[in] dev_desc MMC device descriptor
110 * @param[in] info Boot partition info
111 * @param[out] hdr Where to store read boot image header
113 * @return Boot image header sectors count or 0 on error
115 static lbaint_t fb_mmc_get_boot_header(struct blk_desc *dev_desc,
116 disk_partition_t *info,
117 struct andr_img_hdr *hdr)
119 ulong sector_size; /* boot partition sector size */
120 lbaint_t hdr_sectors; /* boot image header sectors count */
123 /* Calculate boot image sectors count */
124 sector_size = info->blksz;
125 hdr_sectors = DIV_ROUND_UP(sizeof(struct andr_img_hdr), sector_size);
126 if (hdr_sectors == 0) {
127 pr_err("invalid number of boot sectors: 0");
128 fastboot_fail("invalid number of boot sectors: 0");
132 /* Read the boot image header */
133 res = blk_dread(dev_desc, info->start, hdr_sectors, (void *)hdr);
134 if (res != hdr_sectors) {
135 pr_err("cannot read header from boot partition");
136 fastboot_fail("cannot read header from boot partition");
140 /* Check boot header magic string */
141 res = android_image_check_header(hdr);
143 pr_err("bad boot image magic");
144 fastboot_fail("boot partition not initialized");
152 * Write downloaded zImage to boot partition and repack it properly.
154 * @param dev_desc MMC device descriptor
155 * @param download_buffer Address to fastboot buffer with zImage in it
156 * @param download_bytes Size of fastboot buffer, in bytes
158 * @return 0 on success or -1 on error
160 static int fb_mmc_update_zimage(struct blk_desc *dev_desc,
161 void *download_buffer,
162 unsigned int download_bytes)
164 uintptr_t hdr_addr; /* boot image header address */
165 struct andr_img_hdr *hdr; /* boot image header */
166 lbaint_t hdr_sectors; /* boot image header sectors */
168 u32 ramdisk_sector_start;
170 u32 kernel_sector_start;
172 u32 sectors_per_page;
173 disk_partition_t info;
176 puts("Flashing zImage\n");
178 /* Get boot partition info */
179 res = part_get_info_by_name(dev_desc, BOOT_PARTITION_NAME, &info);
181 pr_err("cannot find boot partition");
182 fastboot_fail("cannot find boot partition");
186 /* Put boot image header in fastboot buffer after downloaded zImage */
187 hdr_addr = (uintptr_t)download_buffer + ALIGN(download_bytes, PAGE_SIZE);
188 hdr = (struct andr_img_hdr *)hdr_addr;
190 /* Read boot image header */
191 hdr_sectors = fb_mmc_get_boot_header(dev_desc, &info, hdr);
192 if (hdr_sectors == 0) {
193 pr_err("unable to read boot image header");
194 fastboot_fail("unable to read boot image header");
198 /* Check if boot image has second stage in it (we don't support it) */
199 if (hdr->second_size > 0) {
200 pr_err("moving second stage is not supported yet");
201 fastboot_fail("moving second stage is not supported yet");
205 /* Extract ramdisk location */
206 sectors_per_page = hdr->page_size / info.blksz;
207 ramdisk_sector_start = info.start + sectors_per_page;
208 ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
210 ramdisk_sectors = DIV_ROUND_UP(hdr->ramdisk_size, hdr->page_size) *
213 /* Read ramdisk and put it in fastboot buffer after boot image header */
214 ramdisk_buffer = (u8 *)hdr + (hdr_sectors * info.blksz);
215 res = blk_dread(dev_desc, ramdisk_sector_start, ramdisk_sectors,
217 if (res != ramdisk_sectors) {
218 pr_err("cannot read ramdisk from boot partition");
219 fastboot_fail("cannot read ramdisk from boot partition");
223 /* Write new kernel size to boot image header */
224 hdr->kernel_size = download_bytes;
225 res = blk_dwrite(dev_desc, info.start, hdr_sectors, (void *)hdr);
227 pr_err("cannot writeback boot image header");
228 fastboot_fail("cannot write back boot image header");
232 /* Write the new downloaded kernel */
233 kernel_sector_start = info.start + sectors_per_page;
234 kernel_sectors = DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
236 res = blk_dwrite(dev_desc, kernel_sector_start, kernel_sectors,
239 pr_err("cannot write new kernel");
240 fastboot_fail("cannot write new kernel");
244 /* Write the saved ramdisk back */
245 ramdisk_sector_start = info.start + sectors_per_page;
246 ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
248 res = blk_dwrite(dev_desc, ramdisk_sector_start, ramdisk_sectors,
251 pr_err("cannot write back original ramdisk");
252 fastboot_fail("cannot write back original ramdisk");
256 puts("........ zImage was updated in boot partition\n");
262 void fb_mmc_flash_write(const char *cmd, void *download_buffer,
263 unsigned int download_bytes)
265 struct blk_desc *dev_desc;
266 disk_partition_t info;
268 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
269 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
270 pr_err("invalid mmc device\n");
271 fastboot_fail("invalid mmc device");
275 #if CONFIG_IS_ENABLED(EFI_PARTITION)
276 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
277 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
279 if (is_valid_gpt_buf(dev_desc, download_buffer)) {
280 printf("%s: invalid GPT - refusing to write to flash\n",
282 fastboot_fail("invalid GPT partition");
285 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
286 printf("%s: writing GPT partitions failed\n", __func__);
287 fastboot_fail("writing GPT partitions failed");
290 printf("........ success\n");
296 #if CONFIG_IS_ENABLED(DOS_PARTITION)
297 if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) {
298 printf("%s: updating MBR\n", __func__);
299 if (is_valid_dos_buf(download_buffer)) {
300 printf("%s: invalid MBR - refusing to write to flash\n",
302 fastboot_fail("invalid MBR partition");
305 if (write_mbr_partition(dev_desc, download_buffer)) {
306 printf("%s: writing MBR partition failed\n", __func__);
307 fastboot_fail("writing MBR partition failed");
310 printf("........ success\n");
316 #ifdef CONFIG_ANDROID_BOOT_IMAGE
317 if (strncasecmp(cmd, "zimage", 6) == 0) {
318 fb_mmc_update_zimage(dev_desc, download_buffer, download_bytes);
323 if (part_get_info_by_name_or_alias(dev_desc, cmd, &info) < 0) {
324 pr_err("cannot find partition: '%s'\n", cmd);
325 fastboot_fail("cannot find partition");
329 if (is_sparse_image(download_buffer)) {
330 struct fb_mmc_sparse sparse_priv;
331 struct sparse_storage sparse;
334 sparse_priv.dev_desc = dev_desc;
336 sparse.blksz = info.blksz;
337 sparse.start = info.start;
338 sparse.size = info.size;
339 sparse.write = fb_mmc_sparse_write;
340 sparse.reserve = fb_mmc_sparse_reserve;
341 sparse.mssg = fastboot_fail;
343 printf("Flashing sparse image at offset " LBAFU "\n",
346 sparse.priv = &sparse_priv;
347 err = write_sparse_image(&sparse, cmd, download_buffer);
351 write_raw_image(dev_desc, &info, cmd, download_buffer,
356 void fb_mmc_erase(const char *cmd)
359 struct blk_desc *dev_desc;
360 disk_partition_t info;
361 lbaint_t blks, blks_start, blks_size, grp_size;
362 struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
365 pr_err("invalid mmc device");
366 fastboot_fail("invalid mmc device");
370 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
371 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
372 pr_err("invalid mmc device");
373 fastboot_fail("invalid mmc device");
377 ret = part_get_info_by_name_or_alias(dev_desc, cmd, &info);
379 pr_err("cannot find partition: '%s'", cmd);
380 fastboot_fail("cannot find partition");
384 /* Align blocks to erase group size to avoid erasing other partitions */
385 grp_size = mmc->erase_grp_size;
386 blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
387 if (info.size >= grp_size)
388 blks_size = (info.size - (blks_start - info.start)) &
393 printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
394 blks_start, blks_start + blks_size);
396 blks = blk_derase(dev_desc, blks_start, blks_size);
397 if (blks != blks_size) {
398 pr_err("failed erasing from device %d", dev_desc->devnum);
399 fastboot_fail("failed erasing from device");
403 printf("........ erased " LBAFU " bytes from '%s'\n",
404 blks_size * info.blksz, cmd);