blob: 1bcb2e52d49919e6458c600492eb7da37f53cf85 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Steve Rae792ac482014-08-26 11:47:27 -07002/*
3 * Copyright 2014 Broadcom Corporation.
Steve Rae792ac482014-08-26 11:47:27 -07004 */
5
Steve Rae7d059342014-12-12 15:51:54 -08006#include <config.h>
Steve Rae792ac482014-08-26 11:47:27 -07007#include <common.h>
Simon Glass2ee8ada2016-02-29 15:25:52 -07008#include <blk.h>
Maxime Ripard00e8eb72015-10-15 14:34:13 +02009#include <fastboot.h>
Steve Rae792ac482014-08-26 11:47:27 -070010#include <fb_mmc.h>
Maxime Ripard6af49bc2015-10-15 14:34:19 +020011#include <image-sparse.h>
Steve Rae792ac482014-08-26 11:47:27 -070012#include <part.h>
Dileep Kattab1228202015-02-17 18:48:23 +053013#include <mmc.h>
Siarhei Siamashka58f30d82015-10-28 06:24:16 +020014#include <div64.h>
Sam Protsenko540eb952017-05-18 15:04:59 +030015#include <linux/compat.h>
16#include <android_image.h>
Steve Rae792ac482014-08-26 11:47:27 -070017
Petr Kulhavy4ed1eca2016-09-09 10:27:18 +020018/*
19 * FIXME: Ensure we always set these names via Kconfig once xxx_PARTITION is
20 * migrated
21 */
22#ifndef CONFIG_FASTBOOT_GPT_NAME
23#define CONFIG_FASTBOOT_GPT_NAME "gpt"
Steve Rae7d059342014-12-12 15:51:54 -080024#endif
25
Petr Kulhavy9f174c92016-09-09 10:27:16 +020026
Petr Kulhavy4ed1eca2016-09-09 10:27:18 +020027#ifndef CONFIG_FASTBOOT_MBR_NAME
Petr Kulhavy9f174c92016-09-09 10:27:16 +020028#define CONFIG_FASTBOOT_MBR_NAME "mbr"
29#endif
30
Sam Protsenko540eb952017-05-18 15:04:59 +030031#define BOOT_PARTITION_NAME "boot"
32
Maxime Riparda58e9c72015-10-15 14:34:14 +020033struct fb_mmc_sparse {
Simon Glasse3394752016-02-29 15:25:34 -070034 struct blk_desc *dev_desc;
Maxime Riparda58e9c72015-10-15 14:34:14 +020035};
36
Petr Kulhavy9f174c92016-09-09 10:27:16 +020037static int part_get_info_by_name_or_alias(struct blk_desc *dev_desc,
Michael Scottced33492015-03-11 10:02:31 -070038 const char *name, disk_partition_t *info)
39{
40 int ret;
41
Petr Kulhavy712257e2016-09-09 10:27:15 +020042 ret = part_get_info_by_name(dev_desc, name, info);
Alex Deymoe2f689f2017-04-02 01:49:50 -070043 if (ret < 0) {
Michael Scottced33492015-03-11 10:02:31 -070044 /* strlen("fastboot_partition_alias_") + 32(part_name) + 1 */
45 char env_alias_name[25 + 32 + 1];
46 char *aliased_part_name;
47
48 /* check for alias */
49 strcpy(env_alias_name, "fastboot_partition_alias_");
50 strncat(env_alias_name, name, 32);
Simon Glass64b723f2017-08-03 12:22:12 -060051 aliased_part_name = env_get(env_alias_name);
Michael Scottced33492015-03-11 10:02:31 -070052 if (aliased_part_name != NULL)
Petr Kulhavy712257e2016-09-09 10:27:15 +020053 ret = part_get_info_by_name(dev_desc,
Michael Scottced33492015-03-11 10:02:31 -070054 aliased_part_name, info);
55 }
56 return ret;
57}
58
Steve Rae8cf58db2016-06-07 11:19:36 -070059static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info,
60 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
Maxime Riparda58e9c72015-10-15 14:34:14 +020061{
Steve Rae8cf58db2016-06-07 11:19:36 -070062 struct fb_mmc_sparse *sparse = info->priv;
Simon Glasse3394752016-02-29 15:25:34 -070063 struct blk_desc *dev_desc = sparse->dev_desc;
Maxime Riparda58e9c72015-10-15 14:34:14 +020064
Steve Rae8cf58db2016-06-07 11:19:36 -070065 return blk_dwrite(dev_desc, blk, blkcnt, buffer);
Maxime Riparda58e9c72015-10-15 14:34:14 +020066}
67
Steve Rae8a99be82016-06-07 11:19:38 -070068static lbaint_t fb_mmc_sparse_reserve(struct sparse_storage *info,
69 lbaint_t blk, lbaint_t blkcnt)
70{
71 return blkcnt;
72}
73
Simon Glasse3394752016-02-29 15:25:34 -070074static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info,
Steve Rae792ac482014-08-26 11:47:27 -070075 const char *part_name, void *buffer,
Alex Kiernan27b69de2018-05-29 15:30:40 +000076 unsigned int download_bytes, char *response)
Steve Rae792ac482014-08-26 11:47:27 -070077{
78 lbaint_t blkcnt;
79 lbaint_t blks;
80
81 /* determine number of blocks to write */
82 blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
Siarhei Siamashka58f30d82015-10-28 06:24:16 +020083 blkcnt = lldiv(blkcnt, info->blksz);
Steve Rae792ac482014-08-26 11:47:27 -070084
85 if (blkcnt > info->size) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090086 pr_err("too large for partition: '%s'\n", part_name);
Alex Kiernan27b69de2018-05-29 15:30:40 +000087 fastboot_fail("too large for partition", response);
Steve Rae792ac482014-08-26 11:47:27 -070088 return;
89 }
90
91 puts("Flashing Raw Image\n");
92
Simon Glass2ee8ada2016-02-29 15:25:52 -070093 blks = blk_dwrite(dev_desc, info->start, blkcnt, buffer);
Steve Rae792ac482014-08-26 11:47:27 -070094 if (blks != blkcnt) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090095 pr_err("failed writing to device %d\n", dev_desc->devnum);
Alex Kiernan27b69de2018-05-29 15:30:40 +000096 fastboot_fail("failed writing to device", response);
Steve Rae792ac482014-08-26 11:47:27 -070097 return;
98 }
99
100 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
101 part_name);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000102 fastboot_okay(NULL, response);
Steve Rae792ac482014-08-26 11:47:27 -0700103}
104
Sam Protsenko540eb952017-05-18 15:04:59 +0300105#ifdef CONFIG_ANDROID_BOOT_IMAGE
106/**
107 * Read Android boot image header from boot partition.
108 *
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
112 *
113 * @return Boot image header sectors count or 0 on error
114 */
115static lbaint_t fb_mmc_get_boot_header(struct blk_desc *dev_desc,
116 disk_partition_t *info,
Alex Kiernan27b69de2018-05-29 15:30:40 +0000117 struct andr_img_hdr *hdr,
118 char *response)
Sam Protsenko540eb952017-05-18 15:04:59 +0300119{
120 ulong sector_size; /* boot partition sector size */
121 lbaint_t hdr_sectors; /* boot image header sectors count */
122 int res;
123
124 /* Calculate boot image sectors count */
125 sector_size = info->blksz;
126 hdr_sectors = DIV_ROUND_UP(sizeof(struct andr_img_hdr), sector_size);
127 if (hdr_sectors == 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900128 pr_err("invalid number of boot sectors: 0");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000129 fastboot_fail("invalid number of boot sectors: 0", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300130 return 0;
131 }
132
133 /* Read the boot image header */
134 res = blk_dread(dev_desc, info->start, hdr_sectors, (void *)hdr);
Tom Rinibda6f772017-08-14 21:00:44 -0400135 if (res != hdr_sectors) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900136 pr_err("cannot read header from boot partition");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000137 fastboot_fail("cannot read header from boot partition",
138 response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300139 return 0;
140 }
141
142 /* Check boot header magic string */
143 res = android_image_check_header(hdr);
144 if (res != 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900145 pr_err("bad boot image magic");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000146 fastboot_fail("boot partition not initialized", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300147 return 0;
148 }
149
150 return hdr_sectors;
151}
152
153/**
154 * Write downloaded zImage to boot partition and repack it properly.
155 *
156 * @param dev_desc MMC device descriptor
157 * @param download_buffer Address to fastboot buffer with zImage in it
158 * @param download_bytes Size of fastboot buffer, in bytes
159 *
160 * @return 0 on success or -1 on error
161 */
162static int fb_mmc_update_zimage(struct blk_desc *dev_desc,
163 void *download_buffer,
Alex Kiernan27b69de2018-05-29 15:30:40 +0000164 unsigned int download_bytes,
165 char *response)
Sam Protsenko540eb952017-05-18 15:04:59 +0300166{
Tom Rini0ac605c2017-06-10 09:15:37 -0400167 uintptr_t hdr_addr; /* boot image header address */
Sam Protsenko540eb952017-05-18 15:04:59 +0300168 struct andr_img_hdr *hdr; /* boot image header */
169 lbaint_t hdr_sectors; /* boot image header sectors */
170 u8 *ramdisk_buffer;
171 u32 ramdisk_sector_start;
172 u32 ramdisk_sectors;
173 u32 kernel_sector_start;
174 u32 kernel_sectors;
175 u32 sectors_per_page;
176 disk_partition_t info;
177 int res;
178
179 puts("Flashing zImage\n");
180
181 /* Get boot partition info */
182 res = part_get_info_by_name(dev_desc, BOOT_PARTITION_NAME, &info);
183 if (res < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900184 pr_err("cannot find boot partition");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000185 fastboot_fail("cannot find boot partition", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300186 return -1;
187 }
188
189 /* Put boot image header in fastboot buffer after downloaded zImage */
Tom Rini0ac605c2017-06-10 09:15:37 -0400190 hdr_addr = (uintptr_t)download_buffer + ALIGN(download_bytes, PAGE_SIZE);
Sam Protsenko540eb952017-05-18 15:04:59 +0300191 hdr = (struct andr_img_hdr *)hdr_addr;
192
193 /* Read boot image header */
Alex Kiernan27b69de2018-05-29 15:30:40 +0000194 hdr_sectors = fb_mmc_get_boot_header(dev_desc, &info, hdr, response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300195 if (hdr_sectors == 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900196 pr_err("unable to read boot image header");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000197 fastboot_fail("unable to read boot image header", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300198 return -1;
199 }
200
201 /* Check if boot image has second stage in it (we don't support it) */
202 if (hdr->second_size > 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900203 pr_err("moving second stage is not supported yet");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000204 fastboot_fail("moving second stage is not supported yet",
205 response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300206 return -1;
207 }
208
209 /* Extract ramdisk location */
210 sectors_per_page = hdr->page_size / info.blksz;
211 ramdisk_sector_start = info.start + sectors_per_page;
212 ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
213 sectors_per_page;
214 ramdisk_sectors = DIV_ROUND_UP(hdr->ramdisk_size, hdr->page_size) *
215 sectors_per_page;
216
217 /* Read ramdisk and put it in fastboot buffer after boot image header */
218 ramdisk_buffer = (u8 *)hdr + (hdr_sectors * info.blksz);
219 res = blk_dread(dev_desc, ramdisk_sector_start, ramdisk_sectors,
220 ramdisk_buffer);
Tom Rinibda6f772017-08-14 21:00:44 -0400221 if (res != ramdisk_sectors) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900222 pr_err("cannot read ramdisk from boot partition");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000223 fastboot_fail("cannot read ramdisk from boot partition",
224 response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300225 return -1;
226 }
227
228 /* Write new kernel size to boot image header */
229 hdr->kernel_size = download_bytes;
230 res = blk_dwrite(dev_desc, info.start, hdr_sectors, (void *)hdr);
231 if (res == 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900232 pr_err("cannot writeback boot image header");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000233 fastboot_fail("cannot write back boot image header", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300234 return -1;
235 }
236
237 /* Write the new downloaded kernel */
238 kernel_sector_start = info.start + sectors_per_page;
239 kernel_sectors = DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
240 sectors_per_page;
241 res = blk_dwrite(dev_desc, kernel_sector_start, kernel_sectors,
242 download_buffer);
243 if (res == 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900244 pr_err("cannot write new kernel");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000245 fastboot_fail("cannot write new kernel", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300246 return -1;
247 }
248
249 /* Write the saved ramdisk back */
250 ramdisk_sector_start = info.start + sectors_per_page;
251 ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
252 sectors_per_page;
253 res = blk_dwrite(dev_desc, ramdisk_sector_start, ramdisk_sectors,
254 ramdisk_buffer);
255 if (res == 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900256 pr_err("cannot write back original ramdisk");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000257 fastboot_fail("cannot write back original ramdisk", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300258 return -1;
259 }
260
261 puts("........ zImage was updated in boot partition\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000262 fastboot_okay(NULL, response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300263 return 0;
264}
265#endif
266
Steve Raef1f112f2016-06-07 11:19:35 -0700267void fb_mmc_flash_write(const char *cmd, void *download_buffer,
Alex Kiernan27b69de2018-05-29 15:30:40 +0000268 unsigned int download_bytes, char *response)
Steve Rae792ac482014-08-26 11:47:27 -0700269{
Simon Glasse3394752016-02-29 15:25:34 -0700270 struct blk_desc *dev_desc;
Steve Rae792ac482014-08-26 11:47:27 -0700271 disk_partition_t info;
272
Simon Glassbe1e9bb2016-02-29 15:25:42 -0700273 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
Steve Rae792ac482014-08-26 11:47:27 -0700274 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900275 pr_err("invalid mmc device\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000276 fastboot_fail("invalid mmc device", response);
Steve Rae792ac482014-08-26 11:47:27 -0700277 return;
278 }
279
Patrick Delaunay8a4f2bd2017-01-27 11:00:41 +0100280#if CONFIG_IS_ENABLED(EFI_PARTITION)
Steve Rae7d059342014-12-12 15:51:54 -0800281 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
282 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
283 __func__);
284 if (is_valid_gpt_buf(dev_desc, download_buffer)) {
285 printf("%s: invalid GPT - refusing to write to flash\n",
286 __func__);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000287 fastboot_fail("invalid GPT partition", response);
Steve Rae7d059342014-12-12 15:51:54 -0800288 return;
289 }
290 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
291 printf("%s: writing GPT partitions failed\n", __func__);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000292 fastboot_fail("writing GPT partitions failed",
293 response);
Steve Rae7d059342014-12-12 15:51:54 -0800294 return;
295 }
296 printf("........ success\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000297 fastboot_okay(NULL, response);
Steve Rae7d059342014-12-12 15:51:54 -0800298 return;
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200299 }
300#endif
301
Patrick Delaunayf7e07722017-01-27 11:00:37 +0100302#if CONFIG_IS_ENABLED(DOS_PARTITION)
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200303 if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) {
304 printf("%s: updating MBR\n", __func__);
305 if (is_valid_dos_buf(download_buffer)) {
306 printf("%s: invalid MBR - refusing to write to flash\n",
307 __func__);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000308 fastboot_fail("invalid MBR partition", response);
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200309 return;
310 }
311 if (write_mbr_partition(dev_desc, download_buffer)) {
312 printf("%s: writing MBR partition failed\n", __func__);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000313 fastboot_fail("writing MBR partition failed",
314 response);
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200315 return;
316 }
317 printf("........ success\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000318 fastboot_okay(NULL, response);
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200319 return;
320 }
321#endif
322
Sam Protsenko540eb952017-05-18 15:04:59 +0300323#ifdef CONFIG_ANDROID_BOOT_IMAGE
324 if (strncasecmp(cmd, "zimage", 6) == 0) {
Alex Kiernan27b69de2018-05-29 15:30:40 +0000325 fb_mmc_update_zimage(dev_desc, download_buffer,
326 download_bytes, response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300327 return;
328 }
329#endif
330
Alex Deymoe2f689f2017-04-02 01:49:50 -0700331 if (part_get_info_by_name_or_alias(dev_desc, cmd, &info) < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900332 pr_err("cannot find partition: '%s'\n", cmd);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000333 fastboot_fail("cannot find partition", response);
Steve Rae792ac482014-08-26 11:47:27 -0700334 return;
335 }
336
Maxime Riparda58e9c72015-10-15 14:34:14 +0200337 if (is_sparse_image(download_buffer)) {
338 struct fb_mmc_sparse sparse_priv;
Steve Rae8cf58db2016-06-07 11:19:36 -0700339 struct sparse_storage sparse;
Jassi Brar427e6692018-04-06 12:05:09 +0530340 int err;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200341
342 sparse_priv.dev_desc = dev_desc;
343
Steve Rae8cf58db2016-06-07 11:19:36 -0700344 sparse.blksz = info.blksz;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200345 sparse.start = info.start;
346 sparse.size = info.size;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200347 sparse.write = fb_mmc_sparse_write;
Steve Rae8a99be82016-06-07 11:19:38 -0700348 sparse.reserve = fb_mmc_sparse_reserve;
Jassi Brar427e6692018-04-06 12:05:09 +0530349 sparse.mssg = fastboot_fail;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200350
351 printf("Flashing sparse image at offset " LBAFU "\n",
Steve Rae8cf58db2016-06-07 11:19:36 -0700352 sparse.start);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200353
Steve Rae8cf58db2016-06-07 11:19:36 -0700354 sparse.priv = &sparse_priv;
Alex Kiernan27b69de2018-05-29 15:30:40 +0000355 err = write_sparse_image(&sparse, cmd, download_buffer,
356 response);
Jassi Brar427e6692018-04-06 12:05:09 +0530357 if (!err)
Alex Kiernan27b69de2018-05-29 15:30:40 +0000358 fastboot_okay(NULL, response);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200359 } else {
Steve Raec6015672014-08-26 11:47:30 -0700360 write_raw_image(dev_desc, &info, cmd, download_buffer,
Alex Kiernan27b69de2018-05-29 15:30:40 +0000361 download_bytes, response);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200362 }
Steve Rae792ac482014-08-26 11:47:27 -0700363}
Dileep Kattab1228202015-02-17 18:48:23 +0530364
Alex Kiernan27b69de2018-05-29 15:30:40 +0000365void fb_mmc_erase(const char *cmd, char *response)
Dileep Kattab1228202015-02-17 18:48:23 +0530366{
367 int ret;
Simon Glasse3394752016-02-29 15:25:34 -0700368 struct blk_desc *dev_desc;
Dileep Kattab1228202015-02-17 18:48:23 +0530369 disk_partition_t info;
370 lbaint_t blks, blks_start, blks_size, grp_size;
371 struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
372
373 if (mmc == NULL) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900374 pr_err("invalid mmc device");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000375 fastboot_fail("invalid mmc device", response);
Dileep Kattab1228202015-02-17 18:48:23 +0530376 return;
377 }
378
Simon Glassbe1e9bb2016-02-29 15:25:42 -0700379 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
Dileep Kattab1228202015-02-17 18:48:23 +0530380 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900381 pr_err("invalid mmc device");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000382 fastboot_fail("invalid mmc device", response);
Dileep Kattab1228202015-02-17 18:48:23 +0530383 return;
384 }
385
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200386 ret = part_get_info_by_name_or_alias(dev_desc, cmd, &info);
Alex Deymoe2f689f2017-04-02 01:49:50 -0700387 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900388 pr_err("cannot find partition: '%s'", cmd);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000389 fastboot_fail("cannot find partition", response);
Dileep Kattab1228202015-02-17 18:48:23 +0530390 return;
391 }
392
393 /* Align blocks to erase group size to avoid erasing other partitions */
394 grp_size = mmc->erase_grp_size;
395 blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
396 if (info.size >= grp_size)
397 blks_size = (info.size - (blks_start - info.start)) &
398 (~(grp_size - 1));
399 else
400 blks_size = 0;
401
402 printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
403 blks_start, blks_start + blks_size);
404
Xu Ziyuanec124ce2016-06-15 16:56:18 +0800405 blks = blk_derase(dev_desc, blks_start, blks_size);
Dileep Kattab1228202015-02-17 18:48:23 +0530406 if (blks != blks_size) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900407 pr_err("failed erasing from device %d", dev_desc->devnum);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000408 fastboot_fail("failed erasing from device", response);
Dileep Kattab1228202015-02-17 18:48:23 +0530409 return;
410 }
411
412 printf("........ erased " LBAFU " bytes from '%s'\n",
413 blks_size * info.blksz, cmd);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000414 fastboot_okay(NULL, response);
Dileep Kattab1228202015-02-17 18:48:23 +0530415}