1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#include <efi_driver.h>
32#include <dm/device-internal.h>
33#include <dm/root.h>
34
35
36
37
38
39
40
41struct efi_blk_priv {
42 efi_handle_t handle;
43 struct efi_block_io *io;
44};
45
46
47
48
49
50
51
52
53
54
55static ulong efi_bl_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
56 void *buffer)
57{
58 struct efi_blk_priv *priv = dev->priv;
59 struct efi_block_io *io = priv->io;
60 efi_status_t ret;
61
62 EFI_PRINT("%s: read '%s', from block " LBAFU ", " LBAFU " blocks\n",
63 __func__, dev->name, blknr, blkcnt);
64 ret = EFI_CALL(io->read_blocks(
65 io, io->media->media_id, (u64)blknr,
66 (efi_uintn_t)blkcnt *
67 (efi_uintn_t)io->media->block_size, buffer));
68 EFI_PRINT("%s: r = %u\n", __func__,
69 (unsigned int)(ret & ~EFI_ERROR_MASK));
70 if (ret != EFI_SUCCESS)
71 return 0;
72 return blkcnt;
73}
74
75
76
77
78
79
80
81
82
83
84static ulong efi_bl_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
85 const void *buffer)
86{
87 struct efi_blk_priv *priv = dev->priv;
88 struct efi_block_io *io = priv->io;
89 efi_status_t ret;
90
91 EFI_PRINT("%s: write '%s', from block " LBAFU ", " LBAFU " blocks\n",
92 __func__, dev->name, blknr, blkcnt);
93 ret = EFI_CALL(io->write_blocks(
94 io, io->media->media_id, (u64)blknr,
95 (efi_uintn_t)blkcnt *
96 (efi_uintn_t)io->media->block_size,
97 (void *)buffer));
98 EFI_PRINT("%s: r = %u\n", __func__,
99 (unsigned int)(ret & ~EFI_ERROR_MASK));
100 if (ret != EFI_SUCCESS)
101 return 0;
102 return blkcnt;
103}
104
105
106
107
108
109
110
111static int efi_bl_bind_partitions(efi_handle_t handle, struct udevice *dev)
112{
113 struct blk_desc *desc;
114 const char *if_typename;
115
116 desc = dev_get_uclass_platdata(dev);
117 if_typename = blk_get_if_type_name(desc->if_type);
118
119 return efi_disk_create_partitions(handle, desc, if_typename,
120 desc->devnum, dev->name);
121}
122
123
124
125
126
127
128
129
130static int efi_bl_bind(efi_handle_t handle, void *interface)
131{
132 struct udevice *bdev, *parent = dm_root();
133 int ret, devnum;
134 char *name;
135 struct efi_object *obj = efi_search_obj(handle);
136 struct efi_block_io *io = interface;
137 int disks;
138 struct efi_blk_priv *priv;
139
140 EFI_PRINT("%s: handle %p, interface %p\n", __func__, handle, io);
141
142 if (!obj)
143 return -ENOENT;
144
145 devnum = blk_find_max_devnum(IF_TYPE_EFI);
146 if (devnum == -ENODEV)
147 devnum = 0;
148 else if (devnum < 0)
149 return devnum;
150
151 name = calloc(1, 18);
152 if (!name)
153 return -ENOMEM;
154 sprintf(name, "efiblk#%d", devnum);
155
156
157 ret = blk_create_device(parent, "efi_blk", name, IF_TYPE_EFI, devnum,
158 io->media->block_size,
159 (lbaint_t)io->media->last_block, &bdev);
160 if (ret)
161 return ret;
162 if (!bdev)
163 return -ENOENT;
164
165 device_set_name_alloced(bdev);
166
167 ret = device_probe(bdev);
168 if (ret)
169 return ret;
170 EFI_PRINT("%s: block device '%s' created\n", __func__, bdev->name);
171
172 priv = bdev->priv;
173 priv->handle = handle;
174 priv->io = interface;
175
176 ret = blk_prepare_device(bdev);
177
178
179 disks = efi_bl_bind_partitions(handle, bdev);
180 EFI_PRINT("Found %d partitions\n", disks);
181
182 return 0;
183}
184
185
186static const struct blk_ops efi_blk_ops = {
187 .read = efi_bl_read,
188 .write = efi_bl_write,
189};
190
191
192U_BOOT_DRIVER(efi_blk) = {
193 .name = "efi_blk",
194 .id = UCLASS_BLK,
195 .ops = &efi_blk_ops,
196 .priv_auto_alloc_size = sizeof(struct efi_blk_priv),
197};
198
199
200static const struct efi_driver_ops driver_ops = {
201 .protocol = &efi_block_io_guid,
202 .child_protocol = &efi_block_io_guid,
203 .bind = efi_bl_bind,
204};
205
206
207U_BOOT_DRIVER(efi_block) = {
208 .name = "EFI block driver",
209 .id = UCLASS_EFI,
210 .ops = &driver_ops,
211};
212