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