Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Library/BaseLib.h>
- #include <Uefi.h>
- #include <Library/UefiLib.h>
- #include <Library/UefiApplicationEntryPoint.h>
- #include <Protocol/LoadedImage.h>
- #include <Protocol/DevicePath.h>
- #include <Protocol/DevicePathToText.h>
- #include <Guid/ImageAuthentication.h>
- //AGISGuid = 301d199a-4dc1-4b26-b557-a012d83d7a52
- EFI_GUID AGISGuid = {0x301d199a, 0x4dc1, 0x4b26, {0xb5, 0x57, 0xa0, 0x12, 0xd8, 0x3d, 0x7a, 0x52}};
- //const
- EFI_GUID EfiLoadedImageProtocolGuid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
- EFI_GUID EfiDevicePathProtocolGuid = EFI_DEVICE_PATH_PROTOCOL_GUID;
- EFI_GUID EfiDevicePathToTextProtocolGuid = EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
- EFI_GUID EfiSimpleFileSystemProtocolGuid = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
- EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
- EFI_DEVICE_PATH_PROTOCOL *DevicePath;
- EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *DevicePathToText;
- EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *SimpleFileSystem;
- EFI_FILE_PROTOCOL *RootFs, *CurDir, *FileHandle;
- CHAR16 *c16_ptr = NULL;
- CHAR16 FileName[55];
- EFI_STATUS Status;
- void Dump(const unsigned char *data_buffer, const unsigned int length)
- {
- unsigned char byte;
- unsigned int i, j;
- for (i = 0; i < length; i++)
- {
- byte = data_buffer[i];
- Print(L"%02x ", data_buffer[i]); // display byte in hex
- if (((i % 16) == 15) || (i == length - 1))
- {
- for (j = 0; j < 15 - (i % 16); j++)
- Print(L" ");
- Print(L"| ");
- for (j = (i - (i % 16)); j <= i; j++)
- { // display printable bytes from line
- byte = data_buffer[j];
- if ((byte > 31) && (byte < 127)) // outside printable char range
- Print(L"%c", byte);
- else
- Print(L".");
- }
- Print(L"\n"); // end of the dump line (each line 16 bytes)
- } // end if
- } // end for
- }
- EFI_STATUS
- EFIAPI
- UefiMain(
- IN EFI_HANDLE ImageHandle,
- IN EFI_SYSTEM_TABLE *SystemTable)
- {
- Status = SystemTable->BootServices->HandleProtocol(
- ImageHandle,
- &EfiLoadedImageProtocolGuid,
- (void **)&LoadedImage);
- if (EFI_ERROR(Status))
- {
- Print(L"Loaded Image Protocol Not Found");
- }
- Status = SystemTable->BootServices->HandleProtocol(
- LoadedImage->DeviceHandle,
- &EfiDevicePathProtocolGuid,
- (void **)&DevicePath);
- if (EFI_ERROR(Status))
- {
- Print(L"Device Path Protocol Not Found");
- }
- Status = SystemTable->BootServices->LocateProtocol(
- &EfiDevicePathToTextProtocolGuid,
- NULL,
- (void **)&DevicePathToText);
- if (EFI_ERROR(Status))
- {
- Print(L"Device Path To Text Not Found");
- }
- c16_ptr = DevicePathToText->ConvertDevicePathToText(
- DevicePath,
- FALSE,
- FALSE);
- if (c16_ptr != NULL)
- {
- Print(L"DevicePath = %s\n\r", c16_ptr);
- SystemTable->BootServices->FreePool(c16_ptr);
- }
- else
- Print(L"Something happend on DevicePathTotext");
- c16_ptr = DevicePathToText->ConvertDevicePathToText(
- LoadedImage->FilePath,
- FALSE,
- FALSE);
- if (c16_ptr != NULL)
- {
- Print(L"FilePath = %s\n\r", c16_ptr);
- //SystemTable->BootServices->FreePool(c16_ptr);
- }
- else
- Print(L"Something happend on DevicePathTotext");
- SystemTable->BootServices->HandleProtocol(
- LoadedImage->DeviceHandle,
- &EfiSimpleFileSystemProtocolGuid,
- (void **)&SimpleFileSystem);
- SimpleFileSystem->OpenVolume(
- SimpleFileSystem,
- &RootFs);
- CurDir = RootFs;
- StrCpyS(FileName, 42, c16_ptr);
- UINTN i;
- for (i = StrLen(FileName);
- i >= 0 && FileName[i] != '\\' && FileName[i] != '/';
- i--)
- ;
- FileName[i+1] = 0;
- StrCatS(FileName, 42, L"file_hash.signed");
- CurDir->Open(CurDir, &FileHandle, FileName, EFI_FILE_MODE_READ, 0);
- UINT8 *OsKernelBuffer;
- UINTN Size = 0xf00000;
- SystemTable->BootServices->AllocatePool(EfiLoaderData, Size, (void **)&OsKernelBuffer);
- FileHandle->Read(FileHandle, &Size, OsKernelBuffer);
- FileHandle->Close(FileHandle);
- Print(L"Opening and dumping %s\r\n", FileName);
- Dump(OsKernelBuffer, Size);
- Print(L"Setting the authenticated 'file_hash' content\r\n");
- Status = SystemTable->RuntimeServices->SetVariable(
- L"file_hash",
- //&guid,
- &AGISGuid,
- EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS |
- EFI_VARIABLE_NON_VOLATILE |
- EFI_VARIABLE_BOOTSERVICE_ACCESS |
- EFI_VARIABLE_RUNTIME_ACCESS,
- Size,
- OsKernelBuffer);
- if (EFI_ERROR(Status))
- {
- Print(L"Error: %r\r\n", Status);
- }
- else
- {
- Print(L"It worked Fine !!!\r\n");
- }
- SystemTable->BootServices->FreePool(OsKernelBuffer);
- // SETTING 'file_name' VALUE
- for (i = StrLen(FileName);
- i >= 0 && FileName[i] != '\\' && FileName[i] != '/';
- i--)
- ;
- FileName[i+1] = 0;
- StrCatS(FileName, 42, L"file_name.signed");
- CurDir->Open(CurDir, &FileHandle, FileName, EFI_FILE_MODE_READ, 0);
- Size = 0xf00000;
- SystemTable->BootServices->AllocatePool(EfiLoaderData, Size, (void **)&OsKernelBuffer);
- FileHandle->Read(FileHandle, &Size, OsKernelBuffer);
- FileHandle->Close(FileHandle);
- Print(L"Opening and dumping %s\r\n", FileName);
- Dump(OsKernelBuffer, Size);
- Print(L"Setting the authenticated 'file_name' content\r\n");
- Status = SystemTable->RuntimeServices->SetVariable(
- L"file_name",
- //&guid,
- &AGISGuid,
- EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS |
- EFI_VARIABLE_NON_VOLATILE |
- EFI_VARIABLE_BOOTSERVICE_ACCESS |
- EFI_VARIABLE_RUNTIME_ACCESS,
- Size,
- OsKernelBuffer);
- if (EFI_ERROR(Status))
- {
- Print(L"Error: %r\r\n", Status);
- }
- else
- {
- Print(L"It worked Fine !!!\r\n");
- }
- SystemTable->BootServices->FreePool(OsKernelBuffer);
- return EFI_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment