hex0: Implement various improvements:

* Make sure to close all protocols before exit.
 * Use more sophisticated command line argument processing
   that pushes command line arguments onto stack.
 * Switch to more readable M1 defines.
This commit is contained in:
Andrius Štikonas 2022-09-09 22:20:59 +01:00
parent 9b9ff9ac52
commit 32deb0ffaf
10 changed files with 751 additions and 696 deletions

View File

@ -40,14 +40,14 @@ efi_status_t efi_main(efi_handle_t image_handle, struct efi_system_table *system
*options = 0; *options = 0;
out = ++options; out = ++options;
/* Open file for writing */
struct efi_file_protocol *fout;
rootdir->open(rootdir, &fout, out, EFI_FILE_MODE_CREATE| EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ, 0);
/* Open file for reading */ /* Open file for reading */
struct efi_file_protocol *fin; struct efi_file_protocol *fin;
rootdir->open(rootdir, &fin, in, EFI_FILE_MODE_READ, EFI_FILE_READ_ONLY); rootdir->open(rootdir, &fin, in, EFI_FILE_MODE_READ, EFI_FILE_READ_ONLY);
/* Open file for writing */
struct efi_file_protocol *fout;
rootdir->open(rootdir, &fout, out, EFI_FILE_MODE_CREATE| EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ, 0);
uint8_t c; uint8_t c;
uint64_t size; uint64_t size;
@ -115,6 +115,9 @@ not_comment:
terminate: terminate:
fin->close(fin); fin->close(fin);
fout->close(fout); fout->close(fout);
rootdir->close(rootdir);
system->boot->close_protocol(root_device, &guid2, image_handle, 0);
system->boot->close_protocol(image_handle, &guid1, image_handle, 0);
return 0; return 0;
} }

View File

