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