2 * Copyright 2014 Broadcom Corporation.
4 * SPDX-License-Identifier: GPL-2.0+
12 #include <image-sparse.h>
18 * FIXME: Ensure we always set these names via Kconfig once xxx_PARTITION is
21 #ifndef CONFIG_FASTBOOT_GPT_NAME
22 #define CONFIG_FASTBOOT_GPT_NAME "gpt"
26 #ifndef CONFIG_FASTBOOT_MBR_NAME
27 #define CONFIG_FASTBOOT_MBR_NAME "mbr"
30 struct fb_mmc_sparse {
31 struct blk_desc *dev_desc;
34 static int part_get_info_by_name_or_alias(struct blk_desc *dev_desc,
35 const char *name, disk_partition_t *info)
39 ret = part_get_info_by_name(dev_desc, name, info);
41 /* strlen("fastboot_partition_alias_") + 32(part_name) + 1 */
42 char env_alias_name[25 + 32 + 1];
43 char *aliased_part_name;
46 strcpy(env_alias_name, "fastboot_partition_alias_");
47 strncat(env_alias_name, name, 32);
48 aliased_part_name = getenv(env_alias_name);
49 if (aliased_part_name != NULL)
50 ret = part_get_info_by_name(dev_desc,
51 aliased_part_name, info);
56 static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info,
57 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
59 struct fb_mmc_sparse *sparse = info->priv;
60 struct blk_desc *dev_desc = sparse->dev_desc;
62 return blk_dwrite(dev_desc, blk, blkcnt, buffer);
65 static lbaint_t fb_mmc_sparse_reserve(struct sparse_storage *info,
66 lbaint_t blk, lbaint_t blkcnt)
71 static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info,
72 const char *part_name, void *buffer,
73 unsigned int download_bytes)
78 /* determine number of blocks to write */
79 blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
80 blkcnt = lldiv(blkcnt, info->blksz);
82 if (blkcnt > info->size) {
83 error("too large for partition: '%s'\n", part_name);
84 fastboot_fail("too large for partition");
88 puts("Flashing Raw Image\n");
90 blks = blk_dwrite(dev_desc, info->start, blkcnt, buffer);
92 error("failed writing to device %d\n", dev_desc->devnum);
93 fastboot_fail("failed writing to device");
97 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
102 void fb_mmc_flash_write(const char *cmd, void *download_buffer,
103 unsigned int download_bytes)
105 struct blk_desc *dev_desc;
106 disk_partition_t info;
108 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
109 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
110 error("invalid mmc device\n");
111 fastboot_fail("invalid mmc device");
115 #ifdef CONFIG_EFI_PARTITION
116 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
117 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
119 if (is_valid_gpt_buf(dev_desc, download_buffer)) {
120 printf("%s: invalid GPT - refusing to write to flash\n",
122 fastboot_fail("invalid GPT partition");
125 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
126 printf("%s: writing GPT partitions failed\n", __func__);
127 fastboot_fail("writing GPT partitions failed");
130 printf("........ success\n");
136 #ifdef CONFIG_DOS_PARTITION
137 if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) {
138 printf("%s: updating MBR\n", __func__);
139 if (is_valid_dos_buf(download_buffer)) {
140 printf("%s: invalid MBR - refusing to write to flash\n",
142 fastboot_fail("invalid MBR partition");
145 if (write_mbr_partition(dev_desc, download_buffer)) {
146 printf("%s: writing MBR partition failed\n", __func__);
147 fastboot_fail("writing MBR partition failed");
150 printf("........ success\n");
156 if (part_get_info_by_name_or_alias(dev_desc, cmd, &info)) {
157 error("cannot find partition: '%s'\n", cmd);
158 fastboot_fail("cannot find partition");
162 if (is_sparse_image(download_buffer)) {
163 struct fb_mmc_sparse sparse_priv;
164 struct sparse_storage sparse;
166 sparse_priv.dev_desc = dev_desc;
168 sparse.blksz = info.blksz;
169 sparse.start = info.start;
170 sparse.size = info.size;
171 sparse.write = fb_mmc_sparse_write;
172 sparse.reserve = fb_mmc_sparse_reserve;
174 printf("Flashing sparse image at offset " LBAFU "\n",
177 sparse.priv = &sparse_priv;
178 write_sparse_image(&sparse, cmd, download_buffer,
181 write_raw_image(dev_desc, &info, cmd, download_buffer,
186 void fb_mmc_erase(const char *cmd)
189 struct blk_desc *dev_desc;
190 disk_partition_t info;
191 lbaint_t blks, blks_start, blks_size, grp_size;
192 struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
195 error("invalid mmc device");
196 fastboot_fail("invalid mmc device");
200 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
201 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
202 error("invalid mmc device");
203 fastboot_fail("invalid mmc device");
207 ret = part_get_info_by_name_or_alias(dev_desc, cmd, &info);
209 error("cannot find partition: '%s'", cmd);
210 fastboot_fail("cannot find partition");
214 /* Align blocks to erase group size to avoid erasing other partitions */
215 grp_size = mmc->erase_grp_size;
216 blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
217 if (info.size >= grp_size)
218 blks_size = (info.size - (blks_start - info.start)) &
223 printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
224 blks_start, blks_start + blks_size);
226 blks = blk_derase(dev_desc, blks_start, blks_size);
227 if (blks != blks_size) {
228 error("failed erasing from device %d", dev_desc->devnum);
229 fastboot_fail("failed erasing from device");
233 printf("........ erased " LBAFU " bytes from '%s'\n",
234 blks_size * info.blksz, cmd);