@ -377,15 +377,14 @@ efi_status_t efi_main(efi_handle_t image_handle, struct efi_system_table *system
*options = 0; *options = 0;
out = ++options; out = ++options;
/* Open file for reading */
struct efi_file_protocol *input;
rootdir->open(rootdir, &input, in, EFI_FILE_MODE_READ, EFI_FILE_READ_ONLY);
/* Open file for writing */ /* Open file for writing */
struct efi_file_protocol *fout; struct efi_file_protocol *fout;
rootdir->open(rootdir, &fout, out, EFI_FILE_MODE_CREATE| EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ, 0); rootdir->open(rootdir, &fout, out, EFI_FILE_MODE_CREATE| EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ, 0);
/* Open file for reading */
struct efi_file_protocol *input;
rootdir->open(rootdir, &input, in, EFI_FILE_MODE_READ, EFI_FILE_READ_ONLY);
jump_table = NULL; jump_table = NULL;
Base_Address = 0x00600000; Base_Address = 0x00600000;
output = fout; output = fout;

View File

@ -11,322 +11,340 @@
# 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
# Registers: DEFINE add_eax,edi 01F8
# r12 in/fin: input file name, later reused for handle DEFINE add_rbx, 4883C3
# r13 out/fout: output file name, later reused for handle DEFINE add_rsp, 4883C4
# r14 system->boot->open_protocol from UEFI, later reused for rootdir DEFINE add_rbx,[rdi+BYTE] 48035F
# r15 image_handle from UEFI DEFINE call E8
DEFINE call_[rcx+BYTE] FF51
# hex0 algorithm mostly follows stage0-posix version but uses rdi instead of rbp DEFINE call_[r14+BYTE] 41FF56
DEFINE call_[r14+DWORD] 41FF96
DEFINE cmp_al, 3C
DEFINE ADD_EAX_EDI 01F8 DEFINE cmp_rbx,rdx 4839D3
DEFINE ADDI8_RBX 4883C3 DEFINE dec_ebx FFCB
DEFINE ADDI8_RSP 4883C4 DEFINE je 74
DEFINE CALLI32 E8 DEFINE jge 7D
DEFINE CALL_R14 41FFD6 DEFINE jl 7C
DEFINE CALL_RCX_Immediate8 FF51 DEFINE jmp EB
DEFINE CMPI8_AL 3C DEFINE jne 75
DEFINE COPY_EAX_to_EDI 89C7 DEFINE lea_rdx,[rip+DWORD] 488D15
DEFINE COPY_RBX_to_R12 4989DC DEFINE mov_edi,eax 89C7
DEFINE COPY_RBX_to_R13 4989DD DEFINE mov_rbp,rsp 4889E5
DEFINE COPY_RCX_to_RAX 4889C1 DEFINE mov_rcx,rbx 4889D9
DEFINE COPY_RSP_to_RBP 4889E5 DEFINE mov_rcx,rsi 4889F1
DEFINE COPY_RSP_to_RDX 4889E2 DEFINE mov_rcx,r8 4C89C1
DEFINE COPY_RSP_to_R8 4989E0 DEFINE mov_rcx,r9 4C89C9
DEFINE COPY_RBP_to_RSP 4889EC DEFINE mov_rcx,r12 4C89E1
DEFINE COPY_R8_to_RSP 4989E0 DEFINE mov_rcx,r13 4C89E9
DEFINE COPY_RCX_to_R9 4989C9 DEFINE mov_rdx,rbx 4889DA
DEFINE COPY_R12_to_RCX 4C89E1 DEFINE mov_rdx,rsp 4889E2
DEFINE COPY_R12_to_R8 4D89E0 DEFINE mov_rsp,rbp 4889EC
DEFINE COPY_R13_to_RCX 4C89E9 DEFINE mov_r8,rsp 4989E0
DEFINE COPY_R13_to_R8 4D89E8 DEFINE mov_r8,r15 4D89F8
DEFINE COPY_R14_to_RCX 4C89F1 DEFINE mov_r9,r15 4D89F9
DEFINE COPY_RCX_to_R15 4989CF DEFINE mov_r13,rcx 4989CD
DEFINE COPY_R15_to_R9 4D89F9 DEFINE mov_r15,rcx 4989CF
DEFINE DEC_EBX FFCB DEFINE mov_al,[rbx] 8A03
DEFINE JE8 74 DEFINE mov_[rbx], C603
DEFINE JGE8 7D DEFINE mov_rbx,[rdi+BYTE] 488B5F
DEFINE JL8 7C DEFINE mov_rcx,[rdi+BYTE] 488B4F
DEFINE JMP8 EB DEFINE mov_r14,[rdx+BYTE] 4C8B72
DEFINE JNE8 75 DEFINE pop_rax 58
DEFINE LOAD64_into_RBX_from_Address_RAX_Immediate8 488B58 DEFINE pop_rbx 5B
DEFINE LOAD64_into_RAX_from_Address_RSP_Immediate8 488B4424 DEFINE pop_rcx 59
DEFINE LOAD64_into_RCX_from_Address_RSP_Immediate8 488B4C24 DEFINE pop_rdi 5F
DEFINE LOAD64_into_RCX_from_Address_RCX_Immediate8 488B49 DEFINE pop_rsi 5E
DEFINE LOAD64_into_R12_from_Address_RSP_Immediate8 4C8B6424 DEFINE pop_r8 4158
DEFINE LOAD64_into_R13_from_Address_RSP_Immediate8 4C8B6C24 DEFINE pop_r9 4159
DEFINE LOAD64_into_R14_from_Address_RDX_Immediate8 4C8B72 DEFINE pop_r12 415C
DEFINE LOAD64_into_R14_from_Address_R14_Immediate32 4D8BB6 DEFINE pop_r13 415D
DEFINE LOAD8_AL_from_Address_RBX 8A03 DEFINE pop_r14 415E
DEFINE LOAD64_rel_RDX 488B15 DEFINE pop_r15 415F
DEFINE POP_RAX 58 DEFINE push 6A
DEFINE POP_RBX 5B DEFINE push_rax 50
DEFINE POP_RSI 5E DEFINE push_rbx 53
DEFINE POP_R9 4159 DEFINE push_rdx 52
DEFINE POP_R14 415E DEFINE push_rsi 56
DEFINE PUSH 6A DEFINE push_r13 4155
DEFINE PUSH_RAX 50 DEFINE push_r14 4156
DEFINE PUSH_RBX 53 DEFINE push_r15 4157
DEFINE PUSH_RDX 52 DEFINE ret C3
DEFINE PUSH_RSI 56 DEFINE ror_r9 49D1C9
DEFINE RET C3 DEFINE shl_edi, C1E7
DEFINE ROR_R9 49D1C9 DEFINE sub_al, 2C
DEFINE SHL_EDI_Immediate8 C1E7 DEFINE sub_rbx, 4883EB
DEFINE STOREI8_into_Address_RBX C603 DEFINE sub_rsp, 4883EC
DEFINE SUBI8_RSP 4883EC DEFINE test_eax,eax 85C0
DEFINE SUBI8_AL 2C DEFINE test_ebx,ebx 85DB
DEFINE XOR_EBX_EBX 31DB DEFINE test_esi,esi 85F6
DEFINE XOR_EDI_EDI 31FF DEFINE xor_ebx,ebx 31DB
DEFINE TEST_EAX_EAX 85C0 DEFINE xor_edi,edi 31FF
DEFINE TEST_EBX_EBX 85DB DEFINE xor_esi,esi 31F6
DEFINE TEST_ESI_ESI 85F6 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
COPY_RSP_to_RBP # save stack pointer mov_rbp,rsp # save stack pointer
COPY_RCX_to_R15 # save image_handle mov_r15,rcx # save image_handle
LOAD64_into_R14_from_Address_RDX_Immediate8 !96 # system->boot mov_r14,[rdx+BYTE] !96 # system->boot
LOAD64_into_R14_from_Address_R14_Immediate32 %280 # system->boot->open_protocol
# Open Loaded Image protocol # Open Loaded Image protocol
PUSH_RAX # allocate stack for image mov_r9,r15 # arg4 = image_handle
COPY_RSP_to_R8 # arg3 = &image lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL
LOAD64_rel_RDX %LOADED_IMAGE_PROTOCOL_8 # EFI_LOADED_IMAGE_PROTOCOL_GUID (last 64 bits) mov_rcx,r9 # arg1 = image_handle
PUSH_RDX # push last 64 bits onto stack push_rax # allocate stack for image
LOAD64_rel_RDX %LOADED_IMAGE_PROTOCOL # EFI_LOADED_IMAGE_PROTOCOL_GUID (first 64 bits) mov_r8,rsp # arg3 = &image
PUSH_RDX # push first 64 bits onto stack push !1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
COPY_RSP_to_RDX # arg2 = &guid push !0 # arg5 = NULL
PUSH !1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL sub_rsp, !32 # allocate shadow stack space for UEFI function
PUSH !0 # arg5 = NULL call_[r14+DWORD] %280 # system->boot->open_protocol(image_handle, &guid, &image, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
COPY_RCX_to_R9 # arg4 = image_handle add_rsp, !48 # deallocate stack
# arg1 = ImageHandle (already set) pop_rdi # get image
SUBI8_RSP !32 # allocate shadow stack space for UEFI function
CALL_R14 # 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_RCX_to_RAX # save image
# Command line args
LOAD64_into_RBX_from_Address_RAX_Immediate8 !56 # options = image->load_options
:loop_options1 # Skip application name
ADDI8_RBX !2 # ++options
LOAD8_AL_from_Address_RBX # *options
CMPI8_AL !0x20 # if *options != ' '
JNE8 !loop_options1 # then jump
ADDI8_RBX !2 # ++options
COPY_RBX_to_R12 # save input file
:loop_options2 # Skip argv[1]
ADDI8_RBX !2 # ++options
LOAD8_AL_from_Address_RBX # *options
CMPI8_AL !0x20 # if *options != ' '
JNE8 !loop_options2 # then jump
STOREI8_into_Address_RBX !0 # *options = 0;
ADDI8_RBX !2 # ++options
COPY_RBX_to_R13 # save output file
# Get root file system # Get root file system
PUSH_RAX # allocate stack for rootfs mov_r9,r15 # arg4 = image_handle
COPY_RSP_to_R8 # arg3 = &rootfs lea_rdx,[rip+DWORD] %SIMPLE_FS_PROTOCOL # guid = &SIMPLE_FS_PROTOCOL
LOAD64_rel_RDX %SIMPLE_FS_PROTOCOL_8 # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID (last 64 bits) mov_rcx,[rdi+BYTE] !24 # arg1 = root_device = image->device
PUSH_RDX # push last 64 bits onto stack mov_r13,rcx # save root_device
LOAD64_rel_RDX %SIMPLE_FS_PROTOCOL # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID (first 64 bits) push_rax # allocate stack for rootfs
PUSH_RDX # push first 64 bits onto stack mov_r8,rsp # arg3 = &rootfs
COPY_RSP_to_RDX # arg2 = &guid push !1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
PUSH !1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL push !0 # arg5 = NULL
PUSH !0 # arg5 = NULL sub_rsp, !32 # allocate shadow stack space for UEFI function
COPY_R15_to_R9 # arg4 = image_handle call_[r14+DWORD] %280 # system->boot->open_protocol(root_device, &guid, &rootfs, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
LOAD64_into_RCX_from_Address_RCX_Immediate8 !24 # arg1 = root_device = image->device add_rsp, !48 # deallocate stack
SUBI8_RSP !32 # allocate shadow stack space for UEFI function pop_rcx # get rootfs
CALL_R14 # 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
# Get root directory # Get root directory
PUSH_RDX # allocate stack for rootdir push_rax # allocate stack for rootdir
COPY_RSP_to_RDX # arg2 = &rootdir mov_rdx,rsp # arg2 = &rootdir
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_Immediate8 !8 # rootfs->open_volume(rootfs, &rootdir) call_[rcx+BYTE] !8 # rootfs->open_volume(rootfs, &rootdir)
POP_RAX # deallocate stack pop_rax # deallocate stack
POP_RAX # deallocate stack pop_rax # deallocate stack
POP_R14 # save rootdir pop_rsi # get rootdir
# Open file for writing # Push command line arguments onto stack
PUSH_RDX # allocate stack for fout mov_rbx,[rdi+BYTE] !56 # options = image->load_options
COPY_RSP_to_RDX # arg2 = &fout mov_rdx,rbx # save beginning of load_options
PUSH !0 # arg5 = 0 add_rbx,[rdi+BYTE] !48 # go to the end of load_options
PUSH !7 # to get 0x8000000000000003 we set the rightmost 3 bits :loop_options
POP_R9 # and then do right rotation by 1 cmp_rbx,rdx # Check if we are done
ROR_R9 # arg4 = EFI_FILE_MODE_CREATE| EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ je !loop_options_done # We are done
COPY_R13_to_R8 # arg3 = out sub_rbx, !2 # --options
COPY_R14_to_RCX # arg1 = rootdir mov_al,[rbx] # *options
SUBI8_RSP !32 # allocate shadow stack space for UEFI function cmp_al, !0x20 # if *options != ' '
CALL_RCX_Immediate8 !8 # rootdir->open() jne !loop_options # then continue looping
LOAD64_into_R13_from_Address_RSP_Immediate8 !40 # get fout 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 # Open file for reading
PUSH_RDX # allocate stack for fin pop_r8 # arg3 = in
COPY_RSP_to_RDX # arg2 = &fin push_rdx # allocate stack for fin
PUSH !1 # arg5 = EFI_FILE_READ_ONLY mov_rdx,rsp # arg2 = &fin
PUSH !1 # prepare to set arg4 to EFI_FILE_MODE_READ push !1 # arg5 = EFI_FILE_READ_ONLY
POP_R9 # arg4 = EFI_FILE_MODE_READ push !1 # prepare to set arg4 to EFI_FILE_MODE_READ
COPY_R12_to_R8 # arg3 = in pop_r9 # arg4 = EFI_FILE_MODE_READ
COPY_R14_to_RCX # arg1 = rootdir mov_rcx,rsi # arg1 = rootdir
SUBI8_RSP !32 # allocate shadow stack space for UEFI function sub_rsp, !32 # allocate shadow stack space for UEFI function
CALL_RCX_Immediate8 !8 # rootdir->open() call_[rcx+BYTE] !8 # rootdir->open()
LOAD64_into_R12_from_Address_RSP_Immediate8 !40 # get fin 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 # Our flag for byte processing
PUSH !-1 push !-1
POP_RBX # rbx = -1 pop_rbx # rbx = -1
# temp storage for the sum # temp storage for the sum
XOR_EDI_EDI # rdi = 0 xor_edi,edi # rdi = 0
:loop :loop
# Read a byte # Read a byte
CALLI32 %read_byte call %read_byte
# process byte # process byte
CALLI32 %hex call %hex
# deal with -1 values # deal with -1 values
TEST_EAX_EAX test_eax,eax
JL8 !loop jl !loop
# deal with toggle # deal with toggle
TEST_EBX_EBX # jump if ebx >= 0 test_ebx,ebx # jump if ebx >= 0
JGE8 !print jge !print
# process first byte of pair # process first byte of pair
COPY_EAX_to_EDI mov_edi,eax
XOR_EBX_EBX # rbx = 0 xor_ebx,ebx # rbx = 0
JMP8 !loop jmp !loop
# process second byte of pair # process second byte of pair
:print :print
# update the sum and store in output # update the sum and store in output
SHL_EDI_Immediate8 !4 shl_edi, !4
ADD_EAX_EDI add_eax,edi
# flip the toggle # flip the toggle
DEC_EBX # rbx = -1 dec_ebx # rbx = -1
CALLI32 %write_byte call %write_byte
JMP8 !loop jmp !loop
:hex :hex
# Purge Comment Lines (#) # Purge Comment Lines (#)
CMPI8_AL !35 cmp_al, !35
JE8 !purge_comment je !purge_comment
# Purge Comment Lines (;) # Purge Comment Lines (;)
CMPI8_AL !59 cmp_al, !59
JE8 !purge_comment je !purge_comment
# deal all ascii less than '0' # deal all ascii less than '0'
CMPI8_AL !48 cmp_al, !48
JL8 !ascii_other jl !ascii_other
# deal with 0-9 # deal with 0-9
CMPI8_AL !58 cmp_al, !58
JL8 !ascii_num jl !ascii_num
# deal with all ascii less than 'A' # deal with all ascii less than 'A'
CMPI8_AL !65 cmp_al, !65
JL8 !ascii_other jl !ascii_other
# deal with 'A'-'F' # deal with 'A'-'F'
CMPI8_AL !71 cmp_al, !71
JL8 !ascii_high jl !ascii_high
# deal with all ascii less than 'a' # deal with all ascii less than 'a'
CMPI8_AL !97 cmp_al, !97
JL8 !ascii_other jl !ascii_other
#deal with 'a'-'f' #deal with 'a'-'f'
CMPI8_AL !103 cmp_al, !103
JL8 !ascii_low jl !ascii_low
# The rest that remains needs to be ignored # The rest that remains needs to be ignored
JMP8 !ascii_other jmp !ascii_other
:purge_comment :purge_comment
# Read a byte # Read a byte
CALLI32 %read_byte call %read_byte
# Loop if not LF # Loop if not LF
CMPI8_AL !10 cmp_al, !10
JNE8 !purge_comment jne !purge_comment
# Otherwise return -1 # Otherwise return -1
:ascii_other :ascii_other
PUSH !-1 push !-1
POP_RAX # return = -1 pop_rax # return = -1
RET ret
:ascii_num :ascii_num
SUBI8_AL !48 sub_al, !48
RET ret
:ascii_low :ascii_low
SUBI8_AL !32 # convert to uppercase sub_al, !32 # convert to uppercase
:ascii_high :ascii_high
SUBI8_AL !55 sub_al, !55
RET ret
# Writes byte stored in al # Writes byte stored in al
:write_byte :write_byte
COPY_R13_to_RCX # arg1 = fout mov_rcx,r13 # arg1 = fout
PUSH !1 # size = 1 push !1 # size = 1
COPY_RSP_to_RDX # arg2 = &size mov_rdx,rsp # arg2 = &size
PUSH_RAX # allocate stack push_rax # allocate stack
COPY_RSP_to_R8 # arg3 = &output 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 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_Immediate8 !40 # fout->write() call_[rcx+BYTE] !40 # fout->write()
ADDI8_RSP !40 # deallocate stack add_rsp, !40 # deallocate stack
RET # return ret # return
:read_byte :read_byte
COPY_R12_to_RCX # arg1 = fin mov_rcx,r12 # arg1 = fin
PUSH !1 # size = 1 push !1 # size = 1
COPY_RSP_to_RDX # arg2 = &size mov_rdx,rsp # arg2 = &size
PUSH_RSI # allocate stack xor_esi,esi # zero rsi
COPY_RSP_to_R8 # arg3 = &input push_rsi # allocate stack
PUSH_RAX # allocate shadow stack space for UEFI function 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 push_rax # allocate shadow stack space for UEFI function
CALL_RCX_Immediate8 !32 # fin->read() push_rax # allocate shadow stack space for UEFI function
POP_RAX # deallocate stack call_[rcx+BYTE] !32 # fin->read()
POP_RAX # deallocate stack pop_rax # deallocate stack
POP_RAX # deallocate stack pop_rax # deallocate stack
POP_RAX # save input to rax pop_rax # deallocate stack
POP_RSI # save size to rsi pop_rax # save input to rax
pop_rsi # save size to rsi
# 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
JE8 !terminate # then we are done je !terminate # then we are done
RET # return ret # return
:terminate :terminate
PUSH_RBX # allocate stack pop_rax # remove last return address from stack
COPY_R12_to_RCX # arg1 = fin pop_rsi # restore rootdir
CALL_RCX_Immediate8 !16 # fin->close() pop_r15 # restore image_handle
COPY_R13_to_RCX # arg1 = fout pop_r14 # restore system->boot
CALL_RCX_Immediate8 !16 # fout->close() pop_rbx # restore root_device
COPY_RBP_to_RSP # restore stack push_rbx # allocate stack
RET # return to UEFI 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 # Protocol GUIDs
@ -334,14 +352,12 @@ DEFINE TEST_ESI_ESI 85F6
%0x5b1b31a1 %0x5b1b31a1
@0x9562 @0x9562
@0x11d2 @0x11d2
:LOADED_IMAGE_PROTOCOL_8
!0x8e !0x3f !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b !0x8e !0x3f !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b
:SIMPLE_FS_PROTOCOL :SIMPLE_FS_PROTOCOL
%0x0964e5b22 %0x0964e5b22
@0x6459 @0x6459
@0x11d2 @0x11d2
:SIMPLE_FS_PROTOCOL_8
!0x8e !0x39 !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b !0x8e !0x39 !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b
:PE32_end :PE32_end

View File

@ -11,14 +11,6 @@
# 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
# Registers:
# r12 in/fin: input file name, later reused for handle
# r13 out/fout: output file name, later reused for handle
# r14 system->boot->open_protocol from UEFI, later reused for rootdir
# r15 image_handle from UEFI
# hex0 algorithm mostly follows stage0-posix version but uses rdi instead of rbp
.global _start .global _start
.text .text
@ -27,97 +19,93 @@ _start:
mov rbp, rsp # save stack pointer mov rbp, rsp # save stack pointer
mov r15, rcx # save image_handle mov r15, rcx # save image_handle
mov r14, [rdx+96] # system->boot mov r14, [rdx+96] # system->boot
mov r14, [r14+280] # system->boot->open_protocol
# Open Loaded Image protocol # Open Loaded Image protocol
mov r9, r15 # arg4 = image_handle
lea rdx, [rip+LOADED_IMAGE_PROTOCOL] # guid = &LOADED_IMAGE_PROTOCOL
mov rcx, r9 # arg1 = image_handle
push rax # allocate stack for image push rax # allocate stack for image
mov r8, rsp # arg3 = &image mov r8, rsp # arg3 = &image
mov rdx, [rip+LOADED_IMAGE_PROTOCOL+8] # EFI_LOADED_IMAGE_PROTOCOL_GUID (last 64 bits)
push rdx # push last 64 bits onto stack
mov rdx, [rip+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 1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
push 0 # arg5 = NULL push 0 # arg5 = NULL
mov r9, rcx # arg4 = image_handle
# arg1 = ImageHandle (already set)
sub rsp, 32 # allocate shadow stack space for UEFI function sub rsp, 32 # allocate shadow stack space for UEFI function
call r14 # system->boot->open_protocol(image_handle, &guid, &image, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL) call [r14+280] # system->boot->open_protocol(image_handle, &guid, &image, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
mov rax, [rsp+64] # get image add rsp, 48 # deallocate stack
mov rcx, rax # save image pop rdi # get image
# Command line args
mov rbx, [rax+56] # options = image->load_options
loop_options1: # Skip application name
add rbx, 2 # ++options
mov al, [rbx] # *options
cmp al, 0x20 # if *options != ' '
jne loop_options1 # then jump
add rbx, 2 # ++options
mov r12, rbx # save input file
loop_options2: # Skip argv[1]
add rbx, 2 # ++options
mov al, [rbx] # *options
cmp al, 0x20 # if *options != ' '
jne loop_options2 # then jump
mov byte ptr [rbx], 0 # *options = 0
add rbx, 2 # ++options
mov r13, rbx # save output file
# Get root file system # Get root file system
mov r9, r15 # arg4 = image_handle
lea rdx, [rip+SIMPLE_FS_PROTOCOL] # guid = &SIMPLE_FS_PROTOCOL
mov rcx, [rdi+24] # arg1 = root_device = image->device
mov r13, rcx # save root_device
push rax # allocate stack for rootfs push rax # allocate stack for rootfs
mov r8, rsp # arg3 = &rootfs mov r8, rsp # arg3 = &rootfs
mov rdx, [rip+SIMPLE_FS_PROTOCOL+8] # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID (last 64 bits)
push rdx # push last 64 bits onto stack
mov rdx, [rip+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 1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
push 0 # arg5 = NULL push 0 # arg5 = NULL
mov r9, r15 # arg4 = image_handle
mov rcx, [rcx+24] # arg1 = root_device = image->device
sub rsp, 32 # allocate shadow stack space for UEFI function sub rsp, 32 # allocate shadow stack space for UEFI function
call r14 # system->boot->open_protocol(root_device, &guid, &rootfs, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL) call [r14+280] # system->boot->open_protocol(root_device, &guid, &rootfs, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
mov rcx, [rsp+64] # get rootfs add rsp, 48 # deallocate stack
pop rcx # get rootfs
# Get root directory # Get root directory
push rdx # allocate stack for rootdir push rax # allocate stack for rootdir
mov rdx, rsp # arg2 = &rootdir mov rdx, rsp # arg2 = &rootdir
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+8] # rootfs->open_volume(rootfs, &rootdir) call [rcx+8] # rootfs->open_volume(rootfs, &rootdir)
pop rax # deallocate stack pop rax # deallocate stack
pop rax # deallocate stack pop rax # deallocate stack
pop r14 # save rootdir pop rsi # get rootdir
# Push command line arguments onto stack
mov rbx, [rdi+56] # options = image->load_options
mov rdx, rbx # save beginning of load_options
add rbx, [rdi+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 BYTE PTR [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+8] # rootdir->open()
add rsp, 40 # deallocate stack
pop r12 # get fin
# Open file for writing # Open file for writing
pop r8 # arg3 = out
push r13 # save root_device
push rdx # allocate stack for fout push rdx # allocate stack for fout
mov rdx, rsp # arg2 = &fout mov rdx, rsp # arg2 = &fout
push 0 # arg5 = 0 push 0 # arg5 = 0
push 7 # to get 0x8000000000000003 we set the rightmost 3 bits push 7 # to get 0x8000000000000003 we set the rightmost 3 bits
pop r9 # and then do right rotation by 1 pop r9 # and then do right rotation by 1
ror r9 # arg4 = EFI_FILE_MODE_CREATE| EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ ror r9 # arg4 = EFI_FILE_MODE_CREATE| EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ
mov r8, r13 # arg3 = out mov rcx, rsi # arg1 = rootdir
mov rcx, r14 # arg1 = rootdir
sub rsp, 32 # allocate shadow stack space for UEFI function sub rsp, 32 # allocate shadow stack space for UEFI function
call [rcx+8] # rootdir->open() call [rcx+8] # rootdir->open()
mov r13, [rsp+40] # get fout add rsp, 40 # deallocate stack
pop r13 # get fout
# Open file for reading # Save variables that are needed for cleanup
push rdx # allocate stack for fin push r14 # save system->boot
mov rdx, rsp # arg2 = &fin push r15 # save image_handle
push 1 # arg5 = EFI_FILE_READ_ONLY push rsi # save rootdir
push 1 # prepare to set arg4 to EFI_FILE_MODE_READ
pop r9 # arg4 = EFI_FILE_MODE_READ
mov r8, r12 # arg3 = in
mov rcx, r14 # arg1 = rootdir
sub rsp, 32 # allocate shadow stack space for UEFI function
call [rcx+8] # rootdir->open()
mov r12, [rsp+40] # get fin
# Our flag for byte processing # Our flag for byte processing
push -1 push -1
@ -240,6 +228,7 @@ read_byte:
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
xor esi, esi # zero rsi
push rsi # allocate stack push rsi # allocate stack
mov r8, rsp # arg3 = &input mov r8, rsp # arg3 = &input
push rax # allocate shadow stack space for UEFI function push rax # allocate shadow stack space for UEFI function
@ -259,11 +248,33 @@ read_byte:
ret # return ret # return
terminate: 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 push rbx # allocate stack
mov rcx, r12 # arg1 = fin mov rcx, r12 # arg1 = fin
call [rcx+16] # fin->close() call [rcx+16] # fin->close()
mov rcx, r13 # arg1 = fout mov rcx, r13 # arg1 = fout
call [rcx+16] # fout->close() call [rcx+16] # fout->close()
mov rcx, rsi # arg1 = rootdir
call [rcx+16] # rootdir->close()
mov r8, r15 # arg3 = image_handle
lea rdx, [rip+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+288] # system->boot->close_protocol(root_device, &guid, image_handle, 0)
mov r8, r15 # arg3 = image_handle
lea rdx, [rip+LOADED_IMAGE_PROTOCOL] # guid = &LOADED_IMAGE_PROTOCOL
mov rcx, r8 # arg1 = image_handle
xor r9, r9 # arg4 = NULL
call [r14+288] # system->boot->close_protocol(image_handle, &guid, image_handle, 0)
abort: # used for debugging only abort: # used for debugging only
mov rsp, rbp # restore stack mov rsp, rbp # restore stack
ret # return to UEFI ret # return to UEFI

View File

@ -11,260 +11,269 @@
# 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
# Registers:
# r12 in/fin: input file name, later reused for handle
# r13 out/fout: output file name, later reused for handle
# r14 system->boot->open_protocol from UEFI, later reused for rootdir
# r15 image_handle from UEFI
# hex0 algorithm mostly follows stage0-posix version but uses rdi instead of rbp
# efi_main(void *image_handle, struct efi_system_table *system) # efi_main(void *image_handle, struct efi_system_table *system)
:_start :_start
4889E5 ; COPY_RSP_to_RBP # save stack pointer 4889E5 ; mov_rbp,rsp # save stack pointer
4989CF ; COPY_RCX_to_R15 # save image_handle 4989CF ; mov_r15,rcx # save image_handle
4C8B72 60 ; LOAD64_into_R14_from_Address_RDX_Immediate8 !96 # system->boot 4C8B72 60 ; mov_r14,[rdx+BYTE] !96 # system->boot
4D8BB6 18010000 ; LOAD64_into_R14_from_Address_R14_Immediate32 %280 # system->boot->open_protocol
# Open Loaded Image protocol # Open Loaded Image protocol
50 ; PUSH_RAX # allocate stack for image 4D89F9 ; mov_r9,r15 # arg4 = image_handle
4989E0 ; COPY_RSP_to_R8 # arg3 = &image 488D15 %LOADED_IMAGE_PROTOCOL ; lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL
488B15 %LOADED_IMAGE_PROTOCOL_8 ; LOAD64_rel_RDX %LOADED_IMAGE_PROTOCOL_8 # EFI_LOADED_IMAGE_PROTOCOL_GUID (last 64 bits) 4C89C9 ; mov_rcx,r9 # arg1 = image_handle
52 ; PUSH_RDX # push last 64 bits onto stack 50 ; push_rax # allocate stack for image
488B15 %LOADED_IMAGE_PROTOCOL ; LOAD64_rel_RDX %LOADED_IMAGE_PROTOCOL # EFI_LOADED_IMAGE_PROTOCOL_GUID (first 64 bits) 4989E0 ; mov_r8,rsp # arg3 = &image
52 ; PUSH_RDX # push first 64 bits onto stack 6A 01 ; push !1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
4889E2 ; COPY_RSP_to_RDX # arg2 = &guid 6A 00 ; push !0 # arg5 = NULL
6A 01 ; PUSH !1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL 4883EC 20 ; sub_rsp, !32 # allocate shadow stack space for UEFI function
6A 00 ; PUSH !0 # arg5 = NULL 41FF96 18010000 ; call_[r14+DWORD] %280 # system->boot->open_protocol(image_handle, &guid, &image, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
4989C9 ; COPY_RCX_to_R9 # arg4 = image_handle 4883C4 30 ; add_rsp, !48 # deallocate stack
# arg1 = ImageHandle (already set) 5F ; pop_rdi # get image
4883EC 20 ; SUBI8_RSP !32 # allocate shadow stack space for UEFI function
41FFD6 ; CALL_R14 # 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
4889C1 ; COPY_RCX_to_RAX # save image
# Command line args
488B58 38 ; LOAD64_into_RBX_from_Address_RAX_Immediate8 !56 # options = image->load_options
:loop_options1 # Skip application name
4883C3 02 ; ADDI8_RBX !2 # ++options
8A03 ; LOAD8_AL_from_Address_RBX # *options
3C 20 ; CMPI8_AL !0x20 # if *options != ' '
75 !loop_options1 ; JNE8 !loop_options1 # then jump
4883C3 02 ; ADDI8_RBX !2 # ++options
4989DC ; COPY_RBX_to_R12 # save input file
:loop_options2 # Skip argv[1]
4883C3 02 ; ADDI8_RBX !2 # ++options
8A03 ; LOAD8_AL_from_Address_RBX # *options
3C 20 ; CMPI8_AL !0x20 # if *options != ' '
75 !loop_options2 ; JNE8 !loop_options2 # then jump
C603 00 ; STOREI8_into_Address_RBX !0 # *options = 0;
4883C3 02 ; ADDI8_RBX !2 # ++options
4989DD ; COPY_RBX_to_R13 # save output file
# Get root file system # Get root file system
50 ; PUSH_RAX # allocate stack for rootfs 4D89F9 ; mov_r9,r15 # arg4 = image_handle
4989E0 ; COPY_RSP_to_R8 # arg3 = &rootfs 488D15 %SIMPLE_FS_PROTOCOL ; lea_rdx,[rip+DWORD] %SIMPLE_FS_PROTOCOL # guid = &SIMPLE_FS_PROTOCOL
488B15 %SIMPLE_FS_PROTOCOL_8 ; LOAD64_rel_RDX %SIMPLE_FS_PROTOCOL_8 # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID (last 64 bits) 488B4F 18 ; mov_rcx,[rdi+BYTE] !24 # arg1 = root_device = image->device
52 ; PUSH_RDX # push last 64 bits onto stack 4989CD ; mov_r13,rcx # save root_device
488B15 %SIMPLE_FS_PROTOCOL ; LOAD64_rel_RDX %SIMPLE_FS_PROTOCOL # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID (first 64 bits) 50 ; push_rax # allocate stack for rootfs
52 ; PUSH_RDX # push first 64 bits onto stack 4989E0 ; mov_r8,rsp # arg3 = &rootfs
4889E2 ; COPY_RSP_to_RDX # arg2 = &guid 6A 01 ; push !1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
6A 01 ; PUSH !1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL 6A 00 ; push !0 # arg5 = NULL
6A 00 ; PUSH !0 # arg5 = NULL 4883EC 20 ; sub_rsp, !32 # allocate shadow stack space for UEFI function
4D89F9 ; COPY_R15_to_R9 # arg4 = image_handle 41FF96 18010000 ; call_[r14+DWORD] %280 # system->boot->open_protocol(root_device, &guid, &rootfs, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
488B49 18 ; LOAD64_into_RCX_from_Address_RCX_Immediate8 !24 # arg1 = root_device = image->device 4883C4 30 ; add_rsp, !48 # deallocate stack
4883EC 20 ; SUBI8_RSP !32 # allocate shadow stack space for UEFI function 59 ; pop rcx # get rootfs
41FFD6 ; CALL_R14 # 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
# Get root directory # Get root directory
52 ; PUSH_RDX # allocate stack for rootdir 50 ; push_rax # allocate stack for rootdir
4889E2 ; COPY_RSP_to_RDX # arg2 = &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
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) FF51 08 ; call_[rcx+BYTE] !8 # rootfs->open_volume(rootfs, &rootdir)
58 ; POP_RAX # deallocate stack 58 ; pop_rax # deallocate stack
58 ; POP_RAX # deallocate stack 58 ; pop_rax # deallocate stack
415E ; POP_R14 # save rootdir 5E ; pop_rsi # save rootdir
# Open file for writing # Push command line arguments onto stack
52 ; PUSH_RDX # allocate stack for fout 488B5F 38 ; mov_rbx,[rdi+BYTE] !56 # options = image->load_options
4889E2 ; COPY_RSP_to_RDX # arg2 = &fout 4889DA ; mov_rdx,rbx # save beginning of load_options
6A 00 ; PUSH !0 # arg5 = 0 48035F 30 ; add_rbx,[rdi+BYTE] !48 # go to the end of load_options
6A 07 ; PUSH !7 # to get 0x8000000000000003 we set the rightmost 3 bits :loop_options
4159 ; POP_R9 # and then do right rotation by 1 4839D3 ; cmp_rbx,rdx # Check if we are done
49D1C9 ; ROR_R9 # arg4 = EFI_FILE_MODE_CREATE| EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ 74 !loop_options_done ; je !loop_options_done # We are done
4D89E8 ; COPY_R13_to_R8 # arg3 = out 4883EB 02 ; sub_rbx, !2 # --options
4C89F1 ; COPY_R14_to_RCX # arg1 = rootdir 8A03 ; mov_al,[rbx] # *options
4883EC 20 ; SUBI8_RSP !32 # allocate shadow stack space for UEFI function 3C 20 ; cmp_al, !0x20 # if *options != ' '
FF51 08 ; CALL_RCX_Immediate8 !8 # rootdir->open() 75 !loop_options ; jne !loop_options # then continue looping
4C8B6C24 28 ; LOAD64_into_R13_from_Address_RSP_Immediate8 !40 # get fout 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
# Open file for reading # Open file for reading
52 ; PUSH_RDX # allocate stack for fin 4158 ; pop_r8 # arg3 = in
4889E2 ; COPY_RSP_to_RDX # arg2 = &fin 52 ; push_rdx # allocate stack for fin
6A 01 ; PUSH !1 # arg5 = EFI_FILE_READ_ONLY 4889E2 ; mov_rdx,rsp # arg2 = &fin
6A 01 ; PUSH !1 # prepare to set arg4 to EFI_FILE_MODE_READ 6A 01 ; push !1 # arg5 = EFI_FILE_READ_ONLY
4159 ; POP_R9 # arg4 = EFI_FILE_MODE_READ 6A 01 ; push !1 # prepare to set arg4 to EFI_FILE_MODE_READ
4D89E0 ; COPY_R12_to_R8 # arg3 = in 4159 ; pop_r9 # arg4 = EFI_FILE_MODE_READ
4C89F1 ; COPY_R14_to_RCX # arg1 = rootdir 4889F1 ; mov_rcx,rsi # arg1 = rootdir
4883EC 20 ; SUBI8_RSP !32 # allocate shadow stack space for UEFI function 4883EC 20 ; sub_rsp, !32 # allocate shadow stack space for UEFI function
FF51 08 ; CALL_RCX_Immediate8 !8 # rootdir->open() FF51 08 ; call_[rcx+BYTE] !8 # rootdir->open()
4C8B6424 28 ; LOAD64_into_R12_from_Address_RSP_Immediate8 !40 # get fin 4883C4 28 ; add_rsp, !40 # deallocate stack
415C ; pop_r12 # get fin
# Open file for writing
4158 ; pop_r8 # arg3 = out
4155 ; push_r13 # save root_device
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
4889F1 ; mov_rcx,rsi # 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
415D ; pop_r13 # get fout
# Save variables that are needed for cleanup
4156 ; push_r14 # save system->boot
4157 ; push_r15 # save image_handle
56 ; push_rsi # save rootdir
# Our flag for byte processing # Our flag for byte processing
6A FF ; PUSH !-1 6A FF ; push !-1
5B ; POP_RBX # rbx = -1 5B ; pop_rbx # rbx = -1
# temp storage for the sum # temp storage for the sum
31FF ; XOR_EDI_EDI # rdi = 0 31FF ; xor_edi,edi # rdi = 0
:loop :loop
# Read a byte # Read a byte
E8 %read_byte ; CALLI32 %read_byte E8 %read_byte ; call %read_byte
# process byte # process byte
E8 %hex ; CALLI32 %hex E8 %hex ; call %hex
# deal with -1 values # deal with -1 values
85C0 ; TEST_EAX_EAX 85C0 ; test_eax,eax
7C !loop ; JL8 !loop 7C !loop ; jl !loop
# deal with toggle # deal with toggle
85DB ; TEST_EBX_EBX # jump if ebx >= 0 85DB ; test_ebx,ebx # jump if ebx >= 0
7D !print ; JGE8 !print 7D !print ; jge !print
# process first byte of pair # process first byte of pair
89C7 ; COPY_EAX_to_EDI 89C7 ; mov_edi,eax
31DB ; XOR_EBX_EBX # rbx = 0 31DB ; xor_ebx,ebx # rbx = 0
EB !loop ; JMP8 !loop EB !loop ; jmp !loop
# process second byte of pair # process second byte of pair
:print :print
# update the sum and store in output # update the sum and store in output
C1E7 04 ; SHL_EDI_Immediate8 !4 C1E7 04 ; shl_edi, !4
01F8 ; ADD_EAX_EDI 01F8 ; add_eax,edi
# flip the toggle # flip the toggle
FFCB ; DEC_EBX # rbx = -1 FFCB ; dec_ebx # rbx = -1
E8 %write_byte ; CALLI32 %write_byte E8 %write_byte ; call %write_byte
EB !loop ; JMP8 !loop EB !loop ; jmp !loop
:hex :hex
# Purge Comment Lines (#) # Purge Comment Lines (#)
3C 23 ; CMPI8_AL !35 3C 23 ; cmp_al, !35
74 !purge_comment ; JE8 !purge_comment 74 !purge_comment ; je !purge_comment
# Purge Comment Lines (;) # Purge Comment Lines (;)
3C 3B ; CMPI8_AL !59 3C 3B ; cmp_al, !59
74 !purge_comment ; JE8 !purge_comment 74 !purge_comment ; je !purge_comment
# deal all ascii less than '0' # deal all ascii less than '0'
3C 30 ; CMPI8_AL !48 3C 30 ; cmp_al, !48
7C !ascii_other ; JL8 !ascii_other 7C !ascii_other ; jl !ascii_other
# deal with 0-9 # deal with 0-9
3C 3A ; CMPI8_AL !58 3C 3A ; cmp_al, !58
7C !ascii_num ; JL8 !ascii_num 7C !ascii_num ; jl !ascii_num
# deal with all ascii less than 'A' # deal with all ascii less than 'A'
3C 41 ; CMPI8_AL !65 3C 41 ; cmp_al, !65
7C !ascii_other ; JL8 !ascii_other 7C !ascii_other ; jl !ascii_other
# deal with 'A'-'F' # deal with 'A'-'F'
3C 47 ; CMPI8_AL !71 3C 47 ; cmp_al, !71
7C !ascii_high ; JL8 !ascii_high 7C !ascii_high ; jl !ascii_high
# deal with all ascii less than 'a' # deal with all ascii less than 'a'
3C 61 ; CMPI8_AL !97 3C 61 ; cmp_al, !97
7C !ascii_other ; JL8 !ascii_other 7C !ascii_other ; jl !ascii_other
#deal with 'a'-'f' #deal with 'a'-'f'
3C 67 ; CMPI8_AL !103 3C 67 ; cmp_al, !103
7C !ascii_low ; JL8 !ascii_low 7C !ascii_low ; jl !ascii_low
# The rest that remains needs to be ignored # The rest that remains needs to be ignored
EB !ascii_other ; JMP8 !ascii_other EB !ascii_other ; jmp !ascii_other
:purge_comment :purge_comment
# Read a byte # Read a byte
E8 %read_byte ; CALLI32 %read_byte E8 %read_byte ; call %read_byte
# Loop if not LF # Loop if not LF
3C 0A ; CMPI8_AL !10 3C 0A ; cmp_al, !10
75 !purge_comment ; JNE8 !purge_comment 75 !purge_comment ; jne !purge_comment
# Otherwise return -1 # Otherwise return -1
:ascii_other :ascii_other
6A FF ; PUSH !-1 6A FF ; push !-1
58 ; POP_RAX # return = -1 58 ; pop_rax # return = -1
C3 ; RET C3 ; ret
:ascii_num :ascii_num
2C 30 ; SUBI8_AL !48 2C 30 ; sub_al, !48
C3 ; RET C3 ; ret
:ascii_low :ascii_low
2C 20 ; SUBI8_AL !32 # convert to uppercase 2C 20 ; sub_al, !32 # convert to uppercase
:ascii_high :ascii_high
2C 37 ; SUBI8_AL !55 2C 37 ; sub_al, !55
C3 ; RET C3 ; ret
# Writes byte stored in al # Writes byte stored in al
:write_byte :write_byte
4C89E9 ; COPY_R13_to_RCX # arg1 = fout 4C89E9 ; mov_rcx,r13 # arg1 = fout
6A 01 ; PUSH !1 # size = 1 6A 01 ; push !1 # size = 1
4889E2 ; COPY_RSP_to_RDX # arg2 = &size 4889E2 ; mov_rdx,rsp # arg2 = &size
50 ; PUSH_RAX # allocate stack 50 ; push_rax # allocate stack
4989E0 ; COPY_RSP_to_R8 # arg3 = &output 4989E0 ; mov_r8,rsp # arg3 = &output
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
50 ; PUSH_RAX # allocate shadow stack space for UEFI function 50 ; push_rax # allocate shadow stack space for UEFI function
FF51 28 ; CALL_RCX_Immediate8 !40 # fout->write() FF51 28 ; call_[rcx+BYTE] !40 # fout->write()
4883C4 28 ; ADDI8_RSP !40 # deallocate stack 4883C4 28 ; add_rsp, !40 # deallocate stack
C3 ; RET # return C3 ; ret # return
:read_byte :read_byte
4C89E1 ; COPY_R12_to_RCX # arg1 = fin 4C89E1 ; mov_rcx,r12 # arg1 = fin
6A 01 ; PUSH !1 # size = 1 6A 01 ; push !1 # size = 1
4889E2 ; COPY_RSP_to_RDX # arg2 = &size 4889E2 ; mov_rdx,rsp # arg2 = &size
56 ; PUSH_RSI # allocate stack 31F6 ; xor_esi,esi # zero rsi
4989E0 ; COPY_RSP_to_R8 # arg3 = &input 56 ; push_rsi # allocate stack
50 ; PUSH_RAX # allocate shadow stack space for UEFI function 4989E0 ; mov_r8,rsp # arg3 = &input
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 20 ; CALL_RCX_Immediate8 !32 # fin->read() 50 ; push_rax # allocate shadow stack space for UEFI function
58 ; POP_RAX # deallocate stack FF51 20 ; call_[rcx+BYTE] !32 # fin->read()
58 ; POP_RAX # deallocate stack 58 ; pop_rax # deallocate stack
58 ; POP_RAX # deallocate stack 58 ; pop_rax # deallocate stack
58 ; POP_RAX # save input to rax 58 ; pop_rax # deallocate stack
5E ; POP_RSI # save size to rsi 58 ; pop_rax # save input to rax
5E ; pop_rsi # save size to rsi
# 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
74 !terminate ; JE8 !terminate # then we are done 74 !terminate ; je !terminate # then we are done
C3 ; RET # return C3 ; ret # return
:terminate :terminate
53 ; PUSH_RBX # allocate stack 58 ; pop_rax # remove last return address from stack
4C89E1 ; COPY_R12_to_RCX # arg1 = fin 5E ; pop_rsi # restore rootdir
FF51 10 ; CALL_RCX_Immediate8 !16 # fin->close() 415F ; pop_r15 # restore image_handle
4C89E9 ; COPY_R13_to_RCX # arg1 = fout 415E ; pop_r14 # restore system->boot
FF51 10 ; CALL_RCX_Immediate8 !16 # fout->close() 5B ; pop_rbx # restore root_device
4889EC ; COPY_RBP_to_RSP # restore stack 53 ; push_rbx # allocate stack
C3 ; RET # return to UEFI 4C89E1 ; mov_rcx,r12 # arg1 = fin
FF51 10 ; call_[rcx+BYTE] !16 # fin->close()
4C89E9 ; mov_rcx,r13 # arg1 = fout
FF51 10 ; call_[rcx+BYTE] !16 # fout->close()
4889F1 ; mov_rcx,rsi # arg1 = rootdir
FF51 10 ; call_[rcx+BYTE] !16 # rootdir->close()
4D89F8 ; mov_r8,r15 # arg3 = image_handle
488D15 %SIMPLE_FS_PROTOCOL ; lea_rdx,[rip+DWORD] %SIMPLE_FS_PROTOCOL # guid = &SIMPLE_FS_PROTOCOL
4889D9 ; mov_rcx,rbx # arg1 = root_device
4D31C9 ; xor_r9,r9 # arg4 = NULL
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)
4D89F8 ; mov_r8,r15 # arg3 = image_handle
488D15 %LOADED_IMAGE_PROTOCOL ; lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL
4C89C1 ; mov_rcx,r8 # arg1 = image_handle
4D31C9 ; xor_r9,r9 # arg4 = NULL
41FF96 20010000 ; call_[r14+DWORD] %288 # system->boot->close_protocol(image_handle, &guid, image_handle, 0)
4889EC ; mov_rsp,rbp # restore stack
C3 ; ret # return to UEFI
# Protocol GUIDs # Protocol GUIDs
@ -272,14 +281,12 @@
A1 31 1B 5B ; %0x5b1b31a1 A1 31 1B 5B ; %0x5b1b31a1
62 95 ; @0x9562 62 95 ; @0x9562
D2 11 ; @0x11d2 D2 11 ; @0x11d2
:LOADED_IMAGE_PROTOCOL_8
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 :SIMPLE_FS_PROTOCOL
22 5B 4E 96 ; %0x0964e5b22 22 5B 4E 96 ; %0x0964e5b22
59 64 ; @0x6459 59 64 ; @0x6459
D2 11 ; @0x11d2 D2 11 ; @0x11d2
:SIMPLE_FS_PROTOCOL_8
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
:PE32_end :PE32_end

View File

@ -52,8 +52,6 @@ DEFINE mov_rax,[rax] 488B00
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_rcx,[rsp+BYTE] 488B4C24
DEFINE mov_rdi,[rsp+BYTE] 488B7C24
DEFINE mov_r14,[rdx+BYTE] 4C8B72 DEFINE mov_r14,[rdx+BYTE] 4C8B72
DEFINE mov_[rax],r13 4C8928 DEFINE mov_[rax],r13 4C8928
DEFINE not_r15 49F7D7 DEFINE not_r15 49F7D7
@ -88,6 +86,8 @@ DEFINE xor_edx,edx 31D2
DEFINE xor_esi,esi 31F6 DEFINE xor_esi,esi 31F6
DEFINE xor_r9,r9 4D31C9 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) # efi_main(void *image_handle, struct efi_system_table *system)
:_start :_start
@ -105,7 +105,8 @@ DEFINE xor_r9,r9 4D31C9
push !0 # arg5 = NULL push !0 # arg5 = NULL
sub_rsp, !32 # allocate shadow stack space for UEFI function 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) call_[r14+DWORD] %280 # system->boot->open_protocol(image_handle, &guid, &image, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
mov_rdi,[rsp+BYTE] !48 # save image add_rsp, !48 # deallocate stack
pop_rdi # get image
# Get root file system # Get root file system
mov_r9,r15 # arg4 = image_handle mov_r9,r15 # arg4 = image_handle
@ -118,7 +119,8 @@ DEFINE xor_r9,r9 4D31C9
push !0 # arg5 = NULL push !0 # arg5 = NULL
sub_rsp, !32 # allocate shadow stack space for UEFI function 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) call_[r14+DWORD] %280 # system->boot->open_protocol(root_device, &guid, &rootfs, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
mov_rcx,[rsp+BYTE] !48 # get rootfs add_rsp, !48 # deallocate stack
pop_rcx # get rootfs
# Get root directory # Get root directory
push_rax # allocate stack for rootdir push_rax # allocate stack for rootdir

