unit tydqexecutable;
interface
uses tydqfs,uefi;
type tydq_file_segment=packed record
segmentnamecount:byte;
segmentcodecount:qword;
end;
tydq_file_executable=packed record
ftype:byte;
farch:byte;
fsegmentcount:qword;
end;
const tydq_arch_x64=$00;
tydq_arch_aa64=$01;
tydq_arch_loongarch64=$02;
var machinestr:array[1..256] of WideChar;
machinecode:array[1..268435456] of byte;
machinelen:dword;
procedure tydq_execute_executable(edl:efi_disk_list;filename:PWideChar;parameter:PPointer;userlevel:byte;belonguserindex:qword);
implementation
procedure tydq_execute_executable(systemtable:Pefi_system_table;edl:efi_disk_list;filename:PWideChar;parameter:PPointer;userlevel:byte;belonguserindex:qword);[public,alias:'TYDQ_EXECUTE_EXECUTABLE'];
var fsi:tydqfs_file;
partstr:PWideChar;
procnum,pos,i,j:natuint;
fe:tydq_file_executable;
fs:tydq_file_segment;
Bytecode:^PByte;
begin
partstr:=tydq_fs_locate_fullpath(edl,filename); procnum:=tydq_fs_locate_diskindex(edl,filename);
position:=tydq_fs_file_position(edl,procnum,partstr);
fsi:=tydq_fs_file_info(edl,procnum,partstr);
Wstrfree(partstr);
if(fsi.fattribute=0) then
begin
efi_console_output_string(systemtable,'Error:Executable cannot be executed,Permission denied.'#10); exit;
end
else if(tydq_fs_byte_to_attribute_bool(fsi.fattribute)[1]=false) then
begin
efi_console_output_string(systemtable,'Error:file is not executable file,file cannot be executed.'#10); exit;
end
else
begin
dp^.ReadDisk(dp,bp^.Media^.MediaId,position+sizeof(tydqfs_file),sizeof(tydq_file_executable),fe);
ByteCode:=allocmem(fe.fsegmentcount div sizeof(natuint)*sizeof(Pbyte));
for i:=1 to fe.fsegmentcount div sizeof(natuint) do
begin
dp^.ReadDisk(dp,bp^.Media^.MediaId,position+sizeof(tydqfs_file)+sizeof(tydq_file_executable)+(i-1)*sizeof(natuint),sizeof(natuint),pos);
dp^.ReadDisk(dp,bp^.Media^.MediaId,position+sizeof(tydqfs_file)+pos,sizeof(tydq_file_segment),fs);
dp^.ReadDisk(dp,bp^.Media^.MediaId,position+sizeof(tydqfs_file)+pos+sizeof(tydq_file_segment),
fs.segmentnamecount,machinestr);
dp^.ReadDisk(dp,bp^.Media^.MediaId,position+sizeof(tydqfs_file)+pos+sizeof(tydq_file_segment)+fs.segmentnamecount,
fs.segmentcodecount,machinecode);
(ByteCode+i-1)^:=allocmem(fs.segmentcodecount*sizeof(byte));
for j:=1 to fs.segmentcodecount do
begin
((ByteCode+i-1)^+j-1)^:=machinecode[j];
end;
end;
for i:=fe.fsegmentcount div sizeof(natuint) downto 1 do Freemem((ByteCode+i-1)^);
Freemem(ByteCode);
end;
end;
end.