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