hex2: implement various improvements.

* Make sure to close all protocols before exit.
 * Use more sophisticated command line argument processinh
   that pushes command line arguments onto stack.
 * Switch to more readable M1 defines
 * Handle the case when output file is not specified
This commit is contained in:
Andrius Štikonas 2022-08-27 23:52:38 +01:00
parent 8ad04157da
commit 2b25915169
6 changed files with 1105 additions and 1071 deletions

View File

@ -19,13 +19,13 @@ DEFINE jmp EB
DEFINE jne 75 DEFINE jne 75
DEFINE lea_rdx,[rip+DWORD] 488D15 DEFINE lea_rdx,[rip+DWORD] 488D15
DEFINE mov_rdx, 48C7C2 DEFINE mov_rdx, 48C7C2
DEFINE mov_rax,rdi 4889C7
DEFINE mov_rbx,rax 4889C3 DEFINE mov_rbx,rax 4889C3
DEFINE mov_rcx,rax 4889C1 DEFINE mov_rcx,rax 4889C1
DEFINE mov_rcx,rbx 4889D9 DEFINE mov_rcx,rbx 4889D9
DEFINE mov_rcx,r12 4C89E1 DEFINE mov_rcx,r12 4C89E1
DEFINE mov_rcx,r13 4C89E9 DEFINE mov_rcx,r13 4C89E9
DEFINE mov_rcx,r15 4C89F9 DEFINE mov_rcx,r15 4C89F9
DEFINE mov_rdi,rax 4889C7
DEFINE mov_rdx,rax 4889C2 DEFINE mov_rdx,rax 4889C2
DEFINE mov_rdx,rbx 4889DA DEFINE mov_rdx,rbx 4889DA
DEFINE mov_rdx,rsp 4889E2 DEFINE mov_rdx,rsp 4889E2
@ -70,14 +70,14 @@ DEFINE xor_r9,r9 4D31C9
:_start :_start
mov_rbp,rsp # save stack pointer mov_rbp,rsp # save stack pointer
mov_r12,rcx # save image_handle mov_r12,rcx # save image_handle
mov_r14,[rdx+BYTE] !96 # system->boot mov_r14,[rdx+BYTE] !96 # save system->boot
# Open Loaded Image protocol # Open Loaded Image protocol
mov_r9,r12 # arg4 = image_handle mov_r9,r12 # arg4 = image_handle
lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL
mov_rcx,r12 # arg1 = image_handle mov_rcx,r12 # arg1 = image_handle
call %open_protocol # open protocol call %open_protocol # open protocol
mov_rax,rdi # save image mov_rdi,rax # save image
# Get root file system # Get root file system
mov_r9,r12 # arg4 = image_handle mov_r9,r12 # arg4 = image_handle

View File

@ -11,7 +11,7 @@
_start: _start:
mov rbp, rsp # save stack pointer mov rbp, rsp # save stack pointer
mov r12, rcx # save image_handle mov r12, rcx # save image_handle
mov r14, [rdx+96] # system->boot mov r14, [rdx+96] # save system->boot
# Open Loaded Image protocol # Open Loaded Image protocol
mov r9, r12 # arg4 = image_handle mov r9, r12 # arg4 = image_handle

File diff suppressed because it is too large Load Diff

View File

