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> |
| 9 | #include <fb_mmc.h> |
| 10 | #include <part.h> |
Steve Rae | c601567 | 2014-08-26 11:47:30 -0700 | [diff] [blame] | 11 | #include <aboot.h> |
| 12 | #include <sparse_format.h> |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 13 | |
Steve Rae | 7d05934 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 14 | #ifndef CONFIG_FASTBOOT_GPT_NAME |
| 15 | #define CONFIG_FASTBOOT_GPT_NAME GPT_ENTRY_NAME |
| 16 | #endif |
| 17 | |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 18 | /* The 64 defined bytes plus the '\0' */ |
| 19 | #define RESPONSE_LEN (64 + 1) |
| 20 | |
| 21 | static char *response_str; |
| 22 | |
Steve Rae | c601567 | 2014-08-26 11:47:30 -0700 | [diff] [blame] | 23 | void fastboot_fail(const char *s) |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 24 | { |
Steve Rae | c601567 | 2014-08-26 11:47:30 -0700 | [diff] [blame] | 25 | strncpy(response_str, "FAIL", 4); |
| 26 | strncat(response_str, s, RESPONSE_LEN - 4 - 1); |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Steve Rae | c601567 | 2014-08-26 11:47:30 -0700 | [diff] [blame] | 29 | void fastboot_okay(const char *s) |
| 30 | { |
| 31 | strncpy(response_str, "OKAY", 4); |
| 32 | strncat(response_str, s, RESPONSE_LEN - 4 - 1); |
| 33 | } |
| 34 | |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 35 | static void write_raw_image(block_dev_desc_t *dev_desc, disk_partition_t *info, |
| 36 | const char *part_name, void *buffer, |
| 37 | unsigned int download_bytes) |
| 38 | { |
| 39 | lbaint_t blkcnt; |
| 40 | lbaint_t blks; |
| 41 | |
| 42 | /* determine number of blocks to write */ |
| 43 | blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1)); |
| 44 | blkcnt = blkcnt / info->blksz; |
| 45 | |
| 46 | if (blkcnt > info->size) { |
| 47 | error("too large for partition: '%s'\n", part_name); |
Steve Rae | c601567 | 2014-08-26 11:47:30 -0700 | [diff] [blame] | 48 | fastboot_fail("too large for partition"); |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 49 | return; |
| 50 | } |
| 51 | |
| 52 | puts("Flashing Raw Image\n"); |
| 53 | |
| 54 | blks = dev_desc->block_write(dev_desc->dev, info->start, blkcnt, |
| 55 | buffer); |
| 56 | if (blks != blkcnt) { |
| 57 | error("failed writing to device %d\n", dev_desc->dev); |
Steve Rae | c601567 | 2014-08-26 11:47:30 -0700 | [diff] [blame] | 58 | fastboot_fail("failed writing to device"); |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 59 | return; |
| 60 | } |
| 61 | |
| 62 | printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz, |
| 63 | part_name); |
Steve Rae | c601567 | 2014-08-26 11:47:30 -0700 | [diff] [blame] | 64 | fastboot_okay(""); |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void fb_mmc_flash_write(const char *cmd, void *download_buffer, |
| 68 | unsigned int download_bytes, char *response) |
| 69 | { |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 70 | block_dev_desc_t *dev_desc; |
| 71 | disk_partition_t info; |
| 72 | |
| 73 | /* initialize the response buffer */ |
| 74 | response_str = response; |
| 75 | |
| 76 | dev_desc = get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV); |
| 77 | if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) { |
| 78 | error("invalid mmc device\n"); |
Steve Rae | c601567 | 2014-08-26 11:47:30 -0700 | [diff] [blame] | 79 | fastboot_fail("invalid mmc device"); |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 80 | return; |
| 81 | } |
| 82 | |
Steve Rae | 7d05934 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 83 | if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) { |
| 84 | printf("%s: updating MBR, Primary and Backup GPT(s)\n", |
| 85 | __func__); |
| 86 | if (is_valid_gpt_buf(dev_desc, download_buffer)) { |
| 87 | printf("%s: invalid GPT - refusing to write to flash\n", |
| 88 | __func__); |
| 89 | fastboot_fail("invalid GPT partition"); |
| 90 | return; |
| 91 | } |
| 92 | if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) { |
| 93 | printf("%s: writing GPT partitions failed\n", __func__); |
| 94 | fastboot_fail("writing GPT partitions failed"); |
| 95 | return; |
| 96 | } |
| 97 | printf("........ success\n"); |
| 98 | fastboot_okay(""); |
| 99 | return; |
| 100 | } else if (get_partition_info_efi_by_name(dev_desc, cmd, &info)) { |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 101 | error("cannot find partition: '%s'\n", cmd); |
Steve Rae | c601567 | 2014-08-26 11:47:30 -0700 | [diff] [blame] | 102 | fastboot_fail("cannot find partition"); |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 103 | return; |
| 104 | } |
| 105 | |
Steve Rae | c601567 | 2014-08-26 11:47:30 -0700 | [diff] [blame] | 106 | if (is_sparse_image(download_buffer)) |
| 107 | write_sparse_image(dev_desc, &info, cmd, download_buffer, |
| 108 | download_bytes); |
| 109 | else |
| 110 | write_raw_image(dev_desc, &info, cmd, download_buffer, |
| 111 | download_bytes); |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 112 | } |