blob: 0937e3595a4331834f150d8efe945d89772b8339 [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>
38
39/*
40 * EFI attributes of the udevice handled by this driver.
41 *
42 * handle handle of the controller on which this driver is installed
43 * io block io protocol proxied by this driver
44 */
Simon Glassb75b15b2020-12-03 16:55:23 -070045struct efi_blk_plat {
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010046 efi_handle_t handle;
47 struct efi_block_io *io;
48};
49
Heinrich Schuchardt6492dad2019-07-14 11:52:33 +020050/**
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010051 * Read from block device
52 *
Heinrich Schuchardt6492dad2019-07-14 11:52:33 +020053 * @dev: device
54 * @blknr: first block to be read
55 * @blkcnt: number of blocks to read
56 * @buffer: output buffer
57 * Return: number of blocks transferred
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010058 */
59static ulong efi_bl_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
60 void *buffer)
61{
Simon Glassb75b15b2020-12-03 16:55:23 -070062 struct efi_blk_plat *plat = dev_get_plat(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -070063 struct efi_block_io *io = plat->io;
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010064 efi_status_t ret;
65
66 EFI_PRINT("%s: read '%s', from block " LBAFU ", " LBAFU " blocks\n",
67 __func__, dev->name, blknr, blkcnt);
68 ret = EFI_CALL(io->read_blocks(
69 io, io->media->media_id, (u64)blknr,
70 (efi_uintn_t)blkcnt *
71 (efi_uintn_t)io->media->block_size, buffer));
72 EFI_PRINT("%s: r = %u\n", __func__,
73 (unsigned int)(ret & ~EFI_ERROR_MASK));
74 if (ret != EFI_SUCCESS)
75 return 0;
76 return blkcnt;
77}
78
Heinrich Schuchardt6492dad2019-07-14 11:52:33 +020079/**
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010080 * Write to block device
81 *
Heinrich Schuchardt6492dad2019-07-14 11:52:33 +020082 * @dev: device
83 * @blknr: first block to be write
84 * @blkcnt: number of blocks to write
85 * @buffer: input buffer
86 * Return: number of blocks transferred
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010087 */
88static ulong efi_bl_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
89 const void *buffer)
90{
Simon Glassb75b15b2020-12-03 16:55:23 -070091 struct efi_blk_plat *plat = dev_get_plat(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -070092 struct efi_block_io *io = plat->io;
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010093 efi_status_t ret;
94
95 EFI_PRINT("%s: write '%s', from block " LBAFU ", " LBAFU " blocks\n",
96 __func__, dev->name, blknr, blkcnt);
97 ret = EFI_CALL(io->write_blocks(
98 io, io->media->media_id, (u64)blknr,
99 (efi_uintn_t)blkcnt *
100 (efi_uintn_t)io->media->block_size,
101 (void *)buffer));
102 EFI_PRINT("%s: r = %u\n", __func__,
103 (unsigned int)(ret & ~EFI_ERROR_MASK));
104 if (ret != EFI_SUCCESS)
105 return 0;
106 return blkcnt;
107}
108
Heinrich Schuchardt6492dad2019-07-14 11:52:33 +0200109/**
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100110 * Create partions for the block device.
111 *
Heinrich Schuchardt6492dad2019-07-14 11:52:33 +0200112 * @handle: EFI handle of the block device
113 * @dev: udevice of the block device
114 * Return: number of partitions created
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100115 */
116static int efi_bl_bind_partitions(efi_handle_t handle, struct udevice *dev)
117{
118 struct blk_desc *desc;
119 const char *if_typename;
120
Simon Glass71fa5b42020-12-03 16:55:18 -0700121 desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100122 if_typename = blk_get_if_type_name(desc->if_type);
123
124 return efi_disk_create_partitions(handle, desc, if_typename,
125 desc->devnum, dev->name);
126}
127
Heinrich Schuchardt6492dad2019-07-14 11:52:33 +0200128/**
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100129 * Create a block device for a handle
130 *
Heinrich Schuchardt6492dad2019-07-14 11:52:33 +0200131 * @handle: handle
132 * @interface: block io protocol
133 * Return: 0 = success
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100134 */
135static int efi_bl_bind(efi_handle_t handle, void *interface)
136{
137 struct udevice *bdev, *parent = dm_root();
138 int ret, devnum;
139 char *name;
140 struct efi_object *obj = efi_search_obj(handle);
141 struct efi_block_io *io = interface;
142 int disks;
Simon Glassb75b15b2020-12-03 16:55:23 -0700143 struct efi_blk_plat *plat;
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100144
145 EFI_PRINT("%s: handle %p, interface %p\n", __func__, handle, io);
146
147 if (!obj)
148 return -ENOENT;
149
150 devnum = blk_find_max_devnum(IF_TYPE_EFI);
151 if (devnum == -ENODEV)
152 devnum = 0;
153 else if (devnum < 0)
154 return devnum;
155
156 name = calloc(1, 18); /* strlen("efiblk#2147483648") + 1 */
157 if (!name)
158 return -ENOMEM;
159 sprintf(name, "efiblk#%d", devnum);
160
161 /* Create driver model udevice for the EFI block io device */
162 ret = blk_create_device(parent, "efi_blk", name, IF_TYPE_EFI, devnum,
163 io->media->block_size,
164 (lbaint_t)io->media->last_block, &bdev);
165 if (ret)
166 return ret;
167 if (!bdev)
168 return -ENOENT;
Heinrich Schuchardt25ac13e2018-06-30 07:11:32 +0200169 /* Set the DM_FLAG_NAME_ALLOCED flag to avoid a memory leak */
170 device_set_name_alloced(bdev);
Bin Meng91938632018-10-15 02:21:06 -0700171
Simon Glassfa20e932020-12-03 16:55:20 -0700172 plat = dev_get_plat(bdev);
Simon Glass71fa5b42020-12-03 16:55:18 -0700173 plat->handle = handle;
174 plat->io = interface;
Bin Meng91938632018-10-15 02:21:06 -0700175
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100176 ret = device_probe(bdev);
177 if (ret)
178 return ret;
179 EFI_PRINT("%s: block device '%s' created\n", __func__, bdev->name);
180
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100181 /* Create handles for the partions of the block device */
182 disks = efi_bl_bind_partitions(handle, bdev);
183 EFI_PRINT("Found %d partitions\n", disks);
184
185 return 0;
186}
187
188/* Block device driver operators */
189static const struct blk_ops efi_blk_ops = {
190 .read = efi_bl_read,
191 .write = efi_bl_write,
192};
193
194/* Identify as block device driver */
195U_BOOT_DRIVER(efi_blk) = {
196 .name = "efi_blk",
197 .id = UCLASS_BLK,
198 .ops = &efi_blk_ops,
Simon Glassb75b15b2020-12-03 16:55:23 -0700199 .plat_auto = sizeof(struct efi_blk_plat),
Heinrich Schuchardt11206f42018-01-21 19:29:30 +0100200};
201
202/* EFI driver operators */
203static const struct efi_driver_ops driver_ops = {
204 .protocol = &efi_block_io_guid,
205 .child_protocol = &efi_block_io_guid,
206 .bind = efi_bl_bind,
207};
208
209/* Identify as EFI driver */
210U_BOOT_DRIVER(efi_block) = {
211 .name = "EFI block driver",
212 .id = UCLASS_EFI,
213 .ops = &driver_ops,
214};