2022-08-09 20:04:52 +01:00
|
|
|
# SPDX-FileCopyrightText: 2022 Andrius Štikonas <andrius@stikonas.eu>
|
|
|
|
# SPDX-FileCopyrightText: 2017 Jeremiah Orians <jeremiah@pdp10.guru>
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2022-09-09 22:20:59 +01:00
|
|
|
# Some of the functions are deliberately inlined at the slight expense of
|
|
|
|
# binary size to avoid tricky jump calculations in hex0 code.
|
|
|
|
|
2022-08-09 20:04:52 +01:00
|
|
|
# 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 10 00 00 # BaseOfCode
|
|
|
|
00 00 00 80 01 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"
|
2022-09-01 01:00:56 +01:00
|
|
|
CF 03 00 00 ; VirtualSize
|
2022-08-09 20:04:52 +01:00
|
|
|
00 10 00 00 ; VirtualAddress
|
2022-09-01 01:00:56 +01:00
|
|
|
CF 03 00 00 ; SizeOfRawData
|
2022-08-09 20:04:52 +01:00
|
|
|
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
|
2022-09-01 01:00:56 +01:00
|
|
|
4889E5 ; mov_rbp,rsp # save stack pointer
|
|
|
|
4989CF ; mov_r15,rcx # save image_handle
|
|
|
|
4C8B72 60 ; mov_r14,[rdx+BYTE] !96 # system->boot
|
2022-08-09 20:04:52 +01:00
|
|
|
|
|
|
|
# Open Loaded Image protocol
|
2022-09-01 01:00:56 +01:00
|
|
|
4D89F9 ; mov_r9,r15 # arg4 = image_handle
|
2022-09-12 00:05:09 +01:00
|
|
|
488D15 9B030000 ; lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL
|
2022-09-01 01:00:56 +01:00
|
|
|
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)
|
2022-09-09 22:20:59 +01:00
|
|
|
4883C4 30 ; add_rsp, !48 # deallocate stack
|
|
|
|
5F ; pop_rdi # get image
|
2022-08-09 20:04:52 +01:00
|
|
|
|
|
|
|
# Get root file system
|
2022-09-01 01:00:56 +01:00
|
|
|
4D89F9 ; mov_r9,r15 # arg4 = image_handle
|
2022-09-12 00:05:09 +01:00
|
|
|
488D15 86030000 ; lea_rdx,[rip+DWORD] %SIMPLE_FS_PROTOCOL # guid = &SIMPLE_FS_PROTOCOL
|
2022-09-01 01:00:56 +01:00
|
|
|
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)
|
2022-09-09 22:20:59 +01:00
|
|
|
4883C4 30 ; add_rsp, !48 # deallocate stack
|
|
|
|
59 ; pop rcx # get rootfs
|
2022-08-09 20:04:52 +01:00
|
|
|
|
|
|
|
# Get root directory
|
2022-09-01 01:00:56 +01:00
|
|
|
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
|
2022-09-09 22:20:59 +01:00
|
|
|
# :loop_options [_start + 0x71]
|
2022-09-01 01:00:56 +01:00
|
|
|
4839D3 ; cmp_rbx,rdx # Check if we are done
|
2022-09-12 00:05:09 +01:00
|
|
|
74 14 ; je8 !loop_options_done # We are done
|
2022-09-01 01:00:56 +01:00
|
|
|
4883EB 02 ; sub_rbx, !2 # --options
|
|
|
|
8A03 ; mov_al,[rbx] # *options
|
|
|
|
3C 20 ; cmp_al, !0x20 # if *options != ' '
|
2022-09-09 22:20:59 +01:00
|
|
|
75 F1 ; jne8 !loop_options # then continue looping
|
2022-09-01 01:00:56 +01:00
|
|
|
C603 00 ; mov_[rbx], !0 # zero it
|
|
|
|
4883C3 02 ; add_rbx, !2 # ++options
|
|
|
|
53 ; push_rbx # push another argument onto stack
|
2022-09-09 22:20:59 +01:00
|
|
|
EB E7 ; jmp8 !loop_options # next argument
|
|
|
|
# :loop_options_done [_start + 0x8A]
|
2022-08-09 20:04:52 +01:00
|
|
|
|
|
|
|
# Open file for reading
|
2022-09-01 01:00:56 +01:00
|
|
|
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
|
2022-08-09 20:04:52 +01:00
|
|
|
|
|
|
|
# Open file for writing
|
2022-09-01 01:00:56 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
# [_start+0xe1]
|
|
|
|
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 2B000000 ; call %First_pass # Process it
|
2022-08-09 20:04:52 +01:00
|
|
|
|
|
|
|
# rewind input file
|
2022-09-01 01:00:56 +01:00
|
|
|
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
|
2022-08-09 20:04:52 +01:00
|
|
|
|
2022-09-01 01:00:56 +01:00
|
|
|
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
|
2022-08-09 20:04:52 +01:00
|
|
|
|
2022-09-01 01:00:56 +01:00
|
|
|
E9 34020000 ; jmp %Done
|
2022-08-09 20:04:52 +01:00
|
|
|
|
2022-09-01 01:00:56 +01:00
|
|
|
# :First_pass [_start+0x126]
|
|
|
|
E8 BA010000 ; call %Read_byte
|
2022-08-09 20:04:52 +01:00
|
|
|
|
|
|
|
# Deal with EOF
|
2022-09-01 01:00:56 +01:00
|
|
|
483D FCFFFFFF ; cmp_rax, %-4
|
|
|
|
0F84 67000000 ; je %First_pass_done
|
2022-08-09 20:04:52 +01:00
|
|
|
|
|
|
|
# Check for :
|
2022-09-01 01:00:56 +01:00
|
|
|
483D 3A000000 ; cmp_rax, %0x3a
|
|
|
|
0F85 05000000 ; jne %First_pass_0
|
2022-08-09 20:04:52 +01:00
|
|
|
|
|
|
|
# Deal with label
|
2022-09-01 01:00:56 +01:00
|
|
|
E8 EA010000 ; call %StoreLabel
|
2022-08-09 20:04:52 +01:00
|
|
|
|
2022-09-01 01:00:56 +01:00
|
|
|
# :First_pass_0 [_start+0x148]
|
2022-08-09 20:04:52 +01:00
|
|
|
# Check for %
|
2022-09-01 01:00:56 +01:00
|
|
|
483D 25000000 ; cmp_rax, %0x25
|
|
|
|
0F84 39000000 ; je %First_pass_pointer
|
2022-08-09 20:04:52 +01:00
|
|
|
|
|
|
|
# Deal with everything else
|
2022-09-01 01:00:56 +01:00
|
|
|
E8 46000000 ; call %hex # Process our char
|
2022-08-09 20:04:52 +01:00
|
|
|
|
|
|
|
# Deal with EOF
|
2022-09-01 01:00:56 +01:00
|
|
|
483D FCFFFFFF ; cmp_rax, %-4
|
|
|
|
0F84 39000000 ; je %First_pass_done
|
2022-08-09 20:04:52 +01:00
|
|
|
|
|
|
|
# deal with -1 values
|
2022-09-01 01:00:56 +01:00
|
|
|
483D 00000000 ; cmp_rax, %0
|
|
|
|
0F8C B5FFFFFF ; jl %First_pass
|
2022-08-09 20:04:52 +01:00
|
|
|
|
|
|
|
# deal with toggle
|
2022-09-01 01:00:56 +01:00
|
|
|
4981FF 00000000 ; cmp_r15, %0
|
|
|
|
0F84 07000000 ; je %First_pass_1
|
|
|
|
4981C5 01000000 ; add_r13, %1 # Increment IP
|
2022-08-09 20:04:52 +01:00
|
|
|
|
2022-09-01 01:00:56 +01:00
|
|
|
# :First_pass_1 [_start+0x185]
|
|
|
|
49F7D7 ; not_r15
|
|
|
|
E9 99FFFFFF ; jmp %First_pass
|
2022-08-09 20:04:52 +01:00
|
|
|
|
2022-09-01 01:00:56 +01:00
|
|
|
# :First_pass_pointer [_start+0x18D]
|
2022-08-09 20:04:52 +01:00
|
|
|
# Deal with Pointer to label
|
2022-09-01 01:00:56 +01:00
|
|
|
E8 53010000 ; call %Read_byte # Drop the char
|
|
|
|
4981C5 04000000 ; add_r13, %4 # Increment IP
|
|
|
|
E9 88FFFFFF ; jmp %First_pass # Loop again
|
2022-08-09 20:04:52 +01:00
|
|
|
|
2022-09-01 01:00:56 +01:00
|
|
|
# :First_pass_done [_start+0x19E]
|
|
|
|
C3 ; ret
|
2022-08-09 20:04:52 +01:00
|
|
|
|
2022-09-01 01:00:56 +01:00
|
|
|
# :hex [_start+0x19F]
|
2022-08-09 20:04:52 +01:00
|
|
|
# deal with EOF
|
2022-09-01 01:00:56 +01:00
|
|
|
483D FCFFFFFF ; cmp_rax, %-4
|
|
|
|
0F84 DB000000 ; je %EOF
|
2022-08-09 20:04:52 +01:00
|
|
|
# deal with line comments starting with #
|
2022-09-01 01:00:56 +01:00
|
|
|
483D 23000000 ; cmp_rax, %0x23
|
|
|
|
0F84 E7000000 ; je %ascii_comment
|
2022-08-09 20:04:52 +01:00
|
|
|
# deal with line comments starting with ;
|
2022-09-01 01:00:56 +01:00
|
|
|
483D 3B000000 ; cmp_rax, %0x3b
|
|
|
|
0F84 DB000000 ; je %ascii_comment
|
2022-08-09 20:04:52 +01:00
|
|
|
# deal all ascii less than 0
|
2022-09-01 01:00:56 +01:00
|
|
|
483D 30000000 ; cmp_rax, %0x30
|
|
|
|
0F8C C7000000 ; jl %ascii_other
|
2022-08-09 20:04:52 +01:00
|
|
|
# deal with 0-9
|
2022-09-01 01:00:56 +01:00
|
|
|
483D 3A000000 ; cmp_rax, %0x3a
|
|
|
|
0F8C AC000000 ; jl %ascii_num
|
2022-08-09 20:04:52 +01:00
|
|
|
# deal with all ascii less than A
|
2022-09-01 01:00:56 +01:00
|
|
|
483D 41000000 ; cmp_rax, %0x41
|
|
|
|
0F8C AF000000 ; jl %ascii_other
|
2022-08-09 20:04:52 +01:00
|
|
|
# deal with A-F
|
2022-09-01 01:00:56 +01:00
|
|
|
483D 47000000 ; cmp_rax, %0x47
|
|
|
|
0F8C 9E000000 ; jl %ascii_high
|
2022-08-09 20:04:52 +01:00
|
|
|
# deal with all ascii less than a
|
2022-09-01 01:00:56 +01:00
|
|
|
483D 61000000 ; cmp_rax, %0x61
|
|
|
|
0F8C 97000000 ; jl %ascii_other
|
2022-08-09 20:04:52 +01:00
|
|
|
# deal with a-f
|
2022-09-01 01:00:56 +01:00
|
|
|
483D 67000000 ; cmp_rax, %0x67
|
|
|
|
0F8C 81000000 ; jl %ascii_low
|
2022-08-09 20:04:52 +01:00
|
|
|
# The rest that remains needs to be ignored
|
2022-09-01 01:00:56 +01:00
|
|
|
E9 86000000 ; jmp %ascii_other
|
2022-08-09 20:04:52 +01:00
|
|
|
|
2022-09-01 01:00:56 +01:00
|
|
|
# :Second_pass [_start+0x210]
|
|
|
|
E8 D0000000 ; call %Read_byte
|
2022-08-09 20:04:52 +01:00
|
|
|
|
|
|
|
# Deal with EOF
|
2022-09-01 01:00:56 +01:00
|
|
|
483D FCFFFFFF ; cmp_rax, %-4
|
|
|
|
0F84 65000000 ; je %Second_pass_done
|
2022-08-09 20:04:52 +01:00
|
|
|
|
|
|
|
# Simply drop the label
|
2022-09-01 01:00:56 +01:00
|
|
|
483D 3A000000 ; cmp_rax, %0x3a
|
|
|
|
0F85 0A000000 ; jne %Second_pass_0
|
2022-08-09 20:04:52 +01:00
|
|
|
|
2022-09-01 01:00:56 +01:00
|
|
|
E8 B3000000 ; call %Read_byte
|
|
|
|
E9 D9FFFFFF ; jmp %Second_pass
|
2022-08-09 20:04:52 +01:00
|
|
|
|
2022-09-01 01:00:56 +01:00
|
|
|
# :Second_pass_0 [_start+0x237]
|
2022-08-09 20:04:52 +01:00
|
|
|
# Deal with % pointer
|
2022-09-01 01:00:56 +01:00
|
|
|
483D 25000000 ; cmp_rax, %0x25
|
|
|
|
0F85 0A000000 ; jne %Second_pass_1
|
2022-08-09 20:04:52 +01:00
|
|
|
|
2022-09-01 01:00:56 +01:00
|
|
|
E8 F3000000 ; call %StorePointer
|
|
|
|
E9 C3FFFFFF ; jmp %Second_pass
|
2022-08-09 20:04:52 +01:00
|
|
|
|
2022-09-01 01:00:56 +01:00
|
|
|
# :Second_pass_1 [_start+0x24D]
|
2022-08-09 20:04:52 +01:00
|
|
|
# Deal with everything else
|
2022-09-01 01:00:56 +01:00
|
|
|
E8 4DFFFFFF ; call %hex # Process our char
|
2022-08-09 20:04:52 +01:00
|
|
|
|
|
|
|
# Deal with EOF
|
2022-09-01 01:00:56 +01:00
|
|
|
483D FCFFFFFF ; cmp_rax, %-4
|
|
|
|
0F84 28000000 ; je %Second_pass_done
|
2022-08-09 20:04:52 +01:00
|
|
|
|
|
|
|
# deal with -1 values
|
2022-09-01 01:00:56 +01:00
|
|
|
483D 00000000 ; cmp_rax, %0
|
|
|
|
0F8C A6FFFFFF ; jl %Second_pass
|
2022-08-09 20:04:52 +01:00
|
|
|
|
|
|
|
# deal with toggle
|
2022-09-01 01:00:56 +01:00
|
|
|
4981FF 00000000 ; cmp_r15, %0
|
|
|
|
0F84 4C000000 ; je %print
|
2022-08-09 20:04:52 +01:00
|
|
|
|
|
|
|
# process first byte of pair
|
2022-09-01 01:00:56 +01:00
|
|
|
4989C6 ; mov_r14,rax
|
|
|
|
49C7C7 00000000 ; mov_r15, %0
|
2022-08-09 20:04:52 +01:00
|
|
|
E9 8AFFFFFF ; JMP32 %Second_pass
|
|
|
|
|
2022-09-01 01:00:56 +01:00
|
|
|
# :Second_pass_done [_start+0x286]
|
|
|
|
# :EOF
|
|
|
|
C3 ; ret
|
|
|
|
# :ascii_num [_start+0x287]
|
|
|
|
4883E8 30 ; sub_rax, !0x30
|
|
|
|
C3 ; ret
|
|
|
|
# :ascii_low [_start+0x28C]
|
|
|
|
4883E8 57 ; sub_rax, !0x57
|
|
|
|
C3 ; ret
|
|
|
|
# :ascii_high [_start+0x291]
|
|
|
|
4883E8 37 ; sub_rax, !0x37
|
|
|
|
C3 ; ret
|
|
|
|
# :ascii_other [_start+0x296]
|
|
|
|
48C7C0 FFFFFFFF ; mov_rax, %-1
|
|
|
|
C3 ; ret
|
|
|
|
# :ascii_comment [_start+0x29E]
|
|
|
|
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+0x2BB]
|
|
|
|
48C7C0 FFFFFFFF ; mov_rax, %-1
|
|
|
|
C3 ; ret
|
2022-08-09 20:04:52 +01:00
|
|
|
|
|
|
|
# process second byte of pair
|
2022-09-01 01:00:56 +01:00
|
|
|
# :print [_start+0x2C3]
|
2022-08-09 20:04:52 +01:00
|
|
|
# update the sum and store in output
|
2022-09-01 01:00:56 +01:00
|
|
|
49C1E6 04 ; shl_r14, !4
|
|
|
|
4C01F0 ; add_rax,r14
|
2022-08-09 20:04:52 +01:00
|
|
|
|
|
|
|
# flip the toggle
|
2022-09-01 01:00:56 +01:00
|
|
|
49F7D7 ; not_r15 # R15 = -1
|
|
|
|
|
|
|
|
48C7C2 01000000 ; mov_rdx, %1 # set the size of chars we want
|
|
|
|
E8 31000000 ; call %print_chars
|
|
|
|
|
|
|
|
4981C5 01000000 ; add_r13, %1 # Increment IP
|
|
|
|
E9 2BFFFFFF ; jmp %Second_pass
|
|
|
|
|
|
|
|
|
|
|
|
# :Read_byte [_start+0x2E5]
|
|
|
|
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
|
2022-08-09 20:04:52 +01:00
|
|
|
|
|
|
|
# If the file ended (0 bytes read) return EOF
|
2022-09-01 01:00:56 +01:00
|
|
|
85F6 ; test_esi,esi # if size = 0
|
|
|
|
75 07 ; jne8 !Read_byte_1
|
|
|
|
48C7C0 FCFFFFFF ; mov_rax, %-4 # Put EOF in rax
|
2022-08-09 20:04:52 +01:00
|
|
|
|
2022-09-01 01:00:56 +01:00
|
|
|
# :Read_byte_1 [_start+0x309]
|
|
|
|
C3 ; ret # return
|
2022-08-09 20:04:52 +01:00
|
|
|
|
|
|
|
# Writes bytes stored in rax
|
2022-09-01 01:00:56 +01:00
|
|
|
# :print_chars [_start+0x30A]
|
|
|
|
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 [_start+0x320]
|
|
|
|
E8 C0FFFFFF ; 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+0x332]
|
|
|
|
E8 E9FFFFFF ; call %Get_table_target
|
|
|
|
4C8928 ; mov_[rax],r13 # Write out pointer to table
|
|
|
|
C3 ; ret
|
|
|
|
|
|
|
|
# :StorePointer [_start+0x33B]
|
|
|
|
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 B1FFFFFF ; call %print_chars
|
|
|
|
C3 ; ret
|
|
|
|
|
|
|
|
# :Done [_start+0x35A]
|
|
|
|
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
|
2022-08-09 20:04:52 +01:00
|
|
|
|
2022-09-01 01:00:56 +01:00
|
|
|
# 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 3C000000 ; 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 11000000 ; 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
|
2022-08-09 20:04:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Protocol GUIDs
|
2022-09-01 01:00:56 +01:00
|
|
|
# :LOADED_IMAGE_PROTOCOL [_start+0x3AF]
|
2022-08-09 20:04:52 +01:00
|
|
|
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
|
|
|
|
|
2022-09-01 01:00:56 +01:00
|
|
|
# :SIMPLE_FS_PROTOCOL [_start+0x3BF]
|
2022-08-09 20:04:52 +01:00
|
|
|
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
|
|
|
|
|
2022-09-01 01:00:56 +01:00
|
|
|
# :PE32_end [_start+0x3CF]
|