Skip to content

Commit 97d9ffd

Browse files
committed
Bios: detect firmware type
1 parent fa86bb1 commit 97d9ffd

File tree

8 files changed

+86
-10
lines changed

8 files changed

+86
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
Changes:
44
* `--percent-type` now defaults to 9 (colored percentage numbers)
55
* `fastfetch` now prints LocalIp module by default
6-
* LocalIp module now prints netmask in CIDR format
6+
7+
Features:
8+
* LocalIP module now prints netmask in CIDR format for IPv4 (LocalIP)
9+
* Bios module now detects system firmware type (Bios)
710

811
Bugfixes:
912
* Fix unitialized variables (#609)

src/detection/bios/bios.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ typedef struct FFBiosResult
1111
FFstrbuf release;
1212
FFstrbuf vendor;
1313
FFstrbuf version;
14+
FFstrbuf type;
1415
} FFBiosResult;
1516

1617
const char* ffDetectBios(FFBiosResult* bios);

src/detection/bios/bios_android.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ const char* ffDetectBios(FFBiosResult* bios)
99
if (ffStrbufIgnCaseEqualS(&bios->version, "unknown"))
1010
ffStrbufClear(&bios->version);
1111

12+
ffStrbufSetStatic(&bios->type, "Bootloader");
13+
1214
return NULL;
1315
}

src/detection/bios/bios_apple.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const char* ffDetectBios(FFBiosResult* bios)
2323
ffCfDictGetString(properties, CFSTR("vendor"), &bios->vendor);
2424
ffCfDictGetString(properties, CFSTR("version"), &bios->version);
2525
ffCfDictGetString(properties, CFSTR("release-date"), &bios->date);
26-
ffStrbufAppendS(&bios->release, "Efi");
26+
ffStrbufSetStatic(&bios->type, "UEFI");
2727

2828
CFRelease(properties);
2929
IOObjectRelease(registryEntry);
@@ -54,12 +54,12 @@ const char* ffDetectBios(FFBiosResult* bios)
5454
uint32_t index = ffStrbufFirstIndexC(&bios->version, '-');
5555
if (index != bios->version.length)
5656
{
57-
ffStrbufAppendNS(&bios->release, index, bios->version.chars);
57+
ffStrbufAppendNS(&bios->type, index, bios->version.chars);
5858
ffStrbufRemoveSubstr(&bios->version, 0, index + 1);
5959
}
6060
else
6161
{
62-
ffStrbufAppendS(&bios->release, "iBoot");
62+
ffStrbufSetStatic(&bios->type, "iBoot");
6363
}
6464
CFRelease(properties);
6565
}

src/detection/bios/bios_bsd.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "bios.h"
22

33
#include "common/settings.h"
4+
#include "common/sysctl.h"
45
#include "util/smbiosHelper.h"
56

67
const char* ffDetectBios(FFBiosResult* result)
@@ -13,5 +14,6 @@ const char* ffDetectBios(FFBiosResult* result)
1314
ffCleanUpSmbiosValue(&result->vendor);
1415
ffSettingsGetFreeBSDKenv("smbios.bios.version", &result->version);
1516
ffCleanUpSmbiosValue(&result->version);
17+
ffSysctlGetString("machdep.bootmethod", &result->type);
1618
return NULL;
1719
}

src/detection/bios/bios_linux.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,9 @@ const char* ffDetectBios(FFBiosResult* bios)
2323
getSmbiosValue("/sys/devices/virtual/dmi/id/bios_release", "/sys/class/dmi/id/bios_release", &bios->release);
2424
getSmbiosValue("/sys/devices/virtual/dmi/id/bios_vendor", "/sys/class/dmi/id/bios_vendor", &bios->vendor);
2525
getSmbiosValue("/sys/devices/virtual/dmi/id/bios_version", "/sys/class/dmi/id/bios_version", &bios->version);
26+
if (ffPathExists("/sys/firmware/efi", FF_PATHTYPE_DIRECTORY) || ffPathExists("/sys/firmware/acpi/tables/UEFI", FF_PATHTYPE_FILE))
27+
ffStrbufSetStatic(&bios->type, "UEFI");
28+
else
29+
ffStrbufSetStatic(&bios->type, "BIOS");
2630
return NULL;
2731
}

