]>
Commit | Line | Data |
---|---|---|
0fa2cac4 KS |
1 | /* |
2 | * This program is free software; you can redistribute it and/or modify it | |
3 | * under the terms of the GNU Lesser General Public License as published by | |
4 | * the Free Software Foundation; either version 2.1 of the License, or | |
5 | * (at your option) any later version. | |
6 | * | |
7 | * This program is distributed in the hope that it will be useful, but | |
8 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
10 | * Lesser General Public License for more details. | |
11 | * | |
12 | * Copyright (C) 2012-2015 Kay Sievers <kay@vrfy.org> | |
13 | * Copyright (C) 2012-2015 Harald Hoyer <harald@redhat.com> | |
14 | */ | |
15 | ||
16 | #include <efi.h> | |
17 | #include <efilib.h> | |
18 | ||
0fa2cac4 | 19 | #include "console.h" |
8110e144 | 20 | #include "disk.h" |
cf0fbc49 | 21 | #include "graphics.h" |
0fa2cac4 | 22 | #include "linux.h" |
92ed3bb4 | 23 | #include "measure.h" |
d4cbada2 | 24 | #include "pe.h" |
b2bb40ce | 25 | #include "shim.h" |
d4cbada2 | 26 | #include "util.h" |
0fa2cac4 KS |
27 | |
28 | #ifndef EFI_OS_INDICATIONS_BOOT_TO_FW_UI | |
29 | #define EFI_OS_INDICATIONS_BOOT_TO_FW_UI 0x0000000000000001ULL | |
30 | #endif | |
31 | ||
32 | /* magic string to find in the binary image */ | |
948aaa7c | 33 | static const char __attribute__((used)) magic[] = "#### LoaderInfo: systemd-boot " PACKAGE_VERSION " ####"; |
0fa2cac4 KS |
34 | |
35 | static const EFI_GUID global_guid = EFI_GLOBAL_VARIABLE; | |
36 | ||
37 | enum loader_type { | |
38 | LOADER_UNDEFINED, | |
39 | LOADER_EFI, | |
40 | LOADER_LINUX | |
41 | }; | |
42 | ||
43 | typedef struct { | |
44 | CHAR16 *file; | |
45 | CHAR16 *title_show; | |
46 | CHAR16 *title; | |
47 | CHAR16 *version; | |
48 | CHAR16 *machine_id; | |
49 | EFI_HANDLE *device; | |
50 | enum loader_type type; | |
51 | CHAR16 *loader; | |
52 | CHAR16 *options; | |
0fa2cac4 KS |
53 | CHAR16 key; |
54 | EFI_STATUS (*call)(VOID); | |
55 | BOOLEAN no_autoselect; | |
56 | BOOLEAN non_unique; | |
57 | } ConfigEntry; | |
58 | ||
59 | typedef struct { | |
60 | ConfigEntry **entries; | |
61 | UINTN entry_count; | |
62 | INTN idx_default; | |
63 | INTN idx_default_efivar; | |
64 | UINTN timeout_sec; | |
65 | UINTN timeout_sec_config; | |
66 | INTN timeout_sec_efivar; | |
67 | CHAR16 *entry_default_pattern; | |
0fa2cac4 KS |
68 | CHAR16 *entry_oneshot; |
69 | CHAR16 *options_edit; | |
4db7e6d7 | 70 | BOOLEAN no_editor; |
0fa2cac4 KS |
71 | } Config; |
72 | ||
52efd56a | 73 | static VOID cursor_left(UINTN *cursor, UINTN *first) { |
0fa2cac4 KS |
74 | if ((*cursor) > 0) |
75 | (*cursor)--; | |
76 | else if ((*first) > 0) | |
77 | (*first)--; | |
78 | } | |
79 | ||
52efd56a | 80 | static VOID cursor_right(UINTN *cursor, UINTN *first, UINTN x_max, UINTN len) { |
0fa2cac4 KS |
81 | if ((*cursor)+1 < x_max) |
82 | (*cursor)++; | |
83 | else if ((*first) + (*cursor) < len) | |
84 | (*first)++; | |
85 | } | |
86 | ||
87 | static BOOLEAN line_edit(CHAR16 *line_in, CHAR16 **line_out, UINTN x_max, UINTN y_pos) { | |
88 | CHAR16 *line; | |
89 | UINTN size; | |
90 | UINTN len; | |
91 | UINTN first; | |
92 | CHAR16 *print; | |
93 | UINTN cursor; | |
94 | UINTN clear; | |
95 | BOOLEAN exit; | |
96 | BOOLEAN enter; | |
97 | ||
98 | if (!line_in) | |
99 | line_in = L""; | |
100 | size = StrLen(line_in) + 1024; | |
101 | line = AllocatePool(size * sizeof(CHAR16)); | |
102 | StrCpy(line, line_in); | |
103 | len = StrLen(line); | |
104 | print = AllocatePool((x_max+1) * sizeof(CHAR16)); | |
105 | ||
106 | uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, TRUE); | |
107 | ||
108 | first = 0; | |
109 | cursor = 0; | |
110 | clear = 0; | |
111 | enter = FALSE; | |
112 | exit = FALSE; | |
113 | while (!exit) { | |
114 | EFI_STATUS err; | |
115 | UINT64 key; | |
116 | UINTN i; | |
117 | ||
118 | i = len - first; | |
119 | if (i >= x_max-1) | |
120 | i = x_max-1; | |
121 | CopyMem(print, line + first, i * sizeof(CHAR16)); | |
122 | while (clear > 0 && i < x_max-1) { | |
123 | clear--; | |
124 | print[i++] = ' '; | |
125 | } | |
126 | print[i] = '\0'; | |
127 | ||
128 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_pos); | |
129 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, print); | |
130 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos); | |
131 | ||
132 | err = console_key_read(&key, TRUE); | |
133 | if (EFI_ERROR(err)) | |
134 | continue; | |
135 | ||
136 | switch (key) { | |
137 | case KEYPRESS(0, SCAN_ESC, 0): | |
138 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'c'): | |
139 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'g'): | |
140 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('c')): | |
141 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('g')): | |
142 | exit = TRUE; | |
143 | break; | |
144 | ||
145 | case KEYPRESS(0, SCAN_HOME, 0): | |
146 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'a'): | |
147 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('a')): | |
148 | /* beginning-of-line */ | |
149 | cursor = 0; | |
150 | first = 0; | |
151 | continue; | |
152 | ||
153 | case KEYPRESS(0, SCAN_END, 0): | |
154 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'e'): | |
155 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('e')): | |
156 | /* end-of-line */ | |
157 | cursor = len - first; | |
158 | if (cursor+1 >= x_max) { | |
159 | cursor = x_max-1; | |
160 | first = len - (x_max-1); | |
161 | } | |
162 | continue; | |
163 | ||
164 | case KEYPRESS(0, SCAN_DOWN, 0): | |
165 | case KEYPRESS(EFI_ALT_PRESSED, 0, 'f'): | |
166 | case KEYPRESS(EFI_CONTROL_PRESSED, SCAN_RIGHT, 0): | |
167 | /* forward-word */ | |
168 | while (line[first + cursor] && line[first + cursor] == ' ') | |
169 | cursor_right(&cursor, &first, x_max, len); | |
170 | while (line[first + cursor] && line[first + cursor] != ' ') | |
171 | cursor_right(&cursor, &first, x_max, len); | |
172 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos); | |
173 | continue; | |
174 | ||
175 | case KEYPRESS(0, SCAN_UP, 0): | |
176 | case KEYPRESS(EFI_ALT_PRESSED, 0, 'b'): | |
177 | case KEYPRESS(EFI_CONTROL_PRESSED, SCAN_LEFT, 0): | |
178 | /* backward-word */ | |
179 | if ((first + cursor) > 0 && line[first + cursor-1] == ' ') { | |
180 | cursor_left(&cursor, &first); | |
181 | while ((first + cursor) > 0 && line[first + cursor] == ' ') | |
182 | cursor_left(&cursor, &first); | |
183 | } | |
184 | while ((first + cursor) > 0 && line[first + cursor-1] != ' ') | |
185 | cursor_left(&cursor, &first); | |
186 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos); | |
187 | continue; | |
188 | ||
189 | case KEYPRESS(0, SCAN_RIGHT, 0): | |
190 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'f'): | |
191 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('f')): | |
192 | /* forward-char */ | |
193 | if (first + cursor == len) | |
194 | continue; | |
195 | cursor_right(&cursor, &first, x_max, len); | |
196 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos); | |
197 | continue; | |
198 | ||
199 | case KEYPRESS(0, SCAN_LEFT, 0): | |
200 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'b'): | |
201 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('b')): | |
202 | /* backward-char */ | |
203 | cursor_left(&cursor, &first); | |
204 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos); | |
205 | continue; | |
206 | ||
207 | case KEYPRESS(EFI_ALT_PRESSED, 0, 'd'): | |
208 | /* kill-word */ | |
209 | clear = 0; | |
210 | for (i = first + cursor; i < len && line[i] == ' '; i++) | |
211 | clear++; | |
212 | for (; i < len && line[i] != ' '; i++) | |
213 | clear++; | |
214 | ||
215 | for (i = first + cursor; i + clear < len; i++) | |
216 | line[i] = line[i + clear]; | |
217 | len -= clear; | |
218 | line[len] = '\0'; | |
219 | continue; | |
220 | ||
221 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'w'): | |
222 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('w')): | |
223 | case KEYPRESS(EFI_ALT_PRESSED, 0, CHAR_BACKSPACE): | |
224 | /* backward-kill-word */ | |
225 | clear = 0; | |
226 | if ((first + cursor) > 0 && line[first + cursor-1] == ' ') { | |
227 | cursor_left(&cursor, &first); | |
228 | clear++; | |
229 | while ((first + cursor) > 0 && line[first + cursor] == ' ') { | |
230 | cursor_left(&cursor, &first); | |
231 | clear++; | |
232 | } | |
233 | } | |
234 | while ((first + cursor) > 0 && line[first + cursor-1] != ' ') { | |
235 | cursor_left(&cursor, &first); | |
236 | clear++; | |
237 | } | |
238 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, cursor, y_pos); | |
239 | ||
240 | for (i = first + cursor; i + clear < len; i++) | |
241 | line[i] = line[i + clear]; | |
242 | len -= clear; | |
243 | line[len] = '\0'; | |
244 | continue; | |
245 | ||
246 | case KEYPRESS(0, SCAN_DELETE, 0): | |
247 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'd'): | |
248 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('d')): | |
249 | if (len == 0) | |
250 | continue; | |
251 | if (first + cursor == len) | |
252 | continue; | |
253 | for (i = first + cursor; i < len; i++) | |
254 | line[i] = line[i+1]; | |
255 | clear = 1; | |
256 | len--; | |
257 | continue; | |
258 | ||
259 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'k'): | |
260 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('k')): | |
261 | /* kill-line */ | |
262 | line[first + cursor] = '\0'; | |
263 | clear = len - (first + cursor); | |
264 | len = first + cursor; | |
265 | continue; | |
266 | ||
267 | case KEYPRESS(0, 0, CHAR_LINEFEED): | |
268 | case KEYPRESS(0, 0, CHAR_CARRIAGE_RETURN): | |
269 | if (StrCmp(line, line_in) != 0) { | |
270 | *line_out = line; | |
271 | line = NULL; | |
272 | } | |
273 | enter = TRUE; | |
274 | exit = TRUE; | |
275 | break; | |
276 | ||
277 | case KEYPRESS(0, 0, CHAR_BACKSPACE): | |
278 | if (len == 0) | |
279 | continue; | |
280 | if (first == 0 && cursor == 0) | |
281 | continue; | |
282 | for (i = first + cursor-1; i < len; i++) | |
283 | line[i] = line[i+1]; | |
284 | clear = 1; | |
285 | len--; | |
286 | if (cursor > 0) | |
287 | cursor--; | |
288 | if (cursor > 0 || first == 0) | |
289 | continue; | |
290 | /* show full line if it fits */ | |
291 | if (len < x_max) { | |
292 | cursor = first; | |
293 | first = 0; | |
294 | continue; | |
295 | } | |
296 | /* jump left to see what we delete */ | |
297 | if (first > 10) { | |
298 | first -= 10; | |
299 | cursor = 10; | |
300 | } else { | |
301 | cursor = first; | |
302 | first = 0; | |
303 | } | |
304 | continue; | |
305 | ||
306 | case KEYPRESS(0, 0, ' ') ... KEYPRESS(0, 0, '~'): | |
307 | case KEYPRESS(0, 0, 0x80) ... KEYPRESS(0, 0, 0xffff): | |
308 | if (len+1 == size) | |
309 | continue; | |
310 | for (i = len; i > first + cursor; i--) | |
311 | line[i] = line[i-1]; | |
312 | line[first + cursor] = KEYCHAR(key); | |
313 | len++; | |
314 | line[len] = '\0'; | |
315 | if (cursor+1 < x_max) | |
316 | cursor++; | |
317 | else if (first + cursor < len) | |
318 | first++; | |
319 | continue; | |
320 | } | |
321 | } | |
322 | ||
323 | uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, FALSE); | |
324 | FreePool(print); | |
325 | FreePool(line); | |
326 | return enter; | |
327 | } | |
328 | ||
329 | static UINTN entry_lookup_key(Config *config, UINTN start, CHAR16 key) { | |
330 | UINTN i; | |
331 | ||
332 | if (key == 0) | |
333 | return -1; | |
334 | ||
335 | /* select entry by number key */ | |
336 | if (key >= '1' && key <= '9') { | |
337 | i = key - '0'; | |
338 | if (i > config->entry_count) | |
339 | i = config->entry_count; | |
340 | return i-1; | |
341 | } | |
342 | ||
343 | /* find matching key in config entries */ | |
344 | for (i = start; i < config->entry_count; i++) | |
345 | if (config->entries[i]->key == key) | |
346 | return i; | |
347 | ||
348 | for (i = 0; i < start; i++) | |
349 | if (config->entries[i]->key == key) | |
350 | return i; | |
351 | ||
352 | return -1; | |
353 | } | |
354 | ||
7361099e | 355 | static VOID print_status(Config *config, CHAR16 *loaded_image_path) { |
0fa2cac4 KS |
356 | UINT64 key; |
357 | UINTN i; | |
358 | CHAR16 *s; | |
359 | CHAR8 *b; | |
360 | UINTN x; | |
361 | UINTN y; | |
362 | UINTN size; | |
0fa2cac4 KS |
363 | |
364 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK); | |
365 | uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut); | |
366 | ||
948aaa7c | 367 | Print(L"systemd-boot version: " PACKAGE_VERSION "\n"); |
0fa2cac4 KS |
368 | Print(L"architecture: " EFI_MACHINE_TYPE_NAME "\n"); |
369 | Print(L"loaded image: %s\n", loaded_image_path); | |
370 | Print(L"UEFI specification: %d.%02d\n", ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff); | |
371 | Print(L"firmware vendor: %s\n", ST->FirmwareVendor); | |
372 | Print(L"firmware version: %d.%02d\n", ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff); | |
373 | ||
374 | if (uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut, ST->ConOut->Mode->Mode, &x, &y) == EFI_SUCCESS) | |
375 | Print(L"console size: %d x %d\n", x, y); | |
376 | ||
377 | if (efivar_get_raw(&global_guid, L"SecureBoot", &b, &size) == EFI_SUCCESS) { | |
4db7e6d7 | 378 | Print(L"SecureBoot: %s\n", yes_no(*b > 0)); |
0fa2cac4 KS |
379 | FreePool(b); |
380 | } | |
381 | ||
382 | if (efivar_get_raw(&global_guid, L"SetupMode", &b, &size) == EFI_SUCCESS) { | |
383 | Print(L"SetupMode: %s\n", *b > 0 ? L"setup" : L"user"); | |
384 | FreePool(b); | |
385 | } | |
386 | ||
b2bb40ce MR |
387 | if (shim_loaded()) |
388 | Print(L"Shim: present\n"); | |
389 | ||
0fa2cac4 KS |
390 | if (efivar_get_raw(&global_guid, L"OsIndicationsSupported", &b, &size) == EFI_SUCCESS) { |
391 | Print(L"OsIndicationsSupported: %d\n", (UINT64)*b); | |
392 | FreePool(b); | |
393 | } | |
394 | Print(L"\n"); | |
395 | ||
396 | Print(L"timeout: %d\n", config->timeout_sec); | |
397 | if (config->timeout_sec_efivar >= 0) | |
398 | Print(L"timeout (EFI var): %d\n", config->timeout_sec_efivar); | |
399 | Print(L"timeout (config): %d\n", config->timeout_sec_config); | |
400 | if (config->entry_default_pattern) | |
401 | Print(L"default pattern: '%s'\n", config->entry_default_pattern); | |
4db7e6d7 | 402 | Print(L"editor: %s\n", yes_no(!config->no_editor)); |
0fa2cac4 KS |
403 | Print(L"\n"); |
404 | ||
405 | Print(L"config entry count: %d\n", config->entry_count); | |
406 | Print(L"entry selected idx: %d\n", config->idx_default); | |
407 | if (config->idx_default_efivar >= 0) | |
408 | Print(L"entry EFI var idx: %d\n", config->idx_default_efivar); | |
409 | Print(L"\n"); | |
410 | ||
411 | if (efivar_get_int(L"LoaderConfigTimeout", &i) == EFI_SUCCESS) | |
412 | Print(L"LoaderConfigTimeout: %d\n", i); | |
413 | if (config->entry_oneshot) | |
414 | Print(L"LoaderEntryOneShot: %s\n", config->entry_oneshot); | |
0fa2cac4 KS |
415 | if (efivar_get(L"LoaderDevicePartUUID", &s) == EFI_SUCCESS) { |
416 | Print(L"LoaderDevicePartUUID: %s\n", s); | |
417 | FreePool(s); | |
418 | } | |
419 | if (efivar_get(L"LoaderEntryDefault", &s) == EFI_SUCCESS) { | |
420 | Print(L"LoaderEntryDefault: %s\n", s); | |
421 | FreePool(s); | |
422 | } | |
423 | ||
424 | Print(L"\n--- press key ---\n\n"); | |
425 | console_key_read(&key, TRUE); | |
426 | ||
427 | for (i = 0; i < config->entry_count; i++) { | |
428 | ConfigEntry *entry; | |
429 | ||
430 | if (key == KEYPRESS(0, SCAN_ESC, 0) || key == KEYPRESS(0, 0, 'q')) | |
431 | break; | |
432 | ||
433 | entry = config->entries[i]; | |
0fa2cac4 KS |
434 | Print(L"config entry: %d/%d\n", i+1, config->entry_count); |
435 | if (entry->file) | |
436 | Print(L"file '%s'\n", entry->file); | |
437 | Print(L"title show '%s'\n", entry->title_show); | |
438 | if (entry->title) | |
439 | Print(L"title '%s'\n", entry->title); | |
440 | if (entry->version) | |
441 | Print(L"version '%s'\n", entry->version); | |
442 | if (entry->machine_id) | |
443 | Print(L"machine-id '%s'\n", entry->machine_id); | |
444 | if (entry->device) { | |
445 | EFI_DEVICE_PATH *device_path; | |
446 | CHAR16 *str; | |
447 | ||
448 | device_path = DevicePathFromHandle(entry->device); | |
449 | if (device_path) { | |
450 | str = DevicePathToStr(device_path); | |
451 | Print(L"device handle '%s'\n", str); | |
452 | FreePool(str); | |
453 | } | |
454 | } | |
455 | if (entry->loader) | |
456 | Print(L"loader '%s'\n", entry->loader); | |
457 | if (entry->options) | |
458 | Print(L"options '%s'\n", entry->options); | |
4db7e6d7 | 459 | Print(L"auto-select %s\n", yes_no(!entry->no_autoselect)); |
0fa2cac4 KS |
460 | if (entry->call) |
461 | Print(L"internal call yes\n"); | |
462 | ||
463 | Print(L"\n--- press key ---\n\n"); | |
464 | console_key_read(&key, TRUE); | |
465 | } | |
466 | ||
467 | uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut); | |
468 | } | |
469 | ||
7361099e | 470 | static BOOLEAN menu_run(Config *config, ConfigEntry **chosen_entry, CHAR16 *loaded_image_path) { |
0fa2cac4 KS |
471 | EFI_STATUS err; |
472 | UINTN visible_max; | |
473 | UINTN idx_highlight; | |
474 | UINTN idx_highlight_prev; | |
475 | UINTN idx_first; | |
476 | UINTN idx_last; | |
477 | BOOLEAN refresh; | |
478 | BOOLEAN highlight; | |
479 | UINTN i; | |
480 | UINTN line_width; | |
481 | CHAR16 **lines; | |
482 | UINTN x_start; | |
483 | UINTN y_start; | |
484 | UINTN x_max; | |
485 | UINTN y_max; | |
486 | CHAR16 *status; | |
487 | CHAR16 *clearline; | |
488 | INTN timeout_remain; | |
489 | INT16 idx; | |
490 | BOOLEAN exit = FALSE; | |
491 | BOOLEAN run = TRUE; | |
492 | BOOLEAN wait = FALSE; | |
493 | ||
494 | graphics_mode(FALSE); | |
495 | uefi_call_wrapper(ST->ConIn->Reset, 2, ST->ConIn, FALSE); | |
496 | uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, FALSE); | |
497 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK); | |
498 | ||
499 | /* draw a single character to make ClearScreen work on some firmware */ | |
500 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L" "); | |
501 | uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut); | |
502 | ||
503 | err = uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut, ST->ConOut->Mode->Mode, &x_max, &y_max); | |
504 | if (EFI_ERROR(err)) { | |
505 | x_max = 80; | |
506 | y_max = 25; | |
507 | } | |
508 | ||
509 | /* we check 10 times per second for a keystroke */ | |
510 | if (config->timeout_sec > 0) | |
511 | timeout_remain = config->timeout_sec * 10; | |
512 | else | |
513 | timeout_remain = -1; | |
514 | ||
515 | idx_highlight = config->idx_default; | |
516 | idx_highlight_prev = 0; | |
517 | ||
518 | visible_max = y_max - 2; | |
519 | ||
520 | if ((UINTN)config->idx_default >= visible_max) | |
521 | idx_first = config->idx_default-1; | |
522 | else | |
523 | idx_first = 0; | |
524 | ||
525 | idx_last = idx_first + visible_max-1; | |
526 | ||
527 | refresh = TRUE; | |
528 | highlight = FALSE; | |
529 | ||
530 | /* length of the longest entry */ | |
531 | line_width = 5; | |
532 | for (i = 0; i < config->entry_count; i++) { | |
533 | UINTN entry_len; | |
534 | ||
535 | entry_len = StrLen(config->entries[i]->title_show); | |
536 | if (line_width < entry_len) | |
537 | line_width = entry_len; | |
538 | } | |
539 | if (line_width > x_max-6) | |
540 | line_width = x_max-6; | |
541 | ||
542 | /* offsets to center the entries on the screen */ | |
543 | x_start = (x_max - (line_width)) / 2; | |
544 | if (config->entry_count < visible_max) | |
545 | y_start = ((visible_max - config->entry_count) / 2) + 1; | |
546 | else | |
547 | y_start = 0; | |
548 | ||
549 | /* menu entries title lines */ | |
550 | lines = AllocatePool(sizeof(CHAR16 *) * config->entry_count); | |
551 | for (i = 0; i < config->entry_count; i++) { | |
552 | UINTN j, k; | |
553 | ||
554 | lines[i] = AllocatePool(((x_max+1) * sizeof(CHAR16))); | |
555 | for (j = 0; j < x_start; j++) | |
556 | lines[i][j] = ' '; | |
557 | ||
558 | for (k = 0; config->entries[i]->title_show[k] != '\0' && j < x_max; j++, k++) | |
559 | lines[i][j] = config->entries[i]->title_show[k]; | |
560 | ||
561 | for (; j < x_max; j++) | |
562 | lines[i][j] = ' '; | |
563 | lines[i][x_max] = '\0'; | |
564 | } | |
565 | ||
566 | status = NULL; | |
567 | clearline = AllocatePool((x_max+1) * sizeof(CHAR16)); | |
568 | for (i = 0; i < x_max; i++) | |
569 | clearline[i] = ' '; | |
570 | clearline[i] = 0; | |
571 | ||
572 | while (!exit) { | |
573 | UINT64 key; | |
574 | ||
575 | if (refresh) { | |
576 | for (i = 0; i < config->entry_count; i++) { | |
577 | if (i < idx_first || i > idx_last) | |
578 | continue; | |
579 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_start + i - idx_first); | |
580 | if (i == idx_highlight) | |
581 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, | |
582 | EFI_BLACK|EFI_BACKGROUND_LIGHTGRAY); | |
583 | else | |
584 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, | |
585 | EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK); | |
586 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, lines[i]); | |
587 | if ((INTN)i == config->idx_default_efivar) { | |
588 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, x_start-3, y_start + i - idx_first); | |
589 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"=>"); | |
590 | } | |
591 | } | |
592 | refresh = FALSE; | |
593 | } else if (highlight) { | |
594 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_start + idx_highlight_prev - idx_first); | |
595 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK); | |
596 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, lines[idx_highlight_prev]); | |
597 | if ((INTN)idx_highlight_prev == config->idx_default_efivar) { | |
598 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, x_start-3, y_start + idx_highlight_prev - idx_first); | |
599 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"=>"); | |
600 | } | |
601 | ||
602 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_start + idx_highlight - idx_first); | |
603 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_BLACK|EFI_BACKGROUND_LIGHTGRAY); | |
604 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, lines[idx_highlight]); | |
605 | if ((INTN)idx_highlight == config->idx_default_efivar) { | |
606 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, x_start-3, y_start + idx_highlight - idx_first); | |
607 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"=>"); | |
608 | } | |
609 | highlight = FALSE; | |
610 | } | |
611 | ||
612 | if (timeout_remain > 0) { | |
613 | FreePool(status); | |
614 | status = PoolPrint(L"Boot in %d sec.", (timeout_remain + 5) / 10); | |
615 | } | |
616 | ||
617 | /* print status at last line of screen */ | |
618 | if (status) { | |
619 | UINTN len; | |
620 | UINTN x; | |
621 | ||
622 | /* center line */ | |
623 | len = StrLen(status); | |
624 | if (len < x_max) | |
625 | x = (x_max - len) / 2; | |
626 | else | |
627 | x = 0; | |
628 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK); | |
629 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1); | |
630 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline + (x_max - x)); | |
631 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, status); | |
632 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1 + x + len); | |
633 | } | |
634 | ||
635 | err = console_key_read(&key, wait); | |
636 | if (EFI_ERROR(err)) { | |
637 | /* timeout reached */ | |
638 | if (timeout_remain == 0) { | |
639 | exit = TRUE; | |
640 | break; | |
641 | } | |
642 | ||
643 | /* sleep and update status */ | |
644 | if (timeout_remain > 0) { | |
645 | uefi_call_wrapper(BS->Stall, 1, 100 * 1000); | |
646 | timeout_remain--; | |
647 | continue; | |
648 | } | |
649 | ||
650 | /* timeout disabled, wait for next key */ | |
651 | wait = TRUE; | |
652 | continue; | |
653 | } | |
654 | ||
655 | timeout_remain = -1; | |
656 | ||
657 | /* clear status after keystroke */ | |
658 | if (status) { | |
659 | FreePool(status); | |
660 | status = NULL; | |
661 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK); | |
662 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1); | |
663 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1); | |
664 | } | |
665 | ||
666 | idx_highlight_prev = idx_highlight; | |
667 | ||
668 | switch (key) { | |
669 | case KEYPRESS(0, SCAN_UP, 0): | |
670 | case KEYPRESS(0, 0, 'k'): | |
671 | if (idx_highlight > 0) | |
672 | idx_highlight--; | |
673 | break; | |
674 | ||
675 | case KEYPRESS(0, SCAN_DOWN, 0): | |
676 | case KEYPRESS(0, 0, 'j'): | |
677 | if (idx_highlight < config->entry_count-1) | |
678 | idx_highlight++; | |
679 | break; | |
680 | ||
681 | case KEYPRESS(0, SCAN_HOME, 0): | |
682 | case KEYPRESS(EFI_ALT_PRESSED, 0, '<'): | |
683 | if (idx_highlight > 0) { | |
684 | refresh = TRUE; | |
685 | idx_highlight = 0; | |
686 | } | |
687 | break; | |
688 | ||
689 | case KEYPRESS(0, SCAN_END, 0): | |
690 | case KEYPRESS(EFI_ALT_PRESSED, 0, '>'): | |
691 | if (idx_highlight < config->entry_count-1) { | |
692 | refresh = TRUE; | |
693 | idx_highlight = config->entry_count-1; | |
694 | } | |
695 | break; | |
696 | ||
697 | case KEYPRESS(0, SCAN_PAGE_UP, 0): | |
698 | if (idx_highlight > visible_max) | |
699 | idx_highlight -= visible_max; | |
700 | else | |
701 | idx_highlight = 0; | |
702 | break; | |
703 | ||
704 | case KEYPRESS(0, SCAN_PAGE_DOWN, 0): | |
705 | idx_highlight += visible_max; | |
706 | if (idx_highlight > config->entry_count-1) | |
707 | idx_highlight = config->entry_count-1; | |
708 | break; | |
709 | ||
710 | case KEYPRESS(0, 0, CHAR_LINEFEED): | |
711 | case KEYPRESS(0, 0, CHAR_CARRIAGE_RETURN): | |
712 | exit = TRUE; | |
713 | break; | |
714 | ||
715 | case KEYPRESS(0, SCAN_F1, 0): | |
716 | case KEYPRESS(0, 0, 'h'): | |
717 | case KEYPRESS(0, 0, '?'): | |
718 | status = StrDuplicate(L"(d)efault, (t/T)timeout, (e)dit, (v)ersion (Q)uit (P)rint (h)elp"); | |
719 | break; | |
720 | ||
721 | case KEYPRESS(0, 0, 'Q'): | |
722 | exit = TRUE; | |
723 | run = FALSE; | |
724 | break; | |
725 | ||
726 | case KEYPRESS(0, 0, 'd'): | |
727 | if (config->idx_default_efivar != (INTN)idx_highlight) { | |
728 | /* store the selected entry in a persistent EFI variable */ | |
729 | efivar_set(L"LoaderEntryDefault", config->entries[idx_highlight]->file, TRUE); | |
730 | config->idx_default_efivar = idx_highlight; | |
731 | status = StrDuplicate(L"Default boot entry selected."); | |
732 | } else { | |
733 | /* clear the default entry EFI variable */ | |
734 | efivar_set(L"LoaderEntryDefault", NULL, TRUE); | |
735 | config->idx_default_efivar = -1; | |
736 | status = StrDuplicate(L"Default boot entry cleared."); | |
737 | } | |
738 | refresh = TRUE; | |
739 | break; | |
740 | ||
741 | case KEYPRESS(0, 0, '-'): | |
742 | case KEYPRESS(0, 0, 'T'): | |
743 | if (config->timeout_sec_efivar > 0) { | |
744 | config->timeout_sec_efivar--; | |
745 | efivar_set_int(L"LoaderConfigTimeout", config->timeout_sec_efivar, TRUE); | |
746 | if (config->timeout_sec_efivar > 0) | |
747 | status = PoolPrint(L"Menu timeout set to %d sec.", config->timeout_sec_efivar); | |
748 | else | |
749 | status = StrDuplicate(L"Menu disabled. Hold down key at bootup to show menu."); | |
750 | } else if (config->timeout_sec_efivar <= 0){ | |
751 | config->timeout_sec_efivar = -1; | |
752 | efivar_set(L"LoaderConfigTimeout", NULL, TRUE); | |
753 | if (config->timeout_sec_config > 0) | |
754 | status = PoolPrint(L"Menu timeout of %d sec is defined by configuration file.", | |
755 | config->timeout_sec_config); | |
756 | else | |
757 | status = StrDuplicate(L"Menu disabled. Hold down key at bootup to show menu."); | |
758 | } | |
759 | break; | |
760 | ||
761 | case KEYPRESS(0, 0, '+'): | |
762 | case KEYPRESS(0, 0, 't'): | |
763 | if (config->timeout_sec_efivar == -1 && config->timeout_sec_config == 0) | |
764 | config->timeout_sec_efivar++; | |
765 | config->timeout_sec_efivar++; | |
766 | efivar_set_int(L"LoaderConfigTimeout", config->timeout_sec_efivar, TRUE); | |
767 | if (config->timeout_sec_efivar > 0) | |
768 | status = PoolPrint(L"Menu timeout set to %d sec.", | |
769 | config->timeout_sec_efivar); | |
770 | else | |
771 | status = StrDuplicate(L"Menu disabled. Hold down key at bootup to show menu."); | |
772 | break; | |
773 | ||
774 | case KEYPRESS(0, 0, 'e'): | |
775 | /* only the options of configured entries can be edited */ | |
4db7e6d7 | 776 | if (config->no_editor || config->entries[idx_highlight]->type == LOADER_UNDEFINED) |
0fa2cac4 KS |
777 | break; |
778 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK); | |
779 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1); | |
780 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1); | |
781 | if (line_edit(config->entries[idx_highlight]->options, &config->options_edit, x_max-1, y_max-1)) | |
782 | exit = TRUE; | |
783 | uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, y_max-1); | |
784 | uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, clearline+1); | |
785 | break; | |
786 | ||
787 | case KEYPRESS(0, 0, 'v'): | |
948aaa7c | 788 | status = PoolPrint(L"systemd-boot " PACKAGE_VERSION " (" EFI_MACHINE_TYPE_NAME "), UEFI Specification %d.%02d, Vendor %s %d.%02d", |
0fa2cac4 KS |
789 | ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff, |
790 | ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff); | |
791 | break; | |
792 | ||
793 | case KEYPRESS(0, 0, 'P'): | |
7361099e | 794 | print_status(config, loaded_image_path); |
0fa2cac4 KS |
795 | refresh = TRUE; |
796 | break; | |
797 | ||
798 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'l'): | |
799 | case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('l')): | |
800 | refresh = TRUE; | |
801 | break; | |
802 | ||
803 | default: | |
804 | /* jump with a hotkey directly to a matching entry */ | |
805 | idx = entry_lookup_key(config, idx_highlight+1, KEYCHAR(key)); | |
806 | if (idx < 0) | |
807 | break; | |
808 | idx_highlight = idx; | |
809 | refresh = TRUE; | |
810 | } | |
811 | ||
812 | if (idx_highlight > idx_last) { | |
813 | idx_last = idx_highlight; | |
814 | idx_first = 1 + idx_highlight - visible_max; | |
815 | refresh = TRUE; | |
56c70b32 | 816 | } else if (idx_highlight < idx_first) { |
0fa2cac4 KS |
817 | idx_first = idx_highlight; |
818 | idx_last = idx_highlight + visible_max-1; | |
819 | refresh = TRUE; | |
820 | } | |
821 | ||
0fa2cac4 KS |
822 | if (!refresh && idx_highlight != idx_highlight_prev) |
823 | highlight = TRUE; | |
824 | } | |
825 | ||
826 | *chosen_entry = config->entries[idx_highlight]; | |
827 | ||
828 | for (i = 0; i < config->entry_count; i++) | |
829 | FreePool(lines[i]); | |
830 | FreePool(lines); | |
831 | FreePool(clearline); | |
832 | ||
833 | uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_WHITE|EFI_BACKGROUND_BLACK); | |
834 | uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut); | |
835 | return run; | |
836 | } | |
837 | ||
838 | static VOID config_add_entry(Config *config, ConfigEntry *entry) { | |
839 | if ((config->entry_count & 15) == 0) { | |
840 | UINTN i; | |
841 | ||
842 | i = config->entry_count + 16; | |
843 | if (config->entry_count == 0) | |
844 | config->entries = AllocatePool(sizeof(VOID *) * i); | |
845 | else | |
846 | config->entries = ReallocatePool(config->entries, | |
847 | sizeof(VOID *) * config->entry_count, sizeof(VOID *) * i); | |
848 | } | |
849 | config->entries[config->entry_count++] = entry; | |
850 | } | |
851 | ||
852 | static VOID config_entry_free(ConfigEntry *entry) { | |
853 | FreePool(entry->title_show); | |
854 | FreePool(entry->title); | |
855 | FreePool(entry->machine_id); | |
856 | FreePool(entry->loader); | |
857 | FreePool(entry->options); | |
858 | } | |
859 | ||
52efd56a | 860 | static BOOLEAN is_digit(CHAR16 c) { |
0fa2cac4 KS |
861 | return (c >= '0') && (c <= '9'); |
862 | } | |
863 | ||
52efd56a | 864 | static UINTN c_order(CHAR16 c) { |
0fa2cac4 KS |
865 | if (c == '\0') |
866 | return 0; | |
867 | if (is_digit(c)) | |
868 | return 0; | |
869 | else if ((c >= 'a') && (c <= 'z')) | |
870 | return c; | |
871 | else | |
872 | return c + 0x10000; | |
873 | } | |
874 | ||
52efd56a | 875 | static INTN str_verscmp(CHAR16 *s1, CHAR16 *s2) { |
0fa2cac4 KS |
876 | CHAR16 *os1 = s1; |
877 | CHAR16 *os2 = s2; | |
878 | ||
879 | while (*s1 || *s2) { | |
880 | INTN first; | |
881 | ||
882 | while ((*s1 && !is_digit(*s1)) || (*s2 && !is_digit(*s2))) { | |
883 | INTN order; | |
884 | ||
885 | order = c_order(*s1) - c_order(*s2); | |
886 | if (order) | |
887 | return order; | |
888 | s1++; | |
889 | s2++; | |
890 | } | |
891 | ||
892 | while (*s1 == '0') | |
893 | s1++; | |
894 | while (*s2 == '0') | |
895 | s2++; | |
896 | ||
897 | first = 0; | |
898 | while (is_digit(*s1) && is_digit(*s2)) { | |
899 | if (first == 0) | |
900 | first = *s1 - *s2; | |
901 | s1++; | |
902 | s2++; | |
903 | } | |
904 | ||
905 | if (is_digit(*s1)) | |
906 | return 1; | |
907 | if (is_digit(*s2)) | |
908 | return -1; | |
909 | ||
910 | if (first) | |
911 | return first; | |
912 | } | |
913 | ||
914 | return StrCmp(os1, os2); | |
915 | } | |
916 | ||
917 | static CHAR8 *line_get_key_value(CHAR8 *content, CHAR8 *sep, UINTN *pos, CHAR8 **key_ret, CHAR8 **value_ret) { | |
918 | CHAR8 *line; | |
919 | UINTN linelen; | |
920 | CHAR8 *value; | |
921 | ||
922 | skip: | |
923 | line = content + *pos; | |
924 | if (*line == '\0') | |
925 | return NULL; | |
926 | ||
927 | linelen = 0; | |
928 | while (line[linelen] && !strchra((CHAR8 *)"\n\r", line[linelen])) | |
929 | linelen++; | |
930 | ||
931 | /* move pos to next line */ | |
932 | *pos += linelen; | |
933 | if (content[*pos]) | |
934 | (*pos)++; | |
935 | ||
936 | /* empty line */ | |
937 | if (linelen == 0) | |
938 | goto skip; | |
939 | ||
940 | /* terminate line */ | |
941 | line[linelen] = '\0'; | |
942 | ||
943 | /* remove leading whitespace */ | |
944 | while (strchra((CHAR8 *)" \t", *line)) { | |
945 | line++; | |
946 | linelen--; | |
947 | } | |
948 | ||
949 | /* remove trailing whitespace */ | |
950 | while (linelen > 0 && strchra(sep, line[linelen-1])) | |
951 | linelen--; | |
952 | line[linelen] = '\0'; | |
953 | ||
954 | if (*line == '#') | |
955 | goto skip; | |
956 | ||
957 | /* split key/value */ | |
958 | value = line; | |
959 | while (*value && !strchra(sep, *value)) | |
960 | value++; | |
961 | if (*value == '\0') | |
962 | goto skip; | |
963 | *value = '\0'; | |
964 | value++; | |
965 | while (*value && strchra(sep, *value)) | |
966 | value++; | |
967 | ||
968 | /* unquote */ | |
969 | if (value[0] == '\"' && line[linelen-1] == '\"') { | |
970 | value++; | |
971 | line[linelen-1] = '\0'; | |
972 | } | |
973 | ||
974 | *key_ret = line; | |
975 | *value_ret = value; | |
976 | return line; | |
977 | } | |
978 | ||
979 | static VOID config_defaults_load_from_file(Config *config, CHAR8 *content) { | |
980 | CHAR8 *line; | |
981 | UINTN pos = 0; | |
982 | CHAR8 *key, *value; | |
983 | ||
984 | line = content; | |
985 | while ((line = line_get_key_value(content, (CHAR8 *)" \t", &pos, &key, &value))) { | |
986 | if (strcmpa((CHAR8 *)"timeout", key) == 0) { | |
987 | CHAR16 *s; | |
988 | ||
989 | s = stra_to_str(value); | |
990 | config->timeout_sec_config = Atoi(s); | |
991 | config->timeout_sec = config->timeout_sec_config; | |
992 | FreePool(s); | |
993 | continue; | |
994 | } | |
995 | ||
996 | if (strcmpa((CHAR8 *)"default", key) == 0) { | |
997 | FreePool(config->entry_default_pattern); | |
998 | config->entry_default_pattern = stra_to_str(value); | |
999 | StrLwr(config->entry_default_pattern); | |
1000 | continue; | |
1001 | } | |
4db7e6d7 KS |
1002 | |
1003 | if (strcmpa((CHAR8 *)"editor", key) == 0) { | |
1004 | BOOLEAN on; | |
1005 | ||
1006 | if (EFI_ERROR(parse_boolean(value, &on))) | |
1007 | continue; | |
1008 | config->no_editor = !on; | |
1009 | } | |
0fa2cac4 KS |
1010 | } |
1011 | } | |
1012 | ||
1013 | static VOID config_entry_add_from_file(Config *config, EFI_HANDLE *device, CHAR16 *file, CHAR8 *content, CHAR16 *loaded_image_path) { | |
1014 | ConfigEntry *entry; | |
1015 | CHAR8 *line; | |
1016 | UINTN pos = 0; | |
1017 | CHAR8 *key, *value; | |
1018 | UINTN len; | |
1019 | CHAR16 *initrd = NULL; | |
1020 | ||
1021 | entry = AllocateZeroPool(sizeof(ConfigEntry)); | |
1022 | ||
1023 | line = content; | |
1024 | while ((line = line_get_key_value(content, (CHAR8 *)" \t", &pos, &key, &value))) { | |
1025 | if (strcmpa((CHAR8 *)"title", key) == 0) { | |
1026 | FreePool(entry->title); | |
1027 | entry->title = stra_to_str(value); | |
1028 | continue; | |
1029 | } | |
1030 | ||
1031 | if (strcmpa((CHAR8 *)"version", key) == 0) { | |
1032 | FreePool(entry->version); | |
1033 | entry->version = stra_to_str(value); | |
1034 | continue; | |
1035 | } | |
1036 | ||
1037 | if (strcmpa((CHAR8 *)"machine-id", key) == 0) { | |
1038 | FreePool(entry->machine_id); | |
1039 | entry->machine_id = stra_to_str(value); | |
1040 | continue; | |
1041 | } | |
1042 | ||
1043 | if (strcmpa((CHAR8 *)"linux", key) == 0) { | |
1044 | FreePool(entry->loader); | |
1045 | entry->type = LOADER_LINUX; | |
1046 | entry->loader = stra_to_path(value); | |
1047 | entry->key = 'l'; | |
1048 | continue; | |
1049 | } | |
1050 | ||
1051 | if (strcmpa((CHAR8 *)"efi", key) == 0) { | |
1052 | entry->type = LOADER_EFI; | |
1053 | FreePool(entry->loader); | |
1054 | entry->loader = stra_to_path(value); | |
1055 | ||
1056 | /* do not add an entry for ourselves */ | |
1057 | if (StriCmp(entry->loader, loaded_image_path) == 0) { | |
1058 | entry->type = LOADER_UNDEFINED; | |
1059 | break; | |
1060 | } | |
1061 | continue; | |
1062 | } | |
1063 | ||
1064 | if (strcmpa((CHAR8 *)"architecture", key) == 0) { | |
1065 | /* do not add an entry for an EFI image of architecture not matching with that of the image */ | |
1066 | if (strcmpa((CHAR8 *)EFI_MACHINE_TYPE_NAME, value) != 0) { | |
1067 | entry->type = LOADER_UNDEFINED; | |
1068 | break; | |
1069 | } | |
1070 | continue; | |
1071 | } | |
1072 | ||
1073 | if (strcmpa((CHAR8 *)"initrd", key) == 0) { | |
1074 | CHAR16 *new; | |
1075 | ||
1076 | new = stra_to_path(value); | |
1077 | if (initrd) { | |
1078 | CHAR16 *s; | |
1079 | ||
1080 | s = PoolPrint(L"%s initrd=%s", initrd, new); | |
1081 | FreePool(initrd); | |
1082 | initrd = s; | |
1083 | } else | |
1084 | initrd = PoolPrint(L"initrd=%s", new); | |
1085 | FreePool(new); | |
1086 | continue; | |
1087 | } | |
1088 | ||
1089 | if (strcmpa((CHAR8 *)"options", key) == 0) { | |
1090 | CHAR16 *new; | |
1091 | ||
1092 | new = stra_to_str(value); | |
1093 | if (entry->options) { | |
1094 | CHAR16 *s; | |
1095 | ||
1096 | s = PoolPrint(L"%s %s", entry->options, new); | |
1097 | FreePool(entry->options); | |
1098 | entry->options = s; | |
1099 | } else { | |
1100 | entry->options = new; | |
1101 | new = NULL; | |
1102 | } | |
1103 | FreePool(new); | |
1104 | continue; | |
1105 | } | |
0fa2cac4 KS |
1106 | } |
1107 | ||
1108 | if (entry->type == LOADER_UNDEFINED) { | |
1109 | config_entry_free(entry); | |
1110 | FreePool(initrd); | |
1111 | FreePool(entry); | |
1112 | return; | |
1113 | } | |
1114 | ||
1115 | /* add initrd= to options */ | |
1116 | if (entry->type == LOADER_LINUX && initrd) { | |
1117 | if (entry->options) { | |
1118 | CHAR16 *s; | |
1119 | ||
1120 | s = PoolPrint(L"%s %s", initrd, entry->options); | |
1121 | FreePool(entry->options); | |
1122 | entry->options = s; | |
1123 | } else { | |
1124 | entry->options = initrd; | |
1125 | initrd = NULL; | |
1126 | } | |
1127 | } | |
1128 | FreePool(initrd); | |
1129 | ||
0fa2cac4 KS |
1130 | entry->device = device; |
1131 | entry->file = StrDuplicate(file); | |
1132 | len = StrLen(entry->file); | |
1133 | /* remove ".conf" */ | |
1134 | if (len > 5) | |
1135 | entry->file[len - 5] = '\0'; | |
1136 | StrLwr(entry->file); | |
1137 | ||
1138 | config_add_entry(config, entry); | |
1139 | } | |
1140 | ||
540536ff | 1141 | static VOID config_load_defaults(Config *config, EFI_FILE *root_dir) { |
0fa2cac4 KS |
1142 | CHAR8 *content = NULL; |
1143 | UINTN sec; | |
1144 | UINTN len; | |
540536ff | 1145 | EFI_STATUS err; |
0fa2cac4 KS |
1146 | |
1147 | len = file_read(root_dir, L"\\loader\\loader.conf", 0, 0, &content); | |
1148 | if (len > 0) | |
1149 | config_defaults_load_from_file(config, content); | |
1150 | FreePool(content); | |
1151 | ||
1152 | err = efivar_get_int(L"LoaderConfigTimeout", &sec); | |
1153 | if (!EFI_ERROR(err)) { | |
1154 | config->timeout_sec_efivar = sec; | |
1155 | config->timeout_sec = sec; | |
1156 | } else | |
1157 | config->timeout_sec_efivar = -1; | |
540536ff KS |
1158 | } |
1159 | ||
1160 | static VOID config_load_entries(Config *config, EFI_HANDLE *device, EFI_FILE *root_dir, CHAR16 *loaded_image_path) { | |
1161 | EFI_FILE_HANDLE entries_dir; | |
1162 | EFI_STATUS err; | |
0fa2cac4 KS |
1163 | |
1164 | err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &entries_dir, L"\\loader\\entries", EFI_FILE_MODE_READ, 0ULL); | |
1165 | if (!EFI_ERROR(err)) { | |
1166 | for (;;) { | |
1167 | CHAR16 buf[256]; | |
1168 | UINTN bufsize; | |
1169 | EFI_FILE_INFO *f; | |
1170 | CHAR8 *content = NULL; | |
1171 | UINTN len; | |
1172 | ||
1173 | bufsize = sizeof(buf); | |
1174 | err = uefi_call_wrapper(entries_dir->Read, 3, entries_dir, &bufsize, buf); | |
1175 | if (bufsize == 0 || EFI_ERROR(err)) | |
1176 | break; | |
1177 | ||
1178 | f = (EFI_FILE_INFO *) buf; | |
1179 | if (f->FileName[0] == '.') | |
1180 | continue; | |
1181 | if (f->Attribute & EFI_FILE_DIRECTORY) | |
1182 | continue; | |
9d3dec15 | 1183 | |
0fa2cac4 KS |
1184 | len = StrLen(f->FileName); |
1185 | if (len < 6) | |
1186 | continue; | |
1187 | if (StriCmp(f->FileName + len - 5, L".conf") != 0) | |
1188 | continue; | |
9d3dec15 KS |
1189 | if (StrnCmp(f->FileName, L"auto-", 5) == 0) |
1190 | continue; | |
0fa2cac4 KS |
1191 | |
1192 | len = file_read(entries_dir, f->FileName, 0, 0, &content); | |
1193 | if (len > 0) | |
1194 | config_entry_add_from_file(config, device, f->FileName, content, loaded_image_path); | |
1195 | FreePool(content); | |
1196 | } | |
1197 | uefi_call_wrapper(entries_dir->Close, 1, entries_dir); | |
1198 | } | |
540536ff KS |
1199 | } |
1200 | ||
1201 | static VOID config_sort_entries(Config *config) { | |
1202 | UINTN i; | |
0fa2cac4 | 1203 | |
0fa2cac4 KS |
1204 | for (i = 1; i < config->entry_count; i++) { |
1205 | BOOLEAN more; | |
1206 | UINTN k; | |
1207 | ||
1208 | more = FALSE; | |
1209 | for (k = 0; k < config->entry_count - i; k++) { | |
1210 | ConfigEntry *entry; | |
1211 | ||
1212 | if (str_verscmp(config->entries[k]->file, config->entries[k+1]->file) <= 0) | |
1213 | continue; | |
1214 | entry = config->entries[k]; | |
1215 | config->entries[k] = config->entries[k+1]; | |
1216 | config->entries[k+1] = entry; | |
1217 | more = TRUE; | |
1218 | } | |
1219 | if (!more) | |
1220 | break; | |
1221 | } | |
1222 | } | |
1223 | ||
1224 | static VOID config_default_entry_select(Config *config) { | |
1225 | CHAR16 *var; | |
1226 | EFI_STATUS err; | |
1227 | UINTN i; | |
1228 | ||
1229 | /* | |
1230 | * The EFI variable to specify a boot entry for the next, and only the | |
1231 | * next reboot. The variable is always cleared directly after it is read. | |
1232 | */ | |
1233 | err = efivar_get(L"LoaderEntryOneShot", &var); | |
1234 | if (!EFI_ERROR(err)) { | |
1235 | BOOLEAN found = FALSE; | |
1236 | ||
1237 | for (i = 0; i < config->entry_count; i++) { | |
1238 | if (StrCmp(config->entries[i]->file, var) == 0) { | |
1239 | config->idx_default = i; | |
1240 | found = TRUE; | |
1241 | break; | |
1242 | } | |
1243 | } | |
1244 | ||
1245 | config->entry_oneshot = StrDuplicate(var); | |
1246 | efivar_set(L"LoaderEntryOneShot", NULL, TRUE); | |
1247 | FreePool(var); | |
1248 | if (found) | |
1249 | return; | |
1250 | } | |
1251 | ||
1252 | /* | |
1253 | * The EFI variable to select the default boot entry overrides the | |
1254 | * configured pattern. The variable can be set and cleared by pressing | |
1255 | * the 'd' key in the loader selection menu, the entry is marked with | |
1256 | * an '*'. | |
1257 | */ | |
1258 | err = efivar_get(L"LoaderEntryDefault", &var); | |
1259 | if (!EFI_ERROR(err)) { | |
1260 | BOOLEAN found = FALSE; | |
1261 | ||
1262 | for (i = 0; i < config->entry_count; i++) { | |
1263 | if (StrCmp(config->entries[i]->file, var) == 0) { | |
1264 | config->idx_default = i; | |
1265 | config->idx_default_efivar = i; | |
1266 | found = TRUE; | |
1267 | break; | |
1268 | } | |
1269 | } | |
1270 | FreePool(var); | |
1271 | if (found) | |
1272 | return; | |
1273 | } | |
1274 | config->idx_default_efivar = -1; | |
1275 | ||
1276 | if (config->entry_count == 0) | |
1277 | return; | |
1278 | ||
1279 | /* | |
1280 | * Match the pattern from the end of the list to the start, find last | |
1281 | * entry (largest number) matching the given pattern. | |
1282 | */ | |
1283 | if (config->entry_default_pattern) { | |
1284 | i = config->entry_count; | |
1285 | while (i--) { | |
1286 | if (config->entries[i]->no_autoselect) | |
1287 | continue; | |
1288 | if (MetaiMatch(config->entries[i]->file, config->entry_default_pattern)) { | |
1289 | config->idx_default = i; | |
1290 | return; | |
1291 | } | |
1292 | } | |
1293 | } | |
1294 | ||
1295 | /* select the last suitable entry */ | |
1296 | i = config->entry_count; | |
1297 | while (i--) { | |
1298 | if (config->entries[i]->no_autoselect) | |
1299 | continue; | |
1300 | config->idx_default = i; | |
1301 | return; | |
1302 | } | |
1303 | ||
1304 | /* no entry found */ | |
1305 | config->idx_default = -1; | |
1306 | } | |
1307 | ||
1308 | /* generate a unique title, avoiding non-distinguishable menu entries */ | |
1309 | static VOID config_title_generate(Config *config) { | |
1310 | UINTN i, k; | |
1311 | BOOLEAN unique; | |
1312 | ||
1313 | /* set title */ | |
1314 | for (i = 0; i < config->entry_count; i++) { | |
1315 | CHAR16 *title; | |
1316 | ||
1317 | FreePool(config->entries[i]->title_show); | |
1318 | title = config->entries[i]->title; | |
1319 | if (!title) | |
1320 | title = config->entries[i]->file; | |
1321 | config->entries[i]->title_show = StrDuplicate(title); | |
1322 | } | |
1323 | ||
1324 | unique = TRUE; | |
1325 | for (i = 0; i < config->entry_count; i++) { | |
1326 | for (k = 0; k < config->entry_count; k++) { | |
1327 | if (i == k) | |
1328 | continue; | |
1329 | if (StrCmp(config->entries[i]->title_show, config->entries[k]->title_show) != 0) | |
1330 | continue; | |
1331 | ||
1332 | unique = FALSE; | |
1333 | config->entries[i]->non_unique = TRUE; | |
1334 | config->entries[k]->non_unique = TRUE; | |
1335 | } | |
1336 | } | |
1337 | if (unique) | |
1338 | return; | |
1339 | ||
1340 | /* add version to non-unique titles */ | |
1341 | for (i = 0; i < config->entry_count; i++) { | |
1342 | CHAR16 *s; | |
1343 | ||
1344 | if (!config->entries[i]->non_unique) | |
1345 | continue; | |
1346 | if (!config->entries[i]->version) | |
1347 | continue; | |
1348 | ||
1349 | s = PoolPrint(L"%s (%s)", config->entries[i]->title_show, config->entries[i]->version); | |
1350 | FreePool(config->entries[i]->title_show); | |
1351 | config->entries[i]->title_show = s; | |
1352 | config->entries[i]->non_unique = FALSE; | |
1353 | } | |
1354 | ||
1355 | unique = TRUE; | |
1356 | for (i = 0; i < config->entry_count; i++) { | |
1357 | for (k = 0; k < config->entry_count; k++) { | |
1358 | if (i == k) | |
1359 | continue; | |
1360 | if (StrCmp(config->entries[i]->title_show, config->entries[k]->title_show) != 0) | |
1361 | continue; | |
1362 | ||
1363 | unique = FALSE; | |
1364 | config->entries[i]->non_unique = TRUE; | |
1365 | config->entries[k]->non_unique = TRUE; | |
1366 | } | |
1367 | } | |
1368 | if (unique) | |
1369 | return; | |
1370 | ||
1371 | /* add machine-id to non-unique titles */ | |
1372 | for (i = 0; i < config->entry_count; i++) { | |
1373 | CHAR16 *s; | |
1374 | CHAR16 *m; | |
1375 | ||
1376 | if (!config->entries[i]->non_unique) | |
1377 | continue; | |
1378 | if (!config->entries[i]->machine_id) | |
1379 | continue; | |
1380 | ||
1381 | m = StrDuplicate(config->entries[i]->machine_id); | |
1382 | m[8] = '\0'; | |
1383 | s = PoolPrint(L"%s (%s)", config->entries[i]->title_show, m); | |
1384 | FreePool(config->entries[i]->title_show); | |
1385 | config->entries[i]->title_show = s; | |
1386 | config->entries[i]->non_unique = FALSE; | |
1387 | FreePool(m); | |
1388 | } | |
1389 | ||
1390 | unique = TRUE; | |
1391 | for (i = 0; i < config->entry_count; i++) { | |
1392 | for (k = 0; k < config->entry_count; k++) { | |
1393 | if (i == k) | |
1394 | continue; | |
1395 | if (StrCmp(config->entries[i]->title_show, config->entries[k]->title_show) != 0) | |
1396 | continue; | |
1397 | ||
1398 | unique = FALSE; | |
1399 | config->entries[i]->non_unique = TRUE; | |
1400 | config->entries[k]->non_unique = TRUE; | |
1401 | } | |
1402 | } | |
1403 | if (unique) | |
1404 | return; | |
1405 | ||
1406 | /* add file name to non-unique titles */ | |
1407 | for (i = 0; i < config->entry_count; i++) { | |
1408 | CHAR16 *s; | |
1409 | ||
1410 | if (!config->entries[i]->non_unique) | |
1411 | continue; | |
1412 | s = PoolPrint(L"%s (%s)", config->entries[i]->title_show, config->entries[i]->file); | |
1413 | FreePool(config->entries[i]->title_show); | |
1414 | config->entries[i]->title_show = s; | |
1415 | config->entries[i]->non_unique = FALSE; | |
1416 | } | |
1417 | } | |
1418 | ||
1419 | static BOOLEAN config_entry_add_call(Config *config, CHAR16 *title, EFI_STATUS (*call)(VOID)) { | |
1420 | ConfigEntry *entry; | |
1421 | ||
1422 | entry = AllocateZeroPool(sizeof(ConfigEntry)); | |
1423 | entry->title = StrDuplicate(title); | |
1424 | entry->call = call; | |
1425 | entry->no_autoselect = TRUE; | |
1426 | config_add_entry(config, entry); | |
1427 | return TRUE; | |
1428 | } | |
1429 | ||
1430 | static ConfigEntry *config_entry_add_loader(Config *config, EFI_HANDLE *device, | |
1431 | enum loader_type type,CHAR16 *file, CHAR16 key, CHAR16 *title, CHAR16 *loader) { | |
1432 | ConfigEntry *entry; | |
1433 | ||
1434 | entry = AllocateZeroPool(sizeof(ConfigEntry)); | |
1435 | entry->type = type; | |
1436 | entry->title = StrDuplicate(title); | |
1437 | entry->device = device; | |
1438 | entry->loader = StrDuplicate(loader); | |
1439 | entry->file = StrDuplicate(file); | |
1440 | StrLwr(entry->file); | |
1441 | entry->key = key; | |
1442 | config_add_entry(config, entry); | |
1443 | ||
1444 | return entry; | |
1445 | } | |
1446 | ||
1447 | static BOOLEAN config_entry_add_loader_auto(Config *config, EFI_HANDLE *device, EFI_FILE *root_dir, CHAR16 *loaded_image_path, | |
1448 | CHAR16 *file, CHAR16 key, CHAR16 *title, CHAR16 *loader) { | |
1449 | EFI_FILE_HANDLE handle; | |
1450 | ConfigEntry *entry; | |
1451 | EFI_STATUS err; | |
1452 | ||
1453 | /* do not add an entry for ourselves */ | |
1454 | if (loaded_image_path && StriCmp(loader, loaded_image_path) == 0) | |
1455 | return FALSE; | |
1456 | ||
1457 | /* check existence */ | |
1458 | err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &handle, loader, EFI_FILE_MODE_READ, 0ULL); | |
1459 | if (EFI_ERROR(err)) | |
1460 | return FALSE; | |
1461 | uefi_call_wrapper(handle->Close, 1, handle); | |
1462 | ||
1463 | entry = config_entry_add_loader(config, device, LOADER_UNDEFINED, file, key, title, loader); | |
1464 | if (!entry) | |
1465 | return FALSE; | |
1466 | ||
1467 | /* do not boot right away into auto-detected entries */ | |
1468 | entry->no_autoselect = TRUE; | |
1469 | ||
0fa2cac4 KS |
1470 | return TRUE; |
1471 | } | |
1472 | ||
1473 | static VOID config_entry_add_osx(Config *config) { | |
1474 | EFI_STATUS err; | |
1475 | UINTN handle_count = 0; | |
1476 | EFI_HANDLE *handles = NULL; | |
1477 | ||
1478 | err = LibLocateHandle(ByProtocol, &FileSystemProtocol, NULL, &handle_count, &handles); | |
1479 | if (!EFI_ERROR(err)) { | |
1480 | UINTN i; | |
1481 | ||
1482 | for (i = 0; i < handle_count; i++) { | |
1483 | EFI_FILE *root; | |
1484 | BOOLEAN found; | |
1485 | ||
1486 | root = LibOpenRoot(handles[i]); | |
1487 | if (!root) | |
1488 | continue; | |
b3f76b0d | 1489 | found = config_entry_add_loader_auto(config, handles[i], root, NULL, L"auto-osx", 'a', L"macOS", |
0fa2cac4 KS |
1490 | L"\\System\\Library\\CoreServices\\boot.efi"); |
1491 | uefi_call_wrapper(root->Close, 1, root); | |
1492 | if (found) | |
1493 | break; | |
1494 | } | |
1495 | ||
1496 | FreePool(handles); | |
1497 | } | |
1498 | } | |
1499 | ||
1500 | static VOID config_entry_add_linux( Config *config, EFI_LOADED_IMAGE *loaded_image, EFI_FILE *root_dir) { | |
1501 | EFI_FILE_HANDLE linux_dir; | |
1502 | EFI_STATUS err; | |
b1da143f | 1503 | ConfigEntry *entry; |
0fa2cac4 KS |
1504 | |
1505 | err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &linux_dir, L"\\EFI\\Linux", EFI_FILE_MODE_READ, 0ULL); | |
1506 | if (!EFI_ERROR(err)) { | |
1507 | for (;;) { | |
1508 | CHAR16 buf[256]; | |
1509 | UINTN bufsize; | |
1510 | EFI_FILE_INFO *f; | |
1511 | CHAR8 *sections[] = { | |
1512 | (UINT8 *)".osrel", | |
b1da143f | 1513 | (UINT8 *)".cmdline", |
0fa2cac4 KS |
1514 | NULL |
1515 | }; | |
1516 | UINTN offs[ELEMENTSOF(sections)-1] = {}; | |
1517 | UINTN szs[ELEMENTSOF(sections)-1] = {}; | |
1518 | UINTN addrs[ELEMENTSOF(sections)-1] = {}; | |
1519 | CHAR8 *content = NULL; | |
1520 | UINTN len; | |
1521 | CHAR8 *line; | |
1522 | UINTN pos = 0; | |
1523 | CHAR8 *key, *value; | |
1524 | CHAR16 *os_name = NULL; | |
1525 | CHAR16 *os_id = NULL; | |
1526 | CHAR16 *os_version = NULL; | |
59658d19 | 1527 | CHAR16 *os_build = NULL; |
0fa2cac4 KS |
1528 | |
1529 | bufsize = sizeof(buf); | |
1530 | err = uefi_call_wrapper(linux_dir->Read, 3, linux_dir, &bufsize, buf); | |
1531 | if (bufsize == 0 || EFI_ERROR(err)) | |
1532 | break; | |
1533 | ||
1534 | f = (EFI_FILE_INFO *) buf; | |
1535 | if (f->FileName[0] == '.') | |
1536 | continue; | |
1537 | if (f->Attribute & EFI_FILE_DIRECTORY) | |
1538 | continue; | |
1539 | len = StrLen(f->FileName); | |
1540 | if (len < 5) | |
1541 | continue; | |
1542 | if (StriCmp(f->FileName + len - 4, L".efi") != 0) | |
1543 | continue; | |
1544 | ||
b1da143f | 1545 | /* look for .osrel and .cmdline sections in the .efi binary */ |
d4cbada2 | 1546 | err = pe_file_locate_sections(linux_dir, f->FileName, sections, addrs, offs, szs); |
0fa2cac4 KS |
1547 | if (EFI_ERROR(err)) |
1548 | continue; | |
1549 | ||
1550 | len = file_read(linux_dir, f->FileName, offs[0], szs[0], &content); | |
1551 | if (len <= 0) | |
1552 | continue; | |
1553 | ||
1554 | /* read properties from the embedded os-release file */ | |
1555 | line = content; | |
1556 | while ((line = line_get_key_value(content, (CHAR8 *)"=", &pos, &key, &value))) { | |
1557 | if (strcmpa((CHAR8 *)"PRETTY_NAME", key) == 0) { | |
d8c64b7f | 1558 | FreePool(os_name); |
0fa2cac4 KS |
1559 | os_name = stra_to_str(value); |
1560 | continue; | |
1561 | } | |
1562 | ||
1563 | if (strcmpa((CHAR8 *)"ID", key) == 0) { | |
d8c64b7f | 1564 | FreePool(os_id); |
0fa2cac4 KS |
1565 | os_id = stra_to_str(value); |
1566 | continue; | |
1567 | } | |
1568 | ||
1569 | if (strcmpa((CHAR8 *)"VERSION_ID", key) == 0) { | |
d8c64b7f | 1570 | FreePool(os_version); |
0fa2cac4 KS |
1571 | os_version = stra_to_str(value); |
1572 | continue; | |
1573 | } | |
59658d19 DH |
1574 | |
1575 | if (strcmpa((CHAR8 *)"BUILD_ID", key) == 0) { | |
1576 | FreePool(os_build); | |
1577 | os_build = stra_to_str(value); | |
1578 | continue; | |
1579 | } | |
0fa2cac4 KS |
1580 | } |
1581 | ||
59658d19 | 1582 | if (os_name && os_id && (os_version || os_build)) { |
0fa2cac4 KS |
1583 | CHAR16 *conf; |
1584 | CHAR16 *path; | |
b1da143f | 1585 | CHAR16 *cmdline; |
0fa2cac4 | 1586 | |
59658d19 | 1587 | conf = PoolPrint(L"%s-%s", os_id, os_version ? : os_build); |
0fa2cac4 | 1588 | path = PoolPrint(L"\\EFI\\Linux\\%s", f->FileName); |
b1da143f JCVM |
1589 | entry = config_entry_add_loader(config, loaded_image->DeviceHandle, LOADER_LINUX, conf, 'l', os_name, path); |
1590 | ||
1591 | FreePool(content); | |
1592 | /* read the embedded cmdline file */ | |
1593 | len = file_read(linux_dir, f->FileName, offs[1], szs[1] - 1 , &content); | |
1594 | if (len > 0) { | |
fc7bdb58 KS |
1595 | cmdline = stra_to_str(content); |
1596 | entry->options = cmdline; | |
1597 | cmdline = NULL; | |
b1da143f JCVM |
1598 | } |
1599 | FreePool(cmdline); | |
0fa2cac4 KS |
1600 | FreePool(conf); |
1601 | FreePool(path); | |
0fa2cac4 KS |
1602 | } |
1603 | ||
d8c64b7f DH |
1604 | FreePool(os_name); |
1605 | FreePool(os_id); | |
1606 | FreePool(os_version); | |
59658d19 | 1607 | FreePool(os_build); |
0fa2cac4 KS |
1608 | FreePool(content); |
1609 | } | |
1610 | uefi_call_wrapper(linux_dir->Close, 1, linux_dir); | |
1611 | } | |
1612 | } | |
1613 | ||
1614 | static EFI_STATUS image_start(EFI_HANDLE parent_image, const Config *config, const ConfigEntry *entry) { | |
1615 | EFI_HANDLE image; | |
1616 | EFI_DEVICE_PATH *path; | |
1617 | CHAR16 *options; | |
1618 | EFI_STATUS err; | |
1619 | ||
1620 | path = FileDevicePath(entry->device, entry->loader); | |
1621 | if (!path) { | |
1622 | Print(L"Error getting device path."); | |
1623 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); | |
1624 | return EFI_INVALID_PARAMETER; | |
1625 | } | |
1626 | ||
1627 | err = uefi_call_wrapper(BS->LoadImage, 6, FALSE, parent_image, path, NULL, 0, &image); | |
1628 | if (EFI_ERROR(err)) { | |
1629 | Print(L"Error loading %s: %r", entry->loader, err); | |
1630 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); | |
1631 | goto out; | |
1632 | } | |
1633 | ||
1634 | if (config->options_edit) | |
1635 | options = config->options_edit; | |
1636 | else if (entry->options) | |
1637 | options = entry->options; | |
1638 | else | |
1639 | options = NULL; | |
1640 | if (options) { | |
1641 | EFI_LOADED_IMAGE *loaded_image; | |
1642 | ||
1643 | err = uefi_call_wrapper(BS->OpenProtocol, 6, image, &LoadedImageProtocol, (VOID **)&loaded_image, | |
1644 | parent_image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); | |
1645 | if (EFI_ERROR(err)) { | |
1646 | Print(L"Error getting LoadedImageProtocol handle: %r", err); | |
1647 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); | |
1648 | goto out_unload; | |
1649 | } | |
1650 | loaded_image->LoadOptions = options; | |
1651 | loaded_image->LoadOptionsSize = (StrLen(loaded_image->LoadOptions)+1) * sizeof(CHAR16); | |
92ed3bb4 | 1652 | |
349cc4a5 | 1653 | #if ENABLE_TPM |
7db5706e | 1654 | /* Try to log any options to the TPM, especially to catch manually edited options */ |
92ed3bb4 HH |
1655 | err = tpm_log_event(SD_TPM_PCR, |
1656 | (EFI_PHYSICAL_ADDRESS) loaded_image->LoadOptions, | |
1657 | loaded_image->LoadOptionsSize, loaded_image->LoadOptions); | |
1658 | if (EFI_ERROR(err)) { | |
1659 | Print(L"Unable to add image options measurement: %r", err); | |
522aa9f5 | 1660 | uefi_call_wrapper(BS->Stall, 1, 200 * 1000); |
92ed3bb4 HH |
1661 | } |
1662 | #endif | |
0fa2cac4 KS |
1663 | } |
1664 | ||
1665 | efivar_set_time_usec(L"LoaderTimeExecUSec", 0); | |
1666 | err = uefi_call_wrapper(BS->StartImage, 3, image, NULL, NULL); | |
1667 | out_unload: | |
1668 | uefi_call_wrapper(BS->UnloadImage, 1, image); | |
1669 | out: | |
1670 | FreePool(path); | |
1671 | return err; | |
1672 | } | |
1673 | ||
1674 | static EFI_STATUS reboot_into_firmware(VOID) { | |
1675 | CHAR8 *b; | |
1676 | UINTN size; | |
1677 | UINT64 osind; | |
1678 | EFI_STATUS err; | |
1679 | ||
1680 | osind = EFI_OS_INDICATIONS_BOOT_TO_FW_UI; | |
1681 | ||
1682 | err = efivar_get_raw(&global_guid, L"OsIndications", &b, &size); | |
1683 | if (!EFI_ERROR(err)) | |
1684 | osind |= (UINT64)*b; | |
1685 | FreePool(b); | |
1686 | ||
1687 | err = efivar_set_raw(&global_guid, L"OsIndications", (CHAR8 *)&osind, sizeof(UINT64), TRUE); | |
1688 | if (EFI_ERROR(err)) | |
1689 | return err; | |
1690 | ||
1691 | err = uefi_call_wrapper(RT->ResetSystem, 4, EfiResetCold, EFI_SUCCESS, 0, NULL); | |
1692 | Print(L"Error calling ResetSystem: %r", err); | |
1693 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); | |
1694 | return err; | |
1695 | } | |
1696 | ||
1697 | static VOID config_free(Config *config) { | |
1698 | UINTN i; | |
1699 | ||
1700 | for (i = 0; i < config->entry_count; i++) | |
1701 | config_entry_free(config->entries[i]); | |
1702 | FreePool(config->entries); | |
1703 | FreePool(config->entry_default_pattern); | |
1704 | FreePool(config->options_edit); | |
1705 | FreePool(config->entry_oneshot); | |
0fa2cac4 KS |
1706 | } |
1707 | ||
1708 | EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) { | |
1709 | CHAR16 *s; | |
1710 | CHAR8 *b; | |
1711 | UINTN size; | |
1712 | EFI_LOADED_IMAGE *loaded_image; | |
1713 | EFI_FILE *root_dir; | |
1714 | CHAR16 *loaded_image_path; | |
0fa2cac4 KS |
1715 | EFI_STATUS err; |
1716 | Config config; | |
1717 | UINT64 init_usec; | |
1718 | BOOLEAN menu = FALSE; | |
8110e144 | 1719 | CHAR16 uuid[37]; |
0fa2cac4 KS |
1720 | |
1721 | InitializeLib(image, sys_table); | |
1722 | init_usec = time_usec(); | |
1723 | efivar_set_time_usec(L"LoaderTimeInitUSec", init_usec); | |
948aaa7c | 1724 | efivar_set(L"LoaderInfo", L"systemd-boot " PACKAGE_VERSION, FALSE); |
0fa2cac4 KS |
1725 | s = PoolPrint(L"%s %d.%02d", ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff); |
1726 | efivar_set(L"LoaderFirmwareInfo", s, FALSE); | |
1727 | FreePool(s); | |
1728 | s = PoolPrint(L"UEFI %d.%02d", ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff); | |
1729 | efivar_set(L"LoaderFirmwareType", s, FALSE); | |
1730 | FreePool(s); | |
1731 | ||
1732 | err = uefi_call_wrapper(BS->OpenProtocol, 6, image, &LoadedImageProtocol, (VOID **)&loaded_image, | |
1733 | image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); | |
1734 | if (EFI_ERROR(err)) { | |
1735 | Print(L"Error getting a LoadedImageProtocol handle: %r ", err); | |
1736 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); | |
1737 | return err; | |
1738 | } | |
1739 | ||
1740 | /* export the device path this image is started from */ | |
8110e144 KS |
1741 | if (disk_get_part_uuid(loaded_image->DeviceHandle, uuid) == EFI_SUCCESS) |
1742 | efivar_set(L"LoaderDevicePartUUID", uuid, FALSE); | |
0fa2cac4 KS |
1743 | |
1744 | root_dir = LibOpenRoot(loaded_image->DeviceHandle); | |
1745 | if (!root_dir) { | |
1746 | Print(L"Unable to open root directory: %r ", err); | |
1747 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); | |
1748 | return EFI_LOAD_ERROR; | |
1749 | } | |
1750 | ||
b2bb40ce MR |
1751 | if (secure_boot_enabled() && shim_loaded()) { |
1752 | err = security_policy_install(); | |
1753 | if (EFI_ERROR(err)) { | |
1754 | Print(L"Error installing security policy: %r ", err); | |
1755 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); | |
1756 | return err; | |
1757 | } | |
1758 | } | |
0fa2cac4 KS |
1759 | |
1760 | /* the filesystem path to this image, to prevent adding ourselves to the menu */ | |
1761 | loaded_image_path = DevicePathToStr(loaded_image->FilePath); | |
1762 | efivar_set(L"LoaderImageIdentifier", loaded_image_path, FALSE); | |
1763 | ||
0fa2cac4 | 1764 | ZeroMem(&config, sizeof(Config)); |
540536ff | 1765 | config_load_defaults(&config, root_dir); |
0fa2cac4 | 1766 | |
540536ff | 1767 | /* scan /EFI/Linux/ directory */ |
0fa2cac4 | 1768 | config_entry_add_linux(&config, loaded_image, root_dir); |
540536ff KS |
1769 | |
1770 | /* scan /loader/entries/\*.conf files */ | |
1771 | config_load_entries(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path); | |
1772 | ||
1773 | /* sort entries after version number */ | |
1774 | config_sort_entries(&config); | |
1775 | ||
1776 | /* if we find some well-known loaders, add them to the end of the list */ | |
0fa2cac4 KS |
1777 | config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path, |
1778 | L"auto-windows", 'w', L"Windows Boot Manager", L"\\EFI\\Microsoft\\Boot\\bootmgfw.efi"); | |
1779 | config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path, | |
1780 | L"auto-efi-shell", 's', L"EFI Shell", L"\\shell" EFI_MACHINE_TYPE_NAME ".efi"); | |
1781 | config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path, | |
1782 | L"auto-efi-default", '\0', L"EFI Default Loader", L"\\EFI\\Boot\\boot" EFI_MACHINE_TYPE_NAME ".efi"); | |
1783 | config_entry_add_osx(&config); | |
0fa2cac4 KS |
1784 | |
1785 | if (efivar_get_raw(&global_guid, L"OsIndicationsSupported", &b, &size) == EFI_SUCCESS) { | |
1786 | UINT64 osind = (UINT64)*b; | |
1787 | ||
1788 | if (osind & EFI_OS_INDICATIONS_BOOT_TO_FW_UI) | |
1789 | config_entry_add_call(&config, L"Reboot Into Firmware Interface", reboot_into_firmware); | |
1790 | FreePool(b); | |
1791 | } | |
1792 | ||
1793 | if (config.entry_count == 0) { | |
1794 | Print(L"No loader found. Configuration files in \\loader\\entries\\*.conf are needed."); | |
1795 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); | |
1796 | goto out; | |
1797 | } | |
1798 | ||
1799 | config_title_generate(&config); | |
1800 | ||
13e785f7 | 1801 | /* select entry by configured pattern or EFI LoaderDefaultEntry= variable */ |
0fa2cac4 KS |
1802 | config_default_entry_select(&config); |
1803 | ||
1804 | /* if no configured entry to select from was found, enable the menu */ | |
1805 | if (config.idx_default == -1) { | |
1806 | config.idx_default = 0; | |
1807 | if (config.timeout_sec == 0) | |
1808 | config.timeout_sec = 10; | |
1809 | } | |
1810 | ||
1811 | /* select entry or show menu when key is pressed or timeout is set */ | |
1812 | if (config.timeout_sec == 0) { | |
1813 | UINT64 key; | |
1814 | ||
1815 | err = console_key_read(&key, FALSE); | |
1816 | if (!EFI_ERROR(err)) { | |
1817 | INT16 idx; | |
1818 | ||
1819 | /* find matching key in config entries */ | |
1820 | idx = entry_lookup_key(&config, config.idx_default, KEYCHAR(key)); | |
1821 | if (idx >= 0) | |
1822 | config.idx_default = idx; | |
1823 | else | |
1824 | menu = TRUE; | |
1825 | } | |
1826 | } else | |
1827 | menu = TRUE; | |
1828 | ||
1829 | for (;;) { | |
1830 | ConfigEntry *entry; | |
1831 | ||
1832 | entry = config.entries[config.idx_default]; | |
1833 | if (menu) { | |
1834 | efivar_set_time_usec(L"LoaderTimeMenuUSec", 0); | |
1835 | uefi_call_wrapper(BS->SetWatchdogTimer, 4, 0, 0x10000, 0, NULL); | |
7361099e | 1836 | if (!menu_run(&config, &entry, loaded_image_path)) |
0fa2cac4 KS |
1837 | break; |
1838 | ||
1839 | /* run special entry like "reboot" */ | |
1840 | if (entry->call) { | |
1841 | entry->call(); | |
1842 | continue; | |
1843 | } | |
0fa2cac4 KS |
1844 | } |
1845 | ||
1846 | /* export the selected boot entry to the system */ | |
1847 | efivar_set(L"LoaderEntrySelected", entry->file, FALSE); | |
1848 | ||
1849 | uefi_call_wrapper(BS->SetWatchdogTimer, 4, 5 * 60, 0x10000, 0, NULL); | |
1850 | err = image_start(image, &config, entry); | |
20b1538d | 1851 | if (EFI_ERROR(err)) { |
37fa3690 | 1852 | graphics_mode(FALSE); |
20b1538d | 1853 | Print(L"\nFailed to execute %s (%s): %r\n", entry->title, entry->loader, err); |
0fa2cac4 KS |
1854 | uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000); |
1855 | goto out; | |
1856 | } | |
1857 | ||
1858 | menu = TRUE; | |
1859 | config.timeout_sec = 0; | |
1860 | } | |
1861 | err = EFI_SUCCESS; | |
1862 | out: | |
1863 | FreePool(loaded_image_path); | |
1864 | config_free(&config); | |
1865 | uefi_call_wrapper(root_dir->Close, 1, root_dir); | |
1866 | uefi_call_wrapper(BS->CloseProtocol, 4, image, &LoadedImageProtocol, image, NULL); | |
1867 | return err; | |
1868 | } |