2022-07-10 16:05:05 +01:00
|
|
|
# SPDX-FileCopyrightText: 2022 Andrius Štikonas <andrius@stikonas.eu>
|
2022-07-15 22:37:45 +01:00
|
|
|
# SPDX-FileCopyrightText: 2017 Jeremiah Orians <jeremiah@pdp10.guru>
|
2022-07-10 16:05:05 +01:00
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2022-07-15 23:09:35 +01:00
|
|
|
# Usage: hex0 file.hex0 file
|
2022-07-15 22:37:45 +01:00
|
|
|
# Does not validate the arguments or check for success
|
2022-07-13 22:47:20 +01:00
|
|
|
|
2022-07-11 01:22:34 +01:00
|
|
|
# Calling convention:
|
2022-07-13 22:47:20 +01:00
|
|
|
# First four arguments are passed via registers rcx, rdx, r8, r9 (if they fit in 64-bits)
|
|
|
|
# but we need to leave stack space
|
2022-07-20 22:48:41 +01:00
|
|
|
# rax, rcx, rdx, r8, r9, r10 and r11 are volatile and can be changed by called function
|
2022-07-11 01:22:34 +01:00
|
|
|
|
2022-07-16 20:40:06 +01:00
|
|
|
# Registers:
|
|
|
|
# r12 in/fin: input file name, later reused for handle
|
|
|
|
# r13 out/fout: output file name, later reused for handle
|
2022-07-20 22:48:41 +01:00
|
|
|
# r14 system->boot->open_protocol from UEFI, later reused for rootdir
|
|
|
|
# r15 image_handle from UEFI
|
|
|
|
|
|
|
|
# hex0 algorithm mostly follows stage0-posix version but uses rdi instead of rbp
|
2022-07-16 20:40:06 +01:00
|
|
|
|
2022-07-10 22:08:36 +01:00
|
|
|
.global _start
|
|
|
|
.text
|
|
|
|
|
2022-07-23 15:15:40 +01:00
|
|
|
# efi_main(void *image_handle, struct efi_system_table *system)
|
2022-07-10 22:08:36 +01:00
|
|
|
_start:
|
2022-07-17 11:47:33 +01:00
|
|
|
mov rbp, rsp # save stack pointer
|
|
|
|
mov r15, rcx # save image_handle
|
2022-07-17 12:30:29 +01:00
|
|
|
mov r14, [rdx+96] # system->boot
|
|
|
|
mov r14, [r14+280] # system->boot->open_protocol
|
2022-07-11 01:22:34 +01:00
|
|
|
|
|
|
|
# Open Loaded Image protocol
|
2022-07-17 11:47:33 +01:00
|
|
|
push rax # allocate stack for image
|
|
|
|
mov r8, rsp # arg3 = &image
|
2022-07-24 10:11:45 +01:00
|
|
|
mov rdx, [rip+LOADED_IMAGE_PROTOCOL+8] # EFI_LOADED_IMAGE_PROTOCOL_GUID (last 64 bits)
|
2022-07-17 11:47:33 +01:00
|
|
|
push rdx # push last 64 bits onto stack
|
2022-07-24 10:11:45 +01:00
|
|
|
mov rdx, [rip+LOADED_IMAGE_PROTOCOL] # EFI_LOADED_IMAGE_PROTOCOL_GUID (first 64 bits)
|
2022-07-17 11:47:33 +01:00
|
|
|
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
|
2022-07-11 01:22:34 +01:00
|
|
|
mov r9, rcx # arg4 = image_handle
|
2022-07-17 11:47:33 +01:00
|
|
|
# arg1 = ImageHandle (already set)
|
2022-07-23 20:14:45 +01:00
|
|
|
sub rsp, 32 # allocate shadow stack space for UEFI function
|
2022-07-23 15:15:40 +01:00
|
|
|
call r14 # system->boot->open_protocol(image_handle, &guid, &image, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
|
2022-07-17 11:47:33 +01:00
|
|
|
mov rax, [rsp+64] # get image
|
2022-07-30 10:08:23 +01:00
|
|
|
mov rcx, rax # save image
|
2022-07-10 22:08:36 +01:00
|
|
|
|
2022-07-11 01:22:34 +01:00
|
|
|
# Command line args
|
2022-07-23 15:15:40 +01:00
|
|
|
mov rbx, [rax+56] # options = image->load_options
|
2022-07-13 22:47:20 +01:00
|
|
|
|
|
|
|
loop_options1: # Skip application name
|
2022-07-23 15:15:40 +01:00
|
|
|
add rbx, 2 # ++options
|
|
|
|
mov al, [rbx] # *options
|
|
|
|
cmp al, 0x20 # if *options != ' '
|
2022-07-13 22:47:20 +01:00
|
|
|
jne loop_options1 # then jump
|
|
|
|
|
2022-07-23 15:15:40 +01:00
|
|
|
add rbx, 2 # ++options
|
2022-07-23 18:40:41 +01:00
|
|
|
mov r12, rbx # save input file
|
2022-07-13 22:47:20 +01:00
|
|
|
|
|
|
|
loop_options2: # Skip argv[1]
|
2022-07-23 15:15:40 +01:00
|
|
|
add rbx, 2 # ++options
|
|
|
|
mov al, [rbx] # *options
|
|
|
|
cmp al, 0x20 # if *options != ' '
|
2022-07-13 22:47:20 +01:00
|
|
|
jne loop_options2 # then jump
|
|
|
|
|
2022-07-23 15:15:40 +01:00
|
|
|
mov byte ptr [rbx], 0 # *options = 0;
|
|
|
|
add rbx, 2 # ++options
|
2022-07-23 18:40:41 +01:00
|
|
|
mov r13, rbx # save output file
|
2022-07-13 22:47:20 +01:00
|
|
|
|
2022-07-30 10:08:23 +01:00
|
|
|
# Get root file system
|
2022-07-17 11:47:33 +01:00
|
|
|
push rax # allocate stack for rootfs
|
|
|
|
mov r8, rsp # arg3 = &rootfs
|
2022-07-24 10:11:45 +01:00
|
|
|
mov rdx, [rip+SIMPLE_FS_PROTOCOL+8] # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID (last 64 bits)
|
2022-07-17 11:47:33 +01:00
|
|
|
push rdx # push last 64 bits onto stack
|
2022-07-24 10:11:45 +01:00
|
|
|
mov rdx, [rip+SIMPLE_FS_PROTOCOL] # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID (first 64 bits)
|
2022-07-17 11:47:33 +01:00
|
|
|
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
|
2022-07-16 20:40:06 +01:00
|
|
|
mov r9, r15 # arg4 = image_handle
|
2022-07-17 11:47:33 +01:00
|
|
|
mov rcx, [rcx+24] # arg1 = root_device = image->device
|
2022-07-23 20:14:45 +01:00
|
|
|
sub rsp, 32 # allocate shadow stack space for UEFI function
|
2022-07-17 12:30:29 +01:00
|
|
|
call r14 # system->boot->open_protocol(root_device, &guid, &rootfs, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
|
2022-07-14 22:55:42 +01:00
|
|
|
mov rcx, [rsp+64] # get rootfs
|
|
|
|
|
2022-07-23 18:40:41 +01:00
|
|
|
# Get root directory
|
2022-07-17 11:47:33 +01:00
|
|
|
push rdx # allocate stack for rootdir
|
|
|
|
mov rdx, rsp # arg2 = &rootdir
|
2022-07-17 12:49:03 +01:00
|
|
|
push rax # allocate shadow stack space for UEFI function
|
|
|
|
push rax # allocate shadow stack space for UEFI function
|
2022-07-14 22:55:42 +01:00
|
|
|
call [rcx+8] # rootfs->open_volume(rootfs, &rootdir)
|
2022-07-17 13:23:18 +01:00
|
|
|
pop rax # deallocate stack
|
|
|
|
pop rax # deallocate stack
|
|
|
|
pop r14 # save &rootdir
|
2022-07-14 22:55:42 +01:00
|
|
|
|
|
|
|
# Open file for writing
|
2022-07-17 11:47:33 +01:00
|
|
|
push rdx # allocate stack for fout
|
|
|
|
mov rdx, rsp # arg2 = &fout
|
|
|
|
push 0 # arg5 = 0
|
2022-07-17 13:12:51 +01:00
|
|
|
push 7 # to get 0x8000000000000003 we set the rightmost 3 bits
|
|
|
|
pop r9 # and then do right rotation by 1
|
2022-07-23 18:40:41 +01:00
|
|
|
ror r9 # arg4 = EFI_FILE_MODE_CREATE| EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ
|
2022-07-17 11:47:33 +01:00
|
|
|
mov r8, r13 # arg3 = out
|
2022-07-17 12:30:29 +01:00
|
|
|
mov rcx, r14 # arg1 = rootdir
|
2022-07-23 20:14:45 +01:00
|
|
|
sub rsp, 32 # allocate shadow stack space for UEFI function
|
2022-07-14 22:55:42 +01:00
|
|
|
call [rcx+8] # rootdir->open()
|
2022-07-16 20:40:06 +01:00
|
|
|
mov r13, [rsp+40] # get fout
|
2022-07-14 22:55:42 +01:00
|
|
|
|
2022-07-15 00:43:57 +01:00
|
|
|
# Open file for reading
|
2022-07-17 11:47:33 +01:00
|
|
|
push rdx # allocate stack for fin
|
|
|
|
mov rdx, rsp # arg2 = &fin
|
|
|
|
push 1 # arg5 = EFI_FILE_READ_ONLY
|
2022-07-17 13:12:51 +01:00
|
|
|
push 1 # prepare to set arg4 to EFI_FILE_MODE_READ
|
|
|
|
pop r9 # arg4 = EFI_FILE_MODE_READ
|
2022-07-17 11:47:33 +01:00
|
|
|
mov r8, r12 # arg3 = in
|
2022-07-17 12:30:29 +01:00
|
|
|
mov rcx, r14 # arg1 = rootdir
|
2022-07-23 20:14:45 +01:00
|
|
|
sub rsp, 32 # allocate shadow stack space for UEFI function
|
2022-07-15 00:43:57 +01:00
|
|
|
call [rcx+8] # rootdir->open()
|
2022-07-16 20:40:06 +01:00
|
|
|
mov r12, [rsp+40] # get fin
|
2022-07-15 22:37:45 +01:00
|
|
|
|
|
|
|
# Our flag for byte processing
|
2022-07-16 20:09:53 +01:00
|
|
|
push -1
|
2022-07-20 22:48:41 +01:00
|
|
|
pop rbx # rbx = -1
|
2022-07-15 22:37:45 +01:00
|
|
|
|
|
|
|
# temp storage for the sum
|
2022-07-23 18:40:41 +01:00
|
|
|
xor edi, edi # rdi = 0
|
2022-07-15 22:37:45 +01:00
|
|
|
|
|
|
|
loop:
|
|
|
|
# Read a byte
|
|
|
|
call read_byte
|
|
|
|
|
|
|
|
# process byte
|
|
|
|
call hex
|
|
|
|
|
|
|
|
# Deal with -1 values
|
2022-07-20 22:48:41 +01:00
|
|
|
test eax, eax
|
2022-07-15 22:37:45 +01:00
|
|
|
jl loop
|
|
|
|
|
|
|
|
# deal with toggle
|
2022-07-20 22:48:41 +01:00
|
|
|
test ebx, ebx # jump if ebx >= 0
|
2022-07-15 22:37:45 +01:00
|
|
|
jge print
|
|
|
|
|
|
|
|
# process first byte of pair
|
2022-07-20 22:48:41 +01:00
|
|
|
mov edi, eax
|
2022-07-23 18:40:41 +01:00
|
|
|
xor ebx, ebx # rbx = 0
|
2022-07-15 22:37:45 +01:00
|
|
|
jmp loop
|
|
|
|
|
|
|
|
# process second byte of pair
|
|
|
|
print:
|
|
|
|
# update the sum and store in output
|
2022-07-20 22:48:41 +01:00
|
|
|
shl edi, 4
|
|
|
|
add eax, edi
|
2022-07-15 22:37:45 +01:00
|
|
|
|
|
|
|
# flip the toggle
|
2022-07-20 22:48:41 +01:00
|
|
|
dec ebx # rbx = -1
|
2022-07-15 22:37:45 +01:00
|
|
|
|
|
|
|
call write_byte
|
|
|
|
|
|
|
|
jmp loop
|
|
|
|
|
|
|
|
hex:
|
|
|
|
# Purge Comment Lines (#)
|
2022-07-20 22:48:41 +01:00
|
|
|
cmp al, 35
|
2022-07-15 22:37:45 +01:00
|
|
|
je purge_comment
|
|
|
|
|
|
|
|
# Purge Comment Lines (;)
|
2022-07-20 22:48:41 +01:00
|
|
|
cmp al, 59
|
2022-07-15 22:37:45 +01:00
|
|
|
je purge_comment
|
|
|
|
|
|
|
|
# deal all ascii less than 0
|
2022-07-20 22:48:41 +01:00
|
|
|
cmp al, 48
|
2022-07-15 22:37:45 +01:00
|
|
|
jl ascii_other
|
|
|
|
|
|
|
|
# deal with 0-9
|
2022-07-20 22:48:41 +01:00
|
|
|
cmp al, 58
|
2022-07-15 22:37:45 +01:00
|
|
|
jl ascii_num
|
|
|
|
|
|
|
|
# deal with all ascii less than A
|
2022-07-20 22:48:41 +01:00
|
|
|
cmp al, 65
|
2022-07-15 22:37:45 +01:00
|
|
|
jl ascii_other
|
|
|
|
|
|
|
|
# deal with A-F
|
2022-07-20 22:48:41 +01:00
|
|
|
cmp al, 71
|
2022-07-15 22:37:45 +01:00
|
|
|
jl ascii_high
|
|
|
|
|
|
|
|
# deal with all ascii less than a
|
2022-07-20 22:48:41 +01:00
|
|
|
cmp al, 97
|
2022-07-15 22:37:45 +01:00
|
|
|
jl ascii_other
|
|
|
|
|
|
|
|
# deal with a-f
|
2022-07-20 22:48:41 +01:00
|
|
|
cmp al, 103
|
2022-07-15 22:37:45 +01:00
|
|
|
jl ascii_low
|
|
|
|
|
|
|
|
# The rest that remains needs to be ignored
|
|
|
|
jmp ascii_other
|
2022-07-10 22:08:36 +01:00
|
|
|
|
2022-07-15 22:37:45 +01:00
|
|
|
purge_comment:
|
|
|
|
# Read a byte
|
|
|
|
call read_byte
|
|
|
|
|
|
|
|
# Loop if not LF (works for CR/LF and LF/CR endings too)
|
2022-07-20 22:48:41 +01:00
|
|
|
cmp al, 10
|
2022-07-15 22:37:45 +01:00
|
|
|
jne purge_comment
|
|
|
|
|
|
|
|
# Otherwise return -1
|
2022-07-20 22:48:41 +01:00
|
|
|
|
|
|
|
ascii_other:
|
2022-07-16 20:09:53 +01:00
|
|
|
push -1
|
2022-07-20 22:48:41 +01:00
|
|
|
pop rax # return = -1
|
2022-07-15 22:37:45 +01:00
|
|
|
ret
|
|
|
|
|
|
|
|
ascii_num:
|
2022-07-20 22:48:41 +01:00
|
|
|
sub al, 48
|
2022-07-15 22:37:45 +01:00
|
|
|
ret
|
|
|
|
|
|
|
|
ascii_low:
|
2022-07-20 22:48:41 +01:00
|
|
|
sub al, 32 # convert to uppercase
|
2022-07-10 22:08:36 +01:00
|
|
|
|
2022-07-15 22:37:45 +01:00
|
|
|
ascii_high:
|
2022-07-20 22:48:41 +01:00
|
|
|
sub al, 55
|
2022-07-15 22:37:45 +01:00
|
|
|
ret
|
|
|
|
|
2022-07-20 22:48:41 +01:00
|
|
|
# Writes byte stored in al
|
|
|
|
write_byte:
|
|
|
|
mov rcx, r13 # arg1 = fout
|
|
|
|
push 1 # size = 1
|
|
|
|
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
|
2022-07-15 22:37:45 +01:00
|
|
|
|
2022-07-20 22:48:41 +01:00
|
|
|
ret # return
|
2022-07-15 22:37:45 +01:00
|
|
|
|
|
|
|
read_byte:
|
2022-07-16 20:40:06 +01:00
|
|
|
mov rcx, r12 # arg1 = fin
|
2022-07-16 01:06:47 +01:00
|
|
|
push 1 # size = 1
|
|
|
|
mov rdx, rsp # arg2 = &size
|
2022-07-20 22:48:41 +01:00
|
|
|
push rsi # allocate stack
|
2022-07-16 01:06:47 +01:00
|
|
|
mov r8, rsp # arg3 = &input
|
2022-07-17 12:49:03 +01:00
|
|
|
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
|
2022-07-15 22:37:45 +01:00
|
|
|
call [rcx+32] # fin->read()
|
2022-07-20 22:48:41 +01:00
|
|
|
pop rax # deallocate stack
|
|
|
|
pop rax # deallocate stack
|
|
|
|
pop rax # deallocate stack
|
2022-07-16 01:06:47 +01:00
|
|
|
pop rax # save input to rax
|
2022-07-20 22:48:41 +01:00
|
|
|
pop rsi # save size to rsi
|
2022-07-15 22:37:45 +01:00
|
|
|
|
|
|
|
# If the file ended (0 bytes read) terminate
|
2022-07-23 18:40:41 +01:00
|
|
|
test esi, esi # if size == 0
|
2022-07-15 22:37:45 +01:00
|
|
|
je terminate # then we are done
|
|
|
|
|
|
|
|
ret # return
|
|
|
|
|
2022-07-20 22:48:41 +01:00
|
|
|
terminate:
|
|
|
|
push rbx # allocate stack
|
|
|
|
mov rcx, r12 # arg1 = fin
|
|
|
|
call [rcx+16] # fin->close()
|
2022-07-23 18:40:41 +01:00
|
|
|
mov rcx, r13 # arg1 = fout
|
2022-07-20 22:48:41 +01:00
|
|
|
call [rcx+16] # fout->close()
|
|
|
|
abort: # used for debugging only
|
|
|
|
mov rsp, rbp # restore stack
|
|
|
|
ret # return to UEFI
|
2022-07-15 22:37:45 +01:00
|
|
|
|
2022-07-10 22:08:36 +01:00
|
|
|
.data
|
|
|
|
|
2022-07-11 01:22:34 +01:00
|
|
|
# Protocol GUIDs
|
|
|
|
LOADED_IMAGE_PROTOCOL:
|
|
|
|
.long 0x5b1b31a1
|
|
|
|
.short 0x9562
|
|
|
|
.short 0x11d2
|
2022-07-14 22:55:42 +01:00
|
|
|
.byte 0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b
|
2022-07-13 22:47:20 +01:00
|
|
|
|
|
|
|
SIMPLE_FS_PROTOCOL:
|
|
|
|
.long 0x0964e5b22
|
|
|
|
.short 0x6459
|
|
|
|
.short 0x11d2
|
2022-07-14 22:55:42 +01:00
|
|
|
.byte 0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b
|