2 * Copyright 2014 Broadcom Corporation.
4 * SPDX-License-Identifier: GPL-2.0+
12 #include <image-sparse.h>
17 #ifndef CONFIG_FASTBOOT_GPT_NAME
18 #define CONFIG_FASTBOOT_GPT_NAME GPT_ENTRY_NAME
21 struct fb_mmc_sparse {
22 struct blk_desc *dev_desc;
25 static int part_get_info_efi_by_name_or_alias(struct blk_desc *dev_desc,
26 const char *name, disk_partition_t *info)
30 ret = part_get_info_efi_by_name(dev_desc, name, info);
32 /* strlen("fastboot_partition_alias_") + 32(part_name) + 1 */
33 char env_alias_name[25 + 32 + 1];
34 char *aliased_part_name;
37 strcpy(env_alias_name, "fastboot_partition_alias_");
38 strncat(env_alias_name, name, 32);
39 aliased_part_name = getenv(env_alias_name);
40 if (aliased_part_name != NULL)
41 ret = part_get_info_efi_by_name(dev_desc,
42 aliased_part_name, info);
47 static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info,
48 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
50 struct fb_mmc_sparse *sparse = info->priv;
51 struct blk_desc *dev_desc = sparse->dev_desc;
53 return blk_dwrite(dev_desc, blk, blkcnt, buffer);
56 static lbaint_t fb_mmc_sparse_reserve(struct sparse_storage *info,
57 lbaint_t blk, lbaint_t blkcnt)
62 static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info,
63 const char *part_name, void *buffer,
64 unsigned int download_bytes)
69 /* determine number of blocks to write */
70 blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
71 blkcnt = lldiv(blkcnt, info->blksz);
73 if (blkcnt > info->size) {
74 error("too large for partition: '%s'\n", part_name);
75 fastboot_fail("too large for partition");
79 puts("Flashing Raw Image\n");
81 blks = blk_dwrite(dev_desc, info->start, blkcnt, buffer);
83 error("failed writing to device %d\n", dev_desc->devnum);
84 fastboot_fail("failed writing to device");
88 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
93 void fb_mmc_flash_write(const char *cmd, void *download_buffer,
94 unsigned int download_bytes)
96 struct blk_desc *dev_desc;
97 disk_partition_t info;
99 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
100 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
101 error("invalid mmc device\n");
102 fastboot_fail("invalid mmc device");
106 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
107 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
109 if (is_valid_gpt_buf(dev_desc, download_buffer)) {
110 printf("%s: invalid GPT - refusing to write to flash\n",
112 fastboot_fail("invalid GPT partition");
115 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
116 printf("%s: writing GPT partitions failed\n", __func__);
118 "writing GPT partitions failed");
121 printf("........ success\n");
124 } else if (part_get_info_efi_by_name_or_alias(dev_desc, cmd, &info)) {
125 error("cannot find partition: '%s'\n", cmd);
126 fastboot_fail("cannot find partition");
130 if (is_sparse_image(download_buffer)) {
131 struct fb_mmc_sparse sparse_priv;
132 struct sparse_storage sparse;
134 sparse_priv.dev_desc = dev_desc;
136 sparse.blksz = info.blksz;
137 sparse.start = info.start;
138 sparse.size = info.size;
139 sparse.write = fb_mmc_sparse_write;
140 sparse.reserve = fb_mmc_sparse_reserve;
142 printf("Flashing sparse image at offset " LBAFU "\n",
145 sparse.priv = &sparse_priv;
146 write_sparse_image(&sparse, cmd, download_buffer,
149 write_raw_image(dev_desc, &info, cmd, download_buffer,
154 void fb_mmc_erase(const char *cmd)
157 struct blk_desc *dev_desc;
158 disk_partition_t info;
159 lbaint_t blks, blks_start, blks_size, grp_size;
160 struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
163 error("invalid mmc device");
164 fastboot_fail("invalid mmc device");
168 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
169 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
170 error("invalid mmc device");
171 fastboot_fail("invalid mmc device");
175 ret = part_get_info_efi_by_name_or_alias(dev_desc, cmd, &info);
177 error("cannot find partition: '%s'", cmd);
178 fastboot_fail("cannot find partition");
182 /* Align blocks to erase group size to avoid erasing other partitions */
183 grp_size = mmc->erase_grp_size;
184 blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
185 if (info.size >= grp_size)
186 blks_size = (info.size - (blks_start - info.start)) &
191 printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
192 blks_start, blks_start + blks_size);
194 blks = blk_derase(dev_desc, blks_start, blks_size);
195 if (blks != blks_size) {
196 error("failed erasing from device %d", dev_desc->devnum);
197 fastboot_fail("failed erasing from device");
201 printf("........ erased " LBAFU " bytes from '%s'\n",
202 blks_size * info.blksz, cmd);