stage0-uefi/amd64/hex2.hex1

862 lines
35 KiB
Plaintext

# SPDX-FileCopyrightText: 2022 Andrius Štikonas <andrius@stikonas.eu>
# SPDX-FileCopyrightText: 2017 Jeremiah Orians <jeremiah@pdp10.guru>
#
# SPDX-License-Identifier: GPL-3.0-or-later
# Register usage:
# R15 => Flag
# R14 => High bits
# R13 => IP
# R12 => MALLOC
# R11 => HEAD
# Struct format: (size 24)
# NEXT => 0
# TARGET => 8
# NAME => 16
# DOS MZ header
4D 5A # Signature
00 00 # Number of bytes in the last page.
00 00 # Number of whole/partial pages
00 00 # Number of entries in the relocation table.
00 00 # Header size
00 00 # Minimum allocation
00 00 # Maximum allocation
00 00 # Relocatable segment address for SS
00 00 # Initial value for SP
00 00 # Checksum (I don't think is looked at)
00 00 # Initial value for IP (Seems ignored)
00 00 # Relocatable segment address for CS (Seems ignored)
00 00 # The (absolute) offset to the relocation table.
00 00 # Value used for overlay management. If zero, this is the main executable
00 00 00 00 00 00 00 00 # Reserved in PE
00 00 # OEM identifier
00 00 # OEM info
00 00 00 00 00 00 00 00 00 00 # The required reserved 20 bytes of NULLS
00 00 00 00 00 00 00 00 00 00
40 00 00 00 # Starting address of the PE header
# [0x40]
# PE header
50 45 00 00 # Signature "PE"
64 86 # Machine
01 00 # number of sections
00 00 00 00 # Timestamp supposedly
00 00 00 00 # PointerToSymbolTable
00 00 00 00 # number of symbols
F0 00 # SizeOfOptionalHeader
00 00 # 'Characteristics'
# [0x58]
# COFF header bits
0B 02 # Magic PE32+ (64 bit)
00 00 # Linker version
00 00 00 00 # size of code
00 00 00 00 # sizeOfInitializedData
00 00 00 00 # SizeOfUninitializedData
00 10 00 00 # AddressOfEntryPoint
00 00 00 00 # BaseOfCode
00 00 00 00 00 00 00 00 # ImageBase
01 00 00 00 # SectionAlignment
01 00 00 00 # FileAlignment
00 00 00 00 # OperatingSystemVersion
00 00 00 00 # ImageVersion
00 00 00 00 # SubsystemVersion
00 00 00 00 # Win32VersionValue
00 20 00 00 # SizeOfImage
70 01 00 00 # SizeOfHeaders
00 00 00 00 # CheckSum (isn't used at all)
0A 00 # Subsystem
00 00 # DllCharacteristics
00 00 00 00 # SizeOfStackReserve
00 00 00 00 # SizeOfStackCommit
00 00 00 00 # SizeOfHeapReserve
00 00 00 00 # SizeOfHeapCommit
00 00 00 00 # LoaderFlags
00 00 00 00 # NumberOfRvaAndSizes
# [0xB8]
# Data directories (has to be 16 entries always 16bytes per entry)
00 00 00 00 # Export Table
00 00 00 00 # Size of Export Table
00 00 00 00 # Import Table
10 00 00 00 # Size of Import Table
00 00 00 00 # Resource Table
00 00 00 00 # Size of Resource Table
00 00 00 00 # Exception Table
00 00 00 00 # Size of Exception Table
00 00 00 00 # Certificate Table
00 00 00 00 # Size of Certificate Table
00 00 00 00 # Base Relocation Table
00 00 00 00 # Size of Base Relocation Table
00 00 00 00 # Debug Table
00 00 00 00 # Size of Debug Table
00 00 00 00 # Architecture Data Table
00 00 00 00 # Size of Architecture Data Table
00 00 00 00 # Global Pointer
00 00 00 00 # NULL
00 00 00 00 # TLS Table
00 00 00 00 # Size of TLS Table
00 00 00 00 # Load Config Table
00 00 00 00 # Size of Load Config Table
00 00 00 00 # Bound Import Table
00 00 00 00 # Size of Bound Import Table
00 00 00 00 # Import Address Table
00 00 00 00 # Size of Import Address Table
00 00 00 00 # Delay Import Descriptor Table
00 00 00 00 # Size of Delay Import Descriptor Table
00 00 00 00 # CLR Runtime header table
00 00 00 00 # Size of CLR Runtime Header table
00 00 00 00 00 00 00 00 # MUST BE NULL
# no idea what it is yet
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
# [0x148]
# Start of section headers
00 00 00 00 00 00 00 00 ; Name of the section (empty) but could set to ".text"
52 07 00 00 ; VirtualSize
00 10 00 00 ; VirtualAddress
52 07 00 00 ; SizeOfRawData
70 01 00 00 ; PointerToRawData
00 00 00 00 ; PointerToRelocations
00 00 00 00 ; PointerToLinenumbers
00 00 ; NumberOfRelocations
00 00 ; NumberOfLinenumbers
00 00 00 00 ; 'Characteristics'
# [0x170]
#:PE32_text
# efi_main(void *image_handle, struct efi_system_table *system)
#:_start
# Save non-volatile registers
55 ; push_rbp
53 ; push_rbx
57 ; push_rdi
56 ; push_rsi
4154 ; push_r12
4155 ; push_r13
4156 ; push_r14
4157 ; push_r15
4889E5 ; mov_rbp,rsp # save stack pointer
48890D %5 ; mov_[rip+DWORD],rcx %image_handle # save image_handle
4C8B72 60 ; mov_r14,[rdx+BYTE] !96 # system->boot
4C8935 %4 ; mov_[rip+DWORD],r14 %SystemBoot # save system->boot
# Open Loaded Image protocol
4C8B0D %5 ; mov_r9,[rip+DWORD] %image_handle # arg4 = image_handle
488D15 %Y ; lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL
4C89C9 ; mov_rcx,r9 # arg1 = image_handle
E8 %V ; call %open_protocol # open protocol
4889C7 ; mov_rdi,rax # save image
# Get root file system
4C8B0D %5 ; mov_r9,[rip+DWORD] %image_handle # arg4 = image_handle
488D15 %Z ; lea_rdx,[rip+DWORD] %SIMPLE_FS_PROTOCOL # guid = &SIMPLE_FS_PROTOCOL
488B4F 18 ; mov_rcx,[rdi+BYTE] !24 # arg1 = root_device = image->device
48890D %6 ; mov_[rip+DWORD],rcx %root_device # save root_device
E8 %V ; call %open_protocol # open protocol
4889C1 ; mov_rcx,rax # get rootfs
# Get root directory
488D15 %3 ; lea_rdx,[rip+DWORD] %rootdir # arg2 = &rootdir
4883EC 28 ; sub_rsp, !40 # allocate shadow stack space for UEFI function
FF51 08 ; call_[rcx+BYTE] !8 # rootfs->open_volume(rootfs, &rootdir)
4883C4 28 ; add_rsp, !40 # deallocate stack
# 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
:a #:loop_options
4839D3 ; cmp_rbx,rdx # Check if we are done
0F84 %b ; 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 != ' '
0F85 %a ; 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
E9 %a ; jmp %loop_options # next argument
:b #:loop_options_done
4158 ; pop_r8 # arg3 = in
415C ; pop_r12 # file out
4883E4 F0 ; and_rsp, !-16 # align stack to 16 bytes
# Open file for reading
6A 01 ; push !1 # Set exit code in case of failure
4983F8 00 ; cmp_r8, !0 # If NULL
0F84 %8 ; je %failed_input # then exit
488D15 %0 ; lea_rdx,[rip+DWORD] %fin # 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
488B0D %3 ; mov_rcx,[rip+DWORD] %rootdir # arg1 = rootdir
4883EC 20 ; sub_rsp, !32 # allocate shadow stack space for UEFI function
FF51 08 ; call_[rcx+BYTE] !8 # rootdir->open()
4883F8 00 ; cmp_rax, !0 # If failed to open
0F85 %8 ; jne %failed_input # then exit
4883C4 30 ; add_rsp, !48 # deallocate stack
# Open file for writing
4D89E0 ; mov_r8,r12 # arg3 = out
6A 01 ; push !1 # Set exit code in case of failure
4983F8 00 ; cmp_r8, !0 # If NULL
0F84 %7 ; je %failed_output # then exit
488D15 %1 ; lea_rdx,[rip+DWORD] %fout # 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
488B0D %3 ; mov_rcx,[rip+DWORD] %rootdir # arg1 = rootdir
4883EC 20 ; sub_rsp, !32 # allocate shadow stack space for UEFI function
FF51 08 ; call_[rcx+BYTE] !8 # rootdir->open()
4883C4 30 ; add_rsp, !48 # deallocate stack
# Allocate ourselves 16 MiB of memory
48C7C2 00000001 ; mov_rdx, %0x1000000 # allocate 16 MiB of memory
E8 %X ; call %allocate_pool
488905 %2 ; mov_[rip+DWORD],rax %scratch # Allocate space for scratch area
4805 00080000 ; add_rax, %0x800 # 2 KiB of scratch
4989C4 ; mov_r12,rax # save structs pointer
E8 %H ; call %ClearScratch # Zero scratch
49C7C7 FFFFFFFF ; mov_r15, %-1 # Our flag for byte processing
49C7C6 00000000 ; mov_r14, %0 # temp storage for the sum
49C7C5 00006000 ; mov_r13, %0x00600000 # Our starting IP
49C7C3 00000000 ; mov_r11, %0 # HEAD = NULL
E8 %c ; call %First_pass # Process it
# rewind input file
4153 ; push_r11 # Protect r11
488B0D %0 ; mov_rcx,[rip+DWORD] %fin # Using our input file
31D2 ; xor_edx,edx # Offset Zero
4883EC 28 ; sub_rsp, !40 # allocate shadow stack space for UEFI function
FF51 38 ; call_[rcx+BYTE] !56 # fin->set_position(fin, 0)
4883C4 28 ; add_rsp, !40 # deallocate stack
415B ; pop_r11 # restore r11
49C7C7 FFFFFFFF ; mov_r15, %-1 # Our flag for byte processing
49C7C6 00000000 ; mov_r14, %0 # temp storage for the sum
49C7C5 00006000 ; mov_r13, %0x00600000 # Our starting IP
E8 %m ; call %Second_pass # Process it
E9 %S ; jmp %Done
:c #:First_pass
E8 %x ; call %Read_byte
# Deal with EOF
4883F8 FC ; cmp_rax, !-4
0F84 %k ; je %First_pass_done
# Check for :
4883F8 3A ; cmp_rax, !0x3A
0F85 %d ; jne %First_pass_0
# Deal with label
E8 %C ; call %StoreLabel
:d #:First_pass_0
# Check for !
4883F8 21 ; cmp_rax, !0x21
0F84 %j ; je %First_pass_pointer
# Check for @
4883F8 40 ; cmp_rax, !0x40
0F84 %j ; je %First_pass_pointer
# Check for $
4883F8 24 ; cmp_rax, !0x24
0F84 %j ; je %First_pass_pointer
# Check for %
4883F8 25 ; cmp_rax, !0x25
0F84 %j ; je %First_pass_pointer
# Check for &
4883F8 26 ; cmp_rax, !0x26
0F84 %j ; je %First_pass_pointer
# Deal with everything else
E8 %l ; call %hex # Process our char
# Deal with EOF
4883F8 FC ; cmp_rax, !-4
0F84 %k ; je %First_pass_done
# deal with -1 values
4883F8 00 ; cmp_rax, !0
0F8C %c ; jl %First_pass
# deal with toggle
4983FF 00 ; cmp_r15, !0
0F84 %e ; je %First_pass_1
4983C5 01 ; add_r13, !1 # Increment IP
:e #:First_pass_1
49F7D7 ; not_r15
E9 %c ; jmp %First_pass
:f #:Update_Pointer
# Check for !
4883F8 21 ; cmp_rax, !0x21
0F84 %i ; je %Update_Pointer_1
# Check for @
4883F8 40 ; cmp_rax, !0x40
0F84 %h ; je %Update_Pointer_2
# Check for $
4883F8 24 ; cmp_rax, !0x24
0F84 %h ; je %Update_Pointer_2
# Check for %
4883F8 25 ; cmp_rax, !0x25
0F84 %g ; je %Update_Pointer_4
# Check for &
4883F8 26 ; cmp_rax, !0x26
0F84 %g ; je %Update_Pointer_4
# deal with bad input
E8 %R ; call %fail
:g #:Update_Pointer_4
4983C5 02 ; add_r13, !2 # Increment IP
:h #:Update_Pointer_2
4983C5 01 ; add_r13, !1 # Increment IP
:i #:Update_Pointer_1
4983C5 01 ; add_r13, !1 # Increment IP
C3 ; ret
:j #:First_pass_pointer
# Deal with Pointer to label
E8 %f ; call %Update_Pointer # Increment IP
488B1D %2 ; mov_rbx,[rip+DWORD] %scratch # Using scratch
E8 %A ; call %consume_token # Read token
E8 %H ; call %ClearScratch # Throw away token
4883F8 3E ; cmp_rax, !0x3E # check for '>'
0F85 %c ; jne %First_pass # Loop again
# Deal with %label>label case
488B1D %2 ; mov_rbx,[rip+DWORD] %scratch # Write to scratch
E8 %A ; call %consume_token # get token
E8 %H ; call %ClearScratch # Clean up after ourselves
E9 %c ; jmp %First_pass # Loop again
:k #:First_pass_done
C3 ; ret
:l #:hex
# deal with EOF
4883F8 FC ; cmp_rax, !-4
0F84 %p ; je %EOF
# deal with line comments starting with #
4883F8 23 ; cmp_rax, !0x23
0F84 %u ; je %ascii_comment
# deal with line comments starting with ;
4883F8 3B ; cmp_rax, !0x3B
0F84 %u ; je %ascii_comment
# deal all ascii less than 0
4883F8 30 ; cmp_rax, !0x30
0F8C %t ; jl %ascii_other
# deal with 0-9
4883F8 3A ; cmp_rax, !0x3A
0F8C %q ; jl %ascii_num
# deal with all ascii less than A
4883F8 41 ; cmp_rax, !0x41
0F8C %t ; jl %ascii_other
# deal with A-F
4883F8 47 ; cmp_rax, !0x47
0F8C %s ; jl %ascii_high
# deal with all ascii less than a
4883F8 61 ; cmp_rax, !0x61
0F8C %t ; jl %ascii_other
# deal with a-f
4883F8 67 ; cmp_rax, !0x67
0F8C %r ; jl %ascii_low
# The rest that remains needs to be ignored
E9 %t ; jmp %ascii_other
:m #:Second_pass
E8 %x ; call %Read_byte
# Deal with EOF
4883F8 FC ; cmp_rax, !-4
0F84 %p ; je %Second_pass_done
# Simply drop the label
4883F8 3A ; cmp_rax, !0x3A
0F85 %n ; jne %Second_pass_0
488B1D %2 ; mov_rbx,[rip+DWORD] %scratch # Using scratch
E8 %A ; call %consume_token # Read token
E8 %H ; call %ClearScratch # Throw away token
E9 %m ; jmp %Second_pass
:n #:Second_pass_0
# Deal with % pointer
4883F8 25 ; cmp_rax, !0x25
0F84 %M ; je %StorePointer_rel4
# Deal with @ pointer
4883F8 40 ; cmp_rax, !0x40
0F84 %N ; je %StorePointer_rel2
# Deal with ! pointer
4883F8 21 ; cmp_rax, !0x21
0F84 %O ; je %StorePointer_rel1
# Deal with & pointer
4883F8 26 ; cmp_rax, !0x26
0F84 %P ; je %StorePointer_abs4
# Deal with $ pointer
4883F8 24 ; cmp_rax, !0x24
0F84 %Q ; je %StorePointer_abs2
:o #:Second_pass_1
# Deal with everything else
E8 %l ; call %hex # Process our char
# Deal with EOF
4883F8 FC ; cmp_rax, !-4
0F84 %p ; je %Second_pass_done
# deal with -1 values
4883F8 00 ; cmp_rax, !0
0F8C %m ; jl %Second_pass
# deal with toggle
4983FF 00 ; cmp_r15, !0
0F84 %w ; je %print
# process first byte of pair
4989C6 ; mov_r14,rax
49C7C7 00000000 ; mov_r15, %0
E9 %m ; jmp %Second_pass
:p #:Second_pass_done
#:EOF
C3 ; ret
:q #:ascii_num
83E8 30 ; sub_rax, !0x30
C3 ; ret
:r #:ascii_low
83E8 57 ; sub_rax, !0x57
C3 ; ret
:s #:ascii_high
83E8 37 ; sub_rax, !0x37
C3 ; ret
:t #:ascii_other
48C7C0 FFFFFFFF ; mov_rax, %-1
C3 ; ret
:u #:ascii_comment
E8 %x ; call %Read_byte
4883F8 0D ; cmp_rax, !0x0D
0F84 %v ; je %ascii_comment_cr
4883F8 0A ; cmp_rax, !0x0A
0F85 %u ; jne %ascii_comment
:v #:ascii_comment_cr
48C7C0 FFFFFFFF ; mov_rax, %-1
C3 ; ret
# process second byte of pair
:w #: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
# Print our first Hex
48C7C2 01000000 ; mov_rdx, %1 # set the size of chars we want
E8 %z ; call %print_chars
4983C5 01 ; add_r13, !1 # Increment IP
E9 %m ; jmp %Second_pass
:x #:Read_byte
4153 ; push_r11 # Protect r11
488B0D %0 ; mov_rcx,[rip+DWORD] %fin # arg1 = fin
6A 01 ; push !1 # size = 1
4889E2 ; mov_rdx,rsb # arg2 = &size
31F6 ; xor_esi,esi # zero rsi
56 ; push_rsi # allocate stack
4989E0 ; mov_rsp,r8 # arg3 = &input
54 ; push_rsp # align stack to 16 bytes
FF3424 ; push_[rsp] # align stack to 16 bytes
4883E4 F0 ; and_rsp, !-16 # align stack to 16 bytes
4883EC 20 ; sub_rsp, !32 # allocate shadow stack space for UEFI function
FF51 20 ; call_[rcx+BYTE] !32 # fin->read()
488B6424 28 ; mov_rsp,[rsp+BYTE] !40 # deallocate stack
58 ; pop_rax # save input to rax
5E ; pop_rsi # save size to rsi
415B ; pop_r11 # restore r11
# If the file ended (0 bytes read) return EOF
85F6 ; test_esi,esi
0F85 %y ; jne %Read_byte_1
48C7C0 FCFFFFFF ; mov_rax, %-4 # Put EOF in rax
:y #:Read_byte_1
C3 ; ret
# Writes bytes stored in rax
:z #:print_chars
4153 ; push_r11 # Protect r11
54 ; push_rsp # align stack to 16 bytes
FF3424 ; push_[rsp] # align stack to 16 bytes
4883E4 F0 ; and_rsp, !-16 # align stack to 16 bytes
488B0D %1 ; mov_rcx,[rip+DWORD] %fout # arg1 = fout
52 ; push_rdx # set size
4889E2 ; mov_rdx,rsp # arg2 = &size
50 ; push_rax # allocate stack
4989E0 ; mov_rsp,r8 # arg3 = &output
4883EC 20 ; sub_rsp, !32 # allocate shadow stack space for UEFI function
FF51 28 ; call_[rcx+BYTE] !40 # fout->write()
488B6424 38 ; mov_rsp,[rsp+BYTE] !56 # deallocate stack
415B ; pop_r11 # restore r11
C3 ; ret
# Receives pointer in RBX
# Writes out char and updates RBX
:A #:consume_token
E8 %x ; call %Read_byte # Consume_token
# Check for \t
4883F8 09 ; cmp_rax, !0x09
0F84 %B ; je %consume_token_done
# Check for \n
4883F8 0A ; cmp_rax, !0x0A
0F84 %B ; je %consume_token_done
# Check for ' '
4883F8 20 ; cmp_rax, !0x20
0F84 %B ; je %consume_token_done
# Check for '>'
4883F8 3E ; cmp_rax, !0x3E
0F84 %B ; je %consume_token_done
# Looks like we are still reading token
8803 ; mov_[rbx],al # Store char
4883C3 01 ; add_rbx, !1 # Point to next spot
E9 %A ; jmp %consume_token # loop until done
:B #:consume_token_done
48C7C1 00000000 ; mov_rcx, %0 # Pad with nulls
48890B ; mov_[rbx],rcx
4883C3 08 ; add_rbx, !8
C3 ; ret
:C #:StoreLabel
4C89E0 ; mov_rax,r12 # ENTRY
4983C4 18 ; add_r12, !24 # CALLOC
4C8968 08 ; mov_[rax+BYTE],r13 !8 # ENTRY->TARGET = IP
4C8918 ; mov_[rax],r11 # ENTRY->NEXT = JUMP_TABLE
4989C3 ; mov_r11,rax # JUMP_TABLE = ENTRY
4D8963 10 ; mov_[r11+BYTE],r12 !16 # ENTRY->NAME = TOKEN
4C89E3 ; mov_rbx,r12 # Write Starting after struct
E8 %A ; call %consume_token # Collect whole string
4989DC ; mov_r12,rbx # Update HEAP
E9 %c ; jmp %First_pass
:D #:GetTarget
488B3D %2 ; mov_rdi,[rip+DWORD] %scratch # Reset scratch
4C89D9 ; mov_rcx,r11 # Grab JUMP_TABLE
488B71 10 ; mov_rsi,[rcx+BYTE] !16 # I->NAME
:E #:GetTarget_loop
8A06 ; mov_al,[rsi] # I->NAME[0]
8A1F ; mov_bl,[rdi] # scratch[0]
480FB6DB ; movzx_rbx,bl # Zero extend
480FB6C0 ; movzx_rax,al # Zero extend
38D8 ; cmp_al,bl # IF TOKEN == I->NAME
0F85 %F ; jne %GetTarget_miss # Oops
4883C6 01 ; add_rsi, !1
4883C7 01 ; add_rdi, !1
3C 00 ; cmp_al, !0
0F85 %E ; jne %GetTarget_loop # Loop until
E9 %G ; jmp %GetTarget_done # Match
# Miss
:F #:GetTarget_miss
488B09 ; mov_rcx,[rcx] # I = I->NEXT
4883F9 00 ; cmp_rcx, !0 # IF NULL == I
0F84 %R ; je %fail # Abort hard
488B71 10 ; mov_rsi,[rcx+BYTE] !16 # I->NAME
488B3D %2 ; mov_rdi,[rip+DWORD] %scratch # Reset scratch
E9 %E ; jmp %GetTarget_loop
:G #:GetTarget_done
488B41 08 ; mov_rax,[rcx+BYTE] !8 # Get address
C3 ; ret
:H #:ClearScratch
50 ; push_rax # Protect against changes
53 ; push_rbx # And overwrites
51 ; push_rcx
52 ; push_rdx # While we work
488B1D %2 ; mov_rbx,[rip+DWORD] %scratch # Where our table is
B0 00 ; mov_al, !0 # Using null
4889DA ; mov_rdx,rbx # Get scratch
4881C2 00080000 ; add_rdx, %0x800 # end of scratch area
:I #:ClearScratch_loop
4839D3 ; cmp_rbx,rdx # Make sure
0F84 %J ; je %ClearScratch_end # we do not overflow
488B0B ; mov_rcx,[rbx] # Get current value
8803 ; mov_[rbx],al # Because we want null
4883C3 01 ; add_rbx, !1 # Increment
4883F9 00 ; cmp_rcx, !0 # Check if we hit null
0F85 %I ; jne %ClearScratch_loop # Keep looping
:J #:ClearScratch_end
5A ; pop_rdx
59 ; pop_rcx # Don't Forget to
5B ; pop_rbx # Restore Damage
58 ; pop_rax # Entirely
C3 ; ret
:K #:StorePointer
E8 %f ; call %Update_Pointer # Increment IP
488B1D %2 ; mov_rbx,[rip+DWORD] %scratch # Write to scratch
E8 %A ; call %consume_token # get token
50 ; push_rax # Protect base_sep_p
488B05 %2 ; mov_rax,[rip+DWORD] %scratch # Pointer to scratch
E8 %D ; call %GetTarget # Get address of pointer
E8 %H ; call %ClearScratch # Clean up after ourselves
4C89EA ; mov_rdx,r13 # base = IP
5B ; pop_rbx # Restore base_sep_p
4883FB 3E ; cmp_rbx, !0x3E # If base_sep_p == '>'
0F85 %L ; jne %StorePointer_done # If not
# Deal with %label>label case
50 ; push_rax # We need to preserve main target
488B1D %2 ; mov_rbx,[rip+DWORD] %scratch # Write to scratch
E8 %A ; call %consume_token # get token
488B05 %2 ; mov_rax,[rip+DWORD] %scratch # Pointer to scratch
E8 %D ; call %GetTarget # Get address of pointer
E8 %H ; call %ClearScratch # Clean up after ourselves
4889C2 ; mov_rdx,rax # Use our new base
58 ; pop_rax # Restore main target
:L #:StorePointer_done
C3 ; ret
:M #:StorePointer_rel4
E8 %K ; call %StorePointer # Do Common
4829D0 ; sub_rax,rdx # target - ip
48C7C2 04000000 ; mov_rdx, %4 # set the size of chars we want
E8 %z ; call %print_chars
E9 %m ; jmp %Second_pass
:N #:StorePointer_rel2
E8 %K ; call %StorePointer # Do Common
4829D0 ; sub_rax,rdx # target - ip
48C7C2 02000000 ; mov_rdx, %2 # set the size of chars we want
E8 %z ; call %print_chars
E9 %m ; jmp %Second_pass
:O #:StorePointer_rel1
E8 %K ; call %StorePointer # Do Common
4829D0 ; sub_rax,rdx # target - ip
48C7C2 01000000 ; mov_rdx, %1 # set the size of chars we want
E8 %z ; call %print_chars
E9 %m ; jmp %Second_pass
:P #:StorePointer_abs4
E8 %K ; call %StorePointer # Do Common
48C7C2 04000000 ; mov_rdx, %4 # set the size of chars we want
E8 %z ; call %print_chars
E9 %m ; jmp %Second_pass
:Q #:StorePointer_abs2
E8 %K ; call %StorePointer # Do Common
48C7C2 02000000 ; mov_rdx, %2 # set the size of chars we want
E8 %z ; call %print_chars
E9 %m ; jmp %Second_pass
:R #:fail
48C7C0 01000000 ; mov_rax, %1 # Set exit code 1
E9 %T ; jmp %terminate
:S #:Done
31C0 ; xor_eax,eax # Set exit code 0
:T #:terminate
50 ; push_rax # save exit code
4C8B35 %4 ; mov_r14,[rip+DWORD] %SystemBoot # get system->boot
488B0D %2 ; mov_rcx,[rip+DWORD] %scratch # arg1 = scratch
54 ; push_rsp # align stack to 16 bytes
FF3424 ; push_[rsp] # align stack to 16 bytes
4883E4 F0 ; and_rsp, !-16 # align stack to 16 bytes
4883EC 20 ; sub_rsp, !32 # allocate shadow stack space for UEFI function
41FF56 48 ; call_[r14+BYTE] !72 # system->boot->free_pool(scratch)
488B6424 28 ; mov_rsp,[rsp+BYTE] !40 # deallocate stack
488B0D %1 ; mov_rcx,[rip+DWORD] %fout # get fout
E8 %U ; call %close_file # close fout
:7 # :failed_output
488B0D %0 ; mov_rcx,[rip+DWORD] %fin # get fin
E8 %U ; call %close_file # close fin
:8 # :failed_input
488B0D %3 ; mov_rcx,[rip+DWORD] %rootdir # get rootdir
E8 %U ; call %close_file # close rootdir
4C8B05 %5 ; mov_r8,[rip+DWORD] %image_handle # arg3 = image_handle
488D15 %Z ; lea_rdx,[rip+DWORD] %SIMPLE_FS_PROTOCOL # guid = &SIMPLE_FS_PROTOCOL
488B0D %6 ; mov_rcx,[rip+DWORD] %root_device # arg1 = root_device
E8 %W ; call %close_protocol # close protocol
4C8B05 %5 ; mov_r8,[rip+DWORD] %image_handle # arg3 = image_handle
488D15 %Y ; lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL
4C89C1 ; mov_rcx,r8 # arg1 = image_handle
E8 %W ; call %close_protocol # close protocol
58 ; pop_rax # restore exit code
# Restore non-volatile registers
4889EC ; mov_rsp,rbp
415F ; pop_r15
415E ; pop_r14
415D ; pop_r13
415C ; pop_r12
5E ; pop_rsi
5F ; pop_rdi
5B ; pop_rbx
5D ; pop_rbp
C3 ; ret # return to UEFI
# rcx: file handle
:U #:close_file
54 ; push_rsp # align stack to 16 bytes
FF3424 ; push_[rsp] # align stack to 16 bytes
4883E4 F0 ; and_rsp, !-16 # align stack to 16 bytes
4883EC 20 ; sub_rsp, !32 # allocate shadow stack space for UEFI function
FF51 10 ; call_[rcx+BYTE] !16 # file_handle->close(file_handle)
488B6424 28 ; mov_rsp,[rsp+BYTE] !40 # deallocate stack
C3 ; ret
# rcx: handle
# rdx: &guid
# r9: agent_handle
# returns interface
:V #:open_protocol
50 ; push_rax # allocate stack for interface
4989E0 ; mov_r8,rsp # arg3 = &interface
54 ; push_rsp # align stack to 16 bytes
FF3424 ; push_[rsp] # align stack to 16 bytes
4883E4 F0 ; and_rsp, !-16 # align stack to 16 bytes
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(handle, &guid, &interface, agent_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
488B6424 38 ; mov_rsp,[rsp+BYTE] !56 # deallocate stack
58 ; pop_rax # get interface
C3 ; ret
# rcx: handle
# rdx: &guid
# r8: agent_handle
:W #:close_protocol
54 ; push_rsp # align stack to 16 bytes
FF3424 ; push_[rsp] # align stack to 16 bytes
4883E4 F0 ; and_rsp, !-16 # align stack to 16 bytes
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(handle, &guid, agent_handle, 0)
488B6424 28 ; mov_rsp,[rsp+BYTE] !40 # deallocate stack
C3 ; ret
# rdx: number of bytes to allocate
# r14: system->boot
# returns pointer in rax
:X #:allocate_pool
52 ; push_rdx # allocate stack for pool pointer
4989E0 ; mov_r8,rsp # arg3 = &pool
6A 02 ; push !2
59 ; pop_rcx # arg1 = EFI_LOADER_DATA
54 ; push_rsp # align stack to 16 bytes
FF3424 ; push_[rsp] # align stack to 16 bytes
4883E4 F0 ; and_rsp, !-16 # align stack to 16 bytes
4883EC 20 ; sub_rsp, !32 # allocate shadow stack space for UEFI function
41FF56 40 ; call_[r14+BYTE] !64 # system->boot->allocate_pool(EFI_LOADER_DATA, 2048, &pool)
488B6424 28 ; mov_rsp,[rsp+BYTE] !40 # deallocate stack
58 ; pop_rax # get pool
C3 ; ret
# Protocol GUIDs
:Y #: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
:Z #:SIMPLE_FS_PROTOCOL
22 5B 4E 96 ; %0x964e5b22
59 64 ; $0x6459
D2 11 ; $0x11d2
8E 39 00 A0 C9 69 72 3B ; !0x8e !0x39 !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b
:0 #:fin
00000000 00000000
:1 #:fout
00000000 00000000
:2 #:scratch
00000000 00000000
:3 #:rootdir
00000000 00000000
:4 #:SystemBoot
00000000 00000000
:5 #:image_handle
00000000 00000000
:6 #:root_device
00000000 00000000
# :ELF_end