Merge tag 'tpm-030822' of https://source.denx.de/u-boot/custodians/u-boot-tpm
[platform/kernel/u-boot.git] / drivers / fastboot / fb_mmc.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2014 Broadcom Corporation.
4  */
5
6 #include <config.h>
7 #include <common.h>
8 #include <blk.h>
9 #include <env.h>
10 #include <fastboot.h>
11 #include <fastboot-internal.h>
12 #include <fb_mmc.h>
13 #include <image-sparse.h>
14 #include <image.h>
15 #include <log.h>
16 #include <part.h>
17 #include <mmc.h>
18 #include <div64.h>
19 #include <linux/compat.h>
20 #include <android_image.h>
21
22 #define FASTBOOT_MAX_BLK_WRITE 16384
23
24 #define BOOT_PARTITION_NAME "boot"
25
26 struct fb_mmc_sparse {
27         struct blk_desc *dev_desc;
28 };
29
30 static int raw_part_get_info_by_name(struct blk_desc *dev_desc,
31                                      const char *name,
32                                      struct disk_partition *info)
33 {
34         /* strlen("fastboot_raw_partition_") + PART_NAME_LEN + 1 */
35         char env_desc_name[23 + PART_NAME_LEN + 1];
36         char *raw_part_desc;
37         const char *argv[2];
38         const char **parg = argv;
39
40         /* check for raw partition descriptor */
41         strcpy(env_desc_name, "fastboot_raw_partition_");
42         strlcat(env_desc_name, name, sizeof(env_desc_name));
43         raw_part_desc = strdup(env_get(env_desc_name));
44         if (raw_part_desc == NULL)
45                 return -ENODEV;
46
47         /*
48          * parse partition descriptor
49          *
50          * <lba_start> <lba_size> [mmcpart <num>]
51          */
52         for (; parg < argv + sizeof(argv) / sizeof(*argv); ++parg) {
53                 *parg = strsep(&raw_part_desc, " ");
54                 if (*parg == NULL) {
55                         pr_err("Invalid number of arguments.\n");
56                         return -ENODEV;
57                 }
58         }
59
60         info->start = simple_strtoul(argv[0], NULL, 0);
61         info->size = simple_strtoul(argv[1], NULL, 0);
62         info->blksz = dev_desc->blksz;
63         strlcpy((char *)info->name, name, PART_NAME_LEN);
64
65         if (raw_part_desc) {
66                 if (strcmp(strsep(&raw_part_desc, " "), "mmcpart") == 0) {
67                         ulong mmcpart = simple_strtoul(raw_part_desc, NULL, 0);
68                         int ret = blk_dselect_hwpart(dev_desc, mmcpart);
69
70                         if (ret)
71                                 return ret;
72                 }
73         }
74
75         return 0;
76 }
77
78 static int do_get_part_info(struct blk_desc **dev_desc, const char *name,
79                             struct disk_partition *info)
80 {
81         int ret;
82
83         /* First try partition names on the default device */
84         *dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
85         if (*dev_desc) {
86                 ret = part_get_info_by_name(*dev_desc, name, info);
87                 if (ret >= 0)
88                         return ret;
89
90                 /* Then try raw partitions */
91                 ret = raw_part_get_info_by_name(*dev_desc, name, info);
92                 if (ret >= 0)
93                         return ret;
94         }
95
96         /* Then try dev.hwpart:part */
97         ret = part_get_info_by_dev_and_name_or_num("mmc", name, dev_desc,
98                                                    info, true);
99         return ret;
100 }
101
102 static int part_get_info_by_name_or_alias(struct blk_desc **dev_desc,
103                                           const char *name,
104                                           struct disk_partition *info)
105 {
106         /* strlen("fastboot_partition_alias_") + PART_NAME_LEN + 1 */
107         char env_alias_name[25 + PART_NAME_LEN + 1];
108         char *aliased_part_name;
109
110         /* check for alias */
111         strlcpy(env_alias_name, "fastboot_partition_alias_", sizeof(env_alias_name));
112         strlcat(env_alias_name, name, sizeof(env_alias_name));
113         aliased_part_name = env_get(env_alias_name);
114         if (aliased_part_name)
115                 name = aliased_part_name;
116
117         return do_get_part_info(dev_desc, name, info);
118 }
119
120 /**
121  * fb_mmc_blk_write() - Write/erase MMC in chunks of FASTBOOT_MAX_BLK_WRITE
122  *
123  * @block_dev: Pointer to block device
124  * @start: First block to write/erase
125  * @blkcnt: Count of blocks
126  * @buffer: Pointer to data buffer for write or NULL for erase
127  */
128 static lbaint_t fb_mmc_blk_write(struct blk_desc *block_dev, lbaint_t start,
129                                  lbaint_t blkcnt, const void *buffer)
130 {
131         lbaint_t blk = start;
132         lbaint_t blks_written;
133         lbaint_t cur_blkcnt;
134         lbaint_t blks = 0;
135         int i;
136
137         for (i = 0; i < blkcnt; i += FASTBOOT_MAX_BLK_WRITE) {
138                 cur_blkcnt = min((int)blkcnt - i, FASTBOOT_MAX_BLK_WRITE);
139                 if (buffer) {
140                         if (fastboot_progress_callback)
141                                 fastboot_progress_callback("writing");
142                         blks_written = blk_dwrite(block_dev, blk, cur_blkcnt,
143                                                   buffer + (i * block_dev->blksz));
144                 } else {
145                         if (fastboot_progress_callback)
146                                 fastboot_progress_callback("erasing");
147                         blks_written = blk_derase(block_dev, blk, cur_blkcnt);
148                 }
149                 blk += blks_written;
150                 blks += blks_written;
151         }
152         return blks;
153 }
154
155 static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info,
156                 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
157 {
158         struct fb_mmc_sparse *sparse = info->priv;
159         struct blk_desc *dev_desc = sparse->dev_desc;
160
161         return fb_mmc_blk_write(dev_desc, blk, blkcnt, buffer);
162 }
163
164 static lbaint_t fb_mmc_sparse_reserve(struct sparse_storage *info,
165                 lbaint_t blk, lbaint_t blkcnt)
166 {
167         return blkcnt;
168 }
169
170 static void write_raw_image(struct blk_desc *dev_desc,
171                             struct disk_partition *info, const char *part_name,
172                             void *buffer, u32 download_bytes, char *response)
173 {
174         lbaint_t blkcnt;
175         lbaint_t blks;
176
177         /* determine number of blocks to write */
178         blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
179         blkcnt = lldiv(blkcnt, info->blksz);
180
181         if (blkcnt > info->size) {
182                 pr_err("too large for partition: '%s'\n", part_name);
183                 fastboot_fail("too large for partition", response);
184                 return;
185         }
186
187         puts("Flashing Raw Image\n");
188
189         blks = fb_mmc_blk_write(dev_desc, info->start, blkcnt, buffer);
190
191         if (blks != blkcnt) {
192                 pr_err("failed writing to device %d\n", dev_desc->devnum);
193                 fastboot_fail("failed writing to device", response);
194                 return;
195         }
196
197         printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
198                part_name);
199         fastboot_okay(NULL, response);
200 }
201
202 #if defined(CONFIG_FASTBOOT_MMC_BOOT_SUPPORT) || \
203         defined(CONFIG_FASTBOOT_MMC_USER_SUPPORT)
204 static int fb_mmc_erase_mmc_hwpart(struct blk_desc *dev_desc)
205 {
206         lbaint_t blks;
207
208         debug("Start Erasing mmc hwpart[%u]...\n", dev_desc->hwpart);
209
210         blks = fb_mmc_blk_write(dev_desc, 0, dev_desc->lba, NULL);
211
212         if (blks != dev_desc->lba) {
213                 pr_err("Failed to erase mmc hwpart[%u]\n", dev_desc->hwpart);
214                 return 1;
215         }
216
217         printf("........ erased %lu bytes from mmc hwpart[%u]\n",
218                dev_desc->lba * dev_desc->blksz, dev_desc->hwpart);
219
220         return 0;
221 }
222 #endif
223
224 #ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT
225 static void fb_mmc_boot_ops(struct blk_desc *dev_desc, void *buffer,
226                             int hwpart, u32 buff_sz, char *response)
227 {
228         lbaint_t blkcnt;
229         lbaint_t blks;
230         unsigned long blksz;
231
232         // To operate on EMMC_BOOT1/2 (mmc0boot0/1) we first change the hwpart
233         if (blk_dselect_hwpart(dev_desc, hwpart)) {
234                 pr_err("Failed to select hwpart\n");
235                 fastboot_fail("Failed to select hwpart", response);
236                 return;
237         }
238
239         if (buffer) { /* flash */
240
241                 /* determine number of blocks to write */
242                 blksz = dev_desc->blksz;
243                 blkcnt = ((buff_sz + (blksz - 1)) & ~(blksz - 1));
244                 blkcnt = lldiv(blkcnt, blksz);
245
246                 if (blkcnt > dev_desc->lba) {
247                         pr_err("Image size too large\n");
248                         fastboot_fail("Image size too large", response);
249                         return;
250                 }
251
252                 debug("Start Flashing Image to EMMC_BOOT%d...\n", hwpart);
253
254                 blks = fb_mmc_blk_write(dev_desc, 0, blkcnt, buffer);
255
256                 if (blks != blkcnt) {
257                         pr_err("Failed to write EMMC_BOOT%d\n", hwpart);
258                         fastboot_fail("Failed to write EMMC_BOOT part",
259                                       response);
260                         return;
261                 }
262
263                 printf("........ wrote %lu bytes to EMMC_BOOT%d\n",
264                        blkcnt * blksz, hwpart);
265         } else { /* erase */
266                 if (fb_mmc_erase_mmc_hwpart(dev_desc)) {
267                         pr_err("Failed to erase EMMC_BOOT%d\n", hwpart);
268                         fastboot_fail("Failed to erase EMMC_BOOT part",
269                                       response);
270                         return;
271                 }
272         }
273
274         fastboot_okay(NULL, response);
275 }
276 #endif
277
278 #ifdef CONFIG_ANDROID_BOOT_IMAGE
279 /**
280  * Read Android boot image header from boot partition.
281  *
282  * @param[in] dev_desc MMC device descriptor
283  * @param[in] info Boot partition info
284  * @param[out] hdr Where to store read boot image header
285  *
286  * Return: Boot image header sectors count or 0 on error
287  */
288 static lbaint_t fb_mmc_get_boot_header(struct blk_desc *dev_desc,
289                                        struct disk_partition *info,
290                                        struct andr_img_hdr *hdr,
291                                        char *response)
292 {
293         ulong sector_size;              /* boot partition sector size */
294         lbaint_t hdr_sectors;           /* boot image header sectors count */
295         int res;
296
297         /* Calculate boot image sectors count */
298         sector_size = info->blksz;
299         hdr_sectors = DIV_ROUND_UP(sizeof(struct andr_img_hdr), sector_size);
300         if (hdr_sectors == 0) {
301                 pr_err("invalid number of boot sectors: 0\n");
302                 fastboot_fail("invalid number of boot sectors: 0", response);
303                 return 0;
304         }
305
306         /* Read the boot image header */
307         res = blk_dread(dev_desc, info->start, hdr_sectors, (void *)hdr);
308         if (res != hdr_sectors) {
309                 pr_err("cannot read header from boot partition\n");
310                 fastboot_fail("cannot read header from boot partition",
311                               response);
312                 return 0;
313         }
314
315         /* Check boot header magic string */
316         res = android_image_check_header(hdr);
317         if (res != 0) {
318                 pr_err("bad boot image magic\n");
319                 fastboot_fail("boot partition not initialized", response);
320                 return 0;
321         }
322
323         return hdr_sectors;
324 }
325
326 /**
327  * Write downloaded zImage to boot partition and repack it properly.
328  *
329  * @param dev_desc MMC device descriptor
330  * @param download_buffer Address to fastboot buffer with zImage in it
331  * @param download_bytes Size of fastboot buffer, in bytes
332  *
333  * Return: 0 on success or -1 on error
334  */
335 static int fb_mmc_update_zimage(struct blk_desc *dev_desc,
336                                 void *download_buffer,
337                                 u32 download_bytes,
338                                 char *response)
339 {
340         uintptr_t hdr_addr;                     /* boot image header address */
341         struct andr_img_hdr *hdr;               /* boot image header */
342         lbaint_t hdr_sectors;                   /* boot image header sectors */
343         u8 *ramdisk_buffer;
344         u32 ramdisk_sector_start;
345         u32 ramdisk_sectors;
346         u32 kernel_sector_start;
347         u32 kernel_sectors;
348         u32 sectors_per_page;
349         struct disk_partition info;
350         int res;
351
352         puts("Flashing zImage\n");
353
354         /* Get boot partition info */
355         res = part_get_info_by_name(dev_desc, BOOT_PARTITION_NAME, &info);
356         if (res < 0) {
357                 pr_err("cannot find boot partition\n");
358                 fastboot_fail("cannot find boot partition", response);
359                 return -1;
360         }
361
362         /* Put boot image header in fastboot buffer after downloaded zImage */
363         hdr_addr = (uintptr_t)download_buffer + ALIGN(download_bytes, PAGE_SIZE);
364         hdr = (struct andr_img_hdr *)hdr_addr;
365
366         /* Read boot image header */
367         hdr_sectors = fb_mmc_get_boot_header(dev_desc, &info, hdr, response);
368         if (hdr_sectors == 0) {
369                 pr_err("unable to read boot image header\n");
370                 fastboot_fail("unable to read boot image header", response);
371                 return -1;
372         }
373
374         /* Check if boot image has second stage in it (we don't support it) */
375         if (hdr->second_size > 0) {
376                 pr_err("moving second stage is not supported yet\n");
377                 fastboot_fail("moving second stage is not supported yet",
378                               response);
379                 return -1;
380         }
381
382         /* Extract ramdisk location */
383         sectors_per_page = hdr->page_size / info.blksz;
384         ramdisk_sector_start = info.start + sectors_per_page;
385         ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
386                                              sectors_per_page;
387         ramdisk_sectors = DIV_ROUND_UP(hdr->ramdisk_size, hdr->page_size) *
388                                        sectors_per_page;
389
390         /* Read ramdisk and put it in fastboot buffer after boot image header */
391         ramdisk_buffer = (u8 *)hdr + (hdr_sectors * info.blksz);
392         res = blk_dread(dev_desc, ramdisk_sector_start, ramdisk_sectors,
393                         ramdisk_buffer);
394         if (res != ramdisk_sectors) {
395                 pr_err("cannot read ramdisk from boot partition\n");
396                 fastboot_fail("cannot read ramdisk from boot partition",
397                               response);
398                 return -1;
399         }
400
401         /* Write new kernel size to boot image header */
402         hdr->kernel_size = download_bytes;
403         res = blk_dwrite(dev_desc, info.start, hdr_sectors, (void *)hdr);
404         if (res == 0) {
405                 pr_err("cannot writeback boot image header\n");
406                 fastboot_fail("cannot write back boot image header", response);
407                 return -1;
408         }
409
410         /* Write the new downloaded kernel */
411         kernel_sector_start = info.start + sectors_per_page;
412         kernel_sectors = DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
413                                       sectors_per_page;
414         res = blk_dwrite(dev_desc, kernel_sector_start, kernel_sectors,
415                          download_buffer);
416         if (res == 0) {
417                 pr_err("cannot write new kernel\n");
418                 fastboot_fail("cannot write new kernel", response);
419                 return -1;
420         }
421
422         /* Write the saved ramdisk back */
423         ramdisk_sector_start = info.start + sectors_per_page;
424         ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
425                                              sectors_per_page;
426         res = blk_dwrite(dev_desc, ramdisk_sector_start, ramdisk_sectors,
427                          ramdisk_buffer);
428         if (res == 0) {
429                 pr_err("cannot write back original ramdisk\n");
430                 fastboot_fail("cannot write back original ramdisk", response);
431                 return -1;
432         }
433
434         puts("........ zImage was updated in boot partition\n");
435         fastboot_okay(NULL, response);
436         return 0;
437 }
438 #endif
439
440 /**
441  * fastboot_mmc_get_part_info() - Lookup eMMC partion by name
442  *
443  * @part_name: Named partition to lookup
444  * @dev_desc: Pointer to returned blk_desc pointer
445  * @part_info: Pointer to returned struct disk_partition
446  * @response: Pointer to fastboot response buffer
447  */
448 int fastboot_mmc_get_part_info(const char *part_name,
449                                struct blk_desc **dev_desc,
450                                struct disk_partition *part_info, char *response)
451 {
452         int ret;
453
454         if (!part_name || !strcmp(part_name, "")) {
455                 fastboot_fail("partition not given", response);
456                 return -ENOENT;
457         }
458
459         ret = part_get_info_by_name_or_alias(dev_desc, part_name, part_info);
460         if (ret < 0) {
461                 switch (ret) {
462                 case -ENOSYS:
463                 case -EINVAL:
464                         fastboot_fail("invalid partition or device", response);
465                         break;
466                 case -ENODEV:
467                         fastboot_fail("no such device", response);
468                         break;
469                 case -ENOENT:
470                         fastboot_fail("no such partition", response);
471                         break;
472                 case -EPROTONOSUPPORT:
473                         fastboot_fail("unknown partition table type", response);
474                         break;
475                 default:
476                         fastboot_fail("unanticipated error", response);
477                         break;
478                 }
479         }
480
481         return ret;
482 }
483
484 static struct blk_desc *fastboot_mmc_get_dev(char *response)
485 {
486         struct blk_desc *ret = blk_get_dev("mmc",
487                                            CONFIG_FASTBOOT_FLASH_MMC_DEV);
488
489         if (!ret || ret->type == DEV_TYPE_UNKNOWN) {
490                 pr_err("invalid mmc device\n");
491                 fastboot_fail("invalid mmc device", response);
492                 return NULL;
493         }
494         return ret;
495 }
496
497 /**
498  * fastboot_mmc_flash_write() - Write image to eMMC for fastboot
499  *
500  * @cmd: Named partition to write image to
501  * @download_buffer: Pointer to image data
502  * @download_bytes: Size of image data
503  * @response: Pointer to fastboot response buffer
504  */
505 void fastboot_mmc_flash_write(const char *cmd, void *download_buffer,
506                               u32 download_bytes, char *response)
507 {
508         struct blk_desc *dev_desc;
509         struct disk_partition info = {0};
510
511 #ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT
512         if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) {
513                 dev_desc = fastboot_mmc_get_dev(response);
514                 if (dev_desc)
515                         fb_mmc_boot_ops(dev_desc, download_buffer, 1,
516                                         download_bytes, response);
517                 return;
518         }
519         if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT2_NAME) == 0) {
520                 dev_desc = fastboot_mmc_get_dev(response);
521                 if (dev_desc)
522                         fb_mmc_boot_ops(dev_desc, download_buffer, 2,
523                                         download_bytes, response);
524                 return;
525         }
526 #endif
527
528 #if CONFIG_IS_ENABLED(EFI_PARTITION)
529         if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
530                 dev_desc = fastboot_mmc_get_dev(response);
531                 if (!dev_desc)
532                         return;
533
534                 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
535                        __func__);
536                 if (is_valid_gpt_buf(dev_desc, download_buffer)) {
537                         printf("%s: invalid GPT - refusing to write to flash\n",
538                                __func__);
539                         fastboot_fail("invalid GPT partition", response);
540                         return;
541                 }
542                 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
543                         printf("%s: writing GPT partitions failed\n", __func__);
544                         fastboot_fail("writing GPT partitions failed",
545                                       response);
546                         return;
547                 }
548                 part_init(dev_desc);
549                 printf("........ success\n");
550                 fastboot_okay(NULL, response);
551                 return;
552         }
553 #endif
554
555 #if CONFIG_IS_ENABLED(DOS_PARTITION)
556         if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) {
557                 dev_desc = fastboot_mmc_get_dev(response);
558                 if (!dev_desc)
559                         return;
560
561                 printf("%s: updating MBR\n", __func__);
562                 if (is_valid_dos_buf(download_buffer)) {
563                         printf("%s: invalid MBR - refusing to write to flash\n",
564                                __func__);
565                         fastboot_fail("invalid MBR partition", response);
566                         return;
567                 }
568                 if (write_mbr_sector(dev_desc, download_buffer)) {
569                         printf("%s: writing MBR partition failed\n", __func__);
570                         fastboot_fail("writing MBR partition failed",
571                                       response);
572                         return;
573                 }
574                 part_init(dev_desc);
575                 printf("........ success\n");
576                 fastboot_okay(NULL, response);
577                 return;
578         }
579 #endif
580
581 #ifdef CONFIG_ANDROID_BOOT_IMAGE
582         if (strncasecmp(cmd, "zimage", 6) == 0) {
583                 dev_desc = fastboot_mmc_get_dev(response);
584                 if (dev_desc)
585                         fb_mmc_update_zimage(dev_desc, download_buffer,
586                                              download_bytes, response);
587                 return;
588         }
589 #endif
590
591 #if CONFIG_IS_ENABLED(FASTBOOT_MMC_USER_SUPPORT)
592         if (strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) {
593                 dev_desc = fastboot_mmc_get_dev(response);
594                 if (!dev_desc)
595                         return;
596
597                 strlcpy((char *)&info.name, cmd, sizeof(info.name));
598                 info.size       = dev_desc->lba;
599                 info.blksz      = dev_desc->blksz;
600         }
601 #endif
602
603         if (!info.name[0] &&
604             fastboot_mmc_get_part_info(cmd, &dev_desc, &info, response) < 0)
605                 return;
606
607         if (is_sparse_image(download_buffer)) {
608                 struct fb_mmc_sparse sparse_priv;
609                 struct sparse_storage sparse;
610                 int err;
611
612                 sparse_priv.dev_desc = dev_desc;
613
614                 sparse.blksz = info.blksz;
615                 sparse.start = info.start;
616                 sparse.size = info.size;
617                 sparse.write = fb_mmc_sparse_write;
618                 sparse.reserve = fb_mmc_sparse_reserve;
619                 sparse.mssg = fastboot_fail;
620
621                 printf("Flashing sparse image at offset " LBAFU "\n",
622                        sparse.start);
623
624                 sparse.priv = &sparse_priv;
625                 err = write_sparse_image(&sparse, cmd, download_buffer,
626                                          response);
627                 if (!err)
628                         fastboot_okay(NULL, response);
629         } else {
630                 write_raw_image(dev_desc, &info, cmd, download_buffer,
631                                 download_bytes, response);
632         }
633 }
634
635 /**
636  * fastboot_mmc_flash_erase() - Erase eMMC for fastboot
637  *
638  * @cmd: Named partition to erase
639  * @response: Pointer to fastboot response buffer
640  */
641 void fastboot_mmc_erase(const char *cmd, char *response)
642 {
643         struct blk_desc *dev_desc;
644         struct disk_partition info;
645         lbaint_t blks, blks_start, blks_size, grp_size;
646         struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
647
648 #ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT
649         if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) {
650                 /* erase EMMC boot1 */
651                 dev_desc = fastboot_mmc_get_dev(response);
652                 if (dev_desc)
653                         fb_mmc_boot_ops(dev_desc, NULL, 1, 0, response);
654                 return;
655         }
656         if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT2_NAME) == 0) {
657                 /* erase EMMC boot2 */
658                 dev_desc = fastboot_mmc_get_dev(response);
659                 if (dev_desc)
660                         fb_mmc_boot_ops(dev_desc, NULL, 2, 0, response);
661                 return;
662         }
663 #endif
664
665 #ifdef CONFIG_FASTBOOT_MMC_USER_SUPPORT
666         if (strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) {
667                 /* erase EMMC userdata */
668                 dev_desc = fastboot_mmc_get_dev(response);
669                 if (!dev_desc)
670                         return;
671
672                 if (fb_mmc_erase_mmc_hwpart(dev_desc))
673                         fastboot_fail("Failed to erase EMMC_USER", response);
674                 else
675                         fastboot_okay(NULL, response);
676                 return;
677         }
678 #endif
679
680         if (fastboot_mmc_get_part_info(cmd, &dev_desc, &info, response) < 0)
681                 return;
682
683         /* Align blocks to erase group size to avoid erasing other partitions */
684         grp_size = mmc->erase_grp_size;
685         blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
686         if (info.size >= grp_size)
687                 blks_size = (info.size - (blks_start - info.start)) &
688                                 (~(grp_size - 1));
689         else
690                 blks_size = 0;
691
692         printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
693                blks_start, blks_start + blks_size);
694
695         blks = fb_mmc_blk_write(dev_desc, blks_start, blks_size, NULL);
696
697         if (blks != blks_size) {
698                 pr_err("failed erasing from device %d\n", dev_desc->devnum);
699                 fastboot_fail("failed erasing from device", response);
700                 return;
701         }
702
703         printf("........ erased " LBAFU " bytes from '%s'\n",
704                blks_size * info.blksz, cmd);
705         fastboot_okay(NULL, response);
706 }