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