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 | |
Steve Rae | 7d05934 | 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 | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 21 | struct fb_mmc_sparse { |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 22 | struct blk_desc *dev_desc; |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 23 | }; |
| 24 | |
Simon Glass | b89a844 | 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 | ced3349 | 2015-03-11 10:02:31 -0700 | [diff] [blame] | 26 | const char *name, disk_partition_t *info) |
| 27 | { |
| 28 | int ret; |
| 29 | |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 30 | ret = part_get_info_efi_by_name(dev_desc, name, info); |
Michael Scott | ced3349 | 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 | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 41 | ret = part_get_info_efi_by_name(dev_desc, |
Michael Scott | ced3349 | 2015-03-11 10:02:31 -0700 | [diff] [blame] | 42 | aliased_part_name, info); |
| 43 | } |
| 44 | return ret; |
| 45 | } |
| 46 | |
Steve Rae | 8cf58db | 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 | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 49 | { |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 50 | struct fb_mmc_sparse *sparse = info->priv; |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 51 | struct blk_desc *dev_desc = sparse->dev_desc; |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 52 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 53 | return blk_dwrite(dev_desc, blk, blkcnt, buffer); |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Steve Rae | 8a99be8 | 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 | e339475 | 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 | 792ac48 | 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 | 58f30d8 | 2015-10-28 06:24:16 +0200 | [diff] [blame] | 71 | blkcnt = lldiv(blkcnt, info->blksz); |
Steve Rae | 792ac48 | 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 | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 75 | fastboot_fail("too large for partition"); |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 76 | return; |
| 77 | } |
| 78 | |
| 79 | puts("Flashing Raw Image\n"); |
| 80 | |
Simon Glass | 2ee8ada | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 81 | blks = blk_dwrite(dev_desc, info->start, blkcnt, buffer); |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 82 | if (blks != blkcnt) { |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 83 | error("failed writing to device %d\n", dev_desc->devnum); |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 84 | fastboot_fail("failed writing to device"); |
Steve Rae | 792ac48 | 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 | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 90 | fastboot_okay(""); |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Steve Rae | f1f112f | 2016-06-07 11:19:35 -0700 | [diff] [blame] | 93 | void fb_mmc_flash_write(const char *cmd, void *download_buffer, |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 94 | unsigned int download_bytes) |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 95 | { |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 96 | struct blk_desc *dev_desc; |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 97 | disk_partition_t info; |
| 98 | |
Simon Glass | be1e9bb | 2016-02-29 15:25:42 -0700 | [diff] [blame] | 99 | dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV); |
Steve Rae | 792ac48 | 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 | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 102 | fastboot_fail("invalid mmc device"); |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 103 | return; |
| 104 | } |
| 105 | |
Steve Rae | 7d05934 | 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 | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 112 | fastboot_fail("invalid GPT partition"); |
Steve Rae | 7d05934 | 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 | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 117 | fastboot_fail( |
Maxime Ripard | 00e8eb7 | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 118 | "writing GPT partitions failed"); |
Steve Rae | 7d05934 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 119 | return; |
| 120 | } |
| 121 | printf("........ success\n"); |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 122 | fastboot_okay(""); |
Steve Rae | 7d05934 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 123 | return; |
Simon Glass | b89a844 | 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 | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 125 | error("cannot find partition: '%s'\n", cmd); |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 126 | fastboot_fail("cannot find partition"); |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 127 | return; |
| 128 | } |
| 129 | |
Maxime Ripard | a58e9c7 | 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 | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 132 | struct sparse_storage sparse; |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 133 | |
| 134 | sparse_priv.dev_desc = dev_desc; |
| 135 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 136 | sparse.blksz = info.blksz; |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 137 | sparse.start = info.start; |
| 138 | sparse.size = info.size; |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 139 | sparse.write = fb_mmc_sparse_write; |
Steve Rae | 8a99be8 | 2016-06-07 11:19:38 -0700 | [diff] [blame^] | 140 | sparse.reserve = fb_mmc_sparse_reserve; |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 141 | |
| 142 | printf("Flashing sparse image at offset " LBAFU "\n", |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 143 | sparse.start); |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 144 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 145 | sparse.priv = &sparse_priv; |
| 146 | write_sparse_image(&sparse, cmd, download_buffer, |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 147 | download_bytes); |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 148 | } else { |
Steve Rae | c601567 | 2014-08-26 11:47:30 -0700 | [diff] [blame] | 149 | write_raw_image(dev_desc, &info, cmd, download_buffer, |
| 150 | download_bytes); |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 151 | } |
Steve Rae | 792ac48 | 2014-08-26 11:47:27 -0700 | [diff] [blame] | 152 | } |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 153 | |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 154 | void fb_mmc_erase(const char *cmd) |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 155 | { |
| 156 | int ret; |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 157 | struct blk_desc *dev_desc; |
Dileep Katta | b122820 | 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 | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 164 | fastboot_fail("invalid mmc device"); |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 165 | return; |
| 166 | } |
| 167 | |
Simon Glass | be1e9bb | 2016-02-29 15:25:42 -0700 | [diff] [blame] | 168 | dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV); |
Dileep Katta | b122820 | 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 | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 171 | fastboot_fail("invalid mmc device"); |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 172 | return; |
| 173 | } |
| 174 | |
Simon Glass | b89a844 | 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 | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 176 | if (ret) { |
| 177 | error("cannot find partition: '%s'", cmd); |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 178 | fastboot_fail("cannot find partition"); |
Dileep Katta | b122820 | 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 | |
Stephen Warren | e73f296 | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 194 | blks = dev_desc->block_erase(dev_desc, blks_start, blks_size); |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 195 | if (blks != blks_size) { |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 196 | error("failed erasing from device %d", dev_desc->devnum); |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 197 | fastboot_fail("failed erasing from device"); |
Dileep Katta | b122820 | 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 | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 203 | fastboot_okay(""); |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 204 | } |