stage0-uefi/amd64/hex1.hex0

557 lines
25 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
# Some of the functions are deliberately inlined at the slight expense of
# binary size to avoid tricky jump calculations in hex0 code.
# 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"
09 04 00 00 ; VirtualSize
00 10 00 00 ; VirtualAddress
09 04 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]
# 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
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 C9030000 ; 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 B4030000 ; 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
4883EC 20 ; sub_rsp, !32 # allocate shadow stack space for UEFI function
FF51 08 ; call_[rcx+BYTE] !8 # rootfs->open_volume(rootfs, &rootdir)
4883C4 20 ; add_rsp, !32 # 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 [_start + 0x7F]
4839D3 ; cmp_rbx,rdx # Check if we are done
74 14 ; 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 F1 ; 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 E7 ; jmp8 !loop_options # next argument
# :loop_options_done [_start + 0x98]
4158 ; pop_r8 # arg3 = in
415C ; pop_r12 # file out
# Open file for reading
4883E4 F0 ; and_rsp, !-16 # align stack to 16 bytes
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
50 ; push_rax # align stack
# Open file for writing
4D89E0 ; mov_r8,r12 # 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
5B ; pop_rbx # 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
# 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 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, &table)
4883C4 20 ; add_rsp, !32 # deallocate stack
# [_start+0xF7]
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 2F000000 ; call %First_pass # Process it
# rewind input file
4889F9 ; mov_rcx,rdi # Using our input file
31D2 ; xor_edx,edx # Offset Zero
4883EC 20 ; sub_rsp, !32 # allocate shadow stack space for UEFI function
FF51 38 ; call_[rcx+BYTE] !56 # fin->set_position(fin, 0)
4883C4 20 ; add_rsp, !32 # 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 EF000000 ; call %Second_pass # Process it
E9 49020000 ; jmp %Done
# :First_pass [_start+0x140]
E8 BA010000 ; call %Read_byte
# Deal with EOF
483D FCFFFFFF ; cmp_rax, %-4
0F84 67000000 ; je %First_pass_done
# Check for :
483D 3A000000 ; cmp_rax, %0x3a
0F85 05000000 ; jne %First_pass_0
# Deal with label
E8 FF010000 ; call %StoreLabel
# :First_pass_0 [_start+0x162]
# Check for %
483D 25000000 ; cmp_rax, %0x25
0F84 39000000 ; je %First_pass_pointer
# Deal with everything else
E8 46000000 ; call %hex # Process our char
# Deal with EOF
483D FCFFFFFF ; cmp_rax, %-4
0F84 39000000 ; je %First_pass_done
# deal with -1 values
483D 00000000 ; cmp_rax, %0
0F8C B5FFFFFF ; jl %First_pass
# deal with toggle
4981FF 00000000 ; cmp_r15, %0
0F84 07000000 ; je %First_pass_1
4981C5 01000000 ; add_r13, %1 # Increment IP
# :First_pass_1 [_start+0x19F]
49F7D7 ; not_r15
E9 99FFFFFF ; jmp %First_pass
# :First_pass_pointer [_start+0x1A7]
# Deal with Pointer to label
E8 53010000 ; call %Read_byte # Drop the char
4981C5 04000000 ; add_r13, %4 # Increment IP
E9 88FFFFFF ; jmp %First_pass # Loop again
# :First_pass_done [_start+0x1B8]
C3 ; ret
# :hex [_start+0x1B9]
# deal with EOF
483D FCFFFFFF ; cmp_rax, %-4
0F84 DB000000 ; je %EOF
# deal with line comments starting with #
483D 23000000 ; cmp_rax, %0x23
0F84 E7000000 ; je %ascii_comment
# deal with line comments starting with ;
483D 3B000000 ; cmp_rax, %0x3b
0F84 DB000000 ; je %ascii_comment
# deal all ascii less than 0
483D 30000000 ; cmp_rax, %0x30
0F8C C7000000 ; jl %ascii_other
# deal with 0-9
483D 3A000000 ; cmp_rax, %0x3a
0F8C AC000000 ; jl %ascii_num
# deal with all ascii less than A
483D 41000000 ; cmp_rax, %0x41
0F8C AF000000 ; jl %ascii_other
# deal with A-F
483D 47000000 ; cmp_rax, %0x47
0F8C 9E000000 ; jl %ascii_high
# deal with all ascii less than a
483D 61000000 ; cmp_rax, %0x61
0F8C 97000000 ; jl %ascii_other
# deal with a-f
483D 67000000 ; cmp_rax, %0x67
0F8C 81000000 ; jl %ascii_low
# The rest that remains needs to be ignored
E9 86000000 ; jmp %ascii_other
# :Second_pass [_start+0x22A]
E8 D0000000 ; call %Read_byte
# Deal with EOF
483D FCFFFFFF ; cmp_rax, %-4
0F84 65000000 ; je %Second_pass_done
# Simply drop the label
483D 3A000000 ; cmp_rax, %0x3a
0F85 0A000000 ; jne %Second_pass_0
E8 B3000000 ; call %Read_byte
E9 D9FFFFFF ; jmp %Second_pass
# :Second_pass_0 [_start+0x251]
# Deal with % pointer
483D 25000000 ; cmp_rax, %0x25
0F85 0A000000 ; jne %Second_pass_1
E8 08010000 ; call %StorePointer
E9 C3FFFFFF ; jmp %Second_pass
# :Second_pass_1 [_start+0x267]
# Deal with everything else
E8 4DFFFFFF ; call %hex # Process our char
# Deal with EOF
483D FCFFFFFF ; cmp_rax, %-4
0F84 28000000 ; je %Second_pass_done
# deal with -1 values
483D 00000000 ; cmp_rax, %0
0F8C A6FFFFFF ; jl %Second_pass
# deal with toggle
4981FF 00000000 ; cmp_r15, %0
0F84 4C000000 ; je %print
# process first byte of pair
4989C6 ; mov_r14,rax
49C7C7 00000000 ; mov_r15, %0
E9 8AFFFFFF ; jmp %Second_pass
# :Second_pass_done [_start+0x2A0]
# :EOF
C3 ; ret
# :ascii_num [_start+0x2A1]
4883E8 30 ; sub_rax, !0x30
C3 ; ret
# :ascii_low [_start+0x2A6]
4883E8 57 ; sub_rax, !0x57
C3 ; ret
# :ascii_high [_start+0x2AB]
4883E8 37 ; sub_rax, !0x37
C3 ; ret
# :ascii_other [_start+0x2B0]
48C7C0 FFFFFFFF ; mov_rax, %-1
C3 ; ret
# :ascii_comment [_start+0x2B8]
E8 42000000 ; call %Read_byte
483D 0D000000 ; cmp_rax, %0xd
0F84 0C000000 ; je %ascii_comment_cr
483D 0A000000 ; cmp_rax, %0xa
0F85 E3FFFFFF ; jne %ascii_comment
# :ascii_comment_cr [_start+0x2D5]
48C7C0 FFFFFFFF ; mov_rax, %-1
C3 ; ret
# process second byte of pair
# :print [_start+0x2DD]
# 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 3C000000 ; call %print_chars
4981C5 01000000 ; add_r13, %1 # Increment IP
E9 2BFFFFFF ; jmp %Second_pass
# :Read_byte [_start+0x2FF]
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
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
# If the file ended (0 bytes read) return EOF
85F6 ; test_esi,esi # if size = 0
75 07 ; jne8 !Read_byte_1
48C7C0 FCFFFFFF ; mov_rax, %-4 # Put EOF in rax
# :Read_byte_1 [_start+0x32E]
C3 ; ret # return
# Writes bytes stored in rax
# :print_chars [_start+0x32F]
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
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
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
C3 ; ret # return
# :Get_table_target [_start+0x34F]
E8 ABFFFFFF ; 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 [_start+0x361]
E8 E9FFFFFF ; call %Get_table_target
4C8928 ; mov_[rax],r13 # Write out pointer to table
C3 ; ret
# :StorePointer [_start+0x36A]
4981C5 04000000 ; add_r13, %4 # Increment IP
E8 D9FFFFFF ; 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 A7FFFFFF ; call %print_chars
C3 ; ret
# :Done [_start+0x389]
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
4883EC 28 ; sub_rsp, !40 # 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 44000000 ; lea_rdx,[rip+DWORD] %SIMPLE_FS_PROTOCOL # guid = &SIMPLE_FS_PROTOCOL
4C89E9 ; mov_rcx,r13 # arg1 = root_device
4D31C9 ; xor_r9,r9 # arg4 = NULL
41FF96 20010000 ; call_[r14+DWORD] %288 # system->boot->close_protocol(root_device, &guid, image_handle, 0)
4D89F8 ; mov_r8,r15 # arg3 = image_handle
488D15 1D000000 ; 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)
# 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
# Protocol GUIDs
# :LOADED_IMAGE_PROTOCOL [_start+0x3E9]
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 [_start+0x3F9]
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
# :ELF_end [_start+0x409]