From: Jan Janssen Date: Sun, 15 Oct 2023 07:31:02 +0000 (+0200) Subject: boot: Move parse_boolean to efi-string X-Git-Tag: v255-rc1~241 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=539d0007404e54386aa57021f534dcad8c7256ca;p=thirdparty%2Fsystemd.git boot: Move parse_boolean to efi-string No functional changes. --- diff --git a/src/boot/efi/boot.c b/src/boot/efi/boot.c index 2c2d0bf735a..a4496aed782 100644 --- a/src/boot/efi/boot.c +++ b/src/boot/efi/boot.c @@ -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); diff --git a/src/boot/efi/efi-string.c b/src/boot/efi/efi-string.c index ffe85524530..822be065209 100644 --- a/src/boot/efi/efi-string.c +++ b/src/boot/efi/efi-string.c @@ -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; diff --git a/src/boot/efi/efi-string.h b/src/boot/efi/efi-string.h index 9b82bfeca05..b72f170c929 100644 --- a/src/boot/efi/efi-string.h +++ b/src/boot/efi/efi-string.h @@ -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__ diff --git a/src/boot/efi/test-efi-string.c b/src/boot/efi/test-efi-string.c index 2ffabe04b85..8caaff595f7 100644 --- a/src/boot/efi/test-efi-string.c +++ b/src/boot/efi/test-efi-string.c @@ -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; diff --git a/src/boot/efi/util.c b/src/boot/efi/util.c index bb3ccb82806..32796f9ff2e 100644 --- a/src/boot/efi/util.c +++ b/src/boot/efi/util.c @@ -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); diff --git a/src/boot/efi/util.h b/src/boot/efi/util.h index 25f35d2b5d7..17215bb81a1 100644 --- a/src/boot/efi/util.h +++ b/src/boot/efi/util.h @@ -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);