View File

@ -25,7 +25,8 @@ _start:
push 0 # arg5 = NULL push 0 # arg5 = NULL
sub rsp, 32 # allocate shadow stack space for UEFI function sub rsp, 32 # allocate shadow stack space for UEFI function
call [r14+280] # system->boot->open_protocol(image_handle, &guid, &image, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL) call [r14+280] # system->boot->open_protocol(image_handle, &guid, &image, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
mov rdi, [rsp+48] # save image add rsp, 48 # deallocate stack
pop rdi # get image
# Get root file system # Get root file system
mov r9, r15 # arg4 = image_handle mov r9, r15 # arg4 = image_handle
@ -38,7 +39,8 @@ _start:
push 0 # arg5 = NULL push 0 # arg5 = NULL
sub rsp, 32 # allocate shadow stack space for UEFI function sub rsp, 32 # allocate shadow stack space for UEFI function
call [r14+280] # system->boot->open_protocol(root_device, &guid, &rootfs, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL) call [r14+280] # system->boot->open_protocol(root_device, &guid, &rootfs, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
mov rcx, [rsp+48] # get rootfs add rsp, 48 # deallocate stack
pop rcx # get rootfs
# Get root directory # Get root directory
push rax # allocate stack for rootdir push rax # allocate stack for rootdir
@ -181,7 +183,7 @@ First_pass_pointer:
jmp First_pass # Loop again jmp First_pass # Loop again
First_pass_done: First_pass_done:
RET ret
hex: hex:
# deal with EOF # deal with EOF

