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