blob: 9d25c402028a6dba18cd3c44a3e1cacf22b18a30 [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>
Maxime Ripard3d4ef382015-10-15 14:34:19 +020013#include <image-sparse.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060014#include <image.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Steve Raec0aebb32014-08-26 11:47:27 -070016#include <part.h>
Dileep Katta89792382015-02-17 18:48:23 +053017#include <mmc.h>
Siarhei Siamashka5e0efc12015-10-28 06:24:16 +020018#include <div64.h>
Sam Protsenkoefeccfe2017-05-18 15:04:59 +030019#include <linux/compat.h>
20#include <android_image.h>
Steve Raec0aebb32014-08-26 11:47:27 -070021
Alex Kiernanf73a7df2018-05-29 15:30:53 +000022#define FASTBOOT_MAX_BLK_WRITE 16384
23
Sam Protsenkoefeccfe2017-05-18 15:04:59 +030024#define BOOT_PARTITION_NAME "boot"
25
Maxime Riparda5d1e042015-10-15 14:34:14 +020026struct fb_mmc_sparse {
Simon Glass4101f682016-02-29 15:25:34 -070027 struct blk_desc *dev_desc;
Maxime Riparda5d1e042015-10-15 14:34:14 +020028};
29
Filip Brozovica17c0cb2020-06-29 13:14:37 +020030static int raw_part_get_info_by_name(struct blk_desc *dev_desc,
Sean Andersonf3cf9642021-02-05 09:38:58 -050031 const char *name,
32 struct disk_partition *info)
Filip Brozovica17c0cb2020-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 Schiffer5f9338a2021-07-30 14:23:54 +020042 strlcat(env_desc_name, name, sizeof(env_desc_name));
Filip Brozovica17c0cb2020-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 Anderson69a75292021-03-11 00:15:44 -050063 strlcpy((char *)info->name, name, PART_NAME_LEN);
Filip Brozovica17c0cb2020-06-29 13:14:37 +020064
65 if (raw_part_desc) {
Sean Andersonf3cf9642021-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 Brozovica17c0cb2020-06-29 13:14:37 +020073 }
74
75 return 0;
76}
77
Sean Andersonde1728c2021-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 Andersonae5e6b42021-02-05 09:38:59 -050080{
81 int ret;
82
Sean Andersonde1728c2021-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 Schiffer7e90f772021-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 Andersonde1728c2021-02-05 09:39:00 -0500109
Matthias Schiffer7e90f772021-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 Andersonae5e6b42021-02-05 09:38:59 -0500116
Matthias Schiffer7e90f772021-12-16 11:26:38 +0100117 return do_get_part_info(dev_desc, name, info);
Sean Andersonae5e6b42021-02-05 09:38:59 -0500118}
119
Alex Kiernanf73a7df2018-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 Raecc0f08cd2016-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 Riparda5d1e042015-10-15 14:34:14 +0200157{
Steve Raecc0f08cd2016-06-07 11:19:36 -0700158 struct fb_mmc_sparse *sparse = info->priv;
Simon Glass4101f682016-02-29 15:25:34 -0700159 struct blk_desc *dev_desc = sparse->dev_desc;
Maxime Riparda5d1e042015-10-15 14:34:14 +0200160
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000161 return fb_mmc_blk_write(dev_desc, blk, blkcnt, buffer);
Maxime Riparda5d1e042015-10-15 14:34:14 +0200162}
163
Steve Rae2c724042016-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 Glass05289792020-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 Raec0aebb32014-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 Siamashka5e0efc12015-10-28 06:24:16 +0200179 blkcnt = lldiv(blkcnt, info->blksz);
Steve Raec0aebb32014-08-26 11:47:27 -0700180
181 if (blkcnt > info->size) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900182 pr_err("too large for partition: '%s'\n", part_name);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000183 fastboot_fail("too large for partition", response);
Steve Raec0aebb32014-08-26 11:47:27 -0700184 return;
185 }
186
187 puts("Flashing Raw Image\n");
188
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000189 blks = fb_mmc_blk_write(dev_desc, info->start, blkcnt, buffer);
190
Steve Raec0aebb32014-08-26 11:47:27 -0700191 if (blks != blkcnt) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900192 pr_err("failed writing to device %d\n", dev_desc->devnum);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000193 fastboot_fail("failed writing to device", response);
Steve Raec0aebb32014-08-26 11:47:27 -0700194 return;
195 }
196
197 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
198 part_name);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000199 fastboot_okay(NULL, response);
Steve Raec0aebb32014-08-26 11:47:27 -0700200}
201
Patrick Delaunay3acbc7b2021-01-27 14:46:47 +0100202#if defined(CONFIG_FASTBOOT_MMC_BOOT_SUPPORT) || \
Patrick Delaunay75966962021-01-27 14:46:46 +0100203 defined(CONFIG_FASTBOOT_MMC_USER_SUPPORT)
mingming lee1fdbad02020-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 Delaunay75966962021-01-27 14:46:46 +0100222#endif
mingming lee1fdbad02020-01-16 16:11:42 +0800223
Patrick Delaunay3acbc7b2021-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)
mingming lee1fdbad02020-01-16 16:11:42 +0800227{
228 lbaint_t blkcnt;
229 lbaint_t blks;
230 unsigned long blksz;
231
Patrick Delaunay3acbc7b2021-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)) {
mingming lee1fdbad02020-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 Delaunay3acbc7b2021-01-27 14:46:47 +0100252 debug("Start Flashing Image to EMMC_BOOT%d...\n", hwpart);
mingming lee1fdbad02020-01-16 16:11:42 +0800253
254 blks = fb_mmc_blk_write(dev_desc, 0, blkcnt, buffer);
255
256 if (blks != blkcnt) {
Patrick Delaunay3acbc7b2021-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);
mingming lee1fdbad02020-01-16 16:11:42 +0800260 return;
261 }
262
Patrick Delaunay3acbc7b2021-01-27 14:46:47 +0100263 printf("........ wrote %lu bytes to EMMC_BOOT%d\n",
264 blkcnt * blksz, hwpart);
mingming lee1fdbad02020-01-16 16:11:42 +0800265 } else { /* erase */
266 if (fb_mmc_erase_mmc_hwpart(dev_desc)) {
Patrick Delaunay3acbc7b2021-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);
mingming lee1fdbad02020-01-16 16:11:42 +0800270 return;
271 }
272 }
273
274 fastboot_okay(NULL, response);
275}
276#endif
277
Sam Protsenkoefeccfe2017-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 Schuchardt185f8122022-01-19 18:05:50 +0100286 * Return: Boot image header sectors count or 0 on error
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300287 */
288static lbaint_t fb_mmc_get_boot_header(struct blk_desc *dev_desc,
Simon Glass05289792020-05-10 11:39:57 -0600289 struct disk_partition *info,
Safae Ouajihd71a7322023-02-06 00:50:03 +0100290 struct andr_boot_img_hdr_v0 *hdr,
Alex Kiernanc4ded032018-05-29 15:30:40 +0000291 char *response)
Sam Protsenkoefeccfe2017-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;
Safae Ouajihd71a7322023-02-06 00:50:03 +0100299 hdr_sectors = DIV_ROUND_UP(sizeof(struct andr_boot_img_hdr_v0), sector_size);
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300300 if (hdr_sectors == 0) {
Alex Kiernan1ad5fac2018-05-29 15:30:43 +0000301 pr_err("invalid number of boot sectors: 0\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000302 fastboot_fail("invalid number of boot sectors: 0", response);
Sam Protsenkoefeccfe2017-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 Rini6bafa5a2017-08-14 21:00:44 -0400308 if (res != hdr_sectors) {
Alex Kiernan1ad5fac2018-05-29 15:30:43 +0000309 pr_err("cannot read header from boot partition\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000310 fastboot_fail("cannot read header from boot partition",
311 response);
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300312 return 0;
313 }
314
315 /* Check boot header magic string */
Safae Ouajih734cb472023-02-06 00:50:05 +0100316 if (!is_android_boot_image_header(hdr)) {
Alex Kiernan1ad5fac2018-05-29 15:30:43 +0000317 pr_err("bad boot image magic\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000318 fastboot_fail("boot partition not initialized", response);
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300319 return 0;
320 }
321
322 return hdr_sectors;
323}
324
325/**
326 * Write downloaded zImage to boot partition and repack it properly.
327 *
328 * @param dev_desc MMC device descriptor
329 * @param download_buffer Address to fastboot buffer with zImage in it
330 * @param download_bytes Size of fastboot buffer, in bytes
331 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100332 * Return: 0 on success or -1 on error
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300333 */
334static int fb_mmc_update_zimage(struct blk_desc *dev_desc,
335 void *download_buffer,
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000336 u32 download_bytes,
Alex Kiernanc4ded032018-05-29 15:30:40 +0000337 char *response)
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300338{
Tom Rini2341c802017-06-10 09:15:37 -0400339 uintptr_t hdr_addr; /* boot image header address */
Safae Ouajihd71a7322023-02-06 00:50:03 +0100340 struct andr_boot_img_hdr_v0 *hdr; /* boot image header */
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300341 lbaint_t hdr_sectors; /* boot image header sectors */
342 u8 *ramdisk_buffer;
343 u32 ramdisk_sector_start;
344 u32 ramdisk_sectors;
345 u32 kernel_sector_start;
346 u32 kernel_sectors;
347 u32 sectors_per_page;
Simon Glass05289792020-05-10 11:39:57 -0600348 struct disk_partition info;
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300349 int res;
350
351 puts("Flashing zImage\n");
352
353 /* Get boot partition info */
354 res = part_get_info_by_name(dev_desc, BOOT_PARTITION_NAME, &info);
355 if (res < 0) {
Alex Kiernan1ad5fac2018-05-29 15:30:43 +0000356 pr_err("cannot find boot partition\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000357 fastboot_fail("cannot find boot partition", response);
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300358 return -1;
359 }
360
361 /* Put boot image header in fastboot buffer after downloaded zImage */
Tom Rini2341c802017-06-10 09:15:37 -0400362 hdr_addr = (uintptr_t)download_buffer + ALIGN(download_bytes, PAGE_SIZE);
Safae Ouajihd71a7322023-02-06 00:50:03 +0100363 hdr = (struct andr_boot_img_hdr_v0 *)hdr_addr;
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300364
365 /* Read boot image header */
Alex Kiernanc4ded032018-05-29 15:30:40 +0000366 hdr_sectors = fb_mmc_get_boot_header(dev_desc, &info, hdr, response);
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300367 if (hdr_sectors == 0) {
Alex Kiernan1ad5fac2018-05-29 15:30:43 +0000368 pr_err("unable to read boot image header\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000369 fastboot_fail("unable to read boot image header", response);
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300370 return -1;
371 }
372
Safae Ouajihbc6413b2023-02-06 00:50:16 +0100373 /* Check if boot image header version is 2 or less */
374 if (hdr->header_version > 2) {
375 pr_err("zImage flashing supported only for boot images v2 and less\n");
376 fastboot_fail("zImage flashing supported only for boot images v2 and less",
377 response);
378 return -EOPNOTSUPP;
379 }
380
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300381 /* Check if boot image has second stage in it (we don't support it) */
382 if (hdr->second_size > 0) {
Alex Kiernan1ad5fac2018-05-29 15:30:43 +0000383 pr_err("moving second stage is not supported yet\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000384 fastboot_fail("moving second stage is not supported yet",
385 response);
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300386 return -1;
387 }
388
389 /* Extract ramdisk location */
390 sectors_per_page = hdr->page_size / info.blksz;
391 ramdisk_sector_start = info.start + sectors_per_page;
392 ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
393 sectors_per_page;
394 ramdisk_sectors = DIV_ROUND_UP(hdr->ramdisk_size, hdr->page_size) *
395 sectors_per_page;
396
397 /* Read ramdisk and put it in fastboot buffer after boot image header */
398 ramdisk_buffer = (u8 *)hdr + (hdr_sectors * info.blksz);
399 res = blk_dread(dev_desc, ramdisk_sector_start, ramdisk_sectors,
400 ramdisk_buffer);
Tom Rini6bafa5a2017-08-14 21:00:44 -0400401 if (res != ramdisk_sectors) {
Alex Kiernan1ad5fac2018-05-29 15:30:43 +0000402 pr_err("cannot read ramdisk from boot partition\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000403 fastboot_fail("cannot read ramdisk from boot partition",
404 response);
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300405 return -1;
406 }
407
408 /* Write new kernel size to boot image header */
409 hdr->kernel_size = download_bytes;
410 res = blk_dwrite(dev_desc, info.start, hdr_sectors, (void *)hdr);
411 if (res == 0) {
Alex Kiernan1ad5fac2018-05-29 15:30:43 +0000412 pr_err("cannot writeback boot image header\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000413 fastboot_fail("cannot write back boot image header", response);
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300414 return -1;
415 }
416
417 /* Write the new downloaded kernel */
418 kernel_sector_start = info.start + sectors_per_page;
419 kernel_sectors = DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
420 sectors_per_page;
421 res = blk_dwrite(dev_desc, kernel_sector_start, kernel_sectors,
422 download_buffer);
423 if (res == 0) {
Alex Kiernan1ad5fac2018-05-29 15:30:43 +0000424 pr_err("cannot write new kernel\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000425 fastboot_fail("cannot write new kernel", response);
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300426 return -1;
427 }
428
429 /* Write the saved ramdisk back */
430 ramdisk_sector_start = info.start + sectors_per_page;
431 ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
432 sectors_per_page;
433 res = blk_dwrite(dev_desc, ramdisk_sector_start, ramdisk_sectors,
434 ramdisk_buffer);
435 if (res == 0) {
Alex Kiernan1ad5fac2018-05-29 15:30:43 +0000436 pr_err("cannot write back original ramdisk\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000437 fastboot_fail("cannot write back original ramdisk", response);
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300438 return -1;
439 }
440
441 puts("........ zImage was updated in boot partition\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000442 fastboot_okay(NULL, response);
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300443 return 0;
444}
445#endif
446
Alex Kiernand1a119d2018-05-29 15:30:48 +0000447/**
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000448 * fastboot_mmc_get_part_info() - Lookup eMMC partion by name
449 *
450 * @part_name: Named partition to lookup
451 * @dev_desc: Pointer to returned blk_desc pointer
Simon Glass05289792020-05-10 11:39:57 -0600452 * @part_info: Pointer to returned struct disk_partition
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000453 * @response: Pointer to fastboot response buffer
454 */
Sam Protsenkocacb03e2019-06-13 21:11:07 +0300455int fastboot_mmc_get_part_info(const char *part_name,
456 struct blk_desc **dev_desc,
Simon Glass05289792020-05-10 11:39:57 -0600457 struct disk_partition *part_info, char *response)
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000458{
Sean Andersonde1728c2021-02-05 09:39:00 -0500459 int ret;
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000460
Eugeniu Roscaa40297d2019-03-28 14:31:33 +0100461 if (!part_name || !strcmp(part_name, "")) {
462 fastboot_fail("partition not given", response);
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000463 return -ENOENT;
464 }
465
Sean Andersonde1728c2021-02-05 09:39:00 -0500466 ret = part_get_info_by_name_or_alias(dev_desc, part_name, part_info);
467 if (ret < 0) {
468 switch (ret) {
469 case -ENOSYS:
470 case -EINVAL:
471 fastboot_fail("invalid partition or device", response);
472 break;
473 case -ENODEV:
474 fastboot_fail("no such device", response);
475 break;
476 case -ENOENT:
477 fastboot_fail("no such partition", response);
478 break;
479 case -EPROTONOSUPPORT:
480 fastboot_fail("unknown partition table type", response);
481 break;
482 default:
483 fastboot_fail("unanticipated error", response);
484 break;
Filip Brozovica17c0cb2020-06-29 13:14:37 +0200485 }
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000486 }
487
Sean Andersonde1728c2021-02-05 09:39:00 -0500488 return ret;
489}
490
491static struct blk_desc *fastboot_mmc_get_dev(char *response)
492{
493 struct blk_desc *ret = blk_get_dev("mmc",
494 CONFIG_FASTBOOT_FLASH_MMC_DEV);
495
496 if (!ret || ret->type == DEV_TYPE_UNKNOWN) {
497 pr_err("invalid mmc device\n");
498 fastboot_fail("invalid mmc device", response);
499 return NULL;
500 }
501 return ret;
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000502}
503
504/**
Alex Kiernand1a119d2018-05-29 15:30:48 +0000505 * fastboot_mmc_flash_write() - Write image to eMMC for fastboot
506 *
507 * @cmd: Named partition to write image to
508 * @download_buffer: Pointer to image data
509 * @download_bytes: Size of image data
510 * @response: Pointer to fastboot response buffer
511 */
512void fastboot_mmc_flash_write(const char *cmd, void *download_buffer,
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000513 u32 download_bytes, char *response)
Steve Raec0aebb32014-08-26 11:47:27 -0700514{
Simon Glass4101f682016-02-29 15:25:34 -0700515 struct blk_desc *dev_desc;
Oleh Kravchenko55a202f2021-05-19 13:31:31 +0300516 struct disk_partition info = {0};
Steve Raec0aebb32014-08-26 11:47:27 -0700517
Patrick Delaunay3acbc7b2021-01-27 14:46:47 +0100518#ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT
mingming lee1fdbad02020-01-16 16:11:42 +0800519 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) {
Sean Andersonde1728c2021-02-05 09:39:00 -0500520 dev_desc = fastboot_mmc_get_dev(response);
521 if (dev_desc)
522 fb_mmc_boot_ops(dev_desc, download_buffer, 1,
523 download_bytes, response);
Patrick Delaunay3acbc7b2021-01-27 14:46:47 +0100524 return;
525 }
526 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT2_NAME) == 0) {
Sean Andersonde1728c2021-02-05 09:39:00 -0500527 dev_desc = fastboot_mmc_get_dev(response);
528 if (dev_desc)
Oleh Kravchenko389b6762021-05-15 00:06:21 +0300529 fb_mmc_boot_ops(dev_desc, download_buffer, 2,
Sean Andersonde1728c2021-02-05 09:39:00 -0500530 download_bytes, response);
mingming lee1fdbad02020-01-16 16:11:42 +0800531 return;
532 }
533#endif
534
Patrick Delaunaybd42a942017-01-27 11:00:41 +0100535#if CONFIG_IS_ENABLED(EFI_PARTITION)
Steve Rae0ff7e582014-12-12 15:51:54 -0800536 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
Sean Andersonde1728c2021-02-05 09:39:00 -0500537 dev_desc = fastboot_mmc_get_dev(response);
538 if (!dev_desc)
539 return;
540
Steve Rae0ff7e582014-12-12 15:51:54 -0800541 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
542 __func__);
543 if (is_valid_gpt_buf(dev_desc, download_buffer)) {
544 printf("%s: invalid GPT - refusing to write to flash\n",
545 __func__);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000546 fastboot_fail("invalid GPT partition", response);
Steve Rae0ff7e582014-12-12 15:51:54 -0800547 return;
548 }
549 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
550 printf("%s: writing GPT partitions failed\n", __func__);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000551 fastboot_fail("writing GPT partitions failed",
552 response);
Steve Rae0ff7e582014-12-12 15:51:54 -0800553 return;
554 }
Roman Stratiienkoadb5daf2021-01-27 17:40:16 +0200555 part_init(dev_desc);
Steve Rae0ff7e582014-12-12 15:51:54 -0800556 printf("........ success\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000557 fastboot_okay(NULL, response);
Steve Rae0ff7e582014-12-12 15:51:54 -0800558 return;
Petr Kulhavyb6dd69a2016-09-09 10:27:16 +0200559 }
560#endif
561
Patrick Delaunayb0cf7332017-01-27 11:00:37 +0100562#if CONFIG_IS_ENABLED(DOS_PARTITION)
Petr Kulhavyb6dd69a2016-09-09 10:27:16 +0200563 if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) {
Sean Andersonde1728c2021-02-05 09:39:00 -0500564 dev_desc = fastboot_mmc_get_dev(response);
565 if (!dev_desc)
566 return;
567
Petr Kulhavyb6dd69a2016-09-09 10:27:16 +0200568 printf("%s: updating MBR\n", __func__);
569 if (is_valid_dos_buf(download_buffer)) {
570 printf("%s: invalid MBR - refusing to write to flash\n",
571 __func__);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000572 fastboot_fail("invalid MBR partition", response);
Petr Kulhavyb6dd69a2016-09-09 10:27:16 +0200573 return;
574 }
Marek Szyprowski92f1c892020-12-23 13:55:10 +0100575 if (write_mbr_sector(dev_desc, download_buffer)) {
Petr Kulhavyb6dd69a2016-09-09 10:27:16 +0200576 printf("%s: writing MBR partition failed\n", __func__);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000577 fastboot_fail("writing MBR partition failed",
578 response);
Petr Kulhavyb6dd69a2016-09-09 10:27:16 +0200579 return;
580 }
Roman Stratiienkoadb5daf2021-01-27 17:40:16 +0200581 part_init(dev_desc);
Petr Kulhavyb6dd69a2016-09-09 10:27:16 +0200582 printf("........ success\n");
Alex Kiernanc4ded032018-05-29 15:30:40 +0000583 fastboot_okay(NULL, response);
Petr Kulhavyb6dd69a2016-09-09 10:27:16 +0200584 return;
585 }
586#endif
587
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300588#ifdef CONFIG_ANDROID_BOOT_IMAGE
589 if (strncasecmp(cmd, "zimage", 6) == 0) {
Sean Andersonde1728c2021-02-05 09:39:00 -0500590 dev_desc = fastboot_mmc_get_dev(response);
591 if (dev_desc)
592 fb_mmc_update_zimage(dev_desc, download_buffer,
593 download_bytes, response);
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300594 return;
595 }
596#endif
597
Simon Glass0d9da972023-02-05 15:39:56 -0700598#if IS_ENABLED(CONFIG_FASTBOOT_MMC_USER_SUPPORT)
Oleh Kravchenko55a202f2021-05-19 13:31:31 +0300599 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) {
600 dev_desc = fastboot_mmc_get_dev(response);
601 if (!dev_desc)
602 return;
603
604 strlcpy((char *)&info.name, cmd, sizeof(info.name));
605 info.size = dev_desc->lba;
606 info.blksz = dev_desc->blksz;
607 }
608#endif
609
610 if (!info.name[0] &&
611 fastboot_mmc_get_part_info(cmd, &dev_desc, &info, response) < 0)
Sean Andersonde1728c2021-02-05 09:39:00 -0500612 return;
Steve Raec0aebb32014-08-26 11:47:27 -0700613
Maxime Riparda5d1e042015-10-15 14:34:14 +0200614 if (is_sparse_image(download_buffer)) {
615 struct fb_mmc_sparse sparse_priv;
Steve Raecc0f08cd2016-06-07 11:19:36 -0700616 struct sparse_storage sparse;
Jassi Brar2f83f212018-04-06 12:05:09 +0530617 int err;
Maxime Riparda5d1e042015-10-15 14:34:14 +0200618
619 sparse_priv.dev_desc = dev_desc;
620
Steve Raecc0f08cd2016-06-07 11:19:36 -0700621 sparse.blksz = info.blksz;
Maxime Riparda5d1e042015-10-15 14:34:14 +0200622 sparse.start = info.start;
623 sparse.size = info.size;
Maxime Riparda5d1e042015-10-15 14:34:14 +0200624 sparse.write = fb_mmc_sparse_write;
Steve Rae2c724042016-06-07 11:19:38 -0700625 sparse.reserve = fb_mmc_sparse_reserve;
Jassi Brar2f83f212018-04-06 12:05:09 +0530626 sparse.mssg = fastboot_fail;
Maxime Riparda5d1e042015-10-15 14:34:14 +0200627
628 printf("Flashing sparse image at offset " LBAFU "\n",
Steve Raecc0f08cd2016-06-07 11:19:36 -0700629 sparse.start);
Maxime Riparda5d1e042015-10-15 14:34:14 +0200630
Steve Raecc0f08cd2016-06-07 11:19:36 -0700631 sparse.priv = &sparse_priv;
Alex Kiernanc4ded032018-05-29 15:30:40 +0000632 err = write_sparse_image(&sparse, cmd, download_buffer,
633 response);
Jassi Brar2f83f212018-04-06 12:05:09 +0530634 if (!err)
Alex Kiernanc4ded032018-05-29 15:30:40 +0000635 fastboot_okay(NULL, response);
Maxime Riparda5d1e042015-10-15 14:34:14 +0200636 } else {
Steve Raee5bf9872014-08-26 11:47:30 -0700637 write_raw_image(dev_desc, &info, cmd, download_buffer,
Alex Kiernanc4ded032018-05-29 15:30:40 +0000638 download_bytes, response);
Maxime Riparda5d1e042015-10-15 14:34:14 +0200639 }
Steve Raec0aebb32014-08-26 11:47:27 -0700640}
Dileep Katta89792382015-02-17 18:48:23 +0530641
Alex Kiernand1a119d2018-05-29 15:30:48 +0000642/**
643 * fastboot_mmc_flash_erase() - Erase eMMC for fastboot
644 *
645 * @cmd: Named partition to erase
646 * @response: Pointer to fastboot response buffer
647 */
648void fastboot_mmc_erase(const char *cmd, char *response)
Dileep Katta89792382015-02-17 18:48:23 +0530649{
Simon Glass4101f682016-02-29 15:25:34 -0700650 struct blk_desc *dev_desc;
Simon Glass05289792020-05-10 11:39:57 -0600651 struct disk_partition info;
Dileep Katta89792382015-02-17 18:48:23 +0530652 lbaint_t blks, blks_start, blks_size, grp_size;
653 struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
654
Patrick Delaunay3acbc7b2021-01-27 14:46:47 +0100655#ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT
mingming lee1fdbad02020-01-16 16:11:42 +0800656 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) {
657 /* erase EMMC boot1 */
Sean Andersonde1728c2021-02-05 09:39:00 -0500658 dev_desc = fastboot_mmc_get_dev(response);
659 if (dev_desc)
660 fb_mmc_boot_ops(dev_desc, NULL, 1, 0, response);
Patrick Delaunay3acbc7b2021-01-27 14:46:47 +0100661 return;
662 }
663 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT2_NAME) == 0) {
664 /* erase EMMC boot2 */
Sean Andersonde1728c2021-02-05 09:39:00 -0500665 dev_desc = fastboot_mmc_get_dev(response);
666 if (dev_desc)
Oleh Kravchenko389b6762021-05-15 00:06:21 +0300667 fb_mmc_boot_ops(dev_desc, NULL, 2, 0, response);
mingming lee1fdbad02020-01-16 16:11:42 +0800668 return;
669 }
670#endif
671
Patrick Delaunay75966962021-01-27 14:46:46 +0100672#ifdef CONFIG_FASTBOOT_MMC_USER_SUPPORT
mingming lee1fdbad02020-01-16 16:11:42 +0800673 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) {
674 /* erase EMMC userdata */
Sean Andersonde1728c2021-02-05 09:39:00 -0500675 dev_desc = fastboot_mmc_get_dev(response);
676 if (!dev_desc)
677 return;
678
mingming lee1fdbad02020-01-16 16:11:42 +0800679 if (fb_mmc_erase_mmc_hwpart(dev_desc))
680 fastboot_fail("Failed to erase EMMC_USER", response);
681 else
682 fastboot_okay(NULL, response);
683 return;
684 }
685#endif
686
Sean Andersonde1728c2021-02-05 09:39:00 -0500687 if (fastboot_mmc_get_part_info(cmd, &dev_desc, &info, response) < 0)
688 return;
Dileep Katta89792382015-02-17 18:48:23 +0530689
690 /* Align blocks to erase group size to avoid erasing other partitions */
691 grp_size = mmc->erase_grp_size;
692 blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
693 if (info.size >= grp_size)
694 blks_size = (info.size - (blks_start - info.start)) &
695 (~(grp_size - 1));
696 else
697 blks_size = 0;
698
699 printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
700 blks_start, blks_start + blks_size);
701
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000702 blks = fb_mmc_blk_write(dev_desc, blks_start, blks_size, NULL);
703
Dileep Katta89792382015-02-17 18:48:23 +0530704 if (blks != blks_size) {
Alex Kiernan1ad5fac2018-05-29 15:30:43 +0000705 pr_err("failed erasing from device %d\n", dev_desc->devnum);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000706 fastboot_fail("failed erasing from device", response);
Dileep Katta89792382015-02-17 18:48:23 +0530707 return;
708 }
709
710 printf("........ erased " LBAFU " bytes from '%s'\n",
711 blks_size * info.blksz, cmd);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000712 fastboot_okay(NULL, response);
Dileep Katta89792382015-02-17 18:48:23 +0530713}