2 * Copyright 2014 Broadcom Corporation.
4 * SPDX-License-Identifier: GPL-2.0+
13 #include <image-sparse.h>
15 #include <sparse_format.h>
19 #ifndef CONFIG_FASTBOOT_GPT_NAME
20 #define CONFIG_FASTBOOT_GPT_NAME GPT_ENTRY_NAME
23 static char *response_str;
25 struct fb_mmc_sparse {
26 struct blk_desc *dev_desc;
29 static int part_get_info_efi_by_name_or_alias(struct blk_desc *dev_desc,
30 const char *name, disk_partition_t *info)
34 ret = part_get_info_efi_by_name(dev_desc, name, info);
36 /* strlen("fastboot_partition_alias_") + 32(part_name) + 1 */
37 char env_alias_name[25 + 32 + 1];
38 char *aliased_part_name;
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_efi_by_name(dev_desc,
46 aliased_part_name, info);
52 static int fb_mmc_sparse_write(struct sparse_storage *storage,
58 struct fb_mmc_sparse *sparse = priv;
59 struct blk_desc *dev_desc = sparse->dev_desc;
62 ret = blk_dwrite(dev_desc, offset, size, data);
69 static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info,
70 const char *part_name, void *buffer,
71 unsigned int download_bytes)
76 /* determine number of blocks to write */
77 blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
78 blkcnt = lldiv(blkcnt, info->blksz);
80 if (blkcnt > info->size) {
81 error("too large for partition: '%s'\n", part_name);
82 fastboot_fail(response_str, "too large for partition");
86 puts("Flashing Raw Image\n");
88 blks = blk_dwrite(dev_desc, info->start, blkcnt, buffer);
90 error("failed writing to device %d\n", dev_desc->devnum);
91 fastboot_fail(response_str, "failed writing to device");
95 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
97 fastboot_okay(response_str, "");
100 void fb_mmc_flash_write(const char *cmd, unsigned int session_id,
101 void *download_buffer, unsigned int download_bytes,
104 struct blk_desc *dev_desc;
105 disk_partition_t info;
107 /* initialize the response buffer */
108 response_str = response;
110 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
111 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
112 error("invalid mmc device\n");
113 fastboot_fail(response_str, "invalid mmc device");
117 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
118 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
120 if (is_valid_gpt_buf(dev_desc, download_buffer)) {
121 printf("%s: invalid GPT - refusing to write to flash\n",
123 fastboot_fail(response_str, "invalid GPT partition");
126 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
127 printf("%s: writing GPT partitions failed\n", __func__);
128 fastboot_fail(response_str,
129 "writing GPT partitions failed");
132 printf("........ success\n");
133 fastboot_okay(response_str, "");
135 } else if (part_get_info_efi_by_name_or_alias(dev_desc, cmd, &info)) {
136 error("cannot find partition: '%s'\n", cmd);
137 fastboot_fail(response_str, "cannot find partition");
141 if (is_sparse_image(download_buffer)) {
142 struct fb_mmc_sparse sparse_priv;
143 sparse_storage_t sparse;
145 sparse_priv.dev_desc = dev_desc;
147 sparse.block_sz = info.blksz;
148 sparse.start = info.start;
149 sparse.size = info.size;
151 sparse.write = fb_mmc_sparse_write;
153 printf("Flashing sparse image at offset " LBAFU "\n",
156 store_sparse_image(&sparse, &sparse_priv, session_id,
159 write_raw_image(dev_desc, &info, cmd, download_buffer,
163 fastboot_okay(response_str, "");
166 void fb_mmc_erase(const char *cmd, char *response)
169 struct blk_desc *dev_desc;
170 disk_partition_t info;
171 lbaint_t blks, blks_start, blks_size, grp_size;
172 struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
175 error("invalid mmc device");
176 fastboot_fail(response_str, "invalid mmc device");
180 /* initialize the response buffer */
181 response_str = response;
183 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
184 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
185 error("invalid mmc device");
186 fastboot_fail(response_str, "invalid mmc device");
190 ret = part_get_info_efi_by_name_or_alias(dev_desc, cmd, &info);
192 error("cannot find partition: '%s'", cmd);
193 fastboot_fail(response_str, "cannot find partition");
197 /* Align blocks to erase group size to avoid erasing other partitions */
198 grp_size = mmc->erase_grp_size;
199 blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
200 if (info.size >= grp_size)
201 blks_size = (info.size - (blks_start - info.start)) &
206 printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
207 blks_start, blks_start + blks_size);
209 blks = dev_desc->block_erase(dev_desc, blks_start, blks_size);
210 if (blks != blks_size) {
211 error("failed erasing from device %d", dev_desc->devnum);
212 fastboot_fail(response_str, "failed erasing from device");
216 printf("........ erased " LBAFU " bytes from '%s'\n",
217 blks_size * info.blksz, cmd);
218 fastboot_okay(response_str, "");