Bios: code refactor

This commit is contained in:
李通洲 2023-06-16 16:40:43 +08:00 committed by Carter Li
parent 5483cb9143
commit 05c41ffd89
7 changed files with 41 additions and 63 deletions

View File

@ -11,9 +11,8 @@ typedef struct FFBiosResult
FFstrbuf biosRelease;
FFstrbuf biosVendor;
FFstrbuf biosVersion;
FFstrbuf error;
} FFBiosResult;
void ffDetectBios(FFBiosResult* bios);
const char* ffDetectBios(FFBiosResult* bios);
#endif

View File

@ -3,14 +3,8 @@
#include <IOKit/IOKitLib.h>
void ffDetectBios(FFBiosResult* bios)
const char* ffDetectBios(FFBiosResult* bios)
{
ffStrbufInit(&bios->error);
ffStrbufInit(&bios->biosDate);
ffStrbufInit(&bios->biosRelease);
ffStrbufInit(&bios->biosVendor);
ffStrbufInit(&bios->biosVersion);
io_registry_entry_t registryEntry;
#ifndef __aarch64__
@ -23,8 +17,7 @@ void ffDetectBios(FFBiosResult* bios)
if(IORegistryEntryCreateCFProperties(registryEntry, &properties, kCFAllocatorDefault, kNilOptions) != kIOReturnSuccess)
{
IOObjectRelease(registryEntry);
ffStrbufAppendS(&bios->error, "IORegistryEntryCreateCFProperties(registryEntry) failed");
return;
return "IORegistryEntryCreateCFProperties(registryEntry) failed";
}
ffCfDictGetString(properties, CFSTR("vendor"), &bios->biosVendor);
@ -36,7 +29,7 @@ void ffDetectBios(FFBiosResult* bios)
CFRelease(properties);
IOObjectRelease(registryEntry);
return;
return NULL;
}
#else
@ -51,6 +44,7 @@ void ffDetectBios(FFBiosResult* bios)
CFRelease(properties);
}
IOObjectRelease(registryEntry);
return NULL;
}
if((registryEntry = IORegistryEntryFromPath(MACH_PORT_NULL, "IODeviceTree:/chosen")))
@ -64,7 +58,10 @@ void ffDetectBios(FFBiosResult* bios)
CFRelease(properties);
}
IOObjectRelease(registryEntry);
return NULL;
}
#endif
return "Failed to query bios info";
}

View File

@ -2,16 +2,11 @@
#include "common/settings.h"
void ffDetectBios(FFBiosResult* bios)
const char* ffDetectBios(FFBiosResult* bios)
{
ffStrbufInit(&bios->error);
ffStrbufInit(&bios->biosDate);
ffStrbufInit(&bios->biosRelease);
ffStrbufInit(&bios->biosVendor);
ffStrbufInit(&bios->biosVersion);
ffSettingsGetFreeBSDKenv("smbios.bios.reldate", &bios->biosDate);
ffSettingsGetFreeBSDKenv("smbios.bios.revision", &bios->biosRelease);
ffSettingsGetFreeBSDKenv("smbios.bios.vendor", &bios->biosVendor);
ffSettingsGetFreeBSDKenv("smbios.bios.version", &bios->biosVersion);
return NULL;
}

View File

@ -40,19 +40,11 @@ static void getHostValue(const char* devicesPath, const char* classPath, FFstrbu
ffStrbufClear(buffer);
}
void ffDetectBios(FFBiosResult* bios)
const char* ffDetectBios(FFBiosResult* bios)
{
ffStrbufInit(&bios->error);
ffStrbufInit(&bios->biosDate);
getHostValue("/sys/devices/virtual/dmi/id/bios_date", "/sys/class/dmi/id/bios_date", &bios->biosDate);
ffStrbufInit(&bios->biosRelease);
getHostValue("/sys/devices/virtual/dmi/id/bios_release", "/sys/class/dmi/id/bios_release", &bios->biosRelease);
ffStrbufInit(&bios->biosVendor);
getHostValue("/sys/devices/virtual/dmi/id/bios_vendor", "/sys/class/dmi/id/bios_vendor", &bios->biosVendor);
ffStrbufInit(&bios->biosVersion);
getHostValue("/sys/devices/virtual/dmi/id/bios_version", "/sys/class/dmi/id/bios_version", &bios->biosVersion);
return NULL;
}

View File

@ -1,11 +1,6 @@
#include "bios.h"
void ffDetectBios(FFBiosResult* bios)
const char* ffDetectBios(FF_MAYBE_UNUSED FFBiosResult* bios)
{
ffStrbufInitS(&bios->error, "Not supported on this platform");
ffStrbufInit(&bios->biosDate);
ffStrbufInit(&bios->biosRelease);
ffStrbufInit(&bios->biosVendor);
ffStrbufInit(&bios->biosVersion);
return "Not supported on this platform";
}

