I am trying to load an UEFI application image from memory using Bootservices.loadImage. The buffer in memory is loaded with the binary contents of the HellowWorld.efi application. When I try to load it from memory I get that is an unsupported filetype.
However in digging through the UDK core while running I find that by the time I get to processing the fileheader I am not reading from my buffer correctly. Theirfore all of the magic numbers don't line up, so it sees it as an unrecognized file format. But I haven't been able to trace where along the way my pointer to my source gets messed up.
Im guessing the problem isint in the UDK source, so its in how i'm calling the function.
Code:
#define SIZEOF_HELLO_EFI 39679
UINT8 hexData[SIZEOF_HELLO_EFI] = {//CONTENTS OF helloworld.efi
0x4D, 0x5A, 0x90, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0x00, 0x00, 0xB8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
0x0E, 0x1F, 0xBA, 0x0E, 0x00, 0xB4, 0x09, 0xCD, 0x21, 0xB8, 0x01, 0x4C, 0xCD,
0x21, 0x54, 0x68, 0x69, 0x73, 0x20, 0x70, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D,
0x20, 0x63, 0x61, 0x6E, 0x6E, 0x6F, 0x74, 0x20, 0x62, 0x65, 0x20, 0x72, 0x75,
0x6E, 0x20, 0x69, 0x6E, 0x20, 0x44, 0x4F, 0x53, 0x20, 0x6D, 0x6F, 0x64, 0x65,
0x2E, 0x0D, 0x0D, 0x0A, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50,
0x45, 0x00, 0x00, 0x4C, 0x01, 0x07, 0x00, 0x24, 0x80, 0x00, 0x00, 0x00, 0x7E,
0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xE0, 0x00, 0x06, 0x03, 0x0B, 0x01, 0x02,
0x18, 0x00, 0x4C, 0x00, 0x00, 0x00, 0x2E, 0x00, 0x00, 0x00, 0xB8, 0x84, 0x00,
0x00, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,
//CONTINUES FOR MANY MORE BYTES, BUT HERE IS THE HEADER INFO};
void copy_helloefi(UINT8* buff)
{
int counter = 0;
while(counter < SIZEOF_HELLO_EFI){
buff[counter] = hexData[counter];
counter++;
}
}
EFI_STATUS
efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
{
EFI_BOOT_SERVICES *BootServicesTable;
UINT8* buff_ptr;
EFI_STATUS status;
MEMMAP_DEVICE_PATH mempath[2];
EFI_HANDLE myLoadedImage = NULL;
BootServicesTable = SystemTable->BootServices;
BootServicesTable->AllocatePool(EfiLoaderCode, SIZEOF_HELLO_EFI, (void **)&buff_ptr);
mempath[0].Header.Type = HARDWARE_DEVICE_PATH;
mempath[0].Header.SubType = HW_MEMMAP_DP;
mempath[0].Header.Length[0] = (UINT8)sizeof(MEMMAP_DEVICE_PATH);
mempath[0].Header.Length[1] = (UINT8)(sizeof(MEMMAP_DEVICE_PATH)>> 8);
mempath[0].MemoryType = EfiLoaderCode;
mempath[0].StartingAddress = (UINT32)buff_ptr;
mempath[0].EndingAddress = (UINT32)(buff_ptr + SIZEOF_HELLO_EFI);
mempath[1].Header.Type = END_DEVICE_PATH_TYPE;
mempath[1].Header.SubType = END_INSTANCE_DEVICE_PATH_SUBTYPE;
mempath[1].Header.Length[0] = (UINT8)sizeof(EFI_DEVICE_PATH);
mempath[1].Header.Length[1] = (UINT8)(sizeof(EFI_DEVICE_PATH)>> 8);
copy_helloefi(buff_ptr); //put the contents of helloworld.efi in buffer
status = BootServicesTable->LoadImage(FALSE, ImageHandle, (EFI_DEVICE_PATH*)&mempath, &buff_ptr, SIZEOF_HELLO_EFI, &myLoadedImage);
if (EFI_ERROR(status)) {
Print((CHAR16*)L"Could not LoadImage %r %x \r\n", status, status);
}
Print((CHAR16*)L"Loaded Image Handle %x\r\n", myLoadedImage);
//__debugbreak();
BootServicesTable->FreePool(buff_ptr);
return EFI_SUCCESS;
}
What am I doing wrong? HelloWorld.efi is PE/COFF, and if I directly invoke it it runs fine so I know its a valid efi binary.