xref: /haiku/src/system/boot/platform/efi/console.cpp (revision 6e82e428596071bbde44e296d04740f1f5d54d03)
1 /*
2  * Copyright 2014-2016 Haiku, Inc. All rights reserved.
3  * Copyright 2013 Fredrik Holmqvist, fredrik.holmqvist@gmail.com. All rights
4  * reserved.
5  * Distributed under the terms of the MIT License.
6  */
7 
8 
9 #include "console.h"
10 
11 #include <string.h>
12 
13 #include <SupportDefs.h>
14 
15 #include <boot/stage2.h>
16 #include <boot/platform.h>
17 #include <util/kernel_cpp.h>
18 
19 #include "efi_platform.h"
20 
21 
22 class Console : public ConsoleNode {
23 	public:
24 		Console();
25 
26 		virtual ssize_t ReadAt(void *cookie, off_t pos, void *buffer,
27 			size_t bufferSize);
28 		virtual ssize_t WriteAt(void *cookie, off_t pos, const void *buffer,
29 			size_t bufferSize);
30 };
31 
32 
33 static uint32 sScreenWidth;
34 static uint32 sScreenHeight;
35 static uint32 sScreenMode;
36 static Console sInput, sOutput;
37 FILE *stdin, *stdout, *stderr;
38 
39 
40 //	#pragma mark -
41 
42 
43 Console::Console()
44 	: ConsoleNode()
45 {
46 }
47 
48 
49 ssize_t
50 Console::ReadAt(void *cookie, off_t pos, void *buffer, size_t bufferSize)
51 {
52 	return B_ERROR;
53 }
54 
55 
56 ssize_t
57 Console::WriteAt(void *cookie, off_t /*pos*/, const void *buffer,
58 	size_t bufferSize)
59 {
60 	const char *string = (const char *)buffer;
61 	CHAR16 ucsBuffer[bufferSize + 3];
62 	uint32 j = 0;
63 
64 	for (uint32 i = 0; i < bufferSize; i++) {
65 		switch (string[i]) {
66 			case '\n': {
67 				ucsBuffer[j++] = '\r';
68 				ucsBuffer[j++] = '\n';
69 			} //fallthrough
70 			case 0 : {
71 				//Not sure if we should keep going or abort for 0.
72 				//Keep going was easy anyway.
73 				ucsBuffer[j] = 0;
74 				kSystemTable->ConOut->OutputString(kSystemTable->ConOut,
75 					ucsBuffer);
76 				j = 0;
77 				continue;
78 			}
79 			default:
80 				ucsBuffer[j++] = (CHAR16) string[i];
81 		}
82 	}
83 
84 	if (j > 0) {
85 		ucsBuffer[j] = 0;
86 		kSystemTable->ConOut->OutputString(kSystemTable->ConOut, ucsBuffer);
87 	}
88 	return bufferSize;
89 }
90 
91 
92 //	#pragma mark -
93 
94 
95 void
96 console_clear_screen(void)
97 {
98 	kSystemTable->ConOut->ClearScreen(kSystemTable->ConOut);
99 }
100 
101 
102 int32
103 console_width(void)
104 {
105 	return sScreenWidth;
106 }
107 
108 
109 int32
110 console_height(void)
111 {
112 	return sScreenHeight;
113 }
114 
115 
116 void
117 console_set_cursor(int32 x, int32 y)
118 {
119 	kSystemTable->ConOut->SetCursorPosition(kSystemTable->ConOut, x, y);
120 }
121 
122 
123 void
124 console_show_cursor(void)
125 {
126 	kSystemTable->ConOut->EnableCursor(kSystemTable->ConOut, true);
127 }
128 
129 
130 void
131 console_hide_cursor(void)
132 {
133 	kSystemTable->ConOut->EnableCursor(kSystemTable->ConOut, false);
134 }
135 
136 
137 void
138 console_set_color(int32 foreground, int32 background)
139 {
140 	kSystemTable->ConOut->SetAttribute(kSystemTable->ConOut,
141 		EFI_TEXT_ATTR((foreground & 0xf), (background & 0xf)));
142 }
143 
144 
145 int
146 console_wait_for_key(void)
147 {
148 	UINTN index;
149 	EFI_STATUS status;
150 	EFI_INPUT_KEY key;
151 	EFI_EVENT event = kSystemTable->ConIn->WaitForKey;
152 
153 	do {
154 		kBootServices->WaitForEvent(1, &event, &index);
155 		status = kSystemTable->ConIn->ReadKeyStroke(kSystemTable->ConIn, &key);
156 	} while (status == EFI_NOT_READY);
157 
158 	if (key.UnicodeChar > 0)
159 		return (int) key.UnicodeChar;
160 
161 	switch (key.ScanCode) {
162 		case SCAN_UP:
163 			return TEXT_CONSOLE_KEY_UP;
164 		case SCAN_DOWN:
165 			return TEXT_CONSOLE_KEY_DOWN;
166 		case SCAN_LEFT:
167 			return TEXT_CONSOLE_KEY_LEFT;
168 		case SCAN_RIGHT:
169 			return TEXT_CONSOLE_KEY_RIGHT;
170 		case SCAN_PAGE_UP:
171 			return TEXT_CONSOLE_KEY_PAGE_UP;
172 		case SCAN_PAGE_DOWN:
173 			return TEXT_CONSOLE_KEY_PAGE_DOWN;
174 		case SCAN_HOME:
175 			return TEXT_CONSOLE_KEY_HOME;
176 		case SCAN_END:
177 			return TEXT_CONSOLE_KEY_END;
178 	}
179 	return 0;
180 }
181 
182 
183 static void update_screen_size(void)
184 {
185 	UINTN width, height;
186 	UINTN area = 0;
187 	SIMPLE_TEXT_OUTPUT_INTERFACE *ConOut = kSystemTable->ConOut;
188 
189 	for (int mode = 0; mode < ConOut->Mode->MaxMode; ++mode) {
190 		if (ConOut->QueryMode(ConOut, mode, &width, &height) == EFI_SUCCESS) {
191 			if (width * height > area) {
192 				sScreenWidth = width;
193 				sScreenHeight = height;
194 				sScreenMode = mode;
195 			}
196 		}
197 	}
198 
199 	ConOut->SetMode(ConOut, sScreenMode);
200 }
201 
202 
203 status_t
204 console_init(void)
205 {
206 	update_screen_size();
207 	console_hide_cursor();
208 	console_clear_screen();
209 
210 	// enable stdio functionality
211 	stdin = (FILE *)&sInput;
212 	stdout = stderr = (FILE *)&sOutput;
213 
214 	return B_OK;
215 }
216 
217 
218 uint32
219 console_check_boot_keys(void)
220 {
221 	EFI_STATUS status;
222 	EFI_INPUT_KEY key;
223 
224 	// give the user a chance to press a key
225 	kBootServices->Stall(500000);
226 
227 	status = kSystemTable->ConIn->ReadKeyStroke(kSystemTable->ConIn, &key);
228 
229 	if (status != EFI_SUCCESS)
230 		return 0;
231 
232 	if (key.UnicodeChar == 0 && key.ScanCode == SCAN_ESC)
233 		return BOOT_OPTION_DEBUG_OUTPUT;
234 	if (key.UnicodeChar == ' ')
235 		return BOOT_OPTION_MENU;
236 
237 	return 0;
238 }
239 
240 
241 extern "C" void
242 platform_switch_to_text_mode(void)
243 {
244 	kSystemTable->ConOut->Reset(kSystemTable->ConOut, false);
245 	kSystemTable->ConOut->SetMode(kSystemTable->ConOut, sScreenMode);
246 }
247