diff options
Diffstat (limited to 'UefiLessonsPkg')
-rw-r--r-- | UefiLessonsPkg/SetVariableExample/SetVariableExample.c | 84 | ||||
-rw-r--r-- | UefiLessonsPkg/SetVariableExample/SetVariableExample.inf | 24 | ||||
-rw-r--r-- | UefiLessonsPkg/UefiLessonsPkg.dsc | 1 |
3 files changed, 109 insertions, 0 deletions
diff --git a/UefiLessonsPkg/SetVariableExample/SetVariableExample.c b/UefiLessonsPkg/SetVariableExample/SetVariableExample.c new file mode 100644 index 0000000..9c99e52 --- /dev/null +++ b/UefiLessonsPkg/SetVariableExample/SetVariableExample.c @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2021, Konstantin Aladyshev <aladyshev22@gmail.com> + * + * SPDX-License-Identifier: MIT + */ + +#include <Library/UefiBootServicesTableLib.h> +#include <Library/UefiRuntimeServicesTableLib.h> +#include <Library/UefiLib.h> + + +VOID Usage() +{ + Print(L"Delete variable\n"); + Print(L" SetVariableExample <variable name>\n"); + Print(L"\n"); + Print(L"Set variable\n"); + Print(L" SetVariableExample <variable name> <attributes> <value>\n"); + Print(L"\n"); + Print(L"<attributes> can be <n|b|r>\n"); + Print(L"n - NON_VOLATILE\n"); + Print(L"b - BOOTSERVICE_ACCESS\n"); + Print(L"r - RUNTIME_ACCESS\n"); +} + +INTN EFIAPI ShellAppMain(IN UINTN Argc, IN CHAR16 **Argv) +{ + EFI_STATUS Status; + + if (Argc==2) { + CHAR16* VariableName = Argv[1]; + Status = gRT->SetVariable ( + VariableName, + &gEfiCallerIdGuid, + 0, + 0, + NULL + ); + if (EFI_ERROR (Status)) { + Print(L"%r\n", Status); + } else { + Print(L"Variable %s was successfully deleted\n", VariableName); + } + return Status; + } else if (Argc==4) { + CHAR16* VariableName = Argv[1]; + CHAR16* AttributesStr = Argv[2]; + CHAR16* Value = Argv[3]; + UINT32 Attributes = 0; + for (UINTN i=0; i<StrLen(AttributesStr); i++) { + switch(AttributesStr[i]) { + case L'n': + Attributes |= EFI_VARIABLE_NON_VOLATILE; + break; + case L'b': + Attributes |= EFI_VARIABLE_BOOTSERVICE_ACCESS; + break; + case L'r': + Attributes |= EFI_VARIABLE_RUNTIME_ACCESS; + break; + default: + Print(L"Error! Unknown attribute!"); + return EFI_INVALID_PARAMETER; + } + } + Status = gRT->SetVariable ( + VariableName, + &gEfiCallerIdGuid, + Attributes, + StrSize(Value), + Value + ); + if (EFI_ERROR (Status)) { + Print(L"%r\n", Status); + } else { + Print(L"Variable %s was successfully changed\n", VariableName); + } + return Status; + } else { + Usage(); + } + + return EFI_SUCCESS; +} diff --git a/UefiLessonsPkg/SetVariableExample/SetVariableExample.inf b/UefiLessonsPkg/SetVariableExample/SetVariableExample.inf new file mode 100644 index 0000000..948e49a --- /dev/null +++ b/UefiLessonsPkg/SetVariableExample/SetVariableExample.inf @@ -0,0 +1,24 @@ +## +# Copyright (c) 2021, Konstantin Aladyshev <aladyshev22@gmail.com> +# +# SPDX-License-Identifier: MIT +## + +[Defines] + INF_VERSION = 1.25 + BASE_NAME = SetVariableExample + FILE_GUID = bb2a829f-7943-4691-a03a-f1f48519d7e6 + MODULE_TYPE = UEFI_APPLICATION + VERSION_STRING = 1.0 + ENTRY_POINT = ShellCEntryLib + +[Sources] + SetVariableExample.c + +[Packages] + MdePkg/MdePkg.dec + +[LibraryClasses] + UefiApplicationEntryPoint + UefiLib + ShellCEntryLib diff --git a/UefiLessonsPkg/UefiLessonsPkg.dsc b/UefiLessonsPkg/UefiLessonsPkg.dsc index 67ab576..b33c87d 100644 --- a/UefiLessonsPkg/UefiLessonsPkg.dsc +++ b/UefiLessonsPkg/UefiLessonsPkg.dsc @@ -76,6 +76,7 @@ UefiLessonsPkg/HIIStaticForm/HIIStaticForm.inf UefiLessonsPkg/HIIStaticFormDriver/HIIStaticFormDriver.inf UefiLessonsPkg/DisplayHIIByGuid/DisplayHIIByGuid.inf + UefiLessonsPkg/SetVariableExample/SetVariableExample.inf [PcdsFixedAtBuild] gUefiLessonsPkgTokenSpaceGuid.PcdMyVar32_2|44 |