Re: Examples opening and reading/writing a file with EDK2


alejandro.estay@...
 

Hi I've wrote another little program trying to focus on this (it was added into MdeModulePkg)
------------------------------------------------------------------------------------------------------------------------------
#include <Uefi.h>
#include <Library/UefiApplicationEntryPoint.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiLib.h>
#include <Guid/FileInfo.h>
#include <Protocol/LoadedImage.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/IoLib.h>



// Piezo speaker related
#define EFI_SPEAKER_CONTROL_PORT 0x61
#define EFI_SPEAKER_OFF_MASK 0xFC
#define EFI_BEEP_ON_TIME_INTERVAL 0x50000
#define EFI_BEEP_OFF_TIME_INTERVAL 0x50000

#define EFI_TIMER_CONTROL_PORT 0x43
#define EFI_TIMER_CONTROL2_PORT 0x42

#define EFI_DEFAULT_BEEP_NUMBER 1
#define EFI_DEFAULT_BEEP_ON_TIME 0x50000
#define EFI_DEFAULT_BEEP_OFF_TIME 0x50000
#define EFI_DEFAULT_BEEP_FREQUENCY 0x500
#define EFI_DEFAULT_BEEP_ALTFREQUENCY 0x1500

#define I8254_CTRL_SEL_CTR(x) ((x) << 6)
#define I8254_CTRL_RW(x) (((x) & 0x3) << 4)
#define I8254_CTRL_LATCH I8254_CTRL_RW(0)
#define I8254_CTRL_REG 0x03
#define I8254_CTRL_LSB_MSB I8254_CTRL_RW(3)



/**
as the real entry point for the application.

@param[in] ImageHandle The firmware allocated handle for the EFI image.
@param[in] SystemTable A pointer to the EFI System Table.

@retval EFI_SUCCESS The entry point is executed successfully.
@retval other Some error occurs when executing this entry point.

**/
EFI_STATUS call_status;
//EFI_GUID gEfiSimpleFileSystemProtocolGuid = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
EFI_GUID gEfiFileInfoGuid = EFI_FILE_INFO_ID;
EFI_GUID gEfiLoadedImageProtocolGuid = EFI_LOADED_IMAGE_PROTOCOL_GUID;

EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *FileSystem;



EFI_FILE *Root;
EFI_FILE *text_file;
//CHAR16 *image_text={L"oe\r\n"};
CHAR16 image_text[21]={};
UINTN buffer_siz=21;

int index;




VOID turn_spkr_on( VOID )
{
UINT8 Data;

Data = IoRead8( EFI_SPEAKER_CONTROL_PORT );
Data |= 0x03;
IoWrite8( EFI_SPEAKER_CONTROL_PORT, Data) ;
}


VOID turn_spkr_off( VOID )
{
UINT8 Data;

Data = IoRead8( EFI_SPEAKER_CONTROL_PORT );
Data &= EFI_SPEAKER_OFF_MASK;
IoWrite8( EFI_SPEAKER_CONTROL_PORT, Data );
}


VOID GenerateBeep( VOID )
{

turn_spkr_on();
gBS->Stall( EFI_BEEP_ON_TIME_INTERVAL );
turn_spkr_off();
gBS->Stall( EFI_BEEP_OFF_TIME_INTERVAL );

}


EFI_STATUS ChangeFrequency( UINT16 Frequency )
{
UINT8 Data;

Data = 0xB6;
IoWrite8(EFI_TIMER_CONTROL_PORT, Data);

Data = (UINT8)(Frequency & 0x00FF);
IoWrite8( EFI_TIMER_CONTROL2_PORT, Data );
Data = (UINT8)((Frequency & 0xFF00) >> 8);
IoWrite8( EFI_TIMER_CONTROL2_PORT, Data );

return EFI_SUCCESS;
}


EFI_STATUS EFIAPI UefiMain (IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
{
//DEBUG ((EFI_D_INFO, "My Entry point: 0x%08x\r\n", (CHAR16*)UefiMainMySampleApp ) );
Print(L"abriendo archivo...\n");
EFI_HANDLE_PROTOCOL HandleProtocol = SystemTable->BootServices->HandleProtocol;
call_status = HandleProtocol(ImageHandle, &gEfiLoadedImageProtocolGuid, (void**)&LoadedImage);
call_status = HandleProtocol(LoadedImage->DeviceHandle, &gEfiSimpleFileSystemProtocolGuid, (void**)&FileSystem);
FileSystem->OpenVolume(FileSystem, &Root);
call_status = Root->Open(Root, &text_file, L"text_img", EFI_FILE_MODE_READ, EFI_FILE_READ_ONLY);
if (call_status != EFI_SUCCESS)
{
SystemTable->ConOut->OutputString(SystemTable->ConOut, L"imagen no encontrada\r\n");
return call_status;
}
else
{
call_status=ChangeFrequency(EFI_DEFAULT_BEEP_ALTFREQUENCY);

GenerateBeep();

SystemTable->ConOut->OutputString(SystemTable->ConOut, L"leyendo\r\n");


call_status=Root->Read(text_file, &buffer_siz, &image_text);


SystemTable->ConOut->OutputString(SystemTable->ConOut, image_text);
}

return EFI_SUCCESS;
}
---------------------------------------
When I pass over read, GDB reports that image_text is 0. If I understood this, I believe that Read() must place the address of the first of the read elements from the file (20 elements). The file appears to be properly opened (i have the QEMU OVMF log also), but I'm failing reading it and the pointer variable is not being filled (or I'm confusing it).

I'm kinda rookie with C and pointer notation still confuses me sometimes.
Thanks for your answers

Here's the QEMU OVMF log tail
FSOpen: Open '\boot2.efi' Success
[Security] 3rd party image[0] can be loaded after EndOfDxe: PciRoot(0x0)/Pci(0x1,0x1)/Ata(Primary,Master,0x0)/HD(1,MBR,0xBE1AFDFA,0x3F,0xFBFC1)/\boot2.efi.
InstallProtocolInterface: 5B1B31A1-9562-11D2-8E3F-00A0C969723B 6F328A8
Loading driver at 0x00006AF5000 EntryPoint=0x00006AF626A boot2.efi
InstallProtocolInterface: BC62157E-3E33-4FEC-9920-2D3B36D750DF 6F66E90
ProtectUefiImageCommon - 0x6F328A8
- 0x0000000006AF5000 - 0x0000000000002000
InstallProtocolInterface: 752F3136-4E16-4FDC-A22A-E5F46812F4CA 7EA9AA0
FSOpen: Open 'text_img' Success
FSOpen: Open '\' Success

Join discuss@edk2.groups.io to automatically receive all group messages.