# SPDX-FileCopyrightText: 2022 Andrius Štikonas # SPDX-FileCopyrightText: 2017 Jeremiah Orians # # SPDX-License-Identifier: GPL-3.0-or-later # Usage: hex0 file.hex0 file # Does not validate the arguments or check for success # Calling convention: # First four arguments are passed via registers rcx, rdx, r8, r9 (if they fit in 64-bits) # but we need to leave stack space # rax, rcx, rdx, r8, r9, r10 and r11 are volatile and can be changed by called function # Registers: # r12 in/fin: input file name, later reused for handle # r13 out/fout: output file name, later reused for handle # 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 DEFINE ADD_EAX_EDI 01F8 DEFINE ADDI8_RBX 4883C3 DEFINE ADDI8_RSP 4883C4 DEFINE CALLI32 E8 DEFINE CALL_R14 41FFD6 DEFINE CALL_RCX_Immediate8 FF51 DEFINE CMPI8_AL 3C DEFINE COPY_EAX_to_EDI 89C7 DEFINE COPY_RBX_to_R12 4989DC DEFINE COPY_RBX_to_R13 4989DD DEFINE COPY_RCX_to_RAX 4889C1 DEFINE COPY_RSP_to_RBP 4889E5 DEFINE COPY_RSP_to_RDX 4889E2 DEFINE COPY_RSP_to_R8 4989E0 DEFINE COPY_RBP_to_RSP 4889EC DEFINE COPY_R8_to_RSP 4989E0 DEFINE COPY_RCX_to_R9 4989C9 DEFINE COPY_R12_to_RCX 4C89E1 DEFINE COPY_R12_to_R8 4D89E0 DEFINE COPY_R13_to_RCX 4C89E9 DEFINE COPY_R13_to_R8 4D89E8 DEFINE COPY_R14_to_RCX 4C89F1 DEFINE COPY_RCX_to_R15 4989CF DEFINE COPY_R15_to_R9 4D89F9 DEFINE DEC_EBX FFCB DEFINE JE8 74 DEFINE JGE8 7D DEFINE JL8 7C DEFINE JMP8 EB DEFINE JNE8 75 DEFINE LOAD64_into_RBX_from_Address_RAX_Immediate8 488B58 DEFINE LOAD64_into_RAX_from_Address_RSP_Immediate8 488B4424 DEFINE LOAD64_into_RCX_from_Address_RSP_Immediate8 488B4C24 DEFINE LOAD64_into_RCX_from_Address_RCX_Immediate8 488B49 DEFINE LOAD64_into_R12_from_Address_RSP_Immediate8 4C8B6424 DEFINE LOAD64_into_R13_from_Address_RSP_Immediate8 4C8B6C24 DEFINE LOAD64_into_R14_from_Address_RDX_Immediate8 4C8B72 DEFINE LOAD64_into_R14_from_Address_R14_Immediate32 4D8BB6 DEFINE LOAD8_AL_from_Address_RBX 8A03 DEFINE LOAD64_rel_RDX 488B15 DEFINE POP_RAX 58 DEFINE POP_RBX 5B DEFINE POP_RSI 5E DEFINE POP_R9 4159 DEFINE POP_R14 415E DEFINE PUSH 6A DEFINE PUSH_RAX 50 DEFINE PUSH_RBX 53 DEFINE PUSH_RDX 52 DEFINE PUSH_RSI 56 DEFINE RET C3 DEFINE ROR_R9 49D1C9 DEFINE SHL_EDI_Immediate8 C1E7 DEFINE STOREI8_into_Address_RBX C603 DEFINE SUBI8_RSP 4883EC DEFINE SUBI8_AL 2C DEFINE XOR_EBX_EBX 31DB DEFINE XOR_EDI_EDI 31FF DEFINE TEST_EAX_EAX 85C0 DEFINE TEST_EBX_EBX 85DB DEFINE TEST_ESI_ESI 85F6 # efi_main(void *image_handle, struct efi_system_table *system) :_start COPY_RSP_to_RBP # save stack pointer COPY_RCX_to_R15 # save image_handle LOAD64_into_R14_from_Address_RDX_Immediate8 !96 # system->boot LOAD64_into_R14_from_Address_R14_Immediate32 %280 # system->boot->open_protocol # Open Loaded Image protocol PUSH_RAX # allocate stack for image COPY_RSP_to_R8 # arg3 = &image LOAD64_rel_RDX %LOADED_IMAGE_PROTOCOL_8 # EFI_LOADED_IMAGE_PROTOCOL_GUID (last 64 bits) PUSH_RDX # push last 64 bits onto stack LOAD64_rel_RDX %LOADED_IMAGE_PROTOCOL # EFI_LOADED_IMAGE_PROTOCOL_GUID (first 64 bits) PUSH_RDX # push first 64 bits onto stack COPY_RSP_to_RDX # arg2 = &guid PUSH !1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL PUSH !0 # arg5 = NULL COPY_RCX_to_R9 # arg4 = image_handle # arg1 = ImageHandle (already set) SUBI8_RSP !32 # allocate shadow stack space for UEFI function CALL_R14 # system->boot->open_protocol(image_handle, &guid, &image, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL) LOAD64_into_RAX_from_Address_RSP_Immediate8 !64 # get_image COPY_RCX_to_RAX # save image # Command line args LOAD64_into_RBX_from_Address_RAX_Immediate8 !56 # options = image->load_options :loop_options1 # Skip application name ADDI8_RBX !2 # ++options LOAD8_AL_from_Address_RBX # *options CMPI8_AL !0x20 # if *options != ' ' JNE8 !loop_options1 # then jump ADDI8_RBX !2 # ++options COPY_RBX_to_R12 # save input file :loop_options2 # Skip argv[1] ADDI8_RBX !2 # ++options LOAD8_AL_from_Address_RBX # *options CMPI8_AL !0x20 # if *options != ' ' JNE8 !loop_options2 # then jump STOREI8_into_Address_RBX !0 # *options = 0; ADDI8_RBX !2 # ++options COPY_RBX_to_R13 # save output file # Get root file system PUSH_RAX # allocate stack for rootfs COPY_RSP_to_R8 # arg3 = &rootfs LOAD64_rel_RDX %SIMPLE_FS_PROTOCOL_8 # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID (last 64 bits) PUSH_RDX # push last 64 bits onto stack LOAD64_rel_RDX %SIMPLE_FS_PROTOCOL # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID (first 64 bits) PUSH_RDX # push first 64 bits onto stack COPY_RSP_to_RDX # arg2 = &guid PUSH !1 # arg6 = EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL PUSH !0 # arg5 = NULL COPY_R15_to_R9 # arg4 = image_handle LOAD64_into_RCX_from_Address_RCX_Immediate8 !24 # arg1 = root_device = image->device SUBI8_RSP !32 # allocate shadow stack space for UEFI function CALL_R14 # system->boot->open_protocol(root_device, &guid, &rootfs, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL) LOAD64_into_RCX_from_Address_RSP_Immediate8 !64 # get rootfs # Get root directory PUSH_RDX # allocate stack for rootdir COPY_RSP_to_RDX # arg2 = &rootdir PUSH_RAX # allocate shadow stack space for UEFI function PUSH_RAX # allocate shadow stack space for UEFI function CALL_RCX_Immediate8 !8 # rootfs->open_volume(rootfs, &rootdir) POP_RAX # deallocate stack POP_RAX # deallocate stack POP_R14 # save &rootdir # Open file for writing PUSH_RDX # allocate stack for fout COPY_RSP_to_RDX # 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 COPY_R13_to_R8 # arg3 = out COPY_R14_to_RCX # arg1 = rootdir SUBI8_RSP !32 # allocate shadow stack space for UEFI function CALL_RCX_Immediate8 !8 # rootdir->open() LOAD64_into_R13_from_Address_RSP_Immediate8 !40 # get fout # Open file for reading PUSH_RDX # allocate stack for fin COPY_RSP_to_RDX # 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 COPY_R12_to_R8 # arg3 = in COPY_R14_to_RCX # arg1 = rootdir SUBI8_RSP !32 # allocate shadow stack space for UEFI function CALL_RCX_Immediate8 !8 # rootdir->open() LOAD64_into_R12_from_Address_RSP_Immediate8 !40 # get fin # Our flag for byte processing PUSH !-1 POP_RBX # rbx = -1 # temp storage for the sum XOR_EDI_EDI # rdi = 0 :loop # Read a byte CALLI32 %read_byte # process byte CALLI32 %hex # deal with -1 values TEST_EAX_EAX JL8 !loop # deal with toggle TEST_EBX_EBX # jump if ebx >= 0 JGE8 !print # process first byte of pair COPY_EAX_to_EDI XOR_EBX_EBX # rbx = 0 JMP8 !loop # process second byte of pair :print # update the sum and store in output SHL_EDI_Immediate8 !4 ADD_EAX_EDI # flip the toggle DEC_EBX # rbx = -1 CALLI32 %write_byte JMP8 !loop :hex # Purge Comment Lines (#) CMPI8_AL !35 JE8 !purge_comment # Purge Comment Lines (;) CMPI8_AL !59 JE8 !purge_comment # deal all ascii less than '0' CMPI8_AL !48 JL8 !ascii_other # deal with 0-9 CMPI8_AL !58 JL8 !ascii_num # deal with all ascii less than 'A' CMPI8_AL !65 JL8 !ascii_other # deal with 'A'-'F' CMPI8_AL !71 JL8 !ascii_high # deal with all ascii less than 'a' CMPI8_AL !97 JL8 !ascii_other #deal with 'a'-'f' CMPI8_AL !103 JL8 !ascii_low # The rest that remains needs to be ignored JMP8 !ascii_other :purge_comment # Read a byte CALLI32 %read_byte # Loop if not LF CMPI8_AL !10 JNE8 !purge_comment # Otherwise return -1 :ascii_other PUSH !-1 POP_RAX # return = -1 RET :ascii_num SUBI8_AL !48 RET :ascii_low SUBI8_AL !32 # convert to uppercase :ascii_high SUBI8_AL !55 RET # Writes byte stored in al :write_byte COPY_R13_to_RCX # arg1 = fout PUSH !1 # size = 1 COPY_RSP_to_RDX # arg2 = &size PUSH_RAX # allocate stack COPY_RSP_to_R8 # 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_Immediate8 !40 # fout->write() ADDI8_RSP !40 # deallocate stack RET # return :read_byte COPY_R12_to_RCX # arg1 = fin PUSH !1 # size = 1 COPY_RSP_to_RDX # arg2 = &size PUSH_RSI # allocate stack COPY_RSP_to_R8 # arg3 = &input 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_Immediate8 !32 # fin->read() POP_RAX # deallocate stack POP_RAX # deallocate stack POP_RAX # deallocate stack POP_RAX # save input to rax POP_RSI # save size to rsi # If the file ended (0 bytes read) terminate TEST_ESI_ESI # if size = 0 JE8 !terminate # then we are done RET # return :terminate PUSH_RBX # allocate stack COPY_R12_to_RCX # arg1 = fin CALL_RCX_Immediate8 !16 # fin->close() COPY_R13_to_RCX # arg1 = fout CALL_RCX_Immediate8 !16 # fout->close() COPY_RBP_to_RSP # restore stack RET # return to UEFI # Protocol GUIDs :LOADED_IMAGE_PROTOCOL %0x5b1b31a1 @0x9562 @0x11d2 :LOADED_IMAGE_PROTOCOL_8 !0x8e !0x3f !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b :SIMPLE_FS_PROTOCOL %0x0964e5b22 @0x6459 @0x11d2 :SIMPLE_FS_PROTOCOL_8 !0x8e !0x39 !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b :PE32_end