1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  EFI boot manager
4  *
5  *  Copyright (c) 2017 Rob Clark
6  */
7 
8 #define LOG_CATEGORY LOGC_EFI
9 
10 #include <common.h>
11 #include <charset.h>
12 #include <log.h>
13 #include <malloc.h>
14 #include <efi_loader.h>
15 #include <efi_variable.h>
16 #include <asm/unaligned.h>
17 
18 static const struct efi_boot_services *bs;
19 static const struct efi_runtime_services *rs;
20 
21 /*
22  * bootmgr implements the logic of trying to find a payload to boot
23  * based on the BootOrder + BootXXXX variables, and then loading it.
24  *
25  * TODO detecting a special key held (f9?) and displaying a boot menu
26  * like you would get on a PC would be clever.
27  *
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.
31  */
32 
33 /**
34  * get_var() - get UEFI variable
35  *
36  * It is the caller's duty to free the returned buffer.
37  *
38  * @name:	name of variable
39  * @vendor:	vendor GUID of variable
40  * @size:	size of allocated buffer
41  * Return:	buffer with variable data or NULL
42  */
get_var(u16 * name,const efi_guid_t * vendor,efi_uintn_t * size)43 static void *get_var(u16 *name, const efi_guid_t *vendor,
44 		     efi_uintn_t *size)
45 {
46 	efi_status_t ret;
47 	void *buf = NULL;
48 
49 	*size = 0;
50 	ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL);
51 	if (ret == EFI_BUFFER_TOO_SMALL) {
52 		buf = malloc(*size);
53 		ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL);
54 	}
55 
56 	if (ret != EFI_SUCCESS) {
57 		free(buf);
58 		*size = 0;
59 		return NULL;
60 	}
61 
62 	return buf;
63 }
64 
65 /**
66  * try_load_entry() - try to load image for boot option
67  *
68  * Attempt to load load-option number 'n', returning device_path and file_path
69  * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
70  * and that the specified file to boot exists.
71  *
72  * @n:			number of the boot option, e.g. 0x0a13 for Boot0A13
73  * @handle:		on return handle for the newly installed image
74  * @load_options:	load options set on the loaded image protocol
75  * Return:		status code
76  */
try_load_entry(u16 n,efi_handle_t * handle,void ** load_options)77 static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
78 				   void **load_options)
79 {
80 	struct efi_load_option lo;
81 	u16 varname[] = L"Boot0000";
82 	u16 hexmap[] = L"0123456789ABCDEF";
83 	void *load_option;
84 	efi_uintn_t size;
85 	efi_status_t ret;
86 
87 	varname[4] = hexmap[(n & 0xf000) >> 12];
88 	varname[5] = hexmap[(n & 0x0f00) >> 8];
89 	varname[6] = hexmap[(n & 0x00f0) >> 4];
90 	varname[7] = hexmap[(n & 0x000f) >> 0];
91 
92 	load_option = get_var(varname, &efi_global_variable_guid, &size);
93 	if (!load_option)
94 		return EFI_LOAD_ERROR;
95 
96 	ret = efi_deserialize_load_option(&lo, load_option, &size);
97 	if (ret != EFI_SUCCESS) {
98 		log_warning("Invalid load option for %ls\n", varname);
99 		goto error;
100 	}
101 
102 	if (lo.attributes & LOAD_OPTION_ACTIVE) {
103 		u32 attributes;
104 
105 		log_debug("%s: trying to load \"%ls\" from %pD\n",
106 			  __func__, lo.label, lo.file_path);
107 
108 		ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path,
109 					      NULL, 0, handle));
110 		if (ret != EFI_SUCCESS) {
111 			log_warning("Loading %ls '%ls' failed\n",
112 				    varname, lo.label);
113 			goto error;
114 		}
115 
116 		attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
117 			     EFI_VARIABLE_RUNTIME_ACCESS;
118 		ret = efi_set_variable_int(L"BootCurrent",
119 					   &efi_global_variable_guid,
120 					   attributes, sizeof(n), &n, false);
121 		if (ret != EFI_SUCCESS) {
122 			if (EFI_CALL(efi_unload_image(*handle))
123 			    != EFI_SUCCESS)
124 				log_err("Unloading image failed\n");
125 			goto error;
126 		}
127 
128 		log_info("Booting: %ls\n", lo.label);
129 	} else {
130 		ret = EFI_LOAD_ERROR;
131 	}
132 
133 	/* Set load options */
134 	if (size) {
135 		*load_options = malloc(size);
136 		if (!*load_options) {
137 			ret = EFI_OUT_OF_RESOURCES;
138 			goto error;
139 		}
140 		memcpy(*load_options, lo.optional_data, size);
141 		ret = efi_set_load_options(*handle, size, *load_options);
142 	} else {
143 		*load_options = NULL;
144 	}
145 
146 error:
147 	free(load_option);
148 
149 	return ret;
150 }
151 
152 /**
153  * efi_bootmgr_load() - try to load from BootNext or BootOrder
154  *
155  * Attempt to load from BootNext or in the order specified by BootOrder
156  * EFI variable, the available load-options, finding and returning
157  * the first one that can be loaded successfully.
158  *
159  * @handle:		on return handle for the newly installed image
160  * @load_options:	load options set on the loaded image protocol
161  * Return:		status code
162  */
efi_bootmgr_load(efi_handle_t * handle,void ** load_options)163 efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)
164 {
165 	u16 bootnext, *bootorder;
166 	efi_uintn_t size;
167 	int i, num;
168 	efi_status_t ret;
169 
170 	bs = systab.boottime;
171 	rs = systab.runtime;
172 
173 	/* BootNext */
174 	size = sizeof(bootnext);
175 	ret = efi_get_variable_int(L"BootNext",
176 				   &efi_global_variable_guid,
177 				   NULL, &size, &bootnext, NULL);
178 	if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
179 		/* BootNext does exist here */
180 		if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
181 			log_err("BootNext must be 16-bit integer\n");
182 
183 		/* delete BootNext */
184 		ret = efi_set_variable_int(L"BootNext",
185 					   &efi_global_variable_guid,
186 					   0, 0, NULL, false);
187 
188 		/* load BootNext */
189 		if (ret == EFI_SUCCESS) {
190 			if (size == sizeof(u16)) {
191 				ret = try_load_entry(bootnext, handle,
192 						     load_options);
193 				if (ret == EFI_SUCCESS)
194 					return ret;
195 				log_warning(
196 					"Loading from BootNext failed, falling back to BootOrder\n");
197 			}
198 		} else {
199 			log_err("Deleting BootNext failed\n");
200 		}
201 	}
202 
203 	/* BootOrder */
204 	bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
205 	if (!bootorder) {
206 		log_info("BootOrder not defined\n");
207 		ret = EFI_NOT_FOUND;
208 		goto error;
209 	}
210 
211 	num = size / sizeof(uint16_t);
212 	for (i = 0; i < num; i++) {
213 		log_debug("%s trying to load Boot%04X\n", __func__,
214 			  bootorder[i]);
215 		ret = try_load_entry(bootorder[i], handle, load_options);
216 		if (ret == EFI_SUCCESS)
217 			break;
218 	}
219 
220 	free(bootorder);
221 
222 error:
223 	return ret;
224 }
225