Bios (Linux): detect boot manager & secure boot

This commit is contained in:
Carter Li
2024-05-15 16:26:29 +08:00
committed by 李通洲
parent 85b4679373
commit 8c7edf4d2d
3 changed files with 112 additions and 8 deletions
+2
View File
@@ -9,6 +9,8 @@ typedef struct FFBiosResult
FFstrbuf vendor;
FFstrbuf version;
FFstrbuf type;
FFstrbuf bootmgr;
bool secureBoot;
} FFBiosResult;
const char* ffDetectBios(FFBiosResult* bios);
+97 -1
View File
@@ -4,15 +4,111 @@
#include <stdlib.h>
const char* ffDetectBios(FFBiosResult* bios)
// https://uefi.org/specs/UEFI/2.10/10_Protocols_Device_Path_Protocol.html#generic-device-path-structures
typedef struct ffEfiDevicePathProtocol
{
uint8_t Type;
uint8_t SubType;
uint16_t Length;
uint8_t SpecificDevicePathData[];
} ffEfiDevicePathProtocol;
// https://uefi.org/specs/UEFI/2.10/03_Boot_Manager.html#load-options
typedef struct FFEfiLoadOption
{
uint32_t Attributes;
uint16_t FilePathListLength;
uint16_t Description[];
// ffEfiDevicePathProtocol FilePathList[];
// uint8_t OptionalData[];
} FFEfiLoadOption;
uint8_t ffEvBits(uint16_t val, uint8_t mask, uint8_t shift)
{
return (uint8_t) ((val & (mask << shift)) >> shift);
}
static void ffUcs2ToUtf8(const uint16_t *const chars, FFstrbuf* result)
{
for (uint32_t i = 0; chars[i]; i++)
{
if (chars[i] <= 0x007f)
ffStrbufAppendC(result, (char) chars[i]);
else if (chars[i] > 0x007f && chars[i] <= 0x07ff)
{
ffStrbufAppendC(result, (char) (0xc0 | ffEvBits(chars[i], 0x1f, 6)));
ffStrbufAppendC(result, (char) (0x80 | ffEvBits(chars[i], 0x3f, 0)));
}
else
{
ffStrbufAppendC(result, (char) (0xe0 | ffEvBits(chars[i], 0xf, 12)));
ffStrbufAppendC(result, (char) (0x80 | ffEvBits(chars[i], 0x3f, 6)));
ffStrbufAppendC(result, (char) (0x80 | ffEvBits(chars[i], 0x3f, 0)));
}
}
}
#define FF_EFIVARS_PATH_PREFIX "/sys/firmware/efi/efivars/"
#define FF_EFI_GLOBAL_GUID "8be4df61-93ca-11d2-aa0d-00e098032b8c"
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);
ffGetSmbiosValue("/sys/devices/virtual/dmi/id/bios_release", "/sys/class/dmi/id/bios_release", &bios->release);
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;
}
+13 -7
View File
@@ -4,7 +4,7 @@
#include "modules/bios/bios.h"
#include "util/stringUtils.h"
#define FF_BIOS_NUM_FORMAT_ARGS 5
#define FF_BIOS_NUM_FORMAT_ARGS 7
void ffPrintBios(FFBiosOptions* options)
{
@@ -14,6 +14,8 @@ void ffPrintBios(FFBiosOptions* options)
ffStrbufInit(&bios.vendor);
ffStrbufInit(&bios.version);
ffStrbufInit(&bios.type);
ffStrbufInit(&bios.bootmgr);
bios.secureBoot = false;
const char* error = ffDetectBios(&bios);
@@ -65,6 +67,8 @@ 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},
}));
}
@@ -74,6 +78,7 @@ exit:
ffStrbufDestroy(&bios.vendor);
ffStrbufDestroy(&bios.version);
ffStrbufDestroy(&bios.type);
ffStrbufDestroy(&bios.bootmgr);
}
bool ffParseBiosCommandOptions(FFBiosOptions* options, const char* key, const char* value)
@@ -119,6 +124,8 @@ 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);
@@ -128,18 +135,14 @@ void ffGenerateBiosJsonResult(FF_MAYBE_UNUSED FFBiosOptions* options, yyjson_mut
goto exit;
}
if (bios.version.length == 0)
{
yyjson_mut_obj_add_str(doc, module, "error", "bios_version is not set.");
goto exit;
}
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
yyjson_mut_obj_add_strbuf(doc, obj, "date", &bios.date);
yyjson_mut_obj_add_strbuf(doc, obj, "release", &bios.release);
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);
@@ -147,6 +150,7 @@ exit:
ffStrbufDestroy(&bios.vendor);
ffStrbufDestroy(&bios.version);
ffStrbufDestroy(&bios.type);
ffStrbufDestroy(&bios.bootmgr);
}
void ffPrintBiosHelpFormat(void)
@@ -157,6 +161,8 @@ void ffPrintBiosHelpFormat(void)
"bios vendor",
"bios version",
"firmware type",
"Boot manager used to boot the system",
"Is secure boot enabled",
}));
}