blob: c62e414306c3cd981f03e94b6380b63fe0d41fdb [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>
Simon Glass2dc9c342020-05-10 11:40:01 -060015#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Steve Rae792ac482014-08-26 11:47:27 -070017#include <part.h>
Dileep Kattab1228202015-02-17 18:48:23 +053018#include <mmc.h>
Siarhei Siamashka58f30d82015-10-28 06:24:16 +020019#include <div64.h>
Sam Protsenko540eb952017-05-18 15:04:59 +030020#include <linux/compat.h>
21#include <android_image.h>
Steve Rae792ac482014-08-26 11:47:27 -070022
Alex Kiernand5aa57c2018-05-29 15:30:53 +000023#define FASTBOOT_MAX_BLK_WRITE 16384
24
Sam Protsenko540eb952017-05-18 15:04:59 +030025#define BOOT_PARTITION_NAME "boot"
26
Maxime Riparda58e9c72015-10-15 14:34:14 +020027struct fb_mmc_sparse {
Simon Glasse3394752016-02-29 15:25:34 -070028 struct blk_desc *dev_desc;
Maxime Riparda58e9c72015-10-15 14:34:14 +020029};
30
Filip Brozovic859e6a82020-06-29 13:14:37 +020031static int raw_part_get_info_by_name(struct blk_desc *dev_desc,
Sean Anderson7ea25da2021-02-05 09:38:58 -050032 const char *name,
33 struct disk_partition *info)
Filip Brozovic859e6a82020-06-29 13:14:37 +020034{
35 /* strlen("fastboot_raw_partition_") + PART_NAME_LEN + 1 */
36 char env_desc_name[23 + PART_NAME_LEN + 1];
37 char *raw_part_desc;
38 const char *argv[2];
39 const char **parg = argv;
40
41 /* check for raw partition descriptor */
42 strcpy(env_desc_name, "fastboot_raw_partition_");
Matthias Schiffer04a5b0f2021-07-30 14:23:54 +020043 strlcat(env_desc_name, name, sizeof(env_desc_name));
Filip Brozovic859e6a82020-06-29 13:14:37 +020044 raw_part_desc = strdup(env_get(env_desc_name));
45 if (raw_part_desc == NULL)
46 return -ENODEV;
47
48 /*
49 * parse partition descriptor
50 *
51 * <lba_start> <lba_size> [mmcpart <num>]
52 */
53 for (; parg < argv + sizeof(argv) / sizeof(*argv); ++parg) {
54 *parg = strsep(&raw_part_desc, " ");
55 if (*parg == NULL) {
56 pr_err("Invalid number of arguments.\n");
57 return -ENODEV;
58 }
59 }
60
61 info->start = simple_strtoul(argv[0], NULL, 0);
62 info->size = simple_strtoul(argv[1], NULL, 0);
63 info->blksz = dev_desc->blksz;
Sean Andersoncb29fe82021-03-11 00:15:44 -050064 strlcpy((char *)info->name, name, PART_NAME_LEN);
Filip Brozovic859e6a82020-06-29 13:14:37 +020065
66 if (raw_part_desc) {
Sean Anderson7ea25da2021-02-05 09:38:58 -050067 if (strcmp(strsep(&raw_part_desc, " "), "mmcpart") == 0) {
68 ulong mmcpart = simple_strtoul(raw_part_desc, NULL, 0);
69 int ret = blk_dselect_hwpart(dev_desc, mmcpart);
70
71 if (ret)
72 return ret;
73 }
Filip Brozovic859e6a82020-06-29 13:14:37 +020074 }
75
76 return 0;
77}
78
Sean Andersonc4f2d642021-02-05 09:39:00 -050079static int do_get_part_info(struct blk_desc **dev_desc, const char *name,
80 struct disk_partition *info)
Sean Andersonc83e3842021-02-05 09:38:59 -050081{
82 int ret;
83
Sean Andersonc4f2d642021-02-05 09:39:00 -050084 /* First try partition names on the default device */
85 *dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
86 if (*dev_desc) {
87 ret = part_get_info_by_name(*dev_desc, name, info);
88 if (ret >= 0)
89 return ret;
90
91 /* Then try raw partitions */
92 ret = raw_part_get_info_by_name(*dev_desc, name, info);
93 if (ret >= 0)
94 return ret;
95 }
96
97 /* Then try dev.hwpart:part */
98 ret = part_get_info_by_dev_and_name_or_num("mmc", name, dev_desc,
99 info, true);
100 return ret;
101}
102
103static int part_get_info_by_name_or_alias(struct blk_desc **dev_desc,
104 const char *name,
105 struct disk_partition *info)
106{
Matthias Schiffer80478f42021-12-16 11:26:38 +0100107 /* strlen("fastboot_partition_alias_") + PART_NAME_LEN + 1 */
108 char env_alias_name[25 + PART_NAME_LEN + 1];
109 char *aliased_part_name;
Sean Andersonc4f2d642021-02-05 09:39:00 -0500110
Matthias Schiffer80478f42021-12-16 11:26:38 +0100111 /* check for alias */
112 strlcpy(env_alias_name, "fastboot_partition_alias_", sizeof(env_alias_name));
113 strlcat(env_alias_name, name, sizeof(env_alias_name));
114 aliased_part_name = env_get(env_alias_name);
115 if (aliased_part_name)
116 name = aliased_part_name;
Sean Andersonc83e3842021-02-05 09:38:59 -0500117
Matthias Schiffer80478f42021-12-16 11:26:38 +0100118 return do_get_part_info(dev_desc, name, info);
Sean Andersonc83e3842021-02-05 09:38:59 -0500119}
120
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000121/**
122 * fb_mmc_blk_write() - Write/erase MMC in chunks of FASTBOOT_MAX_BLK_WRITE
123 *
124 * @block_dev: Pointer to block device
125 * @start: First block to write/erase
126 * @blkcnt: Count of blocks
127 * @buffer: Pointer to data buffer for write or NULL for erase
128 */
129static lbaint_t fb_mmc_blk_write(struct blk_desc *block_dev, lbaint_t start,
130 lbaint_t blkcnt, const void *buffer)
131{
132 lbaint_t blk = start;
133 lbaint_t blks_written;
134 lbaint_t cur_blkcnt;
135 lbaint_t blks = 0;
136 int i;
137
138 for (i = 0; i < blkcnt; i += FASTBOOT_MAX_BLK_WRITE) {
139 cur_blkcnt = min((int)blkcnt - i, FASTBOOT_MAX_BLK_WRITE);
140 if (buffer) {
141 if (fastboot_progress_callback)
142 fastboot_progress_callback("writing");
143 blks_written = blk_dwrite(block_dev, blk, cur_blkcnt,
144 buffer + (i * block_dev->blksz));
145 } else {
146 if (fastboot_progress_callback)
147 fastboot_progress_callback("erasing");
148 blks_written = blk_derase(block_dev, blk, cur_blkcnt);
149 }
150 blk += blks_written;
151 blks += blks_written;
152 }
153 return blks;
154}
155
Steve Rae8cf58db2016-06-07 11:19:36 -0700156static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info,
157 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
Maxime Riparda58e9c72015-10-15 14:34:14 +0200158{
Steve Rae8cf58db2016-06-07 11:19:36 -0700159 struct fb_mmc_sparse *sparse = info->priv;
Simon Glasse3394752016-02-29 15:25:34 -0700160 struct blk_desc *dev_desc = sparse->dev_desc;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200161
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000162 return fb_mmc_blk_write(dev_desc, blk, blkcnt, buffer);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200163}
164
Steve Rae8a99be82016-06-07 11:19:38 -0700165static lbaint_t fb_mmc_sparse_reserve(struct sparse_storage *info,
166 lbaint_t blk, lbaint_t blkcnt)
167{
168 return blkcnt;
169}
170
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600171static void write_raw_image(struct blk_desc *dev_desc,
172 struct disk_partition *info, const char *part_name,
173 void *buffer, u32 download_bytes, char *response)
Steve Rae792ac482014-08-26 11:47:27 -0700174{
175 lbaint_t blkcnt;
176 lbaint_t blks;
177
178 /* determine number of blocks to write */
179 blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
Siarhei Siamashka58f30d82015-10-28 06:24:16 +0200180 blkcnt = lldiv(blkcnt, info->blksz);
Steve Rae792ac482014-08-26 11:47:27 -0700181
182 if (blkcnt > info->size) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900183 pr_err("too large for partition: '%s'\n", part_name);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000184 fastboot_fail("too large for partition", response);
Steve Rae792ac482014-08-26 11:47:27 -0700185 return;
186 }
187
188 puts("Flashing Raw Image\n");
189
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000190 blks = fb_mmc_blk_write(dev_desc, info->start, blkcnt, buffer);
191
Steve Rae792ac482014-08-26 11:47:27 -0700192 if (blks != blkcnt) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900193 pr_err("failed writing to device %d\n", dev_desc->devnum);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000194 fastboot_fail("failed writing to device", response);
Steve Rae792ac482014-08-26 11:47:27 -0700195 return;
196 }
197
198 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
199 part_name);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000200 fastboot_okay(NULL, response);
Steve Rae792ac482014-08-26 11:47:27 -0700201}
202
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100203#if defined(CONFIG_FASTBOOT_MMC_BOOT_SUPPORT) || \
Patrick Delaunay8b0a29a2021-01-27 14:46:46 +0100204 defined(CONFIG_FASTBOOT_MMC_USER_SUPPORT)
developer6a105562020-01-16 16:11:42 +0800205static int fb_mmc_erase_mmc_hwpart(struct blk_desc *dev_desc)
206{
207 lbaint_t blks;
208
209 debug("Start Erasing mmc hwpart[%u]...\n", dev_desc->hwpart);
210
211 blks = fb_mmc_blk_write(dev_desc, 0, dev_desc->lba, NULL);
212
213 if (blks != dev_desc->lba) {
214 pr_err("Failed to erase mmc hwpart[%u]\n", dev_desc->hwpart);
215 return 1;
216 }
217
218 printf("........ erased %lu bytes from mmc hwpart[%u]\n",
219 dev_desc->lba * dev_desc->blksz, dev_desc->hwpart);
220
221 return 0;
222}
Patrick Delaunay8b0a29a2021-01-27 14:46:46 +0100223#endif
developer6a105562020-01-16 16:11:42 +0800224
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100225#ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT
226static void fb_mmc_boot_ops(struct blk_desc *dev_desc, void *buffer,
227 int hwpart, u32 buff_sz, char *response)
developer6a105562020-01-16 16:11:42 +0800228{
229 lbaint_t blkcnt;
230 lbaint_t blks;
231 unsigned long blksz;
232
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100233 // To operate on EMMC_BOOT1/2 (mmc0boot0/1) we first change the hwpart
234 if (blk_dselect_hwpart(dev_desc, hwpart)) {
developer6a105562020-01-16 16:11:42 +0800235 pr_err("Failed to select hwpart\n");
236 fastboot_fail("Failed to select hwpart", response);
237 return;
238 }
239
240 if (buffer) { /* flash */
241
242 /* determine number of blocks to write */
243 blksz = dev_desc->blksz;
244 blkcnt = ((buff_sz + (blksz - 1)) & ~(blksz - 1));
245 blkcnt = lldiv(blkcnt, blksz);
246
247 if (blkcnt > dev_desc->lba) {
248 pr_err("Image size too large\n");
249 fastboot_fail("Image size too large", response);
250 return;
251 }
252
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100253 debug("Start Flashing Image to EMMC_BOOT%d...\n", hwpart);
developer6a105562020-01-16 16:11:42 +0800254
255 blks = fb_mmc_blk_write(dev_desc, 0, blkcnt, buffer);
256
257 if (blks != blkcnt) {
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100258 pr_err("Failed to write EMMC_BOOT%d\n", hwpart);
259 fastboot_fail("Failed to write EMMC_BOOT part",
260 response);
developer6a105562020-01-16 16:11:42 +0800261 return;
262 }
263
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100264 printf("........ wrote %lu bytes to EMMC_BOOT%d\n",
265 blkcnt * blksz, hwpart);
developer6a105562020-01-16 16:11:42 +0800266 } else { /* erase */
267 if (fb_mmc_erase_mmc_hwpart(dev_desc)) {
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100268 pr_err("Failed to erase EMMC_BOOT%d\n", hwpart);
269 fastboot_fail("Failed to erase EMMC_BOOT part",
270 response);
developer6a105562020-01-16 16:11:42 +0800271 return;
272 }
273 }
274
275 fastboot_okay(NULL, response);
276}
277#endif
278
Sam Protsenko540eb952017-05-18 15:04:59 +0300279#ifdef CONFIG_ANDROID_BOOT_IMAGE
280/**
281 * Read Android boot image header from boot partition.
282 *
283 * @param[in] dev_desc MMC device descriptor
284 * @param[in] info Boot partition info
285 * @param[out] hdr Where to store read boot image header
286 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100287 * Return: Boot image header sectors count or 0 on error
Sam Protsenko540eb952017-05-18 15:04:59 +0300288 */
289static lbaint_t fb_mmc_get_boot_header(struct blk_desc *dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600290 struct disk_partition *info,
Alex Kiernan27b69de2018-05-29 15:30:40 +0000291 struct andr_img_hdr *hdr,
292 char *response)
Sam Protsenko540eb952017-05-18 15:04:59 +0300293{
294 ulong sector_size; /* boot partition sector size */
295 lbaint_t hdr_sectors; /* boot image header sectors count */
296 int res;
297
298 /* Calculate boot image sectors count */
299 sector_size = info->blksz;
300 hdr_sectors = DIV_ROUND_UP(sizeof(struct andr_img_hdr), sector_size);
301 if (hdr_sectors == 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000302 pr_err("invalid number of boot sectors: 0\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000303 fastboot_fail("invalid number of boot sectors: 0", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300304 return 0;
305 }
306
307 /* Read the boot image header */
308 res = blk_dread(dev_desc, info->start, hdr_sectors, (void *)hdr);
Tom Rinibda6f772017-08-14 21:00:44 -0400309 if (res != hdr_sectors) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000310 pr_err("cannot read header from boot partition\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000311 fastboot_fail("cannot read header from boot partition",
312 response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300313 return 0;
314 }
315
316 /* Check boot header magic string */
317 res = android_image_check_header(hdr);
318 if (res != 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000319 pr_err("bad boot image magic\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000320 fastboot_fail("boot partition not initialized", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300321 return 0;
322 }
323
324 return hdr_sectors;
325}
326
327/**
328 * Write downloaded zImage to boot partition and repack it properly.
329 *
330 * @param dev_desc MMC device descriptor
331 * @param download_buffer Address to fastboot buffer with zImage in it
332 * @param download_bytes Size of fastboot buffer, in bytes
333 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100334 * Return: 0 on success or -1 on error
Sam Protsenko540eb952017-05-18 15:04:59 +0300335 */
336static int fb_mmc_update_zimage(struct blk_desc *dev_desc,
337 void *download_buffer,
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000338 u32 download_bytes,
Alex Kiernan27b69de2018-05-29 15:30:40 +0000339 char *response)
Sam Protsenko540eb952017-05-18 15:04:59 +0300340{
Tom Rini0ac605c2017-06-10 09:15:37 -0400341 uintptr_t hdr_addr; /* boot image header address */
Sam Protsenko540eb952017-05-18 15:04:59 +0300342 struct andr_img_hdr *hdr; /* boot image header */
343 lbaint_t hdr_sectors; /* boot image header sectors */
344 u8 *ramdisk_buffer;
345 u32 ramdisk_sector_start;
346 u32 ramdisk_sectors;
347 u32 kernel_sector_start;
348 u32 kernel_sectors;
349 u32 sectors_per_page;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600350 struct disk_partition info;
Sam Protsenko540eb952017-05-18 15:04:59 +0300351 int res;
352
353 puts("Flashing zImage\n");
354
355 /* Get boot partition info */
356 res = part_get_info_by_name(dev_desc, BOOT_PARTITION_NAME, &info);
357 if (res < 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000358 pr_err("cannot find boot partition\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000359 fastboot_fail("cannot find boot partition", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300360 return -1;
361 }
362
363 /* Put boot image header in fastboot buffer after downloaded zImage */
Tom Rini0ac605c2017-06-10 09:15:37 -0400364 hdr_addr = (uintptr_t)download_buffer + ALIGN(download_bytes, PAGE_SIZE);
Sam Protsenko540eb952017-05-18 15:04:59 +0300365 hdr = (struct andr_img_hdr *)hdr_addr;
366
367 /* Read boot image header */
Alex Kiernan27b69de2018-05-29 15:30:40 +0000368 hdr_sectors = fb_mmc_get_boot_header(dev_desc, &info, hdr, response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300369 if (hdr_sectors == 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000370 pr_err("unable to read boot image header\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000371 fastboot_fail("unable to read boot image header", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300372 return -1;
373 }
374
375 /* Check if boot image has second stage in it (we don't support it) */
376 if (hdr->second_size > 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000377 pr_err("moving second stage is not supported yet\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000378 fastboot_fail("moving second stage is not supported yet",
379 response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300380 return -1;
381 }
382
383 /* Extract ramdisk location */
384 sectors_per_page = hdr->page_size / info.blksz;
385 ramdisk_sector_start = info.start + sectors_per_page;
386 ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
387 sectors_per_page;
388 ramdisk_sectors = DIV_ROUND_UP(hdr->ramdisk_size, hdr->page_size) *
389 sectors_per_page;
390
391 /* Read ramdisk and put it in fastboot buffer after boot image header */
392 ramdisk_buffer = (u8 *)hdr + (hdr_sectors * info.blksz);
393 res = blk_dread(dev_desc, ramdisk_sector_start, ramdisk_sectors,
394 ramdisk_buffer);
Tom Rinibda6f772017-08-14 21:00:44 -0400395 if (res != ramdisk_sectors) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000396 pr_err("cannot read ramdisk from boot partition\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000397 fastboot_fail("cannot read ramdisk from boot partition",
398 response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300399 return -1;
400 }
401
402 /* Write new kernel size to boot image header */
403 hdr->kernel_size = download_bytes;
404 res = blk_dwrite(dev_desc, info.start, hdr_sectors, (void *)hdr);
405 if (res == 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000406 pr_err("cannot writeback boot image header\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000407 fastboot_fail("cannot write back boot image header", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300408 return -1;
409 }
410
411 /* Write the new downloaded kernel */
412 kernel_sector_start = info.start + sectors_per_page;
413 kernel_sectors = DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
414 sectors_per_page;
415 res = blk_dwrite(dev_desc, kernel_sector_start, kernel_sectors,
416 download_buffer);
417 if (res == 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000418 pr_err("cannot write new kernel\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000419 fastboot_fail("cannot write new kernel", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300420 return -1;
421 }
422
423 /* Write the saved ramdisk back */
424 ramdisk_sector_start = info.start + sectors_per_page;
425 ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
426 sectors_per_page;
427 res = blk_dwrite(dev_desc, ramdisk_sector_start, ramdisk_sectors,
428 ramdisk_buffer);
429 if (res == 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000430 pr_err("cannot write back original ramdisk\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000431 fastboot_fail("cannot write back original ramdisk", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300432 return -1;
433 }
434
435 puts("........ zImage was updated in boot partition\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000436 fastboot_okay(NULL, response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300437 return 0;
438}
439#endif
440
Alex Kiernan32c21942018-05-29 15:30:48 +0000441/**
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000442 * fastboot_mmc_get_part_info() - Lookup eMMC partion by name
443 *
444 * @part_name: Named partition to lookup
445 * @dev_desc: Pointer to returned blk_desc pointer
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600446 * @part_info: Pointer to returned struct disk_partition
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000447 * @response: Pointer to fastboot response buffer
448 */
Sam Protsenko0fb37ee2019-06-13 21:11:07 +0300449int fastboot_mmc_get_part_info(const char *part_name,
450 struct blk_desc **dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600451 struct disk_partition *part_info, char *response)
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000452{
Sean Andersonc4f2d642021-02-05 09:39:00 -0500453 int ret;
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000454
Eugeniu Rosca8eba90b2019-03-28 14:31:33 +0100455 if (!part_name || !strcmp(part_name, "")) {
456 fastboot_fail("partition not given", response);
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000457 return -ENOENT;
458 }
459
Sean Andersonc4f2d642021-02-05 09:39:00 -0500460 ret = part_get_info_by_name_or_alias(dev_desc, part_name, part_info);
461 if (ret < 0) {
462 switch (ret) {
463 case -ENOSYS:
464 case -EINVAL:
465 fastboot_fail("invalid partition or device", response);
466 break;
467 case -ENODEV:
468 fastboot_fail("no such device", response);
469 break;
470 case -ENOENT:
471 fastboot_fail("no such partition", response);
472 break;
473 case -EPROTONOSUPPORT:
474 fastboot_fail("unknown partition table type", response);
475 break;
476 default:
477 fastboot_fail("unanticipated error", response);
478 break;
Filip Brozovic859e6a82020-06-29 13:14:37 +0200479 }
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000480 }
481
Sean Andersonc4f2d642021-02-05 09:39:00 -0500482 return ret;
483}
484
485static struct blk_desc *fastboot_mmc_get_dev(char *response)
486{
487 struct blk_desc *ret = blk_get_dev("mmc",
488 CONFIG_FASTBOOT_FLASH_MMC_DEV);
489
490 if (!ret || ret->type == DEV_TYPE_UNKNOWN) {
491 pr_err("invalid mmc device\n");
492 fastboot_fail("invalid mmc device", response);
493 return NULL;
494 }
495 return ret;
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000496}
497
498/**
Alex Kiernan32c21942018-05-29 15:30:48 +0000499 * fastboot_mmc_flash_write() - Write image to eMMC for fastboot
500 *
501 * @cmd: Named partition to write image to
502 * @download_buffer: Pointer to image data
503 * @download_bytes: Size of image data
504 * @response: Pointer to fastboot response buffer
505 */
506void fastboot_mmc_flash_write(const char *cmd, void *download_buffer,
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000507 u32 download_bytes, char *response)
Steve Rae792ac482014-08-26 11:47:27 -0700508{
Simon Glasse3394752016-02-29 15:25:34 -0700509 struct blk_desc *dev_desc;
Oleh Kravchenkoff6c0e32021-05-19 13:31:31 +0300510 struct disk_partition info = {0};
Steve Rae792ac482014-08-26 11:47:27 -0700511
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100512#ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT
developer6a105562020-01-16 16:11:42 +0800513 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) {
Sean Andersonc4f2d642021-02-05 09:39:00 -0500514 dev_desc = fastboot_mmc_get_dev(response);
515 if (dev_desc)
516 fb_mmc_boot_ops(dev_desc, download_buffer, 1,
517 download_bytes, response);
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100518 return;
519 }
520 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT2_NAME) == 0) {
Sean Andersonc4f2d642021-02-05 09:39:00 -0500521 dev_desc = fastboot_mmc_get_dev(response);
522 if (dev_desc)
Oleh Kravchenko8c73f852021-05-15 00:06:21 +0300523 fb_mmc_boot_ops(dev_desc, download_buffer, 2,
Sean Andersonc4f2d642021-02-05 09:39:00 -0500524 download_bytes, response);
developer6a105562020-01-16 16:11:42 +0800525 return;
526 }
527#endif
528
Patrick Delaunay8a4f2bd2017-01-27 11:00:41 +0100529#if CONFIG_IS_ENABLED(EFI_PARTITION)
Steve Rae7d059342014-12-12 15:51:54 -0800530 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
Sean Andersonc4f2d642021-02-05 09:39:00 -0500531 dev_desc = fastboot_mmc_get_dev(response);
532 if (!dev_desc)
533 return;
534
Steve Rae7d059342014-12-12 15:51:54 -0800535 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
536 __func__);
537 if (is_valid_gpt_buf(dev_desc, download_buffer)) {
538 printf("%s: invalid GPT - refusing to write to flash\n",
539 __func__);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000540 fastboot_fail("invalid GPT partition", response);
Steve Rae7d059342014-12-12 15:51:54 -0800541 return;
542 }
543 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
544 printf("%s: writing GPT partitions failed\n", __func__);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000545 fastboot_fail("writing GPT partitions failed",
546 response);
Steve Rae7d059342014-12-12 15:51:54 -0800547 return;
548 }
Roman Stratiienko330bf502021-01-27 17:40:16 +0200549 part_init(dev_desc);
Steve Rae7d059342014-12-12 15:51:54 -0800550 printf("........ success\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000551 fastboot_okay(NULL, response);
Steve Rae7d059342014-12-12 15:51:54 -0800552 return;
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200553 }
554#endif
555
Patrick Delaunayf7e07722017-01-27 11:00:37 +0100556#if CONFIG_IS_ENABLED(DOS_PARTITION)
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200557 if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) {
Sean Andersonc4f2d642021-02-05 09:39:00 -0500558 dev_desc = fastboot_mmc_get_dev(response);
559 if (!dev_desc)
560 return;
561
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200562 printf("%s: updating MBR\n", __func__);
563 if (is_valid_dos_buf(download_buffer)) {
564 printf("%s: invalid MBR - refusing to write to flash\n",
565 __func__);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000566 fastboot_fail("invalid MBR partition", response);
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200567 return;
568 }
Marek Szyprowski29ee06b2020-12-23 13:55:10 +0100569 if (write_mbr_sector(dev_desc, download_buffer)) {
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200570 printf("%s: writing MBR partition failed\n", __func__);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000571 fastboot_fail("writing MBR partition failed",
572 response);
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200573 return;
574 }
Roman Stratiienko330bf502021-01-27 17:40:16 +0200575 part_init(dev_desc);
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200576 printf("........ success\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000577 fastboot_okay(NULL, response);
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200578 return;
579 }
580#endif
581
Sam Protsenko540eb952017-05-18 15:04:59 +0300582#ifdef CONFIG_ANDROID_BOOT_IMAGE
583 if (strncasecmp(cmd, "zimage", 6) == 0) {
Sean Andersonc4f2d642021-02-05 09:39:00 -0500584 dev_desc = fastboot_mmc_get_dev(response);
585 if (dev_desc)
586 fb_mmc_update_zimage(dev_desc, download_buffer,
587 download_bytes, response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300588 return;
589 }
590#endif
591
Oleh Kravchenkoff6c0e32021-05-19 13:31:31 +0300592#if CONFIG_IS_ENABLED(FASTBOOT_MMC_USER_SUPPORT)
593 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) {
594 dev_desc = fastboot_mmc_get_dev(response);
595 if (!dev_desc)
596 return;
597
598 strlcpy((char *)&info.name, cmd, sizeof(info.name));
599 info.size = dev_desc->lba;
600 info.blksz = dev_desc->blksz;
601 }
602#endif
603
604 if (!info.name[0] &&
605 fastboot_mmc_get_part_info(cmd, &dev_desc, &info, response) < 0)
Sean Andersonc4f2d642021-02-05 09:39:00 -0500606 return;
Steve Rae792ac482014-08-26 11:47:27 -0700607
Maxime Riparda58e9c72015-10-15 14:34:14 +0200608 if (is_sparse_image(download_buffer)) {
609 struct fb_mmc_sparse sparse_priv;
Steve Rae8cf58db2016-06-07 11:19:36 -0700610 struct sparse_storage sparse;
Jassi Brar427e6692018-04-06 12:05:09 +0530611 int err;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200612
613 sparse_priv.dev_desc = dev_desc;
614
Steve Rae8cf58db2016-06-07 11:19:36 -0700615 sparse.blksz = info.blksz;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200616 sparse.start = info.start;
617 sparse.size = info.size;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200618 sparse.write = fb_mmc_sparse_write;
Steve Rae8a99be82016-06-07 11:19:38 -0700619 sparse.reserve = fb_mmc_sparse_reserve;
Jassi Brar427e6692018-04-06 12:05:09 +0530620 sparse.mssg = fastboot_fail;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200621
622 printf("Flashing sparse image at offset " LBAFU "\n",
Steve Rae8cf58db2016-06-07 11:19:36 -0700623 sparse.start);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200624
Steve Rae8cf58db2016-06-07 11:19:36 -0700625 sparse.priv = &sparse_priv;
Alex Kiernan27b69de2018-05-29 15:30:40 +0000626 err = write_sparse_image(&sparse, cmd, download_buffer,
627 response);
Jassi Brar427e6692018-04-06 12:05:09 +0530628 if (!err)
Alex Kiernan27b69de2018-05-29 15:30:40 +0000629 fastboot_okay(NULL, response);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200630 } else {
Steve Raec6015672014-08-26 11:47:30 -0700631 write_raw_image(dev_desc, &info, cmd, download_buffer,
Alex Kiernan27b69de2018-05-29 15:30:40 +0000632 download_bytes, response);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200633 }
Steve Rae792ac482014-08-26 11:47:27 -0700634}
Dileep Kattab1228202015-02-17 18:48:23 +0530635
Alex Kiernan32c21942018-05-29 15:30:48 +0000636/**
637 * fastboot_mmc_flash_erase() - Erase eMMC for fastboot
638 *
639 * @cmd: Named partition to erase
640 * @response: Pointer to fastboot response buffer
641 */
642void fastboot_mmc_erase(const char *cmd, char *response)
Dileep Kattab1228202015-02-17 18:48:23 +0530643{
Simon Glasse3394752016-02-29 15:25:34 -0700644 struct blk_desc *dev_desc;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600645 struct disk_partition info;
Dileep Kattab1228202015-02-17 18:48:23 +0530646 lbaint_t blks, blks_start, blks_size, grp_size;
647 struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
648
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100649#ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT
developer6a105562020-01-16 16:11:42 +0800650 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) {
651 /* erase EMMC boot1 */
Sean Andersonc4f2d642021-02-05 09:39:00 -0500652 dev_desc = fastboot_mmc_get_dev(response);
653 if (dev_desc)
654 fb_mmc_boot_ops(dev_desc, NULL, 1, 0, response);
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100655 return;
656 }
657 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT2_NAME) == 0) {
658 /* erase EMMC boot2 */
Sean Andersonc4f2d642021-02-05 09:39:00 -0500659 dev_desc = fastboot_mmc_get_dev(response);
660 if (dev_desc)
Oleh Kravchenko8c73f852021-05-15 00:06:21 +0300661 fb_mmc_boot_ops(dev_desc, NULL, 2, 0, response);
developer6a105562020-01-16 16:11:42 +0800662 return;
663 }
664#endif
665
Patrick Delaunay8b0a29a2021-01-27 14:46:46 +0100666#ifdef CONFIG_FASTBOOT_MMC_USER_SUPPORT
developer6a105562020-01-16 16:11:42 +0800667 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) {
668 /* erase EMMC userdata */
Sean Andersonc4f2d642021-02-05 09:39:00 -0500669 dev_desc = fastboot_mmc_get_dev(response);
670 if (!dev_desc)
671 return;
672
developer6a105562020-01-16 16:11:42 +0800673 if (fb_mmc_erase_mmc_hwpart(dev_desc))
674 fastboot_fail("Failed to erase EMMC_USER", response);
675 else
676 fastboot_okay(NULL, response);
677 return;
678 }
679#endif
Dileep Kattab1228202015-02-17 18:48:23 +0530680
Sean Andersonc4f2d642021-02-05 09:39:00 -0500681 if (fastboot_mmc_get_part_info(cmd, &dev_desc, &info, response) < 0)
682 return;
Dileep Kattab1228202015-02-17 18:48:23 +0530683
684 /* Align blocks to erase group size to avoid erasing other partitions */
685 grp_size = mmc->erase_grp_size;
686 blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
687 if (info.size >= grp_size)
688 blks_size = (info.size - (blks_start - info.start)) &
689 (~(grp_size - 1));
690 else
691 blks_size = 0;
692
693 printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
694 blks_start, blks_start + blks_size);
695
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000696 blks = fb_mmc_blk_write(dev_desc, blks_start, blks_size, NULL);
697
Dileep Kattab1228202015-02-17 18:48:23 +0530698 if (blks != blks_size) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000699 pr_err("failed erasing from device %d\n", dev_desc->devnum);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000700 fastboot_fail("failed erasing from device", response);
Dileep Kattab1228202015-02-17 18:48:23 +0530701 return;
702 }
703
704 printf("........ erased " LBAFU " bytes from '%s'\n",
705 blks_size * info.blksz, cmd);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000706 fastboot_okay(NULL, response);
Dileep Kattab1228202015-02-17 18:48:23 +0530707}