From 95ab01638327b3c01ef8740b434d4c849a724d10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrius=20=C5=A0tikonas?= Date: Mon, 8 Aug 2022 01:21:41 +0100 Subject: [PATCH] Add hex1.hex2 prototype. --- amd64/Development/hex1.M1 | 4 +- amd64/Development/hex1.hex2 | 379 ++++++++++++++++++++++++++++++++++++ 2 files changed, 381 insertions(+), 2 deletions(-) create mode 100644 amd64/Development/hex1.hex2 diff --git a/amd64/Development/hex1.M1 b/amd64/Development/hex1.M1 index 4f61260..e78263b 100644 --- a/amd64/Development/hex1.M1 +++ b/amd64/Development/hex1.M1 @@ -261,10 +261,10 @@ DEFINE TEST_ESI_ESI 85F6 # deal with A-F CMPI32_RAX %0x47 JL32 %ascii_high - #deal with all ascii less than a + # deal with all ascii less than a CMPI32_RAX %0x61 JL32 %ascii_other - #deal with a-f + # deal with a-f CMPI32_RAX %0x67 JL32 %ascii_low # The rest that remains needs to be ignored diff --git a/amd64/Development/hex1.hex2 b/amd64/Development/hex1.hex2 new file mode 100644 index 0000000..b4f6d93 --- /dev/null +++ b/amd64/Development/hex1.hex2 @@ -0,0 +1,379 @@ +# SPDX-FileCopyrightText: 2022 Andrius Štikonas +# SPDX-FileCopyrightText: 2017 Jeremiah Orians +# +# 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_R15_to_RCX # save image_handle + 4C8B72 60 ; LOAD64_into_R14_from_Address_RDX_Immediate8 !96 # system->boot + 4D8BB6 18010000 ; LOAD64_into_R14_from_Address_R14_Immediate32 %280 # system->boot->open_protocol + + # Open Loaded Image protocol + 50 ; PUSH_RAX # allocate stack for image + 4989E0 ; COPY_RSP_to_R8 # arg3 = &image + 488B15 %LOADED_IMAGE_PROTOCOL_8 ; LOADI64_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 ; LOADI64_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 + 4989C9 ; COPY_RCX_to_R9 # arg4 = image_handle + # arg1 = ImageHandle (already set) + 4883EC 20 ; SUBI8_RSP !32 # allocate shadow stack space for UEFI function + 41FFD6 ; CALL_R14 # 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 ; LOADI64_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 ; LOADI64_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 + 41FFD6 ; CALL_R14 # 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 + + # Print our first Hex + 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 + 4805 &table ; ADDI32_to_RAX &table # 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 + 53 ; PUSH_RBX # allocate stack + 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 + +:PE32_end