]> git.ipfire.org Git - people/ms/u-boot.git/blame - lib/efi_loader/efi_disk.c
efi_loader: helper function to add EFI object to list
[people/ms/u-boot.git] / lib / efi_loader / efi_disk.c
CommitLineData
2a22d05d
AG
1/*
2 * EFI application disk support
3 *
4 * Copyright (c) 2016 Alexander Graf
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9#include <common.h>
6dd9faf8 10#include <blk.h>
487d756f 11#include <dm.h>
2a22d05d
AG
12#include <efi_loader.h>
13#include <inttypes.h>
14#include <part.h>
15#include <malloc.h>
16
17static const efi_guid_t efi_block_io_guid = BLOCK_IO_GUID;
18
19struct efi_disk_obj {
20 /* Generic EFI object parent class data */
21 struct efi_object parent;
22 /* EFI Interface callback struct for block I/O */
23 struct efi_block_io ops;
24 /* U-Boot ifname for block device */
25 const char *ifname;
26 /* U-Boot dev_index for block device */
27 int dev_index;
28 /* EFI Interface Media descriptor struct, referenced by ops */
29 struct efi_block_io_media media;
30 /* EFI device path to this block device */
884bcf6f
RC
31 struct efi_device_path *dp;
32 /* partition # */
33 unsigned int part;
2a92080d
RC
34 /* handle to filesys proto (for partition objects) */
35 struct efi_simple_file_system_protocol *volume;
8c3df0bf
AG
36 /* Offset into disk for simple partitions */
37 lbaint_t offset;
f9d334bd 38 /* Internal block device */
884bcf6f 39 struct blk_desc *desc;
2a22d05d
AG
40};
41
2a22d05d
AG
42static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
43 char extended_verification)
44{
45 EFI_ENTRY("%p, %x", this, extended_verification);
46 return EFI_EXIT(EFI_DEVICE_ERROR);
47}
48
49enum efi_disk_direction {
50 EFI_DISK_READ,
51 EFI_DISK_WRITE,
52};
53
a80a232e 54static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
2a22d05d
AG
55 u32 media_id, u64 lba, unsigned long buffer_size,
56 void *buffer, enum efi_disk_direction direction)
57{
58 struct efi_disk_obj *diskobj;
59 struct blk_desc *desc;
60 int blksz;
61 int blocks;
62 unsigned long n;
63
2a22d05d 64 diskobj = container_of(this, struct efi_disk_obj, ops);
f9d334bd 65 desc = (struct blk_desc *) diskobj->desc;
2a22d05d
AG
66 blksz = desc->blksz;
67 blocks = buffer_size / blksz;
8c3df0bf 68 lba += diskobj->offset;
2a22d05d 69
edcef3ba
AG
70 debug("EFI: %s:%d blocks=%x lba=%"PRIx64" blksz=%x dir=%d\n", __func__,
71 __LINE__, blocks, lba, blksz, direction);
2a22d05d
AG
72
73 /* We only support full block access */
74 if (buffer_size & (blksz - 1))
3304990b 75 return EFI_DEVICE_ERROR;
2a22d05d
AG
76
77 if (direction == EFI_DISK_READ)
487d756f 78 n = blk_dread(desc, lba, blocks, buffer);
2a22d05d 79 else
487d756f 80 n = blk_dwrite(desc, lba, blocks, buffer);
2a22d05d
AG
81
82 /* We don't do interrupts, so check for timers cooperatively */
83 efi_timer_check();
84
edcef3ba
AG
85 debug("EFI: %s:%d n=%lx blocks=%x\n", __func__, __LINE__, n, blocks);
86
2a22d05d 87 if (n != blocks)
3304990b 88 return EFI_DEVICE_ERROR;
2a22d05d 89
3304990b 90 return EFI_SUCCESS;
2a22d05d
AG
91}
92
e275458c 93static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
2a22d05d
AG
94 u32 media_id, u64 lba, unsigned long buffer_size,
95 void *buffer)
96{
51735ae0
AG
97 void *real_buffer = buffer;
98 efi_status_t r;
99
100#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
101 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
102 r = efi_disk_read_blocks(this, media_id, lba,
103 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
104 if (r != EFI_SUCCESS)
105 return r;
106 return efi_disk_read_blocks(this, media_id, lba +
107 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
108 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
109 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
110 }
111
112 real_buffer = efi_bounce_buffer;
113#endif
114
115 EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba,
116 buffer_size, buffer);
117
118 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
119 EFI_DISK_READ);
120
121 /* Copy from bounce buffer to real buffer if necessary */
122 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
123 memcpy(buffer, real_buffer, buffer_size);
124
125 return EFI_EXIT(r);
2a22d05d
AG
126}
127
e275458c 128static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
2a22d05d
AG
129 u32 media_id, u64 lba, unsigned long buffer_size,
130 void *buffer)
131{
51735ae0
AG
132 void *real_buffer = buffer;
133 efi_status_t r;
134
135#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
136 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
137 r = efi_disk_write_blocks(this, media_id, lba,
138 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
139 if (r != EFI_SUCCESS)
140 return r;
141 return efi_disk_write_blocks(this, media_id, lba +
142 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
143 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
144 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
145 }
146
147 real_buffer = efi_bounce_buffer;
148#endif
149
150 EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba,
151 buffer_size, buffer);
152
153 /* Populate bounce buffer if necessary */
154 if (real_buffer != buffer)
155 memcpy(real_buffer, buffer, buffer_size);
156
157 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
158 EFI_DISK_WRITE);
159
160 return EFI_EXIT(r);
2a22d05d
AG
161}
162
163static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
164{
165 /* We always write synchronously */
166 EFI_ENTRY("%p", this);
167 return EFI_EXIT(EFI_SUCCESS);
168}
169
170static const struct efi_block_io block_io_disk_template = {
171 .reset = &efi_disk_reset,
172 .read_blocks = &efi_disk_read_blocks,
173 .write_blocks = &efi_disk_write_blocks,
174 .flush_blocks = &efi_disk_flush_blocks,
175};
176
2a92080d
RC
177/*
178 * Find filesystem from a device-path. The passed in path 'p' probably
179 * contains one or more /File(name) nodes, so the comparison stops at
180 * the first /File() node, and returns the pointer to that via 'rp'.
181 * This is mostly intended to be a helper to map a device-path to an
182 * efi_file_handle object.
183 */
184struct efi_simple_file_system_protocol *
185efi_fs_from_path(struct efi_device_path *fp)
186{
187 struct efi_object *efiobj;
188 struct efi_disk_obj *diskobj;
189
190 efiobj = efi_dp_find_obj(fp, NULL);
191 if (!efiobj)
192 return NULL;
193
194 diskobj = container_of(efiobj, struct efi_disk_obj, parent);
195
196 return diskobj->volume;
197}
198
93945f2c
HS
199/*
200 * Create a device for a disk
201 *
202 * @name not used
203 * @if_typename interface name for block device
204 * @desc internal block device
205 * @dev_index device index for block device
206 * @offset offset into disk for simple partitions
207 */
487d756f
SG
208static void efi_disk_add_dev(const char *name,
209 const char *if_typename,
884bcf6f 210 struct blk_desc *desc,
4a12a97c 211 int dev_index,
884bcf6f
RC
212 lbaint_t offset,
213 unsigned int part)
4a12a97c
AG
214{
215 struct efi_disk_obj *diskobj;
4b9f7aaf 216 efi_status_t ret;
4a12a97c 217
0812d1a0
AG
218 /* Don't add empty devices */
219 if (!desc->lba)
220 return;
221
884bcf6f 222 diskobj = calloc(1, sizeof(*diskobj));
4b9f7aaf
HS
223 if (!diskobj)
224 goto out_of_memory;
225
226 /* Hook up to the device list */
44549d62 227 efi_add_handle(&diskobj->parent);
4a12a97c
AG
228
229 /* Fill in object data */
884bcf6f
RC
230 diskobj->dp = efi_dp_from_part(desc, part);
231 diskobj->part = part;
4b9f7aaf
HS
232 ret = efi_add_protocol(diskobj->parent.handle, &efi_block_io_guid,
233 &diskobj->ops);
234 if (ret != EFI_SUCCESS)
235 goto out_of_memory;
236 ret = efi_add_protocol(diskobj->parent.handle, &efi_guid_device_path,
237 diskobj->dp);
238 if (ret != EFI_SUCCESS)
239 goto out_of_memory;
2a92080d
RC
240 if (part >= 1) {
241 diskobj->volume = efi_simple_file_system(desc, part,
242 diskobj->dp);
4b9f7aaf
HS
243 ret = efi_add_protocol(diskobj->parent.handle,
244 &efi_simple_file_system_protocol_guid,
245 &diskobj->volume);
246 if (ret != EFI_SUCCESS)
247 goto out_of_memory;
2a92080d 248 }
4a12a97c 249 diskobj->ops = block_io_disk_template;
487d756f 250 diskobj->ifname = if_typename;
4a12a97c 251 diskobj->dev_index = dev_index;
8c3df0bf 252 diskobj->offset = offset;
f9d334bd 253 diskobj->desc = desc;
4a12a97c
AG
254
255 /* Fill in EFI IO Media info (for read/write callbacks) */
256 diskobj->media.removable_media = desc->removable;
257 diskobj->media.media_present = 1;
258 diskobj->media.block_size = desc->blksz;
259 diskobj->media.io_align = desc->blksz;
0812d1a0 260 diskobj->media.last_block = desc->lba - offset;
4a12a97c 261 diskobj->ops.media = &diskobj->media;
4b9f7aaf
HS
262 return;
263out_of_memory:
264 printf("ERROR: Out of memory\n");
4a12a97c
AG
265}
266
8c3df0bf 267static int efi_disk_create_eltorito(struct blk_desc *desc,
487d756f 268 const char *if_typename,
f9d334bd
AG
269 int diskid,
270 const char *pdevname)
8c3df0bf
AG
271{
272 int disks = 0;
1acc0087 273#if CONFIG_IS_ENABLED(ISO_PARTITION)
ecbe1a07 274 char devname[32] = { 0 }; /* dp->str is u16[32] long */
8c3df0bf 275 disk_partition_t info;
16a73b24 276 int part;
8c3df0bf
AG
277
278 if (desc->part_type != PART_TYPE_ISO)
279 return 0;
280
884bcf6f 281 /* and devices for each partition: */
16a73b24
JG
282 for (part = 1; part <= MAX_SEARCH_PARTITIONS; part++) {
283 if (part_get_info(desc, part, &info))
284 continue;
f9d334bd
AG
285 snprintf(devname, sizeof(devname), "%s:%d", pdevname,
286 part);
487d756f 287 efi_disk_add_dev(devname, if_typename, desc, diskid,
884bcf6f 288 info.start, part);
8c3df0bf
AG
289 disks++;
290 }
884bcf6f
RC
291
292 /* ... and add block device: */
293 efi_disk_add_dev(devname, if_typename, desc, diskid, 0, 0);
8c3df0bf
AG
294#endif
295
296 return disks;
297}
298
2a22d05d
AG
299/*
300 * U-Boot doesn't have a list of all online disk devices. So when running our
301 * EFI payload, we scan through all of the potentially available ones and
302 * store them in our object pool.
303 *
487d756f
SG
304 * TODO(sjg@chromium.org): Actually with CONFIG_BLK, U-Boot does have this.
305 * Consider converting the code to look up devices as needed. The EFI device
306 * could be a child of the UCLASS_BLK block device, perhaps.
307 *
2a22d05d
AG
308 * This gets called from do_bootefi_exec().
309 */
310int efi_disk_register(void)
311{
2a22d05d 312 int disks = 0;
487d756f
SG
313#ifdef CONFIG_BLK
314 struct udevice *dev;
315
70bfcdc6 316 for (uclass_first_device_check(UCLASS_BLK, &dev);
487d756f 317 dev;
70bfcdc6 318 uclass_next_device_check(&dev)) {
487d756f
SG
319 struct blk_desc *desc = dev_get_uclass_platdata(dev);
320 const char *if_typename = dev->driver->name;
884bcf6f 321 disk_partition_t info;
16a73b24 322 int part;
487d756f
SG
323
324 printf("Scanning disk %s...\n", dev->name);
884bcf6f
RC
325
326 /* add devices for each partition: */
16a73b24
JG
327 for (part = 1; part <= MAX_SEARCH_PARTITIONS; part++) {
328 if (part_get_info(desc, part, &info))
329 continue;
884bcf6f
RC
330 efi_disk_add_dev(dev->name, if_typename, desc,
331 desc->devnum, 0, part);
884bcf6f
RC
332 }
333
334 /* ... and add block device: */
335 efi_disk_add_dev(dev->name, if_typename, desc,
336 desc->devnum, 0, 0);
337
487d756f
SG
338 disks++;
339
340 /*
341 * El Torito images show up as block devices in an EFI world,
342 * so let's create them here
343 */
344 disks += efi_disk_create_eltorito(desc, if_typename,
f9d334bd 345 desc->devnum, dev->name);
487d756f
SG
346 }
347#else
348 int i, if_type;
2a22d05d
AG
349
350 /* Search for all available disk devices */
6dd9faf8 351 for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) {
487d756f
SG
352 const struct blk_driver *cur_drvr;
353 const char *if_typename;
354
6dd9faf8
SG
355 cur_drvr = blk_driver_lookup_type(if_type);
356 if (!cur_drvr)
357 continue;
358
487d756f
SG
359 if_typename = cur_drvr->if_typename;
360 printf("Scanning disks on %s...\n", if_typename);
2a22d05d
AG
361 for (i = 0; i < 4; i++) {
362 struct blk_desc *desc;
ecbe1a07 363 char devname[32] = { 0 }; /* dp->str is u16[32] long */
77511b3b 364 disk_partition_t info;
16a73b24 365 int part;
2a22d05d 366
6dd9faf8 367 desc = blk_get_devnum_by_type(if_type, i);
2a22d05d
AG
368 if (!desc)
369 continue;
370 if (desc->type == DEV_TYPE_UNKNOWN)
371 continue;
372
2a22d05d 373 snprintf(devname, sizeof(devname), "%s%d",
487d756f 374 if_typename, i);
77511b3b
RC
375
376 /* add devices for each partition: */
16a73b24
JG
377 for (part = 1; part <= MAX_SEARCH_PARTITIONS; part++) {
378 if (part_get_info(desc, part, &info))
379 continue;
77511b3b
RC
380 efi_disk_add_dev(devname, if_typename, desc,
381 i, 0, part);
77511b3b
RC
382 }
383
384 /* ... and add block device: */
884bcf6f 385 efi_disk_add_dev(devname, if_typename, desc, i, 0, 0);
2a22d05d 386 disks++;
8c3df0bf
AG
387
388 /*
389 * El Torito images show up as block devices
390 * in an EFI world, so let's create them here
391 */
f9d334bd
AG
392 disks += efi_disk_create_eltorito(desc, if_typename,
393 i, devname);
2a22d05d
AG
394 }
395 }
487d756f 396#endif
2a22d05d
AG
397 printf("Found %d disks\n", disks);
398
399 return 0;
400}