@ -21,101 +21,76 @@
# efi_main(void *image_handle, struct efi_system_table *system) # efi_main(void *image_handle, struct efi_system_table *system)
_start: _start:
mov rbp, rsp # save stack pointer mov rbp, rsp # save stack pointer
mov r15, rcx # save image_handle mov [rip+image_handle], rcx # save image_handle
mov r14, [rdx+96] # system->boot mov r14, [rdx+96] # system->boot
mov [rip+SystemBoot], r14 # save system->boot mov [rip+SystemBoot], r14 # save system->boot
# Open Loaded Image protocol # Open Loaded Image protocol
push rax # allocate stack for image mov r9, [rip+image_handle] # arg4 = image_handle
mov r8, rsp # arg3 = &image lea rdx, [rip+LOADED_IMAGE_PROTOCOL] # guid = &LOADED_IMAGE_PROTOCOL
mov rdx, [rip+LOADED_IMAGE_PROTOCOL+8] # EFI_LOADED_IMAGE_PROTOCOL_GUID (last 64 bits) mov rcx, r9 # arg1 = image_handle
push rdx # push last 64 bits onto stack call open_protocol # open protocol
mov rdx, [rip+LOADED_IMAGE_PROTOCOL] # EFI_LOADED_IMAGE_PROTOCOL_GUID (first 64 bits) mov rdi, rax # save image
push rdx # push first 64 bits onto stack
mov rdx, rsp # arg2 = &guid
push 1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
push 0 # arg5 = NULL
mov r9, r15 # arg4 = image_handle
mov rcx, r15 # arg1 = image_handle
sub rsp, 32 # allocate shadow stack space for UEFI function
call [r14+280] # system->boot->open_protocol(image_handle, &guid, &image, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
mov rax, [rsp+64] # get image
mov rcx, rax # save image
# Command line args
mov rbx, [rax+56] # options = image->load_options
loop_options1: # Skip application name
add rbx, 2 # ++options
mov al, [rbx] # *options
cmp al, 0x20 # if *options != ' '
jne loop_options1 # then jump
add rbx, 2 # ++options
mov r12, rbx # save input file
loop_options2: # Skip argv[1]
add rbx, 2 # ++options
mov al, [rbx] # *options
cmp al, 0x20 # if *options != ' '
jne loop_options2 # then jump
mov byte ptr [rbx], 0 # *options = 0
add rbx, 2 # ++options
mov r13, rbx # save output file
# Get root file system # Get root file system
push rax # allocate stack for rootfs mov r9, [rip+image_handle] # arg4 = image_handle
mov r8, rsp # arg3 = &rootfs lea rdx, [rip+SIMPLE_FS_PROTOCOL] # guid = &SIMPLE_FS_PROTOCOL
mov rdx, [rip+SIMPLE_FS_PROTOCOL+8] # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID (last 64 bits) mov rcx, [rdi+24] # arg1 = root_device = image->device
push rdx # push last 64 bits onto stack mov [rip+root_device], rdi # save root_device
mov rdx, [rip+SIMPLE_FS_PROTOCOL] # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID (first 64 bits) call open_protocol # open protocol
push rdx # push first 64 bits onto stack mov rcx, rax # get rootfs
mov rdx, rsp # arg2 = &guid
push 1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
push 0 # arg5 = NULL
mov r9, r15 # arg4 = image_handle
mov rcx, [rcx+24] # arg1 = root_device = image->device
sub rsp, 32 # allocate shadow stack space for UEFI function
call [r14+280] # system->boot->open_protocol(root_device, &guid, &rootfs, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
mov rcx, [rsp+64] # get rootfs
# Get root directory # Get root directory
push rdx # allocate stack for rootdir lea rdx, [rip+rootdir] # arg2 = &rootdir
mov rdx, rsp # arg2 = &rootdir
push rax # allocate shadow stack space for UEFI function push rax # allocate shadow stack space for UEFI function
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) call [rcx+8] # rootfs->open_volume(rootfs, &rootdir)
pop rax # deallocate stack pop rax # deallocate stack
pop rax # deallocate stack pop rax # deallocate stack
pop rbx # get rootdir
mov [rip+rootdir], rbx # 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
je loop_options_done # We are done
sub rbx, 2 # --options
mov al, [rbx] # *options
cmp al, 0x20 # if *options != ' '
jne loop_options # then continue looping
mov BYTE PTR [rbx], 0 # zero it
add rbx, 2 # ++options
push rbx # push another argument onto stack
jmp loop_options # next argument
loop_options_done:
# Open file for reading # Open file for reading
push rdx # allocate stack for fin lea rdx, [rip+fin] # arg2 = &fin
mov rdx, rsp # arg2 = &fin pop r8 # arg3 = in
push 1 # arg5 = EFI_FILE_READ_ONLY push 1 # arg5 = EFI_FILE_READ_ONLY
push 1 # prepare to set arg4 to EFI_FILE_MODE_READ push 1 # prepare to set arg4 to EFI_FILE_MODE_READ
pop r9 # arg4 = EFI_FILE_MODE_READ pop r9 # arg4 = EFI_FILE_MODE_READ
mov r8, r12 # arg3 = in mov rcx, [rip+rootdir] # arg1 = rootdir
mov rcx, rbx # arg1 = rootdir
sub rsp, 32 # allocate shadow stack space for UEFI function sub rsp, 32 # allocate shadow stack space for UEFI function
call [rcx+8] # rootdir->open() call [rcx+8] # rootdir->open()
mov rax, [rsp+40] # get fin add rsp, 40 # deallocate stack
mov [rip+fin], rax # save fin
# Open file for writing # Open file for writing
push rdx # allocate stack for fout pop r8 # arg3 = out
mov rdx, rsp # arg2 = &fout push 1 # Set exit code in case of failure
cmp r8, 0 # If NULL
je failed_output # then exit
lea rdx, [rip+fout] # arg2 = &fout
push 0 # arg5 = 0 push 0 # arg5 = 0
push 7 # to get 0x8000000000000003 we set the rightmost 3 bits push 7 # to get 0x8000000000000003 we set the rightmost 3 bits
pop r9 # and then do right rotation by 1 pop r9 # and then do right rotation by 1
ror r9 # arg4 = EFI_FILE_MODE_CREATE| EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ ror r9 # arg4 = EFI_FILE_MODE_CREATE| EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ
mov r8, r13 # arg3 = out mov rcx, [rip+rootdir] # arg1 = rootdir
mov rcx, rbx # arg1 = rootdir
sub rsp, 32 # allocate shadow stack space for UEFI function sub rsp, 32 # allocate shadow stack space for UEFI function
call [rcx+8] # rootdir->open() call [rcx+8] # rootdir->open()
mov r10, [rsp+40] # get fout add rsp, 40 # deallocate stack
# Allocate ourselves 16 MiB of memory # Allocate ourselves 16 MiB of memory
mov rdx, 0x1000000 # allocate 16 MiB of memory mov rdx, 0x1000000 # allocate 16 MiB of memory
@ -132,7 +107,6 @@ loop_options2: # Skip argv[1]
call First_pass # Process it call First_pass # Process it
# rewind input file # rewind input file
push r10 # Protect r10
push r11 # Protect r11 push r11 # Protect r11
mov rcx, [rip+fin] # Using our input file mov rcx, [rip+fin] # Using our input file
xor edx, edx # Offset Zero xor edx, edx # Offset Zero
@ -142,7 +116,6 @@ loop_options2: # Skip argv[1]
pop rax # deallocate stack pop rax # deallocate stack
pop rax # deallocate stack pop rax # deallocate stack
pop r11 # restore r11 pop r11 # restore r11
pop r10 # restore r10
mov r15, -1 # Our flag for byte processing mov r15, -1 # Our flag for byte processing
mov r14, 0 # temp storage for the sum mov r14, 0 # temp storage for the sum
@ -389,7 +362,6 @@ print:
Read_byte: Read_byte:
push r10 # Protect r10
push r11 # Protect r11 push r11 # Protect r11
mov rcx, [rip+fin] # arg1 = fin mov rcx, [rip+fin] # arg1 = fin
push 1 # size = 1 push 1 # size = 1
@ -407,7 +379,6 @@ Read_byte:
pop rax # save input to rax pop rax # save input to rax
pop rsi # save size to rsi pop rsi # save size to rsi
pop r11 # restore r11 pop r11 # restore r11
pop r10 # restore r10
# If the file ended (0 bytes read) return EOF # If the file ended (0 bytes read) return EOF
test esi, esi # if size == 0 test esi, esi # if size == 0
@ -419,9 +390,8 @@ Read_byte_1:
# Writes bytes stored in rax # Writes bytes stored in rax
print_chars: print_chars:
push r10 # Protect r10
push r11 # Protect r11 push r11 # Protect r11
mov rcx, r10 # arg1 = fout mov rcx, [rip+fout] # arg1 = fout
push rdx # set size push rdx # set size
mov rdx, rsp # arg2 = &size mov rdx, rsp # arg2 = &size
push rax # allocate stack push rax # allocate stack
@ -432,7 +402,6 @@ print_chars:
call [rcx+40] # fout->write() call [rcx+40] # fout->write()
add rsp, 40 # deallocate stack add rsp, 40 # deallocate stack
pop r11 # restore r11 pop r11 # restore r11
pop r10 # restore r10
ret # return ret # return
@ -609,23 +578,33 @@ Done:
terminate: terminate:
# Free pool # Free pool
push rax # save exit code 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 r14, [rip+SystemBoot] # get system->boot
mov rcx, [rip+scratch] # arg1 = scratch mov rcx, [rip+scratch] # arg1 = scratch
push rax # allocate shadow stack space for UEFI function
call [r14+72] # system->boot->free_pool(scratch) call [r14+72] # system->boot->free_pool(scratch)
pop rax # deallocate stack pop rax # deallocate stack
mov rcx, [rip+fin] # arg1 = fin mov rcx, [rip+fout] # get fout
call close_file # close fin
pop rcx # restore fout
call close_file # close fout call close_file # close fout
failed_output:
mov rcx, [rip+fin] # get fin
call close_file # close fin
mov rcx, [rip+rootdir] # get rootdir mov rcx, [rip+rootdir] # get rootdir
call close_file # close rootdir call close_file # close rootdir
mov r8, [rip+image_handle] # arg3 = image_handle
lea rdx, [rip+SIMPLE_FS_PROTOCOL] # guid = &SIMPLE_FS_PROTOCOL
mov rcx, [rip+root_device] # arg1 = root_device
call close_protocol # close protocol
mov r8, [rip+image_handle] # arg3 = image_handle
lea rdx, [rip+LOADED_IMAGE_PROTOCOL] # guid = &LOADED_IMAGE_PROTOCOL
mov rcx, r8 # arg1 = image_handle
call close_protocol # close protocol
pop rax # restore exit code pop rax # restore exit code
abort: # used for debugging only abort: # used for debugging only
@ -639,6 +618,31 @@ close_file:
pop rax # deallocate stack pop rax # deallocate stack
ret ret
# rcx: handle
# rdx: &guid
# r9: agent_handle
# returns interface
open_protocol:
push rax # allocate stack for interface
mov r8, rsp # arg3 = &interface
push 1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
push 0 # arg5 = NULL
sub rsp, 32 # allocate shadow stack space for UEFI function
call [r14+280] # system->boot->open_protocol(handle, &guid, &interface, agent_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
add rsp, 48 # deallocate stack
pop rax # get interface
ret
# rcx: handle
# rdx: &guid
# r8: agent_handle
close_protocol:
xor r9, r9 # arg4 = NULL
sub rsp, 32 # allocate shadow stack space for UEFI function
call [r14+288] # system->boot->close_protocol(handle, &guid, agent_handle, 0)
add rsp, 32 # deallocate stack
ret
# rdx: number of bytes to allocate # rdx: number of bytes to allocate
# r14: system->boot # r14: system->boot
# returns pointer in rax # returns pointer in rax
@ -671,6 +675,9 @@ SIMPLE_FS_PROTOCOL:
fin: fin:
.long 0, 0 .long 0, 0
fout:
.long 0, 0
scratch: scratch:
.long 0, 0 .long 0, 0
@ -679,3 +686,9 @@ rootdir:
SystemBoot: SystemBoot:
.long 0, 0 .long 0, 0
image_handle:
.long 0, 0
root_device:
.long 0, 0

