2022-08-14 19:49:58 +01:00
|
|
|
# SPDX-FileCopyrightText: 2022 Andrius Štikonas <andrius@stikonas.eu>
|
|
|
|
# SPDX-FileCopyrightText: 2019 Jeremiah Orians
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2022-08-22 01:30:22 +01:00
|
|
|
DEFINE add_rbx,[rdi+BYTE] 48035F
|
|
|
|
DEFINE add_rbx, 4883C3
|
|
|
|
DEFINE add_rsp, 4883C4
|
|
|
|
DEFINE call E8
|
|
|
|
DEFINE call_[rcx+BYTE] FF51
|
|
|
|
DEFINE call_[r14+BYTE] 41FF56
|
2022-08-23 00:39:36 +01:00
|
|
|
DEFINE call_[r14+DWORD] 41FF96
|
2022-08-22 01:30:22 +01:00
|
|
|
DEFINE cmp_al, 3C
|
|
|
|
DEFINE cmp_rax, 483D
|
|
|
|
DEFINE cmp_rbx,rdx 4839D3
|
|
|
|
DEFINE cmp_r8, 4983F8
|
|
|
|
DEFINE je 74
|
|
|
|
DEFINE jmp EB
|
|
|
|
DEFINE jne 75
|
2022-08-23 00:39:36 +01:00
|
|
|
DEFINE lea_rdx,[rip+DWORD] 488D15
|
2022-08-22 01:30:22 +01:00
|
|
|
DEFINE mov_rdx, 48C7C2
|
|
|
|
DEFINE mov_rbx,rax 4889C3
|
2022-08-23 00:39:36 +01:00
|
|
|
DEFINE mov_rcx,rax 4889C1
|
2022-08-22 01:30:22 +01:00
|
|
|
DEFINE mov_rcx,rbx 4889D9
|
2022-08-23 00:39:36 +01:00
|
|
|
DEFINE mov_rcx,r12 4C89E1
|
2022-08-22 01:30:22 +01:00
|
|
|
DEFINE mov_rcx,r13 4C89E9
|
|
|
|
DEFINE mov_rcx,r15 4C89F9
|
2022-08-27 23:52:38 +01:00
|
|
|
DEFINE mov_rdi,rax 4889C7
|
2022-08-22 01:30:22 +01:00
|
|
|
DEFINE mov_rdx,rax 4889C2
|
|
|
|
DEFINE mov_rdx,rbx 4889DA
|
|
|
|
DEFINE mov_rdx,rsp 4889E2
|
2022-08-24 00:17:52 +01:00
|
|
|
DEFINE mov_rbp,rsp 4889E5
|
|
|
|
DEFINE mov_rsp,rbp 4889EC
|
2022-08-22 01:30:22 +01:00
|
|
|
DEFINE mov_r8,rbx 4989D8
|
|
|
|
DEFINE mov_r8,rsp 4989E0
|
2022-08-23 00:39:36 +01:00
|
|
|
DEFINE mov_r8,r12 4D89E0
|
|
|
|
DEFINE mov_r9,r12 4D89E1
|
|
|
|
DEFINE mov_r12,rcx 4989CC
|
2022-08-22 01:30:22 +01:00
|
|
|
DEFINE mov_r13,rsi 4989F5
|
|
|
|
DEFINE mov_al,[rbx] 8A03
|
|
|
|
DEFINE mov_rax,[rsp+BYTE] 488B4424
|
|
|
|
DEFINE mov_rbx,[rdi+BYTE] 488B5F
|
|
|
|
DEFINE mov_rcx,[rdi+BYTE] 488B4F
|
|
|
|
DEFINE mov_rcx,[rsp+BYTE] 488B4C24
|
|
|
|
DEFINE mov_r14,[rdx+BYTE] 4C8B72
|
2022-08-23 00:39:36 +01:00
|
|
|
DEFINE mov_rcx,[rip+DWORD] 488B0D
|
|
|
|
DEFINE mov_[rip+DWORD],rax 488905
|
|
|
|
DEFINE mov_[rip+DWORD],rcx 48890D
|
2022-08-22 01:30:22 +01:00
|
|
|
DEFINE mov_[rbx], C603
|
|
|
|
DEFINE pop_rax 58
|
|
|
|
DEFINE pop_rcx 59
|
|
|
|
DEFINE pop_rsi 5E
|
|
|
|
DEFINE pop_r8 4158
|
|
|
|
DEFINE pop_r9 4159
|
|
|
|
DEFINE pop_r15 415F
|
|
|
|
DEFINE push 6A
|
|
|
|
DEFINE push_rax 50
|
|
|
|
DEFINE push_rbx 53
|
|
|
|
DEFINE push_rdx 52
|
|
|
|
DEFINE ret C3
|
|
|
|
DEFINE ror_r9 49D1C9
|
|
|
|
DEFINE sub_rbx, 4883EB
|
|
|
|
DEFINE sub_rsp, 4883EC
|
|
|
|
DEFINE xor_eax,eax 31C0
|
2022-08-23 00:39:36 +01:00
|
|
|
DEFINE test_eax,eax 85C0
|
2022-08-24 00:17:52 +01:00
|
|
|
DEFINE test_r8,r8 4D85C0
|
2022-08-23 00:39:36 +01:00
|
|
|
DEFINE xor_r9,r9 4D31C9
|
2022-08-14 19:49:58 +01:00
|
|
|
|
|
|
|
# efi_main(void *image_handle, struct efi_system_table *system)
|
|
|
|
:_start
|
2022-08-27 23:52:38 +01:00
|
|
|
mov_rbp,rsp # save stack pointer
|
|
|
|
mov_r12,rcx # save image_handle
|
|
|
|
mov_r14,[rdx+BYTE] !96 # save system->boot
|
2022-08-14 19:49:58 +01:00
|
|
|
|
|
|
|
# Open Loaded Image protocol
|
2022-08-27 23:52:38 +01:00
|
|
|
mov_r9,r12 # arg4 = image_handle
|
|
|
|
lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL
|
|
|
|
mov_rcx,r12 # arg1 = image_handle
|
|
|
|
call %open_protocol # open protocol
|
|
|
|
mov_rdi,rax # save image
|
2022-08-14 19:49:58 +01:00
|
|
|
|
|
|
|
# Get root file system
|
2022-08-27 23:52:38 +01:00
|
|
|
mov_r9,r12 # arg4 = image_handle
|
|
|
|
lea_rdx,[rip+DWORD] %SIMPLE_FS_PROTOCOL # guid = &SIMPLE_FS_PROTOCOL
|
|
|
|
mov_rcx,[rdi+BYTE] !24 # arg1 = root_device = image->device
|
|
|
|
mov_[rip+DWORD],rcx %root_device # save root_device
|
|
|
|
call %open_protocol # open protocol
|
|
|
|
mov_rcx,rax # get rootfs
|
2022-08-14 19:49:58 +01:00
|
|
|
|
|
|
|
# Get root directory
|
2022-08-27 23:52:38 +01:00
|
|
|
lea_rdx,[rip+DWORD] %rootdir # arg2 = &rootdir
|
|
|
|
push_rax # allocate shadow stack space for UEFI function
|
|
|
|
push_rax # allocate shadow stack space for UEFI function
|
|
|
|
call_[rcx+BYTE] !8 # rootfs->open_volume(rootfs, &rootdir)
|
|
|
|
pop_rax # deallocate stack
|
|
|
|
pop_rax # deallocate stack
|
2022-08-14 19:49:58 +01:00
|
|
|
|
|
|
|
# Push command line arguments onto stack
|
2022-08-22 01:30:22 +01:00
|
|
|
mov_rbx,[rdi+BYTE] !56 # options = image->load_options
|
|
|
|
mov_rdx,rbx # save beginning of load_options
|
|
|
|
add_rbx,[rdi+BYTE] !48 # go to the end of load_options
|
|
|
|
push !0 # Save end of arguments (NULL) onto stack
|
2022-08-14 19:49:58 +01:00
|
|
|
:loop_options
|
2022-08-22 01:30:22 +01:00
|
|
|
cmp_rbx,rdx # Check if we are done
|
|
|
|
je !loop_options_done # We are done
|
|
|
|
sub_rbx, !2 # --options
|
|
|
|
mov_al,[rbx] # *options
|
|
|
|
cmp_al, !0x20 # if *options != ' '
|
|
|
|
jne !loop_options # then continue looping
|
|
|
|
mov_[rbx], !0 # zero it
|
|
|
|
add_rbx, !2 # ++options
|
|
|
|
push_rbx # push another argument onto stack
|
|
|
|
jmp !loop_options # next argument
|
2022-08-14 19:49:58 +01:00
|
|
|
:loop_options_done
|
2022-08-22 01:30:22 +01:00
|
|
|
pop_r8 # get output file
|
2022-08-24 00:17:52 +01:00
|
|
|
test_r8,r8 # if no output file
|
|
|
|
je !exit_early # then exit
|
2022-08-14 19:49:58 +01:00
|
|
|
|
|
|
|
# Open file for writing
|
2022-08-22 01:30:22 +01:00
|
|
|
push_rdx # allocate stack for fout
|
|
|
|
mov_rdx,rsp # arg2 = &fout
|
|
|
|
push !0 # arg5 = 0
|
|
|
|
push !7 # to get 0x8000000000000003 we set the rightmost 3 bits
|
|
|
|
pop_r9 # and then do right rotation by 1
|
|
|
|
ror_r9 # arg4 = EFI_FILE_MODE_CREATE| EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ
|
2022-08-23 00:39:36 +01:00
|
|
|
mov_rcx,[rip+DWORD] %rootdir # arg1 = rootdir
|
2022-08-22 01:30:22 +01:00
|
|
|
sub_rsp, !32 # allocate shadow stack space for UEFI function
|
|
|
|
call_[rcx+BYTE] !8 # rootdir->open()
|
|
|
|
add_rsp, !40 # deallocate stack
|
|
|
|
pop_r15 # get fout
|
2022-08-14 19:49:58 +01:00
|
|
|
|
2022-08-22 01:30:22 +01:00
|
|
|
mov_rdx, %0x100000 # Allocate 1MB
|
|
|
|
call %allocate_pool # Get memory pool
|
|
|
|
mov_rbx,rax # Save it
|
2022-08-14 19:49:58 +01:00
|
|
|
|
|
|
|
:core
|
2022-08-22 01:30:22 +01:00
|
|
|
pop_r8 # Get the actual input name
|
|
|
|
cmp_r8, !0 # Check for null string
|
|
|
|
je !done # We are done if null string
|
2022-08-14 19:49:58 +01:00
|
|
|
|
2022-08-22 01:30:22 +01:00
|
|
|
call %open_file # Open file as read only
|
|
|
|
test_eax,eax # check if successfully opened
|
|
|
|
jne !core # Else go to another file
|
|
|
|
mov_r13,rsi # Protect fin
|
2022-08-14 19:49:58 +01:00
|
|
|
|
|
|
|
:keep
|
2022-08-22 01:30:22 +01:00
|
|
|
mov_rdx, %0x100000 # set the size of chars we want
|
|
|
|
call %read # read them
|
|
|
|
push_rax # Protect the number of bytes read
|
2022-08-14 19:49:58 +01:00
|
|
|
|
2022-08-22 01:30:22 +01:00
|
|
|
mov_rdx,rax # number of bytes to write
|
|
|
|
call %write # write them
|
2022-08-14 19:49:58 +01:00
|
|
|
|
2022-08-22 01:30:22 +01:00
|
|
|
pop_rax # Get bytes read
|
|
|
|
cmp_rax, %0x100000 # Check if buffer was fully used
|
|
|
|
je !keep # Keep looping if it was full
|
2022-08-14 19:49:58 +01:00
|
|
|
|
2022-08-22 01:30:22 +01:00
|
|
|
mov_rcx,r13 # fin
|
|
|
|
call %close_file # close file
|
|
|
|
jmp !core # Move to next file
|
2022-08-14 19:49:58 +01:00
|
|
|
|
|
|
|
:done
|
2022-08-22 01:30:22 +01:00
|
|
|
mov_rcx,r15 # Get output file
|
|
|
|
call %close_file # close it
|
2022-08-14 19:49:58 +01:00
|
|
|
|
2022-08-22 01:30:22 +01:00
|
|
|
mov_rcx,rbx # Get buffer
|
|
|
|
call %free_pool # release it
|
2022-08-24 00:17:52 +01:00
|
|
|
|
|
|
|
:exit_early
|
|
|
|
mov_rcx,[rip+DWORD] %rootdir # Get rootdir
|
|
|
|
call %close_file # close it
|
2022-08-14 19:49:58 +01:00
|
|
|
|
2022-08-23 00:39:36 +01:00
|
|
|
mov_r8,r12 # arg3 = image_handle
|
|
|
|
lea_rdx,[rip+DWORD] %SIMPLE_FS_PROTOCOL # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID
|
|
|
|
mov_rcx,[rip+DWORD] %root_device # arg1 = root_device
|
|
|
|
call %close_protocol # close protocol
|
|
|
|
|
|
|
|
mov_r8,r12 # arg3 = image_handle
|
|
|
|
lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # EFI_LOADED_IMAGE_PROTOCOL_GUID
|
|
|
|
mov_rcx,r12 # arg1 = image_handle
|
|
|
|
call %close_protocol # close protocol
|
|
|
|
|
2022-08-24 00:17:52 +01:00
|
|
|
mov_rsp,rbp # restore stack pointer
|
2022-08-22 01:30:22 +01:00
|
|
|
ret # return to UEFI
|
2022-08-14 19:49:58 +01:00
|
|
|
|
|
|
|
# r8: input file name
|
|
|
|
# returns input file handle in rsi, status in rax
|
|
|
|
:open_file
|
|
|
|
# Open file for reading
|
2022-08-22 01:30:22 +01:00
|
|
|
push_rdx # allocate stack for fin
|
|
|
|
mov_rdx,rsp # arg2 = &fin
|
|
|
|
push !1 # arg5 = EFI_FILE_READ_ONLY
|
|
|
|
push !1 # prepare to set arg4 to EFI_FILE_MODE_READ
|
|
|
|
pop_r9 # arg4 = EFI_FILE_MODE_READ
|
2022-08-23 00:39:36 +01:00
|
|
|
mov_rcx,[rip+DWORD] %rootdir # arg1 = rootdir
|
2022-08-22 01:30:22 +01:00
|
|
|
sub_rsp, !32 # allocate shadow stack space for UEFI function
|
|
|
|
call_[rcx+BYTE] !8 # rootdir->open()
|
|
|
|
add_rsp, !40 # deallocate stack
|
|
|
|
pop_rsi # get fin
|
|
|
|
ret
|
2022-08-14 19:49:58 +01:00
|
|
|
|
|
|
|
# rcx: file handle
|
|
|
|
:close_file
|
2022-08-22 01:30:22 +01:00
|
|
|
push_rax # allocate shadow stack space for UEFI function
|
|
|
|
call_[rcx+BYTE] !16 # file_handle->close(file_handle)
|
|
|
|
pop_rax # deallocate stack
|
|
|
|
ret
|
2022-08-14 19:49:58 +01:00
|
|
|
|
2022-08-23 00:39:36 +01:00
|
|
|
# rcx: handle
|
|
|
|
# rdx: &guid
|
|
|
|
# r9: agent_handle
|
|
|
|
# returns interface
|
|
|
|
:open_protocol
|
|
|
|
push_rax # allocate stack for interface
|
|
|
|
mov_r8,rsp # arg3 = &interface
|
|
|
|
push !1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
|
|
|
|
push !0 # arg5 = NULL
|
|
|
|
sub_rsp, !32 # allocate shadow stack space for UEFI function
|
|
|
|
call_[r14+DWORD] %280 # system->boot->open_protocol(handle, &guid, &interface, agent_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
|
|
|
|
add_rsp, !48 # deallocate stack
|
|
|
|
pop_rax # get image
|
|
|
|
ret
|
|
|
|
|
|
|
|
# rcx: handle
|
|
|
|
# rdx: &guid
|
|
|
|
# r8: agent_handle
|
|
|
|
:close_protocol
|
|
|
|
xor_r9,r9 # arg4 = NULL
|
|
|
|
sub_rsp, !32 # allocate shadow stack space for UEFI function
|
|
|
|
call_[r14+DWORD] %288 # system->boot->close_protocol(handle, &guid, agent_handle, 0)
|
|
|
|
add_rsp, !32 # deallocate stack
|
|
|
|
ret
|
|
|
|
|
2022-08-14 19:49:58 +01:00
|
|
|
# rdx: number of bytes to allocate
|
|
|
|
# r14: system->boot
|
|
|
|
# returns pointer in rax
|
|
|
|
:allocate_pool
|
2022-08-22 01:30:22 +01:00
|
|
|
push_rdx # allocate stack for pool pointer
|
|
|
|
mov_r8,rsp # arg3 = &pool
|
|
|
|
push !2
|
|
|
|
pop_rcx # arg1 = EFI_LOADER_DATA
|
|
|
|
sub_rsp, !24 # allocate shadow stack space for UEFI
|
|
|
|
call_[r14+BYTE] !64 # system->boot->allocate_pool(EFI_LOADER_DATA, 2048, &pool)
|
|
|
|
add_rsp, !24 # deallocate stack
|
|
|
|
pop_rax # get pool
|
|
|
|
ret
|
2022-08-14 19:49:58 +01:00
|
|
|
|
|
|
|
# rcx: memory pool
|
|
|
|
# r14: system->boot
|
|
|
|
:free_pool
|
2022-08-22 01:30:22 +01:00
|
|
|
push_rax # allocate shadow stack space for UEFI function
|
|
|
|
call_[r14+BYTE] !72 # system->boot->free_pool(pool)
|
|
|
|
pop_rax # deallocate stack
|
|
|
|
ret
|
2022-08-14 19:49:58 +01:00
|
|
|
|
|
|
|
# rdx: number of bytes to read
|
|
|
|
# rbx: buffer
|
|
|
|
# r13: input file handle
|
|
|
|
# returns number of bytes read in rax
|
|
|
|
:read
|
2022-08-22 01:30:22 +01:00
|
|
|
mov_rcx,r13 # arg1 = fin
|
|
|
|
push_rdx # set size
|
|
|
|
mov_rdx,rsp # arg2 = &size
|
|
|
|
mov_r8,rbx # arg3 = buffer
|
|
|
|
sub_rsp, !24 # allocate shadow stack space for UEFI
|
|
|
|
call_[rcx+BYTE] !32 # fin->read()
|
|
|
|
add_rsp, !24 # deallocate stack
|
|
|
|
pop_rax # save size to rax
|
|
|
|
ret
|
2022-08-14 19:49:58 +01:00
|
|
|
|
|
|
|
# rdx: number of bytes to write
|
|
|
|
# rbx: buffer
|
|
|
|
# r14: output file handle
|
|
|
|
# returns number of bytes written in rax
|
|
|
|
:write
|
2022-08-22 01:30:22 +01:00
|
|
|
mov_rcx,r15 # arg1 = fout
|
|
|
|
push_rdx # set size
|
|
|
|
mov_rdx,rsp # arg2 = &size
|
|
|
|
mov_r8,rbx # arg3 = buffer
|
|
|
|
sub_rsp, !24 # allocate shadow stack space for UEFI
|
|
|
|
call_[rcx+BYTE] !40 # fin->write()
|
|
|
|
add_rsp, !24 # deallocate stack
|
|
|
|
pop_rax # save size to rax
|
|
|
|
ret
|
2022-08-14 19:49:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Protocol GUIDs
|
|
|
|
:LOADED_IMAGE_PROTOCOL
|
|
|
|
%0x5b1b31a1
|
|
|
|
@0x9562
|
|
|
|
@0x11d2
|
|
|
|
!0x8e !0x3f !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b
|
|
|
|
|
|
|
|
:SIMPLE_FS_PROTOCOL
|
2022-10-23 22:59:37 +01:00
|
|
|
%0x964e5b22
|
2022-08-14 19:49:58 +01:00
|
|
|
@0x6459
|
|
|
|
@0x11d2
|
|
|
|
!0x8e !0x39 !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b
|
|
|
|
|
|
|
|
:rootdir
|
|
|
|
%0 %0
|
|
|
|
|
2022-08-23 00:39:36 +01:00
|
|
|
:root_device
|
|
|
|
%0 %0
|
|
|
|
|
2022-10-11 00:55:46 +01:00
|
|
|
:ELF_end
|