1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2017 Rob Clark
11 #include <efi_loader.h>
13 static const struct efi_boot_services *bs;
14 static const struct efi_runtime_services *rs;
16 #define LOAD_OPTION_ACTIVE 0x00000001
17 #define LOAD_OPTION_FORCE_RECONNECT 0x00000002
18 #define LOAD_OPTION_HIDDEN 0x00000008
21 * bootmgr implements the logic of trying to find a payload to boot
22 * based on the BootOrder + BootXXXX variables, and then loading it.
24 * TODO detecting a special key held (f9?) and displaying a boot menu
25 * like you would get on a PC would be clever.
27 * TODO if we had a way to write and persist variables after the OS
28 * has started, we'd also want to check OsIndications to see if we
29 * should do normal or recovery boot.
34 * See section 3.1.3 in the v2.7 UEFI spec for more details on
35 * the layout of EFI_LOAD_OPTION. In short it is:
37 * typedef struct _EFI_LOAD_OPTION {
39 * UINT16 FilePathListLength;
40 * // CHAR16 Description[]; <-- variable length, NULL terminated
41 * // EFI_DEVICE_PATH_PROTOCOL FilePathList[]; <-- FilePathListLength bytes
42 * // UINT8 OptionalData[];
49 struct efi_device_path *file_path;
53 /* parse an EFI_LOAD_OPTION, as described above */
54 static void parse_load_option(struct load_option *lo, void *ptr)
56 lo->attributes = *(u32 *)ptr;
59 lo->file_path_length = *(u16 *)ptr;
63 ptr += (utf16_strlen(lo->label) + 1) * 2;
66 ptr += lo->file_path_length;
68 lo->optional_data = ptr;
71 /* free() the result */
72 static void *get_var(u16 *name, const efi_guid_t *vendor,
75 efi_guid_t *v = (efi_guid_t *)vendor;
80 EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
81 if (ret == EFI_BUFFER_TOO_SMALL) {
83 EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
86 if (ret != EFI_SUCCESS) {
96 * Attempt to load load-option number 'n', returning device_path and file_path
97 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
98 * and that the specified file to boot exists.
100 static void *try_load_entry(uint16_t n, struct efi_device_path **device_path,
101 struct efi_device_path **file_path)
103 struct load_option lo;
104 u16 varname[] = L"Boot0000";
105 u16 hexmap[] = L"0123456789ABCDEF";
106 void *load_option, *image = NULL;
109 varname[4] = hexmap[(n & 0xf000) >> 12];
110 varname[5] = hexmap[(n & 0x0f00) >> 8];
111 varname[6] = hexmap[(n & 0x00f0) >> 4];
112 varname[7] = hexmap[(n & 0x000f) >> 0];
114 load_option = get_var(varname, &efi_global_variable_guid, &size);
118 parse_load_option(&lo, load_option);
120 if (lo.attributes & LOAD_OPTION_ACTIVE) {
123 debug("%s: trying to load \"%ls\" from %pD\n",
124 __func__, lo.label, lo.file_path);
126 ret = efi_load_image_from_path(lo.file_path, &image);
128 if (ret != EFI_SUCCESS)
131 printf("Booting: %ls\n", lo.label);
132 efi_dp_split_file_path(lo.file_path, device_path, file_path);
142 * Attempt to load, in the order specified by BootOrder EFI variable, the
143 * available load-options, finding and returning the first one that can
144 * be loaded successfully.
146 void *efi_bootmgr_load(struct efi_device_path **device_path,
147 struct efi_device_path **file_path)
156 bs = systab.boottime;
159 bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
163 num = size / sizeof(uint16_t);
164 for (i = 0; i < num; i++) {
165 debug("%s: trying to load Boot%04X\n", __func__, bootorder[i]);
166 image = try_load_entry(bootorder[i], device_path, file_path);