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