Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2014 Broadcom Corporation. |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Steve Rae | 7d05934 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 6 | #include <config.h> |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 7 | #include <common.h> |
Simon Glass | 2ee8ada | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 8 | #include <blk.h> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 9 | #include <env.h> |
Maxime Ripard | 00e8eb7 | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 10 | #include <fastboot.h> |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 11 | #include <fastboot-internal.h> |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 12 | #include <fb_mmc.h> |
Maxime Ripard | 6af49bc | 2015-10-15 14:34:19 +0200 | [diff] [blame] | 13 | #include <image-sparse.h> |
Simon Glass | 2dc9c34 | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 14 | #include <image.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 16 | #include <part.h> |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 17 | #include <mmc.h> |
Siarhei Siamashka | 58f30d8 | 2015-10-28 06:24:16 +0200 | [diff] [blame] | 18 | #include <div64.h> |
Sam Protsenko | 540eb95 | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 19 | #include <linux/compat.h> |
| 20 | #include <android_image.h> |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 21 | |
Sam Protsenko | 540eb95 | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 22 | #define BOOT_PARTITION_NAME "boot" |
| 23 | |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 24 | struct fb_mmc_sparse { |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 25 | struct blk_desc *dev_desc; |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 26 | }; |
| 27 | |
Filip Brozovic | 859e6a8 | 2020-06-29 13:14:37 +0200 | [diff] [blame] | 28 | static int raw_part_get_info_by_name(struct blk_desc *dev_desc, |
Sean Anderson | 7ea25da | 2021-02-05 09:38:58 -0500 | [diff] [blame] | 29 | const char *name, |
| 30 | struct disk_partition *info) |
Filip Brozovic | 859e6a8 | 2020-06-29 13:14:37 +0200 | [diff] [blame] | 31 | { |
| 32 | /* strlen("fastboot_raw_partition_") + PART_NAME_LEN + 1 */ |
| 33 | char env_desc_name[23 + PART_NAME_LEN + 1]; |
| 34 | char *raw_part_desc; |
| 35 | const char *argv[2]; |
| 36 | const char **parg = argv; |
| 37 | |
| 38 | /* check for raw partition descriptor */ |
| 39 | strcpy(env_desc_name, "fastboot_raw_partition_"); |
Matthias Schiffer | 04a5b0f | 2021-07-30 14:23:54 +0200 | [diff] [blame] | 40 | strlcat(env_desc_name, name, sizeof(env_desc_name)); |
Filip Brozovic | 859e6a8 | 2020-06-29 13:14:37 +0200 | [diff] [blame] | 41 | raw_part_desc = strdup(env_get(env_desc_name)); |
| 42 | if (raw_part_desc == NULL) |
| 43 | return -ENODEV; |
| 44 | |
| 45 | /* |
| 46 | * parse partition descriptor |
| 47 | * |
| 48 | * <lba_start> <lba_size> [mmcpart <num>] |
| 49 | */ |
| 50 | for (; parg < argv + sizeof(argv) / sizeof(*argv); ++parg) { |
| 51 | *parg = strsep(&raw_part_desc, " "); |
| 52 | if (*parg == NULL) { |
| 53 | pr_err("Invalid number of arguments.\n"); |
| 54 | return -ENODEV; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | info->start = simple_strtoul(argv[0], NULL, 0); |
| 59 | info->size = simple_strtoul(argv[1], NULL, 0); |
| 60 | info->blksz = dev_desc->blksz; |
Sean Anderson | cb29fe8 | 2021-03-11 00:15:44 -0500 | [diff] [blame] | 61 | strlcpy((char *)info->name, name, PART_NAME_LEN); |
Filip Brozovic | 859e6a8 | 2020-06-29 13:14:37 +0200 | [diff] [blame] | 62 | |
| 63 | if (raw_part_desc) { |
Sean Anderson | 7ea25da | 2021-02-05 09:38:58 -0500 | [diff] [blame] | 64 | if (strcmp(strsep(&raw_part_desc, " "), "mmcpart") == 0) { |
| 65 | ulong mmcpart = simple_strtoul(raw_part_desc, NULL, 0); |
| 66 | int ret = blk_dselect_hwpart(dev_desc, mmcpart); |
| 67 | |
| 68 | if (ret) |
| 69 | return ret; |
| 70 | } |
Filip Brozovic | 859e6a8 | 2020-06-29 13:14:37 +0200 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
Sean Anderson | c4f2d64 | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 76 | static int do_get_part_info(struct blk_desc **dev_desc, const char *name, |
| 77 | struct disk_partition *info) |
Sean Anderson | c83e384 | 2021-02-05 09:38:59 -0500 | [diff] [blame] | 78 | { |
| 79 | int ret; |
| 80 | |
Sean Anderson | c4f2d64 | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 81 | /* First try partition names on the default device */ |
| 82 | *dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV); |
| 83 | if (*dev_desc) { |
| 84 | ret = part_get_info_by_name(*dev_desc, name, info); |
| 85 | if (ret >= 0) |
| 86 | return ret; |
| 87 | |
| 88 | /* Then try raw partitions */ |
| 89 | ret = raw_part_get_info_by_name(*dev_desc, name, info); |
| 90 | if (ret >= 0) |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | /* Then try dev.hwpart:part */ |
| 95 | ret = part_get_info_by_dev_and_name_or_num("mmc", name, dev_desc, |
| 96 | info, true); |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | static int part_get_info_by_name_or_alias(struct blk_desc **dev_desc, |
| 101 | const char *name, |
| 102 | struct disk_partition *info) |
| 103 | { |
Matthias Schiffer | 80478f4 | 2021-12-16 11:26:38 +0100 | [diff] [blame] | 104 | /* strlen("fastboot_partition_alias_") + PART_NAME_LEN + 1 */ |
| 105 | char env_alias_name[25 + PART_NAME_LEN + 1]; |
| 106 | char *aliased_part_name; |
Sean Anderson | c4f2d64 | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 107 | |
Matthias Schiffer | 80478f4 | 2021-12-16 11:26:38 +0100 | [diff] [blame] | 108 | /* check for alias */ |
| 109 | strlcpy(env_alias_name, "fastboot_partition_alias_", sizeof(env_alias_name)); |
| 110 | strlcat(env_alias_name, name, sizeof(env_alias_name)); |
| 111 | aliased_part_name = env_get(env_alias_name); |
| 112 | if (aliased_part_name) |
| 113 | name = aliased_part_name; |
Sean Anderson | c83e384 | 2021-02-05 09:38:59 -0500 | [diff] [blame] | 114 | |
Matthias Schiffer | 80478f4 | 2021-12-16 11:26:38 +0100 | [diff] [blame] | 115 | return do_get_part_info(dev_desc, name, info); |
Sean Anderson | c83e384 | 2021-02-05 09:38:59 -0500 | [diff] [blame] | 116 | } |
| 117 | |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 118 | /** |
| 119 | * fb_mmc_blk_write() - Write/erase MMC in chunks of FASTBOOT_MAX_BLK_WRITE |
| 120 | * |
| 121 | * @block_dev: Pointer to block device |
| 122 | * @start: First block to write/erase |
| 123 | * @blkcnt: Count of blocks |
| 124 | * @buffer: Pointer to data buffer for write or NULL for erase |
| 125 | */ |
| 126 | static lbaint_t fb_mmc_blk_write(struct blk_desc *block_dev, lbaint_t start, |
| 127 | lbaint_t blkcnt, const void *buffer) |
| 128 | { |
| 129 | lbaint_t blk = start; |
| 130 | lbaint_t blks_written; |
| 131 | lbaint_t cur_blkcnt; |
| 132 | lbaint_t blks = 0; |
| 133 | int i; |
| 134 | |
| 135 | for (i = 0; i < blkcnt; i += FASTBOOT_MAX_BLK_WRITE) { |
| 136 | cur_blkcnt = min((int)blkcnt - i, FASTBOOT_MAX_BLK_WRITE); |
| 137 | if (buffer) { |
| 138 | if (fastboot_progress_callback) |
| 139 | fastboot_progress_callback("writing"); |
| 140 | blks_written = blk_dwrite(block_dev, blk, cur_blkcnt, |
| 141 | buffer + (i * block_dev->blksz)); |
| 142 | } else { |
| 143 | if (fastboot_progress_callback) |
| 144 | fastboot_progress_callback("erasing"); |
| 145 | blks_written = blk_derase(block_dev, blk, cur_blkcnt); |
| 146 | } |
| 147 | blk += blks_written; |
| 148 | blks += blks_written; |
| 149 | } |
| 150 | return blks; |
| 151 | } |
| 152 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 153 | static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info, |
| 154 | lbaint_t blk, lbaint_t blkcnt, const void *buffer) |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 155 | { |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 156 | struct fb_mmc_sparse *sparse = info->priv; |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 157 | struct blk_desc *dev_desc = sparse->dev_desc; |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 158 | |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 159 | return fb_mmc_blk_write(dev_desc, blk, blkcnt, buffer); |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 160 | } |
| 161 | |
Steve Rae | 8a99be8 | 2016-06-07 11:19:38 -0700 | [diff] [blame] | 162 | static lbaint_t fb_mmc_sparse_reserve(struct sparse_storage *info, |
| 163 | lbaint_t blk, lbaint_t blkcnt) |
| 164 | { |
| 165 | return blkcnt; |
| 166 | } |
| 167 | |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 168 | static void write_raw_image(struct blk_desc *dev_desc, |
| 169 | struct disk_partition *info, const char *part_name, |
| 170 | void *buffer, u32 download_bytes, char *response) |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 171 | { |
| 172 | lbaint_t blkcnt; |
| 173 | lbaint_t blks; |
| 174 | |
| 175 | /* determine number of blocks to write */ |
| 176 | blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1)); |
Siarhei Siamashka | 58f30d8 | 2015-10-28 06:24:16 +0200 | [diff] [blame] | 177 | blkcnt = lldiv(blkcnt, info->blksz); |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 178 | |
| 179 | if (blkcnt > info->size) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 180 | pr_err("too large for partition: '%s'\n", part_name); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 181 | fastboot_fail("too large for partition", response); |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 182 | return; |
| 183 | } |
| 184 | |
| 185 | puts("Flashing Raw Image\n"); |
| 186 | |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 187 | blks = fb_mmc_blk_write(dev_desc, info->start, blkcnt, buffer); |
| 188 | |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 189 | if (blks != blkcnt) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 190 | pr_err("failed writing to device %d\n", dev_desc->devnum); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 191 | fastboot_fail("failed writing to device", response); |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 192 | return; |
| 193 | } |
| 194 | |
| 195 | printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz, |
| 196 | part_name); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 197 | fastboot_okay(NULL, response); |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Patrick Delaunay | 2579b0e | 2021-01-27 14:46:47 +0100 | [diff] [blame] | 200 | #if defined(CONFIG_FASTBOOT_MMC_BOOT_SUPPORT) || \ |
Patrick Delaunay | 8b0a29a | 2021-01-27 14:46:46 +0100 | [diff] [blame] | 201 | defined(CONFIG_FASTBOOT_MMC_USER_SUPPORT) |
developer | 6a10556 | 2020-01-16 16:11:42 +0800 | [diff] [blame] | 202 | static int fb_mmc_erase_mmc_hwpart(struct blk_desc *dev_desc) |
| 203 | { |
| 204 | lbaint_t blks; |
| 205 | |
| 206 | debug("Start Erasing mmc hwpart[%u]...\n", dev_desc->hwpart); |
| 207 | |
| 208 | blks = fb_mmc_blk_write(dev_desc, 0, dev_desc->lba, NULL); |
| 209 | |
| 210 | if (blks != dev_desc->lba) { |
| 211 | pr_err("Failed to erase mmc hwpart[%u]\n", dev_desc->hwpart); |
| 212 | return 1; |
| 213 | } |
| 214 | |
| 215 | printf("........ erased %lu bytes from mmc hwpart[%u]\n", |
| 216 | dev_desc->lba * dev_desc->blksz, dev_desc->hwpart); |
| 217 | |
| 218 | return 0; |
| 219 | } |
Patrick Delaunay | 8b0a29a | 2021-01-27 14:46:46 +0100 | [diff] [blame] | 220 | #endif |
developer | 6a10556 | 2020-01-16 16:11:42 +0800 | [diff] [blame] | 221 | |
Patrick Delaunay | 2579b0e | 2021-01-27 14:46:47 +0100 | [diff] [blame] | 222 | #ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT |
| 223 | static void fb_mmc_boot_ops(struct blk_desc *dev_desc, void *buffer, |
| 224 | int hwpart, u32 buff_sz, char *response) |
developer | 6a10556 | 2020-01-16 16:11:42 +0800 | [diff] [blame] | 225 | { |
| 226 | lbaint_t blkcnt; |
| 227 | lbaint_t blks; |
| 228 | unsigned long blksz; |
| 229 | |
Patrick Delaunay | 2579b0e | 2021-01-27 14:46:47 +0100 | [diff] [blame] | 230 | // To operate on EMMC_BOOT1/2 (mmc0boot0/1) we first change the hwpart |
| 231 | if (blk_dselect_hwpart(dev_desc, hwpart)) { |
developer | 6a10556 | 2020-01-16 16:11:42 +0800 | [diff] [blame] | 232 | pr_err("Failed to select hwpart\n"); |
| 233 | fastboot_fail("Failed to select hwpart", response); |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | if (buffer) { /* flash */ |
| 238 | |
| 239 | /* determine number of blocks to write */ |
| 240 | blksz = dev_desc->blksz; |
| 241 | blkcnt = ((buff_sz + (blksz - 1)) & ~(blksz - 1)); |
| 242 | blkcnt = lldiv(blkcnt, blksz); |
| 243 | |
| 244 | if (blkcnt > dev_desc->lba) { |
| 245 | pr_err("Image size too large\n"); |
| 246 | fastboot_fail("Image size too large", response); |
| 247 | return; |
| 248 | } |
| 249 | |
Patrick Delaunay | 2579b0e | 2021-01-27 14:46:47 +0100 | [diff] [blame] | 250 | debug("Start Flashing Image to EMMC_BOOT%d...\n", hwpart); |
developer | 6a10556 | 2020-01-16 16:11:42 +0800 | [diff] [blame] | 251 | |
| 252 | blks = fb_mmc_blk_write(dev_desc, 0, blkcnt, buffer); |
| 253 | |
| 254 | if (blks != blkcnt) { |
Patrick Delaunay | 2579b0e | 2021-01-27 14:46:47 +0100 | [diff] [blame] | 255 | pr_err("Failed to write EMMC_BOOT%d\n", hwpart); |
| 256 | fastboot_fail("Failed to write EMMC_BOOT part", |
| 257 | response); |
developer | 6a10556 | 2020-01-16 16:11:42 +0800 | [diff] [blame] | 258 | return; |
| 259 | } |
| 260 | |
Patrick Delaunay | 2579b0e | 2021-01-27 14:46:47 +0100 | [diff] [blame] | 261 | printf("........ wrote %lu bytes to EMMC_BOOT%d\n", |
| 262 | blkcnt * blksz, hwpart); |
developer | 6a10556 | 2020-01-16 16:11:42 +0800 | [diff] [blame] | 263 | } else { /* erase */ |
| 264 | if (fb_mmc_erase_mmc_hwpart(dev_desc)) { |
Patrick Delaunay | 2579b0e | 2021-01-27 14:46:47 +0100 | [diff] [blame] | 265 | pr_err("Failed to erase EMMC_BOOT%d\n", hwpart); |
| 266 | fastboot_fail("Failed to erase EMMC_BOOT part", |
| 267 | response); |
developer | 6a10556 | 2020-01-16 16:11:42 +0800 | [diff] [blame] | 268 | return; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | fastboot_okay(NULL, response); |
| 273 | } |
| 274 | #endif |
| 275 | |
Sam Protsenko | 540eb95 | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 276 | #ifdef CONFIG_ANDROID_BOOT_IMAGE |
| 277 | /** |
| 278 | * Read Android boot image header from boot partition. |
| 279 | * |
| 280 | * @param[in] dev_desc MMC device descriptor |
| 281 | * @param[in] info Boot partition info |
| 282 | * @param[out] hdr Where to store read boot image header |
| 283 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 284 | * Return: Boot image header sectors count or 0 on error |
Sam Protsenko | 540eb95 | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 285 | */ |
| 286 | static lbaint_t fb_mmc_get_boot_header(struct blk_desc *dev_desc, |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 287 | struct disk_partition *info, |
Safae Ouajih | 8656e38 | 2023-02-06 00:50:03 +0100 | [diff] [blame] | 288 | struct andr_boot_img_hdr_v0 *hdr, |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 289 | char *response) |
Sam Protsenko | 540eb95 | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 290 | { |
| 291 | ulong sector_size; /* boot partition sector size */ |
| 292 | lbaint_t hdr_sectors; /* boot image header sectors count */ |
| 293 | int res; |
| 294 | |
| 295 | /* Calculate boot image sectors count */ |
| 296 | sector_size = info->blksz; |
Safae Ouajih | 8656e38 | 2023-02-06 00:50:03 +0100 | [diff] [blame] | 297 | hdr_sectors = DIV_ROUND_UP(sizeof(struct andr_boot_img_hdr_v0), sector_size); |
Sam Protsenko | 540eb95 | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 298 | if (hdr_sectors == 0) { |
Alex Kiernan | 82fe3f2 | 2018-05-29 15:30:43 +0000 | [diff] [blame] | 299 | pr_err("invalid number of boot sectors: 0\n"); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 300 | fastboot_fail("invalid number of boot sectors: 0", response); |
Sam Protsenko | 540eb95 | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | /* Read the boot image header */ |
| 305 | res = blk_dread(dev_desc, info->start, hdr_sectors, (void *)hdr); |
Tom Rini | bda6f77 | 2017-08-14 21:00:44 -0400 | [diff] [blame] | 306 | if (res != hdr_sectors) { |
Alex Kiernan | 82fe3f2 | 2018-05-29 15:30:43 +0000 | [diff] [blame] | 307 | pr_err("cannot read header from boot partition\n"); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 308 | fastboot_fail("cannot read header from boot partition", |
| 309 | response); |
Sam Protsenko | 540eb95 | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | /* Check boot header magic string */ |
Safae Ouajih | 88ad0c1 | 2023-02-06 00:50:05 +0100 | [diff] [blame] | 314 | if (!is_android_boot_image_header(hdr)) { |
Alex Kiernan | 82fe3f2 | 2018-05-29 15:30:43 +0000 | [diff] [blame] | 315 | pr_err("bad boot image magic\n"); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 316 | fastboot_fail("boot partition not initialized", response); |
Sam Protsenko | 540eb95 | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | return hdr_sectors; |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Write downloaded zImage to boot partition and repack it properly. |
| 325 | * |
| 326 | * @param dev_desc MMC device descriptor |
| 327 | * @param download_buffer Address to fastboot buffer with zImage in it |
| 328 | * @param download_bytes Size of fastboot buffer, in bytes |
| 329 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 330 | * Return: 0 on success or -1 on error |
Sam Protsenko | 540eb95 | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 331 | */ |
| 332 | static int fb_mmc_update_zimage(struct blk_desc *dev_desc, |
| 333 | void *download_buffer, |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 334 | u32 download_bytes, |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 335 | char *response) |
Sam Protsenko | 540eb95 | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 336 | { |
Tom Rini | 0ac605c | 2017-06-10 09:15:37 -0400 | [diff] [blame] | 337 | uintptr_t hdr_addr; /* boot image header address */ |
Safae Ouajih | 8656e38 | 2023-02-06 00:50:03 +0100 | [diff] [blame] | 338 | struct andr_boot_img_hdr_v0 *hdr; /* boot image header */ |
Sam Protsenko | 540eb95 | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 339 | lbaint_t hdr_sectors; /* boot image header sectors */ |
| 340 | u8 *ramdisk_buffer; |
| 341 | u32 ramdisk_sector_start; |
| 342 | u32 ramdisk_sectors; |
| 343 | u32 kernel_sector_start; |
| 344 | u32 kernel_sectors; |
| 345 | u32 sectors_per_page; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 346 | struct disk_partition info; |
Sam Protsenko | 540eb95 | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 347 | int res; |
| 348 | |
| 349 | puts("Flashing zImage\n"); |
| 350 | |
| 351 | /* Get boot partition info */ |
| 352 | res = part_get_info_by_name(dev_desc, BOOT_PARTITION_NAME, &info); |
| 353 | if (res < 0) { |
Alex Kiernan | 82fe3f2 | 2018-05-29 15:30:43 +0000 | [diff] [blame] | 354 | pr_err("cannot find boot partition\n"); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 355 | fastboot_fail("cannot find boot partition", response); |
Sam Protsenko | 540eb95 | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 356 | return -1; |
| 357 | } |
| 358 | |
| 359 | /* Put boot image header in fastboot buffer after downloaded zImage */ |
Tom Rini | 0ac605c | 2017-06-10 09:15:37 -0400 | [diff] [blame] | 360 | hdr_addr = (uintptr_t)download_buffer + ALIGN(download_bytes, PAGE_SIZE); |
Safae Ouajih | 8656e38 | 2023-02-06 00:50:03 +0100 | [diff] [blame] | 361 | hdr = (struct andr_boot_img_hdr_v0 *)hdr_addr; |
Sam Protsenko | 540eb95 | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 362 | |
| 363 | /* Read boot image header */ |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 364 | hdr_sectors = fb_mmc_get_boot_header(dev_desc, &info, hdr, response); |
Sam Protsenko | 540eb95 | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 365 | if (hdr_sectors == 0) { |
Alex Kiernan | 82fe3f2 | 2018-05-29 15:30:43 +0000 | [diff] [blame] | 366 | pr_err("unable to read boot image header\n"); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 367 | fastboot_fail("unable to read boot image header", response); |
Sam Protsenko | 540eb95 | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 368 | return -1; |
| 369 | } |
| 370 | |
Safae Ouajih | 4ae6e43 | 2023-02-06 00:50:16 +0100 | [diff] [blame] | 371 | /* Check if boot image header version is 2 or less */ |
| 372 | if (hdr->header_version > 2) { |
| 373 | pr_err("zImage flashing supported only for boot images v2 and less\n"); |
| 374 | fastboot_fail("zImage flashing supported only for boot images v2 and less", |
| 375 | response); |
| 376 | return -EOPNOTSUPP; |
| 377 | } |
| 378 | |
Sam Protsenko | 540eb95 | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 379 | /* Check if boot image has second stage in it (we don't support it) */ |
| 380 | if (hdr->second_size > 0) { |
Alex Kiernan | 82fe3f2 | 2018-05-29 15:30:43 +0000 | [diff] [blame] | 381 | pr_err("moving second stage is not supported yet\n"); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 382 | fastboot_fail("moving second stage is not supported yet", |
| 383 | response); |
Sam Protsenko | 540eb95 | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 384 | return -1; |
| 385 | } |
| 386 | |
| 387 | /* Extract ramdisk location */ |
| 388 | sectors_per_page = hdr->page_size / info.blksz; |
| 389 | ramdisk_sector_start = info.start + sectors_per_page; |
| 390 | ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) * |
| 391 | sectors_per_page; |
| 392 | ramdisk_sectors = DIV_ROUND_UP(hdr->ramdisk_size, hdr->page_size) * |
| 393 | sectors_per_page; |
| 394 | |
| 395 | /* Read ramdisk and put it in fastboot buffer after boot image header */ |
| 396 | ramdisk_buffer = (u8 *)hdr + (hdr_sectors * info.blksz); |
| 397 | res = blk_dread(dev_desc, ramdisk_sector_start, ramdisk_sectors, |
| 398 | ramdisk_buffer); |
Tom Rini | bda6f77 | 2017-08-14 21:00:44 -0400 | [diff] [blame] | 399 | if (res != ramdisk_sectors) { |
Alex Kiernan | 82fe3f2 | 2018-05-29 15:30:43 +0000 | [diff] [blame] | 400 | pr_err("cannot read ramdisk from boot partition\n"); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 401 | fastboot_fail("cannot read ramdisk from boot partition", |
| 402 | response); |
Sam Protsenko | 540eb95 | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 403 | return -1; |
| 404 | } |
| 405 | |
| 406 | /* Write new kernel size to boot image header */ |
| 407 | hdr->kernel_size = download_bytes; |
| 408 | res = blk_dwrite(dev_desc, info.start, hdr_sectors, (void *)hdr); |
| 409 | if (res == 0) { |
Alex Kiernan | 82fe3f2 | 2018-05-29 15:30:43 +0000 | [diff] [blame] | 410 | pr_err("cannot writeback boot image header\n"); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 411 | fastboot_fail("cannot write back boot image header", response); |
Sam Protsenko | 540eb95 | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 412 | return -1; |
| 413 | } |
| 414 | |
| 415 | /* Write the new downloaded kernel */ |
| 416 | kernel_sector_start = info.start + sectors_per_page; |
| 417 | kernel_sectors = DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) * |
| 418 | sectors_per_page; |
| 419 | res = blk_dwrite(dev_desc, kernel_sector_start, kernel_sectors, |
| 420 | download_buffer); |
| 421 | if (res == 0) { |
Alex Kiernan | 82fe3f2 | 2018-05-29 15:30:43 +0000 | [diff] [blame] | 422 | pr_err("cannot write new kernel\n"); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 423 | fastboot_fail("cannot write new kernel", response); |
Sam Protsenko | 540eb95 | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 424 | return -1; |
| 425 | } |
| 426 | |
| 427 | /* Write the saved ramdisk back */ |
| 428 | ramdisk_sector_start = info.start + sectors_per_page; |
| 429 | ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) * |
| 430 | sectors_per_page; |
| 431 | res = blk_dwrite(dev_desc, ramdisk_sector_start, ramdisk_sectors, |
| 432 | ramdisk_buffer); |
| 433 | if (res == 0) { |
Alex Kiernan | 82fe3f2 | 2018-05-29 15:30:43 +0000 | [diff] [blame] | 434 | pr_err("cannot write back original ramdisk\n"); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 435 | fastboot_fail("cannot write back original ramdisk", response); |
Sam Protsenko | 540eb95 | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 436 | return -1; |
| 437 | } |
| 438 | |
| 439 | puts("........ zImage was updated in boot partition\n"); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 440 | fastboot_okay(NULL, response); |
Sam Protsenko | 540eb95 | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 441 | return 0; |
| 442 | } |
| 443 | #endif |
| 444 | |
Alex Kiernan | 32c2194 | 2018-05-29 15:30:48 +0000 | [diff] [blame] | 445 | /** |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 446 | * fastboot_mmc_get_part_info() - Lookup eMMC partion by name |
| 447 | * |
| 448 | * @part_name: Named partition to lookup |
| 449 | * @dev_desc: Pointer to returned blk_desc pointer |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 450 | * @part_info: Pointer to returned struct disk_partition |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 451 | * @response: Pointer to fastboot response buffer |
| 452 | */ |
Sam Protsenko | 0fb37ee | 2019-06-13 21:11:07 +0300 | [diff] [blame] | 453 | int fastboot_mmc_get_part_info(const char *part_name, |
| 454 | struct blk_desc **dev_desc, |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 455 | struct disk_partition *part_info, char *response) |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 456 | { |
Sean Anderson | c4f2d64 | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 457 | int ret; |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 458 | |
Eugeniu Rosca | 8eba90b | 2019-03-28 14:31:33 +0100 | [diff] [blame] | 459 | if (!part_name || !strcmp(part_name, "")) { |
| 460 | fastboot_fail("partition not given", response); |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 461 | return -ENOENT; |
| 462 | } |
| 463 | |
Sean Anderson | c4f2d64 | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 464 | ret = part_get_info_by_name_or_alias(dev_desc, part_name, part_info); |
| 465 | if (ret < 0) { |
| 466 | switch (ret) { |
| 467 | case -ENOSYS: |
| 468 | case -EINVAL: |
| 469 | fastboot_fail("invalid partition or device", response); |
| 470 | break; |
| 471 | case -ENODEV: |
| 472 | fastboot_fail("no such device", response); |
| 473 | break; |
| 474 | case -ENOENT: |
| 475 | fastboot_fail("no such partition", response); |
| 476 | break; |
| 477 | case -EPROTONOSUPPORT: |
| 478 | fastboot_fail("unknown partition table type", response); |
| 479 | break; |
| 480 | default: |
| 481 | fastboot_fail("unanticipated error", response); |
| 482 | break; |
Filip Brozovic | 859e6a8 | 2020-06-29 13:14:37 +0200 | [diff] [blame] | 483 | } |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Sean Anderson | c4f2d64 | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 486 | return ret; |
| 487 | } |
| 488 | |
| 489 | static struct blk_desc *fastboot_mmc_get_dev(char *response) |
| 490 | { |
| 491 | struct blk_desc *ret = blk_get_dev("mmc", |
| 492 | CONFIG_FASTBOOT_FLASH_MMC_DEV); |
| 493 | |
| 494 | if (!ret || ret->type == DEV_TYPE_UNKNOWN) { |
| 495 | pr_err("invalid mmc device\n"); |
| 496 | fastboot_fail("invalid mmc device", response); |
| 497 | return NULL; |
| 498 | } |
| 499 | return ret; |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | /** |
Alex Kiernan | 32c2194 | 2018-05-29 15:30:48 +0000 | [diff] [blame] | 503 | * fastboot_mmc_flash_write() - Write image to eMMC for fastboot |
| 504 | * |
| 505 | * @cmd: Named partition to write image to |
| 506 | * @download_buffer: Pointer to image data |
| 507 | * @download_bytes: Size of image data |
| 508 | * @response: Pointer to fastboot response buffer |
| 509 | */ |
| 510 | void fastboot_mmc_flash_write(const char *cmd, void *download_buffer, |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 511 | u32 download_bytes, char *response) |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 512 | { |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 513 | struct blk_desc *dev_desc; |
Oleh Kravchenko | ff6c0e3 | 2021-05-19 13:31:31 +0300 | [diff] [blame] | 514 | struct disk_partition info = {0}; |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 515 | |
Patrick Delaunay | 2579b0e | 2021-01-27 14:46:47 +0100 | [diff] [blame] | 516 | #ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT |
developer | 6a10556 | 2020-01-16 16:11:42 +0800 | [diff] [blame] | 517 | if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) { |
Sean Anderson | c4f2d64 | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 518 | dev_desc = fastboot_mmc_get_dev(response); |
| 519 | if (dev_desc) |
| 520 | fb_mmc_boot_ops(dev_desc, download_buffer, 1, |
| 521 | download_bytes, response); |
Patrick Delaunay | 2579b0e | 2021-01-27 14:46:47 +0100 | [diff] [blame] | 522 | return; |
| 523 | } |
| 524 | if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT2_NAME) == 0) { |
Sean Anderson | c4f2d64 | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 525 | dev_desc = fastboot_mmc_get_dev(response); |
| 526 | if (dev_desc) |
Oleh Kravchenko | 8c73f85 | 2021-05-15 00:06:21 +0300 | [diff] [blame] | 527 | fb_mmc_boot_ops(dev_desc, download_buffer, 2, |
Sean Anderson | c4f2d64 | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 528 | download_bytes, response); |
developer | 6a10556 | 2020-01-16 16:11:42 +0800 | [diff] [blame] | 529 | return; |
| 530 | } |
| 531 | #endif |
| 532 | |
Patrick Delaunay | 8a4f2bd | 2017-01-27 11:00:41 +0100 | [diff] [blame] | 533 | #if CONFIG_IS_ENABLED(EFI_PARTITION) |
Steve Rae | 7d05934 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 534 | if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) { |
Sean Anderson | c4f2d64 | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 535 | dev_desc = fastboot_mmc_get_dev(response); |
| 536 | if (!dev_desc) |
| 537 | return; |
| 538 | |
Steve Rae | 7d05934 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 539 | printf("%s: updating MBR, Primary and Backup GPT(s)\n", |
| 540 | __func__); |
| 541 | if (is_valid_gpt_buf(dev_desc, download_buffer)) { |
| 542 | printf("%s: invalid GPT - refusing to write to flash\n", |
| 543 | __func__); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 544 | fastboot_fail("invalid GPT partition", response); |
Steve Rae | 7d05934 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 545 | return; |
| 546 | } |
| 547 | if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) { |
| 548 | printf("%s: writing GPT partitions failed\n", __func__); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 549 | fastboot_fail("writing GPT partitions failed", |
| 550 | response); |
Steve Rae | 7d05934 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 551 | return; |
| 552 | } |
Roman Stratiienko | 330bf50 | 2021-01-27 17:40:16 +0200 | [diff] [blame] | 553 | part_init(dev_desc); |
Steve Rae | 7d05934 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 554 | printf("........ success\n"); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 555 | fastboot_okay(NULL, response); |
Steve Rae | 7d05934 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 556 | return; |
Petr Kulhavy | 9f174c9 | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 557 | } |
| 558 | #endif |
| 559 | |
Patrick Delaunay | f7e0772 | 2017-01-27 11:00:37 +0100 | [diff] [blame] | 560 | #if CONFIG_IS_ENABLED(DOS_PARTITION) |
Petr Kulhavy | 9f174c9 | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 561 | if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) { |
Sean Anderson | c4f2d64 | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 562 | dev_desc = fastboot_mmc_get_dev(response); |
| 563 | if (!dev_desc) |
| 564 | return; |
| 565 | |
Petr Kulhavy | 9f174c9 | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 566 | printf("%s: updating MBR\n", __func__); |
| 567 | if (is_valid_dos_buf(download_buffer)) { |
| 568 | printf("%s: invalid MBR - refusing to write to flash\n", |
| 569 | __func__); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 570 | fastboot_fail("invalid MBR partition", response); |
Petr Kulhavy | 9f174c9 | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 571 | return; |
| 572 | } |
Marek Szyprowski | 29ee06b | 2020-12-23 13:55:10 +0100 | [diff] [blame] | 573 | if (write_mbr_sector(dev_desc, download_buffer)) { |
Petr Kulhavy | 9f174c9 | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 574 | printf("%s: writing MBR partition failed\n", __func__); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 575 | fastboot_fail("writing MBR partition failed", |
| 576 | response); |
Petr Kulhavy | 9f174c9 | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 577 | return; |
| 578 | } |
Roman Stratiienko | 330bf50 | 2021-01-27 17:40:16 +0200 | [diff] [blame] | 579 | part_init(dev_desc); |
Petr Kulhavy | 9f174c9 | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 580 | printf("........ success\n"); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 581 | fastboot_okay(NULL, response); |
Petr Kulhavy | 9f174c9 | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 582 | return; |
| 583 | } |
| 584 | #endif |
| 585 | |
Sam Protsenko | 540eb95 | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 586 | #ifdef CONFIG_ANDROID_BOOT_IMAGE |
| 587 | if (strncasecmp(cmd, "zimage", 6) == 0) { |
Sean Anderson | c4f2d64 | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 588 | dev_desc = fastboot_mmc_get_dev(response); |
| 589 | if (dev_desc) |
| 590 | fb_mmc_update_zimage(dev_desc, download_buffer, |
| 591 | download_bytes, response); |
Sam Protsenko | 540eb95 | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 592 | return; |
| 593 | } |
| 594 | #endif |
| 595 | |
Simon Glass | 8b0d42c | 2023-02-05 15:39:56 -0700 | [diff] [blame] | 596 | #if IS_ENABLED(CONFIG_FASTBOOT_MMC_USER_SUPPORT) |
Oleh Kravchenko | ff6c0e3 | 2021-05-19 13:31:31 +0300 | [diff] [blame] | 597 | if (strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) { |
| 598 | dev_desc = fastboot_mmc_get_dev(response); |
| 599 | if (!dev_desc) |
| 600 | return; |
| 601 | |
| 602 | strlcpy((char *)&info.name, cmd, sizeof(info.name)); |
| 603 | info.size = dev_desc->lba; |
| 604 | info.blksz = dev_desc->blksz; |
| 605 | } |
| 606 | #endif |
| 607 | |
| 608 | if (!info.name[0] && |
| 609 | fastboot_mmc_get_part_info(cmd, &dev_desc, &info, response) < 0) |
Sean Anderson | c4f2d64 | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 610 | return; |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 611 | |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 612 | if (is_sparse_image(download_buffer)) { |
| 613 | struct fb_mmc_sparse sparse_priv; |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 614 | struct sparse_storage sparse; |
Jassi Brar | 427e669 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 615 | int err; |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 616 | |
| 617 | sparse_priv.dev_desc = dev_desc; |
| 618 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 619 | sparse.blksz = info.blksz; |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 620 | sparse.start = info.start; |
| 621 | sparse.size = info.size; |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 622 | sparse.write = fb_mmc_sparse_write; |
Steve Rae | 8a99be8 | 2016-06-07 11:19:38 -0700 | [diff] [blame] | 623 | sparse.reserve = fb_mmc_sparse_reserve; |
Jassi Brar | 427e669 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 624 | sparse.mssg = fastboot_fail; |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 625 | |
| 626 | printf("Flashing sparse image at offset " LBAFU "\n", |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 627 | sparse.start); |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 628 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 629 | sparse.priv = &sparse_priv; |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 630 | err = write_sparse_image(&sparse, cmd, download_buffer, |
| 631 | response); |
Jassi Brar | 427e669 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 632 | if (!err) |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 633 | fastboot_okay(NULL, response); |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 634 | } else { |
Steve Rae | c601567 | 2014-08-26 11:47:30 -0700 | [diff] [blame] | 635 | write_raw_image(dev_desc, &info, cmd, download_buffer, |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 636 | download_bytes, response); |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 637 | } |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 638 | } |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 639 | |
Alex Kiernan | 32c2194 | 2018-05-29 15:30:48 +0000 | [diff] [blame] | 640 | /** |
| 641 | * fastboot_mmc_flash_erase() - Erase eMMC for fastboot |
| 642 | * |
| 643 | * @cmd: Named partition to erase |
| 644 | * @response: Pointer to fastboot response buffer |
| 645 | */ |
| 646 | void fastboot_mmc_erase(const char *cmd, char *response) |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 647 | { |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 648 | struct blk_desc *dev_desc; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 649 | struct disk_partition info; |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 650 | lbaint_t blks, blks_start, blks_size, grp_size; |
| 651 | struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV); |
| 652 | |
Patrick Delaunay | 2579b0e | 2021-01-27 14:46:47 +0100 | [diff] [blame] | 653 | #ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT |
developer | 6a10556 | 2020-01-16 16:11:42 +0800 | [diff] [blame] | 654 | if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) { |
| 655 | /* erase EMMC boot1 */ |
Sean Anderson | c4f2d64 | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 656 | dev_desc = fastboot_mmc_get_dev(response); |
| 657 | if (dev_desc) |
| 658 | fb_mmc_boot_ops(dev_desc, NULL, 1, 0, response); |
Patrick Delaunay | 2579b0e | 2021-01-27 14:46:47 +0100 | [diff] [blame] | 659 | return; |
| 660 | } |
| 661 | if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT2_NAME) == 0) { |
| 662 | /* erase EMMC boot2 */ |
Sean Anderson | c4f2d64 | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 663 | dev_desc = fastboot_mmc_get_dev(response); |
| 664 | if (dev_desc) |
Oleh Kravchenko | 8c73f85 | 2021-05-15 00:06:21 +0300 | [diff] [blame] | 665 | fb_mmc_boot_ops(dev_desc, NULL, 2, 0, response); |
developer | 6a10556 | 2020-01-16 16:11:42 +0800 | [diff] [blame] | 666 | return; |
| 667 | } |
| 668 | #endif |
| 669 | |
Patrick Delaunay | 8b0a29a | 2021-01-27 14:46:46 +0100 | [diff] [blame] | 670 | #ifdef CONFIG_FASTBOOT_MMC_USER_SUPPORT |
developer | 6a10556 | 2020-01-16 16:11:42 +0800 | [diff] [blame] | 671 | if (strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) { |
| 672 | /* erase EMMC userdata */ |
Sean Anderson | c4f2d64 | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 673 | dev_desc = fastboot_mmc_get_dev(response); |
| 674 | if (!dev_desc) |
| 675 | return; |
| 676 | |
developer | 6a10556 | 2020-01-16 16:11:42 +0800 | [diff] [blame] | 677 | if (fb_mmc_erase_mmc_hwpart(dev_desc)) |
| 678 | fastboot_fail("Failed to erase EMMC_USER", response); |
| 679 | else |
| 680 | fastboot_okay(NULL, response); |
| 681 | return; |
| 682 | } |
| 683 | #endif |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 684 | |
Sean Anderson | c4f2d64 | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 685 | if (fastboot_mmc_get_part_info(cmd, &dev_desc, &info, response) < 0) |
| 686 | return; |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 687 | |
| 688 | /* Align blocks to erase group size to avoid erasing other partitions */ |
| 689 | grp_size = mmc->erase_grp_size; |
| 690 | blks_start = (info.start + grp_size - 1) & ~(grp_size - 1); |
| 691 | if (info.size >= grp_size) |
| 692 | blks_size = (info.size - (blks_start - info.start)) & |
| 693 | (~(grp_size - 1)); |
| 694 | else |
| 695 | blks_size = 0; |
| 696 | |
| 697 | printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n", |
| 698 | blks_start, blks_start + blks_size); |
| 699 | |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 700 | blks = fb_mmc_blk_write(dev_desc, blks_start, blks_size, NULL); |
| 701 | |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 702 | if (blks != blks_size) { |
Alex Kiernan | 82fe3f2 | 2018-05-29 15:30:43 +0000 | [diff] [blame] | 703 | pr_err("failed erasing from device %d\n", dev_desc->devnum); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 704 | fastboot_fail("failed erasing from device", response); |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 705 | return; |
| 706 | } |
| 707 | |
| 708 | printf("........ erased " LBAFU " bytes from '%s'\n", |
| 709 | blks_size * info.blksz, cmd); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 710 | fastboot_okay(NULL, response); |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 711 | } |