blob: c666eb0f29b032da78c7254516ea2c439d672bca [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>
Simon Glass0af6e2d2019-08-01 09:46:52 -06009#include <env.h>
Maxime Ripard00e8eb72015-10-15 14:34:13 +020010#include <fastboot.h>
Alex Kiernand5aa57c2018-05-29 15:30:53 +000011#include <fastboot-internal.h>
Steve Rae792ac482014-08-26 11:47:27 -070012#include <fb_mmc.h>
Simon Glass8e201882020-05-10 11:39:54 -060013#include <flash.h>
Maxime Ripard6af49bc2015-10-15 14:34:19 +020014#include <image-sparse.h>
Steve Rae792ac482014-08-26 11:47:27 -070015#include <part.h>
Dileep Kattab1228202015-02-17 18:48:23 +053016#include <mmc.h>
Siarhei Siamashka58f30d82015-10-28 06:24:16 +020017#include <div64.h>
Sam Protsenko540eb952017-05-18 15:04:59 +030018#include <linux/compat.h>
19#include <android_image.h>
Steve Rae792ac482014-08-26 11:47:27 -070020
Alex Kiernand5aa57c2018-05-29 15:30:53 +000021#define FASTBOOT_MAX_BLK_WRITE 16384
22
Sam Protsenko540eb952017-05-18 15:04:59 +030023#define BOOT_PARTITION_NAME "boot"
24
Maxime Riparda58e9c72015-10-15 14:34:14 +020025struct fb_mmc_sparse {
Simon Glasse3394752016-02-29 15:25:34 -070026 struct blk_desc *dev_desc;
Maxime Riparda58e9c72015-10-15 14:34:14 +020027};
28
Petr Kulhavy9f174c92016-09-09 10:27:16 +020029static int part_get_info_by_name_or_alias(struct blk_desc *dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -060030 const char *name, struct disk_partition *info)
Michael Scottced33492015-03-11 10:02:31 -070031{
32 int ret;
33
Petr Kulhavy712257e2016-09-09 10:27:15 +020034 ret = part_get_info_by_name(dev_desc, name, info);
Alex Deymoe2f689f2017-04-02 01:49:50 -070035 if (ret < 0) {
Alex Kiernan50f12232019-04-09 05:30:05 +000036 /* strlen("fastboot_partition_alias_") + PART_NAME_LEN + 1 */
37 char env_alias_name[25 + PART_NAME_LEN + 1];
Michael Scottced33492015-03-11 10:02:31 -070038 char *aliased_part_name;
39
40 /* check for alias */
41 strcpy(env_alias_name, "fastboot_partition_alias_");
Alex Kiernan50f12232019-04-09 05:30:05 +000042 strncat(env_alias_name, name, PART_NAME_LEN);
Simon Glass64b723f2017-08-03 12:22:12 -060043 aliased_part_name = env_get(env_alias_name);
Michael Scottced33492015-03-11 10:02:31 -070044 if (aliased_part_name != NULL)
Petr Kulhavy712257e2016-09-09 10:27:15 +020045 ret = part_get_info_by_name(dev_desc,
Michael Scottced33492015-03-11 10:02:31 -070046 aliased_part_name, info);
47 }
48 return ret;
49}
50
Alex Kiernand5aa57c2018-05-29 15:30:53 +000051/**
52 * fb_mmc_blk_write() - Write/erase MMC in chunks of FASTBOOT_MAX_BLK_WRITE
53 *
54 * @block_dev: Pointer to block device
55 * @start: First block to write/erase
56 * @blkcnt: Count of blocks
57 * @buffer: Pointer to data buffer for write or NULL for erase
58 */
59static lbaint_t fb_mmc_blk_write(struct blk_desc *block_dev, lbaint_t start,
60 lbaint_t blkcnt, const void *buffer)
61{
62 lbaint_t blk = start;
63 lbaint_t blks_written;
64 lbaint_t cur_blkcnt;
65 lbaint_t blks = 0;
66 int i;
67
68 for (i = 0; i < blkcnt; i += FASTBOOT_MAX_BLK_WRITE) {
69 cur_blkcnt = min((int)blkcnt - i, FASTBOOT_MAX_BLK_WRITE);
70 if (buffer) {
71 if (fastboot_progress_callback)
72 fastboot_progress_callback("writing");
73 blks_written = blk_dwrite(block_dev, blk, cur_blkcnt,
74 buffer + (i * block_dev->blksz));
75 } else {
76 if (fastboot_progress_callback)
77 fastboot_progress_callback("erasing");
78 blks_written = blk_derase(block_dev, blk, cur_blkcnt);
79 }
80 blk += blks_written;
81 blks += blks_written;
82 }
83 return blks;
84}
85
Steve Rae8cf58db2016-06-07 11:19:36 -070086static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info,
87 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
Maxime Riparda58e9c72015-10-15 14:34:14 +020088{
Steve Rae8cf58db2016-06-07 11:19:36 -070089 struct fb_mmc_sparse *sparse = info->priv;
Simon Glasse3394752016-02-29 15:25:34 -070090 struct blk_desc *dev_desc = sparse->dev_desc;
Maxime Riparda58e9c72015-10-15 14:34:14 +020091
Alex Kiernand5aa57c2018-05-29 15:30:53 +000092 return fb_mmc_blk_write(dev_desc, blk, blkcnt, buffer);
Maxime Riparda58e9c72015-10-15 14:34:14 +020093}
94
Steve Rae8a99be82016-06-07 11:19:38 -070095static lbaint_t fb_mmc_sparse_reserve(struct sparse_storage *info,
96 lbaint_t blk, lbaint_t blkcnt)
97{
98 return blkcnt;
99}
100
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600101static void write_raw_image(struct blk_desc *dev_desc,
102 struct disk_partition *info, const char *part_name,
103 void *buffer, u32 download_bytes, char *response)
Steve Rae792ac482014-08-26 11:47:27 -0700104{
105 lbaint_t blkcnt;
106 lbaint_t blks;
107
108 /* determine number of blocks to write */
109 blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
Siarhei Siamashka58f30d82015-10-28 06:24:16 +0200110 blkcnt = lldiv(blkcnt, info->blksz);
Steve Rae792ac482014-08-26 11:47:27 -0700111
112 if (blkcnt > info->size) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900113 pr_err("too large for partition: '%s'\n", part_name);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000114 fastboot_fail("too large for partition", response);
Steve Rae792ac482014-08-26 11:47:27 -0700115 return;
116 }
117
118 puts("Flashing Raw Image\n");
119
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000120 blks = fb_mmc_blk_write(dev_desc, info->start, blkcnt, buffer);
121
Steve Rae792ac482014-08-26 11:47:27 -0700122 if (blks != blkcnt) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900123 pr_err("failed writing to device %d\n", dev_desc->devnum);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000124 fastboot_fail("failed writing to device", response);
Steve Rae792ac482014-08-26 11:47:27 -0700125 return;
126 }
127
128 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
129 part_name);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000130 fastboot_okay(NULL, response);
Steve Rae792ac482014-08-26 11:47:27 -0700131}
132
developer6a105562020-01-16 16:11:42 +0800133#ifdef CONFIG_FASTBOOT_MMC_BOOT1_SUPPORT
134static int fb_mmc_erase_mmc_hwpart(struct blk_desc *dev_desc)
135{
136 lbaint_t blks;
137
138 debug("Start Erasing mmc hwpart[%u]...\n", dev_desc->hwpart);
139
140 blks = fb_mmc_blk_write(dev_desc, 0, dev_desc->lba, NULL);
141
142 if (blks != dev_desc->lba) {
143 pr_err("Failed to erase mmc hwpart[%u]\n", dev_desc->hwpart);
144 return 1;
145 }
146
147 printf("........ erased %lu bytes from mmc hwpart[%u]\n",
148 dev_desc->lba * dev_desc->blksz, dev_desc->hwpart);
149
150 return 0;
151}
152
153static void fb_mmc_boot1_ops(struct blk_desc *dev_desc, void *buffer,
154 u32 buff_sz, char *response)
155{
156 lbaint_t blkcnt;
157 lbaint_t blks;
158 unsigned long blksz;
159
160 // To operate on EMMC_BOOT1 (mmc0boot0), we first change the hwpart
161 if (blk_dselect_hwpart(dev_desc, 1)) {
162 pr_err("Failed to select hwpart\n");
163 fastboot_fail("Failed to select hwpart", response);
164 return;
165 }
166
167 if (buffer) { /* flash */
168
169 /* determine number of blocks to write */
170 blksz = dev_desc->blksz;
171 blkcnt = ((buff_sz + (blksz - 1)) & ~(blksz - 1));
172 blkcnt = lldiv(blkcnt, blksz);
173
174 if (blkcnt > dev_desc->lba) {
175 pr_err("Image size too large\n");
176 fastboot_fail("Image size too large", response);
177 return;
178 }
179
180 debug("Start Flashing Image to EMMC_BOOT1...\n");
181
182 blks = fb_mmc_blk_write(dev_desc, 0, blkcnt, buffer);
183
184 if (blks != blkcnt) {
185 pr_err("Failed to write EMMC_BOOT1\n");
186 fastboot_fail("Failed to write EMMC_BOOT1", response);
187 return;
188 }
189
190 printf("........ wrote %lu bytes to EMMC_BOOT1\n",
191 blkcnt * blksz);
192 } else { /* erase */
193 if (fb_mmc_erase_mmc_hwpart(dev_desc)) {
194 fastboot_fail("Failed to erase EMMC_BOOT1", response);
195 return;
196 }
197 }
198
199 fastboot_okay(NULL, response);
200}
201#endif
202
Sam Protsenko540eb952017-05-18 15:04:59 +0300203#ifdef CONFIG_ANDROID_BOOT_IMAGE
204/**
205 * Read Android boot image header from boot partition.
206 *
207 * @param[in] dev_desc MMC device descriptor
208 * @param[in] info Boot partition info
209 * @param[out] hdr Where to store read boot image header
210 *
211 * @return Boot image header sectors count or 0 on error
212 */
213static lbaint_t fb_mmc_get_boot_header(struct blk_desc *dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600214 struct disk_partition *info,
Alex Kiernan27b69de2018-05-29 15:30:40 +0000215 struct andr_img_hdr *hdr,
216 char *response)
Sam Protsenko540eb952017-05-18 15:04:59 +0300217{
218 ulong sector_size; /* boot partition sector size */
219 lbaint_t hdr_sectors; /* boot image header sectors count */
220 int res;
221
222 /* Calculate boot image sectors count */
223 sector_size = info->blksz;
224 hdr_sectors = DIV_ROUND_UP(sizeof(struct andr_img_hdr), sector_size);
225 if (hdr_sectors == 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000226 pr_err("invalid number of boot sectors: 0\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000227 fastboot_fail("invalid number of boot sectors: 0", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300228 return 0;
229 }
230
231 /* Read the boot image header */
232 res = blk_dread(dev_desc, info->start, hdr_sectors, (void *)hdr);
Tom Rinibda6f772017-08-14 21:00:44 -0400233 if (res != hdr_sectors) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000234 pr_err("cannot read header from boot partition\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000235 fastboot_fail("cannot read header from boot partition",
236 response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300237 return 0;
238 }
239
240 /* Check boot header magic string */
241 res = android_image_check_header(hdr);
242 if (res != 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000243 pr_err("bad boot image magic\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000244 fastboot_fail("boot partition not initialized", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300245 return 0;
246 }
247
248 return hdr_sectors;
249}
250
251/**
252 * Write downloaded zImage to boot partition and repack it properly.
253 *
254 * @param dev_desc MMC device descriptor
255 * @param download_buffer Address to fastboot buffer with zImage in it
256 * @param download_bytes Size of fastboot buffer, in bytes
257 *
258 * @return 0 on success or -1 on error
259 */
260static int fb_mmc_update_zimage(struct blk_desc *dev_desc,
261 void *download_buffer,
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000262 u32 download_bytes,
Alex Kiernan27b69de2018-05-29 15:30:40 +0000263 char *response)
Sam Protsenko540eb952017-05-18 15:04:59 +0300264{
Tom Rini0ac605c2017-06-10 09:15:37 -0400265 uintptr_t hdr_addr; /* boot image header address */
Sam Protsenko540eb952017-05-18 15:04:59 +0300266 struct andr_img_hdr *hdr; /* boot image header */
267 lbaint_t hdr_sectors; /* boot image header sectors */
268 u8 *ramdisk_buffer;
269 u32 ramdisk_sector_start;
270 u32 ramdisk_sectors;
271 u32 kernel_sector_start;
272 u32 kernel_sectors;
273 u32 sectors_per_page;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600274 struct disk_partition info;
Sam Protsenko540eb952017-05-18 15:04:59 +0300275 int res;
276
277 puts("Flashing zImage\n");
278
279 /* Get boot partition info */
280 res = part_get_info_by_name(dev_desc, BOOT_PARTITION_NAME, &info);
281 if (res < 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000282 pr_err("cannot find boot partition\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000283 fastboot_fail("cannot find boot partition", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300284 return -1;
285 }
286
287 /* Put boot image header in fastboot buffer after downloaded zImage */
Tom Rini0ac605c2017-06-10 09:15:37 -0400288 hdr_addr = (uintptr_t)download_buffer + ALIGN(download_bytes, PAGE_SIZE);
Sam Protsenko540eb952017-05-18 15:04:59 +0300289 hdr = (struct andr_img_hdr *)hdr_addr;
290
291 /* Read boot image header */
Alex Kiernan27b69de2018-05-29 15:30:40 +0000292 hdr_sectors = fb_mmc_get_boot_header(dev_desc, &info, hdr, response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300293 if (hdr_sectors == 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000294 pr_err("unable to read boot image header\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000295 fastboot_fail("unable to read boot image header", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300296 return -1;
297 }
298
299 /* Check if boot image has second stage in it (we don't support it) */
300 if (hdr->second_size > 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000301 pr_err("moving second stage is not supported yet\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000302 fastboot_fail("moving second stage is not supported yet",
303 response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300304 return -1;
305 }
306
307 /* Extract ramdisk location */
308 sectors_per_page = hdr->page_size / info.blksz;
309 ramdisk_sector_start = info.start + sectors_per_page;
310 ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
311 sectors_per_page;
312 ramdisk_sectors = DIV_ROUND_UP(hdr->ramdisk_size, hdr->page_size) *
313 sectors_per_page;
314
315 /* Read ramdisk and put it in fastboot buffer after boot image header */
316 ramdisk_buffer = (u8 *)hdr + (hdr_sectors * info.blksz);
317 res = blk_dread(dev_desc, ramdisk_sector_start, ramdisk_sectors,
318 ramdisk_buffer);
Tom Rinibda6f772017-08-14 21:00:44 -0400319 if (res != ramdisk_sectors) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000320 pr_err("cannot read ramdisk from boot partition\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000321 fastboot_fail("cannot read ramdisk from boot partition",
322 response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300323 return -1;
324 }
325
326 /* Write new kernel size to boot image header */
327 hdr->kernel_size = download_bytes;
328 res = blk_dwrite(dev_desc, info.start, hdr_sectors, (void *)hdr);
329 if (res == 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000330 pr_err("cannot writeback boot image header\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000331 fastboot_fail("cannot write back boot image header", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300332 return -1;
333 }
334
335 /* Write the new downloaded kernel */
336 kernel_sector_start = info.start + sectors_per_page;
337 kernel_sectors = DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
338 sectors_per_page;
339 res = blk_dwrite(dev_desc, kernel_sector_start, kernel_sectors,
340 download_buffer);
341 if (res == 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000342 pr_err("cannot write new kernel\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000343 fastboot_fail("cannot write new kernel", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300344 return -1;
345 }
346
347 /* Write the saved ramdisk back */
348 ramdisk_sector_start = info.start + sectors_per_page;
349 ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
350 sectors_per_page;
351 res = blk_dwrite(dev_desc, ramdisk_sector_start, ramdisk_sectors,
352 ramdisk_buffer);
353 if (res == 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000354 pr_err("cannot write back original ramdisk\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000355 fastboot_fail("cannot write back original ramdisk", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300356 return -1;
357 }
358
359 puts("........ zImage was updated in boot partition\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000360 fastboot_okay(NULL, response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300361 return 0;
362}
363#endif
364
Alex Kiernan32c21942018-05-29 15:30:48 +0000365/**
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000366 * fastboot_mmc_get_part_info() - Lookup eMMC partion by name
367 *
368 * @part_name: Named partition to lookup
369 * @dev_desc: Pointer to returned blk_desc pointer
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600370 * @part_info: Pointer to returned struct disk_partition
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000371 * @response: Pointer to fastboot response buffer
372 */
Sam Protsenko0fb37ee2019-06-13 21:11:07 +0300373int fastboot_mmc_get_part_info(const char *part_name,
374 struct blk_desc **dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600375 struct disk_partition *part_info, char *response)
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000376{
377 int r;
378
379 *dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
380 if (!*dev_desc) {
381 fastboot_fail("block device not found", response);
382 return -ENOENT;
383 }
Eugeniu Rosca8eba90b2019-03-28 14:31:33 +0100384 if (!part_name || !strcmp(part_name, "")) {
385 fastboot_fail("partition not given", response);
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000386 return -ENOENT;
387 }
388
389 r = part_get_info_by_name_or_alias(*dev_desc, part_name, part_info);
390 if (r < 0) {
391 fastboot_fail("partition not found", response);
392 return r;
393 }
394
395 return r;
396}
397
398/**
Alex Kiernan32c21942018-05-29 15:30:48 +0000399 * fastboot_mmc_flash_write() - Write image to eMMC for fastboot
400 *
401 * @cmd: Named partition to write image to
402 * @download_buffer: Pointer to image data
403 * @download_bytes: Size of image data
404 * @response: Pointer to fastboot response buffer
405 */
406void fastboot_mmc_flash_write(const char *cmd, void *download_buffer,
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000407 u32 download_bytes, char *response)
Steve Rae792ac482014-08-26 11:47:27 -0700408{
Simon Glasse3394752016-02-29 15:25:34 -0700409 struct blk_desc *dev_desc;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600410 struct disk_partition info;
Steve Rae792ac482014-08-26 11:47:27 -0700411
Simon Glassbe1e9bb2016-02-29 15:25:42 -0700412 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
Steve Rae792ac482014-08-26 11:47:27 -0700413 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900414 pr_err("invalid mmc device\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000415 fastboot_fail("invalid mmc device", response);
Steve Rae792ac482014-08-26 11:47:27 -0700416 return;
417 }
418
developer6a105562020-01-16 16:11:42 +0800419#ifdef CONFIG_FASTBOOT_MMC_BOOT1_SUPPORT
420 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) {
421 fb_mmc_boot1_ops(dev_desc, download_buffer,
422 download_bytes, response);
423 return;
424 }
425#endif
426
Patrick Delaunay8a4f2bd2017-01-27 11:00:41 +0100427#if CONFIG_IS_ENABLED(EFI_PARTITION)
developer6a105562020-01-16 16:11:42 +0800428#ifndef CONFIG_FASTBOOT_MMC_USER_NAME
Steve Rae7d059342014-12-12 15:51:54 -0800429 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
developer6a105562020-01-16 16:11:42 +0800430#else
431 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0 ||
432 strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) {
433#endif
Steve Rae7d059342014-12-12 15:51:54 -0800434 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
435 __func__);
436 if (is_valid_gpt_buf(dev_desc, download_buffer)) {
437 printf("%s: invalid GPT - refusing to write to flash\n",
438 __func__);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000439 fastboot_fail("invalid GPT partition", response);
Steve Rae7d059342014-12-12 15:51:54 -0800440 return;
441 }
442 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
443 printf("%s: writing GPT partitions failed\n", __func__);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000444 fastboot_fail("writing GPT partitions failed",
445 response);
Steve Rae7d059342014-12-12 15:51:54 -0800446 return;
447 }
448 printf("........ success\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000449 fastboot_okay(NULL, response);
Steve Rae7d059342014-12-12 15:51:54 -0800450 return;
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200451 }
452#endif
453
Patrick Delaunayf7e07722017-01-27 11:00:37 +0100454#if CONFIG_IS_ENABLED(DOS_PARTITION)
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200455 if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) {
456 printf("%s: updating MBR\n", __func__);
457 if (is_valid_dos_buf(download_buffer)) {
458 printf("%s: invalid MBR - refusing to write to flash\n",
459 __func__);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000460 fastboot_fail("invalid MBR partition", response);
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200461 return;
462 }
463 if (write_mbr_partition(dev_desc, download_buffer)) {
464 printf("%s: writing MBR partition failed\n", __func__);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000465 fastboot_fail("writing MBR partition failed",
466 response);
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200467 return;
468 }
469 printf("........ success\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000470 fastboot_okay(NULL, response);
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200471 return;
472 }
473#endif
474
Sam Protsenko540eb952017-05-18 15:04:59 +0300475#ifdef CONFIG_ANDROID_BOOT_IMAGE
476 if (strncasecmp(cmd, "zimage", 6) == 0) {
Alex Kiernan27b69de2018-05-29 15:30:40 +0000477 fb_mmc_update_zimage(dev_desc, download_buffer,
478 download_bytes, response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300479 return;
480 }
481#endif
482
Alex Deymoe2f689f2017-04-02 01:49:50 -0700483 if (part_get_info_by_name_or_alias(dev_desc, cmd, &info) < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900484 pr_err("cannot find partition: '%s'\n", cmd);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000485 fastboot_fail("cannot find partition", response);
Steve Rae792ac482014-08-26 11:47:27 -0700486 return;
487 }
488
Maxime Riparda58e9c72015-10-15 14:34:14 +0200489 if (is_sparse_image(download_buffer)) {
490 struct fb_mmc_sparse sparse_priv;
Steve Rae8cf58db2016-06-07 11:19:36 -0700491 struct sparse_storage sparse;
Jassi Brar427e6692018-04-06 12:05:09 +0530492 int err;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200493
494 sparse_priv.dev_desc = dev_desc;
495
Steve Rae8cf58db2016-06-07 11:19:36 -0700496 sparse.blksz = info.blksz;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200497 sparse.start = info.start;
498 sparse.size = info.size;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200499 sparse.write = fb_mmc_sparse_write;
Steve Rae8a99be82016-06-07 11:19:38 -0700500 sparse.reserve = fb_mmc_sparse_reserve;
Jassi Brar427e6692018-04-06 12:05:09 +0530501 sparse.mssg = fastboot_fail;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200502
503 printf("Flashing sparse image at offset " LBAFU "\n",
Steve Rae8cf58db2016-06-07 11:19:36 -0700504 sparse.start);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200505
Steve Rae8cf58db2016-06-07 11:19:36 -0700506 sparse.priv = &sparse_priv;
Alex Kiernan27b69de2018-05-29 15:30:40 +0000507 err = write_sparse_image(&sparse, cmd, download_buffer,
508 response);
Jassi Brar427e6692018-04-06 12:05:09 +0530509 if (!err)
Alex Kiernan27b69de2018-05-29 15:30:40 +0000510 fastboot_okay(NULL, response);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200511 } else {
Steve Raec6015672014-08-26 11:47:30 -0700512 write_raw_image(dev_desc, &info, cmd, download_buffer,
Alex Kiernan27b69de2018-05-29 15:30:40 +0000513 download_bytes, response);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200514 }
Steve Rae792ac482014-08-26 11:47:27 -0700515}
Dileep Kattab1228202015-02-17 18:48:23 +0530516
Alex Kiernan32c21942018-05-29 15:30:48 +0000517/**
518 * fastboot_mmc_flash_erase() - Erase eMMC for fastboot
519 *
520 * @cmd: Named partition to erase
521 * @response: Pointer to fastboot response buffer
522 */
523void fastboot_mmc_erase(const char *cmd, char *response)
Dileep Kattab1228202015-02-17 18:48:23 +0530524{
525 int ret;
Simon Glasse3394752016-02-29 15:25:34 -0700526 struct blk_desc *dev_desc;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600527 struct disk_partition info;
Dileep Kattab1228202015-02-17 18:48:23 +0530528 lbaint_t blks, blks_start, blks_size, grp_size;
529 struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
530
531 if (mmc == NULL) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000532 pr_err("invalid mmc device\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000533 fastboot_fail("invalid mmc device", response);
Dileep Kattab1228202015-02-17 18:48:23 +0530534 return;
535 }
536
Simon Glassbe1e9bb2016-02-29 15:25:42 -0700537 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
Dileep Kattab1228202015-02-17 18:48:23 +0530538 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000539 pr_err("invalid mmc device\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000540 fastboot_fail("invalid mmc device", response);
Dileep Kattab1228202015-02-17 18:48:23 +0530541 return;
542 }
developer6a105562020-01-16 16:11:42 +0800543
544#ifdef CONFIG_FASTBOOT_MMC_BOOT1_SUPPORT
545 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) {
546 /* erase EMMC boot1 */
547 fb_mmc_boot1_ops(dev_desc, NULL, 0, response);
548 return;
549 }
550#endif
551
552#ifdef CONFIG_FASTBOOT_MMC_USER_NAME
553 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) {
554 /* erase EMMC userdata */
555 if (fb_mmc_erase_mmc_hwpart(dev_desc))
556 fastboot_fail("Failed to erase EMMC_USER", response);
557 else
558 fastboot_okay(NULL, response);
559 return;
560 }
561#endif
Dileep Kattab1228202015-02-17 18:48:23 +0530562
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200563 ret = part_get_info_by_name_or_alias(dev_desc, cmd, &info);
Alex Deymoe2f689f2017-04-02 01:49:50 -0700564 if (ret < 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000565 pr_err("cannot find partition: '%s'\n", cmd);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000566 fastboot_fail("cannot find partition", response);
Dileep Kattab1228202015-02-17 18:48:23 +0530567 return;
568 }
569
570 /* Align blocks to erase group size to avoid erasing other partitions */
571 grp_size = mmc->erase_grp_size;
572 blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
573 if (info.size >= grp_size)
574 blks_size = (info.size - (blks_start - info.start)) &
575 (~(grp_size - 1));
576 else
577 blks_size = 0;
578
579 printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
580 blks_start, blks_start + blks_size);
581
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000582 blks = fb_mmc_blk_write(dev_desc, blks_start, blks_size, NULL);
583
Dileep Kattab1228202015-02-17 18:48:23 +0530584 if (blks != blks_size) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000585 pr_err("failed erasing from device %d\n", dev_desc->devnum);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000586 fastboot_fail("failed erasing from device", response);
Dileep Kattab1228202015-02-17 18:48:23 +0530587 return;
588 }
589
590 printf("........ erased " LBAFU " bytes from '%s'\n",
591 blks_size * info.blksz, cmd);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000592 fastboot_okay(NULL, response);
Dileep Kattab1228202015-02-17 18:48:23 +0530593}