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 167 plat_stdout_is_framebuffer(void) 168 { 169 return (console_mode == EfiConsoleControlScreenGraphics); 170 } 171 172 void 173 plat_tem_hide_prom_cursor(void) 174 { 175 if (has_boot_services) 176 conout->EnableCursor(conout, FALSE); 177 } 178 179 static void 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 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 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 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 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 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 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 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 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 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 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 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 497 efi_cons_probe(struct console *cp) 498 { 499 cp->c_flags |= C_PRESENTIN | C_PRESENTOUT; 500 } 501 502 static int 503 efi_cons_init(struct console *cp, int arg __unused) 504 { 505 struct efi_console_data *ecd; 506 void *coninex; 507 EFI_STATUS status; 508 UINTN i, max_dim, best_mode, cols, rows; 509 510 if (cp->c_private != NULL) 511 return (0); 512 513 ecd = calloc(1, sizeof (*ecd)); 514 /* 515 * As console probing is called very early, the only reason for 516 * out of memory can be that we just do not have enough memory. 517 */ 518 if (ecd == NULL) 519 panic("efi_cons_probe: This system has not enough memory\n"); 520 cp->c_private = ecd; 521 522 ecd->ecd_conin = ST->ConIn; 523 conout = ST->ConOut; 524 525 conout->SetAttribute(conout, EFI_TEXT_ATTR(DEFAULT_FGCOLOR, 526 DEFAULT_BGCOLOR)); 527 memset(keybuf, 0, KEYBUFSZ); 528 529 status = BS->LocateProtocol(&gEfiConsoleControlProtocolGuid, NULL, 530 (void **)&console_control); 531 if (status == EFI_SUCCESS) { 532 BOOLEAN GopUgaExists, StdInLocked; 533 status = console_control->GetMode(console_control, 534 &console_mode, &GopUgaExists, &StdInLocked); 535 } else { 536 console_mode = EfiConsoleControlScreenText; 537 } 538 539 max_dim = best_mode = 0; 540 for (i = 0; i <= conout->Mode->MaxMode; i++) { 541 status = conout->QueryMode(conout, i, &cols, &rows); 542 if (EFI_ERROR(status)) 543 continue; 544 if (cols * rows > max_dim) { 545 max_dim = cols * rows; 546 best_mode = i; 547 } 548 } 549 if (max_dim > 0) 550 conout->SetMode(conout, best_mode); 551 status = conout->QueryMode(conout, best_mode, &cols, &rows); 552 if (EFI_ERROR(status)) { 553 setenv("screen-#rows", "24", 1); 554 setenv("screen-#cols", "80", 1); 555 } else { 556 char env[8]; 557 snprintf(env, sizeof (env), "%u", (unsigned)rows); 558 setenv("screen-#rows", env, 1); 559 snprintf(env, sizeof (env), "%u", (unsigned)cols); 560 setenv("screen-#cols", env, 1); 561 } 562 563 if (efi_find_framebuffer(&efifb)) { 564 console_mode = EfiConsoleControlScreenText; 565 ecd->ecd_visual_ops = &text_ops; 566 } else { 567 efi_framebuffer_setup(); 568 console_mode = EfiConsoleControlScreenGraphics; 569 ecd->ecd_visual_ops = &fb_ops; 570 } 571 572 if (console_control != NULL) 573 (void) console_control->SetMode(console_control, console_mode); 574 575 /* some firmware enables the cursor when switching modes */ 576 conout->EnableCursor(conout, FALSE); 577 578 coninex = NULL; 579 /* 580 * Try to set up for SimpleTextInputEx protocol. If not available, 581 * we will use SimpleTextInput protocol. 582 */ 583 status = BS->OpenProtocol(ST->ConsoleInHandle, 584 &gEfiSimpleTextInputExProtocolGuid, 585 &coninex, IH, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); 586 if (status == EFI_SUCCESS) 587 ecd->ecd_coninex = coninex; 588 589 gfx_framework_init(); 590 591 if (tem_info_init(cp) == 0 && tem == NULL) { 592 tem = tem_init(); 593 if (tem != NULL) 594 tem_activate(tem, B_TRUE); 595 } 596 597 if (tem == NULL) 598 panic("Failed to set up console terminal"); 599 600 return (0); 601 } 602 603 static void 604 efi_cons_putchar(struct console *cp __unused, int c) 605 { 606 uint8_t buf = c; 607 608 /* make sure we have some console output, support for panic() */ 609 if (tem == NULL) 610 efi_cons_efiputchar(c); 611 else 612 tem_write(tem, &buf, sizeof (buf)); 613 } 614 615 static int 616 keybuf_getchar(void) 617 { 618 int i, c = 0; 619 620 for (i = 0; i < KEYBUFSZ; i++) { 621 if (keybuf[i] != 0) { 622 c = keybuf[i]; 623 keybuf[i] = 0; 624 break; 625 } 626 } 627 628 return (c); 629 } 630 631 static bool 632 keybuf_ischar(void) 633 { 634 int i; 635 636 for (i = 0; i < KEYBUFSZ; i++) { 637 if (keybuf[i] != 0) 638 return (true); 639 } 640 return (false); 641 } 642 643 /* 644 * We are not reading input before keybuf is empty, so we are safe 645 * just to fill keybuf from the beginning. 646 */ 647 static void 648 keybuf_inschar(EFI_INPUT_KEY *key) 649 { 650 651 switch (key->ScanCode) { 652 case SCAN_UP: /* UP */ 653 keybuf[0] = 0x1b; /* esc */ 654 keybuf[1] = '['; 655 keybuf[2] = 'A'; 656 break; 657 case SCAN_DOWN: /* DOWN */ 658 keybuf[0] = 0x1b; /* esc */ 659 keybuf[1] = '['; 660 keybuf[2] = 'B'; 661 break; 662 case SCAN_RIGHT: /* RIGHT */ 663 keybuf[0] = 0x1b; /* esc */ 664 keybuf[1] = '['; 665 keybuf[2] = 'C'; 666 break; 667 case SCAN_LEFT: /* LEFT */ 668 keybuf[0] = 0x1b; /* esc */ 669 keybuf[1] = '['; 670 keybuf[2] = 'D'; 671 break; 672 case SCAN_DELETE: 673 keybuf[0] = CHAR_BACKSPACE; 674 break; 675 case SCAN_ESC: 676 keybuf[0] = 0x1b; /* esc */ 677 break; 678 default: 679 keybuf[0] = key->UnicodeChar; 680 break; 681 } 682 } 683 684 static bool 685 efi_readkey(SIMPLE_INPUT_INTERFACE *conin) 686 { 687 EFI_STATUS status; 688 EFI_INPUT_KEY key; 689 690 status = conin->ReadKeyStroke(conin, &key); 691 if (status == EFI_SUCCESS) { 692 keybuf_inschar(&key); 693 return (true); 694 } 695 return (false); 696 } 697 698 static bool 699 efi_readkey_ex(EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *coninex) 700 { 701 EFI_STATUS status; 702 EFI_INPUT_KEY *kp; 703 EFI_KEY_DATA key_data; 704 uint32_t kss; 705 706 status = coninex->ReadKeyStrokeEx(coninex, &key_data); 707 if (status == EFI_SUCCESS) { 708 kss = key_data.KeyState.KeyShiftState; 709 kp = &key_data.Key; 710 if (kss & EFI_SHIFT_STATE_VALID) { 711 712 /* 713 * quick mapping to control chars, replace with 714 * map lookup later. 715 */ 716 if (kss & EFI_RIGHT_CONTROL_PRESSED || 717 kss & EFI_LEFT_CONTROL_PRESSED) { 718 if (kp->UnicodeChar >= 'a' && 719 kp->UnicodeChar <= 'z') { 720 kp->UnicodeChar -= 'a'; 721 kp->UnicodeChar++; 722 } 723 } 724 } 725 /* 726 * The shift state and/or toggle state may not be valid, 727 * but we still can have ScanCode or UnicodeChar. 728 */ 729 if (kp->ScanCode == 0 && kp->UnicodeChar == 0) 730 return (false); 731 keybuf_inschar(kp); 732 return (true); 733 } 734 return (false); 735 } 736 737 static int 738 efi_cons_getchar(struct console *cp) 739 { 740 struct efi_console_data *ecd; 741 int c; 742 743 if ((c = keybuf_getchar()) != 0) 744 return (c); 745 746 if (!has_boot_services) 747 return (-1); 748 749 ecd = cp->c_private; 750 key_pending = 0; 751 752 if (ecd->ecd_coninex == NULL) { 753 if (efi_readkey(ecd->ecd_conin)) 754 return (keybuf_getchar()); 755 } else { 756 if (efi_readkey_ex(ecd->ecd_coninex)) 757 return (keybuf_getchar()); 758 } 759 760 return (-1); 761 } 762 763 static int 764 efi_cons_poll(struct console *cp) 765 { 766 struct efi_console_data *ecd; 767 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *coninex; 768 SIMPLE_INPUT_INTERFACE *conin; 769 EFI_STATUS status; 770 771 if (keybuf_ischar() || key_pending) 772 return (1); 773 774 if (!has_boot_services) 775 return (0); 776 777 ecd = cp->c_private; 778 coninex = ecd->ecd_coninex; 779 conin = ecd->ecd_conin; 780 /* 781 * Some EFI implementation (u-boot for example) do not support 782 * WaitForKey(). 783 * CheckEvent() can clear the signaled state. 784 */ 785 if (coninex != NULL) { 786 if (coninex->WaitForKeyEx == NULL) 787 key_pending = efi_readkey_ex(coninex); 788 else { 789 status = BS->CheckEvent(coninex->WaitForKeyEx); 790 key_pending = status == EFI_SUCCESS; 791 } 792 } else { 793 if (conin->WaitForKey == NULL) 794 key_pending = efi_readkey(conin); 795 else { 796 status = BS->CheckEvent(conin->WaitForKey); 797 key_pending = status == EFI_SUCCESS; 798 } 799 } 800 801 return (key_pending); 802 } 803 804 /* Plain direct access to EFI OutputString(). */ 805 void 806 efi_cons_efiputchar(int c) 807 { 808 CHAR16 buf[2]; 809 EFI_STATUS status; 810 811 buf[0] = c; 812 buf[1] = 0; /* terminate string */ 813 814 status = conout->TestString(conout, buf); 815 if (EFI_ERROR(status)) 816 buf[0] = '?'; 817 conout->OutputString(conout, buf); 818 } 819 820 static void 821 efi_cons_devinfo_print(EFI_HANDLE handle) 822 { 823 EFI_DEVICE_PATH *dp; 824 CHAR16 *text; 825 826 dp = efi_lookup_devpath(handle); 827 if (dp == NULL) 828 return; 829 830 text = efi_devpath_name(dp); 831 if (text == NULL) 832 return; 833 834 printf("\t%S", text); 835 efi_free_devpath_name(text); 836 } 837 838 static void 839 efi_cons_devinfo(struct console *cp __unused) 840 { 841 EFI_HANDLE *handles; 842 uint_t nhandles; 843 EFI_STATUS status; 844 845 if (gop != NULL) 846 status = efi_get_protocol_handles( 847 &gEfiGraphicsOutputProtocolGuid, &nhandles, &handles); 848 else 849 status = efi_get_protocol_handles(&gEfiUgaDrawProtocolGuid, 850 &nhandles, &handles); 851 852 if (EFI_ERROR(status)) 853 return; 854 855 for (uint_t i = 0; i < nhandles; i++) 856 efi_cons_devinfo_print(handles[i]); 857 858 free(handles); 859 } 860