Bios: move bootmgr detection to a new module

This commit is contained in:
李通洲
2024-05-15 21:09:26 +08:00
committed by 李通洲
parent 06a8f12d6a
commit 7c03fc1550
4 changed files with 1 additions and 145 deletions
-2
View File
@@ -9,8 +9,6 @@ typedef struct FFBiosResult
FFstrbuf vendor;
FFstrbuf version;
FFstrbuf type;
FFstrbuf bootmgr;
bool secureBoot;
} FFBiosResult;
const char* ffDetectBios(FFBiosResult* bios);
-52
View File
@@ -1,57 +1,9 @@
#include "bios.h"
#include "common/io/io.h"
#include "util/smbiosHelper.h"
#include "efi.h"
#include <stdlib.h>
#define FF_EFIVARS_PATH_PREFIX "/sys/firmware/efi/efivars/"
const char *detectBootmgr(FFstrbuf *result)
{
uint8_t buffer[2048];
if (ffReadFileData(FF_EFIVARS_PATH_PREFIX "BootCurrent-" FF_EFI_GLOBAL_GUID, sizeof(buffer), buffer) != 6)
return "Failed to read efivar: BootCurrent";
uint16_t value = *(uint16_t *)&buffer[4];
snprintf((char*) buffer, sizeof(buffer), FF_EFIVARS_PATH_PREFIX "Boot%04X-" FF_EFI_GLOBAL_GUID, value);
ssize_t size = ffReadFileData((const char*) buffer, sizeof(buffer), buffer);
if (size < 5 + (int) sizeof(FFEfiLoadOption) || size == (ssize_t) sizeof(buffer))
return "Failed to read efivar: Boot####";
FFEfiLoadOption *efiOption = (FFEfiLoadOption *)&buffer[4];
uint32_t descLen = 0;
while (efiOption->Description[descLen]) ++descLen;
for (
ffEfiDevicePathProtocol* filePathList = (void*) &efiOption->Description[descLen + 1];
filePathList->Type != 0x7F; // End of Hardware Device Path
filePathList = (void*) ((uint8_t*) filePathList + filePathList->Length))
{
if (filePathList->Type == 4 && filePathList->SubType == 4)
{
// https://uefi.org/specs/UEFI/2.10/10_Protocols_Device_Path_Protocol.html#file-path-media-device-path
ffUcs2ToUtf8((uint16_t*) filePathList->SpecificDevicePathData, result);
return NULL;
}
}
if (!result->length) ffUcs2ToUtf8(efiOption->Description, result);
return NULL;
}
const char *detectSecureBoot(bool* result)
{
uint8_t buffer[5];
if (ffReadFileData(FF_EFIVARS_PATH_PREFIX "SecureBoot-" FF_EFI_GLOBAL_GUID, sizeof(buffer), buffer) != 6)
return "Failed to read efivar: SecureBoot";
*result = buffer[4] == 1;
return NULL;
}
const char *ffDetectBios(FFBiosResult *bios)
{
ffGetSmbiosValue("/sys/devices/virtual/dmi/id/bios_date", "/sys/class/dmi/id/bios_date", &bios->date);
@@ -59,12 +11,8 @@ const char *ffDetectBios(FFBiosResult *bios)
ffGetSmbiosValue("/sys/devices/virtual/dmi/id/bios_vendor", "/sys/class/dmi/id/bios_vendor", &bios->vendor);
ffGetSmbiosValue("/sys/devices/virtual/dmi/id/bios_version", "/sys/class/dmi/id/bios_version", &bios->version);
if (ffPathExists("/sys/firmware/efi", FF_PATHTYPE_DIRECTORY) || ffPathExists("/sys/firmware/acpi/tables/UEFI", FF_PATHTYPE_FILE))
{
ffStrbufSetStatic(&bios->type, "UEFI");
detectBootmgr(&bios->bootmgr);
}
else
ffStrbufSetStatic(&bios->type, "BIOS");
detectSecureBoot(&bios->secureBoot);
return NULL;
}
-78
View File
@@ -1,11 +1,9 @@
#include "bios.h"
#include "util/smbiosHelper.h"
#include "util/windows/registry.h"
#include "efi.h"
#include <ntstatus.h>
#include <winternl.h>
#include <windows.h>
typedef struct _SYSTEM_BOOT_ENVIRONMENT_INFORMATION
{
@@ -54,76 +52,6 @@ typedef struct FFSmbiosBios
static_assert(offsetof(FFSmbiosBios, ExtendedBiosRomSize) == 0x18,
"FFSmbiosBios: Wrong struct alignment");
const char* enablePrivilege(const wchar_t* privilege)
{
HANDLE token;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token))
return "OpenProcessToken() failed";
TOKEN_PRIVILEGES tp = {
.PrivilegeCount = 1,
.Privileges = {
(LUID_AND_ATTRIBUTES) { .Attributes = SE_PRIVILEGE_ENABLED }
},
};
if (!LookupPrivilegeValueW(NULL, privilege, &tp.Privileges[0].Luid))
return "LookupPrivilegeValue() failed";
if (!AdjustTokenPrivileges(token, false, &tp, sizeof(tp), NULL, NULL))
return "AdjustTokenPrivileges() failed";
if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
return "The token does not have the specified privilege";
return NULL;
}
const char *detectBootmgr(FFstrbuf *result)
{
if (enablePrivilege(L"SeSystemEnvironmentPrivilege") != NULL)
return "Failed to enable SeSystemEnvironmentPrivilege";
uint16_t value;
if (GetFirmwareEnvironmentVariableW(L"BootCurrent", L"{" FF_EFI_GLOBAL_GUID L"}", &value, sizeof(value)) != 2)
return "GetFirmwareEnvironmentVariableW(BootCurrent) failed";
uint8_t buffer[2048];
wchar_t key[16];
wsprintfW(key, L"Boot%04X", value);
uint32_t size = GetFirmwareEnvironmentVariableW(key, L"{" FF_EFI_GLOBAL_GUID L"}", buffer, sizeof(buffer));
if (size < sizeof(FFEfiLoadOption) || size == sizeof(buffer))
return "GetFirmwareEnvironmentVariableW(Boot####) failed";
FFEfiLoadOption *efiOption = (FFEfiLoadOption *)buffer;
uint32_t descLen = 0;
while (efiOption->Description[descLen]) ++descLen;
for (
ffEfiDevicePathProtocol* filePathList = (void*) &efiOption->Description[descLen + 1];
filePathList->Type != 0x7F; // End of Hardware Device Path
filePathList = (void*) ((uint8_t*) filePathList + filePathList->Length))
{
if (filePathList->Type == 4 && filePathList->SubType == 4)
{
// https://uefi.org/specs/UEFI/2.10/10_Protocols_Device_Path_Protocol.html#file-path-media-device-path
ffUcs2ToUtf8((uint16_t*) filePathList->SpecificDevicePathData, result);
return NULL;
}
}
if (!result->length) ffUcs2ToUtf8(efiOption->Description, result);
return NULL;
}
const char *detectSecureBoot(bool* result)
{
DWORD uefiSecureBootEnabled = 0, bufSize = 0;
if (RegGetValueW(HKEY_LOCAL_MACHINE, L"SYSTEM\\ControlSet001\\Control\\SecureBoot\\State", L"UEFISecureBootEnabled", RRF_RT_REG_DWORD, NULL, &uefiSecureBootEnabled, &bufSize) != ERROR_SUCCESS)
return "RegGetValueW(HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Control\\SecureBoot\\State) failed";
*result = !!uefiSecureBootEnabled;
return NULL;
}
const char* ffDetectBios(FFBiosResult* bios)
{
const FFSmbiosHeaderTable* smbiosTable = ffGetSmbiosHeaderTable();
@@ -157,12 +85,6 @@ const char* ffDetectBios(FFBiosResult* bios)
case FirmwareTypeUefi: ffStrbufSetStatic(&bios->type, "UEFI"); break;
default: break;
}
if (sbei.FirmwareType == FirmwareTypeUefi)
{
detectSecureBoot(&bios->secureBoot);
detectBootmgr(&bios->bootmgr);
}
}
return NULL;
+1 -13
View File
@@ -4,7 +4,7 @@
#include "modules/bios/bios.h"
#include "util/stringUtils.h"
#define FF_BIOS_NUM_FORMAT_ARGS 7
#define FF_BIOS_NUM_FORMAT_ARGS 5
void ffPrintBios(FFBiosOptions* options)
{
@@ -14,8 +14,6 @@ void ffPrintBios(FFBiosOptions* options)
ffStrbufInit(&bios.vendor);
ffStrbufInit(&bios.version);
ffStrbufInit(&bios.type);
ffStrbufInit(&bios.bootmgr);
bios.secureBoot = false;
const char* error = ffDetectBios(&bios);
@@ -67,8 +65,6 @@ void ffPrintBios(FFBiosOptions* options)
{FF_FORMAT_ARG_TYPE_STRBUF, &bios.vendor},
{FF_FORMAT_ARG_TYPE_STRBUF, &bios.version},
{FF_FORMAT_ARG_TYPE_STRBUF, &bios.type},
{FF_FORMAT_ARG_TYPE_STRBUF, &bios.bootmgr},
{FF_FORMAT_ARG_TYPE_BOOL, &bios.secureBoot},
}));
}
@@ -78,7 +74,6 @@ exit:
ffStrbufDestroy(&bios.vendor);
ffStrbufDestroy(&bios.version);
ffStrbufDestroy(&bios.type);
ffStrbufDestroy(&bios.bootmgr);
}
bool ffParseBiosCommandOptions(FFBiosOptions* options, const char* key, const char* value)
@@ -124,8 +119,6 @@ void ffGenerateBiosJsonResult(FF_MAYBE_UNUSED FFBiosOptions* options, yyjson_mut
ffStrbufInit(&bios.vendor);
ffStrbufInit(&bios.version);
ffStrbufInit(&bios.type);
ffStrbufInit(&bios.bootmgr);
bios.secureBoot = false;
const char* error = ffDetectBios(&bios);
@@ -141,8 +134,6 @@ void ffGenerateBiosJsonResult(FF_MAYBE_UNUSED FFBiosOptions* options, yyjson_mut
yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &bios.vendor);
yyjson_mut_obj_add_strbuf(doc, obj, "version", &bios.version);
yyjson_mut_obj_add_strbuf(doc, obj, "type", &bios.type);
yyjson_mut_obj_add_strbuf(doc, obj, "bootmgr", &bios.bootmgr);
yyjson_mut_obj_add_bool(doc, obj, "secureBoot", bios.secureBoot);
exit:
ffStrbufDestroy(&bios.date);
@@ -150,7 +141,6 @@ exit:
ffStrbufDestroy(&bios.vendor);
ffStrbufDestroy(&bios.version);
ffStrbufDestroy(&bios.type);
ffStrbufDestroy(&bios.bootmgr);
}
void ffPrintBiosHelpFormat(void)
@@ -161,8 +151,6 @@ void ffPrintBiosHelpFormat(void)
"bios vendor",
"bios version",
"firmware type",
"Boot manager used to boot the system",
"Is secure boot enabled",
}));
}