1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2017 Rob Clark
11 #include <efi_loader.h>
12 #include <asm/unaligned.h>
14 static const struct efi_boot_services *bs;
15 static const struct efi_runtime_services *rs;
17 #define LOAD_OPTION_ACTIVE 0x00000001
18 #define LOAD_OPTION_FORCE_RECONNECT 0x00000002
19 #define LOAD_OPTION_HIDDEN 0x00000008
22 * bootmgr implements the logic of trying to find a payload to boot
23 * based on the BootOrder + BootXXXX variables, and then loading it.
25 * TODO detecting a special key held (f9?) and displaying a boot menu
26 * like you would get on a PC would be clever.
28 * TODO if we had a way to write and persist variables after the OS
29 * has started, we'd also want to check OsIndications to see if we
30 * should do normal or recovery boot.
34 /* Parse serialized data and transform it into efi_load_option structure */
35 void efi_deserialize_load_option(struct efi_load_option *lo, u8 *data)
37 lo->attributes = get_unaligned_le32(data);
40 lo->file_path_length = get_unaligned_le16(data);
44 lo->label = (u16 *)data;
45 data += (u16_strlen(lo->label) + 1) * sizeof(u16);
48 lo->file_path = (struct efi_device_path *)data;
49 data += lo->file_path_length;
51 lo->optional_data = data;
55 * Serialize efi_load_option structure into byte stream for BootXXXX.
56 * Return a size of allocated data.
58 unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data)
60 unsigned long label_len, option_len;
64 label_len = (u16_strlen(lo->label) + 1) * sizeof(u16);
65 option_len = strlen((char *)lo->optional_data);
68 size = sizeof(lo->attributes);
69 size += sizeof(lo->file_path_length);
71 size += lo->file_path_length;
72 size += option_len + 1;
79 memcpy(p, &lo->attributes, sizeof(lo->attributes));
80 p += sizeof(lo->attributes);
82 memcpy(p, &lo->file_path_length, sizeof(lo->file_path_length));
83 p += sizeof(lo->file_path_length);
85 memcpy(p, lo->label, label_len);
88 memcpy(p, lo->file_path, lo->file_path_length);
89 p += lo->file_path_length;
91 memcpy(p, lo->optional_data, option_len);
98 /* free() the result */
99 static void *get_var(u16 *name, const efi_guid_t *vendor,
102 efi_guid_t *v = (efi_guid_t *)vendor;
107 EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
108 if (ret == EFI_BUFFER_TOO_SMALL) {
110 EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
113 if (ret != EFI_SUCCESS) {
123 * Attempt to load load-option number 'n', returning device_path and file_path
124 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
125 * and that the specified file to boot exists.
127 static void *try_load_entry(uint16_t n, struct efi_device_path **device_path,
128 struct efi_device_path **file_path)
130 struct efi_load_option lo;
131 u16 varname[] = L"Boot0000";
132 u16 hexmap[] = L"0123456789ABCDEF";
133 void *load_option, *image = NULL;
136 varname[4] = hexmap[(n & 0xf000) >> 12];
137 varname[5] = hexmap[(n & 0x0f00) >> 8];
138 varname[6] = hexmap[(n & 0x00f0) >> 4];
139 varname[7] = hexmap[(n & 0x000f) >> 0];
141 load_option = get_var(varname, &efi_global_variable_guid, &size);
145 efi_deserialize_load_option(&lo, load_option);
147 if (lo.attributes & LOAD_OPTION_ACTIVE) {
150 debug("%s: trying to load \"%ls\" from %pD\n",
151 __func__, lo.label, lo.file_path);
153 ret = efi_load_image_from_path(lo.file_path, &image);
155 if (ret != EFI_SUCCESS)
158 printf("Booting: %ls\n", lo.label);
159 efi_dp_split_file_path(lo.file_path, device_path, file_path);
169 * Attempt to load, in the order specified by BootOrder EFI variable, the
170 * available load-options, finding and returning the first one that can
171 * be loaded successfully.
173 void *efi_bootmgr_load(struct efi_device_path **device_path,
174 struct efi_device_path **file_path)
183 bs = systab.boottime;
186 bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
190 num = size / sizeof(uint16_t);
191 for (i = 0; i < num; i++) {
192 debug("%s: trying to load Boot%04X\n", __func__, bootorder[i]);
193 image = try_load_entry(bootorder[i], device_path, file_path);