blob: c5e4192fe06ca78814743bd213433c3397b4bed7 [file] [log] [blame]
Simon Glassa8ff44d2023-12-19 07:27:19 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * EFI-app board implementation
4 *
5 * Copyright 2023 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 */
8
9#include <dm.h>
10#include <efi.h>
11#include <efi_api.h>
12#include <errno.h>
13#include <malloc.h>
14#include <asm/global_data.h>
15#include <dm/device-internal.h>
16#include <dm/root.h>
17#include <linux/types.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
21/**
22 * efi_bind_block() - bind a new block device to an EFI device
23 *
24 * Binds a new top-level EFI_MEDIA device as well as a child block device so
25 * that the block device can be accessed in U-Boot.
26 *
27 * The device can then be accessed using 'part list efi 0', 'fat ls efi 0:1',
28 * for example, just like any other interface type.
29 *
30 * @handle: handle of the controller on which this driver is installed
31 * @blkio: block io protocol proxied by this driver
32 * @device_path: EFI device path structure for this
33 * @len: Length of @device_path in bytes
34 * @devp: Returns the bound device
35 * Return: 0 if OK, -ve on error
36 */
37int efi_bind_block(efi_handle_t handle, struct efi_block_io *blkio,
38 struct efi_device_path *device_path, int len,
39 struct udevice **devp)
40{
41 struct efi_media_plat plat;
42 struct udevice *dev;
43 char name[18];
44 int ret;
45
46 plat.handle = handle;
47 plat.blkio = blkio;
48 plat.device_path = malloc(device_path->length);
49 if (!plat.device_path)
50 return log_msg_ret("path", -ENOMEM);
51 memcpy(plat.device_path, device_path, device_path->length);
52 ret = device_bind(dm_root(), DM_DRIVER_GET(efi_media), "efi_media",
53 &plat, ofnode_null(), &dev);
54 if (ret)
55 return log_msg_ret("bind", ret);
56
57 snprintf(name, sizeof(name), "efi_media_%x", dev_seq(dev));
58 device_set_name(dev, name);
59 *devp = dev;
60
61 return 0;
62}
63
64/**
65 * devpath_is_partition() - Figure out if a device path is a partition
66 *
67 * Checks if a device path refers to a partition on some media device. This
68 * works by checking for a valid partition number in a hard-driver media device
69 * as the final component of the device path.
70 *
71 * @path: device path
72 * Return: true if a partition, false if not
73 * (e.g. it might be media which contains partitions)
74 */
75static bool devpath_is_partition(const struct efi_device_path *path)
76{
77 const struct efi_device_path *p;
78 bool was_part = false;
79
80 for (p = path; p->type != DEVICE_PATH_TYPE_END;
81 p = (void *)p + p->length) {
82 was_part = false;
83 if (p->type == DEVICE_PATH_TYPE_MEDIA_DEVICE &&
84 p->sub_type == DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH) {
85 struct efi_device_path_hard_drive_path *hd =
86 (void *)path;
87
88 if (hd->partition_number)
89 was_part = true;
90 }
91 }
92
93 return was_part;
94}
95
96/**
97 * setup_block() - Find all block devices and setup EFI devices for them
98 *
99 * Partitions are ignored, since U-Boot has partition handling. Errors with
100 * particular devices produce a warning but execution continues to try to
101 * find others.
102 *
103 * Return: 0 if found, -ENOSYS if there is no boot-services table, -ENOTSUPP
104 * if a required protocol is not supported
105 */
106static int setup_block(void)
107{
108 efi_guid_t efi_blkio_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
109 efi_guid_t efi_devpath_guid = EFI_DEVICE_PATH_PROTOCOL_GUID;
110 efi_guid_t efi_pathutil_guid = EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID;
111 efi_guid_t efi_pathtext_guid = EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
112 struct efi_boot_services *boot = efi_get_boot();
113 struct efi_device_path_utilities_protocol *util;
114 struct efi_device_path_to_text_protocol *text;
115 struct efi_device_path *path;
116 struct efi_block_io *blkio;
117 efi_uintn_t num_handles;
118 efi_handle_t *handle;
119 int ret, i;
120
121 if (!boot)
122 return log_msg_ret("sys", -ENOSYS);
123
124 /* Find all devices which support the block I/O protocol */
125 ret = boot->locate_handle_buffer(BY_PROTOCOL, &efi_blkio_guid, NULL,
126 &num_handles, &handle);
127 if (ret)
128 return log_msg_ret("loc", -ENOTSUPP);
129 log_debug("Found %d handles:\n", (int)num_handles);
130
131 /* We need to look up the path size and convert it to text */
132 ret = boot->locate_protocol(&efi_pathutil_guid, NULL, (void **)&util);
133 if (ret)
134 return log_msg_ret("util", -ENOTSUPP);
135 ret = boot->locate_protocol(&efi_pathtext_guid, NULL, (void **)&text);
136 if (ret)
137 return log_msg_ret("text", -ENOTSUPP);
138
139 for (i = 0; i < num_handles; i++) {
140 struct udevice *dev;
141 const u16 *name;
142 bool is_part;
143 int len;
144
145 ret = boot->handle_protocol(handle[i], &efi_devpath_guid,
146 (void **)&path);
147 if (ret) {
148 log_warning("- devpath %d failed (ret=%d)\n", i, ret);
149 continue;
150 }
151
152 ret = boot->handle_protocol(handle[i], &efi_blkio_guid,
153 (void **)&blkio);
154 if (ret) {
155 log_warning("- blkio %d failed (ret=%d)\n", i, ret);
156 continue;
157 }
158
159 name = text->convert_device_path_to_text(path, true, false);
160 is_part = devpath_is_partition(path);
161
162 if (!is_part) {
163 len = util->get_device_path_size(path);
164 ret = efi_bind_block(handle[i], blkio, path, len, &dev);
165 if (ret) {
166 log_warning("- blkio bind %d failed (ret=%d)\n",
167 i, ret);
168 continue;
169 }
170 } else {
171 dev = NULL;
172 }
173
174 /*
175 * Show the device name if we created one. Otherwise indicate
176 * that it is a partition.
177 */
178 printf("%2d: %-12s %ls\n", i, dev ? dev->name : "<partition>",
179 name);
180 }
181 boot->free_pool(handle);
182
183 return 0;
184}
185
186/**
187 * board_early_init_r() - Scan for UEFI devices that should be available
188 *
189 * This sets up block devices within U-Boot for those found in UEFI. With this,
190 * U-Boot can access those devices
191 *
192 * Returns: 0 on success, -ve on error
193 */
194int board_early_init_r(void)
195{
196 if (gd->flags & GD_FLG_RELOC) {
197 int ret;
198
199 ret = setup_block();
200 if (ret)
201 return ret;
202 }
203
204 return 0;
205}