# SPDX-FileCopyrightText: 2022 Andrius Štikonas # SPDX-FileCopyrightText: 2017 Jeremiah Orians # # SPDX-License-Identifier: GPL-3.0-or-later # Usage: hex0 file.hex0 file # Does not validate the arguments or check for success # Calling convention: # First four arguments are passed via registers rcx, rdx, r8, r9 (if they fit in 64-bits) # but we need to leave stack space # rax, rcx, rdx, r8, r9, r10 and r11 are volatile and can be changed by called function # efi_main(void *image_handle, struct efi_system_table *system) :_start 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 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 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 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 # 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 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 # temp storage for the sum 31FF ; xor_edi,edi # rdi = 0 :loop # Read a byte E8 %read_byte ; call %read_byte # process byte E8 %hex ; call %hex # deal with -1 values 85C0 ; test_eax,eax 7C !loop ; jl !loop # deal with toggle 85DB ; test_ebx,ebx # jump if ebx >= 0 7D !print ; jge !print # process first byte of pair 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, !4 01F8 ; add_eax,edi # flip the toggle FFCB ; dec_ebx # rbx = -1 E8 %write_byte ; call %write_byte EB !loop ; jmp !loop :hex # Purge Comment Lines (#) 3C 23 ; cmp_al, !35 74 !purge_comment ; je !purge_comment # Purge Comment Lines (;) 3C 3B ; cmp_al, !59 74 !purge_comment ; je !purge_comment # deal all ascii less than '0' 3C 30 ; cmp_al, !48 7C !ascii_other ; jl !ascii_other # deal with 0-9 3C 3A ; cmp_al, !58 7C !ascii_num ; jl !ascii_num # deal with all ascii less than 'A' 3C 41 ; cmp_al, !65 7C !ascii_other ; jl !ascii_other # deal with 'A'-'F' 3C 47 ; cmp_al, !71 7C !ascii_high ; jl !ascii_high # deal with all ascii less than 'a' 3C 61 ; cmp_al, !97 7C !ascii_other ; jl !ascii_other #deal with 'a'-'f' 3C 67 ; cmp_al, !103 7C !ascii_low ; jl !ascii_low # The rest that remains needs to be ignored EB !ascii_other ; jmp !ascii_other :purge_comment # Read a byte E8 %read_byte ; call %read_byte # Loop if not LF 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 :ascii_num 2C 30 ; sub_al, !48 C3 ; ret :ascii_low 2C 20 ; sub_al, !32 # convert to uppercase :ascii_high 2C 37 ; sub_al, !55 C3 ; ret # Writes byte stored in al :write_byte 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 :read_byte 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 ; je !terminate # then we are done C3 ; ret # return :terminate 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 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 :LOADED_IMAGE_PROTOCOL A1 31 1B 5B ; %0x5b1b31a1 62 95 ; @0x9562 D2 11 ; @0x11d2 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 8E 39 00 A0 C9 69 72 3B ; !0x8e !0x39 !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b :PE32_end