blob: d60f2f6c28f80e10d068cc25eef1ebac50fa4a45 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass867a6ac2015-07-31 09:31:36 -06002/*
3 * Copyright (c) 2015 Google, Inc
4 *
Simon Glass867a6ac2015-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 Glass9a3b4ce2019-12-28 10:45:01 -070012#include <cpu_func.h>
Simon Glass867a6ac2015-07-31 09:31:36 -060013#include <debug_uart.h>
Bin Mengc81a8f52018-07-19 03:07:29 -070014#include <dm.h>
Simon Glass867a6ac2015-07-31 09:31:36 -060015#include <errno.h>
Simon Glass691d7192020-05-10 11:40:02 -060016#include <init.h>
Simon Glass336d4612020-02-03 07:36:16 -070017#include <malloc.h>
Simon Glass401d1c42020-10-30 21:38:53 -060018#include <asm/global_data.h>
Simon Glass867a6ac2015-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 Mengc81a8f52018-07-19 03:07:29 -070023#include <sysreset.h>
Simon Glass613cd0c2021-12-29 11:57:36 -070024#include <dm/device-internal.h>
25#include <dm/lists.h>
26#include <dm/root.h>
Simon Glass867a6ac2015-07-31 09:31:36 -060027
28DECLARE_GLOBAL_DATA_PTR;
29
Simon Glassf8445732021-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 Glass613cd0c2021-12-29 11:57:36 -070035/**
36 * efi_bind_block() - bind a new block device to an EFI device
37 *
38 * Binds a new top-level EFI_MEDIA device as well as a child block device so
39 * that the block device can be accessed in U-Boot.
40 *
41 * The device can then be accessed using 'part list efi 0', 'fat ls efi 0:1',
42 * for example, just like any other interface type.
43 *
44 * @handle: handle of the controller on which this driver is installed
45 * @blkio: block io protocol proxied by this driver
46 * @device_path: EFI device path structure for this
47 * @len: Length of @device_path in bytes
48 * @devp: Returns the bound device
49 * @return 0 if OK, -ve on error
50 */
51int efi_bind_block(efi_handle_t handle, struct efi_block_io *blkio,
52 struct efi_device_path *device_path, int len,
53 struct udevice **devp)
54{
55 struct efi_media_plat plat;
56 struct udevice *dev;
57 char name[18];
58 int ret;
59
60 plat.handle = handle;
61 plat.blkio = blkio;
62 plat.device_path = malloc(device_path->length);
63 if (!plat.device_path)
64 return log_msg_ret("path", -ENOMEM);
65 memcpy(plat.device_path, device_path, device_path->length);
66 ret = device_bind(dm_root(), DM_DRIVER_GET(efi_media), "efi_media",
67 &plat, ofnode_null(), &dev);
68 if (ret)
69 return log_msg_ret("bind", ret);
70
71 snprintf(name, sizeof(name), "efi_media_%x", dev_seq(dev));
72 device_set_name(dev, name);
73 *devp = dev;
74
75 return 0;
76}
77
Simon Glass867a6ac2015-07-31 09:31:36 -060078static efi_status_t setup_memory(struct efi_priv *priv)
79{
80 struct efi_boot_services *boot = priv->boot;
81 efi_physical_addr_t addr;
82 efi_status_t ret;
83 int pages;
84
85 /*
86 * Use global_data_ptr instead of gd since it is an assignment. There
87 * are very few assignments to global_data in U-Boot and this makes
88 * it easier to find them.
89 */
90 global_data_ptr = efi_malloc(priv, sizeof(struct global_data), &ret);
91 if (!global_data_ptr)
92 return ret;
93 memset(gd, '\0', sizeof(*gd));
94
Andy Yanf1896c42017-07-24 17:43:34 +080095 gd->malloc_base = (ulong)efi_malloc(priv, CONFIG_VAL(SYS_MALLOC_F_LEN),
Simon Glass867a6ac2015-07-31 09:31:36 -060096 &ret);
97 if (!gd->malloc_base)
98 return ret;
99 pages = CONFIG_EFI_RAM_SIZE >> 12;
100
101 /*
102 * Don't allocate any memory above 4GB. U-Boot is a 32-bit application
103 * so we want it to load below 4GB.
104 */
105 addr = 1ULL << 32;
106 ret = boot->allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
107 priv->image_data_type, pages, &addr);
108 if (ret) {
Simon Glass62725e62021-12-29 11:57:49 -0700109 log_info("(using pool %lx) ", ret);
Simon Glass867a6ac2015-07-31 09:31:36 -0600110 priv->ram_base = (ulong)efi_malloc(priv, CONFIG_EFI_RAM_SIZE,
111 &ret);
112 if (!priv->ram_base)
113 return ret;
114 priv->use_pool_for_malloc = true;
115 } else {
Simon Glass62725e62021-12-29 11:57:49 -0700116 log_info("(using allocated RAM address %lx) ", (ulong)addr);
Simon Glass867a6ac2015-07-31 09:31:36 -0600117 priv->ram_base = addr;
118 }
119 gd->ram_size = pages << 12;
120
121 return 0;
122}
123
Simon Glass613cd0c2021-12-29 11:57:36 -0700124/**
125 * free_memory() - Free memory used by the U-Boot app
126 *
127 * This frees memory allocated in setup_memory(), in preparation for returning
128 * to UEFI. It also zeroes the global_data pointer.
129 *
130 * @priv: Private EFI data
131 */
Simon Glass867a6ac2015-07-31 09:31:36 -0600132static void free_memory(struct efi_priv *priv)
133{
134 struct efi_boot_services *boot = priv->boot;
135
136 if (priv->use_pool_for_malloc)
137 efi_free(priv, (void *)priv->ram_base);
138 else
139 boot->free_pages(priv->ram_base, gd->ram_size >> 12);
140
141 efi_free(priv, (void *)gd->malloc_base);
142 efi_free(priv, gd);
143 global_data_ptr = NULL;
144}
145
146/**
Simon Glass613cd0c2021-12-29 11:57:36 -0700147 * devpath_is_partition() - Figure out if a device path is a partition
148 *
149 * Checks if a device path refers to a partition on some media device. This
150 * works by checking for a valid partition number in a hard-driver media device
151 * as the final component of the device path.
152 *
153 * @path: device path
154 * Return: true if a partition, false if not
155 * (e.g. it might be media which contains partitions)
156 */
157static bool devpath_is_partition(const struct efi_device_path *path)
158{
159 const struct efi_device_path *p;
160 bool was_part;
161
162 for (p = path; p->type != DEVICE_PATH_TYPE_END;
163 p = (void *)p + p->length) {
164 was_part = false;
165 if (p->type == DEVICE_PATH_TYPE_MEDIA_DEVICE &&
166 p->sub_type == DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH) {
167 struct efi_device_path_hard_drive_path *hd =
168 (void *)path;
169
170 if (hd->partition_number)
171 was_part = true;
172 }
173 }
174
175 return was_part;
176}
177
178/**
179 * setup_block() - Find all block devices and setup EFI devices for them
180 *
181 * Partitions are ignored, since U-Boot has partition handling. Errors with
182 * particular devices produce a warning but execution continues to try to
183 * find others.
184 *
185 * Return: 0 if found, -ENOSYS if there is no boot-services table, -ENOTSUPP
186 * if a required protocol is not supported
187 */
188static int setup_block(void)
189{
190 efi_guid_t efi_blkio_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
191 efi_guid_t efi_devpath_guid = EFI_DEVICE_PATH_PROTOCOL_GUID;
192 efi_guid_t efi_pathutil_guid = EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID;
193 efi_guid_t efi_pathtext_guid = EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
194 struct efi_boot_services *boot = efi_get_boot();
195 struct efi_device_path_utilities_protocol *util;
196 struct efi_device_path_to_text_protocol *text;
197 struct efi_device_path *path;
198 struct efi_block_io *blkio;
199 efi_uintn_t num_handles;
200 efi_handle_t *handle;
201 int ret, i;
202
203 if (!boot)
204 return log_msg_ret("sys", -ENOSYS);
205
206 /* Find all devices which support the block I/O protocol */
207 ret = boot->locate_handle_buffer(BY_PROTOCOL, &efi_blkio_guid, NULL,
208 &num_handles, &handle);
209 if (ret)
210 return log_msg_ret("loc", -ENOTSUPP);
211 log_debug("Found %d handles:\n", (int)num_handles);
212
213 /* We need to look up the path size and convert it to text */
214 ret = boot->locate_protocol(&efi_pathutil_guid, NULL, (void **)&util);
215 if (ret)
216 return log_msg_ret("util", -ENOTSUPP);
217 ret = boot->locate_protocol(&efi_pathtext_guid, NULL, (void **)&text);
218 if (ret)
219 return log_msg_ret("text", -ENOTSUPP);
220
221 for (i = 0; i < num_handles; i++) {
222 struct udevice *dev;
223 const u16 *name;
224 bool is_part;
225 int len;
226
227 ret = boot->handle_protocol(handle[i], &efi_devpath_guid,
228 (void **)&path);
229 if (ret) {
230 log_warning("- devpath %d failed (ret=%d)\n", i, ret);
231 continue;
232 }
233
234 ret = boot->handle_protocol(handle[i], &efi_blkio_guid,
235 (void **)&blkio);
236 if (ret) {
237 log_warning("- blkio %d failed (ret=%d)\n", i, ret);
238 continue;
239 }
240
241 name = text->convert_device_path_to_text(path, true, false);
242 is_part = devpath_is_partition(path);
243
244 if (!is_part) {
245 len = util->get_device_path_size(path);
246 ret = efi_bind_block(handle[i], blkio, path, len, &dev);
247 if (ret) {
248 log_warning("- blkio bind %d failed (ret=%d)\n",
249 i, ret);
250 continue;
251 }
252 } else {
253 dev = NULL;
254 }
255
256 /*
257 * Show the device name if we created one. Otherwise indicate
258 * that it is a partition.
259 */
260 printf("%2d: %-12s %ls\n", i, dev ? dev->name : "<partition>",
261 name);
262 }
263 boot->free_pool(handle);
264
265 return 0;
266}
267
268/**
269 * dm_scan_other() - Scan for UEFI devices that should be available to U-Boot
270 *
271 * This sets up block devices within U-Boot for those found in UEFI. With this,
272 * U-Boot can access those devices
273 *
274 * @pre_reloc_only: true to only bind pre-relocation devices (ignored)
275 * Returns: 0 on success, -ve on error
276 */
277int dm_scan_other(bool pre_reloc_only)
278{
279 if (gd->flags & GD_FLG_RELOC) {
280 int ret;
281
282 ret = setup_block();
283 if (ret)
284 return ret;
285 }
286
287 return 0;
288}
289
290/**
Simon Glass867a6ac2015-07-31 09:31:36 -0600291 * efi_main() - Start an EFI image
292 *
293 * This function is called by our EFI start-up code. It handles running
294 * U-Boot. If it returns, EFI will continue. Another way to get back to EFI
295 * is via reset_cpu().
296 */
Ivan Gorinov9f0b0112018-06-13 17:27:39 -0700297efi_status_t EFIAPI efi_main(efi_handle_t image,
298 struct efi_system_table *sys_table)
Simon Glass867a6ac2015-07-31 09:31:36 -0600299{
300 struct efi_priv local_priv, *priv = &local_priv;
301 efi_status_t ret;
302
303 /* Set up access to EFI data structures */
Simon Glassbc53a352021-12-29 11:57:47 -0700304 ret = efi_init(priv, "App", image, sys_table);
305 if (ret) {
306 printf("Failed to set up U-Boot: err=%lx\n", ret);
307 return ret;
308 }
Simon Glass2a1cf032021-12-29 11:57:45 -0700309 efi_set_priv(priv);
Simon Glass867a6ac2015-07-31 09:31:36 -0600310
311 /*
312 * Set up the EFI debug UART so that printf() works. This is
313 * implemented in the EFI serial driver, serial_efi.c. The application
314 * can use printf() freely.
315 */
316 debug_uart_init();
317
318 ret = setup_memory(priv);
319 if (ret) {
320 printf("Failed to set up memory: ret=%lx\n", ret);
321 return ret;
322 }
323
324 printf("starting\n");
325
326 board_init_f(GD_FLG_SKIP_RELOC);
327 board_init_r(NULL, 0);
328 free_memory(priv);
329
330 return EFI_SUCCESS;
331}
332
Bin Mengc81a8f52018-07-19 03:07:29 -0700333static void efi_exit(void)
Simon Glass867a6ac2015-07-31 09:31:36 -0600334{
Simon Glass2a1cf032021-12-29 11:57:45 -0700335 struct efi_priv *priv = efi_get_priv();
Simon Glass867a6ac2015-07-31 09:31:36 -0600336
337 free_memory(priv);
338 printf("U-Boot EFI exiting\n");
339 priv->boot->exit(priv->parent_image, EFI_SUCCESS, 0, NULL);
340}
Bin Mengc81a8f52018-07-19 03:07:29 -0700341
342static int efi_sysreset_request(struct udevice *dev, enum sysreset_t type)
343{
344 efi_exit();
345
346 return -EINPROGRESS;
347}
348
349static const struct udevice_id efi_sysreset_ids[] = {
350 { .compatible = "efi,reset" },
351 { }
352};
353
354static struct sysreset_ops efi_sysreset_ops = {
355 .request = efi_sysreset_request,
356};
357
358U_BOOT_DRIVER(efi_sysreset) = {
359 .name = "efi-sysreset",
360 .id = UCLASS_SYSRESET,
361 .of_match = efi_sysreset_ids,
362 .ops = &efi_sysreset_ops,
Bin Mengc81a8f52018-07-19 03:07:29 -0700363};