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
69 ;; We take an input string of bytes without any terminator. We need to turn
70 ;; this string into a string of words, terminated by a null character.
72 mov rdi, .output_buffer ; Current location in output string
75 ;; When there are no characters left in the input string, we are done.
79 ;; Load byte from input string
82 ;; Copy byte to output string
87 ;; It's a newline; replace it with '\r\n' in output string.
99 ;; Not a newline, proceed as normal:
103 ;; The output string has words rather than bytes for charactesr, so we need
104 ;; to add an extra zero:
109 ;; We finished copying character to output string, so pop it from the input
116 ;; Append a final null-word:
119 ; At this point we have our null-terminated word-string at .output_buffer. Now
120 ; we just need to print it.
122 mov rcx, [system_table] ; EFI_SYSTEM_TABLE* rcx
123 mov rcx, [rcx + EFI_SYSTEM_TABLE.ConOut] ; EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL* rcx
124 mov rdx, .output_buffer
125 mov rbx, [rcx + EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString] ; EFI_TEXT_STRING rbx
133 mov rcx, [system_table] ; EFI_SYSTEM_TABLE* rcx
134 mov rcx, [rcx + EFI_SYSTEM_TABLE.ConIn] ; EFI_SIMPLE_TEXT_INPUT_PROTOCOL* rcx
135 mov rbx, [rcx + EFI_SIMPLE_TEXT_INPUT_PROTOCOL.ReadKeyStroke] ; EFI_INPUT_READ_KEY rbx
136 mov rdx, input_key ; EFI_INPUT_KEY* rdx
141 mov r8, EFI_NOT_READY
145 movzx rax, word [input_key.UnicodeChar]
147 ;; Special handling of enter (UEFI gives us '\r', but we want '\n'.)
154 ;; Print the character
155 mov [char_buffer], al
163 ;; Terminate with the given error code.
166 ;; - RCX = Error code
168 mov rcx, terminated_msg
169 mov rdx, terminated_msg.len
173 section '.data' readable writable
175 system_table dq ? ; EFI_SYSTEM_TABLE*
177 terminated_msg db 0xD, 0xA, '(The program has terminated.)', 0xD, 0xA
178 .len = $ - terminated_msg
180 os_print_string.output_buffer rq 0x400
184 input_key EFI_INPUT_KEY