efi: Add a function to check if Secure Boot mode is enabled
PATCH 2/4 efi: Add a function to check if Secure Boot mode is enabled
Also cache it to avoid needing to repeatedly ask the firmware.
--- xen/common/efi/boot.c | 23 +++++++++++++++++++++++ xen/common/efi/runtime.c | 3 +++ xen/include/xen/efi.h | 6 ++++++ 3 files changed, 32 insertions(+)
xen/common/efi/boot.c +23 -0
@@ -870,6 +870,27 @@ static void __init pre_parse(const struct file *file) " last line will be ignored.\r\n"); } +static void __init init_secure_boot_mode (void ) +{ +EFI_STATUS status; +EFI_GUID gv_uuid =EFI_GLOBAL_VARIABLE ; +uint8_t data =0 ; +UINTN size =sizeof (data); +UINT32 attr =0 ; + status = efi_rs->GetVariable ((CHAR16 *)L"SecureBoot" , &gv_uuid, &attr, + &size, &data); + +if ( status ==EFI_NOT_FOUND || + (status ==EFI_SUCCESS && + attr == (EFI_VARIABLE_BOOTSERVICE_ACCESS |EFI_VARIABLE_RUNTIME_ACCESS ) && + size ==1 && data ==0 ) ) +/* Platform does not support Secure Boot or it's disabled. */ + efi_secure_boot = false; +else +/* Everything else play it safe and assume enabled. */ + efi_secure_boot = true; +} +static void __init efi_init (EFI_HANDLE ImageHandle,EFI_SYSTEM_TABLE *SystemTable) { efi_ih = ImageHandle;
@@ -884,6 +905,8 @@ static void __init efi_init(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTabl StdOut = SystemTable->ConOut; StdErr = SystemTable->StdErr ?: StdOut; + +init_secure_boot_mode (); }static void __init efi_console_set_mode (void )
xen/common/efi/runtime.c +3 -0
@@ -40,6 +40,9 @@ void efi_rs_leave(struct efi_rs_state *state);unsigned int __read_mostly efi_num_ct;const EFI_CONFIGURATION_TABLE *__read_mostly efi_ct; +#if defined(CONFIG_X86 ) && !defined(CONFIG_PV_SHIM ) +bool __ro_after_init efi_secure_boot; +#endif unsigned int __read_mostly efi_version;unsigned int __read_mostly efi_fw_revision;const CHAR16 *__read_mostly efi_fw_vendor;
xen/include/xen/efi.h +6 -0
@@ -40,6 +40,12 @@ static inline bool efi_enabled(unsigned int feature) }#endif +#if defined(CONFIG_X86 ) && !defined(CONFIG_PV_SHIM ) +extern bool efi_secure_boot; +#else +#define efi_secure_boot false +#endif +void efi_init_memory (void );bool efi_boot_mem_unused (unsigned long *start,unsigned long *end);bool efi_rs_using_pgtables (void );
Signature · 1 line
2.42.0
CC'ing the EFI maintainers.
Quoted 5 lines · Kevin Lampis
xen/common/efi/boot.c +0
-0
xen/common/efi/boot.c static void __init pre_parse(const struct file *file) · line 879
Quoted 10 lines
xen/common/efi/boot.c +7
-0@@ -870,6 +870,27 @@ static void __init pre_parse(const struct file *file) " last line will be ignored.\r\n"); } +static void __init init_secure_boot_mode (void ) +{ +EFI_STATUS status; +EFI_GUID gv_uuid =EFI_GLOBAL_VARIABLE ; +uint8_t data =0 ; +UINTN size =sizeof (data); +UINT32 attr =0 ;
Newline between variables and code please.
xen/common/efi/boot.c static void __init pre_parse(const struct file *file) · line 892
Quoted 13 lines
xen/common/efi/boot.c +13
-0+ status = efi_rs->GetVariable ((CHAR16 *)L"SecureBoot" , &gv_uuid, &attr, + &size, &data); + +if ( status ==EFI_NOT_FOUND || + (status ==EFI_SUCCESS && + attr == (EFI_VARIABLE_BOOTSERVICE_ACCESS |EFI_VARIABLE_RUNTIME_ACCESS ) && + size ==1 && data ==0 ) ) +/* Platform does not support Secure Boot or it's disabled. */ + efi_secure_boot = false; +else +/* Everything else play it safe and assume enabled. */ + efi_secure_boot = true; +}
I'm not sure this logic does what you want when a weird answer comes back from GetVariable().
Also, you can't have this be a common function, yet ...
xen/common/efi/runtime.c void efi_rs_leave(struct efi_rs_state *state); · line 45
Quoted 11 lines
xen/common/efi/runtime.c +3
-0@@ -40,6 +40,9 @@ void efi_rs_leave(struct efi_rs_state *state);unsigned int __read_mostly efi_num_ct;const EFI_CONFIGURATION_TABLE *__read_mostly efi_ct; +#if defined(CONFIG_X86 ) && !defined(CONFIG_PV_SHIM ) +bool __ro_after_init efi_secure_boot; +#endif
... this variable exist only on x86.
Also, why adjust it for PV shim? None of this is even compiled for PV shim.
~Andrew
Quoted 22 lines · Andrew Cooper
CC'ing the EFI maintainers.
Quoted 5 lines · Kevin Lampis
xen/common/efi/boot.c +0
-0xen/common/efi/boot.c static void __init pre_parse(const struct file *file) · line 879
Quoted 10 lines
xen/common/efi/boot.c +7
-0@@ -870,6 +870,27 @@ static void __init pre_parse(const struct file *file) " last line will be ignored.\r\n"); } +static void __init init_secure_boot_mode (void ) +{ +EFI_STATUS status; +EFI_GUID gv_uuid =EFI_GLOBAL_VARIABLE ; +uint8_t data =0 ; +UINTN size =sizeof (data); +UINT32 attr =0 ;Newline between variables and code please.
Quoted 16 lines
xen/common/efi/boot.c static void __init pre_parse(const struct file *file) · line 892
Quoted 13 lines
xen/common/efi/boot.c +13
-0+ status = efi_rs->GetVariable ((CHAR16 *)L"SecureBoot" , &gv_uuid, &attr, + &size, &data); + +if ( status ==EFI_NOT_FOUND || + (status ==EFI_SUCCESS && + attr == (EFI_VARIABLE_BOOTSERVICE_ACCESS |EFI_VARIABLE_RUNTIME_ACCESS ) && + size ==1 && data ==0 ) ) +/* Platform does not support Secure Boot or it's disabled. */ + efi_secure_boot = false; +else +/* Everything else play it safe and assume enabled. */ + efi_secure_boot = true; +}I'm not sure this logic does what you want when a weird answer comes back from
GetVariable().
What specific case do you think is handled incorrectly here?
Ross
Quoted 49 lines · Andrew Cooper
CC'ing the EFI maintainers.
Quoted 5 lines · Kevin Lampis
xen/common/efi/boot.c +0
-0xen/common/efi/boot.c static void __init pre_parse(const struct file *file) · line 879
Quoted 10 lines
xen/common/efi/boot.c +7
-0@@ -870,6 +870,27 @@ static void __init pre_parse(const struct file *file) " last line will be ignored.\r\n"); } +static void __init init_secure_boot_mode (void ) +{ +EFI_STATUS status; +EFI_GUID gv_uuid =EFI_GLOBAL_VARIABLE ; +uint8_t data =0 ; +UINTN size =sizeof (data); +UINT32 attr =0 ;Newline between variables and code please.
xen/common/efi/boot.c static void __init pre_parse(const struct file *file) · line 892
Quoted 13 lines
xen/common/efi/boot.c +13
-0+ status = efi_rs->GetVariable ((CHAR16 *)L"SecureBoot" , &gv_uuid, &attr, + &size, &data); + +if ( status ==EFI_NOT_FOUND || + (status ==EFI_SUCCESS && + attr == (EFI_VARIABLE_BOOTSERVICE_ACCESS |EFI_VARIABLE_RUNTIME_ACCESS ) && + size ==1 && data ==0 ) ) +/* Platform does not support Secure Boot or it's disabled. */ + efi_secure_boot = false; +else +/* Everything else play it safe and assume enabled. */ + efi_secure_boot = true; +}I'm not sure this logic does what you want when a weird answer comes back from
GetVariable().Also, you can't have this be a common function, yet ...
Quoted 8 lines
xen/common/efi/runtime.c +0
-0@@ -40,6 +40,9 @@ void efi_rs_leave(struct efi_rs_state *state);unsigned int __read_mostly efi_num_ct;const EFI_CONFIGURATION_TABLE *__read_mostly efi_ct;
Quoted 6 lines
xen/common/efi/runtime.c void efi_rs_leave(struct efi_rs_state *state); · line 45
Quoted 3 lines
xen/common/efi/runtime.c +3
-0+#if defined(CONFIG_X86 ) && !defined(CONFIG_PV_SHIM ) +bool __ro_after_init efi_secure_boot; +#endif ... this variable exist only on x86.
On a more high level. Is secure boot specific to x86? I think it's generic so it should be compiled also for ARM or any EFI enabled architectures.
Quoted 4 lines
Also, why adjust it for PV shim? None of this is even compiled for PV shim.
~Andrew
Frediano
Quoted 46 lines · Andrew Cooper
Quoted 5 lines · Kevin Lampis
xen/common/efi/boot.c +0
-0xen/common/efi/boot.c static void __init pre_parse(const struct file *file) · line 879
Quoted 10 lines
xen/common/efi/boot.c +7
-0@@ -870,6 +870,27 @@ static void __init pre_parse(const struct file *file) " last line will be ignored.\r\n"); } +static void __init init_secure_boot_mode (void ) +{ +EFI_STATUS status; +EFI_GUID gv_uuid =EFI_GLOBAL_VARIABLE ; +uint8_t data =0 ; +UINTN size =sizeof (data); +UINT32 attr =0 ;Newline between variables and code please.
xen/common/efi/boot.c static void __init pre_parse(const struct file *file) · line 892
Quoted 13 lines
xen/common/efi/boot.c +13
-0+ status = efi_rs->GetVariable ((CHAR16 *)L"SecureBoot" , &gv_uuid, &attr, + &size, &data); + +if ( status ==EFI_NOT_FOUND || + (status ==EFI_SUCCESS && + attr == (EFI_VARIABLE_BOOTSERVICE_ACCESS |EFI_VARIABLE_RUNTIME_ACCESS ) && + size ==1 && data ==0 ) ) +/* Platform does not support Secure Boot or it's disabled. */ + efi_secure_boot = false; +else +/* Everything else play it safe and assume enabled. */ + efi_secure_boot = true; +}I'm not sure this logic does what you want when a weird answer comes back from
GetVariable().Also, you can't have this be a common function, yet ...
Quoted 8 lines
xen/common/efi/runtime.c +0
-0@@ -40,6 +40,9 @@ void efi_rs_leave(struct efi_rs_state *state);unsigned int __read_mostly efi_num_ct;const EFI_CONFIGURATION_TABLE *__read_mostly efi_ct;
Quoted 7 lines
xen/common/efi/runtime.c void efi_rs_leave(struct efi_rs_state *state); · line 45
Quoted 3 lines
xen/common/efi/runtime.c +3
-0+#if defined(CONFIG_X86 ) && !defined(CONFIG_PV_SHIM ) +bool __ro_after_init efi_secure_boot; +#endif ... this variable exist only on x86.
Also, why adjust it for PV shim? None of this is even compiled for PV shim.
For shim it might be (and a proper variable is needed in that case); for shim-exclusive it shouldn't be.
Jan