I wanted to list the content of the root directory in UEFI, but I didn't find any protocol to do that; therefore I wanted to print the content of the directory file, but the EFI_FILE_PROTOCOL
can't read direcory files. The only other thing that I can do is to read the disk sector which contains that information, but the EFI_BLOCK_IO_PROTOCOL
returns 0x02
.
Here is the code:
#include <efi.h>
#include <efilib.h>
EFI_STATUS
efi_main(EFI_HANDLE Image, EFI_SYSTEM_TABLE* Systab)
{
EFI_LOADED_IMAGE_PROTOCOL* LoadedImage;
EFI_BLOCK_IO_PROTOCOL* BlockIO;
UINT16* Buffer;
EFI_STATUS Status;
InitializeLib(Image, Systab);
Status = uefi_call_wrapper(BS->HandleProtocol, 3,
Image,
&gEfiLoadedImageProtocolGuid,
&LoadedImage);
if(Status != EFI_SUCCESS) {
Print(L"HandleProtocol() failed [0]\n");
}
Status = uefi_call_wrapper(BS->HandleProtocol, 3,
LoadedImage->DeviceHandle,
&gEfiBlockIoProtocolGuid,
&BlockIO);
if(Status != EFI_SUCCESS) {
Print(L"HandleProtocol() failed [1]\n");
}
Status = uefi_call_wrapper(BlockIO->ReadBlocks, 5,
BlockIO,
BlockIO->Media->MediaId,
(EFI_LBA)1,
BlockIO->Media->BlockSize,
Buffer);
if(Status != EFI_SUCCESS) {
Print(L"Failed to read disk with code 0x%x\n", Status);
}
else for(int i = 0; i < 512; i++) {
Print(L"%c", (CHAR8)Buffer[i]);
}
return EFI_SUCCESS;
}
Can you spot what I did wrong?
What's this? An uninitialized pointer! You pass this to
ReadBlocks()
, then you got an error. YourBuffer
should be defined like this:By the way, the correct way to list files in a directory is to use
EFI_FILE_PROTOCOL.Read()
. The UEFI Specification says:Here is the code example (error handling is ignored)