Newer
Older
/*
* cmd_gpt.c -- GPT (GUID Partition Table) handling command
*
* Copyright (C) 2015
* Lukasz Majewski <l.majewski@majess.pl>
*
* Copyright (C) 2012 Samsung Electronics
* author: Lukasz Majewski <l.majewski@samsung.com>
* author: Piotr Wilczek <p.wilczek@samsung.com>
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <malloc.h>
#include <command.h>
#include <part_efi.h>
#include <exports.h>
#include <linux/ctype.h>
#include <memalign.h>
#include <linux/compat.h>
static LIST_HEAD(disk_partitions);
/**
* extract_env(): Expand env name from string format '&{env_name}'
* and return pointer to the env (if the env is set)
*
* @param str - pointer to string
* @param env - pointer to pointer to extracted env
*
* @return - zero on successful expand and env is set
*/
static int extract_env(const char *str, char **env)
int ret = -1;
#ifdef CONFIG_RANDOM_UUID
char uuid_str[UUID_STR_LEN + 1];
#endif
if (!str || strlen(str) < 4)
return -1;
if (!((strncmp(str, "${", 2) == 0) && (str[strlen(str) - 1] == '}')))
return -1;
s = strdup(str);
if (s == NULL)
return -1;
memset(s + strlen(s) - 1, '\0', 1);
memmove(s, s + 2, strlen(s) - 1);
e = getenv(s);
if (e == NULL) {
#ifdef CONFIG_RANDOM_UUID
debug("%s unset. ", str);
gen_rand_uuid_str(uuid_str, UUID_STR_FORMAT_GUID);
setenv(s, uuid_str);
if (e) {
debug("Set to random.\n");
ret = 0;
} else {
debug("Can't get random UUID.\n");
#else
debug("%s unset.\n", str);
#endif
} else {
debug("%s get from environment.\n", str);
ret = 0;
*env = e;
free(s);
return ret;
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
}
/**
* extract_val(): Extract value from a key=value pair list (comma separated).
* Only value for the given key is returend.
* Function allocates memory for the value, remember to free!
*
* @param str - pointer to string with key=values pairs
* @param key - pointer to the key to search for
*
* @return - pointer to allocated string with the value
*/
static char *extract_val(const char *str, const char *key)
{
char *v, *k;
char *s, *strcopy;
char *new = NULL;
strcopy = strdup(str);
if (strcopy == NULL)
return NULL;
s = strcopy;
while (s) {
v = strsep(&s, ",");
if (!v)
break;
k = strsep(&v, "=");
if (!k)
break;
if (strcmp(k, key) == 0) {
new = strdup(v);
break;
}
}
free(strcopy);
return new;
}
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* found_key(): Found key without value in parameter list (comma separated).
*
* @param str - pointer to string with key
* @param key - pointer to the key to search for
*
* @return - true on found key
*/
static bool found_key(const char *str, const char *key)
{
char *k;
char *s, *strcopy;
bool result = false;
strcopy = strdup(str);
if (!strcopy)
return NULL;
s = strcopy;
while (s) {
k = strsep(&s, ",");
if (!k)
break;
if (strcmp(k, key) == 0) {
result = true;
break;
}
}
free(strcopy);
return result;
}
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#ifdef CONFIG_CMD_GPT_RENAME
static void del_gpt_info(void)
{
struct list_head *pos = &disk_partitions;
struct disk_part *curr;
while (!list_empty(pos)) {
curr = list_entry(pos->next, struct disk_part, list);
list_del(pos->next);
free(curr);
}
}
static struct disk_part *allocate_disk_part(disk_partition_t *info, int partnum)
{
struct disk_part *newpart;
newpart = malloc(sizeof(*newpart));
if (!newpart)
return ERR_PTR(-ENOMEM);
memset(newpart, '\0', sizeof(newpart));
newpart->gpt_part_info.start = info->start;
newpart->gpt_part_info.size = info->size;
newpart->gpt_part_info.blksz = info->blksz;
strncpy((char *)newpart->gpt_part_info.name, (const char *)info->name,
PART_NAME_LEN);
newpart->gpt_part_info.name[PART_NAME_LEN - 1] = '\0';
strncpy((char *)newpart->gpt_part_info.type, (const char *)info->type,
PART_TYPE_LEN);
newpart->gpt_part_info.type[PART_TYPE_LEN - 1] = '\0';
newpart->gpt_part_info.bootable = info->bootable;
#ifdef CONFIG_PARTITION_UUIDS
strncpy(newpart->gpt_part_info.uuid, (const char *)info->uuid,
UUID_STR_LEN);
/* UUID_STR_LEN is correct, as uuid[]'s length is UUID_STR_LEN+1 chars */
newpart->gpt_part_info.uuid[UUID_STR_LEN] = '\0';
#endif
newpart->partnum = partnum;
return newpart;
}
static void print_gpt_info(void)
{
struct list_head *pos;
struct disk_part *curr;
list_for_each(pos, &disk_partitions) {
curr = list_entry(pos, struct disk_part, list);
printf("Partition %d:\n", curr->partnum);
printf("1st block %x, size %x\n", (unsigned)curr->gpt_part_info.start,
(unsigned)curr->gpt_part_info.size);
printf("Block size %lu, name %s\n", curr->gpt_part_info.blksz,
curr->gpt_part_info.name);
printf("Type %s, bootable %d\n", curr->gpt_part_info.type,
curr->gpt_part_info.bootable);
#ifdef CONFIG_PARTITION_UUIDS
printf("UUID %s\n", curr->gpt_part_info.uuid);
#endif
printf("\n");
}
}
/*
* read partition info into disk_partitions list where
* it can be printed or modified
*/
static int get_gpt_info(struct blk_desc *dev_desc)
{
/* start partition numbering at 1, as U-Boot does */
int valid_parts = 0, p, ret;
disk_partition_t info;
struct disk_part *new_disk_part;
if (disk_partitions.next == NULL)
INIT_LIST_HEAD(&disk_partitions);
for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
ret = part_get_info(dev_desc, p, &info);
if (ret)
continue;
/* Add 1 here because counter is zero-based but p1 is
the first partition */
new_disk_part = allocate_disk_part(&info, valid_parts+1);
if (IS_ERR(new_disk_part))
goto out;
list_add_tail(&new_disk_part->list, &disk_partitions);
valid_parts++;
}
if (valid_parts == 0) {
printf("** No valid partitions found **\n");
goto out;
}
return valid_parts;
out:
if (valid_parts >= 1)
del_gpt_info();
return -ENODEV;
}
/* a wrapper to test get_gpt_info */
static int do_get_gpt_info(struct blk_desc *dev_desc)
{
int ret;
ret = get_gpt_info(dev_desc);
if (ret > 0) {
print_gpt_info();
del_gpt_info();
return 0;
}
return ret;
}
#endif
/**
* set_gpt_info(): Fill partition information from string
* function allocates memory, remember to free!
*
* @param dev_desc - pointer block device descriptor
* @param str_part - pointer to string with partition information
* @param str_disk_guid - pointer to pointer to allocated string with disk guid
* @param partitions - pointer to pointer to allocated partitions array
* @param parts_count - number of partitions
*
* @return - zero on success, otherwise error
*
*/
static int set_gpt_info(struct blk_desc *dev_desc,
const char *str_part,
char **str_disk_guid,
disk_partition_t **partitions,
u8 *parts_count)
{
char *tok, *str, *s;
int i;
char *val, *p;
int p_count;
disk_partition_t *parts;
int errno = 0;
debug("%s: lba num: 0x%x %d\n", __func__,
(unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba);
if (str_part == NULL)
return -1;
str = strdup(str_part);
/* extract disk guid */
s = str;
val = extract_val(str, "uuid_disk");
#ifdef CONFIG_RANDOM_UUID
*str_disk_guid = malloc(UUID_STR_LEN + 1);
gen_rand_uuid_str(*str_disk_guid, UUID_STR_FORMAT_STD);
#else
#endif
} else {
val = strsep(&val, ";");
if (extract_env(val, &p))
p = val;
*str_disk_guid = strdup(p);
free(val);
/* Move s to first partition */
strsep(&s, ";");
}
if (strlen(s) == 0)
return -3;
i = strlen(s) - 1;
if (s[i] == ';')
s[i] = '\0';
/* calculate expected number of partitions */
p_count = 1;
p = s;
while (*p) {
if (*p++ == ';')
p_count++;
}
/* allocate memory for partitions */
parts = calloc(sizeof(disk_partition_t), p_count);
/* retrieve partitions data from string */
for (i = 0; i < p_count; i++) {
tok = strsep(&s, ";");
if (tok == NULL)
break;
/* uuid */
val = extract_val(tok, "uuid");
if (!val) {
/* 'uuid' is optional if random uuid's are enabled */
#ifdef CONFIG_RANDOM_UUID
gen_rand_uuid_str(parts[i].uuid, UUID_STR_FORMAT_STD);
#else
#endif
} else {
if (extract_env(val, &p))
p = val;
if (strlen(p) >= sizeof(parts[i].uuid)) {
printf("Wrong uuid format for partition %d\n", i);
errno = -4;
goto err;
}
strcpy((char *)parts[i].uuid, p);
free(val);
#ifdef CONFIG_PARTITION_TYPE_GUID
/* guid */
val = extract_val(tok, "type");
if (val) {
/* 'type' is optional */
if (extract_env(val, &p))
p = val;
if (strlen(p) >= sizeof(parts[i].type_guid)) {
printf("Wrong type guid format for partition %d\n",
i);
errno = -4;
goto err;
}
strcpy((char *)parts[i].type_guid, p);
free(val);
}
#endif
/* name */
val = extract_val(tok, "name");
if (!val) { /* name is mandatory */
errno = -4;
goto err;
}
if (extract_env(val, &p))
p = val;
if (strlen(p) >= sizeof(parts[i].name)) {
errno = -4;
goto err;
}
strcpy((char *)parts[i].name, p);
free(val);
/* size */
val = extract_val(tok, "size");
if (!val) { /* 'size' is mandatory */
errno = -4;
goto err;
}
if (extract_env(val, &p))
p = val;
/* Let part efi module to auto extend the size */
parts[i].size = 0;
} else {
size_ll = ustrtoull(p, &p, 0);
parts[i].size = lldiv(size_ll, dev_desc->blksz);
}
free(val);
/* start address */
val = extract_val(tok, "start");
if (val) { /* start address is optional */
if (extract_env(val, &p))
p = val;
start_ll = ustrtoull(p, &p, 0);
parts[i].start = lldiv(start_ll, dev_desc->blksz);
offset += parts[i].size + parts[i].start;
/* bootable */
if (found_key(tok, "bootable"))
parts[i].bootable = 1;
}
*parts_count = p_count;
*partitions = parts;
free(str);
return 0;
err:
free(str);
free(*str_disk_guid);
free(parts);
return errno;
}
static int gpt_default(struct blk_desc *blk_dev_desc, const char *str_part)
{
int ret;
char *str_disk_guid;
u8 part_count = 0;
disk_partition_t *partitions = NULL;
/* fill partitions */
ret = set_gpt_info(blk_dev_desc, str_part,
&str_disk_guid, &partitions, &part_count);
if (ret) {
if (ret == -1)
printf("No partition list provided\n");
if (ret == -2)
printf("Missing disk guid\n");
if ((ret == -3) || (ret == -4))
printf("Partition list incomplete\n");
return -1;
}
/* save partitions layout to disk */
ret = gpt_restore(blk_dev_desc, str_disk_guid, partitions, part_count);
free(str_disk_guid);
free(partitions);
static int gpt_verify(struct blk_desc *blk_dev_desc, const char *str_part)
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
{
ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1,
blk_dev_desc->blksz);
disk_partition_t *partitions = NULL;
gpt_entry *gpt_pte = NULL;
char *str_disk_guid;
u8 part_count = 0;
int ret = 0;
/* fill partitions */
ret = set_gpt_info(blk_dev_desc, str_part,
&str_disk_guid, &partitions, &part_count);
if (ret) {
if (ret == -1) {
printf("No partition list provided - only basic check\n");
ret = gpt_verify_headers(blk_dev_desc, gpt_head,
&gpt_pte);
goto out;
}
if (ret == -2)
printf("Missing disk guid\n");
if ((ret == -3) || (ret == -4))
printf("Partition list incomplete\n");
return -1;
}
/* Check partition layout with provided pattern */
ret = gpt_verify_partitions(blk_dev_desc, partitions, part_count,
gpt_head, &gpt_pte);
free(str_disk_guid);
free(partitions);
out:
free(gpt_pte);
return ret;
}
static int do_disk_guid(struct blk_desc *dev_desc, char * const namestr)
{
int ret;
char disk_guid[UUID_STR_LEN + 1];
ret = get_disk_guid(dev_desc, disk_guid);
if (ret < 0)
return CMD_RET_FAILURE;
if (namestr)
setenv(namestr, disk_guid);
else
printf("%s\n", disk_guid);
return ret;
}
/**
* do_gpt(): Perform GPT operations
*
* @param cmdtp - command name
* @param flag
* @param argc
* @param argv
*
* @return zero on success; otherwise error
*/
static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
int ret = CMD_RET_SUCCESS;
int dev = 0;
struct blk_desc *blk_dev_desc = NULL;
if (argc < 4 || argc > 5)
dev = (int)simple_strtoul(argv[3], &ep, 10);
if (!ep || ep[0] != '\0') {
printf("'%s' is not a number\n", argv[3]);
return CMD_RET_USAGE;
}
blk_dev_desc = blk_get_dev(argv[2], dev);
if (!blk_dev_desc) {
printf("%s: %s dev %d NOT available\n",
__func__, argv[2], dev);
return CMD_RET_FAILURE;
}
if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
printf("Writing GPT: ");
ret = gpt_default(blk_dev_desc, argv[4]);
} else if ((strcmp(argv[1], "verify") == 0)) {
ret = gpt_verify(blk_dev_desc, argv[4]);
printf("Verify GPT: ");
} else if (strcmp(argv[1], "guid") == 0) {
ret = do_disk_guid(blk_dev_desc, argv[4]);
#ifdef CONFIG_CMD_GPT_RENAME
} else if (strcmp(argv[1], "read") == 0) {
ret = do_get_gpt_info(blk_dev_desc);
#endif
} else {
return CMD_RET_USAGE;
}
if (ret) {
printf("error!\n");
return CMD_RET_FAILURE;
}
printf("success!\n");
return CMD_RET_SUCCESS;
}
U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
"GUID Partition Table",
"<command> <interface> <dev> <partitions_list>\n"
" - GUID partition table restoration and validity check\n"
" Restore or verify GPT information on a device connected\n"
" Example usage:\n"
" gpt write mmc 0 $partitions\n"
" gpt verify mmc 0 $partitions\n"
" read <interface> <dev>\n"
" - read GPT into a data structure for manipulation\n"
" guid <interface> <dev>\n"
" - print disk GUID\n"
" guid <interface> <dev> <varname>\n"
" - set environment variable to disk GUID\n"
" Example usage:\n"
" gpt guid mmc 0\n"
" gpt guid mmc 0 varname\n"