View File

@ -1,21 +1,15 @@
#include "bios.h"
#include "util/windows/registry.h"
void ffDetectBios(FFBiosResult* bios)
const char* ffDetectBios(FFBiosResult* bios)
{
ffStrbufInit(&bios->error);
ffStrbufInit(&bios->biosDate);
ffStrbufInit(&bios->biosRelease);
ffStrbufInit(&bios->biosVendor);
ffStrbufInit(&bios->biosVersion);
FF_HKEY_AUTO_DESTROY hKey = NULL;
if(!ffRegOpenKeyForRead(HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\BIOS", &hKey, &bios->error))
return;
if(!ffRegOpenKeyForRead(HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\BIOS", &hKey, NULL))
return "ffRegOpenKeyForRead(HKEY_LOCAL_MACHINE, L\"HARDWARE\\DESCRIPTION\\System\\BIOS\", &hKey, NULL) failed";
if(!ffRegReadStrbuf(hKey, L"BIOSVersion", &bios->biosRelease, NULL))
return "\"HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\BIOS\\BIOSVersion\" doesn't exist";
if(!ffRegReadStrbuf(hKey, L"BIOSVersion", &bios->biosRelease, &bios->error))
return;
ffRegReadStrbuf(hKey, L"BIOSVendor", &bios->biosVendor, NULL);
ffRegReadStrbuf(hKey, L"BIOSReleaseDate", &bios->biosDate, NULL);
@ -25,4 +19,6 @@ void ffDetectBios(FFBiosResult* bios)
ffRegReadUint(hKey, L"BiosMinorRelease", &minor, NULL)
)
ffStrbufAppendF(&bios->biosVersion, "%u.%u", (unsigned)major, (unsigned)minor);
return NULL;
}

View File

@ -7,16 +7,21 @@
void ffPrintBios(FFinstance* instance, FFBiosOptions* options)
{
FFBiosResult result;
ffDetectBios(&result);
FFBiosResult bios;
ffStrbufInit(&bios.biosDate);
ffStrbufInit(&bios.biosRelease);
ffStrbufInit(&bios.biosVendor);
ffStrbufInit(&bios.biosVersion);
if(result.error.length > 0)
const char* error = ffDetectBios(&bios);
if(error)
{
ffPrintError(instance, FF_BIOS_MODULE_NAME, 0, &options->moduleArgs, "%*s", result.error.length, result.error.chars);
ffPrintError(instance, FF_BIOS_MODULE_NAME, 0, &options->moduleArgs, "%s", error);
goto exit;
}
if(result.biosRelease.length == 0)
if(bios.biosRelease.length == 0)
{
ffPrintError(instance, FF_BIOS_MODULE_NAME, 0, &options->moduleArgs, "bios_release is not set.");
goto exit;
@ -25,24 +30,23 @@ void ffPrintBios(FFinstance* instance, FFBiosOptions* options)
if(options->moduleArgs.outputFormat.length == 0)
{
ffPrintLogoAndKey(instance, FF_BIOS_MODULE_NAME, 0, &options->moduleArgs.key);
puts(result.biosRelease.chars);
puts(bios.biosRelease.chars);
}
else
{
ffPrintFormat(instance, FF_BIOS_MODULE_NAME, 0, &options->moduleArgs, FF_BIOS_NUM_FORMAT_ARGS, (FFformatarg[]) {
{FF_FORMAT_ARG_TYPE_STRBUF, &result.biosDate},
{FF_FORMAT_ARG_TYPE_STRBUF, &result.biosRelease},
{FF_FORMAT_ARG_TYPE_STRBUF, &result.biosVendor},
{FF_FORMAT_ARG_TYPE_STRBUF, &result.biosVersion},
{FF_FORMAT_ARG_TYPE_STRBUF, &bios.biosDate},
{FF_FORMAT_ARG_TYPE_STRBUF, &bios.biosRelease},
{FF_FORMAT_ARG_TYPE_STRBUF, &bios.biosVendor},
{FF_FORMAT_ARG_TYPE_STRBUF, &bios.biosVersion},
});
}
exit:
ffStrbufDestroy(&result.biosDate);
ffStrbufDestroy(&result.biosRelease);
ffStrbufDestroy(&result.biosVendor);
ffStrbufDestroy(&result.biosVersion);
ffStrbufDestroy(&result.error);
ffStrbufDestroy(&bios.biosDate);
ffStrbufDestroy(&bios.biosRelease);
ffStrbufDestroy(&bios.biosVendor);
ffStrbufDestroy(&bios.biosVersion);
}
void ffInitBiosOptions(FFBiosOptions* options)