util/smmstoretool: add uint64 data type

It's in particular useful for working with variables that contain 64-bit
pointers, like CapsuleUpdateData* global variables defined by UEFI
specification.

Change-Id: I4b46b41cdc5f69d4ca189659bef1e44f64c0d554
Signed-off-by: Sergii Dmytruk <sergii.dmytruk@3mdeb.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/82611
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Krystian Hebel <krystian.hebel@3mdeb.com>
This commit is contained in:
Sergii Dmytruk
2024-05-22 20:41:02 +03:00
committed by Felix Held
parent dd6c3b4a61
commit d20cc994ba
3 changed files with 41 additions and 13 deletions

View File

@ -57,6 +57,16 @@ void print_data(const uint8_t data[], size_t data_size, enum data_type type)
if (data_size >= 4) if (data_size >= 4)
printf("%u\n", *(uint32_t *)data); printf("%u\n", *(uint32_t *)data);
break; break;
case DATA_TYPE_UINT64:
if (data_size != 8) {
fprintf(stderr,
"warning: expected size of 8, got %zu\n",
data_size);
}
if (data_size >= 8)
printf("%llu\n", (unsigned long long)*(uint64_t *)data);
break;
case DATA_TYPE_ASCII: case DATA_TYPE_ASCII:
for (size_t i = 0; i < data_size; ++i) { for (size_t i = 0; i < data_size; ++i) {
char c = data[i]; char c = data[i];
@ -78,20 +88,24 @@ void print_data(const uint8_t data[], size_t data_size, enum data_type type)
static uint64_t parse_uint(const char source[], static uint64_t parse_uint(const char source[],
const char type[], const char type[],
unsigned long long max) unsigned long long max,
bool *failed)
{ {
char *end; char *end;
unsigned long long uint = strtoull(source, &end, /*base=*/0); unsigned long long uint = strtoull(source, &end, /*base=*/0);
if (*end != '\0') { if (*end != '\0') {
fprintf(stderr, "Trailing characters in \"%s\": %s\n", fprintf(stderr, "Trailing characters in \"%s\": %s\n",
source, end); source, end);
return UINT64_MAX; *failed = true;
return 0;
} }
if (uint > max) { if (uint > max) {
fprintf(stderr, "Invalid %s value: %llu\n", type, uint); fprintf(stderr, "Invalid %s value: %llu\n", type, uint);
return UINT64_MAX; *failed = true;
return 0;
} }
*failed = false;
return uint; return uint;
} }
@ -100,7 +114,8 @@ void *make_data(const char source[], size_t *data_size, enum data_type type)
switch (type) { switch (type) {
void *data; void *data;
bool boolean; bool boolean;
unsigned long long uint; uint64_t uint;
bool failed;
case DATA_TYPE_BOOL: case DATA_TYPE_BOOL:
if (str_eq(source, "true")) { if (str_eq(source, "true")) {
@ -118,8 +133,8 @@ void *make_data(const char source[], size_t *data_size, enum data_type type)
*(uint8_t *)data = boolean; *(uint8_t *)data = boolean;
return data; return data;
case DATA_TYPE_UINT8: case DATA_TYPE_UINT8:
uint = parse_uint(source, "uint8", UINT8_MAX); uint = parse_uint(source, "uint8", UINT8_MAX, &failed);
if (uint == UINT64_MAX) if (failed)
return NULL; return NULL;
*data_size = 1; *data_size = 1;
@ -127,8 +142,8 @@ void *make_data(const char source[], size_t *data_size, enum data_type type)
*(uint8_t *)data = uint; *(uint8_t *)data = uint;
return data; return data;
case DATA_TYPE_UINT16: case DATA_TYPE_UINT16:
uint = parse_uint(source, "uint16", UINT16_MAX); uint = parse_uint(source, "uint16", UINT16_MAX, &failed);
if (uint == UINT64_MAX) if (failed)
return NULL; return NULL;
*data_size = 2; *data_size = 2;
@ -136,14 +151,23 @@ void *make_data(const char source[], size_t *data_size, enum data_type type)
*(uint16_t *)data = uint; *(uint16_t *)data = uint;
return data; return data;
case DATA_TYPE_UINT32: case DATA_TYPE_UINT32:
uint = parse_uint(source, "uint32", UINT32_MAX); uint = parse_uint(source, "uint32", UINT32_MAX, &failed);
if (uint == UINT64_MAX) if (failed)
return NULL; return NULL;
*data_size = 4; *data_size = 4;
data = xmalloc(*data_size); data = xmalloc(*data_size);
*(uint32_t *)data = uint; *(uint32_t *)data = uint;
return data; return data;
case DATA_TYPE_UINT64:
uint = parse_uint(source, "uint64", UINT64_MAX, &failed);
if (failed)
return NULL;
*data_size = 8;
data = xmalloc(*data_size);
*(uint64_t *)data = uint;
return data;
case DATA_TYPE_ASCII: case DATA_TYPE_ASCII:
*data_size = strlen(source) + 1; *data_size = strlen(source) + 1;
return strdup(source); return strdup(source);
@ -167,6 +191,8 @@ bool parse_data_type(const char str[], enum data_type *type)
*type = DATA_TYPE_UINT16; *type = DATA_TYPE_UINT16;
else if (str_eq(str, "uint32")) else if (str_eq(str, "uint32"))
*type = DATA_TYPE_UINT32; *type = DATA_TYPE_UINT32;
else if (str_eq(str, "uint64"))
*type = DATA_TYPE_UINT64;
else if (str_eq(str, "ascii")) else if (str_eq(str, "ascii"))
*type = DATA_TYPE_ASCII; *type = DATA_TYPE_ASCII;
else if (str_eq(str, "unicode")) else if (str_eq(str, "unicode"))

View File

@ -12,6 +12,7 @@ enum data_type {
DATA_TYPE_UINT8, DATA_TYPE_UINT8,
DATA_TYPE_UINT16, DATA_TYPE_UINT16,
DATA_TYPE_UINT32, DATA_TYPE_UINT32,
DATA_TYPE_UINT64,
DATA_TYPE_ASCII, DATA_TYPE_ASCII,
DATA_TYPE_UNICODE, DATA_TYPE_UNICODE,
DATA_TYPE_RAW, DATA_TYPE_RAW,

View File

@ -122,9 +122,10 @@ static void print_types(FILE *f)
{ {
fprintf(f, "Types and their values:\n"); fprintf(f, "Types and their values:\n");
fprintf(f, " * bool (true, false)\n"); fprintf(f, " * bool (true, false)\n");
fprintf(f, " * uint8 (0-255)\n"); fprintf(f, " * uint8 (0..255)\n");
fprintf(f, " * uint16 (0-65535)\n"); fprintf(f, " * uint16 (0..65535)\n");
fprintf(f, " * uint32 (0-4294967295)\n"); fprintf(f, " * uint32 (0..4294967295)\n");
fprintf(f, " * uint64 (0..2^64-1)\n");
fprintf(f, " * ascii (NUL-terminated)\n"); fprintf(f, " * ascii (NUL-terminated)\n");
fprintf(f, " * unicode (widened and NUL-terminated)\n"); fprintf(f, " * unicode (widened and NUL-terminated)\n");
fprintf(f, " * raw (output only; raw bytes on output)\n"); fprintf(f, " * raw (output only; raw bytes on output)\n");