xref: /openbmc/u-boot/lib/efi_loader/efi_bootmgr.c (revision 7086a71a)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  EFI utils
4  *
5  *  Copyright (c) 2017 Rob Clark
6  */
7 
8 #include <common.h>
9 #include <charset.h>
10 #include <malloc.h>
11 #include <efi_loader.h>
12 
13 static const struct efi_boot_services *bs;
14 static const struct efi_runtime_services *rs;
15 
16 #define LOAD_OPTION_ACTIVE		0x00000001
17 #define LOAD_OPTION_FORCE_RECONNECT	0x00000002
18 #define LOAD_OPTION_HIDDEN		0x00000008
19 
20 /*
21  * bootmgr implements the logic of trying to find a payload to boot
22  * based on the BootOrder + BootXXXX variables, and then loading it.
23  *
24  * TODO detecting a special key held (f9?) and displaying a boot menu
25  * like you would get on a PC would be clever.
26  *
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.
30  */
31 
32 
33 /*
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:
36  *
37  *    typedef struct _EFI_LOAD_OPTION {
38  *        UINT32 Attributes;
39  *        UINT16 FilePathListLength;
40  *        // CHAR16 Description[];   <-- variable length, NULL terminated
41  *        // EFI_DEVICE_PATH_PROTOCOL FilePathList[];  <-- FilePathListLength bytes
42  *        // UINT8 OptionalData[];
43  *    } EFI_LOAD_OPTION;
44  */
45 struct load_option {
46 	u32 attributes;
47 	u16 file_path_length;
48 	u16 *label;
49 	struct efi_device_path *file_path;
50 	u8 *optional_data;
51 };
52 
53 /* parse an EFI_LOAD_OPTION, as described above */
54 static void parse_load_option(struct load_option *lo, void *ptr)
55 {
56 	lo->attributes = *(u32 *)ptr;
57 	ptr += sizeof(u32);
58 
59 	lo->file_path_length = *(u16 *)ptr;
60 	ptr += sizeof(u16);
61 
62 	lo->label = ptr;
63 	ptr += (u16_strlen(lo->label) + 1) * 2;
64 
65 	lo->file_path = ptr;
66 	ptr += lo->file_path_length;
67 
68 	lo->optional_data = ptr;
69 }
70 
71 /* free() the result */
72 static void *get_var(u16 *name, const efi_guid_t *vendor,
73 		     efi_uintn_t *size)
74 {
75 	efi_guid_t *v = (efi_guid_t *)vendor;
76 	efi_status_t ret;
77 	void *buf = NULL;
78 
79 	*size = 0;
80 	EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
81 	if (ret == EFI_BUFFER_TOO_SMALL) {
82 		buf = malloc(*size);
83 		EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
84 	}
85 
86 	if (ret != EFI_SUCCESS) {
87 		free(buf);
88 		*size = 0;
89 		return NULL;
90 	}
91 
92 	return buf;
93 }
94 
95 /*
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.
99  */
100 static void *try_load_entry(uint16_t n, struct efi_device_path **device_path,
101 			    struct efi_device_path **file_path)
102 {
103 	struct load_option lo;
104 	u16 varname[] = L"Boot0000";
105 	u16 hexmap[] = L"0123456789ABCDEF";
106 	void *load_option, *image = NULL;
107 	efi_uintn_t size;
108 
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];
113 
114 	load_option = get_var(varname, &efi_global_variable_guid, &size);
115 	if (!load_option)
116 		return NULL;
117 
118 	parse_load_option(&lo, load_option);
119 
120 	if (lo.attributes & LOAD_OPTION_ACTIVE) {
121 		efi_status_t ret;
122 
123 		debug("%s: trying to load \"%ls\" from %pD\n",
124 		      __func__, lo.label, lo.file_path);
125 
126 		ret = efi_load_image_from_path(lo.file_path, &image);
127 
128 		if (ret != EFI_SUCCESS)
129 			goto error;
130 
131 		printf("Booting: %ls\n", lo.label);
132 		efi_dp_split_file_path(lo.file_path, device_path, file_path);
133 	}
134 
135 error:
136 	free(load_option);
137 
138 	return image;
139 }
140 
141 /*
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.
145  */
146 void *efi_bootmgr_load(struct efi_device_path **device_path,
147 		       struct efi_device_path **file_path)
148 {
149 	uint16_t *bootorder;
150 	efi_uintn_t size;
151 	void *image = NULL;
152 	int i, num;
153 
154 	__efi_entry_check();
155 
156 	bs = systab.boottime;
157 	rs = systab.runtime;
158 
159 	bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
160 	if (!bootorder)
161 		goto error;
162 
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);
167 		if (image)
168 			break;
169 	}
170 
171 	free(bootorder);
172 
173 error:
174 	__efi_exit_check();
175 
176 	return image;
177 }
178