Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 2 | /* |
| 3 | * cmd_gpt.c -- GPT (GUID Partition Table) handling command |
| 4 | * |
Lukasz Majewski | 309691b | 2015-11-20 08:06:17 +0100 | [diff] [blame] | 5 | * Copyright (C) 2015 |
| 6 | * Lukasz Majewski <l.majewski@majess.pl> |
| 7 | * |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 8 | * Copyright (C) 2012 Samsung Electronics |
| 9 | * author: Lukasz Majewski <l.majewski@samsung.com> |
| 10 | * author: Piotr Wilczek <p.wilczek@samsung.com> |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 11 | */ |
| 12 | |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 13 | #include <blk.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 14 | #include <env.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 16 | #include <malloc.h> |
| 17 | #include <command.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 18 | #include <part.h> |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 19 | #include <part_efi.h> |
Corneliu Doban | 0c6776c | 2020-11-09 11:49:16 +0530 | [diff] [blame] | 20 | #include <part.h> |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 21 | #include <exports.h> |
Simon Glass | 6f04498 | 2020-05-10 11:39:52 -0600 | [diff] [blame] | 22 | #include <uuid.h> |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 23 | #include <linux/ctype.h> |
Piotr Wilczek | 36c06b1 | 2013-01-27 22:59:25 +0000 | [diff] [blame] | 24 | #include <div64.h> |
Lukasz Majewski | 309691b | 2015-11-20 08:06:17 +0100 | [diff] [blame] | 25 | #include <memalign.h> |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 26 | #include <linux/compat.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 27 | #include <linux/err.h> |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 28 | #include <linux/sizes.h> |
| 29 | #include <stdlib.h> |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 30 | |
| 31 | static LIST_HEAD(disk_partitions); |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 32 | |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 33 | /** |
| 34 | * extract_env(): Expand env name from string format '&{env_name}' |
| 35 | * and return pointer to the env (if the env is set) |
| 36 | * |
| 37 | * @param str - pointer to string |
| 38 | * @param env - pointer to pointer to extracted env |
| 39 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 40 | * Return: - zero on successful expand and env is set |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 41 | */ |
Przemyslaw Marczak | 5ec2ed2 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 42 | static int extract_env(const char *str, char **env) |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 43 | { |
Przemyslaw Marczak | 5ec2ed2 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 44 | int ret = -1; |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 45 | char *e, *s; |
Przemyslaw Marczak | 5ec2ed2 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 46 | #ifdef CONFIG_RANDOM_UUID |
| 47 | char uuid_str[UUID_STR_LEN + 1]; |
| 48 | #endif |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 49 | |
| 50 | if (!str || strlen(str) < 4) |
| 51 | return -1; |
| 52 | |
Przemyslaw Marczak | 5ec2ed2 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 53 | if (!((strncmp(str, "${", 2) == 0) && (str[strlen(str) - 1] == '}'))) |
| 54 | return -1; |
| 55 | |
| 56 | s = strdup(str); |
| 57 | if (s == NULL) |
| 58 | return -1; |
| 59 | |
| 60 | memset(s + strlen(s) - 1, '\0', 1); |
| 61 | memmove(s, s + 2, strlen(s) - 1); |
| 62 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 63 | e = env_get(s); |
Przemyslaw Marczak | 5ec2ed2 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 64 | if (e == NULL) { |
| 65 | #ifdef CONFIG_RANDOM_UUID |
| 66 | debug("%s unset. ", str); |
Vincent Tinelli | bdfacea | 2017-02-27 16:11:15 +0200 | [diff] [blame] | 67 | gen_rand_uuid_str(uuid_str, UUID_STR_FORMAT_GUID); |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 68 | env_set(s, uuid_str); |
Przemyslaw Marczak | 5ec2ed2 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 69 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 70 | e = env_get(s); |
Przemyslaw Marczak | 5ec2ed2 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 71 | if (e) { |
| 72 | debug("Set to random.\n"); |
| 73 | ret = 0; |
| 74 | } else { |
| 75 | debug("Can't get random UUID.\n"); |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 76 | } |
Przemyslaw Marczak | 5ec2ed2 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 77 | #else |
| 78 | debug("%s unset.\n", str); |
| 79 | #endif |
| 80 | } else { |
| 81 | debug("%s get from environment.\n", str); |
| 82 | ret = 0; |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Przemyslaw Marczak | 5ec2ed2 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 85 | *env = e; |
| 86 | free(s); |
| 87 | |
| 88 | return ret; |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** |
| 92 | * extract_val(): Extract value from a key=value pair list (comma separated). |
| 93 | * Only value for the given key is returend. |
| 94 | * Function allocates memory for the value, remember to free! |
| 95 | * |
| 96 | * @param str - pointer to string with key=values pairs |
| 97 | * @param key - pointer to the key to search for |
| 98 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 99 | * Return: - pointer to allocated string with the value |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 100 | */ |
| 101 | static char *extract_val(const char *str, const char *key) |
| 102 | { |
| 103 | char *v, *k; |
| 104 | char *s, *strcopy; |
| 105 | char *new = NULL; |
| 106 | |
| 107 | strcopy = strdup(str); |
| 108 | if (strcopy == NULL) |
| 109 | return NULL; |
| 110 | |
| 111 | s = strcopy; |
| 112 | while (s) { |
| 113 | v = strsep(&s, ","); |
| 114 | if (!v) |
| 115 | break; |
| 116 | k = strsep(&v, "="); |
| 117 | if (!k) |
| 118 | break; |
| 119 | if (strcmp(k, key) == 0) { |
| 120 | new = strdup(v); |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | free(strcopy); |
| 126 | |
| 127 | return new; |
| 128 | } |
| 129 | |
| 130 | /** |
Patrick Delaunay | 7840c4a | 2015-11-17 11:36:52 +0100 | [diff] [blame] | 131 | * found_key(): Found key without value in parameter list (comma separated). |
| 132 | * |
| 133 | * @param str - pointer to string with key |
| 134 | * @param key - pointer to the key to search for |
| 135 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 136 | * Return: - true on found key |
Patrick Delaunay | 7840c4a | 2015-11-17 11:36:52 +0100 | [diff] [blame] | 137 | */ |
| 138 | static bool found_key(const char *str, const char *key) |
| 139 | { |
| 140 | char *k; |
| 141 | char *s, *strcopy; |
| 142 | bool result = false; |
| 143 | |
| 144 | strcopy = strdup(str); |
| 145 | if (!strcopy) |
| 146 | return NULL; |
| 147 | |
| 148 | s = strcopy; |
| 149 | while (s) { |
| 150 | k = strsep(&s, ","); |
| 151 | if (!k) |
| 152 | break; |
| 153 | if (strcmp(k, key) == 0) { |
| 154 | result = true; |
| 155 | break; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | free(strcopy); |
| 160 | |
| 161 | return result; |
| 162 | } |
| 163 | |
Heinrich Schuchardt | d4bb653 | 2023-09-02 09:35:23 +0200 | [diff] [blame] | 164 | /** |
| 165 | * calc_parts_list_len() - get size of partition table description |
| 166 | * |
| 167 | * @numparts: number of partitions |
| 168 | * Return: string size including terminating NUL |
| 169 | */ |
Alison Chaiken | 8dcee7c | 2017-07-04 11:19:46 -0700 | [diff] [blame] | 170 | static int calc_parts_list_len(int numparts) |
| 171 | { |
Heinrich Schuchardt | d4bb653 | 2023-09-02 09:35:23 +0200 | [diff] [blame] | 172 | /* number of hexadecimal digits of the lbaint_t representation */ |
| 173 | const int lbaint_size = 2 * sizeof(lbaint_t); |
| 174 | int partlistlen; |
Alison Chaiken | 8dcee7c | 2017-07-04 11:19:46 -0700 | [diff] [blame] | 175 | |
Heinrich Schuchardt | d4bb653 | 2023-09-02 09:35:23 +0200 | [diff] [blame] | 176 | /* media description including terminating NUL */ |
| 177 | partlistlen = strlen("uuid_disk=;") + UUID_STR_LEN + 1; |
| 178 | /* per-partition descriptions; numparts */ |
| 179 | partlistlen += numparts * (strlen("name=,") + PART_NAME_LEN); |
Alison Chaiken | 8dcee7c | 2017-07-04 11:19:46 -0700 | [diff] [blame] | 180 | /* see part.h for definition of struct disk_partition */ |
Heinrich Schuchardt | d4bb653 | 2023-09-02 09:35:23 +0200 | [diff] [blame] | 181 | partlistlen += numparts * (strlen("start=0x,") + lbaint_size); |
| 182 | partlistlen += numparts * (strlen("size=0x,") + lbaint_size); |
Heinrich Schuchardt | d4bb653 | 2023-09-02 09:35:23 +0200 | [diff] [blame] | 183 | if (IS_ENABLED(CONFIG_PARTITION_UUIDS)) |
Heinrich Schuchardt | c97dbc4 | 2023-09-02 09:35:24 +0200 | [diff] [blame] | 184 | partlistlen += numparts * (strlen("uuid=,") + UUID_STR_LEN); |
| 185 | if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID)) |
| 186 | partlistlen += numparts * (strlen("type=;") + UUID_STR_LEN); |
Heinrich Schuchardt | d4bb653 | 2023-09-02 09:35:23 +0200 | [diff] [blame] | 187 | debug("Length of partitions_list is %d for %d partitions\n", |
| 188 | partlistlen, numparts); |
Alison Chaiken | 8dcee7c | 2017-07-04 11:19:46 -0700 | [diff] [blame] | 189 | return partlistlen; |
| 190 | } |
| 191 | |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 192 | #ifdef CONFIG_CMD_GPT_RENAME |
| 193 | static void del_gpt_info(void) |
| 194 | { |
| 195 | struct list_head *pos = &disk_partitions; |
| 196 | struct disk_part *curr; |
| 197 | while (!list_empty(pos)) { |
| 198 | curr = list_entry(pos->next, struct disk_part, list); |
| 199 | list_del(pos->next); |
| 200 | free(curr); |
| 201 | } |
| 202 | } |
| 203 | |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 204 | static struct disk_part *allocate_disk_part(struct disk_partition *info, |
| 205 | int partnum) |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 206 | { |
| 207 | struct disk_part *newpart; |
Heinrich Schuchardt | 30725f5 | 2017-09-21 19:03:06 +0200 | [diff] [blame] | 208 | newpart = calloc(1, sizeof(struct disk_part)); |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 209 | if (!newpart) |
| 210 | return ERR_PTR(-ENOMEM); |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 211 | |
| 212 | newpart->gpt_part_info.start = info->start; |
| 213 | newpart->gpt_part_info.size = info->size; |
| 214 | newpart->gpt_part_info.blksz = info->blksz; |
| 215 | strncpy((char *)newpart->gpt_part_info.name, (const char *)info->name, |
| 216 | PART_NAME_LEN); |
| 217 | newpart->gpt_part_info.name[PART_NAME_LEN - 1] = '\0'; |
| 218 | strncpy((char *)newpart->gpt_part_info.type, (const char *)info->type, |
| 219 | PART_TYPE_LEN); |
| 220 | newpart->gpt_part_info.type[PART_TYPE_LEN - 1] = '\0'; |
| 221 | newpart->gpt_part_info.bootable = info->bootable; |
Heinrich Schuchardt | 2d07fb4 | 2023-09-02 09:35:22 +0200 | [diff] [blame] | 222 | if (IS_ENABLED(CONFIG_PARTITION_UUIDS)) |
| 223 | disk_partition_set_uuid(&newpart->gpt_part_info, |
| 224 | disk_partition_uuid(info)); |
Heinrich Schuchardt | c97dbc4 | 2023-09-02 09:35:24 +0200 | [diff] [blame] | 225 | if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID)) |
| 226 | disk_partition_set_type_guid(&newpart->gpt_part_info, |
| 227 | disk_partition_type_guid(info)); |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 228 | newpart->partnum = partnum; |
| 229 | |
| 230 | return newpart; |
| 231 | } |
| 232 | |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 233 | static void prettyprint_part_size(char *sizestr, lbaint_t partsize, |
| 234 | lbaint_t blksize) |
| 235 | { |
| 236 | unsigned long long partbytes, partmegabytes; |
| 237 | |
| 238 | partbytes = partsize * blksize; |
| 239 | partmegabytes = lldiv(partbytes, SZ_1M); |
| 240 | snprintf(sizestr, 16, "%lluMiB", partmegabytes); |
| 241 | } |
| 242 | |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 243 | static void print_gpt_info(void) |
| 244 | { |
| 245 | struct list_head *pos; |
| 246 | struct disk_part *curr; |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 247 | char partstartstr[16]; |
| 248 | char partsizestr[16]; |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 249 | |
| 250 | list_for_each(pos, &disk_partitions) { |
| 251 | curr = list_entry(pos, struct disk_part, list); |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 252 | prettyprint_part_size(partstartstr, curr->gpt_part_info.start, |
| 253 | curr->gpt_part_info.blksz); |
| 254 | prettyprint_part_size(partsizestr, curr->gpt_part_info.size, |
| 255 | curr->gpt_part_info.blksz); |
| 256 | |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 257 | printf("Partition %d:\n", curr->partnum); |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 258 | printf("Start %s, size %s\n", partstartstr, partsizestr); |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 259 | printf("Block size %lu, name %s\n", curr->gpt_part_info.blksz, |
| 260 | curr->gpt_part_info.name); |
| 261 | printf("Type %s, bootable %d\n", curr->gpt_part_info.type, |
Heinrich Schuchardt | 59a860d | 2020-03-19 13:49:34 +0100 | [diff] [blame] | 262 | curr->gpt_part_info.bootable & PART_BOOTABLE); |
Heinrich Schuchardt | 2d07fb4 | 2023-09-02 09:35:22 +0200 | [diff] [blame] | 263 | if (CONFIG_IS_ENABLED(PARTITION_UUIDS)) |
| 264 | printf("UUID %s\n", |
| 265 | disk_partition_uuid(&curr->gpt_part_info)); |
Heinrich Schuchardt | c97dbc4 | 2023-09-02 09:35:24 +0200 | [diff] [blame] | 266 | if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID)) |
| 267 | printf("Type GUID %s\n", |
| 268 | disk_partition_type_guid(&curr->gpt_part_info)); |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 269 | printf("\n"); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /* |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 274 | * create the string that upstream 'gpt write' command will accept as an |
| 275 | * argument |
| 276 | * |
| 277 | * From doc/README.gpt, Format of partitions layout: |
| 278 | * "uuid_disk=...;name=u-boot,size=60MiB,uuid=...; |
| 279 | * name=kernel,size=60MiB,uuid=...;" |
| 280 | * The fields 'name' and 'size' are mandatory for every partition. |
| 281 | * The field 'start' is optional. The fields 'uuid' and 'uuid_disk' |
| 282 | * are optional if CONFIG_RANDOM_UUID is enabled. |
| 283 | */ |
| 284 | static int create_gpt_partitions_list(int numparts, const char *guid, |
| 285 | char *partitions_list) |
| 286 | { |
| 287 | struct list_head *pos; |
| 288 | struct disk_part *curr; |
| 289 | char partstr[PART_NAME_LEN + 1]; |
| 290 | |
| 291 | if (!partitions_list) |
| 292 | return -EINVAL; |
| 293 | |
| 294 | strcpy(partitions_list, "uuid_disk="); |
| 295 | strncat(partitions_list, guid, UUID_STR_LEN + 1); |
| 296 | strcat(partitions_list, ";"); |
| 297 | |
| 298 | list_for_each(pos, &disk_partitions) { |
| 299 | curr = list_entry(pos, struct disk_part, list); |
| 300 | strcat(partitions_list, "name="); |
| 301 | strncat(partitions_list, (const char *)curr->gpt_part_info.name, |
| 302 | PART_NAME_LEN + 1); |
Patrick Delaunay | 309a6fa | 2017-10-18 15:11:08 +0200 | [diff] [blame] | 303 | sprintf(partstr, ",start=0x%llx", |
| 304 | (unsigned long long)curr->gpt_part_info.start * |
| 305 | curr->gpt_part_info.blksz); |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 306 | /* one extra byte for NULL */ |
| 307 | strncat(partitions_list, partstr, PART_NAME_LEN + 1); |
Patrick Delaunay | 309a6fa | 2017-10-18 15:11:08 +0200 | [diff] [blame] | 308 | sprintf(partstr, ",size=0x%llx", |
| 309 | (unsigned long long)curr->gpt_part_info.size * |
| 310 | curr->gpt_part_info.blksz); |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 311 | strncat(partitions_list, partstr, PART_NAME_LEN + 1); |
| 312 | |
Heinrich Schuchardt | c97dbc4 | 2023-09-02 09:35:24 +0200 | [diff] [blame] | 313 | if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID)) { |
| 314 | strcat(partitions_list, ",type="); |
| 315 | strncat(partitions_list, |
| 316 | disk_partition_type_guid(&curr->gpt_part_info), |
| 317 | UUID_STR_LEN + 1); |
| 318 | } |
Heinrich Schuchardt | 2d07fb4 | 2023-09-02 09:35:22 +0200 | [diff] [blame] | 319 | if (CONFIG_IS_ENABLED(PARTITION_UUIDS)) { |
| 320 | strcat(partitions_list, ",uuid="); |
| 321 | strncat(partitions_list, |
| 322 | disk_partition_uuid(&curr->gpt_part_info), |
| 323 | UUID_STR_LEN + 1); |
| 324 | } |
Joshua Watt | 9e58adf | 2023-08-31 10:51:40 -0600 | [diff] [blame] | 325 | if (curr->gpt_part_info.bootable & PART_BOOTABLE) |
| 326 | strcat(partitions_list, ",bootable"); |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 327 | strcat(partitions_list, ";"); |
| 328 | } |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | /* |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 333 | * read partition info into disk_partitions list where |
| 334 | * it can be printed or modified |
| 335 | */ |
| 336 | static int get_gpt_info(struct blk_desc *dev_desc) |
| 337 | { |
| 338 | /* start partition numbering at 1, as U-Boot does */ |
| 339 | int valid_parts = 0, p, ret; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 340 | struct disk_partition info; |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 341 | struct disk_part *new_disk_part; |
| 342 | |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 343 | /* |
| 344 | * Always re-read partition info from device, in case |
| 345 | * it has changed |
| 346 | */ |
| 347 | INIT_LIST_HEAD(&disk_partitions); |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 348 | |
| 349 | for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) { |
| 350 | ret = part_get_info(dev_desc, p, &info); |
| 351 | if (ret) |
| 352 | continue; |
| 353 | |
| 354 | /* Add 1 here because counter is zero-based but p1 is |
| 355 | the first partition */ |
| 356 | new_disk_part = allocate_disk_part(&info, valid_parts+1); |
| 357 | if (IS_ERR(new_disk_part)) |
| 358 | goto out; |
| 359 | |
| 360 | list_add_tail(&new_disk_part->list, &disk_partitions); |
| 361 | valid_parts++; |
| 362 | } |
| 363 | if (valid_parts == 0) { |
| 364 | printf("** No valid partitions found **\n"); |
| 365 | goto out; |
| 366 | } |
| 367 | return valid_parts; |
| 368 | out: |
| 369 | if (valid_parts >= 1) |
| 370 | del_gpt_info(); |
| 371 | return -ENODEV; |
| 372 | } |
| 373 | |
| 374 | /* a wrapper to test get_gpt_info */ |
Farhan Ali | 078d022 | 2021-02-26 10:17:33 -0800 | [diff] [blame] | 375 | static int do_get_gpt_info(struct blk_desc *dev_desc, char * const namestr) |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 376 | { |
Farhan Ali | 078d022 | 2021-02-26 10:17:33 -0800 | [diff] [blame] | 377 | int numparts; |
| 378 | |
| 379 | numparts = get_gpt_info(dev_desc); |
| 380 | |
| 381 | if (numparts > 0) { |
| 382 | if (namestr) { |
| 383 | char disk_guid[UUID_STR_LEN + 1]; |
| 384 | char *partitions_list; |
| 385 | int partlistlen; |
| 386 | int ret = -1; |
| 387 | |
| 388 | ret = get_disk_guid(dev_desc, disk_guid); |
| 389 | if (ret < 0) |
| 390 | return ret; |
| 391 | |
| 392 | partlistlen = calc_parts_list_len(numparts); |
| 393 | partitions_list = malloc(partlistlen); |
| 394 | if (!partitions_list) { |
| 395 | del_gpt_info(); |
| 396 | return -ENOMEM; |
| 397 | } |
| 398 | memset(partitions_list, '\0', partlistlen); |
| 399 | |
| 400 | ret = create_gpt_partitions_list(numparts, disk_guid, |
| 401 | partitions_list); |
| 402 | if (ret < 0) |
| 403 | printf("Error: Could not create partition list string!\n"); |
| 404 | else |
| 405 | env_set(namestr, partitions_list); |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 406 | |
Farhan Ali | 078d022 | 2021-02-26 10:17:33 -0800 | [diff] [blame] | 407 | free(partitions_list); |
| 408 | } else { |
| 409 | print_gpt_info(); |
| 410 | } |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 411 | del_gpt_info(); |
| 412 | return 0; |
| 413 | } |
Farhan Ali | 078d022 | 2021-02-26 10:17:33 -0800 | [diff] [blame] | 414 | return numparts; |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 415 | } |
| 416 | #endif |
| 417 | |
Patrick Delaunay | 7840c4a | 2015-11-17 11:36:52 +0100 | [diff] [blame] | 418 | /** |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 419 | * set_gpt_info(): Fill partition information from string |
| 420 | * function allocates memory, remember to free! |
| 421 | * |
| 422 | * @param dev_desc - pointer block device descriptor |
| 423 | * @param str_part - pointer to string with partition information |
| 424 | * @param str_disk_guid - pointer to pointer to allocated string with disk guid |
| 425 | * @param partitions - pointer to pointer to allocated partitions array |
| 426 | * @param parts_count - number of partitions |
| 427 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 428 | * Return: - zero on success, otherwise error |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 429 | * |
| 430 | */ |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 431 | static int set_gpt_info(struct blk_desc *dev_desc, |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 432 | const char *str_part, |
| 433 | char **str_disk_guid, |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 434 | struct disk_partition **partitions, |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 435 | u8 *parts_count) |
| 436 | { |
| 437 | char *tok, *str, *s; |
| 438 | int i; |
| 439 | char *val, *p; |
| 440 | int p_count; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 441 | struct disk_partition *parts; |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 442 | int errno = 0; |
Piotr Wilczek | 36c06b1 | 2013-01-27 22:59:25 +0000 | [diff] [blame] | 443 | uint64_t size_ll, start_ll; |
Michael Trimarchi | 853445f | 2016-06-08 10:18:16 +0200 | [diff] [blame] | 444 | lbaint_t offset = 0; |
Alison Chaiken | 8dcee7c | 2017-07-04 11:19:46 -0700 | [diff] [blame] | 445 | int max_str_part = calc_parts_list_len(MAX_SEARCH_PARTITIONS); |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 446 | |
Egbert Eich | bfd4a71 | 2013-10-04 18:53:04 +0200 | [diff] [blame] | 447 | debug("%s: lba num: 0x%x %d\n", __func__, |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 448 | (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba); |
| 449 | |
| 450 | if (str_part == NULL) |
| 451 | return -1; |
| 452 | |
| 453 | str = strdup(str_part); |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 454 | if (str == NULL) |
| 455 | return -ENOMEM; |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 456 | |
| 457 | /* extract disk guid */ |
| 458 | s = str; |
Rob Herring | d976197 | 2015-01-26 09:44:18 -0600 | [diff] [blame] | 459 | val = extract_val(str, "uuid_disk"); |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 460 | if (!val) { |
Rob Herring | d976197 | 2015-01-26 09:44:18 -0600 | [diff] [blame] | 461 | #ifdef CONFIG_RANDOM_UUID |
| 462 | *str_disk_guid = malloc(UUID_STR_LEN + 1); |
Tom Rini | a29829e | 2017-10-07 11:27:59 -0400 | [diff] [blame] | 463 | if (*str_disk_guid == NULL) |
Alison Chaiken | 8dcee7c | 2017-07-04 11:19:46 -0700 | [diff] [blame] | 464 | return -ENOMEM; |
Rob Herring | d976197 | 2015-01-26 09:44:18 -0600 | [diff] [blame] | 465 | gen_rand_uuid_str(*str_disk_guid, UUID_STR_FORMAT_STD); |
| 466 | #else |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 467 | free(str); |
| 468 | return -2; |
Rob Herring | d976197 | 2015-01-26 09:44:18 -0600 | [diff] [blame] | 469 | #endif |
| 470 | } else { |
| 471 | val = strsep(&val, ";"); |
| 472 | if (extract_env(val, &p)) |
| 473 | p = val; |
| 474 | *str_disk_guid = strdup(p); |
| 475 | free(val); |
| 476 | /* Move s to first partition */ |
| 477 | strsep(&s, ";"); |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 478 | } |
Alison Chaiken | 8dcee7c | 2017-07-04 11:19:46 -0700 | [diff] [blame] | 479 | if (s == NULL) { |
| 480 | printf("Error: is the partitions string NULL-terminated?\n"); |
| 481 | return -EINVAL; |
| 482 | } |
| 483 | if (strnlen(s, max_str_part) == 0) |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 484 | return -3; |
| 485 | |
Alison Chaiken | 8dcee7c | 2017-07-04 11:19:46 -0700 | [diff] [blame] | 486 | i = strnlen(s, max_str_part) - 1; |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 487 | if (s[i] == ';') |
| 488 | s[i] = '\0'; |
| 489 | |
| 490 | /* calculate expected number of partitions */ |
| 491 | p_count = 1; |
| 492 | p = s; |
| 493 | while (*p) { |
| 494 | if (*p++ == ';') |
| 495 | p_count++; |
| 496 | } |
| 497 | |
| 498 | /* allocate memory for partitions */ |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 499 | parts = calloc(sizeof(struct disk_partition), p_count); |
Alison Chaiken | 8dcee7c | 2017-07-04 11:19:46 -0700 | [diff] [blame] | 500 | if (parts == NULL) |
| 501 | return -ENOMEM; |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 502 | |
Robert P. J. Day | e3f2c2e | 2013-08-21 11:39:19 -0400 | [diff] [blame] | 503 | /* retrieve partitions data from string */ |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 504 | for (i = 0; i < p_count; i++) { |
| 505 | tok = strsep(&s, ";"); |
| 506 | |
| 507 | if (tok == NULL) |
| 508 | break; |
| 509 | |
| 510 | /* uuid */ |
| 511 | val = extract_val(tok, "uuid"); |
Rob Herring | d976197 | 2015-01-26 09:44:18 -0600 | [diff] [blame] | 512 | if (!val) { |
| 513 | /* 'uuid' is optional if random uuid's are enabled */ |
| 514 | #ifdef CONFIG_RANDOM_UUID |
| 515 | gen_rand_uuid_str(parts[i].uuid, UUID_STR_FORMAT_STD); |
| 516 | #else |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 517 | errno = -4; |
| 518 | goto err; |
Rob Herring | d976197 | 2015-01-26 09:44:18 -0600 | [diff] [blame] | 519 | #endif |
| 520 | } else { |
| 521 | if (extract_env(val, &p)) |
| 522 | p = val; |
Alison Chaiken | 8dcee7c | 2017-07-04 11:19:46 -0700 | [diff] [blame] | 523 | if (strnlen(p, max_str_part) >= sizeof(parts[i].uuid)) { |
Rob Herring | d976197 | 2015-01-26 09:44:18 -0600 | [diff] [blame] | 524 | printf("Wrong uuid format for partition %d\n", i); |
| 525 | errno = -4; |
| 526 | goto err; |
| 527 | } |
Alison Chaiken | 8dcee7c | 2017-07-04 11:19:46 -0700 | [diff] [blame] | 528 | strncpy((char *)parts[i].uuid, p, max_str_part); |
Rob Herring | d976197 | 2015-01-26 09:44:18 -0600 | [diff] [blame] | 529 | free(val); |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 530 | } |
Patrick Delaunay | 8248c8d | 2015-10-27 11:00:27 +0100 | [diff] [blame] | 531 | #ifdef CONFIG_PARTITION_TYPE_GUID |
| 532 | /* guid */ |
| 533 | val = extract_val(tok, "type"); |
| 534 | if (val) { |
| 535 | /* 'type' is optional */ |
| 536 | if (extract_env(val, &p)) |
| 537 | p = val; |
Alison Chaiken | 8dcee7c | 2017-07-04 11:19:46 -0700 | [diff] [blame] | 538 | if (strnlen(p, max_str_part) >= sizeof(parts[i].type_guid)) { |
Patrick Delaunay | 8248c8d | 2015-10-27 11:00:27 +0100 | [diff] [blame] | 539 | printf("Wrong type guid format for partition %d\n", |
| 540 | i); |
| 541 | errno = -4; |
| 542 | goto err; |
| 543 | } |
Alison Chaiken | 8dcee7c | 2017-07-04 11:19:46 -0700 | [diff] [blame] | 544 | strncpy((char *)parts[i].type_guid, p, max_str_part); |
Patrick Delaunay | 8248c8d | 2015-10-27 11:00:27 +0100 | [diff] [blame] | 545 | free(val); |
| 546 | } |
| 547 | #endif |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 548 | /* name */ |
| 549 | val = extract_val(tok, "name"); |
| 550 | if (!val) { /* name is mandatory */ |
| 551 | errno = -4; |
| 552 | goto err; |
| 553 | } |
| 554 | if (extract_env(val, &p)) |
| 555 | p = val; |
Alison Chaiken | 8dcee7c | 2017-07-04 11:19:46 -0700 | [diff] [blame] | 556 | if (strnlen(p, max_str_part) >= sizeof(parts[i].name)) { |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 557 | errno = -4; |
| 558 | goto err; |
| 559 | } |
Alison Chaiken | 8dcee7c | 2017-07-04 11:19:46 -0700 | [diff] [blame] | 560 | strncpy((char *)parts[i].name, p, max_str_part); |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 561 | free(val); |
| 562 | |
| 563 | /* size */ |
| 564 | val = extract_val(tok, "size"); |
| 565 | if (!val) { /* 'size' is mandatory */ |
| 566 | errno = -4; |
| 567 | goto err; |
| 568 | } |
| 569 | if (extract_env(val, &p)) |
| 570 | p = val; |
Michael Trimarchi | 853445f | 2016-06-08 10:18:16 +0200 | [diff] [blame] | 571 | if ((strcmp(p, "-") == 0)) { |
Kever Yang | f1d070d | 2016-07-29 11:12:18 +0800 | [diff] [blame] | 572 | /* Let part efi module to auto extend the size */ |
| 573 | parts[i].size = 0; |
Michael Trimarchi | 853445f | 2016-06-08 10:18:16 +0200 | [diff] [blame] | 574 | } else { |
| 575 | size_ll = ustrtoull(p, &p, 0); |
| 576 | parts[i].size = lldiv(size_ll, dev_desc->blksz); |
| 577 | } |
| 578 | |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 579 | free(val); |
| 580 | |
| 581 | /* start address */ |
| 582 | val = extract_val(tok, "start"); |
| 583 | if (val) { /* start address is optional */ |
| 584 | if (extract_env(val, &p)) |
| 585 | p = val; |
Piotr Wilczek | 36c06b1 | 2013-01-27 22:59:25 +0000 | [diff] [blame] | 586 | start_ll = ustrtoull(p, &p, 0); |
| 587 | parts[i].start = lldiv(start_ll, dev_desc->blksz); |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 588 | free(val); |
| 589 | } |
Patrick Delaunay | 7840c4a | 2015-11-17 11:36:52 +0100 | [diff] [blame] | 590 | |
Michael Trimarchi | 853445f | 2016-06-08 10:18:16 +0200 | [diff] [blame] | 591 | offset += parts[i].size + parts[i].start; |
| 592 | |
Patrick Delaunay | 7840c4a | 2015-11-17 11:36:52 +0100 | [diff] [blame] | 593 | /* bootable */ |
| 594 | if (found_key(tok, "bootable")) |
Heinrich Schuchardt | 59a860d | 2020-03-19 13:49:34 +0100 | [diff] [blame] | 595 | parts[i].bootable = PART_BOOTABLE; |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | *parts_count = p_count; |
| 599 | *partitions = parts; |
| 600 | free(str); |
| 601 | |
| 602 | return 0; |
| 603 | err: |
| 604 | free(str); |
| 605 | free(*str_disk_guid); |
| 606 | free(parts); |
| 607 | |
| 608 | return errno; |
| 609 | } |
| 610 | |
Philippe Reynes | 5121529 | 2022-04-22 17:46:49 +0200 | [diff] [blame] | 611 | static int gpt_repair(struct blk_desc *blk_dev_desc) |
| 612 | { |
| 613 | int ret = 0; |
| 614 | |
| 615 | ret = gpt_repair_headers(blk_dev_desc); |
| 616 | |
| 617 | return ret; |
| 618 | } |
| 619 | |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 620 | static int gpt_default(struct blk_desc *blk_dev_desc, const char *str_part) |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 621 | { |
| 622 | int ret; |
| 623 | char *str_disk_guid; |
| 624 | u8 part_count = 0; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 625 | struct disk_partition *partitions = NULL; |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 626 | |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 627 | /* fill partitions */ |
Egbert Eich | bfd4a71 | 2013-10-04 18:53:04 +0200 | [diff] [blame] | 628 | ret = set_gpt_info(blk_dev_desc, str_part, |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 629 | &str_disk_guid, &partitions, &part_count); |
| 630 | if (ret) { |
| 631 | if (ret == -1) |
| 632 | printf("No partition list provided\n"); |
| 633 | if (ret == -2) |
| 634 | printf("Missing disk guid\n"); |
| 635 | if ((ret == -3) || (ret == -4)) |
| 636 | printf("Partition list incomplete\n"); |
| 637 | return -1; |
| 638 | } |
| 639 | |
| 640 | /* save partitions layout to disk */ |
Rob Herring | 008ab49 | 2015-01-26 09:43:15 -0600 | [diff] [blame] | 641 | ret = gpt_restore(blk_dev_desc, str_disk_guid, partitions, part_count); |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 642 | free(str_disk_guid); |
| 643 | free(partitions); |
| 644 | |
Kishan Dudhatra | cdea43b | 2024-04-22 23:47:33 +0530 | [diff] [blame] | 645 | /* initialize partition table */ |
| 646 | if (blk_enabled()) |
| 647 | part_init(blk_dev_desc); |
| 648 | |
Lukasz Majewski | 309691b | 2015-11-20 08:06:17 +0100 | [diff] [blame] | 649 | return ret; |
| 650 | } |
| 651 | |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 652 | static int gpt_verify(struct blk_desc *blk_dev_desc, const char *str_part) |
Lukasz Majewski | 309691b | 2015-11-20 08:06:17 +0100 | [diff] [blame] | 653 | { |
| 654 | ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, |
| 655 | blk_dev_desc->blksz); |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 656 | struct disk_partition *partitions = NULL; |
Lukasz Majewski | 309691b | 2015-11-20 08:06:17 +0100 | [diff] [blame] | 657 | gpt_entry *gpt_pte = NULL; |
| 658 | char *str_disk_guid; |
| 659 | u8 part_count = 0; |
| 660 | int ret = 0; |
| 661 | |
| 662 | /* fill partitions */ |
| 663 | ret = set_gpt_info(blk_dev_desc, str_part, |
| 664 | &str_disk_guid, &partitions, &part_count); |
| 665 | if (ret) { |
| 666 | if (ret == -1) { |
| 667 | printf("No partition list provided - only basic check\n"); |
| 668 | ret = gpt_verify_headers(blk_dev_desc, gpt_head, |
| 669 | &gpt_pte); |
| 670 | goto out; |
| 671 | } |
| 672 | if (ret == -2) |
| 673 | printf("Missing disk guid\n"); |
| 674 | if ((ret == -3) || (ret == -4)) |
| 675 | printf("Partition list incomplete\n"); |
| 676 | return -1; |
| 677 | } |
| 678 | |
| 679 | /* Check partition layout with provided pattern */ |
| 680 | ret = gpt_verify_partitions(blk_dev_desc, partitions, part_count, |
| 681 | gpt_head, &gpt_pte); |
| 682 | free(str_disk_guid); |
| 683 | free(partitions); |
| 684 | out: |
Sam Protsenko | def1e37 | 2024-06-19 16:23:30 -0500 | [diff] [blame] | 685 | if (!ret) |
| 686 | free(gpt_pte); |
Rob Herring | 008ab49 | 2015-01-26 09:43:15 -0600 | [diff] [blame] | 687 | return ret; |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 688 | } |
| 689 | |
Corneliu Doban | 0c6776c | 2020-11-09 11:49:16 +0530 | [diff] [blame] | 690 | /** |
| 691 | * gpt_enumerate() - Enumerate partition names into environment variable. |
| 692 | * |
| 693 | * Enumerate partition names. Partition names are stored in gpt_partition_list |
| 694 | * environment variable. Each partition name is delimited by space. |
| 695 | * |
| 696 | * @desc: block device descriptor |
| 697 | * |
| 698 | * @Return: '0' on success and -ve error on failure |
| 699 | */ |
| 700 | static int gpt_enumerate(struct blk_desc *desc) |
| 701 | { |
| 702 | struct part_driver *first_drv, *part_drv; |
| 703 | int str_len = 0, tmp_len; |
| 704 | char part_list[2048]; |
| 705 | int n_drvs; |
| 706 | char *ptr; |
| 707 | |
| 708 | part_list[0] = 0; |
| 709 | n_drvs = part_driver_get_count(); |
| 710 | if (!n_drvs) { |
| 711 | printf("Failed to get partition driver count\n"); |
| 712 | return -ENOENT; |
| 713 | } |
| 714 | |
| 715 | first_drv = part_driver_get_first(); |
| 716 | for (part_drv = first_drv; part_drv != first_drv + n_drvs; part_drv++) { |
| 717 | struct disk_partition pinfo; |
| 718 | int ret; |
| 719 | int i; |
| 720 | |
Heinrich Schuchardt | 16e48de | 2023-08-26 03:53:42 +0200 | [diff] [blame] | 721 | if (part_drv->test(desc)) |
| 722 | continue; |
| 723 | |
Corneliu Doban | 0c6776c | 2020-11-09 11:49:16 +0530 | [diff] [blame] | 724 | for (i = 1; i < part_drv->max_entries; i++) { |
| 725 | ret = part_drv->get_info(desc, i, &pinfo); |
Heinrich Schuchardt | 16e48de | 2023-08-26 03:53:42 +0200 | [diff] [blame] | 726 | if (ret) |
| 727 | continue; |
Corneliu Doban | 0c6776c | 2020-11-09 11:49:16 +0530 | [diff] [blame] | 728 | |
| 729 | ptr = &part_list[str_len]; |
| 730 | tmp_len = strlen((const char *)pinfo.name); |
| 731 | str_len += tmp_len; |
| 732 | /* +1 for space */ |
| 733 | str_len++; |
| 734 | if (str_len > sizeof(part_list)) { |
| 735 | printf("Error insufficient memory\n"); |
| 736 | return -ENOMEM; |
| 737 | } |
| 738 | strcpy(ptr, (const char *)pinfo.name); |
| 739 | /* One byte for space(" ") delimiter */ |
| 740 | ptr[tmp_len] = ' '; |
| 741 | } |
Heinrich Schuchardt | 16e48de | 2023-08-26 03:53:42 +0200 | [diff] [blame] | 742 | if (*part_list) |
| 743 | part_list[strlen(part_list) - 1] = 0; |
| 744 | break; |
Corneliu Doban | 0c6776c | 2020-11-09 11:49:16 +0530 | [diff] [blame] | 745 | } |
Corneliu Doban | 0c6776c | 2020-11-09 11:49:16 +0530 | [diff] [blame] | 746 | debug("setenv gpt_partition_list %s\n", part_list); |
| 747 | |
| 748 | return env_set("gpt_partition_list", part_list); |
| 749 | } |
| 750 | |
| 751 | /** |
| 752 | * gpt_setenv_part_variables() - setup partition environmental variables |
| 753 | * |
| 754 | * Setup the gpt_partition_name, gpt_partition_entry, gpt_partition_addr |
Joshua Watt | fd1134e | 2023-08-31 10:51:37 -0600 | [diff] [blame] | 755 | * and gpt_partition_size, gpt_partition_bootable environment variables. |
Corneliu Doban | 0c6776c | 2020-11-09 11:49:16 +0530 | [diff] [blame] | 756 | * |
| 757 | * @pinfo: pointer to disk partition |
| 758 | * @i: partition entry |
| 759 | * |
| 760 | * @Return: '0' on success and -ENOENT on failure |
| 761 | */ |
| 762 | static int gpt_setenv_part_variables(struct disk_partition *pinfo, int i) |
| 763 | { |
| 764 | int ret; |
| 765 | |
| 766 | ret = env_set_hex("gpt_partition_addr", pinfo->start); |
| 767 | if (ret) |
| 768 | goto fail; |
| 769 | |
| 770 | ret = env_set_hex("gpt_partition_size", pinfo->size); |
| 771 | if (ret) |
| 772 | goto fail; |
| 773 | |
Heinrich Schuchardt | 7e879da | 2023-08-26 03:53:43 +0200 | [diff] [blame] | 774 | ret = env_set_hex("gpt_partition_entry", i); |
Corneliu Doban | 0c6776c | 2020-11-09 11:49:16 +0530 | [diff] [blame] | 775 | if (ret) |
| 776 | goto fail; |
| 777 | |
| 778 | ret = env_set("gpt_partition_name", (const char *)pinfo->name); |
| 779 | if (ret) |
| 780 | goto fail; |
| 781 | |
Joshua Watt | fd1134e | 2023-08-31 10:51:37 -0600 | [diff] [blame] | 782 | ret = env_set_ulong("gpt_partition_bootable", !!(pinfo->bootable & PART_BOOTABLE)); |
| 783 | if (ret) |
| 784 | goto fail; |
| 785 | |
Corneliu Doban | 0c6776c | 2020-11-09 11:49:16 +0530 | [diff] [blame] | 786 | return 0; |
| 787 | |
| 788 | fail: |
| 789 | return -ENOENT; |
| 790 | } |
| 791 | |
| 792 | /** |
| 793 | * gpt_setenv() - Dynamically setup environment variables. |
| 794 | * |
| 795 | * Dynamically setup environment variables for name, index, offset and size |
| 796 | * for partition in GPT table after running "gpt setenv" for a partition name. |
| 797 | * |
| 798 | * @desc: block device descriptor |
| 799 | * @name: partition name |
| 800 | * |
| 801 | * @Return: '0' on success and -ve err on failure |
| 802 | */ |
| 803 | static int gpt_setenv(struct blk_desc *desc, const char *name) |
| 804 | { |
| 805 | struct part_driver *first_drv, *part_drv; |
| 806 | int n_drvs; |
| 807 | int ret = -1; |
| 808 | |
| 809 | n_drvs = part_driver_get_count(); |
| 810 | if (!n_drvs) { |
| 811 | printf("Failed to get partition driver count\n"); |
| 812 | goto fail; |
| 813 | } |
| 814 | |
| 815 | first_drv = part_driver_get_first(); |
| 816 | for (part_drv = first_drv; part_drv != first_drv + n_drvs; part_drv++) { |
| 817 | struct disk_partition pinfo; |
| 818 | int i; |
| 819 | |
| 820 | for (i = 1; i < part_drv->max_entries; i++) { |
| 821 | ret = part_drv->get_info(desc, i, &pinfo); |
Heinrich Schuchardt | 441dc54 | 2023-08-26 03:53:41 +0200 | [diff] [blame] | 822 | if (ret) |
| 823 | continue; |
Corneliu Doban | 0c6776c | 2020-11-09 11:49:16 +0530 | [diff] [blame] | 824 | |
| 825 | if (!strcmp(name, (const char *)pinfo.name)) { |
| 826 | /* match found, setup environment variables */ |
| 827 | ret = gpt_setenv_part_variables(&pinfo, i); |
| 828 | if (ret) |
| 829 | goto fail; |
| 830 | |
| 831 | return 0; |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | fail: |
| 837 | return ret; |
| 838 | } |
| 839 | |
Alison Chaiken | e422258 | 2017-06-25 16:43:23 -0700 | [diff] [blame] | 840 | static int do_disk_guid(struct blk_desc *dev_desc, char * const namestr) |
| 841 | { |
| 842 | int ret; |
| 843 | char disk_guid[UUID_STR_LEN + 1]; |
| 844 | |
| 845 | ret = get_disk_guid(dev_desc, disk_guid); |
| 846 | if (ret < 0) |
| 847 | return CMD_RET_FAILURE; |
| 848 | |
| 849 | if (namestr) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 850 | env_set(namestr, disk_guid); |
Alison Chaiken | e422258 | 2017-06-25 16:43:23 -0700 | [diff] [blame] | 851 | else |
| 852 | printf("%s\n", disk_guid); |
| 853 | |
| 854 | return ret; |
| 855 | } |
| 856 | |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 857 | #ifdef CONFIG_CMD_GPT_RENAME |
| 858 | static int do_rename_gpt_parts(struct blk_desc *dev_desc, char *subcomm, |
| 859 | char *name1, char *name2) |
| 860 | { |
| 861 | struct list_head *pos; |
| 862 | struct disk_part *curr; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 863 | struct disk_partition *new_partitions = NULL; |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 864 | char disk_guid[UUID_STR_LEN + 1]; |
Tom Rini | b0d3c0b | 2020-01-21 11:53:38 -0500 | [diff] [blame] | 865 | char *partitions_list, *str_disk_guid = NULL; |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 866 | u8 part_count = 0; |
| 867 | int partlistlen, ret, numparts = 0, partnum, i = 1, ctr1 = 0, ctr2 = 0; |
| 868 | |
Joshua Watt | d88ba5f | 2023-08-31 10:51:41 -0600 | [diff] [blame] | 869 | if (!subcomm || !name1 || !name2 || |
| 870 | (strcmp(subcomm, "swap") && strcmp(subcomm, "rename") && |
| 871 | strcmp(subcomm, "transpose"))) |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 872 | return -EINVAL; |
| 873 | |
| 874 | ret = get_disk_guid(dev_desc, disk_guid); |
| 875 | if (ret < 0) |
| 876 | return ret; |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 877 | /* |
| 878 | * Allocates disk_partitions, requiring matching call to del_gpt_info() |
| 879 | * if successful. |
| 880 | */ |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 881 | numparts = get_gpt_info(dev_desc); |
| 882 | if (numparts <= 0) |
| 883 | return numparts ? numparts : -ENODEV; |
| 884 | |
| 885 | partlistlen = calc_parts_list_len(numparts); |
| 886 | partitions_list = malloc(partlistlen); |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 887 | if (!partitions_list) { |
| 888 | del_gpt_info(); |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 889 | return -ENOMEM; |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 890 | } |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 891 | memset(partitions_list, '\0', partlistlen); |
| 892 | |
| 893 | ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list); |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 894 | if (ret < 0) { |
| 895 | free(partitions_list); |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 896 | return ret; |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 897 | } |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 898 | /* |
| 899 | * Uncomment the following line to print a string that 'gpt write' |
| 900 | * or 'gpt verify' will accept as input. |
| 901 | */ |
| 902 | debug("OLD partitions_list is %s with %u chars\n", partitions_list, |
| 903 | (unsigned)strlen(partitions_list)); |
| 904 | |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 905 | /* set_gpt_info allocates new_partitions and str_disk_guid */ |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 906 | ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid, |
| 907 | &new_partitions, &part_count); |
Tom Rini | b0d3c0b | 2020-01-21 11:53:38 -0500 | [diff] [blame] | 908 | if (ret < 0) |
| 909 | goto out; |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 910 | |
| 911 | if (!strcmp(subcomm, "swap")) { |
| 912 | if ((strlen(name1) > PART_NAME_LEN) || (strlen(name2) > PART_NAME_LEN)) { |
| 913 | printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN); |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 914 | ret = -EINVAL; |
| 915 | goto out; |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 916 | } |
| 917 | list_for_each(pos, &disk_partitions) { |
| 918 | curr = list_entry(pos, struct disk_part, list); |
| 919 | if (!strcmp((char *)curr->gpt_part_info.name, name1)) { |
| 920 | strcpy((char *)curr->gpt_part_info.name, name2); |
| 921 | ctr1++; |
| 922 | } else if (!strcmp((char *)curr->gpt_part_info.name, name2)) { |
| 923 | strcpy((char *)curr->gpt_part_info.name, name1); |
| 924 | ctr2++; |
| 925 | } |
| 926 | } |
| 927 | if ((ctr1 + ctr2 < 2) || (ctr1 != ctr2)) { |
| 928 | printf("Cannot swap partition names except in pairs.\n"); |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 929 | ret = -EINVAL; |
| 930 | goto out; |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 931 | } |
Joshua Watt | d88ba5f | 2023-08-31 10:51:41 -0600 | [diff] [blame] | 932 | } else if (!strcmp(subcomm, "transpose")) { |
| 933 | int idx1, idx2; |
| 934 | struct disk_partition* first = NULL; |
| 935 | struct disk_partition* second= NULL; |
| 936 | struct disk_partition tmp_part; |
| 937 | |
| 938 | idx1 = simple_strtoul(name1, NULL, 10); |
| 939 | idx2 = simple_strtoul(name2, NULL, 10); |
| 940 | if (idx1 == idx2) { |
| 941 | printf("Cannot swap partition with itself\n"); |
| 942 | ret = -EINVAL; |
| 943 | goto out; |
| 944 | } |
| 945 | |
| 946 | list_for_each(pos, &disk_partitions) { |
| 947 | curr = list_entry(pos, struct disk_part, list); |
| 948 | if (curr->partnum == idx1) |
| 949 | first = &curr->gpt_part_info; |
| 950 | else if (curr->partnum == idx2) |
| 951 | second = &curr->gpt_part_info; |
| 952 | } |
| 953 | if (!first) { |
| 954 | printf("Illegal partition number %s\n", name1); |
| 955 | ret = -EINVAL; |
| 956 | goto out; |
| 957 | } |
| 958 | if (!second) { |
| 959 | printf("Illegal partition number %s\n", name2); |
| 960 | ret = -EINVAL; |
| 961 | goto out; |
| 962 | } |
| 963 | |
| 964 | tmp_part = *first; |
| 965 | *first = *second; |
| 966 | *second = tmp_part; |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 967 | } else { /* rename */ |
| 968 | if (strlen(name2) > PART_NAME_LEN) { |
| 969 | printf("Names longer than %d characters are truncated.\n", PART_NAME_LEN); |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 970 | ret = -EINVAL; |
| 971 | goto out; |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 972 | } |
| 973 | partnum = (int)simple_strtol(name1, NULL, 10); |
| 974 | if ((partnum < 0) || (partnum > numparts)) { |
| 975 | printf("Illegal partition number %s\n", name1); |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 976 | ret = -EINVAL; |
| 977 | goto out; |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 978 | } |
| 979 | ret = part_get_info(dev_desc, partnum, new_partitions); |
| 980 | if (ret < 0) |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 981 | goto out; |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 982 | |
| 983 | /* U-Boot partition numbering starts at 1 */ |
| 984 | list_for_each(pos, &disk_partitions) { |
| 985 | curr = list_entry(pos, struct disk_part, list); |
| 986 | if (i == partnum) { |
| 987 | strcpy((char *)curr->gpt_part_info.name, name2); |
| 988 | break; |
| 989 | } |
| 990 | i++; |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | ret = create_gpt_partitions_list(numparts, disk_guid, partitions_list); |
| 995 | if (ret < 0) |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 996 | goto out; |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 997 | debug("NEW partitions_list is %s with %u chars\n", partitions_list, |
| 998 | (unsigned)strlen(partitions_list)); |
| 999 | |
| 1000 | ret = set_gpt_info(dev_desc, partitions_list, &str_disk_guid, |
| 1001 | &new_partitions, &part_count); |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 1002 | /* |
| 1003 | * Even though valid pointers are here passed into set_gpt_info(), |
| 1004 | * it mallocs again, and there's no way to tell which failed. |
| 1005 | */ |
Tom Rini | b0d3c0b | 2020-01-21 11:53:38 -0500 | [diff] [blame] | 1006 | if (ret < 0) |
| 1007 | goto out; |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 1008 | |
| 1009 | debug("Writing new partition table\n"); |
| 1010 | ret = gpt_restore(dev_desc, disk_guid, new_partitions, numparts); |
| 1011 | if (ret < 0) { |
| 1012 | printf("Writing new partition table failed\n"); |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 1013 | goto out; |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | debug("Reading back new partition table\n"); |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 1017 | /* |
| 1018 | * Empty the existing disk_partitions list, as otherwise the memory in |
| 1019 | * the original list is unreachable. |
| 1020 | */ |
| 1021 | del_gpt_info(); |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 1022 | numparts = get_gpt_info(dev_desc); |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 1023 | if (numparts <= 0) { |
| 1024 | ret = numparts ? numparts : -ENODEV; |
| 1025 | goto out; |
| 1026 | } |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 1027 | printf("new partition table with %d partitions is:\n", numparts); |
| 1028 | print_gpt_info(); |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 1029 | out: |
Tom Rini | b0d3c0b | 2020-01-21 11:53:38 -0500 | [diff] [blame] | 1030 | del_gpt_info(); |
| 1031 | #ifdef CONFIG_RANDOM_UUID |
Heinrich Schuchardt | 443806d | 2020-04-28 21:40:13 +0200 | [diff] [blame] | 1032 | free(str_disk_guid); |
Tom Rini | b0d3c0b | 2020-01-21 11:53:38 -0500 | [diff] [blame] | 1033 | #endif |
Heinrich Schuchardt | 443806d | 2020-04-28 21:40:13 +0200 | [diff] [blame] | 1034 | free(new_partitions); |
Alison Chaiken | 7e805c0 | 2017-09-26 07:42:28 -0700 | [diff] [blame] | 1035 | free(partitions_list); |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 1036 | return ret; |
| 1037 | } |
Joshua Watt | 44d6233 | 2023-08-31 10:51:38 -0600 | [diff] [blame] | 1038 | |
| 1039 | /** |
| 1040 | * gpt_set_bootable() - Set bootable flags for partitions |
| 1041 | * |
| 1042 | * Sets the bootable flag for any partition names in the comma separated list of |
| 1043 | * partition names. Any partitions not in the list have their bootable flag |
| 1044 | * cleared |
| 1045 | * |
| 1046 | * @desc: block device descriptor |
| 1047 | * @name: Comma separated list of partition names |
| 1048 | * |
| 1049 | * @Return: '0' on success and -ve error on failure |
| 1050 | */ |
| 1051 | static int gpt_set_bootable(struct blk_desc *blk_dev_desc, char *const part_list) |
| 1052 | { |
| 1053 | char *name; |
| 1054 | char disk_guid[UUID_STR_LEN + 1]; |
| 1055 | struct list_head *pos; |
| 1056 | struct disk_part *curr; |
| 1057 | struct disk_partition *partitions = NULL; |
| 1058 | int part_count = 0; |
| 1059 | int ret = get_disk_guid(blk_dev_desc, disk_guid); |
| 1060 | |
| 1061 | if (ret < 0) |
| 1062 | return ret; |
| 1063 | |
| 1064 | ret = get_gpt_info(blk_dev_desc); |
| 1065 | if (ret <= 0) |
| 1066 | goto out; |
| 1067 | |
| 1068 | part_count = ret; |
| 1069 | partitions = malloc(sizeof(*partitions) * part_count); |
| 1070 | if (!partitions) { |
| 1071 | ret = -ENOMEM; |
| 1072 | goto out; |
| 1073 | } |
| 1074 | |
| 1075 | /* Copy partitions and clear bootable flag */ |
| 1076 | part_count = 0; |
| 1077 | list_for_each(pos, &disk_partitions) { |
| 1078 | curr = list_entry(pos, struct disk_part, list); |
| 1079 | partitions[part_count] = curr->gpt_part_info; |
| 1080 | partitions[part_count].bootable &= ~PART_BOOTABLE; |
| 1081 | part_count++; |
| 1082 | } |
| 1083 | |
| 1084 | name = strtok(part_list, ","); |
| 1085 | while (name) { |
| 1086 | bool found = false; |
| 1087 | |
| 1088 | for (int i = 0; i < part_count; i++) { |
| 1089 | if (strcmp((char *)partitions[i].name, name) == 0) { |
| 1090 | partitions[i].bootable |= PART_BOOTABLE; |
| 1091 | found = true; |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | if (!found) { |
| 1096 | printf("Warning: No partition matching '%s' found\n", |
| 1097 | name); |
| 1098 | } |
| 1099 | |
| 1100 | name = strtok(NULL, ","); |
| 1101 | } |
| 1102 | |
| 1103 | ret = gpt_restore(blk_dev_desc, disk_guid, partitions, part_count); |
| 1104 | |
| 1105 | out: |
| 1106 | del_gpt_info(); |
| 1107 | |
| 1108 | if (partitions) |
| 1109 | free(partitions); |
| 1110 | |
| 1111 | return ret; |
| 1112 | } |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 1113 | #endif |
| 1114 | |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 1115 | /** |
| 1116 | * do_gpt(): Perform GPT operations |
| 1117 | * |
| 1118 | * @param cmdtp - command name |
| 1119 | * @param flag |
| 1120 | * @param argc |
| 1121 | * @param argv |
| 1122 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 1123 | * Return: zero on success; otherwise error |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 1124 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1125 | static int do_gpt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 1126 | { |
| 1127 | int ret = CMD_RET_SUCCESS; |
| 1128 | int dev = 0; |
Egbert Eich | bfd4a71 | 2013-10-04 18:53:04 +0200 | [diff] [blame] | 1129 | char *ep; |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 1130 | struct blk_desc *blk_dev_desc = NULL; |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 1131 | |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 1132 | #ifndef CONFIG_CMD_GPT_RENAME |
Lukasz Majewski | 309691b | 2015-11-20 08:06:17 +0100 | [diff] [blame] | 1133 | if (argc < 4 || argc > 5) |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 1134 | #else |
| 1135 | if (argc < 4 || argc > 6) |
| 1136 | #endif |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 1137 | return CMD_RET_USAGE; |
| 1138 | |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 1139 | dev = (int)dectoul(argv[3], &ep); |
Lukasz Majewski | 309691b | 2015-11-20 08:06:17 +0100 | [diff] [blame] | 1140 | if (!ep || ep[0] != '\0') { |
| 1141 | printf("'%s' is not a number\n", argv[3]); |
| 1142 | return CMD_RET_USAGE; |
| 1143 | } |
Simon Glass | be1e9bb | 2016-02-29 15:25:42 -0700 | [diff] [blame] | 1144 | blk_dev_desc = blk_get_dev(argv[2], dev); |
Lukasz Majewski | 309691b | 2015-11-20 08:06:17 +0100 | [diff] [blame] | 1145 | if (!blk_dev_desc) { |
| 1146 | printf("%s: %s dev %d NOT available\n", |
| 1147 | __func__, argv[2], dev); |
| 1148 | return CMD_RET_FAILURE; |
| 1149 | } |
Przemyslaw Marczak | 5ec2ed2 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 1150 | |
Philippe Reynes | 5121529 | 2022-04-22 17:46:49 +0200 | [diff] [blame] | 1151 | if (strcmp(argv[1], "repair") == 0) { |
| 1152 | printf("Repairing GPT: "); |
| 1153 | ret = gpt_repair(blk_dev_desc); |
| 1154 | } else if ((strcmp(argv[1], "write") == 0) && (argc == 5)) { |
Lukasz Majewski | 309691b | 2015-11-20 08:06:17 +0100 | [diff] [blame] | 1155 | printf("Writing GPT: "); |
Przemyslaw Marczak | 5ec2ed2 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 1156 | ret = gpt_default(blk_dev_desc, argv[4]); |
Lukasz Majewski | 309691b | 2015-11-20 08:06:17 +0100 | [diff] [blame] | 1157 | } else if ((strcmp(argv[1], "verify") == 0)) { |
| 1158 | ret = gpt_verify(blk_dev_desc, argv[4]); |
| 1159 | printf("Verify GPT: "); |
Corneliu Doban | 0c6776c | 2020-11-09 11:49:16 +0530 | [diff] [blame] | 1160 | } else if ((strcmp(argv[1], "setenv") == 0)) { |
| 1161 | ret = gpt_setenv(blk_dev_desc, argv[4]); |
| 1162 | } else if ((strcmp(argv[1], "enumerate") == 0)) { |
| 1163 | ret = gpt_enumerate(blk_dev_desc); |
Alison Chaiken | e422258 | 2017-06-25 16:43:23 -0700 | [diff] [blame] | 1164 | } else if (strcmp(argv[1], "guid") == 0) { |
| 1165 | ret = do_disk_guid(blk_dev_desc, argv[4]); |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 1166 | #ifdef CONFIG_CMD_GPT_RENAME |
| 1167 | } else if (strcmp(argv[1], "read") == 0) { |
Farhan Ali | 078d022 | 2021-02-26 10:17:33 -0800 | [diff] [blame] | 1168 | ret = do_get_gpt_info(blk_dev_desc, (argc == 5) ? argv[4] : NULL); |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 1169 | } else if ((strcmp(argv[1], "swap") == 0) || |
Joshua Watt | d88ba5f | 2023-08-31 10:51:41 -0600 | [diff] [blame] | 1170 | (strcmp(argv[1], "rename") == 0) || |
| 1171 | (strcmp(argv[1], "transpose") == 0)) { |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 1172 | ret = do_rename_gpt_parts(blk_dev_desc, argv[1], argv[4], argv[5]); |
Joshua Watt | 44d6233 | 2023-08-31 10:51:38 -0600 | [diff] [blame] | 1173 | } else if ((strcmp(argv[1], "set-bootable") == 0)) { |
| 1174 | ret = gpt_set_bootable(blk_dev_desc, argv[4]); |
Alison Chaiken | f04d4b5 | 2017-07-04 11:18:50 -0700 | [diff] [blame] | 1175 | #endif |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 1176 | } else { |
| 1177 | return CMD_RET_USAGE; |
| 1178 | } |
Lukasz Majewski | 309691b | 2015-11-20 08:06:17 +0100 | [diff] [blame] | 1179 | |
| 1180 | if (ret) { |
| 1181 | printf("error!\n"); |
| 1182 | return CMD_RET_FAILURE; |
| 1183 | } |
| 1184 | |
| 1185 | printf("success!\n"); |
| 1186 | return CMD_RET_SUCCESS; |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt, |
| 1190 | "GUID Partition Table", |
Robert P. J. Day | e3f2c2e | 2013-08-21 11:39:19 -0400 | [diff] [blame] | 1191 | "<command> <interface> <dev> <partitions_list>\n" |
Lukasz Majewski | cc5f3d7 | 2015-11-13 07:42:10 +0100 | [diff] [blame] | 1192 | " - GUID partition table restoration and validity check\n" |
| 1193 | " Restore or verify GPT information on a device connected\n" |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 1194 | " to interface\n" |
Lukasz Majewski | cc5f3d7 | 2015-11-13 07:42:10 +0100 | [diff] [blame] | 1195 | " Example usage:\n" |
Philippe Reynes | 5121529 | 2022-04-22 17:46:49 +0200 | [diff] [blame] | 1196 | " gpt repair mmc 0\n" |
| 1197 | " - repair the GPT on the device\n" |
Lukasz Majewski | cc5f3d7 | 2015-11-13 07:42:10 +0100 | [diff] [blame] | 1198 | " gpt write mmc 0 $partitions\n" |
Corneliu Doban | 0c6776c | 2020-11-09 11:49:16 +0530 | [diff] [blame] | 1199 | " - write the GPT to device\n" |
Lukasz Majewski | cc5f3d7 | 2015-11-13 07:42:10 +0100 | [diff] [blame] | 1200 | " gpt verify mmc 0 $partitions\n" |
Corneliu Doban | 0c6776c | 2020-11-09 11:49:16 +0530 | [diff] [blame] | 1201 | " - verify the GPT on device against $partitions\n" |
| 1202 | " gpt setenv mmc 0 $name\n" |
| 1203 | " - setup environment variables for partition $name:\n" |
| 1204 | " gpt_partition_addr, gpt_partition_size,\n" |
Joshua Watt | fd1134e | 2023-08-31 10:51:37 -0600 | [diff] [blame] | 1205 | " gpt_partition_name, gpt_partition_entry,\n" |
| 1206 | " gpt_partition_bootable\n" |
Corneliu Doban | 0c6776c | 2020-11-09 11:49:16 +0530 | [diff] [blame] | 1207 | " gpt enumerate mmc 0\n" |
| 1208 | " - store list of partitions to gpt_partition_list environment variable\n" |
Eugeniu Rosca | fa8acbc | 2019-04-30 04:53:46 +0200 | [diff] [blame] | 1209 | " gpt guid <interface> <dev>\n" |
Alison Chaiken | e422258 | 2017-06-25 16:43:23 -0700 | [diff] [blame] | 1210 | " - print disk GUID\n" |
Eugeniu Rosca | fa8acbc | 2019-04-30 04:53:46 +0200 | [diff] [blame] | 1211 | " gpt guid <interface> <dev> <varname>\n" |
Alison Chaiken | e422258 | 2017-06-25 16:43:23 -0700 | [diff] [blame] | 1212 | " - set environment variable to disk GUID\n" |
| 1213 | " Example usage:\n" |
| 1214 | " gpt guid mmc 0\n" |
| 1215 | " gpt guid mmc 0 varname\n" |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 1216 | #ifdef CONFIG_CMD_GPT_RENAME |
| 1217 | "gpt partition renaming commands:\n" |
Farhan Ali | 078d022 | 2021-02-26 10:17:33 -0800 | [diff] [blame] | 1218 | " gpt read <interface> <dev> [<varname>]\n" |
Eugeniu Rosca | fa8acbc | 2019-04-30 04:53:46 +0200 | [diff] [blame] | 1219 | " - read GPT into a data structure for manipulation\n" |
Farhan Ali | 078d022 | 2021-02-26 10:17:33 -0800 | [diff] [blame] | 1220 | " - read GPT partitions into environment variable\n" |
Eugeniu Rosca | fa8acbc | 2019-04-30 04:53:46 +0200 | [diff] [blame] | 1221 | " gpt swap <interface> <dev> <name1> <name2>\n" |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 1222 | " - change all partitions named name1 to name2\n" |
| 1223 | " and vice-versa\n" |
Joshua Watt | d88ba5f | 2023-08-31 10:51:41 -0600 | [diff] [blame] | 1224 | " gpt transpose <interface> <dev> <part1> <part2>\n" |
| 1225 | " - Swap the order of the entries for part1 and part2 in the partition table\n" |
Eugeniu Rosca | fa8acbc | 2019-04-30 04:53:46 +0200 | [diff] [blame] | 1226 | " gpt rename <interface> <dev> <part> <name>\n" |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 1227 | " - rename the specified partition\n" |
Joshua Watt | 44d6233 | 2023-08-31 10:51:38 -0600 | [diff] [blame] | 1228 | " gpt set-bootable <interface> <dev> <list>\n" |
| 1229 | " - make partition names in list bootable\n" |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 1230 | " Example usage:\n" |
| 1231 | " gpt swap mmc 0 foo bar\n" |
| 1232 | " gpt rename mmc 0 3 foo\n" |
Joshua Watt | 44d6233 | 2023-08-31 10:51:38 -0600 | [diff] [blame] | 1233 | " gpt set-bootable mmc 0 boot_a,boot_b\n" |
Joshua Watt | d88ba5f | 2023-08-31 10:51:41 -0600 | [diff] [blame] | 1234 | " gpt transpose mmc 0 1 2\n" |
Alison Chaiken | 8b3397f | 2017-07-04 11:19:18 -0700 | [diff] [blame] | 1235 | #endif |
Piotr Wilczek | cc00ea8 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 1236 | ); |