From 30584642632d5691c24e9c697b288aff9ac7120d Mon Sep 17 00:00:00 2001 From: Benjamin Doron Date: Thu, 10 Jul 2025 11:57:41 -0400 Subject: [PATCH] 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 Reviewed-on: https://review.coreboot.org/c/coreboot/+/88377 Reviewed-by: Angel Pons Tested-by: build bot (Jenkins) --- util/smmstoretool/data.c | 16 ++++++++++++++++ util/smmstoretool/data.h | 1 + util/smmstoretool/main.c | 1 + 3 files changed, 18 insertions(+) diff --git a/util/smmstoretool/data.c b/util/smmstoretool/data.c index 41d3b75aba..5eca5f6a81 100644 --- a/util/smmstoretool/data.c +++ b/util/smmstoretool/data.c @@ -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 diff --git a/util/smmstoretool/data.h b/util/smmstoretool/data.h index 58e6c7d7a6..9bd4506856 100644 --- a/util/smmstoretool/data.h +++ b/util/smmstoretool/data.h @@ -15,6 +15,7 @@ enum data_type { DATA_TYPE_UINT64, DATA_TYPE_ASCII, DATA_TYPE_UNICODE, + DATA_TYPE_FILE, DATA_TYPE_RAW, }; diff --git a/util/smmstoretool/main.c b/util/smmstoretool/main.c index 89d58d619a..4092fc59dc 100644 --- a/util/smmstoretool/main.c +++ b/util/smmstoretool/main.c @@ -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"); }