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