stage0-uefi/amd64/catm.hex2

424 lines
20 KiB
Plaintext

# SPDX-FileCopyrightText: 2022 Andrius Štikonas <andrius@stikonas.eu>
# SPDX-FileCopyrightText: 2019 Jeremiah Orians
#
# SPDX-License-Identifier: GPL-3.0-or-later
:PE32_base
# 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
@OptionalHeader_end>PE_header # 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 00 10 00 # SizeOfImage
%PE32_text>PE32_base # 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
# [0x130]
:OptionalHeader_end
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
2E 74 65 78 74 00 00 00 ; Name of the section: ".text"
%ELF_end>PE32_text ; VirtualSize
00 10 00 00 ; VirtualAddress
%ELF_end>PE32_text ; 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
4989CC ; mov_r12,rcx # save image_handle
4C8B72 60 ; mov_r14,[rdx+BYTE] !96 # save system->boot
# Open Loaded Image protocol
4D89E1 ; mov_r9,r12 # arg4 = image_handle
488D15 %LOADED_IMAGE_PROTOCOL ; lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL
4C89E1 ; mov_rcx,r12 # arg1 = image_handle
E8 %open_protocol ; call %open_protocol # open protocol
4889C7 ; mov_rdi,rax # save image
# Get root file system
4D89E1 ; mov_r9,r12 # 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
48890D %root_device ; mov_[rip+DWORD],rcx %root_device # save root_device
E8 %open_protocol ; call %open_protocol # open protocol
4889C1 ; mov_rcx,rax # get rootfs
# Get root directory
488D15 %rootdir ; 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
:loop_options
4839D3 ; cmp_rbx,rdx # Check if we are done
74 !loop_options_done ; 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 != ' '
75 !loop_options ; 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
EB !loop_options ; jmp !loop_options # next argument
:loop_options_done
4158 ; pop_r8 # get output file
4D85C0 ; test_r8,r8 # if no output file
0F84 %exit_early ; je32 %exit_early # then exit
# Open file for writing
52 ; push_rdx # allocate stack for fout
4889E2 ; mov_rdx,rsp # arg2 = &fout
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
50 ; push_rax # align stack
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 %rootdir ; 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()
488B6424 38 ; mov_rsp,[rsp+BYTE] !56 # deallocate stack
415F ; pop_r15 # get fout
48C7C2 00001000 ; mov_rdx, %0x100000 # Allocate 1MB
E8 %allocate_pool ; call %allocate_pool # Get memory pool
4889C3 ; mov_rbx,rax # Save it
:core
4158 ; pop_r8 # Get the actual input name
4983F8 00 ; cmp_r8, !0 # Check for null string
74 !done ; je !done # We are done if null string
E8 %open_file ; call %open_file # Open file as read only
85C0 ; test_eax,eax # check if successfully opened
75 !core ; jne !core # Else go to another file
4989F5 ; mov_r13,rsi # Protect fin
:keep
48C7C2 00001000 ; mov_rdx, %0x100000 # set the size of chars we want
E8 %read ; call %read # read them
50 ; push_rax # Protect the number of bytes read
4889C2 ; mov_rdx,rax # number of bytes to write
E8 %write ; call %write # write them
58 ; pop_rax # Get bytes read
483D 00001000 ; cmp_rax, %0x100000 # Check if buffer was fully used
74 !keep ; je !keep # Keep looping if it was full
4C89E9 ; mov_rcx,r13 # fin
E8 %close_file ; call %close_file # close file
EB !core ; jmp !core # Move to next file
:done
4C89F9 ; mov_rcx,r15 # Get output file
E8 %close_file ; call %close_file # close it
4889D9 ; mov_rcx,rbx # Get buffer
E8 %free_pool ; call %free_pool # release it
:exit_early
488B0D %rootdir ; mov_rcx,[rip+DWORD] %rootdir # Get rootdir
E8 %close_file ; call %close_file # close it
4D89E0 ; mov_r8,r12 # arg3 = image_handle
488D15 %SIMPLE_FS_PROTOCOL ; lea_rdx,[rip+DWORD] %SIMPLE_FS_PROTOCOL # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID
488B0D %root_device ; mov_rcx,[rip+DWORD] %root_device # arg1 = root_device
E8 %close_protocol ; call %close_protocol # close protocol
4D89E0 ; mov_r8,r12 # arg3 = image_handle
488D15 %LOADED_IMAGE_PROTOCOL ; lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # EFI_LOADED_IMAGE_PROTOCOL_GUID
4C89E1 ; mov_rcx,r12 # arg1 = image_handle
E8 %close_protocol ; call %close_protocol # close protocol
# 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
# r8: input file name
# returns input file handle in rsi, status in rax
:open_file
# Open file for reading
52 ; push_rdx # allocate stack for fin
4889E2 ; mov_rdx,rsp # arg2 = &fin
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
50 ; push_rax # align stack
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 %rootdir ; 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()
488B6424 38 ; mov_rsp,[rsp+BYTE] !56 # deallocate stack
5E ; pop_rsi # get fin
C3 ; ret
# rcx: file handle
: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
: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
: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
: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
# rcx: memory pool
# r14: system->boot
:free_pool
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(pool)
488B6424 28 ; mov_rsp,[rsp+BYTE] !40 # deallocate stack
C3 ; ret
# rdx: number of bytes to read
# rbx: buffer
# r13: input file handle
# returns number of bytes read in rax
:read
4C89E9 ; mov_rcx,r13 # arg1 = fin
52 ; push_rdx # set size
4889E2 ; mov_rdx,rsp # arg2 = &size
4989D8 ; mov_r8,rbx # arg3 = buffer
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 size to rax
C3 ; ret
# rdx: number of bytes to write
# rbx: buffer
# r14: output file handle
# returns number of bytes written in rax
:write
4C89F9 ; mov_rcx,r15 # arg1 = fout
52 ; push_rdx # set size
4889E2 ; mov_rdx,rsp # arg2 = &size
4989D8 ; mov_r8,rbx # arg3 = buffer
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 28 ; call_[rcx+BYTE] !40 # fin->write()
488B6424 28 ; mov_rsp,[rsp+BYTE] !40 # deallocate stack
58 ; pop_rax # save size to rax
C3 ; ret
# 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 ; %0x964e5b22
59 64 ; $0x6459
D2 11 ; $0x11d2
8E 39 00 A0 C9 69 72 3B ; !0x8e !0x39 !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b
:rootdir
00000000 00000000
:root_device
00000000 00000000
:ELF_end