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