src/detection/bios/bios_windows.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,30 @@
22
#include "util/windows/registry.h"
33
#include "util/smbiosHelper.h"
44

5+
#include <ntstatus.h>
6+
#include <winternl.h>
7+
8+
typedef struct _SYSTEM_BOOT_ENVIRONMENT_INFORMATION
9+
{
10+
GUID BootIdentifier;
11+
FIRMWARE_TYPE FirmwareType;
12+
union
13+
{
14+
ULONGLONG BootFlags;
15+
struct
16+
{
17+
ULONGLONG DbgMenuOsSelection : 1; // REDSTONE4
18+
ULONGLONG DbgHiberBoot : 1;
19+
ULONGLONG DbgSoftBoot : 1;
20+
ULONGLONG DbgMeasuredLaunch : 1;
21+
ULONGLONG DbgMeasuredLaunchCapable : 1; // 19H1
22+
ULONGLONG DbgSystemHiveReplace : 1;
23+
ULONGLONG DbgMeasuredLaunchSmmProtections : 1;
24+
ULONGLONG DbgMeasuredLaunchSmmLevel : 7; // 20H1
25+
};
26+
};
27+
} SYSTEM_BOOT_ENVIRONMENT_INFORMATION;
28+
529
const char* ffDetectBios(FFBiosResult* bios)
630
{
731
FF_HKEY_AUTO_DESTROY hKey = NULL;
@@ -24,5 +48,18 @@ const char* ffDetectBios(FFBiosResult* bios)
2448
)
2549
ffStrbufAppendF(&bios->release, "%u.%u", (unsigned)major, (unsigned)minor);
2650

51+
// Same as GetFirmwareType, but support (?) Windows 7
52+
// https://ntdoc.m417z.com/system_information_class
53+
SYSTEM_BOOT_ENVIRONMENT_INFORMATION sbei;
54+
if (NT_SUCCESS(NtQuerySystemInformation(90 /*SystemBootEnvironmentInformation*/, &sbei, sizeof(sbei), NULL)))
55+
{
56+
switch (sbei.FirmwareType)
57+
{
58+
case FirmwareTypeBios: ffStrbufSetStatic(&bios->type, "BIOS"); break;
59+
case FirmwareTypeUefi: ffStrbufSetStatic(&bios->type, "UEFI"); break;
60+
default: break;
61+
}
62+
}
63+
2764
return NULL;
2865
}

src/modules/bios/bios.c

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "modules/bios/bios.h"
55
#include "util/stringUtils.h"
66

7-
#define FF_BIOS_NUM_FORMAT_ARGS 4
7+
#define FF_BIOS_NUM_FORMAT_ARGS 5
88

