blob: 71eeb02c8f19259a3c98a10d30aa917630c8d6ff [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Steve Rae792ac482014-08-26 11:47:27 -07002/*
3 * Copyright 2014 Broadcom Corporation.
Steve Rae792ac482014-08-26 11:47:27 -07004 */
5
Steve Rae7d059342014-12-12 15:51:54 -08006#include <config.h>
Steve Rae792ac482014-08-26 11:47:27 -07007#include <common.h>
Simon Glass2ee8ada2016-02-29 15:25:52 -07008#include <blk.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -06009#include <env.h>
Maxime Ripard00e8eb72015-10-15 14:34:13 +020010#include <fastboot.h>
Alex Kiernand5aa57c2018-05-29 15:30:53 +000011#include <fastboot-internal.h>
Steve Rae792ac482014-08-26 11:47:27 -070012#include <fb_mmc.h>
Simon Glass8e201882020-05-10 11:39:54 -060013#include <flash.h>
Maxime Ripard6af49bc2015-10-15 14:34:19 +020014#include <image-sparse.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060015#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Steve Rae792ac482014-08-26 11:47:27 -070017#include <part.h>
Dileep Kattab1228202015-02-17 18:48:23 +053018#include <mmc.h>
Siarhei Siamashka58f30d82015-10-28 06:24:16 +020019#include <div64.h>
Sam Protsenko540eb952017-05-18 15:04:59 +030020#include <linux/compat.h>
21#include <android_image.h>
Steve Rae792ac482014-08-26 11:47:27 -070022
Alex Kiernand5aa57c2018-05-29 15:30:53 +000023#define FASTBOOT_MAX_BLK_WRITE 16384
24
Sam Protsenko540eb952017-05-18 15:04:59 +030025#define BOOT_PARTITION_NAME "boot"
26
Maxime Riparda58e9c72015-10-15 14:34:14 +020027struct fb_mmc_sparse {
Simon Glasse3394752016-02-29 15:25:34 -070028 struct blk_desc *dev_desc;
Maxime Riparda58e9c72015-10-15 14:34:14 +020029};
30
Filip Brozovic859e6a82020-06-29 13:14:37 +020031static int raw_part_get_info_by_name(struct blk_desc *dev_desc,
Sean Anderson7ea25da2021-02-05 09:38:58 -050032 const char *name,
33 struct disk_partition *info)
Filip Brozovic859e6a82020-06-29 13:14:37 +020034{
35 /* strlen("fastboot_raw_partition_") + PART_NAME_LEN + 1 */
36 char env_desc_name[23 + PART_NAME_LEN + 1];
37 char *raw_part_desc;
38 const char *argv[2];
39 const char **parg = argv;
40
41 /* check for raw partition descriptor */
42 strcpy(env_desc_name, "fastboot_raw_partition_");
43 strncat(env_desc_name, name, PART_NAME_LEN);
44 raw_part_desc = strdup(env_get(env_desc_name));
45 if (raw_part_desc == NULL)
46 return -ENODEV;
47
48 /*
49 * parse partition descriptor
50 *
51 * <lba_start> <lba_size> [mmcpart <num>]
52 */
53 for (; parg < argv + sizeof(argv) / sizeof(*argv); ++parg) {
54 *parg = strsep(&raw_part_desc, " ");
55 if (*parg == NULL) {
56 pr_err("Invalid number of arguments.\n");
57 return -ENODEV;
58 }
59 }
60
61 info->start = simple_strtoul(argv[0], NULL, 0);
62 info->size = simple_strtoul(argv[1], NULL, 0);
63 info->blksz = dev_desc->blksz;
64 strncpy((char *)info->name, name, PART_NAME_LEN);
65
66 if (raw_part_desc) {
Sean Anderson7ea25da2021-02-05 09:38:58 -050067 if (strcmp(strsep(&raw_part_desc, " "), "mmcpart") == 0) {
68 ulong mmcpart = simple_strtoul(raw_part_desc, NULL, 0);
69 int ret = blk_dselect_hwpart(dev_desc, mmcpart);
70
71 if (ret)
72 return ret;
73 }
Filip Brozovic859e6a82020-06-29 13:14:37 +020074 }
75
76 return 0;
77}
78
Sean Andersonc83e3842021-02-05 09:38:59 -050079static int part_get_info_by_name_or_alias(struct blk_desc *dev_desc,
80 const char *name, struct disk_partition *info)
81{
82 int ret;
83
84 ret = part_get_info_by_name(dev_desc, name, info);
85 if (ret < 0) {
86 /* strlen("fastboot_partition_alias_") + PART_NAME_LEN + 1 */
87 char env_alias_name[25 + PART_NAME_LEN + 1];
88 char *aliased_part_name;
89
90 /* check for alias */
91 strcpy(env_alias_name, "fastboot_partition_alias_");
92 strncat(env_alias_name, name, PART_NAME_LEN);
93 aliased_part_name = env_get(env_alias_name);
94 if (aliased_part_name != NULL)
95 ret = part_get_info_by_name(dev_desc,
96 aliased_part_name, info);
97 }
98 return ret;
99}
100
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000101/**
102 * fb_mmc_blk_write() - Write/erase MMC in chunks of FASTBOOT_MAX_BLK_WRITE
103 *
104 * @block_dev: Pointer to block device
105 * @start: First block to write/erase
106 * @blkcnt: Count of blocks
107 * @buffer: Pointer to data buffer for write or NULL for erase
108 */
109static lbaint_t fb_mmc_blk_write(struct blk_desc *block_dev, lbaint_t start,
110 lbaint_t blkcnt, const void *buffer)
111{
112 lbaint_t blk = start;
113 lbaint_t blks_written;
114 lbaint_t cur_blkcnt;
115 lbaint_t blks = 0;
116 int i;
117
118 for (i = 0; i < blkcnt; i += FASTBOOT_MAX_BLK_WRITE) {
119 cur_blkcnt = min((int)blkcnt - i, FASTBOOT_MAX_BLK_WRITE);
120 if (buffer) {
121 if (fastboot_progress_callback)
122 fastboot_progress_callback("writing");
123 blks_written = blk_dwrite(block_dev, blk, cur_blkcnt,
124 buffer + (i * block_dev->blksz));
125 } else {
126 if (fastboot_progress_callback)
127 fastboot_progress_callback("erasing");
128 blks_written = blk_derase(block_dev, blk, cur_blkcnt);
129 }
130 blk += blks_written;
131 blks += blks_written;
132 }
133 return blks;
134}
135
Steve Rae8cf58db2016-06-07 11:19:36 -0700136static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info,
137 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
Maxime Riparda58e9c72015-10-15 14:34:14 +0200138{
Steve Rae8cf58db2016-06-07 11:19:36 -0700139 struct fb_mmc_sparse *sparse = info->priv;
Simon Glasse3394752016-02-29 15:25:34 -0700140 struct blk_desc *dev_desc = sparse->dev_desc;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200141
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000142 return fb_mmc_blk_write(dev_desc, blk, blkcnt, buffer);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200143}
144
Steve Rae8a99be82016-06-07 11:19:38 -0700145static lbaint_t fb_mmc_sparse_reserve(struct sparse_storage *info,
146 lbaint_t blk, lbaint_t blkcnt)
147{
148 return blkcnt;
149}
150
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600151static void write_raw_image(struct blk_desc *dev_desc,
152 struct disk_partition *info, const char *part_name,
153 void *buffer, u32 download_bytes, char *response)
Steve Rae792ac482014-08-26 11:47:27 -0700154{
155 lbaint_t blkcnt;
156 lbaint_t blks;
157
158 /* determine number of blocks to write */
159 blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
Siarhei Siamashka58f30d82015-10-28 06:24:16 +0200160 blkcnt = lldiv(blkcnt, info->blksz);
Steve Rae792ac482014-08-26 11:47:27 -0700161
162 if (blkcnt > info->size) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900163 pr_err("too large for partition: '%s'\n", part_name);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000164 fastboot_fail("too large for partition", response);
Steve Rae792ac482014-08-26 11:47:27 -0700165 return;
166 }
167
168 puts("Flashing Raw Image\n");
169
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000170 blks = fb_mmc_blk_write(dev_desc, info->start, blkcnt, buffer);
171
Steve Rae792ac482014-08-26 11:47:27 -0700172 if (blks != blkcnt) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900173 pr_err("failed writing to device %d\n", dev_desc->devnum);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000174 fastboot_fail("failed writing to device", response);
Steve Rae792ac482014-08-26 11:47:27 -0700175 return;
176 }
177
178 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
179 part_name);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000180 fastboot_okay(NULL, response);
Steve Rae792ac482014-08-26 11:47:27 -0700181}
182
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100183#if defined(CONFIG_FASTBOOT_MMC_BOOT_SUPPORT) || \
Patrick Delaunay8b0a29a2021-01-27 14:46:46 +0100184 defined(CONFIG_FASTBOOT_MMC_USER_SUPPORT)
developer6a105562020-01-16 16:11:42 +0800185static int fb_mmc_erase_mmc_hwpart(struct blk_desc *dev_desc)
186{
187 lbaint_t blks;
188
189 debug("Start Erasing mmc hwpart[%u]...\n", dev_desc->hwpart);
190
191 blks = fb_mmc_blk_write(dev_desc, 0, dev_desc->lba, NULL);
192
193 if (blks != dev_desc->lba) {
194 pr_err("Failed to erase mmc hwpart[%u]\n", dev_desc->hwpart);
195 return 1;
196 }
197
198 printf("........ erased %lu bytes from mmc hwpart[%u]\n",
199 dev_desc->lba * dev_desc->blksz, dev_desc->hwpart);
200
201 return 0;
202}
Patrick Delaunay8b0a29a2021-01-27 14:46:46 +0100203#endif
developer6a105562020-01-16 16:11:42 +0800204
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100205#ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT
206static void fb_mmc_boot_ops(struct blk_desc *dev_desc, void *buffer,
207 int hwpart, u32 buff_sz, char *response)
developer6a105562020-01-16 16:11:42 +0800208{
209 lbaint_t blkcnt;
210 lbaint_t blks;
211 unsigned long blksz;
212
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100213 // To operate on EMMC_BOOT1/2 (mmc0boot0/1) we first change the hwpart
214 if (blk_dselect_hwpart(dev_desc, hwpart)) {
developer6a105562020-01-16 16:11:42 +0800215 pr_err("Failed to select hwpart\n");
216 fastboot_fail("Failed to select hwpart", response);
217 return;
218 }
219
220 if (buffer) { /* flash */
221
222 /* determine number of blocks to write */
223 blksz = dev_desc->blksz;
224 blkcnt = ((buff_sz + (blksz - 1)) & ~(blksz - 1));
225 blkcnt = lldiv(blkcnt, blksz);
226
227 if (blkcnt > dev_desc->lba) {
228 pr_err("Image size too large\n");
229 fastboot_fail("Image size too large", response);
230 return;
231 }
232
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100233 debug("Start Flashing Image to EMMC_BOOT%d...\n", hwpart);
developer6a105562020-01-16 16:11:42 +0800234
235 blks = fb_mmc_blk_write(dev_desc, 0, blkcnt, buffer);
236
237 if (blks != blkcnt) {
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100238 pr_err("Failed to write EMMC_BOOT%d\n", hwpart);
239 fastboot_fail("Failed to write EMMC_BOOT part",
240 response);
developer6a105562020-01-16 16:11:42 +0800241 return;
242 }
243
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100244 printf("........ wrote %lu bytes to EMMC_BOOT%d\n",
245 blkcnt * blksz, hwpart);
developer6a105562020-01-16 16:11:42 +0800246 } else { /* erase */
247 if (fb_mmc_erase_mmc_hwpart(dev_desc)) {
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100248 pr_err("Failed to erase EMMC_BOOT%d\n", hwpart);
249 fastboot_fail("Failed to erase EMMC_BOOT part",
250 response);
developer6a105562020-01-16 16:11:42 +0800251 return;
252 }
253 }
254
255 fastboot_okay(NULL, response);
256}
257#endif
258
Sam Protsenko540eb952017-05-18 15:04:59 +0300259#ifdef CONFIG_ANDROID_BOOT_IMAGE
260/**
261 * Read Android boot image header from boot partition.
262 *
263 * @param[in] dev_desc MMC device descriptor
264 * @param[in] info Boot partition info
265 * @param[out] hdr Where to store read boot image header
266 *
267 * @return Boot image header sectors count or 0 on error
268 */
269static lbaint_t fb_mmc_get_boot_header(struct blk_desc *dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600270 struct disk_partition *info,
Alex Kiernan27b69de2018-05-29 15:30:40 +0000271 struct andr_img_hdr *hdr,
272 char *response)
Sam Protsenko540eb952017-05-18 15:04:59 +0300273{
274 ulong sector_size; /* boot partition sector size */
275 lbaint_t hdr_sectors; /* boot image header sectors count */
276 int res;
277
278 /* Calculate boot image sectors count */
279 sector_size = info->blksz;
280 hdr_sectors = DIV_ROUND_UP(sizeof(struct andr_img_hdr), sector_size);
281 if (hdr_sectors == 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000282 pr_err("invalid number of boot sectors: 0\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000283 fastboot_fail("invalid number of boot sectors: 0", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300284 return 0;
285 }
286
287 /* Read the boot image header */
288 res = blk_dread(dev_desc, info->start, hdr_sectors, (void *)hdr);
Tom Rinibda6f772017-08-14 21:00:44 -0400289 if (res != hdr_sectors) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000290 pr_err("cannot read header from boot partition\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000291 fastboot_fail("cannot read header from boot partition",
292 response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300293 return 0;
294 }
295
296 /* Check boot header magic string */
297 res = android_image_check_header(hdr);
298 if (res != 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000299 pr_err("bad boot image magic\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000300 fastboot_fail("boot partition not initialized", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300301 return 0;
302 }
303
304 return hdr_sectors;
305}
306
307/**
308 * Write downloaded zImage to boot partition and repack it properly.
309 *
310 * @param dev_desc MMC device descriptor
311 * @param download_buffer Address to fastboot buffer with zImage in it
312 * @param download_bytes Size of fastboot buffer, in bytes
313 *
314 * @return 0 on success or -1 on error
315 */
316static int fb_mmc_update_zimage(struct blk_desc *dev_desc,
317 void *download_buffer,
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000318 u32 download_bytes,
Alex Kiernan27b69de2018-05-29 15:30:40 +0000319 char *response)
Sam Protsenko540eb952017-05-18 15:04:59 +0300320{
Tom Rini0ac605c2017-06-10 09:15:37 -0400321 uintptr_t hdr_addr; /* boot image header address */
Sam Protsenko540eb952017-05-18 15:04:59 +0300322 struct andr_img_hdr *hdr; /* boot image header */
323 lbaint_t hdr_sectors; /* boot image header sectors */
324 u8 *ramdisk_buffer;
325 u32 ramdisk_sector_start;
326 u32 ramdisk_sectors;
327 u32 kernel_sector_start;
328 u32 kernel_sectors;
329 u32 sectors_per_page;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600330 struct disk_partition info;
Sam Protsenko540eb952017-05-18 15:04:59 +0300331 int res;
332
333 puts("Flashing zImage\n");
334
335 /* Get boot partition info */
336 res = part_get_info_by_name(dev_desc, BOOT_PARTITION_NAME, &info);
337 if (res < 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000338 pr_err("cannot find boot partition\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000339 fastboot_fail("cannot find boot partition", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300340 return -1;
341 }
342
343 /* Put boot image header in fastboot buffer after downloaded zImage */
Tom Rini0ac605c2017-06-10 09:15:37 -0400344 hdr_addr = (uintptr_t)download_buffer + ALIGN(download_bytes, PAGE_SIZE);
Sam Protsenko540eb952017-05-18 15:04:59 +0300345 hdr = (struct andr_img_hdr *)hdr_addr;
346
347 /* Read boot image header */
Alex Kiernan27b69de2018-05-29 15:30:40 +0000348 hdr_sectors = fb_mmc_get_boot_header(dev_desc, &info, hdr, response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300349 if (hdr_sectors == 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000350 pr_err("unable to read boot image header\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000351 fastboot_fail("unable to read boot image header", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300352 return -1;
353 }
354
355 /* Check if boot image has second stage in it (we don't support it) */
356 if (hdr->second_size > 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000357 pr_err("moving second stage is not supported yet\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000358 fastboot_fail("moving second stage is not supported yet",
359 response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300360 return -1;
361 }
362
363 /* Extract ramdisk location */
364 sectors_per_page = hdr->page_size / info.blksz;
365 ramdisk_sector_start = info.start + sectors_per_page;
366 ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
367 sectors_per_page;
368 ramdisk_sectors = DIV_ROUND_UP(hdr->ramdisk_size, hdr->page_size) *
369 sectors_per_page;
370
371 /* Read ramdisk and put it in fastboot buffer after boot image header */
372 ramdisk_buffer = (u8 *)hdr + (hdr_sectors * info.blksz);
373 res = blk_dread(dev_desc, ramdisk_sector_start, ramdisk_sectors,
374 ramdisk_buffer);
Tom Rinibda6f772017-08-14 21:00:44 -0400375 if (res != ramdisk_sectors) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000376 pr_err("cannot read ramdisk from boot partition\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000377 fastboot_fail("cannot read ramdisk from boot partition",
378 response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300379 return -1;
380 }
381
382 /* Write new kernel size to boot image header */
383 hdr->kernel_size = download_bytes;
384 res = blk_dwrite(dev_desc, info.start, hdr_sectors, (void *)hdr);
385 if (res == 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000386 pr_err("cannot writeback boot image header\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000387 fastboot_fail("cannot write back boot image header", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300388 return -1;
389 }
390
391 /* Write the new downloaded kernel */
392 kernel_sector_start = info.start + sectors_per_page;
393 kernel_sectors = DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
394 sectors_per_page;
395 res = blk_dwrite(dev_desc, kernel_sector_start, kernel_sectors,
396 download_buffer);
397 if (res == 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000398 pr_err("cannot write new kernel\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000399 fastboot_fail("cannot write new kernel", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300400 return -1;
401 }
402
403 /* Write the saved ramdisk back */
404 ramdisk_sector_start = info.start + sectors_per_page;
405 ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
406 sectors_per_page;
407 res = blk_dwrite(dev_desc, ramdisk_sector_start, ramdisk_sectors,
408 ramdisk_buffer);
409 if (res == 0) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000410 pr_err("cannot write back original ramdisk\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000411 fastboot_fail("cannot write back original ramdisk", response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300412 return -1;
413 }
414
415 puts("........ zImage was updated in boot partition\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000416 fastboot_okay(NULL, response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300417 return 0;
418}
419#endif
420
Alex Kiernan32c21942018-05-29 15:30:48 +0000421/**
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000422 * fastboot_mmc_get_part_info() - Lookup eMMC partion by name
423 *
424 * @part_name: Named partition to lookup
425 * @dev_desc: Pointer to returned blk_desc pointer
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600426 * @part_info: Pointer to returned struct disk_partition
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000427 * @response: Pointer to fastboot response buffer
428 */
Sam Protsenko0fb37ee2019-06-13 21:11:07 +0300429int fastboot_mmc_get_part_info(const char *part_name,
430 struct blk_desc **dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600431 struct disk_partition *part_info, char *response)
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000432{
Filip Brozovic859e6a82020-06-29 13:14:37 +0200433 int r = 0;
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000434
435 *dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
436 if (!*dev_desc) {
437 fastboot_fail("block device not found", response);
438 return -ENOENT;
439 }
Eugeniu Rosca8eba90b2019-03-28 14:31:33 +0100440 if (!part_name || !strcmp(part_name, "")) {
441 fastboot_fail("partition not given", response);
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000442 return -ENOENT;
443 }
444
Sean Anderson7ea25da2021-02-05 09:38:58 -0500445 if (raw_part_get_info_by_name(*dev_desc, part_name, part_info) < 0) {
Filip Brozovic859e6a82020-06-29 13:14:37 +0200446 r = part_get_info_by_name_or_alias(*dev_desc, part_name, part_info);
447 if (r < 0) {
448 fastboot_fail("partition not found", response);
449 return r;
450 }
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000451 }
452
453 return r;
454}
455
456/**
Alex Kiernan32c21942018-05-29 15:30:48 +0000457 * fastboot_mmc_flash_write() - Write image to eMMC for fastboot
458 *
459 * @cmd: Named partition to write image to
460 * @download_buffer: Pointer to image data
461 * @download_bytes: Size of image data
462 * @response: Pointer to fastboot response buffer
463 */
464void fastboot_mmc_flash_write(const char *cmd, void *download_buffer,
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000465 u32 download_bytes, char *response)
Steve Rae792ac482014-08-26 11:47:27 -0700466{
Simon Glasse3394752016-02-29 15:25:34 -0700467 struct blk_desc *dev_desc;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600468 struct disk_partition info;
Steve Rae792ac482014-08-26 11:47:27 -0700469
Simon Glassbe1e9bb2016-02-29 15:25:42 -0700470 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
Steve Rae792ac482014-08-26 11:47:27 -0700471 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900472 pr_err("invalid mmc device\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000473 fastboot_fail("invalid mmc device", response);
Steve Rae792ac482014-08-26 11:47:27 -0700474 return;
475 }
476
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100477#ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT
developer6a105562020-01-16 16:11:42 +0800478 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) {
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100479 fb_mmc_boot_ops(dev_desc, download_buffer, 1,
480 download_bytes, response);
481 return;
482 }
483 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT2_NAME) == 0) {
484 fb_mmc_boot_ops(dev_desc, download_buffer, 2,
485 download_bytes, response);
developer6a105562020-01-16 16:11:42 +0800486 return;
487 }
488#endif
489
Patrick Delaunay8a4f2bd2017-01-27 11:00:41 +0100490#if CONFIG_IS_ENABLED(EFI_PARTITION)
Patrick Delaunay8b0a29a2021-01-27 14:46:46 +0100491#ifndef CONFIG_FASTBOOT_MMC_USER_SUPPORT
Steve Rae7d059342014-12-12 15:51:54 -0800492 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
developer6a105562020-01-16 16:11:42 +0800493#else
494 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0 ||
495 strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) {
496#endif
Steve Rae7d059342014-12-12 15:51:54 -0800497 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
498 __func__);
499 if (is_valid_gpt_buf(dev_desc, download_buffer)) {
500 printf("%s: invalid GPT - refusing to write to flash\n",
501 __func__);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000502 fastboot_fail("invalid GPT partition", response);
Steve Rae7d059342014-12-12 15:51:54 -0800503 return;
504 }
505 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
506 printf("%s: writing GPT partitions failed\n", __func__);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000507 fastboot_fail("writing GPT partitions failed",
508 response);
Steve Rae7d059342014-12-12 15:51:54 -0800509 return;
510 }
Roman Stratiienko330bf502021-01-27 17:40:16 +0200511 part_init(dev_desc);
Steve Rae7d059342014-12-12 15:51:54 -0800512 printf("........ success\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000513 fastboot_okay(NULL, response);
Steve Rae7d059342014-12-12 15:51:54 -0800514 return;
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200515 }
516#endif
517
Patrick Delaunayf7e07722017-01-27 11:00:37 +0100518#if CONFIG_IS_ENABLED(DOS_PARTITION)
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200519 if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) {
520 printf("%s: updating MBR\n", __func__);
521 if (is_valid_dos_buf(download_buffer)) {
522 printf("%s: invalid MBR - refusing to write to flash\n",
523 __func__);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000524 fastboot_fail("invalid MBR partition", response);
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200525 return;
526 }
Marek Szyprowski29ee06b2020-12-23 13:55:10 +0100527 if (write_mbr_sector(dev_desc, download_buffer)) {
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200528 printf("%s: writing MBR partition failed\n", __func__);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000529 fastboot_fail("writing MBR partition failed",
530 response);
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200531 return;
532 }
Roman Stratiienko330bf502021-01-27 17:40:16 +0200533 part_init(dev_desc);
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200534 printf("........ success\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000535 fastboot_okay(NULL, response);
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200536 return;
537 }
538#endif
539
Sam Protsenko540eb952017-05-18 15:04:59 +0300540#ifdef CONFIG_ANDROID_BOOT_IMAGE
541 if (strncasecmp(cmd, "zimage", 6) == 0) {
Alex Kiernan27b69de2018-05-29 15:30:40 +0000542 fb_mmc_update_zimage(dev_desc, download_buffer,
543 download_bytes, response);
Sam Protsenko540eb952017-05-18 15:04:59 +0300544 return;
545 }
546#endif
547
Sean Anderson7ea25da2021-02-05 09:38:58 -0500548 if (raw_part_get_info_by_name(dev_desc, cmd, &info) != 0) {
549 if (part_get_info_by_name_or_alias(dev_desc, cmd, &info) < 0) {
550 pr_err("cannot find partition: '%s'\n", cmd);
551 fastboot_fail("cannot find partition", response);
Filip Brozovic859e6a82020-06-29 13:14:37 +0200552 return;
553 }
Steve Rae792ac482014-08-26 11:47:27 -0700554 }
555
Maxime Riparda58e9c72015-10-15 14:34:14 +0200556 if (is_sparse_image(download_buffer)) {
557 struct fb_mmc_sparse sparse_priv;
Steve Rae8cf58db2016-06-07 11:19:36 -0700558 struct sparse_storage sparse;
Jassi Brar427e6692018-04-06 12:05:09 +0530559 int err;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200560
561 sparse_priv.dev_desc = dev_desc;
562
Steve Rae8cf58db2016-06-07 11:19:36 -0700563 sparse.blksz = info.blksz;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200564 sparse.start = info.start;
565 sparse.size = info.size;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200566 sparse.write = fb_mmc_sparse_write;
Steve Rae8a99be82016-06-07 11:19:38 -0700567 sparse.reserve = fb_mmc_sparse_reserve;
Jassi Brar427e6692018-04-06 12:05:09 +0530568 sparse.mssg = fastboot_fail;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200569
570 printf("Flashing sparse image at offset " LBAFU "\n",
Steve Rae8cf58db2016-06-07 11:19:36 -0700571 sparse.start);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200572
Steve Rae8cf58db2016-06-07 11:19:36 -0700573 sparse.priv = &sparse_priv;
Alex Kiernan27b69de2018-05-29 15:30:40 +0000574 err = write_sparse_image(&sparse, cmd, download_buffer,
575 response);
Jassi Brar427e6692018-04-06 12:05:09 +0530576 if (!err)
Alex Kiernan27b69de2018-05-29 15:30:40 +0000577 fastboot_okay(NULL, response);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200578 } else {
Steve Raec6015672014-08-26 11:47:30 -0700579 write_raw_image(dev_desc, &info, cmd, download_buffer,
Alex Kiernan27b69de2018-05-29 15:30:40 +0000580 download_bytes, response);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200581 }
Steve Rae792ac482014-08-26 11:47:27 -0700582}
Dileep Kattab1228202015-02-17 18:48:23 +0530583
Alex Kiernan32c21942018-05-29 15:30:48 +0000584/**
585 * fastboot_mmc_flash_erase() - Erase eMMC for fastboot
586 *
587 * @cmd: Named partition to erase
588 * @response: Pointer to fastboot response buffer
589 */
590void fastboot_mmc_erase(const char *cmd, char *response)
Dileep Kattab1228202015-02-17 18:48:23 +0530591{
Simon Glasse3394752016-02-29 15:25:34 -0700592 struct blk_desc *dev_desc;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600593 struct disk_partition info;
Dileep Kattab1228202015-02-17 18:48:23 +0530594 lbaint_t blks, blks_start, blks_size, grp_size;
595 struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
596
597 if (mmc == NULL) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000598 pr_err("invalid mmc device\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000599 fastboot_fail("invalid mmc device", response);
Dileep Kattab1228202015-02-17 18:48:23 +0530600 return;
601 }
602
Simon Glassbe1e9bb2016-02-29 15:25:42 -0700603 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
Dileep Kattab1228202015-02-17 18:48:23 +0530604 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000605 pr_err("invalid mmc device\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000606 fastboot_fail("invalid mmc device", response);
Dileep Kattab1228202015-02-17 18:48:23 +0530607 return;
608 }
developer6a105562020-01-16 16:11:42 +0800609
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100610#ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT
developer6a105562020-01-16 16:11:42 +0800611 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) {
612 /* erase EMMC boot1 */
Patrick Delaunay2579b0e2021-01-27 14:46:47 +0100613 fb_mmc_boot_ops(dev_desc, NULL, 1, 0, response);
614 return;
615 }
616 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT2_NAME) == 0) {
617 /* erase EMMC boot2 */
618 fb_mmc_boot_ops(dev_desc, NULL, 2, 0, response);
developer6a105562020-01-16 16:11:42 +0800619 return;
620 }
621#endif
622
Patrick Delaunay8b0a29a2021-01-27 14:46:46 +0100623#ifdef CONFIG_FASTBOOT_MMC_USER_SUPPORT
developer6a105562020-01-16 16:11:42 +0800624 if (strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) {
625 /* erase EMMC userdata */
626 if (fb_mmc_erase_mmc_hwpart(dev_desc))
627 fastboot_fail("Failed to erase EMMC_USER", response);
628 else
629 fastboot_okay(NULL, response);
630 return;
631 }
632#endif
Dileep Kattab1228202015-02-17 18:48:23 +0530633
Sean Anderson7ea25da2021-02-05 09:38:58 -0500634 if (raw_part_get_info_by_name(dev_desc, cmd, &info) != 0) {
635 if (part_get_info_by_name_or_alias(dev_desc, cmd, &info) < 0) {
636 pr_err("cannot find partition: '%s'\n", cmd);
637 fastboot_fail("cannot find partition", response);
Filip Brozovic859e6a82020-06-29 13:14:37 +0200638 return;
639 }
Dileep Kattab1228202015-02-17 18:48:23 +0530640 }
641
642 /* Align blocks to erase group size to avoid erasing other partitions */
643 grp_size = mmc->erase_grp_size;
644 blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
645 if (info.size >= grp_size)
646 blks_size = (info.size - (blks_start - info.start)) &
647 (~(grp_size - 1));
648 else
649 blks_size = 0;
650
651 printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
652 blks_start, blks_start + blks_size);
653
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000654 blks = fb_mmc_blk_write(dev_desc, blks_start, blks_size, NULL);
655
Dileep Kattab1228202015-02-17 18:48:23 +0530656 if (blks != blks_size) {
Alex Kiernan82fe3f22018-05-29 15:30:43 +0000657 pr_err("failed erasing from device %d\n", dev_desc->devnum);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000658 fastboot_fail("failed erasing from device", response);
Dileep Kattab1228202015-02-17 18:48:23 +0530659 return;
660 }
661
662 printf("........ erased " LBAFU " bytes from '%s'\n",
663 blks_size * info.blksz, cmd);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000664 fastboot_okay(NULL, response);
Dileep Kattab1228202015-02-17 18:48:23 +0530665}