From: Lennart Poettering Date: Mon, 15 Jul 2024 16:18:35 +0000 (+0200) Subject: efivars: add helper that reads an fs path from an efi var X-Git-Tag: v257-rc1~433^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=c8d60ae79d1763c6ef16fdb306b65d909a769de8;p=thirdparty%2Fsystemd.git efivars: add helper that reads an fs path from an efi var --- diff --git a/src/basic/efivars.c b/src/basic/efivars.c index 8470d085c77..0ff07fa1133 100644 --- a/src/basic/efivars.c +++ b/src/basic/efivars.c @@ -145,8 +145,10 @@ int efi_get_variable( int efi_get_variable_string(const char *variable, char **ret) { _cleanup_free_ void *s = NULL; size_t ss = 0; - int r; char *x; + int r; + + assert(variable); r = efi_get_variable(variable, NULL, &s, &ss); if (r < 0) @@ -156,10 +158,27 @@ int efi_get_variable_string(const char *variable, char **ret) { if (!x) return -ENOMEM; - *ret = x; + if (ret) + *ret = x; + return 0; } +int efi_get_variable_path(const char *variable, char **ret) { + int r; + + assert(variable); + + r = efi_get_variable_string(variable, ret); + if (r < 0) + return r; + + if (ret) + efi_tilt_backslashes(*ret); + + return r; +} + static int efi_verify_variable(const char *variable, uint32_t attr, const void *value, size_t size) { _cleanup_free_ void *buf = NULL; size_t n; diff --git a/src/basic/efivars.h b/src/basic/efivars.h index 34d697fbff4..fe2be3107a0 100644 --- a/src/basic/efivars.h +++ b/src/basic/efivars.h @@ -11,6 +11,7 @@ #include "sd-id128.h" #include "efivars-fundamental.h" +#include "string-util.h" #include "time-util.h" #define EFI_VENDOR_LOADER SD_ID128_MAKE(4a,67,b0,82,0a,4c,41,cf,b6,c7,44,0b,29,bb,8c,4f) @@ -47,6 +48,7 @@ int efi_get_variable(const char *variable, uint32_t *attribute, void **ret_value, size_t *ret_size); int efi_get_variable_string(const char *variable, char **ret); +int efi_get_variable_path(const char *variable, char **ret); int efi_set_variable(const char *variable, const void *value, size_t size); int efi_set_variable_string(const char *variable, const char *p); @@ -68,6 +70,10 @@ static inline int efi_get_variable_string(const char *variable, char **ret) { return -EOPNOTSUPP; } +static inline int efi_get_variable_path(const char *variable, char **ret) { + return -EOPNOTSUPP; +} + static inline int efi_set_variable(const char *variable, const void *value, size_t size) { return -EOPNOTSUPP; } @@ -100,3 +106,7 @@ static inline int systemd_efi_options_efivarfs_if_newer(char **line) { return -ENODATA; } #endif + +static inline char *efi_tilt_backslashes(char *s) { + return string_replace_char(s, '\\', '/'); +} diff --git a/src/boot/bless-boot.c b/src/boot/bless-boot.c index f86d102f0be..4853aaefe9a 100644 --- a/src/boot/bless-boot.c +++ b/src/boot/bless-boot.c @@ -219,14 +219,12 @@ static int acquire_boot_count_path( uint64_t left, done; int r; - r = efi_get_variable_string(EFI_LOADER_VARIABLE(LoaderBootCountPath), &path); + r = efi_get_variable_path(EFI_LOADER_VARIABLE(LoaderBootCountPath), &path); if (r == -ENOENT) return -EUNATCH; /* in this case, let the caller print a message */ if (r < 0) return log_error_errno(r, "Failed to read LoaderBootCountPath EFI variable: %m"); - efi_tilt_backslashes(path); - if (!path_is_normalized(path)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Path read from LoaderBootCountPath is not normalized, refusing: %s", diff --git a/src/boot/bootctl-status.c b/src/boot/bootctl-status.c index c8ab363de59..8e5462f5480 100644 --- a/src/boot/bootctl-status.c +++ b/src/boot/bootctl-status.c @@ -298,12 +298,24 @@ fail: return r; } -static void read_efi_var(const char *variable, char **ret) { +static int efi_get_variable_string_and_warn(const char *variable, char **ret) { int r; r = efi_get_variable_string(variable, ret); if (r < 0 && r != -ENOENT) - log_warning_errno(r, "Failed to read EFI variable '%s', ignoring: %m", variable); + return log_warning_errno(r, "Failed to read EFI variable '%s', ignoring: %m", variable); + + return r; +} + +static int efi_get_variable_path_and_warn(const char *variable, char **ret) { + int r; + + r = efi_get_variable_path(variable, ret); + if (r < 0 && r != -ENOENT) + return log_warning_errno(r, "Failed to read EFI variable '%s', ignoring: %m", variable); + + return r; } static void print_yes_no_line(bool first, bool good, const char *name) { @@ -401,20 +413,15 @@ int verb_status(int argc, char *argv[], void *userdata) { Tpm2Support s; int have; - read_efi_var(EFI_LOADER_VARIABLE(LoaderFirmwareType), &fw_type); - read_efi_var(EFI_LOADER_VARIABLE(LoaderFirmwareInfo), &fw_info); - read_efi_var(EFI_LOADER_VARIABLE(LoaderInfo), &loader); - read_efi_var(EFI_LOADER_VARIABLE(StubInfo), &stub); - read_efi_var(EFI_LOADER_VARIABLE(LoaderImageIdentifier), &loader_path); - read_efi_var(EFI_LOADER_VARIABLE(StubImageIdentifier), &stub_path); + (void) efi_get_variable_string_and_warn(EFI_LOADER_VARIABLE(LoaderFirmwareType), &fw_type); + (void) efi_get_variable_string_and_warn(EFI_LOADER_VARIABLE(LoaderFirmwareInfo), &fw_info); + (void) efi_get_variable_string_and_warn(EFI_LOADER_VARIABLE(LoaderInfo), &loader); + (void) efi_get_variable_string_and_warn(EFI_LOADER_VARIABLE(StubInfo), &stub); + (void) efi_get_variable_path_and_warn(EFI_LOADER_VARIABLE(LoaderImageIdentifier), &loader_path); + (void) efi_get_variable_path_and_warn(EFI_LOADER_VARIABLE(StubImageIdentifier), &stub_path); (void) efi_loader_get_features(&loader_features); (void) efi_stub_get_features(&stub_features); - if (loader_path) - efi_tilt_backslashes(loader_path); - if (stub_path) - efi_tilt_backslashes(stub_path); - SecureBootMode secure = efi_get_secure_boot_mode(); printf("%sSystem:%s\n", ansi_underline(), ansi_normal()); printf(" Firmware: %s%s (%s)%s\n", ansi_highlight(), strna(fw_type), strna(fw_info), ansi_normal()); diff --git a/src/shared/efi-api.h b/src/shared/efi-api.h index 09071b22c99..61ef91df9fb 100644 --- a/src/shared/efi-api.h +++ b/src/shared/efi-api.h @@ -66,9 +66,5 @@ static inline bool efi_has_tpm2(void) { #endif -static inline char *efi_tilt_backslashes(char *s) { - return string_replace_char(s, '\\', '/'); -} - sd_id128_t efi_guid_to_id128(const void *guid); void efi_id128_to_guid(sd_id128_t id, void *ret_guid);