From b2ea5d8f3e3b5eaf0caec53a2b564a2a442b26c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 16 May 2024 11:12:52 +0800 Subject: [PATCH] Bootmgr: reuse shared code --- src/detection/bootmgr/bootmgr.c | 28 +++++++++++++++++-- src/detection/bootmgr/bootmgr_bsd.c | 22 ++------------- src/detection/bootmgr/bootmgr_linux.c | 22 ++------------- src/detection/bootmgr/bootmgr_windows.c | 22 ++------------- src/detection/bootmgr/{efi.h => efi_helper.h} | 4 +-- 5 files changed, 34 insertions(+), 64 deletions(-) rename src/detection/bootmgr/{efi.h => efi_helper.h} (85%) diff --git a/src/detection/bootmgr/bootmgr.c b/src/detection/bootmgr/bootmgr.c index ad38d4bd8..9d4dfa838 100644 --- a/src/detection/bootmgr/bootmgr.c +++ b/src/detection/bootmgr/bootmgr.c @@ -1,11 +1,11 @@ -#include "efi.h" +#include "efi_helper.h" static inline uint8_t evBits(uint16_t val, uint8_t mask, uint8_t shift) { return (uint8_t) ((val & (mask << shift)) >> shift); } -void ffEfiUcs2ToUtf8(const uint16_t *const chars, FFstrbuf* result) +static void ffEfiUcs2ToUtf8(const uint16_t *const chars, FFstrbuf* result) { for (uint32_t i = 0; chars[i]; i++) { @@ -24,3 +24,27 @@ void ffEfiUcs2ToUtf8(const uint16_t *const chars, FFstrbuf* result) } } } + +bool ffEfiFillLoadOption(const FFEfiLoadOption* efiOption, FFBootmgrResult* result) +{ + uint32_t descLen = 0; + while (efiOption->Description[descLen]) ++descLen; + + if (descLen) + ffEfiUcs2ToUtf8(efiOption->Description, &result->name); + + 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 + ffEfiUcs2ToUtf8((uint16_t*) filePathList->SpecificDevicePathData, &result->firmware); + return true; + } + } + + return false; +} diff --git a/src/detection/bootmgr/bootmgr_bsd.c b/src/detection/bootmgr/bootmgr_bsd.c index 180a66070..77d4e6a43 100644 --- a/src/detection/bootmgr/bootmgr_bsd.c +++ b/src/detection/bootmgr/bootmgr_bsd.c @@ -1,5 +1,5 @@ #include "bootmgr.h" -#include "efi.h" +#include "efi_helper.h" #include "common/io/io.h" #include @@ -31,25 +31,7 @@ const char* ffDetectBootmgr(FFBootmgrResult* result) if (ioctl(efifd, EFIIOC_VAR_GET, &ioc) < 0 || ioc.datasize == sizeof(buffer)) return "ioctl(EFIIOC_VAR_GET, Boot####) failed"; - FFEfiLoadOption *efiOption = (FFEfiLoadOption *)buffer; - uint32_t descLen = 0; - while (efiOption->Description[descLen]) ++descLen; - - if (descLen) - ffEfiUcs2ToUtf8(efiOption->Description, &result->name); - - 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 - ffEfiUcs2ToUtf8((uint16_t*) filePathList->SpecificDevicePathData, &result->firmware); - break; - } - } + ffEfiFillLoadOption((FFEfiLoadOption *)buffer, result); ioc.name = (efi_char[]){ 'S', 'e', 'c', 'u', 'r', 'e', 'B', 'o', 'o', 't', '\0' }; ioc.namesize = sizeof("SecureBoot") * 2; diff --git a/src/detection/bootmgr/bootmgr_linux.c b/src/detection/bootmgr/bootmgr_linux.c index dda5f5e90..e1e11f967 100644 --- a/src/detection/bootmgr/bootmgr_linux.c +++ b/src/detection/bootmgr/bootmgr_linux.c @@ -1,6 +1,6 @@ #include "bootmgr.h" #include "common/io/io.h" -#include "efi.h" +#include "efi_helper.h" #define FF_EFIVARS_PATH_PREFIX "/sys/firmware/efi/efivars/" @@ -18,25 +18,7 @@ const char* ffDetectBootmgr(FFBootmgrResult* result) 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; - - if (descLen) - ffEfiUcs2ToUtf8(efiOption->Description, &result->name); - - 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 - ffEfiUcs2ToUtf8((uint16_t*) filePathList->SpecificDevicePathData, &result->firmware); - break; - } - } + ffEfiFillLoadOption((FFEfiLoadOption *)&buffer[4], result); if (ffReadFileData(FF_EFIVARS_PATH_PREFIX "SecureBoot-" FF_EFI_GLOBAL_GUID, sizeof(buffer), buffer) == 6) result->secureBoot = buffer[4] == 1; diff --git a/src/detection/bootmgr/bootmgr_windows.c b/src/detection/bootmgr/bootmgr_windows.c index 5aed4296b..e3c687d02 100644 --- a/src/detection/bootmgr/bootmgr_windows.c +++ b/src/detection/bootmgr/bootmgr_windows.c @@ -1,5 +1,5 @@ #include "bootmgr.h" -#include "efi.h" +#include "efi_helper.h" #include @@ -43,25 +43,7 @@ const char* ffDetectBootmgr(FFBootmgrResult* result) if (size < sizeof(FFEfiLoadOption) || size == sizeof(buffer)) return "GetFirmwareEnvironmentVariableW(Boot####) failed"; - FFEfiLoadOption *efiOption = (FFEfiLoadOption *)buffer; - uint32_t descLen = 0; - while (efiOption->Description[descLen]) ++descLen; - - if (descLen) - ffEfiUcs2ToUtf8(efiOption->Description, &result->name); - - 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 - ffEfiUcs2ToUtf8((uint16_t*) filePathList->SpecificDevicePathData, &result->firmware); - break; - } - } + ffEfiFillLoadOption((FFEfiLoadOption *)buffer, result); DWORD uefiSecureBootEnabled = 0, bufSize = 0; if (RegGetValueW(HKEY_LOCAL_MACHINE, L"SYSTEM\\ControlSet001\\Control\\SecureBoot\\State", L"UEFISecureBootEnabled", RRF_RT_REG_DWORD, NULL, &uefiSecureBootEnabled, &bufSize) == ERROR_SUCCESS) diff --git a/src/detection/bootmgr/efi.h b/src/detection/bootmgr/efi_helper.h similarity index 85% rename from src/detection/bootmgr/efi.h rename to src/detection/bootmgr/efi_helper.h index 584915d1f..d650c8e97 100644 --- a/src/detection/bootmgr/efi.h +++ b/src/detection/bootmgr/efi_helper.h @@ -1,4 +1,4 @@ -#include "fastfetch.h" +#include "bootmgr.h" // https://uefi.org/specs/UEFI/2.10/10_Protocols_Device_Path_Protocol.html#generic-device-path-structures typedef struct ffEfiDevicePathProtocol @@ -19,6 +19,6 @@ typedef struct FFEfiLoadOption // uint8_t OptionalData[]; } FFEfiLoadOption; -void ffEfiUcs2ToUtf8(const uint16_t *const chars, FFstrbuf* result); +bool ffEfiFillLoadOption(const FFEfiLoadOption* efiOption, FFBootmgrResult* result); #define FF_EFI_GLOBAL_GUID "8be4df61-93ca-11d2-aa0d-00e098032b8c"