blob: ae8e8e512f2ecd8dacc26cee210f1fd1aaaa3973 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Steve Raec0aebb32014-08-26 11:47:27 -07002/*
3 * Copyright 2014 Broadcom Corporation.
Steve Raec0aebb32014-08-26 11:47:27 -07004 */
5
Steve Rae0ff7e582014-12-12 15:51:54 -08006#include <config.h>
Steve Raec0aebb32014-08-26 11:47:27 -07007#include <common.h>
Simon Glass2a981dc2016-02-29 15:25:52 -07008#include <blk.h>
Simon Glass7b51b572019-08-01 09:46:52 -06009#include <env.h>
Maxime Ripard3c8f98f2015-10-15 14:34:13 +020010#include <fastboot.h>
Alex Kiernanf73a7df2018-05-29 15:30:53 +000011#include <fastboot-internal.h>
Steve Raec0aebb32014-08-26 11:47:27 -070012#include <fb_mmc.h>
Simon Glassb79fdc72020-05-10 11:39:54 -060013#include <flash.h>
Maxime Ripard3d4ef382015-10-15 14:34:19 +020014#include <image-sparse.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060015#include <image.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060016#include <log.h>
Steve Raec0aebb32014-08-26 11:47:27 -070017#include <part.h>
Dileep Katta89792382015-02-17 18:48:23 +053018#include <mmc.h>
Siarhei Siamashka5e0efc12015-10-28 06:24:16 +020019#include <div64.h>
Sam Protsenkoefeccfe2017-05-18 15:04:59 +030020#include <linux/compat.h>
21#include <android_image.h>
Steve Raec0aebb32014-08-26 11:47:27 -070022
Alex Kiernanf73a7df2018-05-29 15:30:53 +000023#define FASTBOOT_MAX_BLK_WRITE 16384
24
Sam Protsenkoefeccfe2017-05-18 15:04:59 +030025#define BOOT_PARTITION_NAME "boot"
26
Maxime Riparda5d1e042015-10-15 14:34:14 +020027struct fb_mmc_sparse {
Simon Glass4101f682016-02-29 15:25:34 -070028 struct blk_desc *dev_desc;
Maxime Riparda5d1e042015-10-15 14:34:14 +020029};
30
Petr Kulhavyb6dd69a2016-09-09 10:27:16 +020031static int part_get_info_by_name_or_alias(struct blk_desc *dev_desc,
Simon Glass05289792020-05-10 11:39:57 -060032 const char *name, struct disk_partition *info)
Michael Scott8a418022015-03-11 10:02:31 -070033{
34 int ret;
35
Petr Kulhavy87b85302016-09-09 10:27:15 +020036 ret = part_get_info_by_name(dev_desc, name, info);
Alex Deymo88b63292017-04-02 01:49:50 -070037 if (ret < 0) {
Alex Kiernanb762aa12019-04-09 05:30:05 +000038 /* strlen("fastboot_partition_alias_") + PART_NAME_LEN + 1 */
39 char env_alias_name[25 + PART_NAME_LEN + 1];
Michael Scott8a418022015-03-11 10:02:31 -070040 char *aliased_part_name;
41
42 /* check for alias */
43 strcpy(env_alias_name, "fastboot_partition_alias_");
Alex Kiernanb762aa12019-04-09 05:30:05 +000044 strncat(env_alias_name, name, PART_NAME_LEN);
Simon Glass00caae62017-08-03 12:22:12 -060045 aliased_part_name = env_get(env_alias_name);
Michael Scott8a418022015-03-11 10:02:31 -070046 if (aliased_part_name != NULL)
Petr Kulhavy87b85302016-09-09 10:27:15 +020047 ret = part_get_info_by_name(dev_desc,
Michael Scott8a418022015-03-11 10:02:31 -070048 aliased_part_name, info);
49 }
50 return ret;
51}
52
Filip Brozovica17c0cb2020-06-29 13:14:37 +020053static int raw_part_get_info_by_name(struct blk_desc *dev_desc,
54 const char *name, struct disk_partition *info, int *mmcpart)
55{
56 /* strlen("fastboot_raw_partition_") + PART_NAME_LEN + 1 */
57 char env_desc_name[23 + PART_NAME_LEN + 1];
58 char *raw_part_desc;
59 const char *argv[2];
60 const char **parg = argv;
61
62 /* check for raw partition descriptor */
63 strcpy(env_desc_name, "fastboot_raw_partition_");
64 strncat(env_desc_name, name, PART_NAME_LEN);
65 raw_part_desc = strdup(env_get(env_desc_name));
66 if (raw_part_desc == NULL)
67 return -ENODEV;
68
69 /*
70 * parse partition descriptor
71 *
72 * <lba_start> <lba_size> [mmcpart <num>]
73 */
74 for (; parg < argv + sizeof(argv) / sizeof(*argv); ++parg) {
75 *parg = strsep(&raw_part_desc, " ");
76 if (*parg == NULL) {
77 pr_err("Invalid number of arguments.\n");
78 return -ENODEV;
79 }
80 }
81
82 info->start = simple_strtoul(argv[0], NULL, 0);
83 info->size = simple_strtoul(argv[1], NULL, 0);
84 info->blksz = dev_desc->blksz;
85 strncpy((char *)info->name, name, PART_NAME_LEN);
86
87 if (raw_part_desc) {
88 if (strcmp(strsep(&raw_part_desc, " "), "mmcpart") == 0)
89 *mmcpart = simple_strtoul(raw_part_desc, NULL, 0);
90 }
91
92 return 0;
93}
94
Alex Kiernanf73a7df2018-05-29 15:30:53 +000095/**
96 * fb_mmc_blk_write() - Write/erase MMC in chunks of FASTBOOT_MAX_BLK_WRITE
97 *
98 * @block_dev: Pointer to block device
99 * @start: First block to write/erase
100 * @blkcnt: Count of blocks
101 * @buffer: Pointer to data buffer for write or NULL for erase
102 */
103static lbaint_t fb_mmc_blk_write(struct blk_desc *block_dev, lbaint_t start,
104 lbaint_t blkcnt, const void *buffer)
105{
106 lbaint_t blk = start;
107 lbaint_t blks_written;
108 lbaint_t cur_blkcnt;
109 lbaint_t blks = 0;
110 int i;
111
112 for (i = 0; i < blkcnt; i += FASTBOOT_MAX_BLK_WRITE) {
113 cur_blkcnt = min((int)blkcnt - i, FASTBOOT_MAX_BLK_WRITE);
114 if (buffer) {
115 if (fastboot_progress_callback)
116 fastboot_progress_callback("writing");
117 blks_written = blk_dwrite(block_dev, blk, cur_blkcnt,
118 buffer + (i * block_dev->blksz));
119 } else {
120 if (fastboot_progress_callback)
121 fastboot_progress_callback("erasing");
122 blks_written = blk_derase(block_dev, blk, cur_blkcnt);
123 }
124 blk += blks_written;
125 blks += blks_written;
126 }
127 return blks;
128}
129
Steve Raecc0f08cd2016-06-07 11:19:36 -0700130static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info,
131 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
Maxime Riparda5d1e042015-10-15 14:34:14 +0200132{
Steve Raecc0f08cd2016-06-07 11:19:36 -0700133 struct fb_mmc_sparse *sparse = info->priv;
Simon Glass4101f682016-02-29 15:25:34 -0700134 struct blk_desc *dev_desc = sparse->dev_desc;
Maxime Riparda5d1e042015-10-15 14:34:14 +0200135
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000136 return fb_mmc_blk_write(dev_desc, blk, blkcnt, buffer);
Maxime Riparda5d1e042015-10-15 14:34:14 +0200137}
138
Steve Rae2c724042016-06-07 11:19:38 -0700139static lbaint_t fb_mmc_sparse_reserve(struct sparse_storage *info,
140 lbaint_t blk, lbaint_t blkcnt)
141{
142 return blkcnt;
143}
144
Simon Glass05289792020-05-10 11:39:57 -0600145static void write_raw_image(struct blk_desc *dev_desc,
146 struct disk_partition *info, const char *part_name,
147 void *buffer, u32 download_bytes, char *response)
Steve Raec0aebb32014-08-26 11:47:27 -0700148{
149 lbaint_t blkcnt;
150 lbaint_t blks;
151
152 /* determine number of blocks to write */
153 blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
Siarhei Siamashka5e0efc12015-10-28 06:24:16 +0200154 blkcnt = lldiv(blkcnt, info->blksz);
Steve Raec0aebb32014-08-26 11:47:27 -0700155
156 if (blkcnt > info->size) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900157 pr_err("too large for partition: '%s'\n", part_name);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000158 fastboot_fail("too large for partition", response);
Steve Raec0aebb32014-08-26 11:47:27 -0700159 return;
160 }
161
162 puts("Flashing Raw Image\n");
163
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000164 blks = fb_mmc_blk_write(dev_desc, info->start, blkcnt, buffer);
165
Steve Raec0aebb32014-08-26 11:47:27 -0700166 if (blks != blkcnt) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900167 pr_err("failed writing to device %d\n", dev_desc->devnum);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000168 fastboot_fail("failed writing to device", response);
Steve Raec0aebb32014-08-26 11:47:27 -0700169 return;
170 }
171
172 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
173 part_name);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000174 fastboot_okay(NULL, response);
Steve Raec0aebb32014-08-26 11:47:27 -0700175}
176
mingming lee1fdbad02020-01-16 16:11:42 +0800177#ifdef CONFIG_FASTBOOT_MMC_BOOT1_SUPPORT
178static int fb_mmc_erase_mmc_hwpart(struct blk_desc *dev_desc)
179{
180 lbaint_t blks;
181
182 debug("Start Erasing mmc hwpart[%u]...\n", dev_desc->hwpart);
183
184 blks = fb_mmc_blk_write(dev_desc, 0, dev_desc->lba, NULL);
185
186 if (blks != dev_desc->lba) {
187 pr_err("Failed to erase mmc hwpart[%u]\n", dev_desc->hwpart);
188 return 1;
189 }
190
191 printf("........ erased %lu bytes from mmc hwpart[%u]\n",
192 dev_desc->lba * dev_desc->blksz, dev_desc->hwpart);
193
194 return 0;
195}
196
197static void fb_mmc_boot1_ops(struct blk_desc *dev_desc, void *buffer,
198 u32 buff_sz, char *response)
199{
200 lbaint_t blkcnt;
201 lbaint_t blks;
202 unsigned long blksz;
203
204 // To operate on EMMC_BOOT1 (mmc0boot0), we first change the hwpart
205 if (blk_dselect_hwpart(dev_desc, 1)) {
206 pr_err("Failed to select hwpart\n");
207 fastboot_fail("Failed to select hwpart", response);
208 return;
209 }
210
211 if (buffer) { /* flash */
212
213 /* determine number of blocks to write */
214 blksz = dev_desc->blksz;
215 blkcnt = ((buff_sz + (blksz - 1)) & ~(blksz - 1));
216 blkcnt = lldiv(blkcnt, blksz);
217
218 if (blkcnt > dev_desc->lba) {
219 pr_err("Image size too large\n");
220 fastboot_fail("Image size too large", response);
221 return;
222 }
223
224 debug("Start Flashing Image to EMMC_BOOT1...\n");
225
226 blks = fb_mmc_blk_write(dev_desc, 0, blkcnt, buffer);
227
228 if (blks != blkcnt) {
229 pr_err("Failed to write EMMC_BOOT1\n");
230 fastboot_fail("Failed to write EMMC_BOOT1", response);
231 return;
232 }
233
234 printf("........ wrote %lu bytes to EMMC_BOOT1\n",
235 blkcnt * blksz);
236 } else { /* erase */
237 if (fb_mmc_erase_mmc_hwpart(dev_desc)) {
238 fastboot_fail("Failed to erase EMMC_BOOT1", response);
239 return;
240 }
241 }
242
243 fastboot_okay(NULL, response);
244}
245#endif
246
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300247#ifdef CONFIG_ANDROID_BOOT_IMAGE
248/**
249 * Read Android boot image header from boot partition.
250 *
251 * @param[in] dev_desc MMC device descriptor
252 * @param[in] info Boot partition info
253 * @param[out] hdr Where to store read boot image header
254 *
255 * @return Boot image header sectors count or 0 on error
256 */
257static lbaint_t fb_mmc_get_boot_header(struct blk_desc *dev_desc,
Simon Glass05289792020-05-10 11:39:57 -0600258 struct disk_partition *info,
Alex Kiernanc4ded032018-05-29 15:30:40 +0000259 struct andr_img_hdr *hdr,
260 char *response)
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300261{
262 ulong sector_size; /* boot partition sector size */
263 lbaint_t hdr_sectors; /* boot image header sectors count */
264 int res;
265
266 /* Calculate boot image sectors count */
267 sector_size = info->blksz;
268 hdr_sectors = DIV_ROUND_UP(sizeof(struct andr_img_hdr), sector_size);
269 if (hdr_sectors == 0) {
Alex Kiernan1ad5fac2018-05-29 15:30:43 +0000270 pr_err("invalid number of boot sectors: 0\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000271 fastboot_fail("invalid number of boot sectors: 0", response);
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300272 return 0;
273 }
274
275 /* Read the boot image header */
276 res = blk_dread(dev_desc, info->start, hdr_sectors, (void *)hdr);
Tom Rini6bafa5a2017-08-14 21:00:44 -0400277 if (res != hdr_sectors) {
Alex Kiernan1ad5fac2018-05-29 15:30:43 +0000278 pr_err("cannot read header from boot partition\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000279 fastboot_fail("cannot read header from boot partition",
280 response);
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300281 return 0;
282 }
283
284 /* Check boot header magic string */
285 res = android_image_check_header(hdr);
286 if (res != 0) {
Alex Kiernan1ad5fac2018-05-29 15:30:43 +0000287 pr_err("bad boot image magic\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000288 fastboot_fail("boot partition not initialized", response);
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300289 return 0;
290 }
291
292 return hdr_sectors;
293}
294
295/**
296 * Write downloaded zImage to boot partition and repack it properly.
297 *
298 * @param dev_desc MMC device descriptor
299 * @param download_buffer Address to fastboot buffer with zImage in it
300 * @param download_bytes Size of fastboot buffer, in bytes
301 *
302 * @return 0 on success or -1 on error
303 */
304static int fb_mmc_update_zimage(struct blk_desc *dev_desc,
305 void *download_buffer,
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000306 u32 download_bytes,
Alex Kiernanc4ded032018-05-29 15:30:40 +0000307 char *response)
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300308{
Tom Rini2341c802017-06-10 09:15:37 -0400309 uintptr_t hdr_addr; /* boot image header address */
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300310 struct andr_img_hdr *hdr; /* boot image header */
311 lbaint_t hdr_sectors; /* boot image header sectors */
312 u8 *ramdisk_buffer;
313 u32 ramdisk_sector_start;
314 u32 ramdisk_sectors;
315 u32 kernel_sector_start;
316 u32 kernel_sectors;
317 u32 sectors_per_page;
Simon Glass05289792020-05-10 11:39:57 -0600318 struct disk_partition info;
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300319 int res;
320
321 puts("Flashing zImage\n");
322
323 /* Get boot partition info */
324 res = part_get_info_by_name(dev_desc, BOOT_PARTITION_NAME, &info);
325 if (res < 0) {
Alex Kiernan1ad5fac2018-05-29 15:30:43 +0000326 pr_err("cannot find boot partition\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000327 fastboot_fail("cannot find boot partition", response);
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300328 return -1;
329 }
330
331 /* Put boot image header in fastboot buffer after downloaded zImage */
Tom Rini2341c802017-06-10 09:15:37 -0400332 hdr_addr = (uintptr_t)download_buffer + ALIGN(download_bytes, PAGE_SIZE);
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300333 hdr = (struct andr_img_hdr *)hdr_addr;
334
335 /* Read boot image header */
Alex Kiernanc4ded032018-05-29 15:30:40 +0000336 hdr_sectors = fb_mmc_get_boot_header(dev_desc, &info, hdr, response);
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300337 if (hdr_sectors == 0) {
Alex Kiernan1ad5fac2018-05-29 15:30:43 +0000338 pr_err("unable to read boot image header\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000339 fastboot_fail("unable to read boot image header", response);
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300340 return -1;
341 }
342
343 /* Check if boot image has second stage in it (we don't support it) */
344 if (hdr->second_size > 0) {
Alex Kiernan1ad5fac2018-05-29 15:30:43 +0000345 pr_err("moving second stage is not supported yet\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000346 fastboot_fail("moving second stage is not supported yet",
347 response);
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300348 return -1;
349 }
350
351 /* Extract ramdisk location */
352 sectors_per_page = hdr->page_size / info.blksz;
353 ramdisk_sector_start = info.start + sectors_per_page;
354 ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
355 sectors_per_page;
356 ramdisk_sectors = DIV_ROUND_UP(hdr->ramdisk_size, hdr->page_size) *
357 sectors_per_page;
358
359 /* Read ramdisk and put it in fastboot buffer after boot image header */
360 ramdisk_buffer = (u8 *)hdr + (hdr_sectors * info.blksz);
361 res = blk_dread(dev_desc, ramdisk_sector_start, ramdisk_sectors,
362 ramdisk_buffer);
Tom Rini6bafa5a2017-08-14 21:00:44 -0400363 if (res != ramdisk_sectors) {
Alex Kiernan1ad5fac2018-05-29 15:30:43 +0000364 pr_err("cannot read ramdisk from boot partition\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000365 fastboot_fail("cannot read ramdisk from boot partition",
366 response);
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300367 return -1;
368 }
369
370 /* Write new kernel size to boot image header */
371 hdr->kernel_size = download_bytes;
372 res = blk_dwrite(dev_desc, info.start, hdr_sectors, (void *)hdr);
373 if (res == 0) {
Alex Kiernan1ad5fac2018-05-29 15:30:43 +0000374 pr_err("cannot writeback boot image header\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000375 fastboot_fail("cannot write back boot image header", response);
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300376 return -1;
377 }
378
379 /* Write the new downloaded kernel */
380 kernel_sector_start = info.start + sectors_per_page;
381 kernel_sectors = DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
382 sectors_per_page;
383 res = blk_dwrite(dev_desc, kernel_sector_start, kernel_sectors,
384 download_buffer);
385 if (res == 0) {
Alex Kiernan1ad5fac2018-05-29 15:30:43 +0000386 pr_err("cannot write new kernel\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000387 fastboot_fail("cannot write new kernel", response);
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300388 return -1;
389 }
390
391 /* Write the saved ramdisk back */
392 ramdisk_sector_start = info.start + sectors_per_page;
393 ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
394 sectors_per_page;
395 res = blk_dwrite(dev_desc, ramdisk_sector_start, ramdisk_sectors,
396 ramdisk_buffer);
397 if (res == 0) {
Alex Kiernan1ad5fac2018-05-29 15:30:43 +0000398 pr_err("cannot write back original ramdisk\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000399 fastboot_fail("cannot write back original ramdisk", response);
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300400 return -1;
401 }
402
403 puts("........ zImage was updated in boot partition\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000404 fastboot_okay(NULL, response);
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300405 return 0;
406}
407#endif
408
Alex Kiernand1a119d2018-05-29 15:30:48 +0000409/**
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000410 * fastboot_mmc_get_part_info() - Lookup eMMC partion by name
411 *
412 * @part_name: Named partition to lookup
413 * @dev_desc: Pointer to returned blk_desc pointer
Simon Glass05289792020-05-10 11:39:57 -0600414 * @part_info: Pointer to returned struct disk_partition
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000415 * @response: Pointer to fastboot response buffer
416 */
Sam Protsenkocacb03e2019-06-13 21:11:07 +0300417int fastboot_mmc_get_part_info(const char *part_name,
418 struct blk_desc **dev_desc,
Simon Glass05289792020-05-10 11:39:57 -0600419 struct disk_partition *part_info, char *response)
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000420{
Filip Brozovica17c0cb2020-06-29 13:14:37 +0200421 int r = 0;
422 int mmcpart;
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000423
424 *dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
425 if (!*dev_desc) {
426 fastboot_fail("block device not found", response);
427 return -ENOENT;
428 }
Eugeniu Roscaa40297d2019-03-28 14:31:33 +0100429 if (!part_name || !strcmp(part_name, "")) {
430 fastboot_fail("partition not given", response);
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000431 return -ENOENT;
432 }
433
Filip Brozovica17c0cb2020-06-29 13:14:37 +0200434 if (raw_part_get_info_by_name(*dev_desc, part_name, part_info, &mmcpart) < 0) {
435 r = part_get_info_by_name_or_alias(*dev_desc, part_name, part_info);
436 if (r < 0) {
437 fastboot_fail("partition not found", response);
438 return r;
439 }
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000440 }
441
442 return r;
443}
444
445/**
Alex Kiernand1a119d2018-05-29 15:30:48 +0000446 * fastboot_mmc_flash_write() - Write image to eMMC for fastboot
447 *
448 * @cmd: Named partition to write image to
449 * @download_buffer: Pointer to image data
450 * @download_bytes: Size of image data
451 * @response: Pointer to fastboot response buffer
452 */
453void fastboot_mmc_flash_write(const char *cmd, void *download_buffer,
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000454 u32 download_bytes, char *response)
Steve Raec0aebb32014-08-26 11:47:27 -0700455{
Simon Glass4101f682016-02-29 15:25:34 -0700456 struct blk_desc *dev_desc;
Simon Glass05289792020-05-10 11:39:57 -0600457 struct disk_partition info;
Filip Brozovica17c0cb2020-06-29 13:14:37 +0200458 int mmcpart = 0;
Steve Raec0aebb32014-08-26 11:47:27 -0700459
Simon Glassdb1d9e72016-02-29 15:25:42 -0700460 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
Steve Raec0aebb32014-08-26 11:47:27 -0700461 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900462 pr_err("invalid mmc device\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000463 fastboot_fail("invalid mmc device", response);
Steve Raec0aebb32014-08-26 11:47:27 -0700464 return;
465 }
466
mingming lee1fdbad02020-01-16 16:11:42 +0800467#ifdef CONFIG_FASTBOOT_MMC_BOOT1_SUPPORT
468 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) {
469 fb_mmc_boot1_ops(dev_desc, download_buffer,
470 download_bytes, response);
471 return;
472 }
473#endif
474
Patrick Delaunaybd42a942017-01-27 11:00:41 +0100475#if CONFIG_IS_ENABLED(EFI_PARTITION)
mingming lee1fdbad02020-01-16 16:11:42 +0800476#ifndef CONFIG_FASTBOOT_MMC_USER_NAME
Steve Rae0ff7e582014-12-12 15:51:54 -0800477 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
mingming lee1fdbad02020-01-16 16:11:42 +0800478#else
479 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0 ||
480 strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) {
481#endif
Steve Rae0ff7e582014-12-12 15:51:54 -0800482 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
483 __func__);
484 if (is_valid_gpt_buf(dev_desc, download_buffer)) {
485 printf("%s: invalid GPT - refusing to write to flash\n",
486 __func__);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000487 fastboot_fail("invalid GPT partition", response);
Steve Rae0ff7e582014-12-12 15:51:54 -0800488 return;
489 }
490 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
491 printf("%s: writing GPT partitions failed\n", __func__);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000492 fastboot_fail("writing GPT partitions failed",
493 response);
Steve Rae0ff7e582014-12-12 15:51:54 -0800494 return;
495 }
496 printf("........ success\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000497 fastboot_okay(NULL, response);
Steve Rae0ff7e582014-12-12 15:51:54 -0800498 return;
Petr Kulhavyb6dd69a2016-09-09 10:27:16 +0200499 }
500#endif
501
Patrick Delaunayb0cf7332017-01-27 11:00:37 +0100502#if CONFIG_IS_ENABLED(DOS_PARTITION)
Petr Kulhavyb6dd69a2016-09-09 10:27:16 +0200503 if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) {
504 printf("%s: updating MBR\n", __func__);
505 if (is_valid_dos_buf(download_buffer)) {
506 printf("%s: invalid MBR - refusing to write to flash\n",
507 __func__);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000508 fastboot_fail("invalid MBR partition", response);
Petr Kulhavyb6dd69a2016-09-09 10:27:16 +0200509 return;
510 }
511 if (write_mbr_partition(dev_desc, download_buffer)) {
512 printf("%s: writing MBR partition failed\n", __func__);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000513 fastboot_fail("writing MBR partition failed",
514 response);
Petr Kulhavyb6dd69a2016-09-09 10:27:16 +0200515 return;
516 }
517 printf("........ success\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000518 fastboot_okay(NULL, response);
Petr Kulhavyb6dd69a2016-09-09 10:27:16 +0200519 return;
520 }
521#endif
522
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300523#ifdef CONFIG_ANDROID_BOOT_IMAGE
524 if (strncasecmp(cmd, "zimage", 6) == 0) {
Alex Kiernanc4ded032018-05-29 15:30:40 +0000525 fb_mmc_update_zimage(dev_desc, download_buffer,
526 download_bytes, response);
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300527 return;
528 }
529#endif
530
Filip Brozovica17c0cb2020-06-29 13:14:37 +0200531 if (raw_part_get_info_by_name(dev_desc, cmd, &info, &mmcpart) == 0) {
532 if (blk_dselect_hwpart(dev_desc, mmcpart)) {
533 pr_err("Failed to select hwpart\n");
534 fastboot_fail("Failed to select hwpart", response);
535 return;
536 }
537 } else if (part_get_info_by_name_or_alias(dev_desc, cmd, &info) < 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900538 pr_err("cannot find partition: '%s'\n", cmd);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000539 fastboot_fail("cannot find partition", response);
Steve Raec0aebb32014-08-26 11:47:27 -0700540 return;
541 }
542
Maxime Riparda5d1e042015-10-15 14:34:14 +0200543 if (is_sparse_image(download_buffer)) {
544 struct fb_mmc_sparse sparse_priv;
Steve Raecc0f08cd2016-06-07 11:19:36 -0700545 struct sparse_storage sparse;
Jassi Brar2f83f212018-04-06 12:05:09 +0530546 int err;
Maxime Riparda5d1e042015-10-15 14:34:14 +0200547
548 sparse_priv.dev_desc = dev_desc;
549
Steve Raecc0f08cd2016-06-07 11:19:36 -0700550 sparse.blksz = info.blksz;
Maxime Riparda5d1e042015-10-15 14:34:14 +0200551 sparse.start = info.start;
552 sparse.size = info.size;
Maxime Riparda5d1e042015-10-15 14:34:14 +0200553 sparse.write = fb_mmc_sparse_write;
Steve Rae2c724042016-06-07 11:19:38 -0700554 sparse.reserve = fb_mmc_sparse_reserve;
Jassi Brar2f83f212018-04-06 12:05:09 +0530555 sparse.mssg = fastboot_fail;
Maxime Riparda5d1e042015-10-15 14:34:14 +0200556
557 printf("Flashing sparse image at offset " LBAFU "\n",
Steve Raecc0f08cd2016-06-07 11:19:36 -0700558 sparse.start);
Maxime Riparda5d1e042015-10-15 14:34:14 +0200559
Steve Raecc0f08cd2016-06-07 11:19:36 -0700560 sparse.priv = &sparse_priv;
Alex Kiernanc4ded032018-05-29 15:30:40 +0000561 err = write_sparse_image(&sparse, cmd, download_buffer,
562 response);
Jassi Brar2f83f212018-04-06 12:05:09 +0530563 if (!err)
Alex Kiernanc4ded032018-05-29 15:30:40 +0000564 fastboot_okay(NULL, response);
Maxime Riparda5d1e042015-10-15 14:34:14 +0200565 } else {
Steve Raee5bf9872014-08-26 11:47:30 -0700566 write_raw_image(dev_desc, &info, cmd, download_buffer,
Alex Kiernanc4ded032018-05-29 15:30:40 +0000567 download_bytes, response);
Maxime Riparda5d1e042015-10-15 14:34:14 +0200568 }
Steve Raec0aebb32014-08-26 11:47:27 -0700569}
Dileep Katta89792382015-02-17 18:48:23 +0530570
Alex Kiernand1a119d2018-05-29 15:30:48 +0000571/**
572 * fastboot_mmc_flash_erase() - Erase eMMC for fastboot
573 *
574 * @cmd: Named partition to erase
575 * @response: Pointer to fastboot response buffer
576 */
577void fastboot_mmc_erase(const char *cmd, char *response)
Dileep Katta89792382015-02-17 18:48:23 +0530578{
Simon Glass4101f682016-02-29 15:25:34 -0700579 struct blk_desc *dev_desc;
Simon Glass05289792020-05-10 11:39:57 -0600580 struct disk_partition info;
Dileep Katta89792382015-02-17 18:48:23 +0530581 lbaint_t blks, blks_start, blks_size, grp_size;
582 struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
Filip Brozovica17c0cb2020-06-29 13:14:37 +0200583 int mmcpart = 0;
Dileep Katta89792382015-02-17 18:48:23 +0530584
585 if (mmc == NULL) {
Alex Kiernan1ad5fac2018-05-29 15:30:43 +0000586 pr_err("invalid mmc device\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000587 fastboot_fail("invalid mmc device", response);
Dileep Katta89792382015-02-17 18:48:23 +0530588 return;
589 }
590
Simon Glassdb1d9e72016-02-29 15:25:42 -0700591 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
Dileep Katta89792382015-02-17 18:48:23 +0530592 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
Alex Kiernan1ad5fac2018-05-29 15:30:43 +0000593 pr_err("invalid mmc device\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000594 fastboot_fail("invalid mmc device", response);
Dileep Katta89792382015-02-17 18:48:23 +0530595 return;
596 }
597
mingming lee1fdbad02020-01-16 16:11:42 +0800598#ifdef CONFIG_FASTBOOT_MMC_BOOT1_SUPPORT
599 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) {
600 /* erase EMMC boot1 */
601 fb_mmc_boot1_ops(dev_desc, NULL, 0, response);
602 return;
603 }
604#endif
605
606#ifdef CONFIG_FASTBOOT_MMC_USER_NAME
607 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) {
608 /* erase EMMC userdata */
609 if (fb_mmc_erase_mmc_hwpart(dev_desc))
610 fastboot_fail("Failed to erase EMMC_USER", response);
611 else
612 fastboot_okay(NULL, response);
613 return;
614 }
615#endif
616
Filip Brozovica17c0cb2020-06-29 13:14:37 +0200617 if (raw_part_get_info_by_name(dev_desc, cmd, &info, &mmcpart) == 0) {
618 if (blk_dselect_hwpart(dev_desc, mmcpart)) {
619 pr_err("Failed to select hwpart\n");
620 fastboot_fail("Failed to select hwpart", response);
621 return;
622 }
623 } else if (part_get_info_by_name_or_alias(dev_desc, cmd, &info) < 0) {
Alex Kiernan1ad5fac2018-05-29 15:30:43 +0000624 pr_err("cannot find partition: '%s'\n", cmd);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000625 fastboot_fail("cannot find partition", response);
Dileep Katta89792382015-02-17 18:48:23 +0530626 return;
627 }
628
629 /* Align blocks to erase group size to avoid erasing other partitions */
630 grp_size = mmc->erase_grp_size;
631 blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
632 if (info.size >= grp_size)
633 blks_size = (info.size - (blks_start - info.start)) &
634 (~(grp_size - 1));
635 else
636 blks_size = 0;
637
638 printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
639 blks_start, blks_start + blks_size);
640
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000641 blks = fb_mmc_blk_write(dev_desc, blks_start, blks_size, NULL);
642
Dileep Katta89792382015-02-17 18:48:23 +0530643 if (blks != blks_size) {
Alex Kiernan1ad5fac2018-05-29 15:30:43 +0000644 pr_err("failed erasing from device %d\n", dev_desc->devnum);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000645 fastboot_fail("failed erasing from device", response);
Dileep Katta89792382015-02-17 18:48:23 +0530646 return;
647 }
648
649 printf("........ erased " LBAFU " bytes from '%s'\n",
650 blks_size * info.blksz, cmd);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000651 fastboot_okay(NULL, response);
Dileep Katta89792382015-02-17 18:48:23 +0530652}