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 | |
Petr Kulhavy | b6dd69a | 2016-09-09 10:27:16 +0200 | [diff] [blame^] | 17 | #if defined(CONFIG_EFI_PARTITION) && !defined(CONFIG_FASTBOOT_GPT_NAME) |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 18 | #define CONFIG_FASTBOOT_GPT_NAME GPT_ENTRY_NAME |
| 19 | #endif |
| 20 | |
Petr Kulhavy | b6dd69a | 2016-09-09 10:27:16 +0200 | [diff] [blame^] | 21 | |
| 22 | #if defined(CONFIG_DOS_PARTITION) && !defined(CONFIG_FASTBOOT_MBR_NAME) |
| 23 | #define CONFIG_FASTBOOT_MBR_NAME "mbr" |
| 24 | #endif |
| 25 | |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 26 | struct fb_mmc_sparse { |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 27 | struct blk_desc *dev_desc; |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 28 | }; |
| 29 | |
Petr Kulhavy | b6dd69a | 2016-09-09 10:27:16 +0200 | [diff] [blame^] | 30 | static int part_get_info_by_name_or_alias(struct blk_desc *dev_desc, |
Michael Scott | 8a41802 | 2015-03-11 10:02:31 -0700 | [diff] [blame] | 31 | const char *name, disk_partition_t *info) |
| 32 | { |
| 33 | int ret; |
| 34 | |
Petr Kulhavy | 87b8530 | 2016-09-09 10:27:15 +0200 | [diff] [blame] | 35 | ret = part_get_info_by_name(dev_desc, name, info); |
Michael Scott | 8a41802 | 2015-03-11 10:02:31 -0700 | [diff] [blame] | 36 | if (ret) { |
| 37 | /* strlen("fastboot_partition_alias_") + 32(part_name) + 1 */ |
| 38 | char env_alias_name[25 + 32 + 1]; |
| 39 | char *aliased_part_name; |
| 40 | |
| 41 | /* check for alias */ |
| 42 | strcpy(env_alias_name, "fastboot_partition_alias_"); |
| 43 | strncat(env_alias_name, name, 32); |
| 44 | aliased_part_name = getenv(env_alias_name); |
| 45 | if (aliased_part_name != NULL) |
Petr Kulhavy | 87b8530 | 2016-09-09 10:27:15 +0200 | [diff] [blame] | 46 | ret = part_get_info_by_name(dev_desc, |
Michael Scott | 8a41802 | 2015-03-11 10:02:31 -0700 | [diff] [blame] | 47 | aliased_part_name, info); |
| 48 | } |
| 49 | return ret; |
| 50 | } |
| 51 | |
Steve Rae | cc0f08c | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 52 | static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info, |
| 53 | lbaint_t blk, lbaint_t blkcnt, const void *buffer) |
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 | struct fb_mmc_sparse *sparse = info->priv; |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 56 | struct blk_desc *dev_desc = sparse->dev_desc; |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 57 | |
Steve Rae | cc0f08c | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 58 | return blk_dwrite(dev_desc, blk, blkcnt, buffer); |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Steve Rae | 2c72404 | 2016-06-07 11:19:38 -0700 | [diff] [blame] | 61 | static lbaint_t fb_mmc_sparse_reserve(struct sparse_storage *info, |
| 62 | lbaint_t blk, lbaint_t blkcnt) |
| 63 | { |
| 64 | return blkcnt; |
| 65 | } |
| 66 | |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 67 | 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] | 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)); |
Siarhei Siamashka | 5e0efc1 | 2015-10-28 06:24:16 +0200 | [diff] [blame] | 76 | blkcnt = lldiv(blkcnt, info->blksz); |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 77 | |
| 78 | if (blkcnt > info->size) { |
| 79 | error("too large for partition: '%s'\n", part_name); |
Steve Rae | 9bc3479 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 80 | fastboot_fail("too large for partition"); |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 81 | return; |
| 82 | } |
| 83 | |
| 84 | puts("Flashing Raw Image\n"); |
| 85 | |
Simon Glass | 2a981dc | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 86 | blks = blk_dwrite(dev_desc, info->start, blkcnt, buffer); |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 87 | if (blks != blkcnt) { |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 88 | error("failed writing to device %d\n", dev_desc->devnum); |
Steve Rae | 9bc3479 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 89 | fastboot_fail("failed writing to device"); |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 90 | return; |
| 91 | } |
| 92 | |
| 93 | printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz, |
| 94 | part_name); |
Steve Rae | 9bc3479 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 95 | fastboot_okay(""); |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Steve Rae | 64ece84 | 2016-06-07 11:19:35 -0700 | [diff] [blame] | 98 | void fb_mmc_flash_write(const char *cmd, void *download_buffer, |
Steve Rae | 9bc3479 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 99 | unsigned int download_bytes) |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 100 | { |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 101 | struct blk_desc *dev_desc; |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 102 | disk_partition_t info; |
| 103 | |
Simon Glass | db1d9e7 | 2016-02-29 15:25:42 -0700 | [diff] [blame] | 104 | dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV); |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 105 | if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) { |
| 106 | error("invalid mmc device\n"); |
Steve Rae | 9bc3479 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 107 | fastboot_fail("invalid mmc device"); |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 108 | return; |
| 109 | } |
| 110 | |
Petr Kulhavy | b6dd69a | 2016-09-09 10:27:16 +0200 | [diff] [blame^] | 111 | #ifdef CONFIG_EFI_PARTITION |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 112 | if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) { |
| 113 | printf("%s: updating MBR, Primary and Backup GPT(s)\n", |
| 114 | __func__); |
| 115 | if (is_valid_gpt_buf(dev_desc, download_buffer)) { |
| 116 | printf("%s: invalid GPT - refusing to write to flash\n", |
| 117 | __func__); |
Steve Rae | 9bc3479 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 118 | fastboot_fail("invalid GPT partition"); |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 119 | return; |
| 120 | } |
| 121 | if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) { |
| 122 | printf("%s: writing GPT partitions failed\n", __func__); |
Petr Kulhavy | b6dd69a | 2016-09-09 10:27:16 +0200 | [diff] [blame^] | 123 | fastboot_fail("writing GPT partitions failed"); |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 124 | return; |
| 125 | } |
| 126 | printf("........ success\n"); |
Steve Rae | 9bc3479 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 127 | fastboot_okay(""); |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 128 | return; |
Petr Kulhavy | b6dd69a | 2016-09-09 10:27:16 +0200 | [diff] [blame^] | 129 | } |
| 130 | #endif |
| 131 | |
| 132 | #ifdef CONFIG_DOS_PARTITION |
| 133 | if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) { |
| 134 | printf("%s: updating MBR\n", __func__); |
| 135 | if (is_valid_dos_buf(download_buffer)) { |
| 136 | printf("%s: invalid MBR - refusing to write to flash\n", |
| 137 | __func__); |
| 138 | fastboot_fail("invalid MBR partition"); |
| 139 | return; |
| 140 | } |
| 141 | if (write_mbr_partition(dev_desc, download_buffer)) { |
| 142 | printf("%s: writing MBR partition failed\n", __func__); |
| 143 | fastboot_fail("writing MBR partition failed"); |
| 144 | return; |
| 145 | } |
| 146 | printf("........ success\n"); |
| 147 | fastboot_okay(""); |
| 148 | return; |
| 149 | } |
| 150 | #endif |
| 151 | |
| 152 | if (part_get_info_by_name_or_alias(dev_desc, cmd, &info)) { |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 153 | error("cannot find partition: '%s'\n", cmd); |
Steve Rae | 9bc3479 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 154 | fastboot_fail("cannot find partition"); |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 155 | return; |
| 156 | } |
| 157 | |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 158 | if (is_sparse_image(download_buffer)) { |
| 159 | struct fb_mmc_sparse sparse_priv; |
Steve Rae | cc0f08c | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 160 | struct sparse_storage sparse; |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 161 | |
| 162 | sparse_priv.dev_desc = dev_desc; |
| 163 | |
Steve Rae | cc0f08c | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 164 | sparse.blksz = info.blksz; |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 165 | sparse.start = info.start; |
| 166 | sparse.size = info.size; |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 167 | sparse.write = fb_mmc_sparse_write; |
Steve Rae | 2c72404 | 2016-06-07 11:19:38 -0700 | [diff] [blame] | 168 | sparse.reserve = fb_mmc_sparse_reserve; |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 169 | |
| 170 | printf("Flashing sparse image at offset " LBAFU "\n", |
Steve Rae | cc0f08c | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 171 | sparse.start); |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 172 | |
Steve Rae | cc0f08c | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 173 | sparse.priv = &sparse_priv; |
| 174 | write_sparse_image(&sparse, cmd, download_buffer, |
Steve Rae | 9bc3479 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 175 | download_bytes); |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 176 | } else { |
Steve Rae | e5bf987 | 2014-08-26 11:47:30 -0700 | [diff] [blame] | 177 | write_raw_image(dev_desc, &info, cmd, download_buffer, |
| 178 | download_bytes); |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 179 | } |
Steve Rae | c0aebb3 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 180 | } |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 181 | |
Steve Rae | 9bc3479 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 182 | void fb_mmc_erase(const char *cmd) |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 183 | { |
| 184 | int ret; |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 185 | struct blk_desc *dev_desc; |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 186 | disk_partition_t info; |
| 187 | lbaint_t blks, blks_start, blks_size, grp_size; |
| 188 | struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV); |
| 189 | |
| 190 | if (mmc == NULL) { |
| 191 | error("invalid mmc device"); |
Steve Rae | 9bc3479 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 192 | fastboot_fail("invalid mmc device"); |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 193 | return; |
| 194 | } |
| 195 | |
Simon Glass | db1d9e7 | 2016-02-29 15:25:42 -0700 | [diff] [blame] | 196 | dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV); |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 197 | if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) { |
| 198 | error("invalid mmc device"); |
Steve Rae | 9bc3479 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 199 | fastboot_fail("invalid mmc device"); |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 200 | return; |
| 201 | } |
| 202 | |
Petr Kulhavy | b6dd69a | 2016-09-09 10:27:16 +0200 | [diff] [blame^] | 203 | ret = part_get_info_by_name_or_alias(dev_desc, cmd, &info); |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 204 | if (ret) { |
| 205 | error("cannot find partition: '%s'", cmd); |
Steve Rae | 9bc3479 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 206 | fastboot_fail("cannot find partition"); |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 207 | return; |
| 208 | } |
| 209 | |
| 210 | /* Align blocks to erase group size to avoid erasing other partitions */ |
| 211 | grp_size = mmc->erase_grp_size; |
| 212 | blks_start = (info.start + grp_size - 1) & ~(grp_size - 1); |
| 213 | if (info.size >= grp_size) |
| 214 | blks_size = (info.size - (blks_start - info.start)) & |
| 215 | (~(grp_size - 1)); |
| 216 | else |
| 217 | blks_size = 0; |
| 218 | |
| 219 | printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n", |
| 220 | blks_start, blks_start + blks_size); |
| 221 | |
Xu Ziyuan | ec3cde1 | 2016-06-15 16:56:18 +0800 | [diff] [blame] | 222 | blks = blk_derase(dev_desc, blks_start, blks_size); |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 223 | if (blks != blks_size) { |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 224 | error("failed erasing from device %d", dev_desc->devnum); |
Steve Rae | 9bc3479 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 225 | fastboot_fail("failed erasing from device"); |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 226 | return; |
| 227 | } |
| 228 | |
| 229 | printf("........ erased " LBAFU " bytes from '%s'\n", |
| 230 | blks_size * info.blksz, cmd); |
Steve Rae | 9bc3479 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 231 | fastboot_okay(""); |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 232 | } |