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