Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2014 Broadcom Corporation. |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 6 | #include <config.h> |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 7 | #include <common.h> |
Simon Glass | 2a981dc | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 8 | #include <blk.h> |
Simon Glass | 7b51b57 | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 9 | #include <env.h> |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 10 | #include <fastboot.h> |
Alex Kiernan | f73a7df | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 11 | #include <fastboot-internal.h> |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 12 | #include <fb_mmc.h> |
Maxime Ripard | 3d4ef38 | 2015-10-15 14:34:19 +0200 | [diff] [blame] | 13 | #include <image-sparse.h> |
Simon Glass | 4d72caa | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 14 | #include <image.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 16 | #include <part.h> |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 17 | #include <mmc.h> |
Siarhei Siamashka | 5e0efc1 | 2015-10-28 06:24:16 +0200 | [diff] [blame] | 18 | #include <div64.h> |
Sam Protsenko | efeccfe | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 19 | #include <linux/compat.h> |
| 20 | #include <android_image.h> |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 21 | |
Alex Kiernan | f73a7df | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 22 | #define FASTBOOT_MAX_BLK_WRITE 16384 |
| 23 | |
Sam Protsenko | efeccfe | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 24 | #define BOOT_PARTITION_NAME "boot" |
| 25 | |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 26 | struct fb_mmc_sparse { |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 27 | struct blk_desc *dev_desc; |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 28 | }; |
| 29 | |
Filip Brozovic | a17c0cb | 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 | f3cf964 | 2021-02-05 09:38:58 -0500 | [diff] [blame] | 31 | const char *name, |
| 32 | struct disk_partition *info) |
Filip Brozovic | a17c0cb | 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 | 5f9338a | 2021-07-30 14:23:54 +0200 | [diff] [blame] | 42 | strlcat(env_desc_name, name, sizeof(env_desc_name)); |
Filip Brozovic | a17c0cb | 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 | 69a7529 | 2021-03-11 00:15:44 -0500 | [diff] [blame] | 63 | strlcpy((char *)info->name, name, PART_NAME_LEN); |
Filip Brozovic | a17c0cb | 2020-06-29 13:14:37 +0200 | [diff] [blame] | 64 | |
| 65 | if (raw_part_desc) { |
Sean Anderson | f3cf964 | 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 | a17c0cb | 2020-06-29 13:14:37 +0200 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
Sean Anderson | de1728c | 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 | ae5e6b4 | 2021-02-05 09:38:59 -0500 | [diff] [blame] | 80 | { |
| 81 | int ret; |
| 82 | |
Sean Anderson | de1728c | 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 | 7e90f77 | 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 | de1728c | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 109 | |
Matthias Schiffer | 7e90f77 | 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 | ae5e6b4 | 2021-02-05 09:38:59 -0500 | [diff] [blame] | 116 | |
Matthias Schiffer | 7e90f77 | 2021-12-16 11:26:38 +0100 | [diff] [blame] | 117 | return do_get_part_info(dev_desc, name, info); |
Sean Anderson | ae5e6b4 | 2021-02-05 09:38:59 -0500 | [diff] [blame] | 118 | } |
| 119 | |
Alex Kiernan | f73a7df | 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 | cc0f08cd | 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 | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 157 | { |
Steve Rae | cc0f08cd | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 158 | struct fb_mmc_sparse *sparse = info->priv; |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 159 | struct blk_desc *dev_desc = sparse->dev_desc; |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 160 | |
Alex Kiernan | f73a7df | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 161 | return fb_mmc_blk_write(dev_desc, blk, blkcnt, buffer); |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 162 | } |
| 163 | |
Steve Rae | 2c72404 | 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 | 0528979 | 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 | c0aebb3 | 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 | 5e0efc1 | 2015-10-28 06:24:16 +0200 | [diff] [blame] | 179 | blkcnt = lldiv(blkcnt, info->blksz); |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 180 | |
| 181 | if (blkcnt > info->size) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 182 | pr_err("too large for partition: '%s'\n", part_name); |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 183 | fastboot_fail("too large for partition", response); |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | |
| 187 | puts("Flashing Raw Image\n"); |
| 188 | |
Alex Kiernan | f73a7df | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 189 | blks = fb_mmc_blk_write(dev_desc, info->start, blkcnt, buffer); |
| 190 | |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 191 | if (blks != blkcnt) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 192 | pr_err("failed writing to device %d\n", dev_desc->devnum); |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 193 | fastboot_fail("failed writing to device", response); |
Steve Rae | c0aebb3 | 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 | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 199 | fastboot_okay(NULL, response); |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Patrick Delaunay | 3acbc7b | 2021-01-27 14:46:47 +0100 | [diff] [blame] | 202 | #if defined(CONFIG_FASTBOOT_MMC_BOOT_SUPPORT) || \ |
Patrick Delaunay | 7596696 | 2021-01-27 14:46:46 +0100 | [diff] [blame] | 203 | defined(CONFIG_FASTBOOT_MMC_USER_SUPPORT) |
mingming lee | 1fdbad0 | 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 | 7596696 | 2021-01-27 14:46:46 +0100 | [diff] [blame] | 222 | #endif |
mingming lee | 1fdbad0 | 2020-01-16 16:11:42 +0800 | [diff] [blame] | 223 | |
Patrick Delaunay | 3acbc7b | 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) |
mingming lee | 1fdbad0 | 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 | 3acbc7b | 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)) { |
mingming lee | 1fdbad0 | 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 | 3acbc7b | 2021-01-27 14:46:47 +0100 | [diff] [blame] | 252 | debug("Start Flashing Image to EMMC_BOOT%d...\n", hwpart); |
mingming lee | 1fdbad0 | 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 | 3acbc7b | 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); |
mingming lee | 1fdbad0 | 2020-01-16 16:11:42 +0800 | [diff] [blame] | 260 | return; |
| 261 | } |
| 262 | |
Patrick Delaunay | 3acbc7b | 2021-01-27 14:46:47 +0100 | [diff] [blame] | 263 | printf("........ wrote %lu bytes to EMMC_BOOT%d\n", |
| 264 | blkcnt * blksz, hwpart); |
mingming lee | 1fdbad0 | 2020-01-16 16:11:42 +0800 | [diff] [blame] | 265 | } else { /* erase */ |
| 266 | if (fb_mmc_erase_mmc_hwpart(dev_desc)) { |
Patrick Delaunay | 3acbc7b | 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); |
mingming lee | 1fdbad0 | 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 | efeccfe | 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 | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 286 | * Return: Boot image header sectors count or 0 on error |
Sam Protsenko | efeccfe | 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 | 0528979 | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 289 | struct disk_partition *info, |
Safae Ouajih | d71a732 | 2023-02-06 00:50:03 +0100 | [diff] [blame] | 290 | struct andr_boot_img_hdr_v0 *hdr, |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 291 | char *response) |
Sam Protsenko | efeccfe | 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; |
Safae Ouajih | d71a732 | 2023-02-06 00:50:03 +0100 | [diff] [blame] | 299 | hdr_sectors = DIV_ROUND_UP(sizeof(struct andr_boot_img_hdr_v0), sector_size); |
Sam Protsenko | efeccfe | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 300 | if (hdr_sectors == 0) { |
Alex Kiernan | 1ad5fac | 2018-05-29 15:30:43 +0000 | [diff] [blame] | 301 | pr_err("invalid number of boot sectors: 0\n"); |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 302 | fastboot_fail("invalid number of boot sectors: 0", response); |
Sam Protsenko | efeccfe | 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 | 6bafa5a | 2017-08-14 21:00:44 -0400 | [diff] [blame] | 308 | if (res != hdr_sectors) { |
Alex Kiernan | 1ad5fac | 2018-05-29 15:30:43 +0000 | [diff] [blame] | 309 | pr_err("cannot read header from boot partition\n"); |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 310 | fastboot_fail("cannot read header from boot partition", |
| 311 | response); |
Sam Protsenko | efeccfe | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | /* Check boot header magic string */ |
Safae Ouajih | 734cb47 | 2023-02-06 00:50:05 +0100 | [diff] [blame] | 316 | if (!is_android_boot_image_header(hdr)) { |
Alex Kiernan | 1ad5fac | 2018-05-29 15:30:43 +0000 | [diff] [blame] | 317 | pr_err("bad boot image magic\n"); |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 318 | fastboot_fail("boot partition not initialized", response); |
Sam Protsenko | efeccfe | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | return hdr_sectors; |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Write downloaded zImage to boot partition and repack it properly. |
| 327 | * |
| 328 | * @param dev_desc MMC device descriptor |
| 329 | * @param download_buffer Address to fastboot buffer with zImage in it |
| 330 | * @param download_bytes Size of fastboot buffer, in bytes |
| 331 | * |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 332 | * Return: 0 on success or -1 on error |
Sam Protsenko | efeccfe | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 333 | */ |
| 334 | static int fb_mmc_update_zimage(struct blk_desc *dev_desc, |
| 335 | void *download_buffer, |
Alex Kiernan | f73a7df | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 336 | u32 download_bytes, |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 337 | char *response) |
Sam Protsenko | efeccfe | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 338 | { |
Tom Rini | 2341c80 | 2017-06-10 09:15:37 -0400 | [diff] [blame] | 339 | uintptr_t hdr_addr; /* boot image header address */ |
Safae Ouajih | d71a732 | 2023-02-06 00:50:03 +0100 | [diff] [blame] | 340 | struct andr_boot_img_hdr_v0 *hdr; /* boot image header */ |
Sam Protsenko | efeccfe | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 341 | lbaint_t hdr_sectors; /* boot image header sectors */ |
| 342 | u8 *ramdisk_buffer; |
| 343 | u32 ramdisk_sector_start; |
| 344 | u32 ramdisk_sectors; |
| 345 | u32 kernel_sector_start; |
| 346 | u32 kernel_sectors; |
| 347 | u32 sectors_per_page; |
Simon Glass | 0528979 | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 348 | struct disk_partition info; |
Sam Protsenko | efeccfe | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 349 | int res; |
| 350 | |
| 351 | puts("Flashing zImage\n"); |
| 352 | |
| 353 | /* Get boot partition info */ |
| 354 | res = part_get_info_by_name(dev_desc, BOOT_PARTITION_NAME, &info); |
| 355 | if (res < 0) { |
Alex Kiernan | 1ad5fac | 2018-05-29 15:30:43 +0000 | [diff] [blame] | 356 | pr_err("cannot find boot partition\n"); |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 357 | fastboot_fail("cannot find boot partition", response); |
Sam Protsenko | efeccfe | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 358 | return -1; |
| 359 | } |
| 360 | |
| 361 | /* Put boot image header in fastboot buffer after downloaded zImage */ |
Tom Rini | 2341c80 | 2017-06-10 09:15:37 -0400 | [diff] [blame] | 362 | hdr_addr = (uintptr_t)download_buffer + ALIGN(download_bytes, PAGE_SIZE); |
Safae Ouajih | d71a732 | 2023-02-06 00:50:03 +0100 | [diff] [blame] | 363 | hdr = (struct andr_boot_img_hdr_v0 *)hdr_addr; |
Sam Protsenko | efeccfe | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 364 | |
| 365 | /* Read boot image header */ |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 366 | hdr_sectors = fb_mmc_get_boot_header(dev_desc, &info, hdr, response); |
Sam Protsenko | efeccfe | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 367 | if (hdr_sectors == 0) { |
Alex Kiernan | 1ad5fac | 2018-05-29 15:30:43 +0000 | [diff] [blame] | 368 | pr_err("unable to read boot image header\n"); |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 369 | fastboot_fail("unable to read boot image header", response); |
Sam Protsenko | efeccfe | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 370 | return -1; |
| 371 | } |
| 372 | |
Safae Ouajih | bc6413b | 2023-02-06 00:50:16 +0100 | [diff] [blame^] | 373 | /* Check if boot image header version is 2 or less */ |
| 374 | if (hdr->header_version > 2) { |
| 375 | pr_err("zImage flashing supported only for boot images v2 and less\n"); |
| 376 | fastboot_fail("zImage flashing supported only for boot images v2 and less", |
| 377 | response); |
| 378 | return -EOPNOTSUPP; |
| 379 | } |
| 380 | |
Sam Protsenko | efeccfe | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 381 | /* Check if boot image has second stage in it (we don't support it) */ |
| 382 | if (hdr->second_size > 0) { |
Alex Kiernan | 1ad5fac | 2018-05-29 15:30:43 +0000 | [diff] [blame] | 383 | pr_err("moving second stage is not supported yet\n"); |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 384 | fastboot_fail("moving second stage is not supported yet", |
| 385 | response); |
Sam Protsenko | efeccfe | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 386 | return -1; |
| 387 | } |
| 388 | |
| 389 | /* Extract ramdisk location */ |
| 390 | sectors_per_page = hdr->page_size / info.blksz; |
| 391 | ramdisk_sector_start = info.start + sectors_per_page; |
| 392 | ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) * |
| 393 | sectors_per_page; |
| 394 | ramdisk_sectors = DIV_ROUND_UP(hdr->ramdisk_size, hdr->page_size) * |
| 395 | sectors_per_page; |
| 396 | |
| 397 | /* Read ramdisk and put it in fastboot buffer after boot image header */ |
| 398 | ramdisk_buffer = (u8 *)hdr + (hdr_sectors * info.blksz); |
| 399 | res = blk_dread(dev_desc, ramdisk_sector_start, ramdisk_sectors, |
| 400 | ramdisk_buffer); |
Tom Rini | 6bafa5a | 2017-08-14 21:00:44 -0400 | [diff] [blame] | 401 | if (res != ramdisk_sectors) { |
Alex Kiernan | 1ad5fac | 2018-05-29 15:30:43 +0000 | [diff] [blame] | 402 | pr_err("cannot read ramdisk from boot partition\n"); |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 403 | fastboot_fail("cannot read ramdisk from boot partition", |
| 404 | response); |
Sam Protsenko | efeccfe | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 405 | return -1; |
| 406 | } |
| 407 | |
| 408 | /* Write new kernel size to boot image header */ |
| 409 | hdr->kernel_size = download_bytes; |
| 410 | res = blk_dwrite(dev_desc, info.start, hdr_sectors, (void *)hdr); |
| 411 | if (res == 0) { |
Alex Kiernan | 1ad5fac | 2018-05-29 15:30:43 +0000 | [diff] [blame] | 412 | pr_err("cannot writeback boot image header\n"); |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 413 | fastboot_fail("cannot write back boot image header", response); |
Sam Protsenko | efeccfe | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 414 | return -1; |
| 415 | } |
| 416 | |
| 417 | /* Write the new downloaded kernel */ |
| 418 | kernel_sector_start = info.start + sectors_per_page; |
| 419 | kernel_sectors = DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) * |
| 420 | sectors_per_page; |
| 421 | res = blk_dwrite(dev_desc, kernel_sector_start, kernel_sectors, |
| 422 | download_buffer); |
| 423 | if (res == 0) { |
Alex Kiernan | 1ad5fac | 2018-05-29 15:30:43 +0000 | [diff] [blame] | 424 | pr_err("cannot write new kernel\n"); |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 425 | fastboot_fail("cannot write new kernel", response); |
Sam Protsenko | efeccfe | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 426 | return -1; |
| 427 | } |
| 428 | |
| 429 | /* Write the saved ramdisk back */ |
| 430 | ramdisk_sector_start = info.start + sectors_per_page; |
| 431 | ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) * |
| 432 | sectors_per_page; |
| 433 | res = blk_dwrite(dev_desc, ramdisk_sector_start, ramdisk_sectors, |
| 434 | ramdisk_buffer); |
| 435 | if (res == 0) { |
Alex Kiernan | 1ad5fac | 2018-05-29 15:30:43 +0000 | [diff] [blame] | 436 | pr_err("cannot write back original ramdisk\n"); |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 437 | fastboot_fail("cannot write back original ramdisk", response); |
Sam Protsenko | efeccfe | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 438 | return -1; |
| 439 | } |
| 440 | |
| 441 | puts("........ zImage was updated in boot partition\n"); |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 442 | fastboot_okay(NULL, response); |
Sam Protsenko | efeccfe | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 443 | return 0; |
| 444 | } |
| 445 | #endif |
| 446 | |
Alex Kiernan | d1a119d | 2018-05-29 15:30:48 +0000 | [diff] [blame] | 447 | /** |
Alex Kiernan | f73a7df | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 448 | * fastboot_mmc_get_part_info() - Lookup eMMC partion by name |
| 449 | * |
| 450 | * @part_name: Named partition to lookup |
| 451 | * @dev_desc: Pointer to returned blk_desc pointer |
Simon Glass | 0528979 | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 452 | * @part_info: Pointer to returned struct disk_partition |
Alex Kiernan | f73a7df | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 453 | * @response: Pointer to fastboot response buffer |
| 454 | */ |
Sam Protsenko | cacb03e | 2019-06-13 21:11:07 +0300 | [diff] [blame] | 455 | int fastboot_mmc_get_part_info(const char *part_name, |
| 456 | struct blk_desc **dev_desc, |
Simon Glass | 0528979 | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 457 | struct disk_partition *part_info, char *response) |
Alex Kiernan | f73a7df | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 458 | { |
Sean Anderson | de1728c | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 459 | int ret; |
Alex Kiernan | f73a7df | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 460 | |
Eugeniu Rosca | a40297d | 2019-03-28 14:31:33 +0100 | [diff] [blame] | 461 | if (!part_name || !strcmp(part_name, "")) { |
| 462 | fastboot_fail("partition not given", response); |
Alex Kiernan | f73a7df | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 463 | return -ENOENT; |
| 464 | } |
| 465 | |
Sean Anderson | de1728c | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 466 | ret = part_get_info_by_name_or_alias(dev_desc, part_name, part_info); |
| 467 | if (ret < 0) { |
| 468 | switch (ret) { |
| 469 | case -ENOSYS: |
| 470 | case -EINVAL: |
| 471 | fastboot_fail("invalid partition or device", response); |
| 472 | break; |
| 473 | case -ENODEV: |
| 474 | fastboot_fail("no such device", response); |
| 475 | break; |
| 476 | case -ENOENT: |
| 477 | fastboot_fail("no such partition", response); |
| 478 | break; |
| 479 | case -EPROTONOSUPPORT: |
| 480 | fastboot_fail("unknown partition table type", response); |
| 481 | break; |
| 482 | default: |
| 483 | fastboot_fail("unanticipated error", response); |
| 484 | break; |
Filip Brozovic | a17c0cb | 2020-06-29 13:14:37 +0200 | [diff] [blame] | 485 | } |
Alex Kiernan | f73a7df | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Sean Anderson | de1728c | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 488 | return ret; |
| 489 | } |
| 490 | |
| 491 | static struct blk_desc *fastboot_mmc_get_dev(char *response) |
| 492 | { |
| 493 | struct blk_desc *ret = blk_get_dev("mmc", |
| 494 | CONFIG_FASTBOOT_FLASH_MMC_DEV); |
| 495 | |
| 496 | if (!ret || ret->type == DEV_TYPE_UNKNOWN) { |
| 497 | pr_err("invalid mmc device\n"); |
| 498 | fastboot_fail("invalid mmc device", response); |
| 499 | return NULL; |
| 500 | } |
| 501 | return ret; |
Alex Kiernan | f73a7df | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | /** |
Alex Kiernan | d1a119d | 2018-05-29 15:30:48 +0000 | [diff] [blame] | 505 | * fastboot_mmc_flash_write() - Write image to eMMC for fastboot |
| 506 | * |
| 507 | * @cmd: Named partition to write image to |
| 508 | * @download_buffer: Pointer to image data |
| 509 | * @download_bytes: Size of image data |
| 510 | * @response: Pointer to fastboot response buffer |
| 511 | */ |
| 512 | void fastboot_mmc_flash_write(const char *cmd, void *download_buffer, |
Alex Kiernan | f73a7df | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 513 | u32 download_bytes, char *response) |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 514 | { |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 515 | struct blk_desc *dev_desc; |
Oleh Kravchenko | 55a202f | 2021-05-19 13:31:31 +0300 | [diff] [blame] | 516 | struct disk_partition info = {0}; |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 517 | |
Patrick Delaunay | 3acbc7b | 2021-01-27 14:46:47 +0100 | [diff] [blame] | 518 | #ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT |
mingming lee | 1fdbad0 | 2020-01-16 16:11:42 +0800 | [diff] [blame] | 519 | if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) { |
Sean Anderson | de1728c | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 520 | dev_desc = fastboot_mmc_get_dev(response); |
| 521 | if (dev_desc) |
| 522 | fb_mmc_boot_ops(dev_desc, download_buffer, 1, |
| 523 | download_bytes, response); |
Patrick Delaunay | 3acbc7b | 2021-01-27 14:46:47 +0100 | [diff] [blame] | 524 | return; |
| 525 | } |
| 526 | if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT2_NAME) == 0) { |
Sean Anderson | de1728c | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 527 | dev_desc = fastboot_mmc_get_dev(response); |
| 528 | if (dev_desc) |
Oleh Kravchenko | 389b676 | 2021-05-15 00:06:21 +0300 | [diff] [blame] | 529 | fb_mmc_boot_ops(dev_desc, download_buffer, 2, |
Sean Anderson | de1728c | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 530 | download_bytes, response); |
mingming lee | 1fdbad0 | 2020-01-16 16:11:42 +0800 | [diff] [blame] | 531 | return; |
| 532 | } |
| 533 | #endif |
| 534 | |
Patrick Delaunay | bd42a94 | 2017-01-27 11:00:41 +0100 | [diff] [blame] | 535 | #if CONFIG_IS_ENABLED(EFI_PARTITION) |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 536 | if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) { |
Sean Anderson | de1728c | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 537 | dev_desc = fastboot_mmc_get_dev(response); |
| 538 | if (!dev_desc) |
| 539 | return; |
| 540 | |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 541 | printf("%s: updating MBR, Primary and Backup GPT(s)\n", |
| 542 | __func__); |
| 543 | if (is_valid_gpt_buf(dev_desc, download_buffer)) { |
| 544 | printf("%s: invalid GPT - refusing to write to flash\n", |
| 545 | __func__); |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 546 | fastboot_fail("invalid GPT partition", response); |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 547 | return; |
| 548 | } |
| 549 | if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) { |
| 550 | printf("%s: writing GPT partitions failed\n", __func__); |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 551 | fastboot_fail("writing GPT partitions failed", |
| 552 | response); |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 553 | return; |
| 554 | } |
Roman Stratiienko | adb5daf | 2021-01-27 17:40:16 +0200 | [diff] [blame] | 555 | part_init(dev_desc); |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 556 | printf("........ success\n"); |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 557 | fastboot_okay(NULL, response); |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 558 | return; |
Petr Kulhavy | b6dd69a | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 559 | } |
| 560 | #endif |
| 561 | |
Patrick Delaunay | b0cf733 | 2017-01-27 11:00:37 +0100 | [diff] [blame] | 562 | #if CONFIG_IS_ENABLED(DOS_PARTITION) |
Petr Kulhavy | b6dd69a | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 563 | if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) { |
Sean Anderson | de1728c | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 564 | dev_desc = fastboot_mmc_get_dev(response); |
| 565 | if (!dev_desc) |
| 566 | return; |
| 567 | |
Petr Kulhavy | b6dd69a | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 568 | printf("%s: updating MBR\n", __func__); |
| 569 | if (is_valid_dos_buf(download_buffer)) { |
| 570 | printf("%s: invalid MBR - refusing to write to flash\n", |
| 571 | __func__); |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 572 | fastboot_fail("invalid MBR partition", response); |
Petr Kulhavy | b6dd69a | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 573 | return; |
| 574 | } |
Marek Szyprowski | 92f1c89 | 2020-12-23 13:55:10 +0100 | [diff] [blame] | 575 | if (write_mbr_sector(dev_desc, download_buffer)) { |
Petr Kulhavy | b6dd69a | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 576 | printf("%s: writing MBR partition failed\n", __func__); |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 577 | fastboot_fail("writing MBR partition failed", |
| 578 | response); |
Petr Kulhavy | b6dd69a | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 579 | return; |
| 580 | } |
Roman Stratiienko | adb5daf | 2021-01-27 17:40:16 +0200 | [diff] [blame] | 581 | part_init(dev_desc); |
Petr Kulhavy | b6dd69a | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 582 | printf("........ success\n"); |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 583 | fastboot_okay(NULL, response); |
Petr Kulhavy | b6dd69a | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 584 | return; |
| 585 | } |
| 586 | #endif |
| 587 | |
Sam Protsenko | efeccfe | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 588 | #ifdef CONFIG_ANDROID_BOOT_IMAGE |
| 589 | if (strncasecmp(cmd, "zimage", 6) == 0) { |
Sean Anderson | de1728c | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 590 | dev_desc = fastboot_mmc_get_dev(response); |
| 591 | if (dev_desc) |
| 592 | fb_mmc_update_zimage(dev_desc, download_buffer, |
| 593 | download_bytes, response); |
Sam Protsenko | efeccfe | 2017-05-18 15:04:59 +0300 | [diff] [blame] | 594 | return; |
| 595 | } |
| 596 | #endif |
| 597 | |
Simon Glass | 0d9da97 | 2023-02-05 15:39:56 -0700 | [diff] [blame] | 598 | #if IS_ENABLED(CONFIG_FASTBOOT_MMC_USER_SUPPORT) |
Oleh Kravchenko | 55a202f | 2021-05-19 13:31:31 +0300 | [diff] [blame] | 599 | if (strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) { |
| 600 | dev_desc = fastboot_mmc_get_dev(response); |
| 601 | if (!dev_desc) |
| 602 | return; |
| 603 | |
| 604 | strlcpy((char *)&info.name, cmd, sizeof(info.name)); |
| 605 | info.size = dev_desc->lba; |
| 606 | info.blksz = dev_desc->blksz; |
| 607 | } |
| 608 | #endif |
| 609 | |
| 610 | if (!info.name[0] && |
| 611 | fastboot_mmc_get_part_info(cmd, &dev_desc, &info, response) < 0) |
Sean Anderson | de1728c | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 612 | return; |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 613 | |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 614 | if (is_sparse_image(download_buffer)) { |
| 615 | struct fb_mmc_sparse sparse_priv; |
Steve Rae | cc0f08cd | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 616 | struct sparse_storage sparse; |
Jassi Brar | 2f83f21 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 617 | int err; |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 618 | |
| 619 | sparse_priv.dev_desc = dev_desc; |
| 620 | |
Steve Rae | cc0f08cd | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 621 | sparse.blksz = info.blksz; |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 622 | sparse.start = info.start; |
| 623 | sparse.size = info.size; |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 624 | sparse.write = fb_mmc_sparse_write; |
Steve Rae | 2c72404 | 2016-06-07 11:19:38 -0700 | [diff] [blame] | 625 | sparse.reserve = fb_mmc_sparse_reserve; |
Jassi Brar | 2f83f21 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 626 | sparse.mssg = fastboot_fail; |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 627 | |
| 628 | printf("Flashing sparse image at offset " LBAFU "\n", |
Steve Rae | cc0f08cd | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 629 | sparse.start); |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 630 | |
Steve Rae | cc0f08cd | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 631 | sparse.priv = &sparse_priv; |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 632 | err = write_sparse_image(&sparse, cmd, download_buffer, |
| 633 | response); |
Jassi Brar | 2f83f21 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 634 | if (!err) |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 635 | fastboot_okay(NULL, response); |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 636 | } else { |
Steve Rae | e5bf987 | 2014-08-26 11:47:30 -0700 | [diff] [blame] | 637 | write_raw_image(dev_desc, &info, cmd, download_buffer, |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 638 | download_bytes, response); |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 639 | } |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 640 | } |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 641 | |
Alex Kiernan | d1a119d | 2018-05-29 15:30:48 +0000 | [diff] [blame] | 642 | /** |
| 643 | * fastboot_mmc_flash_erase() - Erase eMMC for fastboot |
| 644 | * |
| 645 | * @cmd: Named partition to erase |
| 646 | * @response: Pointer to fastboot response buffer |
| 647 | */ |
| 648 | void fastboot_mmc_erase(const char *cmd, char *response) |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 649 | { |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 650 | struct blk_desc *dev_desc; |
Simon Glass | 0528979 | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 651 | struct disk_partition info; |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 652 | lbaint_t blks, blks_start, blks_size, grp_size; |
| 653 | struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV); |
| 654 | |
Patrick Delaunay | 3acbc7b | 2021-01-27 14:46:47 +0100 | [diff] [blame] | 655 | #ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT |
mingming lee | 1fdbad0 | 2020-01-16 16:11:42 +0800 | [diff] [blame] | 656 | if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) { |
| 657 | /* erase EMMC boot1 */ |
Sean Anderson | de1728c | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 658 | dev_desc = fastboot_mmc_get_dev(response); |
| 659 | if (dev_desc) |
| 660 | fb_mmc_boot_ops(dev_desc, NULL, 1, 0, response); |
Patrick Delaunay | 3acbc7b | 2021-01-27 14:46:47 +0100 | [diff] [blame] | 661 | return; |
| 662 | } |
| 663 | if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT2_NAME) == 0) { |
| 664 | /* erase EMMC boot2 */ |
Sean Anderson | de1728c | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 665 | dev_desc = fastboot_mmc_get_dev(response); |
| 666 | if (dev_desc) |
Oleh Kravchenko | 389b676 | 2021-05-15 00:06:21 +0300 | [diff] [blame] | 667 | fb_mmc_boot_ops(dev_desc, NULL, 2, 0, response); |
mingming lee | 1fdbad0 | 2020-01-16 16:11:42 +0800 | [diff] [blame] | 668 | return; |
| 669 | } |
| 670 | #endif |
| 671 | |
Patrick Delaunay | 7596696 | 2021-01-27 14:46:46 +0100 | [diff] [blame] | 672 | #ifdef CONFIG_FASTBOOT_MMC_USER_SUPPORT |
mingming lee | 1fdbad0 | 2020-01-16 16:11:42 +0800 | [diff] [blame] | 673 | if (strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) { |
| 674 | /* erase EMMC userdata */ |
Sean Anderson | de1728c | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 675 | dev_desc = fastboot_mmc_get_dev(response); |
| 676 | if (!dev_desc) |
| 677 | return; |
| 678 | |
mingming lee | 1fdbad0 | 2020-01-16 16:11:42 +0800 | [diff] [blame] | 679 | if (fb_mmc_erase_mmc_hwpart(dev_desc)) |
| 680 | fastboot_fail("Failed to erase EMMC_USER", response); |
| 681 | else |
| 682 | fastboot_okay(NULL, response); |
| 683 | return; |
| 684 | } |
| 685 | #endif |
| 686 | |
Sean Anderson | de1728c | 2021-02-05 09:39:00 -0500 | [diff] [blame] | 687 | if (fastboot_mmc_get_part_info(cmd, &dev_desc, &info, response) < 0) |
| 688 | return; |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 689 | |
| 690 | /* Align blocks to erase group size to avoid erasing other partitions */ |
| 691 | grp_size = mmc->erase_grp_size; |
| 692 | blks_start = (info.start + grp_size - 1) & ~(grp_size - 1); |
| 693 | if (info.size >= grp_size) |
| 694 | blks_size = (info.size - (blks_start - info.start)) & |
| 695 | (~(grp_size - 1)); |
| 696 | else |
| 697 | blks_size = 0; |
| 698 | |
| 699 | printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n", |
| 700 | blks_start, blks_start + blks_size); |
| 701 | |
Alex Kiernan | f73a7df | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 702 | blks = fb_mmc_blk_write(dev_desc, blks_start, blks_size, NULL); |
| 703 | |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 704 | if (blks != blks_size) { |
Alex Kiernan | 1ad5fac | 2018-05-29 15:30:43 +0000 | [diff] [blame] | 705 | pr_err("failed erasing from device %d\n", dev_desc->devnum); |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 706 | fastboot_fail("failed erasing from device", response); |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 707 | return; |
| 708 | } |
| 709 | |
| 710 | printf("........ erased " LBAFU " bytes from '%s'\n", |
| 711 | blks_size * info.blksz, cmd); |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 712 | fastboot_okay(NULL, response); |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 713 | } |