stage0-uefi/amd64/Development/catm.M1

286 lines
11 KiB
Plaintext

# SPDX-FileCopyrightText: 2022 Andrius Štikonas <andrius@stikonas.eu>
# SPDX-FileCopyrightText: 2019 Jeremiah Orians
#
# SPDX-License-Identifier: GPL-3.0-or-later
DEFINE add_rbx,[rdi+BYTE] 48035F
DEFINE add_rbx, 4883C3
DEFINE add_rsp, 4883C4
DEFINE call E8
DEFINE call_[rcx+BYTE] FF51
DEFINE call_[r14+BYTE] 41FF56
DEFINE call_[r14+QWORD] 41FF96
DEFINE cmp_al, 3C
DEFINE cmp_rax, 483D
DEFINE cmp_rbx,rdx 4839D3
DEFINE cmp_r8, 4983F8
DEFINE je 74
DEFINE jmp EB
DEFINE jne 75
DEFINE mov_rdx, 48C7C2
DEFINE mov_rax,rdi 4889C7
DEFINE mov_rbx,rax 4889C3
DEFINE mov_rcx,rbx 4889D9
DEFINE mov_rcx,r13 4C89E9
DEFINE mov_rcx,r15 4C89F9
DEFINE mov_rdx,rax 4889C2
DEFINE mov_rdx,rbx 4889DA
DEFINE mov_rdx,rsp 4889E2
DEFINE mov_rbp,rsp 4889E5
DEFINE mov_rsp,rbp 4889EC
DEFINE mov_r8,rbx 4989D8
DEFINE mov_r8,rsp 4989E0
DEFINE mov_r9,r15 4D89F9
DEFINE mov_r13,rsi 4989F5
DEFINE mov_r15,rcx 4989CF
DEFINE mov_al,[rbx] 8A03
DEFINE mov_rax,[rsp+BYTE] 488B4424
DEFINE mov_rbx,[rdi+BYTE] 488B5F
DEFINE mov_rcx,[rdi+BYTE] 488B4F
DEFINE mov_rcx,[rsp+BYTE] 488B4C24
DEFINE mov_r14,[rdx+BYTE] 4C8B72
DEFINE mov_rcx,[rip+QWORD] 488B0D
DEFINE mov_rdx,[rip+QWORD] 488B15
DEFINE mov_[rip+QWORD],rax 488905
DEFINE mov_[rbx], C603
DEFINE pop_rax 58
DEFINE pop_rcx 59
DEFINE pop_rsi 5E
DEFINE pop_r8 4158
DEFINE pop_r9 4159
DEFINE pop_r15 415F
DEFINE push 6A
DEFINE push_rax 50
DEFINE push_rbx 53
DEFINE push_rdx 52
DEFINE ret C3
DEFINE ror_r9 49D1C9
DEFINE sub_rbx, 4883EB
DEFINE sub_rsp, 4883EC
DEFINE test_eax,eax 85C0
DEFINE xor_eax,eax 31C0
# 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
push_rax # allocate stack for image
mov_r8,rsp # arg3 = &image
mov_rdx,[rip+QWORD] %LOADED_IMAGE_PROTOCOL_8 # EFI_LOADED_IMAGE_PROTOCOL_GUID (last 64 bits)
push_rdx # push last 64 bits onto stack
mov_rdx,[rip+QWORD] %LOADED_IMAGE_PROTOCOL # EFI_LOADED_IMAGE_PROTOCOL_GUID (first 64 bits)
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+QWORD] %280 # system->boot->open_protocol(image_handle, &guid, &image, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
mov_rax,[rsp+BYTE] !64 # get_image
mov_rax,rdi # save image
# Get root file system
push_rax # allocate stack for rootfs
mov_r8,rsp # arg3 = &rootfs
mov_rdx,[rip+QWORD] %SIMPLE_FS_PROTOCOL_8 # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID (last 64 bits)
push_rdx # push last 64 bits onto stack
mov_rdx,[rip+QWORD] %SIMPLE_FS_PROTOCOL # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID (first 64 bits)
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,[rdi+BYTE] !24 # arg1 = root_device = image->device
sub_rsp, !32 # allocate shadow stack space for UEFI function
call_[r14+QWORD] %280 # system->boot->open_protocol(root_device, &guid, &rootfs, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
mov_rcx,[rsp+BYTE] !64 # get rootfs
# Get root directory
push_rdx # 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_rax # get rootdir
mov_[rip+QWORD],rax %rootdir # save 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
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
pop_r8 # get output file
# Open file for writing
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,[rip+QWORD] %rootdir # arg1 = rootdir
sub_rsp, !32 # allocate shadow stack space for UEFI function
call_[rcx+BYTE] !8 # rootdir->open()
add_rsp, !40 # deallocate stack
pop_r15 # get fout
mov_rdx, %0x100000 # Allocate 1MB
call %allocate_pool # Get memory pool
mov_rbx,rax # Save it
:core
pop_r8 # Get the actual input name
cmp_r8, !0 # Check for null string
je !done # We are done if null string
call %open_file # Open file as read only
test_eax,eax # check if successfully opened
jne !core # Else go to another file
mov_r13,rsi # Protect fin
:keep
mov_rdx, %0x100000 # set the size of chars we want
call %read # read them
push_rax # Protect the number of bytes read
mov_rdx,rax # number of bytes to write
call %write # write them
pop_rax # Get bytes read
cmp_rax, %0x100000 # Check if buffer was fully used
je !keep # Keep looping if it was full
mov_rcx,r13 # fin
call %close_file # close file
jmp !core # Move to next file
:done
mov_rcx,r15 # Get output file
call %close_file # close it
mov_rcx,[rip+QWORD] %rootdir # Get rootdir
call %rootdir # close it
mov_rcx,rbx # Get buffer
call %free_pool # release it
xor_eax,eax # Exit code 0
mov_rsp,rbp # restore stack
ret # return to UEFI
# r8: input file name
# returns input file handle in rsi, status in rax
:open_file
# Open file for reading
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,[rip+QWORD] %rootdir # arg1 = rootdir
sub_rsp, !32 # allocate shadow stack space for UEFI function
call_[rcx+BYTE] !8 # rootdir->open()
add_rsp, !40 # deallocate stack
pop_rsi # get fin
ret
# rcx: file handle
:close_file
push_rax # allocate shadow stack space for UEFI function
call_[rcx+BYTE] !16 # file_handle->close(file_handle)
pop_rax # deallocate stack
ret
# rdx: number of bytes to allocate
# r14: system->boot
# returns pointer in rax
:allocate_pool
push_rdx # allocate stack for pool pointer
mov_r8,rsp # arg3 = &pool
push !2
pop_rcx # arg1 = EFI_LOADER_DATA
sub_rsp, !24 # allocate shadow stack space for UEFI
call_[r14+BYTE] !64 # system->boot->allocate_pool(EFI_LOADER_DATA, 2048, &pool)
add_rsp, !24 # deallocate stack
pop_rax # get pool
ret
# rcx: memory pool
# r14: system->boot
:free_pool
push_rax # allocate shadow stack space for UEFI function
call_[r14+BYTE] !72 # system->boot->free_pool(pool)
pop_rax # deallocate stack
ret
# rdx: number of bytes to read
# rbx: buffer
# r13: input file handle
# returns number of bytes read in rax
:read
mov_rcx,r13 # arg1 = fin
push_rdx # set size
mov_rdx,rsp # arg2 = &size
mov_r8,rbx # arg3 = buffer
sub_rsp, !24 # allocate shadow stack space for UEFI
call_[rcx+BYTE] !32 # fin->read()
add_rsp, !24 # deallocate stack
pop_rax # save size to rax
ret
# rdx: number of bytes to write
# rbx: buffer
# r14: output file handle
# returns number of bytes written in rax
:write
mov_rcx,r15 # arg1 = fout
push_rdx # set size
mov_rdx,rsp # arg2 = &size
mov_r8,rbx # arg3 = buffer
sub_rsp, !24 # allocate shadow stack space for UEFI
call_[rcx+BYTE] !40 # fin->write()
add_rsp, !24 # deallocate stack
pop_rax # save size to rax
ret
# Protocol GUIDs
:LOADED_IMAGE_PROTOCOL
%0x5b1b31a1
@0x9562
@0x11d2
:LOADED_IMAGE_PROTOCOL_8
!0x8e !0x3f !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b
:SIMPLE_FS_PROTOCOL
%0x0964e5b22
@0x6459
@0x11d2
:SIMPLE_FS_PROTOCOL_8
!0x8e !0x39 !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b
:rootdir
%0 %0
:PE32_end