View File

@ -3,6 +3,8 @@
# #
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
# 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) # efi_main(void *image_handle, struct efi_system_table *system)
:_start :_start
@ -20,7 +22,8 @@
6A 00 ; push !0 # arg5 = NULL 6A 00 ; push !0 # arg5 = 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 18010000 ; call_[r14+DWORD] %280 # system->boot->open_protocol(image_handle, &guid, &image, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL) 41FF96 18010000 ; call_[r14+DWORD] %280 # system->boot->open_protocol(image_handle, &guid, &image, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
488B7C24 30 ; mov_rdi,[rsp+BYTE] !48 # save image 4883C4 30 ; add_rsp, !48 # deallocate stack
5F ; pop_rdi # get image
# Get root file system # Get root file system
4D89F9 ; mov_r9,r15 # arg4 = image_handle 4D89F9 ; mov_r9,r15 # arg4 = image_handle
@ -33,7 +36,8 @@
6A 00 ; push !0 # arg5 = NULL 6A 00 ; push !0 # arg5 = 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 18010000 ; call_[r14+DWORD] %280 # system->boot->open_protocol(root_device, &guid, &rootfs, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL) 41FF96 18010000 ; call_[r14+DWORD] %280 # system->boot->open_protocol(root_device, &guid, &rootfs, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
488B4C24 30 ; mov_rcx,[rsp+BYTE] !48 # get rootfs 4883C4 30 ; add_rsp, !48 # deallocate stack
59 ; pop rcx # get rootfs
# Get root directory # Get root directory
50 ; push_rax # allocate stack for rootdir 50 ; push_rax # allocate stack for rootdir

