blob: 52bea4d5411ef26dd10957241c82af2f9d7014e1 [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
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +02008#define LOG_CATEGORY LOGC_EFI
9
Rob Clark9975fe92017-09-13 18:05:38 -040010#include <common.h>
11#include <charset.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Rob Clark9975fe92017-09-13 18:05:38 -040013#include <malloc.h>
14#include <efi_loader.h>
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +020015#include <efi_variable.h>
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090016#include <asm/unaligned.h>
Rob Clark9975fe92017-09-13 18:05:38 -040017
18static const struct efi_boot_services *bs;
19static const struct efi_runtime_services *rs;
20
Rob Clark9975fe92017-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 Schuchardt1064d042020-08-07 17:47:13 +020033/**
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +020034 * try_load_entry() - try to load image for boot option
35 *
Rob Clark9975fe92017-09-13 18:05:38 -040036 * Attempt to load load-option number 'n', returning device_path and file_path
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +020037 * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
Rob Clark9975fe92017-09-13 18:05:38 -040038 * and that the specified file to boot exists.
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +020039 *
Heinrich Schuchardt0ad64002020-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 Clark9975fe92017-09-13 18:05:38 -040044 */
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +020045static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
46 void **load_options)
Rob Clark9975fe92017-09-13 18:05:38 -040047{
AKASHI Takahiro1a82b342018-11-05 18:06:41 +090048 struct efi_load_option lo;
Heinrich Schuchardt4f419962022-04-25 23:35:01 +020049 u16 varname[9];
AKASHI Takahiro6b95b382019-04-19 12:22:35 +090050 void *load_option;
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +020051 efi_uintn_t size;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +090052 efi_status_t ret;
Rob Clark9975fe92017-09-13 18:05:38 -040053
Heinrich Schuchardt4f419962022-04-25 23:35:01 +020054 efi_create_indexed_name(varname, sizeof(varname), "Boot", n);
Rob Clark9975fe92017-09-13 18:05:38 -040055
Ilias Apalodimasf4dc1bc2021-03-27 10:56:07 +020056 load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
Rob Clark9975fe92017-09-13 18:05:38 -040057 if (!load_option)
AKASHI Takahiro6b95b382019-04-19 12:22:35 +090058 return EFI_LOAD_ERROR;
Rob Clark9975fe92017-09-13 18:05:38 -040059
Heinrich Schuchardt0e69bcf2020-05-31 22:46:09 +020060 ret = efi_deserialize_load_option(&lo, load_option, &size);
61 if (ret != EFI_SUCCESS) {
62 log_warning("Invalid load option for %ls\n", varname);
63 goto error;
64 }
Rob Clark9975fe92017-09-13 18:05:38 -040065
66 if (lo.attributes & LOAD_OPTION_ACTIVE) {
AKASHI Takahiro37279ad2019-03-20 09:07:55 +090067 u32 attributes;
Rob Clark9975fe92017-09-13 18:05:38 -040068
Heinrich Schuchardt7ea79e52022-04-29 07:15:04 +020069 log_debug("trying to load \"%ls\" from %pD\n", lo.label,
70 lo.file_path);
Rob Clark9975fe92017-09-13 18:05:38 -040071
AKASHI Takahiro6b95b382019-04-19 12:22:35 +090072 ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path,
73 NULL, 0, handle));
AKASHI Takahiro94e6e552019-05-29 20:54:25 +020074 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +020075 log_warning("Loading %ls '%ls' failed\n",
76 varname, lo.label);
Rob Clark9975fe92017-09-13 18:05:38 -040077 goto error;
AKASHI Takahiro94e6e552019-05-29 20:54:25 +020078 }
Rob Clark9975fe92017-09-13 18:05:38 -040079
AKASHI Takahiro37279ad2019-03-20 09:07:55 +090080 attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
81 EFI_VARIABLE_RUNTIME_ACCESS;
Simon Glass156ccbc2022-01-23 12:55:12 -070082 ret = efi_set_variable_int(u"BootCurrent",
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +020083 &efi_global_variable_guid,
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +020084 attributes, sizeof(n), &n, false);
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +020085 if (ret != EFI_SUCCESS)
86 goto unload;
87 /* try to register load file2 for initrd's */
88 if (IS_ENABLED(CONFIG_EFI_LOAD_FILE2_INITRD)) {
89 ret = efi_initrd_register();
90 if (ret != EFI_SUCCESS)
91 goto unload;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +090092 }
AKASHI Takahiro37279ad2019-03-20 09:07:55 +090093
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +020094 log_info("Booting: %ls\n", lo.label);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +090095 } else {
96 ret = EFI_LOAD_ERROR;
Rob Clark9975fe92017-09-13 18:05:38 -040097 }
98
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +020099 /* Set load options */
100 if (size) {
101 *load_options = malloc(size);
102 if (!*load_options) {
103 ret = EFI_OUT_OF_RESOURCES;
104 goto error;
105 }
106 memcpy(*load_options, lo.optional_data, size);
107 ret = efi_set_load_options(*handle, size, *load_options);
108 } else {
Heinrich Schuchardte4343112020-12-27 15:46:00 +0100109 *load_options = NULL;
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200110 }
111
Rob Clark9975fe92017-09-13 18:05:38 -0400112error:
113 free(load_option);
114
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900115 return ret;
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +0200116
117unload:
118 if (EFI_CALL(efi_unload_image(*handle)) != EFI_SUCCESS)
119 log_err("Unloading image failed\n");
120 free(load_option);
121
122 return ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400123}
124
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200125/**
126 * efi_bootmgr_load() - try to load from BootNext or BootOrder
127 *
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900128 * Attempt to load from BootNext or in the order specified by BootOrder
129 * EFI variable, the available load-options, finding and returning
130 * the first one that can be loaded successfully.
Heinrich Schuchardt15621aa2019-07-14 13:20:28 +0200131 *
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200132 * @handle: on return handle for the newly installed image
133 * @load_options: load options set on the loaded image protocol
134 * Return: status code
Rob Clark9975fe92017-09-13 18:05:38 -0400135 */
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200136efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)
Rob Clark9975fe92017-09-13 18:05:38 -0400137{
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900138 u16 bootnext, *bootorder;
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200139 efi_uintn_t size;
Rob Clark9975fe92017-09-13 18:05:38 -0400140 int i, num;
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900141 efi_status_t ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400142
Rob Clark9975fe92017-09-13 18:05:38 -0400143 bs = systab.boottime;
144 rs = systab.runtime;
145
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900146 /* BootNext */
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900147 size = sizeof(bootnext);
Simon Glass156ccbc2022-01-23 12:55:12 -0700148 ret = efi_get_variable_int(u"BootNext",
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +0200149 &efi_global_variable_guid,
150 NULL, &size, &bootnext, NULL);
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900151 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
152 /* BootNext does exist here */
153 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200154 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900155
156 /* delete BootNext */
Simon Glass156ccbc2022-01-23 12:55:12 -0700157 ret = efi_set_variable_int(u"BootNext",
Heinrich Schuchardtdda8c712020-06-24 19:09:18 +0200158 &efi_global_variable_guid,
159 0, 0, NULL, false);
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900160
161 /* load BootNext */
162 if (ret == EFI_SUCCESS) {
163 if (size == sizeof(u16)) {
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200164 ret = try_load_entry(bootnext, handle,
165 load_options);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900166 if (ret == EFI_SUCCESS)
167 return ret;
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200168 log_warning(
169 "Loading from BootNext failed, falling back to BootOrder\n");
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900170 }
171 } else {
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200172 log_err("Deleting BootNext failed\n");
AKASHI Takahiro37279ad2019-03-20 09:07:55 +0900173 }
174 }
175
176 /* BootOrder */
Simon Glass156ccbc2022-01-23 12:55:12 -0700177 bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
Heinrich Schuchardt33e44972019-02-24 04:44:48 +0100178 if (!bootorder) {
Heinrich Schuchardt7a373e52020-05-31 10:07:31 +0200179 log_info("BootOrder not defined\n");
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900180 ret = EFI_NOT_FOUND;
Rob Clark9975fe92017-09-13 18:05:38 -0400181 goto error;
Heinrich Schuchardt33e44972019-02-24 04:44:48 +0100182 }
Rob Clark9975fe92017-09-13 18:05:38 -0400183
184 num = size / sizeof(uint16_t);
185 for (i = 0; i < num; i++) {
Heinrich Schuchardt7ea79e52022-04-29 07:15:04 +0200186 log_debug("trying to load Boot%04X\n", bootorder[i]);
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200187 ret = try_load_entry(bootorder[i], handle, load_options);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900188 if (ret == EFI_SUCCESS)
Rob Clark9975fe92017-09-13 18:05:38 -0400189 break;
190 }
191
192 free(bootorder);
193
194error:
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900195 return ret;
Rob Clark9975fe92017-09-13 18:05:38 -0400196}