6 ;; EFI struct definitions {{{
8 EFI_NOT_READY = 0x8000_0000_0000_0000 or 6
10 ;; Based on https://wiki.osdev.org/Uefi.inc
17 struc EFI_TABLE_HEADER {
25 struc EFI_SYSTEM_TABLE {
27 .FirmwareVendor dq ? ; CHAR16*
28 .FirmwareRevision dd ? ; UINT32
30 .ConsoleInHandle dq ? ; EFI_HANDLE
31 .ConIn dq ? ; EFI_SIMPLE_TEXT_INPUT_PROTOCOL*
32 .ConsoleOutHandle dq ? ; EFI_HANDLE
33 .ConOut dq ? ; EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL*
36 struct EFI_SYSTEM_TABLE
38 struc EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL {
39 .Reset dq ? ; EFI_TEXT_RESET
40 .OutputString dq ? ; EFI_TEXT_STRING
43 struct EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
45 struc EFI_SIMPLE_TEXT_INPUT_PROTOCOL {
46 .Reset dq ? ; EFI_INPUT_RESET
47 .ReadKeyStroke dq ? ; EFI_INPUT_READ_KEY
50 struct EFI_SIMPLE_TEXT_INPUT_PROTOCOL
53 .ScanCode dw ? ; UINT16
54 .UnicodeChar dw ? ; CHAR16
61 section '.text' code executable readable
64 ; At program startup, RDX contains an EFI_SYSTEM_TABLE*.
65 mov [system_table], rdx
68 ;; Print a string of the given length.
71 ;; - RCX = String buffer
72 ;; - RDX = String length
74 ;; [TODO] Handle newlines correctly. (I.e. translate '\n' to '\r\n'.)
82 ; We take an input string of bytes without any terminator. We need to turn
83 ; this string into a string of words, terminated by a null character.
90 mov al, byte [r8 + rsi]
91 lea rdx, [.output_buffer + rcx]
96 lea rdx, [.output_buffer + rcx]
102 lea rdx, [.output_buffer + r10]
105 ; At this point we have our null-terminated word-string at .output_buffer. Now
106 ; we just need to print it.
108 mov rcx, [system_table] ; EFI_SYSTEM_TABLE* rcx
109 mov rcx, [rcx + EFI_SYSTEM_TABLE.ConOut] ; EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL* rcx
110 mov rdx, .output_buffer
111 mov rbx, [rcx + EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString] ; EFI_TEXT_STRING rbx
117 ;; Read a character as an ASCII byte into the given buffer.
120 ;; - RCX = Character buffer (1 byte)
122 ;; [TODO] Handle enter key correctly (should return '\n').
126 mov rcx, [system_table] ; EFI_SYSTEM_TABLE* rcx
127 mov rcx, [rcx + EFI_SYSTEM_TABLE.ConIn] ; EFI_SIMPLE_TEXT_INPUT_PROTOCOL* rcx
128 mov rbx, [rcx + EFI_SIMPLE_TEXT_INPUT_PROTOCOL.ReadKeyStroke] ; EFI_INPUT_READ_KEY rbx
129 mov rdx, input_key ; EFI_INPUT_KEY* rdx
134 mov r8, EFI_NOT_READY
138 mov ax, [input_key.UnicodeChar]
141 ;; Print the character
144 call uefi_print_string
148 ;; Terminate with the given error code.
151 ;; - RCX = Error code
153 mov rcx, terminated_msg
154 mov rdx, terminated_msg.len
155 call uefi_print_string
158 section '.data' readable writable
160 system_table dq ? ; EFI_SYSTEM_TABLE*
162 terminated_msg db 0xD, 0xA, '(The program has terminated.)', 0xD, 0xA
163 .len = $ - terminated_msg
165 uefi_print_string.output_buffer rq 0x400
169 input_key EFI_INPUT_KEY