This is the implementation of the writing part of the library.
Signed-off-by: Tobias Schmidl <[email protected]>
---
env/env_user_config_file.c | 120 ++++++++++++++++++++++++++++++++++++-
tools/bg_setenv.c | 10 ++++
2 files changed, 129 insertions(+), 1 deletion(-)
diff --git a/env/env_user_config_file.c b/env/env_user_config_file.c
index 4b24d9c..80dbafd 100644
--- a/env/env_user_config_file.c
+++ b/env/env_user_config_file.c
@@ -17,6 +17,9 @@
#include <iniparser/iniparser.h>
#include <sys/param.h>
+#define DEFAULT_DICTIONARY_SIZE 16384
+#define DEFAULT_STRING_SIZE 4096
+
#ifndef ERROR
#define ERROR wprintf
#endif
@@ -136,7 +139,117 @@ cleanup_get:
int env_user_config_file_write(const char *file_name, const BG_ENVDATA *data,
bool verbose)
{
- return 1;
+ bool bgenv_verbosity = verbose;
+ VERBOSE(stderr, "entered %s.\n", __PRETTY_FUNCTION__);
+ int return_value = 0;
+
+ dictionary *ini_dict = dictionary_new(DEFAULT_DICTIONARY_SIZE);
+ dictionary_set(ini_dict, BG_ENVDATA_STRING, NULL);
+ char *key_name = alloca(DEFAULT_STRING_SIZE);
+ char *buffer = alloca(DEFAULT_STRING_SIZE);
+ key_name[DEFAULT_STRING_SIZE - 1] = 0;
+
+ { // kernelfile
+ if (data->kernelfile == NULL ||
+ str16to8(buffer, data->kernelfile) == NULL ||
+ snprintf(key_name, DEFAULT_STRING_SIZE - 1,
+ BG_ENVDATA_STRING ":" BG_ENV_KERNELFILE_STRING) <=
+ 0) { /* return with error message */
+ ERROR(L"Cannot parse kernelfile.\n");
+ return_value = EINVAL;
+ goto cleanup_set;
+ }
+ VERBOSE(stderr, "%s=%s\n", key_name, buffer);
+ dictionary_set(ini_dict, key_name, buffer);
+ }
+
+ { // kernelparams
+ if (data->kernelparams == NULL ||
+ str16to8(buffer, data->kernelparams) == NULL ||
+ snprintf(key_name, DEFAULT_STRING_SIZE - 1,
+ BG_ENVDATA_STRING
+ ":" BG_ENV_KERNELPARAMS_STRING) <=
+ 0) { // return with error message
+ ERROR(L"Cannot parse kernelparams.\n");
+ return_value = EINVAL;
+ goto cleanup_set;
+ }
+ VERBOSE(stderr, "%s=%s\n", key_name, buffer);
+ dictionary_set(ini_dict, key_name, buffer);
+ }
+
+ uint32_t revision;
+
+ { // in_progress
+ if (snprintf(key_name, DEFAULT_STRING_SIZE - 1,
+ BG_ENVDATA_STRING
+ ":" BG_ENV_IN_PROGRESS_STRING) <= 0 ||
+ snprintf(buffer, DEFAULT_STRING_SIZE - 1, "%hhu",
+ data->in_progress) <=
+ 0) { // return with error message
+ ERROR(L"Cannot parse in_progress.\n");
+ return_value = EINVAL;
+ goto cleanup_set;
+ }
+ VERBOSE(stderr, "%s=%s\n", key_name, buffer);
+ dictionary_set(ini_dict, key_name, buffer);
+ }
+
+ { // ustate
+ if (snprintf(key_name, DEFAULT_STRING_SIZE - 1,
+ BG_ENVDATA_STRING ":" BG_ENV_USTATE_STRING) <= 0 ||
+ snprintf(buffer, DEFAULT_STRING_SIZE - 1, "%hhu",
+ data->ustate) <= 0) { // return with error message
+ ERROR(L"Cannot parse ustate.\n");
+ return_value = EINVAL;
+ goto cleanup_set;
+ }
+ VERBOSE(stderr, "%s=%s\n", key_name, buffer);
+ dictionary_set(ini_dict, key_name, buffer);
+ }
+
+ { // watchdog_timeout_sec
+ if (snprintf(key_name, DEFAULT_STRING_SIZE - 1,
+ BG_ENVDATA_STRING
+ ":" BG_ENV_WATCHDOG_TIMEOUT_SEC_STRING) <= 0 ||
+ snprintf(buffer, DEFAULT_STRING_SIZE - 1, "%hu",
+ data->watchdog_timeout_sec) <=
+ 0) { // return with error message
+ ERROR(L"Cannot parse watchdog_timeout_sec.\n");
+ return_value = EINVAL;
+ goto cleanup_set;
+ }
+ VERBOSE(stderr, "%s=%s\n", key_name, buffer);
+ dictionary_set(ini_dict, key_name, buffer);
+ }
+
+ { // revision
+ if (snprintf(key_name, DEFAULT_STRING_SIZE - 1,
+ BG_ENVDATA_STRING
+ ":" BG_ENV_REVISION_STRING) <= 0 ||
+ snprintf(buffer, DEFAULT_STRING_SIZE - 1, "0x%08lx",
+ data->revision) <=
+ 0) { // return with error message
+ ERROR(L"Cannot parse revision.\n");
+ return_value = EINVAL;
+ goto cleanup_set;
+ }
+ VERBOSE(stderr, "%s=%s\n", key_name, buffer);
+ dictionary_set(ini_dict, key_name, buffer);
+ }
+
+ FILE *output_file = NULL;
+ if (NULL == (output_file = fopen(file_name, "w"))) {
+ return_value = errno;
+ ERROR(L"Cannot write output INI file.\n");
+ } else {
+ iniparser_dump_ini(ini_dict, output_file);
+ fclose(output_file);
+ }
+cleanup_set:
+ dictionary_del(ini_dict);
+ VERBOSE(stderr, "left %s\n.", __PRETTY_FUNCTION__);
+ return return_value;
}
#ifdef INI_MAIN
@@ -174,6 +287,11 @@ int main(int argc, char *argv[])
int return_value =
env_user_config_file_read(input_file, &data, verbose);
fprintf(stderr, "env_user_config_file_read: %i\n", return_value);
+ if (return_value != 0) return return_value;
+
+ return_value =
+ env_user_config_file_write("/dev/stdout", &data, verbose);
+ fprintf(stderr, "env_user_config_file_write: %i\n", return_value);
return return_value;
}
diff --git a/tools/bg_setenv.c b/tools/bg_setenv.c
index d685412..a145e29 100644
--- a/tools/bg_setenv.c
+++ b/tools/bg_setenv.c
@@ -21,6 +21,7 @@
#include "bg_envtools.h"
#include "bg_setenv.h"
#include "bg_printenv.h"
+#include "env_user_config_file.h"
static char tool_doc[] =
"bg_setenv - Environment tool for the EFI Boot Guard";
@@ -351,6 +352,15 @@ static int dumpenv_to_file(char *envfilepath, bool
verbosity, bool preserve_env)
if (verbosity) {
dump_env(env.data, &ALL_FIELDS, false);
}
+
+ /* compare the filename. if we operate on bgenv.dat, use the old way,
+ * otherwise try the INI file approach. */
+ const char *file_name = strrchr(envfilepath, '/');
+ file_name = (file_name != NULL) ? file_name + 1 : envfilepath;
+
+ if (strcasecmp(file_name, FAT_ENV_INI_FILENAME) == 0)
+ return env_user_config_file_write(envfilepath, &data,
verbosity);
+
FILE *of = fopen(envfilepath, "wb");
if (of) {
if (fwrite(&data, sizeof(BG_ENVDATA), 1, of) != 1) {
--
2.36.1
--
You received this message because you are subscribed to the Google Groups "EFI
Boot Guard" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/efibootguard-dev/20220812114246.1727090-4-tobiasschmidl%40siemens.com.