# SPDX-FileCopyrightText: 2022 Andrius Štikonas # SPDX-FileCopyrightText: 2017 Jeremiah Orians # # SPDX-License-Identifier: GPL-3.0-or-later # 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) 488B7C24 30 ; mov_rdi,[rsp+BYTE] !48 # save 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) 488B4C24 30 ; mov_rcx,[rsp+BYTE] !48 # 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 6A 00 ; push !0 # Save end of arguments (NULL) onto stack :loop_options 4839D3 ; cmp_rbx,rdx # Check if we are done 74 !loop_options_done ; je8 !loop_options_done # We are done 4883EB 02 ; sub_rbx, !2 # --options 8A03 ; mov_al,[rbx] # *options 3C 20 ; cmp_al, !0x20 # if *options != ' ' 75 !loop_options ; 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 !loop_options ; jmp8 !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 5F ; pop_rdi # get fin # Open file for writing 4158 ; pop_r8 # arg3 = out 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 5B ; pop_rbx # get fout # Save variables that are needed for cleanup 4155 ; push_r13 # save root_device 4156 ; push_r14 # save system->boot 4157 ; push_r15 # save image_handle 56 ; push_rsi # save rootdir # Allocate pool for single-character label table # pointer to table will be stored at the top of the stack 52 ; push_rdx # allocate stack for table 4989E0 ; mov_r8,rsp # arg3 = &table 31D2 ; xor_edx,edx # zero rdx B6 08 ; mov_dh, !0x8 # arg2 = 256 * 8 = 2048 = 0x800 6A 02 ; push !2 59 ; pop_rcx # arg1 = EFI_LOADER_DATA 4883EC 18 ; sub_rsp, !24 # allocate shadow stack space for UEFI 41FF56 40 ; call_[r14+BYTE] !64 # system->boot->allocate_pool(EFI_LOADER_DATA, 2048, &table) 4883C4 18 ; add_rsp, !24 # deallocate stack 49C7C7 FFFFFFFF ; mov_r15, %-1 # Our flag for byte processing 49C7C6 00000000 ; mov_r14, %0 # temp storage for the sum 49C7C5 00000000 ; mov_r13, %0 # Our starting IP E8 %First_pass ; call %First_pass # Process it # rewind input file 4889F9 ; mov_rcx,rdi # Using our input file 31D2 ; xor_edx,edx # Offset Zero 50 ; push_rax # allocate shadow stack space for UEFI function 50 ; push_rax # allocate shadow stack space for UEFI function FF51 38 ; call_[rcx+BYTE] !56 # fin->set_position(fin, 0) 58 ; pop_rax # deallocate stack 58 ; pop_rax # deallocate stack 49C7C7 FFFFFFFF ; mov_r15, %-1 # Our flag for byte processing 49C7C6 00000000 ; mov_r14, %0 # temp storage for the sum 49C7C5 00000000 ; mov_r13, %0 # Our starting IP E8 %Second_pass ; call %Second_pass # Process it E9 %Done ; jmp %Done :First_pass E8 %Read_byte ; call %Read_byte # Deal with EOF 483D FCFFFFFF ; cmp_rax, %-4 0F84 %First_pass_done ; je %First_pass_done # Check for : 483D 3A000000 ; cmp_rax, %0x3a 0F85 %First_pass_0 ; jne %First_pass_0 # Deal with label E8 %StoreLabel ; call %StoreLabel :First_pass_0 # Check for % 483D 25000000 ; cmp_rax, %0x25 0F84 %First_pass_pointer ; je %First_pass_pointer # Deal with everything else E8 %hex ; call %hex # Process our char # Deal with EOF 483D FCFFFFFF ; cmp_rax, %-4 0F84 %First_pass_done ; je %First_pass_done # deal with -1 values 483D 00000000 ; cmp_rax, %0 0F8C %First_pass ; jl %First_pass # deal with toggle 4981FF 00000000 ; cmp_r15, %0 0F84 %First_pass_1 ; je %First_pass_1 4981C5 01000000 ; add_r13, %1 # Increment IP :First_pass_1 49F7D7 ; not_r15 E9 %First_pass ; jmp %First_pass :First_pass_pointer # Deal with Pointer to label E8 %Read_byte ; call %Read_byte # Drop the char 4981C5 04000000 ; add_r13, %4 # Increment IP E9 %First_pass ; jmp %First_pass # Loop again :First_pass_done C3 ; ret :hex # deal with EOF 483D FCFFFFFF ; cmp_rax, %-4 0F84 %EOF ; je %EOF # deal with line comments starting with # 483D 23000000 ; cmp_rax, %0x23 0F84 %ascii_comment ; je %ascii_comment # deal with line comments starting with ; 483D 3B000000 ; cmp_rax, %0x3b 0F84 %ascii_comment ; je %ascii_comment # deal all ascii less than 0 483D 30000000 ; cmp_rax, %0x30 0F8C %ascii_other ; jl %ascii_other # deal with 0-9 483D 3A000000 ; cmp_rax, %0x3a 0F8C %ascii_num ; jl %ascii_num # deal with all ascii less than A 483D 41000000 ; cmp_rax, %0x41 0F8C %ascii_other ; jl %ascii_other # deal with A-F 483D 47000000 ; cmp_rax, %0x47 0F8C %ascii_high ; jl %ascii_high # deal with all ascii less than a 483D 61000000 ; cmp_rax, %0x61 0F8C %ascii_other ; jl %ascii_other # deal with a-f 483D 67000000 ; cmp_rax, %0x67 0F8C %ascii_low ; jl %ascii_low # The rest that remains needs to be ignored E9 %ascii_other ; jmp %ascii_other :Second_pass E8 %Read_byte ; call %Read_byte # Deal with EOF 483D FCFFFFFF ; cmp_rax, %-4 0F84 %Second_pass_done ; je %Second_pass_done # Simply drop the label 483D 3A000000 ; cmp_rax, %0x3a 0F85 %Second_pass_0 ; jne %Second_pass_0 E8 %Read_byte ; call %Read_byte E9 %Second_pass ; jmp %Second_pass :Second_pass_0 # Deal with % pointer 483D 25000000 ; cmp_rax, %0x25 0F85 %Second_pass_1 ; jne %Second_pass_1 E8 %StorePointer ; call %StorePointer E9 %Second_pass ; jmp %Second_pass :Second_pass_1 # Deal with everything else E8 %hex ; call %hex # Process our char # Deal with EOF 483D FCFFFFFF ; cmp_rax, %-4 0F84 %Second_pass_done ; je %Second_pass_done # deal with -1 values 483D 00000000 ; cmp_rax, %0 0F8C %Second_pass ; jl %Second_pass # deal with toggle 4981FF 00000000 ; cmp_r15, %0 0F84 %print ; je %print # process first byte of pair 4989C6 ; mov_r14,rax 49C7C7 00000000 ; mov_r15, %0 E9 %Second_pass ; jmp %Second_pass :Second_pass_done :EOF C3 ; ret :ascii_num 4883E8 30 ; sub_rax, !0x30 C3 ; ret :ascii_low 4883E8 57 ; sub_rax, !0x57 C3 ; ret :ascii_high 4883E8 37 ; sub_rax, !0x37 C3 ; ret :ascii_other 48C7C0 FFFFFFFF ; mov_rax, %-1 C3 ; ret :ascii_comment E8 %Read_byte ; call %Read_byte 483D 0D000000 ; cmp_rax, %0xd 0F84 %ascii_comment_cr ; je %ascii_comment_cr 483D 0A000000 ; cmp_rax, %0xa 0F85 %ascii_comment ; jne %ascii_comment :ascii_comment_cr 48C7C0 FFFFFFFF ; mov_rax, %-1 C3 ; ret # process second byte of pair :print # update the sum and store in output 49C1E6 04 ; shl_r14, !4 4C01F0 ; add_rax,r14 # flip the toggle 49F7D7 ; not_r15 # R15 = -1 48C7C2 01000000 ; mov_rdx, %1 # set the size of chars we want E8 %print_chars ; call %print_chars 4981C5 01000000 ; add_r13, %1 # Increment IP E9 %Second_pass ; jmp %Second_pass :Read_byte 4889F9 ; mov_rcx,rdi # 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) return EOF 85F6 ; test_esi,esi # if size = 0 75 !Read_byte_1 ; jne8 !Read_byte_1 48C7C0 FCFFFFFF ; mov_rax, %-4 # Put EOF in rax :Read_byte_1 C3 ; ret # return # Writes bytes stored in rax :print_chars 4889D9 ; mov_rcx,rbx # arg1 = fout 52 ; push_rdx # set size 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 :Get_table_target E8 %Read_byte ; call %Read_byte # Get single char label 48C1E0 03 ; shl_rax, !3 # Each label in table takes 8 bytes to store 488B4C24 18 ; mov_rcx,[rsp+BYTE] !24 # Get table 4801C8 ; add_rax,rcx # Calculate offset C3 ; ret :StoreLabel E8 %Get_table_target ; call %Get_table_target 4C8928 ; mov_[rax],r13 # Write out pointer to table C3 ; ret :StorePointer 4981C5 04000000 ; add_r13, %4 # Increment IP E8 %Get_table_target ; call %Get_table_target # Get address of pointer 488B00 ; mov_rax,[rax] # Get pointer 4C29E8 ; sub_rax,r13 # target - ip 48C7C2 04000000 ; mov_rdx, %4 # set the size of chars we want E8 %print_chars ; call %print_chars C3 ; ret :Done 59 ; pop_rcx # restore table 5E ; pop_rsi # restore rootdir 415F ; pop_r15 # restore image_handle 415E ; pop_r14 # restore system->boot 415D ; pop_r13 # restore root_device # Free pool # arg1 = table 50 ; push_rax # allocate shadow stack space for UEFI function 41FF56 48 ; call_[r14+BYTE] !72 # system->boot->free_pool(table) 4889F9 ; mov_rcx,rdi # arg1 = fin FF51 10 ; call_[rcx+BYTE] !16 # fin->close() 4889D9 ; mov_rcx,rbx # 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 4C89E9 ; mov_rcx,r13 # 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