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