Steve Rae | c0aebb3 | 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 | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 7 | #include <config.h> |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 8 | #include <common.h> |
| 9 | #include <fb_mmc.h> |
| 10 | #include <part.h> |
Steve Rae | e5bf987 | 2014-08-26 11:47:30 -0700 | [diff] [blame] | 11 | #include <aboot.h> |
| 12 | #include <sparse_format.h> |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 13 | #include <mmc.h> |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 14 | |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 15 | #ifndef CONFIG_FASTBOOT_GPT_NAME |
| 16 | #define CONFIG_FASTBOOT_GPT_NAME GPT_ENTRY_NAME |
| 17 | #endif |
| 18 | |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 19 | /* The 64 defined bytes plus the '\0' */ |
| 20 | #define RESPONSE_LEN (64 + 1) |
| 21 | |
| 22 | static char *response_str; |
| 23 | |
Steve Rae | e5bf987 | 2014-08-26 11:47:30 -0700 | [diff] [blame] | 24 | void fastboot_fail(const char *s) |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 25 | { |
Dileep Katta | e874207 | 2015-02-13 14:33:42 +0800 | [diff] [blame] | 26 | strncpy(response_str, "FAIL\0", 5); |
Steve Rae | e5bf987 | 2014-08-26 11:47:30 -0700 | [diff] [blame] | 27 | strncat(response_str, s, RESPONSE_LEN - 4 - 1); |
| 28 | } |
| 29 | |
| 30 | void fastboot_okay(const char *s) |
| 31 | { |
Dileep Katta | e874207 | 2015-02-13 14:33:42 +0800 | [diff] [blame] | 32 | strncpy(response_str, "OKAY\0", 5); |
Steve Rae | e5bf987 | 2014-08-26 11:47:30 -0700 | [diff] [blame] | 33 | strncat(response_str, s, RESPONSE_LEN - 4 - 1); |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Michael Scott | 8a41802 | 2015-03-11 10:02:31 -0700 | [diff] [blame] | 36 | static int get_partition_info_efi_by_name_or_alias(block_dev_desc_t *dev_desc, |
| 37 | const char *name, disk_partition_t *info) |
| 38 | { |
| 39 | int ret; |
| 40 | |
| 41 | ret = get_partition_info_efi_by_name(dev_desc, name, info); |
| 42 | if (ret) { |
| 43 | /* strlen("fastboot_partition_alias_") + 32(part_name) + 1 */ |
| 44 | char env_alias_name[25 + 32 + 1]; |
| 45 | char *aliased_part_name; |
| 46 | |
| 47 | /* check for alias */ |
| 48 | strcpy(env_alias_name, "fastboot_partition_alias_"); |
| 49 | strncat(env_alias_name, name, 32); |
| 50 | aliased_part_name = getenv(env_alias_name); |
| 51 | if (aliased_part_name != NULL) |
| 52 | ret = get_partition_info_efi_by_name(dev_desc, |
| 53 | aliased_part_name, info); |
| 54 | } |
| 55 | return ret; |
| 56 | } |
| 57 | |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 58 | static void write_raw_image(block_dev_desc_t *dev_desc, disk_partition_t *info, |
| 59 | const char *part_name, void *buffer, |
| 60 | unsigned int download_bytes) |
| 61 | { |
| 62 | lbaint_t blkcnt; |
| 63 | lbaint_t blks; |
| 64 | |
| 65 | /* determine number of blocks to write */ |
| 66 | blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1)); |
| 67 | blkcnt = blkcnt / info->blksz; |
| 68 | |
| 69 | if (blkcnt > info->size) { |
| 70 | error("too large for partition: '%s'\n", part_name); |
Steve Rae | e5bf987 | 2014-08-26 11:47:30 -0700 | [diff] [blame] | 71 | fastboot_fail("too large for partition"); |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 72 | return; |
| 73 | } |
| 74 | |
| 75 | puts("Flashing Raw Image\n"); |
| 76 | |
| 77 | blks = dev_desc->block_write(dev_desc->dev, info->start, blkcnt, |
| 78 | buffer); |
| 79 | if (blks != blkcnt) { |
| 80 | error("failed writing to device %d\n", dev_desc->dev); |
Steve Rae | e5bf987 | 2014-08-26 11:47:30 -0700 | [diff] [blame] | 81 | fastboot_fail("failed writing to device"); |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 82 | return; |
| 83 | } |
| 84 | |
| 85 | printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz, |
| 86 | part_name); |
Steve Rae | e5bf987 | 2014-08-26 11:47:30 -0700 | [diff] [blame] | 87 | fastboot_okay(""); |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void fb_mmc_flash_write(const char *cmd, void *download_buffer, |
| 91 | unsigned int download_bytes, char *response) |
| 92 | { |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 93 | block_dev_desc_t *dev_desc; |
| 94 | disk_partition_t info; |
| 95 | |
| 96 | /* initialize the response buffer */ |
| 97 | response_str = response; |
| 98 | |
| 99 | dev_desc = get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV); |
| 100 | if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) { |
| 101 | error("invalid mmc device\n"); |
Steve Rae | e5bf987 | 2014-08-26 11:47:30 -0700 | [diff] [blame] | 102 | fastboot_fail("invalid mmc device"); |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 103 | return; |
| 104 | } |
| 105 | |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 106 | if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) { |
| 107 | printf("%s: updating MBR, Primary and Backup GPT(s)\n", |
| 108 | __func__); |
| 109 | if (is_valid_gpt_buf(dev_desc, download_buffer)) { |
| 110 | printf("%s: invalid GPT - refusing to write to flash\n", |
| 111 | __func__); |
| 112 | fastboot_fail("invalid GPT partition"); |
| 113 | return; |
| 114 | } |
| 115 | if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) { |
| 116 | printf("%s: writing GPT partitions failed\n", __func__); |
| 117 | fastboot_fail("writing GPT partitions failed"); |
| 118 | return; |
| 119 | } |
| 120 | printf("........ success\n"); |
| 121 | fastboot_okay(""); |
| 122 | return; |
Michael Scott | 8a41802 | 2015-03-11 10:02:31 -0700 | [diff] [blame] | 123 | } else if (get_partition_info_efi_by_name_or_alias(dev_desc, cmd, &info)) { |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 124 | error("cannot find partition: '%s'\n", cmd); |
Steve Rae | e5bf987 | 2014-08-26 11:47:30 -0700 | [diff] [blame] | 125 | fastboot_fail("cannot find partition"); |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 126 | return; |
| 127 | } |
| 128 | |
Steve Rae | e5bf987 | 2014-08-26 11:47:30 -0700 | [diff] [blame] | 129 | if (is_sparse_image(download_buffer)) |
| 130 | write_sparse_image(dev_desc, &info, cmd, download_buffer, |
| 131 | download_bytes); |
| 132 | else |
| 133 | write_raw_image(dev_desc, &info, cmd, download_buffer, |
| 134 | download_bytes); |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 135 | } |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 136 | |
| 137 | void fb_mmc_erase(const char *cmd, char *response) |
| 138 | { |
| 139 | int ret; |
| 140 | block_dev_desc_t *dev_desc; |
| 141 | disk_partition_t info; |
| 142 | lbaint_t blks, blks_start, blks_size, grp_size; |
| 143 | struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV); |
| 144 | |
| 145 | if (mmc == NULL) { |
| 146 | error("invalid mmc device"); |
| 147 | fastboot_fail("invalid mmc device"); |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | /* initialize the response buffer */ |
| 152 | response_str = response; |
| 153 | |
| 154 | dev_desc = get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV); |
| 155 | if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) { |
| 156 | error("invalid mmc device"); |
| 157 | fastboot_fail("invalid mmc device"); |
| 158 | return; |
| 159 | } |
| 160 | |
Michael Scott | 8a41802 | 2015-03-11 10:02:31 -0700 | [diff] [blame] | 161 | ret = get_partition_info_efi_by_name_or_alias(dev_desc, cmd, &info); |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 162 | if (ret) { |
| 163 | error("cannot find partition: '%s'", cmd); |
| 164 | fastboot_fail("cannot find partition"); |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | /* Align blocks to erase group size to avoid erasing other partitions */ |
| 169 | grp_size = mmc->erase_grp_size; |
| 170 | blks_start = (info.start + grp_size - 1) & ~(grp_size - 1); |
| 171 | if (info.size >= grp_size) |
| 172 | blks_size = (info.size - (blks_start - info.start)) & |
| 173 | (~(grp_size - 1)); |
| 174 | else |
| 175 | blks_size = 0; |
| 176 | |
| 177 | printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n", |
| 178 | blks_start, blks_start + blks_size); |
| 179 | |
| 180 | blks = dev_desc->block_erase(dev_desc->dev, blks_start, blks_size); |
| 181 | if (blks != blks_size) { |
| 182 | error("failed erasing from device %d", dev_desc->dev); |
| 183 | fastboot_fail("failed erasing from device"); |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | printf("........ erased " LBAFU " bytes from '%s'\n", |
| 188 | blks_size * info.blksz, cmd); |
| 189 | fastboot_okay(""); |
| 190 | } |