Bios: detect firmware type

This commit is contained in:
李通洲
2023-11-06 11:10:26 +08:00
parent fa86bb1f0b
commit 97d9ffd193
8 changed files with 89 additions and 13 deletions
+4 -1
View File
@@ -3,7 +3,10 @@
Changes:
* `--percent-type` now defaults to 9 (colored percentage numbers)
* `fastfetch` now prints LocalIp module by default
* LocalIp module now prints netmask in CIDR format
Features:
* LocalIP module now prints netmask in CIDR format for IPv4 (LocalIP)
* Bios module now detects system firmware type (Bios)
Bugfixes:
* Fix unitialized variables (#609)
+1
View File
@@ -11,6 +11,7 @@ typedef struct FFBiosResult
FFstrbuf release;
FFstrbuf vendor;
FFstrbuf version;
FFstrbuf type;
} FFBiosResult;
const char* ffDetectBios(FFBiosResult* bios);
+2
View File
@@ -9,5 +9,7 @@ const char* ffDetectBios(FFBiosResult* bios)
if (ffStrbufIgnCaseEqualS(&bios->version, "unknown"))
ffStrbufClear(&bios->version);
ffStrbufSetStatic(&bios->type, "Bootloader");
return NULL;
}
+3 -3
View File
@@ -23,7 +23,7 @@ const char* ffDetectBios(FFBiosResult* bios)
ffCfDictGetString(properties, CFSTR("vendor"), &bios->vendor);
ffCfDictGetString(properties, CFSTR("version"), &bios->version);
ffCfDictGetString(properties, CFSTR("release-date"), &bios->date);
ffStrbufAppendS(&bios->release, "Efi");
ffStrbufSetStatic(&bios->type, "UEFI");
CFRelease(properties);
IOObjectRelease(registryEntry);
@@ -54,12 +54,12 @@ const char* ffDetectBios(FFBiosResult* bios)
uint32_t index = ffStrbufFirstIndexC(&bios->version, '-');
if (index != bios->version.length)
{
ffStrbufAppendNS(&bios->release, index, bios->version.chars);
ffStrbufAppendNS(&bios->type, index, bios->version.chars);
ffStrbufRemoveSubstr(&bios->version, 0, index + 1);
}
else
{
ffStrbufAppendS(&bios->release, "iBoot");
ffStrbufSetStatic(&bios->type, "iBoot");
}
CFRelease(properties);
}
+2
View File
@@ -1,6 +1,7 @@
#include "bios.h"
#include "common/settings.h"
#include "common/sysctl.h"
#include "util/smbiosHelper.h"
const char* ffDetectBios(FFBiosResult* result)
@@ -13,5 +14,6 @@ const char* ffDetectBios(FFBiosResult* result)
ffCleanUpSmbiosValue(&result->vendor);
ffSettingsGetFreeBSDKenv("smbios.bios.version", &result->version);
ffCleanUpSmbiosValue(&result->version);
ffSysctlGetString("machdep.bootmethod", &result->type);
return NULL;
}
+4
View File
@@ -23,5 +23,9 @@ const char* ffDetectBios(FFBiosResult* bios)
getSmbiosValue("/sys/devices/virtual/dmi/id/bios_release", "/sys/class/dmi/id/bios_release", &bios->release);
getSmbiosValue("/sys/devices/virtual/dmi/id/bios_vendor", "/sys/class/dmi/id/bios_vendor", &bios->vendor);
getSmbiosValue("/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");
else
ffStrbufSetStatic(&bios->type, "BIOS");
return NULL;
}
+37
View File
@@ -2,6 +2,30 @@
#include "util/windows/registry.h"
#include "util/smbiosHelper.h"
#include <ntstatus.h>
#include <winternl.h>
typedef struct _SYSTEM_BOOT_ENVIRONMENT_INFORMATION
{
GUID BootIdentifier;
FIRMWARE_TYPE FirmwareType;
union
{
ULONGLONG BootFlags;
struct
{
ULONGLONG DbgMenuOsSelection : 1; // REDSTONE4
ULONGLONG DbgHiberBoot : 1;
ULONGLONG DbgSoftBoot : 1;
ULONGLONG DbgMeasuredLaunch : 1;
ULONGLONG DbgMeasuredLaunchCapable : 1; // 19H1
ULONGLONG DbgSystemHiveReplace : 1;
ULONGLONG DbgMeasuredLaunchSmmProtections : 1;
ULONGLONG DbgMeasuredLaunchSmmLevel : 7; // 20H1
};
};
} SYSTEM_BOOT_ENVIRONMENT_INFORMATION;
const char* ffDetectBios(FFBiosResult* bios)
{
FF_HKEY_AUTO_DESTROY hKey = NULL;
@@ -24,5 +48,18 @@ const char* ffDetectBios(FFBiosResult* bios)
)
ffStrbufAppendF(&bios->release, "%u.%u", (unsigned)major, (unsigned)minor);
// Same as GetFirmwareType, but support (?) Windows 7
// https://ntdoc.m417z.com/system_information_class
SYSTEM_BOOT_ENVIRONMENT_INFORMATION sbei;
if (NT_SUCCESS(NtQuerySystemInformation(90 /*SystemBootEnvironmentInformation*/, &sbei, sizeof(sbei), NULL)))
{
switch (sbei.FirmwareType)
{
case FirmwareTypeBios: ffStrbufSetStatic(&bios->type, "BIOS"); break;
case FirmwareTypeUefi: ffStrbufSetStatic(&bios->type, "UEFI"); break;
default: break;
}
}
return NULL;
}
+36 -9
View File
@@ -4,7 +4,7 @@
#include "modules/bios/bios.h"
#include "util/stringUtils.h"
#define FF_BIOS_NUM_FORMAT_ARGS 4
#define FF_BIOS_NUM_FORMAT_ARGS 5
void ffPrintBios(FFBiosOptions* options)
{
@@ -13,9 +13,12 @@ void ffPrintBios(FFBiosOptions* options)
ffStrbufInit(&bios.release);
ffStrbufInit(&bios.vendor);
ffStrbufInit(&bios.version);
ffStrbufInit(&bios.type);
const char* error = ffDetectBios(&bios);
FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate();
if(error)
{
ffPrintError(FF_BIOS_MODULE_NAME, 0, &options->moduleArgs, "%s", error);
@@ -28,21 +31,40 @@ void ffPrintBios(FFBiosOptions* options)
goto exit;
}
if(options->moduleArgs.outputFormat.length == 0)
if(options->moduleArgs.key.length == 0)
{
ffPrintLogoAndKey(FF_BIOS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
ffStrbufWriteTo(&bios.version, stdout);
if (bios.release.length)
printf(" (%s)", bios.release.chars);
putchar('\n');
if(bios.type.length == 0)
ffStrbufSetStatic(&bios.type, "Unknown");
else if (ffStrbufIgnCaseEqualS(&bios.type, "BIOS"))
ffStrbufSetStatic(&bios.type, "Legacy");
ffStrbufSetF(&key, FF_BIOS_MODULE_NAME " (%s)", bios.type.chars);
}
else
{
ffPrintFormat(FF_BIOS_MODULE_NAME, 0, &options->moduleArgs, FF_BIOS_NUM_FORMAT_ARGS, (FFformatarg[]) {
ffStrbufClear(&key);
ffParseFormatString(&key, &options->moduleArgs.key, 3, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRBUF, &bios.type},
});
}
if(options->moduleArgs.outputFormat.length == 0)
{
ffPrintLogoAndKey(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
ffStrbufWriteTo(&bios.version, stdout);
if (bios.release.length)
printf(" (%s)\n", bios.release.chars);
else
putchar('\n');
}
else
{
ffPrintFormat(key.chars, 0, &options->moduleArgs, FF_BIOS_NUM_FORMAT_ARGS, (FFformatarg[]) {
{FF_FORMAT_ARG_TYPE_STRBUF, &bios.date},
{FF_FORMAT_ARG_TYPE_STRBUF, &bios.release},
{FF_FORMAT_ARG_TYPE_STRBUF, &bios.vendor},
{FF_FORMAT_ARG_TYPE_STRBUF, &bios.version},
{FF_FORMAT_ARG_TYPE_STRBUF, &bios.type},
});
}
@@ -51,6 +73,7 @@ exit:
ffStrbufDestroy(&bios.release);
ffStrbufDestroy(&bios.vendor);
ffStrbufDestroy(&bios.version);
ffStrbufDestroy(&bios.type);
}
bool ffParseBiosCommandOptions(FFBiosOptions* options, const char* key, const char* value)
@@ -95,6 +118,7 @@ void ffGenerateBiosJsonResult(FF_MAYBE_UNUSED FFBiosOptions* options, yyjson_mut
ffStrbufInit(&bios.release);
ffStrbufInit(&bios.vendor);
ffStrbufInit(&bios.version);
ffStrbufInit(&bios.type);
const char* error = ffDetectBios(&bios);
@@ -115,12 +139,14 @@ void ffGenerateBiosJsonResult(FF_MAYBE_UNUSED FFBiosOptions* options, yyjson_mut
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);
exit:
ffStrbufDestroy(&bios.date);
ffStrbufDestroy(&bios.release);
ffStrbufDestroy(&bios.vendor);
ffStrbufDestroy(&bios.version);
ffStrbufDestroy(&bios.type);
}
void ffPrintBiosHelpFormat(void)
@@ -129,7 +155,8 @@ void ffPrintBiosHelpFormat(void)
"bios date",
"bios release",
"bios vendor",
"bios version"
"bios version",
"firmware type",
});
}