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