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