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