# SPDX-FileCopyrightText: 2022 Andrius Štikonas # SPDX-FileCopyrightText: 2019 Jeremiah Orians # # SPDX-License-Identifier: GPL-3.0-or-later DEFINE add_rbx,[rdi+BYTE] 48035F DEFINE add_rbx, 4883C3 DEFINE add_rsp, 4883C4 DEFINE call E8 DEFINE call_[rcx+BYTE] FF51 DEFINE call_[r14+BYTE] 41FF56 DEFINE call_[r14+DWORD] 41FF96 DEFINE cmp_al, 3C DEFINE cmp_rax, 483D DEFINE cmp_rbx,rdx 4839D3 DEFINE cmp_r8, 4983F8 DEFINE je 74 DEFINE jmp EB DEFINE jne 75 DEFINE lea_rdx,[rip+DWORD] 488D15 DEFINE mov_rdx, 48C7C2 DEFINE mov_rbx,rax 4889C3 DEFINE mov_rcx,rax 4889C1 DEFINE mov_rcx,rbx 4889D9 DEFINE mov_rcx,r12 4C89E1 DEFINE mov_rcx,r13 4C89E9 DEFINE mov_rcx,r15 4C89F9 DEFINE mov_rdi,rax 4889C7 DEFINE mov_rdx,rax 4889C2 DEFINE mov_rdx,rbx 4889DA DEFINE mov_rdx,rsp 4889E2 DEFINE mov_rbp,rsp 4889E5 DEFINE mov_rsp,rbp 4889EC DEFINE mov_r8,rbx 4989D8 DEFINE mov_r8,rsp 4989E0 DEFINE mov_r8,r12 4D89E0 DEFINE mov_r9,r12 4D89E1 DEFINE mov_r12,rcx 4989CC DEFINE mov_r13,rsi 4989F5 DEFINE mov_al,[rbx] 8A03 DEFINE mov_rax,[rsp+BYTE] 488B4424 DEFINE mov_rbx,[rdi+BYTE] 488B5F DEFINE mov_rcx,[rdi+BYTE] 488B4F DEFINE mov_rcx,[rsp+BYTE] 488B4C24 DEFINE mov_r14,[rdx+BYTE] 4C8B72 DEFINE mov_rcx,[rip+DWORD] 488B0D DEFINE mov_[rip+DWORD],rax 488905 DEFINE mov_[rip+DWORD],rcx 48890D DEFINE mov_[rbx], C603 DEFINE pop_rax 58 DEFINE pop_rcx 59 DEFINE pop_rsi 5E DEFINE pop_r8 4158 DEFINE pop_r9 4159 DEFINE pop_r15 415F DEFINE push 6A DEFINE push_rax 50 DEFINE push_rbx 53 DEFINE push_rdx 52 DEFINE ret C3 DEFINE ror_r9 49D1C9 DEFINE sub_rbx, 4883EB DEFINE sub_rsp, 4883EC DEFINE xor_eax,eax 31C0 DEFINE test_eax,eax 85C0 DEFINE test_r8,r8 4D85C0 DEFINE xor_r9,r9 4D31C9 # efi_main(void *image_handle, struct efi_system_table *system) :_start mov_rbp,rsp # save stack pointer mov_r12,rcx # save image_handle mov_r14,[rdx+BYTE] !96 # save system->boot # Open Loaded Image protocol mov_r9,r12 # arg4 = image_handle lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL mov_rcx,r12 # arg1 = image_handle call %open_protocol # open protocol mov_rdi,rax # save image # Get root file system mov_r9,r12 # 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_[rip+DWORD],rcx %root_device # save root_device call %open_protocol # open protocol mov_rcx,rax # get rootfs # Get root directory lea_rdx,[rip+DWORD] %rootdir # 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 # Push command line arguments onto stack mov_rbx,[rdi+BYTE] !56 # options = image->load_options mov_rdx,rbx # save beginning of load_options add_rbx,[rdi+BYTE] !48 # go to the end of load_options push !0 # Save end of arguments (NULL) onto stack :loop_options cmp_rbx,rdx # Check if we are done je !loop_options_done # We are done sub_rbx, !2 # --options mov_al,[rbx] # *options cmp_al, !0x20 # if *options != ' ' jne !loop_options # then continue looping mov_[rbx], !0 # zero it add_rbx, !2 # ++options push_rbx # push another argument onto stack jmp !loop_options # next argument :loop_options_done pop_r8 # get output file test_r8,r8 # if no output file je !exit_early # then exit # Open file for writing push_rdx # allocate stack for fout mov_rdx,rsp # arg2 = &fout push !0 # arg5 = 0 push !7 # to get 0x8000000000000003 we set the rightmost 3 bits pop_r9 # and then do right rotation by 1 ror_r9 # arg4 = EFI_FILE_MODE_CREATE| EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ mov_rcx,[rip+DWORD] %rootdir # arg1 = rootdir sub_rsp, !32 # allocate shadow stack space for UEFI function call_[rcx+BYTE] !8 # rootdir->open() add_rsp, !40 # deallocate stack pop_r15 # get fout mov_rdx, %0x100000 # Allocate 1MB call %allocate_pool # Get memory pool mov_rbx,rax # Save it :core pop_r8 # Get the actual input name cmp_r8, !0 # Check for null string je !done # We are done if null string call %open_file # Open file as read only test_eax,eax # check if successfully opened jne !core # Else go to another file mov_r13,rsi # Protect fin :keep mov_rdx, %0x100000 # set the size of chars we want call %read # read them push_rax # Protect the number of bytes read mov_rdx,rax # number of bytes to write call %write # write them pop_rax # Get bytes read cmp_rax, %0x100000 # Check if buffer was fully used je !keep # Keep looping if it was full mov_rcx,r13 # fin call %close_file # close file jmp !core # Move to next file :done mov_rcx,r15 # Get output file call %close_file # close it mov_rcx,rbx # Get buffer call %free_pool # release it :exit_early mov_rcx,[rip+DWORD] %rootdir # Get rootdir call %close_file # close it mov_r8,r12 # arg3 = image_handle lea_rdx,[rip+DWORD] %SIMPLE_FS_PROTOCOL # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID mov_rcx,[rip+DWORD] %root_device # arg1 = root_device call %close_protocol # close protocol mov_r8,r12 # arg3 = image_handle lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # EFI_LOADED_IMAGE_PROTOCOL_GUID mov_rcx,r12 # arg1 = image_handle call %close_protocol # close protocol mov_rsp,rbp # restore stack pointer ret # return to UEFI # r8: input file name # returns input file handle in rsi, status in rax :open_file # Open file for reading push_rdx # allocate stack for fin mov_rdx,rsp # arg2 = &fin push !1 # arg5 = EFI_FILE_READ_ONLY push !1 # prepare to set arg4 to EFI_FILE_MODE_READ pop_r9 # arg4 = EFI_FILE_MODE_READ mov_rcx,[rip+DWORD] %rootdir # arg1 = rootdir sub_rsp, !32 # allocate shadow stack space for UEFI function call_[rcx+BYTE] !8 # rootdir->open() add_rsp, !40 # deallocate stack pop_rsi # get fin ret # rcx: file handle :close_file push_rax # allocate shadow stack space for UEFI function call_[rcx+BYTE] !16 # file_handle->close(file_handle) pop_rax # deallocate stack ret # rcx: handle # rdx: &guid # r9: agent_handle # returns interface :open_protocol push_rax # allocate stack for interface mov_r8,rsp # arg3 = &interface 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(handle, &guid, &interface, agent_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL) add_rsp, !48 # deallocate stack pop_rax # get image ret # rcx: handle # rdx: &guid # r8: agent_handle :close_protocol xor_r9,r9 # arg4 = NULL sub_rsp, !32 # allocate shadow stack space for UEFI function call_[r14+DWORD] %288 # system->boot->close_protocol(handle, &guid, agent_handle, 0) add_rsp, !32 # deallocate stack ret # rdx: number of bytes to allocate # r14: system->boot # returns pointer in rax :allocate_pool push_rdx # allocate stack for pool pointer mov_r8,rsp # arg3 = &pool push !2 pop_rcx # arg1 = EFI_LOADER_DATA sub_rsp, !24 # allocate shadow stack space for UEFI call_[r14+BYTE] !64 # system->boot->allocate_pool(EFI_LOADER_DATA, 2048, &pool) add_rsp, !24 # deallocate stack pop_rax # get pool ret # rcx: memory pool # r14: system->boot :free_pool push_rax # allocate shadow stack space for UEFI function call_[r14+BYTE] !72 # system->boot->free_pool(pool) pop_rax # deallocate stack ret # rdx: number of bytes to read # rbx: buffer # r13: input file handle # returns number of bytes read in rax :read mov_rcx,r13 # arg1 = fin push_rdx # set size mov_rdx,rsp # arg2 = &size mov_r8,rbx # arg3 = buffer sub_rsp, !24 # allocate shadow stack space for UEFI call_[rcx+BYTE] !32 # fin->read() add_rsp, !24 # deallocate stack pop_rax # save size to rax ret # rdx: number of bytes to write # rbx: buffer # r14: output file handle # returns number of bytes written in rax :write mov_rcx,r15 # arg1 = fout push_rdx # set size mov_rdx,rsp # arg2 = &size mov_r8,rbx # arg3 = buffer sub_rsp, !24 # allocate shadow stack space for UEFI call_[rcx+BYTE] !40 # fin->write() add_rsp, !24 # deallocate stack pop_rax # save size to rax ret # Protocol GUIDs :LOADED_IMAGE_PROTOCOL %0x5b1b31a1 @0x9562 @0x11d2 !0x8e !0x3f !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b :SIMPLE_FS_PROTOCOL %0x0964e5b22 @0x6459 @0x11d2 !0x8e !0x39 !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b :rootdir %0 %0 :root_device %0 %0 :PE32_end