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