99
void ffPrintBios(FFBiosOptions* options)
1010
{
@@ -13,9 +13,12 @@ void ffPrintBios(FFBiosOptions* options)
1313
ffStrbufInit(&bios.release);
1414
ffStrbufInit(&bios.vendor);
1515
ffStrbufInit(&bios.version);
16+
ffStrbufInit(&bios.type);
1617

1718
const char* error = ffDetectBios(&bios);
1819

20+
FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate();
21+
1922
if(error)
2023
{
2124
ffPrintError(FF_BIOS_MODULE_NAME, 0, &options->moduleArgs, "%s", error);
@@ -28,21 +31,40 @@ void ffPrintBios(FFBiosOptions* options)
2831
goto exit;
2932
}
3033

34+
if(options->moduleArgs.key.length == 0)
35+
{
36+
if(bios.type.length == 0)
37+
ffStrbufSetStatic(&bios.type, "Unknown");
38+
else if (ffStrbufIgnCaseEqualS(&bios.type, "BIOS"))
39+
ffStrbufSetStatic(&bios.type, "Legacy");
40+
41+
ffStrbufSetF(&key, FF_BIOS_MODULE_NAME " (%s)", bios.type.chars);
42+
}
43+
else
44+
{
45+
ffStrbufClear(&key);
46+
ffParseFormatString(&key, &options->moduleArgs.key, 3, (FFformatarg[]){
47+
{FF_FORMAT_ARG_TYPE_STRBUF, &bios.type},
48+
});
49+
}
50+
3151
if(options->moduleArgs.outputFormat.length == 0)
3252
{
33-
ffPrintLogoAndKey(FF_BIOS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
53+
ffPrintLogoAndKey(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
3454
ffStrbufWriteTo(&bios.version, stdout);
3555
if (bios.release.length)
36-
printf(" (%s)", bios.release.chars);
37-
putchar('\n');
56+
printf(" (%s)\n", bios.release.chars);
57+
else
58+
putchar('\n');
3859
}
3960
else
4061
{
41-
ffPrintFormat(FF_BIOS_MODULE_NAME, 0, &options->moduleArgs, FF_BIOS_NUM_FORMAT_ARGS, (FFformatarg[]) {
62+
ffPrintFormat(key.chars, 0, &options->moduleArgs, FF_BIOS_NUM_FORMAT_ARGS, (FFformatarg[]) {
4263
{FF_FORMAT_ARG_TYPE_STRBUF, &bios.date},
4364
{FF_FORMAT_ARG_TYPE_STRBUF, &bios.release},
4465
{FF_FORMAT_ARG_TYPE_STRBUF, &bios.vendor},
4566
{FF_FORMAT_ARG_TYPE_STRBUF, &bios.version},
67+
{FF_FORMAT_ARG_TYPE_STRBUF, &bios.type},
4668
});
4769
}
4870

@@ -51,6 +73,7 @@ void ffPrintBios(FFBiosOptions* options)
5173
ffStrbufDestroy(&bios.release);
5274
ffStrbufDestroy(&bios.vendor);
5375
ffStrbufDestroy(&bios.version);
76+
ffStrbufDestroy(&bios.type);
5477
}
5578

5679
bool ffParseBiosCommandOptions(FFBiosOptions* options, const char* key, const char* value)
@@ -95,6 +118,7 @@ void ffGenerateBiosJsonResult(FF_MAYBE_UNUSED FFBiosOptions* options, yyjson_mut
95118
ffStrbufInit(&bios.release);
96119
ffStrbufInit(&bios.vendor);
97120
ffStrbufInit(&bios.version);
121+
ffStrbufInit(&bios.type);
98122

99123
const char* error = ffDetectBios(&bios);
100124

@@ -115,12 +139,14 @@ void ffGenerateBiosJsonResult(FF_MAYBE_UNUSED FFBiosOptions* options, yyjson_mut
115139
yyjson_mut_obj_add_strbuf(doc, obj, "release", &bios.release);
116140
yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &bios.vendor);
117141
yyjson_mut_obj_add_strbuf(doc, obj, "version", &bios.version);
142+
yyjson_mut_obj_add_strbuf(doc, obj, "type", &bios.type);
118143

119144
exit:
120145
ffStrbufDestroy(&bios.date);
121146
ffStrbufDestroy(&bios.release);
122147
ffStrbufDestroy(&bios.vendor);
123148
ffStrbufDestroy(&bios.version);
149+
ffStrbufDestroy(&bios.type);
124150
}
125151

126152
void ffPrintBiosHelpFormat(void)
@@ -129,7 +155,8 @@ void ffPrintBiosHelpFormat(void)
129155
"bios date",
130156
"bios release",
131157
"bios vendor",
132-
"bios version"
158+
"bios version",
159+
"firmware type",
133160
});
134161
}
135162

0 commit comments

Comments
 (0)