From e14033b3d9259aaf0465dc58deb1932ad8cc8dc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrius=20=C5=A0tikonas?= Date: Wed, 24 Aug 2022 00:17:52 +0100 Subject: [PATCH] Make catm work when no arguments are supplied. --- amd64/Development/catm.M1 | 21 ++++++++++++--------- amd64/Development/catm.S | 19 ++++++++++--------- amd64/catm.hex2 | 19 +++++++++---------- 3 files changed, 31 insertions(+), 28 deletions(-) diff --git a/amd64/Development/catm.M1 b/amd64/Development/catm.M1 index 2cdcd04..d9d6c7e 100644 --- a/amd64/Development/catm.M1 +++ b/amd64/Development/catm.M1 @@ -29,6 +29,8 @@ DEFINE mov_rcx,r15 4C89F9 DEFINE mov_rdx,rax 4889C2 DEFINE mov_rdx,rbx 4889DA DEFINE mov_rdx,rsp 4889E2 +DEFINE mov_rbp,rsp 4889E5 +DEFINE mov_rsp,rbp 4889EC DEFINE mov_r8,rbx 4989D8 DEFINE mov_r8,rsp 4989E0 DEFINE mov_r8,r12 4D89E0 @@ -61,10 +63,12 @@ DEFINE sub_rbx, 4883EB DEFINE sub_rsp, 4883EC DEFINE xor_eax,eax 31C0 DEFINE test_eax,eax 85C0 +DEFINE test_r8,r8 4D85C0 DEFINE xor_r9,r9 4D31C9 # efi_main(void *image_handle, struct efi_system_table *system) :_start + mov_rbp,rsp # save stack pointer mov_r12,rcx # save image_handle mov_r14,[rdx+BYTE] !96 # system->boot @@ -84,21 +88,17 @@ DEFINE xor_r9,r9 4D31C9 mov_rcx,rax # get rootfs # Get root directory - push_rdx # allocate stack for rootdir - mov_rdx,rsp # arg2 = &rootdir + lea_rdx,[rip+DWORD] %rootdir # arg2 = &rootdir push_rax # allocate shadow stack space for UEFI function push_rax # allocate shadow stack space for UEFI function call_[rcx+BYTE] !8 # rootfs->open_volume(rootfs, &rootdir) pop_rax # deallocate stack pop_rax # deallocate stack - pop_rax # get rootdir - mov_[rip+DWORD],rax %rootdir # save rootdir # Push command line arguments onto stack mov_rbx,[rdi+BYTE] !56 # options = image->load_options mov_rdx,rbx # save beginning of load_options add_rbx,[rdi+BYTE] !48 # go to the end of load_options - push !0 # Save end of arguments (NULL) onto stack :loop_options cmp_rbx,rdx # Check if we are done @@ -113,6 +113,8 @@ DEFINE xor_r9,r9 4D31C9 jmp !loop_options # next argument :loop_options_done pop_r8 # get output file + test_r8,r8 # if no output file + je !exit_early # then exit # Open file for writing push_rdx # allocate stack for fout @@ -161,12 +163,12 @@ DEFINE xor_r9,r9 4D31C9 mov_rcx,r15 # Get output file call %close_file # close it - mov_rcx,[rip+DWORD] %rootdir # Get rootdir - call %close_file # close it - mov_rcx,rbx # Get buffer call %free_pool # release it - xor_eax,eax # Exit code 0 + +:exit_early + mov_rcx,[rip+DWORD] %rootdir # Get rootdir + call %close_file # close it mov_r8,r12 # arg3 = image_handle lea_rdx,[rip+DWORD] %SIMPLE_FS_PROTOCOL # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID @@ -178,6 +180,7 @@ DEFINE xor_r9,r9 4D31C9 mov_rcx,r12 # arg1 = image_handle call %close_protocol # close protocol + mov_rsp,rbp # restore stack pointer ret # return to UEFI # r8: input file name diff --git a/amd64/Development/catm.S b/amd64/Development/catm.S index 46978ed..19728bd 100644 --- a/amd64/Development/catm.S +++ b/amd64/Development/catm.S @@ -9,6 +9,7 @@ # efi_main(void *image_handle, struct efi_system_table *system) _start: + mov rbp, rsp # save stack pointer mov r12, rcx # save image_handle mov r14, [rdx+96] # system->boot @@ -27,21 +28,18 @@ _start: call open_protocol # open protocol mov rcx, rax # get rootfs - push rdx # allocate stack for rootdir - mov rdx, rsp # arg2 = &rootdir + # Get root directory + lea rdx, [rip+rootdir] # arg2 = &rootdir push rax # allocate shadow stack space for UEFI function push rax # allocate shadow stack space for UEFI function call [rcx+8] # rootfs->open_volume(rootfs, &rootdir) pop rax # deallocate stack pop rax # deallocate stack - pop rax # get rootdir - mov [rip+rootdir], rax # save rootdir # Push command line arguments onto stack mov rbx, [rdi+56] # options = image->load_options mov rdx, rbx # save beginning of load_options add rbx, [rdi+48] # go to the end of load_options - push 0 # Save end of arguments (NULL) onto stack loop_options: cmp rbx, rdx # Check if we are done @@ -56,6 +54,8 @@ loop_options: jmp loop_options # next argument loop_options_done: pop r8 # get output file + test r8, r8 # if no output file + je exit_early # then exit # Open file for writing push rdx # allocate stack for fout @@ -104,12 +104,12 @@ done: mov rcx, r15 # Get output file call close_file # close it - mov rcx, [rip+rootdir] # Get rootdir - call close_file # close it - mov rcx, rbx # Get buffer call free_pool # release it - xor eax, eax # Exit code 0 + +exit_early: + mov rcx, [rip+rootdir] # Get rootdir + call close_file # close it mov r8, r12 # arg3 = image_handle lea rdx, [rip+SIMPLE_FS_PROTOCOL] # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID @@ -121,6 +121,7 @@ done: mov rcx, r12 # arg1 = image_handle call close_protocol # close protocol + mov rsp, rbp # restore stack pointer ret # return to UEFI # r8: input file name diff --git a/amd64/catm.hex2 b/amd64/catm.hex2 index 60c5532..f5090b7 100644 --- a/amd64/catm.hex2 +++ b/amd64/catm.hex2 @@ -136,7 +136,7 @@ F0 00 # SizeOfOptionalHeader # efi_main(void *image_handle, struct efi_system_table *system) :_start - + 4889E5 ; mov_rbp,rsp # save stack pointer 4989CC ; mov_r12,rcx # save image_handle 4C8B72 60 ; mov_r14,[rdx+BYTE] !96 # system->boot @@ -156,21 +156,17 @@ F0 00 # SizeOfOptionalHeader 4889C1 ; mov_rcx,rax # get rootfs # Get root directory - 52 ; push_rdx # allocate stack for rootdir - 4889E2 ; mov_rdx,rsp # arg2 = &rootdir + 488D15 %rootdir ; lea_rdx,[rip+DWORD] %rootdir # arg2 = &rootdir 50 ; push_rax # allocate shadow stack space for UEFI function 50 ; push_rax # allocate shadow stack space for UEFI function FF51 08 ; call_[rcx+BYTE] !8 # rootfs->open_volume(rootfs, &rootdir) 58 ; pop_rax # deallocate stack 58 ; pop_rax # deallocate stack - 58 ; pop_rax # get rootdir - 488905 %rootdir ; mov_[rip+DWORD],rax %rootdir # save rootdir # Push command line arguments onto stack 488B5F 38 ; mov_rbx,[rdi+BYTE] !56 # options = image->load_options 4889DA ; mov_rdx,rbx # save beginning of load_options 48035F 30 ; add_rbx,[rdi+BYTE] !48 # go to the end of load_options - 6A 00 ; push !0 # Save end of arguments (NULL) onto stack :loop_options 4839D3 ; cmp_rbx,rdx # Check if we are done @@ -185,6 +181,8 @@ F0 00 # SizeOfOptionalHeader EB !loop_options ; jmp !loop_options # next argument :loop_options_done 4158 ; pop_r8 # get output file + 4D85C0 ; test_r8,r8 # if no output file + 74 !exit_early ; je !exit_early # then exit # Open file for writing 52 ; push_rdx # allocate stack for fout @@ -233,12 +231,12 @@ F0 00 # SizeOfOptionalHeader 4C89F9 ; mov_rcx,r15 # Get output file E8 %close_file ; call %close_file # close it - 488B0D %rootdir ; mov_rcx,[rip+DWORD] %rootdir # Get rootdir - E8 %close_file ; call %close_file # close it - 4889D9 ; mov_rcx,rbx # Get buffer E8 %free_pool ; call %free_pool # release it - 31C0 ; xor_eax,eax # Exit code 0 + +:exit_early + 488B0D %rootdir ; mov_rcx,[rip+DWORD] %rootdir # Get rootdir + E8 %close_file ; call %close_file # close it 4D89E0 ; mov_r8,r12 # arg3 = image_handle 488D15 %SIMPLE_FS_PROTOCOL ; lea_rdx,[rip+DWORD] %SIMPLE_FS_PROTOCOL # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID @@ -250,6 +248,7 @@ F0 00 # SizeOfOptionalHeader 4C89E1 ; mov_rcx,r12 # arg1 = image_handle E8 %close_protocol ; call %close_protocol # close protocol + 4889EC ; mov_rsp,rbp # restore stack C3 ; ret # return to UEFI # r8: input file name