View File

@ -138,14 +138,14 @@ F0 00 # SizeOfOptionalHeader
:_start :_start
4889E5 ; mov_rbp,rsp # save stack pointer 4889E5 ; mov_rbp,rsp # save stack pointer
4989CC ; mov_r12,rcx # save image_handle 4989CC ; mov_r12,rcx # save image_handle
4C8B72 60 ; mov_r14,[rdx+BYTE] !96 # system->boot 4C8B72 60 ; mov_r14,[rdx+BYTE] !96 # save system->boot
# Open Loaded Image protocol # Open Loaded Image protocol
4D89E1 ; mov_r9,r12 # arg4 = image_handle 4D89E1 ; mov_r9,r12 # arg4 = image_handle
488D15 %LOADED_IMAGE_PROTOCOL ; lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL 488D15 %LOADED_IMAGE_PROTOCOL ; lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL
4C89E1 ; mov_rcx,r12 # arg1 = image_handle 4C89E1 ; mov_rcx,r12 # arg1 = image_handle
E8 %open_protocol ; call %open_protocol # open protocol E8 %open_protocol ; call %open_protocol # open protocol
4889C7 ; mov_rax,rdi # save image 4889C7 ; mov_rdi,rax # save image
# Get root file system # Get root file system
4D89E1 ; mov_r9,r12 # arg4 = image_handle 4D89E1 ; mov_r9,r12 # arg4 = image_handle

File diff suppressed because it is too large Load Diff