diff --git a/amd64/Development/hex2.M1 b/amd64/Development/hex2.M1 index 8e9f52b..6e9595d 100644 --- a/amd64/Development/hex2.M1 +++ b/amd64/Development/hex2.M1 @@ -105,6 +105,7 @@ DEFINE STORE32_R11_into_Address_RAX 4C8918 DEFINE STORE32_R12_into_Address_R11_Immediate8 4D8963 DEFINE STORE32_R13_into_Address_RAX_Immediate8 4C8968 DEFINE STORE64_rel_RAX 488905 +DEFINE STORE64_rel_RBX 48891D DEFINE STORE64_rel_R14 4C8935 DEFINE SUBI8_RAX 83E8 DEFINE SUBI8_RSP 4883EC @@ -199,7 +200,8 @@ DEFINE ZERO_EXTEND_BL 480FB6DB CALL_RCX_Immediate8 !8 # rootfs->open_volume(rootfs, &rootdir) POP_RAX # deallocate stack POP_RAX # deallocate stack - POP_RBX # save rootdir + POP_RBX # get rootdir + STORE64_rel_RBX %rootdir # save rootdir # Open file for reading PUSH_RDX # allocate stack for fin @@ -720,23 +722,34 @@ DEFINE ZERO_EXTEND_BL 480FB6DB :terminate PUSH_RAX # save exit code PUSH_R10 # protect fout + PUSH_RAX # allocate shadow stack space for UEFI function LOAD64_rel_R14 %SystemBoot # get system->boot LOAD64_rel_RCX %scratch # arg1 = scratch CALL_R14_Immediate8 !72 # system->boot->free_pool(scratch) + POP_RAX # deallocate stack LOAD64_rel_RCX %fin # arg1 = fin - CALL_RCX_Immediate8 !16 # fin->close(fin) - POP_RAX # deallocate stack + CALLI32 %close_file # close fin + POP_RCX # restore fout - PUSH_RAX # allocate shadow stack space for UEFI function - CALL_RCX_Immediate8 !16 # fout->close(fout) - POP_RAX # deallocate stack + CALLI32 %close_file # close fout + + LOAD64_rel_RCX %rootdir # get rootdir + CALLI32 %close_file # close rootdir + POP_RAX # restore exit code COPY_RBP_to_RSP # restore stack RET # return to UEFI +# rcx: file handle +:close_file + PUSH_RAX # allocate shadow stack space for UEFI function + CALL_RCX_Immediate8 !16 # file_handle->close(file_handle) + POP_RAX # deallocate stack + RET + # rdx: number of bytes to allocate # r14: system->boot # returns pointer in rax @@ -776,4 +789,7 @@ DEFINE ZERO_EXTEND_BL 480FB6DB :SystemBoot %0 %0 +:rootdir + %0 %0 + :PE32_end diff --git a/amd64/Development/hex2.S b/amd64/Development/hex2.S index 8b8fc77..f13272d 100644 --- a/amd64/Development/hex2.S +++ b/amd64/Development/hex2.S @@ -88,7 +88,8 @@ loop_options2: # Skip argv[1] call [rcx+8] # rootfs->open_volume(rootfs, &rootdir) pop rax # deallocate stack pop rax # deallocate stack - pop rbx # save rootdir + pop rbx # get rootdir + mov [rip+rootdir], rbx # save rootdir # Open file for reading push rdx # allocate stack for fin @@ -609,24 +610,35 @@ terminate: # Free pool push rax # save exit code push r10 # protect fout + push rax # allocate shadow stack space for UEFI function mov r14, [rip+SystemBoot] # get system->boot mov rcx, [rip+scratch] # arg1 = scratch call [r14+72] # system->boot->free_pool(scratch) + pop rax # deallocate stack mov rcx, [rip+fin] # arg1 = fin - call [rcx+16] # fin->close(fin) - pop rax # deallocate stack + call close_file # close fin + pop rcx # restore fout - push rax # allocate shadow stack space for UEFI function - call [rcx+16] # fout->close(fout) - pop rax # deallocate stack + call close_file # close fout + + mov rcx, [rip+rootdir] # get rootdir + call close_file # close rootdir + pop rax # restore exit code abort: # used for debugging only mov rsp, rbp # restore stack ret # return to UEFI +# rcx: file handle +close_file: + push rax # allocate shadow stack space for UEFI function + call [rcx+16] # file_handle->close(file_handle) + pop rax # deallocate stack + ret + # rdx: number of bytes to allocate # r14: system->boot # returns pointer in rax @@ -662,5 +674,8 @@ fin: scratch: .long 0, 0 +rootdir: +.long 0, 0 + SystemBoot: .long 0, 0 diff --git a/amd64/hex2.hex1 b/amd64/hex2.hex1 index 6b32411..b930d2d 100644 --- a/amd64/hex2.hex1 +++ b/amd64/hex2.hex1 @@ -132,9 +132,9 @@ F0 00 # SizeOfOptionalHeader # [0x148] # Start of section headers 00 00 00 00 00 00 00 00 ; Name of the section (empty) but could set to ".text" -52 06 00 00 ; VirtualSize +75 06 00 00 ; VirtualSize 00 10 00 00 ; VirtualAddress -52 06 00 00 ; SizeOfRawData +75 06 00 00 ; SizeOfRawData 70 01 00 00 ; PointerToRawData 00 00 00 00 ; PointerToRelocations 00 00 00 00 ; PointerToLinenumbers @@ -216,7 +216,8 @@ F0 00 # SizeOfOptionalHeader FF51 08 ; CALL_RCX_Immediate8 !8 # rootfs->open_volume(rootfs, &rootdir) 58 ; POP_RAX # deallocate stack 58 ; POP_RAX # deallocate stack - 5B ; POP_RBX # save rootdir + 5B ; POP_RBX # get rootdir + 48891D %2 ; STORE64_rel_RBX %rootdir # save rootdir # Open file for reading 52 ; PUSH_RDX # allocate stack for fin @@ -737,23 +738,34 @@ F0 00 # SizeOfOptionalHeader :T #:terminate 50 ; PUSH_RAX # save exit code 4152 ; PUSH_R10 # protect fout + 50 ; PUSH_RAX # allocate shadow stack space for UEFI function 4C8B35 %1 ; LOAD64_rel_R14 %SystemBoot # get system->boot 488B0D %0 ; LOAD64_rel_RCX %scratch # arg1 = scratch 41FF56 48 ; CALL_R14_Immediate8 !72 # system->boot->free_pool(scratch) + 58 ; POP_RAX # deallocate stack 488B0D %Z ; LOAD64_rel_RCX %fin # arg1 = fin - FF51 10 ; CALL_RCX_Immediate8 !16 # fin->close(fin) - 58 ; POP_RAX # deallocate stack + E8 %3 ; CALLI32 %close_file # close fin + 59 ; POP_RCX # restore fout - 50 ; PUSH_RAX # allocate shadow stack space for UEFI function - FF51 10 ; CALL_RCX_Immediate8 !16 # fout->close(fout) - 58 ; POP_RAX # deallocate stack + E8 %3 ; CALLI32 %close_file # close fout + + 488B0D %2 ; LOAD64_rel_RCX %rootdir # get rootdir + E8 %3 ; CALLI32 %close_file # close rootdir + 58 ; POP_RAX # restore exit code 4889EC ; COPY_RBP_to_RSP # restore stack C3 ; RET # return to UEFI +# rcx: file handle +:3 #:close_file + 50 ; PUSH_RAX # allocate shadow stack space for UEFI function + FF51 10 ; CALL_RCX_Immediate8 !16 # file_handle->close(file_handle) + 58 ; POP_RAX # deallocate stack + C3 ; RET + # rdx: number of bytes to allocate # r14: system->boot # returns pointer in rax @@ -793,4 +805,7 @@ F0 00 # SizeOfOptionalHeader :1 #:SystemBoot 00000000 00000000 +:2 #:rootdir + 00000000 00000000 + # :PE32_end