blob: 119db6602cfb8b274dc3b72b6d0ee959768ecf22 [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
Simon Glassafb02152019-12-28 10:45:01 -070011#include <cpu_func.h>
Simon Glass9539e692015-07-31 09:31:36 -060012#include <debug_uart.h>
Bin Meng268bc9e2018-07-19 03:07:29 -070013#include <dm.h>
Simon Glassf1b35bd2023-11-12 13:55:09 -070014#include <efi.h>
15#include <efi_api.h>
Simon Glass9539e692015-07-31 09:31:36 -060016#include <errno.h>
Simon Glass97589732020-05-10 11:40:02 -060017#include <init.h>
Simon Glass9bc15642020-02-03 07:36:16 -070018#include <malloc.h>
Simon Glassf1b35bd2023-11-12 13:55:09 -070019#include <sysreset.h>
20#include <uuid.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060021#include <asm/global_data.h>
Simon Glass9539e692015-07-31 09:31:36 -060022#include <linux/err.h>
23#include <linux/types.h>
Simon Glassf1b35bd2023-11-12 13:55:09 -070024#include <asm/global_data.h>
Simon Glass2d4b33e2021-12-29 11:57:36 -070025#include <dm/device-internal.h>
26#include <dm/lists.h>
27#include <dm/root.h>
Simon Glassf1b35bd2023-11-12 13:55:09 -070028#include <mapmem.h>
Simon Glass9539e692015-07-31 09:31:36 -060029
30DECLARE_GLOBAL_DATA_PTR;
31
Simon Glass0a3e7b32021-11-03 21:09:09 -060032int efi_info_get(enum efi_entry_t type, void **datap, int *sizep)
33{
34 return -ENOSYS;
35}
36
Simon Glassba042532022-01-04 03:51:12 -070037int efi_get_mmap(struct efi_mem_desc **descp, int *sizep, uint *keyp,
38 int *desc_sizep, uint *versionp)
39{
40 struct efi_priv *priv = efi_get_priv();
41 struct efi_boot_services *boot = priv->sys_table->boottime;
42 efi_uintn_t size, desc_size, key;
43 struct efi_mem_desc *desc;
44 efi_status_t ret;
45 u32 version;
46
47 /* Get the memory map so we can switch off EFI */
48 size = 0;
49 ret = boot->get_memory_map(&size, NULL, &key, &desc_size, &version);
50 if (ret != EFI_BUFFER_TOO_SMALL)
51 return log_msg_ret("get", -ENOMEM);
52
53 desc = malloc(size);
54 if (!desc)
55 return log_msg_ret("mem", -ENOMEM);
56
57 ret = boot->get_memory_map(&size, desc, &key, &desc_size, &version);
58 if (ret)
59 return log_msg_ret("get", -EINVAL);
60
61 *descp = desc;
62 *sizep = size;
63 *desc_sizep = desc_size;
64 *versionp = version;
65 *keyp = key;
66
67 return 0;
68}
69
Simon Glass2d4b33e2021-12-29 11:57:36 -070070/**
71 * efi_bind_block() - bind a new block device to an EFI device
72 *
73 * Binds a new top-level EFI_MEDIA device as well as a child block device so
74 * that the block device can be accessed in U-Boot.
75 *
76 * The device can then be accessed using 'part list efi 0', 'fat ls efi 0:1',
77 * for example, just like any other interface type.
78 *
79 * @handle: handle of the controller on which this driver is installed
80 * @blkio: block io protocol proxied by this driver
81 * @device_path: EFI device path structure for this
82 * @len: Length of @device_path in bytes
83 * @devp: Returns the bound device
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010084 * Return: 0 if OK, -ve on error
Simon Glass2d4b33e2021-12-29 11:57:36 -070085 */
86int efi_bind_block(efi_handle_t handle, struct efi_block_io *blkio,
87 struct efi_device_path *device_path, int len,
88 struct udevice **devp)
89{
90 struct efi_media_plat plat;
91 struct udevice *dev;
92 char name[18];
93 int ret;
94
95 plat.handle = handle;
96 plat.blkio = blkio;
97 plat.device_path = malloc(device_path->length);
98 if (!plat.device_path)
99 return log_msg_ret("path", -ENOMEM);
100 memcpy(plat.device_path, device_path, device_path->length);
101 ret = device_bind(dm_root(), DM_DRIVER_GET(efi_media), "efi_media",
102 &plat, ofnode_null(), &dev);
103 if (ret)
104 return log_msg_ret("bind", ret);
105
106 snprintf(name, sizeof(name), "efi_media_%x", dev_seq(dev));
107 device_set_name(dev, name);
108 *devp = dev;
109
110 return 0;
111}
112
Simon Glass9539e692015-07-31 09:31:36 -0600113static efi_status_t setup_memory(struct efi_priv *priv)
114{
115 struct efi_boot_services *boot = priv->boot;
116 efi_physical_addr_t addr;
117 efi_status_t ret;
118 int pages;
119
120 /*
121 * Use global_data_ptr instead of gd since it is an assignment. There
122 * are very few assignments to global_data in U-Boot and this makes
123 * it easier to find them.
124 */
125 global_data_ptr = efi_malloc(priv, sizeof(struct global_data), &ret);
126 if (!global_data_ptr)
127 return ret;
128 memset(gd, '\0', sizeof(*gd));
129
Andy Yan1fa20e4d2017-07-24 17:43:34 +0800130 gd->malloc_base = (ulong)efi_malloc(priv, CONFIG_VAL(SYS_MALLOC_F_LEN),
Simon Glass9539e692015-07-31 09:31:36 -0600131 &ret);
132 if (!gd->malloc_base)
133 return ret;
134 pages = CONFIG_EFI_RAM_SIZE >> 12;
135
136 /*
137 * Don't allocate any memory above 4GB. U-Boot is a 32-bit application
138 * so we want it to load below 4GB.
139 */
140 addr = 1ULL << 32;
141 ret = boot->allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
142 priv->image_data_type, pages, &addr);
143 if (ret) {
Simon Glass3a4e8a92021-12-29 11:57:49 -0700144 log_info("(using pool %lx) ", ret);
Simon Glass9539e692015-07-31 09:31:36 -0600145 priv->ram_base = (ulong)efi_malloc(priv, CONFIG_EFI_RAM_SIZE,
146 &ret);
147 if (!priv->ram_base)
148 return ret;
149 priv->use_pool_for_malloc = true;
150 } else {
Simon Glass3a4e8a92021-12-29 11:57:49 -0700151 log_info("(using allocated RAM address %lx) ", (ulong)addr);
Simon Glass9539e692015-07-31 09:31:36 -0600152 priv->ram_base = addr;
153 }
154 gd->ram_size = pages << 12;
155
156 return 0;
157}
158
Simon Glass2d4b33e2021-12-29 11:57:36 -0700159/**
160 * free_memory() - Free memory used by the U-Boot app
161 *
162 * This frees memory allocated in setup_memory(), in preparation for returning
163 * to UEFI. It also zeroes the global_data pointer.
164 *
165 * @priv: Private EFI data
166 */
Simon Glass9539e692015-07-31 09:31:36 -0600167static void free_memory(struct efi_priv *priv)
168{
169 struct efi_boot_services *boot = priv->boot;
170
171 if (priv->use_pool_for_malloc)
172 efi_free(priv, (void *)priv->ram_base);
173 else
174 boot->free_pages(priv->ram_base, gd->ram_size >> 12);
175
176 efi_free(priv, (void *)gd->malloc_base);
177 efi_free(priv, gd);
178 global_data_ptr = NULL;
179}
180
181/**
Simon Glass2d4b33e2021-12-29 11:57:36 -0700182 * devpath_is_partition() - Figure out if a device path is a partition
183 *
184 * Checks if a device path refers to a partition on some media device. This
185 * works by checking for a valid partition number in a hard-driver media device
186 * as the final component of the device path.
187 *
188 * @path: device path
189 * Return: true if a partition, false if not
190 * (e.g. it might be media which contains partitions)
191 */
192static bool devpath_is_partition(const struct efi_device_path *path)
193{
194 const struct efi_device_path *p;
Heinrich Schuchardte353ba12022-04-25 23:21:20 +0200195 bool was_part = false;
Simon Glass2d4b33e2021-12-29 11:57:36 -0700196
197 for (p = path; p->type != DEVICE_PATH_TYPE_END;
198 p = (void *)p + p->length) {
199 was_part = false;
200 if (p->type == DEVICE_PATH_TYPE_MEDIA_DEVICE &&
201 p->sub_type == DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH) {
202 struct efi_device_path_hard_drive_path *hd =
203 (void *)path;
204
205 if (hd->partition_number)
206 was_part = true;
207 }
208 }
209
210 return was_part;
211}
212
213/**
214 * setup_block() - Find all block devices and setup EFI devices for them
215 *
216 * Partitions are ignored, since U-Boot has partition handling. Errors with
217 * particular devices produce a warning but execution continues to try to
218 * find others.
219 *
220 * Return: 0 if found, -ENOSYS if there is no boot-services table, -ENOTSUPP
221 * if a required protocol is not supported
222 */
223static int setup_block(void)
224{
225 efi_guid_t efi_blkio_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
226 efi_guid_t efi_devpath_guid = EFI_DEVICE_PATH_PROTOCOL_GUID;
227 efi_guid_t efi_pathutil_guid = EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID;
228 efi_guid_t efi_pathtext_guid = EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
229 struct efi_boot_services *boot = efi_get_boot();
230 struct efi_device_path_utilities_protocol *util;
231 struct efi_device_path_to_text_protocol *text;
232 struct efi_device_path *path;
233 struct efi_block_io *blkio;
234 efi_uintn_t num_handles;
235 efi_handle_t *handle;
236 int ret, i;
237
238 if (!boot)
239 return log_msg_ret("sys", -ENOSYS);
240
241 /* Find all devices which support the block I/O protocol */
242 ret = boot->locate_handle_buffer(BY_PROTOCOL, &efi_blkio_guid, NULL,
243 &num_handles, &handle);
244 if (ret)
245 return log_msg_ret("loc", -ENOTSUPP);
246 log_debug("Found %d handles:\n", (int)num_handles);
247
248 /* We need to look up the path size and convert it to text */
249 ret = boot->locate_protocol(&efi_pathutil_guid, NULL, (void **)&util);
250 if (ret)
251 return log_msg_ret("util", -ENOTSUPP);
252 ret = boot->locate_protocol(&efi_pathtext_guid, NULL, (void **)&text);
253 if (ret)
254 return log_msg_ret("text", -ENOTSUPP);
255
256 for (i = 0; i < num_handles; i++) {
257 struct udevice *dev;
258 const u16 *name;
259 bool is_part;
260 int len;
261
262 ret = boot->handle_protocol(handle[i], &efi_devpath_guid,
263 (void **)&path);
264 if (ret) {
265 log_warning("- devpath %d failed (ret=%d)\n", i, ret);
266 continue;
267 }
268
269 ret = boot->handle_protocol(handle[i], &efi_blkio_guid,
270 (void **)&blkio);
271 if (ret) {
272 log_warning("- blkio %d failed (ret=%d)\n", i, ret);
273 continue;
274 }
275
276 name = text->convert_device_path_to_text(path, true, false);
277 is_part = devpath_is_partition(path);
278
279 if (!is_part) {
280 len = util->get_device_path_size(path);
281 ret = efi_bind_block(handle[i], blkio, path, len, &dev);
282 if (ret) {
283 log_warning("- blkio bind %d failed (ret=%d)\n",
284 i, ret);
285 continue;
286 }
287 } else {
288 dev = NULL;
289 }
290
291 /*
292 * Show the device name if we created one. Otherwise indicate
293 * that it is a partition.
294 */
295 printf("%2d: %-12s %ls\n", i, dev ? dev->name : "<partition>",
296 name);
297 }
298 boot->free_pool(handle);
299
300 return 0;
301}
302
303/**
304 * dm_scan_other() - Scan for UEFI devices that should be available to U-Boot
305 *
306 * This sets up block devices within U-Boot for those found in UEFI. With this,
307 * U-Boot can access those devices
308 *
309 * @pre_reloc_only: true to only bind pre-relocation devices (ignored)
310 * Returns: 0 on success, -ve on error
311 */
312int dm_scan_other(bool pre_reloc_only)
313{
314 if (gd->flags & GD_FLG_RELOC) {
315 int ret;
316
317 ret = setup_block();
318 if (ret)
319 return ret;
320 }
321
322 return 0;
323}
324
Simon Glassf1b35bd2023-11-12 13:55:09 -0700325static void scan_tables(struct efi_system_table *sys_table)
326{
327 efi_guid_t acpi = EFI_ACPI_TABLE_GUID;
328 uint i;
329
330 for (i = 0; i < sys_table->nr_tables; i++) {
331 struct efi_configuration_table *tab = &sys_table->tables[i];
332
333 if (!memcmp(&tab->guid, &acpi, sizeof(efi_guid_t)))
334 gd_set_acpi_start(map_to_sysmem(tab->table));
335 }
336}
337
Simon Glass2d4b33e2021-12-29 11:57:36 -0700338/**
Simon Glass9539e692015-07-31 09:31:36 -0600339 * efi_main() - Start an EFI image
340 *
341 * This function is called by our EFI start-up code. It handles running
342 * U-Boot. If it returns, EFI will continue. Another way to get back to EFI
343 * is via reset_cpu().
344 */
Ivan Gorinov41236622018-06-13 17:27:39 -0700345efi_status_t EFIAPI efi_main(efi_handle_t image,
346 struct efi_system_table *sys_table)
Simon Glass9539e692015-07-31 09:31:36 -0600347{
348 struct efi_priv local_priv, *priv = &local_priv;
349 efi_status_t ret;
350
351 /* Set up access to EFI data structures */
Simon Glassa008d682021-12-29 11:57:47 -0700352 ret = efi_init(priv, "App", image, sys_table);
353 if (ret) {
354 printf("Failed to set up U-Boot: err=%lx\n", ret);
355 return ret;
356 }
Simon Glassb13771f2021-12-29 11:57:45 -0700357 efi_set_priv(priv);
Simon Glass9539e692015-07-31 09:31:36 -0600358
359 /*
360 * Set up the EFI debug UART so that printf() works. This is
361 * implemented in the EFI serial driver, serial_efi.c. The application
362 * can use printf() freely.
363 */
364 debug_uart_init();
365
366 ret = setup_memory(priv);
367 if (ret) {
368 printf("Failed to set up memory: ret=%lx\n", ret);
369 return ret;
370 }
371
Simon Glassf1b35bd2023-11-12 13:55:09 -0700372 scan_tables(priv->sys_table);
373
Simon Glass029f48e2022-01-04 03:51:10 -0700374 /*
375 * We could store the EFI memory map here, but it changes all the time,
376 * so this is only useful for debugging.
377 *
378 * ret = efi_store_memory_map(priv);
379 * if (ret)
380 * return ret;
381 */
382
Simon Glass9539e692015-07-31 09:31:36 -0600383 printf("starting\n");
384
385 board_init_f(GD_FLG_SKIP_RELOC);
386 board_init_r(NULL, 0);
387 free_memory(priv);
388
389 return EFI_SUCCESS;
390}
391
Bin Meng268bc9e2018-07-19 03:07:29 -0700392static void efi_exit(void)
Simon Glass9539e692015-07-31 09:31:36 -0600393{
Simon Glassb13771f2021-12-29 11:57:45 -0700394 struct efi_priv *priv = efi_get_priv();
Simon Glass9539e692015-07-31 09:31:36 -0600395
396 free_memory(priv);
397 printf("U-Boot EFI exiting\n");
398 priv->boot->exit(priv->parent_image, EFI_SUCCESS, 0, NULL);
399}
Bin Meng268bc9e2018-07-19 03:07:29 -0700400
401static int efi_sysreset_request(struct udevice *dev, enum sysreset_t type)
402{
403 efi_exit();
404
405 return -EINPROGRESS;
406}
407
408static const struct udevice_id efi_sysreset_ids[] = {
409 { .compatible = "efi,reset" },
410 { }
411};
412
413static struct sysreset_ops efi_sysreset_ops = {
414 .request = efi_sysreset_request,
415};
416
417U_BOOT_DRIVER(efi_sysreset) = {
418 .name = "efi-sysreset",
419 .id = UCLASS_SYSRESET,
420 .of_match = efi_sysreset_ids,
421 .ops = &efi_sysreset_ops,
Bin Meng268bc9e2018-07-19 03:07:29 -0700422};