From 555caea6c789d2ef76f0eed334027e2c7c143ee5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrius=20=C5=A0tikonas?= Date: Sat, 16 Jul 2022 20:40:06 +0100 Subject: [PATCH] hex0.S: move global variables to stack. --- amd64/Development/hex0.S | 71 ++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/amd64/Development/hex0.S b/amd64/Development/hex0.S index 092c1c1..5b37f1c 100644 --- a/amd64/Development/hex0.S +++ b/amd64/Development/hex0.S @@ -10,13 +10,21 @@ # First four arguments are passed via registers rcx, rdx, r8, r9 (if they fit in 64-bits) # but we need to leave stack space +# Registers: +# rsi rootdir +# r12 in/fin: input file name, later reused for handle +# r13 out/fout: output file name, later reused for handle +# r14 system->boot from UEFI, later reused for hex0 algorithm +# r15 image_handle from UEFI, later reused for hex0 algorithm + .global _start .text _start: - mov [ImageHandle], rcx # ImageHandle *image_handle + mov rbp, rsp # save stack + mov r15, rcx # image_handle mov rax, [rdx+96] # system->boot - mov [SystemBoot], rax # save system->boot + mov r14, rax # save system->boot # Open Loaded Image protocol sub rsp, 72 # allocate stack @@ -35,7 +43,6 @@ _start: # Command line args mov rax, [rsp+64] # get image - add rsp, 72 # deallocate stack mov rcx, rax # save image mov rax, [rax+56] # options = image->load_options @@ -46,7 +53,8 @@ loop_options1: # Skip application name jne loop_options1 # then jump add rax, 2 # ++options - push rax # in = options + #push rax # in = options + mov r12, rax loop_options2: # Skip argv[1] add rax, 2 # ++options @@ -56,7 +64,8 @@ loop_options2: # Skip argv[1] mov byte ptr [rax], 0 # *options = 0; add rax, 2 # ++options - push rax # out = options + #push rax # out = options + mov r13, rax # Get root device sub rsp, 72 # allocate stack @@ -67,43 +76,39 @@ loop_options2: # Skip argv[1] mov [rsp+56], rdx # save onto stack lea rdx, [rsp+48] # arg2 = &EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID lea r8, [rsp+64] # arg3 = &rootfs - mov r9, [ImageHandle] # arg4 = image_handle + mov r9, r15 # arg4 = image_handle mov qword ptr [rsp+32], 0 # arg5 = NULL mov qword ptr [rsp+40], 1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL - mov rax, [SystemBoot] # system->boot + mov rax, r14 # system->boot call [rax+280] # system->boot->open_protocol(); + mov rcx, [rsp+64] # get rootfs # Get root fs - mov rcx, [rsp+64] # get rootfs - add rsp, 72 # deallocate stacks - sub rsp, 24 # allocate stack - lea rdx, [RootDir] # arg2 = &rootdir + lea rdx, [rsp+16] # arg2 = &rootdir call [rcx+8] # rootfs->open_volume(rootfs, &rootdir) + mov rsi, [rsp+16] # save &rootdir # Open file for writing - add rsp, 24 # deallocate stack - - mov rcx, [RootDir] # arg1 = rootdir - pop r8 # arg3 = out - sub rsp, 40 # allocate stack - lea rdx, [fout] # arg2 = &fout + sub rsp, 48 # allocate stack + mov rcx, rsi # arg1 = rootdir + lea rdx, [rsp+40] # arg2 = &fout + mov r8, r13 # arg3 = out mov r9, 0x8000000000000003 # arg4 = EFI_FILE_MODE_CREATE| EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ mov qword ptr [rsp+32], 0 # arg5 = 0 call [rcx+8] # rootdir->open() + mov r13, [rsp+40] # get fout # Open file for reading - add rsp, 40 # deallocate stack - - mov rcx, [RootDir] # arg1 = rootdir - pop r8 # arg3 = in - sub rsp, 40 # allocate stack - lea rdx, [fin] # arg2 = &fin + mov rcx, rsi # arg1 = rootdir + mov r8, r12 # arg3 = in + sub rsp, 48 # allocate stack + lea rdx, [rsp+40] # arg2 = &fin mov r9, 1 # arg4 = EFI_FILE_MODE_READ mov qword ptr [rsp+32], 1 # arg5 = EFI_FILE_READ_ONLY call [rcx+8] # rootdir->open() - add rsp, 40 # deallocate stack + mov r12, [rsp+40] # get fin # Our flag for byte processing push -1 @@ -213,17 +218,17 @@ ascii_other: ret terminate: - sub rsp, 8 # allocate stack - mov rcx, [fin] # arg1 = fin + push rbx # allocate stack + mov rcx, r12 # arg1 = fin call [rcx+16] # fin->close() - mov rcx, [fout] # arg1 = fout + mov rcx, r13 # arg1 = fin call [rcx+16] # fout->close() - add rsp, 16 # deallocate stack + mov rsp, rbp # restore stack ret # return to UEFI read_byte: - mov rcx, [fin] # arg1 = fin + mov rcx, r12 # arg1 = fin push 1 # size = 1 mov rdx, rsp # arg2 = &size push rbx # allocate stack @@ -242,7 +247,7 @@ read_byte: # Writes byte stored in al write_byte: - mov rcx, [fout] # arg1 = fout + mov rcx, r13 # arg1 = fout push 1 # size = 1 mov rdx, rsp # arg2 = &size push rax # allocate stack @@ -254,12 +259,6 @@ write_byte: ret # return .data -ImageHandle: .quad 0 -SystemBoot: .quad 0 -RootDir: .quad 0 -fin: .quad 0 -fout: .quad 0 - # Protocol GUIDs LOADED_IMAGE_PROTOCOL: .long 0x5b1b31a1