# SPDX-FileCopyrightText: 2022 Andrius Štikonas # SPDX-FileCopyrightText: 2017 Jeremiah Orians # # SPDX-License-Identifier: GPL-3.0-or-later .global _start .text # Register usage: # R15 => Flag # R14 => High bits # R13 => IP # R12 => MALLOC # R11 => HEAD # Struct format: (size 24) # NEXT => 0 # TARGET => 8 # NAME => 16 # efi_main(void *image_handle, struct efi_system_table *system) _start: mov rbp, rsp # save stack pointer mov r15, rcx # save image_handle mov r14, [rdx+96] # system->boot mov [rip+SystemBoot], r14 # save system->boot # Allocate pool for scratch area xor edx, edx # zero rdx mov dh, 0x8 # arg2 = 256 * 8 = 2048 = 0x800 call allocate_pool mov [rip+scratch], rax # save scratch # Open Loaded Image protocol 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, r15 # arg4 = image_handle mov rcx, r15 # arg1 = image_handle 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 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 # Get root file system 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+280] # system->boot->open_protocol(root_device, &guid, &rootfs, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL) mov rcx, [rsp+64] # get rootfs # Get root directory push rdx # 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 rbx # save rootdir # 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, rbx # arg1 = rootdir sub rsp, 32 # allocate shadow stack space for UEFI function call [rcx+8] # rootdir->open() mov rax, [rsp+40] # get fin mov [rip+fin], rax # save fin # 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 r8, r13 # arg3 = out mov rcx, rbx # arg1 = rootdir sub rsp, 32 # allocate shadow stack space for UEFI function call [rcx+8] # rootdir->open() mov r10, [rsp+40] # get fout # Allocate pool for structs mov rdx, 0x1000000 # allocate 16 MiB of memory call allocate_pool mov r12, rax # get structs call ClearScratch # Zero scratch mov r15, -1 # Our flag for byte processing mov r14, 0 # temp storage for the sum mov r13, 0x00600000 # Our starting IP mov r11, 0 # HEAD = NULL call First_pass # Process it # rewind input file push r10 # Protect r10 push r11 # Protect r11 mov rcx, [rip+fin] # Using our input file xor edx, edx # Offset Zero push rax # allocate shadow stack space for UEFI function push rax # allocate shadow stack space for UEFI function call [rcx+56] # fin->set_position(fin, 0) pop rax # deallocate stack pop rax # deallocate stack pop r11 # restore r11 pop r10 # restore r10 mov r15, -1 # Our flag for byte processing mov r14, 0 # temp storage for the sum mov r13, 0x00600000 # Our starting IP call Second_pass # Process it jmp Done First_pass: call Read_byte # Deal with EOF cmp rax, -4 je First_pass_done # Check for : cmp rax, 0x3a jne First_pass_0 # Deal with label jmp StoreLabel First_pass_0: # Check for ! cmp rax, 0x21 je First_pass_pointer # Check for @ cmp rax, 0x40 je First_pass_pointer # Check for $ cmp rax, 0x24 je First_pass_pointer # Check for % cmp rax, 0x25 je First_pass_pointer # Check for & cmp rax, 0x26 je First_pass_pointer # Deal with everything else call hex # Process our char # Deal with EOF cmp rax, -4 je First_pass_done # deal with -1 values cmp rax, 0 jl First_pass # deal with toggle cmp r15, 0 je First_pass_1 add r13, 1 # Increment IP First_pass_1: not r15 jmp First_pass Update_Pointer: # Check for ! cmp rax, 0x21 je Update_Pointer_1 # Check for @ cmp rax, 0x40 je Update_Pointer_2 # Check for $ cmp rax, 0x24 je Update_Pointer_2 # Check for % cmp rax, 0x25 je Update_Pointer_4 # Check for & cmp rax, 0x26 je Update_Pointer_4 # deal with bad input call fail Update_Pointer_4: add r13, 2 # Increment IP Update_Pointer_2: add r13, 1 # Increment IP Update_Pointer_1: add r13, 1 # Increment IP ret First_pass_pointer: # Deal with Pointer to label call Update_Pointer # Increment IP mov rbx, [rip+scratch] # Using scratch call consume_token # Read token call ClearScratch # Throw away token cmp rax, 0x3E # check for '>' jne First_pass # Loop again # Deal with %label>label case mov rbx, [rip+scratch] # Write to scratch call consume_token # get token call ClearScratch # Clean up after ourselves jmp First_pass # Loop again First_pass_done: ret hex: # deal with EOF cmp rax, -4 je EOF # deal with line comments starting with # cmp rax, 0x23 je ascii_comment # deal with line comments starting with ; cmp rax, 0x3b je ascii_comment # deal all ascii less than 0 cmp rax, 0x30 jl ascii_other # deal with 0-9 cmp rax, 0x3a jl ascii_num # deal with all ascii less than A cmp rax, 0x41 jl ascii_other # deal with A-F cmp rax, 0x47 jl ascii_high # deal with all ascii less than a cmp rax, 0x61 jl ascii_other # deal with a-f cmp rax, 0x67 jl ascii_low # The rest that remains needs to be ignored jmp ascii_other Second_pass: call Read_byte # Deal with EOF cmp rax, -4 je Second_pass_done # Simply drop the label cmp rax, 0x3a jne Second_pass_0 mov rbx, [rip+scratch] # Using scratch call consume_token # Read token call ClearScratch # Throw away token jmp Second_pass Second_pass_0: # Deal with % pointer cmp rax, 0x25 je StorePointer_rel4 # Deal with @ pointer cmp rax, 0x40 je StorePointer_rel2 # Deal with ! pointer cmp rax, 0x21 je StorePointer_rel1 # Deal with & pointer cmp rax, 0x26 je StorePointer_abs4 # Deal with $ pointer cmp rax, 0x24 je StorePointer_abs2 Second_pass_1: # Deal with everything else call hex # Process our char # Deal with EOF cmp rax, -4 je Second_pass_done # deal with -1 values cmp rax, 0 jl Second_pass # deal with toggle cmp r15, 0 je print # process first byte of pair mov r14, rax mov r15, 0 jmp Second_pass Second_pass_done: EOF: ret ascii_num: sub rax, 0x30 ret ascii_low: sub rax, 0x57 ret ascii_high: sub rax, 0x37 ret ascii_other: mov rax, -1 ret ascii_comment: call Read_byte cmp rax, 0xd je ascii_comment_cr cmp rax, 0xa jne ascii_comment ascii_comment_cr: mov rax, -1 ret # process second byte of pair print: # update the sum and store in output shl r14, 4 add rax, r14 # flip the toggle not r15 # r15 = -1 mov rdx, 1 # set the size of chars we want call print_chars add r13, 1 # Increment IP jmp Second_pass Read_byte: push r10 # Protect r10 push r11 # Protect r11 mov rcx, [rip+fin] # 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+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 pop r11 # restore r11 pop r10 # restore r10 # If the file ended (0 bytes read) return EOF test esi, esi # if size == 0 jne Read_byte_1 mov rax, -4 # Put EOF in rax Read_byte_1: ret # return # Writes bytes stored in rax print_chars: push r10 # Protect r10 push r11 # Protect r11 mov rcx, r10 # arg1 = fout push rdx # set size 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+40] # fout->write() add rsp, 40 # deallocate stack pop r11 # restore r11 pop r10 # restore r10 ret # return # Receives pointer in RBX # Writes out char and updates RBX consume_token: call Read_byte # Consume_token # Check for \t cmp rax, 0x09 je consume_token_done # Check for \n cmp rax, 0x0A je consume_token_done # Check for ' ' cmp rax, 0x20 je consume_token_done # Check for '>' cmp rax, 0x3E je consume_token_done # Looks like we are still reading token mov [rbx], al # Store char add rbx, 1 # Point to next spot jmp consume_token # loop until done consume_token_done: mov rcx, 0 # Pad with nulls mov [rbx], rcx add rbx, 8 ret StoreLabel: mov rax, r12 # ENTRY add r12, 24 # CALLOC mov [rax+8], r13 # ENTRY->TARGET = IP mov [rax], r11 # ENTRY->NEXT = JUMP_TABLE mov r11, rax # JUMP_TABLE = ENTRY mov [r11+16], r12 # ENTRY->NAME = TOKEN mov rbx, r12 # Write Starting after struct call consume_token # Collect whole string mov r12, rbx # Update HEAP jmp First_pass GetTarget: mov rdi, [rip+scratch] # Reset scratch mov rcx, r11 # Grab JUMP_TABLE mov rsi, [rcx+16] # I->NAME GetTarget_loop: mov al, [rsi] # I->NAME[0] mov bl, [rdi] # scratch[0] movzx rbx, bl # Zero extend movzx rax, al # Zero extend cmp al, bl # IF TOKEN == I->NAME jne GetTarget_miss # Oops add rsi, 1 add rdi, 1 cmp al, 0 jne GetTarget_loop # Loop until jmp GetTarget_done # Match # Miss GetTarget_miss: mov rcx, [rcx] # I = I->NEXT cmp rcx, 0 # IF NULL == I je fail # Abort hard mov rsi, [rcx+16] # I->NAME mov rdi, [rip+scratch] # Reset scratch jmp GetTarget_loop GetTarget_done: mov rax, [rcx+8] # Get address ret ClearScratch: push rax # Protect against changes push rbx # And overwrites push rcx push rdx # While we work mov rbx, [rip+scratch] # Where our scratch area is mov al, 0 # Using null mov rdx, rbx # Get scratch add rdx, 0x800 # end of scratch area ClearScratch_loop: cmp rbx, rdx # Make sure je ClearScratch_end # we do not overflow mov rcx, [rbx] # Get current value mov [rbx], al # Because we want null add rbx, 1 # Increment cmp rcx, 0 # Check if we hit null jne ClearScratch_loop # Keep looping ClearScratch_end: pop rdx pop rcx # Don't Forget to pop rbx # Restore Damage pop rax # Entirely ret StorePointer: call Update_Pointer # Increment IP mov rbx, [rip+scratch] # Write to scratch call consume_token # get token push rax # Protect base_sep_p mov rax, [rip+scratch] # Pointer to scratch call GetTarget # Get address of pointer call ClearScratch # Clean up after ourselves mov rdx, r13 # base = IP pop rbx # Restore base_sep_p cmp rbx, 0x3E # If base_sep_p == '>' jne StorePointer_done # If not # Deal with %label>label case push rax # We need to preserve main target mov rbx, [rip+scratch] # Write to scratch call consume_token # get token mov rax, [rip+scratch] # Pointer to scratch call GetTarget # Get address of pointer call ClearScratch # Clean up after ourselves mov rdx, rax # Use our new base pop rax # Restore main target StorePointer_done: ret StorePointer_rel4: call StorePointer # Do Common sub rax, rdx # target - ip mov rdx, 4 # set the size of chars we want call print_chars jmp Second_pass StorePointer_rel2: call StorePointer # Do Common sub rax, rdx # target - ip mov rdx, 2 # set the size of chars we want call print_chars jmp Second_pass StorePointer_rel1: call StorePointer # Do Common sub rax, rdx # target - ip mov rdx, 1 # set the size of chars we want call print_chars jmp Second_pass StorePointer_abs4: call StorePointer # Do Common mov rdx, 4 # set the size of chars we want call print_chars jmp Second_pass StorePointer_abs2: call StorePointer # Do Common mov rdx, 2 # set the size of chars we want call print_chars jmp Second_pass fail: mov rax, 1 # Set exit code 1 jmp terminate Done: xor eax, eax # Set exit code 0 terminate: # Free pool push rax # save exit code push r10 # protect fout push rax # allocate shadow stack space for UEFI function mov r14, [rip+SystemBoot] # get system->boot mov rcx, [rip+scratch] # arg1 = scratch call [r14+72] # system->boot->free_pool(scratch) mov rcx, r12 # arg1 = structs call [r14+72] # system->boot->free_pool(structs) mov rcx, [rip+fin] # arg1 = fin call [rcx+16] # fin->close(fin) pop rax # deallocate stack pop rcx # restore fout push rax # allocate shadow stack space for UEFI function call [rcx+16] # fout->close(fout) pop rax # deallocate stack pop rax # restore exit code abort: # used for debugging only mov rsp, rbp # restore stack ret # return to UEFI # 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+64] # system->boot->allocate_pool(EFI_LOADER_DATA, 2048, &pool) add rsp, 24 # deallocate stack pop rax # get pool ret .data # Protocol GUIDs LOADED_IMAGE_PROTOCOL: .long 0x5b1b31a1 .short 0x9562 .short 0x11d2 .byte 0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b SIMPLE_FS_PROTOCOL: .long 0x0964e5b22 .short 0x6459 .short 0x11d2 .byte 0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b fin: .long 0, 0 scratch: .long 0, 0 SystemBoot: .long 0, 0