Fix stack alignment to 16 bytes and save non-volatile registers.
This commit is contained in:
parent
d290d4101b
commit
dde77cdb0f
|
@ -10,11 +10,13 @@
|
||||||
# First four arguments are passed via registers rcx, rdx, r8, r9 (if they fit in 64-bits)
|
# First four arguments are passed via registers rcx, rdx, r8, r9 (if they fit in 64-bits)
|
||||||
# but we need to leave stack space
|
# but we need to leave stack space
|
||||||
# rax, rcx, rdx, r8, r9, r10 and r11 are volatile and can be changed by called function
|
# rax, rcx, rdx, r8, r9, r10 and r11 are volatile and can be changed by called function
|
||||||
|
# Stack must be 16-byte aligned before UEFI calls.
|
||||||
|
|
||||||
DEFINE add_eax,edi 01F8
|
DEFINE add_eax,edi 01F8
|
||||||
DEFINE add_rbx, 4883C3
|
DEFINE add_rbx, 4883C3
|
||||||
DEFINE add_rsp, 4883C4
|
DEFINE add_rsp, 4883C4
|
||||||
DEFINE add_rbx,[rdi+BYTE] 48035F
|
DEFINE add_rbx,[rdi+BYTE] 48035F
|
||||||
|
DEFINE and_rsp, 4883E4
|
||||||
DEFINE call E8
|
DEFINE call E8
|
||||||
DEFINE call_[rcx+BYTE] FF51
|
DEFINE call_[rcx+BYTE] FF51
|
||||||
DEFINE call_[r14+BYTE] 41FF56
|
DEFINE call_[r14+BYTE] 41FF56
|
||||||
|
@ -39,18 +41,22 @@ DEFINE mov_rcx,r13 4C89E9
|
||||||
DEFINE mov_rdx,rbx 4889DA
|
DEFINE mov_rdx,rbx 4889DA
|
||||||
DEFINE mov_rdx,rsp 4889E2
|
DEFINE mov_rdx,rsp 4889E2
|
||||||
DEFINE mov_rsp,rbp 4889EC
|
DEFINE mov_rsp,rbp 4889EC
|
||||||
|
DEFINE mov_rsp,r15 4C89FC
|
||||||
DEFINE mov_r8,rsp 4989E0
|
DEFINE mov_r8,rsp 4989E0
|
||||||
DEFINE mov_r8,r15 4D89F8
|
DEFINE mov_r8,r15 4D89F8
|
||||||
DEFINE mov_r9,r15 4D89F9
|
DEFINE mov_r9,r15 4D89F9
|
||||||
DEFINE mov_r13,rcx 4989CD
|
DEFINE mov_r13,rcx 4989CD
|
||||||
DEFINE mov_r15,rcx 4989CF
|
DEFINE mov_r15,rcx 4989CF
|
||||||
|
DEFINE mov_r15,rsp 4989E7
|
||||||
DEFINE mov_al,[rbx] 8A03
|
DEFINE mov_al,[rbx] 8A03
|
||||||
DEFINE mov_[rbx], C603
|
DEFINE mov_[rbx], C603
|
||||||
DEFINE mov_rbx,[rdi+BYTE] 488B5F
|
DEFINE mov_rbx,[rdi+BYTE] 488B5F
|
||||||
DEFINE mov_rcx,[rdi+BYTE] 488B4F
|
DEFINE mov_rcx,[rdi+BYTE] 488B4F
|
||||||
|
DEFINE mov_rsp,[rsp+BYTE] 488B6424
|
||||||
DEFINE mov_r14,[rdx+BYTE] 4C8B72
|
DEFINE mov_r14,[rdx+BYTE] 4C8B72
|
||||||
DEFINE pop_rax 58
|
DEFINE pop_rax 58
|
||||||
DEFINE pop_rbx 5B
|
DEFINE pop_rbx 5B
|
||||||
|
DEFINE pop_rbp 5D
|
||||||
DEFINE pop_rcx 59
|
DEFINE pop_rcx 59
|
||||||
DEFINE pop_rdi 5F
|
DEFINE pop_rdi 5F
|
||||||
DEFINE pop_rsi 5E
|
DEFINE pop_rsi 5E
|
||||||
|
@ -62,12 +68,17 @@ DEFINE pop_r14 415E
|
||||||
DEFINE pop_r15 415F
|
DEFINE pop_r15 415F
|
||||||
DEFINE push 6A
|
DEFINE push 6A
|
||||||
DEFINE push_rax 50
|
DEFINE push_rax 50
|
||||||
|
DEFINE push_rbp 55
|
||||||
DEFINE push_rbx 53
|
DEFINE push_rbx 53
|
||||||
|
DEFINE push_rdi 57
|
||||||
DEFINE push_rdx 52
|
DEFINE push_rdx 52
|
||||||
DEFINE push_rsi 56
|
DEFINE push_rsi 56
|
||||||
|
DEFINE push_rsp 54
|
||||||
|
DEFINE push_r12 4154
|
||||||
DEFINE push_r13 4155
|
DEFINE push_r13 4155
|
||||||
DEFINE push_r14 4156
|
DEFINE push_r14 4156
|
||||||
DEFINE push_r15 4157
|
DEFINE push_r15 4157
|
||||||
|
DEFINE push_[rsp] FF3424
|
||||||
DEFINE ret C3
|
DEFINE ret C3
|
||||||
DEFINE ror_r9 49D1C9
|
DEFINE ror_r9 49D1C9
|
||||||
DEFINE shl_edi, C1E7
|
DEFINE shl_edi, C1E7
|
||||||
|
@ -84,7 +95,17 @@ DEFINE xor_r9,r9 4D31C9
|
||||||
|
|
||||||
# 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
|
# Save non-volatile registers
|
||||||
|
push_rbp
|
||||||
|
mov_rbp,rsp
|
||||||
|
push_rbx
|
||||||
|
push_rdi
|
||||||
|
push_rsi
|
||||||
|
push_r12
|
||||||
|
push_r13
|
||||||
|
push_r14
|
||||||
|
push_r15
|
||||||
|
|
||||||
mov_r15,rcx # save image_handle
|
mov_r15,rcx # save image_handle
|
||||||
mov_r14,[rdx+BYTE] !96 # system->boot
|
mov_r14,[rdx+BYTE] !96 # system->boot
|
||||||
|
|
||||||
|
@ -144,6 +165,7 @@ DEFINE xor_r9,r9 4D31C9
|
||||||
|
|
||||||
# Open file for reading
|
# Open file for reading
|
||||||
pop_r8 # arg3 = in
|
pop_r8 # arg3 = in
|
||||||
|
and_rsp, !-16 # align stack to 16 bytes
|
||||||
push_rdx # allocate stack for fin
|
push_rdx # allocate stack for fin
|
||||||
mov_rdx,rsp # arg2 = &fin
|
mov_rdx,rsp # arg2 = &fin
|
||||||
push !1 # arg5 = EFI_FILE_READ_ONLY
|
push !1 # arg5 = EFI_FILE_READ_ONLY
|
||||||
|
@ -174,6 +196,7 @@ DEFINE xor_r9,r9 4D31C9
|
||||||
push_r14 # save system->boot
|
push_r14 # save system->boot
|
||||||
push_r15 # save image_handle
|
push_r15 # save image_handle
|
||||||
push_rsi # save rootdir
|
push_rsi # save rootdir
|
||||||
|
mov_r15,rsp # save stack location for cleanup
|
||||||
|
|
||||||
# Our flag for byte processing
|
# Our flag for byte processing
|
||||||
push !-1
|
push !-1
|
||||||
|
@ -279,6 +302,10 @@ DEFINE xor_r9,r9 4D31C9
|
||||||
|
|
||||||
# Writes byte stored in al
|
# Writes byte stored in al
|
||||||
:write_byte
|
:write_byte
|
||||||
|
push_rsp # align stack to 16 bytes
|
||||||
|
push_[rsp] # align stack to 16 bytes
|
||||||
|
and_rsp, !-16 # align stack to 16 bytes
|
||||||
|
push_rax # make sure next call is aligned to 16 bytes
|
||||||
mov_rcx,r13 # arg1 = fout
|
mov_rcx,r13 # arg1 = fout
|
||||||
push !1 # size = 1
|
push !1 # size = 1
|
||||||
mov_rdx,rsp # arg2 = &size
|
mov_rdx,rsp # arg2 = &size
|
||||||
|
@ -288,11 +315,15 @@ DEFINE xor_r9,r9 4D31C9
|
||||||
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+BYTE] !40 # fout->write()
|
call_[rcx+BYTE] !40 # fout->write()
|
||||||
add_rsp, !40 # deallocate stack
|
mov_rsp,[rsp+BYTE] !56 # deallocate stack
|
||||||
|
|
||||||
ret # return
|
ret # return
|
||||||
|
|
||||||
:read_byte
|
:read_byte
|
||||||
|
push_rsp # align stack to 16 bytes
|
||||||
|
push_[rsp] # align stack to 16 bytes
|
||||||
|
and_rsp, !-16 # align stack to 16 bytes
|
||||||
|
push_rax # make sure next call is aligned to 16 bytes
|
||||||
mov_rcx,r12 # arg1 = fin
|
mov_rcx,r12 # arg1 = fin
|
||||||
push !1 # size = 1
|
push !1 # size = 1
|
||||||
mov_rdx,rsp # arg2 = &size
|
mov_rdx,rsp # arg2 = &size
|
||||||
|
@ -307,6 +338,7 @@ DEFINE xor_r9,r9 4D31C9
|
||||||
pop_rax # deallocate stack
|
pop_rax # deallocate stack
|
||||||
pop_rax # save input to rax
|
pop_rax # save input to rax
|
||||||
pop_rsi # save size to rsi
|
pop_rsi # save size to rsi
|
||||||
|
mov_rsp,[rsp+BYTE] !16 # deallocate stack
|
||||||
|
|
||||||
# If the file ended (0 bytes read) terminate
|
# If the file ended (0 bytes read) terminate
|
||||||
test_esi,esi # if size = 0
|
test_esi,esi # if size = 0
|
||||||
|
@ -315,7 +347,7 @@ DEFINE xor_r9,r9 4D31C9
|
||||||
ret # return
|
ret # return
|
||||||
|
|
||||||
:terminate
|
:terminate
|
||||||
pop_rax # remove last return address from stack
|
mov_rsp,r15 # restore stack location for cleanup
|
||||||
pop_rsi # restore rootdir
|
pop_rsi # restore rootdir
|
||||||
pop_r15 # restore image_handle
|
pop_r15 # restore image_handle
|
||||||
pop_r14 # restore system->boot
|
pop_r14 # restore system->boot
|
||||||
|
@ -342,7 +374,17 @@ DEFINE xor_r9,r9 4D31C9
|
||||||
xor_r9,r9 # arg4 = NULL
|
xor_r9,r9 # arg4 = NULL
|
||||||
call_[r14+DWORD] %288 # system->boot->close_protocol(image_handle, &guid, image_handle, 0)
|
call_[r14+DWORD] %288 # system->boot->close_protocol(image_handle, &guid, image_handle, 0)
|
||||||
|
|
||||||
|
# Restore non-volatile registers
|
||||||
|
pop_r15
|
||||||
|
pop_r14
|
||||||
|
pop_r13
|
||||||
|
pop_r12
|
||||||
|
pop_rsi
|
||||||
|
pop_rdi
|
||||||
|
pop_rbx
|
||||||
|
|
||||||
mov_rsp,rbp # restore stack
|
mov_rsp,rbp # restore stack
|
||||||
|
pop_rbp
|
||||||
ret # return to UEFI
|
ret # return to UEFI
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
# First four arguments are passed via registers rcx, rdx, r8, r9 (if they fit in 64-bits)
|
# First four arguments are passed via registers rcx, rdx, r8, r9 (if they fit in 64-bits)
|
||||||
# but we need to leave stack space
|
# but we need to leave stack space
|
||||||
# rax, rcx, rdx, r8, r9, r10 and r11 are volatile and can be changed by called function
|
# rax, rcx, rdx, r8, r9, r10 and r11 are volatile and can be changed by called function
|
||||||
|
# Stack must be 16-byte aligned before UEFI calls.
|
||||||
|
|
||||||
.intel_syntax noprefix
|
.intel_syntax noprefix
|
||||||
.global _start
|
.global _start
|
||||||
|
@ -17,7 +18,17 @@
|
||||||
|
|
||||||
# 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
|
# Save non-volatile registers
|
||||||
|
push rbp
|
||||||
|
mov rbp, rsp
|
||||||
|
push rbx
|
||||||
|
push rdi
|
||||||
|
push rsi
|
||||||
|
push r12
|
||||||
|
push r13
|
||||||
|
push r14
|
||||||
|
push r15
|
||||||
|
|
||||||
mov r15, rcx # save image_handle
|
mov r15, rcx # save image_handle
|
||||||
mov r14, [rdx+96] # system->boot
|
mov r14, [rdx+96] # system->boot
|
||||||
|
|
||||||
|
@ -77,6 +88,7 @@ loop_options_done:
|
||||||
|
|
||||||
# Open file for reading
|
# Open file for reading
|
||||||
pop r8 # arg3 = in
|
pop r8 # arg3 = in
|
||||||
|
and rsp, -16 # align stack to 16 bytes
|
||||||
push rdx # allocate stack for fin
|
push rdx # allocate stack for fin
|
||||||
mov rdx, rsp # arg2 = &fin
|
mov rdx, rsp # arg2 = &fin
|
||||||
push 1 # arg5 = EFI_FILE_READ_ONLY
|
push 1 # arg5 = EFI_FILE_READ_ONLY
|
||||||
|
@ -107,6 +119,7 @@ loop_options_done:
|
||||||
push r14 # save system->boot
|
push r14 # save system->boot
|
||||||
push r15 # save image_handle
|
push r15 # save image_handle
|
||||||
push rsi # save rootdir
|
push rsi # save rootdir
|
||||||
|
mov r15, rsp # save stack location for cleanup
|
||||||
|
|
||||||
# Our flag for byte processing
|
# Our flag for byte processing
|
||||||
push -1
|
push -1
|
||||||
|
@ -212,6 +225,10 @@ ascii_high:
|
||||||
|
|
||||||
# Writes byte stored in al
|
# Writes byte stored in al
|
||||||
write_byte:
|
write_byte:
|
||||||
|
push rsp # align stack to 16 bytes
|
||||||
|
push [rsp] # align stack to 16 bytes
|
||||||
|
and rsp, -16 # align stack to 16 bytes
|
||||||
|
push rax # make sure next call is aligned to 16 bytes
|
||||||
mov rcx, r13 # arg1 = fout
|
mov rcx, r13 # arg1 = fout
|
||||||
push 1 # size = 1
|
push 1 # size = 1
|
||||||
mov rdx, rsp # arg2 = &size
|
mov rdx, rsp # arg2 = &size
|
||||||
|
@ -221,11 +238,15 @@ write_byte:
|
||||||
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+40] # fout->write()
|
call [rcx+40] # fout->write()
|
||||||
add rsp, 40 # deallocate stack
|
mov rsp, [rsp+56] # deallocate stack
|
||||||
|
|
||||||
ret # return
|
ret # return
|
||||||
|
|
||||||
read_byte:
|
read_byte:
|
||||||
|
push rsp # align stack to 16 bytes
|
||||||
|
push [rsp] # align stack to 16 bytes
|
||||||
|
and rsp, -16 # align stack to 16 bytes
|
||||||
|
push rax # make sure next call is aligned to 16 bytes
|
||||||
mov rcx, r12 # arg1 = fin
|
mov rcx, r12 # arg1 = fin
|
||||||
push 1 # size = 1
|
push 1 # size = 1
|
||||||
mov rdx, rsp # arg2 = &size
|
mov rdx, rsp # arg2 = &size
|
||||||
|
@ -241,6 +262,7 @@ read_byte:
|
||||||
pop rax # deallocate stack
|
pop rax # deallocate stack
|
||||||
pop rax # save input to rax
|
pop rax # save input to rax
|
||||||
pop rsi # save size to rsi
|
pop rsi # save size to rsi
|
||||||
|
mov rsp, [rsp+16] # deallocate stack
|
||||||
|
|
||||||
# If the file ended (0 bytes read) terminate
|
# If the file ended (0 bytes read) terminate
|
||||||
test esi, esi # if size == 0
|
test esi, esi # if size == 0
|
||||||
|
@ -249,7 +271,7 @@ read_byte:
|
||||||
ret # return
|
ret # return
|
||||||
|
|
||||||
terminate:
|
terminate:
|
||||||
pop rax # remove last return address from stack
|
mov rsp, r15 # restore stack location for cleanup
|
||||||
pop rsi # restore rootdir
|
pop rsi # restore rootdir
|
||||||
pop r15 # restore image_handle
|
pop r15 # restore image_handle
|
||||||
pop r14 # restore system->boot
|
pop r14 # restore system->boot
|
||||||
|
@ -276,8 +298,18 @@ terminate:
|
||||||
xor r9, r9 # arg4 = NULL
|
xor r9, r9 # arg4 = NULL
|
||||||
call [r14+288] # system->boot->close_protocol(image_handle, &guid, image_handle, 0)
|
call [r14+288] # system->boot->close_protocol(image_handle, &guid, image_handle, 0)
|
||||||
|
|
||||||
|
# Restore non-volatile registers
|
||||||
|
pop r15
|
||||||
|
pop r14
|
||||||
|
pop r13
|
||||||
|
pop r12
|
||||||
|
pop rsi
|
||||||
|
pop rdi
|
||||||
|
pop rbx
|
||||||
|
|
||||||
abort: # used for debugging only
|
abort: # used for debugging only
|
||||||
mov rsp, rbp # restore stack
|
mov rsp, rbp # restore stack
|
||||||
|
pop rbp
|
||||||
ret # return to UEFI
|
ret # return to UEFI
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -10,10 +10,21 @@
|
||||||
# First four arguments are passed via registers rcx, rdx, r8, r9 (if they fit in 64-bits)
|
# First four arguments are passed via registers rcx, rdx, r8, r9 (if they fit in 64-bits)
|
||||||
# but we need to leave stack space
|
# but we need to leave stack space
|
||||||
# rax, rcx, rdx, r8, r9, r10 and r11 are volatile and can be changed by called function
|
# rax, rcx, rdx, r8, r9, r10 and r11 are volatile and can be changed by called function
|
||||||
|
# Stack must be 16-byte aligned before UEFI calls.
|
||||||
|
|
||||||
# efi_main(void *image_handle, struct efi_system_table *system)
|
# efi_main(void *image_handle, struct efi_system_table *system)
|
||||||
:_start
|
:_start
|
||||||
4889E5 ; mov_rbp,rsp # save stack pointer
|
# Save non-volatile registers
|
||||||
|
55 ; push_rbp
|
||||||
|
4889E5 ; mov_rbp,rsp
|
||||||
|
53 ; push_rbx
|
||||||
|
57 ; push_rdi
|
||||||
|
56 ; push_rsi
|
||||||
|
4154 ; push_r12
|
||||||
|
4155 ; push_r13
|
||||||
|
4156 ; push_r14
|
||||||
|
4157 ; push_r15
|
||||||
|
|
||||||
4989CF ; mov_r15,rcx # save image_handle
|
4989CF ; mov_r15,rcx # save image_handle
|
||||||
4C8B72 60 ; mov_r14,[rdx+BYTE] !96 # system->boot
|
4C8B72 60 ; mov_r14,[rdx+BYTE] !96 # system->boot
|
||||||
|
|
||||||
|
@ -73,6 +84,7 @@
|
||||||
|
|
||||||
# Open file for reading
|
# Open file for reading
|
||||||
4158 ; pop_r8 # arg3 = in
|
4158 ; pop_r8 # arg3 = in
|
||||||
|
4883E4 F0 ; and_rsp, !-16 # align stack to 16 bytes
|
||||||
52 ; push_rdx # allocate stack for fin
|
52 ; push_rdx # allocate stack for fin
|
||||||
4889E2 ; mov_rdx,rsp # arg2 = &fin
|
4889E2 ; mov_rdx,rsp # arg2 = &fin
|
||||||
6A 01 ; push !1 # arg5 = EFI_FILE_READ_ONLY
|
6A 01 ; push !1 # arg5 = EFI_FILE_READ_ONLY
|
||||||
|
@ -103,6 +115,7 @@
|
||||||
4156 ; push_r14 # save system->boot
|
4156 ; push_r14 # save system->boot
|
||||||
4157 ; push_r15 # save image_handle
|
4157 ; push_r15 # save image_handle
|
||||||
56 ; push_rsi # save rootdir
|
56 ; push_rsi # save rootdir
|
||||||
|
4989E7 ; mov_r15,rsp # save stack location for cleanup
|
||||||
|
|
||||||
# Our flag for byte processing
|
# Our flag for byte processing
|
||||||
6A FF ; push !-1
|
6A FF ; push !-1
|
||||||
|
@ -208,6 +221,10 @@
|
||||||
|
|
||||||
# Writes byte stored in al
|
# Writes byte stored in al
|
||||||
:write_byte
|
:write_byte
|
||||||
|
54 ; push_rsp # align stack to 16 bytes
|
||||||
|
FF3424 ; push_[rsp] # align stack to 16 bytes
|
||||||
|
4883E4 F0 ; and_rsp, !-16 # align stack to 16 bytes
|
||||||
|
50 ; push_rax # make sure next call is aligned to 16 bytes
|
||||||
4C89E9 ; mov_rcx,r13 # arg1 = fout
|
4C89E9 ; mov_rcx,r13 # arg1 = fout
|
||||||
6A 01 ; push !1 # size = 1
|
6A 01 ; push !1 # size = 1
|
||||||
4889E2 ; mov_rdx,rsp # arg2 = &size
|
4889E2 ; mov_rdx,rsp # arg2 = &size
|
||||||
|
@ -217,11 +234,15 @@
|
||||||
50 ; push_rax # allocate shadow stack space for UEFI function
|
50 ; push_rax # allocate shadow stack space for UEFI function
|
||||||
50 ; push_rax # allocate shadow stack space for UEFI function
|
50 ; push_rax # allocate shadow stack space for UEFI function
|
||||||
FF51 28 ; call_[rcx+BYTE] !40 # fout->write()
|
FF51 28 ; call_[rcx+BYTE] !40 # fout->write()
|
||||||
4883C4 28 ; add_rsp, !40 # deallocate stack
|
488B6424 38 ; mov_rsp,[rsp+BYTE] !56 # deallocate stack
|
||||||
|
|
||||||
C3 ; ret # return
|
C3 ; ret # return
|
||||||
|
|
||||||
:read_byte
|
:read_byte
|
||||||
|
54 ; push_rsp # align stack to 16 bytes
|
||||||
|
FF3424 ; push_[rsp] # align stack to 16 bytes
|
||||||
|
4883E4 F0 ; and_rsp, !-16 # align stack to 16 bytes
|
||||||
|
50 ; push_rax # make sure next call is aligned to 16 bytes
|
||||||
4C89E1 ; mov_rcx,r12 # arg1 = fin
|
4C89E1 ; mov_rcx,r12 # arg1 = fin
|
||||||
6A 01 ; push !1 # size = 1
|
6A 01 ; push !1 # size = 1
|
||||||
4889E2 ; mov_rdx,rsp # arg2 = &size
|
4889E2 ; mov_rdx,rsp # arg2 = &size
|
||||||
|
@ -236,6 +257,7 @@
|
||||||
58 ; pop_rax # deallocate stack
|
58 ; pop_rax # deallocate stack
|
||||||
58 ; pop_rax # save input to rax
|
58 ; pop_rax # save input to rax
|
||||||
5E ; pop_rsi # save size to rsi
|
5E ; pop_rsi # save size to rsi
|
||||||
|
488B6424 10 ; mov_rsp,[rsp+BYTE] !16 # deallocate stack
|
||||||
|
|
||||||
# If the file ended (0 bytes read) terminate
|
# If the file ended (0 bytes read) terminate
|
||||||
85F6 ; test_esi,esi # if size = 0
|
85F6 ; test_esi,esi # if size = 0
|
||||||
|
@ -244,7 +266,7 @@
|
||||||
C3 ; ret # return
|
C3 ; ret # return
|
||||||
|
|
||||||
:terminate
|
:terminate
|
||||||
58 ; pop_rax # remove last return address from stack
|
4C89FC ; mov_rsp,r15 # restore stack location for cleanup
|
||||||
5E ; pop_rsi # restore rootdir
|
5E ; pop_rsi # restore rootdir
|
||||||
415F ; pop_r15 # restore image_handle
|
415F ; pop_r15 # restore image_handle
|
||||||
415E ; pop_r14 # restore system->boot
|
415E ; pop_r14 # restore system->boot
|
||||||
|
@ -271,7 +293,17 @@
|
||||||
4D31C9 ; xor_r9,r9 # arg4 = NULL
|
4D31C9 ; xor_r9,r9 # arg4 = NULL
|
||||||
41FF96 20010000 ; call_[r14+DWORD] %288 # system->boot->close_protocol(image_handle, &guid, image_handle, 0)
|
41FF96 20010000 ; call_[r14+DWORD] %288 # system->boot->close_protocol(image_handle, &guid, image_handle, 0)
|
||||||
|
|
||||||
|
# Restore non-volatile registers
|
||||||
|
415F ; pop_r15
|
||||||
|
415E ; pop_r14
|
||||||
|
415D ; pop_r13
|
||||||
|
415C ; pop_r12
|
||||||
|
5E ; pop_rsi
|
||||||
|
5F ; pop_rdi
|
||||||
|
5B ; pop_rbx
|
||||||
|
|
||||||
4889EC ; mov_rsp,rbp # restore stack
|
4889EC ; mov_rsp,rbp # restore stack
|
||||||
|
5D ; pop_rbp
|
||||||
C3 ; ret # return to UEFI
|
C3 ; ret # return to UEFI
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
# First four arguments are passed via registers rcx, rdx, r8, r9 (if they fit in 64-bits)
|
# First four arguments are passed via registers rcx, rdx, r8, r9 (if they fit in 64-bits)
|
||||||
# but we need to leave stack space
|
# but we need to leave stack space
|
||||||
# rax, rcx, rdx, r8, r9, r10 and r11 are volatile and can be changed by called function
|
# rax, rcx, rdx, r8, r9, r10 and r11 are volatile and can be changed by called function
|
||||||
|
# Stack must be 16-byte aligned before UEFI calls.
|
||||||
|
|
||||||
# DOS MZ header
|
# DOS MZ header
|
||||||
4D 5A # Signature
|
4D 5A # Signature
|
||||||
|
@ -128,9 +129,9 @@ F0 00 # SizeOfOptionalHeader
|
||||||
# [0x148]
|
# [0x148]
|
||||||
# Start of section headers
|
# Start of section headers
|
||||||
00 00 00 00 00 00 00 00 ; Name of the section (empty) but could set to ".text"
|
00 00 00 00 00 00 00 00 ; Name of the section (empty) but could set to ".text"
|
||||||
D0 01 00 00 ; VirtualSize
|
09 02 00 00 ; VirtualSize
|
||||||
00 10 00 00 ; VirtualAddress
|
00 10 00 00 ; VirtualAddress
|
||||||
D0 01 00 00 ; SizeOfRawData
|
09 02 00 00 ; SizeOfRawData
|
||||||
70 01 00 00 ; PointerToRawData
|
70 01 00 00 ; PointerToRawData
|
||||||
00 00 00 00 ; PointerToRelocations
|
00 00 00 00 ; PointerToRelocations
|
||||||
00 00 00 00 ; PointerToLinenumbers
|
00 00 00 00 ; PointerToLinenumbers
|
||||||
|
@ -142,13 +143,23 @@ D0 01 00 00 ; SizeOfRawData
|
||||||
# Our actual program
|
# Our actual program
|
||||||
# efi_main(void *image_handle, struct efi_system_table *system)
|
# efi_main(void *image_handle, struct efi_system_table *system)
|
||||||
# :_start
|
# :_start
|
||||||
4889E5 ; mov_rbp,rsp # save stack pointer
|
# Save non-volatile registers
|
||||||
|
55 ; push_rbp
|
||||||
|
4889E5 ; mov_rbp,rsp
|
||||||
|
53 ; push_rbx
|
||||||
|
57 ; push_rdi
|
||||||
|
56 ; push_rsi
|
||||||
|
4154 ; push_r12
|
||||||
|
4155 ; push_r13
|
||||||
|
4156 ; push_r14
|
||||||
|
4157 ; push_r15
|
||||||
|
|
||||||
4989CF ; mov_r15,rcx # save image_handle
|
4989CF ; mov_r15,rcx # save image_handle
|
||||||
4C8B72 60 ; mov_r14,[rdx+BYTE] !96 # system->boot
|
4C8B72 60 ; mov_r14,[rdx+BYTE] !96 # system->boot
|
||||||
|
|
||||||
# Open Loaded Image protocol
|
# Open Loaded Image protocol
|
||||||
4D89F9 ; mov_r9,r15 # arg4 = image_handle
|
4D89F9 ; mov_r9,r15 # arg4 = image_handle
|
||||||
488D15 9C010000 ; lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL
|
488D15 C9010000 ; lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL
|
||||||
4C89C9 ; mov_rcx,r9 # arg1 = image_handle
|
4C89C9 ; mov_rcx,r9 # arg1 = image_handle
|
||||||
50 ; push_rax # allocate stack for image
|
50 ; push_rax # allocate stack for image
|
||||||
4989E0 ; mov_r8,rsp # arg3 = &image
|
4989E0 ; mov_r8,rsp # arg3 = &image
|
||||||
|
@ -161,7 +172,7 @@ D0 01 00 00 ; SizeOfRawData
|
||||||
|
|
||||||
# Get root file system
|
# Get root file system
|
||||||
4D89F9 ; mov_r9,r15 # arg4 = image_handle
|
4D89F9 ; mov_r9,r15 # arg4 = image_handle
|
||||||
488D15 87010000 ; lea_rdx,[rip+DWORD] %SIMPLE_FS_PROTOCOL # guid = &SIMPLE_FS_PROTOCOL
|
488D15 B4010000 ; lea_rdx,[rip+DWORD] %SIMPLE_FS_PROTOCOL # guid = &SIMPLE_FS_PROTOCOL
|
||||||
488B4F 18 ; mov_rcx,[rdi+BYTE] !24 # arg1 = root_device = image->device
|
488B4F 18 ; mov_rcx,[rdi+BYTE] !24 # arg1 = root_device = image->device
|
||||||
4989CD ; mov_r13,rcx # save root_device
|
4989CD ; mov_r13,rcx # save root_device
|
||||||
50 ; push_rax # allocate stack for rootfs
|
50 ; push_rax # allocate stack for rootfs
|
||||||
|
@ -187,7 +198,7 @@ D0 01 00 00 ; SizeOfRawData
|
||||||
488B5F 38 ; mov_rbx,[rdi+BYTE] !56 # options = image->load_options
|
488B5F 38 ; mov_rbx,[rdi+BYTE] !56 # options = image->load_options
|
||||||
4889DA ; mov_rdx,rbx # save beginning of 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
|
48035F 30 ; add_rbx,[rdi+BYTE] !48 # go to the end of load_options
|
||||||
# :loop_options [_start + 0x6F]
|
# :loop_options [_start + 0x7B]
|
||||||
4839D3 ; cmp_rbx,rdx # Check if we are done
|
4839D3 ; cmp_rbx,rdx # Check if we are done
|
||||||
74 14 ; je !loop_options_done # We are done
|
74 14 ; je !loop_options_done # We are done
|
||||||
4883EB 02 ; sub_rbx, !2 # --options
|
4883EB 02 ; sub_rbx, !2 # --options
|
||||||
|
@ -198,10 +209,11 @@ D0 01 00 00 ; SizeOfRawData
|
||||||
4883C3 02 ; add_rbx, !2 # ++options
|
4883C3 02 ; add_rbx, !2 # ++options
|
||||||
53 ; push_rbx # push another argument onto stack
|
53 ; push_rbx # push another argument onto stack
|
||||||
EB E7 ; jmp !loop_options # next argument
|
EB E7 ; jmp !loop_options # next argument
|
||||||
# :loop_options_done [_start + 0x88]
|
# :loop_options_done [_start + 0x94]
|
||||||
|
|
||||||
# Open file for reading
|
# Open file for reading
|
||||||
4158 ; pop_r8 # arg3 = in
|
4158 ; pop_r8 # arg3 = in
|
||||||
|
4883E4 F0 ; and_rsp, !-16 # align stack to 16 bytes
|
||||||
52 ; push_rdx # allocate stack for fin
|
52 ; push_rdx # allocate stack for fin
|
||||||
4889E2 ; mov_rdx,rsp # arg2 = &fin
|
4889E2 ; mov_rdx,rsp # arg2 = &fin
|
||||||
6A 01 ; push !1 # arg5 = EFI_FILE_READ_ONLY
|
6A 01 ; push !1 # arg5 = EFI_FILE_READ_ONLY
|
||||||
|
@ -232,6 +244,7 @@ D0 01 00 00 ; SizeOfRawData
|
||||||
4156 ; push_r14 # save system->boot
|
4156 ; push_r14 # save system->boot
|
||||||
4157 ; push_r15 # save image_handle
|
4157 ; push_r15 # save image_handle
|
||||||
56 ; push_rsi # save rootdir
|
56 ; push_rsi # save rootdir
|
||||||
|
4989E7 ; mov_r15,rsp # save stack location for cleanup
|
||||||
|
|
||||||
# Our flag for byte processing
|
# Our flag for byte processing
|
||||||
6A FF ; push !-1
|
6A FF ; push !-1
|
||||||
|
@ -240,9 +253,9 @@ D0 01 00 00 ; SizeOfRawData
|
||||||
# temp storage for the sum
|
# temp storage for the sum
|
||||||
31FF ; xor_edi,edi # rdi = 0
|
31FF ; xor_edi,edi # rdi = 0
|
||||||
|
|
||||||
# :loop [_start+0xCF]
|
# :loop [_start+0xE2]
|
||||||
# Read a byte
|
# Read a byte
|
||||||
E8 6F000000 ; call %read_byte
|
E8 79000000 ; call %read_byte
|
||||||
|
|
||||||
# process byte
|
# process byte
|
||||||
E8 1C000000 ; call %hex
|
E8 1C000000 ; call %hex
|
||||||
|
@ -261,7 +274,7 @@ D0 01 00 00 ; SizeOfRawData
|
||||||
EB E8 ; jmp !loop
|
EB E8 ; jmp !loop
|
||||||
|
|
||||||
# process second byte of pair
|
# process second byte of pair
|
||||||
# :print [_start+0xE7]
|
# :print [_start+0xFA]
|
||||||
# update the sum and store in output
|
# update the sum and store in output
|
||||||
C1E7 04 ; shl_edi, !4
|
C1E7 04 ; shl_edi, !4
|
||||||
01F8 ; add_eax,edi
|
01F8 ; add_eax,edi
|
||||||
|
@ -273,7 +286,7 @@ D0 01 00 00 ; SizeOfRawData
|
||||||
|
|
||||||
EB DA ; jmp !loop
|
EB DA ; jmp !loop
|
||||||
|
|
||||||
# :hex [_start+0xF5]
|
# :hex [_start+0x108]
|
||||||
# Purge Comment Lines (#)
|
# Purge Comment Lines (#)
|
||||||
3C 23 ; cmp_al, !35
|
3C 23 ; cmp_al, !35
|
||||||
74 1E ; je !purge_comment
|
74 1E ; je !purge_comment
|
||||||
|
@ -309,9 +322,9 @@ D0 01 00 00 ; SizeOfRawData
|
||||||
# The rest that remains needs to be ignored
|
# The rest that remains needs to be ignored
|
||||||
EB 09 ; jmp !ascii_other
|
EB 09 ; jmp !ascii_other
|
||||||
|
|
||||||
# :purge_comment [_start+0x117]
|
# :purge_comment [_start+0x12A]
|
||||||
# Read a byte
|
# Read a byte
|
||||||
E8 27000000 ; call %read_byte
|
E8 31000000 ; call %read_byte
|
||||||
|
|
||||||
# Loop if not LF
|
# Loop if not LF
|
||||||
3C 0A ; cmp_al, !10
|
3C 0A ; cmp_al, !10
|
||||||
|
@ -319,24 +332,28 @@ D0 01 00 00 ; SizeOfRawData
|
||||||
|
|
||||||
# Otherwise return -1
|
# Otherwise return -1
|
||||||
|
|
||||||
# :ascii_other [_start+0x120]
|
# :ascii_other [_start+0x133]
|
||||||
6A FF ; push !-1
|
6A FF ; push !-1
|
||||||
58 ; pop_rax # return = -1
|
58 ; pop_rax # return = -1
|
||||||
C3 ; ret
|
C3 ; ret
|
||||||
|
|
||||||
# :ascii_num [_start+0x124]
|
# :ascii_num [_start+0x137]
|
||||||
2C 30 ; sub_al, !48
|
2C 30 ; sub_al, !48
|
||||||
C3 ; ret
|
C3 ; ret
|
||||||
|
|
||||||
# :ascii_low [_start+0x127]
|
# :ascii_low [_start+0x13A]
|
||||||
2C 20 ; sub_al, !32 # convert to uppercase
|
2C 20 ; sub_al, !32 # convert to uppercase
|
||||||
|
|
||||||
# :ascii_high [_start+0x129]
|
# :ascii_high [_start+0x13C]
|
||||||
2C 37 ; sub_al, !55
|
2C 37 ; sub_al, !55
|
||||||
C3 ; ret
|
C3 ; ret
|
||||||
|
|
||||||
# Writes byte stored in al
|
# Writes byte stored in al
|
||||||
# :write_byte [_start+0x12C]
|
# :write_byte [_start+0x13F]
|
||||||
|
54 ; push_rsp # align stack to 16 bytes
|
||||||
|
FF3424 ; push_[rsp] # align stack to 16 bytes
|
||||||
|
4883E4 F0 ; and_rsp, !-16 # align stack to 16 bytes
|
||||||
|
50 ; push_rax # make sure next call is aligned to 16 bytes
|
||||||
4C89E9 ; mov_rcx,r13 # arg1 = fout
|
4C89E9 ; mov_rcx,r13 # arg1 = fout
|
||||||
6A 01 ; push !1 # size = 1
|
6A 01 ; push !1 # size = 1
|
||||||
4889E2 ; mov_rdx,rsp # arg2 = &size
|
4889E2 ; mov_rdx,rsp # arg2 = &size
|
||||||
|
@ -346,11 +363,15 @@ D0 01 00 00 ; SizeOfRawData
|
||||||
50 ; push_rax # allocate shadow stack space for UEFI function
|
50 ; push_rax # allocate shadow stack space for UEFI function
|
||||||
50 ; push_rax # allocate shadow stack space for UEFI function
|
50 ; push_rax # allocate shadow stack space for UEFI function
|
||||||
FF51 28 ; call_[rcx+BYTE] !40 # fout->write()
|
FF51 28 ; call_[rcx+BYTE] !40 # fout->write()
|
||||||
4883C4 28 ; add_rsp, !40 # deallocate stack
|
488B6424 38 ; mov_rsp,[rsp+BYTE] !56 # deallocate stack
|
||||||
|
|
||||||
C3 ; ret # return
|
C3 ; ret # return
|
||||||
|
|
||||||
# :read_byte [_start+0x143]
|
# :read_byte [_start+0x160]
|
||||||
|
54 ; push_rsp # align stack to 16 bytes
|
||||||
|
FF3424 ; push_[rsp] # align stack to 16 bytes
|
||||||
|
4883E4 F0 ; and_rsp, !-16 # align stack to 16 bytes
|
||||||
|
50 ; push_rax # make sure next call is aligned to 16 bytes
|
||||||
4C89E1 ; mov_rcx,r12 # arg1 = fin
|
4C89E1 ; mov_rcx,r12 # arg1 = fin
|
||||||
6A 01 ; push !1 # size = 1
|
6A 01 ; push !1 # size = 1
|
||||||
4889E2 ; mov_rdx,rsp # arg2 = &size
|
4889E2 ; mov_rdx,rsp # arg2 = &size
|
||||||
|
@ -365,6 +386,7 @@ D0 01 00 00 ; SizeOfRawData
|
||||||
58 ; pop_rax # deallocate stack
|
58 ; pop_rax # deallocate stack
|
||||||
58 ; pop_rax # save input to rax
|
58 ; pop_rax # save input to rax
|
||||||
5E ; pop_rsi # save size to rsi
|
5E ; pop_rsi # save size to rsi
|
||||||
|
488B6424 10 ; mov_rsp,[rsp+BYTE] !16 # deallocate stack
|
||||||
|
|
||||||
# If the file ended (0 bytes read) terminate
|
# If the file ended (0 bytes read) terminate
|
||||||
85F6 ; test_esi,esi # if size = 0
|
85F6 ; test_esi,esi # if size = 0
|
||||||
|
@ -372,8 +394,8 @@ D0 01 00 00 ; SizeOfRawData
|
||||||
|
|
||||||
C3 ; ret # return
|
C3 ; ret # return
|
||||||
|
|
||||||
# :terminate [_start+0x160]
|
# :terminate [_start+0x18B]
|
||||||
58 ; pop_rax # remove last return address from stack
|
4C89FC ; mov_rsp,r15 # restore stack location for cleanup
|
||||||
5E ; pop_rsi # restore rootdir
|
5E ; pop_rsi # restore rootdir
|
||||||
415F ; pop_r15 # restore image_handle
|
415F ; pop_r15 # restore image_handle
|
||||||
415E ; pop_r14 # restore system->boot
|
415E ; pop_r14 # restore system->boot
|
||||||
|
@ -388,33 +410,43 @@ D0 01 00 00 ; SizeOfRawData
|
||||||
FF51 10 ; call_[rcx+BYTE] !16 # rootdir->close()
|
FF51 10 ; call_[rcx+BYTE] !16 # rootdir->close()
|
||||||
|
|
||||||
4D89F8 ; mov_r8,r15 # arg3 = image_handle
|
4D89F8 ; mov_r8,r15 # arg3 = image_handle
|
||||||
488D15 3C000000 ; lea_rdx,[rip+DWORD] %SIMPLE_FS_PROTOCOL # guid = &SIMPLE_FS_PROTOCOL
|
488D15 48000000 ; lea_rdx,[rip+DWORD] %SIMPLE_FS_PROTOCOL # guid = &SIMPLE_FS_PROTOCOL
|
||||||
4889D9 ; mov_rcx,rbx # arg1 = root_device
|
4889D9 ; mov_rcx,rbx # arg1 = root_device
|
||||||
4D31C9 ; xor_r9,r9 # arg4 = NULL
|
4D31C9 ; xor_r9,r9 # arg4 = NULL
|
||||||
4883EC 20 ; sub_rsp, !32 # allocate shadow stack space for UEFI function
|
4883EC 20 ; sub_rsp, !32 # allocate shadow stack space for UEFI function
|
||||||
41FF96 20010000 ; call_[r14+DWORD] %288 # system->boot->close_protocol(root_device, &guid, image_handle, 0)
|
41FF96 20010000 ; call_[r14+DWORD] %288 # system->boot->close_protocol(root_device, &guid, image_handle, 0)
|
||||||
|
|
||||||
4D89F8 ; mov_r8,r15 # arg3 = image_handle
|
4D89F8 ; mov_r8,r15 # arg3 = image_handle
|
||||||
488D15 11000000 ; lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL
|
488D15 1D000000 ; lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL
|
||||||
4C89C1 ; mov_rcx,r8 # arg1 = image_handle
|
4C89C1 ; mov_rcx,r8 # arg1 = image_handle
|
||||||
4D31C9 ; xor_r9,r9 # arg4 = NULL
|
4D31C9 ; xor_r9,r9 # arg4 = NULL
|
||||||
41FF96 20010000 ; call_[r14+DWORD] %288 # system->boot->close_protocol(image_handle, &guid, image_handle, 0)
|
41FF96 20010000 ; call_[r14+DWORD] %288 # system->boot->close_protocol(image_handle, &guid, image_handle, 0)
|
||||||
|
|
||||||
|
# Restore non-volatile registers
|
||||||
|
415F ; pop_r15
|
||||||
|
415E ; pop_r14
|
||||||
|
415D ; pop_r13
|
||||||
|
415C ; pop_r12
|
||||||
|
5E ; pop_rsi
|
||||||
|
5F ; pop_rdi
|
||||||
|
5B ; pop_rbx
|
||||||
|
|
||||||
4889EC ; mov_rsp,rbp # restore stack
|
4889EC ; mov_rsp,rbp # restore stack
|
||||||
|
5D ; pop_rbp
|
||||||
C3 ; ret # return to UEFI
|
C3 ; ret # return to UEFI
|
||||||
|
|
||||||
|
|
||||||
# Protocol GUIDs
|
# Protocol GUIDs
|
||||||
# :LOADED_IMAGE_PROTOCOL [_start+0x1B0]
|
# :LOADED_IMAGE_PROTOCOL [_start+0x1E9]
|
||||||
A1 31 1B 5B ; %0x5b1b31a1
|
A1 31 1B 5B ; %0x5b1b31a1
|
||||||
62 95 ; @0x9562
|
62 95 ; @0x9562
|
||||||
D2 11 ; @0x11d2
|
D2 11 ; @0x11d2
|
||||||
8E 3F 00 A0 C9 69 72 3B ; !0x8e !0x3f !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b
|
8E 3F 00 A0 C9 69 72 3B ; !0x8e !0x3f !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b
|
||||||
|
|
||||||
# :SIMPLE_FS_PROTOCOL [_start+0x1C0]
|
# :SIMPLE_FS_PROTOCOL [_start+0x1F9]
|
||||||
22 5B 4E 96 ; %0x964e5b22
|
22 5B 4E 96 ; %0x964e5b22
|
||||||
59 64 ; @0x6459
|
59 64 ; @0x6459
|
||||||
D2 11 ; @0x11d2
|
D2 11 ; @0x11d2
|
||||||
8E 39 00 A0 C9 69 72 3B ; !0x8e !0x39 !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b
|
8E 39 00 A0 C9 69 72 3B ; !0x8e !0x39 !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b
|
||||||
|
|
||||||
# :ELF_end [_start+0x1D0]
|
# :ELF_end [_start+0x209]
|
||||||
|
|
Loading…
Reference in New Issue