Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 1 | /* |
| 2 | * cmd_gpt.c -- GPT (GUID Partition Table) handling command |
| 3 | * |
| 4 | * Copyright (C) 2012 Samsung Electronics |
| 5 | * author: Lukasz Majewski <l.majewski@samsung.com> |
| 6 | * author: Piotr Wilczek <p.wilczek@samsung.com> |
| 7 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <malloc.h> |
| 13 | #include <command.h> |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 14 | #include <part_efi.h> |
| 15 | #include <exports.h> |
| 16 | #include <linux/ctype.h> |
Piotr Wilczek | 3e34cf7 | 2013-01-27 22:59:25 +0000 | [diff] [blame] | 17 | #include <div64.h> |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 18 | |
| 19 | #ifndef CONFIG_PARTITION_UUIDS |
| 20 | #error CONFIG_PARTITION_UUIDS must be enabled for CONFIG_CMD_GPT to be enabled |
| 21 | #endif |
| 22 | |
| 23 | /** |
| 24 | * extract_env(): Expand env name from string format '&{env_name}' |
| 25 | * and return pointer to the env (if the env is set) |
| 26 | * |
| 27 | * @param str - pointer to string |
| 28 | * @param env - pointer to pointer to extracted env |
| 29 | * |
| 30 | * @return - zero on successful expand and env is set |
| 31 | */ |
Przemyslaw Marczak | 3920638 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 32 | static int extract_env(const char *str, char **env) |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 33 | { |
Przemyslaw Marczak | 3920638 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 34 | int ret = -1; |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 35 | char *e, *s; |
Przemyslaw Marczak | 3920638 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 36 | #ifdef CONFIG_RANDOM_UUID |
| 37 | char uuid_str[UUID_STR_LEN + 1]; |
| 38 | #endif |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 39 | |
| 40 | if (!str || strlen(str) < 4) |
| 41 | return -1; |
| 42 | |
Przemyslaw Marczak | 3920638 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 43 | if (!((strncmp(str, "${", 2) == 0) && (str[strlen(str) - 1] == '}'))) |
| 44 | return -1; |
| 45 | |
| 46 | s = strdup(str); |
| 47 | if (s == NULL) |
| 48 | return -1; |
| 49 | |
| 50 | memset(s + strlen(s) - 1, '\0', 1); |
| 51 | memmove(s, s + 2, strlen(s) - 1); |
| 52 | |
| 53 | e = getenv(s); |
| 54 | if (e == NULL) { |
| 55 | #ifdef CONFIG_RANDOM_UUID |
| 56 | debug("%s unset. ", str); |
| 57 | gen_rand_uuid_str(uuid_str, UUID_STR_FORMAT_STD); |
| 58 | setenv(s, uuid_str); |
| 59 | |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 60 | e = getenv(s); |
Przemyslaw Marczak | 3920638 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 61 | if (e) { |
| 62 | debug("Set to random.\n"); |
| 63 | ret = 0; |
| 64 | } else { |
| 65 | debug("Can't get random UUID.\n"); |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 66 | } |
Przemyslaw Marczak | 3920638 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 67 | #else |
| 68 | debug("%s unset.\n", str); |
| 69 | #endif |
| 70 | } else { |
| 71 | debug("%s get from environment.\n", str); |
| 72 | ret = 0; |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Przemyslaw Marczak | 3920638 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 75 | *env = e; |
| 76 | free(s); |
| 77 | |
| 78 | return ret; |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * extract_val(): Extract value from a key=value pair list (comma separated). |
| 83 | * Only value for the given key is returend. |
| 84 | * Function allocates memory for the value, remember to free! |
| 85 | * |
| 86 | * @param str - pointer to string with key=values pairs |
| 87 | * @param key - pointer to the key to search for |
| 88 | * |
| 89 | * @return - pointer to allocated string with the value |
| 90 | */ |
| 91 | static char *extract_val(const char *str, const char *key) |
| 92 | { |
| 93 | char *v, *k; |
| 94 | char *s, *strcopy; |
| 95 | char *new = NULL; |
| 96 | |
| 97 | strcopy = strdup(str); |
| 98 | if (strcopy == NULL) |
| 99 | return NULL; |
| 100 | |
| 101 | s = strcopy; |
| 102 | while (s) { |
| 103 | v = strsep(&s, ","); |
| 104 | if (!v) |
| 105 | break; |
| 106 | k = strsep(&v, "="); |
| 107 | if (!k) |
| 108 | break; |
| 109 | if (strcmp(k, key) == 0) { |
| 110 | new = strdup(v); |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | free(strcopy); |
| 116 | |
| 117 | return new; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * set_gpt_info(): Fill partition information from string |
| 122 | * function allocates memory, remember to free! |
| 123 | * |
| 124 | * @param dev_desc - pointer block device descriptor |
| 125 | * @param str_part - pointer to string with partition information |
| 126 | * @param str_disk_guid - pointer to pointer to allocated string with disk guid |
| 127 | * @param partitions - pointer to pointer to allocated partitions array |
| 128 | * @param parts_count - number of partitions |
| 129 | * |
| 130 | * @return - zero on success, otherwise error |
| 131 | * |
| 132 | */ |
| 133 | static int set_gpt_info(block_dev_desc_t *dev_desc, |
| 134 | const char *str_part, |
| 135 | char **str_disk_guid, |
| 136 | disk_partition_t **partitions, |
| 137 | u8 *parts_count) |
| 138 | { |
| 139 | char *tok, *str, *s; |
| 140 | int i; |
| 141 | char *val, *p; |
| 142 | int p_count; |
| 143 | disk_partition_t *parts; |
| 144 | int errno = 0; |
Piotr Wilczek | 3e34cf7 | 2013-01-27 22:59:25 +0000 | [diff] [blame] | 145 | uint64_t size_ll, start_ll; |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 146 | |
Egbert Eich | 619f0fd | 2013-10-04 18:53:04 +0200 | [diff] [blame] | 147 | debug("%s: lba num: 0x%x %d\n", __func__, |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 148 | (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba); |
| 149 | |
| 150 | if (str_part == NULL) |
| 151 | return -1; |
| 152 | |
| 153 | str = strdup(str_part); |
| 154 | |
| 155 | /* extract disk guid */ |
| 156 | s = str; |
Rob Herring | 0c7e8d1 | 2015-01-26 09:44:18 -0600 | [diff] [blame] | 157 | val = extract_val(str, "uuid_disk"); |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 158 | if (!val) { |
Rob Herring | 0c7e8d1 | 2015-01-26 09:44:18 -0600 | [diff] [blame] | 159 | #ifdef CONFIG_RANDOM_UUID |
| 160 | *str_disk_guid = malloc(UUID_STR_LEN + 1); |
| 161 | gen_rand_uuid_str(*str_disk_guid, UUID_STR_FORMAT_STD); |
| 162 | #else |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 163 | free(str); |
| 164 | return -2; |
Rob Herring | 0c7e8d1 | 2015-01-26 09:44:18 -0600 | [diff] [blame] | 165 | #endif |
| 166 | } else { |
| 167 | val = strsep(&val, ";"); |
| 168 | if (extract_env(val, &p)) |
| 169 | p = val; |
| 170 | *str_disk_guid = strdup(p); |
| 171 | free(val); |
| 172 | /* Move s to first partition */ |
| 173 | strsep(&s, ";"); |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 174 | } |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 175 | if (strlen(s) == 0) |
| 176 | return -3; |
| 177 | |
| 178 | i = strlen(s) - 1; |
| 179 | if (s[i] == ';') |
| 180 | s[i] = '\0'; |
| 181 | |
| 182 | /* calculate expected number of partitions */ |
| 183 | p_count = 1; |
| 184 | p = s; |
| 185 | while (*p) { |
| 186 | if (*p++ == ';') |
| 187 | p_count++; |
| 188 | } |
| 189 | |
| 190 | /* allocate memory for partitions */ |
| 191 | parts = calloc(sizeof(disk_partition_t), p_count); |
| 192 | |
Robert P. J. Day | 1f8b546 | 2013-08-21 11:39:19 -0400 | [diff] [blame] | 193 | /* retrieve partitions data from string */ |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 194 | for (i = 0; i < p_count; i++) { |
| 195 | tok = strsep(&s, ";"); |
| 196 | |
| 197 | if (tok == NULL) |
| 198 | break; |
| 199 | |
| 200 | /* uuid */ |
| 201 | val = extract_val(tok, "uuid"); |
Rob Herring | 0c7e8d1 | 2015-01-26 09:44:18 -0600 | [diff] [blame] | 202 | if (!val) { |
| 203 | /* 'uuid' is optional if random uuid's are enabled */ |
| 204 | #ifdef CONFIG_RANDOM_UUID |
| 205 | gen_rand_uuid_str(parts[i].uuid, UUID_STR_FORMAT_STD); |
| 206 | #else |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 207 | errno = -4; |
| 208 | goto err; |
Rob Herring | 0c7e8d1 | 2015-01-26 09:44:18 -0600 | [diff] [blame] | 209 | #endif |
| 210 | } else { |
| 211 | if (extract_env(val, &p)) |
| 212 | p = val; |
| 213 | if (strlen(p) >= sizeof(parts[i].uuid)) { |
| 214 | printf("Wrong uuid format for partition %d\n", i); |
| 215 | errno = -4; |
| 216 | goto err; |
| 217 | } |
| 218 | strcpy((char *)parts[i].uuid, p); |
| 219 | free(val); |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 220 | } |
Patrick Delaunay | 7561b25 | 2015-10-27 11:00:27 +0100 | [diff] [blame] | 221 | #ifdef CONFIG_PARTITION_TYPE_GUID |
| 222 | /* guid */ |
| 223 | val = extract_val(tok, "type"); |
| 224 | if (val) { |
| 225 | /* 'type' is optional */ |
| 226 | if (extract_env(val, &p)) |
| 227 | p = val; |
| 228 | if (strlen(p) >= sizeof(parts[i].type_guid)) { |
| 229 | printf("Wrong type guid format for partition %d\n", |
| 230 | i); |
| 231 | errno = -4; |
| 232 | goto err; |
| 233 | } |
| 234 | strcpy((char *)parts[i].type_guid, p); |
| 235 | free(val); |
| 236 | } |
| 237 | #endif |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 238 | /* name */ |
| 239 | val = extract_val(tok, "name"); |
| 240 | if (!val) { /* name is mandatory */ |
| 241 | errno = -4; |
| 242 | goto err; |
| 243 | } |
| 244 | if (extract_env(val, &p)) |
| 245 | p = val; |
| 246 | if (strlen(p) >= sizeof(parts[i].name)) { |
| 247 | errno = -4; |
| 248 | goto err; |
| 249 | } |
| 250 | strcpy((char *)parts[i].name, p); |
| 251 | free(val); |
| 252 | |
| 253 | /* size */ |
| 254 | val = extract_val(tok, "size"); |
| 255 | if (!val) { /* 'size' is mandatory */ |
| 256 | errno = -4; |
| 257 | goto err; |
| 258 | } |
| 259 | if (extract_env(val, &p)) |
| 260 | p = val; |
Piotr Wilczek | 3e34cf7 | 2013-01-27 22:59:25 +0000 | [diff] [blame] | 261 | size_ll = ustrtoull(p, &p, 0); |
| 262 | parts[i].size = lldiv(size_ll, dev_desc->blksz); |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 263 | free(val); |
| 264 | |
| 265 | /* start address */ |
| 266 | val = extract_val(tok, "start"); |
| 267 | if (val) { /* start address is optional */ |
| 268 | if (extract_env(val, &p)) |
| 269 | p = val; |
Piotr Wilczek | 3e34cf7 | 2013-01-27 22:59:25 +0000 | [diff] [blame] | 270 | start_ll = ustrtoull(p, &p, 0); |
| 271 | parts[i].start = lldiv(start_ll, dev_desc->blksz); |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 272 | free(val); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | *parts_count = p_count; |
| 277 | *partitions = parts; |
| 278 | free(str); |
| 279 | |
| 280 | return 0; |
| 281 | err: |
| 282 | free(str); |
| 283 | free(*str_disk_guid); |
| 284 | free(parts); |
| 285 | |
| 286 | return errno; |
| 287 | } |
| 288 | |
Egbert Eich | 619f0fd | 2013-10-04 18:53:04 +0200 | [diff] [blame] | 289 | static int gpt_default(block_dev_desc_t *blk_dev_desc, const char *str_part) |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 290 | { |
| 291 | int ret; |
| 292 | char *str_disk_guid; |
| 293 | u8 part_count = 0; |
| 294 | disk_partition_t *partitions = NULL; |
| 295 | |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 296 | /* fill partitions */ |
Egbert Eich | 619f0fd | 2013-10-04 18:53:04 +0200 | [diff] [blame] | 297 | ret = set_gpt_info(blk_dev_desc, str_part, |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 298 | &str_disk_guid, &partitions, &part_count); |
| 299 | if (ret) { |
| 300 | if (ret == -1) |
| 301 | printf("No partition list provided\n"); |
| 302 | if (ret == -2) |
| 303 | printf("Missing disk guid\n"); |
| 304 | if ((ret == -3) || (ret == -4)) |
| 305 | printf("Partition list incomplete\n"); |
| 306 | return -1; |
| 307 | } |
| 308 | |
| 309 | /* save partitions layout to disk */ |
Rob Herring | a150e6c | 2015-01-26 09:43:15 -0600 | [diff] [blame] | 310 | ret = gpt_restore(blk_dev_desc, str_disk_guid, partitions, part_count); |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 311 | free(str_disk_guid); |
| 312 | free(partitions); |
| 313 | |
Rob Herring | a150e6c | 2015-01-26 09:43:15 -0600 | [diff] [blame] | 314 | return ret; |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /** |
| 318 | * do_gpt(): Perform GPT operations |
| 319 | * |
| 320 | * @param cmdtp - command name |
| 321 | * @param flag |
| 322 | * @param argc |
| 323 | * @param argv |
| 324 | * |
| 325 | * @return zero on success; otherwise error |
| 326 | */ |
| 327 | static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 328 | { |
| 329 | int ret = CMD_RET_SUCCESS; |
| 330 | int dev = 0; |
Egbert Eich | 619f0fd | 2013-10-04 18:53:04 +0200 | [diff] [blame] | 331 | char *ep; |
| 332 | block_dev_desc_t *blk_dev_desc; |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 333 | |
| 334 | if (argc < 5) |
| 335 | return CMD_RET_USAGE; |
| 336 | |
| 337 | /* command: 'write' */ |
| 338 | if ((strcmp(argv[1], "write") == 0) && (argc == 5)) { |
Egbert Eich | 619f0fd | 2013-10-04 18:53:04 +0200 | [diff] [blame] | 339 | dev = (int)simple_strtoul(argv[3], &ep, 10); |
| 340 | if (!ep || ep[0] != '\0') { |
| 341 | printf("'%s' is not a number\n", argv[3]); |
| 342 | return CMD_RET_USAGE; |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 343 | } |
Egbert Eich | 619f0fd | 2013-10-04 18:53:04 +0200 | [diff] [blame] | 344 | blk_dev_desc = get_dev(argv[2], dev); |
| 345 | if (!blk_dev_desc) { |
| 346 | printf("%s: %s dev %d NOT available\n", |
| 347 | __func__, argv[2], dev); |
| 348 | return CMD_RET_FAILURE; |
| 349 | } |
| 350 | |
Przemyslaw Marczak | 3920638 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 351 | puts("Writing GPT: "); |
| 352 | |
| 353 | ret = gpt_default(blk_dev_desc, argv[4]); |
| 354 | if (!ret) { |
| 355 | puts("success!\n"); |
| 356 | return CMD_RET_SUCCESS; |
| 357 | } else { |
| 358 | puts("error!\n"); |
Egbert Eich | 619f0fd | 2013-10-04 18:53:04 +0200 | [diff] [blame] | 359 | return CMD_RET_FAILURE; |
Przemyslaw Marczak | 3920638 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 360 | } |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 361 | } else { |
| 362 | return CMD_RET_USAGE; |
| 363 | } |
| 364 | return ret; |
| 365 | } |
| 366 | |
| 367 | U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt, |
| 368 | "GUID Partition Table", |
Robert P. J. Day | 1f8b546 | 2013-08-21 11:39:19 -0400 | [diff] [blame] | 369 | "<command> <interface> <dev> <partitions_list>\n" |
Lukasz Majewski | 74f889b | 2015-11-13 07:42:10 +0100 | [diff] [blame^] | 370 | " - GUID partition table restoration and validity check\n" |
| 371 | " Restore or verify GPT information on a device connected\n" |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 372 | " to interface\n" |
Lukasz Majewski | 74f889b | 2015-11-13 07:42:10 +0100 | [diff] [blame^] | 373 | " Example usage:\n" |
| 374 | " gpt write mmc 0 $partitions\n" |
| 375 | " gpt verify mmc 0 $partitions\n" |
Piotr Wilczek | 8b09623 | 2012-12-11 11:09:47 +0100 | [diff] [blame] | 376 | ); |