Skip to content

Commit

Permalink
Revert "boot: Skip safety countdown when running in a VM"
Browse files Browse the repository at this point in the history
This reverts commit bafc594.
  • Loading branch information
mrc0mmand committed Aug 4, 2022
1 parent 49d5f89 commit 5cc1682
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 34 deletions.
5 changes: 0 additions & 5 deletions src/boot/efi/secure-boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ EFI_STATUS secure_boot_enroll_at(EFI_FILE *root_dir, const char16_t *path) {

unsigned timeout_sec = 15;
for(;;) {
/* Enrolling secure boot keys is safe to do in virtualized environments as there is nothing
* we can brick there. */
if (in_hypervisor())
break;

PrintAt(0, ST->ConOut->Mode->CursorRow, L"Enrolling in %2u s, press any key to abort.", timeout_sec);

uint64_t key;
Expand Down
26 changes: 22 additions & 4 deletions src/boot/efi/ticks.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,35 @@

#include <efi.h>
#include <efilib.h>
#if defined(__i386__) || defined(__x86_64__)
#include <cpuid.h>
#endif
#include <stdbool.h>

#include "ticks.h"
#include "util.h"

#if defined(__i386__) || defined(__x86_64__)
static bool in_hypervisor(void) {
uint32_t eax, ebx, ecx, edx;

/* The TSC might or might not be virtualized in VMs (and thus might not be accurate or start at zero
* at boot), depending on hypervisor and CPU functionality. If it's not virtualized it's not useful
* for keeping time, hence don't attempt to use it.
*
* This is a dumbed down version of src/basic/virt.c's detect_vm() that safely works in the UEFI
* environment. */

if (__get_cpuid(1, &eax, &ebx, &ecx, &edx) == 0)
return false;

return !!(ecx & 0x80000000U);
}
#endif

#ifdef __x86_64__
static uint64_t ticks_read(void) {
uint64_t a, d;

/* The TSC might or might not be virtualized in VMs (and thus might not be accurate or start at zero
* at boot), depending on hypervisor and CPU functionality. If it's not virtualized it's not useful
* for keeping time, hence don't attempt to use it. */
if (in_hypervisor())
return 0;

Expand Down
17 changes: 0 additions & 17 deletions src/boot/efi/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

#include <efi.h>
#include <efilib.h>
#if defined(__i386__) || defined(__x86_64__)
# include <cpuid.h>
#endif

#include "ticks.h"
#include "util.h"
Expand Down Expand Up @@ -771,17 +768,3 @@ EFI_STATUS make_file_device_path(EFI_HANDLE device, const char16_t *file, EFI_DE
SetDevicePathEndNode(dp);
return EFI_SUCCESS;
}

#if defined(__i386__) || defined(__x86_64__)
bool in_hypervisor(void) {
uint32_t eax, ebx, ecx, edx;

/* This is a dumbed down version of src/basic/virt.c's detect_vm() that safely works in the UEFI
* environment. */

if (__get_cpuid(1, &eax, &ebx, &ecx, &edx) == 0)
return false;

return !!(ecx & 0x80000000U);
}
#endif
8 changes: 0 additions & 8 deletions src/boot/efi/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,3 @@ static inline void beep(UINTN beep_count) {}

EFI_STATUS open_volume(EFI_HANDLE device, EFI_FILE **ret_file);
EFI_STATUS make_file_device_path(EFI_HANDLE device, const char16_t *file, EFI_DEVICE_PATH **ret_dp);

#if defined(__i386__) || defined(__x86_64__)
bool in_hypervisor(void);
#else
static inline bool in_hypervisor(void) {
return false;
}
#endif

0 comments on commit 5cc1682

Please sign in to comment.