blob: e3abd90275c8200824ff761d5bdebca4ad7792e4 [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Heinrich Schuchardt11206f42018-01-21 19:29:30 +01002/*
3 * EFI block driver
4 *
5 * Copyright (c) 2017 Heinrich Schuchardt
6 *
Heinrich Schuchardt11206f42018-01-21 19:29:30 +01007 * The EFI uclass creates a handle for this driver and installs the
8 * driver binding protocol on it.
9 *
10 * The EFI block driver binds to controllers implementing the block io
11 * protocol.
12 *
13 * When the bind function of the EFI block driver is called it creates a
14 * new U-Boot block device. It installs child handles for all partitions and
15 * installs the simple file protocol on these.
16 *
17 * The read and write functions of the EFI block driver delegate calls to the
18 * controller that it is bound to.
19 *
20 * A usage example is as following:
21 *
22 * U-Boot loads the iPXE snp.efi executable. iPXE connects an iSCSI drive and
23 * exposes a handle with the block IO protocol. It calls ConnectController.
24 *
25 * Now the EFI block driver installs the partitions with the simple file
26 * protocol.
27 *
28 * iPXE uses the simple file protocol to load Grub or the Linux Kernel.
29 */
30
Simon Glass655306c2020-05-10 11:39:58 -060031#include <common.h>
32#include <blk.h>
Simon Glassf6977c12020-07-19 10:15:43 -060033#include <dm.h>
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010034#include <efi_driver.h>
Simon Glass9bc15642020-02-03 07:36:16 -070035#include <malloc.h>
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010036#include <dm/device-internal.h>
37#include <dm/root.h>
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +090038#include <dm/tag.h>
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010039
Heinrich Schuchardt35679a92022-10-04 18:53:34 +020040/**
41 * struct efi_blk_plat - attributes of a block device
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010042 *
Heinrich Schuchardt35679a92022-10-04 18:53:34 +020043 * @handle: handle of the controller on which this driver is installed
44 * @io: block io protocol proxied by this driver
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010045 */
Simon Glassb75b15b2020-12-03 16:55:23 -070046struct efi_blk_plat {
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010047 efi_handle_t handle;
48 struct efi_block_io *io;
49};
50
Heinrich Schuchardt6492dad2019-07-14 11:52:33 +020051/**
Heinrich Schuchardt35679a92022-10-04 18:53:34 +020052 * efi_bl_read() - read from block device
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010053 *
Heinrich Schuchardt6492dad2019-07-14 11:52:33 +020054 * @dev: device
55 * @blknr: first block to be read
56 * @blkcnt: number of blocks to read
57 * @buffer: output buffer
58 * Return: number of blocks transferred
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010059 */
60static ulong efi_bl_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
61 void *buffer)
62{
Simon Glassb75b15b2020-12-03 16:55:23 -070063 struct efi_blk_plat *plat = dev_get_plat(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -070064 struct efi_block_io *io = plat->io;
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010065 efi_status_t ret;
66
67 EFI_PRINT("%s: read '%s', from block " LBAFU ", " LBAFU " blocks\n",
68 __func__, dev->name, blknr, blkcnt);
69 ret = EFI_CALL(io->read_blocks(
70 io, io->media->media_id, (u64)blknr,
71 (efi_uintn_t)blkcnt *
72 (efi_uintn_t)io->media->block_size, buffer));
73 EFI_PRINT("%s: r = %u\n", __func__,
74 (unsigned int)(ret & ~EFI_ERROR_MASK));
75 if (ret != EFI_SUCCESS)
76 return 0;
77 return blkcnt;
78}
79
Heinrich Schuchardt6492dad2019-07-14 11:52:33 +020080/**
Heinrich Schuchardt35679a92022-10-04 18:53:34 +020081 * efi_bl_write() - write to block device
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010082 *
Heinrich Schuchardt6492dad2019-07-14 11:52:33 +020083 * @dev: device
84 * @blknr: first block to be write
85 * @blkcnt: number of blocks to write
86 * @buffer: input buffer
87 * Return: number of blocks transferred
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010088 */
89static ulong efi_bl_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
90 const void *buffer)
91{
Simon Glassb75b15b2020-12-03 16:55:23 -070092 struct efi_blk_plat *plat = dev_get_plat(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -070093 struct efi_block_io *io = plat->io;
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010094 efi_status_t ret;
95
96 EFI_PRINT("%s: write '%s', from block " LBAFU ", " LBAFU " blocks\n",
97 __func__, dev->name, blknr, blkcnt);
98 ret = EFI_CALL(io->write_blocks(
99 io, io->media->media_id, (u64)blknr,
100 (efi_uintn_t)blkcnt *
101 (efi_uintn_t)io->media->block_size,
102 (void *)buffer));
103 EFI_PRINT("%s: r = %u\n", __func__,
104 (unsigned int)(ret & ~EFI_ERROR_MASK));
105 if (ret != EFI_SUCCESS)
106 return 0;
107 return blkcnt;
108}
109
Heinrich Schuchardt6492dad2019-07-14 11:52:33 +0200110/**
Heinrich Schuchardt35679a92022-10-04 18:53:34 +0200111 * efi_bl_create_block_device() - create a block device for a handle
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100112 *
Heinrich Schuchardt6492dad2019-07-14 11:52:33 +0200113 * @handle: handle
114 * @interface: block io protocol
Heinrich Schuchardt3f712db2022-10-03 10:35:35 +0200115 * Return: status code
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100116 */
Heinrich Schuchardtcc00eb42022-10-04 16:19:30 +0200117static efi_status_t
118efi_bl_create_block_device(efi_handle_t handle, void *interface)
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100119{
Heinrich Schuchardt3f712db2022-10-03 10:35:35 +0200120 struct udevice *bdev = NULL, *parent = dm_root();
121 efi_status_t ret;
122 int devnum;
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100123 char *name;
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100124 struct efi_block_io *io = interface;
Simon Glassb75b15b2020-12-03 16:55:23 -0700125 struct efi_blk_plat *plat;
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100126
Masahisa Kojima676c2742023-07-03 15:08:45 +0900127 devnum = blk_next_free_devnum(UCLASS_EFI_LOADER);
128 if (devnum < 0)
Heinrich Schuchardt3f712db2022-10-03 10:35:35 +0200129 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100130
131 name = calloc(1, 18); /* strlen("efiblk#2147483648") + 1 */
132 if (!name)
Heinrich Schuchardt3f712db2022-10-03 10:35:35 +0200133 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100134 sprintf(name, "efiblk#%d", devnum);
135
136 /* Create driver model udevice for the EFI block io device */
Heinrich Schuchardt3f712db2022-10-03 10:35:35 +0200137 if (blk_create_device(parent, "efi_blk", name, UCLASS_EFI_LOADER,
138 devnum, io->media->block_size,
139 (lbaint_t)io->media->last_block, &bdev)) {
140 ret = EFI_OUT_OF_RESOURCES;
141 free(name);
142 goto err;
143 }
Heinrich Schuchardt25ac13e2018-06-30 07:11:32 +0200144 /* Set the DM_FLAG_NAME_ALLOCED flag to avoid a memory leak */
145 device_set_name_alloced(bdev);
Bin Meng91938632018-10-15 02:21:06 -0700146
Simon Glassfa20e932020-12-03 16:55:20 -0700147 plat = dev_get_plat(bdev);
Simon Glass71fa5b42020-12-03 16:55:18 -0700148 plat->handle = handle;
149 plat->io = interface;
Bin Meng91938632018-10-15 02:21:06 -0700150
Heinrich Schuchardt3f712db2022-10-03 10:35:35 +0200151 if (efi_link_dev(handle, bdev)) {
152 ret = EFI_OUT_OF_RESOURCES;
153 goto err;
154 }
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900155
Heinrich Schuchardt3f712db2022-10-03 10:35:35 +0200156 if (device_probe(bdev)) {
157 ret = EFI_DEVICE_ERROR;
158 goto err;
159 }
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100160 EFI_PRINT("%s: block device '%s' created\n", __func__, bdev->name);
161
Heinrich Schuchardt3f712db2022-10-03 10:35:35 +0200162 return EFI_SUCCESS;
163
164err:
165 efi_unlink_dev(handle);
166 if (bdev)
167 device_unbind(bdev);
168
169 return ret;
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100170}
171
Heinrich Schuchardtcc00eb42022-10-04 16:19:30 +0200172/**
173 * efi_bl_bind() - bind to a block io protocol
174 *
Heinrich Schuchardtff1d50b2022-10-04 19:12:59 +0200175 * @this: driver binding protocol
Heinrich Schuchardtcc00eb42022-10-04 16:19:30 +0200176 * @handle: handle
177 * @interface: block io protocol
178 * Return: status code
179 */
Heinrich Schuchardtff1d50b2022-10-04 19:12:59 +0200180static efi_status_t efi_bl_bind(
181 struct efi_driver_binding_extended_protocol *this,
182 efi_handle_t handle, void *interface)
Heinrich Schuchardtcc00eb42022-10-04 16:19:30 +0200183{
184 efi_status_t ret = EFI_SUCCESS;
185 struct efi_object *obj = efi_search_obj(handle);
186
187 EFI_PRINT("%s: handle %p, interface %p\n", __func__, handle, interface);
188
189 if (!obj || !interface)
190 return EFI_INVALID_PARAMETER;
191
192 if (!handle->dev)
193 ret = efi_bl_create_block_device(handle, interface);
194
195 return ret;
196}
197
Heinrich Schuchardt6c5fd212022-10-05 11:28:47 +0200198/**
199 * efi_bl_init() - initialize block device driver
200 *
201 * @this: extended driver binding protocol
202 */
203static efi_status_t
204efi_bl_init(struct efi_driver_binding_extended_protocol *this)
205{
Heinrich Schuchardtda620cf2022-10-06 07:29:41 +0200206 int ret;
207
208 ret = event_register("efi_disk add", EVT_DM_POST_PROBE,
209 efi_disk_probe, this);
210 if (ret) {
211 log_err("Event registration for efi_disk add failed\n");
212 return EFI_OUT_OF_RESOURCES;
213 }
214
215 ret = event_register("efi_disk del", EVT_DM_PRE_REMOVE,
216 efi_disk_remove, this);
217 if (ret) {
218 log_err("Event registration for efi_disk del failed\n");
219 return EFI_OUT_OF_RESOURCES;
220 }
221
Heinrich Schuchardt6c5fd212022-10-05 11:28:47 +0200222 return EFI_SUCCESS;
223}
224
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100225/* Block device driver operators */
226static const struct blk_ops efi_blk_ops = {
227 .read = efi_bl_read,
228 .write = efi_bl_write,
229};
230
231/* Identify as block device driver */
232U_BOOT_DRIVER(efi_blk) = {
Heinrich Schuchardt35679a92022-10-04 18:53:34 +0200233 .name = "efi_blk",
234 .id = UCLASS_BLK,
235 .ops = &efi_blk_ops,
Simon Glassb75b15b2020-12-03 16:55:23 -0700236 .plat_auto = sizeof(struct efi_blk_plat),
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100237};
238
239/* EFI driver operators */
240static const struct efi_driver_ops driver_ops = {
241 .protocol = &efi_block_io_guid,
242 .child_protocol = &efi_block_io_guid,
Heinrich Schuchardt6c5fd212022-10-05 11:28:47 +0200243 .init = efi_bl_init,
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100244 .bind = efi_bl_bind,
245};
246
247/* Identify as EFI driver */
248U_BOOT_DRIVER(efi_block) = {
249 .name = "EFI block driver",
Simon Glass15c4d672021-12-04 08:56:30 -0700250 .id = UCLASS_EFI_LOADER,
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100251 .ops = &driver_ops,
252};