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 149 plat_stdout_is_framebuffer(void) 150 { 151 return (console_mode == EfiConsoleControlScreenGraphics); 152 } 153 154 void 155 plat_tem_hide_prom_cursor(void) 156 { 157 if (has_boot_services) 158 conout->EnableCursor(conout, FALSE); 159 } 160 161 static void 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 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 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 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 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 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 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 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 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 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 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 424 efi_framebuffer_setup(void) 425 { 426 int bpp, pos; 427 extern EFI_GRAPHICS_OUTPUT_BLT_PIXEL *shadow_fb; 428 429 bpp = fls(efifb.fb_mask_red | efifb.fb_mask_green | 430 efifb.fb_mask_blue | efifb.fb_mask_reserved); 431 432 if (shadow_fb != NULL) 433 free(shadow_fb); 434 shadow_fb = malloc(efifb.fb_width * efifb.fb_height * 435 sizeof (*shadow_fb)); 436 437 gfx_fb.framebuffer_common.mb_type = MULTIBOOT_TAG_TYPE_FRAMEBUFFER; 438 gfx_fb.framebuffer_common.mb_size = sizeof (gfx_fb); 439 gfx_fb.framebuffer_common.framebuffer_addr = efifb.fb_addr; 440 gfx_fb.framebuffer_common.framebuffer_width = efifb.fb_width; 441 gfx_fb.framebuffer_common.framebuffer_height = efifb.fb_height; 442 gfx_fb.framebuffer_common.framebuffer_bpp = bpp; 443 gfx_fb.framebuffer_common.framebuffer_pitch = 444 efifb.fb_stride * (bpp >> 3); 445 gfx_fb.framebuffer_common.framebuffer_type = 446 MULTIBOOT_FRAMEBUFFER_TYPE_RGB; 447 gfx_fb.framebuffer_common.mb_reserved = 0; 448 449 pos = ffs(efifb.fb_mask_red); 450 if (pos != 0) 451 pos--; 452 gfx_fb.u.fb2.framebuffer_red_mask_size = fls(efifb.fb_mask_red >> pos); 453 gfx_fb.u.fb2.framebuffer_red_field_position = pos; 454 pos = ffs(efifb.fb_mask_green); 455 if (pos != 0) 456 pos--; 457 gfx_fb.u.fb2.framebuffer_green_mask_size = 458 fls(efifb.fb_mask_green >> pos); 459 gfx_fb.u.fb2.framebuffer_green_field_position = pos; 460 pos = ffs(efifb.fb_mask_blue); 461 if (pos != 0) 462 pos--; 463 gfx_fb.u.fb2.framebuffer_blue_mask_size = 464 fls(efifb.fb_mask_blue >> pos); 465 gfx_fb.u.fb2.framebuffer_blue_field_position = pos; 466 } 467 468 static void 469 efi_cons_probe(struct console *cp) 470 { 471 cp->c_flags |= C_PRESENTIN | C_PRESENTOUT; 472 } 473 474 static int 475 efi_cons_init(struct console *cp, int arg __unused) 476 { 477 struct efi_console_data *ecd; 478 void *coninex; 479 EFI_STATUS status; 480 UINTN i, max_dim, best_mode, cols, rows; 481 482 if (cp->c_private != NULL) 483 return (0); 484 485 ecd = calloc(1, sizeof (*ecd)); 486 /* 487 * As console probing is called very early, the only reason for 488 * out of memory can be that we just do not have enough memory. 489 */ 490 if (ecd == NULL) 491 panic("efi_cons_probe: This system has not enough memory\n"); 492 cp->c_private = ecd; 493 494 ecd->ecd_conin = ST->ConIn; 495 conout = ST->ConOut; 496 497 conout->SetAttribute(conout, EFI_TEXT_ATTR(DEFAULT_FGCOLOR, 498 DEFAULT_BGCOLOR)); 499 memset(keybuf, 0, KEYBUFSZ); 500 501 status = BS->LocateProtocol(&ccontrol_protocol_guid, NULL, 502 (void **)&console_control); 503 if (status == EFI_SUCCESS) { 504 BOOLEAN GopUgaExists, StdInLocked; 505 status = console_control->GetMode(console_control, 506 &console_mode, &GopUgaExists, &StdInLocked); 507 } else { 508 console_mode = EfiConsoleControlScreenText; 509 } 510 511 max_dim = best_mode = 0; 512 for (i = 0; i <= conout->Mode->MaxMode; i++) { 513 status = conout->QueryMode(conout, i, &cols, &rows); 514 if (EFI_ERROR(status)) 515 continue; 516 if (cols * rows > max_dim) { 517 max_dim = cols * rows; 518 best_mode = i; 519 } 520 } 521 if (max_dim > 0) 522 conout->SetMode(conout, best_mode); 523 status = conout->QueryMode(conout, best_mode, &cols, &rows); 524 if (EFI_ERROR(status)) { 525 setenv("screen-#rows", "24", 1); 526 setenv("screen-#cols", "80", 1); 527 } else { 528 char env[8]; 529 snprintf(env, sizeof (env), "%u", (unsigned)rows); 530 setenv("screen-#rows", env, 1); 531 snprintf(env, sizeof (env), "%u", (unsigned)cols); 532 setenv("screen-#cols", env, 1); 533 } 534 535 if (efi_find_framebuffer(&efifb)) { 536 console_mode = EfiConsoleControlScreenText; 537 ecd->ecd_visual_ops = &text_ops; 538 } else { 539 efi_framebuffer_setup(); 540 console_mode = EfiConsoleControlScreenGraphics; 541 ecd->ecd_visual_ops = &fb_ops; 542 } 543 544 if (console_control != NULL) 545 (void) console_control->SetMode(console_control, console_mode); 546 547 /* some firmware enables the cursor when switching modes */ 548 conout->EnableCursor(conout, FALSE); 549 550 coninex = NULL; 551 /* 552 * Try to set up for SimpleTextInputEx protocol. If not available, 553 * we will use SimpleTextInput protocol. 554 */ 555 status = BS->OpenProtocol(ST->ConsoleInHandle, &simple_input_ex_guid, 556 &coninex, IH, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); 557 if (status == EFI_SUCCESS) 558 ecd->ecd_coninex = coninex; 559 560 gfx_framework_init(); 561 562 if (tem_info_init(cp) == 0 && tem == NULL) { 563 tem = tem_init(); 564 if (tem != NULL) 565 tem_activate(tem, B_TRUE); 566 } 567 568 if (tem == NULL) 569 panic("Failed to set up console terminal"); 570 571 return (0); 572 } 573 574 static void 575 efi_cons_putchar(struct console *cp __unused, int c) 576 { 577 uint8_t buf = c; 578 579 /* make sure we have some console output, support for panic() */ 580 if (tem == NULL) 581 efi_cons_efiputchar(c); 582 else 583 tem_write(tem, &buf, sizeof (buf)); 584 } 585 586 static int 587 keybuf_getchar(void) 588 { 589 int i, c = 0; 590 591 for (i = 0; i < KEYBUFSZ; i++) { 592 if (keybuf[i] != 0) { 593 c = keybuf[i]; 594 keybuf[i] = 0; 595 break; 596 } 597 } 598 599 return (c); 600 } 601 602 static bool 603 keybuf_ischar(void) 604 { 605 int i; 606 607 for (i = 0; i < KEYBUFSZ; i++) { 608 if (keybuf[i] != 0) 609 return (true); 610 } 611 return (false); 612 } 613 614 /* 615 * We are not reading input before keybuf is empty, so we are safe 616 * just to fill keybuf from the beginning. 617 */ 618 static void 619 keybuf_inschar(EFI_INPUT_KEY *key) 620 { 621 622 switch (key->ScanCode) { 623 case SCAN_UP: /* UP */ 624 keybuf[0] = 0x1b; /* esc */ 625 keybuf[1] = '['; 626 keybuf[2] = 'A'; 627 break; 628 case SCAN_DOWN: /* DOWN */ 629 keybuf[0] = 0x1b; /* esc */ 630 keybuf[1] = '['; 631 keybuf[2] = 'B'; 632 break; 633 case SCAN_RIGHT: /* RIGHT */ 634 keybuf[0] = 0x1b; /* esc */ 635 keybuf[1] = '['; 636 keybuf[2] = 'C'; 637 break; 638 case SCAN_LEFT: /* LEFT */ 639 keybuf[0] = 0x1b; /* esc */ 640 keybuf[1] = '['; 641 keybuf[2] = 'D'; 642 break; 643 case SCAN_DELETE: 644 keybuf[0] = CHAR_BACKSPACE; 645 break; 646 case SCAN_ESC: 647 keybuf[0] = 0x1b; /* esc */ 648 break; 649 default: 650 keybuf[0] = key->UnicodeChar; 651 break; 652 } 653 } 654 655 static bool 656 efi_readkey(SIMPLE_INPUT_INTERFACE *conin) 657 { 658 EFI_STATUS status; 659 EFI_INPUT_KEY key; 660 661 status = conin->ReadKeyStroke(conin, &key); 662 if (status == EFI_SUCCESS) { 663 keybuf_inschar(&key); 664 return (true); 665 } 666 return (false); 667 } 668 669 static bool 670 efi_readkey_ex(EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *coninex) 671 { 672 EFI_STATUS status; 673 EFI_INPUT_KEY *kp; 674 EFI_KEY_DATA key_data; 675 uint32_t kss; 676 677 status = coninex->ReadKeyStrokeEx(coninex, &key_data); 678 if (status == EFI_SUCCESS) { 679 kss = key_data.KeyState.KeyShiftState; 680 kp = &key_data.Key; 681 if (kss & EFI_SHIFT_STATE_VALID) { 682 683 /* 684 * quick mapping to control chars, replace with 685 * map lookup later. 686 */ 687 if (kss & EFI_RIGHT_CONTROL_PRESSED || 688 kss & EFI_LEFT_CONTROL_PRESSED) { 689 if (kp->UnicodeChar >= 'a' && 690 kp->UnicodeChar <= 'z') { 691 kp->UnicodeChar -= 'a'; 692 kp->UnicodeChar++; 693 } 694 } 695 } 696 /* 697 * The shift state and/or toggle state may not be valid, 698 * but we still can have ScanCode or UnicodeChar. 699 */ 700 if (kp->ScanCode == 0 && kp->UnicodeChar == 0) 701 return (false); 702 keybuf_inschar(kp); 703 return (true); 704 } 705 return (false); 706 } 707 708 static int 709 efi_cons_getchar(struct console *cp) 710 { 711 struct efi_console_data *ecd; 712 int c; 713 714 if ((c = keybuf_getchar()) != 0) 715 return (c); 716 717 if (!has_boot_services) 718 return (-1); 719 720 ecd = cp->c_private; 721 key_pending = 0; 722 723 if (ecd->ecd_coninex == NULL) { 724 if (efi_readkey(ecd->ecd_conin)) 725 return (keybuf_getchar()); 726 } else { 727 if (efi_readkey_ex(ecd->ecd_coninex)) 728 return (keybuf_getchar()); 729 } 730 731 return (-1); 732 } 733 734 static int 735 efi_cons_poll(struct console *cp) 736 { 737 struct efi_console_data *ecd; 738 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *coninex; 739 SIMPLE_INPUT_INTERFACE *conin; 740 EFI_STATUS status; 741 742 if (keybuf_ischar() || key_pending) 743 return (1); 744 745 if (!has_boot_services) 746 return (0); 747 748 ecd = cp->c_private; 749 coninex = ecd->ecd_coninex; 750 conin = ecd->ecd_conin; 751 /* 752 * Some EFI implementation (u-boot for example) do not support 753 * WaitForKey(). 754 * CheckEvent() can clear the signaled state. 755 */ 756 if (coninex != NULL) { 757 if (coninex->WaitForKeyEx == NULL) 758 key_pending = efi_readkey_ex(coninex); 759 else { 760 status = BS->CheckEvent(coninex->WaitForKeyEx); 761 key_pending = status == EFI_SUCCESS; 762 } 763 } else { 764 if (conin->WaitForKey == NULL) 765 key_pending = efi_readkey(conin); 766 else { 767 status = BS->CheckEvent(conin->WaitForKey); 768 key_pending = status == EFI_SUCCESS; 769 } 770 } 771 772 return (key_pending); 773 } 774 775 /* Plain direct access to EFI OutputString(). */ 776 void 777 efi_cons_efiputchar(int c) 778 { 779 CHAR16 buf[2]; 780 EFI_STATUS status; 781 782 buf[0] = c; 783 buf[1] = 0; /* terminate string */ 784 785 status = conout->TestString(conout, buf); 786 if (EFI_ERROR(status)) 787 buf[0] = '?'; 788 conout->OutputString(conout, buf); 789 } 790 791 static void 792 efi_cons_devinfo_print(EFI_HANDLE handle) 793 { 794 EFI_DEVICE_PATH *dp; 795 CHAR16 *text; 796 797 dp = efi_lookup_devpath(handle); 798 if (dp == NULL) 799 return; 800 801 text = efi_devpath_name(dp); 802 if (text == NULL) 803 return; 804 805 printf("\t%S", text); 806 efi_free_devpath_name(text); 807 } 808 809 static void 810 efi_cons_devinfo(struct console *cp __unused) 811 { 812 EFI_HANDLE *handles; 813 UINTN nhandles; 814 extern EFI_GUID gop_guid; 815 extern EFI_GUID uga_guid; 816 EFI_STATUS status; 817 818 if (gop != NULL) 819 status = BS->LocateHandleBuffer(ByProtocol, &gop_guid, NULL, 820 &nhandles, &handles); 821 else 822 status = BS->LocateHandleBuffer(ByProtocol, &uga_guid, NULL, 823 &nhandles, &handles); 824 825 if (EFI_ERROR(status)) 826 return; 827 828 for (UINTN i = 0; i < nhandles; i++) 829 efi_cons_devinfo_print(handles[i]); 830 831 BS->FreePool(handles); 832 } 833