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