]>
Commit | Line | Data |
---|---|---|
c0aebb33 SR |
1 | /* |
2 | * Copyright 2014 Broadcom Corporation. | |
3 | * | |
4 | * SPDX-License-Identifier: GPL-2.0+ | |
5 | */ | |
6 | ||
0ff7e585 | 7 | #include <config.h> |
c0aebb33 | 8 | #include <common.h> |
2a981dc2 | 9 | #include <blk.h> |
3c8f98f5 | 10 | #include <fastboot.h> |
c0aebb33 | 11 | #include <fb_mmc.h> |
3d4ef38d | 12 | #include <image-sparse.h> |
c0aebb33 | 13 | #include <part.h> |
89792381 | 14 | #include <mmc.h> |
5e0efc1b | 15 | #include <div64.h> |
c0aebb33 | 16 | |
0ff7e585 SR |
17 | #ifndef CONFIG_FASTBOOT_GPT_NAME |
18 | #define CONFIG_FASTBOOT_GPT_NAME GPT_ENTRY_NAME | |
19 | #endif | |
20 | ||
a5d1e04a | 21 | struct fb_mmc_sparse { |
4101f687 | 22 | struct blk_desc *dev_desc; |
a5d1e04a MR |
23 | }; |
24 | ||
3e8bd469 | 25 | static int part_get_info_efi_by_name_or_alias(struct blk_desc *dev_desc, |
8a41802f MS |
26 | const char *name, disk_partition_t *info) |
27 | { | |
28 | int ret; | |
29 | ||
3e8bd469 | 30 | ret = part_get_info_efi_by_name(dev_desc, name, info); |
8a41802f MS |
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) | |
3e8bd469 | 41 | ret = part_get_info_efi_by_name(dev_desc, |
8a41802f MS |
42 | aliased_part_name, info); |
43 | } | |
44 | return ret; | |
45 | } | |
46 | ||
cc0f08cd SR |
47 | static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info, |
48 | lbaint_t blk, lbaint_t blkcnt, const void *buffer) | |
a5d1e04a | 49 | { |
cc0f08cd | 50 | struct fb_mmc_sparse *sparse = info->priv; |
4101f687 | 51 | struct blk_desc *dev_desc = sparse->dev_desc; |
a5d1e04a | 52 | |
cc0f08cd | 53 | return blk_dwrite(dev_desc, blk, blkcnt, buffer); |
a5d1e04a MR |
54 | } |
55 | ||
2c724046 SR |
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 | ||
4101f687 | 62 | static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info, |
c0aebb33 SR |
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)); | |
5e0efc1b | 71 | blkcnt = lldiv(blkcnt, info->blksz); |
c0aebb33 SR |
72 | |
73 | if (blkcnt > info->size) { | |
74 | error("too large for partition: '%s'\n", part_name); | |
9bc34799 | 75 | fastboot_fail("too large for partition"); |
c0aebb33 SR |
76 | return; |
77 | } | |
78 | ||
79 | puts("Flashing Raw Image\n"); | |
80 | ||
2a981dc2 | 81 | blks = blk_dwrite(dev_desc, info->start, blkcnt, buffer); |
c0aebb33 | 82 | if (blks != blkcnt) { |
bcce53d0 | 83 | error("failed writing to device %d\n", dev_desc->devnum); |
9bc34799 | 84 | fastboot_fail("failed writing to device"); |
c0aebb33 SR |
85 | return; |
86 | } | |
87 | ||
88 | printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz, | |
89 | part_name); | |
9bc34799 | 90 | fastboot_okay(""); |
c0aebb33 SR |
91 | } |
92 | ||
64ece848 | 93 | void fb_mmc_flash_write(const char *cmd, void *download_buffer, |
9bc34799 | 94 | unsigned int download_bytes) |
c0aebb33 | 95 | { |
4101f687 | 96 | struct blk_desc *dev_desc; |
c0aebb33 SR |
97 | disk_partition_t info; |
98 | ||
db1d9e78 | 99 | dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV); |
c0aebb33 SR |
100 | if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) { |
101 | error("invalid mmc device\n"); | |
9bc34799 | 102 | fastboot_fail("invalid mmc device"); |
c0aebb33 SR |
103 | return; |
104 | } | |
105 | ||
0ff7e585 SR |
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__); | |
9bc34799 | 112 | fastboot_fail("invalid GPT partition"); |
0ff7e585 SR |
113 | return; |
114 | } | |
115 | if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) { | |
116 | printf("%s: writing GPT partitions failed\n", __func__); | |
9bc34799 | 117 | fastboot_fail( |
3c8f98f5 | 118 | "writing GPT partitions failed"); |
0ff7e585 SR |
119 | return; |
120 | } | |
121 | printf("........ success\n"); | |
9bc34799 | 122 | fastboot_okay(""); |
0ff7e585 | 123 | return; |
3e8bd469 | 124 | } else if (part_get_info_efi_by_name_or_alias(dev_desc, cmd, &info)) { |
c0aebb33 | 125 | error("cannot find partition: '%s'\n", cmd); |
9bc34799 | 126 | fastboot_fail("cannot find partition"); |
c0aebb33 SR |
127 | return; |
128 | } | |
129 | ||
a5d1e04a MR |
130 | if (is_sparse_image(download_buffer)) { |
131 | struct fb_mmc_sparse sparse_priv; | |
cc0f08cd | 132 | struct sparse_storage sparse; |
a5d1e04a MR |
133 | |
134 | sparse_priv.dev_desc = dev_desc; | |
135 | ||
cc0f08cd | 136 | sparse.blksz = info.blksz; |
a5d1e04a MR |
137 | sparse.start = info.start; |
138 | sparse.size = info.size; | |
a5d1e04a | 139 | sparse.write = fb_mmc_sparse_write; |
2c724046 | 140 | sparse.reserve = fb_mmc_sparse_reserve; |
a5d1e04a MR |
141 | |
142 | printf("Flashing sparse image at offset " LBAFU "\n", | |
cc0f08cd | 143 | sparse.start); |
a5d1e04a | 144 | |
cc0f08cd SR |
145 | sparse.priv = &sparse_priv; |
146 | write_sparse_image(&sparse, cmd, download_buffer, | |
9bc34799 | 147 | download_bytes); |
a5d1e04a | 148 | } else { |
e5bf9878 SR |
149 | write_raw_image(dev_desc, &info, cmd, download_buffer, |
150 | download_bytes); | |
a5d1e04a | 151 | } |
c0aebb33 | 152 | } |
89792381 | 153 | |
9bc34799 | 154 | void fb_mmc_erase(const char *cmd) |
89792381 DK |
155 | { |
156 | int ret; | |
4101f687 | 157 | struct blk_desc *dev_desc; |
89792381 DK |
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"); | |
9bc34799 | 164 | fastboot_fail("invalid mmc device"); |
89792381 DK |
165 | return; |
166 | } | |
167 | ||
db1d9e78 | 168 | dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV); |
89792381 DK |
169 | if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) { |
170 | error("invalid mmc device"); | |
9bc34799 | 171 | fastboot_fail("invalid mmc device"); |
89792381 DK |
172 | return; |
173 | } | |
174 | ||
3e8bd469 | 175 | ret = part_get_info_efi_by_name_or_alias(dev_desc, cmd, &info); |
89792381 DK |
176 | if (ret) { |
177 | error("cannot find partition: '%s'", cmd); | |
9bc34799 | 178 | fastboot_fail("cannot find partition"); |
89792381 DK |
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 | ||
ec3cde1e | 194 | blks = blk_derase(dev_desc, blks_start, blks_size); |
89792381 | 195 | if (blks != blks_size) { |
bcce53d0 | 196 | error("failed erasing from device %d", dev_desc->devnum); |
9bc34799 | 197 | fastboot_fail("failed erasing from device"); |
89792381 DK |
198 | return; |
199 | } | |
200 | ||
201 | printf("........ erased " LBAFU " bytes from '%s'\n", | |
202 | blks_size * info.blksz, cmd); | |
9bc34799 | 203 | fastboot_okay(""); |
89792381 | 204 | } |