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