stage0-uefi/amd64/Development/hex1.M1

492 lines
16 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
DEFINE add_rbx, 4883C3
DEFINE add_rsp, 4883C4
DEFINE add_r13, 4981C5
DEFINE add_rax,rcx 4801C8
DEFINE add_rax,r14 4C01F0
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_rax, 483D
DEFINE cmp_r15, 4981FF
DEFINE cmp_rbx,rdx 4839D3
DEFINE je 0F84
DEFINE je8 74
DEFINE jl 0F8C
DEFINE jmp E9
DEFINE jmp8 EB
DEFINE jne 0F85
DEFINE jne8 75
DEFINE lea_rdx,[rip+DWORD] 488D15
DEFINE mov_dh, B6
DEFINE mov_rax, 48C7C0
DEFINE mov_rdx, 48C7C2
DEFINE mov_r13, 49C7C5
DEFINE mov_r14, 49C7C6
DEFINE mov_r15, 49C7C7
DEFINE mov_rbp,rsp 4889E5
DEFINE mov_rcx,rbx 4889D9
DEFINE mov_rcx,rdi 4889F9
DEFINE mov_rcx,rsi 4889F1
DEFINE mov_rcx,r8 4C89C1
DEFINE mov_rcx,r9 4C89C9
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_r14,rax 4989C6
DEFINE mov_r15,rcx 4989CF
DEFINE mov_al,[rbx] 8A03
DEFINE mov_rax,[rax] 488B00
DEFINE mov_[rbx], C603
DEFINE mov_rbx,[rdi+BYTE] 488B5F
DEFINE mov_rcx,[rdi+BYTE] 488B4F
DEFINE mov_r14,[rdx+BYTE] 4C8B72
DEFINE mov_[rax],r13 4C8928
DEFINE not_r15 49F7D7
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_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_rax, 48C1E0
DEFINE shl_r14, 49C1E6
DEFINE sub_rax, 4883E8
DEFINE sub_rbx, 4883EB
DEFINE sub_rsp, 4883EC
DEFINE sub_rax,r13 4C29E8
DEFINE test_esi,esi 85F6
DEFINE xor_edx,edx 31D2
DEFINE xor_esi,esi 31F6
DEFINE xor_r9,r9 4D31C9
# Some of the functions are deliberately inlined at the slight expense of
# binary size to avoid tricky jump calculations in hex0 code.
# 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
push !0 # Save end of arguments (NULL) onto stack
:loop_options
cmp_rbx,rdx # Check if we are done
je8 !loop_options_done # We are done
sub_rbx, !2 # --options
mov_al,[rbx] # *options
cmp_al, !0x20 # if *options != ' '
jne8 !loop_options # then continue looping
mov_[rbx], !0 # zero it
add_rbx, !2 # ++options
push_rbx # push another argument onto stack
jmp8 !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_rdi # get fin
# Open file for writing
pop_r8 # arg3 = out
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_rbx # get fout
# Save variables that are needed for cleanup
push_r13 # save root_device
push_r14 # save system->boot
push_r15 # save image_handle
push_rsi # save rootdir
# Allocate pool for single-character label table
# pointer to table will be stored at the top of the stack
push_rdx # allocate stack for table
mov_r8,rsp # arg3 = &table
xor_edx,edx # zero rdx
mov_dh, !0x8 # arg2 = 256 * 8 = 2048 = 0x800
push !2
pop_rcx # arg1 = EFI_LOADER_DATA
sub_rsp, !24 # allocate shadow stack space for UEFI function
call_[r14+BYTE] !64 # system->boot->allocate_pool(EFI_LOADER_DATA, 2048, &table)
add_rsp, !24 # deallocate stack
mov_r15, %-1 # Our flag for byte processing
mov_r14, %0 # temp storage for the sum
mov_r13, %0 # Our starting IP
call %First_pass # Process it
# rewind input file
mov_rcx,rdi # Using our input file
xor_edx,edx # Offset Zero
push_rax # allocate shadow stack space for UEFI function
push_rax # allocate shadow stack space for UEFI function
call_[rcx+BYTE] !56 # fin->set_position(fin, 0)
pop_rax # deallocate stack
pop_rax # deallocate stack
mov_r15, %-1 # Our flag for byte processing
mov_r14, %0 # temp storage for the sum
mov_r13, %0 # Our starting IP
call %Second_pass # Process it
jmp %Done
:First_pass
call %Read_byte
# Deal with EOF
cmp_rax, %-4
je %First_pass_done
# Check for :
cmp_rax, %0x3a
jne %First_pass_0
# Deal with label
call %StoreLabel
:First_pass_0
# Check for %
cmp_rax, %0x25
je %First_pass_pointer
# Deal with everything else
call %hex # Process our char
# Deal with EOF
cmp_rax, %-4
je %First_pass_done
# deal with -1 values
cmp_rax, %0
jl %First_pass
# deal with toggle
cmp_r15, %0
je %First_pass_1
add_r13, %1 # Increment IP
:First_pass_1
not_r15
jmp %First_pass
:First_pass_pointer
# Deal with Pointer to label
call %Read_byte # Drop the char
add_r13, %4 # Increment IP
jmp %First_pass # Loop again
:First_pass_done
ret
:hex
# deal with EOF
cmp_rax, %-4
je %EOF
# deal with line comments starting with #
cmp_rax, %0x23
je %ascii_comment
# deal with line comments starting with ;
cmp_rax, %0x3b
je %ascii_comment
# deal all ascii less than 0
cmp_rax, %0x30
jl %ascii_other
# deal with 0-9
cmp_rax, %0x3a
jl %ascii_num
# deal with all ascii less than A
cmp_rax, %0x41
jl %ascii_other
# deal with A-F
cmp_rax, %0x47
jl %ascii_high
# deal with all ascii less than a
cmp_rax, %0x61
jl %ascii_other
# deal with a-f
cmp_rax, %0x67
jl %ascii_low
# The rest that remains needs to be ignored
jmp %ascii_other
:Second_pass
call %Read_byte
# Deal with EOF
cmp_rax, %-4
je %Second_pass_done
# Simply drop the label
cmp_rax, %0x3a
jne %Second_pass_0
call %Read_byte
jmp %Second_pass
:Second_pass_0
# Deal with % pointer
cmp_rax, %0x25
jne %Second_pass_1
call %StorePointer
jmp %Second_pass
:Second_pass_1
# Deal with everything else
call %hex # Process our char
# Deal with EOF
cmp_rax, %-4
je %Second_pass_done
# deal with -1 values
cmp_rax, %0
jl %Second_pass
# deal with toggle
cmp_r15, %0
je %print
# process first byte of pair
mov_r14,rax
mov_r15, %0
jmp %Second_pass
:Second_pass_done
:EOF
ret
:ascii_num
sub_rax, !0x30
ret
:ascii_low
sub_rax, !0x57
ret
:ascii_high
sub_rax, !0x37
ret
:ascii_other
mov_rax, %-1
ret
:ascii_comment
call %Read_byte
cmp_rax, %0xd
je %ascii_comment_cr
cmp_rax, %0xa
jne %ascii_comment
:ascii_comment_cr
mov_rax, %-1
ret
# process second byte of pair
:print
# update the sum and store in output
shl_r14, !4
add_rax,r14
# flip the toggle
not_r15 # R15 = -1
mov_rdx, %1 # set the size of chars we want
call %print_chars
add_r13, %1 # Increment IP
jmp %Second_pass
:Read_byte
mov_rcx,rdi # 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) return EOF
test_esi,esi # if size = 0
jne8 !Read_byte_1
mov_rax, %-4 # Put EOF in rax
:Read_byte_1
ret # return
# Writes bytes stored in rax
:print_chars
mov_rcx,rbx # arg1 = fout
push_rdx # set size
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
:Get_table_target
call %Read_byte # Get single char label
shl_rax, !3 # Each label in table takes 8 bytes to store
mov_rcx,[rsp+BYTE] !24 # Get table
add_rax,rcx # Calculate offset
ret
:StoreLabel
call %Get_table_target
mov_[rax],r13 # Write out pointer to table
ret
:StorePointer
add_r13, %4 # Increment IP
call %Get_table_target # Get address of pointer
mov_rax,[rax] # Get pointer
sub_rax,r13 # target - ip
mov_rdx, %4 # set the size of chars we want
call %print_chars
ret
:Done
pop_rcx # restore table
pop_rsi # restore rootdir
pop_r15 # restore image_handle
pop_r14 # restore system->boot
pop_r13 # restore root_device
# Free pool
# arg1 = table
push_rax # allocate shadow stack space for UEFI function
call_[r14+BYTE] !72 # system->boot->free_pool(table)
mov_rcx,rdi # arg1 = fin
call_[rcx+BYTE] !16 # fin->close()
mov_rcx,rbx # 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,r13 # 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