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