catm: Switch to M1 defines that are closer to intel syntax.

This commit is contained in:
Andrius Štikonas 2022-08-22 01:30:22 +01:00
parent 01fae8cd06
commit 3528be5b2b
2 changed files with 342 additions and 343 deletions

View File

@ -3,266 +3,265 @@
#
# SPDX-License-Identifier: GPL-3.0-or-later
DEFINE ADD_RBX_from_Address_RDI_Immediate8 48035F
DEFINE ADDI8_RBX 4883C3
DEFINE ADDI8_RSP 4883C4
DEFINE ADDI32_to_RDX 48C7C2
DEFINE CALLI32 E8
DEFINE CALL_RCX_Immediate8 FF51
DEFINE CALL_R14_Immediate8 41FF56
DEFINE CALL_R14_Immediate32 41FF96
DEFINE CMPI8_AL 3C
DEFINE CMPI32_RAX 483D
DEFINE CMP_RBX_RDX 4839D3
DEFINE CMP_R8_Immediate8 4983F8
DEFINE COPY_RAX_to_RBX 4889C3
DEFINE COPY_RAX_to_RDX 4889C2
DEFINE COPY_RBX_to_RCX 4889D9
DEFINE COPY_RBX_to_RDX 4889DA
DEFINE COPY_RBP_to_RSP 4889EC
DEFINE COPY_RBX_to_R8 4989D8
DEFINE COPY_RCX_to_R15 4989CF
DEFINE COPY_RDI_to_RAX 4889C7
DEFINE COPY_RSI_to_R13 4989F5
DEFINE COPY_RSP_to_RBP 4889E5
DEFINE COPY_RSP_to_RDX 4889E2
DEFINE COPY_RSP_to_R8 4989E0
DEFINE COPY_R13_to_RCX 4C89E9
DEFINE COPY_R15_to_RCX 4C89F9
DEFINE COPY_R15_to_R9 4D89F9
DEFINE JE8 74
DEFINE JMP8 EB
DEFINE JNE8 75
DEFINE LOAD8_AL_from_Address_RBX 8A03
DEFINE LOAD64_into_RAX_from_Address_RSP_Immediate8 488B4424
DEFINE LOAD64_into_RBX_from_Address_RDI_Immediate8 488B5F
DEFINE LOAD64_into_RCX_from_Address_RDI_Immediate8 488B4F
DEFINE LOAD64_into_RCX_from_Address_RSP_Immediate8 488B4C24
DEFINE LOAD64_into_R14_from_Address_RDX_Immediate8 4C8B72
DEFINE LOAD64_rel_RCX 488B0D
DEFINE LOAD64_rel_RDX 488B15
DEFINE POP_RAX 58
DEFINE POP_RBX 5B
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 STOREI8_into_Address_RBX C603
DEFINE STORE64_rel_RAX 488905
DEFINE SUBI8_RBX 4883EB
DEFINE SUBI8_RSP 4883EC
DEFINE TEST_EAX_EAX 85C0
DEFINE XOR_EAX_EAX 31C0
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
COPY_RSP_to_RBP # save stack pointer
COPY_RCX_to_R15 # save image_handle
LOAD64_into_R14_from_Address_RDX_Immediate8 !96 # system->boot
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
COPY_RSP_to_R8 # arg3 = &image
LOAD64_rel_RDX %LOADED_IMAGE_PROTOCOL_8 # EFI_LOADED_IMAGE_PROTOCOL_GUID (last 64 bits)
PUSH_RDX # push last 64 bits onto stack
LOAD64_rel_RDX %LOADED_IMAGE_PROTOCOL # EFI_LOADED_IMAGE_PROTOCOL_GUID (first 64 bits)
PUSH_RDX # push first 64 bits onto stack
COPY_RSP_to_RDX # arg2 = &guid
PUSH !1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
PUSH !0 # arg5 = NULL
COPY_R15_to_R9 # arg4 = image_handle
COPY_R15_to_RCX # arg1 = image_handle
SUBI8_RSP !32 # allocate shadow stack space for UEFI function
CALL_R14_Immediate32 %280 # system->boot->open_protocol(image_handle, &guid, &image, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
LOAD64_into_RAX_from_Address_RSP_Immediate8 !64 # get_image
COPY_RDI_to_RAX # save image
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
COPY_RSP_to_R8 # arg3 = &rootfs
LOAD64_rel_RDX %SIMPLE_FS_PROTOCOL_8 # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID (last 64 bits)
PUSH_RDX # push last 64 bits onto stack
LOAD64_rel_RDX %SIMPLE_FS_PROTOCOL # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID (first 64 bits)
PUSH_RDX # push first 64 bits onto stack
COPY_RSP_to_RDX # arg2 = &guid
PUSH !1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
PUSH !0 # arg5 = NULL
COPY_R15_to_R9 # arg4 = image_handle
LOAD64_into_RCX_from_Address_RDI_Immediate8 !24 # arg1 = root_device = image->device
SUBI8_RSP !32 # allocate shadow stack space for UEFI function
CALL_R14_Immediate32 %280 # system->boot->open_protocol(root_device, &guid, &rootfs, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
LOAD64_into_RCX_from_Address_RSP_Immediate8 !64 # get rootfs
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
COPY_RSP_to_RDX # arg2 = &rootdir
PUSH_RAX # allocate shadow stack space for UEFI function
PUSH_RAX # allocate shadow stack space for UEFI function
CALL_RCX_Immediate8 !8 # rootfs->open_volume(rootfs, &rootdir)
POP_RAX # deallocate stack
POP_RAX # deallocate stack
POP_RAX # get rootdir
STORE64_rel_RAX %rootdir # save rootdir
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
LOAD64_into_RBX_from_Address_RDI_Immediate8 !56 # options = image->load_options
COPY_RBX_to_RDX # save beginning of load_options
ADD_RBX_from_Address_RDI_Immediate8 !48 # go to the end of load_options
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
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
SUBI8_RBX !2 # --options
LOAD8_AL_from_Address_RBX # *options
CMPI8_AL !0x20 # if *options != ' '
JNE8 !loop_options # then continue looping
STOREI8_into_Address_RBX !0 # zero it
ADDI8_RBX !2 # ++options
PUSH_RBX # push another argument onto stack
JMP8 !loop_options # next argument
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
pop_r8 # get output file
# Open file for writing
PUSH_RDX # allocate stack for fout
COPY_RSP_to_RDX # 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
LOAD64_rel_RCX %rootdir # arg1 = rootdir
SUBI8_RSP !32 # allocate shadow stack space for UEFI function
CALL_RCX_Immediate8 !8 # rootdir->open()
ADDI8_RSP !40 # deallocate stack
POP_R15 # get fout
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
ADDI32_to_RDX %0x100000 # Allocate 1MB
CALLI32 %allocate_pool # Get memory pool
COPY_RAX_to_RBX # Save it
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_Immediate8 !0 # Check for null string
JE8 !done # We are done if null string
pop_r8 # Get the actual input name
cmp_r8, !0 # Check for null string
je !done # We are done if null string
CALLI32 %open_file # Open file as read only
TEST_EAX_EAX # check if successfully opened
JNE8 !core # Else go to another file
COPY_RSI_to_R13 # Protect fin
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
ADDI32_to_RDX %0x100000 # set the size of chars we want
CALLI32 %read # read them
PUSH_RAX # Protect the number of bytes read
mov_rdx, %0x100000 # set the size of chars we want
call %read # read them
push_rax # Protect the number of bytes read
COPY_RAX_to_RDX # number of bytes to write
CALLI32 %write # write them
mov_rdx,rax # number of bytes to write
call %write # write them
POP_RAX # Get bytes read
CMPI32_RAX %0x100000 # Check if buffer was fully used
JE8 !keep # Keep looping if it was full
pop_rax # Get bytes read
cmp_rax, %0x100000 # Check if buffer was fully used
je !keep # Keep looping if it was full
COPY_R13_to_RCX # fin
CALLI32 %close_file # close file
JMP8 !core # Move to next file
mov_rcx,r13 # fin
call %close_file # close file
jmp !core # Move to next file
:done
COPY_R15_to_RCX # Get output file
CALLI32 %close_file # close it
mov_rcx,r15 # Get output file
call %close_file # close it
LOAD64_rel_RCX %rootdir # Get rootdir
CALLI32 %rootdir # close it
mov_rcx,[rip+QWORD] %rootdir # Get rootdir
call %rootdir # close it
COPY_RBX_to_RCX # Get buffer
CALLI32 %free_pool # release it
XOR_EAX_EAX # Exit code 0
mov_rcx,rbx # Get buffer
call %free_pool # release it
xor_eax,eax # Exit code 0
COPY_RBP_to_RSP # restore stack
RET # return to UEFI
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
COPY_RSP_to_RDX # 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
LOAD64_rel_RCX %rootdir # arg1 = rootdir
SUBI8_RSP !32 # allocate shadow stack space for UEFI function
CALL_RCX_Immediate8 !8 # rootdir->open()
ADDI8_RSP !40 # deallocate stack
POP_RSI # get fin
RET
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_Immediate8 !16 # file_handle->close(file_handle)
POP_RAX # deallocate stack
RET
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
COPY_RSP_to_R8 # arg3 = &pool
PUSH !2
POP_RCX # arg1 = EFI_LOADER_DATA
SUBI8_RSP !24 # allocate shadow stack space for UEFI
CALL_R14_Immediate8 !64 # system->boot->allocate_pool(EFI_LOADER_DATA, 2048, &pool)
ADDI8_RSP !24 # deallocate stack
POP_RAX # get pool
RET
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_Immediate8 !72 # system->boot->free_pool(pool)
POP_RAX # deallocate stack
RET
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
COPY_R13_to_RCX # arg1 = fin
PUSH_RDX # set size
COPY_RSP_to_RDX # arg2 = &size
COPY_RBX_to_R8 # arg3 = buffer
SUBI8_RSP !24 # allocate shadow stack space for UEFI
CALL_RCX_Immediate8 !32 # fin->read()
ADDI8_RSP !24 # deallocate stack
POP_RAX # save size to rax
RET
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
COPY_R15_to_RCX # arg1 = fout
PUSH_RDX # set size
COPY_RSP_to_RDX # arg2 = &size
COPY_RBX_to_R8 # arg3 = buffer
SUBI8_RSP !24 # allocate shadow stack space for UEFI
CALL_RCX_Immediate8 !40 # fin->write()
ADDI8_RSP !24 # deallocate stack
POP_RAX # save size to rax
RET
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