View File

@ -11,14 +11,6 @@
# 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
# Registers:
# r12 in/fin: input file name, later reused for handle
# r13 out/fout: output file name, later reused for handle
# r14 system->boot->open_protocol from UEFI, later reused for rootdir
# r15 image_handle from UEFI
# hex0 algorithm mostly follows stage0-posix version but uses rdi instead of rbp
# DOS MZ header # DOS MZ header
4D 5A # Signature 4D 5A # Signature
00 00 # Number of bytes in the last page. 00 00 # Number of bytes in the last page.
@ -136,9 +128,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"
A3 01 00 00 ; VirtualSize D1 01 00 00 ; VirtualSize
00 10 00 00 ; VirtualAddress 00 10 00 00 ; VirtualAddress
A3 01 00 00 ; SizeOfRawData D1 01 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
@ -150,266 +142,280 @@ A3 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 ; COPY_RSP_to_RBP # save stack pointer 4889E5 ; mov_rbp,rsp # save stack pointer
4989CF ; COPY_RCX_to_R15 # save image_handle 4989CF ; mov_r15,rcx # save image_handle
4C8B72 60 ; LOAD64_into_R14_from_Address_RDX_Immediate8 !96 # system->boot 4C8B72 60 ; mov_r14,[rdx+BYTE] !96 # system->boot
4D8BB6 18010000 ; LOAD64_into_R14_from_Address_R14_Immediate32 %280 # system->boot->open_protocol
# Open Loaded Image protocol # Open Loaded Image protocol
50 ; PUSH_RAX # allocate stack for image 4D89F9 ; mov_r9,r15 # arg4 = image_handle
4989E0 ; COPY_RSP_to_R8 # arg3 = &image 488D15 9D010000 ; lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL
488B15 6F010000 ; LOAD64_rel_RDX %LOADED_IMAGE_PROTOCOL_8 # EFI_LOADED_IMAGE_PROTOCOL_GUID (last 64 bits) 4C89C9 ; mov_rcx,r9 # arg1 = image_handle
# [_start+0x1C] 50 ; push_rax # allocate stack for image
52 ; PUSH_RDX # push last 64 bits onto stack 4989E0 ; mov_r8,rsp # arg3 = &image
488B15 5F010000 ; LOAD64_rel_RDX %LOADED_IMAGE_PROTOCOL # EFI_LOADED_IMAGE_PROTOCOL_GUID (first 64 bits) 6A 01 ; push !1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
# [_start+0x24] 6A 00 ; push !0 # arg5 = NULL
52 ; PUSH_RDX # push first 64 bits onto stack 4883EC 20 ; sub_rsp, !32 # allocate shadow stack space for UEFI function
4889E2 ; COPY_RSP_to_RDX # arg2 = &guid 41FF96 18010000 ; call_[r14+DWORD] %280 # system->boot->open_protocol(image_handle, &guid, &image, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
6A 01 ; PUSH !1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL 4883C4 30 ; add_rsp, !48 # deallocate stack
6A 00 ; PUSH !0 # arg5 = NULL 5F ; pop_rdi # get image
4989C9 ; COPY_RCX_to_R9 # arg4 = image_handle
# arg1 = ImageHandle (already set)
4883EC 20 ; SUBI8_RSP !32 # allocate shadow stack space for UEFI function
41FFD6 ; CALL_R14 # 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
4889C1 ; COPY_RCX_to_RAX # save image
# Command line args # Get root file system
488B58 38 ; LOAD64_into_RBX_from_Address_RAX_Immediate8 !56 # options = image->load_options 4D89F9 ; mov_r9,r15 # arg4 = image_handle
488D15 88010000 ; lea_rdx,[rip+DWORD] %SIMPLE_FS_PROTOCOL # guid = &SIMPLE_FS_PROTOCOL
# :loop_options1 [_start+0x42] # Skip application name 488B4F 18 ; mov_rcx,[rdi+BYTE] !24 # arg1 = root_device = image->device
4883C3 02 ; ADDI8_RBX !2 # ++options 4989CD ; mov_r13,rcx # save root_device
8A03 ; LOAD8_AL_from_Address_RBX # *options 50 ; push_rax # allocate stack for rootfs
3C 20 ; CMPI8_AL !0x20 # if *options != ' ' 4989E0 ; mov_r8,rsp # arg3 = &rootfs
75 F6 ; JNE8 !loop_options1 # then jump 6A 01 ; push !1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
6A 00 ; push !0 # arg5 = NULL
4883C3 02 ; ADDI8_RBX !2 # ++options 4883EC 20 ; sub_rsp, !32 # allocate shadow stack space for UEFI function
4989DC ; COPY_RBX_to_R12 # save input file 41FF96 18010000 ; call_[r14+DWORD] %280 # system->boot->open_protocol(root_device, &guid, &rootfs, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
4883C4 30 ; add_rsp, !48 # deallocate stack
# :loop_options2 [_start+0x53] # Skip argv[1] 59 ; pop rcx # get rootfs
4883C3 02 ; ADDI8_RBX !2 # ++options
8A03 ; LOAD8_AL_from_Address_RBX # *options
3C 20 ; CMPI8_AL !0x20 # if *options != ' '
75 F6 ; JNE8 !loop_options2 # then jump
C603 00 ; STOREI8_into_Address_RBX !0 # *options = 0;
4883C3 02 ; ADDI8_RBX !2 # ++options
4989DD ; COPY_RBX_to_R13 # save output file
# Get root file system [_start+0x67]
50 ; PUSH_RAX # allocate stack for rootfs
4989E0 ; COPY_R8_to_RSP # arg3 = &rootfs
488B15 29010000 ; 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 19010000 ; 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
488B49 18 ; LOAD64_into_RCX_from_Address_RCX_Immediate8 !24 # arg1 = root_device = image->device
4883EC 20 ; SUBI8_RSP !32 # allocate shadow stack space for UEFI function
41FFD6 ; CALL_R14 # 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
# Get root directory # Get root directory
52 ; PUSH_RDX # allocate stack for rootdir 50 ; push_rax # allocate stack for rootdir
4889E2 ; COPY_RSP_to_RDX # arg2 = &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
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) FF51 08 ; call_[rcx+BYTE] !8 # rootfs->open_volume(rootfs, &rootdir)
58 ; POP_RAX # deallocate stack 58 ; pop_rax # deallocate stack
58 ; POP_RAX # deallocate stack 58 ; pop_rax # deallocate stack
415E ; POP_R14 # save rootdir 5E ; pop_rsi # save rootdir
# Open file for writing # Push command line arguments onto stack
52 ; PUSH_RDX # allocate stack for fout 488B5F 38 ; mov_rbx,[rdi+BYTE] !56 # options = image->load_options
4889E2 ; COPY_RSP_to_RDX # arg2 = &fout 4889DA ; mov_rdx,rbx # save beginning of load_options
6A 00 ; PUSH !0 # arg5 = 0 48035F 30 ; add_rbx,[rdi+BYTE] !48 # go to the end of load_options
6A 07 ; PUSH !7 # to get 0x8000000000000003 we set the rightmost 3 bits # :loop_options [_start + 0x6F]
4159 ; POP_R9 # and then do right rotation by 1 4839D3 ; cmp_rbx,rdx # Check if we are done
49D1C9 ; ROR_R9 # arg4 = EFI_FILE_MODE_CREATE| EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ 74 14 ; je !loop_options_done # We are done
4D89E8 ; COPY_R13_to_R8 # arg3 = out 4883EB 02 ; sub_rbx, !2 # --options
4C89F1 ; COPY_R14_to_RCX # arg1 = rootdir 8A03 ; mov_al,[rbx] # *options
4883EC 20 ; SUBI8_RSP !32 # allocate shadow stack space for UEFI function 3C 20 ; cmp_al, !0x20 # if *options != ' '
FF51 08 ; CALL_RCX_Immediate8 !8 # rootdir->open() 75 F1 ; jne !loop_options # then continue looping
4C8B6C24 28 ; LOAD64_into_R13_from_Address_RSP_Immediate8 !40 # get fout C603 00 ; mov_[rbx], !0 # zero it
4883C3 02 ; add_rbx, !2 # ++options
53 ; push_rbx # push another argument onto stack
EB E7 ; jmp !loop_options # next argument
# :loop_options_done [_start + 0x88]
# Open file for reading # Open file for reading
52 ; PUSH_RDX # allocate stack for fin 4158 ; pop_r8 # arg3 = in
4889E2 ; COPY_RSP_to_RDX # arg2 = &fin 52 ; push_rdx # allocate stack for fin
6A 01 ; PUSH !1 # arg5 = EFI_FILE_READ_ONLY 4889E2 ; mov_rdx,rsp # arg2 = &fin
6A 01 ; PUSH !1 # prepare to set arg4 to EFI_FILE_MODE_READ 6A 01 ; push !1 # arg5 = EFI_FILE_READ_ONLY
4159 ; POP_R9 # arg4 = EFI_FILE_MODE_READ 6A 01 ; push !1 # prepare to set arg4 to EFI_FILE_MODE_READ
4D89E0 ; COPY_R12_to_R8 # arg3 = in 4159 ; pop_r9 # arg4 = EFI_FILE_MODE_READ
4C89F1 ; COPY_R14_to_RCX # arg1 = rootdir 4889F1 ; mov_rcx,rsi # arg1 = rootdir
4883EC 20 ; SUBI8_RSP !32 # allocate shadow stack space for UEFI function 4883EC 20 ; sub_rsp, !32 # allocate shadow stack space for UEFI function
FF51 08 ; CALL_RCX_Immediate8 !8 # rootdir->open() FF51 08 ; call_[rcx+BYTE] !8 # rootdir->open()
4C8B6424 28 ; LOAD64_into_R12_from_Address_RSP_Immediate8 !40 # get fin 4883C4 28 ; add_rsp, !40 # deallocate stack
415C ; pop_r12 # get fin
# Open file for writing
4158 ; pop_r8 # arg3 = out
4155 ; push_r13 # save root_device
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
4889F1 ; mov_rcx,rsi # 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
415D ; pop_r13 # get fout
# Save variables that are needed for cleanup
4156 ; push_r14 # save system->boot
4157 ; push_r15 # save image_handle
56 ; push_rsi # save rootdir
# Our flag for byte processing # Our flag for byte processing
6A FF ; PUSH !-1 6A FF ; push !-1
5B ; POP_RBX # rbx = -1 5B ; pop_rbx # rbx = -1
# temp storage for the sum # temp storage for the sum
31FF ; XOR_EDI_EDI # rdi = 0 31FF ; xor_edi,edi # rdi = 0
# :loop [_start+0xE2] # :loop [_start+0xCF]
# Read a byte # Read a byte
E8 6F000000 ; CALLI32 %read_byte E8 6F000000 ; call %read_byte
# process byte # process byte
E8 1C000000 ; CALLI32 %hex E8 1C000000 ; call %hex
# deal with -1 values # deal with -1 values
85C0 ; TEST_EAX_EAX 85C0 ; test_eax,eax
7C F2 ; JL8 !loop 7C F2 ; jl !loop
# deal with toggle # deal with toggle
85DB ; TEST_EBX_EBX # jump if ebx >= 0 85DB ; test_ebx,ebx # jump if ebx >= 0
7D 06 ; JGE8 !print 7D 06 ; jge !print
# process first byte of pair # process first byte of pair
89C7 ; COPY_EAX_to_EDI 89C7 ; mov_edi,eax
31DB ; XOR_EBX_EBX # rbx = 0 31DB ; xor_ebx,ebx # rbx = 0
EB E8 ; JMP8 !loop EB E8 ; jmp !loop
# process second byte of pair # process second byte of pair
# :print [_start+0xFA] # :print [_start+0xE7]
# update the sum and store in output # update the sum and store in output
C1E7 04 ; SHL_EDI_Immediate8 !4 C1E7 04 ; shl_edi, !4
01F8 ; ADD_EAX_EDI 01F8 ; add_eax,edi
# flip the toggle # flip the toggle
FFCB ; DEC_EBX # rbx = -1 FFCB ; dec_ebx # rbx = -1
E8 39000000 ; CALLI32 %write_byte E8 39000000 ; call %write_byte
EB DA ; JMP8 !loop EB DA ; jmp !loop
# :hex [_start+0x108] # :hex [_start+0xF5]
# Purge Comment Lines (#) # Purge Comment Lines (#)
3C 23 ; CMPI8_AL !35 3C 23 ; cmp_al, !35
74 1E ; JE8 !purge_comment 74 1E ; je !purge_comment
# Purge Comment Lines (;) # Purge Comment Lines (;)
3C 3B ; CMPI8_AL !59 3C 3B ; cmp_al, !59
74 1A ; JE8 !purge_comment 74 1A ; je !purge_comment
# deal all ascii less than '0' # deal all ascii less than '0'
3C 30 ; CMPI8_AL !48 3C 30 ; cmp_al, !48
7C 1F ; JL8 !ascii_other 7C 1F ; jl !ascii_other
# deal with 0-9 # deal with 0-9
3C 3A ; CMPI8_AL !58 3C 3A ; cmp_al, !58
7C 1F ; JL8 !ascii_num 7C 1F ; jl !ascii_num
# deal with all ascii less than 'A' # deal with all ascii less than 'A'
3C 41 ; CMPI8_AL !65 3C 41 ; cmp_al, !65
7C 17 ; JL8 !ascii_other 7C 17 ; jl !ascii_other
# deal with 'A'-'F' # deal with 'A'-'F'
3C 47 ; CMPI8_AL !71 3C 47 ; cmp_al, !71
7C 1C ; JL8 !ascii_high 7C 1C ; jl !ascii_high
# deal with all ascii less than 'a' # deal with all ascii less than 'a'
3C 61 ; CMPI8_AL !97 3C 61 ; cmp_al, !97
7C 0F ; JL8 !ascii_other 7C 0F ; jl !ascii_other
#deal with 'a'-'f' #deal with 'a'-'f'
3C 67 ; CMPI8_AL !103 3C 67 ; cmp_al, !103
7C 12 ; JL8 !ascii_low 7C 12 ; jl !ascii_low
# The rest that remains needs to be ignored # The rest that remains needs to be ignored
EB 09 ; JMP8 !ascii_other EB 09 ; jmp !ascii_other
# :purge_comment [_start+0x12A] # :purge_comment [_start+0x117]
# Read a byte # Read a byte
E8 27000000 ; CALLI32 %read_byte E8 27000000 ; call %read_byte
# Loop if not LF # Loop if not LF
3C 0A ; CMPI8_AL !10 3C 0A ; cmp_al, !10
75 F7 ; JNE8 !purge_comment 75 F7 ; jne !purge_comment
# Otherwise return -1 # Otherwise return -1
# :ascii_other [_start+0x133] # :ascii_other [_start+0x120]
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+0x137] # :ascii_num [_start+0x124]
2C 30 ; SUBI8_AL !48 2C 30 ; sub_al, !48
C3 ; RET C3 ; ret
# :ascii_low [_start+0x13A] # :ascii_low [_start+0x127]
2C 20 ; SUBI8_AL !32 # convert to uppercase 2C 20 ; sub_al, !32 # convert to uppercase
# :ascii_high [_start+0x13C] # :ascii_high [_start+0x129]
2C 37 ; SUBI8_AL !55 2C 37 ; sub_al, !55
C3 ; RET C3 ; ret
# Writes byte stored in al # Writes byte stored in al
# :write_byte [_start+0x13F] # :write_byte [_start+0x12C]
4C89E9 ; COPY_R13_to_RCX # arg1 = fout 4C89E9 ; mov_rcx,r13 # arg1 = fout
6A 01 ; PUSH !1 # size = 1 6A 01 ; push !1 # size = 1
4889E2 ; COPY_RSP_to_RDX # arg2 = &size 4889E2 ; mov_rdx,rsp # arg2 = &size
50 ; PUSH_RAX # allocate stack 50 ; push_rax # allocate stack
4989E0 ; COPY_RSP_to_R8 # arg3 = &output 4989E0 ; mov_r8,rsp # arg3 = &output
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
50 ; PUSH_RAX # allocate shadow stack space for UEFI function 50 ; push_rax # allocate shadow stack space for UEFI function
FF51 28 ; CALL_RCX_Immediate8 !40 # fout->write() FF51 28 ; call_[rcx+BYTE] !40 # fout->write()
4883C4 28 ; ADDI8_RSP !40 # deallocate stack 4883C4 28 ; add_rsp, !40 # deallocate stack
C3 ; RET # return C3 ; ret # return
# :read_byte [_start+0x156] # :read_byte [_start+0x143]
4C89E1 ; COPY_R12_to_RCX # arg1 = fin 4C89E1 ; mov_rcx,r12 # arg1 = fin
6A 01 ; PUSH !1 # size = 1 6A 01 ; push !1 # size = 1
4889E2 ; COPY_RSP_to_RDX # arg2 = &size 4889E2 ; mov_rdx,rsp # arg2 = &size
56 ; PUSH_RSI # allocate stack 31F6 ; xor_esi,esi # zero rsi
4989E0 ; COPY_RSP_to_R8 # arg3 = &input 56 ; push_rsi # allocate stack
50 ; PUSH_RAX # allocate shadow stack space for UEFI function 4989E0 ; mov_r8,rsp # arg3 = &input
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 20 ; CALL_RCX_Immediate8 !32 # fin->read() 50 ; push_rax # allocate shadow stack space for UEFI function
58 ; POP_RAX # deallocate stack FF51 20 ; call_[rcx+BYTE] !32 # fin->read()
58 ; POP_RAX # deallocate stack 58 ; pop_rax # deallocate stack
58 ; POP_RAX # deallocate stack 58 ; pop_rax # deallocate stack
58 ; POP_RAX # save input to rax 58 ; pop_rax # deallocate stack
5E ; POP_RSI # save size to rsi 58 ; pop_rax # save input to rax
5E ; pop_rsi # save size to rsi
# 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
74 01 ; JE8 !terminate # then we are done 74 01 ; je !terminate # then we are done
C3 ; RET # return C3 ; ret # return
# :terminate [_start+0x172] # :terminate [_start+0x161]
53 ; PUSH_RBX # allocate stack 58 ; pop_rax # remove last return address from stack
4C89E1 ; COPY_R12_to_RCX # arg1 = fin 5E ; pop_rsi # restore rootdir
FF51 10 ; CALL_RCX_Immediate8 !16 # fin->close() 415F ; pop_r15 # restore image_handle
4C89E9 ; COPY_R13_to_RCX # arg1 = fout 415E ; pop_r14 # restore system->boot
FF51 10 ; CALL_RCX_Immediate8 !16 # fout->close() 5B ; pop_rbx # restore root_device
4889EC ; COPY_RBP_to_RSP # restore stack 53 ; push_rbx # allocate stack
C3 ; RET # return to UEFI 4C89E1 ; mov_rcx,r12 # arg1 = fin
FF51 10 ; call_[rcx+BYTE] !16 # fin->close()
4C89E9 ; mov_rcx,r13 # arg1 = fout
FF51 10 ; call_[rcx+BYTE] !16 # fout->close()
4889F1 ; mov_rcx,rsi # arg1 = rootdir
FF51 10 ; call_[rcx+BYTE] !16 # rootdir->close()
4D89F8 ; mov_r8,r15 # arg3 = image_handle
488D15 3C000000 ; lea_rdx,[rip+DWORD] %SIMPLE_FS_PROTOCOL # guid = &SIMPLE_FS_PROTOCOL
4889D9 ; mov_rcx,rbx # arg1 = root_device
4D31C9 ; xor_r9,r9 # arg4 = NULL
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)
4D89F8 ; mov_r8,r15 # arg3 = image_handle
488D15 11000000 ; lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL
4C89C1 ; mov_rcx,r8 # arg1 = image_handle
4D31C9 ; xor_r9,r9 # arg4 = NULL
41FF96 20010000 ; call_[r14+DWORD] %288 # system->boot->close_protocol(image_handle, &guid, image_handle, 0)
4889EC ; mov_rsp,rbp # restore stack
C3 ; ret # return to UEFI
# Protocol GUIDs # Protocol GUIDs
# :LOADED_IMAGE_PROTOCOL [_start+0x183] # :LOADED_IMAGE_PROTOCOL [_start+0x1B1]
A1 31 1B 5B ; %0x5b1b31a1 A1 31 1B 5B ; %0x5b1b31a1
62 95 ; @0x9562 62 95 ; @0x9562
D2 11 ; @0x11d2 D2 11 ; @0x11d2
# :LOADED_IMAGE_PROTOCOL_8 [_start+0x18B]
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+0x193] # :SIMPLE_FS_PROTOCOL [_start+0x1C1]
22 5B 4E 96 ; %0x0964e5b22 22 5B 4E 96 ; %0x0964e5b22
59 64 ; @0x6459 59 64 ; @0x6459
D2 11 ; @0x11d2 D2 11 ; @0x11d2
# :SIMPLE_FS_PROTOCOL_8 [_start+0x19B]
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
# :PE32_end [_start+0x1A3] # :PE32_end [_start+0x1D1]

