Guest User

Untitled

a guest
Nov 26th, 2019
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.78 KB | None | 0 0
  1. #include <Library/BaseLib.h>
  2. #include <Uefi.h>
  3. #include <Library/UefiLib.h>
  4. #include <Library/UefiApplicationEntryPoint.h>
  5. #include <Protocol/LoadedImage.h>
  6. #include <Protocol/DevicePath.h>
  7. #include <Protocol/DevicePathToText.h>
  8. #include <Guid/ImageAuthentication.h>
  9.  
  10. //AGISGuid = 301d199a-4dc1-4b26-b557-a012d83d7a52
  11. EFI_GUID AGISGuid = {0x301d199a, 0x4dc1, 0x4b26, {0xb5, 0x57, 0xa0, 0x12, 0xd8, 0x3d, 0x7a, 0x52}};
  12.  
  13. //const
  14. EFI_GUID EfiLoadedImageProtocolGuid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
  15. EFI_GUID EfiDevicePathProtocolGuid = EFI_DEVICE_PATH_PROTOCOL_GUID;
  16. EFI_GUID EfiDevicePathToTextProtocolGuid = EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
  17. EFI_GUID EfiSimpleFileSystemProtocolGuid = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
  18.  
  19. EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
  20. EFI_DEVICE_PATH_PROTOCOL *DevicePath;
  21. EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *DevicePathToText;
  22. EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *SimpleFileSystem;
  23. EFI_FILE_PROTOCOL *RootFs, *CurDir, *FileHandle;
  24.  
  25. CHAR16 *c16_ptr = NULL;
  26. CHAR16 FileName[55];
  27.  
  28. EFI_STATUS Status;
  29.  
  30. void Dump(const unsigned char *data_buffer, const unsigned int length)
  31. {
  32.   unsigned char byte;
  33.   unsigned int i, j;
  34.   for (i = 0; i < length; i++)
  35.   {
  36.     byte = data_buffer[i];
  37.     Print(L"%02x ", data_buffer[i]); // display byte in hex
  38.     if (((i % 16) == 15) || (i == length - 1))
  39.     {
  40.       for (j = 0; j < 15 - (i % 16); j++)
  41.         Print(L"   ");
  42.       Print(L"| ");
  43.       for (j = (i - (i % 16)); j <= i; j++)
  44.       { // display printable bytes from line
  45.         byte = data_buffer[j];
  46.         if ((byte > 31) && (byte < 127)) // outside printable char range
  47.           Print(L"%c", byte);
  48.         else
  49.           Print(L".");
  50.       }
  51.       Print(L"\n"); // end of the dump line (each line 16 bytes)
  52.     }               // end if
  53.   }                 // end for
  54. }
  55.  
  56. EFI_STATUS
  57. EFIAPI
  58. UefiMain(
  59.     IN EFI_HANDLE ImageHandle,
  60.     IN EFI_SYSTEM_TABLE *SystemTable)
  61. {
  62.   Status = SystemTable->BootServices->HandleProtocol(
  63.       ImageHandle,
  64.       &EfiLoadedImageProtocolGuid,
  65.       (void **)&LoadedImage);
  66.  
  67.   if (EFI_ERROR(Status))
  68.   {
  69.     Print(L"Loaded Image Protocol Not Found");
  70.   }
  71.  
  72.   Status = SystemTable->BootServices->HandleProtocol(
  73.       LoadedImage->DeviceHandle,
  74.       &EfiDevicePathProtocolGuid,
  75.       (void **)&DevicePath);
  76.  
  77.   if (EFI_ERROR(Status))
  78.   {
  79.     Print(L"Device Path Protocol Not Found");
  80.   }
  81.  
  82.   Status = SystemTable->BootServices->LocateProtocol(
  83.       &EfiDevicePathToTextProtocolGuid,
  84.       NULL,
  85.       (void **)&DevicePathToText);
  86.  
  87.   if (EFI_ERROR(Status))
  88.   {
  89.     Print(L"Device Path To Text Not Found");
  90.   }
  91.  
  92.   c16_ptr = DevicePathToText->ConvertDevicePathToText(
  93.       DevicePath,
  94.       FALSE,
  95.       FALSE);
  96.  
  97.   if (c16_ptr != NULL)
  98.   {
  99.     Print(L"DevicePath = %s\n\r", c16_ptr);
  100.     SystemTable->BootServices->FreePool(c16_ptr);
  101.   }
  102.   else
  103.     Print(L"Something happend on DevicePathTotext");
  104.  
  105.   c16_ptr = DevicePathToText->ConvertDevicePathToText(
  106.       LoadedImage->FilePath,
  107.       FALSE,
  108.       FALSE);
  109.  
  110.   if (c16_ptr != NULL)
  111.   {
  112.     Print(L"FilePath = %s\n\r", c16_ptr);
  113.     //SystemTable->BootServices->FreePool(c16_ptr);
  114.   }
  115.   else
  116.     Print(L"Something happend on DevicePathTotext");
  117.  
  118.   SystemTable->BootServices->HandleProtocol(
  119.       LoadedImage->DeviceHandle,
  120.       &EfiSimpleFileSystemProtocolGuid,
  121.       (void **)&SimpleFileSystem);
  122.  
  123.   SimpleFileSystem->OpenVolume(
  124.       SimpleFileSystem,
  125.       &RootFs);
  126.  
  127.   CurDir = RootFs;
  128.  
  129.   StrCpyS(FileName, 42, c16_ptr);
  130.   UINTN i;
  131.   for (i = StrLen(FileName);
  132.        i >= 0 && FileName[i] != '\\' && FileName[i] != '/';
  133.        i--)
  134.     ;
  135.   FileName[i+1] = 0;
  136.   StrCatS(FileName, 42, L"file_hash.signed");
  137.   CurDir->Open(CurDir, &FileHandle, FileName, EFI_FILE_MODE_READ, 0);
  138.  
  139.   UINT8 *OsKernelBuffer;
  140.   UINTN Size = 0xf00000;
  141.   SystemTable->BootServices->AllocatePool(EfiLoaderData, Size, (void **)&OsKernelBuffer);
  142.   FileHandle->Read(FileHandle, &Size, OsKernelBuffer);
  143.   FileHandle->Close(FileHandle);
  144.  
  145.   Print(L"Opening and dumping %s\r\n", FileName);
  146.   Dump(OsKernelBuffer, Size);
  147.  
  148.   Print(L"Setting the authenticated 'file_hash' content\r\n");
  149.   Status = SystemTable->RuntimeServices->SetVariable(
  150.       L"file_hash",
  151.       //&guid,
  152.       &AGISGuid,
  153.       EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS |
  154.           EFI_VARIABLE_NON_VOLATILE |
  155.           EFI_VARIABLE_BOOTSERVICE_ACCESS |
  156.           EFI_VARIABLE_RUNTIME_ACCESS,
  157.       Size,
  158.       OsKernelBuffer);
  159.  
  160.   if (EFI_ERROR(Status))
  161.   {
  162.     Print(L"Error: %r\r\n", Status);
  163.   }
  164.   else
  165.   {
  166.     Print(L"It worked Fine !!!\r\n");
  167.   }
  168.  
  169.   SystemTable->BootServices->FreePool(OsKernelBuffer);
  170.  
  171.   // SETTING 'file_name' VALUE
  172.   for (i = StrLen(FileName);
  173.        i >= 0 && FileName[i] != '\\' && FileName[i] != '/';
  174.        i--)
  175.     ;
  176.   FileName[i+1] = 0;
  177.   StrCatS(FileName, 42, L"file_name.signed");
  178.   CurDir->Open(CurDir, &FileHandle, FileName, EFI_FILE_MODE_READ, 0);
  179.  
  180.   Size = 0xf00000;
  181.  
  182.   SystemTable->BootServices->AllocatePool(EfiLoaderData, Size, (void **)&OsKernelBuffer);
  183.   FileHandle->Read(FileHandle, &Size, OsKernelBuffer);
  184.   FileHandle->Close(FileHandle);
  185.  
  186.   Print(L"Opening and dumping %s\r\n", FileName);
  187.   Dump(OsKernelBuffer, Size);
  188.  
  189.   Print(L"Setting the authenticated 'file_name' content\r\n");
  190.   Status = SystemTable->RuntimeServices->SetVariable(
  191.       L"file_name",
  192.       //&guid,
  193.       &AGISGuid,
  194.       EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS |
  195.           EFI_VARIABLE_NON_VOLATILE |
  196.           EFI_VARIABLE_BOOTSERVICE_ACCESS |
  197.           EFI_VARIABLE_RUNTIME_ACCESS,
  198.       Size,
  199.       OsKernelBuffer);
  200.  
  201.   if (EFI_ERROR(Status))
  202.   {
  203.     Print(L"Error: %r\r\n", Status);
  204.   }
  205.   else
  206.   {
  207.     Print(L"It worked Fine !!!\r\n");
  208.   }
  209.  
  210.   SystemTable->BootServices->FreePool(OsKernelBuffer);
  211.   return EFI_SUCCESS;
  212. }
Advertisement
Add Comment
Please, Sign In to add comment