blob: 47fcf74b6973e425fe1b4ef6b8725d9ed5aabe65 [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
Sam Protsenko540eb952017-05-18 15:04:59 +030018#define BOOT_PARTITION_NAME "boot"
19
Maxime Riparda58e9c72015-10-15 14:34:14 +020020struct fb_mmc_sparse {
Simon Glasse3394752016-02-29 15:25:34 -070021 struct blk_desc *dev_desc;
Maxime Riparda58e9c72015-10-15 14:34:14 +020022};
23
Petr Kulhavy9f174c92016-09-09 10:27:16 +020024static int part_get_info_by_name_or_alias(struct blk_desc *dev_desc,
Michael Scottced33492015-03-11 10:02:31 -070025 const char *name, disk_partition_t *info)
26{
27 int ret;
28
Petr Kulhavy712257e2016-09-09 10:27:15 +020029 ret = part_get_info_by_name(dev_desc, name, info);
Alex Deymoe2f689f2017-04-02 01:49:50 -070030 if (ret < 0) {
Michael Scottced33492015-03-11 10:02:31 -070031 /* strlen("fastboot_partition_alias_") + 32(part_name) + 1 */
32 char env_alias_name[25 + 32 + 1];
33 char *aliased_part_name;
34
35 /* check for alias */
36 strcpy(env_alias_name, "fastboot_partition_alias_");
37 strncat(env_alias_name, name, 32);
Simon Glass64b723f2017-08-03 12:22:12 -060038 aliased_part_name = env_get(env_alias_name);
Michael Scottced33492015-03-11 10:02:31 -070039 if (aliased_part_name != NULL)
Petr Kulhavy712257e2016-09-09 10:27:15 +020040 ret = part_get_info_by_name(dev_desc,
Michael Scottced33492015-03-11 10:02:31 -070041 aliased_part_name, info);
42 }
43 return ret;
44}
45
Steve Rae8cf58db2016-06-07 11:19:36 -070046static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info,
47 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
Maxime Riparda58e9c72015-10-15 14:34:14 +020048{
Steve Rae8cf58db2016-06-07 11:19:36 -070049 struct fb_mmc_sparse *sparse = info->priv;
Simon Glasse3394752016-02-29 15:25:34 -070050 struct blk_desc *dev_desc = sparse->dev_desc;
Maxime Riparda58e9c72015-10-15 14:34:14 +020051
Steve Rae8cf58db2016-06-07 11:19:36 -070052 return blk_dwrite(dev_desc, blk, blkcnt, buffer);
Maxime Riparda58e9c72015-10-15 14:34:14 +020053}
54
Steve Rae8a99be82016-06-07 11:19:38 -070055static lbaint_t fb_mmc_sparse_reserve(struct sparse_storage *info,
56 lbaint_t blk, lbaint_t blkcnt)
57{
58 return blkcnt;
59}
60
Simon Glasse3394752016-02-29 15:25:34 -070061static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info,
Steve Rae792ac482014-08-26 11:47:27 -070062 const char *part_name, void *buffer,
Alex Kiernan27b69de2018-05-29 15:30:40 +000063 unsigned int download_bytes, char *response)
Steve Rae792ac482014-08-26 11:47:27 -070064{
65 lbaint_t blkcnt;
66 lbaint_t blks;
67
68 /* determine number of blocks to write */
69 blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
Siarhei Siamashka58f30d82015-10-28 06:24:16 +020070 blkcnt = lldiv(blkcnt, info->blksz);
Steve Rae792ac482014-08-26 11:47:27 -070071
72 if (blkcnt > info->size) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090073 pr_err("too large for partition: '%s'\n", part_name);
Alex Kiernan27b69de2018-05-29 15:30:40 +000074 fastboot_fail("too large for partition", response);
Steve Rae792ac482014-08-26 11:47:27 -070075 return;
76 }
77
78 puts("Flashing Raw Image\n");
79
Simon Glass2ee8ada2016-02-29 15:25:52 -070080 blks = blk_dwrite(dev_desc, info->start, blkcnt, buffer);
Steve Rae792ac482014-08-26 11:47:27 -070081 if (blks != blkcnt) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090082 pr_err("failed writing to device %d\n", dev_desc->devnum);
Alex Kiernan27b69de2018-05-29 15:30:40 +000083 fastboot_fail("failed writing to device", response);
Steve Rae792ac482014-08-26 11:47:27 -070084 return;
85 }
86
87 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
88 part_name);
Alex Kiernan27b69de2018-05-29 15:30:40 +000089 fastboot_okay(NULL, response);
Steve Rae792ac482014-08-26 11:47:27 -070090}
91
Sam Protsenko540eb952017-05-18 15:04:59 +030092#ifdef CONFIG_ANDROID_BOOT_IMAGE
93/**
94 * Read Android boot image header from boot partition.
95 *
96 * @param[in] dev_desc MMC device descriptor
97 * @param[in] info Boot partition info
98 * @param[out] hdr Where to store read boot image header
99 *
100 * @return Boot image header sectors count or 0 on error
101 */
102static lbaint_t fb_mmc_get_boot_header(struct blk_desc *dev_desc,
103 disk_partition_t *info,
Alex Kiernan27b69de2018-05-29 15:30:40 +0000104 struct andr_img_hdr *hdr,
105 char *response)
Sam Protsenko540eb952017-05-18 15:04:59 +0300106{
107 ulong sector_size; /* boot partition sector size */
108 lbaint_t hdr_sectors; /* boot image header sectors count */
109 int res;
110
111 /* Calculate boot image sectors count */
112 sector_size = info->blksz;
113 hdr_sectors = DIV_ROUND_UP(sizeof(struct andr_img_hdr), sector_size);
114 if (hdr_sectors == 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000115 pr_err("invalid number of boot sectors: 0\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000116 fastboot_fail("invalid number of boot sectors: 0", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300117 return 0;
118 }
119
120 /* Read the boot image header */
121 res = blk_dread(dev_desc, info->start, hdr_sectors, (void *)hdr);
Tom Rinibda6f772017-08-14 21:00:44 -0400122 if (res != hdr_sectors) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000123 pr_err("cannot read header from boot partition\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000124 fastboot_fail("cannot read header from boot partition",
125 response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300126 return 0;
127 }
128
129 /* Check boot header magic string */
130 res = android_image_check_header(hdr);
131 if (res != 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000132 pr_err("bad boot image magic\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000133 fastboot_fail("boot partition not initialized", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300134 return 0;
135 }
136
137 return hdr_sectors;
138}
139
140/**
141 * Write downloaded zImage to boot partition and repack it properly.
142 *
143 * @param dev_desc MMC device descriptor
144 * @param download_buffer Address to fastboot buffer with zImage in it
145 * @param download_bytes Size of fastboot buffer, in bytes
146 *
147 * @return 0 on success or -1 on error
148 */
149static int fb_mmc_update_zimage(struct blk_desc *dev_desc,
150 void *download_buffer,
Alex Kiernan27b69de2018-05-29 15:30:40 +0000151 unsigned int download_bytes,
152 char *response)
Sam Protsenko540eb952017-05-18 15:04:59 +0300153{
Tom Rini0ac605c2017-06-10 09:15:37 -0400154 uintptr_t hdr_addr; /* boot image header address */
Sam Protsenko540eb952017-05-18 15:04:59 +0300155 struct andr_img_hdr *hdr; /* boot image header */
156 lbaint_t hdr_sectors; /* boot image header sectors */
157 u8 *ramdisk_buffer;
158 u32 ramdisk_sector_start;
159 u32 ramdisk_sectors;
160 u32 kernel_sector_start;
161 u32 kernel_sectors;
162 u32 sectors_per_page;
163 disk_partition_t info;
164 int res;
165
166 puts("Flashing zImage\n");
167
168 /* Get boot partition info */
169 res = part_get_info_by_name(dev_desc, BOOT_PARTITION_NAME, &info);
170 if (res < 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000171 pr_err("cannot find boot partition\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000172 fastboot_fail("cannot find boot partition", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300173 return -1;
174 }
175
176 /* Put boot image header in fastboot buffer after downloaded zImage */
Tom Rini0ac605c2017-06-10 09:15:37 -0400177 hdr_addr = (uintptr_t)download_buffer + ALIGN(download_bytes, PAGE_SIZE);
Sam Protsenko540eb952017-05-18 15:04:59 +0300178 hdr = (struct andr_img_hdr *)hdr_addr;
179
180 /* Read boot image header */
Alex Kiernan27b69de2018-05-29 15:30:40 +0000181 hdr_sectors = fb_mmc_get_boot_header(dev_desc, &info, hdr, response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300182 if (hdr_sectors == 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000183 pr_err("unable to read boot image header\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000184 fastboot_fail("unable to read boot image header", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300185 return -1;
186 }
187
188 /* Check if boot image has second stage in it (we don't support it) */
189 if (hdr->second_size > 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000190 pr_err("moving second stage is not supported yet\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000191 fastboot_fail("moving second stage is not supported yet",
192 response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300193 return -1;
194 }
195
196 /* Extract ramdisk location */
197 sectors_per_page = hdr->page_size / info.blksz;
198 ramdisk_sector_start = info.start + sectors_per_page;
199 ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
200 sectors_per_page;
201 ramdisk_sectors = DIV_ROUND_UP(hdr->ramdisk_size, hdr->page_size) *
202 sectors_per_page;
203
204 /* Read ramdisk and put it in fastboot buffer after boot image header */
205 ramdisk_buffer = (u8 *)hdr + (hdr_sectors * info.blksz);
206 res = blk_dread(dev_desc, ramdisk_sector_start, ramdisk_sectors,
207 ramdisk_buffer);
Tom Rinibda6f772017-08-14 21:00:44 -0400208 if (res != ramdisk_sectors) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000209 pr_err("cannot read ramdisk from boot partition\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000210 fastboot_fail("cannot read ramdisk from boot partition",
211 response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300212 return -1;
213 }
214
215 /* Write new kernel size to boot image header */
216 hdr->kernel_size = download_bytes;
217 res = blk_dwrite(dev_desc, info.start, hdr_sectors, (void *)hdr);
218 if (res == 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000219 pr_err("cannot writeback boot image header\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000220 fastboot_fail("cannot write back boot image header", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300221 return -1;
222 }
223
224 /* Write the new downloaded kernel */
225 kernel_sector_start = info.start + sectors_per_page;
226 kernel_sectors = DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
227 sectors_per_page;
228 res = blk_dwrite(dev_desc, kernel_sector_start, kernel_sectors,
229 download_buffer);
230 if (res == 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000231 pr_err("cannot write new kernel\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000232 fastboot_fail("cannot write new kernel", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300233 return -1;
234 }
235
236 /* Write the saved ramdisk back */
237 ramdisk_sector_start = info.start + sectors_per_page;
238 ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
239 sectors_per_page;
240 res = blk_dwrite(dev_desc, ramdisk_sector_start, ramdisk_sectors,
241 ramdisk_buffer);
242 if (res == 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000243 pr_err("cannot write back original ramdisk\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000244 fastboot_fail("cannot write back original ramdisk", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300245 return -1;
246 }
247
248 puts("........ zImage was updated in boot partition\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000249 fastboot_okay(NULL, response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300250 return 0;
251}
252#endif
253
Alex Kiernan32c21942018-05-29 15:30:48 +0000254/**
255 * fastboot_mmc_flash_write() - Write image to eMMC for fastboot
256 *
257 * @cmd: Named partition to write image to
258 * @download_buffer: Pointer to image data
259 * @download_bytes: Size of image data
260 * @response: Pointer to fastboot response buffer
261 */
262void fastboot_mmc_flash_write(const char *cmd, void *download_buffer,
263 unsigned int download_bytes, char *response)
Steve Rae792ac482014-08-26 11:47:27 -0700264{
Simon Glasse3394752016-02-29 15:25:34 -0700265 struct blk_desc *dev_desc;
Steve Rae792ac482014-08-26 11:47:27 -0700266 disk_partition_t info;
267
Simon Glassbe1e9bb2016-02-29 15:25:42 -0700268 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
Steve Rae792ac482014-08-26 11:47:27 -0700269 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900270 pr_err("invalid mmc device\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000271 fastboot_fail("invalid mmc device", response);
Steve Rae792ac482014-08-26 11:47:27 -0700272 return;
273 }
274
Patrick Delaunay8a4f2bd2017-01-27 11:00:41 +0100275#if CONFIG_IS_ENABLED(EFI_PARTITION)
Steve Rae7d059342014-12-12 15:51:54 -0800276 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
277 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
278 __func__);
279 if (is_valid_gpt_buf(dev_desc, download_buffer)) {
280 printf("%s: invalid GPT - refusing to write to flash\n",
281 __func__);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000282 fastboot_fail("invalid GPT partition", response);
Steve Rae7d059342014-12-12 15:51:54 -0800283 return;
284 }
285 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
286 printf("%s: writing GPT partitions failed\n", __func__);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000287 fastboot_fail("writing GPT partitions failed",
288 response);
Steve Rae7d059342014-12-12 15:51:54 -0800289 return;
290 }
291 printf("........ success\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000292 fastboot_okay(NULL, response);
Steve Rae7d059342014-12-12 15:51:54 -0800293 return;
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200294 }
295#endif
296
Patrick Delaunayf7e07722017-01-27 11:00:37 +0100297#if CONFIG_IS_ENABLED(DOS_PARTITION)
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200298 if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) {
299 printf("%s: updating MBR\n", __func__);
300 if (is_valid_dos_buf(download_buffer)) {
301 printf("%s: invalid MBR - refusing to write to flash\n",
302 __func__);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000303 fastboot_fail("invalid MBR partition", response);
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200304 return;
305 }
306 if (write_mbr_partition(dev_desc, download_buffer)) {
307 printf("%s: writing MBR partition failed\n", __func__);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000308 fastboot_fail("writing MBR partition failed",
309 response);
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200310 return;
311 }
312 printf("........ success\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000313 fastboot_okay(NULL, response);
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200314 return;
315 }
316#endif
317
Sam Protsenko540eb952017-05-18 15:04:59 +0300318#ifdef CONFIG_ANDROID_BOOT_IMAGE
319 if (strncasecmp(cmd, "zimage", 6) == 0) {
Alex Kiernan27b69de2018-05-29 15:30:40 +0000320 fb_mmc_update_zimage(dev_desc, download_buffer,
321 download_bytes, response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300322 return;
323 }
324#endif
325
Alex Deymoe2f689f2017-04-02 01:49:50 -0700326 if (part_get_info_by_name_or_alias(dev_desc, cmd, &info) < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900327 pr_err("cannot find partition: '%s'\n", cmd);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000328 fastboot_fail("cannot find partition", response);
Steve Rae792ac482014-08-26 11:47:27 -0700329 return;
330 }
331
Maxime Riparda58e9c72015-10-15 14:34:14 +0200332 if (is_sparse_image(download_buffer)) {
333 struct fb_mmc_sparse sparse_priv;
Steve Rae8cf58db2016-06-07 11:19:36 -0700334 struct sparse_storage sparse;
Jassi Brar427e6692018-04-06 12:05:09 +0530335 int err;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200336
337 sparse_priv.dev_desc = dev_desc;
338
Steve Rae8cf58db2016-06-07 11:19:36 -0700339 sparse.blksz = info.blksz;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200340 sparse.start = info.start;
341 sparse.size = info.size;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200342 sparse.write = fb_mmc_sparse_write;
Steve Rae8a99be82016-06-07 11:19:38 -0700343 sparse.reserve = fb_mmc_sparse_reserve;
Jassi Brar427e6692018-04-06 12:05:09 +0530344 sparse.mssg = fastboot_fail;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200345
346 printf("Flashing sparse image at offset " LBAFU "\n",
Steve Rae8cf58db2016-06-07 11:19:36 -0700347 sparse.start);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200348
Steve Rae8cf58db2016-06-07 11:19:36 -0700349 sparse.priv = &sparse_priv;
Alex Kiernan27b69de2018-05-29 15:30:40 +0000350 err = write_sparse_image(&sparse, cmd, download_buffer,
351 response);
Jassi Brar427e6692018-04-06 12:05:09 +0530352 if (!err)
Alex Kiernan27b69de2018-05-29 15:30:40 +0000353 fastboot_okay(NULL, response);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200354 } else {
Steve Raec6015672014-08-26 11:47:30 -0700355 write_raw_image(dev_desc, &info, cmd, download_buffer,
Alex Kiernan27b69de2018-05-29 15:30:40 +0000356 download_bytes, response);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200357 }
Steve Rae792ac482014-08-26 11:47:27 -0700358}
Dileep Kattab1228202015-02-17 18:48:23 +0530359
Alex Kiernan32c21942018-05-29 15:30:48 +0000360/**
361 * fastboot_mmc_flash_erase() - Erase eMMC for fastboot
362 *
363 * @cmd: Named partition to erase
364 * @response: Pointer to fastboot response buffer
365 */
366void fastboot_mmc_erase(const char *cmd, char *response)
Dileep Kattab1228202015-02-17 18:48:23 +0530367{
368 int ret;
Simon Glasse3394752016-02-29 15:25:34 -0700369 struct blk_desc *dev_desc;
Dileep Kattab1228202015-02-17 18:48:23 +0530370 disk_partition_t info;
371 lbaint_t blks, blks_start, blks_size, grp_size;
372 struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
373
374 if (mmc == NULL) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000375 pr_err("invalid mmc device\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000376 fastboot_fail("invalid mmc device", response);
Dileep Kattab1228202015-02-17 18:48:23 +0530377 return;
378 }
379
Simon Glassbe1e9bb2016-02-29 15:25:42 -0700380 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
Dileep Kattab1228202015-02-17 18:48:23 +0530381 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000382 pr_err("invalid mmc device\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000383 fastboot_fail("invalid mmc device", response);
Dileep Kattab1228202015-02-17 18:48:23 +0530384 return;
385 }
386
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200387 ret = part_get_info_by_name_or_alias(dev_desc, cmd, &info);
Alex Deymoe2f689f2017-04-02 01:49:50 -0700388 if (ret < 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000389 pr_err("cannot find partition: '%s'\n", cmd);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000390 fastboot_fail("cannot find partition", response);
Dileep Kattab1228202015-02-17 18:48:23 +0530391 return;
392 }
393
394 /* Align blocks to erase group size to avoid erasing other partitions */
395 grp_size = mmc->erase_grp_size;
396 blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
397 if (info.size >= grp_size)
398 blks_size = (info.size - (blks_start - info.start)) &
399 (~(grp_size - 1));
400 else
401 blks_size = 0;
402
403 printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
404 blks_start, blks_start + blks_size);
405
Xu Ziyuanec124ce2016-06-15 16:56:18 +0800406 blks = blk_derase(dev_desc, blks_start, blks_size);
Dileep Kattab1228202015-02-17 18:48:23 +0530407 if (blks != blks_size) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000408 pr_err("failed erasing from device %d\n", dev_desc->devnum);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000409 fastboot_fail("failed erasing from device", response);
Dileep Kattab1228202015-02-17 18:48:23 +0530410 return;
411 }
412
413 printf("........ erased " LBAFU " bytes from '%s'\n",
414 blks_size * info.blksz, cmd);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000415 fastboot_okay(NULL, response);
Dileep Kattab1228202015-02-17 18:48:23 +0530416}