From cf884399031c711205f837a5b65eeeb26fee3258 Mon Sep 17 00:00:00 2001 From: Konstantin Aladyshev Date: Fri, 25 Feb 2022 15:39:26 +0300 Subject: Add lesson 61 Signed-off-by: Konstantin Aladyshev --- UefiLessonsPkg/UefiLessonsPkg.dsc | 1 + .../UpdateDmpstoreDump/UpdateDmpstoreDump.c | 123 +++++++++++++++++++++ .../UpdateDmpstoreDump/UpdateDmpstoreDump.inf | 26 +++++ 3 files changed, 150 insertions(+) create mode 100644 UefiLessonsPkg/UpdateDmpstoreDump/UpdateDmpstoreDump.c create mode 100644 UefiLessonsPkg/UpdateDmpstoreDump/UpdateDmpstoreDump.inf (limited to 'UefiLessonsPkg') diff --git a/UefiLessonsPkg/UefiLessonsPkg.dsc b/UefiLessonsPkg/UefiLessonsPkg.dsc index b33c87d..2180620 100644 --- a/UefiLessonsPkg/UefiLessonsPkg.dsc +++ b/UefiLessonsPkg/UefiLessonsPkg.dsc @@ -77,6 +77,7 @@ UefiLessonsPkg/HIIStaticFormDriver/HIIStaticFormDriver.inf UefiLessonsPkg/DisplayHIIByGuid/DisplayHIIByGuid.inf UefiLessonsPkg/SetVariableExample/SetVariableExample.inf + UefiLessonsPkg/UpdateDmpstoreDump/UpdateDmpstoreDump.inf [PcdsFixedAtBuild] gUefiLessonsPkgTokenSpaceGuid.PcdMyVar32_2|44 diff --git a/UefiLessonsPkg/UpdateDmpstoreDump/UpdateDmpstoreDump.c b/UefiLessonsPkg/UpdateDmpstoreDump/UpdateDmpstoreDump.c new file mode 100644 index 0000000..e926514 --- /dev/null +++ b/UefiLessonsPkg/UpdateDmpstoreDump/UpdateDmpstoreDump.c @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2021, Konstantin Aladyshev + * + * SPDX-License-Identifier: MIT + */ + +#include +#include +#include +#include + +VOID Usage() +{ + Print(L"Recalculate CRCs for dmpstore command dump\n"); + Print(L"\n"); + Print(L" UpdateDmpstoreDump \n"); +} + +INTN EFIAPI ShellAppMain(IN UINTN Argc, IN CHAR16 **Argv) +{ + if (Argc!=2) { + Usage(); + return EFI_INVALID_PARAMETER; + } + + SHELL_FILE_HANDLE FileHandle; + + CHAR16* Filename = Argv[1]; + EFI_STATUS Status = ShellOpenFileByName( + Filename, + &FileHandle, + EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ, + 0 + ); + if (EFI_ERROR(Status)) { + Print(L"Error! Can't open file %s\n", Filename); + return Status; + } + + UINT64 FileSize; + Status = ShellGetFileSize(FileHandle, &FileSize); + if (EFI_ERROR(Status)) { + Status = ShellCloseFile(&FileHandle); + return SHELL_DEVICE_ERROR; + } + + UINT64 FilePos = 0; + while (FilePos < FileSize) { + UINTN ToReadSize; + UINT32 NameSize; + ToReadSize = sizeof(NameSize); + Status = ShellReadFile(FileHandle, &ToReadSize, &NameSize); + if (EFI_ERROR(Status) || (ToReadSize != sizeof(NameSize))) { + Status = SHELL_VOLUME_CORRUPTED; + break; + } + FilePos += ToReadSize; + + UINT32 DataSize; + ToReadSize = sizeof(DataSize); + Status = ShellReadFile(FileHandle, &ToReadSize, &DataSize); + if (EFI_ERROR(Status) || (ToReadSize != sizeof(DataSize))) { + Status = SHELL_VOLUME_CORRUPTED; + break; + } + FilePos += ToReadSize; + + UINTN RemainingSize = NameSize + + sizeof(EFI_GUID) + + sizeof(UINT32) + + DataSize; + UINT8* Buffer = AllocatePool(sizeof(NameSize) + sizeof(DataSize) + RemainingSize); + if (Buffer == NULL) { + Status = SHELL_OUT_OF_RESOURCES; + break; + } + + *(UINT32*)Buffer = NameSize; + *((UINT32*)Buffer + 1) = DataSize; + + ToReadSize = RemainingSize; + Status = ShellReadFile(FileHandle, &ToReadSize, (UINT32*)Buffer + 2); + if (EFI_ERROR(Status) || (ToReadSize != RemainingSize)) { + Status = SHELL_VOLUME_CORRUPTED; + FreePool (Buffer); + break; + } + FilePos += ToReadSize; + + + UINT32 Crc32; + gBS->CalculateCrc32 ( + Buffer, + sizeof(NameSize) + sizeof(DataSize) + RemainingSize, + &Crc32 + ); + + UINTN ToWriteSize = sizeof(Crc32); + Status = ShellWriteFile( + FileHandle, + &ToWriteSize, + &Crc32 + ); + if (EFI_ERROR(Status) || (ToWriteSize != sizeof(Crc32))) { + Print(L"Error! Not all data was written\n"); + FreePool(Buffer); + break; + } + FilePos += ToWriteSize; + FreePool(Buffer); + } + + if (EFI_ERROR(Status)) { + Print(L"Error! %r\n", Status); + } + + Status = ShellCloseFile(&FileHandle); + if (EFI_ERROR(Status)) { + Print(L"Can't close file: %r\n", Status); + } + + return EFI_SUCCESS; +} diff --git a/UefiLessonsPkg/UpdateDmpstoreDump/UpdateDmpstoreDump.inf b/UefiLessonsPkg/UpdateDmpstoreDump/UpdateDmpstoreDump.inf new file mode 100644 index 0000000..eef23cc --- /dev/null +++ b/UefiLessonsPkg/UpdateDmpstoreDump/UpdateDmpstoreDump.inf @@ -0,0 +1,26 @@ +## +# Copyright (c) 2021, Konstantin Aladyshev +# +# SPDX-License-Identifier: MIT +## + +[Defines] + INF_VERSION = 1.25 + BASE_NAME = UpdateDmpstoreDump + FILE_GUID = d14fe21b-7dbf-40ff-96cb-5d6f5b63cda6 + MODULE_TYPE = UEFI_APPLICATION + VERSION_STRING = 1.0 + ENTRY_POINT = ShellCEntryLib + +[Sources] + UpdateDmpstoreDump.c + +[Packages] + MdePkg/MdePkg.dec + ShellPkg/ShellPkg.dec + +[LibraryClasses] + UefiApplicationEntryPoint + UefiLib + ShellCEntryLib + ShellLib -- cgit v1.2.3-18-g5258