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