From aede42a840cc6e3509048182eb204b5ce8f3187c Mon Sep 17 00:00:00 2001 From: kavsrf Date: Wed, 14 Jun 2017 12:49:40 +0300 Subject: BML update to sort "BootOrder" --- DcsBml/DcsBml.c | 52 ++++++++++++++++++++++++++++----------------- DcsBoot/DcsBoot.c | 8 ++++--- Include/Library/CommonLib.h | 3 ++- Library/CommonLib/EfiVar.c | 9 ++++---- 4 files changed, 44 insertions(+), 28 deletions(-) diff --git a/DcsBml/DcsBml.c b/DcsBml/DcsBml.c index f5aaf3a..3a5406f 100644 --- a/DcsBml/DcsBml.c +++ b/DcsBml/DcsBml.c @@ -33,7 +33,7 @@ typedef struct _BML_GLOBALS { } BML_GLOBALS, *PBML_GLOBALS; STATIC PBML_GLOBALS gBmlData = NULL; -STATIC BOOLEAN BootMenuLocked = FALSE; +STATIC BOOLEAN BootMenuLocked = TRUE; EFI_EVENT mBmlVirtualAddrChangeEvent; EFI_SET_VARIABLE orgSetVariable = NULL; @@ -138,6 +138,32 @@ DcsBmlUnload( return EFI_SUCCESS; } +////////////////////////////////////////////////////////////////////////// +// Boot order +////////////////////////////////////////////////////////////////////////// +EFI_STATUS +UpdateBootOrder() +{ + EFI_STATUS res; + UINT16 DcsBootNum = 0x0DC5B; + UINTN boIndex = 1; + UINTN len; + UINT32 attr; + CHAR16* tmp = NULL; + res = EfiGetVar(L"BootDC5B", &gEfiGlobalVariableGuid, &tmp, &len, &attr); + if (EFI_ERROR(res)) { + InitFS(); + res = BootMenuItemCreate(L"BootDC5B", sDcsBootEfiDesc, gFileRootHandle, sDcsBootEfi, TRUE); + res = BootOrderInsert(L"BootOrder", 0, 0x0DC5B); + } else { + if (EFI_ERROR(BootOrderPresent(L"BootOrder", 0x0DC5B, &boIndex) || boIndex != 0)) { + res = BootOrderInsert(L"BootOrder", 0, 0x0DC5B); + } + } + res = EfiSetVar(L"BootNext", &gEfiGlobalVariableGuid, &DcsBootNum, sizeof(DcsBootNum), EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS); + MEM_FREE(tmp); + return res; +} /** The actual entry point for the application. @@ -173,7 +199,7 @@ DcsBmlMain( ASSERT_EFI_ERROR(res); if (EFI_ERROR(res)) { - Print(L"Install protocol %r\n", res); + ERR_PRINT(L"Install protocol %r\n", res); return res; } @@ -185,7 +211,7 @@ DcsBmlMain( ); if (EFI_ERROR(res)) { - Print(L"Allocate runtime globals %r\n", res); + ERR_PRINT(L"Allocate runtime globals %r\n", res); return res; } @@ -202,29 +228,15 @@ DcsBmlMain( ); if (EFI_ERROR(res)) { - Print(L"Register notify %r\n", res); + ERR_PRINT(L"Register notify %r\n", res); return res; } + UpdateBootOrder(); + orgSetVariable = gST->RuntimeServices->SetVariable; gST->RuntimeServices->SetVariable = BmlSetVaribale; - // select boot next - { - UINT16 DcsBootNum = 0x0DC5B; - UINTN len; - UINT32 attr; - CHAR16* tmp = NULL; - res = EfiGetVar(L"BootDC5B", &gEfiGlobalVariableGuid, &tmp, &len, &attr); - if (EFI_ERROR(res)) { - InitFS(); - res = BootMenuItemCreate(L"BootDC5B", sDcsBootEfiDesc, gFileRootHandle, sDcsBootEfi, TRUE); - res = BootOrderInsert(L"BootOrder", 0, 0x0DC5B); - } - res = EfiSetVar(L"BootNext", &gEfiGlobalVariableGuid, &DcsBootNum, sizeof(DcsBootNum), EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS); - MEM_FREE(tmp); - } - // Prepare BootDC5B return EFI_SUCCESS; } diff --git a/DcsBoot/DcsBoot.c b/DcsBoot/DcsBoot.c index 90dcb5d..de79e93 100644 --- a/DcsBoot/DcsBoot.c +++ b/DcsBoot/DcsBoot.c @@ -54,7 +54,7 @@ DoExecCmd() CHAR16* sDcsBmlEfi = L"EFI\\VeraCrypt\\DcsBml.dcs"; CHAR16* sDcsBmlEfiDesc = L"VeraCrypt(DcsBml) driver"; CHAR16* sDcsBmlDriverVar = L"DriverDC5B"; -UINT16 sDcsBmlDriverNum = 0x0DC5B; +UINT16 DcsBmlDriverNum = 0x0DC5B; VOID UpdateDriverBmlStart() { @@ -72,12 +72,14 @@ UpdateDriverBmlStart() { if (EFI_ERROR(res)) { // No -> install res = BootMenuItemCreate(sDcsBmlDriverVar, sDcsBmlEfiDesc, gFileRootHandle, sDcsBmlEfi, FALSE); +// ERR_PRINT(L"Drv %s %r\n", sDcsBmlDriverVar, res); if (!EFI_ERROR(res)) { len = 0; res = EfiGetVar(L"DriverOrder", &gEfiGlobalVariableGuid, &tmp, &len, &attr); if (!EFI_ERROR(res)) len = len / 2; - res = BootOrderInsert(L"DriverOrder", len, sDcsBmlDriverNum); + res = BootOrderInsert(L"DriverOrder", len, DcsBmlDriverNum); } +// ERR_PRINT(L"Drv order %d %r\n", len, res); } MEM_FREE(tmp); } @@ -86,7 +88,7 @@ UpdateDriverBmlStart() { res = EfiGetVar(sDcsBmlDriverVar, &gEfiGlobalVariableGuid, &tmp, &len, &attr); if (!EFI_ERROR(res)) { BootMenuItemRemove(sDcsBmlDriverVar); - BootOrderRemove(L"DriverOrder", sDcsBmlDriverNum); + BootOrderRemove(L"DriverOrder", DcsBmlDriverNum); } } MEM_FREE(tmp); diff --git a/Include/Library/CommonLib.h b/Include/Library/CommonLib.h index d64ae41..cd7e068 100644 --- a/Include/Library/CommonLib.h +++ b/Include/Library/CommonLib.h @@ -609,7 +609,8 @@ BootOrderRemove( EFI_STATUS BootOrderPresent( IN CHAR16 *OrderVarName, - UINT16 value); + UINT16 value, + UINTN *index); EFI_STATUS BootMenuItemCreate( diff --git a/Library/CommonLib/EfiVar.c b/Library/CommonLib/EfiVar.c index e9c1082..e03544f 100644 --- a/Library/CommonLib/EfiVar.c +++ b/Library/CommonLib/EfiVar.c @@ -148,7 +148,8 @@ BootOrderRemove( EFI_STATUS BootOrderPresent( IN CHAR16 *OrderVarName, - UINT16 value) + UINT16 value, + UINTN *index) { EFI_STATUS res = EFI_NOT_READY; UINT16* varBootOrder; @@ -156,19 +157,19 @@ BootOrderPresent( UINT32 varBootOrderAttr; UINTN BootOrderCount; UINTN i; - UINTN j; res = EfiGetVar(OrderVarName, &gEfiGlobalVariableGuid, &varBootOrder, &varBootOrderSize, &varBootOrderAttr); if (EFI_ERROR(res)) return res; BootOrderCount = varBootOrderSize / sizeof(UINT16); res = EFI_NOT_FOUND; - for (j = 0, i = 0; i < BootOrderCount; ++i) { + for (i = 0; i < BootOrderCount; ++i) { if (varBootOrder[i] == value) { - MEM_FREE(varBootOrder); res = EFI_SUCCESS; break; } } + if (index != NULL) *index = i; + MEM_FREE(varBootOrder); return res; } -- cgit v1.2.3