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