401 lines
19 KiB
Plaintext
401 lines
19 KiB
Plaintext
# SPDX-FileCopyrightText: 2022 Andrius Štikonas <andrius@stikonas.eu>
|
|
# SPDX-FileCopyrightText: 2017 Jeremiah Orians <jeremiah@pdp10.guru>
|
|
#
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
# efi_main(void *image_handle, struct efi_system_table *system)
|
|
:_start
|
|
4889E5 ; COPY_RSP_to_RBP # save stack pointer
|
|
4989CF ; COPY_RCX_to_R15 # save image_handle
|
|
4C8B72 60 ; LOAD64_into_R14_from_Address_RDX_Immediate8 !96 # system->boot
|
|
4C8935 %SystemBoot ; STORE64_rel_R14 %SystemBoot # save system->boot
|
|
|
|
# Allocate pool for single-character label table
|
|
52 ; PUSH_RDX # allocate stack for table
|
|
4989E0 ; COPY_RSP_to_R8 # arg3 = &table
|
|
31D2 ; XOR_EDX_EDX # zero rdx
|
|
B6 08 ; LOADI8_DH !0x8 # arg2 = 256 * 8 = 2048 = 0x800
|
|
6A 02 ; PUSH !2
|
|
59 ; POP_RCX # arg1 = EFI_LOADER_DATA
|
|
4883EC 18 ; SUBI8_RSP !24 # allocate shadow stack space for UEFI
|
|
41FF56 40 ; CALL_R14_Immediate8 !64 # system->boot->allocate_pool(EFI_LOADER_DATA, 2048, &table)
|
|
4883C4 18 ; ADDI8_RSP !24 # deallocate stack
|
|
58 ; POP_RAX # get table
|
|
488905 %table ; STORE64_rel_RAX %table # save table
|
|
|
|
# Open Loaded Image protocol
|
|
50 ; PUSH_RAX # allocate stack for image
|
|
4989E0 ; COPY_RSP_to_R8 # arg3 = &image
|
|
488B15 %LOADED_IMAGE_PROTOCOL_8 ; LOAD64_rel_RDX !LOADED_IMAGE_PROTOCOL_8 # EFI_LOADED_IMAGE_PROTOCOL_GUID (last 64 bits)
|
|
52 ; PUSH_RDX # push last 64 bits onto stack
|
|
488B15 %LOADED_IMAGE_PROTOCOL ; LOAD64_rel_RDX !LOADED_IMAGE_PROTOCOL # EFI_LOADED_IMAGE_PROTOCOL_GUID (first 64 bits)
|
|
52 ; PUSH_RDX # push first 64 bits onto stack
|
|
4889E2 ; COPY_RSP_to_RDX # arg2 = &guid
|
|
6A 01 ; PUSH !1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
|
|
6A 00 ; PUSH !0 # arg5 = NULL
|
|
4D89F9 ; COPY_R15_to_R9 # arg4 = image_handle
|
|
4C89F9 ; COPY_R15_to_RCX # arg1 = image_handle
|
|
4883EC 20 ; SUBI8_RSP !32 # allocate shadow stack space for UEFI function
|
|
41FF96 18010000 ; CALL_R14_Immediate32 %280 # system->boot->open_protocol(image_handle, &guid, &image, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
|
|
488B4424 40 ; LOAD64_into_RAX_from_Address_RSP_Immediate8 !64 # get_image
|
|
4889C1 ; COPY_RCX_to_RAX # save image
|
|
|
|
# Command line args
|
|
488B58 38 ; LOAD64_into_RBX_from_Address_RAX_Immediate8 !56 # options = image->load_options
|
|
|
|
:loop_options1 # Skip application name
|
|
4883C3 02 ; ADDI8_RBX !2 # ++options
|
|
8A03 ; LOAD8_AL_from_Address_RBX # *options
|
|
3C 20 ; CMPI8_AL !0x20 # if *options != ' '
|
|
75 !loop_options1 ; JNE8 !loop_options1 # then jump
|
|
|
|
4883C3 02 ; ADDI8_RBX !2 # ++options
|
|
4989DC ; COPY_RBX_to_R12 # save input file
|
|
|
|
:loop_options2 # Skip argv[1]
|
|
4883C3 02 ; ADDI8_RBX !2 # ++options
|
|
8A03 ; LOAD8_AL_from_Address_RBX # *options
|
|
3C 20 ; CMPI8_AL !0x20 # if *options != ' '
|
|
75 !loop_options2 ; JNE8 !loop_options2 # then jump
|
|
|
|
C603 00 ; STOREI8_into_Address_RBX !0 # *options = 0;
|
|
4883C3 02 ; ADDI8_RBX !2 # ++options
|
|
4989DD ; COPY_RBX_to_R13 # save output file
|
|
|
|
# Get root file system
|
|
50 ; PUSH_RAX # allocate stack for rootfs
|
|
4989E0 ; COPY_RSP_to_R8 # arg3 = &rootfs
|
|
488B15 %SIMPLE_FS_PROTOCOL_8 ; LOAD64_rel_RDX %SIMPLE_FS_PROTOCOL_8 # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID (last 64 bits)
|
|
52 ; PUSH_RDX # push last 64 bits onto stack
|
|
488B15 %SIMPLE_FS_PROTOCOL ; LOAD64_rel_RDX %SIMPLE_FS_PROTOCOL # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID (first 64 bits)
|
|
52 ; PUSH_RDX # push first 64 bits onto stack
|
|
4889E2 ; COPY_RSP_to_RDX # arg2 = &guid
|
|
6A 01 ; PUSH !1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
|
|
6A 00 ; PUSH !0 # arg5 = NULL
|
|
4D89F9 ; COPY_R15_to_R9 # arg4 = image_handle
|
|
488B49 18 ; LOAD64_into_RCX_from_Address_RCX_Immediate8 !24 # arg1 = root_device = image->device
|
|
4883EC 20 ; SUBI8_RSP !32 # allocate shadow stack space for UEFI function
|
|
41FF96 18010000 ; CALL_R14_Immediate32 %280 # system->boot->open_protocol(root_device, &guid, &rootfs, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
|
|
488B4C24 40 ; LOAD64_into_RCX_from_Address_RSP_Immediate8 !64 # get rootfs
|
|
|
|
# Get root directory
|
|
52 ; PUSH_RDX # allocate stack for rootdir
|
|
4889E2 ; COPY_RSP_to_RDX # 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_Immediate8 !8 # rootfs->open_volume(rootfs, &rootdir)
|
|
58 ; POP_RAX # deallocate stack
|
|
58 ; POP_RAX # deallocate stack
|
|
415E ; POP_R14 # save &rootdir
|
|
|
|
# Open file for reading
|
|
52 ; PUSH_RDX # allocate stack for fin
|
|
4889E2 ; COPY_RSP_to_RDX # 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
|
|
4D89E0 ; COPY_R12_to_R8 # arg3 = in
|
|
4C89F1 ; COPY_R14_to_RCX # arg1 = rootdir
|
|
4883EC 20 ; SUBI8_RSP !32 # allocate shadow stack space for UEFI function
|
|
FF51 08 ; CALL_RCX_Immediate8 !8 # rootdir->open()
|
|
488B7C24 28 ; LOAD64_into_RDI_from_Address_RSP_Immediate8 !40 # get fin
|
|
|
|
# Open file for writing
|
|
52 ; PUSH_RDX # allocate stack for fout
|
|
4889E2 ; COPY_RSP_to_RDX # 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
|
|
4D89E8 ; COPY_R13_to_R8 # arg3 = out
|
|
4C89F1 ; COPY_R14_to_RCX # arg1 = rootdir
|
|
4883EC 20 ; SUBI8_RSP !32 # allocate shadow stack space for UEFI function
|
|
FF51 08 ; CALL_RCX_Immediate8 !8 # rootdir->open()
|
|
488B5C24 28 ; LOAD64_into_RBX_from_Address_RSP_Immediate8 !40 # get fout
|
|
|
|
49C7C7 FFFFFFFF ; LOADI32_R15 %-1 # Our flag for byte processing
|
|
49C7C6 00000000 ; LOADI32_R14 %0 # temp storage for the sum
|
|
49C7C5 00000000 ; LOADI32_R13 %0 # Our starting IP
|
|
E8 %First_pass ; CALLI32 %First_pass # Process it
|
|
|
|
# rewind input file
|
|
4889F9 ; COPY_RDI_to_RCX # 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_Immediate8 !56 # fin->set_position(fin, 0)
|
|
58 ; POP_RAX # deallocate stack
|
|
58 ; POP_RAX # deallocate stack
|
|
|
|
49C7C7 FFFFFFFF ; LOADI32_R15 %-1 # Our flag for byte processing
|
|
49C7C6 00000000 ; LOADI32_R14 %0 # temp storage for the sum
|
|
49C7C5 00000000 ; LOADI32_R13 %0 # Our starting IP
|
|
E8 %Second_pass ; CALLI32 %Second_pass # Process it
|
|
|
|
E9 %Done ; JMP32 %Done
|
|
|
|
:First_pass
|
|
E8 %Read_byte ; CALLI32 %Read_byte
|
|
|
|
# Deal with EOF
|
|
483D FCFFFFFF ; CMPI32_RAX %-4
|
|
0F84 %First_pass_done ; JE32 %First_pass_done
|
|
|
|
# Check for :
|
|
483D 3A000000 ; CMPI32_RAX %0x3a
|
|
0F85 %First_pass_0 ; JNE32 %First_pass_0
|
|
|
|
# Deal with label
|
|
E8 %StoreLabel ; CALLI32 %StoreLabel
|
|
|
|
:First_pass_0
|
|
# Check for %
|
|
483D 25000000 ; CMPI32_RAX %0x25
|
|
0F84 %First_pass_pointer ; JE32 %First_pass_pointer
|
|
|
|
# Deal with everything else
|
|
E8 %hex ; CALLI32 %hex # Process our char
|
|
|
|
# Deal with EOF
|
|
483D FCFFFFFF ; CMPI32_RAX %-4
|
|
0F84 %First_pass_done ; JE32 %First_pass_done
|
|
|
|
# deal with -1 values
|
|
483D 00000000 ; CMPI32_RAX %0
|
|
0F8C %First_pass ; JL32 %First_pass
|
|
|
|
# deal with toggle
|
|
4981FF 00000000 ; CMPI32_R15 %0
|
|
0F84 %First_pass_1 ; JE32 %First_pass_1
|
|
4981C5 01000000 ; ADDI32_to_R13 %1 # Increment IP
|
|
|
|
:First_pass_1
|
|
49F7D7 ; NOT_R15
|
|
E9 %First_pass ; JMP32 %First_pass
|
|
|
|
:First_pass_pointer
|
|
# Deal with Pointer to label
|
|
E8 %Read_byte ; CALLI32 %Read_byte # Drop the char
|
|
4981C5 04000000 ; ADDI32_to_R13 %4 # Increment IP
|
|
E9 %First_pass ; JMP32 %First_pass # Loop again
|
|
|
|
:First_pass_done
|
|
C3 ; RET
|
|
|
|
:hex
|
|
# deal with EOF
|
|
483D FCFFFFFF ; CMPI32_RAX %-4
|
|
0F84 %EOF ; JE32 %EOF
|
|
# deal with line comments starting with #
|
|
483D 23000000 ; CMPI32_RAX %0x23
|
|
0F84 %ascii_comment ; JE32 %ascii_comment
|
|
# deal with line comments starting with ;
|
|
483D 3B000000 ; CMPI32_RAX %0x3b
|
|
0F84 %ascii_comment ; JE32 %ascii_comment
|
|
# deal all ascii less than 0
|
|
483D 30000000 ; CMPI32_RAX %0x30
|
|
0F8C %ascii_other ; JL32 %ascii_other
|
|
# deal with 0-9
|
|
483D 3A000000 ; CMPI32_RAX %0x3a
|
|
0F8C %ascii_num ; JL32 %ascii_num
|
|
# deal with all ascii less than A
|
|
483D 41000000 ; CMPI32_RAX %0x41
|
|
0F8C %ascii_other ; JL32 %ascii_other
|
|
# deal with A-F
|
|
483D 47000000 ; CMPI32_RAX %0x47
|
|
0F8C %ascii_high ; JL32 %ascii_high
|
|
# deal with all ascii less than a
|
|
483D 61000000 ; CMPI32_RAX %0x61
|
|
0F8C %ascii_other ; JL32 %ascii_other
|
|
# deal with a-f
|
|
483D 67000000 ; CMPI32_RAX %0x67
|
|
0F8C %ascii_low ; JL32 %ascii_low
|
|
# The rest that remains needs to be ignored
|
|
E9 %ascii_other ; JMP32 %ascii_other
|
|
|
|
:Second_pass
|
|
E8 %Read_byte ; CALLI32 %Read_byte
|
|
|
|
# Deal with EOF
|
|
483D FCFFFFFF ; CMPI32_RAX %-4
|
|
0F84 %Second_pass_done ; JE32 %Second_pass_done
|
|
|
|
# Simply drop the label
|
|
483D 3A000000 ; CMPI32_RAX %0x3a
|
|
0F85 %Second_pass_0 ; JNE32 %Second_pass_0
|
|
|
|
E8 %Read_byte ; CALLI32 %Read_byte
|
|
E9 %Second_pass ; JMP32 %Second_pass
|
|
|
|
:Second_pass_0
|
|
# Deal with % pointer
|
|
483D 25000000 ; CMPI32_RAX %0x25
|
|
0F85 %Second_pass_1 ; JNE32 %Second_pass_1
|
|
|
|
E8 %StorePointer ; CALLI32 %StorePointer
|
|
E9 %Second_pass ; JMP32 %Second_pass
|
|
|
|
:Second_pass_1
|
|
# Deal with everything else
|
|
E8 %hex ; CALLI32 %hex # Process our char
|
|
|
|
# Deal with EOF
|
|
483D FCFFFFFF ; CMPI32_RAX %-4
|
|
0F84 %Second_pass_done ; JE32 %Second_pass_done
|
|
|
|
# deal with -1 values
|
|
483D 00000000 ; CMPI32_RAX %0
|
|
0F8C %Second_pass ; JL32 %Second_pass
|
|
|
|
# deal with toggle
|
|
4981FF 00000000 ; CMPI32_R15 %0
|
|
0F84 %print ; JE32 %print
|
|
|
|
# process first byte of pair
|
|
4989C6 ; COPY_RAX_to_R14
|
|
49C7C7 00000000 ; LOADI32_R15 %0
|
|
E9 %Second_pass ; JMP32 %Second_pass
|
|
|
|
:Second_pass_done
|
|
C3 ; RET
|
|
|
|
:EOF
|
|
C3 ; RET
|
|
:ascii_num
|
|
4883E8 30 ; SUBI8_from_RAX !0x30
|
|
C3 ; RET
|
|
:ascii_low
|
|
4883E8 57 ; SUBI8_from_RAX !0x57
|
|
C3 ; RET
|
|
:ascii_high
|
|
4883E8 37 ; SUBI8_from_RAX !0x37
|
|
C3 ; RET
|
|
:ascii_other
|
|
48C7C0 FFFFFFFF ; LOADI32_RAX %-1
|
|
C3 ; RET
|
|
:ascii_comment
|
|
E8 %Read_byte ; CALLI32 %Read_byte
|
|
483D 0D000000 ; CMPI32_RAX %0xd
|
|
0F84 %ascii_comment_cr ; JE32 %ascii_comment_cr
|
|
483D 0A000000 ; CMPI32_RAX %0xa
|
|
0F85 %ascii_comment ; JNE32 %ascii_comment
|
|
:ascii_comment_cr
|
|
48C7C0 FFFFFFFF ; LOADI32_RAX %-1
|
|
C3 ; RET
|
|
|
|
# process second byte of pair
|
|
:print
|
|
# update the sum and store in output
|
|
49C1E6 04 ; SHL8_R14 !4
|
|
4C01F0 ; ADD_R14_to_RAX
|
|
|
|
# flip the toggle
|
|
49F7D7 ; NOT_R15 # R15 = -1
|
|
|
|
48C7C2 01000000 ; LOADI32_RDX %1 # set the size of chars we want
|
|
E8 %print_chars ; CALLI32 %print_chars
|
|
|
|
4981C5 01000000 ; ADDI32_to_R13 %1 # Increment IP
|
|
E9 %Second_pass ; JMP32 %Second_pass
|
|
|
|
|
|
:Read_byte
|
|
4889F9 ; COPY_RDI_to_RCX # arg1 = fin
|
|
6A 01 ; PUSH !1 # size = 1
|
|
4889E2 ; COPY_RSP_to_RDX # arg2 = &size
|
|
56 ; PUSH_RSI # allocate stack
|
|
4989E0 ; COPY_RSP_to_R8 # 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_Immediate8 !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
|
|
|
|
# If the file ended (0 bytes read) return EOF
|
|
85F6 ; TEST_ESI_ESI # if size = 0
|
|
75 !Read_byte_1 ; JNE8 !Read_byte_1
|
|
48C7C0 FCFFFFFF ; LOADI32_RAX %-4 # Put EOF in rax
|
|
|
|
:Read_byte_1
|
|
C3 ; RET # return
|
|
|
|
# Writes bytes stored in rax
|
|
:print_chars
|
|
4889D9 ; COPY_RBX_to_RCX # arg1 = fout
|
|
52 ; PUSH_RDX # set size
|
|
4889E2 ; COPY_RSP_to_RDX # arg2 = &size
|
|
50 ; PUSH_RAX # allocate stack
|
|
4989E0 ; COPY_RSP_to_R8 # 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_Immediate8 !40 # fout->write()
|
|
4883C4 28 ; ADDI8_RSP !40 # deallocate stack
|
|
|
|
C3 ; RET # return
|
|
|
|
:Get_table_target
|
|
E8 %Read_byte ; CALLI32 %Read_byte # Get single char label
|
|
48C1E0 03 ; SHL8_RAX !3 # Each label in table takes 8 bytes to store
|
|
488B0D %table ; LOAD64_rel_RCX %table # Get table
|
|
4801C8 ; ADD_RCX_to_RAX # Calculate offset
|
|
C3 ; RET
|
|
|
|
:StoreLabel
|
|
E8 %Get_table_target ; CALLI32 %Get_table_target
|
|
4C8928 ; STORE32_R13_to_Address_in_RAX # Write out pointer to table
|
|
C3 ; RET
|
|
|
|
:StorePointer
|
|
4981C5 04000000 ; ADDI32_to_R13 %4 # Increment IP
|
|
E8 %Get_table_target ; CALLI32 %Get_table_target # Get address of pointer
|
|
678B00 ; LOAD32_Address_in_RAX_into_RAX # Get pointer
|
|
4C29E8 ; SUB_R13_from_RAX # target - ip
|
|
48C7C2 04000000 ; LOADI32_RDX %4 # set the size of chars we want
|
|
E8 %print_chars ; CALLI32 %print_chars
|
|
C3 ; RET
|
|
|
|
:Done
|
|
# Free pool
|
|
488B0D %table ; LOAD64_rel_RCX %table # arg1 = table
|
|
50 ; PUSH_RAX # allocate shadow stack space for UEFI function
|
|
4C8B35 %SystemBoot ; LOAD64_rel_R14 %SystemBoot # get system->boot
|
|
41FF56 48 ; CALL_R14_Immediate8 !72 # system->boot->free_pool(table)
|
|
|
|
4889F9 ; COPY_RDI_to_RCX # arg1 = fin
|
|
FF51 10 ; CALL_RCX_Immediate8 !16 # fin->close()
|
|
4889D9 ; COPY_RBX_to_RCX # arg1 = fout
|
|
FF51 10 ; CALL_RCX_Immediate8 !16 # fout->close()
|
|
|
|
4889EC ; COPY_RBP_to_RSP # restore stack
|
|
C3 ; RET # return to UEFI
|
|
|
|
|
|
# Protocol GUIDs
|
|
:LOADED_IMAGE_PROTOCOL
|
|
A1 31 1B 5B ; %0x5b1b31a1
|
|
62 95 ; @0x9562
|
|
D2 11 ; @0x11d2
|
|
:LOADED_IMAGE_PROTOCOL_8
|
|
8E 3F 00 A0 C9 69 72 3B ; !0x8e !0x3f !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b
|
|
|
|
:SIMPLE_FS_PROTOCOL
|
|
22 5B 4E 96 ; %0x0964e5b22
|
|
59 64 ; @0x6459
|
|
D2 11 ; @0x11d2
|
|
:SIMPLE_FS_PROTOCOL_8
|
|
8E 39 00 A0 C9 69 72 3B ; !0x8e !0x39 !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b
|
|
|
|
:table
|
|
00000000 00000000
|
|
|
|
:SystemBoot
|
|
00000000 00000000
|
|
|
|
:PE32_end
|