I met an error when coding in UEFI (LocateProtocol returns efi_not_found(according to UEFI specificiations) and I don’t know why)and there is the my pascal code:
These are my codes of UEFI,it is too long to look up all,so you must bear it if you want to solve my problem.
system.pas:
unit system;
{$MODE FPC}
{$POINTERMATH ON}
interface
{$IFDEF CPU32}
const maxheap=16777216;
maxsection=4096;
{$ELSE CPU32}
const maxheap=67108864;
maxsection=16384;
{$ENDIF CPU32}
type
hresult = LongInt;
DWord = LongWord;
Cardinal = LongWord;
Integer = SmallInt;
UInt64 = QWord;
Pbyte=^byte;
Pchar=^char;
PWideChar=^WideChar;
PPWideChar=^PWideChar;
variableargument_list=^byte;
Pword=^word;
Pdword=^dword;
Pqword=^qword;
PPointer=^Pointer;
Pboolean=^boolean;
{$IFDEF CPU32}
NatUint=dword;
PNatUint=^dword;
Natint=integer;
PNatint=^integer;
{$ELSE CPU32}
NatUint=qword;
PNatUint=^qword;
Natint=int64;
PNatint=^int64;
{$ENDIF CPU32}
TTypeKind = (tkUnknown, tkInteger, tkChar, tkEnumeration, tkFloat, tkSet,
tkMethod, tkSString, tkLString, tkAString, tkWString, tkVariant, tkArray,
tkRecord, tkInterface, tkClass, tkObject, tkWChar, tkBool, tkInt64, tkQWord,
tkDynArray, tkInterfaceRaw, tkProcVar, tkUString, tkUChar, tkHelper, tkFile,
tkClassRef, tkPointer);
jmp_buf = packed record
rbx, rbp, r12, r13, r14, r15, rsp, rip: QWord;
{$IFDEF CPU64}
rsi, rdi: QWord;
xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15: record
m1, m2: QWord;
end;
mxcsr: LongWord;
fpucw: word;
padding: word;
{$ENDIF CPU64}
end;
Pjmp_buf = ^jmp_buf;
PExceptAddr = ^TExceptAddr;
TExceptAddr = record
buf: Pjmp_buf;
next: PExceptAddr;
{$IFDEF CPU16}
frametype: SmallInt;
{$ELSE CPU16}
frametype: LongInt;
{$ENDIF CPU16}
end;
PGuid = ^TGuid;
TGuid = packed record
case Integer of
1:
(Data1: DWord;
Data2: word;
Data3: word;
Data4: array [0 .. 7] of byte;
);
2:
(D1: DWord;
D2: word;
D3: word;
D4: array [0 .. 7] of byte;
);
3:
( { uuid fields according to RFC4122 }
time_low: DWord; // The low field of the timestamp
time_mid: word; // The middle field of the timestamp
time_hi_and_version: word;
// The high field of the timestamp multiplexed with the version number
clock_seq_hi_and_reserved: byte;
// The high field of the clock sequence multiplexed with the variant
clock_seq_low: byte; // The low field of the clock sequence
node: array [0 .. 5] of byte; // The spatially unique node identifier
);
end;
systemheap=record
heapcontent:array[1..maxheap] of byte;
heapsection:array[1..maxsection,1..2] of natuint;
heapcount,heaprest:natuint;
end;
function sys_getmem(size:natuint):Pointer;compilerproc;
procedure sys_freemem(var p:pointer);compilerproc;
function sys_allocmem(size:natuint):Pointer;compilerproc;
procedure sys_reallocmem(var p:Pointer;size:natuint);compilerproc;
procedure sys_move(const source;var dest;count:natuint);compilerproc;
function getmem(size:natuint):Pointer;
procedure freemem(var p:pointer);
function allocmem(size:natuint):Pointer;
procedure reallocmem(var p:Pointer;size:natuint);
procedure move(const source;var dest;count:natuint);
function strlen(str:Pchar):natuint;
function wstrlen(str:PWideChar):natuint;
procedure strinit(var str:PChar;size:natuint);
procedure wstrinit(var str:PWideChar;Size:natuint);
procedure strset(var str:PChar;val:Pchar);
procedure wstrset(var str:PWideChar;val:Pwidechar);
function strcmp(str1,str2:Pchar):natint;
function Wstrcmp(str1,str2:PwideChar):natint;
function UIntToPChar(UInt:natuint):Pchar;
function UIntToPWChar(UInt:natuint):PWideChar;
var compheap,sysheap:systemheap;
implementation
procedure compheap_delete_item(p:pointer);
var i,j,len:natuint;
begin
for i:=1 to compheap.heapcount do
begin
if(natuint(p)>=compheap.heapsection[i,1]) and (natuint(p)<=compheap.heapsection[i,2]) then break;
end;
if(i>compheap.heapcount) then exit;
len:=compheap.heapsection[i,2]-compheap.heapsection[i,1];
for j:=i+1 to compheap.heapcount do
begin
compheap.heapsection[j-1,1]:=compheap.heapsection[j,1]-len;
compheap.heapsection[j-1,2]:=compheap.heapsection[j,2]-len;
end;
compheap.heapsection[compheap.heapcount,1]:=0;
compheap.heapsection[compheap.heapcount,2]:=0;
dec(compheap.heapcount); inc(compheap.heaprest,len);
end;
function sys_getmem(size:natuint):Pointer;compilerproc;[public,alias:'FPC_GETMEM'];
var i,istart:natuint;
begin
if(compheap.heapcount>=maxsection) then sys_getmem:=nil;
if(compheap.heaprest<size) then sys_getmem:=nil;
if(size=0) then sys_getmem:=nil;
if(compheap.heapcount>0) then istart:=compheap.heapsection[compheap.heapcount,2]+1 else istart:=Natuint(@compheap.heapcontent);
inc(compheap.heapcount);
compheap.heapsection[compheap.heapcount,1]:=istart;
compheap.heapsection[compheap.heapcount,2]:=istart+size-1;
for i:=1 to size do
begin
compheap.heapcontent[istart+i-1]:=0;
end;
dec(compheap.heaprest,size);
sys_getmem:=Pointer(compheap.heapsection[compheap.heapcount,1]);
end;
procedure sys_freemem(var p:pointer);compilerproc;[public,alias:'FPC_FREEMEM'];
begin
compheap_delete_item(p); p:=nil;
end;
function sys_allocmem(size:natuint):Pointer;compilerproc;[public,alias:'FPC_ALLOCMEM'];
var i,istart:natuint;
begin
if(compheap.heapcount>=maxsection) then sys_allocmem:=nil;
if(compheap.heaprest<size) then sys_allocmem:=nil;
if(size=0) then sys_allocmem:=nil;
if(compheap.heapcount>0) then istart:=compheap.heapsection[compheap.heapcount,2]+1 else istart:=NatUint(@compheap.heapcontent);
inc(compheap.heapcount);
compheap.heapsection[compheap.heapcount,1]:=istart;
compheap.heapsection[compheap.heapcount,2]:=istart+size-1;
for i:=1 to size do
begin
compheap.heapcontent[istart+i-1]:=0;
end;
dec(compheap.heaprest,size);
sys_allocmem:=Pointer(compheap.heapsection[compheap.heapcount,1]);
end;
procedure sys_reallocmem(var p:Pointer;size:natuint);compilerproc;[public,alias:'FPC_REALLOCMEM'];
var i,istart,len:Natuint;
newp:Pointer;
p1,p2:Pchar;
begin
if(compheap.heapcount>=maxsection) then exit;
if(compheap.heaprest<size) then exit;
if(size=0) then exit;
if(compheap.heapcount>0) then istart:=compheap.heapsection[compheap.heapcount,2]+1 else istart:=Natuint(@compheap.heapcontent);
inc(compheap.heapcount);
compheap.heapsection[compheap.heapcount,1]:=istart;
compheap.heapsection[compheap.heapcount,2]:=istart+size-1;
for i:=1 to size do
begin
compheap.heapcontent[istart+i-1]:=0;
end;
dec(compheap.heaprest,size);
newp:=Pointer(compheap.heapsection[compheap.heapcount,1]);
for i:=1 to compheap.heapcount do
begin
if(NatUint(p)>=compheap.heapsection[i,1]) and (NatUint(p)<=compheap.heapsection[i,2]) then break;
end;
len:=NatUint(p)-compheap.heapsection[i,1];
p1:=@p^; p2:=@newp^;
for i:=1 to compheap.heapsection[i,2]-compheap.heapsection[i,1]+1 do p2^:=p1^;
compheap_delete_item(p); p:=newp+len;
end;
procedure sys_move(const source;var dest;count:natuint);compilerproc;[public,alias:'FPC_MOVE'];
var p1,p2:Pchar;
i:natuint;
begin
p1:=@source; p2:=@dest;
for i:=1 to count do p2^:=p1^;
end;
procedure sysheap_delete_item(p:pointer);
var i,j,len:natuint;
begin
for i:=1 to sysheap.heapcount do
begin
if(natuint(p)>=sysheap.heapsection[i,1]) and (natuint(p)<=sysheap.heapsection[i,2]) then break;
end;
if(i>sysheap.heapcount) then exit;
len:=sysheap.heapsection[i,2]- sysheap.heapsection[i,1];
for j:=i+1 to sysheap.heapcount do
begin
sysheap.heapsection[j-1,1]:= sysheap.heapsection[j,1]-len;
sysheap.heapsection[j-1,2]:= sysheap.heapsection[j,2]-len;
end;
sysheap.heapsection[sysheap.heapcount,1]:=0;
sysheap.heapsection[sysheap.heapcount,2]:=0;
dec(sysheap.heapcount); inc(sysheap.heaprest,len);
end;
function getmem(size:natuint):Pointer;[public,alias:'getmem'];
var i,istart:natuint;
begin
if(sysheap.heapcount>=maxsection) then getmem:=nil;
if(sysheap.heaprest<size) then getmem:=nil;
if(size=0) then getmem:=nil;
if(sysheap.heapcount>0) then istart:=sysheap.heapsection[sysheap.heapcount,2]+1 else istart:=Natuint(@sysheap.heapcontent);
inc(sysheap.heapcount);
sysheap.heapsection[sysheap.heapcount,1]:=istart;
sysheap.heapsection[sysheap.heapcount,2]:=istart+size-1;
for i:=1 to size do
begin
sysheap.heapcontent[istart+i-1]:=0;
end;
dec(sysheap.heaprest,size);
getmem:=Pointer(sysheap.heapsection[sysheap.heapcount,1]);
end;
procedure freemem(var p:pointer);[public,alias:'freemem'];
begin
sysheap_delete_item(p); p:=nil;
end;
function allocmem(size:natuint):Pointer;[public,alias:'allocmem'];
var i,istart:natuint;
begin
if(sysheap.heapcount>=maxsection) then allocmem:=nil;
if(sysheap.heaprest<size) then allocmem:=nil;
if(size=0) then allocmem:=nil;
if(sysheap.heapcount>0) then istart:=sysheap.heapsection[sysheap.heapcount,2]+1 else istart:=NatUint(@sysheap.heapcontent);
inc(sysheap.heapcount);
sysheap.heapsection[sysheap.heapcount,1]:=istart;
sysheap.heapsection[sysheap.heapcount,2]:=istart+size-1;
for i:=1 to size do
begin
sysheap.heapcontent[istart+i-1]:=0;
end;
dec(sysheap.heaprest,size);
allocmem:=Pointer(sysheap.heapsection[sysheap.heapcount,1]);
end;
procedure reallocmem(var p:Pointer;size:natuint);[public,alias:'reallocmem'];
var i,len:Natuint;
newp:Pointer;
begin
newp:=getmem(size);
for i:=1 to sysheap.heapcount do
begin
if(NatUint(p)>=sysheap.heapsection[i,1]) and (NatUint(p)<=sysheap.heapsection[i,2]) then break;
end;
len:=NatUint(p)-compheap.heapsection[i,1];
move(p^,newp^,sysheap.heapsection[i,2]-sysheap.heapsection[i,1]+1);
sysheap_delete_item(p); p:=newp+len;
end;
procedure move(const source;var dest;count:natuint);[public,alias:'move'];
var p1,p2:Pchar;
i:natuint;
begin
p1:=@source; p2:=@dest;
for i:=1 to count do p2^:=p1^;
end;
function strlen(str:Pchar):natuint;[public,alias:'strlen'];
var res:natuint;
begin
res:=0;
while(str^<>#0) do
begin
inc(str); inc(res);
end;
dec(str,res);
strlen:=res;
end;
function wstrlen(str:PWideChar):natuint;[public,alias:'wstrlen'];
var res:natuint;
begin
res:=0;
while(str^<>#0) do
begin
inc(str); inc(res);
end;
dec(str,res);
wstrlen:=res;
end;
function strcmp(str1,str2:Pchar):natint;[public,alias:'strcmp'];
var i:natint;
begin
i:=0;
while((str1+i)^=(str2+i)^) and ((str1+i)^<>#0) and ((str2+i)^<>#0) do inc(i);
if((str1+i)^>(str2+i)^) then strcmp:=1
else if((str1+i)^<(str2+i)^) then strcmp:=-1
else strcmp:=0;
end;
function Wstrcmp(str1,str2:PwideChar):natint;[public,alias:'wstrcmp'];
var i:natint;
begin
i:=0;
while((str1+i)^=(str2+i)^) and ((str1+i)^<>#0) and ((str2+i)^<>#0) do inc(i);
if((str1+i)^>(str2+i)^) then Wstrcmp:=1
else if((str1+i)^<(str2+i)^) then Wstrcmp:=-1
else Wstrcmp:=0;
end;
procedure strinit(var str:PChar;size:natuint);[public,alias:'strinit'];
begin
str:=allocmem(sizeof(char)*(size+1));
end;
procedure wstrinit(var str:PWideChar;Size:natuint);[public,alias:'wstrinit'];
begin
str:=allocmem(sizeof(WideChar)*(size+1));
end;
procedure strset(var str:PChar;val:Pchar);[public,alias:'strset'];
var i:natuint;
begin
i:=0;
while((val+i)^<>#0) do
begin
(str+i)^:=(val+i)^;
end;
end;
procedure wstrset(var str:PWideChar;val:Pwidechar);[public,alias:'wstrset'];
var i:natuint;
begin
i:=0;
while((val+i)^<>#0) do
begin
(str+i)^:=(val+i)^;
end;
end;
function UIntToPChar(UInt:natuint):Pchar;[public,alias:'uinttochar'];
const numchar:PChar='0123456789';
var i:byte;
myint:natuint;
mychar:PChar;
begin
mychar:=allocmem(sizeof(Char)*21);
i:=20; myint:=uint; (mychar+20)^:=#0;
repeat
begin
(mychar+i-1)^:=(numchar+myint mod 10)^;
myint:=myint div 10;
dec(i);
end;
until (myint=0);
UIntToPChar:=mychar+i;
end;
function UIntToPWChar(UInt:natuint):PWideChar;[public,alias:'uinttopwchar'];
const numchar:PWideChar='0123456789';
var i:byte;
myint:natuint;
mychar:PWideChar;
begin
mychar:=allocmem(sizeof(WideChar)*21);
i:=20; myint:=uint; (mychar+20)^:=#0;
repeat
begin
(mychar+i-1)^:=(numchar+myint mod 10)^;
myint:=myint div 10;
dec(i);
end;
until (myint=0);
UIntToPWChar:=mychar+i;
end;
var i:dword;
begin
compheap.heapcount:=0; compheap.heaprest:=maxheap;
for i:=1 to maxheap do
begin
compheap.heapcontent[i]:=0;
compheap.heapsection[i,1]:=0; compheap.heapsection[i,2]:=0;
end;
end.
uefi.pas(It is too long so I have to delete unimportant parts of it):
unit uefi;
interface
{User Defined Types}
efi_file_info_list=record
file_info:^efi_file_info;
file_count:qword;
end;
{User Defined End}
const efi_load_file_protocol_guid:efi_guid=(data1:$56EC3091;data2:$954C;data3:$11D2;data4:($8E,$3F,$00,$A0,$C9,$69,$72,$3B));
function efi_file_get_list(SystemTable:Pefi_system_table):efi_file_info_list;cdecl;
implementation
function efi_file_get_list(SystemTable:Pefi_system_table):efi_file_info_list;cdecl;[public,alias:'EFI_FILE_GET_LIST'];
var sfspp:Pointer;
sfsp:Pefi_simple_file_system_protocol;
root:Pefi_file_protocol;
filelist:efi_file_info_list;
buf_size:natuint;
buf:Pointer;
status:efi_status;
begin
status:=SystemTable^.BootServices^.LocateProtocol(@efi_simple_file_system_protocol_guid,nil,sfspp);
filelist.file_count:=0;
filelist.file_info:=nil;
if(status<>efi_success) then
begin
efi_file_get_list:=filelist;
while (True) do;
exit(filelist);
end;
sfsp:=Pefi_simple_file_system_protocol(sfspp);
sfsp^.OpenVolume(sfsp,root);
while (True) do
begin
buf_size:=sizeof(efi_file_info);
Root^.efiRead(Root,buf_size,buf);
if(buf_size=0) then break;
inc(filelist.file_count);
(filelist.file_info+filelist.file_count-1)^:=Pefi_file_info(buf)^;
end;
Root^.Close(Root);
efi_file_get_list:=filelist;
end;
end.
uefi_main.pas
unit uefimain;
interface
uses uefi;
function efi_main(ImageHandle:efi_handle;systemtable:Pefi_system_table):efi_status;cdecl;
implementation
function efi_main(ImageHandle:efi_handle;systemtable:Pefi_system_table):efi_status;cdecl;[public,alias:'efi_main'];
var mystr:PWideChar;
efi_file_list:efi_file_info_list;
i:natuint;
begin
efi_console_set_global_colour(Systemtable,efi_bck_black,efi_lightgrey);
efi_console_clear_screen(systemtable);
efi_console_get_max_row_and_max_column(systemtable,false);
efi_console_enable_mouse(systemtable);
efi_console_enable_mouse_blink(systemtable,true,500);
efi_file_list:=efi_file_get_list(systemtable);
for i:=1 to efi_file_list.file_count do
begin
efi_console_output_string(systemtable,(efi_file_list.file_info+i-1)^.FileName);
efi_console_output_string(systemtable,#13#10);
end;
efi_console_output_string(systemtable,'Hello UEFI!'+#13#10);
efi_console_read_string(systemtable,mystr);
efi_console_output_string(systemtable,mystr);
while (True) do;
efi_main:=efi_success;
end;
end.
(The Names of function is same as the UEFI API provided)
These are my shell code to build the cdrom to boot the UEFI System(It is UEFI Application in essence) and mount the virtual disk file to store the files(But it didn’t work because LocateProtocol failed,why?).
build.sh
fpc -XPx86_64-w64-mingw32- -Aas -n -O4 -Si -Sc -Sg -Xd -CX -XXs -Px86_64 -Rintel -Twin64 -Cg uefimain.pas
x86_64-w64-mingw32-ld --gc-sections -shared -Bsymbolic -nostdlib --oformat pei-x86-64 uefimain.o uefi.o system.o -e efi_main -o main.dll
objcopy -I pei-x86-64 -O efi-app-x86_64 main.dll bootx64.efi
dd if=/dev/zero of=fat.img bs=512 count=131072
/usr/sbin/mkfs.vfat -F 32 fat.img
mmd -i fat.img ::/EFI
mmd -i fat.img ::/EFI/BOOT
mcopy -i fat.img bootx64.efi ::/EFI/BOOT
mkdir iso
cp fat.img iso
xorriso -as mkisofs -R -f -e fat.img -no-emul-boot -o cdimage.iso iso
qemu-system-x86_64 -bios OVMF.fd -hda test.img -m 2048 -cdrom cdimage.iso
rm -rf iso
rm -rf *.ppu
rm -rf fat.img
rm -rf *.o
img.sh
qemu-img create -f vmdk test.img 4G
/usr/sbin/mkfs.vfat -F 32 test.img
These return none on the screen because the locateProtocol function meets the error.
I want an solution to solve why the locateProtocol function return an error code called efi_not_found(I should mount the disk in the UEFI system according to the My Linux Shell Code?)
(If you want to solve my problem using C or Rust,I will adopt them too.)