blob: 1fe19237f9a6ce2ea18558717985e62aec184192 [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Rob Clarkc84c1102017-09-13 18:05:38 -04002/*
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +02003 * EFI boot manager
Rob Clarkc84c1102017-09-13 18:05:38 -04004 *
5 * Copyright (c) 2017 Rob Clark
Rob Clarkc84c1102017-09-13 18:05:38 -04006 */
7
Heinrich Schuchardt273df962020-05-31 10:07:31 +02008#define LOG_CATEGORY LOGC_EFI
9
Rob Clarkc84c1102017-09-13 18:05:38 -040010#include <common.h>
11#include <charset.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040013#include <malloc.h>
14#include <efi_loader.h>
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +020015#include <efi_variable.h>
AKASHI Takahirobd237742018-11-05 18:06:41 +090016#include <asm/unaligned.h>
Rob Clarkc84c1102017-09-13 18:05:38 -040017
18static const struct efi_boot_services *bs;
19static const struct efi_runtime_services *rs;
20
Rob Clarkc84c1102017-09-13 18:05:38 -040021/*
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
Heinrich Schuchardt25c6be52020-08-07 17:47:13 +020033/**
Heinrich Schuchardte612ba62019-07-14 13:20:28 +020034 * try_load_entry() - try to load image for boot option
35 *
Rob Clarkc84c1102017-09-13 18:05:38 -040036 * Attempt to load load-option number 'n', returning device_path and file_path
Heinrich Schuchardte612ba62019-07-14 13:20:28 +020037 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
Rob Clarkc84c1102017-09-13 18:05:38 -040038 * and that the specified file to boot exists.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +020039 *
Heinrich Schuchardta7647a72020-08-07 17:49:39 +020040 * @n: number of the boot option, e.g. 0x0a13 for Boot0A13
41 * @handle: on return handle for the newly installed image
42 * @load_options: load options set on the loaded image protocol
43 * Return: status code
Rob Clarkc84c1102017-09-13 18:05:38 -040044 */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +020045static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
46 void **load_options)
Rob Clarkc84c1102017-09-13 18:05:38 -040047{
AKASHI Takahirobd237742018-11-05 18:06:41 +090048 struct efi_load_option lo;
Rob Clarkc84c1102017-09-13 18:05:38 -040049 u16 varname[] = L"Boot0000";
50 u16 hexmap[] = L"0123456789ABCDEF";
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +090051 void *load_option;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +020052 efi_uintn_t size;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +090053 efi_status_t ret;
Rob Clarkc84c1102017-09-13 18:05:38 -040054
55 varname[4] = hexmap[(n & 0xf000) >> 12];
56 varname[5] = hexmap[(n & 0x0f00) >> 8];
57 varname[6] = hexmap[(n & 0x00f0) >> 4];
58 varname[7] = hexmap[(n & 0x000f) >> 0];
59
Ilias Apalodimasfc4ca6b2021-03-27 10:56:07 +020060 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
Rob Clarkc84c1102017-09-13 18:05:38 -040061 if (!load_option)
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +090062 return EFI_LOAD_ERROR;
Rob Clarkc84c1102017-09-13 18:05:38 -040063
Heinrich Schuchardt7ca84f82020-05-31 22:46:09 +020064 ret = efi_deserialize_load_option(&lo, load_option, &size);
65 if (ret != EFI_SUCCESS) {
66 log_warning("Invalid load option for %ls\n", varname);
67 goto error;
68 }
Rob Clarkc84c1102017-09-13 18:05:38 -040069
70 if (lo.attributes & LOAD_OPTION_ACTIVE) {
AKASHI Takahiroe6577f92019-03-20 09:07:55 +090071 u32 attributes;
Rob Clarkc84c1102017-09-13 18:05:38 -040072
Heinrich Schuchardt273df962020-05-31 10:07:31 +020073 log_debug("%s: trying to load \"%ls\" from %pD\n",
74 __func__, lo.label, lo.file_path);
Rob Clarkc84c1102017-09-13 18:05:38 -040075
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +090076 ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path,
77 NULL, 0, handle));
AKASHI Takahiro15db9ce2019-05-29 20:54:25 +020078 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt273df962020-05-31 10:07:31 +020079 log_warning("Loading %ls '%ls' failed\n",
80 varname, lo.label);
Rob Clarkc84c1102017-09-13 18:05:38 -040081 goto error;
AKASHI Takahiro15db9ce2019-05-29 20:54:25 +020082 }
Rob Clarkc84c1102017-09-13 18:05:38 -040083
AKASHI Takahiroe6577f92019-03-20 09:07:55 +090084 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
85 EFI_VARIABLE_RUNTIME_ACCESS;
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +020086 ret = efi_set_variable_int(L"BootCurrent",
87 &efi_global_variable_guid,
Heinrich Schuchardta7647a72020-08-07 17:49:39 +020088 attributes, sizeof(n), &n, false);
Ilias Apalodimasb307e3d2021-03-17 21:55:00 +020089 if (ret != EFI_SUCCESS)
90 goto unload;
91 /* try to register load file2 for initrd's */
92 if (IS_ENABLED(CONFIG_EFI_LOAD_FILE2_INITRD)) {
93 ret = efi_initrd_register();
94 if (ret != EFI_SUCCESS)
95 goto unload;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +090096 }
AKASHI Takahiroe6577f92019-03-20 09:07:55 +090097
Heinrich Schuchardt273df962020-05-31 10:07:31 +020098 log_info("Booting: %ls\n", lo.label);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +090099 } else {
100 ret = EFI_LOAD_ERROR;
Rob Clarkc84c1102017-09-13 18:05:38 -0400101 }
102
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200103 /* Set load options */
104 if (size) {
105 *load_options = malloc(size);
106 if (!*load_options) {
107 ret = EFI_OUT_OF_RESOURCES;
108 goto error;
109 }
110 memcpy(*load_options, lo.optional_data, size);
111 ret = efi_set_load_options(*handle, size, *load_options);
112 } else {
Heinrich Schuchardt7c2a8592020-12-27 15:46:00 +0100113 *load_options = NULL;
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200114 }
115
Rob Clarkc84c1102017-09-13 18:05:38 -0400116error:
117 free(load_option);
118
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900119 return ret;
Ilias Apalodimasb307e3d2021-03-17 21:55:00 +0200120
121unload:
122 if (EFI_CALL(efi_unload_image(*handle)) != EFI_SUCCESS)
123 log_err("Unloading image failed\n");
124 free(load_option);
125
126 return ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400127}
128
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200129/**
130 * efi_bootmgr_load() - try to load from BootNext or BootOrder
131 *
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900132 * Attempt to load from BootNext or in the order specified by BootOrder
133 * EFI variable, the available load-options, finding and returning
134 * the first one that can be loaded successfully.
Heinrich Schuchardte612ba62019-07-14 13:20:28 +0200135 *
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200136 * @handle: on return handle for the newly installed image
137 * @load_options: load options set on the loaded image protocol
138 * Return: status code
Rob Clarkc84c1102017-09-13 18:05:38 -0400139 */
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200140efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)
Rob Clarkc84c1102017-09-13 18:05:38 -0400141{
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900142 u16 bootnext, *bootorder;
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200143 efi_uintn_t size;
Rob Clarkc84c1102017-09-13 18:05:38 -0400144 int i, num;
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900145 efi_status_t ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400146
Rob Clarkc84c1102017-09-13 18:05:38 -0400147 bs = systab.boottime;
148 rs = systab.runtime;
149
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900150 /* BootNext */
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900151 size = sizeof(bootnext);
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200152 ret = efi_get_variable_int(L"BootNext",
153 &efi_global_variable_guid,
154 NULL, &size, &bootnext, NULL);
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900155 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
156 /* BootNext does exist here */
157 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200158 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900159
160 /* delete BootNext */
Heinrich Schuchardtf625fed2020-06-24 19:09:18 +0200161 ret = efi_set_variable_int(L"BootNext",
162 &efi_global_variable_guid,
163 0, 0, NULL, false);
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900164
165 /* load BootNext */
166 if (ret == EFI_SUCCESS) {
167 if (size == sizeof(u16)) {
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200168 ret = try_load_entry(bootnext, handle,
169 load_options);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900170 if (ret == EFI_SUCCESS)
171 return ret;
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200172 log_warning(
173 "Loading from BootNext failed, falling back to BootOrder\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900174 }
175 } else {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200176 log_err("Deleting BootNext failed\n");
AKASHI Takahiroe6577f92019-03-20 09:07:55 +0900177 }
178 }
179
180 /* BootOrder */
Ilias Apalodimasfc4ca6b2021-03-27 10:56:07 +0200181 bootorder = efi_get_var(L"BootOrder", &efi_global_variable_guid, &size);
Heinrich Schuchardt3d2257f2019-02-24 04:44:48 +0100182 if (!bootorder) {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200183 log_info("BootOrder not defined\n");
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900184 ret = EFI_NOT_FOUND;
Rob Clarkc84c1102017-09-13 18:05:38 -0400185 goto error;
Heinrich Schuchardt3d2257f2019-02-24 04:44:48 +0100186 }
Rob Clarkc84c1102017-09-13 18:05:38 -0400187
188 num = size / sizeof(uint16_t);
189 for (i = 0; i < num; i++) {
Heinrich Schuchardt273df962020-05-31 10:07:31 +0200190 log_debug("%s trying to load Boot%04X\n", __func__,
191 bootorder[i]);
Heinrich Schuchardta7647a72020-08-07 17:49:39 +0200192 ret = try_load_entry(bootorder[i], handle, load_options);
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900193 if (ret == EFI_SUCCESS)
Rob Clarkc84c1102017-09-13 18:05:38 -0400194 break;
195 }
196
197 free(bootorder);
198
199error:
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900200 return ret;
Rob Clarkc84c1102017-09-13 18:05:38 -0400201}