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> |
Simon Glass | 2a981dc | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 9 | #include <blk.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> |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 14 | #include <mmc.h> |
Siarhei Siamashka | 5e0efc1 | 2015-10-28 06:24:16 +0200 | [diff] [blame] | 15 | #include <div64.h> |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 16 | |
Steve Rae | 0ff7e58 | 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 | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 21 | static char *response_str; |
| 22 | |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 23 | struct fb_mmc_sparse { |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 24 | struct blk_desc *dev_desc; |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 25 | }; |
| 26 | |
Simon Glass | 3e8bd46 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 27 | static int part_get_info_efi_by_name_or_alias(struct blk_desc *dev_desc, |
Michael Scott | 8a41802 | 2015-03-11 10:02:31 -0700 | [diff] [blame] | 28 | const char *name, disk_partition_t *info) |
| 29 | { |
| 30 | int ret; |
| 31 | |
Simon Glass | 3e8bd46 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 32 | ret = part_get_info_efi_by_name(dev_desc, name, info); |
Michael Scott | 8a41802 | 2015-03-11 10:02:31 -0700 | [diff] [blame] | 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) |
Simon Glass | 3e8bd46 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 43 | ret = part_get_info_efi_by_name(dev_desc, |
Michael Scott | 8a41802 | 2015-03-11 10:02:31 -0700 | [diff] [blame] | 44 | aliased_part_name, info); |
| 45 | } |
| 46 | return ret; |
| 47 | } |
| 48 | |
Steve Rae | cc0f08c | 2016-06-07 11:19:36 -0700 | [diff] [blame^] | 49 | static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info, |
| 50 | lbaint_t blk, lbaint_t blkcnt, const void *buffer) |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 51 | { |
Steve Rae | cc0f08c | 2016-06-07 11:19:36 -0700 | [diff] [blame^] | 52 | struct fb_mmc_sparse *sparse = info->priv; |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 53 | struct blk_desc *dev_desc = sparse->dev_desc; |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 54 | |
Steve Rae | cc0f08c | 2016-06-07 11:19:36 -0700 | [diff] [blame^] | 55 | return blk_dwrite(dev_desc, blk, blkcnt, buffer); |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 58 | static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info, |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 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)); |
Siarhei Siamashka | 5e0efc1 | 2015-10-28 06:24:16 +0200 | [diff] [blame] | 67 | blkcnt = lldiv(blkcnt, info->blksz); |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 68 | |
| 69 | if (blkcnt > info->size) { |
| 70 | error("too large for partition: '%s'\n", part_name); |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 71 | fastboot_fail(response_str, "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 | |
Simon Glass | 2a981dc | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 77 | blks = blk_dwrite(dev_desc, info->start, blkcnt, buffer); |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 78 | if (blks != blkcnt) { |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 79 | error("failed writing to device %d\n", dev_desc->devnum); |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 80 | fastboot_fail(response_str, "failed writing to device"); |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 81 | return; |
| 82 | } |
| 83 | |
| 84 | printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz, |
| 85 | part_name); |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 86 | fastboot_okay(response_str, ""); |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Steve Rae | 64ece84 | 2016-06-07 11:19:35 -0700 | [diff] [blame] | 89 | void fb_mmc_flash_write(const char *cmd, void *download_buffer, |
| 90 | unsigned int download_bytes, char *response) |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 91 | { |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 92 | struct blk_desc *dev_desc; |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 93 | disk_partition_t info; |
| 94 | |
| 95 | /* initialize the response buffer */ |
| 96 | response_str = response; |
| 97 | |
Simon Glass | db1d9e7 | 2016-02-29 15:25:42 -0700 | [diff] [blame] | 98 | dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV); |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 99 | if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) { |
| 100 | error("invalid mmc device\n"); |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 101 | fastboot_fail(response_str, "invalid mmc device"); |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 102 | return; |
| 103 | } |
| 104 | |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 105 | if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) { |
| 106 | printf("%s: updating MBR, Primary and Backup GPT(s)\n", |
| 107 | __func__); |
| 108 | if (is_valid_gpt_buf(dev_desc, download_buffer)) { |
| 109 | printf("%s: invalid GPT - refusing to write to flash\n", |
| 110 | __func__); |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 111 | fastboot_fail(response_str, "invalid GPT partition"); |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 112 | return; |
| 113 | } |
| 114 | if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) { |
| 115 | printf("%s: writing GPT partitions failed\n", __func__); |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 116 | fastboot_fail(response_str, |
| 117 | "writing GPT partitions failed"); |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 118 | return; |
| 119 | } |
| 120 | printf("........ success\n"); |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 121 | fastboot_okay(response_str, ""); |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 122 | return; |
Simon Glass | 3e8bd46 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 123 | } else if (part_get_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); |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 125 | fastboot_fail(response_str, "cannot find partition"); |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 126 | return; |
| 127 | } |
| 128 | |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 129 | if (is_sparse_image(download_buffer)) { |
| 130 | struct fb_mmc_sparse sparse_priv; |
Steve Rae | cc0f08c | 2016-06-07 11:19:36 -0700 | [diff] [blame^] | 131 | struct sparse_storage sparse; |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 132 | |
| 133 | sparse_priv.dev_desc = dev_desc; |
| 134 | |
Steve Rae | cc0f08c | 2016-06-07 11:19:36 -0700 | [diff] [blame^] | 135 | sparse.blksz = info.blksz; |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 136 | sparse.start = info.start; |
| 137 | sparse.size = info.size; |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 138 | sparse.write = fb_mmc_sparse_write; |
| 139 | |
| 140 | printf("Flashing sparse image at offset " LBAFU "\n", |
Steve Rae | cc0f08c | 2016-06-07 11:19:36 -0700 | [diff] [blame^] | 141 | sparse.start); |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 142 | |
Steve Rae | cc0f08c | 2016-06-07 11:19:36 -0700 | [diff] [blame^] | 143 | sparse.priv = &sparse_priv; |
| 144 | write_sparse_image(&sparse, cmd, download_buffer, |
| 145 | download_bytes, response_str); |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 146 | } else { |
Steve Rae | e5bf987 | 2014-08-26 11:47:30 -0700 | [diff] [blame] | 147 | write_raw_image(dev_desc, &info, cmd, download_buffer, |
| 148 | download_bytes); |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 149 | } |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 150 | } |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 151 | |
| 152 | void fb_mmc_erase(const char *cmd, char *response) |
| 153 | { |
| 154 | int ret; |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 155 | struct blk_desc *dev_desc; |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 156 | disk_partition_t info; |
| 157 | lbaint_t blks, blks_start, blks_size, grp_size; |
| 158 | struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV); |
| 159 | |
| 160 | if (mmc == NULL) { |
| 161 | error("invalid mmc device"); |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 162 | fastboot_fail(response_str, "invalid mmc device"); |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 163 | return; |
| 164 | } |
| 165 | |
| 166 | /* initialize the response buffer */ |
| 167 | response_str = response; |
| 168 | |
Simon Glass | db1d9e7 | 2016-02-29 15:25:42 -0700 | [diff] [blame] | 169 | dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV); |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 170 | if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) { |
| 171 | error("invalid mmc device"); |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 172 | fastboot_fail(response_str, "invalid mmc device"); |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 173 | return; |
| 174 | } |
| 175 | |
Simon Glass | 3e8bd46 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 176 | ret = part_get_info_efi_by_name_or_alias(dev_desc, cmd, &info); |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 177 | if (ret) { |
| 178 | error("cannot find partition: '%s'", cmd); |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 179 | fastboot_fail(response_str, "cannot find partition"); |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 180 | return; |
| 181 | } |
| 182 | |
| 183 | /* Align blocks to erase group size to avoid erasing other partitions */ |
| 184 | grp_size = mmc->erase_grp_size; |
| 185 | blks_start = (info.start + grp_size - 1) & ~(grp_size - 1); |
| 186 | if (info.size >= grp_size) |
| 187 | blks_size = (info.size - (blks_start - info.start)) & |
| 188 | (~(grp_size - 1)); |
| 189 | else |
| 190 | blks_size = 0; |
| 191 | |
| 192 | printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n", |
| 193 | blks_start, blks_start + blks_size); |
| 194 | |
Stephen Warren | 7c4213f | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 195 | blks = dev_desc->block_erase(dev_desc, blks_start, blks_size); |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 196 | if (blks != blks_size) { |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 197 | error("failed erasing from device %d", dev_desc->devnum); |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 198 | fastboot_fail(response_str, "failed erasing from device"); |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 199 | return; |
| 200 | } |
| 201 | |
| 202 | printf("........ erased " LBAFU " bytes from '%s'\n", |
| 203 | blks_size * info.blksz, cmd); |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 204 | fastboot_okay(response_str, ""); |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 205 | } |