]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
boot: Move parse_boolean to efi-string
authorJan Janssen <medhefgo@web.de>
Sun, 15 Oct 2023 07:31:02 +0000 (09:31 +0200)
committerJan Janssen <medhefgo@web.de>
Sun, 15 Oct 2023 15:41:25 +0000 (17:41 +0200)
No functional changes.

src/boot/efi/boot.c
src/boot/efi/efi-string.c
src/boot/efi/efi-string.h
src/boot/efi/test-efi-string.c
src/boot/efi/util.c
src/boot/efi/util.h

index 2c2d0bf735a174118638d96a1e2b127a9518e891..a4496aed782ec4183e46c6e4d77c2d430f19cd42 100644 (file)
@@ -1244,7 +1244,6 @@ static void config_defaults_load_from_file(Config *config, char *content) {
         char *line;
         size_t pos = 0;
         char *key, *value;
-        EFI_STATUS err;
 
         assert(config);
         assert(content);
@@ -1275,38 +1274,31 @@ static void config_defaults_load_from_file(Config *config, char *content) {
                         config->entry_default_config = xstr8_to_16(value);
 
                 } else if (streq8(key, "editor")) {
-                        err = parse_boolean(value, &config->editor);
-                        if (err != EFI_SUCCESS)
+                        if (!parse_boolean(value, &config->editor))
                                 log_error("Error parsing 'editor' config option, ignoring: %s", value);
 
                 } else if (streq8(key, "auto-entries")) {
-                        err = parse_boolean(value, &config->auto_entries);
-                        if (err != EFI_SUCCESS)
+                        if (!parse_boolean(value, &config->auto_entries))
                                 log_error("Error parsing 'auto-entries' config option, ignoring: %s", value);
 
                 } else if (streq8(key, "auto-firmware")) {
-                        err = parse_boolean(value, &config->auto_firmware);
-                        if (err != EFI_SUCCESS)
+                        if (!parse_boolean(value, &config->auto_firmware))
                                 log_error("Error parsing 'auto-firmware' config option, ignoring: %s", value);
 
                 } else if (streq8(key, "auto-poweroff")) {
-                        err = parse_boolean(value, &config->auto_poweroff);
-                        if (err != EFI_SUCCESS)
+                        if (!parse_boolean(value, &config->auto_poweroff))
                                 log_error("Error parsing 'auto-poweroff' config option, ignoring: %s", value);
 
                 } else if (streq8(key, "auto-reboot")) {
-                        err = parse_boolean(value, &config->auto_reboot);
-                        if (err != EFI_SUCCESS)
+                        if (!parse_boolean(value, &config->auto_reboot))
                                 log_error("Error parsing 'auto-reboot' config option, ignoring: %s", value);
 
                 } else if (streq8(key, "beep")) {
-                        err = parse_boolean(value, &config->beep);
-                        if (err != EFI_SUCCESS)
+                        if (!parse_boolean(value, &config->beep))
                                 log_error("Error parsing 'beep' config option, ignoring: %s", value);
 
                 } else if (streq8(key, "reboot-for-bitlocker")) {
-                        err = parse_boolean(value, &config->reboot_for_bitlocker);
-                        if (err != EFI_SUCCESS)
+                        if (!parse_boolean(value, &config->reboot_for_bitlocker))
                                 log_error("Error parsing 'reboot-for-bitlocker' config option, ignoring: %s",
                                           value);
 
index ffe855245304795641e9ed69a6e1f3954e25913e..822be06520955a2d3212237cc10dcff3a4f21d89 100644 (file)
@@ -391,6 +391,27 @@ bool efi_fnmatch(const char16_t *pattern, const char16_t *haystack) {
 DEFINE_PARSE_NUMBER(char, parse_number8);
 DEFINE_PARSE_NUMBER(char16_t, parse_number16);
 
+bool parse_boolean(const char *v, bool *ret) {
+        assert(ret);
+
+        if (!v)
+                return false;
+
+        if (streq8(v, "1") || streq8(v, "yes") || streq8(v, "y") || streq8(v, "true") || streq8(v, "t") ||
+            streq8(v, "on")) {
+                *ret = true;
+                return true;
+        }
+
+        if (streq8(v, "0") || streq8(v, "no") || streq8(v, "n") || streq8(v, "false") || streq8(v, "f") ||
+            streq8(v, "off")) {
+                *ret = false;
+                return true;
+        }
+
+        return false;
+}
+
 char16_t *hexdump(const void *data, size_t size) {
         static const char hex[16] = "0123456789abcdef";
         const uint8_t *d = data;
index 9b82bfeca05e0bde09eccd9be6f455f0b1a306f3..b72f170c929e37a5490a96a5e6a16f3a91fc9586 100644 (file)
@@ -108,6 +108,8 @@ bool efi_fnmatch(const char16_t *pattern, const char16_t *haystack);
 bool parse_number8(const char *s, uint64_t *ret_u, const char **ret_tail);
 bool parse_number16(const char16_t *s, uint64_t *ret_u, const char16_t **ret_tail);
 
+bool parse_boolean(const char *v, bool *ret);
+
 char16_t *hexdump(const void *data, size_t size);
 
 #ifdef __clang__
index 2ffabe04b85499537215858441fca06dec5de6b4..8caaff595f771bd8ccfeeeaf8eb58b918d5a6abf 100644 (file)
@@ -484,6 +484,26 @@ TEST(parse_number16) {
         assert_se(streq16(tail, u"rest"));
 }
 
+TEST(parse_boolean) {
+        bool b;
+
+        assert_se(!parse_boolean(NULL, &b));
+        assert_se(!parse_boolean("", &b));
+        assert_se(!parse_boolean("ja", &b));
+        assert_se(parse_boolean("1", &b) && b == true);
+        assert_se(parse_boolean("y", &b) && b == true);
+        assert_se(parse_boolean("yes", &b) && b == true);
+        assert_se(parse_boolean("t", &b) && b == true);
+        assert_se(parse_boolean("true", &b) && b == true);
+        assert_se(parse_boolean("on", &b) && b == true);
+        assert_se(parse_boolean("0", &b) && b == false);
+        assert_se(parse_boolean("n", &b) && b == false);
+        assert_se(parse_boolean("no", &b) && b == false);
+        assert_se(parse_boolean("f", &b) && b == false);
+        assert_se(parse_boolean("false", &b) && b == false);
+        assert_se(parse_boolean("off", &b) && b == false);
+}
+
 TEST(hexdump) {
         char16_t *hex;
 
index bb3ccb8280647b00ccbbf0ac1d0acc4bd58c52f3..32796f9ff2e36072114f043481f176d47e0cb5e5 100644 (file)
@@ -8,27 +8,6 @@
 #include "util.h"
 #include "version.h"
 
-EFI_STATUS parse_boolean(const char *v, bool *b) {
-        assert(b);
-
-        if (!v)
-                return EFI_INVALID_PARAMETER;
-
-        if (streq8(v, "1") || streq8(v, "yes") || streq8(v, "y") || streq8(v, "true") || streq8(v, "t") ||
-            streq8(v, "on")) {
-                *b = true;
-                return EFI_SUCCESS;
-        }
-
-        if (streq8(v, "0") || streq8(v, "no") || streq8(v, "n") || streq8(v, "false") || streq8(v, "f") ||
-            streq8(v, "off")) {
-                *b = false;
-                return EFI_SUCCESS;
-        }
-
-        return EFI_INVALID_PARAMETER;
-}
-
 EFI_STATUS efivar_set_raw(const EFI_GUID *vendor, const char16_t *name, const void *buf, size_t size, uint32_t flags) {
         assert(vendor);
         assert(name);
index 25f35d2b5d72b3c82f9d5763b4276eedc69c0688..17215bb81a1d59ecdc2d6f9929600c6516c75fad 100644 (file)
@@ -91,8 +91,6 @@ static inline Pages xmalloc_pages(
         };
 }
 
-EFI_STATUS parse_boolean(const char *v, bool *b);
-
 EFI_STATUS efivar_set(const EFI_GUID *vendor, const char16_t *name, const char16_t *value, uint32_t flags);
 EFI_STATUS efivar_set_raw(const EFI_GUID *vendor, const char16_t *name, const void *buf, size_t size, uint32_t flags);
 EFI_STATUS efivar_set_uint_string(const EFI_GUID *vendor, const char16_t *name, size_t i, uint32_t flags);