Signed-off-by: Tobias Schmidl <[email protected]>
---
Makefile.am | 1 +
env/env_user_config_file.c | 167 +++++++++++++++++++++++++++++++++
include/env_user_config_file.h | 23 +++++
include/envdata.h | 1 +
4 files changed, 192 insertions(+)
create mode 100644 env/env_user_config_file.c
create mode 100644 include/env_user_config_file.h
diff --git a/Makefile.am b/Makefile.am
index 48c560f..b5384c9 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -89,6 +89,7 @@ libebgenv_a_SOURCES = \
env/env_config_file.c \
env/env_config_partitions.c \
env/env_disk_utils.c \
+ env/env_user_config_file.c \
env/uservars.c \
tools/ebgpart.c
diff --git a/env/env_user_config_file.c b/env/env_user_config_file.c
new file mode 100644
index 0000000..8d20ed3
--- /dev/null
+++ b/env/env_user_config_file.c
@@ -0,0 +1,167 @@
+/*
+ * EFI Boot Guard
+ *
+ * Copyright (c) Siemens AG, 2022
+ *
+ * Authors:
+ * Tobias Schmidl <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2. See
+ * the COPYING file in the top-level directory.
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ */
+
+#include "env_user_config_file.h"
+#include "version.h"
+#include <iniparser/iniparser.h>
+#include <sys/param.h>
+
+#ifndef ERROR
+#define ERROR wprintf
+#endif
+
+#ifndef INFO
+#define INFO wprintf
+#endif
+
+#ifndef WARNING
+#define WARNING wprintf
+#endif
+
+extern EBGENVKEY bgenv_str2enum(char *key);
+
+int env_user_config_file_read(const char *file_name, BG_ENVDATA *data,
+ bool verbose)
+{
+ bool bgenv_verbosity = verbose;
+ VERBOSE(stderr, "entered %s.\n", __PRETTY_FUNCTION__);
+ int return_value = 0;
+
+ dictionary *ini_file = iniparser_load(file_name);
+ if (ini_file == NULL) { // exit if we cannot parse the ini file
+ ERROR(L"Cannot parse the INI file.\n");
+ return EINVAL;
+ }
+
+ const int number_of_sections = iniparser_getnsec(ini_file);
+ VERBOSE(stderr, "found %i sections.\n", number_of_sections);
+ for (int i = 0; i < number_of_sections;
+ ++i) { // iterate over all sections
+ const char *section_name = iniparser_getsecname(ini_file, i);
+ const int number_of_keys =
+ iniparser_getsecnkeys(ini_file, section_name);
+ VERBOSE(stderr, "Section: %s (%i keys)\n", section_name,
+ number_of_keys);
+
+ /* we're not in the correct section */
+ if (strcmp(section_name, BG_ENVDATA_STRING) != 0) continue;
+
+ /* get the section keys */
+ const char *keys[number_of_keys + 1];
+ if (NULL ==
+ iniparser_getseckeys(
+ ini_file, section_name,
+ keys)) { // exit if we cannot get the sections
+ ERROR(L"Cannot get the section keys.\n");
+ return_value = EINVAL;
+ goto cleanup_get;
+ }
+
+ int prefix_index = 0;
+ { // get key prefix
+ const char *first_string = keys[0];
+ for (; prefix_index < strlen(first_string);
+ ++prefix_index)
+ if (first_string[prefix_index] == ':') break;
+ if (prefix_index + 1 < strlen(first_string))
+ VERBOSE(stderr, "index: %i\n", prefix_index);
+ }
+
+ for (int j = 0; j < number_of_keys;
+ ++j) { // iterate over all keys
+ const char *key = keys[j] + prefix_index + 1;
+ const char *value =
+ iniparser_getstring(ini_file, keys[j], NULL);
+ if (strcmp(key, "kernelfile") == 0 &&
+ strlen(value) < ENV_STRING_LENGTH) {
+ str8to16(data->kernelfile, value);
+ VERBOSE(stderr, "kernelfile: %s\n", value);
+ } else if (strcmp(key, "kernelparams") == 0 &&
+ strlen(value) < ENV_STRING_LENGTH) {
+ str8to16(data->kernelparams, value);
+ VERBOSE(stderr, "kernelparams: %s\n", value);
+ } else if (strcmp(key, "in_progress") == 0) {
+ data->in_progress = MIN(
+ strtoul(value, NULL, 10), UINT8_MAX);
+ VERBOSE(stdout, "in_progress: %hhu\n",
+ data->in_progress);
+ } else if (strcmp(key, "ustate") == 0) {
+ data->ustate = MIN(strtoul(value, NULL, 10),
+ UINT8_MAX);
+ VERBOSE(stdout, "ustate: %hhu\n", data->ustate);
+ } else if (strcmp(key, "watchdog_timeout_sec") == 0) {
+ data->watchdog_timeout_sec = MIN(
+ strtoul(value, NULL, 10), UINT16_MAX);
+ VERBOSE(stdout, "watchdog_timeout_sec: %hu\n",
+ data->watchdog_timeout_sec);
+ } else if (strcmp(key, "revision") == 0) {
+ data->revision = strtoul(value, NULL, 0);
+ VERBOSE(stdout, "revision: 0x%08lx\n",
+ data->revision);
+ } else
+ VERBOSE(stderr, "<unknown>: %s\n", value);
+ }
+ }
+
+cleanup_get:
+ iniparser_freedict(ini_file);
+ VERBOSE(stderr, "left %s\n.", __PRETTY_FUNCTION__);
+ return return_value;
+}
+
+int env_user_config_file_write(const char *file_name, const BG_ENVDATA *data,
+ bool verbose)
+{
+ return 1;
+}
+
+#ifdef INI_MAIN
+
+void print_usage_and_exit(const char *program_name) __attribute__((noreturn));
+void print_usage_and_exit(const char *program_name)
+{
+ fprintf(stderr, "Usage: %s [-v] -i <inputfile>\n", program_name);
+ fprintf(stderr, "\nWhere:\n\t-v\t\t\tMore verbose output\n\t-i "
+ "<inputfile>\t\tInput ini\n");
+ exit(1);
+}
+
+int main(int argc, char *argv[])
+{
+ fprintf(stderr, "version: %s\n", EFIBOOTGUARD_VERSION);
+ const char *input_file = NULL;
+ bool verbose = false;
+ for (int opt = 0; - 1 != (opt = getopt(argc, argv, "vi:"));) {
+ switch (opt) {
+ case 'v':
+ verbose = true;
+ break;
+ case 'i':
+ input_file = optarg;
+ break;
+ default:
+ print_usage_and_exit(argv[0]);
+ break;
+ }
+ }
+ if (input_file == NULL) print_usage_and_exit(argv[0]);
+
+ BG_ENVDATA data;
+ int return_value =
+ env_user_config_file_read(input_file, &data, verbose);
+ fprintf(stderr, "env_user_config_file_read: %i\n", return_value);
+ return return_value;
+}
+
+#endif
diff --git a/include/env_user_config_file.h b/include/env_user_config_file.h
new file mode 100644
index 0000000..a74714e
--- /dev/null
+++ b/include/env_user_config_file.h
@@ -0,0 +1,23 @@
+/*
+ * EFI Boot Guard
+ *
+ * Copyright (c) Siemens AG, 2017
+ *
+ * Authors:
+ * Andreas Reichel <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2. See
+ * the COPYING file in the top-level directory.
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ */
+
+#pragma once
+
+#include <stdio.h>
+#include "env_api.h"
+
+int env_user_config_file_read(const char *file_name, BG_ENVDATA *data,
+ bool verbose);
+int env_user_config_file_write(const char *file_name, const BG_ENVDATA *data,
+ bool verbose);
diff --git a/include/envdata.h b/include/envdata.h
index fddfa7f..7c3cfca 100644
--- a/include/envdata.h
+++ b/include/envdata.h
@@ -17,6 +17,7 @@
#include <stdint.h>
#define FAT_ENV_FILENAME "BGENV.DAT"
+#define BG_ENVDATA_STRING "bg_envdata"
#define ENV_STRING_LENGTH 255
#define USTATE_OK 0
--
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-2-tobiasschmidl%40siemens.com.