View File

@ -3,6 +3,9 @@
# #
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
# Some of the functions are deliberately inlined at the slight expense of
# binary size to avoid tricky jump calculations in hex0 code.
# DOS MZ header # DOS MZ header
4D 5A # Signature 4D 5A # Signature
00 00 # Number of bytes in the last page. 00 00 # Number of bytes in the last page.
@ -148,7 +151,8 @@ CF 03 00 00 ; SizeOfRawData
6A 00 ; push !0 # arg5 = NULL 6A 00 ; push !0 # arg5 = 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 18010000 ; call_[r14+DWORD] %280 # system->boot->open_protocol(image_handle, &guid, &image, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL) 41FF96 18010000 ; call_[r14+DWORD] %280 # system->boot->open_protocol(image_handle, &guid, &image, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
488B7C24 30 ; mov_rdi,[rsp+BYTE] !48 # save image 4883C4 30 ; add_rsp, !48 # deallocate stack
5F ; pop_rdi # get image
# Get root file system # Get root file system
4D89F9 ; mov_r9,r15 # arg4 = image_handle 4D89F9 ; mov_r9,r15 # arg4 = image_handle
@ -161,7 +165,8 @@ CF 03 00 00 ; SizeOfRawData
6A 00 ; push !0 # arg5 = NULL 6A 00 ; push !0 # arg5 = 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 18010000 ; call_[r14+DWORD] %280 # system->boot->open_protocol(root_device, &guid, &rootfs, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL) 41FF96 18010000 ; call_[r14+DWORD] %280 # system->boot->open_protocol(root_device, &guid, &rootfs, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
488B4C24 30 ; mov_rcx,[rsp+BYTE] !48 # get rootfs 4883C4 30 ; add_rsp, !48 # deallocate stack
59 ; pop rcx # get rootfs
# Get root directory # Get root directory
50 ; push_rax # allocate stack for rootdir 50 ; push_rax # allocate stack for rootdir
@ -178,18 +183,18 @@ CF 03 00 00 ; SizeOfRawData
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
6A 00 ; push !0 # Save end of arguments (NULL) onto stack 6A 00 ; push !0 # Save end of arguments (NULL) onto stack
# :loop_options # :loop_options [_start + 0x71]
4839D3 ; cmp_rbx,rdx # Check if we are done 4839D3 ; cmp_rbx,rdx # Check if we are done
74 14 ; je8 !loop_options_done # We are done 74 14 ; je8 !loop_options_done # We are done
4883EB 02 ; sub_rbx, !2 # --options 4883EB 02 ; sub_rbx, !2 # --options
8A03 ; mov_al,[rbx] # *options 8A03 ; mov_al,[rbx] # *options
3C 20 ; cmp_al, !0x20 # if *options != ' ' 3C 20 ; cmp_al, !0x20 # if *options != ' '
75 F1 ; jne8 !loop_options # then continue looping 75 F1 ; jne8 !loop_options # then continue looping
C603 00 ; mov_[rbx], !0 # zero it C603 00 ; mov_[rbx], !0 # zero it
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 ; jmp8 !loop_options # next argument EB E7 ; jmp8 !loop_options # next argument
# :loop_options_done # :loop_options_done [_start + 0x8A]
# Open file for reading # Open file for reading
4158 ; pop_r8 # arg3 = in 4158 ; pop_r8 # arg3 = in