From 02d8d9c523b3db5976d3ebe46afe386d964250c0 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 21 Jan 2026 19:27:01 +0900 Subject: [PATCH] efivars: allow to override is_efi_boot() --- src/basic/efivars.c | 28 +++++++++++++++++----------- src/basic/efivars.h | 5 +++++ 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/basic/efivars.c b/src/basic/efivars.c index a655382af8..0eeee1038e 100644 --- a/src/basic/efivars.c +++ b/src/basic/efivars.c @@ -319,20 +319,26 @@ int efi_set_variable_string(const char *variable, const char *value) { return efi_set_variable(variable, u16, (char16_strlen(u16) + 1) * sizeof(char16_t)); } -bool is_efi_boot(void) { - static int cache = -1; +static int cache_efi_boot = -1; - if (cache < 0) { - if (detect_container() > 0) - cache = false; - else { - cache = access("/sys/firmware/efi/", F_OK) >= 0; - if (!cache && errno != ENOENT) - log_debug_errno(errno, "Unable to test whether /sys/firmware/efi/ exists, assuming EFI not available: %m"); - } +bool set_efi_boot(bool b) { + return (cache_efi_boot = b); +} + +bool is_efi_boot(void) { + if (cache_efi_boot >= 0) + return cache_efi_boot; + + if (detect_container() > 0) + return (cache_efi_boot = false); + + if (access("/sys/firmware/efi/", F_OK) < 0) { + if (errno != ENOENT) + log_debug_errno(errno, "Unable to test whether /sys/firmware/efi/ exists, assuming EFI not available: %m"); + return (cache_efi_boot = false); } - return cache; + return (cache_efi_boot = true); } static int read_flag(const char *variable) { diff --git a/src/basic/efivars.h b/src/basic/efivars.h index eeccbb1806..22e0eab9a0 100644 --- a/src/basic/efivars.h +++ b/src/basic/efivars.h @@ -45,6 +45,7 @@ int efi_get_variable_path(const char *variable, char **ret); int efi_set_variable(const char *variable, const void *value, size_t size) _nonnull_if_nonzero_(2, 3); int efi_set_variable_string(const char *variable, const char *value); +bool set_efi_boot(bool b); bool is_efi_boot(void); bool is_efi_secure_boot(void); SecureBootMode efi_get_secure_boot_mode(void); @@ -71,6 +72,10 @@ static inline int efi_set_variable_string(const char *variable, const char *p) { return -EOPNOTSUPP; } +static inline bool set_efi_boot(bool b) { + return false; +} + static inline bool is_efi_boot(void) { return false; }