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 | 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> |
Maxime Ripard | 6af49bc | 2015-10-15 14:34:19 +0200 | [diff] [blame] | 12 | #include <image-sparse.h> |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 13 | #include <part.h> |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 14 | #include <mmc.h> |
Siarhei Siamashka | 58f30d8 | 2015-10-28 06:24:16 +0200 | [diff] [blame] | 15 | #include <div64.h> |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 16 | |
Petr Kulhavy | 4ed1eca | 2016-09-09 10:27:18 +0200 | [diff] [blame] | 17 | /* |
| 18 | * FIXME: Ensure we always set these names via Kconfig once xxx_PARTITION is |
| 19 | * migrated |
| 20 | */ |
| 21 | #ifndef CONFIG_FASTBOOT_GPT_NAME |
| 22 | #define CONFIG_FASTBOOT_GPT_NAME "gpt" |
Steve Rae | 7d05934 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 23 | #endif |
| 24 | |
Petr Kulhavy | 9f174c9 | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 25 | |
Petr Kulhavy | 4ed1eca | 2016-09-09 10:27:18 +0200 | [diff] [blame] | 26 | #ifndef CONFIG_FASTBOOT_MBR_NAME |
Petr Kulhavy | 9f174c9 | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 27 | #define CONFIG_FASTBOOT_MBR_NAME "mbr" |
| 28 | #endif |
| 29 | |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 30 | struct fb_mmc_sparse { |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 31 | struct blk_desc *dev_desc; |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 32 | }; |
| 33 | |
Petr Kulhavy | 9f174c9 | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 34 | static int part_get_info_by_name_or_alias(struct blk_desc *dev_desc, |
Michael Scott | ced3349 | 2015-03-11 10:02:31 -0700 | [diff] [blame] | 35 | const char *name, disk_partition_t *info) |
| 36 | { |
| 37 | int ret; |
| 38 | |
Petr Kulhavy | 712257e | 2016-09-09 10:27:15 +0200 | [diff] [blame] | 39 | ret = part_get_info_by_name(dev_desc, name, info); |
Michael Scott | ced3349 | 2015-03-11 10:02:31 -0700 | [diff] [blame] | 40 | if (ret) { |
| 41 | /* strlen("fastboot_partition_alias_") + 32(part_name) + 1 */ |
| 42 | char env_alias_name[25 + 32 + 1]; |
| 43 | char *aliased_part_name; |
| 44 | |
| 45 | /* check for alias */ |
| 46 | strcpy(env_alias_name, "fastboot_partition_alias_"); |
| 47 | strncat(env_alias_name, name, 32); |
| 48 | aliased_part_name = getenv(env_alias_name); |
| 49 | if (aliased_part_name != NULL) |
Petr Kulhavy | 712257e | 2016-09-09 10:27:15 +0200 | [diff] [blame] | 50 | ret = part_get_info_by_name(dev_desc, |
Michael Scott | ced3349 | 2015-03-11 10:02:31 -0700 | [diff] [blame] | 51 | aliased_part_name, info); |
| 52 | } |
| 53 | return ret; |
| 54 | } |
| 55 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 56 | static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info, |
| 57 | lbaint_t blk, lbaint_t blkcnt, const void *buffer) |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 58 | { |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 59 | struct fb_mmc_sparse *sparse = info->priv; |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 60 | struct blk_desc *dev_desc = sparse->dev_desc; |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 61 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 62 | return blk_dwrite(dev_desc, blk, blkcnt, buffer); |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 63 | } |
| 64 | |
Steve Rae | 8a99be8 | 2016-06-07 11:19:38 -0700 | [diff] [blame] | 65 | static lbaint_t fb_mmc_sparse_reserve(struct sparse_storage *info, |
| 66 | lbaint_t blk, lbaint_t blkcnt) |
| 67 | { |
| 68 | return blkcnt; |
| 69 | } |
| 70 | |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 71 | 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] | 72 | const char *part_name, void *buffer, |
| 73 | unsigned int download_bytes) |
| 74 | { |
| 75 | lbaint_t blkcnt; |
| 76 | lbaint_t blks; |
| 77 | |
| 78 | /* determine number of blocks to write */ |
| 79 | blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1)); |
Siarhei Siamashka | 58f30d8 | 2015-10-28 06:24:16 +0200 | [diff] [blame] | 80 | blkcnt = lldiv(blkcnt, info->blksz); |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 81 | |
| 82 | if (blkcnt > info->size) { |
| 83 | error("too large for partition: '%s'\n", part_name); |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 84 | fastboot_fail("too large for partition"); |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 85 | return; |
| 86 | } |
| 87 | |
| 88 | puts("Flashing Raw Image\n"); |
| 89 | |
Simon Glass | 2ee8ada | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 90 | blks = blk_dwrite(dev_desc, info->start, blkcnt, buffer); |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 91 | if (blks != blkcnt) { |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 92 | error("failed writing to device %d\n", dev_desc->devnum); |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 93 | fastboot_fail("failed writing to device"); |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 94 | return; |
| 95 | } |
| 96 | |
| 97 | printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz, |
| 98 | part_name); |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 99 | fastboot_okay(""); |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Steve Rae | f1f112f | 2016-06-07 11:19:35 -0700 | [diff] [blame] | 102 | void fb_mmc_flash_write(const char *cmd, void *download_buffer, |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 103 | unsigned int download_bytes) |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 104 | { |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 105 | struct blk_desc *dev_desc; |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 106 | disk_partition_t info; |
| 107 | |
Simon Glass | be1e9bb | 2016-02-29 15:25:42 -0700 | [diff] [blame] | 108 | dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV); |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 109 | if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) { |
| 110 | error("invalid mmc device\n"); |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 111 | fastboot_fail("invalid mmc device"); |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 112 | return; |
| 113 | } |
| 114 | |
Patrick Delaunay | 8a4f2bd | 2017-01-27 11:00:41 +0100 | [diff] [blame^] | 115 | #if CONFIG_IS_ENABLED(EFI_PARTITION) |
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__); |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 122 | fastboot_fail("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__); |
Petr Kulhavy | 9f174c9 | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 127 | fastboot_fail("writing GPT partitions failed"); |
Steve Rae | 7d05934 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 128 | return; |
| 129 | } |
| 130 | printf("........ success\n"); |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 131 | fastboot_okay(""); |
Steve Rae | 7d05934 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 132 | return; |
Petr Kulhavy | 9f174c9 | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 133 | } |
| 134 | #endif |
| 135 | |
Patrick Delaunay | f7e0772 | 2017-01-27 11:00:37 +0100 | [diff] [blame] | 136 | #if CONFIG_IS_ENABLED(DOS_PARTITION) |
Petr Kulhavy | 9f174c9 | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 137 | if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) { |
| 138 | printf("%s: updating MBR\n", __func__); |
| 139 | if (is_valid_dos_buf(download_buffer)) { |
| 140 | printf("%s: invalid MBR - refusing to write to flash\n", |
| 141 | __func__); |
| 142 | fastboot_fail("invalid MBR partition"); |
| 143 | return; |
| 144 | } |
| 145 | if (write_mbr_partition(dev_desc, download_buffer)) { |
| 146 | printf("%s: writing MBR partition failed\n", __func__); |
| 147 | fastboot_fail("writing MBR partition failed"); |
| 148 | return; |
| 149 | } |
| 150 | printf("........ success\n"); |
| 151 | fastboot_okay(""); |
| 152 | return; |
| 153 | } |
| 154 | #endif |
| 155 | |
| 156 | if (part_get_info_by_name_or_alias(dev_desc, cmd, &info)) { |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 157 | error("cannot find partition: '%s'\n", cmd); |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 158 | fastboot_fail("cannot find partition"); |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 159 | return; |
| 160 | } |
| 161 | |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 162 | if (is_sparse_image(download_buffer)) { |
| 163 | struct fb_mmc_sparse sparse_priv; |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 164 | struct sparse_storage sparse; |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 165 | |
| 166 | sparse_priv.dev_desc = dev_desc; |
| 167 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 168 | sparse.blksz = info.blksz; |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 169 | sparse.start = info.start; |
| 170 | sparse.size = info.size; |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 171 | sparse.write = fb_mmc_sparse_write; |
Steve Rae | 8a99be8 | 2016-06-07 11:19:38 -0700 | [diff] [blame] | 172 | sparse.reserve = fb_mmc_sparse_reserve; |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 173 | |
| 174 | printf("Flashing sparse image at offset " LBAFU "\n", |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 175 | sparse.start); |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 176 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 177 | sparse.priv = &sparse_priv; |
| 178 | write_sparse_image(&sparse, cmd, download_buffer, |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 179 | download_bytes); |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 180 | } else { |
Steve Rae | c601567 | 2014-08-26 11:47:30 -0700 | [diff] [blame] | 181 | write_raw_image(dev_desc, &info, cmd, download_buffer, |
| 182 | download_bytes); |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 183 | } |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 184 | } |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 185 | |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 186 | void fb_mmc_erase(const char *cmd) |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 187 | { |
| 188 | int ret; |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 189 | struct blk_desc *dev_desc; |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 190 | disk_partition_t info; |
| 191 | lbaint_t blks, blks_start, blks_size, grp_size; |
| 192 | struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV); |
| 193 | |
| 194 | if (mmc == NULL) { |
| 195 | error("invalid mmc device"); |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 196 | fastboot_fail("invalid mmc device"); |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 197 | return; |
| 198 | } |
| 199 | |
Simon Glass | be1e9bb | 2016-02-29 15:25:42 -0700 | [diff] [blame] | 200 | dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV); |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 201 | if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) { |
| 202 | error("invalid mmc device"); |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 203 | fastboot_fail("invalid mmc device"); |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 204 | return; |
| 205 | } |
| 206 | |
Petr Kulhavy | 9f174c9 | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 207 | ret = part_get_info_by_name_or_alias(dev_desc, cmd, &info); |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 208 | if (ret) { |
| 209 | error("cannot find partition: '%s'", cmd); |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 210 | fastboot_fail("cannot find partition"); |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 211 | return; |
| 212 | } |
| 213 | |
| 214 | /* Align blocks to erase group size to avoid erasing other partitions */ |
| 215 | grp_size = mmc->erase_grp_size; |
| 216 | blks_start = (info.start + grp_size - 1) & ~(grp_size - 1); |
| 217 | if (info.size >= grp_size) |
| 218 | blks_size = (info.size - (blks_start - info.start)) & |
| 219 | (~(grp_size - 1)); |
| 220 | else |
| 221 | blks_size = 0; |
| 222 | |
| 223 | printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n", |
| 224 | blks_start, blks_start + blks_size); |
| 225 | |
Xu Ziyuan | ec124ce | 2016-06-15 16:56:18 +0800 | [diff] [blame] | 226 | blks = blk_derase(dev_desc, blks_start, blks_size); |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 227 | if (blks != blks_size) { |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 228 | error("failed erasing from device %d", dev_desc->devnum); |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 229 | fastboot_fail("failed erasing from device"); |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 230 | return; |
| 231 | } |
| 232 | |
| 233 | printf("........ erased " LBAFU " bytes from '%s'\n", |
| 234 | blks_size * info.blksz, cmd); |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 235 | fastboot_okay(""); |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 236 | } |