blob: dce154e728ad0bca68259a66372773bb5209f861 [file] [log] [blame]
AKASHI Takahiro9b08b9a2024-01-17 13:39:41 +09001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * For the code moved from cmd/bootefi.c
4 * Copyright (c) 2016 Alexander Graf
5 */
6
7#define LOG_CATEGORY LOGC_EFI
8
9#include <charset.h>
10#include <efi.h>
11#include <efi_loader.h>
12#include <env.h>
13#include <image.h>
14#include <log.h>
15#include <malloc.h>
16
17static struct efi_device_path *bootefi_image_path;
18static struct efi_device_path *bootefi_device_path;
19static void *image_addr;
20static size_t image_size;
21
22/**
23 * efi_get_image_parameters() - return image parameters
24 *
25 * @img_addr: address of loaded image in memory
26 * @img_size: size of loaded image
27 */
28void efi_get_image_parameters(void **img_addr, size_t *img_size)
29{
30 *img_addr = image_addr;
31 *img_size = image_size;
32}
33
34/**
35 * efi_clear_bootdev() - clear boot device
36 */
37void efi_clear_bootdev(void)
38{
39 efi_free_pool(bootefi_device_path);
40 efi_free_pool(bootefi_image_path);
41 bootefi_device_path = NULL;
42 bootefi_image_path = NULL;
43 image_addr = NULL;
44 image_size = 0;
45}
46
47/**
Simon Glassa53fbb72025-01-09 08:02:38 -070048 * calculate_paths() - Calculate the device and image patch from strings
49 *
50 * @dev: device, e.g. "MMC"
51 * @devnr: number of the device, e.g. "1:2"
52 * @path: path to file loaded
53 * @device_pathp: returns EFI device path
54 * @image_pathp: returns EFI image path
55 * Return: EFI_SUCCESS on success, else error code
56 */
57static efi_status_t calculate_paths(const char *dev, const char *devnr,
58 const char *path,
59 struct efi_device_path **device_pathp,
60 struct efi_device_path **image_pathp)
61{
62 struct efi_device_path *image, *device;
63 efi_status_t ret;
64
65#if IS_ENABLED(CONFIG_NETDEVICES)
66 if (!strcmp(dev, "Net") || !strcmp(dev, "Http")) {
67 ret = efi_net_set_dp(dev, devnr);
68 if (ret != EFI_SUCCESS)
69 return ret;
70 }
71#endif
72
73 ret = efi_dp_from_name(dev, devnr, path, &device, &image);
74 if (ret != EFI_SUCCESS)
75 return ret;
76
77 *device_pathp = device;
78 if (image) {
79 /* FIXME: image should not contain device */
80 struct efi_device_path *image_tmp = image;
81
82 efi_dp_split_file_path(image, &device, &image);
83 efi_free_pool(image_tmp);
84 }
85 *image_pathp = image;
86 log_debug("- boot device %pD\n", device);
87 if (image)
88 log_debug("- image %pD\n", image);
89
90 return EFI_SUCCESS;
91}
92
93/**
AKASHI Takahiro9b08b9a2024-01-17 13:39:41 +090094 * efi_set_bootdev() - set boot device
95 *
96 * This function is called when a file is loaded, e.g. via the 'load' command.
97 * We use the path to this file to inform the UEFI binary about the boot device.
98 *
Simon Glassa53fbb72025-01-09 08:02:38 -070099 * For a valid image, it sets:
100 * - image_addr to the provided buffer
101 * - image_size to the provided buffer_size
102 * - bootefi_device_path to the EFI device-path
103 * - bootefi_image_path to the EFI image-path
104 *
AKASHI Takahiro9b08b9a2024-01-17 13:39:41 +0900105 * @dev: device, e.g. "MMC"
106 * @devnr: number of the device, e.g. "1:2"
107 * @path: path to file loaded
108 * @buffer: buffer with file loaded
109 * @buffer_size: size of file loaded
110 */
111void efi_set_bootdev(const char *dev, const char *devnr, const char *path,
112 void *buffer, size_t buffer_size)
113{
AKASHI Takahiro9b08b9a2024-01-17 13:39:41 +0900114 efi_status_t ret;
115
116 log_debug("dev=%s, devnr=%s, path=%s, buffer=%p, size=%zx\n", dev,
117 devnr, path, buffer, buffer_size);
118
119 /* Forget overwritten image */
120 if (buffer + buffer_size >= image_addr &&
121 image_addr + image_size >= buffer)
122 efi_clear_bootdev();
123
124 /* Remember only PE-COFF and FIT images */
125 if (efi_check_pe(buffer, buffer_size, NULL) != EFI_SUCCESS) {
126 if (IS_ENABLED(CONFIG_FIT) &&
127 !fit_check_format(buffer, IMAGE_SIZE_INVAL)) {
128 /*
129 * FIT images of type EFI_OS are started via command
130 * bootm. We should not use their boot device with the
131 * bootefi command.
132 */
133 buffer = 0;
134 buffer_size = 0;
135 } else {
136 log_debug("- not remembering image\n");
137 return;
138 }
139 }
140
141 /* efi_set_bootdev() is typically called repeatedly, recover memory */
142 efi_clear_bootdev();
143
144 image_addr = buffer;
145 image_size = buffer_size;
146
Simon Glassa53fbb72025-01-09 08:02:38 -0700147 ret = calculate_paths(dev, devnr, path, &bootefi_device_path,
148 &bootefi_image_path);
149 if (ret) {
150 log_debug("- efi_dp_from_name() failed, err=%lx\n", ret);
151 efi_clear_bootdev();
AKASHI Takahiro9b08b9a2024-01-17 13:39:41 +0900152 }
153}
154
155/**
156 * efi_run_image() - run loaded UEFI image
157 *
158 * @source_buffer: memory address of the UEFI image
159 * @source_size: size of the UEFI image
Simon Glassa39a78b2025-01-23 15:07:20 -0700160 * @dp_dev: EFI device-path
161 * @dp_img: EFI image-path
AKASHI Takahiro9b08b9a2024-01-17 13:39:41 +0900162 * Return: status code
163 */
Simon Glassa39a78b2025-01-23 15:07:20 -0700164static efi_status_t efi_run_image(void *source_buffer, efi_uintn_t source_size,
165 struct efi_device_path *dp_dev,
166 struct efi_device_path *dp_img)
AKASHI Takahiro9b08b9a2024-01-17 13:39:41 +0900167{
Simon Glassc14b38f2025-01-23 15:07:22 -0700168 efi_handle_t handle;
169 struct efi_device_path *msg_path, *file_path;
Heinrich Schuchardtb41a33f2024-03-16 10:36:42 +0100170 efi_status_t ret;
AKASHI Takahiro9b08b9a2024-01-17 13:39:41 +0900171 u16 *load_options;
172
Simon Glassc14b38f2025-01-23 15:07:22 -0700173 file_path = efi_dp_concat(dp_dev, dp_img, 0);
174 msg_path = dp_img;
AKASHI Takahiro9b08b9a2024-01-17 13:39:41 +0900175
176 log_info("Booting %pD\n", msg_path);
177
178 ret = EFI_CALL(efi_load_image(false, efi_root, file_path, source_buffer,
179 source_size, &handle));
180 if (ret != EFI_SUCCESS) {
181 log_err("Loading image failed\n");
182 goto out;
183 }
184
185 /* Transfer environment variable as load options */
186 ret = efi_env_set_load_options(handle, "bootargs", &load_options);
187 if (ret != EFI_SUCCESS)
188 goto out;
189
190 ret = do_bootefi_exec(handle, load_options);
191
192out:
Heinrich Schuchardtb41a33f2024-03-16 10:36:42 +0100193
194 return ret;
AKASHI Takahiro9b08b9a2024-01-17 13:39:41 +0900195}
196
197/**
Simon Glass44d57f62025-01-23 15:07:21 -0700198 * efi_binary_run_dp() - run loaded UEFI image
AKASHI Takahiro9b08b9a2024-01-17 13:39:41 +0900199 *
200 * @image: memory address of the UEFI image
201 * @size: size of the UEFI image
202 * @fdt: device-tree
Simon Glass44d57f62025-01-23 15:07:21 -0700203 * @dp_dev: EFI device-path
204 * @dp_img: EFI image-path
AKASHI Takahiro9b08b9a2024-01-17 13:39:41 +0900205 *
206 * Execute an EFI binary image loaded at @image.
207 * @size may be zero if the binary is loaded with U-Boot load command.
208 *
209 * Return: status code
210 */
Simon Glassc14b38f2025-01-23 15:07:22 -0700211static efi_status_t efi_binary_run_dp(void *image, size_t size, void *fdt,
212 struct efi_device_path *dp_dev,
213 struct efi_device_path *dp_img)
AKASHI Takahiro9b08b9a2024-01-17 13:39:41 +0900214{
215 efi_status_t ret;
216
217 /* Initialize EFI drivers */
218 ret = efi_init_obj_list();
219 if (ret != EFI_SUCCESS) {
220 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
221 ret & ~EFI_ERROR_MASK);
222 return -1;
223 }
224
225 ret = efi_install_fdt(fdt);
226 if (ret != EFI_SUCCESS)
227 return ret;
228
Simon Glass44d57f62025-01-23 15:07:21 -0700229 return efi_run_image(image, size, dp_dev, dp_img);
230}
231
232/**
233 * efi_binary_run() - run loaded UEFI image
234 *
235 * @image: memory address of the UEFI image
236 * @size: size of the UEFI image
237 * @fdt: device-tree
238 *
239 * Execute an EFI binary image loaded at @image.
240 * @size may be zero if the binary is loaded with U-Boot load command.
241 *
242 * Return: status code
243 */
244efi_status_t efi_binary_run(void *image, size_t size, void *fdt)
245{
Simon Glassc14b38f2025-01-23 15:07:22 -0700246 efi_handle_t mem_handle = NULL;
247 struct efi_device_path *file_path = NULL;
248 efi_status_t ret;
249
250 if (!bootefi_device_path || !bootefi_image_path) {
251 log_debug("Not loaded from disk\n");
252 /*
253 * Special case for efi payload not loaded from disk,
254 * such as 'bootefi hello' or for example payload
255 * loaded directly into memory via JTAG, etc:
256 */
257 file_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
258 (uintptr_t)image, size);
259 /*
260 * Make sure that device for device_path exist
261 * in load_image(). Otherwise, shell and grub will fail.
262 */
263 ret = efi_install_multiple_protocol_interfaces(&mem_handle,
264 &efi_guid_device_path,
265 file_path, NULL);
266 if (ret != EFI_SUCCESS)
267 goto out;
268 } else {
269 log_debug("Loaded from disk\n");
270 }
271
272 ret = efi_binary_run_dp(image, size, fdt, bootefi_device_path,
273 bootefi_image_path);
274out:
275 if (mem_handle) {
276 efi_status_t r;
277
278 r = efi_uninstall_multiple_protocol_interfaces(mem_handle,
279 &efi_guid_device_path, file_path, NULL);
280 if (r != EFI_SUCCESS)
281 log_err("Uninstalling protocol interfaces failed\n");
282 }
283 efi_free_pool(file_path);
284
285 return ret;
AKASHI Takahiro9b08b9a2024-01-17 13:39:41 +0900286}