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