xref: /illumos-gate/usr/src/boot/efi/libefi/efi_console.c (revision 4ac2186d79f65de18b11f2693e78f73b27d5308b)
1 /*
2  * Copyright (c) 2000 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 
29 #include <efi.h>
30 #include <efilib.h>
31 #include <eficonsctl.h>
32 #include <Guid/ConsoleInDevice.h>
33 #include <Guid/ConsoleOutDevice.h>
34 #include <Guid/StandardErrorDevice.h>
35 #include <Protocol/GraphicsOutput.h>
36 #include <Protocol/UgaDraw.h>
37 #include <Protocol/SimpleTextIn.h>
38 #include <Protocol/SimpleTextInEx.h>
39 #include <Protocol/SimpleTextOut.h>
40 #include <sys/tem_impl.h>
41 #include <sys/multiboot2.h>
42 #include <machine/metadata.h>
43 #include <gfx_fb.h>
44 
45 #include "bootstrap.h"
46 
47 struct efi_fb			efifb;
48 EFI_GRAPHICS_OUTPUT_PROTOCOL	*gop;
49 EFI_UGA_DRAW_PROTOCOL		*uga;
50 
51 EFI_GUID gEfiConsoleInDeviceGuid = EFI_CONSOLE_IN_DEVICE_GUID;
52 EFI_GUID gEfiConsoleOutDeviceGuid = EFI_CONSOLE_OUT_DEVICE_GUID;
53 EFI_GUID gEfiStandardErrorDeviceGuid = EFI_STANDARD_ERROR_DEVICE_GUID;
54 EFI_GUID gEfiConsoleControlProtocolGuid = EFI_CONSOLE_CONTROL_PROTOCOL_GUID;
55 EFI_GUID gEfiSimpleTextInProtocolGuid = EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID;
56 EFI_GUID gEfiSimpleTextInputExProtocolGuid =
57     EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID;
58 EFI_GUID gEfiSimpleTextOutProtocolGuid = EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID;
59 
60 extern EFI_GRAPHICS_OUTPUT_BLT_PIXEL *shadow_fb;
61 static size_t shadow_sz;	/* units of pages */
62 static EFI_CONSOLE_CONTROL_PROTOCOL	*console_control;
63 static EFI_CONSOLE_CONTROL_SCREEN_MODE	console_mode;
64 static SIMPLE_TEXT_OUTPUT_INTERFACE	*conout;
65 
66 /* mode change callback and argument from tem */
67 static vis_modechg_cb_t modechg_cb;
68 static struct vis_modechg_arg *modechg_arg;
69 static tem_vt_state_t tem;
70 
71 struct efi_console_data {
72 	struct visual_ops			*ecd_visual_ops;
73 	SIMPLE_INPUT_INTERFACE			*ecd_conin;
74 	EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL	*ecd_coninex;
75 };
76 
77 #define	KEYBUFSZ 10
78 static unsigned keybuf[KEYBUFSZ];	/* keybuf for extended codes */
79 
80 static int key_pending;
81 
82 static const unsigned char solaris_color_to_efi_color[16] = {
83 	EFI_WHITE,
84 	EFI_BLACK,
85 	EFI_BLUE,
86 	EFI_GREEN,
87 	EFI_CYAN,
88 	EFI_RED,
89 	EFI_MAGENTA,
90 	EFI_BROWN,
91 	EFI_LIGHTGRAY,
92 	EFI_DARKGRAY,
93 	EFI_LIGHTBLUE,
94 	EFI_LIGHTGREEN,
95 	EFI_LIGHTCYAN,
96 	EFI_LIGHTRED,
97 	EFI_LIGHTMAGENTA,
98 	EFI_YELLOW
99 };
100 
101 #define	DEFAULT_FGCOLOR	EFI_LIGHTGRAY
102 #define	DEFAULT_BGCOLOR	EFI_BLACK
103 
104 extern int efi_find_framebuffer(struct efi_fb *efifb);
105 
106 static void efi_framebuffer_setup(void);
107 static void efi_cons_probe(struct console *);
108 static int efi_cons_init(struct console *, int);
109 static void efi_cons_putchar(struct console *, int);
110 static void efi_cons_efiputchar(int);
111 static int efi_cons_getchar(struct console *);
112 static int efi_cons_poll(struct console *);
113 static int efi_cons_ioctl(struct console *cp, int cmd, void *data);
114 static void efi_cons_devinfo(struct console *);
115 
116 static int efi_fb_devinit(struct vis_devinit *);
117 static void efi_cons_cursor(struct vis_conscursor *);
118 
119 static int efi_text_devinit(struct vis_devinit *);
120 static int efi_text_cons_clear(struct vis_consclear *);
121 static void efi_text_cons_copy(struct vis_conscopy *);
122 static void efi_text_cons_display(struct vis_consdisplay *);
123 
124 struct console efi_console = {
125 	.c_name = "text",
126 	.c_desc = "EFI console",
127 	.c_flags = C_WIDEOUT,
128 	.c_probe = efi_cons_probe,
129 	.c_init = efi_cons_init,
130 	.c_out = efi_cons_putchar,
131 	.c_in = efi_cons_getchar,
132 	.c_ready = efi_cons_poll,
133 	.c_ioctl = efi_cons_ioctl,
134 	.c_devinfo = efi_cons_devinfo,
135 	.c_private = NULL
136 };
137 
138 static struct vis_identifier fb_ident = { "efi_fb" };
139 static struct vis_identifier text_ident = { "efi_text" };
140 
141 struct visual_ops fb_ops = {
142 	.ident = &fb_ident,
143 	.kdsetmode = NULL,
144 	.devinit = efi_fb_devinit,
145 	.cons_copy = gfx_fb_cons_copy,
146 	.cons_display = gfx_fb_cons_display,
147 	.cons_cursor = efi_cons_cursor,
148 	.cons_clear = gfx_fb_cons_clear,
149 	.cons_put_cmap = NULL
150 };
151 
152 struct visual_ops text_ops = {
153 	.ident = &text_ident,
154 	.kdsetmode = NULL,
155 	.devinit = efi_text_devinit,
156 	.cons_copy = efi_text_cons_copy,
157 	.cons_display = efi_text_cons_display,
158 	.cons_cursor = efi_cons_cursor,
159 	.cons_clear = efi_text_cons_clear,
160 	.cons_put_cmap = NULL
161 };
162 
163 /*
164  * platform specific functions for tem
165  */
166 int
plat_stdout_is_framebuffer(void)167 plat_stdout_is_framebuffer(void)
168 {
169 	return (console_mode == EfiConsoleControlScreenGraphics);
170 }
171 
172 void
plat_tem_hide_prom_cursor(void)173 plat_tem_hide_prom_cursor(void)
174 {
175 	if (has_boot_services)
176 		conout->EnableCursor(conout, FALSE);
177 }
178 
179 static void
plat_tem_display_prom_cursor(screen_pos_t row,screen_pos_t col)180 plat_tem_display_prom_cursor(screen_pos_t row, screen_pos_t col)
181 {
182 
183 	if (has_boot_services) {
184 		conout->SetCursorPosition(conout, col, row);
185 		conout->EnableCursor(conout, TRUE);
186 	}
187 }
188 
189 void
plat_tem_get_prom_pos(uint32_t * row,uint32_t * col)190 plat_tem_get_prom_pos(uint32_t *row, uint32_t *col)
191 {
192 	if (console_mode == EfiConsoleControlScreenText) {
193 		*col = (uint32_t)conout->Mode->CursorColumn;
194 		*row = (uint32_t)conout->Mode->CursorRow;
195 	} else {
196 		*col = 0;
197 		*row = 0;
198 	}
199 }
200 
201 /*
202  * plat_tem_get_prom_size() is supposed to return screen size
203  * in chars. Return real data for text mode and TEM defaults for graphical
204  * mode, so the tem can compute values based on default and font.
205  */
206 void
plat_tem_get_prom_size(size_t * height,size_t * width)207 plat_tem_get_prom_size(size_t *height, size_t *width)
208 {
209 	UINTN cols, rows;
210 	if (console_mode == EfiConsoleControlScreenText) {
211 		(void) conout->QueryMode(conout, conout->Mode->Mode,
212 		    &cols, &rows);
213 		*height = (size_t)rows;
214 		*width = (size_t)cols;
215 	} else {
216 		*height = TEM_DEFAULT_ROWS;
217 		*width = TEM_DEFAULT_COLS;
218 	}
219 }
220 
221 /*
222  * Callback to notify about console mode change.
223  * mode is value from enum EFI_CONSOLE_CONTROL_SCREEN_MODE.
224  */
225 void
plat_cons_update_mode(int mode)226 plat_cons_update_mode(int mode)
227 {
228 	UINTN cols, rows;
229 	struct vis_devinit devinit;
230 	struct efi_console_data *ecd = efi_console.c_private;
231 
232 	/* Make sure we have usable console. */
233 	if (efi_find_framebuffer(&efifb)) {
234 		console_mode = EfiConsoleControlScreenText;
235 	} else {
236 		efi_framebuffer_setup();
237 		if (mode != -1 && console_mode != mode)
238 			console_mode = mode;
239 	}
240 
241 	if (console_control != NULL)
242 		(void) console_control->SetMode(console_control, console_mode);
243 
244 	/* some firmware enables the cursor when switching modes */
245 	conout->EnableCursor(conout, FALSE);
246 	if (console_mode == EfiConsoleControlScreenText) {
247 		(void) conout->QueryMode(conout, conout->Mode->Mode,
248 		    &cols, &rows);
249 		devinit.version = VIS_CONS_REV;
250 		devinit.width = cols;
251 		devinit.height = rows;
252 		devinit.depth = 4;
253 		devinit.linebytes = cols;
254 		devinit.color_map = NULL;
255 		devinit.mode = VIS_TEXT;
256 		ecd->ecd_visual_ops = &text_ops;
257 	} else {
258 		devinit.version = VIS_CONS_REV;
259 		devinit.width = gfx_fb.framebuffer_common.framebuffer_width;
260 		devinit.height = gfx_fb.framebuffer_common.framebuffer_height;
261 		devinit.depth = gfx_fb.framebuffer_common.framebuffer_bpp;
262 		devinit.linebytes = gfx_fb.framebuffer_common.framebuffer_pitch;
263 		devinit.color_map = gfx_fb_color_map;
264 		devinit.mode = VIS_PIXEL;
265 		ecd->ecd_visual_ops = &fb_ops;
266 	}
267 
268 	modechg_cb(modechg_arg, &devinit);
269 }
270 
271 static int
efi_fb_devinit(struct vis_devinit * data)272 efi_fb_devinit(struct vis_devinit *data)
273 {
274 	if (console_mode != EfiConsoleControlScreenGraphics)
275 		return (1);
276 
277 	data->version = VIS_CONS_REV;
278 	data->width = gfx_fb.framebuffer_common.framebuffer_width;
279 	data->height = gfx_fb.framebuffer_common.framebuffer_height;
280 	data->depth = gfx_fb.framebuffer_common.framebuffer_bpp;
281 	data->linebytes = gfx_fb.framebuffer_common.framebuffer_pitch;
282 	data->color_map = gfx_fb_color_map;
283 	data->mode = VIS_PIXEL;
284 
285 	modechg_cb = data->modechg_cb;
286 	modechg_arg = data->modechg_arg;
287 
288 	return (0);
289 }
290 
291 static int
efi_text_devinit(struct vis_devinit * data)292 efi_text_devinit(struct vis_devinit *data)
293 {
294 	UINTN cols, rows;
295 
296 	if (console_mode != EfiConsoleControlScreenText)
297 		return (1);
298 
299 	(void) conout->QueryMode(conout, conout->Mode->Mode, &cols, &rows);
300 	data->version = VIS_CONS_REV;
301 	data->width = cols;
302 	data->height = rows;
303 	data->depth = 4;
304 	data->linebytes = cols;
305 	data->color_map = NULL;
306 	data->mode = VIS_TEXT;
307 
308 	modechg_cb = data->modechg_cb;
309 	modechg_arg = data->modechg_arg;
310 
311 	return (0);
312 }
313 
314 static int
efi_text_cons_clear(struct vis_consclear * ca)315 efi_text_cons_clear(struct vis_consclear *ca)
316 {
317 	EFI_STATUS st;
318 	UINTN attr = conout->Mode->Attribute & 0x0F;
319 	uint8_t bg;
320 
321 	if (!has_boot_services)
322 		return (0);
323 
324 	bg = solaris_color_to_efi_color[ca->bg_color.four & 0xF] & 0x7;
325 
326 	attr = EFI_TEXT_ATTR(attr, bg);
327 	st = conout->SetAttribute(conout, attr);
328 	if (EFI_ERROR(st))
329 		return (1);
330 	st = conout->ClearScreen(conout);
331 	if (EFI_ERROR(st))
332 		return (1);
333 	return (0);
334 }
335 
336 static void
efi_text_cons_copy(struct vis_conscopy * ma)337 efi_text_cons_copy(struct vis_conscopy *ma)
338 {
339 	UINTN col, row;
340 
341 	if (!has_boot_services)
342 		return;
343 
344 	col = 0;
345 	row = ma->e_row;
346 	conout->SetCursorPosition(conout, col, row);
347 
348 	efi_cons_efiputchar('\n');
349 }
350 
351 static void
efi_text_cons_display(struct vis_consdisplay * da)352 efi_text_cons_display(struct vis_consdisplay *da)
353 {
354 	EFI_STATUS st;
355 	UINTN attr;
356 	UINTN row, col;
357 	tem_char_t *data;
358 	uint8_t fg, bg;
359 	int i;
360 
361 	if (!has_boot_services)
362 		return;
363 
364 	(void) conout->QueryMode(conout, conout->Mode->Mode, &col, &row);
365 
366 	/* reduce clear line on bottom row by one to prevent autoscroll */
367 	if (row - 1 == da->row && da->col == 0 && da->width == col)
368 		da->width--;
369 
370 	data = (tem_char_t *)da->data;
371 	fg = solaris_color_to_efi_color[da->fg_color.four & 0xf];
372 	bg = solaris_color_to_efi_color[da->bg_color.four & 0xf] & 0x7;
373 	attr = EFI_TEXT_ATTR(fg, bg);
374 
375 	st = conout->SetAttribute(conout, attr);
376 	if (EFI_ERROR(st))
377 		return;
378 	row = da->row;
379 	col = da->col;
380 	conout->SetCursorPosition(conout, col, row);
381 	for (i = 0; i < da->width; i++)
382 		efi_cons_efiputchar(data[i]);
383 }
384 
efi_cons_cursor(struct vis_conscursor * cc)385 static void efi_cons_cursor(struct vis_conscursor *cc)
386 {
387 	switch (cc->action) {
388 	case VIS_HIDE_CURSOR:
389 		if (plat_stdout_is_framebuffer())
390 			gfx_fb_display_cursor(cc);
391 		else
392 			plat_tem_hide_prom_cursor();
393 		break;
394 	case VIS_DISPLAY_CURSOR:
395 		if (plat_stdout_is_framebuffer())
396 			gfx_fb_display_cursor(cc);
397 		else
398 			plat_tem_display_prom_cursor(cc->row, cc->col);
399 		break;
400 	case VIS_GET_CURSOR: {	/* only used at startup */
401 		uint32_t row, col;
402 
403 		row = col = 0;
404 		plat_tem_get_prom_pos(&row, &col);
405 		cc->row = row;
406 		cc->col = col;
407 		}
408 		break;
409 	}
410 }
411 
412 static int
efi_cons_ioctl(struct console * cp,int cmd,void * data)413 efi_cons_ioctl(struct console *cp, int cmd, void *data)
414 {
415 	struct efi_console_data *ecd = cp->c_private;
416 	struct visual_ops *ops = ecd->ecd_visual_ops;
417 
418 	switch (cmd) {
419 	case VIS_GETIDENTIFIER:
420 		memmove(data, ops->ident, sizeof (struct vis_identifier));
421 		break;
422 	case VIS_DEVINIT:
423 		return (ops->devinit(data));
424 	case VIS_CONSCLEAR:
425 		return (ops->cons_clear(data));
426 	case VIS_CONSCOPY:
427 		ops->cons_copy(data);
428 		break;
429 	case VIS_CONSDISPLAY:
430 		ops->cons_display(data);
431 		break;
432 	case VIS_CONSCURSOR:
433 		ops->cons_cursor(data);
434 		break;
435 	default:
436 		return (EINVAL);
437 	}
438 	return (0);
439 }
440 
441 static void
efi_framebuffer_setup(void)442 efi_framebuffer_setup(void)
443 {
444 	int bpp, pos;
445 	EFI_STATUS status;
446 
447 	bpp = fls(efifb.fb_mask_red | efifb.fb_mask_green |
448 	    efifb.fb_mask_blue | efifb.fb_mask_reserved);
449 
450 	/*
451 	 * To save heap space, allocate shadow fb with AllocatePages().
452 	 * FB memory can be rather large and its size depends on resolution.
453 	 */
454 	if (shadow_fb != NULL) {
455 		BS->FreePages((EFI_PHYSICAL_ADDRESS)(uintptr_t)shadow_fb,
456 		    shadow_sz);
457 	}
458 	shadow_sz = EFI_SIZE_TO_PAGES(efifb.fb_width * efifb.fb_height *
459 	    sizeof (*shadow_fb));
460 	status = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData,
461 	    shadow_sz, (EFI_PHYSICAL_ADDRESS *)&shadow_fb);
462 	if (status != EFI_SUCCESS)
463 		shadow_fb = NULL;
464 
465 	gfx_fb.framebuffer_common.mb_type = MULTIBOOT_TAG_TYPE_FRAMEBUFFER;
466 	gfx_fb.framebuffer_common.mb_size = sizeof (gfx_fb);
467 	gfx_fb.framebuffer_common.framebuffer_addr = efifb.fb_addr;
468 	gfx_fb.framebuffer_common.framebuffer_width = efifb.fb_width;
469 	gfx_fb.framebuffer_common.framebuffer_height = efifb.fb_height;
470 	gfx_fb.framebuffer_common.framebuffer_bpp = bpp;
471 	gfx_fb.framebuffer_common.framebuffer_pitch =
472 	    efifb.fb_stride * (bpp >> 3);
473 	gfx_fb.framebuffer_common.framebuffer_type =
474 	    MULTIBOOT_FRAMEBUFFER_TYPE_RGB;
475 	gfx_fb.framebuffer_common.mb_reserved = 0;
476 
477 	pos = ffs(efifb.fb_mask_red);
478 	if (pos != 0)
479 		pos--;
480 	gfx_fb.u.fb2.framebuffer_red_mask_size = fls(efifb.fb_mask_red >> pos);
481 	gfx_fb.u.fb2.framebuffer_red_field_position = pos;
482 	pos = ffs(efifb.fb_mask_green);
483 	if (pos != 0)
484 		pos--;
485 	gfx_fb.u.fb2.framebuffer_green_mask_size =
486 	    fls(efifb.fb_mask_green >> pos);
487 	gfx_fb.u.fb2.framebuffer_green_field_position = pos;
488 	pos = ffs(efifb.fb_mask_blue);
489 	if (pos != 0)
490 		pos--;
491 	gfx_fb.u.fb2.framebuffer_blue_mask_size =
492 	    fls(efifb.fb_mask_blue >> pos);
493 	gfx_fb.u.fb2.framebuffer_blue_field_position = pos;
494 }
495 
496 static void
efi_cons_probe(struct console * cp)497 efi_cons_probe(struct console *cp)
498 {
499 	struct efi_console_data *ecd;
500 	void *coninex;
501 	EFI_STATUS status;
502 
503 	cp->c_flags |= C_PRESENTIN | C_PRESENTOUT;
504 
505 	if (cp->c_private != NULL)
506 		return;
507 
508 	memset(keybuf, 0, KEYBUFSZ);
509 	conout = ST->ConOut;
510 	ecd = calloc(1, sizeof (*ecd));
511 	/*
512 	 * As console probing is called very early, the only reason for
513 	 * out of memory can be that we just do not have enough memory.
514 	 */
515 	if (ecd == NULL)
516 		panic("efi_cons_probe: This system has not enough memory\n");
517 	cp->c_private = ecd;
518 
519 	ecd->ecd_conin = ST->ConIn;
520 	/*
521 	 * Try to set up for SimpleTextInputEx protocol. If not available,
522 	 * we will use SimpleTextInput protocol.
523 	 */
524 	coninex = NULL;
525 	status = BS->OpenProtocol(ST->ConsoleInHandle,
526 	    &gEfiSimpleTextInputExProtocolGuid,
527 	    &coninex, IH, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
528 	if (status == EFI_SUCCESS)
529 		ecd->ecd_coninex = coninex;
530 
531 	if (efi_find_framebuffer(&efifb)) {
532 		console_mode = EfiConsoleControlScreenText;
533 		ecd->ecd_visual_ops = &text_ops;
534 	} else {
535 		efi_framebuffer_setup();
536 		console_mode = EfiConsoleControlScreenGraphics;
537 		ecd->ecd_visual_ops = &fb_ops;
538 	}
539 
540 	/*
541 	 * UEFI console is tricky, it may be video or serial device(s),
542 	 * or both, SimpleTextOutput protocol will output on all listed
543 	 * devices in "ConOut". If video console is present, we will not
544 	 * use SimpleTextOutput and render the text on video screen only.
545 	 */
546 	if (console_mode == EfiConsoleControlScreenGraphics)
547 		(void) setenv("console", cp->c_name, 1);
548 }
549 
550 static int
efi_cons_init(struct console * cp,int arg __unused)551 efi_cons_init(struct console *cp, int arg __unused)
552 {
553 	EFI_STATUS status;
554 	UINTN i, max_dim, best_mode, cols, rows;
555 
556 	if (tem != NULL)
557 		return (0);
558 
559 	conout->SetAttribute(conout, EFI_TEXT_ATTR(DEFAULT_FGCOLOR,
560 	    DEFAULT_BGCOLOR));
561 
562 	status = BS->LocateProtocol(&gEfiConsoleControlProtocolGuid, NULL,
563 	    (void **)&console_control);
564 
565 	max_dim = best_mode = 0;
566 	for (i = 0; i <= conout->Mode->MaxMode; i++) {
567 		status = conout->QueryMode(conout, i, &cols, &rows);
568 		if (EFI_ERROR(status))
569 			continue;
570 		if (cols * rows > max_dim) {
571 			max_dim = cols * rows;
572 			best_mode = i;
573 		}
574 	}
575 	if (max_dim > 0)
576 		conout->SetMode(conout, best_mode);
577 	status = conout->QueryMode(conout, best_mode, &cols, &rows);
578 	if (EFI_ERROR(status)) {
579 		setenv("screen-#rows", "24", 1);
580 		setenv("screen-#cols", "80", 1);
581 	} else {
582 		char env[8];
583 		snprintf(env, sizeof (env), "%u", (unsigned)rows);
584 		setenv("screen-#rows", env, 1);
585 		snprintf(env, sizeof (env), "%u", (unsigned)cols);
586 		setenv("screen-#cols", env, 1);
587 	}
588 
589 
590 	if (console_control != NULL)
591 		(void) console_control->SetMode(console_control, console_mode);
592 
593 	/* some firmware enables the cursor when switching modes */
594 	conout->EnableCursor(conout, FALSE);
595 
596 	gfx_framework_init();
597 
598 	if (tem_info_init(cp) == 0 && tem == NULL) {
599 		tem = tem_init();
600 		if (tem != NULL)
601 			tem_activate(tem, B_TRUE);
602 	}
603 
604 	if (tem == NULL)
605 		panic("Failed to set up console terminal");
606 
607 	return (0);
608 }
609 
610 static void
efi_cons_putchar(struct console * cp __unused,int c)611 efi_cons_putchar(struct console *cp __unused, int c)
612 {
613 	uint8_t buf = c;
614 
615 	/* make sure we have some console output, support for panic() */
616 	if (tem == NULL)
617 		efi_cons_efiputchar(c);
618 	else
619 		tem_write(tem, &buf, sizeof (buf));
620 }
621 
622 static int
keybuf_getchar(void)623 keybuf_getchar(void)
624 {
625 	int i, c = 0;
626 
627 	for (i = 0; i < KEYBUFSZ; i++) {
628 		if (keybuf[i] != 0) {
629 			c = keybuf[i];
630 			keybuf[i] = 0;
631 			break;
632 		}
633 	}
634 
635 	return (c);
636 }
637 
638 static bool
keybuf_ischar(void)639 keybuf_ischar(void)
640 {
641 	int i;
642 
643 	for (i = 0; i < KEYBUFSZ; i++) {
644 		if (keybuf[i] != 0)
645 			return (true);
646 	}
647 	return (false);
648 }
649 
650 /*
651  * We are not reading input before keybuf is empty, so we are safe
652  * just to fill keybuf from the beginning.
653  */
654 static void
keybuf_inschar(EFI_INPUT_KEY * key)655 keybuf_inschar(EFI_INPUT_KEY *key)
656 {
657 
658 	switch (key->ScanCode) {
659 	case SCAN_UP: /* UP */
660 		keybuf[0] = 0x1b;	/* esc */
661 		keybuf[1] = '[';
662 		keybuf[2] = 'A';
663 		break;
664 	case SCAN_DOWN: /* DOWN */
665 		keybuf[0] = 0x1b;	/* esc */
666 		keybuf[1] = '[';
667 		keybuf[2] = 'B';
668 		break;
669 	case SCAN_RIGHT: /* RIGHT */
670 		keybuf[0] = 0x1b;	/* esc */
671 		keybuf[1] = '[';
672 		keybuf[2] = 'C';
673 		break;
674 	case SCAN_LEFT: /* LEFT */
675 		keybuf[0] = 0x1b;	/* esc */
676 		keybuf[1] = '[';
677 		keybuf[2] = 'D';
678 		break;
679 	case SCAN_DELETE:
680 		keybuf[0] = CHAR_BACKSPACE;
681 		break;
682 	case SCAN_ESC:
683 		keybuf[0] = 0x1b;	/* esc */
684 		break;
685 	default:
686 		keybuf[0] = key->UnicodeChar;
687 		break;
688 	}
689 }
690 
691 static bool
efi_readkey(SIMPLE_INPUT_INTERFACE * conin)692 efi_readkey(SIMPLE_INPUT_INTERFACE *conin)
693 {
694 	EFI_STATUS status;
695 	EFI_INPUT_KEY key;
696 
697 	status = conin->ReadKeyStroke(conin, &key);
698 	if (status == EFI_SUCCESS) {
699 		keybuf_inschar(&key);
700 		return (true);
701 	}
702 	return (false);
703 }
704 
705 static bool
efi_readkey_ex(EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL * coninex)706 efi_readkey_ex(EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *coninex)
707 {
708 	EFI_STATUS status;
709 	EFI_INPUT_KEY *kp;
710 	EFI_KEY_DATA  key_data;
711 	uint32_t kss;
712 
713 	status = coninex->ReadKeyStrokeEx(coninex, &key_data);
714 	if (status == EFI_SUCCESS) {
715 		kss = key_data.KeyState.KeyShiftState;
716 		kp = &key_data.Key;
717 		if (kss & EFI_SHIFT_STATE_VALID) {
718 
719 			/*
720 			 * quick mapping to control chars, replace with
721 			 * map lookup later.
722 			 */
723 			if (kss & EFI_RIGHT_CONTROL_PRESSED ||
724 			    kss & EFI_LEFT_CONTROL_PRESSED) {
725 				if (kp->UnicodeChar >= 'a' &&
726 				    kp->UnicodeChar <= 'z') {
727 					kp->UnicodeChar -= 'a';
728 					kp->UnicodeChar++;
729 				}
730 			}
731 		}
732 		/*
733 		 * The shift state and/or toggle state may not be valid,
734 		 * but we still can have ScanCode or UnicodeChar.
735 		 */
736 		if (kp->ScanCode == 0 && kp->UnicodeChar == 0)
737 			return (false);
738 		keybuf_inschar(kp);
739 		return (true);
740 	}
741 	return (false);
742 }
743 
744 static int
efi_cons_getchar(struct console * cp)745 efi_cons_getchar(struct console *cp)
746 {
747 	struct efi_console_data *ecd;
748 	int c;
749 
750 	if ((c = keybuf_getchar()) != 0)
751 		return (c);
752 
753 	if (!has_boot_services)
754 		return (-1);
755 
756 	ecd = cp->c_private;
757 	key_pending = 0;
758 
759 	if (ecd->ecd_coninex == NULL) {
760 		if (efi_readkey(ecd->ecd_conin))
761 			return (keybuf_getchar());
762 	} else {
763 		if (efi_readkey_ex(ecd->ecd_coninex))
764 			return (keybuf_getchar());
765 	}
766 
767 	return (-1);
768 }
769 
770 static int
efi_cons_poll(struct console * cp)771 efi_cons_poll(struct console *cp)
772 {
773 	struct efi_console_data *ecd;
774 	EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *coninex;
775 	SIMPLE_INPUT_INTERFACE *conin;
776 	EFI_STATUS status;
777 
778 	if (keybuf_ischar() || key_pending)
779 		return (1);
780 
781 	if (!has_boot_services)
782 		return (0);
783 
784 	ecd = cp->c_private;
785 	coninex = ecd->ecd_coninex;
786 	conin = ecd->ecd_conin;
787 	/*
788 	 * Some EFI implementation (u-boot for example) do not support
789 	 * WaitForKey().
790 	 * CheckEvent() can clear the signaled state.
791 	 */
792 	if (coninex != NULL) {
793 		if (coninex->WaitForKeyEx == NULL)
794 			key_pending = efi_readkey_ex(coninex);
795 		else {
796 			status = BS->CheckEvent(coninex->WaitForKeyEx);
797 			key_pending = status == EFI_SUCCESS;
798 		}
799 	} else {
800 		if (conin->WaitForKey == NULL)
801 			key_pending = efi_readkey(conin);
802 		else {
803 			status = BS->CheckEvent(conin->WaitForKey);
804 			key_pending = status == EFI_SUCCESS;
805 		}
806 	}
807 
808 	return (key_pending);
809 }
810 
811 /* Plain direct access to EFI OutputString(). */
812 void
efi_cons_efiputchar(int c)813 efi_cons_efiputchar(int c)
814 {
815 	CHAR16 buf[2];
816 	EFI_STATUS status;
817 
818 	buf[0] = c;
819 	buf[1] = 0;	/* terminate string */
820 
821 	status = conout->TestString(conout, buf);
822 	if (EFI_ERROR(status))
823 		buf[0] = '?';
824 	conout->OutputString(conout, buf);
825 }
826 
827 static void
efi_cons_devinfo_print(EFI_HANDLE handle)828 efi_cons_devinfo_print(EFI_HANDLE handle)
829 {
830 	EFI_DEVICE_PATH *dp;
831 	CHAR16 *text;
832 
833 	dp = efi_lookup_devpath(handle);
834 	if (dp == NULL)
835 		return;
836 
837 	text = efi_devpath_name(dp);
838 	if (text == NULL)
839 		return;
840 
841 	printf("\t%S", text);
842 	efi_free_devpath_name(text);
843 }
844 
845 static void
efi_cons_devinfo(struct console * cp __unused)846 efi_cons_devinfo(struct console *cp __unused)
847 {
848 	EFI_HANDLE *handles;
849 	uint_t nhandles;
850 	EFI_STATUS status;
851 
852 	if (gop != NULL)
853 		status = efi_get_protocol_handles(
854 		    &gEfiGraphicsOutputProtocolGuid, &nhandles, &handles);
855 	else
856 		status = efi_get_protocol_handles(&gEfiUgaDrawProtocolGuid,
857 		    &nhandles, &handles);
858 
859 	if (EFI_ERROR(status))
860 		return;
861 
862 	for (uint_t i = 0; i < nhandles; i++)
863 		efi_cons_devinfo_print(handles[i]);
864 
865 	free(handles);
866 }
867