util/smmstoretool: Add support for creating variable from file contents

This helps with initialising UEFI secure boot variables for the first
boot, for example, by setting PKDefault, KEKDefault, dbDefault and
dbxDefault to the desired certificates.

Tested, and the get subcommand returns the same data that the set
command added. However, EDK2's variable driver (from approximately
edk2-stable202505) asserts that the variable store isn't the expected
size, and UEFITool can't decode it correctly. This is also the case for
other types supported before this patch, suggesting that the bug is in
general variable-handling code in this utility. Will be debugged and
addressed in a follow-up.

Change-Id: If36394bb56388a35882702c93e26e63124fe0a63
Signed-off-by: Benjamin Doron <benjamin.doron@9elements.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/88377
Reviewed-by: Angel Pons <th3fanbus@gmail.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
This commit is contained in:
Benjamin Doron 2025-07-10 11:57:41 -04:00 committed by Matt DeVillier
commit 3058464263
3 changed files with 18 additions and 0 deletions

View file

@ -80,6 +80,9 @@ void print_data(const uint8_t data[], size_t data_size, enum data_type type)
printf("%s\n", chars);
free(chars);
break;
case DATA_TYPE_FILE:
fprintf(stderr, "File data type is input only\n");
break;
case DATA_TYPE_RAW:
fwrite(data, 1, data_size, stdout);
break;
@ -115,6 +118,7 @@ void *make_data(const char source[], size_t *data_size, enum data_type type)
void *data;
bool boolean;
uint64_t uint;
struct mem_range_t file;
bool failed;
case DATA_TYPE_BOOL:
@ -173,6 +177,16 @@ void *make_data(const char source[], size_t *data_size, enum data_type type)
return strdup(source);
case DATA_TYPE_UNICODE:
return to_uchars(source, data_size);
case DATA_TYPE_FILE:
file = map_file(source, /*rw=*/false);
if (file.start == NULL || file.length == 0)
return NULL;
*data_size = file.length;
data = xmalloc(*data_size);
memcpy(data, file.start, *data_size);
unmap_file(file);
return data;
case DATA_TYPE_RAW:
fprintf(stderr, "Raw data type is output only\n");
return NULL;
@ -197,6 +211,8 @@ bool parse_data_type(const char str[], enum data_type *type)
*type = DATA_TYPE_ASCII;
else if (str_eq(str, "unicode"))
*type = DATA_TYPE_UNICODE;
else if (str_eq(str, "file"))
*type = DATA_TYPE_FILE;
else if (str_eq(str, "raw"))
*type = DATA_TYPE_RAW;
else

View file

@ -15,6 +15,7 @@ enum data_type {
DATA_TYPE_UINT64,
DATA_TYPE_ASCII,
DATA_TYPE_UNICODE,
DATA_TYPE_FILE,
DATA_TYPE_RAW,
};

View file

@ -128,6 +128,7 @@ static void print_types(FILE *f)
fprintf(f, " * uint64 (0..2^64-1)\n");
fprintf(f, " * ascii (NUL-terminated)\n");
fprintf(f, " * unicode (widened and NUL-terminated)\n");
fprintf(f, " * file (input only; file contents as variable)\n");
fprintf(f, " * raw (output only; raw bytes on output)\n");
}