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:
parent
9b9ff9ac52
commit
32deb0ffaf
|
@ -40,14 +40,14 @@ efi_status_t efi_main(efi_handle_t image_handle, struct efi_system_table *system
|
|||
*options = 0;
|
||||
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 */
|
||||
struct efi_file_protocol *fin;
|
||||
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;
|
||||
uint64_t size;
|
||||
|
||||
|
@ -115,6 +115,9 @@ not_comment:
|
|||
terminate:
|
||||
fin->close(fin);
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -377,15 +377,14 @@ efi_status_t efi_main(efi_handle_t image_handle, struct efi_system_table *system
|
|||
*options = 0;
|
||||
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 */
|
||||
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 */
|
||||
struct efi_file_protocol *input;
|
||||
rootdir->open(rootdir, &input, in, EFI_FILE_MODE_READ, EFI_FILE_READ_ONLY);
|
||||
|
||||
jump_table = NULL;
|
||||
Base_Address = 0x00600000;
|
||||
output = fout;
|
||||
|
|
|
@ -11,322 +11,340 @@
|
|||
# but we need to leave stack space
|
||||
# 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
|
||||
|
||||
|
||||
DEFINE ADD_EAX_EDI 01F8
|
||||
DEFINE ADDI8_RBX 4883C3
|
||||
DEFINE ADDI8_RSP 4883C4
|
||||
DEFINE CALLI32 E8
|
||||
DEFINE CALL_R14 41FFD6
|
||||
DEFINE CALL_RCX_Immediate8 FF51
|
||||
DEFINE CMPI8_AL 3C
|
||||
DEFINE COPY_EAX_to_EDI 89C7
|
||||
DEFINE COPY_RBX_to_R12 4989DC
|
||||
DEFINE COPY_RBX_to_R13 4989DD
|
||||
DEFINE COPY_RCX_to_RAX 4889C1
|
||||
DEFINE COPY_RSP_to_RBP 4889E5
|
||||
DEFINE COPY_RSP_to_RDX 4889E2
|
||||
DEFINE COPY_RSP_to_R8 4989E0
|
||||
DEFINE COPY_RBP_to_RSP 4889EC
|
||||
DEFINE COPY_R8_to_RSP 4989E0
|
||||
DEFINE COPY_RCX_to_R9 4989C9
|
||||
DEFINE COPY_R12_to_RCX 4C89E1
|
||||
DEFINE COPY_R12_to_R8 4D89E0
|
||||
DEFINE COPY_R13_to_RCX 4C89E9
|
||||
DEFINE COPY_R13_to_R8 4D89E8
|
||||
DEFINE COPY_R14_to_RCX 4C89F1
|
||||
DEFINE COPY_RCX_to_R15 4989CF
|
||||
DEFINE COPY_R15_to_R9 4D89F9
|
||||
DEFINE DEC_EBX FFCB
|
||||
DEFINE JE8 74
|
||||
DEFINE JGE8 7D
|
||||
DEFINE JL8 7C
|
||||
DEFINE JMP8 EB
|
||||
DEFINE JNE8 75
|
||||
DEFINE LOAD64_into_RBX_from_Address_RAX_Immediate8 488B58
|
||||
DEFINE LOAD64_into_RAX_from_Address_RSP_Immediate8 488B4424
|
||||
DEFINE LOAD64_into_RCX_from_Address_RSP_Immediate8 488B4C24
|
||||
DEFINE LOAD64_into_RCX_from_Address_RCX_Immediate8 488B49
|
||||
DEFINE LOAD64_into_R12_from_Address_RSP_Immediate8 4C8B6424
|
||||
DEFINE LOAD64_into_R13_from_Address_RSP_Immediate8 4C8B6C24
|
||||
DEFINE LOAD64_into_R14_from_Address_RDX_Immediate8 4C8B72
|
||||
DEFINE LOAD64_into_R14_from_Address_R14_Immediate32 4D8BB6
|
||||
DEFINE LOAD8_AL_from_Address_RBX 8A03
|
||||
DEFINE LOAD64_rel_RDX 488B15
|
||||
DEFINE POP_RAX 58
|
||||
DEFINE POP_RBX 5B
|
||||
DEFINE POP_RSI 5E
|
||||
DEFINE POP_R9 4159
|
||||
DEFINE POP_R14 415E
|
||||
DEFINE PUSH 6A
|
||||
DEFINE PUSH_RAX 50
|
||||
DEFINE PUSH_RBX 53
|
||||
DEFINE PUSH_RDX 52
|
||||
DEFINE PUSH_RSI 56
|
||||
DEFINE RET C3
|
||||
DEFINE ROR_R9 49D1C9
|
||||
DEFINE SHL_EDI_Immediate8 C1E7
|
||||
DEFINE STOREI8_into_Address_RBX C603
|
||||
DEFINE SUBI8_RSP 4883EC
|
||||
DEFINE SUBI8_AL 2C
|
||||
DEFINE XOR_EBX_EBX 31DB
|
||||
DEFINE XOR_EDI_EDI 31FF
|
||||
DEFINE TEST_EAX_EAX 85C0
|
||||
DEFINE TEST_EBX_EBX 85DB
|
||||
DEFINE TEST_ESI_ESI 85F6
|
||||
DEFINE add_eax,edi 01F8
|
||||
DEFINE add_rbx, 4883C3
|
||||
DEFINE add_rsp, 4883C4
|
||||
DEFINE add_rbx,[rdi+BYTE] 48035F
|
||||
DEFINE call E8
|
||||
DEFINE call_[rcx+BYTE] FF51
|
||||
DEFINE call_[r14+BYTE] 41FF56
|
||||
DEFINE call_[r14+DWORD] 41FF96
|
||||
DEFINE cmp_al, 3C
|
||||
DEFINE cmp_rbx,rdx 4839D3
|
||||
DEFINE dec_ebx FFCB
|
||||
DEFINE je 74
|
||||
DEFINE jge 7D
|
||||
DEFINE jl 7C
|
||||
DEFINE jmp EB
|
||||
DEFINE jne 75
|
||||
DEFINE lea_rdx,[rip+DWORD] 488D15
|
||||
DEFINE mov_edi,eax 89C7
|
||||
DEFINE mov_rbp,rsp 4889E5
|
||||
DEFINE mov_rcx,rbx 4889D9
|
||||
DEFINE mov_rcx,rsi 4889F1
|
||||
DEFINE mov_rcx,r8 4C89C1
|
||||
DEFINE mov_rcx,r9 4C89C9
|
||||
DEFINE mov_rcx,r12 4C89E1
|
||||
DEFINE mov_rcx,r13 4C89E9
|
||||
DEFINE mov_rdx,rbx 4889DA
|
||||
DEFINE mov_rdx,rsp 4889E2
|
||||
DEFINE mov_rsp,rbp 4889EC
|
||||
DEFINE mov_r8,rsp 4989E0
|
||||
DEFINE mov_r8,r15 4D89F8
|
||||
DEFINE mov_r9,r15 4D89F9
|
||||
DEFINE mov_r13,rcx 4989CD
|
||||
DEFINE mov_r15,rcx 4989CF
|
||||
DEFINE mov_al,[rbx] 8A03
|
||||
DEFINE mov_[rbx], C603
|
||||
DEFINE mov_rbx,[rdi+BYTE] 488B5F
|
||||
DEFINE mov_rcx,[rdi+BYTE] 488B4F
|
||||
DEFINE mov_r14,[rdx+BYTE] 4C8B72
|
||||
DEFINE pop_rax 58
|
||||
DEFINE pop_rbx 5B
|
||||
DEFINE pop_rcx 59
|
||||
DEFINE pop_rdi 5F
|
||||
DEFINE pop_rsi 5E
|
||||
DEFINE pop_r8 4158
|
||||
DEFINE pop_r9 4159
|
||||
DEFINE pop_r12 415C
|
||||
DEFINE pop_r13 415D
|
||||
DEFINE pop_r14 415E
|
||||
DEFINE pop_r15 415F
|
||||
DEFINE push 6A
|
||||
DEFINE push_rax 50
|
||||
DEFINE push_rbx 53
|
||||
DEFINE push_rdx 52
|
||||
DEFINE push_rsi 56
|
||||
DEFINE push_r13 4155
|
||||
DEFINE push_r14 4156
|
||||
DEFINE push_r15 4157
|
||||
DEFINE ret C3
|
||||
DEFINE ror_r9 49D1C9
|
||||
DEFINE shl_edi, C1E7
|
||||
DEFINE sub_al, 2C
|
||||
DEFINE sub_rbx, 4883EB
|
||||
DEFINE sub_rsp, 4883EC
|
||||
DEFINE test_eax,eax 85C0
|
||||
DEFINE test_ebx,ebx 85DB
|
||||
DEFINE test_esi,esi 85F6
|
||||
DEFINE xor_ebx,ebx 31DB
|
||||
DEFINE xor_edi,edi 31FF
|
||||
DEFINE xor_esi,esi 31F6
|
||||
DEFINE xor_r9,r9 4D31C9
|
||||
|
||||
# efi_main(void *image_handle, struct efi_system_table *system)
|
||||
:_start
|
||||
COPY_RSP_to_RBP # save stack pointer
|
||||
COPY_RCX_to_R15 # save image_handle
|
||||
LOAD64_into_R14_from_Address_RDX_Immediate8 !96 # system->boot
|
||||
LOAD64_into_R14_from_Address_R14_Immediate32 %280 # system->boot->open_protocol
|
||||
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_RCX_to_R9 # arg4 = image_handle
|
||||
# arg1 = ImageHandle (already set)
|
||||
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
|
||||
mov_r9,r15 # arg4 = image_handle
|
||||
lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL
|
||||
mov_rcx,r9 # arg1 = image_handle
|
||||
push_rax # allocate stack for image
|
||||
mov_r8,rsp # arg3 = &image
|
||||
push !1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
|
||||
push !0 # arg5 = NULL
|
||||
sub_rsp, !32 # allocate shadow stack space for UEFI function
|
||||
call_[r14+DWORD] %280 # system->boot->open_protocol(image_handle, &guid, &image, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
|
||||
add_rsp, !48 # deallocate stack
|
||||
pop_rdi # get image
|
||||
|
||||
# Get root file system
|
||||
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_RCX_Immediate8 !24 # arg1 = root_device = image->device
|
||||
SUBI8_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)
|
||||
LOAD64_into_RCX_from_Address_RSP_Immediate8 !64 # get rootfs
|
||||
mov_r9,r15 # arg4 = image_handle
|
||||
lea_rdx,[rip+DWORD] %SIMPLE_FS_PROTOCOL # guid = &SIMPLE_FS_PROTOCOL
|
||||
mov_rcx,[rdi+BYTE] !24 # arg1 = root_device = image->device
|
||||
mov_r13,rcx # save root_device
|
||||
push_rax # allocate stack for rootfs
|
||||
mov_r8,rsp # arg3 = &rootfs
|
||||
push !1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
|
||||
push !0 # arg5 = NULL
|
||||
sub_rsp, !32 # allocate shadow stack space for UEFI function
|
||||
call_[r14+DWORD] %280 # system->boot->open_protocol(root_device, &guid, &rootfs, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
|
||||
add_rsp, !48 # deallocate stack
|
||||
pop_rcx # get rootfs
|
||||
|
||||
# Get root directory
|
||||
PUSH_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_R14 # save rootdir
|
||||
push_rax # allocate stack for rootdir
|
||||
mov_rdx,rsp # arg2 = &rootdir
|
||||
push_rax # allocate shadow stack space for UEFI function
|
||||
push_rax # allocate shadow stack space for UEFI function
|
||||
call_[rcx+BYTE] !8 # rootfs->open_volume(rootfs, &rootdir)
|
||||
pop_rax # deallocate stack
|
||||
pop_rax # deallocate stack
|
||||
pop_rsi # get rootdir
|
||||
|
||||
# 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
|
||||
COPY_R13_to_R8 # arg3 = out
|
||||
COPY_R14_to_RCX # arg1 = rootdir
|
||||
SUBI8_RSP !32 # allocate shadow stack space for UEFI function
|
||||
CALL_RCX_Immediate8 !8 # rootdir->open()
|
||||
LOAD64_into_R13_from_Address_RSP_Immediate8 !40 # get fout
|
||||
# Push command line arguments onto stack
|
||||
mov_rbx,[rdi+BYTE] !56 # options = image->load_options
|
||||
mov_rdx,rbx # save beginning of load_options
|
||||
add_rbx,[rdi+BYTE] !48 # go to the end of load_options
|
||||
:loop_options
|
||||
cmp_rbx,rdx # Check if we are done
|
||||
je !loop_options_done # We are done
|
||||
sub_rbx, !2 # --options
|
||||
mov_al,[rbx] # *options
|
||||
cmp_al, !0x20 # if *options != ' '
|
||||
jne !loop_options # then continue looping
|
||||
mov_[rbx], !0 # zero it
|
||||
add_rbx, !2 # ++options
|
||||
push_rbx # push another argument onto stack
|
||||
jmp !loop_options # next argument
|
||||
:loop_options_done
|
||||
|
||||
# Open file for reading
|
||||
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
|
||||
COPY_R12_to_R8 # arg3 = in
|
||||
COPY_R14_to_RCX # arg1 = rootdir
|
||||
SUBI8_RSP !32 # allocate shadow stack space for UEFI function
|
||||
CALL_RCX_Immediate8 !8 # rootdir->open()
|
||||
LOAD64_into_R12_from_Address_RSP_Immediate8 !40 # get fin
|
||||
pop_r8 # arg3 = in
|
||||
push_rdx # allocate stack for fin
|
||||
mov_rdx,rsp # arg2 = &fin
|
||||
push !1 # arg5 = EFI_FILE_READ_ONLY
|
||||
push !1 # prepare to set arg4 to EFI_FILE_MODE_READ
|
||||
pop_r9 # arg4 = EFI_FILE_MODE_READ
|
||||
mov_rcx,rsi # arg1 = rootdir
|
||||
sub_rsp, !32 # allocate shadow stack space for UEFI function
|
||||
call_[rcx+BYTE] !8 # rootdir->open()
|
||||
add_rsp, !40 # deallocate stack
|
||||
pop_r12 # get fin
|
||||
|
||||
# Open file for writing
|
||||
pop_r8 # arg3 = out
|
||||
push_r13 # save root_device
|
||||
push_rdx # allocate stack for fout
|
||||
mov_rdx,rsp # arg2 = &fout
|
||||
push !0 # arg5 = 0
|
||||
push !7 # to get 0x8000000000000003 we set the rightmost 3 bits
|
||||
pop_r9 # and then do right rotation by 1
|
||||
ror_r9 # arg4 = EFI_FILE_MODE_CREATE| EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ
|
||||
mov_rcx,rsi # arg1 = rootdir
|
||||
sub_rsp, !32 # allocate shadow stack space for UEFI function
|
||||
call_[rcx+BYTE] !8 # rootdir->open()
|
||||
add_rsp, !40 # deallocate stack
|
||||
pop_r13 # get fout
|
||||
|
||||
# Save variables that are needed for cleanup
|
||||
push_r14 # save system->boot
|
||||
push_r15 # save image_handle
|
||||
push_rsi # save rootdir
|
||||
|
||||
# Our flag for byte processing
|
||||
PUSH !-1
|
||||
POP_RBX # rbx = -1
|
||||
push !-1
|
||||
pop_rbx # rbx = -1
|
||||
|
||||
# temp storage for the sum
|
||||
XOR_EDI_EDI # rdi = 0
|
||||
xor_edi,edi # rdi = 0
|
||||
|
||||
:loop
|
||||
# Read a byte
|
||||
CALLI32 %read_byte
|
||||
call %read_byte
|
||||
|
||||
# process byte
|
||||
CALLI32 %hex
|
||||
call %hex
|
||||
|
||||
# deal with -1 values
|
||||
TEST_EAX_EAX
|
||||
JL8 !loop
|
||||
test_eax,eax
|
||||
jl !loop
|
||||
|
||||
# deal with toggle
|
||||
TEST_EBX_EBX # jump if ebx >= 0
|
||||
JGE8 !print
|
||||
test_ebx,ebx # jump if ebx >= 0
|
||||
jge !print
|
||||
|
||||
# process first byte of pair
|
||||
COPY_EAX_to_EDI
|
||||
XOR_EBX_EBX # rbx = 0
|
||||
JMP8 !loop
|
||||
mov_edi,eax
|
||||
xor_ebx,ebx # rbx = 0
|
||||
jmp !loop
|
||||
|
||||
# process second byte of pair
|
||||
:print
|
||||
# update the sum and store in output
|
||||
SHL_EDI_Immediate8 !4
|
||||
ADD_EAX_EDI
|
||||
shl_edi, !4
|
||||
add_eax,edi
|
||||
|
||||
# flip the toggle
|
||||
DEC_EBX # rbx = -1
|
||||
dec_ebx # rbx = -1
|
||||
|
||||
CALLI32 %write_byte
|
||||
call %write_byte
|
||||
|
||||
JMP8 !loop
|
||||
jmp !loop
|
||||
|
||||
:hex
|
||||
# Purge Comment Lines (#)
|
||||
CMPI8_AL !35
|
||||
JE8 !purge_comment
|
||||
cmp_al, !35
|
||||
je !purge_comment
|
||||
|
||||
# Purge Comment Lines (;)
|
||||
CMPI8_AL !59
|
||||
JE8 !purge_comment
|
||||
cmp_al, !59
|
||||
je !purge_comment
|
||||
|
||||
# deal all ascii less than '0'
|
||||
CMPI8_AL !48
|
||||
JL8 !ascii_other
|
||||
cmp_al, !48
|
||||
jl !ascii_other
|
||||
|
||||
# deal with 0-9
|
||||
CMPI8_AL !58
|
||||
JL8 !ascii_num
|
||||
cmp_al, !58
|
||||
jl !ascii_num
|
||||
|
||||
# deal with all ascii less than 'A'
|
||||
CMPI8_AL !65
|
||||
JL8 !ascii_other
|
||||
cmp_al, !65
|
||||
jl !ascii_other
|
||||
|
||||
# deal with 'A'-'F'
|
||||
CMPI8_AL !71
|
||||
JL8 !ascii_high
|
||||
cmp_al, !71
|
||||
jl !ascii_high
|
||||
|
||||
# deal with all ascii less than 'a'
|
||||
CMPI8_AL !97
|
||||
JL8 !ascii_other
|
||||
cmp_al, !97
|
||||
jl !ascii_other
|
||||
|
||||
#deal with 'a'-'f'
|
||||
CMPI8_AL !103
|
||||
JL8 !ascii_low
|
||||
cmp_al, !103
|
||||
jl !ascii_low
|
||||
|
||||
# The rest that remains needs to be ignored
|
||||
JMP8 !ascii_other
|
||||
jmp !ascii_other
|
||||
|
||||
:purge_comment
|
||||
# Read a byte
|
||||
CALLI32 %read_byte
|
||||
call %read_byte
|
||||
|
||||
# Loop if not LF
|
||||
CMPI8_AL !10
|
||||
JNE8 !purge_comment
|
||||
cmp_al, !10
|
||||
jne !purge_comment
|
||||
|
||||
# Otherwise return -1
|
||||
|
||||
:ascii_other
|
||||
PUSH !-1
|
||||
POP_RAX # return = -1
|
||||
RET
|
||||
push !-1
|
||||
pop_rax # return = -1
|
||||
ret
|
||||
|
||||
:ascii_num
|
||||
SUBI8_AL !48
|
||||
RET
|
||||
sub_al, !48
|
||||
ret
|
||||
|
||||
:ascii_low
|
||||
SUBI8_AL !32 # convert to uppercase
|
||||
sub_al, !32 # convert to uppercase
|
||||
|
||||
:ascii_high
|
||||
SUBI8_AL !55
|
||||
RET
|
||||
sub_al, !55
|
||||
ret
|
||||
|
||||
# Writes byte stored in al
|
||||
:write_byte
|
||||
COPY_R13_to_RCX # arg1 = fout
|
||||
PUSH !1 # size = 1
|
||||
COPY_RSP_to_RDX # arg2 = &size
|
||||
PUSH_RAX # allocate stack
|
||||
COPY_RSP_to_R8 # arg3 = &output
|
||||
PUSH_RAX # allocate shadow stack space for UEFI function
|
||||
PUSH_RAX # allocate shadow stack space for UEFI function
|
||||
PUSH_RAX # allocate shadow stack space for UEFI function
|
||||
CALL_RCX_Immediate8 !40 # fout->write()
|
||||
ADDI8_RSP !40 # deallocate stack
|
||||
mov_rcx,r13 # arg1 = fout
|
||||
push !1 # size = 1
|
||||
mov_rdx,rsp # arg2 = &size
|
||||
push_rax # allocate stack
|
||||
mov_r8,rsp # arg3 = &output
|
||||
push_rax # allocate shadow stack space for UEFI function
|
||||
push_rax # allocate shadow stack space for UEFI function
|
||||
push_rax # allocate shadow stack space for UEFI function
|
||||
call_[rcx+BYTE] !40 # fout->write()
|
||||
add_rsp, !40 # deallocate stack
|
||||
|
||||
RET # return
|
||||
ret # return
|
||||
|
||||
:read_byte
|
||||
COPY_R12_to_RCX # arg1 = fin
|
||||
PUSH !1 # size = 1
|
||||
COPY_RSP_to_RDX # arg2 = &size
|
||||
PUSH_RSI # allocate stack
|
||||
COPY_RSP_to_R8 # arg3 = &input
|
||||
PUSH_RAX # allocate shadow stack space for UEFI function
|
||||
PUSH_RAX # allocate shadow stack space for UEFI function
|
||||
PUSH_RAX # allocate shadow stack space for UEFI function
|
||||
CALL_RCX_Immediate8 !32 # fin->read()
|
||||
POP_RAX # deallocate stack
|
||||
POP_RAX # deallocate stack
|
||||
POP_RAX # deallocate stack
|
||||
POP_RAX # save input to rax
|
||||
POP_RSI # save size to rsi
|
||||
mov_rcx,r12 # arg1 = fin
|
||||
push !1 # size = 1
|
||||
mov_rdx,rsp # arg2 = &size
|
||||
xor_esi,esi # zero rsi
|
||||
push_rsi # allocate stack
|
||||
mov_r8,rsp # arg3 = &input
|
||||
push_rax # allocate shadow stack space for UEFI function
|
||||
push_rax # allocate shadow stack space for UEFI function
|
||||
push_rax # allocate shadow stack space for UEFI function
|
||||
call_[rcx+BYTE] !32 # fin->read()
|
||||
pop_rax # deallocate stack
|
||||
pop_rax # deallocate stack
|
||||
pop_rax # deallocate stack
|
||||
pop_rax # save input to rax
|
||||
pop_rsi # save size to rsi
|
||||
|
||||
# If the file ended (0 bytes read) terminate
|
||||
TEST_ESI_ESI # if size = 0
|
||||
JE8 !terminate # then we are done
|
||||
test_esi,esi # if size = 0
|
||||
je !terminate # then we are done
|
||||
|
||||
RET # return
|
||||
ret # return
|
||||
|
||||
:terminate
|
||||
PUSH_RBX # allocate stack
|
||||
COPY_R12_to_RCX # arg1 = fin
|
||||
CALL_RCX_Immediate8 !16 # fin->close()
|
||||
COPY_R13_to_RCX # arg1 = fout
|
||||
CALL_RCX_Immediate8 !16 # fout->close()
|
||||
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
|
||||
|
||||
COPY_RBP_to_RSP # restore stack
|
||||
RET # return to UEFI
|
||||
push_rbx # allocate stack
|
||||
mov_rcx,r12 # arg1 = fin
|
||||
call_[rcx+BYTE] !16 # fin->close()
|
||||
mov_rcx,r13 # arg1 = fout
|
||||
call_[rcx+BYTE] !16 # fout->close()
|
||||
mov_rcx,rsi # arg1 = rootdir
|
||||
call_[rcx+BYTE] !16 # rootdir->close()
|
||||
|
||||
mov_r8,r15 # arg3 = image_handle
|
||||
lea_rdx,[rip+DWORD] %SIMPLE_FS_PROTOCOL # guid = &SIMPLE_FS_PROTOCOL
|
||||
mov_rcx,rbx # arg1 = root_device
|
||||
xor_r9,r9 # arg4 = NULL
|
||||
sub_rsp, !32 # allocate shadow stack space for UEFI function
|
||||
call_[r14+DWORD] %288 # system->boot->close_protocol(root_device, &guid, image_handle, 0)
|
||||
|
||||
mov_r8,r15 # arg3 = image_handle
|
||||
lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL
|
||||
mov_rcx,r8 # arg1 = image_handle
|
||||
xor_r9,r9 # arg4 = NULL
|
||||
call_[r14+DWORD] %288 # system->boot->close_protocol(image_handle, &guid, image_handle, 0)
|
||||
|
||||
mov_rsp,rbp # restore stack
|
||||
ret # return to UEFI
|
||||
|
||||
|
||||
# Protocol GUIDs
|
||||
|
@ -334,14 +352,12 @@ DEFINE TEST_ESI_ESI 85F6
|
|||
%0x5b1b31a1
|
||||
@0x9562
|
||||
@0x11d2
|
||||
:LOADED_IMAGE_PROTOCOL_8
|
||||
!0x8e !0x3f !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b
|
||||
|
||||
:SIMPLE_FS_PROTOCOL
|
||||
%0x0964e5b22
|
||||
@0x6459
|
||||
@0x11d2
|
||||
:SIMPLE_FS_PROTOCOL_8
|
||||
!0x8e !0x39 !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b
|
||||
|
||||
:PE32_end
|
||||
|
|
|
@ -11,14 +11,6 @@
|
|||
# but we need to leave stack space
|
||||
# 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
|
||||
.text
|
||||
|
||||
|
@ -27,97 +19,93 @@ _start:
|
|||
mov rbp, rsp # save stack pointer
|
||||
mov r15, rcx # save image_handle
|
||||
mov r14, [rdx+96] # system->boot
|
||||
mov r14, [r14+280] # system->boot->open_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
|
||||
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 0 # arg5 = NULL
|
||||
mov r9, rcx # arg4 = image_handle
|
||||
# arg1 = ImageHandle (already set)
|
||||
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)
|
||||
mov rax, [rsp+64] # get image
|
||||
mov rcx, rax # save 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
|
||||
call [r14+280] # system->boot->open_protocol(image_handle, &guid, &image, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
|
||||
add rsp, 48 # deallocate stack
|
||||
pop rdi # get image
|
||||
|
||||
# Get root file system
|
||||
mov r9, r15 # arg4 = image_handle
|
||||
lea rdx, [rip+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
|
||||
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 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
|
||||
call r14 # system->boot->open_protocol(root_device, &guid, &rootfs, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
|
||||
mov rcx, [rsp+64] # get rootfs
|
||||
call [r14+280] # system->boot->open_protocol(root_device, &guid, &rootfs, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
|
||||
add rsp, 48 # deallocate stack
|
||||
pop rcx # get rootfs
|
||||
|
||||
# Get root directory
|
||||
push rdx # allocate stack for rootdir
|
||||
push rax # allocate stack for rootdir
|
||||
mov rdx, rsp # arg2 = &rootdir
|
||||
push rax # allocate shadow stack space for UEFI function
|
||||
push rax # allocate shadow stack space for UEFI function
|
||||
call [rcx+8] # rootfs->open_volume(rootfs, &rootdir)
|
||||
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
|
||||
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 r8, r13 # arg3 = out
|
||||
mov rcx, r14 # arg1 = rootdir
|
||||
mov rcx, rsi # arg1 = rootdir
|
||||
sub rsp, 32 # allocate shadow stack space for UEFI function
|
||||
call [rcx+8] # rootdir->open()
|
||||
mov r13, [rsp+40] # get fout
|
||||
add rsp, 40 # deallocate stack
|
||||
pop r13 # get fout
|
||||
|
||||
# Open file for reading
|
||||
push rdx # allocate stack for fin
|
||||
mov rdx, rsp # arg2 = &fin
|
||||
push 1 # arg5 = EFI_FILE_READ_ONLY
|
||||
push 1 # prepare to set arg4 to EFI_FILE_MODE_READ
|
||||
pop r9 # arg4 = EFI_FILE_MODE_READ
|
||||
mov 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
|
||||
# Save variables that are needed for cleanup
|
||||
push r14 # save system->boot
|
||||
push r15 # save image_handle
|
||||
push rsi # save rootdir
|
||||
|
||||
# Our flag for byte processing
|
||||
push -1
|
||||
|
@ -240,6 +228,7 @@ read_byte:
|
|||
mov rcx, r12 # arg1 = fin
|
||||
push 1 # size = 1
|
||||
mov rdx, rsp # arg2 = &size
|
||||
xor esi, esi # zero rsi
|
||||
push rsi # allocate stack
|
||||
mov r8, rsp # arg3 = &input
|
||||
push rax # allocate shadow stack space for UEFI function
|
||||
|
@ -259,11 +248,33 @@ read_byte:
|
|||
ret # return
|
||||
|
||||
terminate:
|
||||
pop rax # remove last return address from stack
|
||||
pop rsi # restore rootdir
|
||||
pop r15 # restore image_handle
|
||||
pop r14 # restore system->boot
|
||||
pop rbx # restore root_device
|
||||
|
||||
push rbx # allocate stack
|
||||
mov rcx, r12 # arg1 = fin
|
||||
call [rcx+16] # fin->close()
|
||||
mov rcx, r13 # arg1 = fout
|
||||
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
|
||||
mov rsp, rbp # restore stack
|
||||
ret # return to UEFI
|
||||
|
|
|
@ -11,260 +11,269 @@
|
|||
# but we need to leave stack space
|
||||
# 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)
|
||||
:_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
|
||||
4D8BB6 18010000 ; LOAD64_into_R14_from_Address_R14_Immediate32 %280 # system->boot->open_protocol
|
||||
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
|
||||
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
|
||||
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
|
||||
4D89F9 ; mov_r9,r15 # arg4 = image_handle
|
||||
488D15 %LOADED_IMAGE_PROTOCOL ; lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL
|
||||
4C89C9 ; mov_rcx,r9 # arg1 = image_handle
|
||||
50 ; push_rax # allocate stack for image
|
||||
4989E0 ; mov_r8,rsp # arg3 = &image
|
||||
6A 01 ; push !1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
|
||||
6A 00 ; push !0 # arg5 = NULL
|
||||
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)
|
||||
4883C4 30 ; add_rsp, !48 # deallocate stack
|
||||
5F ; pop_rdi # get 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
|
||||
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
|
||||
4D89F9 ; mov_r9,r15 # arg4 = image_handle
|
||||
488D15 %SIMPLE_FS_PROTOCOL ; lea_rdx,[rip+DWORD] %SIMPLE_FS_PROTOCOL # guid = &SIMPLE_FS_PROTOCOL
|
||||
488B4F 18 ; mov_rcx,[rdi+BYTE] !24 # arg1 = root_device = image->device
|
||||
4989CD ; mov_r13,rcx # save root_device
|
||||
50 ; push_rax # allocate stack for rootfs
|
||||
4989E0 ; mov_r8,rsp # arg3 = &rootfs
|
||||
6A 01 ; push !1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
|
||||
6A 00 ; push !0 # arg5 = NULL
|
||||
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)
|
||||
4883C4 30 ; add_rsp, !48 # deallocate stack
|
||||
59 ; pop rcx # 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
|
||||
415E ; POP_R14 # save rootdir
|
||||
50 ; push_rax # 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
|
||||
5E ; pop_rsi # save rootdir
|
||||
|
||||
# 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
|
||||
4D89E8 ; COPY_R13_to_R8 # arg3 = out
|
||||
4C89F1 ; COPY_R14_to_RCX # arg1 = rootdir
|
||||
4883EC 20 ; SUBI8_RSP !32 # allocate shadow stack space for UEFI function
|
||||
FF51 08 ; CALL_RCX_Immediate8 !8 # rootdir->open()
|
||||
4C8B6C24 28 ; LOAD64_into_R13_from_Address_RSP_Immediate8 !40 # get fout
|
||||
# Push command line arguments onto stack
|
||||
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
|
||||
:loop_options
|
||||
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
|
||||
|
||||
# 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
|
||||
4D89E0 ; COPY_R12_to_R8 # arg3 = in
|
||||
4C89F1 ; COPY_R14_to_RCX # arg1 = rootdir
|
||||
4883EC 20 ; SUBI8_RSP !32 # allocate shadow stack space for UEFI function
|
||||
FF51 08 ; CALL_RCX_Immediate8 !8 # rootdir->open()
|
||||
4C8B6424 28 ; LOAD64_into_R12_from_Address_RSP_Immediate8 !40 # get fin
|
||||
4158 ; pop_r8 # arg3 = in
|
||||
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
|
||||
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
|
||||
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
|
||||
6A FF ; PUSH !-1
|
||||
5B ; POP_RBX # rbx = -1
|
||||
6A FF ; push !-1
|
||||
5B ; pop_rbx # rbx = -1
|
||||
|
||||
# temp storage for the sum
|
||||
31FF ; XOR_EDI_EDI # rdi = 0
|
||||
31FF ; xor_edi,edi # rdi = 0
|
||||
|
||||
:loop
|
||||
# Read a byte
|
||||
E8 %read_byte ; CALLI32 %read_byte
|
||||
E8 %read_byte ; call %read_byte
|
||||
|
||||
# process byte
|
||||
E8 %hex ; CALLI32 %hex
|
||||
E8 %hex ; call %hex
|
||||
|
||||
# deal with -1 values
|
||||
85C0 ; TEST_EAX_EAX
|
||||
7C !loop ; JL8 !loop
|
||||
85C0 ; test_eax,eax
|
||||
7C !loop ; jl !loop
|
||||
|
||||
# deal with toggle
|
||||
85DB ; TEST_EBX_EBX # jump if ebx >= 0
|
||||
7D !print ; JGE8 !print
|
||||
85DB ; test_ebx,ebx # jump if ebx >= 0
|
||||
7D !print ; jge !print
|
||||
|
||||
# process first byte of pair
|
||||
89C7 ; COPY_EAX_to_EDI
|
||||
31DB ; XOR_EBX_EBX # rbx = 0
|
||||
EB !loop ; JMP8 !loop
|
||||
89C7 ; mov_edi,eax
|
||||
31DB ; xor_ebx,ebx # rbx = 0
|
||||
EB !loop ; jmp !loop
|
||||
|
||||
# process second byte of pair
|
||||
:print
|
||||
# update the sum and store in output
|
||||
C1E7 04 ; SHL_EDI_Immediate8 !4
|
||||
01F8 ; ADD_EAX_EDI
|
||||
C1E7 04 ; shl_edi, !4
|
||||
01F8 ; add_eax,edi
|
||||
|
||||
# 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
|
||||
# Purge Comment Lines (#)
|
||||
3C 23 ; CMPI8_AL !35
|
||||
74 !purge_comment ; JE8 !purge_comment
|
||||
3C 23 ; cmp_al, !35
|
||||
74 !purge_comment ; je !purge_comment
|
||||
|
||||
# Purge Comment Lines (;)
|
||||
3C 3B ; CMPI8_AL !59
|
||||
74 !purge_comment ; JE8 !purge_comment
|
||||
3C 3B ; cmp_al, !59
|
||||
74 !purge_comment ; je !purge_comment
|
||||
|
||||
# deal all ascii less than '0'
|
||||
3C 30 ; CMPI8_AL !48
|
||||
7C !ascii_other ; JL8 !ascii_other
|
||||
3C 30 ; cmp_al, !48
|
||||
7C !ascii_other ; jl !ascii_other
|
||||
|
||||
# deal with 0-9
|
||||
3C 3A ; CMPI8_AL !58
|
||||
7C !ascii_num ; JL8 !ascii_num
|
||||
3C 3A ; cmp_al, !58
|
||||
7C !ascii_num ; jl !ascii_num
|
||||
|
||||
# deal with all ascii less than 'A'
|
||||
3C 41 ; CMPI8_AL !65
|
||||
7C !ascii_other ; JL8 !ascii_other
|
||||
3C 41 ; cmp_al, !65
|
||||
7C !ascii_other ; jl !ascii_other
|
||||
|
||||
# deal with 'A'-'F'
|
||||
3C 47 ; CMPI8_AL !71
|
||||
7C !ascii_high ; JL8 !ascii_high
|
||||
3C 47 ; cmp_al, !71
|
||||
7C !ascii_high ; jl !ascii_high
|
||||
|
||||
# deal with all ascii less than 'a'
|
||||
3C 61 ; CMPI8_AL !97
|
||||
7C !ascii_other ; JL8 !ascii_other
|
||||
3C 61 ; cmp_al, !97
|
||||
7C !ascii_other ; jl !ascii_other
|
||||
|
||||
#deal with 'a'-'f'
|
||||
3C 67 ; CMPI8_AL !103
|
||||
7C !ascii_low ; JL8 !ascii_low
|
||||
3C 67 ; cmp_al, !103
|
||||
7C !ascii_low ; jl !ascii_low
|
||||
|
||||
# The rest that remains needs to be ignored
|
||||
EB !ascii_other ; JMP8 !ascii_other
|
||||
EB !ascii_other ; jmp !ascii_other
|
||||
|
||||
:purge_comment
|
||||
# Read a byte
|
||||
E8 %read_byte ; CALLI32 %read_byte
|
||||
E8 %read_byte ; call %read_byte
|
||||
|
||||
# Loop if not LF
|
||||
3C 0A ; CMPI8_AL !10
|
||||
75 !purge_comment ; JNE8 !purge_comment
|
||||
3C 0A ; cmp_al, !10
|
||||
75 !purge_comment ; jne !purge_comment
|
||||
|
||||
# Otherwise return -1
|
||||
|
||||
:ascii_other
|
||||
6A FF ; PUSH !-1
|
||||
58 ; POP_RAX # return = -1
|
||||
C3 ; RET
|
||||
6A FF ; push !-1
|
||||
58 ; pop_rax # return = -1
|
||||
C3 ; ret
|
||||
|
||||
:ascii_num
|
||||
2C 30 ; SUBI8_AL !48
|
||||
C3 ; RET
|
||||
2C 30 ; sub_al, !48
|
||||
C3 ; ret
|
||||
|
||||
:ascii_low
|
||||
2C 20 ; SUBI8_AL !32 # convert to uppercase
|
||||
2C 20 ; sub_al, !32 # convert to uppercase
|
||||
|
||||
:ascii_high
|
||||
2C 37 ; SUBI8_AL !55
|
||||
C3 ; RET
|
||||
2C 37 ; sub_al, !55
|
||||
C3 ; ret
|
||||
|
||||
# Writes byte stored in al
|
||||
:write_byte
|
||||
4C89E9 ; COPY_R13_to_RCX # arg1 = fout
|
||||
6A 01 ; PUSH !1 # size = 1
|
||||
4889E2 ; COPY_RSP_to_RDX # arg2 = &size
|
||||
50 ; PUSH_RAX # allocate stack
|
||||
4989E0 ; COPY_RSP_to_R8 # 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
|
||||
FF51 28 ; CALL_RCX_Immediate8 !40 # fout->write()
|
||||
4883C4 28 ; ADDI8_RSP !40 # deallocate stack
|
||||
4C89E9 ; mov_rcx,r13 # arg1 = fout
|
||||
6A 01 ; push !1 # size = 1
|
||||
4889E2 ; mov_rdx,rsp # arg2 = &size
|
||||
50 ; push_rax # allocate stack
|
||||
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
|
||||
FF51 28 ; call_[rcx+BYTE] !40 # fout->write()
|
||||
4883C4 28 ; add_rsp, !40 # deallocate stack
|
||||
|
||||
C3 ; RET # return
|
||||
C3 ; ret # return
|
||||
|
||||
:read_byte
|
||||
4C89E1 ; COPY_R12_to_RCX # arg1 = fin
|
||||
6A 01 ; PUSH !1 # size = 1
|
||||
4889E2 ; COPY_RSP_to_RDX # arg2 = &size
|
||||
56 ; PUSH_RSI # allocate stack
|
||||
4989E0 ; COPY_RSP_to_R8 # 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
|
||||
FF51 20 ; CALL_RCX_Immediate8 !32 # fin->read()
|
||||
58 ; POP_RAX # deallocate stack
|
||||
58 ; POP_RAX # deallocate stack
|
||||
58 ; POP_RAX # deallocate stack
|
||||
58 ; POP_RAX # save input to rax
|
||||
5E ; POP_RSI # save size to rsi
|
||||
4C89E1 ; mov_rcx,r12 # arg1 = fin
|
||||
6A 01 ; push !1 # size = 1
|
||||
4889E2 ; mov_rdx,rsp # arg2 = &size
|
||||
31F6 ; xor_esi,esi # zero rsi
|
||||
56 ; push_rsi # allocate stack
|
||||
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
|
||||
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 # save input to rax
|
||||
5E ; pop_rsi # save size to rsi
|
||||
|
||||
# If the file ended (0 bytes read) terminate
|
||||
85F6 ; TEST_ESI_ESI # if size = 0
|
||||
74 !terminate ; JE8 !terminate # then we are done
|
||||
85F6 ; test_esi,esi # if size = 0
|
||||
74 !terminate ; je !terminate # then we are done
|
||||
|
||||
C3 ; RET # return
|
||||
C3 ; ret # return
|
||||
|
||||
:terminate
|
||||
53 ; PUSH_RBX # allocate stack
|
||||
4C89E1 ; COPY_R12_to_RCX # arg1 = fin
|
||||
FF51 10 ; CALL_RCX_Immediate8 !16 # fin->close()
|
||||
4C89E9 ; COPY_R13_to_RCX # arg1 = fout
|
||||
FF51 10 ; CALL_RCX_Immediate8 !16 # fout->close()
|
||||
58 ; pop_rax # remove last return address from stack
|
||||
5E ; pop_rsi # restore rootdir
|
||||
415F ; pop_r15 # restore image_handle
|
||||
415E ; pop_r14 # restore system->boot
|
||||
5B ; pop_rbx # restore root_device
|
||||
|
||||
4889EC ; COPY_RBP_to_RSP # restore stack
|
||||
C3 ; RET # return to UEFI
|
||||
53 ; push_rbx # allocate stack
|
||||
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
|
||||
|
@ -272,14 +281,12 @@
|
|||
A1 31 1B 5B ; %0x5b1b31a1
|
||||
62 95 ; @0x9562
|
||||
D2 11 ; @0x11d2
|
||||
:LOADED_IMAGE_PROTOCOL_8
|
||||
8E 3F 00 A0 C9 69 72 3B ; !0x8e !0x3f !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b
|
||||
|
||||
:SIMPLE_FS_PROTOCOL
|
||||
22 5B 4E 96 ; %0x0964e5b22
|
||||
59 64 ; @0x6459
|
||||
D2 11 ; @0x11d2
|
||||
:SIMPLE_FS_PROTOCOL_8
|
||||
8E 39 00 A0 C9 69 72 3B ; !0x8e !0x39 !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b
|
||||
|
||||
:PE32_end
|
||||
|
|
|
@ -52,8 +52,6 @@ DEFINE mov_rax,[rax] 488B00
|
|||
DEFINE mov_[rbx], C603
|
||||
DEFINE mov_rbx,[rdi+BYTE] 488B5F
|
||||
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_[rax],r13 4C8928
|
||||
DEFINE not_r15 49F7D7
|
||||
|
@ -88,6 +86,8 @@ DEFINE xor_edx,edx 31D2
|
|||
DEFINE xor_esi,esi 31F6
|
||||
DEFINE xor_r9,r9 4D31C9
|
||||
|
||||
# Some of the functions are deliberately inlined at the slight expense of
|
||||
# binary size to avoid tricky jump calculations in hex0 code.
|
||||
|
||||
# efi_main(void *image_handle, struct efi_system_table *system)
|
||||
:_start
|
||||
|
@ -105,7 +105,8 @@ DEFINE xor_r9,r9 4D31C9
|
|||
push !0 # arg5 = NULL
|
||||
sub_rsp, !32 # allocate shadow stack space for UEFI function
|
||||
call_[r14+DWORD] %280 # system->boot->open_protocol(image_handle, &guid, &image, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
|
||||
mov_rdi,[rsp+BYTE] !48 # save image
|
||||
add_rsp, !48 # deallocate stack
|
||||
pop_rdi # get image
|
||||
|
||||
# Get root file system
|
||||
mov_r9,r15 # arg4 = image_handle
|
||||
|
@ -118,7 +119,8 @@ DEFINE xor_r9,r9 4D31C9
|
|||
push !0 # arg5 = NULL
|
||||
sub_rsp, !32 # allocate shadow stack space for UEFI function
|
||||
call_[r14+DWORD] %280 # system->boot->open_protocol(root_device, &guid, &rootfs, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
|
||||
mov_rcx,[rsp+BYTE] !48 # get rootfs
|
||||
add_rsp, !48 # deallocate stack
|
||||
pop_rcx # get rootfs
|
||||
|
||||
# Get root directory
|
||||
push_rax # allocate stack for rootdir
|
||||
|
|
|
@ -25,7 +25,8 @@ _start:
|
|||
push 0 # arg5 = NULL
|
||||
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)
|
||||
mov rdi, [rsp+48] # save image
|
||||
add rsp, 48 # deallocate stack
|
||||
pop rdi # get image
|
||||
|
||||
# Get root file system
|
||||
mov r9, r15 # arg4 = image_handle
|
||||
|
@ -38,7 +39,8 @@ _start:
|
|||
push 0 # arg5 = NULL
|
||||
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)
|
||||
mov rcx, [rsp+48] # get rootfs
|
||||
add rsp, 48 # deallocate stack
|
||||
pop rcx # get rootfs
|
||||
|
||||
# Get root directory
|
||||
push rax # allocate stack for rootdir
|
||||
|
@ -181,7 +183,7 @@ First_pass_pointer:
|
|||
jmp First_pass # Loop again
|
||||
|
||||
First_pass_done:
|
||||
RET
|
||||
ret
|
||||
|
||||
hex:
|
||||
# deal with EOF
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
#
|
||||
# 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)
|
||||
:_start
|
||||
|
@ -20,7 +22,8 @@
|
|||
6A 00 ; push !0 # arg5 = NULL
|
||||
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)
|
||||
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
|
||||
4D89F9 ; mov_r9,r15 # arg4 = image_handle
|
||||
|
@ -33,7 +36,8 @@
|
|||
6A 00 ; push !0 # arg5 = NULL
|
||||
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)
|
||||
488B4C24 30 ; mov_rcx,[rsp+BYTE] !48 # get rootfs
|
||||
4883C4 30 ; add_rsp, !48 # deallocate stack
|
||||
59 ; pop rcx # get rootfs
|
||||
|
||||
# Get root directory
|
||||
50 ; push_rax # allocate stack for rootdir
|
||||
|
|
394
amd64/hex0.hex0
394
amd64/hex0.hex0
|
@ -11,14 +11,6 @@
|
|||
# but we need to leave stack space
|
||||
# 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
|
||||
4D 5A # Signature
|
||||
00 00 # Number of bytes in the last page.
|
||||
|
@ -136,9 +128,9 @@ F0 00 # SizeOfOptionalHeader
|
|||
# [0x148]
|
||||
# Start of section headers
|
||||
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
|
||||
A3 01 00 00 ; SizeOfRawData
|
||||
D1 01 00 00 ; SizeOfRawData
|
||||
70 01 00 00 ; PointerToRawData
|
||||
00 00 00 00 ; PointerToRelocations
|
||||
00 00 00 00 ; PointerToLinenumbers
|
||||
|
@ -150,266 +142,280 @@ A3 01 00 00 ; SizeOfRawData
|
|||
# Our actual program
|
||||
# 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
|
||||
4D8BB6 18010000 ; LOAD64_into_R14_from_Address_R14_Immediate32 %280 # system->boot->open_protocol
|
||||
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 6F010000 ; LOAD64_rel_RDX %LOADED_IMAGE_PROTOCOL_8 # EFI_LOADED_IMAGE_PROTOCOL_GUID (last 64 bits)
|
||||
# [_start+0x1C]
|
||||
52 ; PUSH_RDX # push last 64 bits onto stack
|
||||
488B15 5F010000 ; LOAD64_rel_RDX %LOADED_IMAGE_PROTOCOL # EFI_LOADED_IMAGE_PROTOCOL_GUID (first 64 bits)
|
||||
# [_start+0x24]
|
||||
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
|
||||
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
|
||||
4D89F9 ; mov_r9,r15 # arg4 = image_handle
|
||||
488D15 9D010000 ; lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL
|
||||
4C89C9 ; mov_rcx,r9 # arg1 = image_handle
|
||||
50 ; push_rax # allocate stack for image
|
||||
4989E0 ; mov_r8,rsp # arg3 = &image
|
||||
6A 01 ; push !1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
|
||||
6A 00 ; push !0 # arg5 = NULL
|
||||
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)
|
||||
4883C4 30 ; add_rsp, !48 # deallocate stack
|
||||
5F ; pop_rdi # get image
|
||||
|
||||
# Command line args
|
||||
488B58 38 ; LOAD64_into_RBX_from_Address_RAX_Immediate8 !56 # options = image->load_options
|
||||
|
||||
# :loop_options1 [_start+0x42] # Skip application name
|
||||
4883C3 02 ; ADDI8_RBX !2 # ++options
|
||||
8A03 ; LOAD8_AL_from_Address_RBX # *options
|
||||
3C 20 ; CMPI8_AL !0x20 # if *options != ' '
|
||||
75 F6 ; JNE8 !loop_options1 # then jump
|
||||
|
||||
4883C3 02 ; ADDI8_RBX !2 # ++options
|
||||
4989DC ; COPY_RBX_to_R12 # save input file
|
||||
|
||||
# :loop_options2 [_start+0x53] # Skip argv[1]
|
||||
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 file system
|
||||
4D89F9 ; mov_r9,r15 # arg4 = image_handle
|
||||
488D15 88010000 ; lea_rdx,[rip+DWORD] %SIMPLE_FS_PROTOCOL # guid = &SIMPLE_FS_PROTOCOL
|
||||
488B4F 18 ; mov_rcx,[rdi+BYTE] !24 # arg1 = root_device = image->device
|
||||
4989CD ; mov_r13,rcx # save root_device
|
||||
50 ; push_rax # allocate stack for rootfs
|
||||
4989E0 ; mov_r8,rsp # arg3 = &rootfs
|
||||
6A 01 ; push !1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
|
||||
6A 00 ; push !0 # arg5 = NULL
|
||||
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)
|
||||
4883C4 30 ; add_rsp, !48 # deallocate stack
|
||||
59 ; pop rcx # 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
|
||||
415E ; POP_R14 # save rootdir
|
||||
50 ; push_rax # 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
|
||||
5E ; pop_rsi # save rootdir
|
||||
|
||||
# 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
|
||||
4D89E8 ; COPY_R13_to_R8 # arg3 = out
|
||||
4C89F1 ; COPY_R14_to_RCX # arg1 = rootdir
|
||||
4883EC 20 ; SUBI8_RSP !32 # allocate shadow stack space for UEFI function
|
||||
FF51 08 ; CALL_RCX_Immediate8 !8 # rootdir->open()
|
||||
4C8B6C24 28 ; LOAD64_into_R13_from_Address_RSP_Immediate8 !40 # get fout
|
||||
# Push command line arguments onto stack
|
||||
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
|
||||
# :loop_options [_start + 0x6F]
|
||||
4839D3 ; cmp_rbx,rdx # Check if we are done
|
||||
74 14 ; 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 F1 ; 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 E7 ; jmp !loop_options # next argument
|
||||
# :loop_options_done [_start + 0x88]
|
||||
|
||||
# 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
|
||||
4D89E0 ; COPY_R12_to_R8 # arg3 = in
|
||||
4C89F1 ; COPY_R14_to_RCX # arg1 = rootdir
|
||||
4883EC 20 ; SUBI8_RSP !32 # allocate shadow stack space for UEFI function
|
||||
FF51 08 ; CALL_RCX_Immediate8 !8 # rootdir->open()
|
||||
4C8B6424 28 ; LOAD64_into_R12_from_Address_RSP_Immediate8 !40 # get fin
|
||||
4158 ; pop_r8 # arg3 = in
|
||||
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
|
||||
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
|
||||
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
|
||||
6A FF ; PUSH !-1
|
||||
5B ; POP_RBX # rbx = -1
|
||||
6A FF ; push !-1
|
||||
5B ; pop_rbx # rbx = -1
|
||||
|
||||
# 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
|
||||
E8 6F000000 ; CALLI32 %read_byte
|
||||
E8 6F000000 ; call %read_byte
|
||||
|
||||
# process byte
|
||||
E8 1C000000 ; CALLI32 %hex
|
||||
E8 1C000000 ; call %hex
|
||||
|
||||
# deal with -1 values
|
||||
85C0 ; TEST_EAX_EAX
|
||||
7C F2 ; JL8 !loop
|
||||
85C0 ; test_eax,eax
|
||||
7C F2 ; jl !loop
|
||||
|
||||
# deal with toggle
|
||||
85DB ; TEST_EBX_EBX # jump if ebx >= 0
|
||||
7D 06 ; JGE8 !print
|
||||
85DB ; test_ebx,ebx # jump if ebx >= 0
|
||||
7D 06 ; jge !print
|
||||
|
||||
# process first byte of pair
|
||||
89C7 ; COPY_EAX_to_EDI
|
||||
31DB ; XOR_EBX_EBX # rbx = 0
|
||||
EB E8 ; JMP8 !loop
|
||||
89C7 ; mov_edi,eax
|
||||
31DB ; xor_ebx,ebx # rbx = 0
|
||||
EB E8 ; jmp !loop
|
||||
|
||||
# process second byte of pair
|
||||
# :print [_start+0xFA]
|
||||
# :print [_start+0xE7]
|
||||
# update the sum and store in output
|
||||
C1E7 04 ; SHL_EDI_Immediate8 !4
|
||||
01F8 ; ADD_EAX_EDI
|
||||
C1E7 04 ; shl_edi, !4
|
||||
01F8 ; add_eax,edi
|
||||
|
||||
# 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 (#)
|
||||
3C 23 ; CMPI8_AL !35
|
||||
74 1E ; JE8 !purge_comment
|
||||
3C 23 ; cmp_al, !35
|
||||
74 1E ; je !purge_comment
|
||||
|
||||
# Purge Comment Lines (;)
|
||||
3C 3B ; CMPI8_AL !59
|
||||
74 1A ; JE8 !purge_comment
|
||||
3C 3B ; cmp_al, !59
|
||||
74 1A ; je !purge_comment
|
||||
|
||||
# deal all ascii less than '0'
|
||||
3C 30 ; CMPI8_AL !48
|
||||
7C 1F ; JL8 !ascii_other
|
||||
3C 30 ; cmp_al, !48
|
||||
7C 1F ; jl !ascii_other
|
||||
|
||||
# deal with 0-9
|
||||
3C 3A ; CMPI8_AL !58
|
||||
7C 1F ; JL8 !ascii_num
|
||||
3C 3A ; cmp_al, !58
|
||||
7C 1F ; jl !ascii_num
|
||||
|
||||
# deal with all ascii less than 'A'
|
||||
3C 41 ; CMPI8_AL !65
|
||||
7C 17 ; JL8 !ascii_other
|
||||
3C 41 ; cmp_al, !65
|
||||
7C 17 ; jl !ascii_other
|
||||
|
||||
# deal with 'A'-'F'
|
||||
3C 47 ; CMPI8_AL !71
|
||||
7C 1C ; JL8 !ascii_high
|
||||
3C 47 ; cmp_al, !71
|
||||
7C 1C ; jl !ascii_high
|
||||
|
||||
# deal with all ascii less than 'a'
|
||||
3C 61 ; CMPI8_AL !97
|
||||
7C 0F ; JL8 !ascii_other
|
||||
3C 61 ; cmp_al, !97
|
||||
7C 0F ; jl !ascii_other
|
||||
|
||||
#deal with 'a'-'f'
|
||||
3C 67 ; CMPI8_AL !103
|
||||
7C 12 ; JL8 !ascii_low
|
||||
3C 67 ; cmp_al, !103
|
||||
7C 12 ; jl !ascii_low
|
||||
|
||||
# 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
|
||||
E8 27000000 ; CALLI32 %read_byte
|
||||
E8 27000000 ; call %read_byte
|
||||
|
||||
# Loop if not LF
|
||||
3C 0A ; CMPI8_AL !10
|
||||
75 F7 ; JNE8 !purge_comment
|
||||
3C 0A ; cmp_al, !10
|
||||
75 F7 ; jne !purge_comment
|
||||
|
||||
# Otherwise return -1
|
||||
|
||||
# :ascii_other [_start+0x133]
|
||||
6A FF ; PUSH !-1
|
||||
58 ; POP_RAX # return = -1
|
||||
C3 ; RET
|
||||
# :ascii_other [_start+0x120]
|
||||
6A FF ; push !-1
|
||||
58 ; pop_rax # return = -1
|
||||
C3 ; ret
|
||||
|
||||
# :ascii_num [_start+0x137]
|
||||
2C 30 ; SUBI8_AL !48
|
||||
C3 ; RET
|
||||
# :ascii_num [_start+0x124]
|
||||
2C 30 ; sub_al, !48
|
||||
C3 ; ret
|
||||
|
||||
# :ascii_low [_start+0x13A]
|
||||
2C 20 ; SUBI8_AL !32 # convert to uppercase
|
||||
# :ascii_low [_start+0x127]
|
||||
2C 20 ; sub_al, !32 # convert to uppercase
|
||||
|
||||
# :ascii_high [_start+0x13C]
|
||||
2C 37 ; SUBI8_AL !55
|
||||
C3 ; RET
|
||||
# :ascii_high [_start+0x129]
|
||||
2C 37 ; sub_al, !55
|
||||
C3 ; ret
|
||||
|
||||
# Writes byte stored in al
|
||||
# :write_byte [_start+0x13F]
|
||||
4C89E9 ; COPY_R13_to_RCX # arg1 = fout
|
||||
6A 01 ; PUSH !1 # size = 1
|
||||
4889E2 ; COPY_RSP_to_RDX # arg2 = &size
|
||||
50 ; PUSH_RAX # allocate stack
|
||||
4989E0 ; COPY_RSP_to_R8 # 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
|
||||
FF51 28 ; CALL_RCX_Immediate8 !40 # fout->write()
|
||||
4883C4 28 ; ADDI8_RSP !40 # deallocate stack
|
||||
# :write_byte [_start+0x12C]
|
||||
4C89E9 ; mov_rcx,r13 # arg1 = fout
|
||||
6A 01 ; push !1 # size = 1
|
||||
4889E2 ; mov_rdx,rsp # arg2 = &size
|
||||
50 ; push_rax # allocate stack
|
||||
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
|
||||
FF51 28 ; call_[rcx+BYTE] !40 # fout->write()
|
||||
4883C4 28 ; add_rsp, !40 # deallocate stack
|
||||
|
||||
C3 ; RET # return
|
||||
C3 ; ret # return
|
||||
|
||||
# :read_byte [_start+0x156]
|
||||
4C89E1 ; COPY_R12_to_RCX # arg1 = fin
|
||||
6A 01 ; PUSH !1 # size = 1
|
||||
4889E2 ; COPY_RSP_to_RDX # arg2 = &size
|
||||
56 ; PUSH_RSI # allocate stack
|
||||
4989E0 ; COPY_RSP_to_R8 # 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
|
||||
FF51 20 ; CALL_RCX_Immediate8 !32 # fin->read()
|
||||
58 ; POP_RAX # deallocate stack
|
||||
58 ; POP_RAX # deallocate stack
|
||||
58 ; POP_RAX # deallocate stack
|
||||
58 ; POP_RAX # save input to rax
|
||||
5E ; POP_RSI # save size to rsi
|
||||
# :read_byte [_start+0x143]
|
||||
4C89E1 ; mov_rcx,r12 # arg1 = fin
|
||||
6A 01 ; push !1 # size = 1
|
||||
4889E2 ; mov_rdx,rsp # arg2 = &size
|
||||
31F6 ; xor_esi,esi # zero rsi
|
||||
56 ; push_rsi # allocate stack
|
||||
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
|
||||
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 # save input to rax
|
||||
5E ; pop_rsi # save size to rsi
|
||||
|
||||
# If the file ended (0 bytes read) terminate
|
||||
85F6 ; TEST_ESI_ESI # if size = 0
|
||||
74 01 ; JE8 !terminate # then we are done
|
||||
85F6 ; test_esi,esi # if size = 0
|
||||
74 01 ; je !terminate # then we are done
|
||||
|
||||
C3 ; RET # return
|
||||
C3 ; ret # return
|
||||
|
||||
# :terminate [_start+0x172]
|
||||
53 ; PUSH_RBX # allocate stack
|
||||
4C89E1 ; COPY_R12_to_RCX # arg1 = fin
|
||||
FF51 10 ; CALL_RCX_Immediate8 !16 # fin->close()
|
||||
4C89E9 ; COPY_R13_to_RCX # arg1 = fout
|
||||
FF51 10 ; CALL_RCX_Immediate8 !16 # fout->close()
|
||||
# :terminate [_start+0x161]
|
||||
58 ; pop_rax # remove last return address from stack
|
||||
5E ; pop_rsi # restore rootdir
|
||||
415F ; pop_r15 # restore image_handle
|
||||
415E ; pop_r14 # restore system->boot
|
||||
5B ; pop_rbx # restore root_device
|
||||
|
||||
4889EC ; COPY_RBP_to_RSP # restore stack
|
||||
C3 ; RET # return to UEFI
|
||||
53 ; push_rbx # allocate stack
|
||||
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
|
||||
# :LOADED_IMAGE_PROTOCOL [_start+0x183]
|
||||
# :LOADED_IMAGE_PROTOCOL [_start+0x1B1]
|
||||
A1 31 1B 5B ; %0x5b1b31a1
|
||||
62 95 ; @0x9562
|
||||
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
|
||||
|
||||
# :SIMPLE_FS_PROTOCOL [_start+0x193]
|
||||
# :SIMPLE_FS_PROTOCOL [_start+0x1C1]
|
||||
22 5B 4E 96 ; %0x0964e5b22
|
||||
59 64 ; @0x6459
|
||||
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
|
||||
|
||||
# :PE32_end [_start+0x1A3]
|
||||
# :PE32_end [_start+0x1D1]
|
||||
|
|
|
@ -3,6 +3,9 @@
|
|||
#
|
||||
# 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
|
||||
4D 5A # Signature
|
||||
00 00 # Number of bytes in the last page.
|
||||
|
@ -148,7 +151,8 @@ CF 03 00 00 ; SizeOfRawData
|
|||
6A 00 ; push !0 # arg5 = NULL
|
||||
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)
|
||||
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
|
||||
4D89F9 ; mov_r9,r15 # arg4 = image_handle
|
||||
|
@ -161,7 +165,8 @@ CF 03 00 00 ; SizeOfRawData
|
|||
6A 00 ; push !0 # arg5 = NULL
|
||||
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)
|
||||
488B4C24 30 ; mov_rcx,[rsp+BYTE] !48 # get rootfs
|
||||
4883C4 30 ; add_rsp, !48 # deallocate stack
|
||||
59 ; pop rcx # get rootfs
|
||||
|
||||
# Get root directory
|
||||
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
|
||||
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
|
||||
# :loop_options
|
||||
# :loop_options [_start + 0x71]
|
||||
4839D3 ; cmp_rbx,rdx # Check if we are done
|
||||
74 14 ; je8 !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 F1 ; jne8 !loop_options # then continue looping
|
||||
75 F1 ; jne8 !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 E7 ; jmp8 !loop_options # next argument
|
||||
# :loop_options_done
|
||||
EB E7 ; jmp8 !loop_options # next argument
|
||||
# :loop_options_done [_start + 0x8A]
|
||||
|
||||
# Open file for reading
|
||||
4158 ; pop_r8 # arg3 = in
|
||||
|
|
Loading…
Reference in New Issue