stage0-uefi/amd64/Development/hex0.M1

364 lines
12 KiB
Plaintext

# SPDX-FileCopyrightText: 2022 Andrius Štikonas <andrius@stikonas.eu>
# SPDX-FileCopyrightText: 2017 Jeremiah Orians <jeremiah@pdp10.guru>
#
# SPDX-License-Identifier: GPL-3.0-or-later
# Usage: hex0 file.hex0 file
# Does not validate the arguments or check for success
# Calling convention:
# First four arguments are passed via registers rcx, rdx, r8, r9 (if they fit in 64-bits)
# but we need to leave stack space
# rax, rcx, rdx, r8, r9, r10 and r11 are volatile and can be changed by called function
DEFINE add_eax,edi 01F8
DEFINE add_rbx, 4883C3
DEFINE add_rsp, 4883C4
DEFINE add_rbx,[rdi+BYTE] 48035F
DEFINE call E8
DEFINE call_[rcx+BYTE] FF51
DEFINE call_[r14+BYTE] 41FF56
DEFINE call_[r14+DWORD] 41FF96
DEFINE cmp_al, 3C
DEFINE cmp_rbx,rdx 4839D3
DEFINE dec_ebx FFCB
DEFINE je 74
DEFINE jge 7D
DEFINE jl 7C
DEFINE jmp EB
DEFINE jne 75
DEFINE lea_rdx,[rip+DWORD] 488D15
DEFINE mov_edi,eax 89C7
DEFINE mov_rbp,rsp 4889E5
DEFINE mov_rcx,rbx 4889D9
DEFINE mov_rcx,rsi 4889F1
DEFINE mov_rcx,r8 4C89C1
DEFINE mov_rcx,r9 4C89C9
DEFINE mov_rcx,r12 4C89E1
DEFINE mov_rcx,r13 4C89E9
DEFINE mov_rdx,rbx 4889DA
DEFINE mov_rdx,rsp 4889E2
DEFINE mov_rsp,rbp 4889EC
DEFINE mov_r8,rsp 4989E0
DEFINE mov_r8,r15 4D89F8
DEFINE mov_r9,r15 4D89F9
DEFINE mov_r13,rcx 4989CD
DEFINE mov_r15,rcx 4989CF
DEFINE mov_al,[rbx] 8A03
DEFINE mov_[rbx], C603
DEFINE mov_rbx,[rdi+BYTE] 488B5F
DEFINE mov_rcx,[rdi+BYTE] 488B4F
DEFINE mov_r14,[rdx+BYTE] 4C8B72
DEFINE pop_rax 58
DEFINE pop_rbx 5B
DEFINE pop_rcx 59
DEFINE pop_rdi 5F
DEFINE pop_rsi 5E
DEFINE pop_r8 4158
DEFINE pop_r9 4159
DEFINE pop_r12 415C
DEFINE pop_r13 415D
DEFINE pop_r14 415E
DEFINE pop_r15 415F
DEFINE push 6A
DEFINE push_rax 50
DEFINE push_rbx 53
DEFINE push_rdx 52
DEFINE push_rsi 56
DEFINE push_r13 4155
DEFINE push_r14 4156
DEFINE push_r15 4157
DEFINE ret C3
DEFINE ror_r9 49D1C9
DEFINE shl_edi, C1E7
DEFINE sub_al, 2C
DEFINE sub_rbx, 4883EB
DEFINE sub_rsp, 4883EC
DEFINE test_eax,eax 85C0
DEFINE test_ebx,ebx 85DB
DEFINE test_esi,esi 85F6
DEFINE xor_ebx,ebx 31DB
DEFINE xor_edi,edi 31FF
DEFINE xor_esi,esi 31F6
DEFINE xor_r9,r9 4D31C9
# efi_main(void *image_handle, struct efi_system_table *system)
:_start
mov_rbp,rsp # save stack pointer
mov_r15,rcx # save image_handle
mov_r14,[rdx+BYTE] !96 # system->boot
# Open Loaded Image protocol
mov_r9,r15 # arg4 = image_handle
lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL
mov_rcx,r9 # arg1 = image_handle
push_rax # allocate stack for image
mov_r8,rsp # arg3 = &image
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+DWORD] %280 # system->boot->open_protocol(image_handle, &guid, &image, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
add_rsp, !48 # deallocate stack
pop_rdi # get image
# Get root file system
mov_r9,r15 # arg4 = image_handle
lea_rdx,[rip+DWORD] %SIMPLE_FS_PROTOCOL # guid = &SIMPLE_FS_PROTOCOL
mov_rcx,[rdi+BYTE] !24 # arg1 = root_device = image->device
mov_r13,rcx # save root_device
push_rax # allocate stack for rootfs
mov_r8,rsp # arg3 = &rootfs
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+DWORD] %280 # system->boot->open_protocol(root_device, &guid, &rootfs, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
add_rsp, !48 # deallocate stack
pop_rcx # get rootfs
# Get root directory
push_rax # allocate stack for rootdir
mov_rdx,rsp # 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_rsi # get 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
: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_[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
pop_r8 # arg3 = in
push_rdx # allocate stack for fin
mov_rdx,rsp # arg2 = &fin
push !1 # arg5 = EFI_FILE_READ_ONLY
push !1 # prepare to set arg4 to EFI_FILE_MODE_READ
pop_r9 # arg4 = EFI_FILE_MODE_READ
mov_rcx,rsi # arg1 = rootdir
sub_rsp, !32 # allocate shadow stack space for UEFI function
call_[rcx+BYTE] !8 # rootdir->open()
add_rsp, !40 # deallocate stack
pop_r12 # get fin
# Open file for writing
pop_r8 # arg3 = out
push_r13 # save root_device
push_rdx # allocate stack for fout
mov_rdx,rsp # arg2 = &fout
push !0 # arg5 = 0
push !7 # to get 0x8000000000000003 we set the rightmost 3 bits
pop_r9 # and then do right rotation by 1
ror_r9 # arg4 = EFI_FILE_MODE_CREATE| EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ
mov_rcx,rsi # arg1 = rootdir
sub_rsp, !32 # allocate shadow stack space for UEFI function
call_[rcx+BYTE] !8 # rootdir->open()
add_rsp, !40 # deallocate stack
pop_r13 # get fout
# Save variables that are needed for cleanup
push_r14 # save system->boot
push_r15 # save image_handle
push_rsi # save rootdir
# Our flag for byte processing
push !-1
pop_rbx # rbx = -1
# temp storage for the sum
xor_edi,edi # rdi = 0
:loop
# Read a byte
call %read_byte
# process byte
call %hex
# deal with -1 values
test_eax,eax
jl !loop
# deal with toggle
test_ebx,ebx # jump if ebx >= 0
jge !print
# process first byte of pair
mov_edi,eax
xor_ebx,ebx # rbx = 0
jmp !loop
# process second byte of pair
:print
# update the sum and store in output
shl_edi, !4
add_eax,edi
# flip the toggle
dec_ebx # rbx = -1
call %write_byte
jmp !loop
:hex
# Purge Comment Lines (#)
cmp_al, !35
je !purge_comment
# Purge Comment Lines (;)
cmp_al, !59
je !purge_comment
# deal all ascii less than '0'
cmp_al, !48
jl !ascii_other
# deal with 0-9
cmp_al, !58
jl !ascii_num
# deal with all ascii less than 'A'
cmp_al, !65
jl !ascii_other
# deal with 'A'-'F'
cmp_al, !71
jl !ascii_high
# deal with all ascii less than 'a'
cmp_al, !97
jl !ascii_other
#deal with 'a'-'f'
cmp_al, !103
jl !ascii_low
# The rest that remains needs to be ignored
jmp !ascii_other
:purge_comment
# Read a byte
call %read_byte
# Loop if not LF
cmp_al, !10
jne !purge_comment
# Otherwise return -1
:ascii_other
push !-1
pop_rax # return = -1
ret
:ascii_num
sub_al, !48
ret
:ascii_low
sub_al, !32 # convert to uppercase
:ascii_high
sub_al, !55
ret
# Writes byte stored in al
:write_byte
mov_rcx,r13 # arg1 = fout
push !1 # size = 1
mov_rdx,rsp # arg2 = &size
push_rax # allocate stack
mov_r8,rsp # arg3 = &output
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()
add_rsp, !40 # deallocate stack
ret # return
:read_byte
mov_rcx,r12 # arg1 = fin
push !1 # size = 1
mov_rdx,rsp # arg2 = &size
xor_esi,esi # zero rsi
push_rsi # allocate stack
mov_r8,rsp # arg3 = &input
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] !32 # fin->read()
pop_rax # deallocate stack
pop_rax # deallocate stack
pop_rax # deallocate stack
pop_rax # save input to rax
pop_rsi # save size to rsi
# If the file ended (0 bytes read) terminate
test_esi,esi # if size = 0
je !terminate # then we are done
ret # return
:terminate
pop_rax # remove last return address from stack
pop_rsi # restore rootdir
pop_r15 # restore image_handle
pop_r14 # restore system->boot
pop_rbx # restore root_device
push_rbx # allocate stack
mov_rcx,r12 # arg1 = fin
call_[rcx+BYTE] !16 # fin->close()
mov_rcx,r13 # arg1 = fout
call_[rcx+BYTE] !16 # fout->close()
mov_rcx,rsi # arg1 = rootdir
call_[rcx+BYTE] !16 # rootdir->close()
mov_r8,r15 # arg3 = image_handle
lea_rdx,[rip+DWORD] %SIMPLE_FS_PROTOCOL # guid = &SIMPLE_FS_PROTOCOL
mov_rcx,rbx # arg1 = root_device
xor_r9,r9 # arg4 = NULL
sub_rsp, !32 # allocate shadow stack space for UEFI function
call_[r14+DWORD] %288 # system->boot->close_protocol(root_device, &guid, image_handle, 0)
mov_r8,r15 # arg3 = image_handle
lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL
mov_rcx,r8 # arg1 = image_handle
xor_r9,r9 # arg4 = NULL
call_[r14+DWORD] %288 # system->boot->close_protocol(image_handle, &guid, image_handle, 0)
mov_rsp,rbp # restore stack
ret # return to UEFI
# Protocol GUIDs
:LOADED_IMAGE_PROTOCOL
%0x5b1b31a1
@0x9562
@0x11d2
!0x8e !0x3f !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b
:SIMPLE_FS_PROTOCOL
%0x0964e5b22
@0x6459
@0x11d2
!0x8e !0x39 !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b
:PE32_end