From 359a7232e6f9bbb693a418b7289e521f5884ae16 Mon Sep 17 00:00:00 2001 From: Konstantin Aladyshev Date: Tue, 6 Feb 2024 16:07:23 +0300 Subject: Add applications for hidden BIOS settings lesson Signed-off-by: Konstantin Aladyshev --- UefiLessonsPkg/HiddenSettings/Data.h | 17 ++ UefiLessonsPkg/HiddenSettings/Form.vfr | 93 ++++++++ UefiLessonsPkg/HiddenSettings/HiddenSettings.c | 267 +++++++++++++++++++++++ UefiLessonsPkg/HiddenSettings/HiddenSettings.inf | 32 +++ UefiLessonsPkg/HiddenSettings/Strings.uni | 28 +++ 5 files changed, 437 insertions(+) create mode 100644 UefiLessonsPkg/HiddenSettings/Data.h create mode 100644 UefiLessonsPkg/HiddenSettings/Form.vfr create mode 100644 UefiLessonsPkg/HiddenSettings/HiddenSettings.c create mode 100644 UefiLessonsPkg/HiddenSettings/HiddenSettings.inf create mode 100644 UefiLessonsPkg/HiddenSettings/Strings.uni (limited to 'UefiLessonsPkg/HiddenSettings') diff --git a/UefiLessonsPkg/HiddenSettings/Data.h b/UefiLessonsPkg/HiddenSettings/Data.h new file mode 100644 index 0000000..171e406 --- /dev/null +++ b/UefiLessonsPkg/HiddenSettings/Data.h @@ -0,0 +1,17 @@ +#ifndef _DATA_H_ +#define _DATA_H_ + +#define FORMSET_GUID {0x98259761, 0x6b0b, 0x4531, {0xa7, 0x4b, 0x67, 0xaf, 0x5d, 0x8a, 0x81, 0x53}} +#define DATAPATH_GUID {0x3f3d5b97, 0x48e8, 0x4f4a, {0x85, 0x1e, 0x82, 0xe6, 0x50, 0x93, 0x0a, 0xeb}} +#define STORAGE_GUID {0x3f5996a7, 0x8b56, 0x472c, {0x95, 0x38, 0x47, 0x5f, 0x77, 0x15, 0x7c, 0x84}} +#define STORAGE_EFI_GUID {0x01539e15, 0x2a73, 0x4aaf, {0x9d, 0x3e, 0x43, 0x7b, 0xfa, 0xbb, 0x46, 0x66}} + +#pragma pack(1) +typedef struct { + UINT8 CheckboxValue; + UINT16 NumericValue; + UINT8 OneOfValue; +} VARIABLE_STRUCTURE; +#pragma pack() + +#endif diff --git a/UefiLessonsPkg/HiddenSettings/Form.vfr b/UefiLessonsPkg/HiddenSettings/Form.vfr new file mode 100644 index 0000000..1aafddc --- /dev/null +++ b/UefiLessonsPkg/HiddenSettings/Form.vfr @@ -0,0 +1,93 @@ +#include +#include "Data.h" + +formset + guid = FORMSET_GUID, + title = STRING_TOKEN(FORMSET_TITLE), + help = STRING_TOKEN(FORMSET_HELP), + + varstore VARIABLE_STRUCTURE, + name = FormData, + guid = STORAGE_GUID; + + efivarstore VARIABLE_STRUCTURE, + attribute = EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE, + name = FormEfiData, + guid = STORAGE_EFI_GUID; + + defaultstore StandardDefault, + prompt = STRING_TOKEN(STANDARD_DEFAULT_PROMPT), + attribute = 0x0000; + + form + formid = 1, + title = STRING_TOKEN(FORMID1_TITLE); + +suppressif TRUE; + checkbox + varid = FormData.CheckboxValue, + prompt = STRING_TOKEN(CHECKBOX_PROMPT), + help = STRING_TOKEN(CHECKBOX_HELP), + default = FALSE, defaultstore = StandardDefault, + endcheckbox; +endif; + +suppressif ideqval FormData.CheckboxValue == 0; + numeric + name = NumericQuestion, + varid = FormData.NumericValue, + prompt = STRING_TOKEN(NUMERIC_PROMPT), + help = STRING_TOKEN(NUMERIC_HELP), + flags = NUMERIC_SIZE_2 | DISPLAY_UINT_HEX, + minimum = 0, + maximum = 10, + step = 1, + default = 5, defaultstore = StandardDefault, + endnumeric; + + oneof + name = OneOfQuestion, + varid = FormData.OneOfValue, + prompt = STRING_TOKEN(ONEOF_PROMPT), + help = STRING_TOKEN(ONEOF_HELP), + option text = STRING_TOKEN(ONEOF_OPTION1), value = 0x00, flags = 0; + option text = STRING_TOKEN(ONEOF_OPTION2), value = 0x33, flags = DEFAULT; + option text = STRING_TOKEN(ONEOF_OPTION3), value = 0x55, flags = 0; + endoneof; +endif; + +grayoutif TRUE; + checkbox + varid = FormEfiData.CheckboxValue, + prompt = STRING_TOKEN(CHECKBOX_EFI_PROMPT), + help = STRING_TOKEN(CHECKBOX_EFI_HELP), + default = FALSE, defaultstore = StandardDefault, + endcheckbox; +endif; + +grayoutif ideqval FormEfiData.CheckboxValue == 0; + numeric + name = NumericEfiQuestion, + varid = FormEfiData.NumericValue, + prompt = STRING_TOKEN(NUMERIC_EFI_PROMPT), + help = STRING_TOKEN(NUMERIC_EFI_HELP), + flags = NUMERIC_SIZE_2 | DISPLAY_UINT_HEX, + minimum = 0, + maximum = 10, + step = 1, + default = 6, defaultstore = StandardDefault, + endnumeric; + + oneof + name = OneOfEfiQuestion, + varid = FormEfiData.OneOfValue, + prompt = STRING_TOKEN(ONEOF_EFI_PROMPT), + help = STRING_TOKEN(ONEOF_EFI_HELP), + option text = STRING_TOKEN(ONEOF_OPTION1), value = 0x00, flags = 0; + option text = STRING_TOKEN(ONEOF_OPTION2), value = 0x33, flags = 0; + option text = STRING_TOKEN(ONEOF_OPTION3), value = 0x55, flags = DEFAULT; + endoneof; +endif; + + endform; +endformset; diff --git a/UefiLessonsPkg/HiddenSettings/HiddenSettings.c b/UefiLessonsPkg/HiddenSettings/HiddenSettings.c new file mode 100644 index 0000000..58a23d4 --- /dev/null +++ b/UefiLessonsPkg/HiddenSettings/HiddenSettings.c @@ -0,0 +1,267 @@ +/* + * Copyright (c) 2024, Konstantin Aladyshev + * + * SPDX-License-Identifier: MIT + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "Data.h" + +extern UINT8 FormBin[]; + +#pragma pack(1) +typedef struct { + VENDOR_DEVICE_PATH VendorDevicePath; + EFI_DEVICE_PATH_PROTOCOL End; +} HII_VENDOR_DEVICE_PATH; +#pragma pack() + +HII_VENDOR_DEVICE_PATH mHiiVendorDevicePath = { + { + { + HARDWARE_DEVICE_PATH, + HW_VENDOR_DP, + { + (UINT8) (sizeof (VENDOR_DEVICE_PATH)), + (UINT8) ((sizeof (VENDOR_DEVICE_PATH)) >> 8) + } + }, + DATAPATH_GUID + }, + { + END_DEVICE_PATH_TYPE, + END_ENTIRE_DEVICE_PATH_SUBTYPE, + { + (UINT8) (END_DEVICE_PATH_LENGTH), + (UINT8) ((END_DEVICE_PATH_LENGTH) >> 8) + } + } +}; + +EFI_HII_HANDLE mHiiHandle = NULL; +EFI_HANDLE mDriverHandle = NULL; +EFI_HII_CONFIG_ACCESS_PROTOCOL mConfigAccess; +EFI_GUID StorageGuid = STORAGE_GUID; +EFI_STRING StorageName = L"FormData"; + +VARIABLE_STRUCTURE FormStorage; + +STATIC +EFI_STATUS +EFIAPI +ExtractConfig ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN CONST EFI_STRING Request, + OUT EFI_STRING *Progress, + OUT EFI_STRING *Results +) +{ + BOOLEAN AllocatedRequest = FALSE; + + if (Progress == NULL || Results == NULL) { + return EFI_INVALID_PARAMETER; + } + + if ((Request != NULL) && !HiiIsConfigHdrMatch(Request, &StorageGuid, StorageName)) { + return EFI_NOT_FOUND; + } + + EFI_STRING ConfigRequest = Request; + if ((Request == NULL) || (StrStr (Request, L"OFFSET") == NULL)) { + EFI_STRING ConfigRequestHdr = HiiConstructConfigHdr(&StorageGuid, StorageName, mDriverHandle); + UINTN Size = (StrLen(ConfigRequestHdr) + StrLen(L"&OFFSET=0&WIDTH=") + sizeof(UINTN)*2 + 1) * sizeof(CHAR16); + ConfigRequest = AllocateZeroPool(Size); + AllocatedRequest = TRUE; + UnicodeSPrint(ConfigRequest, Size, L"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr, sizeof(VARIABLE_STRUCTURE)); + FreePool(ConfigRequestHdr); + } + EFI_STATUS Status = gHiiConfigRouting->BlockToConfig(gHiiConfigRouting, + ConfigRequest, + (UINT8*)&FormStorage, + sizeof(VARIABLE_STRUCTURE), + Results, + Progress); + + if (AllocatedRequest) { + FreePool(ConfigRequest); + if (Request == NULL) { + *Progress = NULL; + } else if (StrStr(Request, L"OFFSET") == NULL) { + *Progress = Request + StrLen(Request); + } + } + + return Status; +} + +STATIC +EFI_STATUS +EFIAPI +RouteConfig ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN CONST EFI_STRING Configuration, + OUT EFI_STRING *Progress +) +{ + if (Configuration == NULL || Progress == NULL) { + return EFI_INVALID_PARAMETER; + } + + UINTN BlockSize = sizeof(VARIABLE_STRUCTURE); + EFI_STATUS Status = gHiiConfigRouting->ConfigToBlock(gHiiConfigRouting, + Configuration, + (UINT8*)&FormStorage, + &BlockSize, + Progress); + + return Status; +} + +STATIC +EFI_STATUS +EFIAPI +Callback ( + IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, + IN EFI_BROWSER_ACTION Action, + IN EFI_QUESTION_ID QuestionId, + IN UINT8 Type, + IN OUT EFI_IFR_TYPE_VALUE *Value, + OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest + ) +{ + return EFI_UNSUPPORTED; +} + +EFI_STATUS +EFIAPI +HiddenSettingsUnload ( + EFI_HANDLE ImageHandle + ) +{ + if (mHiiHandle != NULL) + HiiRemovePackages(mHiiHandle); + + + EFI_STATUS Status = gBS->UninstallMultipleProtocolInterfaces( + mDriverHandle, + &gEfiDevicePathProtocolGuid, + &mHiiVendorDevicePath, + &gEfiHiiConfigAccessProtocolGuid, + &mConfigAccess, + NULL); + + UINTN BufferSize; + VARIABLE_STRUCTURE EfiVarstore; + BufferSize = sizeof(VARIABLE_STRUCTURE); + EFI_GUID EfiVarGuid = STORAGE_EFI_GUID; + Status = gRT->GetVariable( + L"FormEfiData", + &EfiVarGuid, + NULL, + &BufferSize, + &EfiVarstore); + if (!EFI_ERROR(Status)) { + Status = gRT->SetVariable( + L"FormEfiData", + &EfiVarGuid, + 0, + 0, + NULL); + if (EFI_ERROR(Status)) { + Print(L"Error! Can't delete variable! %r\n", Status); + } + } + + return Status; +} + +EFI_STATUS +EFIAPI +HiddenSettingsEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + mConfigAccess.ExtractConfig = &ExtractConfig; + mConfigAccess.RouteConfig = &RouteConfig; + mConfigAccess.Callback = &Callback; + + EFI_STATUS Status; + Status = gBS->InstallMultipleProtocolInterfaces( + &mDriverHandle, + &gEfiDevicePathProtocolGuid, + &mHiiVendorDevicePath, + &gEfiHiiConfigAccessProtocolGuid, + &mConfigAccess, + NULL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + mHiiHandle = HiiAddPackages( + &gEfiCallerIdGuid, + mDriverHandle, + HiddenSettingsStrings, + FormBin, + NULL + ); + if (mHiiHandle == NULL) { + gBS->UninstallMultipleProtocolInterfaces( + mDriverHandle, + &gEfiDevicePathProtocolGuid, + &mHiiVendorDevicePath, + &gEfiHiiConfigAccessProtocolGuid, + &mConfigAccess, + NULL); + return EFI_OUT_OF_RESOURCES; + } + + EFI_STRING ConfigStr; + UINT16 DefaultId = 0; + + UINTN BufferSize; + VARIABLE_STRUCTURE EfiVarstore; + BufferSize = sizeof(VARIABLE_STRUCTURE); + EFI_GUID EfiVarGuid = STORAGE_EFI_GUID; + Status = gRT->GetVariable ( + L"FormEfiData", + &EfiVarGuid, + NULL, + &BufferSize, + &EfiVarstore); + if (EFI_ERROR(Status)) { + ZeroMem(&EfiVarstore, sizeof(EfiVarstore)); + Status = gRT->SetVariable( + L"FormEfiData", + &EfiVarGuid, + EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS, + sizeof(EfiVarstore), + &EfiVarstore); + if (EFI_ERROR(Status)) { + Print(L"Error! Can't create variable! %r\n", Status); + } + + ConfigStr = HiiConstructConfigHdr(&EfiVarGuid, L"FormEfiData", mDriverHandle); + UINT16 DefaultId = 0; + if (!HiiSetToDefaults(ConfigStr, DefaultId)) { + Print(L"Error! Can't set default configuration #%d\n", DefaultId); + } + } + + ConfigStr = HiiConstructConfigHdr(&StorageGuid, L"FormData", mDriverHandle); + if (!HiiSetToDefaults(ConfigStr, DefaultId)) { + Print(L"Error! Can't set default configuration #%d\n", DefaultId); + } + + return EFI_SUCCESS; +} diff --git a/UefiLessonsPkg/HiddenSettings/HiddenSettings.inf b/UefiLessonsPkg/HiddenSettings/HiddenSettings.inf new file mode 100644 index 0000000..731f245 --- /dev/null +++ b/UefiLessonsPkg/HiddenSettings/HiddenSettings.inf @@ -0,0 +1,32 @@ +## +# Copyright (c) 2024, Konstantin Aladyshev +# +# SPDX-License-Identifier: MIT +## + +[Defines] + INF_VERSION = 1.25 + BASE_NAME = HiddenSettings + FILE_GUID = 269d7962-badc-44da-815a-4af4f293f3e0 + MODULE_TYPE = UEFI_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = HiddenSettingsEntryPoint + UNLOAD_IMAGE = HiddenSettingsUnload + +[Sources] + HiddenSettings.c + Strings.uni + Form.vfr + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + +[LibraryClasses] + UefiDriverEntryPoint + UefiLib + UefiHiiServicesLib + HiiLib + +[Protocols] + gEfiHiiConfigAccessProtocolGuid diff --git a/UefiLessonsPkg/HiddenSettings/Strings.uni b/UefiLessonsPkg/HiddenSettings/Strings.uni new file mode 100644 index 0000000..acca471 --- /dev/null +++ b/UefiLessonsPkg/HiddenSettings/Strings.uni @@ -0,0 +1,28 @@ +// +// Copyright (c) 2024, Konstantin Aladyshev +// +// SPDX-License-Identifier: MIT +// + +#langdef en-US "English" + +#string FORMSET_TITLE #language en-US "Formset with hidden settings" +#string FORMSET_HELP #language en-US "Formset with hidden settings" +#string FORMID1_TITLE #language en-US "Form with hidden settings" +#string CHECKBOX_PROMPT #language en-US "Show varstore settings" +#string CHECKBOX_HELP #language en-US "Show varstore settings" +#string NUMERIC_PROMPT #language en-US "Numeric prompt" +#string NUMERIC_HELP #language en-US "Numeric help" +#string ONEOF_PROMPT #language en-US "OneOf list prompt" +#string ONEOF_HELP #language en-US "OneOf list help" +#string ONEOF_OPTION1 #language en-US "OneOf list option 1" +#string ONEOF_OPTION2 #language en-US "OneOf list option 2" +#string ONEOF_OPTION3 #language en-US "OneOf list option 3" +#string STRING_DEFAULT #language en-US "String default" +#string CHECKBOX_EFI_PROMPT #language en-US "Enable efivarstorage settings" +#string CHECKBOX_EFI_HELP #language en-US "Enable efivarstorage settings" +#string NUMERIC_EFI_PROMPT #language en-US "Numeric EFI prompt" +#string NUMERIC_EFI_HELP #language en-US "Numeric EFI help" +#string ONEOF_EFI_PROMPT #language en-US "OneOf list EFI prompt" +#string ONEOF_EFI_HELP #language en-US "OneOf list EFI help" +#string STANDARD_DEFAULT_PROMPT #language en-US "Standard default" -- cgit v1.2.3-18-g5258