Signed-off-by: Tobias Schmidl <[email protected]>
---
 env/env_user_config_file.c | 120 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 119 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;
 }
 
-- 
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/20220809135300.1470407-6-tobiasschmidl%40siemens.com.

Reply via email to