blob: a06c590234fa8a9a755117617159444db769a8dc [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>
Maxime Ripard6af49bc2015-10-15 14:34:19 +020013#include <image-sparse.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060014#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Steve Rae792ac482014-08-26 11:47:27 -070016#include <part.h>
Dileep Kattab1228202015-02-17 18:48:23 +053017#include <mmc.h>
Siarhei Siamashka58f30d82015-10-28 06:24:16 +020018#include <div64.h>
Sam Protsenko540eb952017-05-18 15:04:59 +030019#include <linux/compat.h>
20#include <android_image.h>
Steve Rae792ac482014-08-26 11:47:27 -070021
Alex Kiernand5aa57c2018-05-29 15:30:53 +000022#define FASTBOOT_MAX_BLK_WRITE 16384
23
Sam Protsenko540eb952017-05-18 15:04:59 +030024#define BOOT_PARTITION_NAME "boot"
25
Maxime Riparda58e9c72015-10-15 14:34:14 +020026struct fb_mmc_sparse {
Simon Glasse3394752016-02-29 15:25:34 -070027 struct blk_desc *dev_desc;
Maxime Riparda58e9c72015-10-15 14:34:14 +020028};
29
Filip Brozovic859e6a82020-06-29 13:14:37 +020030static int raw_part_get_info_by_name(struct blk_desc *dev_desc,
Sean Anderson7ea25da2021-02-05 09:38:58 -050031 const char *name,
32 struct disk_partition *info)
Filip Brozovic859e6a82020-06-29 13:14:37 +020033{
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_");
Matthias Schiffer04a5b0f2021-07-30 14:23:54 +020042 strlcat(env_desc_name, name, sizeof(env_desc_name));
Filip Brozovic859e6a82020-06-29 13:14:37 +020043 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;
Sean Andersoncb29fe82021-03-11 00:15:44 -050063 strlcpy((char *)info->name, name, PART_NAME_LEN);
Filip Brozovic859e6a82020-06-29 13:14:37 +020064
65 if (raw_part_desc) {
Sean Anderson7ea25da2021-02-05 09:38:58 -050066 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 }
Filip Brozovic859e6a82020-06-29 13:14:37 +020073 }
74
75 return 0;
76}
77
Sean Andersonc4f2d642021-02-05 09:39:00 -050078static int do_get_part_info(struct blk_desc **dev_desc, const char *name,
79 struct disk_partition *info)
Sean Andersonc83e3842021-02-05 09:38:59 -050080{
81 int ret;
82
Sean Andersonc4f2d642021-02-05 09:39:00 -050083 /* 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
102static int part_get_info_by_name_or_alias(struct blk_desc **dev_desc,
103 const char *name,
104 struct disk_partition *info)
105{
Matthias Schiffer80478f42021-12-16 11:26:38 +0100106 /* strlen("fastboot_partition_alias_") + PART_NAME_LEN + 1 */
107 char env_alias_name[25 + PART_NAME_LEN + 1];
108 char *aliased_part_name;
Sean Andersonc4f2d642021-02-05 09:39:00 -0500109
Matthias Schiffer80478f42021-12-16 11:26:38 +0100110 /* 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;
Sean Andersonc83e3842021-02-05 09:38:59 -0500116
Matthias Schiffer80478f42021-12-16 11:26:38 +0100117 return do_get_part_info(dev_desc, name, info);
Sean Andersonc83e3842021-02-05 09:38:59 -0500118}
119
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000120/**
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 */
128static 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
Steve Rae8cf58db2016-06-07 11:19:36 -0700155static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info,
156 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
Maxime Riparda58e9c72015-10-15 14:34:14 +0200157{
Steve Rae8cf58db2016-06-07 11:19:36 -0700158 struct fb_mmc_sparse *sparse = info->priv;
Simon Glasse3394752016-02-29 15:25:34 -0700159 struct blk_desc *dev_desc = sparse->dev_desc;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200160
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000161 return fb_mmc_blk_write(dev_desc, blk, blkcnt, buffer);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200162}
163
Steve Rae8a99be82016-06-07 11:19:38 -0700164static lbaint_t fb_mmc_sparse_reserve(struct sparse_storage *info,
165 lbaint_t blk, lbaint_t blkcnt)
166{
167 return blkcnt;
168}
169
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600170static 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)
Steve Rae792ac482014-08-26 11:47:27 -0700173{
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));
Siarhei Siamashka58f30d82015-10-28 06:24:16 +0200179 blkcnt = lldiv(blkcnt, info->blksz);
Steve Rae792ac482014-08-26 11:47:27 -0700180
181 if (blkcnt > info->size) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900182 pr_err("too large for partition: '%s'\n", part_name);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000183 fastboot_fail("too large for partition", response);
Steve Rae792ac482014-08-26 11:47:27 -0700184 return;
185 }
186
187 puts("Flashing Raw Image\n");
188
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000189 blks = fb_mmc_blk_write(dev_desc, info->start, blkcnt, buffer);
190
Steve Rae792ac482014-08-26 11:47:27 -0700191 if (blks != blkcnt) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900192 pr_err("failed writing to device %d\n", dev_desc->devnum);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000193 fastboot_fail("failed writing to device", response);
Steve Rae792ac482014-08-26 11:47:27 -0700194 return;
195 }
196
197 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
198 part_name);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000199 fastboot_okay(NULL, response);
Steve Rae792ac482014-08-26 11:47:27 -0700200}
201
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100202#if defined(CONFIG_FASTBOOT_MMC_BOOT_SUPPORT) || \
Patrick Delaunay8b0a29a2021-01-27 14:46:46 +0100203 defined(CONFIG_FASTBOOT_MMC_USER_SUPPORT)
developer6a105562020-01-16 16:11:42 +0800204static 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}
Patrick Delaunay8b0a29a2021-01-27 14:46:46 +0100222#endif
developer6a105562020-01-16 16:11:42 +0800223
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100224#ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT
225static void fb_mmc_boot_ops(struct blk_desc *dev_desc, void *buffer,
226 int hwpart, u32 buff_sz, char *response)
developer6a105562020-01-16 16:11:42 +0800227{
228 lbaint_t blkcnt;
229 lbaint_t blks;
230 unsigned long blksz;
231
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100232 // To operate on EMMC_BOOT1/2 (mmc0boot0/1) we first change the hwpart
233 if (blk_dselect_hwpart(dev_desc, hwpart)) {
developer6a105562020-01-16 16:11:42 +0800234 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
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100252 debug("Start Flashing Image to EMMC_BOOT%d...\n", hwpart);
developer6a105562020-01-16 16:11:42 +0800253
254 blks = fb_mmc_blk_write(dev_desc, 0, blkcnt, buffer);
255
256 if (blks != blkcnt) {
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100257 pr_err("Failed to write EMMC_BOOT%d\n", hwpart);
258 fastboot_fail("Failed to write EMMC_BOOT part",
259 response);
developer6a105562020-01-16 16:11:42 +0800260 return;
261 }
262
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100263 printf("........ wrote %lu bytes to EMMC_BOOT%d\n",
264 blkcnt * blksz, hwpart);
developer6a105562020-01-16 16:11:42 +0800265 } else { /* erase */
266 if (fb_mmc_erase_mmc_hwpart(dev_desc)) {
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100267 pr_err("Failed to erase EMMC_BOOT%d\n", hwpart);
268 fastboot_fail("Failed to erase EMMC_BOOT part",
269 response);
developer6a105562020-01-16 16:11:42 +0800270 return;
271 }
272 }
273
274 fastboot_okay(NULL, response);
275}
276#endif
277
Sam Protsenko540eb952017-05-18 15:04:59 +0300278#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 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100286 * Return: Boot image header sectors count or 0 on error
Sam Protsenko540eb952017-05-18 15:04:59 +0300287 */
288static lbaint_t fb_mmc_get_boot_header(struct blk_desc *dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600289 struct disk_partition *info,
Alex Kiernan27b69de2018-05-29 15:30:40 +0000290 struct andr_img_hdr *hdr,
291 char *response)
Sam Protsenko540eb952017-05-18 15:04:59 +0300292{
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) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000301 pr_err("invalid number of boot sectors: 0\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000302 fastboot_fail("invalid number of boot sectors: 0", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300303 return 0;
304 }
305
306 /* Read the boot image header */
307 res = blk_dread(dev_desc, info->start, hdr_sectors, (void *)hdr);
Tom Rinibda6f772017-08-14 21:00:44 -0400308 if (res != hdr_sectors) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000309 pr_err("cannot read header from boot partition\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000310 fastboot_fail("cannot read header from boot partition",
311 response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300312 return 0;
313 }
314
315 /* Check boot header magic string */
316 res = android_image_check_header(hdr);
317 if (res != 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000318 pr_err("bad boot image magic\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000319 fastboot_fail("boot partition not initialized", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300320 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 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100333 * Return: 0 on success or -1 on error
Sam Protsenko540eb952017-05-18 15:04:59 +0300334 */
335static int fb_mmc_update_zimage(struct blk_desc *dev_desc,
336 void *download_buffer,
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000337 u32 download_bytes,
Alex Kiernan27b69de2018-05-29 15:30:40 +0000338 char *response)
Sam Protsenko540eb952017-05-18 15:04:59 +0300339{
Tom Rini0ac605c2017-06-10 09:15:37 -0400340 uintptr_t hdr_addr; /* boot image header address */
Sam Protsenko540eb952017-05-18 15:04:59 +0300341 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;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600349 struct disk_partition info;
Sam Protsenko540eb952017-05-18 15:04:59 +0300350 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) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000357 pr_err("cannot find boot partition\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000358 fastboot_fail("cannot find boot partition", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300359 return -1;
360 }
361
362 /* Put boot image header in fastboot buffer after downloaded zImage */
Tom Rini0ac605c2017-06-10 09:15:37 -0400363 hdr_addr = (uintptr_t)download_buffer + ALIGN(download_bytes, PAGE_SIZE);
Sam Protsenko540eb952017-05-18 15:04:59 +0300364 hdr = (struct andr_img_hdr *)hdr_addr;
365
366 /* Read boot image header */
Alex Kiernan27b69de2018-05-29 15:30:40 +0000367 hdr_sectors = fb_mmc_get_boot_header(dev_desc, &info, hdr, response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300368 if (hdr_sectors == 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000369 pr_err("unable to read boot image header\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000370 fastboot_fail("unable to read boot image header", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300371 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) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000376 pr_err("moving second stage is not supported yet\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000377 fastboot_fail("moving second stage is not supported yet",
378 response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300379 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);
Tom Rinibda6f772017-08-14 21:00:44 -0400394 if (res != ramdisk_sectors) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000395 pr_err("cannot read ramdisk from boot partition\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000396 fastboot_fail("cannot read ramdisk from boot partition",
397 response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300398 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) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000405 pr_err("cannot writeback boot image header\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000406 fastboot_fail("cannot write back boot image header", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300407 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) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000417 pr_err("cannot write new kernel\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000418 fastboot_fail("cannot write new kernel", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300419 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) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000429 pr_err("cannot write back original ramdisk\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000430 fastboot_fail("cannot write back original ramdisk", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300431 return -1;
432 }
433
434 puts("........ zImage was updated in boot partition\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000435 fastboot_okay(NULL, response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300436 return 0;
437}
438#endif
439
Alex Kiernan32c21942018-05-29 15:30:48 +0000440/**
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000441 * 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
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600445 * @part_info: Pointer to returned struct disk_partition
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000446 * @response: Pointer to fastboot response buffer
447 */
Sam Protsenko0fb37ee2019-06-13 21:11:07 +0300448int fastboot_mmc_get_part_info(const char *part_name,
449 struct blk_desc **dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600450 struct disk_partition *part_info, char *response)
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000451{
Sean Andersonc4f2d642021-02-05 09:39:00 -0500452 int ret;
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000453
Eugeniu Rosca8eba90b2019-03-28 14:31:33 +0100454 if (!part_name || !strcmp(part_name, "")) {
455 fastboot_fail("partition not given", response);
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000456 return -ENOENT;
457 }
458
Sean Andersonc4f2d642021-02-05 09:39:00 -0500459 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;
Filip Brozovic859e6a82020-06-29 13:14:37 +0200478 }
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000479 }
480
Sean Andersonc4f2d642021-02-05 09:39:00 -0500481 return ret;
482}
483
484static 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;
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000495}
496
497/**
Alex Kiernan32c21942018-05-29 15:30:48 +0000498 * 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 */
505void fastboot_mmc_flash_write(const char *cmd, void *download_buffer,
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000506 u32 download_bytes, char *response)
Steve Rae792ac482014-08-26 11:47:27 -0700507{
Simon Glasse3394752016-02-29 15:25:34 -0700508 struct blk_desc *dev_desc;
Oleh Kravchenkoff6c0e32021-05-19 13:31:31 +0300509 struct disk_partition info = {0};
Steve Rae792ac482014-08-26 11:47:27 -0700510
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100511#ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT
developer6a105562020-01-16 16:11:42 +0800512 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) {
Sean Andersonc4f2d642021-02-05 09:39:00 -0500513 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);
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100517 return;
518 }
519 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT2_NAME) == 0) {
Sean Andersonc4f2d642021-02-05 09:39:00 -0500520 dev_desc = fastboot_mmc_get_dev(response);
521 if (dev_desc)
Oleh Kravchenko8c73f852021-05-15 00:06:21 +0300522 fb_mmc_boot_ops(dev_desc, download_buffer, 2,
Sean Andersonc4f2d642021-02-05 09:39:00 -0500523 download_bytes, response);
developer6a105562020-01-16 16:11:42 +0800524 return;
525 }
526#endif
527
Patrick Delaunay8a4f2bd2017-01-27 11:00:41 +0100528#if CONFIG_IS_ENABLED(EFI_PARTITION)
Steve Rae7d059342014-12-12 15:51:54 -0800529 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
Sean Andersonc4f2d642021-02-05 09:39:00 -0500530 dev_desc = fastboot_mmc_get_dev(response);
531 if (!dev_desc)
532 return;
533
Steve Rae7d059342014-12-12 15:51:54 -0800534 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__);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000539 fastboot_fail("invalid GPT partition", response);
Steve Rae7d059342014-12-12 15:51:54 -0800540 return;
541 }
542 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
543 printf("%s: writing GPT partitions failed\n", __func__);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000544 fastboot_fail("writing GPT partitions failed",
545 response);
Steve Rae7d059342014-12-12 15:51:54 -0800546 return;
547 }
Roman Stratiienko330bf502021-01-27 17:40:16 +0200548 part_init(dev_desc);
Steve Rae7d059342014-12-12 15:51:54 -0800549 printf("........ success\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000550 fastboot_okay(NULL, response);
Steve Rae7d059342014-12-12 15:51:54 -0800551 return;
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200552 }
553#endif
554
Patrick Delaunayf7e07722017-01-27 11:00:37 +0100555#if CONFIG_IS_ENABLED(DOS_PARTITION)
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200556 if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) {
Sean Andersonc4f2d642021-02-05 09:39:00 -0500557 dev_desc = fastboot_mmc_get_dev(response);
558 if (!dev_desc)
559 return;
560
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200561 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__);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000565 fastboot_fail("invalid MBR partition", response);
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200566 return;
567 }
Marek Szyprowski29ee06b2020-12-23 13:55:10 +0100568 if (write_mbr_sector(dev_desc, download_buffer)) {
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200569 printf("%s: writing MBR partition failed\n", __func__);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000570 fastboot_fail("writing MBR partition failed",
571 response);
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200572 return;
573 }
Roman Stratiienko330bf502021-01-27 17:40:16 +0200574 part_init(dev_desc);
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200575 printf("........ success\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000576 fastboot_okay(NULL, response);
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200577 return;
578 }
579#endif
580
Sam Protsenko540eb952017-05-18 15:04:59 +0300581#ifdef CONFIG_ANDROID_BOOT_IMAGE
582 if (strncasecmp(cmd, "zimage", 6) == 0) {
Sean Andersonc4f2d642021-02-05 09:39:00 -0500583 dev_desc = fastboot_mmc_get_dev(response);
584 if (dev_desc)
585 fb_mmc_update_zimage(dev_desc, download_buffer,
586 download_bytes, response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300587 return;
588 }
589#endif
590
Simon Glass8b0d42c2023-02-05 15:39:56 -0700591#if IS_ENABLED(CONFIG_FASTBOOT_MMC_USER_SUPPORT)
Oleh Kravchenkoff6c0e32021-05-19 13:31:31 +0300592 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)
Sean Andersonc4f2d642021-02-05 09:39:00 -0500605 return;
Steve Rae792ac482014-08-26 11:47:27 -0700606
Maxime Riparda58e9c72015-10-15 14:34:14 +0200607 if (is_sparse_image(download_buffer)) {
608 struct fb_mmc_sparse sparse_priv;
Steve Rae8cf58db2016-06-07 11:19:36 -0700609 struct sparse_storage sparse;
Jassi Brar427e6692018-04-06 12:05:09 +0530610 int err;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200611
612 sparse_priv.dev_desc = dev_desc;
613
Steve Rae8cf58db2016-06-07 11:19:36 -0700614 sparse.blksz = info.blksz;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200615 sparse.start = info.start;
616 sparse.size = info.size;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200617 sparse.write = fb_mmc_sparse_write;
Steve Rae8a99be82016-06-07 11:19:38 -0700618 sparse.reserve = fb_mmc_sparse_reserve;
Jassi Brar427e6692018-04-06 12:05:09 +0530619 sparse.mssg = fastboot_fail;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200620
621 printf("Flashing sparse image at offset " LBAFU "\n",
Steve Rae8cf58db2016-06-07 11:19:36 -0700622 sparse.start);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200623
Steve Rae8cf58db2016-06-07 11:19:36 -0700624 sparse.priv = &sparse_priv;
Alex Kiernan27b69de2018-05-29 15:30:40 +0000625 err = write_sparse_image(&sparse, cmd, download_buffer,
626 response);
Jassi Brar427e6692018-04-06 12:05:09 +0530627 if (!err)
Alex Kiernan27b69de2018-05-29 15:30:40 +0000628 fastboot_okay(NULL, response);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200629 } else {
Steve Raec6015672014-08-26 11:47:30 -0700630 write_raw_image(dev_desc, &info, cmd, download_buffer,
Alex Kiernan27b69de2018-05-29 15:30:40 +0000631 download_bytes, response);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200632 }
Steve Rae792ac482014-08-26 11:47:27 -0700633}
Dileep Kattab1228202015-02-17 18:48:23 +0530634
Alex Kiernan32c21942018-05-29 15:30:48 +0000635/**
636 * fastboot_mmc_flash_erase() - Erase eMMC for fastboot
637 *
638 * @cmd: Named partition to erase
639 * @response: Pointer to fastboot response buffer
640 */
641void fastboot_mmc_erase(const char *cmd, char *response)
Dileep Kattab1228202015-02-17 18:48:23 +0530642{
Simon Glasse3394752016-02-29 15:25:34 -0700643 struct blk_desc *dev_desc;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600644 struct disk_partition info;
Dileep Kattab1228202015-02-17 18:48:23 +0530645 lbaint_t blks, blks_start, blks_size, grp_size;
646 struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
647
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100648#ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT
developer6a105562020-01-16 16:11:42 +0800649 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) {
650 /* erase EMMC boot1 */
Sean Andersonc4f2d642021-02-05 09:39:00 -0500651 dev_desc = fastboot_mmc_get_dev(response);
652 if (dev_desc)
653 fb_mmc_boot_ops(dev_desc, NULL, 1, 0, response);
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100654 return;
655 }
656 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT2_NAME) == 0) {
657 /* erase EMMC boot2 */
Sean Andersonc4f2d642021-02-05 09:39:00 -0500658 dev_desc = fastboot_mmc_get_dev(response);
659 if (dev_desc)
Oleh Kravchenko8c73f852021-05-15 00:06:21 +0300660 fb_mmc_boot_ops(dev_desc, NULL, 2, 0, response);
developer6a105562020-01-16 16:11:42 +0800661 return;
662 }
663#endif
664
Patrick Delaunay8b0a29a2021-01-27 14:46:46 +0100665#ifdef CONFIG_FASTBOOT_MMC_USER_SUPPORT
developer6a105562020-01-16 16:11:42 +0800666 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) {
667 /* erase EMMC userdata */
Sean Andersonc4f2d642021-02-05 09:39:00 -0500668 dev_desc = fastboot_mmc_get_dev(response);
669 if (!dev_desc)
670 return;
671
developer6a105562020-01-16 16:11:42 +0800672 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
Dileep Kattab1228202015-02-17 18:48:23 +0530679
Sean Andersonc4f2d642021-02-05 09:39:00 -0500680 if (fastboot_mmc_get_part_info(cmd, &dev_desc, &info, response) < 0)
681 return;
Dileep Kattab1228202015-02-17 18:48:23 +0530682
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
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000695 blks = fb_mmc_blk_write(dev_desc, blks_start, blks_size, NULL);
696
Dileep Kattab1228202015-02-17 18:48:23 +0530697 if (blks != blks_size) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000698 pr_err("failed erasing from device %d\n", dev_desc->devnum);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000699 fastboot_fail("failed erasing from device", response);
Dileep Kattab1228202015-02-17 18:48:23 +0530700 return;
701 }
702
703 printf("........ erased " LBAFU " bytes from '%s'\n",
704 blks_size * info.blksz, cmd);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000705 fastboot_okay(NULL, response);
Dileep Kattab1228202015-02-17 18:48:23 +0530706}