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 biosRelease;
FFstrbuf biosVendor; FFstrbuf biosVendor;
FFstrbuf biosVersion; FFstrbuf biosVersion;
FFstrbuf error;
} FFBiosResult; } FFBiosResult;
void ffDetectBios(FFBiosResult* bios); const char* ffDetectBios(FFBiosResult* bios);
#endif #endif

View File

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

View File

@ -2,16 +2,11 @@
#include "common/settings.h" #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.reldate", &bios->biosDate);
ffSettingsGetFreeBSDKenv("smbios.bios.revision", &bios->biosRelease); ffSettingsGetFreeBSDKenv("smbios.bios.revision", &bios->biosRelease);
ffSettingsGetFreeBSDKenv("smbios.bios.vendor", &bios->biosVendor); ffSettingsGetFreeBSDKenv("smbios.bios.vendor", &bios->biosVendor);
ffSettingsGetFreeBSDKenv("smbios.bios.version", &bios->biosVersion); 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); 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); 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); 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); 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); 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" #include "bios.h"
void ffDetectBios(FFBiosResult* bios) const char* ffDetectBios(FF_MAYBE_UNUSED FFBiosResult* bios)
{ {
ffStrbufInitS(&bios->error, "Not supported on this platform"); return "Not supported on this platform";
ffStrbufInit(&bios->biosDate);
ffStrbufInit(&bios->biosRelease);
ffStrbufInit(&bios->biosVendor);
ffStrbufInit(&bios->biosVersion);
} }

View File

@ -1,21 +1,15 @@
#include "bios.h" #include "bios.h"
#include "util/windows/registry.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; FF_HKEY_AUTO_DESTROY hKey = NULL;
if(!ffRegOpenKeyForRead(HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\BIOS", &hKey, &bios->error)) if(!ffRegOpenKeyForRead(HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\BIOS", &hKey, NULL))
return; 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"BIOSVendor", &bios->biosVendor, NULL);
ffRegReadStrbuf(hKey, L"BIOSReleaseDate", &bios->biosDate, NULL); ffRegReadStrbuf(hKey, L"BIOSReleaseDate", &bios->biosDate, NULL);
@ -25,4 +19,6 @@ void ffDetectBios(FFBiosResult* bios)
ffRegReadUint(hKey, L"BiosMinorRelease", &minor, NULL) ffRegReadUint(hKey, L"BiosMinorRelease", &minor, NULL)
) )
ffStrbufAppendF(&bios->biosVersion, "%u.%u", (unsigned)major, (unsigned)minor); ffStrbufAppendF(&bios->biosVersion, "%u.%u", (unsigned)major, (unsigned)minor);
return NULL;
} }

View File

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