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