| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "console.h" |
| 4 |
#include "device-path-util.h" |
| 5 |
#include "efi-log.h" |
| 6 |
#include "efi-string.h" |
| 7 |
#include "proto/graphics-output.h" |
| 8 |
#include "proto/pci-io.h" |
| 9 |
#include "string-util.h" |
| 10 |
#include "util.h" |
| 11 |
|
| 12 |
#define SYSTEM_FONT_WIDTH 8 |
| 13 |
#define SYSTEM_FONT_HEIGHT 19 |
| 14 |
#define HORIZONTAL_MAX_OK 1920 |
| 15 |
#define VERTICAL_MAX_OK 1080 |
| 16 |
#define VIEWPORT_RATIO 10 |
| 17 |
|
| 18 |
static void event_closep(EFI_EVENT *event) { |
| 19 |
assert(event); |
| 20 |
|
| 21 |
if (!*event) |
| 22 |
return; |
| 23 |
|
| 24 |
BS->CloseEvent(*event); |
| 25 |
} |
| 26 |
|
| 27 |
/* |
| 28 |
* Reading input from the console sounds like an easy task to do, but thanks to broken |
| 29 |
* firmware it is actually a nightmare. |
| 30 |
* |
| 31 |
* There is a SimpleTextInput and SimpleTextInputEx API for this. Ideally we want to use |
| 32 |
* TextInputEx, because that gives us Ctrl/Alt/Shift key state information. Unfortunately, |
| 33 |
* it is not always available and sometimes just non-functional. |
| 34 |
* |
| 35 |
* On some firmware, calling ReadKeyStroke or ReadKeyStrokeEx on the default console input |
| 36 |
* device will just freeze no matter what (even though it *reported* being ready). |
| 37 |
* Also, multiple input protocols can be backed by the same device, but they can be out of |
| 38 |
* sync. Falling back on a different protocol can end up with double input. |
| 39 |
* |
| 40 |
* Therefore, we will preferably use TextInputEx for ConIn if that is available. Additionally, |
| 41 |
* we look for the first TextInputEx device the firmware gives us as a fallback option. It |
| 42 |
* will replace ConInEx permanently if it ever reports a key press. |
| 43 |
* Lastly, a timer event allows us to provide a input timeout without having to call into |
| 44 |
* any input functions that can freeze on us or using a busy/stall loop. */ |
| 45 |
EFI_STATUS console_key_read(uint64_t *ret_key, uint64_t timeout_usec) { |
| 46 |
static EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *conInEx = NULL, *extraInEx = NULL; |
| 47 |
static bool checked = false; |
| 48 |
size_t index; |
| 49 |
EFI_STATUS err; |
| 50 |
_cleanup_(event_closep) EFI_EVENT timer = NULL; |
| 51 |
|
| 52 |
if (!checked) { |
| 53 |
/* Get the *first* TextInputEx device. */ |
| 54 |
err = BS->LocateProtocol( |
| 55 |
MAKE_GUID_PTR(EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL), NULL, (void **) &extraInEx); |
| 56 |
if (err != EFI_SUCCESS || BS->CheckEvent(extraInEx->WaitForKeyEx) == EFI_INVALID_PARAMETER) |
| 57 |
/* If WaitForKeyEx fails here, the firmware pretends it talks this |
| 58 |
* protocol, but it really doesn't. */ |
| 59 |
extraInEx = NULL; |
| 60 |
|
| 61 |
/* Get the TextInputEx version of ST->ConIn. */ |
| 62 |
err = BS->HandleProtocol( |
| 63 |
ST->ConsoleInHandle, |
| 64 |
MAKE_GUID_PTR(EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL), |
| 65 |
(void **) &conInEx); |
| 66 |
if (err != EFI_SUCCESS || BS->CheckEvent(conInEx->WaitForKeyEx) == EFI_INVALID_PARAMETER) |
| 67 |
conInEx = NULL; |
| 68 |
|
| 69 |
if (conInEx == extraInEx) |
| 70 |
extraInEx = NULL; |
| 71 |
|
| 72 |
checked = true; |
| 73 |
} |
| 74 |
|
| 75 |
err = BS->CreateEvent(EVT_TIMER, 0, NULL, NULL, &timer); |
| 76 |
if (err != EFI_SUCCESS) |
| 77 |
return log_error_status(err, "Error creating timer event: %m"); |
| 78 |
|
| 79 |
EFI_EVENT events[] = { |
| 80 |
timer, |
| 81 |
conInEx ? conInEx->WaitForKeyEx : ST->ConIn->WaitForKey, |
| 82 |
extraInEx ? extraInEx->WaitForKeyEx : NULL, |
| 83 |
}; |
| 84 |
size_t n_events = extraInEx ? 3 : 2; |
| 85 |
|
| 86 |
/* Watchdog rearming loop in case the user never provides us with input or some |
| 87 |
* broken firmware never returns from WaitForEvent. */ |
| 88 |
for (;;) { |
| 89 |
uint64_t watchdog_timeout_sec = 5 * 60, |
| 90 |
watchdog_ping_usec = watchdog_timeout_sec / 2 * 1000 * 1000; |
| 91 |
|
| 92 |
/* SetTimer expects 100ns units for some reason. */ |
| 93 |
err = BS->SetTimer( |
| 94 |
timer, |
| 95 |
TimerRelative, |
| 96 |
MIN(timeout_usec, watchdog_ping_usec) * 10); |
| 97 |
if (err != EFI_SUCCESS) |
| 98 |
return log_error_status(err, "Error arming timer event: %m"); |
| 99 |
|
| 100 |
(void) BS->SetWatchdogTimer(watchdog_timeout_sec, 0x10000, 0, NULL); |
| 101 |
err = BS->WaitForEvent(n_events, events, &index); |
| 102 |
(void) BS->SetWatchdogTimer(watchdog_timeout_sec, 0x10000, 0, NULL); |
| 103 |
|
| 104 |
if (err != EFI_SUCCESS) |
| 105 |
return log_error_status(err, "Error waiting for events: %m"); |
| 106 |
|
| 107 |
/* We have keyboard input, process it after this loop. */ |
| 108 |
if (timer != events[index]) |
| 109 |
break; |
| 110 |
|
| 111 |
/* The EFI timer fired instead. If this was a watchdog timeout, loop again. */ |
| 112 |
if (timeout_usec == UINT64_MAX) |
| 113 |
continue; |
| 114 |
else if (timeout_usec > watchdog_ping_usec) { |
| 115 |
timeout_usec -= watchdog_ping_usec; |
| 116 |
continue; |
| 117 |
} |
| 118 |
|
| 119 |
/* The caller requested a timeout? They shall have one! */ |
| 120 |
return EFI_TIMEOUT; |
| 121 |
} |
| 122 |
|
| 123 |
/* If the extra input device we found returns something, always use that instead |
| 124 |
* to work around broken firmware freezing on ConIn/ConInEx. */ |
| 125 |
if (extraInEx && BS->CheckEvent(extraInEx->WaitForKeyEx) == EFI_SUCCESS) |
| 126 |
conInEx = TAKE_PTR(extraInEx); |
| 127 |
|
| 128 |
/* Do not fall back to ConIn if we have a ConIn that supports TextInputEx. |
| 129 |
* The two may be out of sync on some firmware, giving us double input. */ |
| 130 |
if (conInEx) { |
| 131 |
EFI_KEY_DATA keydata; |
| 132 |
uint32_t shift = 0; |
| 133 |
|
| 134 |
err = conInEx->ReadKeyStrokeEx(conInEx, &keydata); |
| 135 |
if (err != EFI_SUCCESS) |
| 136 |
return err; |
| 137 |
|
| 138 |
if (FLAGS_SET(keydata.KeyState.KeyShiftState, EFI_SHIFT_STATE_VALID)) { |
| 139 |
/* Do not distinguish between left and right keys (set both flags). */ |
| 140 |
if (keydata.KeyState.KeyShiftState & EFI_CONTROL_PRESSED) |
| 141 |
shift |= EFI_CONTROL_PRESSED; |
| 142 |
if (keydata.KeyState.KeyShiftState & EFI_ALT_PRESSED) |
| 143 |
shift |= EFI_ALT_PRESSED; |
| 144 |
if (keydata.KeyState.KeyShiftState & EFI_LOGO_PRESSED) |
| 145 |
shift |= EFI_LOGO_PRESSED; |
| 146 |
|
| 147 |
/* Shift is not supposed to be reported for keys that can be represented as uppercase |
| 148 |
* unicode chars (Shift+f is reported as F instead). Some firmware does it anyway, so |
| 149 |
* filter those out. */ |
| 150 |
if ((keydata.KeyState.KeyShiftState & EFI_SHIFT_PRESSED) && |
| 151 |
keydata.Key.UnicodeChar == 0) |
| 152 |
shift |= EFI_SHIFT_PRESSED; |
| 153 |
} |
| 154 |
|
| 155 |
/* 32 bit modifier keys + 16 bit scan code + 16 bit unicode */ |
| 156 |
if (ret_key) |
| 157 |
*ret_key = KEYPRESS(shift, keydata.Key.ScanCode, keydata.Key.UnicodeChar); |
| 158 |
return EFI_SUCCESS; |
| 159 |
} else if (BS->CheckEvent(ST->ConIn->WaitForKey) == EFI_SUCCESS) { |
| 160 |
EFI_INPUT_KEY k; |
| 161 |
|
| 162 |
err = ST->ConIn->ReadKeyStroke(ST->ConIn, &k); |
| 163 |
if (err != EFI_SUCCESS) |
| 164 |
return err; |
| 165 |
|
| 166 |
if (ret_key) |
| 167 |
*ret_key = KEYPRESS(0, k.ScanCode, k.UnicodeChar); |
| 168 |
return EFI_SUCCESS; |
| 169 |
} |
| 170 |
|
| 171 |
return EFI_NOT_READY; |
| 172 |
} |
| 173 |
|
| 174 |
static EFI_STATUS change_mode(int64_t mode) { |
| 175 |
EFI_STATUS err; |
| 176 |
int32_t old_mode; |
| 177 |
|
| 178 |
/* SetMode expects a size_t, so make sure these values are sane. */ |
| 179 |
mode = CLAMP(mode, CONSOLE_MODE_RANGE_MIN, CONSOLE_MODE_RANGE_MAX); |
| 180 |
old_mode = MAX(CONSOLE_MODE_RANGE_MIN, ST->ConOut->Mode->Mode); |
| 181 |
|
| 182 |
log_wait(); |
| 183 |
err = ST->ConOut->SetMode(ST->ConOut, mode); |
| 184 |
if (err == EFI_SUCCESS) |
| 185 |
return EFI_SUCCESS; |
| 186 |
|
| 187 |
/* Something went wrong. Output is probably borked, so try to revert to previous mode. */ |
| 188 |
if (ST->ConOut->SetMode(ST->ConOut, old_mode) == EFI_SUCCESS) |
| 189 |
return err; |
| 190 |
|
| 191 |
/* Maybe the device is on fire? */ |
| 192 |
ST->ConOut->Reset(ST->ConOut, true); |
| 193 |
ST->ConOut->SetMode(ST->ConOut, CONSOLE_MODE_RANGE_MIN); |
| 194 |
return err; |
| 195 |
} |
| 196 |
|
| 197 |
EFI_STATUS query_screen_resolution(uint32_t *ret_w, uint32_t *ret_h) { |
| 198 |
EFI_STATUS err; |
| 199 |
EFI_GRAPHICS_OUTPUT_PROTOCOL *go; |
| 200 |
|
| 201 |
assert(ret_w); |
| 202 |
assert(ret_h); |
| 203 |
|
| 204 |
err = BS->LocateProtocol(MAKE_GUID_PTR(EFI_GRAPHICS_OUTPUT_PROTOCOL), NULL, (void **) &go); |
| 205 |
if (err != EFI_SUCCESS) |
| 206 |
return err; |
| 207 |
|
| 208 |
if (!go->Mode || !go->Mode->Info) |
| 209 |
return EFI_DEVICE_ERROR; |
| 210 |
|
| 211 |
*ret_w = go->Mode->Info->HorizontalResolution; |
| 212 |
*ret_h = go->Mode->Info->VerticalResolution; |
| 213 |
return EFI_SUCCESS; |
| 214 |
} |
| 215 |
|
| 216 |
static int64_t get_auto_mode(void) { |
| 217 |
uint32_t screen_width, screen_height; |
| 218 |
|
| 219 |
if (query_screen_resolution(&screen_width, &screen_height) == EFI_SUCCESS) { |
| 220 |
bool keep = false; |
| 221 |
|
| 222 |
/* Start verifying if we are in a resolution larger than Full HD |
| 223 |
* (1920x1080). If we're not, assume we're in a good mode and do not |
| 224 |
* try to change it. */ |
| 225 |
if (screen_width <= HORIZONTAL_MAX_OK && screen_height <= VERTICAL_MAX_OK) |
| 226 |
keep = true; |
| 227 |
/* For larger resolutions, calculate the ratio of the total screen |
| 228 |
* area to the text viewport area. If it's less than 10 times bigger, |
| 229 |
* then assume the text is readable and keep the text mode. */ |
| 230 |
else { |
| 231 |
uint64_t text_area; |
| 232 |
size_t x_max, y_max; |
| 233 |
uint64_t screen_area = (uint64_t)screen_width * (uint64_t)screen_height; |
| 234 |
|
| 235 |
console_query_mode(&x_max, &y_max); |
| 236 |
text_area = SYSTEM_FONT_WIDTH * SYSTEM_FONT_HEIGHT * (uint64_t)x_max * (uint64_t)y_max; |
| 237 |
|
| 238 |
if (text_area != 0 && screen_area/text_area < VIEWPORT_RATIO) |
| 239 |
keep = true; |
| 240 |
} |
| 241 |
|
| 242 |
if (keep) |
| 243 |
return ST->ConOut->Mode->Mode; |
| 244 |
} |
| 245 |
|
| 246 |
/* If we reached here, then we have a high resolution screen and the text |
| 247 |
* viewport is less than 10% the screen area, so the firmware developer |
| 248 |
* screwed up. Try to switch to a better mode. Mode number 2 is first non |
| 249 |
* standard mode, which is provided by the device manufacturer, so it should |
| 250 |
* be a good mode. |
| 251 |
* Note: MaxMode is the number of modes, not the last mode. */ |
| 252 |
if (ST->ConOut->Mode->MaxMode > CONSOLE_MODE_FIRMWARE_FIRST) |
| 253 |
return CONSOLE_MODE_FIRMWARE_FIRST; |
| 254 |
|
| 255 |
/* Try again with mode different than zero (assume user requests |
| 256 |
* auto mode due to some problem with mode zero). */ |
| 257 |
if (ST->ConOut->Mode->MaxMode > CONSOLE_MODE_80_50) |
| 258 |
return CONSOLE_MODE_80_50; |
| 259 |
|
| 260 |
return CONSOLE_MODE_80_25; |
| 261 |
} |
| 262 |
|
| 263 |
static int next_mode(int64_t mode, int64_t direction) { |
| 264 |
assert(IN_SET(direction, 1, -1)); |
| 265 |
assert(ST->ConOut->Mode->MaxMode > 0); |
| 266 |
|
| 267 |
/* Always start at the beginning if we are out of range or reached the last mode already */ |
| 268 |
if (direction > 0) { |
| 269 |
if (mode < CONSOLE_MODE_RANGE_MIN || mode >= ST->ConOut->Mode->MaxMode-1) |
| 270 |
return CONSOLE_MODE_RANGE_MIN; |
| 271 |
} else if (direction < 0) { |
| 272 |
if (mode <= CONSOLE_MODE_RANGE_MIN || mode > ST->ConOut->Mode->MaxMode-1) |
| 273 |
return ST->ConOut->Mode->MaxMode-1; |
| 274 |
} else |
| 275 |
assert_not_reached(); |
| 276 |
|
| 277 |
return mode + direction; |
| 278 |
} |
| 279 |
|
| 280 |
EFI_STATUS console_set_mode(int64_t mode) { |
| 281 |
EFI_STATUS r; |
| 282 |
|
| 283 |
/* If there are no modes defined, fail immediately */ |
| 284 |
if (ST->ConOut->Mode->MaxMode <= 0) |
| 285 |
return mode == CONSOLE_MODE_KEEP ? EFI_SUCCESS : EFI_UNSUPPORTED; |
| 286 |
|
| 287 |
int64_t target, direction = 1; |
| 288 |
switch (mode) { |
| 289 |
case CONSOLE_MODE_KEEP: |
| 290 |
/* If the firmware indicates the current mode is invalid, change it anyway. */ |
| 291 |
if (ST->ConOut->Mode->Mode >= CONSOLE_MODE_RANGE_MIN && |
| 292 |
ST->ConOut->Mode->Mode < ST->ConOut->Mode->MaxMode) |
| 293 |
return EFI_SUCCESS; |
| 294 |
|
| 295 |
target = CONSOLE_MODE_RANGE_MIN; |
| 296 |
break; |
| 297 |
|
| 298 |
case CONSOLE_MODE_NEXT: |
| 299 |
target = next_mode(ST->ConOut->Mode->Mode, direction); |
| 300 |
break; |
| 301 |
|
| 302 |
case CONSOLE_MODE_AUTO: |
| 303 |
target = get_auto_mode(); |
| 304 |
break; |
| 305 |
|
| 306 |
case CONSOLE_MODE_FIRMWARE_MAX: |
| 307 |
/* Note: MaxMode is the number of modes, not the last mode. */ |
| 308 |
target = ST->ConOut->Mode->MaxMode - 1; |
| 309 |
direction = -1; /* search backwards for a working mode */ |
| 310 |
break; |
| 311 |
|
| 312 |
case CONSOLE_MODE_RANGE_MIN...CONSOLE_MODE_RANGE_MAX: |
| 313 |
target = mode; |
| 314 |
break; |
| 315 |
|
| 316 |
default: |
| 317 |
assert_not_reached(); |
| 318 |
} |
| 319 |
|
| 320 |
for (int64_t attempt = 0;; attempt++) { |
| 321 |
r = change_mode(target); |
| 322 |
if (r == EFI_SUCCESS) |
| 323 |
return EFI_SUCCESS; |
| 324 |
if (attempt >= ST->ConOut->Mode->MaxMode-1) /* give up, once we tried them all */ |
| 325 |
return r; |
| 326 |
|
| 327 |
/* If this mode is broken/unsupported, try the next. */ |
| 328 |
target = next_mode(target, direction); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
EFI_STATUS console_query_mode(size_t *x_max, size_t *y_max) { |
| 333 |
EFI_STATUS err; |
| 334 |
|
| 335 |
assert(x_max); |
| 336 |
assert(y_max); |
| 337 |
|
| 338 |
err = ST->ConOut->QueryMode(ST->ConOut, ST->ConOut->Mode->Mode, x_max, y_max); |
| 339 |
if (err != EFI_SUCCESS) { |
| 340 |
/* Fallback values mandated by UEFI spec. */ |
| 341 |
switch (ST->ConOut->Mode->Mode) { |
| 342 |
case CONSOLE_MODE_80_50: |
| 343 |
*x_max = 80; |
| 344 |
*y_max = 50; |
| 345 |
break; |
| 346 |
case CONSOLE_MODE_80_25: |
| 347 |
default: |
| 348 |
*x_max = 80; |
| 349 |
*y_max = 25; |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
return err; |
| 354 |
} |
| 355 |
|
| 356 |
static bool has_virtio_console_pci_device(void) { |
| 357 |
_cleanup_free_ EFI_HANDLE *handles = NULL; |
| 358 |
size_t n_handles = 0; |
| 359 |
|
| 360 |
EFI_STATUS err = BS->LocateHandleBuffer( |
| 361 |
ByProtocol, |
| 362 |
MAKE_GUID_PTR(EFI_PCI_IO_PROTOCOL), |
| 363 |
NULL, |
| 364 |
&n_handles, |
| 365 |
&handles); |
| 366 |
if (err != EFI_SUCCESS) { |
| 367 |
log_debug_status(err, "Failed to locate PCI I/O protocol handles, assuming no VirtIO console: %m"); |
| 368 |
return false; |
| 369 |
} |
| 370 |
|
| 371 |
if (n_handles == 0) { |
| 372 |
log_debug("No PCI devices found, not scanning for VirtIO console."); |
| 373 |
return false; |
| 374 |
} |
| 375 |
|
| 376 |
log_debug("Found %zu PCI devices, scanning for VirtIO console...", n_handles); |
| 377 |
|
| 378 |
size_t n_virtio_console = 0; |
| 379 |
|
| 380 |
for (size_t i = 0; i < n_handles; i++) { |
| 381 |
EFI_PCI_IO_PROTOCOL *pci_io = NULL; |
| 382 |
|
| 383 |
if (BS->HandleProtocol(handles[i], MAKE_GUID_PTR(EFI_PCI_IO_PROTOCOL), (void **) &pci_io) != EFI_SUCCESS) |
| 384 |
continue; |
| 385 |
|
| 386 |
/* Read PCI vendor ID and device ID (at offsets 0x00 and 0x02 in PCI config space) */ |
| 387 |
uint16_t pci_id[2] = {}; |
| 388 |
if (pci_io->Pci.Read(pci_io, EfiPciIoWidthUint16, /* offset= */ 0x00, /* count= */ 2, pci_id) != EFI_SUCCESS) |
| 389 |
continue; |
| 390 |
|
| 391 |
log_debug("PCI device %zu: vendor=%04x device=%04x", i, pci_id[0], pci_id[1]); |
| 392 |
|
| 393 |
if (pci_id[0] == PCI_VENDOR_ID_REDHAT && pci_id[1] == PCI_DEVICE_ID_VIRTIO_CONSOLE) |
| 394 |
n_virtio_console++; |
| 395 |
|
| 396 |
if (n_virtio_console > 1) { |
| 397 |
log_debug("There is more than one VirtIO console PCI device, cannot determine which one is the console."); |
| 398 |
return false; |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
if (n_virtio_console == 0) { |
| 403 |
log_debug("No VirtIO console PCI device found."); |
| 404 |
return false; |
| 405 |
} |
| 406 |
|
| 407 |
log_debug("Found exactly one VirtIO console PCI device."); |
| 408 |
return true; |
| 409 |
} |
| 410 |
|
| 411 |
static bool has_graphics_output(void) { |
| 412 |
EFI_GRAPHICS_OUTPUT_PROTOCOL *gop = NULL; |
| 413 |
EFI_STATUS err; |
| 414 |
|
| 415 |
err = BS->LocateProtocol(MAKE_GUID_PTR(EFI_GRAPHICS_OUTPUT_PROTOCOL), NULL, (void **) &gop); |
| 416 |
if (err != EFI_SUCCESS) { |
| 417 |
log_debug_status(err, "No EFI Graphics Output Protocol found: %m"); |
| 418 |
return false; |
| 419 |
} |
| 420 |
|
| 421 |
log_debug("EFI Graphics Output Protocol found."); |
| 422 |
return true; |
| 423 |
} |
| 424 |
|
| 425 |
#if defined(__i386__) || defined(__x86_64__) |
| 426 |
|
| 427 |
/* Walk the device path looking for a UART console and determine the COM port index from the |
| 428 |
* ACPI device path node. On x86, the Linux kernel assigns fixed ttyS indices based on I/O port |
| 429 |
* addresses (see arch/x86/include/asm/serial.h): |
| 430 |
* |
| 431 |
* ttyS0=0x3F8, ttyS1=0x2F8, ttyS2=0x3E8, ttyS3=0x2E8 |
| 432 |
* |
| 433 |
* On standard PC firmware, the ACPI UID for PNP0501 (16550 UART) maps directly to the COM port |
| 434 |
* index: UID 0 = COM1 (0x3F8) = ttyS0, UID 1 = COM2 (0x2F8) = ttyS1, etc. |
| 435 |
* |
| 436 |
* Returns EFI_SUCCESS and sets *ret_index on success, or EFI_NOT_FOUND if no PNP0501 UART |
| 437 |
* was found. */ |
| 438 |
static EFI_STATUS device_path_get_uart_index(const EFI_DEVICE_PATH *dp, uint32_t *ret_index) { |
| 439 |
assert(ret_index); |
| 440 |
|
| 441 |
for (const EFI_DEVICE_PATH *node = dp; !device_path_is_end(node); node = device_path_next_node(node)) |
| 442 |
if (node->Type == ACPI_DEVICE_PATH && |
| 443 |
node->SubType == ACPI_DP && |
| 444 |
node->Length >= sizeof(ACPI_HID_DEVICE_PATH)) { |
| 445 |
const ACPI_HID_DEVICE_PATH *acpi = (const ACPI_HID_DEVICE_PATH *) node; |
| 446 |
if (acpi->HID == EISA_PNP_ID(0x0501)) { |
| 447 |
*ret_index = acpi->UID; |
| 448 |
return EFI_SUCCESS; |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
return EFI_NOT_FOUND; |
| 453 |
} |
| 454 |
|
| 455 |
/* Check if the console output is a serial UART. If so, determine the COM port index from the |
| 456 |
* ACPI device path so we can pass the correct console= device to the kernel. */ |
| 457 |
static EFI_STATUS find_serial_console_index(uint32_t *ret_index) { |
| 458 |
assert(ret_index); |
| 459 |
|
| 460 |
/* First try the ConOut handle directly. */ |
| 461 |
EFI_DEVICE_PATH *dp = NULL; |
| 462 |
if (BS->HandleProtocol(ST->ConsoleOutHandle, MAKE_GUID_PTR(EFI_DEVICE_PATH_PROTOCOL), (void **) &dp) == EFI_SUCCESS) { |
| 463 |
_cleanup_free_ char16_t *dp_str = NULL; |
| 464 |
(void) device_path_to_str(dp, &dp_str); |
| 465 |
log_debug("ConOut device path: %ls", strempty(dp_str)); |
| 466 |
|
| 467 |
if (device_path_get_uart_index(dp, ret_index) == EFI_SUCCESS) { |
| 468 |
log_debug("ConOut is a serial console (port index %u).", *ret_index); |
| 469 |
return EFI_SUCCESS; |
| 470 |
} |
| 471 |
|
| 472 |
log_debug("ConOut device path does not contain a PNP0501 UART node."); |
| 473 |
return EFI_NOT_FOUND; |
| 474 |
} |
| 475 |
|
| 476 |
/* ConOut handle has no device path (e.g. ConSplitter virtual handle). Enumerate all |
| 477 |
* text output handles and check if any of them is a serial console. */ |
| 478 |
log_debug("ConOut handle has no device path, enumerating text output handles..."); |
| 479 |
|
| 480 |
_cleanup_free_ EFI_HANDLE *handles = NULL; |
| 481 |
size_t n_handles = 0; |
| 482 |
if (BS->LocateHandleBuffer( |
| 483 |
ByProtocol, |
| 484 |
MAKE_GUID_PTR(EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL), |
| 485 |
NULL, |
| 486 |
&n_handles, |
| 487 |
&handles) != EFI_SUCCESS) { |
| 488 |
log_debug("Failed to enumerate text output handles."); |
| 489 |
return EFI_NOT_FOUND; |
| 490 |
} |
| 491 |
|
| 492 |
bool found = false; |
| 493 |
|
| 494 |
for (size_t i = 0; i < n_handles; i++) { |
| 495 |
dp = NULL; |
| 496 |
if (BS->HandleProtocol(handles[i], MAKE_GUID_PTR(EFI_DEVICE_PATH_PROTOCOL), (void **) &dp) != EFI_SUCCESS) |
| 497 |
continue; |
| 498 |
|
| 499 |
_cleanup_free_ char16_t *dp_str = NULL; |
| 500 |
(void) device_path_to_str(dp, &dp_str); |
| 501 |
log_debug("Text output handle %zu device path: %ls", i, strempty(dp_str)); |
| 502 |
|
| 503 |
uint32_t index; |
| 504 |
if (device_path_get_uart_index(dp, &index) != EFI_SUCCESS) |
| 505 |
continue; |
| 506 |
|
| 507 |
log_debug("Text output handle %zu is a serial console (port index %u).", i, index); |
| 508 |
|
| 509 |
if (found && *ret_index != index) { |
| 510 |
log_debug("Multiple serial consoles with different port indices found, cannot determine which one to use."); |
| 511 |
return EFI_NOT_FOUND; |
| 512 |
} |
| 513 |
|
| 514 |
*ret_index = index; |
| 515 |
found = true; |
| 516 |
} |
| 517 |
|
| 518 |
if (!found) { |
| 519 |
log_debug("No serial console found among text output handles."); |
| 520 |
return EFI_NOT_FOUND; |
| 521 |
} |
| 522 |
|
| 523 |
return EFI_SUCCESS; |
| 524 |
} |
| 525 |
|
| 526 |
static const char16_t *serial_console_arg(uint32_t index) { |
| 527 |
/* Use the uart I/O port address format (see Documentation/admin-guide/kernel-parameters.txt) |
| 528 |
* instead of ttyS names. This addresses the 8250/16550 UART at the specified I/O port |
| 529 |
* directly and switches to the matching ttyS device later. The I/O port addresses for |
| 530 |
* the standard COM ports are fixed (see arch/x86/include/asm/serial.h), and the ACPI UID |
| 531 |
* for PNP0501 maps directly to the COM port index. */ |
| 532 |
static const char16_t *const table[] = { |
| 533 |
u"console=uart,io,0x3f8", /* COM1 */ |
| 534 |
u"console=uart,io,0x2f8", /* COM2 */ |
| 535 |
u"console=uart,io,0x3e8", /* COM3 */ |
| 536 |
u"console=uart,io,0x2e8", /* COM4 */ |
| 537 |
}; |
| 538 |
|
| 539 |
if (index >= ELEMENTSOF(table)) |
| 540 |
return NULL; |
| 541 |
|
| 542 |
return table[index]; |
| 543 |
} |
| 544 |
|
| 545 |
#endif /* __i386__ || __x86_64__ */ |
| 546 |
|
| 547 |
/* If there's no console= in the command line yet, try to detect the appropriate console device. |
| 548 |
* |
| 549 |
* Detection order: |
| 550 |
* 1. If exactly one VirtIO console PCI device exists -> console=hvc0 |
| 551 |
* 2. If there's graphical output (GOP) -> don't add console=, the kernel defaults are fine |
| 552 |
* 3. On x86, if exactly one serial console exists -> console=uart,io,<addr> |
| 553 |
* 4. Otherwise -> don't add console=, let the user handle it |
| 554 |
* |
| 555 |
* VirtIO console takes priority since it's explicitly configured by the VMM. Graphics is |
| 556 |
* checked before serial to avoid accidentally redirecting output away from a graphical |
| 557 |
* console by adding a serial console= argument. |
| 558 |
* |
| 559 |
* Serial console auto-detection is restricted to x86 where ACPI PNP0501 UIDs map to fixed |
| 560 |
* I/O port addresses for 8250/16550 UARTs. On non-x86 (e.g. ARM), serial device indices are |
| 561 |
* assigned dynamically, and the kernel has its own console auto-detection mechanisms |
| 562 |
* (DT stdout-path, etc.). |
| 563 |
* |
| 564 |
* Not TPM-measured because the value is deterministically derived from firmware-reported |
| 565 |
* hardware state (PCI device enumeration, GOP presence, serial device paths). */ |
| 566 |
void cmdline_append_console(char16_t **cmdline) { |
| 567 |
assert(cmdline); |
| 568 |
|
| 569 |
if (*cmdline && (efi_fnmatch(u"console=*", *cmdline) || efi_fnmatch(u"* console=*", *cmdline))) { |
| 570 |
log_debug("Kernel command line already contains console=, not adding one."); |
| 571 |
return; |
| 572 |
} |
| 573 |
|
| 574 |
const char16_t *console_arg = NULL; |
| 575 |
|
| 576 |
if (has_virtio_console_pci_device()) |
| 577 |
console_arg = u"console=hvc0"; |
| 578 |
else if (has_graphics_output()) { |
| 579 |
log_debug("Graphical output available, not adding console= to kernel command line."); |
| 580 |
return; |
| 581 |
} |
| 582 |
#if defined(__i386__) || defined(__x86_64__) |
| 583 |
else { |
| 584 |
uint32_t serial_index; |
| 585 |
if (find_serial_console_index(&serial_index) == EFI_SUCCESS) |
| 586 |
console_arg = serial_console_arg(serial_index); |
| 587 |
} |
| 588 |
#endif |
| 589 |
|
| 590 |
if (!console_arg) { |
| 591 |
log_debug("Cannot determine console type, not adding console= to kernel command line."); |
| 592 |
return; |
| 593 |
} |
| 594 |
|
| 595 |
log_debug("Appending %ls to kernel command line.", console_arg); |
| 596 |
|
| 597 |
_cleanup_free_ char16_t *old = TAKE_PTR(*cmdline); |
| 598 |
if (isempty(old)) |
| 599 |
*cmdline = xstrdup16(console_arg); |
| 600 |
else |
| 601 |
*cmdline = xasprintf("%ls %ls", old, console_arg); |
| 602 |
} |