2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2015-02-08 12:25:35 +01:00
|
|
|
|
|
|
|
#include <efi.h>
|
|
|
|
#include <efilib.h>
|
|
|
|
|
|
|
|
#include "console.h"
|
2015-07-29 19:26:49 +02:00
|
|
|
#include "disk.h"
|
2015-11-16 22:09:36 +01:00
|
|
|
#include "graphics.h"
|
2015-02-08 12:25:35 +01:00
|
|
|
#include "linux.h"
|
2016-02-03 17:39:24 +01:00
|
|
|
#include "measure.h"
|
2017-06-30 20:27:47 +02:00
|
|
|
#include "pe.h"
|
2017-05-09 20:57:40 +02:00
|
|
|
#include "shim.h"
|
2017-06-30 20:27:47 +02:00
|
|
|
#include "util.h"
|
2015-02-08 12:25:35 +01:00
|
|
|
|
|
|
|
#ifndef EFI_OS_INDICATIONS_BOOT_TO_FW_UI
|
|
|
|
#define EFI_OS_INDICATIONS_BOOT_TO_FW_UI 0x0000000000000001ULL
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* magic string to find in the binary image */
|
2017-04-03 18:15:33 +02:00
|
|
|
static const char __attribute__((used)) magic[] = "#### LoaderInfo: systemd-boot " PACKAGE_VERSION " ####";
|
2015-02-08 12:25:35 +01:00
|
|
|
|
|
|
|
static const EFI_GUID global_guid = EFI_GLOBAL_VARIABLE;
|
|
|
|
|
|
|
|
enum loader_type {
|
|
|
|
LOADER_UNDEFINED,
|
|
|
|
LOADER_EFI,
|
2018-06-20 18:25:01 +02:00
|
|
|
LOADER_LINUX,
|
2015-02-08 12:25:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
2018-06-20 18:49:10 +02:00
|
|
|
CHAR16 *id; /* The identifier for this entry (note that this id is not necessarily unique though!) */
|
2015-02-08 12:25:35 +01:00
|
|
|
CHAR16 *title_show;
|
|
|
|
CHAR16 *title;
|
|
|
|
CHAR16 *version;
|
|
|
|
CHAR16 *machine_id;
|
|
|
|
EFI_HANDLE *device;
|
|
|
|
enum loader_type type;
|
|
|
|
CHAR16 *loader;
|
|
|
|
CHAR16 *options;
|
|
|
|
CHAR16 key;
|
|
|
|
EFI_STATUS (*call)(VOID);
|
|
|
|
BOOLEAN no_autoselect;
|
|
|
|
BOOLEAN non_unique;
|
2018-06-20 18:25:01 +02:00
|
|
|
UINTN tries_done;
|
|
|
|
UINTN tries_left;
|
|
|
|
CHAR16 *path;
|
|
|
|
CHAR16 *current_name;
|
|
|
|
CHAR16 *next_name;
|
2015-02-08 12:25:35 +01:00
|
|
|
} ConfigEntry;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
ConfigEntry **entries;
|
|
|
|
UINTN entry_count;
|
|
|
|
INTN idx_default;
|
|
|
|
INTN idx_default_efivar;
|
|
|
|
UINTN timeout_sec;
|
|
|
|
UINTN timeout_sec_config;
|
|
|
|
INTN timeout_sec_efivar;
|
|
|
|
CHAR16 *entry_default_pattern;
|
|
|
|
CHAR16 *entry_oneshot;
|
|
|
|
CHAR16 *options_edit;
|
2018-01-28 17:06:22 +01:00
|
|
|
BOOLEAN editor;
|
|
|
|
BOOLEAN auto_entries;
|
|
|
|
BOOLEAN auto_firmware;
|
2017-12-16 19:23:06 +01:00
|
|
|
UINTN console_mode;
|
|
|
|
enum console_mode_change_type console_mode_change;
|
2015-02-08 12:25:35 +01:00
|
|
|
} Config;
|
|
|
|
|
2015-08-27 17:48:24 +02:00
|
|
|
static VOID cursor_left(UINTN *cursor, UINTN *first) {
|
2015-02-08 12:25:35 +01:00
|
|
|
if ((*cursor) > 0)
|
|
|
|
(*cursor)--;
|
|
|
|
else if ((*first) > 0)
|
|
|
|
(*first)--;
|
|
|
|
}
|
|
|
|
|
2018-06-20 18:45:58 +02:00
|
|
|
static VOID cursor_right(
|
|
|
|
UINTN *cursor,
|
|
|
|
UINTN *first,
|
|
|
|
UINTN x_max,
|
|
|
|
UINTN len) {
|
|
|
|
|
2015-02-08 12:25:35 +01:00
|
|
|
if ((*cursor)+1 < x_max)
|
|
|
|
(*cursor)++;
|
|
|
|
else if ((*first) + (*cursor) < len)
|
|
|
|
(*first)++;
|
|
|
|
}
|
|
|
|
|
2018-06-20 18:45:58 +02:00
|
|
|
static BOOLEAN line_edit(
|
|
|
|
CHAR16 *line_in,
|
|
|
|
CHAR16 **line_out,
|
|
|
|
UINTN x_max,
|
|
|
|
UINTN y_pos) {
|
|
|
|
|
2018-03-13 10:47:17 +01:00
|
|
|
_cleanup_freepool_ CHAR16 *line = NULL, *print = NULL;
|
|
|
|
UINTN size, len, first, cursor, clear;
|
|
|
|
BOOLEAN exit, enter;
|
2015-02-08 12:25:35 +01:00
|
|
|
|
|
|
|
if (!line_in)
|
|
|
|
line_in = L"";
|
|
|
|
size = StrLen(line_in) + 1024;
|
|
|
|
line = AllocatePool(size * sizeof(CHAR16));
|
|
|
|
StrCpy(line, line_in);
|
|
|
|
len = StrLen(line);
|
|
|
|
print = AllocatePool((x_max+1) * sizeof(CHAR16));
|
|
|
|
|
|
|
|
uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, TRUE);
|
|
|
|
|
|
|
|
first = 0;
|
|
|
|
cursor = 0;
|
|
|
|
clear = 0;
|
|
|
|
enter = FALSE;
|
|
|
|
exit = FALSE;
|
|
|
|
while (!exit) {
|
|
|
|
EFI_STATUS err;
|
|
|
|
UINT64 key;
|
|
|
|
UINTN i;
|
|
|
|
|
|
|
|
i = len - first;
|
|
|
|
if (i >= x_max-1)
|
|
|
|
i = x_max-1;
|
|
|
|
CopyMem(print, line + first, i * sizeof(CHAR16));
|
|
|
|
while (clear > 0 && i < x_max-1) {
|
|
|
|
clear--;
|
|
|
|
print[i++] = ' ';
|
|
|
|
}
|
|
|
|
print[i] = '\0';
|
|
|
|
|
|
|
|
uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_pos);
|
|
|
|
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, print);
|
|
|
|
uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
|
|
|
|
|
|
|
|
err = console_key_read(&key, TRUE);
|
|
|
|
if (EFI_ERROR(err))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch (key) {
|
|
|
|
case KEYPRESS(0, SCAN_ESC, 0):
|
|
|
|
case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'c'):
|
|
|
|
case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'g'):
|
|
|
|
case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('c')):
|
|
|
|
case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('g')):
|
|
|
|
exit = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEYPRESS(0, SCAN_HOME, 0):
|
|
|
|
case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'a'):
|
|
|
|
case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('a')):
|
|
|
|
/* beginning-of-line */
|
|
|
|
cursor = 0;
|
|
|
|
first = 0;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case KEYPRESS(0, SCAN_END, 0):
|
|
|
|
case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'e'):
|
|
|
|
case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('e')):
|
|
|
|
/* end-of-line */
|
|
|
|
cursor = len - first;
|
|
|
|
if (cursor+1 >= x_max) {
|
|
|
|
cursor = x_max-1;
|
|
|
|
first = len - (x_max-1);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case KEYPRESS(0, SCAN_DOWN, 0):
|
|
|
|
case KEYPRESS(EFI_ALT_PRESSED, 0, 'f'):
|
|
|
|
case KEYPRESS(EFI_CONTROL_PRESSED, SCAN_RIGHT, 0):
|
|
|
|
/* forward-word */
|
2017-12-25 05:38:49 +01:00
|
|
|
while (line[first + cursor] == ' ')
|
2015-02-08 12:25:35 +01:00
|
|
|
cursor_right(&cursor, &first, x_max, len);
|
|
|
|
while (line[first + cursor] && line[first + cursor] != ' ')
|
|
|
|
cursor_right(&cursor, &first, x_max, len);
|
|
|
|
uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case KEYPRESS(0, SCAN_UP, 0):
|
|
|
|
case KEYPRESS(EFI_ALT_PRESSED, 0, 'b'):
|
|
|
|
case KEYPRESS(EFI_CONTROL_PRESSED, SCAN_LEFT, 0):
|
|
|
|
/* backward-word */
|
|
|
|
if ((first + cursor) > 0 && line[first + cursor-1] == ' ') {
|
|
|
|
cursor_left(&cursor, &first);
|
|
|
|
while ((first + cursor) > 0 && line[first + cursor] == ' ')
|
|
|
|
cursor_left(&cursor, &first);
|
|
|
|
}
|
|
|
|
while ((first + cursor) > 0 && line[first + cursor-1] != ' ')
|
|
|
|
cursor_left(&cursor, &first);
|
|
|
|
uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case KEYPRESS(0, SCAN_RIGHT, 0):
|
|
|
|
case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'f'):
|
|
|
|
case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('f')):
|
|
|
|
/* forward-char */
|
|
|
|
if (first + cursor == len)
|
|
|
|
continue;
|
|
|
|
cursor_right(&cursor, &first, x_max, len);
|
|
|
|
uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case KEYPRESS(0, SCAN_LEFT, 0):
|
|
|
|
case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'b'):
|
|
|
|
case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('b')):
|
|
|
|
/* backward-char */
|
|
|
|
cursor_left(&cursor, &first);
|
|
|
|
uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case KEYPRESS(EFI_ALT_PRESSED, 0, 'd'):
|
|
|
|
/* kill-word */
|
|
|
|
clear = 0;
|
|
|
|
for (i = first + cursor; i < len && line[i] == ' '; i++)
|
|
|
|
clear++;
|
|
|
|
for (; i < len && line[i] != ' '; i++)
|
|
|
|
clear++;
|
|
|
|
|
|
|
|
for (i = first + cursor; i + clear < len; i++)
|
|
|
|
line[i] = line[i + clear];
|
|
|
|
len -= clear;
|
|
|
|
line[len] = '\0';
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'w'):
|
|
|
|
case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('w')):
|
|
|
|
case KEYPRESS(EFI_ALT_PRESSED, 0, CHAR_BACKSPACE):
|
|
|
|
/* backward-kill-word */
|
|
|
|
clear = 0;
|
|
|
|
if ((first + cursor) > 0 && line[first + cursor-1] == ' ') {
|
|
|
|
cursor_left(&cursor, &first);
|
|
|
|
clear++;
|
|
|
|
while ((first + cursor) > 0 && line[first + cursor] == ' ') {
|
|
|
|
cursor_left(&cursor, &first);
|
|
|
|
clear++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while ((first + cursor) > 0 && line[first + cursor-1] != ' ') {
|
|
|
|
cursor_left(&cursor, &first);
|
|
|
|
clear++;
|
|
|
|
}
|
|
|
|
uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos);
|
|
|
|
|
|
|
|
for (i = first + cursor; i + clear < len; i++)
|
|
|
|
line[i] = line[i + clear];
|
|
|
|
len -= clear;
|
|
|
|
line[len] = '\0';
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case KEYPRESS(0, SCAN_DELETE, 0):
|
|
|
|
case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'd'):
|
|
|
|
case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('d')):
|
|
|
|
if (len == 0)
|
|
|
|
continue;
|
|
|
|
if (first + cursor == len)
|
|
|
|
continue;
|
|
|
|
for (i = first + cursor; i < len; i++)
|
|
|
|
line[i] = line[i+1];
|
|
|
|
clear = 1;
|
|
|
|
len--;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'k'):
|
|
|
|
case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('k')):
|
|
|
|
/* kill-line */
|
|
|
|
line[first + cursor] = '\0';
|
|
|
|
clear = len - (first + cursor);
|
|
|
|
len = first + cursor;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case KEYPRESS(0, 0, CHAR_LINEFEED):
|
|
|
|
case KEYPRESS(0, 0, CHAR_CARRIAGE_RETURN):
|
|
|
|
if (StrCmp(line, line_in) != 0) {
|
|
|
|
*line_out = line;
|
|
|
|
line = NULL;
|
|
|
|
}
|
|
|
|
enter = TRUE;
|
|
|
|
exit = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEYPRESS(0, 0, CHAR_BACKSPACE):
|
|
|
|
if (len == 0)
|
|
|
|
continue;
|
|
|
|
if (first == 0 && cursor == 0)
|
|
|
|
continue;
|
|
|
|
for (i = first + cursor-1; i < len; i++)
|
|
|
|
line[i] = line[i+1];
|
|
|
|
clear = 1;
|
|
|
|
len--;
|
|
|
|
if (cursor > 0)
|
|
|
|
cursor--;
|
|
|
|
if (cursor > 0 || first == 0)
|
|
|
|
continue;
|
|
|
|
/* show full line if it fits */
|
|
|
|
if (len < x_max) {
|
|
|
|
cursor = first;
|
|
|
|
first = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* jump left to see what we delete */
|
|
|
|
if (first > 10) {
|
|
|
|
first -= 10;
|
|
|
|
cursor = 10;
|
|
|
|
} else {
|
|
|
|
cursor = first;
|
|
|
|
first = 0;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case KEYPRESS(0, 0, ' ') ... KEYPRESS(0, 0, '~'):
|
|
|
|
case KEYPRESS(0, 0, 0x80) ... KEYPRESS(0, 0, 0xffff):
|
|
|
|
if (len+1 == size)
|
|
|
|
continue;
|
|
|
|
for (i = len; i > first + cursor; i--)
|
|
|
|
line[i] = line[i-1];
|
|
|
|
line[first + cursor] = KEYCHAR(key);
|
|
|
|
len++;
|
|
|
|
line[len] = '\0';
|
|
|
|
if (cursor+1 < x_max)
|
|
|
|
cursor++;
|
|
|
|
else if (first + cursor < len)
|
|
|
|
first++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, FALSE);
|
|
|
|
return enter;
|
|
|
|
}
|
|
|
|
|
|
|
|
static UINTN entry_lookup_key(Config *config, UINTN start, CHAR16 key) {
|
|
|
|
UINTN i;
|
|
|
|
|
|
|
|
if (key == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* select entry by number key */
|
|
|
|
if (key >= '1' && key <= '9') {
|
|
|
|
i = key - '0';
|
|
|
|
if (i > config->entry_count)
|
|
|
|
i = config->entry_count;
|
|
|
|
return i-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find matching key in config entries */
|
|
|
|
for (i = start; i < config->entry_count; i++)
|
|
|
|
if (config->entries[i]->key == key)
|
|
|
|
return i;
|
|
|
|
|
|
|
|
for (i = 0; i < start; i++)
|
|
|
|
if (config->entries[i]->key == key)
|
|
|
|
return i;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-02-26 19:51:08 +01:00
|
|
|
static VOID print_status(Config *config, CHAR16 *loaded_image_path) {
|
2015-02-08 12:25:35 +01:00
|
|
|
UINT64 key;
|
|
|
|
UINTN i;
|
2018-03-13 10:47:17 +01:00
|
|
|
_cleanup_freepool_ CHAR8 *bootvar = NULL, *modevar = NULL, *indvar = NULL;
|
|
|
|
_cleanup_freepool_ CHAR16 *partstr = NULL, *defaultstr = NULL;
|
|
|
|
UINTN x, y, size;
|
2015-02-08 12:25:35 +01:00
|
|
|
|
|
|
|
uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
|
|
|
|
uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
|
|
|
|
|
2017-04-03 18:15:33 +02:00
|
|
|
Print(L"systemd-boot version: " PACKAGE_VERSION "\n");
|
2015-02-08 12:25:35 +01:00
|
|
|
Print(L"architecture: " EFI_MACHINE_TYPE_NAME "\n");
|
|
|
|
Print(L"loaded image: %s\n", loaded_image_path);
|
|
|
|
Print(L"UEFI specification: %d.%02d\n", ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff);
|
|
|
|
Print(L"firmware vendor: %s\n", ST->FirmwareVendor);
|
|
|
|
Print(L"firmware version: %d.%02d\n", ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
|
|
|
|
|
|
|
|
if (uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut, ST->ConOut->Mode->Mode, &x, &y) == EFI_SUCCESS)
|
|
|
|
Print(L"console size: %d x %d\n", x, y);
|
|
|
|
|
2018-03-13 10:47:17 +01:00
|
|
|
if (efivar_get_raw(&global_guid, L"SecureBoot", &bootvar, &size) == EFI_SUCCESS)
|
|
|
|
Print(L"SecureBoot: %s\n", yes_no(*bootvar > 0));
|
2015-02-08 12:25:35 +01:00
|
|
|
|
2018-03-13 10:47:17 +01:00
|
|
|
if (efivar_get_raw(&global_guid, L"SetupMode", &modevar, &size) == EFI_SUCCESS)
|
|
|
|
Print(L"SetupMode: %s\n", *modevar > 0 ? L"setup" : L"user");
|
2015-02-08 12:25:35 +01:00
|
|
|
|
2017-05-09 20:57:40 +02:00
|
|
|
if (shim_loaded())
|
|
|
|
Print(L"Shim: present\n");
|
|
|
|
|
2018-03-13 10:47:17 +01:00
|
|
|
if (efivar_get_raw(&global_guid, L"OsIndicationsSupported", &indvar, &size) == EFI_SUCCESS)
|
|
|
|
Print(L"OsIndicationsSupported: %d\n", (UINT64)*indvar);
|
2018-01-28 17:06:22 +01:00
|
|
|
|
|
|
|
Print(L"\n--- press key ---\n\n");
|
|
|
|
console_key_read(&key, TRUE);
|
2015-02-08 12:25:35 +01:00
|
|
|
|
|
|
|
Print(L"timeout: %d\n", config->timeout_sec);
|
|
|
|
if (config->timeout_sec_efivar >= 0)
|
|
|
|
Print(L"timeout (EFI var): %d\n", config->timeout_sec_efivar);
|
|
|
|
Print(L"timeout (config): %d\n", config->timeout_sec_config);
|
|
|
|
if (config->entry_default_pattern)
|
|
|
|
Print(L"default pattern: '%s'\n", config->entry_default_pattern);
|
2018-01-28 17:06:22 +01:00
|
|
|
Print(L"editor: %s\n", yes_no(config->editor));
|
|
|
|
Print(L"auto-entries: %s\n", yes_no(config->auto_entries));
|
|
|
|
Print(L"auto-firmware: %s\n", yes_no(config->auto_firmware));
|
2015-02-08 12:25:35 +01:00
|
|
|
Print(L"\n");
|
|
|
|
|
|
|
|
Print(L"config entry count: %d\n", config->entry_count);
|
|
|
|
Print(L"entry selected idx: %d\n", config->idx_default);
|
|
|
|
if (config->idx_default_efivar >= 0)
|
|
|
|
Print(L"entry EFI var idx: %d\n", config->idx_default_efivar);
|
|
|
|
Print(L"\n");
|
|
|
|
|
|
|
|
if (efivar_get_int(L"LoaderConfigTimeout", &i) == EFI_SUCCESS)
|
|
|
|
Print(L"LoaderConfigTimeout: %d\n", i);
|
|
|
|
if (config->entry_oneshot)
|
|
|
|
Print(L"LoaderEntryOneShot: %s\n", config->entry_oneshot);
|
2018-03-13 10:47:17 +01:00
|
|
|
if (efivar_get(L"LoaderDevicePartUUID", &partstr) == EFI_SUCCESS)
|
|
|
|
Print(L"LoaderDevicePartUUID: %s\n", partstr);
|
|
|
|
if (efivar_get(L"LoaderEntryDefault", &defaultstr) == EFI_SUCCESS)
|
|
|
|
Print(L"LoaderEntryDefault: %s\n", defaultstr);
|
2015-02-08 12:25:35 +01:00
|
|
|
|
|
|
|
Print(L"\n--- press key ---\n\n");
|
|
|
|
console_key_read(&key, TRUE);
|
|
|
|
|
|
|
|
for (i = 0; i < config->entry_count; i++) {
|
|
|
|
ConfigEntry *entry;
|
|
|
|
|
|
|
|
if (key == KEYPRESS(0, SCAN_ESC, 0) || key == KEYPRESS(0, 0, 'q'))
|
|
|
|
break;
|
|
|
|
|
|
|
|
entry = config->entries[i];
|
|
|
|
Print(L"config entry: %d/%d\n", i+1, config->entry_count);
|
2018-06-20 18:49:10 +02:00
|
|
|
if (entry->id)
|
|
|
|
Print(L"id '%s'\n", entry->id);
|
2015-02-08 12:25:35 +01:00
|
|
|
Print(L"title show '%s'\n", entry->title_show);
|
|
|
|
if (entry->title)
|
|
|
|
Print(L"title '%s'\n", entry->title);
|
|
|
|
if (entry->version)
|
|
|
|
Print(L"version '%s'\n", entry->version);
|
|
|
|
if (entry->machine_id)
|
|
|
|
Print(L"machine-id '%s'\n", entry->machine_id);
|
|
|
|
if (entry->device) {
|
|
|
|
EFI_DEVICE_PATH *device_path;
|
|
|
|
|
|
|
|
device_path = DevicePathFromHandle(entry->device);
|
|
|
|
if (device_path) {
|
2018-03-13 10:47:17 +01:00
|
|
|
_cleanup_freepool_ CHAR16 *str;
|
|
|
|
|
2015-02-08 12:25:35 +01:00
|
|
|
str = DevicePathToStr(device_path);
|
|
|
|
Print(L"device handle '%s'\n", str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (entry->loader)
|
|
|
|
Print(L"loader '%s'\n", entry->loader);
|
|
|
|
if (entry->options)
|
|
|
|
Print(L"options '%s'\n", entry->options);
|
2015-03-11 23:26:48 +01:00
|
|
|
Print(L"auto-select %s\n", yes_no(!entry->no_autoselect));
|
2015-02-08 12:25:35 +01:00
|
|
|
if (entry->call)
|
|
|
|
Print(L"internal call yes\n");
|
|
|
|
|
2018-06-20 18:25:01 +02:00
|
|
|
if (entry->tries_left != (UINTN) -1)
|
|
|
|
Print(L"counting boots yes\n"
|
|
|
|
"tries done %u\n"
|
|
|
|
"tries left %u\n"
|
|
|
|
"current path %s\\%s\n"
|
|
|
|
"next path %s\\%s\n",
|
|
|
|
entry->tries_done,
|
|
|
|
entry->tries_left,
|
|
|
|
entry->path, entry->current_name,
|
|
|
|
entry->path, entry->next_name);
|
|
|
|
|
2015-02-08 12:25:35 +01:00
|
|
|
Print(L"\n--- press key ---\n\n");
|
|
|
|
console_key_read(&key, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
|
|
|
|
}
|
|
|
|
|
2018-06-20 18:45:58 +02:00
|
|
|
static BOOLEAN menu_run(
|
|
|
|
Config *config,
|
|
|
|
ConfigEntry **chosen_entry,
|
|
|
|
CHAR16 *loaded_image_path) {
|
|
|
|
|
2015-02-08 12:25:35 +01:00
|
|
|
EFI_STATUS err;
|
|
|
|
UINTN visible_max;
|
|
|
|
UINTN idx_highlight;
|
|
|
|
UINTN idx_highlight_prev;
|
|
|
|
UINTN idx_first;
|
|
|
|
UINTN idx_last;
|
|
|
|
BOOLEAN refresh;
|
|
|
|
BOOLEAN highlight;
|
|
|
|
UINTN i;
|
|
|
|
UINTN line_width;
|
|
|
|
CHAR16 **lines;
|
|
|
|
UINTN x_start;
|
|
|
|
UINTN y_start;
|
|
|
|
UINTN x_max;
|
|
|
|
UINTN y_max;
|
|
|
|
CHAR16 *status;
|
|
|
|
CHAR16 *clearline;
|
|
|
|
INTN timeout_remain;
|
|
|
|
INT16 idx;
|
|
|
|
BOOLEAN exit = FALSE;
|
|
|
|
BOOLEAN run = TRUE;
|
|
|
|
BOOLEAN wait = FALSE;
|
2017-12-16 19:23:06 +01:00
|
|
|
BOOLEAN cleared_screen = FALSE;
|
2015-02-08 12:25:35 +01:00
|
|
|
|
|
|
|
graphics_mode(FALSE);
|
|
|
|
uefi_call_wrapper(ST->ConIn->Reset, 2, ST->ConIn, FALSE);
|
|
|
|
uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, FALSE);
|
|
|
|
uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
|
|
|
|
|
|
|
|
/* draw a single character to make ClearScreen work on some firmware */
|
|
|
|
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L" ");
|
2017-12-16 19:23:06 +01:00
|
|
|
|
|
|
|
if (config->console_mode_change != CONSOLE_MODE_KEEP) {
|
|
|
|
err = console_set_mode(&config->console_mode, config->console_mode_change);
|
|
|
|
if (!EFI_ERROR(err))
|
|
|
|
cleared_screen = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cleared_screen)
|
|
|
|
uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
|
|
|
|
|
|
|
|
if (config->console_mode_change != CONSOLE_MODE_KEEP && EFI_ERROR(err))
|
|
|
|
Print(L"Error switching console mode to %ld: %r.\r", (UINT64)config->console_mode, err);
|
2015-02-08 12:25:35 +01:00
|
|
|
|
|
|
|
err = uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut, ST->ConOut->Mode->Mode, &x_max, &y_max);
|
|
|
|
if (EFI_ERROR(err)) {
|
|
|
|
x_max = 80;
|
|
|
|
y_max = 25;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we check 10 times per second for a keystroke */
|
|
|
|
if (config->timeout_sec > 0)
|
|
|
|
timeout_remain = config->timeout_sec * 10;
|
|
|
|
else
|
|
|
|
timeout_remain = -1;
|
|
|
|
|
|
|
|
idx_highlight = config->idx_default;
|
|
|
|
idx_highlight_prev = 0;
|
|
|
|
|
|
|
|
visible_max = y_max - 2;
|
|
|
|
|
|
|
|
if ((UINTN)config->idx_default >= visible_max)
|
|
|
|
idx_first = config->idx_default-1;
|
|
|
|
else
|
|
|
|
idx_first = 0;
|
|
|
|
|
|
|
|
idx_last = idx_first + visible_max-1;
|
|
|
|
|
|
|
|
refresh = TRUE;
|
|
|
|
highlight = FALSE;
|
|
|
|
|
|
|
|
/* length of the longest entry */
|
|
|
|
line_width = 5;
|
|
|
|
for (i = 0; i < config->entry_count; i++) {
|
|
|
|
UINTN entry_len;
|
|
|
|
|
|
|
|
entry_len = StrLen(config->entries[i]->title_show);
|
|
|
|
if (line_width < entry_len)
|
|
|
|
line_width = entry_len;
|
|
|
|
}
|
|
|
|
if (line_width > x_max-6)
|
|
|
|
line_width = x_max-6;
|
|
|
|
|
|
|
|
/* offsets to center the entries on the screen */
|
|
|
|
x_start = (x_max - (line_width)) / 2;
|
|
|
|
if (config->entry_count < visible_max)
|
|
|
|
y_start = ((visible_max - config->entry_count) / 2) + 1;
|
|
|
|
else
|
|
|
|
y_start = 0;
|
|
|
|
|
|
|
|
/* menu entries title lines */
|
|
|
|
lines = AllocatePool(sizeof(CHAR16 *) * config->entry_count);
|
|
|
|
for (i = 0; i < config->entry_count; i++) {
|
|
|
|
UINTN j, k;
|
|
|
|
|
|
|
|
lines[i] = AllocatePool(((x_max+1) * sizeof(CHAR16)));
|
|
|
|
for (j = 0; j < x_start; j++)
|
|
|
|
lines[i][j] = ' ';
|
|
|
|
|
|
|
|
for (k = 0; config->entries[i]->title_show[k] != '\0' && j < x_max; j++, k++)
|
|
|
|
lines[i][j] = config->entries[i]->title_show[k];
|
|
|
|
|
|
|
|
for (; j < x_max; j++)
|
|
|
|
lines[i][j] = ' ';
|
|
|
|
lines[i][x_max] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
status = NULL;
|
|
|
|
clearline = AllocatePool((x_max+1) * sizeof(CHAR16));
|
|
|
|
for (i = 0; i < x_max; i++)
|
|
|
|
clearline[i] = ' ';
|
|
|
|
clearline[i] = 0;
|
|
|
|
|
|
|
|
while (!exit) {
|
|
|
|
UINT64 key;
|
|
|
|
|
|
|
|
if (refresh) {
|
|
|
|
for (i = 0; i < config->entry_count; i++) {
|
|
|
|
if (i < idx_first || i > idx_last)
|
|
|
|
continue;
|
|
|
|
uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_start + i - idx_first);
|
|
|
|
if (i == idx_highlight)
|
|
|
|
uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut,
|
|
|
|
EFI_BLACK|EFI_BACKGROUND_LIGHTGRAY);
|
|
|
|
else
|
|
|
|
uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut,
|
|
|
|
EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
|
|
|
|
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, lines[i]);
|
|
|
|
if ((INTN)i == config->idx_default_efivar) {
|
|
|
|
uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, x_start-3, y_start + i - idx_first);
|
|
|
|
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"=>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
refresh = FALSE;
|
|
|
|
} else if (highlight) {
|
|
|
|
uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_start + idx_highlight_prev - idx_first);
|
|
|
|
uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
|
|
|
|
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, lines[idx_highlight_prev]);
|
|
|
|
if ((INTN)idx_highlight_prev == config->idx_default_efivar) {
|
|
|
|
uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, x_start-3, y_start + idx_highlight_prev - idx_first);
|
|
|
|
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"=>");
|
|
|
|
}
|
|
|
|
|
|
|
|
uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_start + idx_highlight - idx_first);
|
|
|
|
uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_BLACK|EFI_BACKGROUND_LIGHTGRAY);
|
|
|
|
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, lines[idx_highlight]);
|
|
|
|
if ((INTN)idx_highlight == config->idx_default_efivar) {
|
|
|
|
uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, x_start-3, y_start + idx_highlight - idx_first);
|
|
|
|
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"=>");
|
|
|
|
}
|
|
|
|
highlight = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timeout_remain > 0) {
|
|
|
|
FreePool(status);
|
|
|
|
status = PoolPrint(L"Boot in %d sec.", (timeout_remain + 5) / 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* print status at last line of screen */
|
|
|
|
if (status) {
|
|
|
|
UINTN len;
|
|
|
|
UINTN x;
|
|
|
|
|
|
|
|
/* center line */
|
|
|
|
len = StrLen(status);
|
|
|
|
if (len < x_max)
|
|
|
|
x = (x_max - len) / 2;
|
|
|
|
else
|
|
|
|
x = 0;
|
|
|
|
uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
|
|
|
|
uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1);
|
|
|
|
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline + (x_max - x));
|
|
|
|
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, status);
|
|
|
|
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1 + x + len);
|
|
|
|
}
|
|
|
|
|
|
|
|
err = console_key_read(&key, wait);
|
|
|
|
if (EFI_ERROR(err)) {
|
|
|
|
/* timeout reached */
|
|
|
|
if (timeout_remain == 0) {
|
|
|
|
exit = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sleep and update status */
|
|
|
|
if (timeout_remain > 0) {
|
|
|
|
uefi_call_wrapper(BS->Stall, 1, 100 * 1000);
|
|
|
|
timeout_remain--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* timeout disabled, wait for next key */
|
|
|
|
wait = TRUE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout_remain = -1;
|
|
|
|
|
|
|
|
/* clear status after keystroke */
|
|
|
|
if (status) {
|
|
|
|
FreePool(status);
|
|
|
|
status = NULL;
|
|
|
|
uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
|
|
|
|
uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1);
|
|
|
|
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
idx_highlight_prev = idx_highlight;
|
|
|
|
|
|
|
|
switch (key) {
|
|
|
|
case KEYPRESS(0, SCAN_UP, 0):
|
|
|
|
case KEYPRESS(0, 0, 'k'):
|
|
|
|
if (idx_highlight > 0)
|
|
|
|
idx_highlight--;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEYPRESS(0, SCAN_DOWN, 0):
|
|
|
|
case KEYPRESS(0, 0, 'j'):
|
|
|
|
if (idx_highlight < config->entry_count-1)
|
|
|
|
idx_highlight++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEYPRESS(0, SCAN_HOME, 0):
|
|
|
|
case KEYPRESS(EFI_ALT_PRESSED, 0, '<'):
|
|
|
|
if (idx_highlight > 0) {
|
|
|
|
refresh = TRUE;
|
|
|
|
idx_highlight = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEYPRESS(0, SCAN_END, 0):
|
|
|
|
case KEYPRESS(EFI_ALT_PRESSED, 0, '>'):
|
|
|
|
if (idx_highlight < config->entry_count-1) {
|
|
|
|
refresh = TRUE;
|
|
|
|
idx_highlight = config->entry_count-1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEYPRESS(0, SCAN_PAGE_UP, 0):
|
|
|
|
if (idx_highlight > visible_max)
|
|
|
|
idx_highlight -= visible_max;
|
|
|
|
else
|
|
|
|
idx_highlight = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEYPRESS(0, SCAN_PAGE_DOWN, 0):
|
|
|
|
idx_highlight += visible_max;
|
|
|
|
if (idx_highlight > config->entry_count-1)
|
|
|
|
idx_highlight = config->entry_count-1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEYPRESS(0, 0, CHAR_LINEFEED):
|
|
|
|
case KEYPRESS(0, 0, CHAR_CARRIAGE_RETURN):
|
|
|
|
exit = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEYPRESS(0, SCAN_F1, 0):
|
|
|
|
case KEYPRESS(0, 0, 'h'):
|
|
|
|
case KEYPRESS(0, 0, '?'):
|
|
|
|
status = StrDuplicate(L"(d)efault, (t/T)timeout, (e)dit, (v)ersion (Q)uit (P)rint (h)elp");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEYPRESS(0, 0, 'Q'):
|
|
|
|
exit = TRUE;
|
|
|
|
run = FALSE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEYPRESS(0, 0, 'd'):
|
|
|
|
if (config->idx_default_efivar != (INTN)idx_highlight) {
|
|
|
|
/* store the selected entry in a persistent EFI variable */
|
2018-06-20 18:49:10 +02:00
|
|
|
efivar_set(L"LoaderEntryDefault", config->entries[idx_highlight]->id, TRUE);
|
2015-02-08 12:25:35 +01:00
|
|
|
config->idx_default_efivar = idx_highlight;
|
|
|
|
status = StrDuplicate(L"Default boot entry selected.");
|
|
|
|
} else {
|
|
|
|
/* clear the default entry EFI variable */
|
|
|
|
efivar_set(L"LoaderEntryDefault", NULL, TRUE);
|
|
|
|
config->idx_default_efivar = -1;
|
|
|
|
status = StrDuplicate(L"Default boot entry cleared.");
|
|
|
|
}
|
|
|
|
refresh = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEYPRESS(0, 0, '-'):
|
|
|
|
case KEYPRESS(0, 0, 'T'):
|
|
|
|
if (config->timeout_sec_efivar > 0) {
|
|
|
|
config->timeout_sec_efivar--;
|
|
|
|
efivar_set_int(L"LoaderConfigTimeout", config->timeout_sec_efivar, TRUE);
|
|
|
|
if (config->timeout_sec_efivar > 0)
|
|
|
|
status = PoolPrint(L"Menu timeout set to %d sec.", config->timeout_sec_efivar);
|
|
|
|
else
|
|
|
|
status = StrDuplicate(L"Menu disabled. Hold down key at bootup to show menu.");
|
|
|
|
} else if (config->timeout_sec_efivar <= 0){
|
|
|
|
config->timeout_sec_efivar = -1;
|
|
|
|
efivar_set(L"LoaderConfigTimeout", NULL, TRUE);
|
|
|
|
if (config->timeout_sec_config > 0)
|
|
|
|
status = PoolPrint(L"Menu timeout of %d sec is defined by configuration file.",
|
|
|
|
config->timeout_sec_config);
|
|
|
|
else
|
|
|
|
status = StrDuplicate(L"Menu disabled. Hold down key at bootup to show menu.");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEYPRESS(0, 0, '+'):
|
|
|
|
case KEYPRESS(0, 0, 't'):
|
|
|
|
if (config->timeout_sec_efivar == -1 && config->timeout_sec_config == 0)
|
|
|
|
config->timeout_sec_efivar++;
|
|
|
|
config->timeout_sec_efivar++;
|
|
|
|
efivar_set_int(L"LoaderConfigTimeout", config->timeout_sec_efivar, TRUE);
|
|
|
|
if (config->timeout_sec_efivar > 0)
|
|
|
|
status = PoolPrint(L"Menu timeout set to %d sec.",
|
|
|
|
config->timeout_sec_efivar);
|
|
|
|
else
|
|
|
|
status = StrDuplicate(L"Menu disabled. Hold down key at bootup to show menu.");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEYPRESS(0, 0, 'e'):
|
|
|
|
/* only the options of configured entries can be edited */
|
2018-01-28 17:06:22 +01:00
|
|
|
if (!config->editor || config->entries[idx_highlight]->type == LOADER_UNDEFINED)
|
2015-02-08 12:25:35 +01:00
|
|
|
break;
|
|
|
|
uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
|
|
|
|
uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1);
|
|
|
|
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1);
|
|
|
|
if (line_edit(config->entries[idx_highlight]->options, &config->options_edit, x_max-1, y_max-1))
|
|
|
|
exit = TRUE;
|
|
|
|
uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1);
|
|
|
|
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEYPRESS(0, 0, 'v'):
|
2017-04-03 18:15:33 +02:00
|
|
|
status = PoolPrint(L"systemd-boot " PACKAGE_VERSION " (" EFI_MACHINE_TYPE_NAME "), UEFI Specification %d.%02d, Vendor %s %d.%02d",
|
2015-02-08 12:25:35 +01:00
|
|
|
ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff,
|
|
|
|
ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEYPRESS(0, 0, 'P'):
|
2015-02-26 19:51:08 +01:00
|
|
|
print_status(config, loaded_image_path);
|
2015-02-08 12:25:35 +01:00
|
|
|
refresh = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'l'):
|
|
|
|
case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('l')):
|
|
|
|
refresh = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* jump with a hotkey directly to a matching entry */
|
|
|
|
idx = entry_lookup_key(config, idx_highlight+1, KEYCHAR(key));
|
|
|
|
if (idx < 0)
|
|
|
|
break;
|
|
|
|
idx_highlight = idx;
|
|
|
|
refresh = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (idx_highlight > idx_last) {
|
|
|
|
idx_last = idx_highlight;
|
|
|
|
idx_first = 1 + idx_highlight - visible_max;
|
|
|
|
refresh = TRUE;
|
2015-03-09 20:12:17 +01:00
|
|
|
} else if (idx_highlight < idx_first) {
|
2015-02-08 12:25:35 +01:00
|
|
|
idx_first = idx_highlight;
|
|
|
|
idx_last = idx_highlight + visible_max-1;
|
|
|
|
refresh = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!refresh && idx_highlight != idx_highlight_prev)
|
|
|
|
highlight = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*chosen_entry = config->entries[idx_highlight];
|
|
|
|
|
|
|
|
for (i = 0; i < config->entry_count; i++)
|
|
|
|
FreePool(lines[i]);
|
|
|
|
FreePool(lines);
|
|
|
|
FreePool(clearline);
|
|
|
|
|
|
|
|
uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_WHITE|EFI_BACKGROUND_BLACK);
|
|
|
|
uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
|
|
|
|
return run;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VOID config_add_entry(Config *config, ConfigEntry *entry) {
|
|
|
|
if ((config->entry_count & 15) == 0) {
|
|
|
|
UINTN i;
|
|
|
|
|
|
|
|
i = config->entry_count + 16;
|
|
|
|
if (config->entry_count == 0)
|
|
|
|
config->entries = AllocatePool(sizeof(VOID *) * i);
|
|
|
|
else
|
|
|
|
config->entries = ReallocatePool(config->entries,
|
|
|
|
sizeof(VOID *) * config->entry_count, sizeof(VOID *) * i);
|
|
|
|
}
|
|
|
|
config->entries[config->entry_count++] = entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VOID config_entry_free(ConfigEntry *entry) {
|
2018-06-20 23:20:47 +02:00
|
|
|
if (!entry)
|
|
|
|
return;
|
|
|
|
|
|
|
|
FreePool(entry->id);
|
2015-02-08 12:25:35 +01:00
|
|
|
FreePool(entry->title_show);
|
|
|
|
FreePool(entry->title);
|
2018-06-20 23:20:47 +02:00
|
|
|
FreePool(entry->version);
|
2015-02-08 12:25:35 +01:00
|
|
|
FreePool(entry->machine_id);
|
|
|
|
FreePool(entry->loader);
|
|
|
|
FreePool(entry->options);
|
2018-06-20 18:25:01 +02:00
|
|
|
FreePool(entry->path);
|
|
|
|
FreePool(entry->current_name);
|
|
|
|
FreePool(entry->next_name);
|
2018-06-20 23:20:47 +02:00
|
|
|
FreePool(entry);
|
2015-02-08 12:25:35 +01:00
|
|
|
}
|
|
|
|
|
2015-08-27 17:48:24 +02:00
|
|
|
static BOOLEAN is_digit(CHAR16 c) {
|
2015-02-08 12:25:35 +01:00
|
|
|
return (c >= '0') && (c <= '9');
|
|
|
|
}
|
|
|
|
|
2015-08-27 17:48:24 +02:00
|
|
|
static UINTN c_order(CHAR16 c) {
|
2015-02-08 12:25:35 +01:00
|
|
|
if (c == '\0')
|
|
|
|
return 0;
|
|
|
|
if (is_digit(c))
|
|
|
|
return 0;
|
|
|
|
else if ((c >= 'a') && (c <= 'z'))
|
|
|
|
return c;
|
|
|
|
else
|
|
|
|
return c + 0x10000;
|
|
|
|
}
|
|
|
|
|
2015-08-27 17:48:24 +02:00
|
|
|
static INTN str_verscmp(CHAR16 *s1, CHAR16 *s2) {
|
2015-02-08 12:25:35 +01:00
|
|
|
CHAR16 *os1 = s1;
|
|
|
|
CHAR16 *os2 = s2;
|
|
|
|
|
|
|
|
while (*s1 || *s2) {
|
|
|
|
INTN first;
|
|
|
|
|
|
|
|
while ((*s1 && !is_digit(*s1)) || (*s2 && !is_digit(*s2))) {
|
|
|
|
INTN order;
|
|
|
|
|
|
|
|
order = c_order(*s1) - c_order(*s2);
|
2018-06-21 18:51:14 +02:00
|
|
|
if (order != 0)
|
2015-02-08 12:25:35 +01:00
|
|
|
return order;
|
|
|
|
s1++;
|
|
|
|
s2++;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*s1 == '0')
|
|
|
|
s1++;
|
|
|
|
while (*s2 == '0')
|
|
|
|
s2++;
|
|
|
|
|
|
|
|
first = 0;
|
|
|
|
while (is_digit(*s1) && is_digit(*s2)) {
|
|
|
|
if (first == 0)
|
|
|
|
first = *s1 - *s2;
|
|
|
|
s1++;
|
|
|
|
s2++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_digit(*s1))
|
|
|
|
return 1;
|
|
|
|
if (is_digit(*s2))
|
|
|
|
return -1;
|
|
|
|
|
2018-06-21 18:51:14 +02:00
|
|
|
if (first != 0)
|
2015-02-08 12:25:35 +01:00
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
|
|
|
return StrCmp(os1, os2);
|
|
|
|
}
|
|
|
|
|
2018-06-20 18:45:58 +02:00
|
|
|
static CHAR8 *line_get_key_value(
|
|
|
|
CHAR8 *content,
|
|
|
|
CHAR8 *sep,
|
|
|
|
UINTN *pos,
|
|
|
|
CHAR8 **key_ret,
|
|
|
|
CHAR8 **value_ret) {
|
|
|
|
|
2015-02-08 12:25:35 +01:00
|
|
|
CHAR8 *line;
|
|
|
|
UINTN linelen;
|
|
|
|
CHAR8 *value;
|
|
|
|
|
|
|
|
skip:
|
|
|
|
line = content + *pos;
|
|
|
|
if (*line == '\0')
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
linelen = 0;
|
|
|
|
while (line[linelen] && !strchra((CHAR8 *)"\n\r", line[linelen]))
|
|
|
|
linelen++;
|
|
|
|
|
|
|
|
/* move pos to next line */
|
|
|
|
*pos += linelen;
|
|
|
|
if (content[*pos])
|
|
|
|
(*pos)++;
|
|
|
|
|
|
|
|
/* empty line */
|
|
|
|
if (linelen == 0)
|
|
|
|
goto skip;
|
|
|
|
|
|
|
|
/* terminate line */
|
|
|
|
line[linelen] = '\0';
|
|
|
|
|
|
|
|
/* remove leading whitespace */
|
|
|
|
while (strchra((CHAR8 *)" \t", *line)) {
|
|
|
|
line++;
|
|
|
|
linelen--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove trailing whitespace */
|
|
|
|
while (linelen > 0 && strchra(sep, line[linelen-1]))
|
|
|
|
linelen--;
|
|
|
|
line[linelen] = '\0';
|
|
|
|
|
|
|
|
if (*line == '#')
|
|
|
|
goto skip;
|
|
|
|
|
|
|
|
/* split key/value */
|
|
|
|
value = line;
|
|
|
|
while (*value && !strchra(sep, *value))
|
|
|
|
value++;
|
|
|
|
if (*value == '\0')
|
|
|
|
goto skip;
|
|
|
|
*value = '\0';
|
|
|
|
value++;
|
|
|
|
while (*value && strchra(sep, *value))
|
|
|
|
value++;
|
|
|
|
|
|
|
|
/* unquote */
|
|
|
|
if (value[0] == '\"' && line[linelen-1] == '\"') {
|
|
|
|
value++;
|
|
|
|
line[linelen-1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
*key_ret = line;
|
|
|
|
*value_ret = value;
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VOID config_defaults_load_from_file(Config *config, CHAR8 *content) {
|
|
|
|
CHAR8 *line;
|
|
|
|
UINTN pos = 0;
|
|
|
|
CHAR8 *key, *value;
|
|
|
|
|
|
|
|
while ((line = line_get_key_value(content, (CHAR8 *)" \t", &pos, &key, &value))) {
|
|
|
|
if (strcmpa((CHAR8 *)"timeout", key) == 0) {
|
2018-03-13 10:47:17 +01:00
|
|
|
_cleanup_freepool_ CHAR16 *s = NULL;
|
2015-02-08 12:25:35 +01:00
|
|
|
|
|
|
|
s = stra_to_str(value);
|
|
|
|
config->timeout_sec_config = Atoi(s);
|
|
|
|
config->timeout_sec = config->timeout_sec_config;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmpa((CHAR8 *)"default", key) == 0) {
|
|
|
|
FreePool(config->entry_default_pattern);
|
|
|
|
config->entry_default_pattern = stra_to_str(value);
|
|
|
|
StrLwr(config->entry_default_pattern);
|
|
|
|
continue;
|
|
|
|
}
|
2015-03-11 23:26:48 +01:00
|
|
|
|
|
|
|
if (strcmpa((CHAR8 *)"editor", key) == 0) {
|
|
|
|
BOOLEAN on;
|
|
|
|
|
|
|
|
if (EFI_ERROR(parse_boolean(value, &on)))
|
|
|
|
continue;
|
2018-01-28 17:06:22 +01:00
|
|
|
config->editor = on;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmpa((CHAR8 *)"auto-entries", key) == 0) {
|
|
|
|
BOOLEAN on;
|
|
|
|
|
|
|
|
if (EFI_ERROR(parse_boolean(value, &on)))
|
|
|
|
continue;
|
|
|
|
config->auto_entries = on;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmpa((CHAR8 *)"auto-firmware", key) == 0) {
|
|
|
|
BOOLEAN on;
|
|
|
|
|
|
|
|
if (EFI_ERROR(parse_boolean(value, &on)))
|
|
|
|
continue;
|
|
|
|
config->auto_firmware = on;
|
2015-03-11 23:26:48 +01:00
|
|
|
}
|
2017-12-16 19:23:06 +01:00
|
|
|
|
|
|
|
if (strcmpa((CHAR8 *)"console-mode", key) == 0) {
|
|
|
|
if (strcmpa((CHAR8 *)"auto", value) == 0)
|
|
|
|
config->console_mode_change = CONSOLE_MODE_AUTO;
|
|
|
|
else if (strcmpa((CHAR8 *)"max", value) == 0)
|
|
|
|
config->console_mode_change = CONSOLE_MODE_MAX;
|
|
|
|
else if (strcmpa((CHAR8 *)"keep", value) == 0)
|
|
|
|
config->console_mode_change = CONSOLE_MODE_KEEP;
|
|
|
|
else {
|
2018-03-13 10:47:17 +01:00
|
|
|
_cleanup_freepool_ CHAR16 *s = NULL;
|
|
|
|
|
2017-12-16 19:23:06 +01:00
|
|
|
s = stra_to_str(value);
|
|
|
|
config->console_mode = Atoi(s);
|
|
|
|
config->console_mode_change = CONSOLE_MODE_SET;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2015-02-08 12:25:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-20 18:25:01 +02:00
|
|
|
static VOID config_entry_parse_tries(
|
|
|
|
ConfigEntry *entry,
|
|
|
|
CHAR16 *path,
|
|
|
|
CHAR16 *file,
|
|
|
|
CHAR16 *suffix) {
|
|
|
|
|
|
|
|
UINTN left = (UINTN) -1, done = (UINTN) -1, factor = 1, i, next_left, next_done;
|
|
|
|
_cleanup_freepool_ CHAR16 *prefix = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parses a suffix of two counters (one going down, one going up) in the form "+LEFT-DONE" from the end of the
|
|
|
|
* filename (but before the .efi/.conf suffix), where the "-DONE" part is optional and may be left out (in
|
|
|
|
* which case that counter as assumed to be zero, i.e. the missing part is synonymous to "-0").
|
|
|
|
*
|
|
|
|
* Names we grok, and the series they result in:
|
|
|
|
*
|
|
|
|
* foobar+3.efi → foobar+2-1.efi → foobar+1-2.efi → foobar+0-3.efi → STOP!
|
|
|
|
* foobar+4-0.efi → foobar+3-1.efi → foobar+2-2.efi → foobar+1-3.efi → foobar+0-4.efi → STOP!
|
|
|
|
*/
|
|
|
|
|
|
|
|
i = StrLen(file);
|
|
|
|
|
|
|
|
/* Chop off any suffix such as ".conf" or ".efi" */
|
|
|
|
if (suffix) {
|
|
|
|
UINTN suffix_length;
|
|
|
|
|
|
|
|
suffix_length = StrLen(suffix);
|
|
|
|
if (i < suffix_length)
|
|
|
|
return;
|
|
|
|
|
|
|
|
i -= suffix_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Go backwards through the string and parse everything we encounter */
|
|
|
|
for (;;) {
|
|
|
|
if (i == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
i--;
|
|
|
|
|
|
|
|
switch (file[i]) {
|
|
|
|
|
|
|
|
case '+':
|
|
|
|
if (left == (UINTN) -1) /* didn't read at least one digit for 'left'? */
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (done == (UINTN) -1) /* no 'done' counter? If so, it's equivalent to 0 */
|
|
|
|
done = 0;
|
|
|
|
|
|
|
|
goto good;
|
|
|
|
|
|
|
|
case '-':
|
|
|
|
if (left == (UINTN) -1) /* didn't parse any digit yet? */
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (done != (UINTN) -1) /* already encountered a dash earlier? */
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* So we encountered a dash. This means this counter is of the form +LEFT-DONE. Let's assign
|
|
|
|
* what we already parsed to 'done', and start fresh for the 'left' part. */
|
|
|
|
|
|
|
|
done = left;
|
|
|
|
left = (UINTN) -1;
|
|
|
|
factor = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '0'...'9': {
|
|
|
|
UINTN new_factor;
|
|
|
|
|
|
|
|
if (left == (UINTN) -1)
|
|
|
|
left = file[i] - '0';
|
|
|
|
else {
|
|
|
|
UINTN new_left, digit;
|
|
|
|
|
|
|
|
digit = file[i] - '0';
|
|
|
|
if (digit > (UINTN) -1 / factor) /* overflow check */
|
|
|
|
return;
|
|
|
|
|
|
|
|
new_left = left + digit * factor;
|
|
|
|
if (new_left < left) /* overflow check */
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (new_left == (UINTN) -1) /* don't allow us to be confused */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_factor = factor * 10;
|
|
|
|
if (new_factor < factor) /* overflow chck */
|
|
|
|
return;
|
|
|
|
|
|
|
|
factor = new_factor;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
good:
|
|
|
|
entry->tries_left = left;
|
|
|
|
entry->tries_done = done;
|
|
|
|
|
|
|
|
entry->path = StrDuplicate(path);
|
|
|
|
entry->current_name = StrDuplicate(file);
|
|
|
|
|
|
|
|
next_left = left <= 0 ? 0 : left - 1;
|
|
|
|
next_done = done >= (UINTN) -2 ? (UINTN) -2 : done + 1;
|
|
|
|
|
|
|
|
prefix = StrDuplicate(file);
|
|
|
|
prefix[i] = 0;
|
|
|
|
|
|
|
|
entry->next_name = PoolPrint(L"%s+%u-%u%s", prefix, next_left, next_done, suffix ?: L"");
|
|
|
|
}
|
|
|
|
|
|
|
|
static VOID config_entry_bump_counters(
|
|
|
|
ConfigEntry *entry,
|
|
|
|
EFI_FILE_HANDLE root_dir) {
|
|
|
|
|
|
|
|
_cleanup_freepool_ CHAR16* old_path = NULL, *new_path = NULL;
|
|
|
|
_cleanup_(FileHandleClosep) EFI_FILE_HANDLE handle = NULL;
|
|
|
|
static EFI_GUID EfiFileInfoGuid = EFI_FILE_INFO_ID;
|
|
|
|
_cleanup_freepool_ EFI_FILE_INFO *file_info = NULL;
|
|
|
|
UINTN file_info_size, a, b;
|
|
|
|
EFI_STATUS r;
|
|
|
|
|
|
|
|
if (entry->tries_left == (UINTN) -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!entry->path || !entry->current_name || !entry->next_name)
|
|
|
|
return;
|
|
|
|
|
|
|
|
old_path = PoolPrint(L"%s\\%s", entry->path, entry->current_name);
|
|
|
|
|
|
|
|
r = uefi_call_wrapper(root_dir->Open, 5, root_dir, &handle, old_path, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE, 0ULL);
|
|
|
|
if (EFI_ERROR(r))
|
|
|
|
return;
|
|
|
|
|
|
|
|
a = StrLen(entry->current_name);
|
|
|
|
b = StrLen(entry->next_name);
|
|
|
|
|
|
|
|
file_info_size = OFFSETOF(EFI_FILE_INFO, FileName) + (a > b ? a : b) + 1;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
file_info = AllocatePool(file_info_size);
|
|
|
|
|
|
|
|
r = uefi_call_wrapper(handle->GetInfo, 4, handle, &EfiFileInfoGuid, &file_info_size, file_info);
|
|
|
|
if (!EFI_ERROR(r))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (r != EFI_BUFFER_TOO_SMALL || file_info_size * 2 < file_info_size) {
|
|
|
|
Print(L"\nFailed to get file info for '%s': %r\n", old_path, r);
|
|
|
|
uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
file_info_size *= 2;
|
|
|
|
FreePool(file_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And rename the file */
|
|
|
|
StrCpy(file_info->FileName, entry->next_name);
|
|
|
|
r = uefi_call_wrapper(handle->SetInfo, 4, handle, &EfiFileInfoGuid, file_info_size, file_info);
|
|
|
|
if (EFI_ERROR(r)) {
|
|
|
|
Print(L"\nFailed to rename '%s' to '%s', ignoring: %r\n", old_path, entry->next_name, r);
|
|
|
|
uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Flush everything to disk, just in case… */
|
|
|
|
(void) uefi_call_wrapper(handle->Flush, 1, handle);
|
|
|
|
|
|
|
|
/* Let's tell the OS that we renamed this file, so that it knows what to rename to the counter-less name on
|
|
|
|
* success */
|
|
|
|
new_path = PoolPrint(L"%s\\%s", entry->path, entry->next_name);
|
|
|
|
efivar_set(L"LoaderBootCountPath", new_path, FALSE);
|
|
|
|
|
|
|
|
/* If the file we just renamed is the loader path, then let's update that. */
|
|
|
|
if (StrCmp(entry->loader, old_path) == 0) {
|
|
|
|
FreePool(entry->loader);
|
|
|
|
entry->loader = new_path;
|
|
|
|
new_path = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-20 18:45:58 +02:00
|
|
|
static VOID config_entry_add_from_file(
|
|
|
|
Config *config,
|
|
|
|
EFI_HANDLE *device,
|
2018-06-20 18:25:01 +02:00
|
|
|
CHAR16 *path,
|
2018-06-20 18:45:58 +02:00
|
|
|
CHAR16 *file,
|
|
|
|
CHAR8 *content,
|
|
|
|
CHAR16 *loaded_image_path) {
|
|
|
|
|
2015-02-08 12:25:35 +01:00
|
|
|
ConfigEntry *entry;
|
|
|
|
CHAR8 *line;
|
|
|
|
UINTN pos = 0;
|
|
|
|
CHAR8 *key, *value;
|
|
|
|
UINTN len;
|
2018-03-13 10:47:17 +01:00
|
|
|
_cleanup_freepool_ CHAR16 *initrd = NULL;
|
2015-02-08 12:25:35 +01:00
|
|
|
|
2018-06-20 18:25:01 +02:00
|
|
|
entry = AllocatePool(sizeof(ConfigEntry));
|
|
|
|
|
|
|
|
*entry = (ConfigEntry) {
|
|
|
|
.tries_done = (UINTN) -1,
|
|
|
|
.tries_left = (UINTN) -1,
|
|
|
|
};
|
2015-02-08 12:25:35 +01:00
|
|
|
|
|
|
|
while ((line = line_get_key_value(content, (CHAR8 *)" \t", &pos, &key, &value))) {
|
|
|
|
if (strcmpa((CHAR8 *)"title", key) == 0) {
|
|
|
|
FreePool(entry->title);
|
|
|
|
entry->title = stra_to_str(value);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmpa((CHAR8 *)"version", key) == 0) {
|
|
|
|
FreePool(entry->version);
|
|
|
|
entry->version = stra_to_str(value);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmpa((CHAR8 *)"machine-id", key) == 0) {
|
|
|
|
FreePool(entry->machine_id);
|
|
|
|
entry->machine_id = stra_to_str(value);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmpa((CHAR8 *)"linux", key) == 0) {
|
|
|
|
FreePool(entry->loader);
|
|
|
|
entry->type = LOADER_LINUX;
|
|
|
|
entry->loader = stra_to_path(value);
|
|
|
|
entry->key = 'l';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmpa((CHAR8 *)"efi", key) == 0) {
|
|
|
|
entry->type = LOADER_EFI;
|
|
|
|
FreePool(entry->loader);
|
|
|
|
entry->loader = stra_to_path(value);
|
|
|
|
|
|
|
|
/* do not add an entry for ourselves */
|
|
|
|
if (StriCmp(entry->loader, loaded_image_path) == 0) {
|
|
|
|
entry->type = LOADER_UNDEFINED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmpa((CHAR8 *)"architecture", key) == 0) {
|
|
|
|
/* do not add an entry for an EFI image of architecture not matching with that of the image */
|
|
|
|
if (strcmpa((CHAR8 *)EFI_MACHINE_TYPE_NAME, value) != 0) {
|
|
|
|
entry->type = LOADER_UNDEFINED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmpa((CHAR8 *)"initrd", key) == 0) {
|
2018-03-13 10:47:17 +01:00
|
|
|
_cleanup_freepool_ CHAR16 *new = NULL;
|
2015-02-08 12:25:35 +01:00
|
|
|
|
|
|
|
new = stra_to_path(value);
|
|
|
|
if (initrd) {
|
|
|
|
CHAR16 *s;
|
|
|
|
|
|
|
|
s = PoolPrint(L"%s initrd=%s", initrd, new);
|
|
|
|
FreePool(initrd);
|
|
|
|
initrd = s;
|
|
|
|
} else
|
|
|
|
initrd = PoolPrint(L"initrd=%s", new);
|
2018-03-13 10:47:17 +01:00
|
|
|
|
2015-02-08 12:25:35 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmpa((CHAR8 *)"options", key) == 0) {
|
2018-03-13 10:47:17 +01:00
|
|
|
_cleanup_freepool_ CHAR16 *new = NULL;
|
2015-02-08 12:25:35 +01:00
|
|
|
|
|
|
|
new = stra_to_str(value);
|
|
|
|
if (entry->options) {
|
|
|
|
CHAR16 *s;
|
|
|
|
|
|
|
|
s = PoolPrint(L"%s %s", entry->options, new);
|
|
|
|
FreePool(entry->options);
|
|
|
|
entry->options = s;
|
|
|
|
} else {
|
|
|
|
entry->options = new;
|
|
|
|
new = NULL;
|
|
|
|
}
|
2018-03-13 10:47:17 +01:00
|
|
|
|
2015-02-08 12:25:35 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry->type == LOADER_UNDEFINED) {
|
|
|
|
config_entry_free(entry);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add initrd= to options */
|
|
|
|
if (entry->type == LOADER_LINUX && initrd) {
|
|
|
|
if (entry->options) {
|
|
|
|
CHAR16 *s;
|
|
|
|
|
|
|
|
s = PoolPrint(L"%s %s", initrd, entry->options);
|
|
|
|
FreePool(entry->options);
|
|
|
|
entry->options = s;
|
|
|
|
} else {
|
|
|
|
entry->options = initrd;
|
|
|
|
initrd = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
entry->device = device;
|
2018-06-20 18:49:10 +02:00
|
|
|
entry->id = StrDuplicate(file);
|
|
|
|
len = StrLen(entry->id);
|
2015-02-08 12:25:35 +01:00
|
|
|
/* remove ".conf" */
|
|
|
|
if (len > 5)
|
2018-06-20 18:49:10 +02:00
|
|
|
entry->id[len - 5] = '\0';
|
|
|
|
StrLwr(entry->id);
|
2015-02-08 12:25:35 +01:00
|
|
|
|
|
|
|
config_add_entry(config, entry);
|
2018-06-20 18:25:01 +02:00
|
|
|
|
|
|
|
config_entry_parse_tries(entry, path, file, L".conf");
|
2015-02-08 12:25:35 +01:00
|
|
|
}
|
|
|
|
|
2015-07-30 14:09:19 +02:00
|
|
|
static VOID config_load_defaults(Config *config, EFI_FILE *root_dir) {
|
2018-03-13 10:47:17 +01:00
|
|
|
_cleanup_freepool_ CHAR8 *content = NULL;
|
2015-02-08 12:25:35 +01:00
|
|
|
UINTN sec;
|
2015-07-30 14:09:19 +02:00
|
|
|
EFI_STATUS err;
|
2015-02-08 12:25:35 +01:00
|
|
|
|
2018-01-28 17:06:22 +01:00
|
|
|
config->editor = TRUE;
|
|
|
|
config->auto_entries = TRUE;
|
|
|
|
config->auto_firmware = TRUE;
|
|
|
|
|
2018-01-07 11:53:34 +01:00
|
|
|
err = file_read(root_dir, L"\\loader\\loader.conf", 0, 0, &content, NULL);
|
|
|
|
if (!EFI_ERROR(err))
|
2015-02-08 12:25:35 +01:00
|
|
|
config_defaults_load_from_file(config, content);
|
|
|
|
|
|
|
|
err = efivar_get_int(L"LoaderConfigTimeout", &sec);
|
|
|
|
if (!EFI_ERROR(err)) {
|
|
|
|
config->timeout_sec_efivar = sec;
|
|
|
|
config->timeout_sec = sec;
|
|
|
|
} else
|
|
|
|
config->timeout_sec_efivar = -1;
|
2015-07-30 14:09:19 +02:00
|
|
|
}
|
|
|
|
|
2018-06-20 18:45:58 +02:00
|
|
|
static VOID config_load_entries(
|
|
|
|
Config *config,
|
|
|
|
EFI_HANDLE *device,
|
|
|
|
EFI_FILE *root_dir,
|
|
|
|
CHAR16 *loaded_image_path) {
|
|
|
|
|
2015-07-30 14:09:19 +02:00
|
|
|
EFI_FILE_HANDLE entries_dir;
|
|
|
|
EFI_STATUS err;
|
2015-02-08 12:25:35 +01:00
|
|
|
|
|
|
|
err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &entries_dir, L"\\loader\\entries", EFI_FILE_MODE_READ, 0ULL);
|
|
|
|
if (!EFI_ERROR(err)) {
|
|
|
|
for (;;) {
|
|
|
|
CHAR16 buf[256];
|
|
|
|
UINTN bufsize;
|
|
|
|
EFI_FILE_INFO *f;
|
2018-03-13 10:47:17 +01:00
|
|
|
_cleanup_freepool_ CHAR8 *content = NULL;
|
2015-02-08 12:25:35 +01:00
|
|
|
UINTN len;
|
|
|
|
|
|
|
|
bufsize = sizeof(buf);
|
|
|
|
err = uefi_call_wrapper(entries_dir->Read, 3, entries_dir, &bufsize, buf);
|
|
|
|
if (bufsize == 0 || EFI_ERROR(err))
|
|
|
|
break;
|
|
|
|
|
|
|
|
f = (EFI_FILE_INFO *) buf;
|
|
|
|
if (f->FileName[0] == '.')
|
|
|
|
continue;
|
|
|
|
if (f->Attribute & EFI_FILE_DIRECTORY)
|
|
|
|
continue;
|
2015-02-28 12:21:25 +01:00
|
|
|
|
2015-02-08 12:25:35 +01:00
|
|
|
len = StrLen(f->FileName);
|
|
|
|
if (len < 6)
|
|
|
|
continue;
|
|
|
|
if (StriCmp(f->FileName + len - 5, L".conf") != 0)
|
|
|
|
continue;
|
2015-02-28 12:21:25 +01:00
|
|
|
if (StrnCmp(f->FileName, L"auto-", 5) == 0)
|
|
|
|
continue;
|
2015-02-08 12:25:35 +01:00
|
|
|
|
2018-01-07 11:53:34 +01:00
|
|
|
err = file_read(entries_dir, f->FileName, 0, 0, &content, NULL);
|
|
|
|
if (!EFI_ERROR(err))
|
2018-06-20 18:25:01 +02:00
|
|
|
config_entry_add_from_file(config, device, L"\\loader\\entries", f->FileName, content, loaded_image_path);
|
2015-02-08 12:25:35 +01:00
|
|
|
}
|
|
|
|
uefi_call_wrapper(entries_dir->Close, 1, entries_dir);
|
|
|
|
}
|
2015-07-30 14:09:19 +02:00
|
|
|
}
|
|
|
|
|
2018-06-20 18:25:01 +02:00
|
|
|
static INTN config_entry_compare(ConfigEntry *a, ConfigEntry *b) {
|
|
|
|
INTN r;
|
|
|
|
|
|
|
|
/* Order entries that have no tries left to the end of the list */
|
|
|
|
if (a->tries_left != 0 && b->tries_left == 0)
|
|
|
|
return -1;
|
|
|
|
if (a->tries_left == 0 && b->tries_left != 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
r = str_verscmp(a->id, b->id);
|
|
|
|
if (r != 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (a->tries_left == (UINTN) -1 ||
|
|
|
|
b->tries_left == (UINTN) -1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* If both items have boot counting, and otherwise are identical, put the entry with more tries left first */
|
|
|
|
if (a->tries_left > b->tries_left)
|
|
|
|
return -1;
|
|
|
|
if (a->tries_left < b->tries_left)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* If they have the same number of tries left, then let the one win which was tried fewer times so far */
|
|
|
|
if (a->tries_done < b->tries_done)
|
|
|
|
return -1;
|
|
|
|
if (a->tries_done > b->tries_done)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-07-30 14:09:19 +02:00
|
|
|
static VOID config_sort_entries(Config *config) {
|
|
|
|
UINTN i;
|
2015-02-08 12:25:35 +01:00
|
|
|
|
|
|
|
for (i = 1; i < config->entry_count; i++) {
|
|
|
|
BOOLEAN more;
|
|
|
|
UINTN k;
|
|
|
|
|
|
|
|
more = FALSE;
|
|
|
|
for (k = 0; k < config->entry_count - i; k++) {
|
|
|
|
ConfigEntry *entry;
|
|
|
|
|
2018-06-20 18:25:01 +02:00
|
|
|
if (config_entry_compare(config->entries[k], config->entries[k+1]) <= 0)
|
2015-02-08 12:25:35 +01:00
|
|
|
continue;
|
2018-06-20 18:25:01 +02:00
|
|
|
|
2015-02-08 12:25:35 +01:00
|
|
|
entry = config->entries[k];
|
|
|
|
config->entries[k] = config->entries[k+1];
|
|
|
|
config->entries[k+1] = entry;
|
|
|
|
more = TRUE;
|
|
|
|
}
|
|
|
|
if (!more)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VOID config_default_entry_select(Config *config) {
|
2018-03-13 10:47:17 +01:00
|
|
|
_cleanup_freepool_ CHAR16 *entry_oneshot = NULL, *entry_default = NULL;
|
2015-02-08 12:25:35 +01:00
|
|
|
EFI_STATUS err;
|
|
|
|
UINTN i;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The EFI variable to specify a boot entry for the next, and only the
|
|
|
|
* next reboot. The variable is always cleared directly after it is read.
|
|
|
|
*/
|
2018-03-13 10:47:17 +01:00
|
|
|
err = efivar_get(L"LoaderEntryOneShot", &entry_oneshot);
|
2015-02-08 12:25:35 +01:00
|
|
|
if (!EFI_ERROR(err)) {
|
|
|
|
BOOLEAN found = FALSE;
|
|
|
|
|
2018-03-13 10:47:17 +01:00
|
|
|
for (i = 0; i < config->entry_count; i++)
|
2018-06-20 18:49:10 +02:00
|
|
|
if (StrCmp(config->entries[i]->id, entry_oneshot) == 0) {
|
2015-02-08 12:25:35 +01:00
|
|
|
config->idx_default = i;
|
|
|
|
found = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-03-13 10:47:17 +01:00
|
|
|
config->entry_oneshot = StrDuplicate(entry_oneshot);
|
2015-02-08 12:25:35 +01:00
|
|
|
efivar_set(L"LoaderEntryOneShot", NULL, TRUE);
|
|
|
|
if (found)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The EFI variable to select the default boot entry overrides the
|
|
|
|
* configured pattern. The variable can be set and cleared by pressing
|
|
|
|
* the 'd' key in the loader selection menu, the entry is marked with
|
|
|
|
* an '*'.
|
|
|
|
*/
|
2018-03-13 10:47:17 +01:00
|
|
|
err = efivar_get(L"LoaderEntryDefault", &entry_default);
|
2015-02-08 12:25:35 +01:00
|
|
|
if (!EFI_ERROR(err)) {
|
2018-03-13 10:47:17 +01:00
|
|
|
for (i = 0; i < config->entry_count; i++)
|
2018-06-20 18:49:10 +02:00
|
|
|
if (StrCmp(config->entries[i]->id, entry_default) == 0) {
|
2015-02-08 12:25:35 +01:00
|
|
|
config->idx_default = i;
|
|
|
|
config->idx_default_efivar = i;
|
2018-03-13 10:47:17 +01:00
|
|
|
return;
|
2015-02-08 12:25:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
config->idx_default_efivar = -1;
|
|
|
|
|
|
|
|
if (config->entry_count == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Match the pattern from the end of the list to the start, find last
|
|
|
|
* entry (largest number) matching the given pattern.
|
|
|
|
*/
|
|
|
|
if (config->entry_default_pattern) {
|
|
|
|
i = config->entry_count;
|
|
|
|
while (i--) {
|
|
|
|
if (config->entries[i]->no_autoselect)
|
|
|
|
continue;
|
2018-06-20 18:49:10 +02:00
|
|
|
if (MetaiMatch(config->entries[i]->id, config->entry_default_pattern)) {
|
2015-02-08 12:25:35 +01:00
|
|
|
config->idx_default = i;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* select the last suitable entry */
|
|
|
|
i = config->entry_count;
|
|
|
|
while (i--) {
|
|
|
|
if (config->entries[i]->no_autoselect)
|
|
|
|
continue;
|
|
|
|
config->idx_default = i;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* no entry found */
|
|
|
|
config->idx_default = -1;
|
|
|
|
}
|
|
|
|
|
2017-10-20 17:51:54 +02:00
|
|
|
static BOOLEAN find_nonunique(ConfigEntry **entries, UINTN entry_count) {
|
|
|
|
BOOLEAN non_unique = FALSE;
|
|
|
|
UINTN i, k;
|
|
|
|
|
|
|
|
for (i = 0; i < entry_count; i++)
|
|
|
|
entries[i]->non_unique = FALSE;
|
|
|
|
|
|
|
|
for (i = 0; i < entry_count; i++)
|
|
|
|
for (k = 0; k < entry_count; k++) {
|
|
|
|
if (i == k)
|
|
|
|
continue;
|
|
|
|
if (StrCmp(entries[i]->title_show, entries[k]->title_show) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
non_unique = entries[i]->non_unique = entries[k]->non_unique = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return non_unique;
|
|
|
|
}
|
|
|
|
|
2015-02-08 12:25:35 +01:00
|
|
|
/* generate a unique title, avoiding non-distinguishable menu entries */
|
|
|
|
static VOID config_title_generate(Config *config) {
|
2017-10-20 17:51:54 +02:00
|
|
|
UINTN i;
|
2015-02-08 12:25:35 +01:00
|
|
|
|
|
|
|
/* set title */
|
|
|
|
for (i = 0; i < config->entry_count; i++) {
|
|
|
|
CHAR16 *title;
|
|
|
|
|
|
|
|
FreePool(config->entries[i]->title_show);
|
|
|
|
title = config->entries[i]->title;
|
|
|
|
if (!title)
|
2018-06-20 18:49:10 +02:00
|
|
|
title = config->entries[i]->id;
|
2015-02-08 12:25:35 +01:00
|
|
|
config->entries[i]->title_show = StrDuplicate(title);
|
|
|
|
}
|
|
|
|
|
2017-10-20 17:51:54 +02:00
|
|
|
if (!find_nonunique(config->entries, config->entry_count))
|
2015-02-08 12:25:35 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* add version to non-unique titles */
|
|
|
|
for (i = 0; i < config->entry_count; i++) {
|
|
|
|
CHAR16 *s;
|
|
|
|
|
|
|
|
if (!config->entries[i]->non_unique)
|
|
|
|
continue;
|
|
|
|
if (!config->entries[i]->version)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
s = PoolPrint(L"%s (%s)", config->entries[i]->title_show, config->entries[i]->version);
|
|
|
|
FreePool(config->entries[i]->title_show);
|
|
|
|
config->entries[i]->title_show = s;
|
|
|
|
}
|
|
|
|
|
2017-10-20 17:51:54 +02:00
|
|
|
if (!find_nonunique(config->entries, config->entry_count))
|
2015-02-08 12:25:35 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* add machine-id to non-unique titles */
|
|
|
|
for (i = 0; i < config->entry_count; i++) {
|
|
|
|
CHAR16 *s;
|
2018-03-13 10:47:17 +01:00
|
|
|
_cleanup_freepool_ CHAR16 *m = NULL;
|
2015-02-08 12:25:35 +01:00
|
|
|
|
|
|
|
if (!config->entries[i]->non_unique)
|
|
|
|
continue;
|
|
|
|
if (!config->entries[i]->machine_id)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
m = StrDuplicate(config->entries[i]->machine_id);
|
|
|
|
m[8] = '\0';
|
|
|
|
s = PoolPrint(L"%s (%s)", config->entries[i]->title_show, m);
|
|
|
|
FreePool(config->entries[i]->title_show);
|
|
|
|
config->entries[i]->title_show = s;
|
|
|
|
}
|
|
|
|
|
2017-10-20 17:51:54 +02:00
|
|
|
if (!find_nonunique(config->entries, config->entry_count))
|
2015-02-08 12:25:35 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* add file name to non-unique titles */
|
|
|
|
for (i = 0; i < config->entry_count; i++) {
|
|
|
|
CHAR16 *s;
|
|
|
|
|
|
|
|
if (!config->entries[i]->non_unique)
|
|
|
|
continue;
|
2018-06-20 18:49:10 +02:00
|
|
|
s = PoolPrint(L"%s (%s)", config->entries[i]->title_show, config->entries[i]->id);
|
2015-02-08 12:25:35 +01:00
|
|
|
FreePool(config->entries[i]->title_show);
|
|
|
|
config->entries[i]->title_show = s;
|
|
|
|
config->entries[i]->non_unique = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-20 18:45:58 +02:00
|
|
|
static BOOLEAN config_entry_add_call(
|
|
|
|
Config *config,
|
|
|
|
CHAR16 *title,
|
|
|
|
EFI_STATUS (*call)(VOID)) {
|
|
|
|
|
2015-02-08 12:25:35 +01:00
|
|
|
ConfigEntry *entry;
|
|
|
|
|
2018-06-20 18:25:01 +02:00
|
|
|
entry = AllocatePool(sizeof(ConfigEntry));
|
|
|
|
*entry = (ConfigEntry) {
|
|
|
|
.title = StrDuplicate(title),
|
|
|
|
.call = call,
|
|
|
|
.no_autoselect = TRUE,
|
|
|
|
.tries_done = (UINTN) -1,
|
|
|
|
.tries_left = (UINTN) -1,
|
|
|
|
};
|
|
|
|
|
2015-02-08 12:25:35 +01:00
|
|
|
config_add_entry(config, entry);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2018-06-20 18:45:58 +02:00
|
|
|
static ConfigEntry *config_entry_add_loader(
|
|
|
|
Config *config,
|
|
|
|
EFI_HANDLE *device,
|
|
|
|
enum loader_type type,
|
2018-06-20 18:49:10 +02:00
|
|
|
CHAR16 *id,
|
2018-06-20 18:45:58 +02:00
|
|
|
CHAR16 key,
|
|
|
|
CHAR16 *title,
|
|
|
|
CHAR16 *loader) {
|
|
|
|
|
2015-02-08 12:25:35 +01:00
|
|
|
ConfigEntry *entry;
|
|
|
|
|
2018-06-20 18:25:01 +02:00
|
|
|
entry = AllocatePool(sizeof(ConfigEntry));
|
|
|
|
*entry = (ConfigEntry) {
|
|
|
|
.type = type,
|
|
|
|
.title = StrDuplicate(title),
|
|
|
|
.device = device,
|
|
|
|
.loader = StrDuplicate(loader),
|
|
|
|
.id = StrDuplicate(id),
|
|
|
|
.key = key,
|
|
|
|
.tries_done = (UINTN) -1,
|
|
|
|
.tries_left = (UINTN) -1,
|
|
|
|
};
|
|
|
|
|
2018-06-20 18:49:10 +02:00
|
|
|
StrLwr(entry->id);
|
2015-02-08 12:25:35 +01:00
|
|
|
|
2018-06-20 18:25:01 +02:00
|
|
|
config_add_entry(config, entry);
|
2015-02-08 12:25:35 +01:00
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2018-06-20 18:45:58 +02:00
|
|
|
static BOOLEAN config_entry_add_loader_auto(
|
|
|
|
Config *config,
|
|
|
|
EFI_HANDLE *device,
|
|
|
|
EFI_FILE *root_dir,
|
|
|
|
CHAR16 *loaded_image_path,
|
2018-06-20 18:49:10 +02:00
|
|
|
CHAR16 *id,
|
2018-06-20 18:45:58 +02:00
|
|
|
CHAR16 key,
|
|
|
|
CHAR16 *title,
|
|
|
|
CHAR16 *loader) {
|
|
|
|
|
2015-02-08 12:25:35 +01:00
|
|
|
EFI_FILE_HANDLE handle;
|
|
|
|
ConfigEntry *entry;
|
|
|
|
EFI_STATUS err;
|
|
|
|
|
2018-01-28 17:06:22 +01:00
|
|
|
if (!config->auto_entries)
|
|
|
|
return FALSE;
|
|
|
|
|
2015-02-08 12:25:35 +01:00
|
|
|
/* do not add an entry for ourselves */
|
2018-01-06 19:21:48 +01:00
|
|
|
if (loaded_image_path) {
|
|
|
|
UINTN len;
|
2018-03-13 10:47:17 +01:00
|
|
|
_cleanup_freepool_ CHAR8 *content = NULL;
|
2018-01-06 19:21:48 +01:00
|
|
|
|
|
|
|
if (StriCmp(loader, loaded_image_path) == 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* look for systemd-boot magic string */
|
|
|
|
err = file_read(root_dir, loader, 0, 100*1024, &content, &len);
|
|
|
|
if (!EFI_ERROR(err)) {
|
|
|
|
CHAR8 *start = content;
|
|
|
|
CHAR8 *last = content + len - sizeof(magic) - 1;
|
|
|
|
|
|
|
|
for (; start <= last; start++)
|
2018-03-13 10:47:17 +01:00
|
|
|
if (start[0] == magic[0] && CompareMem(start, magic, sizeof(magic) - 1) == 0)
|
2018-01-06 19:21:48 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2015-02-08 12:25:35 +01:00
|
|
|
|
|
|
|
/* check existence */
|
|
|
|
err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &handle, loader, EFI_FILE_MODE_READ, 0ULL);
|
|
|
|
if (EFI_ERROR(err))
|
|
|
|
return FALSE;
|
|
|
|
uefi_call_wrapper(handle->Close, 1, handle);
|
|
|
|
|
2018-06-20 18:49:10 +02:00
|
|
|
entry = config_entry_add_loader(config, device, LOADER_UNDEFINED, id, key, title, loader);
|
2015-02-08 12:25:35 +01:00
|
|
|
if (!entry)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* do not boot right away into auto-detected entries */
|
|
|
|
entry->no_autoselect = TRUE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VOID config_entry_add_osx(Config *config) {
|
|
|
|
EFI_STATUS err;
|
|
|
|
UINTN handle_count = 0;
|
2018-03-13 10:47:17 +01:00
|
|
|
_cleanup_freepool_ EFI_HANDLE *handles = NULL;
|
2015-02-08 12:25:35 +01:00
|
|
|
|
2018-01-28 17:06:22 +01:00
|
|
|
if (!config->auto_entries)
|
|
|
|
return;
|
|
|
|
|
2015-02-08 12:25:35 +01:00
|
|
|
err = LibLocateHandle(ByProtocol, &FileSystemProtocol, NULL, &handle_count, &handles);
|
|
|
|
if (!EFI_ERROR(err)) {
|
|
|
|
UINTN i;
|
|
|
|
|
|
|
|
for (i = 0; i < handle_count; i++) {
|
|
|
|
EFI_FILE *root;
|
|
|
|
BOOLEAN found;
|
|
|
|
|
|
|
|
root = LibOpenRoot(handles[i]);
|
|
|
|
if (!root)
|
|
|
|
continue;
|
2016-12-01 15:58:58 +01:00
|
|
|
found = config_entry_add_loader_auto(config, handles[i], root, NULL, L"auto-osx", 'a', L"macOS",
|
2015-02-08 12:25:35 +01:00
|
|
|
L"\\System\\Library\\CoreServices\\boot.efi");
|
|
|
|
uefi_call_wrapper(root->Close, 1, root);
|
|
|
|
if (found)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-20 18:45:58 +02:00
|
|
|
static VOID config_entry_add_linux(
|
|
|
|
Config *config,
|
|
|
|
EFI_LOADED_IMAGE *loaded_image,
|
|
|
|
EFI_FILE *root_dir) {
|
|
|
|
|
2015-02-08 12:25:35 +01:00
|
|
|
EFI_FILE_HANDLE linux_dir;
|
|
|
|
EFI_STATUS err;
|
2015-07-16 22:39:44 +02:00
|
|
|
ConfigEntry *entry;
|
2015-02-08 12:25:35 +01:00
|
|
|
|
|
|
|
err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &linux_dir, L"\\EFI\\Linux", EFI_FILE_MODE_READ, 0ULL);
|
2018-03-07 08:55:04 +01:00
|
|
|
if (EFI_ERROR(err))
|
|
|
|
return;
|
2015-02-08 12:25:35 +01:00
|
|
|
|
2018-03-07 08:55:04 +01:00
|
|
|
for (;;) {
|
|
|
|
CHAR16 buf[256];
|
|
|
|
UINTN bufsize = sizeof buf;
|
|
|
|
EFI_FILE_INFO *f;
|
|
|
|
CHAR8 *sections[] = {
|
|
|
|
(UINT8 *)".osrel",
|
|
|
|
(UINT8 *)".cmdline",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
UINTN offs[ELEMENTSOF(sections)-1] = {};
|
|
|
|
UINTN szs[ELEMENTSOF(sections)-1] = {};
|
|
|
|
UINTN addrs[ELEMENTSOF(sections)-1] = {};
|
|
|
|
CHAR8 *content = NULL;
|
|
|
|
UINTN len;
|
|
|
|
CHAR8 *line;
|
|
|
|
UINTN pos = 0;
|
|
|
|
CHAR8 *key, *value;
|
|
|
|
CHAR16 *os_name = NULL;
|
|
|
|
CHAR16 *os_id = NULL;
|
|
|
|
CHAR16 *os_version = NULL;
|
|
|
|
CHAR16 *os_build = NULL;
|
|
|
|
|
|
|
|
err = uefi_call_wrapper(linux_dir->Read, 3, linux_dir, &bufsize, buf);
|
|
|
|
if (bufsize == 0 || EFI_ERROR(err))
|
|
|
|
break;
|
2015-02-08 12:25:35 +01:00
|
|
|
|
2018-03-07 08:55:04 +01:00
|
|
|
f = (EFI_FILE_INFO *) buf;
|
|
|
|
if (f->FileName[0] == '.')
|
|
|
|
continue;
|
|
|
|
if (f->Attribute & EFI_FILE_DIRECTORY)
|
|
|
|
continue;
|
|
|
|
len = StrLen(f->FileName);
|
|
|
|
if (len < 5)
|
|
|
|
continue;
|
|
|
|
if (StriCmp(f->FileName + len - 4, L".efi") != 0)
|
|
|
|
continue;
|
2015-02-08 12:25:35 +01:00
|
|
|
|
2018-03-07 08:55:04 +01:00
|
|
|
/* look for .osrel and .cmdline sections in the .efi binary */
|
|
|
|
err = pe_file_locate_sections(linux_dir, f->FileName, sections, addrs, offs, szs);
|
|
|
|
if (EFI_ERROR(err))
|
|
|
|
continue;
|
2015-02-08 12:25:35 +01:00
|
|
|
|
2018-03-07 08:55:04 +01:00
|
|
|
err = file_read(linux_dir, f->FileName, offs[0], szs[0], &content, NULL);
|
|
|
|
if (EFI_ERROR(err))
|
|
|
|
continue;
|
2015-02-08 12:25:35 +01:00
|
|
|
|
2018-03-07 08:55:04 +01:00
|
|
|
/* read properties from the embedded os-release file */
|
|
|
|
while ((line = line_get_key_value(content, (CHAR8 *)"=", &pos, &key, &value))) {
|
|
|
|
if (strcmpa((CHAR8 *)"PRETTY_NAME", key) == 0) {
|
|
|
|
FreePool(os_name);
|
|
|
|
os_name = stra_to_str(value);
|
|
|
|
continue;
|
|
|
|
}
|
2015-02-08 12:25:35 +01:00
|
|
|
|
2018-03-07 08:55:04 +01:00
|
|
|
if (strcmpa((CHAR8 *)"ID", key) == 0) {
|
|
|
|
FreePool(os_id);
|
|
|
|
os_id = stra_to_str(value);
|
|
|
|
continue;
|
|
|
|
}
|
2015-07-09 13:04:58 +02:00
|
|
|
|
2018-03-07 08:55:04 +01:00
|
|
|
if (strcmpa((CHAR8 *)"VERSION_ID", key) == 0) {
|
|
|
|
FreePool(os_version);
|
|
|
|
os_version = stra_to_str(value);
|
|
|
|
continue;
|
2015-02-08 12:25:35 +01:00
|
|
|
}
|
|
|
|
|
2018-03-07 08:55:04 +01:00
|
|
|
if (strcmpa((CHAR8 *)"BUILD_ID", key) == 0) {
|
|
|
|
FreePool(os_build);
|
|
|
|
os_build = stra_to_str(value);
|
|
|
|
continue;
|
2015-02-08 12:25:35 +01:00
|
|
|
}
|
2018-03-07 08:55:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (os_name && os_id && (os_version || os_build)) {
|
2018-06-20 23:32:21 +02:00
|
|
|
_cleanup_freepool_ CHAR16 *conf = NULL, *path = NULL;
|
2018-03-07 08:55:04 +01:00
|
|
|
|
|
|
|
conf = PoolPrint(L"%s-%s", os_id, os_version ? : os_build);
|
|
|
|
path = PoolPrint(L"\\EFI\\Linux\\%s", f->FileName);
|
2018-06-20 23:32:21 +02:00
|
|
|
|
2018-03-07 08:55:04 +01:00
|
|
|
entry = config_entry_add_loader(config, loaded_image->DeviceHandle, LOADER_LINUX, conf, 'l', os_name, path);
|
2015-02-08 12:25:35 +01:00
|
|
|
|
|
|
|
FreePool(content);
|
2018-03-07 08:55:04 +01:00
|
|
|
content = NULL;
|
2018-06-20 23:32:21 +02:00
|
|
|
|
2018-03-07 08:55:04 +01:00
|
|
|
/* read the embedded cmdline file */
|
2018-06-25 10:52:58 +02:00
|
|
|
err = file_read(linux_dir, f->FileName, offs[1], szs[1], &content, NULL);
|
2018-03-07 08:55:04 +01:00
|
|
|
if (!EFI_ERROR(err)) {
|
2018-06-25 10:52:58 +02:00
|
|
|
|
|
|
|
/* chomp the newline */
|
|
|
|
if (content[szs[1]-1] == '\n')
|
|
|
|
content[szs[1]-1] = '\0';
|
|
|
|
|
|
|
|
entry->options = stra_to_str(content);
|
2018-03-07 08:55:04 +01:00
|
|
|
}
|
2018-06-20 18:25:01 +02:00
|
|
|
|
|
|
|
config_entry_parse_tries(entry, L"\\EFI\\Linux", f->FileName, L".efi");
|
2015-02-08 12:25:35 +01:00
|
|
|
}
|
2018-03-07 08:55:04 +01:00
|
|
|
|
|
|
|
FreePool(os_name);
|
|
|
|
FreePool(os_id);
|
|
|
|
FreePool(os_version);
|
|
|
|
FreePool(os_build);
|
|
|
|
FreePool(content);
|
2015-02-08 12:25:35 +01:00
|
|
|
}
|
2018-03-07 08:55:04 +01:00
|
|
|
|
|
|
|
uefi_call_wrapper(linux_dir->Close, 1, linux_dir);
|
2015-02-08 12:25:35 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 18:45:58 +02:00
|
|
|
static EFI_STATUS image_start(
|
|
|
|
EFI_HANDLE parent_image,
|
|
|
|
const Config *config,
|
|
|
|
const ConfigEntry *entry) {
|
|
|
|
|
2015-02-08 12:25:35 +01:00
|
|
|
EFI_HANDLE image;
|
2018-03-13 10:47:17 +01:00
|
|
|
_cleanup_freepool_ EFI_DEVICE_PATH *path = NULL;
|
2015-02-08 12:25:35 +01:00
|
|
|
CHAR16 *options;
|
|
|
|
EFI_STATUS err;
|
|
|
|
|
|
|
|
path = FileDevicePath(entry->device, entry->loader);
|
|
|
|
if (!path) {
|
|
|
|
Print(L"Error getting device path.");
|
|
|
|
uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = uefi_call_wrapper(BS->LoadImage, 6, FALSE, parent_image, path, NULL, 0, &image);
|
|
|
|
if (EFI_ERROR(err)) {
|
|
|
|
Print(L"Error loading %s: %r", entry->loader, err);
|
|
|
|
uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
|
2018-03-13 10:47:17 +01:00
|
|
|
return err;
|
2015-02-08 12:25:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (config->options_edit)
|
|
|
|
options = config->options_edit;
|
|
|
|
else if (entry->options)
|
|
|
|
options = entry->options;
|
|
|
|
else
|
|
|
|
options = NULL;
|
|
|
|
if (options) {
|
|
|
|
EFI_LOADED_IMAGE *loaded_image;
|
|
|
|
|
|
|
|
err = uefi_call_wrapper(BS->OpenProtocol, 6, image, &LoadedImageProtocol, (VOID **)&loaded_image,
|
|
|
|
parent_image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
|
|
|
|
if (EFI_ERROR(err)) {
|
|
|
|
Print(L"Error getting LoadedImageProtocol handle: %r", err);
|
|
|
|
uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
|
|
|
|
goto out_unload;
|
|
|
|
}
|
|
|
|
loaded_image->LoadOptions = options;
|
|
|
|
loaded_image->LoadOptionsSize = (StrLen(loaded_image->LoadOptions)+1) * sizeof(CHAR16);
|
2016-02-03 17:39:24 +01:00
|
|
|
|
2017-10-03 10:41:51 +02:00
|
|
|
#if ENABLE_TPM
|
2017-01-28 05:06:57 +01:00
|
|
|
/* Try to log any options to the TPM, especially to catch manually edited options */
|
2016-02-03 17:39:24 +01:00
|
|
|
err = tpm_log_event(SD_TPM_PCR,
|
2018-05-29 17:12:39 +02:00
|
|
|
(EFI_PHYSICAL_ADDRESS) (UINTN) loaded_image->LoadOptions,
|
2016-02-03 17:39:24 +01:00
|
|
|
loaded_image->LoadOptionsSize, loaded_image->LoadOptions);
|
|
|
|
if (EFI_ERROR(err)) {
|
|
|
|
Print(L"Unable to add image options measurement: %r", err);
|
2017-07-28 09:46:05 +02:00
|
|
|
uefi_call_wrapper(BS->Stall, 1, 200 * 1000);
|
2016-02-03 17:39:24 +01:00
|
|
|
}
|
|
|
|
#endif
|
2015-02-08 12:25:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
efivar_set_time_usec(L"LoaderTimeExecUSec", 0);
|
|
|
|
err = uefi_call_wrapper(BS->StartImage, 3, image, NULL, NULL);
|
|
|
|
out_unload:
|
|
|
|
uefi_call_wrapper(BS->UnloadImage, 1, image);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static EFI_STATUS reboot_into_firmware(VOID) {
|
2018-03-13 10:47:17 +01:00
|
|
|
_cleanup_freepool_ CHAR8 *b = NULL;
|
2015-02-08 12:25:35 +01:00
|
|
|
UINTN size;
|
|
|
|
UINT64 osind;
|
|
|
|
EFI_STATUS err;
|
|
|
|
|
|
|
|
osind = EFI_OS_INDICATIONS_BOOT_TO_FW_UI;
|
|
|
|
|
|
|
|
err = efivar_get_raw(&global_guid, L"OsIndications", &b, &size);
|
|
|
|
if (!EFI_ERROR(err))
|
|
|
|
osind |= (UINT64)*b;
|
|
|
|
|
2018-06-25 18:18:20 +02:00
|
|
|
err = efivar_set_raw(&global_guid, L"OsIndications", &osind, sizeof(UINT64), TRUE);
|
2015-02-08 12:25:35 +01:00
|
|
|
if (EFI_ERROR(err))
|
|
|
|
return err;
|
|
|
|
|
|
|
|
err = uefi_call_wrapper(RT->ResetSystem, 4, EfiResetCold, EFI_SUCCESS, 0, NULL);
|
|
|
|
Print(L"Error calling ResetSystem: %r", err);
|
|
|
|
uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VOID config_free(Config *config) {
|
|
|
|
UINTN i;
|
|
|
|
|
|
|
|
for (i = 0; i < config->entry_count; i++)
|
|
|
|
config_entry_free(config->entries[i]);
|
|
|
|
FreePool(config->entries);
|
|
|
|
FreePool(config->entry_default_pattern);
|
|
|
|
FreePool(config->options_edit);
|
|
|
|
FreePool(config->entry_oneshot);
|
|
|
|
}
|
|
|
|
|
|
|
|
EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
|
2018-03-13 10:47:17 +01:00
|
|
|
_cleanup_freepool_ CHAR16 *infostr = NULL, *typestr = NULL;
|
2015-02-08 12:25:35 +01:00
|
|
|
CHAR8 *b;
|
|
|
|
UINTN size;
|
|
|
|
EFI_LOADED_IMAGE *loaded_image;
|
|
|
|
EFI_FILE *root_dir;
|
|
|
|
CHAR16 *loaded_image_path;
|
|
|
|
EFI_STATUS err;
|
|
|
|
Config config;
|
|
|
|
UINT64 init_usec;
|
|
|
|
BOOLEAN menu = FALSE;
|
2015-07-29 19:26:49 +02:00
|
|
|
CHAR16 uuid[37];
|
2015-02-08 12:25:35 +01:00
|
|
|
|
|
|
|
InitializeLib(image, sys_table);
|
|
|
|
init_usec = time_usec();
|
|
|
|
efivar_set_time_usec(L"LoaderTimeInitUSec", init_usec);
|
2017-04-03 18:15:33 +02:00
|
|
|
efivar_set(L"LoaderInfo", L"systemd-boot " PACKAGE_VERSION, FALSE);
|
2018-03-13 10:47:17 +01:00
|
|
|
|
|
|
|
infostr = PoolPrint(L"%s %d.%02d", ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
|
|
|
|
efivar_set(L"LoaderFirmwareInfo", infostr, FALSE);
|
|
|
|
|
|
|
|
typestr = PoolPrint(L"UEFI %d.%02d", ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff);
|
|
|
|
efivar_set(L"LoaderFirmwareType", typestr, FALSE);
|
2015-02-08 12:25:35 +01:00
|
|
|
|
|
|
|
err = uefi_call_wrapper(BS->OpenProtocol, 6, image, &LoadedImageProtocol, (VOID **)&loaded_image,
|
|
|
|
image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
|
|
|
|
if (EFI_ERROR(err)) {
|
|
|
|
Print(L"Error getting a LoadedImageProtocol handle: %r ", err);
|
|
|
|
uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* export the device path this image is started from */
|
2015-07-29 19:26:49 +02:00
|
|
|
if (disk_get_part_uuid(loaded_image->DeviceHandle, uuid) == EFI_SUCCESS)
|
|
|
|
efivar_set(L"LoaderDevicePartUUID", uuid, FALSE);
|
2015-02-08 12:25:35 +01:00
|
|
|
|
|
|
|
root_dir = LibOpenRoot(loaded_image->DeviceHandle);
|
|
|
|
if (!root_dir) {
|
|
|
|
Print(L"Unable to open root directory: %r ", err);
|
|
|
|
uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
|
|
|
|
return EFI_LOAD_ERROR;
|
|
|
|
}
|
|
|
|
|
2017-05-09 20:57:40 +02:00
|
|
|
if (secure_boot_enabled() && shim_loaded()) {
|
|
|
|
err = security_policy_install();
|
|
|
|
if (EFI_ERROR(err)) {
|
|
|
|
Print(L"Error installing security policy: %r ", err);
|
|
|
|
uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
2015-02-08 12:25:35 +01:00
|
|
|
|
|
|
|
/* the filesystem path to this image, to prevent adding ourselves to the menu */
|
|
|
|
loaded_image_path = DevicePathToStr(loaded_image->FilePath);
|
|
|
|
efivar_set(L"LoaderImageIdentifier", loaded_image_path, FALSE);
|
|
|
|
|
|
|
|
ZeroMem(&config, sizeof(Config));
|
2015-07-30 14:09:19 +02:00
|
|
|
config_load_defaults(&config, root_dir);
|
2015-02-08 12:25:35 +01:00
|
|
|
|
2015-07-30 14:09:19 +02:00
|
|
|
/* scan /EFI/Linux/ directory */
|
2015-02-08 12:25:35 +01:00
|
|
|
config_entry_add_linux(&config, loaded_image, root_dir);
|
2015-07-30 14:09:19 +02:00
|
|
|
|
|
|
|
/* scan /loader/entries/\*.conf files */
|
|
|
|
config_load_entries(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path);
|
|
|
|
|
|
|
|
/* sort entries after version number */
|
|
|
|
config_sort_entries(&config);
|
|
|
|
|
|
|
|
/* if we find some well-known loaders, add them to the end of the list */
|
2018-01-06 19:21:48 +01:00
|
|
|
config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, NULL,
|
2015-02-08 12:25:35 +01:00
|
|
|
L"auto-windows", 'w', L"Windows Boot Manager", L"\\EFI\\Microsoft\\Boot\\bootmgfw.efi");
|
2018-01-06 19:21:48 +01:00
|
|
|
config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, NULL,
|
2015-02-08 12:25:35 +01:00
|
|
|
L"auto-efi-shell", 's', L"EFI Shell", L"\\shell" EFI_MACHINE_TYPE_NAME ".efi");
|
|
|
|
config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path,
|
|
|
|
L"auto-efi-default", '\0', L"EFI Default Loader", L"\\EFI\\Boot\\boot" EFI_MACHINE_TYPE_NAME ".efi");
|
|
|
|
config_entry_add_osx(&config);
|
|
|
|
|
2018-01-28 17:06:22 +01:00
|
|
|
if (config.auto_firmware && efivar_get_raw(&global_guid, L"OsIndicationsSupported", &b, &size) == EFI_SUCCESS) {
|
2015-02-08 12:25:35 +01:00
|
|
|
UINT64 osind = (UINT64)*b;
|
|
|
|
|
|
|
|
if (osind & EFI_OS_INDICATIONS_BOOT_TO_FW_UI)
|
|
|
|
config_entry_add_call(&config, L"Reboot Into Firmware Interface", reboot_into_firmware);
|
|
|
|
FreePool(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.entry_count == 0) {
|
|
|
|
Print(L"No loader found. Configuration files in \\loader\\entries\\*.conf are needed.");
|
|
|
|
uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
config_title_generate(&config);
|
|
|
|
|
2017-02-24 18:14:02 +01:00
|
|
|
/* select entry by configured pattern or EFI LoaderDefaultEntry= variable */
|
2015-02-08 12:25:35 +01:00
|
|
|
config_default_entry_select(&config);
|
|
|
|
|
|
|
|
/* if no configured entry to select from was found, enable the menu */
|
|
|
|
if (config.idx_default == -1) {
|
|
|
|
config.idx_default = 0;
|
|
|
|
if (config.timeout_sec == 0)
|
|
|
|
config.timeout_sec = 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* select entry or show menu when key is pressed or timeout is set */
|
|
|
|
if (config.timeout_sec == 0) {
|
|
|
|
UINT64 key;
|
|
|
|
|
|
|
|
err = console_key_read(&key, FALSE);
|
|
|
|
if (!EFI_ERROR(err)) {
|
|
|
|
INT16 idx;
|
|
|
|
|
|
|
|
/* find matching key in config entries */
|
|
|
|
idx = entry_lookup_key(&config, config.idx_default, KEYCHAR(key));
|
|
|
|
if (idx >= 0)
|
|
|
|
config.idx_default = idx;
|
|
|
|
else
|
|
|
|
menu = TRUE;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
menu = TRUE;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
ConfigEntry *entry;
|
|
|
|
|
|
|
|
entry = config.entries[config.idx_default];
|
|
|
|
if (menu) {
|
|
|
|
efivar_set_time_usec(L"LoaderTimeMenuUSec", 0);
|
|
|
|
uefi_call_wrapper(BS->SetWatchdogTimer, 4, 0, 0x10000, 0, NULL);
|
2015-02-26 19:51:08 +01:00
|
|
|
if (!menu_run(&config, &entry, loaded_image_path))
|
2015-02-08 12:25:35 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* run special entry like "reboot" */
|
|
|
|
if (entry->call) {
|
|
|
|
entry->call();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-20 18:25:01 +02:00
|
|
|
config_entry_bump_counters(entry, root_dir);
|
|
|
|
|
2015-02-08 12:25:35 +01:00
|
|
|
/* export the selected boot entry to the system */
|
2018-06-20 18:49:10 +02:00
|
|
|
efivar_set(L"LoaderEntrySelected", entry->id, FALSE);
|
2015-02-08 12:25:35 +01:00
|
|
|
|
|
|
|
uefi_call_wrapper(BS->SetWatchdogTimer, 4, 5 * 60, 0x10000, 0, NULL);
|
|
|
|
err = image_start(image, &config, entry);
|
2015-02-26 01:15:18 +01:00
|
|
|
if (EFI_ERROR(err)) {
|
2015-02-26 18:33:37 +01:00
|
|
|
graphics_mode(FALSE);
|
2015-02-26 01:15:18 +01:00
|
|
|
Print(L"\nFailed to execute %s (%s): %r\n", entry->title, entry->loader, err);
|
2015-02-08 12:25:35 +01:00
|
|
|
uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
menu = TRUE;
|
|
|
|
config.timeout_sec = 0;
|
|
|
|
}
|
|
|
|
err = EFI_SUCCESS;
|
|
|
|
out:
|
|
|
|
FreePool(loaded_image_path);
|
|
|
|
config_free(&config);
|
|
|
|
uefi_call_wrapper(root_dir->Close, 1, root_dir);
|
|
|
|
uefi_call_wrapper(BS->CloseProtocol, 4, image, &LoadedImageProtocol, image, NULL);
|
|
|
|
return err;
|
|
|
|
}
|