blob: d3c668dc1837bb235cfca19caeed167d6b202219 [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
Heinrich Schuchardt955a3212025-01-16 20:26:59 +010031#define LOG_CATEGORY LOGC_EFI
32
Simon Glass655306c2020-05-10 11:39:58 -060033#include <blk.h>
Simon Glassf6977c12020-07-19 10:15:43 -060034#include <dm.h>
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010035#include <efi_driver.h>
Simon Glass9bc15642020-02-03 07:36:16 -070036#include <malloc.h>
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010037#include <dm/device-internal.h>
38#include <dm/root.h>
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +090039#include <dm/tag.h>
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010040
Heinrich Schuchardt35679a92022-10-04 18:53:34 +020041/**
42 * struct efi_blk_plat - attributes of a block device
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010043 *
Heinrich Schuchardt35679a92022-10-04 18:53:34 +020044 * @handle: handle of the controller on which this driver is installed
45 * @io: block io protocol proxied by this driver
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010046 */
Simon Glassb75b15b2020-12-03 16:55:23 -070047struct efi_blk_plat {
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010048 efi_handle_t handle;
49 struct efi_block_io *io;
50};
51
Heinrich Schuchardt6492dad2019-07-14 11:52:33 +020052/**
Heinrich Schuchardt35679a92022-10-04 18:53:34 +020053 * efi_bl_read() - read from block device
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010054 *
Heinrich Schuchardt6492dad2019-07-14 11:52:33 +020055 * @dev: device
56 * @blknr: first block to be read
57 * @blkcnt: number of blocks to read
58 * @buffer: output buffer
59 * Return: number of blocks transferred
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010060 */
61static ulong efi_bl_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
62 void *buffer)
63{
Simon Glassb75b15b2020-12-03 16:55:23 -070064 struct efi_blk_plat *plat = dev_get_plat(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -070065 struct efi_block_io *io = plat->io;
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010066 efi_status_t ret;
67
68 EFI_PRINT("%s: read '%s', from block " LBAFU ", " LBAFU " blocks\n",
69 __func__, dev->name, blknr, blkcnt);
70 ret = EFI_CALL(io->read_blocks(
71 io, io->media->media_id, (u64)blknr,
72 (efi_uintn_t)blkcnt *
73 (efi_uintn_t)io->media->block_size, buffer));
74 EFI_PRINT("%s: r = %u\n", __func__,
75 (unsigned int)(ret & ~EFI_ERROR_MASK));
76 if (ret != EFI_SUCCESS)
77 return 0;
78 return blkcnt;
79}
80
Heinrich Schuchardt6492dad2019-07-14 11:52:33 +020081/**
Heinrich Schuchardt35679a92022-10-04 18:53:34 +020082 * efi_bl_write() - write to block device
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010083 *
Heinrich Schuchardt6492dad2019-07-14 11:52:33 +020084 * @dev: device
85 * @blknr: first block to be write
86 * @blkcnt: number of blocks to write
87 * @buffer: input buffer
88 * Return: number of blocks transferred
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010089 */
90static ulong efi_bl_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
91 const void *buffer)
92{
Simon Glassb75b15b2020-12-03 16:55:23 -070093 struct efi_blk_plat *plat = dev_get_plat(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -070094 struct efi_block_io *io = plat->io;
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010095 efi_status_t ret;
96
97 EFI_PRINT("%s: write '%s', from block " LBAFU ", " LBAFU " blocks\n",
98 __func__, dev->name, blknr, blkcnt);
99 ret = EFI_CALL(io->write_blocks(
100 io, io->media->media_id, (u64)blknr,
101 (efi_uintn_t)blkcnt *
102 (efi_uintn_t)io->media->block_size,
103 (void *)buffer));
104 EFI_PRINT("%s: r = %u\n", __func__,
105 (unsigned int)(ret & ~EFI_ERROR_MASK));
106 if (ret != EFI_SUCCESS)
107 return 0;
108 return blkcnt;
109}
110
Heinrich Schuchardt6492dad2019-07-14 11:52:33 +0200111/**
Heinrich Schuchardt35679a92022-10-04 18:53:34 +0200112 * efi_bl_create_block_device() - create a block device for a handle
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100113 *
Heinrich Schuchardt6492dad2019-07-14 11:52:33 +0200114 * @handle: handle
115 * @interface: block io protocol
Heinrich Schuchardt3f712db2022-10-03 10:35:35 +0200116 * Return: status code
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100117 */
Heinrich Schuchardtcc00eb42022-10-04 16:19:30 +0200118static efi_status_t
119efi_bl_create_block_device(efi_handle_t handle, void *interface)
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100120{
Heinrich Schuchardt3f712db2022-10-03 10:35:35 +0200121 struct udevice *bdev = NULL, *parent = dm_root();
122 efi_status_t ret;
123 int devnum;
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100124 char *name;
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100125 struct efi_block_io *io = interface;
Simon Glassb75b15b2020-12-03 16:55:23 -0700126 struct efi_blk_plat *plat;
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100127
Masahisa Kojima676c2742023-07-03 15:08:45 +0900128 devnum = blk_next_free_devnum(UCLASS_EFI_LOADER);
129 if (devnum < 0)
Heinrich Schuchardt3f712db2022-10-03 10:35:35 +0200130 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100131
132 name = calloc(1, 18); /* strlen("efiblk#2147483648") + 1 */
133 if (!name)
Heinrich Schuchardt3f712db2022-10-03 10:35:35 +0200134 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100135 sprintf(name, "efiblk#%d", devnum);
136
137 /* Create driver model udevice for the EFI block io device */
Heinrich Schuchardtda87af92024-10-18 03:30:14 +0200138 if (blk_create_devicef(parent, "efi_blk", name, UCLASS_EFI_LOADER,
139 devnum, io->media->block_size,
140 (lbaint_t)io->media->last_block, &bdev)) {
Heinrich Schuchardt3f712db2022-10-03 10:35:35 +0200141 ret = EFI_OUT_OF_RESOURCES;
142 free(name);
143 goto err;
144 }
Bin Meng91938632018-10-15 02:21:06 -0700145
Simon Glassfa20e932020-12-03 16:55:20 -0700146 plat = dev_get_plat(bdev);
Simon Glass71fa5b42020-12-03 16:55:18 -0700147 plat->handle = handle;
148 plat->io = interface;
Bin Meng91938632018-10-15 02:21:06 -0700149
Heinrich Schuchardt3f712db2022-10-03 10:35:35 +0200150 if (efi_link_dev(handle, bdev)) {
151 ret = EFI_OUT_OF_RESOURCES;
152 goto err;
153 }
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900154
Heinrich Schuchardt3f712db2022-10-03 10:35:35 +0200155 if (device_probe(bdev)) {
156 ret = EFI_DEVICE_ERROR;
157 goto err;
158 }
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100159 EFI_PRINT("%s: block device '%s' created\n", __func__, bdev->name);
160
Heinrich Schuchardt3f712db2022-10-03 10:35:35 +0200161 return EFI_SUCCESS;
162
163err:
164 efi_unlink_dev(handle);
165 if (bdev)
166 device_unbind(bdev);
167
168 return ret;
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100169}
170
Heinrich Schuchardtcc00eb42022-10-04 16:19:30 +0200171/**
172 * efi_bl_bind() - bind to a block io protocol
173 *
Heinrich Schuchardtff1d50b2022-10-04 19:12:59 +0200174 * @this: driver binding protocol
Heinrich Schuchardtcc00eb42022-10-04 16:19:30 +0200175 * @handle: handle
176 * @interface: block io protocol
177 * Return: status code
178 */
Heinrich Schuchardtff1d50b2022-10-04 19:12:59 +0200179static efi_status_t efi_bl_bind(
180 struct efi_driver_binding_extended_protocol *this,
181 efi_handle_t handle, void *interface)
Heinrich Schuchardtcc00eb42022-10-04 16:19:30 +0200182{
183 efi_status_t ret = EFI_SUCCESS;
184 struct efi_object *obj = efi_search_obj(handle);
185
186 EFI_PRINT("%s: handle %p, interface %p\n", __func__, handle, interface);
187
188 if (!obj || !interface)
189 return EFI_INVALID_PARAMETER;
190
191 if (!handle->dev)
192 ret = efi_bl_create_block_device(handle, interface);
193
194 return ret;
195}
196
Heinrich Schuchardt6c5fd212022-10-05 11:28:47 +0200197/**
198 * efi_bl_init() - initialize block device driver
199 *
200 * @this: extended driver binding protocol
201 */
202static efi_status_t
203efi_bl_init(struct efi_driver_binding_extended_protocol *this)
204{
Heinrich Schuchardtda620cf2022-10-06 07:29:41 +0200205 int ret;
206
207 ret = event_register("efi_disk add", EVT_DM_POST_PROBE,
208 efi_disk_probe, this);
209 if (ret) {
210 log_err("Event registration for efi_disk add failed\n");
211 return EFI_OUT_OF_RESOURCES;
212 }
213
214 ret = event_register("efi_disk del", EVT_DM_PRE_REMOVE,
215 efi_disk_remove, this);
216 if (ret) {
217 log_err("Event registration for efi_disk del failed\n");
218 return EFI_OUT_OF_RESOURCES;
219 }
220
Heinrich Schuchardt6c5fd212022-10-05 11:28:47 +0200221 return EFI_SUCCESS;
222}
223
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100224/* Block device driver operators */
225static const struct blk_ops efi_blk_ops = {
226 .read = efi_bl_read,
227 .write = efi_bl_write,
228};
229
230/* Identify as block device driver */
231U_BOOT_DRIVER(efi_blk) = {
Heinrich Schuchardt35679a92022-10-04 18:53:34 +0200232 .name = "efi_blk",
233 .id = UCLASS_BLK,
234 .ops = &efi_blk_ops,
Simon Glassb75b15b2020-12-03 16:55:23 -0700235 .plat_auto = sizeof(struct efi_blk_plat),
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100236};
237
238/* EFI driver operators */
239static const struct efi_driver_ops driver_ops = {
240 .protocol = &efi_block_io_guid,
241 .child_protocol = &efi_block_io_guid,
Heinrich Schuchardt6c5fd212022-10-05 11:28:47 +0200242 .init = efi_bl_init,
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100243 .bind = efi_bl_bind,
244};
245
246/* Identify as EFI driver */
247U_BOOT_DRIVER(efi_block) = {
248 .name = "EFI block driver",
Simon Glass15c4d672021-12-04 08:56:30 -0700249 .id = UCLASS_EFI_LOADER,
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100250 .ops = &driver_ops,
251};