From 9eca6e0b4833c3f6992c3b4e5b7a30ea268af5cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sun, 23 Mar 2025 21:30:53 +0800 Subject: [PATCH] Bootmgr: report boot order --- src/detection/bootmgr/bootmgr.h | 1 + src/detection/bootmgr/bootmgr_bsd.c | 4 +++- src/detection/bootmgr/bootmgr_linux.c | 6 ++++-- src/detection/bootmgr/bootmgr_windows.c | 5 ++--- src/modules/bootmgr/bootmgr.c | 3 +++ 5 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/detection/bootmgr/bootmgr.h b/src/detection/bootmgr/bootmgr.h index 082149e8e..c2d1ac2a3 100644 --- a/src/detection/bootmgr/bootmgr.h +++ b/src/detection/bootmgr/bootmgr.h @@ -6,6 +6,7 @@ typedef struct FFBootmgrResult { FFstrbuf name; FFstrbuf firmware; + uint16_t order; bool secureBoot; } FFBootmgrResult; diff --git a/src/detection/bootmgr/bootmgr_bsd.c b/src/detection/bootmgr/bootmgr_bsd.c index 74a8114af..c2924fcf6 100644 --- a/src/detection/bootmgr/bootmgr_bsd.c +++ b/src/detection/bootmgr/bootmgr_bsd.c @@ -35,8 +35,10 @@ const char* ffDetectBootmgr(FFBootmgrResult* result) if (ioctl(efifd, EFIIOC_VAR_GET, &ioc) < 0 || ioc.datasize != 2) return "ioctl(EFIIOC_VAR_GET, BootCurrent) failed"; + result->order = *(uint16_t*)buffer; + unsigned char hex[5]; - snprintf((char*) hex, sizeof(hex), "%04X", *(uint16_t*)buffer); + snprintf((char*) hex, sizeof(hex), "%04X", result->order); ioc.datasize = sizeof(buffer); ioc.name = (efi_char[]){ 'B', 'o', 'o', 't', hex[0], hex[1], hex[2], hex[3], '\0' }; ioc.namesize = sizeof("Boot####") * 2; diff --git a/src/detection/bootmgr/bootmgr_linux.c b/src/detection/bootmgr/bootmgr_linux.c index 27738f724..7a47dc5f3 100644 --- a/src/detection/bootmgr/bootmgr_linux.c +++ b/src/detection/bootmgr/bootmgr_linux.c @@ -11,8 +11,10 @@ const char* ffDetectBootmgr(FFBootmgrResult* result) if (ffReadFileData(FF_EFIVARS_PATH_PREFIX "BootCurrent-" FF_EFI_GLOBAL_GUID, sizeof(buffer), buffer) != 6) return "Failed to read efivar: BootCurrent"; - uint16_t value = *(uint16_t *)&buffer[4]; - snprintf((char*) buffer, sizeof(buffer), FF_EFIVARS_PATH_PREFIX "Boot%04X-" FF_EFI_GLOBAL_GUID, value); + + result->order = *(uint16_t *)&buffer[4]; + + snprintf((char*) buffer, sizeof(buffer), FF_EFIVARS_PATH_PREFIX "Boot%04X-" FF_EFI_GLOBAL_GUID, result->order); ssize_t size = ffReadFileData((const char*) buffer, sizeof(buffer), buffer); if (size < 5 + (int) sizeof(FFEfiLoadOption) || size == (ssize_t) sizeof(buffer)) diff --git a/src/detection/bootmgr/bootmgr_windows.c b/src/detection/bootmgr/bootmgr_windows.c index 54314bbad..36b946b8f 100644 --- a/src/detection/bootmgr/bootmgr_windows.c +++ b/src/detection/bootmgr/bootmgr_windows.c @@ -32,13 +32,12 @@ const char* ffDetectBootmgr(FFBootmgrResult* result) if (enablePrivilege(L"SeSystemEnvironmentPrivilege") != NULL) return "Failed to enable SeSystemEnvironmentPrivilege"; - uint16_t value; - if (GetFirmwareEnvironmentVariableW(L"BootCurrent", L"{" FF_EFI_GLOBAL_GUID L"}", &value, sizeof(value)) != 2) + if (GetFirmwareEnvironmentVariableW(L"BootCurrent", L"{" FF_EFI_GLOBAL_GUID L"}", &result->order, sizeof(result->order)) != 2) return "GetFirmwareEnvironmentVariableW(BootCurrent) failed"; uint8_t buffer[2048]; wchar_t key[16]; - swprintf(key, ARRAY_SIZE(key), L"Boot%04X", value); + swprintf(key, ARRAY_SIZE(key), L"Boot%04X", result->order); uint32_t size = GetFirmwareEnvironmentVariableW(key, L"{" FF_EFI_GLOBAL_GUID L"}", buffer, sizeof(buffer)); if (size < sizeof(FFEfiLoadOption) || size == ARRAY_SIZE(buffer)) return "GetFirmwareEnvironmentVariableW(Boot####) failed"; diff --git a/src/modules/bootmgr/bootmgr.c b/src/modules/bootmgr/bootmgr.c index 70e081e00..19162f15a 100644 --- a/src/modules/bootmgr/bootmgr.c +++ b/src/modules/bootmgr/bootmgr.c @@ -42,6 +42,7 @@ void ffPrintBootmgr(FFBootmgrOptions* options) FF_FORMAT_ARG(bootmgr.firmware, "firmware-path"), FF_FORMAT_ARG(firmwareName, "firmware-name"), FF_FORMAT_ARG(bootmgr.secureBoot, "secure-boot"), + FF_FORMAT_ARG(bootmgr.order, "order"), })); } @@ -102,6 +103,7 @@ void ffGenerateBootmgrJsonResult(FF_MAYBE_UNUSED FFBootmgrOptions* options, yyjs yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); yyjson_mut_obj_add_strbuf(doc, obj, "name", &bootmgr.name); yyjson_mut_obj_add_strbuf(doc, obj, "firmware", &bootmgr.firmware); + yyjson_mut_obj_add_uint(doc, obj, "order", bootmgr.order); yyjson_mut_obj_add_bool(doc, obj, "secureBoot", bootmgr.secureBoot); exit: @@ -122,6 +124,7 @@ static FFModuleBaseInfo ffModuleInfo = { {"Firmware file path", "firmware-path"}, {"Firmware file name", "firmware-name"}, {"Is secure boot enabled", "secure-boot"}, + {"Boot order", "order"}, })) };