blob: 1e5606c7b824b59bf9e0c2fb5890d4d58c9d010a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass9539e692015-07-31 09:31:36 -06002/*
3 * Copyright (c) 2015 Google, Inc
4 *
Simon Glass9539e692015-07-31 09:31:36 -06005 * EFI information obtained here:
6 * http://wiki.phoenix.com/wiki/index.php/EFI_BOOT_SERVICES
7 *
8 * This file implements U-Boot running as an EFI application.
9 */
10
11#include <common.h>
Simon Glassafb02152019-12-28 10:45:01 -070012#include <cpu_func.h>
Simon Glass9539e692015-07-31 09:31:36 -060013#include <debug_uart.h>
Bin Meng268bc9e2018-07-19 03:07:29 -070014#include <dm.h>
Simon Glass9539e692015-07-31 09:31:36 -060015#include <errno.h>
Simon Glass97589732020-05-10 11:40:02 -060016#include <init.h>
Simon Glass9bc15642020-02-03 07:36:16 -070017#include <malloc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060018#include <asm/global_data.h>
Simon Glass9539e692015-07-31 09:31:36 -060019#include <linux/err.h>
20#include <linux/types.h>
21#include <efi.h>
22#include <efi_api.h>
Bin Meng268bc9e2018-07-19 03:07:29 -070023#include <sysreset.h>
Simon Glass2d4b33e2021-12-29 11:57:36 -070024#include <dm/device-internal.h>
25#include <dm/lists.h>
26#include <dm/root.h>
Simon Glass9539e692015-07-31 09:31:36 -060027
28DECLARE_GLOBAL_DATA_PTR;
29
Simon Glass0a3e7b32021-11-03 21:09:09 -060030int efi_info_get(enum efi_entry_t type, void **datap, int *sizep)
31{
32 return -ENOSYS;
33}
34
Simon Glassba042532022-01-04 03:51:12 -070035int efi_get_mmap(struct efi_mem_desc **descp, int *sizep, uint *keyp,
36 int *desc_sizep, uint *versionp)
37{
38 struct efi_priv *priv = efi_get_priv();
39 struct efi_boot_services *boot = priv->sys_table->boottime;
40 efi_uintn_t size, desc_size, key;
41 struct efi_mem_desc *desc;
42 efi_status_t ret;
43 u32 version;
44
45 /* Get the memory map so we can switch off EFI */
46 size = 0;
47 ret = boot->get_memory_map(&size, NULL, &key, &desc_size, &version);
48 if (ret != EFI_BUFFER_TOO_SMALL)
49 return log_msg_ret("get", -ENOMEM);
50
51 desc = malloc(size);
52 if (!desc)
53 return log_msg_ret("mem", -ENOMEM);
54
55 ret = boot->get_memory_map(&size, desc, &key, &desc_size, &version);
56 if (ret)
57 return log_msg_ret("get", -EINVAL);
58
59 *descp = desc;
60 *sizep = size;
61 *desc_sizep = desc_size;
62 *versionp = version;
63 *keyp = key;
64
65 return 0;
66}
67
Simon Glass2d4b33e2021-12-29 11:57:36 -070068/**
69 * efi_bind_block() - bind a new block device to an EFI device
70 *
71 * Binds a new top-level EFI_MEDIA device as well as a child block device so
72 * that the block device can be accessed in U-Boot.
73 *
74 * The device can then be accessed using 'part list efi 0', 'fat ls efi 0:1',
75 * for example, just like any other interface type.
76 *
77 * @handle: handle of the controller on which this driver is installed
78 * @blkio: block io protocol proxied by this driver
79 * @device_path: EFI device path structure for this
80 * @len: Length of @device_path in bytes
81 * @devp: Returns the bound device
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010082 * Return: 0 if OK, -ve on error
Simon Glass2d4b33e2021-12-29 11:57:36 -070083 */
84int efi_bind_block(efi_handle_t handle, struct efi_block_io *blkio,
85 struct efi_device_path *device_path, int len,
86 struct udevice **devp)
87{
88 struct efi_media_plat plat;
89 struct udevice *dev;
90 char name[18];
91 int ret;
92
93 plat.handle = handle;
94 plat.blkio = blkio;
95 plat.device_path = malloc(device_path->length);
96 if (!plat.device_path)
97 return log_msg_ret("path", -ENOMEM);
98 memcpy(plat.device_path, device_path, device_path->length);
99 ret = device_bind(dm_root(), DM_DRIVER_GET(efi_media), "efi_media",
100 &plat, ofnode_null(), &dev);
101 if (ret)
102 return log_msg_ret("bind", ret);
103
104 snprintf(name, sizeof(name), "efi_media_%x", dev_seq(dev));
105 device_set_name(dev, name);
106 *devp = dev;
107
108 return 0;
109}
110
Simon Glass9539e692015-07-31 09:31:36 -0600111static efi_status_t setup_memory(struct efi_priv *priv)
112{
113 struct efi_boot_services *boot = priv->boot;
114 efi_physical_addr_t addr;
115 efi_status_t ret;
116 int pages;
117
118 /*
119 * Use global_data_ptr instead of gd since it is an assignment. There
120 * are very few assignments to global_data in U-Boot and this makes
121 * it easier to find them.
122 */
123 global_data_ptr = efi_malloc(priv, sizeof(struct global_data), &ret);
124 if (!global_data_ptr)
125 return ret;
126 memset(gd, '\0', sizeof(*gd));
127
Andy Yan1fa20e4d2017-07-24 17:43:34 +0800128 gd->malloc_base = (ulong)efi_malloc(priv, CONFIG_VAL(SYS_MALLOC_F_LEN),
Simon Glass9539e692015-07-31 09:31:36 -0600129 &ret);
130 if (!gd->malloc_base)
131 return ret;
132 pages = CONFIG_EFI_RAM_SIZE >> 12;
133
134 /*
135 * Don't allocate any memory above 4GB. U-Boot is a 32-bit application
136 * so we want it to load below 4GB.
137 */
138 addr = 1ULL << 32;
139 ret = boot->allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
140 priv->image_data_type, pages, &addr);
141 if (ret) {
Simon Glass3a4e8a92021-12-29 11:57:49 -0700142 log_info("(using pool %lx) ", ret);
Simon Glass9539e692015-07-31 09:31:36 -0600143 priv->ram_base = (ulong)efi_malloc(priv, CONFIG_EFI_RAM_SIZE,
144 &ret);
145 if (!priv->ram_base)
146 return ret;
147 priv->use_pool_for_malloc = true;
148 } else {
Simon Glass3a4e8a92021-12-29 11:57:49 -0700149 log_info("(using allocated RAM address %lx) ", (ulong)addr);
Simon Glass9539e692015-07-31 09:31:36 -0600150 priv->ram_base = addr;
151 }
152 gd->ram_size = pages << 12;
153
154 return 0;
155}
156
Simon Glass2d4b33e2021-12-29 11:57:36 -0700157/**
158 * free_memory() - Free memory used by the U-Boot app
159 *
160 * This frees memory allocated in setup_memory(), in preparation for returning
161 * to UEFI. It also zeroes the global_data pointer.
162 *
163 * @priv: Private EFI data
164 */
Simon Glass9539e692015-07-31 09:31:36 -0600165static void free_memory(struct efi_priv *priv)
166{
167 struct efi_boot_services *boot = priv->boot;
168
169 if (priv->use_pool_for_malloc)
170 efi_free(priv, (void *)priv->ram_base);
171 else
172 boot->free_pages(priv->ram_base, gd->ram_size >> 12);
173
174 efi_free(priv, (void *)gd->malloc_base);
175 efi_free(priv, gd);
176 global_data_ptr = NULL;
177}
178
179/**
Simon Glass2d4b33e2021-12-29 11:57:36 -0700180 * devpath_is_partition() - Figure out if a device path is a partition
181 *
182 * Checks if a device path refers to a partition on some media device. This
183 * works by checking for a valid partition number in a hard-driver media device
184 * as the final component of the device path.
185 *
186 * @path: device path
187 * Return: true if a partition, false if not
188 * (e.g. it might be media which contains partitions)
189 */
190static bool devpath_is_partition(const struct efi_device_path *path)
191{
192 const struct efi_device_path *p;
193 bool was_part;
194
195 for (p = path; p->type != DEVICE_PATH_TYPE_END;
196 p = (void *)p + p->length) {
197 was_part = false;
198 if (p->type == DEVICE_PATH_TYPE_MEDIA_DEVICE &&
199 p->sub_type == DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH) {
200 struct efi_device_path_hard_drive_path *hd =
201 (void *)path;
202
203 if (hd->partition_number)
204 was_part = true;
205 }
206 }
207
208 return was_part;
209}
210
211/**
212 * setup_block() - Find all block devices and setup EFI devices for them
213 *
214 * Partitions are ignored, since U-Boot has partition handling. Errors with
215 * particular devices produce a warning but execution continues to try to
216 * find others.
217 *
218 * Return: 0 if found, -ENOSYS if there is no boot-services table, -ENOTSUPP
219 * if a required protocol is not supported
220 */
221static int setup_block(void)
222{
223 efi_guid_t efi_blkio_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
224 efi_guid_t efi_devpath_guid = EFI_DEVICE_PATH_PROTOCOL_GUID;
225 efi_guid_t efi_pathutil_guid = EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID;
226 efi_guid_t efi_pathtext_guid = EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
227 struct efi_boot_services *boot = efi_get_boot();
228 struct efi_device_path_utilities_protocol *util;
229 struct efi_device_path_to_text_protocol *text;
230 struct efi_device_path *path;
231 struct efi_block_io *blkio;
232 efi_uintn_t num_handles;
233 efi_handle_t *handle;
234 int ret, i;
235
236 if (!boot)
237 return log_msg_ret("sys", -ENOSYS);
238
239 /* Find all devices which support the block I/O protocol */
240 ret = boot->locate_handle_buffer(BY_PROTOCOL, &efi_blkio_guid, NULL,
241 &num_handles, &handle);
242 if (ret)
243 return log_msg_ret("loc", -ENOTSUPP);
244 log_debug("Found %d handles:\n", (int)num_handles);
245
246 /* We need to look up the path size and convert it to text */
247 ret = boot->locate_protocol(&efi_pathutil_guid, NULL, (void **)&util);
248 if (ret)
249 return log_msg_ret("util", -ENOTSUPP);
250 ret = boot->locate_protocol(&efi_pathtext_guid, NULL, (void **)&text);
251 if (ret)
252 return log_msg_ret("text", -ENOTSUPP);
253
254 for (i = 0; i < num_handles; i++) {
255 struct udevice *dev;
256 const u16 *name;
257 bool is_part;
258 int len;
259
260 ret = boot->handle_protocol(handle[i], &efi_devpath_guid,
261 (void **)&path);
262 if (ret) {
263 log_warning("- devpath %d failed (ret=%d)\n", i, ret);
264 continue;
265 }
266
267 ret = boot->handle_protocol(handle[i], &efi_blkio_guid,
268 (void **)&blkio);
269 if (ret) {
270 log_warning("- blkio %d failed (ret=%d)\n", i, ret);
271 continue;
272 }
273
274 name = text->convert_device_path_to_text(path, true, false);
275 is_part = devpath_is_partition(path);
276
277 if (!is_part) {
278 len = util->get_device_path_size(path);
279 ret = efi_bind_block(handle[i], blkio, path, len, &dev);
280 if (ret) {
281 log_warning("- blkio bind %d failed (ret=%d)\n",
282 i, ret);
283 continue;
284 }
285 } else {
286 dev = NULL;
287 }
288
289 /*
290 * Show the device name if we created one. Otherwise indicate
291 * that it is a partition.
292 */
293 printf("%2d: %-12s %ls\n", i, dev ? dev->name : "<partition>",
294 name);
295 }
296 boot->free_pool(handle);
297
298 return 0;
299}
300
301/**
302 * dm_scan_other() - Scan for UEFI devices that should be available to U-Boot
303 *
304 * This sets up block devices within U-Boot for those found in UEFI. With this,
305 * U-Boot can access those devices
306 *
307 * @pre_reloc_only: true to only bind pre-relocation devices (ignored)
308 * Returns: 0 on success, -ve on error
309 */
310int dm_scan_other(bool pre_reloc_only)
311{
312 if (gd->flags & GD_FLG_RELOC) {
313 int ret;
314
315 ret = setup_block();
316 if (ret)
317 return ret;
318 }
319
320 return 0;
321}
322
323/**
Simon Glass9539e692015-07-31 09:31:36 -0600324 * efi_main() - Start an EFI image
325 *
326 * This function is called by our EFI start-up code. It handles running
327 * U-Boot. If it returns, EFI will continue. Another way to get back to EFI
328 * is via reset_cpu().
329 */
Ivan Gorinov41236622018-06-13 17:27:39 -0700330efi_status_t EFIAPI efi_main(efi_handle_t image,
331 struct efi_system_table *sys_table)
Simon Glass9539e692015-07-31 09:31:36 -0600332{
333 struct efi_priv local_priv, *priv = &local_priv;
334 efi_status_t ret;
335
336 /* Set up access to EFI data structures */
Simon Glassa008d682021-12-29 11:57:47 -0700337 ret = efi_init(priv, "App", image, sys_table);
338 if (ret) {
339 printf("Failed to set up U-Boot: err=%lx\n", ret);
340 return ret;
341 }
Simon Glassb13771f2021-12-29 11:57:45 -0700342 efi_set_priv(priv);
Simon Glass9539e692015-07-31 09:31:36 -0600343
344 /*
345 * Set up the EFI debug UART so that printf() works. This is
346 * implemented in the EFI serial driver, serial_efi.c. The application
347 * can use printf() freely.
348 */
349 debug_uart_init();
350
351 ret = setup_memory(priv);
352 if (ret) {
353 printf("Failed to set up memory: ret=%lx\n", ret);
354 return ret;
355 }
356
Simon Glass029f48e2022-01-04 03:51:10 -0700357 /*
358 * We could store the EFI memory map here, but it changes all the time,
359 * so this is only useful for debugging.
360 *
361 * ret = efi_store_memory_map(priv);
362 * if (ret)
363 * return ret;
364 */
365
Simon Glass9539e692015-07-31 09:31:36 -0600366 printf("starting\n");
367
368 board_init_f(GD_FLG_SKIP_RELOC);
369 board_init_r(NULL, 0);
370 free_memory(priv);
371
372 return EFI_SUCCESS;
373}
374
Bin Meng268bc9e2018-07-19 03:07:29 -0700375static void efi_exit(void)
Simon Glass9539e692015-07-31 09:31:36 -0600376{
Simon Glassb13771f2021-12-29 11:57:45 -0700377 struct efi_priv *priv = efi_get_priv();
Simon Glass9539e692015-07-31 09:31:36 -0600378
379 free_memory(priv);
380 printf("U-Boot EFI exiting\n");
381 priv->boot->exit(priv->parent_image, EFI_SUCCESS, 0, NULL);
382}
Bin Meng268bc9e2018-07-19 03:07:29 -0700383
384static int efi_sysreset_request(struct udevice *dev, enum sysreset_t type)
385{
386 efi_exit();
387
388 return -EINPROGRESS;
389}
390
391static const struct udevice_id efi_sysreset_ids[] = {
392 { .compatible = "efi,reset" },
393 { }
394};
395
396static struct sysreset_ops efi_sysreset_ops = {
397 .request = efi_sysreset_request,
398};
399
400U_BOOT_DRIVER(efi_sysreset) = {
401 .name = "efi-sysreset",
402 .id = UCLASS_SYSRESET,
403 .of_match = efi_sysreset_ids,
404 .ops = &efi_sysreset_ops,
Bin Meng268bc9e2018-07-19 03:07:29 -0700405};