2022-08-06 21:54:11 +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-12 00:05:09 +01:00
|
|
|
DEFINE add_rax, 4883C0
|
|
|
|
DEFINE add_rsp, 4883C4
|
|
|
|
DEFINE add_rbx, 4883C3
|
|
|
|
DEFINE add_rsi, 4883C6
|
|
|
|
DEFINE add_rbx,rsi 4801F3
|
|
|
|
DEFINE add_r15,rbx 4901DF
|
|
|
|
DEFINE add_r15,rdi 4901FF
|
|
|
|
DEFINE add_rbx,[rdi+BYTE] 48035F
|
2023-12-10 01:17:39 +00:00
|
|
|
DEFINE and_rsp, 4883E4
|
2022-09-12 00:05:09 +01:00
|
|
|
DEFINE call E8
|
|
|
|
DEFINE call_[rcx+BYTE] FF51
|
|
|
|
DEFINE call_[r14+BYTE] 41FF56
|
|
|
|
DEFINE call_[r14+DWORD] 41FF96
|
|
|
|
DEFINE cmp_al, 3C
|
|
|
|
DEFINE cmp_rbx,rdx 4839D3
|
|
|
|
DEFINE inc_rax 48FFC0
|
|
|
|
DEFINE je8 74
|
|
|
|
DEFINE jmp8 EB
|
|
|
|
DEFINE jmp E9
|
|
|
|
DEFINE jne8 75
|
|
|
|
DEFINE jne 0F85
|
|
|
|
DEFINE lea_rdx,[rip+DWORD] 488D15
|
|
|
|
DEFINE lea_r8,[rip+DWORD] 4C8D05
|
|
|
|
DEFINE mov_dh, B6
|
|
|
|
DEFINE mov_rbp,rsp 4889E5
|
|
|
|
DEFINE mov_rbx,rax 4889C3
|
|
|
|
DEFINE mov_rbx,rcx 4889CB
|
|
|
|
DEFINE mov_rcx,rax 4889C1
|
|
|
|
DEFINE mov_rcx,rbx 4889D9
|
|
|
|
DEFINE mov_rcx,rdi 4889F9
|
|
|
|
DEFINE mov_rcx,r8 4C89C1
|
|
|
|
DEFINE mov_rcx,r9 4C89C9
|
|
|
|
DEFINE mov_rcx,r12 4C89E1
|
|
|
|
DEFINE mov_rcx,r15 4C89F9
|
|
|
|
DEFINE mov_rdi,rax 4889C7
|
|
|
|
DEFINE mov_rdx,rbx 4889DA
|
|
|
|
DEFINE mov_rdx,rsp 4889E2
|
|
|
|
DEFINE mov_rsp,rbp 4889EC
|
|
|
|
DEFINE mov_r8,rax 4989C0
|
|
|
|
DEFINE mov_r8,rbx 4989D8
|
|
|
|
DEFINE mov_r8,rdi 4989F8
|
|
|
|
DEFINE mov_r8,rsp 4989E0
|
|
|
|
DEFINE mov_r8,r15 4D89F8
|
|
|
|
DEFINE mov_r9,rax 4989C1
|
|
|
|
DEFINE mov_r9,rbx 4989D9
|
|
|
|
DEFINE mov_r9,rdi 4989F9
|
|
|
|
DEFINE mov_r9,r15 4D89F9
|
|
|
|
DEFINE mov_r15,rsi 4989F7
|
|
|
|
DEFINE mov_r15,rax 4989C7
|
|
|
|
DEFINE mov_[rax], C700
|
|
|
|
DEFINE mov_[rbx], C603
|
|
|
|
DEFINE mov_[r8], 49C700
|
|
|
|
DEFINE mov_[rax],BYTE C600
|
|
|
|
DEFINE mov_[rax],WORD 66C700
|
|
|
|
DEFINE mov_[rbx],WORD 66C703
|
|
|
|
DEFINE mov_[r15],WORD 6641C707
|
|
|
|
DEFINE mov_al,[rbx] 8A03
|
|
|
|
DEFINE mov_[rax],r15 4C8938
|
|
|
|
DEFINE mov_[rbx],ax 668903
|
|
|
|
DEFINE mov_rax,[rdx+BYTE] 488B42
|
|
|
|
DEFINE mov_rbx,[rdi+BYTE] 488B5F
|
|
|
|
DEFINE mov_rcx,[rcx+BYTE] 488B49
|
|
|
|
DEFINE mov_rcx,[rdi+BYTE] 488B4F
|
|
|
|
DEFINE mov_rdx,[rcx+BYTE] 488B51
|
|
|
|
DEFINE mov_r12,[rsp+BYTE] 4C8B6424
|
|
|
|
DEFINE mov_r14,[rdx+BYTE] 4C8B72
|
|
|
|
DEFINE mov_rcx,[rip+DWORD] 488B0D
|
|
|
|
DEFINE mov_rdx,[rip+DWORD] 488B15
|
|
|
|
DEFINE mov_r8,[rip+DWORD] 4C8B05
|
|
|
|
DEFINE mov_[rax+BYTE],rbx 488958
|
|
|
|
DEFINE mov_[rax+BYTE],rcx 488948
|
|
|
|
DEFINE mov_[rax+BYTE],rsi 488970
|
|
|
|
DEFINE mov_[rip+DWORD],rcx 48890D
|
|
|
|
DEFINE mov_[rip+DWORD],rax 488905
|
|
|
|
DEFINE pop_rax 58
|
2023-12-10 01:17:39 +00:00
|
|
|
DEFINE pop_rbx 5B
|
|
|
|
DEFINE pop_rbp 5D
|
2022-09-12 00:05:09 +01:00
|
|
|
DEFINE pop_rcx 59
|
|
|
|
DEFINE pop_rdi 5F
|
|
|
|
DEFINE pop_rdx 5A
|
2023-12-10 01:17:39 +00:00
|
|
|
DEFINE pop_rsi 5E
|
2022-09-12 00:05:09 +01:00
|
|
|
DEFINE pop_r8 4158
|
|
|
|
DEFINE pop_r9 4159
|
2023-12-10 01:17:39 +00:00
|
|
|
DEFINE pop_r12 415C
|
|
|
|
DEFINE pop_r13 415D
|
|
|
|
DEFINE pop_r14 415E
|
|
|
|
DEFINE pop_r15 415F
|
2022-09-12 00:05:09 +01:00
|
|
|
DEFINE push 6A
|
|
|
|
DEFINE push_rax 50
|
2023-12-10 01:17:39 +00:00
|
|
|
DEFINE push_rbp 55
|
2022-09-12 00:05:09 +01:00
|
|
|
DEFINE push_rbx 53
|
|
|
|
DEFINE push_rdi 57
|
|
|
|
DEFINE push_rdx 52
|
2023-12-10 01:17:39 +00:00
|
|
|
DEFINE push_rsi 56
|
2022-09-12 00:05:09 +01:00
|
|
|
DEFINE push_rsp 54
|
|
|
|
DEFINE push_r8 4150
|
2023-12-10 01:17:39 +00:00
|
|
|
DEFINE push_r12 4154
|
|
|
|
DEFINE push_r13 4155
|
|
|
|
DEFINE push_r14 4156
|
|
|
|
DEFINE push_r15 4157
|
2022-09-12 00:05:09 +01:00
|
|
|
DEFINE ret C3
|
|
|
|
DEFINE sub_rbx, 4883EB
|
|
|
|
DEFINE sub_rsp, 4883EC
|
|
|
|
DEFINE sub_rbx,rsi 4829F3
|
|
|
|
DEFINE sub_r15,rdi 4929FF
|
|
|
|
DEFINE test_eax,eax 85C0
|
|
|
|
DEFINE test_ecx,ecx 85C9
|
|
|
|
DEFINE test_r8,r8 4D85C0
|
|
|
|
DEFINE test_r15,r15 4D85FF
|
|
|
|
DEFINE xor_ecx,ecx 31C9
|
|
|
|
DEFINE xor_edx,edx 31D2
|
|
|
|
DEFINE xor_esi,esi 31F6
|
|
|
|
DEFINE xor_r8,r8 4D31C0
|
|
|
|
DEFINE xor_r9,r9 4D31C9
|
|
|
|
DEFINE xor_r15,r15 4D31FF
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
# efi_main(void *image_handle, struct efi_system_table *system)
|
|
|
|
:_start
|
2023-12-10 01:17:39 +00:00
|
|
|
# Save non-volatile registers
|
|
|
|
push_rbp
|
|
|
|
push_rbx
|
|
|
|
push_rdi
|
|
|
|
push_rsi
|
|
|
|
push_r12
|
|
|
|
push_r13
|
|
|
|
push_r14
|
|
|
|
push_r15
|
2023-12-17 16:38:53 +00:00
|
|
|
mov_rbp,rsp
|
2023-12-10 01:17:39 +00:00
|
|
|
|
2022-09-12 00:05:09 +01:00
|
|
|
mov_[rip+DWORD],rcx %image_handle # save image_handle
|
|
|
|
mov_rbx,rcx # save image_handle
|
|
|
|
mov_rax,[rdx+BYTE] !64 # system->out
|
|
|
|
mov_[rip+DWORD],rax %system_out # save system->out
|
|
|
|
mov_r14,[rdx+BYTE] !96 # system->boot
|
|
|
|
|
|
|
|
xor_ecx,ecx # timeout = 0
|
|
|
|
xor_edx,edx # watchdog_code = 0
|
|
|
|
xor_r8,r8 # data_size = 0
|
|
|
|
xor_r9,r9 # watchdog_data = 0
|
2023-12-17 16:38:53 +00:00
|
|
|
sub_rsp, !40 # allocate shadow stack space for UEFI function and align stack
|
2022-09-12 00:05:09 +01:00
|
|
|
call_[r14+DWORD] %240 # system->boot->set_watchdog_timer
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
# Open Loaded Image protocol
|
2022-09-12 00:05:09 +01:00
|
|
|
mov_r9,rbx # arg4 = image_handle
|
|
|
|
lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL
|
|
|
|
mov_rcx,rbx # arg1 = image_handle
|
|
|
|
call %open_protocol # open protocol
|
|
|
|
mov_rdi,rax # save image
|
|
|
|
mov_[rip+DWORD],rax %image # save image
|
|
|
|
|
2022-08-06 21:54:11 +01:00
|
|
|
# Get root file system
|
2022-09-12 00:05:09 +01:00
|
|
|
mov_r9,rbx # 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-06 21:54:11 +01:00
|
|
|
|
|
|
|
# Get root directory
|
2022-09-12 00:05:09 +01:00
|
|
|
lea_rdx,[rip+DWORD] %rootdir # arg2 = &rootdir
|
2023-12-17 16:38:53 +00:00
|
|
|
sub_rsp, !32 # allocate shadow stack space for UEFI function
|
2022-09-12 00:05:09 +01:00
|
|
|
call_[rcx+BYTE] !8 # rootfs->open_volume(rootfs, &rootdir)
|
2023-12-17 16:38:53 +00:00
|
|
|
add_rsp, !32 # deallocate stack
|
2022-09-12 00:05:09 +01:00
|
|
|
|
|
|
|
# Push command line arguments onto stack
|
|
|
|
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
|
|
|
|
:loop_options
|
|
|
|
cmp_rbx,rdx # Check if we are done
|
|
|
|
je8 !loop_options_done # We are done
|
|
|
|
sub_rbx, !2 # --options
|
|
|
|
mov_al,[rbx] # *options
|
|
|
|
cmp_al, !0x20 # if *options != ' '
|
|
|
|
jne8 !loop_options # then continue looping
|
|
|
|
mov_[rbx], !0 # zero it
|
|
|
|
add_rbx, !2 # ++options
|
|
|
|
push_rbx # push another argument onto stack
|
|
|
|
jmp8 !loop_options # next argument
|
|
|
|
:loop_options_done
|
|
|
|
|
|
|
|
pop_r8 # get input file
|
|
|
|
test_r8,r8 # Check if argument is specified
|
|
|
|
jne8 !arg_done # then use it
|
|
|
|
# Else use default_file
|
|
|
|
lea_r8,[rip+DWORD] %default_file # Use "kaem.amd64"
|
|
|
|
:arg_done
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
# Open file for reading
|
2023-12-10 01:17:39 +00:00
|
|
|
and_rsp, !-16 # align stack to 16 bytes
|
2022-09-12 00:05:09 +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
|
|
|
|
# arg3 = script
|
|
|
|
mov_rcx,[rip+DWORD] %rootdir # arg1 = rootdir
|
|
|
|
sub_rsp, !32 # allocate shadow stack space for UEFI function
|
|
|
|
call_[rcx+BYTE] !8 # rootdir->open()
|
|
|
|
test_eax,eax # if status != EFI_SUCCESS
|
2023-12-17 16:38:53 +00:00
|
|
|
push_rax # store error code
|
2023-12-10 01:17:39 +00:00
|
|
|
jne %terminate_2 # then exit without closing file
|
2023-12-17 16:38:53 +00:00
|
|
|
pop_rax # deallocate stack
|
2022-09-12 00:05:09 +01:00
|
|
|
mov_r12,[rsp+BYTE] !40 # get fin
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
# Allocate pool for command
|
2022-09-12 00:05:09 +01:00
|
|
|
xor_edx,edx # zero RDX
|
|
|
|
mov_dh, !0x10 # arg2 = 4096 = 0x1000
|
|
|
|
call %allocate_pool # allocate memory
|
|
|
|
mov_rbx,rax # get command
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
:next_command
|
2022-09-12 00:05:09 +01:00
|
|
|
xor_esi,esi # i = 0
|
|
|
|
xor_r15,r15 # command_length = 0
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
:read_command
|
2022-09-12 00:05:09 +01:00
|
|
|
call %read_byte # read another byte c
|
2022-08-06 21:54:11 +01:00
|
|
|
|
2022-09-12 00:05:09 +01:00
|
|
|
cmp_al, !0xa # if c == '\n'
|
|
|
|
je8 !read_command_done # then we are done with this command
|
2022-08-06 21:54:11 +01:00
|
|
|
|
2022-09-12 00:05:09 +01:00
|
|
|
cmp_al, !0x20 # if c == ' '
|
|
|
|
jne8 !read_command_comments
|
|
|
|
test_r15,r15 # and command_length == 0
|
|
|
|
jne8 !read_command_comments
|
|
|
|
mov_r15,rsi # command_length = i
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
:read_command_comments
|
2022-09-12 00:05:09 +01:00
|
|
|
cmp_al, !0x23 # if c == '#' then process comment
|
|
|
|
jne8 !read_command_store_char # else store char
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
:read_command_skip_comment
|
2022-09-12 00:05:09 +01:00
|
|
|
call %read_byte # get another char
|
|
|
|
cmp_al, !0xa # if c == '\n'
|
|
|
|
jne8 !read_command_skip_comment # continue reading until newline
|
2022-08-06 21:54:11 +01:00
|
|
|
|
2022-09-12 00:05:09 +01:00
|
|
|
jmp8 !next_command # deal with another line
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
:read_command_store_char
|
2022-09-12 00:05:09 +01:00
|
|
|
add_rbx,rsi # rbx = &command[i]
|
|
|
|
mov_[rbx],ax # command[i] = c
|
|
|
|
sub_rbx,rsi # rbx = &command[0]
|
|
|
|
add_rsi, !2 # location of the next char
|
|
|
|
jmp8 !read_command # continue looping
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
:read_command_done
|
2022-09-12 00:05:09 +01:00
|
|
|
test_r15,r15 # if command_length == 0
|
|
|
|
je8 !next_command # deal with another line
|
2022-08-06 21:54:11 +01:00
|
|
|
|
2022-09-12 00:05:09 +01:00
|
|
|
add_rbx,rsi # rbx = &command[i]
|
|
|
|
mov_[rbx],WORD @0 # command[i] = 0
|
|
|
|
sub_rbx,rsi # rbx = &command[0]
|
2022-08-06 21:54:11 +01:00
|
|
|
|
2022-09-12 00:05:09 +01:00
|
|
|
add_rsi, !2 # add 2 to get string length with NULL terminator
|
2022-08-06 21:54:11 +01:00
|
|
|
|
2022-09-12 00:05:09 +01:00
|
|
|
lea_rdx,[rip+DWORD] %prefix # get prefix " +> "
|
|
|
|
call %File_Print # print it
|
|
|
|
mov_rdx,rbx # get command
|
|
|
|
call %File_Print # print it
|
|
|
|
lea_rdx,[rip+DWORD] %suffix # get suffix "\n\r"
|
|
|
|
call %File_Print # print it
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
# Remove command line options
|
2022-09-12 00:05:09 +01:00
|
|
|
add_r15,rbx # go to the space separating command and its options
|
|
|
|
mov_[r15],WORD @0 # zero it to hide command line options
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
# Open executable file for reading
|
2022-09-12 00:05:09 +01:00
|
|
|
push_rdx # allocate stack for fcmd
|
|
|
|
mov_rdx,rsp # arg2 = &fcmd
|
|
|
|
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
|
|
|
|
mov_r8,rbx # arg3 = command
|
|
|
|
mov_rcx,[rip+DWORD] %rootdir # arg1 = rootdir
|
|
|
|
sub_rsp, !32 # allocate shadow stack space for UEFI function
|
|
|
|
call_[rcx+BYTE] !8 # rootdir->open()
|
|
|
|
test_eax,eax # if status != EFI_SUCCESS
|
|
|
|
jne %print_error # then exit
|
|
|
|
add_rsp, !40 # deallocate stack
|
|
|
|
pop_rdi # get fcmd
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
# Restore command line arguments
|
2022-09-12 00:05:09 +01:00
|
|
|
mov_[r15],WORD @0x20 # restore command line options by readding ' '
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
# Allocate pool for file_info
|
2022-09-12 00:05:09 +01:00
|
|
|
xor_edx,edx # zero RDX
|
|
|
|
mov_dh, !0x10 # arg2 = 4096 = 0x1000
|
|
|
|
call %allocate_pool # allocate memory
|
|
|
|
mov_r9,rax # get file_info (arg4 for get_info)
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
# Get file info
|
2022-09-12 00:05:09 +01:00
|
|
|
push_rax # save file_info
|
|
|
|
push_rax # allocate stack for file_size
|
|
|
|
mov_r8,rsp # arg3 = &file_size
|
|
|
|
mov_[r8], %0x1000 # file_size = 0x1000
|
|
|
|
lea_rdx,[rip+DWORD] %FILE_INFO_PROTOCOL # arg2 = &EFI_FILE_INFO_PROTOCOL
|
|
|
|
mov_rcx,rdi # arg1 = fcmd
|
|
|
|
sub_rsp, !32 # allocate shadow stack space for UEFI function
|
|
|
|
call_[rcx+BYTE] !64 # fcmd->get_info(fcmd, &guid, &file_size, file_info)
|
|
|
|
add_rsp, !40 # deallocate stack
|
|
|
|
pop_rcx # restore file_info
|
|
|
|
mov_rdx,[rcx+BYTE] !8 # get file_size
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
# Free file_info pool
|
2022-09-12 00:05:09 +01:00
|
|
|
push_rdx # save file_size onto stack
|
2023-12-17 16:38:53 +00:00
|
|
|
sub_rsp, !40 # allocate shadow stack space for UEFI function and align stack
|
2022-09-12 00:05:09 +01:00
|
|
|
call_[r14+BYTE] !72 # system->boot->free_pool(file_info)
|
2023-12-17 16:38:53 +00:00
|
|
|
add_rsp, !40 # deallocate stack
|
2022-09-12 00:05:09 +01:00
|
|
|
pop_rdx # restore file_size from stack (arg2 for allocate_pool)
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
# Allocate pool for executable
|
2022-09-12 00:05:09 +01:00
|
|
|
push_rdx # save file_size onto stack
|
2023-12-17 16:38:53 +00:00
|
|
|
push_rax # align stack
|
2022-09-12 00:05:09 +01:00
|
|
|
call %allocate_pool # allocate memory
|
|
|
|
mov_r15,rax # get executable
|
2023-12-17 16:38:53 +00:00
|
|
|
pop_rdx # deallocate stack
|
2022-09-12 00:05:09 +01:00
|
|
|
pop_rdx # restore file_size
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
# Load executable into memory
|
2022-09-12 00:05:09 +01:00
|
|
|
push_rdx # save file_size onto stack
|
|
|
|
mov_r8,r15 # arg3 = executable
|
|
|
|
mov_rdx,rsp # arg2 = &file_size
|
|
|
|
mov_rcx,rdi # arg1 = fcmd
|
2023-12-17 16:38:53 +00:00
|
|
|
sub_rsp, !40 # allocate shadow stack space for UEFI and align stack
|
2022-09-12 00:05:09 +01:00
|
|
|
call_[rcx+BYTE] !32 # fcmd->read(fcmd, &file_size, executable)
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
# Close fcmd
|
2022-09-12 00:05:09 +01:00
|
|
|
mov_rcx,rdi # arg1 = fcmd
|
|
|
|
call_[rcx+BYTE] !16 # fcmd->close(fcmd)
|
2023-12-17 16:38:53 +00:00
|
|
|
add_rsp, !40 # deallocate stack
|
2022-08-06 21:54:11 +01:00
|
|
|
|
2022-09-12 00:05:09 +01:00
|
|
|
pop_rdi # restore file_size
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
# Allocate memory for device_path struct
|
2022-09-12 00:05:09 +01:00
|
|
|
push !28 # 4 + sizeof(struct efi_device_path_protocol)
|
|
|
|
pop_rdx # arg2 = 28
|
|
|
|
call %allocate_pool # allocate memory
|
|
|
|
mov_r8,rax # get device_path
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
# Initialize struct
|
2022-09-12 00:05:09 +01:00
|
|
|
mov_[rax],BYTE !1 # device_path->type = HARDWARE_DEVICE_PATH
|
|
|
|
inc_rax # next member
|
|
|
|
mov_[rax],BYTE !3 # device_path->subtype = MEMORY_MAPPED
|
|
|
|
inc_rax # next member
|
|
|
|
mov_[rax],WORD @24 # device_path->length = 24
|
|
|
|
add_rax, !2 # next member
|
|
|
|
mov_[rax], %1 # device_path->memory_type = EFI_LOADER_CODE
|
|
|
|
add_rax, !4 # next member
|
|
|
|
mov_[rax],r15 # device_path->start_address = executable
|
|
|
|
add_rax, !8 # next member
|
|
|
|
add_r15,rdi # end_address = executable + file_size
|
|
|
|
mov_[rax],r15 # device_path->end_address = end_address
|
|
|
|
sub_r15,rdi # restore r15 = executable
|
|
|
|
add_rax, !8 # next member
|
|
|
|
mov_[rax],BYTE !0x7f # device_path[1].type = END_HARDWARE_DEVICE_PATH
|
|
|
|
inc_rax # next member
|
|
|
|
mov_[rax],BYTE !0xff # device_path[1].subtype = END_ENTIRE_DEVICE_PATH
|
|
|
|
inc_rax # next member
|
|
|
|
mov_[rax],WORD @4 # device_path[1].length = 4
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
# Load image
|
2022-09-12 00:05:09 +01:00
|
|
|
push_r8 # save device_path
|
|
|
|
push_rax # allocate stack for child_ih
|
|
|
|
push_rsp # arg6 = &child_ih
|
|
|
|
push_rdi # arg5 = file size
|
|
|
|
mov_r9,r15 # arg4 = executable
|
|
|
|
# arg3 = device_path
|
|
|
|
mov_rdx,[rip+DWORD] %image_handle # arg2 = image_handle
|
|
|
|
xor_ecx,ecx # arg1 = 0
|
|
|
|
sub_rsp, !32 # allocate shadow stack space for UEFI function
|
|
|
|
call_[r14+DWORD] %200 # system->boot->load_image()
|
|
|
|
add_rsp, !48 # deallocate stack
|
|
|
|
pop_rdi # save child_ih
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
# Free device_path pool
|
2022-09-12 00:05:09 +01:00
|
|
|
pop_rcx # arg1 = device_path
|
2023-12-17 16:38:53 +00:00
|
|
|
sub_rsp, !32 # allocate shadow stack space for UEFI function
|
2022-09-12 00:05:09 +01:00
|
|
|
call_[r14+BYTE] !72 # system->boot->free_pool(device_path)
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
# Free executable pool
|
2022-09-12 00:05:09 +01:00
|
|
|
mov_rcx,r15 # arg1 = executable
|
|
|
|
call_[r14+BYTE] !72 # system->boot->free_pool(executable)
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
# Open Child Image
|
2022-09-12 00:05:09 +01:00
|
|
|
mov_r9,rdi # arg4 = child_ih
|
2023-12-17 16:38:53 +00:00
|
|
|
push_rdx # align stack
|
2022-09-12 00:05:09 +01:00
|
|
|
lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL
|
|
|
|
push_rdx # save &LOADED_IMAGE_PROTOCOL
|
|
|
|
mov_rcx,r9 # arg1 = child_ih
|
|
|
|
call %open_protocol # open protocol
|
|
|
|
|
|
|
|
mov_[rax+BYTE],rbx !56 # child_image->load_options = command
|
|
|
|
mov_[rax+BYTE],rsi !48 # set child_image->load_options_size
|
|
|
|
mov_rcx,[rip+DWORD] %image # get image
|
|
|
|
mov_rcx,[rcx+BYTE] !24 # image->device
|
|
|
|
mov_[rax+BYTE],rcx !24 # child_image->device = image->device
|
|
|
|
|
|
|
|
mov_r8,rdi # arg3 = image_handle
|
|
|
|
pop_rdx # arg2 = &LOADED_IMAGE_PROTOCOL
|
2023-12-17 16:38:53 +00:00
|
|
|
pop_rax # deallocate stack
|
2022-09-12 00:05:09 +01:00
|
|
|
mov_rcx,r8 # arg1 = image_handle
|
|
|
|
call %close_protocol # close protocol
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
# Run command
|
2022-09-12 00:05:09 +01:00
|
|
|
xor_r8,r8 # arg3 = 0 (ExitData)
|
|
|
|
xor_edx,edx # arg2 = 0 (ExitData size)
|
|
|
|
mov_rcx,rdi # arg1 = child_ih
|
|
|
|
call_[r14+DWORD] %208 # system->boot->start_image()
|
2023-12-17 16:38:53 +00:00
|
|
|
add_rsp, !32 # deallocate stack
|
2022-09-12 00:05:09 +01:00
|
|
|
|
|
|
|
test_eax,eax # check if return code is 0
|
|
|
|
jne8 !print_error # print error and exit
|
|
|
|
jmp %next_command # process another line from kaem script
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
:print_error
|
2023-12-17 16:38:53 +00:00
|
|
|
push_rax # align stack
|
2022-09-12 00:05:09 +01:00
|
|
|
push_rax # save exit code
|
|
|
|
lea_rdx,[rip+DWORD] %subprocess_error # get error message
|
|
|
|
call %File_Print # print it
|
|
|
|
pop_rax # restore exit code
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
# Close script file and exit
|
|
|
|
:terminate
|
2023-12-17 16:38:53 +00:00
|
|
|
push_rax # align stack
|
2023-12-10 01:17:39 +00:00
|
|
|
|
2022-08-06 21:54:11 +01:00
|
|
|
# Free pool
|
2022-09-12 00:05:09 +01:00
|
|
|
mov_rcx,rbx # arg1 = command
|
|
|
|
push_rax # save exit code
|
2023-12-17 16:38:53 +00:00
|
|
|
sub_rsp, !40 # allocate shadow stack space for UEFI function
|
2022-09-12 00:05:09 +01:00
|
|
|
call_[r14+BYTE] !72 # system->boot->free_pool(commmand)
|
|
|
|
|
|
|
|
mov_rcx,r12 # arg1 = fin
|
|
|
|
call_[rcx+BYTE] !16 # fin->close(fin)
|
|
|
|
|
|
|
|
mov_rcx,[rip+DWORD] %rootdir # arg1 = rootdir
|
|
|
|
call_[rcx+BYTE] !16 # rootdir->close(rootdir)
|
2022-08-06 21:54:11 +01:00
|
|
|
|
2023-12-17 16:38:53 +00:00
|
|
|
add_rsp, !40 # deallocate stack
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
# Exit without closing script file
|
2022-09-12 00:05:09 +01:00
|
|
|
:terminate_2
|
|
|
|
mov_r8,[rip+DWORD] %image_handle # arg3 = image_handle
|
|
|
|
push_r8 # save image_handle
|
|
|
|
lea_rdx,[rip+DWORD] %SIMPLE_FS_PROTOCOL # guid = &SIMPLE_FS_PROTOCOL
|
|
|
|
mov_rcx,[rip+DWORD] %root_device # arg1 = root_device
|
|
|
|
call %close_protocol # close protocol
|
|
|
|
|
|
|
|
pop_r8 # arg3 = image_handle
|
2023-12-17 16:38:53 +00:00
|
|
|
push_rax # align stack to 16 bytes
|
2022-09-12 00:05:09 +01:00
|
|
|
lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL
|
|
|
|
mov_rcx,r8 # arg1 = image_handle
|
|
|
|
call %close_protocol # close protocol
|
2023-12-17 16:38:53 +00:00
|
|
|
pop_rax # dellocate stack
|
|
|
|
|
|
|
|
pop_rax # restore exit code
|
2022-09-12 00:05:09 +01:00
|
|
|
|
2023-12-17 16:38:53 +00:00
|
|
|
:abort
|
2023-12-10 01:17:39 +00:00
|
|
|
# Restore non-volatile registers
|
2023-12-17 16:38:53 +00:00
|
|
|
mov_rsp,rbp # restore stack
|
2023-12-10 01:17:39 +00:00
|
|
|
pop_r15
|
|
|
|
pop_r14
|
|
|
|
pop_r13
|
|
|
|
pop_r12
|
|
|
|
pop_rsi
|
|
|
|
pop_rdi
|
|
|
|
pop_rbx
|
|
|
|
pop_rbp
|
2023-12-10 23:10:28 +00:00
|
|
|
ret # return to UEFI
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
# read_byte function
|
|
|
|
# reads a single character
|
|
|
|
:read_byte
|
2022-09-12 00:05:09 +01:00
|
|
|
mov_rcx,r12 # arg1 = fin
|
|
|
|
push !1 # size = 1
|
|
|
|
mov_rdx,rsp # arg2 = &size
|
2022-10-09 21:35:00 +01:00
|
|
|
push !0 # allocate stack
|
2022-09-12 00:05:09 +01:00
|
|
|
mov_r8,rsp # arg3 = &c
|
2023-12-17 16:38:53 +00:00
|
|
|
sub_rsp, !40 # allocate shadow stack space for UEFI and align stack
|
2022-09-12 00:05:09 +01:00
|
|
|
call_[rcx+BYTE] !32 # fin->read()
|
2023-12-17 16:38:53 +00:00
|
|
|
add_rsp, !40 # deallocate stack
|
2022-09-12 00:05:09 +01:00
|
|
|
pop_rax # save c to rax
|
|
|
|
pop_rcx # save size to rcx
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
# If the file ended (0 bytes read) terminate
|
2022-09-12 00:05:09 +01:00
|
|
|
test_ecx,ecx # if size = 0
|
2023-12-17 16:38:53 +00:00
|
|
|
je8 !terminate # then we are done
|
2022-09-12 00:05:09 +01:00
|
|
|
|
|
|
|
ret # return
|
|
|
|
|
2023-12-17 16:38:53 +00:00
|
|
|
# File_Print function
|
|
|
|
# Receives WCHAR* in RDX
|
|
|
|
:File_Print
|
|
|
|
mov_rcx,[rip+DWORD] %system_out # get system_out
|
|
|
|
sub_rsp, !40 # allocate shadow stack space for UEFI function
|
|
|
|
call_[rcx+BYTE] !8 # system->out->output_string(system->out, WCHAR*)
|
|
|
|
add_rsp, !40 # deallocate stack
|
|
|
|
ret
|
|
|
|
|
2022-09-12 00:05:09 +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)
|
2023-12-17 16:38:53 +00:00
|
|
|
add_rsp, !48 # deallocate stack
|
2022-09-12 00:05:09 +01:00
|
|
|
pop_rax # get interface
|
|
|
|
ret
|
|
|
|
|
|
|
|
# rcx: handle
|
|
|
|
# rdx: &guid
|
|
|
|
# r8: agent_handle
|
|
|
|
:close_protocol
|
|
|
|
xor_r9,r9 # arg4 = NULL
|
2023-12-17 16:38:53 +00:00
|
|
|
sub_rsp, !40 # allocate shadow stack space for UEFI function
|
2022-09-12 00:05:09 +01:00
|
|
|
call_[r14+DWORD] %288 # system->boot->close_protocol(handle, &guid, agent_handle, 0)
|
2023-12-17 16:38:53 +00:00
|
|
|
add_rsp, !40 # deallocate stack
|
2022-09-12 00:05:09 +01:00
|
|
|
ret
|
|
|
|
|
|
|
|
# rdx: number of bytes to allocate
|
|
|
|
# r14: system->boot
|
|
|
|
# returns pointer in rax
|
|
|
|
:allocate_pool
|
|
|
|
push_rdx # allocate stack for pool pointer
|
|
|
|
mov_r8,rsp # arg3 = &pool
|
|
|
|
push !2
|
|
|
|
pop_rcx # arg1 = EFI_LOADER_DATA
|
2023-12-10 01:17:39 +00:00
|
|
|
sub_rsp, !32 # allocate shadow stack space for UEFI and align stack
|
2022-09-12 00:05:09 +01:00
|
|
|
call_[r14+BYTE] !64 # system->boot->allocate_pool(EFI_LOADER_DATA, 2048, &pool)
|
2023-12-17 16:38:53 +00:00
|
|
|
add_rsp, !32 # deallocate stack
|
2022-09-12 00:05:09 +01:00
|
|
|
pop_rax # get pool
|
|
|
|
ret
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Protocol GUIDs
|
|
|
|
:LOADED_IMAGE_PROTOCOL
|
|
|
|
%0x5b1b31a1
|
2023-12-10 01:17:39 +00:00
|
|
|
$0x9562
|
|
|
|
$0x11d2
|
2022-08-06 21:54:11 +01:00
|
|
|
!0x8e !0x3f !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b
|
|
|
|
|
|
|
|
:SIMPLE_FS_PROTOCOL
|
2022-10-23 22:59:37 +01:00
|
|
|
%0x964e5b22
|
2023-12-10 01:17:39 +00:00
|
|
|
$0x6459
|
|
|
|
$0x11d2
|
2022-08-06 21:54:11 +01:00
|
|
|
!0x8e !0x39 !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b
|
|
|
|
|
|
|
|
:FILE_INFO_PROTOCOL
|
|
|
|
%0x09576e92
|
2023-12-10 01:17:39 +00:00
|
|
|
$0x6d3f
|
|
|
|
$0x11d2
|
2022-09-12 00:05:09 +01:00
|
|
|
!0x8e !0x39 !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b
|
2022-08-06 21:54:11 +01:00
|
|
|
|
|
|
|
:default_file
|
|
|
|
"k" "a" "e" "m" "." "a" "m" "d" "6" "4" @0
|
|
|
|
|
|
|
|
:prefix
|
|
|
|
" " "+" ">" " " @0
|
|
|
|
|
|
|
|
:subprocess_error
|
|
|
|
"S" "u" "b" "p" "r" "o" "c" "e" "s" "s" " "
|
|
|
|
"e" "r" "r" "o" "r"
|
|
|
|
:suffix
|
|
|
|
!0xa !0 !0xd !0 @0 # L"\n\r"
|
|
|
|
|
|
|
|
:image_handle
|
|
|
|
%0 %0
|
|
|
|
|
|
|
|
:image
|
|
|
|
%0 %0
|
|
|
|
|
2022-09-12 00:05:09 +01:00
|
|
|
:rootdir
|
|
|
|
%0 %0
|
|
|
|
|
|
|
|
:root_device
|
|
|
|
%0 %0
|
|
|
|
|
2022-08-06 21:54:11 +01:00
|
|
|
:system_out
|
|
|
|
%0 %0
|
|
|
|
|
2022-10-11 00:55:46 +01:00
|
|
|
:ELF_end
|