blob: 10f3cb90c43aeb808bbd006f734d83acace33d32 [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +01002/*
3 * EFI block driver
4 *
5 * Copyright (c) 2017 Heinrich Schuchardt
6 *
Heinrich Schuchardt05ef48a2018-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 Glasse6f6f9e2020-05-10 11:39:58 -060031#include <common.h>
32#include <blk.h>
Simon Glasse1e10f22020-07-19 10:15:43 -060033#include <dm.h>
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +010034#include <efi_driver.h>
Simon Glass336d4612020-02-03 07:36:16 -070035#include <malloc.h>
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +010036#include <dm/device-internal.h>
37#include <dm/root.h>
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +090038#include <dm/tag.h>
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +010039
40/*
41 * EFI attributes of the udevice handled by this driver.
42 *
43 * handle handle of the controller on which this driver is installed
44 * io block io protocol proxied by this driver
45 */
Simon Glass8a8d24b2020-12-03 16:55:23 -070046struct efi_blk_plat {
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +010047 efi_handle_t handle;
48 struct efi_block_io *io;
49};
50
Heinrich Schuchardt3f33f302019-07-14 11:52:33 +020051/**
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +010052 * Read from block device
53 *
Heinrich Schuchardt3f33f302019-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 Schuchardt05ef48a2018-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 Glass8a8d24b2020-12-03 16:55:23 -070063 struct efi_blk_plat *plat = dev_get_plat(dev);
Simon Glasscaa4daa2020-12-03 16:55:18 -070064 struct efi_block_io *io = plat->io;
Heinrich Schuchardt05ef48a2018-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 Schuchardt3f33f302019-07-14 11:52:33 +020080/**
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +010081 * Write to block device
82 *
Heinrich Schuchardt3f33f302019-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 Schuchardt05ef48a2018-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 Glass8a8d24b2020-12-03 16:55:23 -070092 struct efi_blk_plat *plat = dev_get_plat(dev);
Simon Glasscaa4daa2020-12-03 16:55:18 -070093 struct efi_block_io *io = plat->io;
Heinrich Schuchardt05ef48a2018-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 Schuchardt3f33f302019-07-14 11:52:33 +0200110/**
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100111 * Create a block device for a handle
112 *
Heinrich Schuchardt3f33f302019-07-14 11:52:33 +0200113 * @handle: handle
114 * @interface: block io protocol
Heinrich Schuchardt43a58912022-10-03 10:35:35 +0200115 * Return: status code
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100116 */
Heinrich Schuchardta6d4f702022-10-04 16:19:30 +0200117static efi_status_t
118efi_bl_create_block_device(efi_handle_t handle, void *interface)
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100119{
Heinrich Schuchardt43a58912022-10-03 10:35:35 +0200120 struct udevice *bdev = NULL, *parent = dm_root();
121 efi_status_t ret;
122 int devnum;
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100123 char *name;
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100124 struct efi_block_io *io = interface;
Simon Glass8a8d24b2020-12-03 16:55:23 -0700125 struct efi_blk_plat *plat;
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100126
Simon Glasse33a5c62022-08-11 19:34:59 -0600127 devnum = blk_find_max_devnum(UCLASS_EFI_LOADER);
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100128 if (devnum == -ENODEV)
129 devnum = 0;
130 else if (devnum < 0)
Heinrich Schuchardt43a58912022-10-03 10:35:35 +0200131 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100132
133 name = calloc(1, 18); /* strlen("efiblk#2147483648") + 1 */
134 if (!name)
Heinrich Schuchardt43a58912022-10-03 10:35:35 +0200135 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100136 sprintf(name, "efiblk#%d", devnum);
137
138 /* Create driver model udevice for the EFI block io device */
Heinrich Schuchardt43a58912022-10-03 10:35:35 +0200139 if (blk_create_device(parent, "efi_blk", name, UCLASS_EFI_LOADER,
140 devnum, io->media->block_size,
141 (lbaint_t)io->media->last_block, &bdev)) {
142 ret = EFI_OUT_OF_RESOURCES;
143 free(name);
144 goto err;
145 }
Heinrich Schuchardtdf764312018-06-30 07:11:32 +0200146 /* Set the DM_FLAG_NAME_ALLOCED flag to avoid a memory leak */
147 device_set_name_alloced(bdev);
Bin Mengf26ce032018-10-15 02:21:06 -0700148
Simon Glassc69cda22020-12-03 16:55:20 -0700149 plat = dev_get_plat(bdev);
Simon Glasscaa4daa2020-12-03 16:55:18 -0700150 plat->handle = handle;
151 plat->io = interface;
Bin Mengf26ce032018-10-15 02:21:06 -0700152
Heinrich Schuchardt43a58912022-10-03 10:35:35 +0200153 if (efi_link_dev(handle, bdev)) {
154 ret = EFI_OUT_OF_RESOURCES;
155 goto err;
156 }
AKASHI Takahiroa9bf0242022-04-19 10:05:12 +0900157
Heinrich Schuchardt43a58912022-10-03 10:35:35 +0200158 if (device_probe(bdev)) {
159 ret = EFI_DEVICE_ERROR;
160 goto err;
161 }
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100162 EFI_PRINT("%s: block device '%s' created\n", __func__, bdev->name);
163
Heinrich Schuchardt43a58912022-10-03 10:35:35 +0200164 return EFI_SUCCESS;
165
166err:
167 efi_unlink_dev(handle);
168 if (bdev)
169 device_unbind(bdev);
170
171 return ret;
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100172}
173
Heinrich Schuchardta6d4f702022-10-04 16:19:30 +0200174/**
175 * efi_bl_bind() - bind to a block io protocol
176 *
177 * @handle: handle
178 * @interface: block io protocol
179 * Return: status code
180 */
181static efi_status_t efi_bl_bind(efi_handle_t handle, void *interface)
182{
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 Schuchardt05ef48a2018-01-21 19:29:30 +0100197/* Block device driver operators */
198static const struct blk_ops efi_blk_ops = {
199 .read = efi_bl_read,
200 .write = efi_bl_write,
201};
202
203/* Identify as block device driver */
204U_BOOT_DRIVER(efi_blk) = {
205 .name = "efi_blk",
206 .id = UCLASS_BLK,
207 .ops = &efi_blk_ops,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700208 .plat_auto = sizeof(struct efi_blk_plat),
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100209};
210
211/* EFI driver operators */
212static const struct efi_driver_ops driver_ops = {
213 .protocol = &efi_block_io_guid,
214 .child_protocol = &efi_block_io_guid,
215 .bind = efi_bl_bind,
216};
217
218/* Identify as EFI driver */
219U_BOOT_DRIVER(efi_block) = {
220 .name = "EFI block driver",
Simon Glass2abd8d12021-12-04 08:56:30 -0700221 .id = UCLASS_EFI_LOADER,
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +0100222 .ops = &driver_ops,
223};