blob: 196116b54729b871e1aa864af2cb981e835a1537 [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Rob Clark9975fe92017-09-13 18:05:38 -04002/*
Heinrich Schuchardte1fec152018-10-18 21:51:38 +02003 * EFI boot manager
Rob Clark9975fe92017-09-13 18:05:38 -04004 *
5 * Copyright (c) 2017 Rob Clark
Rob Clark9975fe92017-09-13 18:05:38 -04006 */
7
8#include <common.h>
9#include <charset.h>
10#include <malloc.h>
11#include <efi_loader.h>
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090012#include <asm/unaligned.h>
Rob Clark9975fe92017-09-13 18:05:38 -040013
14static const struct efi_boot_services *bs;
15static const struct efi_runtime_services *rs;
16
17#define LOAD_OPTION_ACTIVE 0x00000001
18#define LOAD_OPTION_FORCE_RECONNECT 0x00000002
19#define LOAD_OPTION_HIDDEN 0x00000008
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
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090034/* Parse serialized data and transform it into efi_load_option structure */
35void efi_deserialize_load_option(struct efi_load_option *lo, u8 *data)
Rob Clark9975fe92017-09-13 18:05:38 -040036{
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090037 lo->attributes = get_unaligned_le32(data);
38 data += sizeof(u32);
Rob Clark9975fe92017-09-13 18:05:38 -040039
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090040 lo->file_path_length = get_unaligned_le16(data);
41 data += sizeof(u16);
Rob Clark9975fe92017-09-13 18:05:38 -040042
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090043 /* FIXME */
44 lo->label = (u16 *)data;
45 data += (u16_strlen(lo->label) + 1) * sizeof(u16);
Rob Clark9975fe92017-09-13 18:05:38 -040046
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090047 /* FIXME */
48 lo->file_path = (struct efi_device_path *)data;
49 data += lo->file_path_length;
Rob Clark9975fe92017-09-13 18:05:38 -040050
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090051 lo->optional_data = data;
52}
53
54/*
55 * Serialize efi_load_option structure into byte stream for BootXXXX.
56 * Return a size of allocated data.
57 */
58unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data)
59{
60 unsigned long label_len, option_len;
61 unsigned long size;
62 u8 *p;
63
64 label_len = (u16_strlen(lo->label) + 1) * sizeof(u16);
65 option_len = strlen((char *)lo->optional_data);
66
67 /* total size */
68 size = sizeof(lo->attributes);
69 size += sizeof(lo->file_path_length);
70 size += label_len;
71 size += lo->file_path_length;
72 size += option_len + 1;
73 p = malloc(size);
74 if (!p)
75 return 0;
76
77 /* copy data */
78 *data = p;
79 memcpy(p, &lo->attributes, sizeof(lo->attributes));
80 p += sizeof(lo->attributes);
81
82 memcpy(p, &lo->file_path_length, sizeof(lo->file_path_length));
83 p += sizeof(lo->file_path_length);
84
85 memcpy(p, lo->label, label_len);
86 p += label_len;
87
88 memcpy(p, lo->file_path, lo->file_path_length);
89 p += lo->file_path_length;
90
91 memcpy(p, lo->optional_data, option_len);
92 p += option_len;
93 *(char *)p = '\0';
94
95 return size;
Rob Clark9975fe92017-09-13 18:05:38 -040096}
97
98/* free() the result */
99static void *get_var(u16 *name, const efi_guid_t *vendor,
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200100 efi_uintn_t *size)
Rob Clark9975fe92017-09-13 18:05:38 -0400101{
102 efi_guid_t *v = (efi_guid_t *)vendor;
103 efi_status_t ret;
104 void *buf = NULL;
105
106 *size = 0;
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200107 EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
Rob Clark9975fe92017-09-13 18:05:38 -0400108 if (ret == EFI_BUFFER_TOO_SMALL) {
109 buf = malloc(*size);
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200110 EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
Rob Clark9975fe92017-09-13 18:05:38 -0400111 }
112
113 if (ret != EFI_SUCCESS) {
114 free(buf);
115 *size = 0;
116 return NULL;
117 }
118
119 return buf;
120}
121
122/*
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.
126 */
127static void *try_load_entry(uint16_t n, struct efi_device_path **device_path,
128 struct efi_device_path **file_path)
129{
AKASHI Takahiro1a82b342018-11-05 18:06:41 +0900130 struct efi_load_option lo;
Rob Clark9975fe92017-09-13 18:05:38 -0400131 u16 varname[] = L"Boot0000";
132 u16 hexmap[] = L"0123456789ABCDEF";
133 void *load_option, *image = NULL;
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200134 efi_uintn_t size;
Rob Clark9975fe92017-09-13 18:05:38 -0400135
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];
140
141 load_option = get_var(varname, &efi_global_variable_guid, &size);
142 if (!load_option)
143 return NULL;
144
AKASHI Takahiro1a82b342018-11-05 18:06:41 +0900145 efi_deserialize_load_option(&lo, load_option);
Rob Clark9975fe92017-09-13 18:05:38 -0400146
147 if (lo.attributes & LOAD_OPTION_ACTIVE) {
148 efi_status_t ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400149
Heinrich Schuchardt4a8b5e72018-01-26 07:20:54 +0100150 debug("%s: trying to load \"%ls\" from %pD\n",
151 __func__, lo.label, lo.file_path);
Rob Clark9975fe92017-09-13 18:05:38 -0400152
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +0100153 ret = efi_load_image_from_path(lo.file_path, &image, &size);
Rob Clark9975fe92017-09-13 18:05:38 -0400154
155 if (ret != EFI_SUCCESS)
156 goto error;
157
158 printf("Booting: %ls\n", lo.label);
159 efi_dp_split_file_path(lo.file_path, device_path, file_path);
160 }
161
162error:
163 free(load_option);
164
165 return image;
166}
167
168/*
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.
172 */
173void *efi_bootmgr_load(struct efi_device_path **device_path,
174 struct efi_device_path **file_path)
175{
176 uint16_t *bootorder;
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200177 efi_uintn_t size;
Rob Clark9975fe92017-09-13 18:05:38 -0400178 void *image = NULL;
179 int i, num;
180
181 __efi_entry_check();
182
183 bs = systab.boottime;
184 rs = systab.runtime;
185
186 bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
187 if (!bootorder)
188 goto error;
189
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);
194 if (image)
195 break;
196 }
197
198 free(bootorder);
199
200error:
201 __efi_exit_check();
202
203 return image;
204}