blob: f11eb66761b1ed46d2e8401e71b0e4342b656ec6 [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>
Simon Glass2ee8ada2016-02-29 15:25:52 -07007#include <blk.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -06008#include <env.h>
Maxime Ripard00e8eb72015-10-15 14:34:13 +02009#include <fastboot.h>
Alex Kiernand5aa57c2018-05-29 15:30:53 +000010#include <fastboot-internal.h>
Steve Rae792ac482014-08-26 11:47:27 -070011#include <fb_mmc.h>
Maxime Ripard6af49bc2015-10-15 14:34:19 +020012#include <image-sparse.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060013#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Steve Rae792ac482014-08-26 11:47:27 -070015#include <part.h>
Dileep Kattab1228202015-02-17 18:48:23 +053016#include <mmc.h>
Siarhei Siamashka58f30d82015-10-28 06:24:16 +020017#include <div64.h>
Sam Protsenko540eb952017-05-18 15:04:59 +030018#include <linux/compat.h>
19#include <android_image.h>
Steve Rae792ac482014-08-26 11:47:27 -070020
Sam Protsenko540eb952017-05-18 15:04:59 +030021#define BOOT_PARTITION_NAME "boot"
22
Maxime Riparda58e9c72015-10-15 14:34:14 +020023struct fb_mmc_sparse {
Simon Glasse3394752016-02-29 15:25:34 -070024 struct blk_desc *dev_desc;
Maxime Riparda58e9c72015-10-15 14:34:14 +020025};
26
Filip Brozovic859e6a82020-06-29 13:14:37 +020027static int raw_part_get_info_by_name(struct blk_desc *dev_desc,
Sean Anderson7ea25da2021-02-05 09:38:58 -050028 const char *name,
29 struct disk_partition *info)
Filip Brozovic859e6a82020-06-29 13:14:37 +020030{
31 /* strlen("fastboot_raw_partition_") + PART_NAME_LEN + 1 */
32 char env_desc_name[23 + PART_NAME_LEN + 1];
33 char *raw_part_desc;
34 const char *argv[2];
35 const char **parg = argv;
36
37 /* check for raw partition descriptor */
38 strcpy(env_desc_name, "fastboot_raw_partition_");
Matthias Schiffer04a5b0f2021-07-30 14:23:54 +020039 strlcat(env_desc_name, name, sizeof(env_desc_name));
Filip Brozovic859e6a82020-06-29 13:14:37 +020040 raw_part_desc = strdup(env_get(env_desc_name));
41 if (raw_part_desc == NULL)
42 return -ENODEV;
43
44 /*
45 * parse partition descriptor
46 *
47 * <lba_start> <lba_size> [mmcpart <num>]
48 */
49 for (; parg < argv + sizeof(argv) / sizeof(*argv); ++parg) {
50 *parg = strsep(&raw_part_desc, " ");
51 if (*parg == NULL) {
52 pr_err("Invalid number of arguments.\n");
53 return -ENODEV;
54 }
55 }
56
57 info->start = simple_strtoul(argv[0], NULL, 0);
58 info->size = simple_strtoul(argv[1], NULL, 0);
59 info->blksz = dev_desc->blksz;
Sean Andersoncb29fe82021-03-11 00:15:44 -050060 strlcpy((char *)info->name, name, PART_NAME_LEN);
Filip Brozovic859e6a82020-06-29 13:14:37 +020061
62 if (raw_part_desc) {
Sean Anderson7ea25da2021-02-05 09:38:58 -050063 if (strcmp(strsep(&raw_part_desc, " "), "mmcpart") == 0) {
64 ulong mmcpart = simple_strtoul(raw_part_desc, NULL, 0);
65 int ret = blk_dselect_hwpart(dev_desc, mmcpart);
66
67 if (ret)
68 return ret;
69 }
Filip Brozovic859e6a82020-06-29 13:14:37 +020070 }
71
72 return 0;
73}
74
Sean Andersonc4f2d642021-02-05 09:39:00 -050075static int do_get_part_info(struct blk_desc **dev_desc, const char *name,
76 struct disk_partition *info)
Sean Andersonc83e3842021-02-05 09:38:59 -050077{
78 int ret;
79
Sean Andersonc4f2d642021-02-05 09:39:00 -050080 /* First try partition names on the default device */
81 *dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
82 if (*dev_desc) {
83 ret = part_get_info_by_name(*dev_desc, name, info);
84 if (ret >= 0)
85 return ret;
86
87 /* Then try raw partitions */
88 ret = raw_part_get_info_by_name(*dev_desc, name, info);
89 if (ret >= 0)
90 return ret;
91 }
92
93 /* Then try dev.hwpart:part */
94 ret = part_get_info_by_dev_and_name_or_num("mmc", name, dev_desc,
95 info, true);
96 return ret;
97}
98
99static int part_get_info_by_name_or_alias(struct blk_desc **dev_desc,
100 const char *name,
101 struct disk_partition *info)
102{
Matthias Schiffer80478f42021-12-16 11:26:38 +0100103 /* strlen("fastboot_partition_alias_") + PART_NAME_LEN + 1 */
104 char env_alias_name[25 + PART_NAME_LEN + 1];
105 char *aliased_part_name;
Sean Andersonc4f2d642021-02-05 09:39:00 -0500106
Matthias Schiffer80478f42021-12-16 11:26:38 +0100107 /* check for alias */
108 strlcpy(env_alias_name, "fastboot_partition_alias_", sizeof(env_alias_name));
109 strlcat(env_alias_name, name, sizeof(env_alias_name));
110 aliased_part_name = env_get(env_alias_name);
111 if (aliased_part_name)
112 name = aliased_part_name;
Sean Andersonc83e3842021-02-05 09:38:59 -0500113
Matthias Schiffer80478f42021-12-16 11:26:38 +0100114 return do_get_part_info(dev_desc, name, info);
Sean Andersonc83e3842021-02-05 09:38:59 -0500115}
116
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000117/**
118 * fb_mmc_blk_write() - Write/erase MMC in chunks of FASTBOOT_MAX_BLK_WRITE
119 *
120 * @block_dev: Pointer to block device
121 * @start: First block to write/erase
122 * @blkcnt: Count of blocks
123 * @buffer: Pointer to data buffer for write or NULL for erase
124 */
125static lbaint_t fb_mmc_blk_write(struct blk_desc *block_dev, lbaint_t start,
126 lbaint_t blkcnt, const void *buffer)
127{
128 lbaint_t blk = start;
129 lbaint_t blks_written;
130 lbaint_t cur_blkcnt;
131 lbaint_t blks = 0;
132 int i;
133
134 for (i = 0; i < blkcnt; i += FASTBOOT_MAX_BLK_WRITE) {
135 cur_blkcnt = min((int)blkcnt - i, FASTBOOT_MAX_BLK_WRITE);
136 if (buffer) {
137 if (fastboot_progress_callback)
138 fastboot_progress_callback("writing");
139 blks_written = blk_dwrite(block_dev, blk, cur_blkcnt,
140 buffer + (i * block_dev->blksz));
141 } else {
142 if (fastboot_progress_callback)
143 fastboot_progress_callback("erasing");
144 blks_written = blk_derase(block_dev, blk, cur_blkcnt);
145 }
146 blk += blks_written;
147 blks += blks_written;
148 }
149 return blks;
150}
151
Steve Rae8cf58db2016-06-07 11:19:36 -0700152static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info,
153 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
Maxime Riparda58e9c72015-10-15 14:34:14 +0200154{
Steve Rae8cf58db2016-06-07 11:19:36 -0700155 struct fb_mmc_sparse *sparse = info->priv;
Simon Glasse3394752016-02-29 15:25:34 -0700156 struct blk_desc *dev_desc = sparse->dev_desc;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200157
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000158 return fb_mmc_blk_write(dev_desc, blk, blkcnt, buffer);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200159}
160
Steve Rae8a99be82016-06-07 11:19:38 -0700161static lbaint_t fb_mmc_sparse_reserve(struct sparse_storage *info,
162 lbaint_t blk, lbaint_t blkcnt)
163{
164 return blkcnt;
165}
166
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600167static void write_raw_image(struct blk_desc *dev_desc,
168 struct disk_partition *info, const char *part_name,
169 void *buffer, u32 download_bytes, char *response)
Steve Rae792ac482014-08-26 11:47:27 -0700170{
171 lbaint_t blkcnt;
172 lbaint_t blks;
173
174 /* determine number of blocks to write */
175 blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
Siarhei Siamashka58f30d82015-10-28 06:24:16 +0200176 blkcnt = lldiv(blkcnt, info->blksz);
Steve Rae792ac482014-08-26 11:47:27 -0700177
178 if (blkcnt > info->size) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900179 pr_err("too large for partition: '%s'\n", part_name);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000180 fastboot_fail("too large for partition", response);
Steve Rae792ac482014-08-26 11:47:27 -0700181 return;
182 }
183
184 puts("Flashing Raw Image\n");
185
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000186 blks = fb_mmc_blk_write(dev_desc, info->start, blkcnt, buffer);
187
Steve Rae792ac482014-08-26 11:47:27 -0700188 if (blks != blkcnt) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900189 pr_err("failed writing to device %d\n", dev_desc->devnum);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000190 fastboot_fail("failed writing to device", response);
Steve Rae792ac482014-08-26 11:47:27 -0700191 return;
192 }
193
194 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
195 part_name);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000196 fastboot_okay(NULL, response);
Steve Rae792ac482014-08-26 11:47:27 -0700197}
198
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100199#if defined(CONFIG_FASTBOOT_MMC_BOOT_SUPPORT) || \
Patrick Delaunay8b0a29a2021-01-27 14:46:46 +0100200 defined(CONFIG_FASTBOOT_MMC_USER_SUPPORT)
developer6a105562020-01-16 16:11:42 +0800201static int fb_mmc_erase_mmc_hwpart(struct blk_desc *dev_desc)
202{
203 lbaint_t blks;
204
205 debug("Start Erasing mmc hwpart[%u]...\n", dev_desc->hwpart);
206
207 blks = fb_mmc_blk_write(dev_desc, 0, dev_desc->lba, NULL);
208
209 if (blks != dev_desc->lba) {
210 pr_err("Failed to erase mmc hwpart[%u]\n", dev_desc->hwpart);
211 return 1;
212 }
213
214 printf("........ erased %lu bytes from mmc hwpart[%u]\n",
215 dev_desc->lba * dev_desc->blksz, dev_desc->hwpart);
216
217 return 0;
218}
Patrick Delaunay8b0a29a2021-01-27 14:46:46 +0100219#endif
developer6a105562020-01-16 16:11:42 +0800220
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100221#ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT
222static void fb_mmc_boot_ops(struct blk_desc *dev_desc, void *buffer,
223 int hwpart, u32 buff_sz, char *response)
developer6a105562020-01-16 16:11:42 +0800224{
225 lbaint_t blkcnt;
226 lbaint_t blks;
227 unsigned long blksz;
228
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100229 // To operate on EMMC_BOOT1/2 (mmc0boot0/1) we first change the hwpart
230 if (blk_dselect_hwpart(dev_desc, hwpart)) {
developer6a105562020-01-16 16:11:42 +0800231 pr_err("Failed to select hwpart\n");
232 fastboot_fail("Failed to select hwpart", response);
233 return;
234 }
235
236 if (buffer) { /* flash */
237
238 /* determine number of blocks to write */
239 blksz = dev_desc->blksz;
240 blkcnt = ((buff_sz + (blksz - 1)) & ~(blksz - 1));
241 blkcnt = lldiv(blkcnt, blksz);
242
243 if (blkcnt > dev_desc->lba) {
244 pr_err("Image size too large\n");
245 fastboot_fail("Image size too large", response);
246 return;
247 }
248
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100249 debug("Start Flashing Image to EMMC_BOOT%d...\n", hwpart);
developer6a105562020-01-16 16:11:42 +0800250
251 blks = fb_mmc_blk_write(dev_desc, 0, blkcnt, buffer);
252
253 if (blks != blkcnt) {
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100254 pr_err("Failed to write EMMC_BOOT%d\n", hwpart);
255 fastboot_fail("Failed to write EMMC_BOOT part",
256 response);
developer6a105562020-01-16 16:11:42 +0800257 return;
258 }
259
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100260 printf("........ wrote %lu bytes to EMMC_BOOT%d\n",
261 blkcnt * blksz, hwpart);
developer6a105562020-01-16 16:11:42 +0800262 } else { /* erase */
263 if (fb_mmc_erase_mmc_hwpart(dev_desc)) {
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100264 pr_err("Failed to erase EMMC_BOOT%d\n", hwpart);
265 fastboot_fail("Failed to erase EMMC_BOOT part",
266 response);
developer6a105562020-01-16 16:11:42 +0800267 return;
268 }
269 }
270
271 fastboot_okay(NULL, response);
272}
273#endif
274
Sam Protsenko540eb952017-05-18 15:04:59 +0300275#ifdef CONFIG_ANDROID_BOOT_IMAGE
276/**
277 * Read Android boot image header from boot partition.
278 *
279 * @param[in] dev_desc MMC device descriptor
280 * @param[in] info Boot partition info
281 * @param[out] hdr Where to store read boot image header
282 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100283 * Return: Boot image header sectors count or 0 on error
Sam Protsenko540eb952017-05-18 15:04:59 +0300284 */
285static lbaint_t fb_mmc_get_boot_header(struct blk_desc *dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600286 struct disk_partition *info,
Safae Ouajih8656e382023-02-06 00:50:03 +0100287 struct andr_boot_img_hdr_v0 *hdr,
Alex Kiernan27b69de2018-05-29 15:30:40 +0000288 char *response)
Sam Protsenko540eb952017-05-18 15:04:59 +0300289{
290 ulong sector_size; /* boot partition sector size */
291 lbaint_t hdr_sectors; /* boot image header sectors count */
292 int res;
293
294 /* Calculate boot image sectors count */
295 sector_size = info->blksz;
Safae Ouajih8656e382023-02-06 00:50:03 +0100296 hdr_sectors = DIV_ROUND_UP(sizeof(struct andr_boot_img_hdr_v0), sector_size);
Sam Protsenko540eb952017-05-18 15:04:59 +0300297 if (hdr_sectors == 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000298 pr_err("invalid number of boot sectors: 0\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000299 fastboot_fail("invalid number of boot sectors: 0", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300300 return 0;
301 }
302
303 /* Read the boot image header */
304 res = blk_dread(dev_desc, info->start, hdr_sectors, (void *)hdr);
Tom Rinibda6f772017-08-14 21:00:44 -0400305 if (res != hdr_sectors) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000306 pr_err("cannot read header from boot partition\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000307 fastboot_fail("cannot read header from boot partition",
308 response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300309 return 0;
310 }
311
312 /* Check boot header magic string */
Safae Ouajih88ad0c12023-02-06 00:50:05 +0100313 if (!is_android_boot_image_header(hdr)) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000314 pr_err("bad boot image magic\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000315 fastboot_fail("boot partition not initialized", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300316 return 0;
317 }
318
319 return hdr_sectors;
320}
321
322/**
323 * Write downloaded zImage to boot partition and repack it properly.
324 *
325 * @param dev_desc MMC device descriptor
326 * @param download_buffer Address to fastboot buffer with zImage in it
327 * @param download_bytes Size of fastboot buffer, in bytes
328 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100329 * Return: 0 on success or -1 on error
Sam Protsenko540eb952017-05-18 15:04:59 +0300330 */
331static int fb_mmc_update_zimage(struct blk_desc *dev_desc,
332 void *download_buffer,
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000333 u32 download_bytes,
Alex Kiernan27b69de2018-05-29 15:30:40 +0000334 char *response)
Sam Protsenko540eb952017-05-18 15:04:59 +0300335{
Tom Rini0ac605c2017-06-10 09:15:37 -0400336 uintptr_t hdr_addr; /* boot image header address */
Safae Ouajih8656e382023-02-06 00:50:03 +0100337 struct andr_boot_img_hdr_v0 *hdr; /* boot image header */
Sam Protsenko540eb952017-05-18 15:04:59 +0300338 lbaint_t hdr_sectors; /* boot image header sectors */
339 u8 *ramdisk_buffer;
340 u32 ramdisk_sector_start;
341 u32 ramdisk_sectors;
342 u32 kernel_sector_start;
343 u32 kernel_sectors;
344 u32 sectors_per_page;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600345 struct disk_partition info;
Sam Protsenko540eb952017-05-18 15:04:59 +0300346 int res;
347
348 puts("Flashing zImage\n");
349
350 /* Get boot partition info */
351 res = part_get_info_by_name(dev_desc, BOOT_PARTITION_NAME, &info);
352 if (res < 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000353 pr_err("cannot find boot partition\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000354 fastboot_fail("cannot find boot partition", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300355 return -1;
356 }
357
358 /* Put boot image header in fastboot buffer after downloaded zImage */
Tom Rini0ac605c2017-06-10 09:15:37 -0400359 hdr_addr = (uintptr_t)download_buffer + ALIGN(download_bytes, PAGE_SIZE);
Safae Ouajih8656e382023-02-06 00:50:03 +0100360 hdr = (struct andr_boot_img_hdr_v0 *)hdr_addr;
Sam Protsenko540eb952017-05-18 15:04:59 +0300361
362 /* Read boot image header */
Alex Kiernan27b69de2018-05-29 15:30:40 +0000363 hdr_sectors = fb_mmc_get_boot_header(dev_desc, &info, hdr, response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300364 if (hdr_sectors == 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000365 pr_err("unable to read boot image header\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000366 fastboot_fail("unable to read boot image header", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300367 return -1;
368 }
369
Safae Ouajih4ae6e432023-02-06 00:50:16 +0100370 /* Check if boot image header version is 2 or less */
371 if (hdr->header_version > 2) {
372 pr_err("zImage flashing supported only for boot images v2 and less\n");
373 fastboot_fail("zImage flashing supported only for boot images v2 and less",
374 response);
375 return -EOPNOTSUPP;
376 }
377
Sam Protsenko540eb952017-05-18 15:04:59 +0300378 /* Check if boot image has second stage in it (we don't support it) */
379 if (hdr->second_size > 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000380 pr_err("moving second stage is not supported yet\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000381 fastboot_fail("moving second stage is not supported yet",
382 response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300383 return -1;
384 }
385
386 /* Extract ramdisk location */
387 sectors_per_page = hdr->page_size / info.blksz;
388 ramdisk_sector_start = info.start + sectors_per_page;
389 ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
390 sectors_per_page;
391 ramdisk_sectors = DIV_ROUND_UP(hdr->ramdisk_size, hdr->page_size) *
392 sectors_per_page;
393
394 /* Read ramdisk and put it in fastboot buffer after boot image header */
395 ramdisk_buffer = (u8 *)hdr + (hdr_sectors * info.blksz);
396 res = blk_dread(dev_desc, ramdisk_sector_start, ramdisk_sectors,
397 ramdisk_buffer);
Tom Rinibda6f772017-08-14 21:00:44 -0400398 if (res != ramdisk_sectors) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000399 pr_err("cannot read ramdisk from boot partition\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000400 fastboot_fail("cannot read ramdisk from boot partition",
401 response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300402 return -1;
403 }
404
405 /* Write new kernel size to boot image header */
406 hdr->kernel_size = download_bytes;
407 res = blk_dwrite(dev_desc, info.start, hdr_sectors, (void *)hdr);
408 if (res == 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000409 pr_err("cannot writeback boot image header\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000410 fastboot_fail("cannot write back boot image header", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300411 return -1;
412 }
413
414 /* Write the new downloaded kernel */
415 kernel_sector_start = info.start + sectors_per_page;
416 kernel_sectors = DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
417 sectors_per_page;
418 res = blk_dwrite(dev_desc, kernel_sector_start, kernel_sectors,
419 download_buffer);
420 if (res == 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000421 pr_err("cannot write new kernel\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000422 fastboot_fail("cannot write new kernel", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300423 return -1;
424 }
425
426 /* Write the saved ramdisk back */
427 ramdisk_sector_start = info.start + sectors_per_page;
428 ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
429 sectors_per_page;
430 res = blk_dwrite(dev_desc, ramdisk_sector_start, ramdisk_sectors,
431 ramdisk_buffer);
432 if (res == 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000433 pr_err("cannot write back original ramdisk\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000434 fastboot_fail("cannot write back original ramdisk", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300435 return -1;
436 }
437
438 puts("........ zImage was updated in boot partition\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000439 fastboot_okay(NULL, response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300440 return 0;
441}
442#endif
443
Alex Kiernan32c21942018-05-29 15:30:48 +0000444/**
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000445 * fastboot_mmc_get_part_info() - Lookup eMMC partion by name
446 *
447 * @part_name: Named partition to lookup
448 * @dev_desc: Pointer to returned blk_desc pointer
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600449 * @part_info: Pointer to returned struct disk_partition
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000450 * @response: Pointer to fastboot response buffer
451 */
Sam Protsenko0fb37ee2019-06-13 21:11:07 +0300452int fastboot_mmc_get_part_info(const char *part_name,
453 struct blk_desc **dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600454 struct disk_partition *part_info, char *response)
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000455{
Sean Andersonc4f2d642021-02-05 09:39:00 -0500456 int ret;
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000457
Eugeniu Rosca8eba90b2019-03-28 14:31:33 +0100458 if (!part_name || !strcmp(part_name, "")) {
459 fastboot_fail("partition not given", response);
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000460 return -ENOENT;
461 }
462
Sean Andersonc4f2d642021-02-05 09:39:00 -0500463 ret = part_get_info_by_name_or_alias(dev_desc, part_name, part_info);
464 if (ret < 0) {
465 switch (ret) {
466 case -ENOSYS:
467 case -EINVAL:
468 fastboot_fail("invalid partition or device", response);
469 break;
470 case -ENODEV:
471 fastboot_fail("no such device", response);
472 break;
473 case -ENOENT:
474 fastboot_fail("no such partition", response);
475 break;
476 case -EPROTONOSUPPORT:
477 fastboot_fail("unknown partition table type", response);
478 break;
479 default:
480 fastboot_fail("unanticipated error", response);
481 break;
Filip Brozovic859e6a82020-06-29 13:14:37 +0200482 }
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000483 }
484
Sean Andersonc4f2d642021-02-05 09:39:00 -0500485 return ret;
486}
487
488static struct blk_desc *fastboot_mmc_get_dev(char *response)
489{
490 struct blk_desc *ret = blk_get_dev("mmc",
491 CONFIG_FASTBOOT_FLASH_MMC_DEV);
492
493 if (!ret || ret->type == DEV_TYPE_UNKNOWN) {
494 pr_err("invalid mmc device\n");
495 fastboot_fail("invalid mmc device", response);
496 return NULL;
497 }
498 return ret;
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000499}
500
501/**
Alex Kiernan32c21942018-05-29 15:30:48 +0000502 * fastboot_mmc_flash_write() - Write image to eMMC for fastboot
503 *
504 * @cmd: Named partition to write image to
505 * @download_buffer: Pointer to image data
506 * @download_bytes: Size of image data
507 * @response: Pointer to fastboot response buffer
508 */
509void fastboot_mmc_flash_write(const char *cmd, void *download_buffer,
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000510 u32 download_bytes, char *response)
Steve Rae792ac482014-08-26 11:47:27 -0700511{
Simon Glasse3394752016-02-29 15:25:34 -0700512 struct blk_desc *dev_desc;
Oleh Kravchenkoff6c0e32021-05-19 13:31:31 +0300513 struct disk_partition info = {0};
Steve Rae792ac482014-08-26 11:47:27 -0700514
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100515#ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT
developer6a105562020-01-16 16:11:42 +0800516 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) {
Sean Andersonc4f2d642021-02-05 09:39:00 -0500517 dev_desc = fastboot_mmc_get_dev(response);
518 if (dev_desc)
519 fb_mmc_boot_ops(dev_desc, download_buffer, 1,
520 download_bytes, response);
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100521 return;
522 }
523 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT2_NAME) == 0) {
Sean Andersonc4f2d642021-02-05 09:39:00 -0500524 dev_desc = fastboot_mmc_get_dev(response);
525 if (dev_desc)
Oleh Kravchenko8c73f852021-05-15 00:06:21 +0300526 fb_mmc_boot_ops(dev_desc, download_buffer, 2,
Sean Andersonc4f2d642021-02-05 09:39:00 -0500527 download_bytes, response);
developer6a105562020-01-16 16:11:42 +0800528 return;
529 }
530#endif
531
Patrick Delaunay8a4f2bd2017-01-27 11:00:41 +0100532#if CONFIG_IS_ENABLED(EFI_PARTITION)
Steve Rae7d059342014-12-12 15:51:54 -0800533 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
Sean Andersonc4f2d642021-02-05 09:39:00 -0500534 dev_desc = fastboot_mmc_get_dev(response);
535 if (!dev_desc)
536 return;
537
Steve Rae7d059342014-12-12 15:51:54 -0800538 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
539 __func__);
540 if (is_valid_gpt_buf(dev_desc, download_buffer)) {
541 printf("%s: invalid GPT - refusing to write to flash\n",
542 __func__);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000543 fastboot_fail("invalid GPT partition", response);
Steve Rae7d059342014-12-12 15:51:54 -0800544 return;
545 }
546 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
547 printf("%s: writing GPT partitions failed\n", __func__);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000548 fastboot_fail("writing GPT partitions failed",
549 response);
Steve Rae7d059342014-12-12 15:51:54 -0800550 return;
551 }
Roman Stratiienko330bf502021-01-27 17:40:16 +0200552 part_init(dev_desc);
Steve Rae7d059342014-12-12 15:51:54 -0800553 printf("........ success\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000554 fastboot_okay(NULL, response);
Steve Rae7d059342014-12-12 15:51:54 -0800555 return;
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200556 }
557#endif
558
Patrick Delaunayf7e07722017-01-27 11:00:37 +0100559#if CONFIG_IS_ENABLED(DOS_PARTITION)
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200560 if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) {
Sean Andersonc4f2d642021-02-05 09:39:00 -0500561 dev_desc = fastboot_mmc_get_dev(response);
562 if (!dev_desc)
563 return;
564
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200565 printf("%s: updating MBR\n", __func__);
566 if (is_valid_dos_buf(download_buffer)) {
567 printf("%s: invalid MBR - refusing to write to flash\n",
568 __func__);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000569 fastboot_fail("invalid MBR partition", response);
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200570 return;
571 }
Marek Szyprowski29ee06b2020-12-23 13:55:10 +0100572 if (write_mbr_sector(dev_desc, download_buffer)) {
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200573 printf("%s: writing MBR partition failed\n", __func__);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000574 fastboot_fail("writing MBR partition failed",
575 response);
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200576 return;
577 }
Roman Stratiienko330bf502021-01-27 17:40:16 +0200578 part_init(dev_desc);
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200579 printf("........ success\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000580 fastboot_okay(NULL, response);
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200581 return;
582 }
583#endif
584
Sam Protsenko540eb952017-05-18 15:04:59 +0300585#ifdef CONFIG_ANDROID_BOOT_IMAGE
586 if (strncasecmp(cmd, "zimage", 6) == 0) {
Sean Andersonc4f2d642021-02-05 09:39:00 -0500587 dev_desc = fastboot_mmc_get_dev(response);
588 if (dev_desc)
589 fb_mmc_update_zimage(dev_desc, download_buffer,
590 download_bytes, response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300591 return;
592 }
593#endif
594
Simon Glass8b0d42c2023-02-05 15:39:56 -0700595#if IS_ENABLED(CONFIG_FASTBOOT_MMC_USER_SUPPORT)
Oleh Kravchenkoff6c0e32021-05-19 13:31:31 +0300596 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) {
597 dev_desc = fastboot_mmc_get_dev(response);
598 if (!dev_desc)
599 return;
600
601 strlcpy((char *)&info.name, cmd, sizeof(info.name));
602 info.size = dev_desc->lba;
603 info.blksz = dev_desc->blksz;
604 }
605#endif
606
607 if (!info.name[0] &&
608 fastboot_mmc_get_part_info(cmd, &dev_desc, &info, response) < 0)
Sean Andersonc4f2d642021-02-05 09:39:00 -0500609 return;
Steve Rae792ac482014-08-26 11:47:27 -0700610
Maxime Riparda58e9c72015-10-15 14:34:14 +0200611 if (is_sparse_image(download_buffer)) {
612 struct fb_mmc_sparse sparse_priv;
Steve Rae8cf58db2016-06-07 11:19:36 -0700613 struct sparse_storage sparse;
Jassi Brar427e6692018-04-06 12:05:09 +0530614 int err;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200615
616 sparse_priv.dev_desc = dev_desc;
617
Steve Rae8cf58db2016-06-07 11:19:36 -0700618 sparse.blksz = info.blksz;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200619 sparse.start = info.start;
620 sparse.size = info.size;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200621 sparse.write = fb_mmc_sparse_write;
Steve Rae8a99be82016-06-07 11:19:38 -0700622 sparse.reserve = fb_mmc_sparse_reserve;
Jassi Brar427e6692018-04-06 12:05:09 +0530623 sparse.mssg = fastboot_fail;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200624
625 printf("Flashing sparse image at offset " LBAFU "\n",
Steve Rae8cf58db2016-06-07 11:19:36 -0700626 sparse.start);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200627
Steve Rae8cf58db2016-06-07 11:19:36 -0700628 sparse.priv = &sparse_priv;
Alex Kiernan27b69de2018-05-29 15:30:40 +0000629 err = write_sparse_image(&sparse, cmd, download_buffer,
630 response);
Jassi Brar427e6692018-04-06 12:05:09 +0530631 if (!err)
Alex Kiernan27b69de2018-05-29 15:30:40 +0000632 fastboot_okay(NULL, response);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200633 } else {
Steve Raec6015672014-08-26 11:47:30 -0700634 write_raw_image(dev_desc, &info, cmd, download_buffer,
Alex Kiernan27b69de2018-05-29 15:30:40 +0000635 download_bytes, response);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200636 }
Steve Rae792ac482014-08-26 11:47:27 -0700637}
Dileep Kattab1228202015-02-17 18:48:23 +0530638
Alex Kiernan32c21942018-05-29 15:30:48 +0000639/**
640 * fastboot_mmc_flash_erase() - Erase eMMC for fastboot
641 *
642 * @cmd: Named partition to erase
643 * @response: Pointer to fastboot response buffer
644 */
645void fastboot_mmc_erase(const char *cmd, char *response)
Dileep Kattab1228202015-02-17 18:48:23 +0530646{
Simon Glasse3394752016-02-29 15:25:34 -0700647 struct blk_desc *dev_desc;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600648 struct disk_partition info;
Dileep Kattab1228202015-02-17 18:48:23 +0530649 lbaint_t blks, blks_start, blks_size, grp_size;
650 struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
651
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100652#ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT
developer6a105562020-01-16 16:11:42 +0800653 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) {
654 /* erase EMMC boot1 */
Sean Andersonc4f2d642021-02-05 09:39:00 -0500655 dev_desc = fastboot_mmc_get_dev(response);
656 if (dev_desc)
657 fb_mmc_boot_ops(dev_desc, NULL, 1, 0, response);
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100658 return;
659 }
660 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT2_NAME) == 0) {
661 /* erase EMMC boot2 */
Sean Andersonc4f2d642021-02-05 09:39:00 -0500662 dev_desc = fastboot_mmc_get_dev(response);
663 if (dev_desc)
Oleh Kravchenko8c73f852021-05-15 00:06:21 +0300664 fb_mmc_boot_ops(dev_desc, NULL, 2, 0, response);
developer6a105562020-01-16 16:11:42 +0800665 return;
666 }
667#endif
668
Patrick Delaunay8b0a29a2021-01-27 14:46:46 +0100669#ifdef CONFIG_FASTBOOT_MMC_USER_SUPPORT
developer6a105562020-01-16 16:11:42 +0800670 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) {
671 /* erase EMMC userdata */
Sean Andersonc4f2d642021-02-05 09:39:00 -0500672 dev_desc = fastboot_mmc_get_dev(response);
673 if (!dev_desc)
674 return;
675
developer6a105562020-01-16 16:11:42 +0800676 if (fb_mmc_erase_mmc_hwpart(dev_desc))
677 fastboot_fail("Failed to erase EMMC_USER", response);
678 else
679 fastboot_okay(NULL, response);
680 return;
681 }
682#endif
Dileep Kattab1228202015-02-17 18:48:23 +0530683
Sean Andersonc4f2d642021-02-05 09:39:00 -0500684 if (fastboot_mmc_get_part_info(cmd, &dev_desc, &info, response) < 0)
685 return;
Dileep Kattab1228202015-02-17 18:48:23 +0530686
687 /* Align blocks to erase group size to avoid erasing other partitions */
688 grp_size = mmc->erase_grp_size;
689 blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
690 if (info.size >= grp_size)
691 blks_size = (info.size - (blks_start - info.start)) &
692 (~(grp_size - 1));
693 else
694 blks_size = 0;
695
696 printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
697 blks_start, blks_start + blks_size);
698
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000699 blks = fb_mmc_blk_write(dev_desc, blks_start, blks_size, NULL);
700
Dileep Kattab1228202015-02-17 18:48:23 +0530701 if (blks != blks_size) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000702 pr_err("failed erasing from device %d\n", dev_desc->devnum);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000703 fastboot_fail("failed erasing from device", response);
Dileep Kattab1228202015-02-17 18:48:23 +0530704 return;
705 }
706
707 printf("........ erased " LBAFU " bytes from '%s'\n",
708 blks_size * info.blksz, cmd);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000709 fastboot_okay(NULL, response);
Dileep Kattab1228202015-02-17 18:48:23 +0530710}