blob: 214cef943019b032a3cc880dea17c258cbc70570 [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) {
109 printf("(using pool %lx) ", ret);
110 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 {
116 priv->ram_base = addr;
117 }
118 gd->ram_size = pages << 12;
119
120 return 0;
121}
122
Simon Glass613cd0c2021-12-29 11:57:36 -0700123/**
124 * free_memory() - Free memory used by the U-Boot app
125 *
126 * This frees memory allocated in setup_memory(), in preparation for returning
127 * to UEFI. It also zeroes the global_data pointer.
128 *
129 * @priv: Private EFI data
130 */
Simon Glass867a6ac2015-07-31 09:31:36 -0600131static void free_memory(struct efi_priv *priv)
132{
133 struct efi_boot_services *boot = priv->boot;
134
135 if (priv->use_pool_for_malloc)
136 efi_free(priv, (void *)priv->ram_base);
137 else
138 boot->free_pages(priv->ram_base, gd->ram_size >> 12);
139
140 efi_free(priv, (void *)gd->malloc_base);
141 efi_free(priv, gd);
142 global_data_ptr = NULL;
143}
144
145/**
Simon Glass613cd0c2021-12-29 11:57:36 -0700146 * devpath_is_partition() - Figure out if a device path is a partition
147 *
148 * Checks if a device path refers to a partition on some media device. This
149 * works by checking for a valid partition number in a hard-driver media device
150 * as the final component of the device path.
151 *
152 * @path: device path
153 * Return: true if a partition, false if not
154 * (e.g. it might be media which contains partitions)
155 */
156static bool devpath_is_partition(const struct efi_device_path *path)
157{
158 const struct efi_device_path *p;
159 bool was_part;
160
161 for (p = path; p->type != DEVICE_PATH_TYPE_END;
162 p = (void *)p + p->length) {
163 was_part = false;
164 if (p->type == DEVICE_PATH_TYPE_MEDIA_DEVICE &&
165 p->sub_type == DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH) {
166 struct efi_device_path_hard_drive_path *hd =
167 (void *)path;
168
169 if (hd->partition_number)
170 was_part = true;
171 }
172 }
173
174 return was_part;
175}
176
177/**
178 * setup_block() - Find all block devices and setup EFI devices for them
179 *
180 * Partitions are ignored, since U-Boot has partition handling. Errors with
181 * particular devices produce a warning but execution continues to try to
182 * find others.
183 *
184 * Return: 0 if found, -ENOSYS if there is no boot-services table, -ENOTSUPP
185 * if a required protocol is not supported
186 */
187static int setup_block(void)
188{
189 efi_guid_t efi_blkio_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
190 efi_guid_t efi_devpath_guid = EFI_DEVICE_PATH_PROTOCOL_GUID;
191 efi_guid_t efi_pathutil_guid = EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID;
192 efi_guid_t efi_pathtext_guid = EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
193 struct efi_boot_services *boot = efi_get_boot();
194 struct efi_device_path_utilities_protocol *util;
195 struct efi_device_path_to_text_protocol *text;
196 struct efi_device_path *path;
197 struct efi_block_io *blkio;
198 efi_uintn_t num_handles;
199 efi_handle_t *handle;
200 int ret, i;
201
202 if (!boot)
203 return log_msg_ret("sys", -ENOSYS);
204
205 /* Find all devices which support the block I/O protocol */
206 ret = boot->locate_handle_buffer(BY_PROTOCOL, &efi_blkio_guid, NULL,
207 &num_handles, &handle);
208 if (ret)
209 return log_msg_ret("loc", -ENOTSUPP);
210 log_debug("Found %d handles:\n", (int)num_handles);
211
212 /* We need to look up the path size and convert it to text */
213 ret = boot->locate_protocol(&efi_pathutil_guid, NULL, (void **)&util);
214 if (ret)
215 return log_msg_ret("util", -ENOTSUPP);
216 ret = boot->locate_protocol(&efi_pathtext_guid, NULL, (void **)&text);
217 if (ret)
218 return log_msg_ret("text", -ENOTSUPP);
219
220 for (i = 0; i < num_handles; i++) {
221 struct udevice *dev;
222 const u16 *name;
223 bool is_part;
224 int len;
225
226 ret = boot->handle_protocol(handle[i], &efi_devpath_guid,
227 (void **)&path);
228 if (ret) {
229 log_warning("- devpath %d failed (ret=%d)\n", i, ret);
230 continue;
231 }
232
233 ret = boot->handle_protocol(handle[i], &efi_blkio_guid,
234 (void **)&blkio);
235 if (ret) {
236 log_warning("- blkio %d failed (ret=%d)\n", i, ret);
237 continue;
238 }
239
240 name = text->convert_device_path_to_text(path, true, false);
241 is_part = devpath_is_partition(path);
242
243 if (!is_part) {
244 len = util->get_device_path_size(path);
245 ret = efi_bind_block(handle[i], blkio, path, len, &dev);
246 if (ret) {
247 log_warning("- blkio bind %d failed (ret=%d)\n",
248 i, ret);
249 continue;
250 }
251 } else {
252 dev = NULL;
253 }
254
255 /*
256 * Show the device name if we created one. Otherwise indicate
257 * that it is a partition.
258 */
259 printf("%2d: %-12s %ls\n", i, dev ? dev->name : "<partition>",
260 name);
261 }
262 boot->free_pool(handle);
263
264 return 0;
265}
266
267/**
268 * dm_scan_other() - Scan for UEFI devices that should be available to U-Boot
269 *
270 * This sets up block devices within U-Boot for those found in UEFI. With this,
271 * U-Boot can access those devices
272 *
273 * @pre_reloc_only: true to only bind pre-relocation devices (ignored)
274 * Returns: 0 on success, -ve on error
275 */
276int dm_scan_other(bool pre_reloc_only)
277{
278 if (gd->flags & GD_FLG_RELOC) {
279 int ret;
280
281 ret = setup_block();
282 if (ret)
283 return ret;
284 }
285
286 return 0;
287}
288
289/**
Simon Glass867a6ac2015-07-31 09:31:36 -0600290 * efi_main() - Start an EFI image
291 *
292 * This function is called by our EFI start-up code. It handles running
293 * U-Boot. If it returns, EFI will continue. Another way to get back to EFI
294 * is via reset_cpu().
295 */
Ivan Gorinov9f0b0112018-06-13 17:27:39 -0700296efi_status_t EFIAPI efi_main(efi_handle_t image,
297 struct efi_system_table *sys_table)
Simon Glass867a6ac2015-07-31 09:31:36 -0600298{
299 struct efi_priv local_priv, *priv = &local_priv;
300 efi_status_t ret;
301
302 /* Set up access to EFI data structures */
303 efi_init(priv, "App", image, sys_table);
304
Simon Glass2a1cf032021-12-29 11:57:45 -0700305 efi_set_priv(priv);
Simon Glass867a6ac2015-07-31 09:31:36 -0600306
307 /*
308 * Set up the EFI debug UART so that printf() works. This is
309 * implemented in the EFI serial driver, serial_efi.c. The application
310 * can use printf() freely.
311 */
312 debug_uart_init();
313
314 ret = setup_memory(priv);
315 if (ret) {
316 printf("Failed to set up memory: ret=%lx\n", ret);
317 return ret;
318 }
319
320 printf("starting\n");
321
322 board_init_f(GD_FLG_SKIP_RELOC);
323 board_init_r(NULL, 0);
324 free_memory(priv);
325
326 return EFI_SUCCESS;
327}
328
Bin Mengc81a8f52018-07-19 03:07:29 -0700329static void efi_exit(void)
Simon Glass867a6ac2015-07-31 09:31:36 -0600330{
Simon Glass2a1cf032021-12-29 11:57:45 -0700331 struct efi_priv *priv = efi_get_priv();
Simon Glass867a6ac2015-07-31 09:31:36 -0600332
333 free_memory(priv);
334 printf("U-Boot EFI exiting\n");
335 priv->boot->exit(priv->parent_image, EFI_SUCCESS, 0, NULL);
336}
Bin Mengc81a8f52018-07-19 03:07:29 -0700337
338static int efi_sysreset_request(struct udevice *dev, enum sysreset_t type)
339{
340 efi_exit();
341
342 return -EINPROGRESS;
343}
344
345static const struct udevice_id efi_sysreset_ids[] = {
346 { .compatible = "efi,reset" },
347 { }
348};
349
350static struct sysreset_ops efi_sysreset_ops = {
351 .request = efi_sysreset_request,
352};
353
354U_BOOT_DRIVER(efi_sysreset) = {
355 .name = "efi-sysreset",
356 .id = UCLASS_SYSRESET,
357 .of_match = efi_sysreset_ids,
358 .ops = &efi_sysreset_ops,
Bin Mengc81a8f52018-07-19 03:07:29 -0700359};