View File

@ -137,205 +137,205 @@ F0 00 # SizeOfOptionalHeader
# efi_main(void *image_handle, struct efi_system_table *system)
:_start
4889E5 ; COPY_RSP_to_RBP # save stack pointer
4989CF ; COPY_RCX_to_R15 # save image_handle
4C8B72 60 ; LOAD64_into_R14_from_Address_RDX_Immediate8 !96 # system->boot
4889E5 ; mov_rbp,rsp # save stack pointer
4989CF ; mov_r15,rcx # save image_handle
4C8B72 60 ; mov_r14,[rdx+BYTE] !96 # system->boot
# Open Loaded Image protocol
50 ; PUSH_RAX # allocate stack for image
4989E0 ; COPY_RSP_to_R8 # arg3 = &image
488B15 %LOADED_IMAGE_PROTOCOL_8 ; LOAD64_rel_RDX %LOADED_IMAGE_PROTOCOL_8 # EFI_LOADED_IMAGE_PROTOCOL_GUID (last 64 bits)
52 ; PUSH_RDX # push last 64 bits onto stack
488B15 %LOADED_IMAGE_PROTOCOL ; LOAD64_rel_RDX %LOADED_IMAGE_PROTOCOL # EFI_LOADED_IMAGE_PROTOCOL_GUID (first 64 bits)
52 ; PUSH_RDX # push first 64 bits onto stack
4889E2 ; COPY_RSP_to_RDX # arg2 = &guid
6A 01 ; PUSH !1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
6A 00 ; PUSH !0 # arg5 = NULL
4D89F9 ; COPY_R15_to_R9 # arg4 = image_handle
4C89F9 ; COPY_R15_to_RCX # arg1 = image_handle
4883EC 20 ; SUBI8_RSP !32 # allocate shadow stack space for UEFI function
41FF96 18010000 ; CALL_R14_Immediate32 %280 # system->boot->open_protocol(image_handle, &guid, &image, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
488B4424 40 ; LOAD64_into_RAX_from_Address_RSP_Immediate8 !64 # get_image
4889C7 ; COPY_RDI_to_RAX # save image
50 ; push_rax # allocate stack for image
4989E0 ; mov_r8,rsp # arg3 = &image
488B15 %LOADED_IMAGE_PROTOCOL_8 ; mov_rdx,[rip+QWORD] %LOADED_IMAGE_PROTOCOL_8 # EFI_LOADED_IMAGE_PROTOCOL_GUID (last 64 bits)
52 ; push_rdx # push last 64 bits onto stack
488B15 %LOADED_IMAGE_PROTOCOL ; mov_rdx,[rip+QWORD] %LOADED_IMAGE_PROTOCOL # EFI_LOADED_IMAGE_PROTOCOL_GUID (first 64 bits)
52 ; push_rdx # push first 64 bits onto stack
4889E2 ; mov_rdx,rsp # arg2 = &guid
6A 01 ; push !1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
6A 00 ; push !0 # arg5 = NULL
4D89F9 ; mov_r9,r15 # arg4 = image_handle
4C89F9 ; mov_rcx,r15 # arg1 = image_handle
4883EC 20 ; sub_rsp, !32 # allocate shadow stack space for UEFI function
41FF96 18010000 ; call_[r14+QWORD] %280 # system->boot->open_protocol(image_handle, &guid, &image, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
488B4424 40 ; mov_rax,[rsp+BYTE] !64 # get_image
4889C7 ; mov_rax,rdi # save image
# Get root file system
50 ; PUSH_RAX # allocate stack for rootfs
4989E0 ; COPY_RSP_to_R8 # arg3 = &rootfs
488B15 %SIMPLE_FS_PROTOCOL_8 ; LOAD64_rel_RDX %SIMPLE_FS_PROTOCOL_8 # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID (last 64 bits)
52 ; PUSH_RDX # push last 64 bits onto stack
488B15 %SIMPLE_FS_PROTOCOL ; LOAD64_rel_RDX %SIMPLE_FS_PROTOCOL # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID (first 64 bits)
52 ; PUSH_RDX # push first 64 bits onto stack
4889E2 ; COPY_RSP_to_RDX # arg2 = &guid
6A 01 ; PUSH !1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
6A 00 ; PUSH !0 # arg5 = NULL
4D89F9 ; COPY_R15_to_R9 # arg4 = image_handle
488B4F 18 ; LOAD64_into_RCX_from_Address_RDI_Immediate8 !24 # arg1 = root_device = image->device
4883EC 20 ; SUBI8_RSP !32 # allocate shadow stack space for UEFI function
41FF96 18010000 ; CALL_R14_Immediate32 %280 # system->boot->open_protocol(root_device, &guid, &rootfs, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
488B4C24 40 ; LOAD64_into_RCX_from_Address_RSP_Immediate8 !64 # get rootfs
50 ; push_rax # allocate stack for rootfs
4989E0 ; mov_r8,rsp # arg3 = &rootfs
488B15 %SIMPLE_FS_PROTOCOL_8 ; mov_rdx,[rip+QWORD] %SIMPLE_FS_PROTOCOL_8 # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID (last 64 bits)
52 ; push_rdx # push last 64 bits onto stack
488B15 %SIMPLE_FS_PROTOCOL ; mov_rdx,[rip+QWORD] %SIMPLE_FS_PROTOCOL # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID (first 64 bits)
52 ; push_rdx # push first 64 bits onto stack
4889E2 ; mov_rdx,rsp # arg2 = &guid
6A 01 ; push !1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
6A 00 ; push !0 # arg5 = NULL
4D89F9 ; mov_r9,r15 # arg4 = image_handle
488B4F 18 ; mov_rcx,[rdi+BYTE] !24 # arg1 = root_device = image->device
4883EC 20 ; sub_rsp, !32 # allocate shadow stack space for UEFI function
41FF96 18010000 ; call_[r14+QWORD] %280 # system->boot->open_protocol(root_device, &guid, &rootfs, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
488B4C24 40 ; mov_rcx,[rsp+BYTE] !64 # get rootfs
# Get root directory
52 ; PUSH_RDX # allocate stack for rootdir
4889E2 ; COPY_RSP_to_RDX # arg2 = &rootdir
50 ; PUSH_RAX # allocate shadow stack space for UEFI function
50 ; PUSH_RAX # allocate shadow stack space for UEFI function
FF51 08 ; CALL_RCX_Immediate8 !8 # rootfs->open_volume(rootfs, &rootdir)
58 ; POP_RAX # deallocate stack
58 ; POP_RAX # deallocate stack
58 ; POP_RAX # get rootdir
488905 %rootdir ; STORE64_rel_RAX %rootdir # save rootdir
52 ; push_rdx # allocate stack for rootdir
4889E2 ; mov_rdx,rsp # arg2 = &rootdir
50 ; push_rax # allocate shadow stack space for UEFI function
50 ; push_rax # allocate shadow stack space for UEFI function
FF51 08 ; call_[rcx+BYTE] !8 # rootfs->open_volume(rootfs, &rootdir)
58 ; pop_rax # deallocate stack
58 ; pop_rax # deallocate stack
58 ; pop_rax # get rootdir
488905 %rootdir ; mov_[rip+QWORD],rax %rootdir # save rootdir
# Push command line arguments onto stack
488B5F 38 ; LOAD64_into_RBX_from_Address_RDI_Immediate8 !56 # options = image->load_options
4889DA ; COPY_RBX_to_RDX # save beginning of load_options
48035F 30 ; ADD_RBX_from_Address_RDI_Immediate8 !48 # go to the end of load_options
488B5F 38 ; mov_rbx,[rdi+BYTE] !56 # options = image->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
6A 00 ; PUSH !0 # Save end of arguments (NULL) onto stack
6A 00 ; push !0 # Save end of arguments (NULL) onto stack
:loop_options
4839D3 ; CMP_RBX_RDX # Check if we are done
74 !loop_options_done ; JE8 !loop_options_done # We are done
4883EB 02 ; SUBI8_RBX !2 # --options
8A03 ; LOAD8_AL_from_Address_RBX # *options
3C 20 ; CMPI8_AL !0x20 # if *options != ' '
75 !loop_options ; JNE8 !loop_options # then continue looping
C603 00 ; STOREI8_into_Address_RBX !0 # zero it
4883C3 02 ; ADDI8_RBX !2 # ++options
53 ; PUSH_RBX # push another argument onto stack
EB !loop_options ; JMP8 !loop_options # next argument
4839D3 ; cmp_rbx,rdx # Check if we are done
74 !loop_options_done ; je !loop_options_done # We are done
4883EB 02 ; sub_rbx, !2 # --options
8A03 ; mov_al,[rbx] # *options
3C 20 ; cmp_al, !0x20 # if *options != ' '
75 !loop_options ; jne !loop_options # then continue looping
C603 00 ; mov_[rbx], !0 # zero it
4883C3 02 ; add_rbx, !2 # ++options
53 ; push_rbx # push another argument onto stack
EB !loop_options ; jmp !loop_options # next argument
:loop_options_done
4158 ; POP_R8 # get output file
4158 ; pop_r8 # get output file
# Open file for writing
52 ; PUSH_RDX # allocate stack for fout
4889E2 ; COPY_RSP_to_RDX # arg2 = &fout
6A 00 ; PUSH !0 # arg5 = 0
6A 07 ; PUSH !7 # to get 0x8000000000000003 we set the rightmost 3 bits
4159 ; POP_R9 # and then do right rotation by 1
49D1C9 ; ROR_R9 # arg4 = EFI_FILE_MODE_CREATE| EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ
488B0D %rootdir ; LOAD64_rel_RCX %rootdir # arg1 = rootdir
4883EC 20 ; SUBI8_RSP !32 # allocate shadow stack space for UEFI function
FF51 08 ; CALL_RCX_Immediate8 !8 # rootdir->open()
4883C4 28 ; ADDI8_RSP !40 # deallocate stack
415F ; POP_R15 # get fout
52 ; push_rdx # allocate stack for fout
4889E2 ; mov_rdx,rsp # arg2 = &fout
6A 00 ; push !0 # arg5 = 0
6A 07 ; push !7 # to get 0x8000000000000003 we set the rightmost 3 bits
4159 ; pop_r9 # and then do right rotation by 1
49D1C9 ; ror_r9 # arg4 = EFI_FILE_MODE_CREATE| EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ
488B0D %rootdir ; mov_rcx,[rip+QWORD] %rootdir # arg1 = rootdir
4883EC 20 ; sub_rsp, !32 # allocate shadow stack space for UEFI function
FF51 08 ; call_[rcx+BYTE] !8 # rootdir->open()
4883C4 28 ; add_rsp, !40 # deallocate stack
415F ; pop_r15 # get fout
48C7C2 00001000 ; ADDI32_to_RDX %0x100000 # Allocate 1MB
E8 %allocate_pool ; CALLI32 %allocate_pool # Get memory pool
4889C3 ; COPY_RAX_to_RBX # Save it
48C7C2 00001000 ; mov_rdx, %0x100000 # Allocate 1MB
E8 %allocate_pool ; call %allocate_pool # Get memory pool
4889C3 ; mov_rbx,rax # Save it
:core
4158 ; POP_R8 # Get the actual input name
4983F8 00 ; CMP_R8_Immediate8 !0 # Check for null string
74 !done ; JE8 !done # We are done if null string
4158 ; pop_r8 # Get the actual input name
4983F8 00 ; cmp_r8, !0 # Check for null string
74 !done ; je !done # We are done if null string
E8 %open_file ; CALLI32 %open_file # Open file as read only
85C0 ; TEST_EAX_EAX # check if successfully opened
75 !core ; JNE8 !core # Else go to another file
4989F5 ; COPY_RSI_to_R13 # Protect fin
E8 %open_file ; call %open_file # Open file as read only
85C0 ; test_eax,eax # check if successfully opened
75 !core ; jne !core # Else go to another file
4989F5 ; mov_r13,rsi # Protect fin
:keep
48C7C2 00001000 ; ADDI32_to_RDX %0x100000 # set the size of chars we want
E8 %read ; CALLI32 %read # read them
50 ; PUSH_RAX # Protect the number of bytes read
48C7C2 00001000 ; mov_rdx, %0x100000 # set the size of chars we want
E8 %read ; call %read # read them
50 ; push_rax # Protect the number of bytes read
4889C2 ; COPY_RAX_to_RDX # number of bytes to write
E8 %write ; CALLI32 %write # write them
4889C2 ; mov_rdx,rax # number of bytes to write
E8 %write ; call %write # write them
58 ; POP_RAX # Get bytes read
483D 00001000 ; CMPI32_RAX %0x100000 # Check if buffer was fully used
74 !keep ; JE8 !keep # Keep looping if it was full
58 ; pop_rax # Get bytes read
483D 00001000 ; cmp_rax, %0x100000 # Check if buffer was fully used
74 !keep ; je !keep # Keep looping if it was full
4C89E9 ; COPY_R13_to_RCX # fin
E8 %close_file ; CALLI32 %close_file # close file
EB !core ; JMP8 !core # Move to next file
4C89E9 ; mov_rcx,r13 # fin
E8 %close_file ; call %close_file # close file
EB !core ; jmp !core # Move to next file
:done
4C89F9 ; COPY_R15_to_RCX # Get output file
E8 %close_file ; CALLI32 %close_file # close it
4C89F9 ; mov_rcx,r15 # Get output file
E8 %close_file ; call %close_file # close it
488B0D %rootdir ; LOAD64_rel_RCX %rootdir # Get rootdir
E8 %close_file ; CALLI32 %rootdir # close it
488B0D %rootdir ; mov_rcx,[rip+QWORD] %rootdir # Get rootdir
E8 %close_file ; call %rootdir # close it
4889D9 ; COPY_RBX_to_RCX # Get buffer
E8 %free_pool ; CALLI32 %free_pool # release it
31C0 ; XOR_EAX_EAX # Exit code 0
4889D9 ; mov_rcx,rbx # Get buffer
E8 %free_pool ; call %free_pool # release it
31C0 ; xor_eax,eax # Exit code 0
4889EC ; COPY_RBP_to_RSP # restore stack
C3 ; RET # return to UEFI
4889EC ; mov_rsp,rbp # restore stack
C3 ; ret # return to UEFI
# r8: input file name
# returns input file handle in rsi, status in rax
:open_file
# Open file for reading
52 ; PUSH_RDX # allocate stack for fin
4889E2 ; COPY_RSP_to_RDX # arg2 = &fin
6A 01 ; PUSH !1 # arg5 = EFI_FILE_READ_ONLY
6A 01 ; PUSH !1 # prepare to set arg4 to EFI_FILE_MODE_READ
4159 ; POP_R9 # arg4 = EFI_FILE_MODE_READ
488B0D %rootdir ; LOAD64_rel_RCX %rootdir # arg1 = rootdir
4883EC 20 ; SUBI8_RSP !32 # allocate shadow stack space for UEFI function
FF51 08 ; CALL_RCX_Immediate8 !8 # rootdir->open()
4883C4 28 ; ADDI8_RSP !40 # deallocate stack
5E ; POP_RSI # get fin
C3 ; RET
52 ; push_rdx # allocate stack for fin
4889E2 ; mov_rdx,rsp # arg2 = &fin
6A 01 ; push !1 # arg5 = EFI_FILE_READ_ONLY
6A 01 ; push !1 # prepare to set arg4 to EFI_FILE_MODE_READ
4159 ; pop_r9 # arg4 = EFI_FILE_MODE_READ
488B0D %rootdir ; mov_rcx,[rip+QWORD] %rootdir # arg1 = rootdir
4883EC 20 ; sub_rsp, !32 # allocate shadow stack space for UEFI function
FF51 08 ; call_[rcx+BYTE] !8 # rootdir->open()
4883C4 28 ; add_rsp, !40 # deallocate stack
5E ; pop_rsi # get fin
C3 ; ret
# rcx: file handle
:close_file
50 ; PUSH_RAX # allocate shadow stack space for UEFI function
FF51 10 ; CALL_RCX_Immediate8 !16 # file_handle->close(file_handle)
58 ; POP_RAX # deallocate stack
C3 ; RET
50 ; push_rax # allocate shadow stack space for UEFI function
FF51 10 ; call_[rcx+BYTE] !16 # file_handle->close(file_handle)
58 ; pop_rax # deallocate stack
C3 ; ret
# rdx: number of bytes to allocate
# r14: system->boot
# returns pointer in rax
:allocate_pool
52 ; PUSH_RDX # allocate stack for pool pointer
4989E0 ; COPY_RSP_to_R8 # arg3 = &pool
6A 02 ; PUSH !2
59 ; POP_RCX # arg1 = EFI_LOADER_DATA
4883EC 18 ; SUBI8_RSP !24 # allocate shadow stack space for UEFI
41FF56 40 ; CALL_R14_Immediate8 !64 # system->boot->allocate_pool(EFI_LOADER_DATA, 2048, &pool)
4883C4 18 ; ADDI8_RSP !24 # deallocate stack
58 ; POP_RAX # get pool
C3 ; RET
52 ; push_rdx # allocate stack for pool pointer
4989E0 ; mov_r8,rsp # arg3 = &pool
6A 02 ; push !2
59 ; pop_rcx # arg1 = EFI_LOADER_DATA
4883EC 18 ; sub_rsp, !24 # allocate shadow stack space for UEFI
41FF56 40 ; call_[r14+BYTE] !64 # system->boot->allocate_pool(EFI_LOADER_DATA, 2048, &pool)
4883C4 18 ; add_rsp, !24 # deallocate stack
58 ; pop_rax # get pool
C3 ; ret
# rcx: memory pool
# r14: system->boot
:free_pool
50 ; PUSH_RAX # allocate shadow stack space for UEFI function
41FF56 48 ; CALL_R14_Immediate8 !72 # system->boot->free_pool(pool)
58 ; POP_RAX # deallocate stack
C3 ; RET
50 ; push_rax # allocate shadow stack space for UEFI function
41FF56 48 ; call_[r14+BYTE] !72 # system->boot->free_pool(pool)
58 ; pop_rax # deallocate stack
C3 ; ret
# rdx: number of bytes to read
# rbx: buffer
# r13: input file handle
# returns number of bytes read in rax
:read
4C89E9 ; COPY_R13_to_RCX # arg1 = fin
52 ; PUSH_RDX # set size
4889E2 ; COPY_RSP_to_RDX # arg2 = &size
4989D8 ; COPY_RBX_to_R8 # arg3 = buffer
4883EC 18 ; SUBI8_RSP !24 # allocate shadow stack space for UEFI
FF51 20 ; CALL_RCX_Immediate8 !32 # fin->read()
4883C4 18 ; ADDI8_RSP !24 # deallocate stack
58 ; POP_RAX # save size to rax
C3 ; RET
4C89E9 ; mov_rcx,r13 # arg1 = fin
52 ; push_rdx # set size
4889E2 ; mov_rdx,rsp # arg2 = &size
4989D8 ; mov_r8,rbx # arg3 = buffer
4883EC 18 ; sub_rsp, !24 # allocate shadow stack space for UEFI
FF51 20 ; call_[rcx+BYTE] !32 # fin->read()
4883C4 18 ; add_rsp, !24 # deallocate stack
58 ; pop_rax # save size to rax
C3 ; ret
# rdx: number of bytes to write
# rbx: buffer
# r14: output file handle
# returns number of bytes written in rax
:write
4C89F9 ; COPY_R15_to_RCX # arg1 = fout
52 ; PUSH_RDX # set size
4889E2 ; COPY_RSP_to_RDX # arg2 = &size
4989D8 ; COPY_RBX_to_R8 # arg3 = buffer
4883EC 18 ; SUBI8_RSP !24 # allocate shadow stack space for UEFI
FF51 28 ; CALL_RCX_Immediate8 !40 # fin->write()
4883C4 18 ; ADDI8_RSP !24 # deallocate stack
58 ; POP_RAX # save size to rax
C3 ; RET
4C89F9 ; mov_rcx,r15 # arg1 = fout
52 ; push_rdx # set size
4889E2 ; mov_rdx,rsp # arg2 = &size
4989D8 ; mov_r8,rbx # arg3 = buffer
4883EC 18 ; sub_rsp, !24 # allocate shadow stack space for UEFI
FF51 28 ; call_[rcx+BYTE] !40 # fin->write()
4883C4 18 ; add_rsp, !24 # deallocate stack
58 ; pop_rax # save size to rax
C3 ; ret
# Protocol GUIDs