1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application disk support
5 * Copyright (c) 2016 Alexander Graf
11 #include <efi_loader.h>
16 const efi_guid_t efi_block_io_guid = BLOCK_IO_GUID;
19 /* Generic EFI object parent class data */
20 struct efi_object parent;
21 /* EFI Interface callback struct for block I/O */
22 struct efi_block_io ops;
23 /* U-Boot ifname for block device */
25 /* U-Boot dev_index for block device */
27 /* EFI Interface Media descriptor struct, referenced by ops */
28 struct efi_block_io_media media;
29 /* EFI device path to this block device */
30 struct efi_device_path *dp;
33 /* handle to filesys proto (for partition objects) */
34 struct efi_simple_file_system_protocol *volume;
35 /* Offset into disk for simple partitions */
37 /* Internal block device */
38 struct blk_desc *desc;
41 static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
42 char extended_verification)
44 EFI_ENTRY("%p, %x", this, extended_verification);
45 return EFI_EXIT(EFI_DEVICE_ERROR);
48 enum efi_disk_direction {
53 static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
54 u32 media_id, u64 lba, unsigned long buffer_size,
55 void *buffer, enum efi_disk_direction direction)
57 struct efi_disk_obj *diskobj;
58 struct blk_desc *desc;
63 diskobj = container_of(this, struct efi_disk_obj, ops);
64 desc = (struct blk_desc *) diskobj->desc;
66 blocks = buffer_size / blksz;
67 lba += diskobj->offset;
69 debug("EFI: %s:%d blocks=%x lba=%"PRIx64" blksz=%x dir=%d\n", __func__,
70 __LINE__, blocks, lba, blksz, direction);
72 /* We only support full block access */
73 if (buffer_size & (blksz - 1))
74 return EFI_DEVICE_ERROR;
76 if (direction == EFI_DISK_READ)
77 n = blk_dread(desc, lba, blocks, buffer);
79 n = blk_dwrite(desc, lba, blocks, buffer);
81 /* We don't do interrupts, so check for timers cooperatively */
84 debug("EFI: %s:%d n=%lx blocks=%x\n", __func__, __LINE__, n, blocks);
87 return EFI_DEVICE_ERROR;
92 static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
93 u32 media_id, u64 lba, efi_uintn_t buffer_size,
96 void *real_buffer = buffer;
99 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
100 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
101 r = efi_disk_read_blocks(this, media_id, lba,
102 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
103 if (r != EFI_SUCCESS)
105 return efi_disk_read_blocks(this, media_id, lba +
106 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
107 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
108 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
111 real_buffer = efi_bounce_buffer;
114 EFI_ENTRY("%p, %x, %" PRIx64 ", %zx, %p", this, media_id, lba,
115 buffer_size, buffer);
117 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
120 /* Copy from bounce buffer to real buffer if necessary */
121 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
122 memcpy(buffer, real_buffer, buffer_size);
127 static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
128 u32 media_id, u64 lba, efi_uintn_t buffer_size,
131 void *real_buffer = buffer;
134 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
135 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
136 r = efi_disk_write_blocks(this, media_id, lba,
137 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
138 if (r != EFI_SUCCESS)
140 return efi_disk_write_blocks(this, media_id, lba +
141 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
142 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
143 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
146 real_buffer = efi_bounce_buffer;
149 EFI_ENTRY("%p, %x, %" PRIx64 ", %zx, %p", this, media_id, lba,
150 buffer_size, buffer);
152 /* Populate bounce buffer if necessary */
153 if (real_buffer != buffer)
154 memcpy(real_buffer, buffer, buffer_size);
156 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
162 static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
164 /* We always write synchronously */
165 EFI_ENTRY("%p", this);
166 return EFI_EXIT(EFI_SUCCESS);
169 static const struct efi_block_io block_io_disk_template = {
170 .reset = &efi_disk_reset,
171 .read_blocks = &efi_disk_read_blocks,
172 .write_blocks = &efi_disk_write_blocks,
173 .flush_blocks = &efi_disk_flush_blocks,
177 * Get the simple file system protocol for a file device path.
179 * The full path provided is split into device part and into a file
180 * part. The device part is used to find the handle on which the
181 * simple file system protocol is installed.
183 * @full_path device path including device and file
184 * @return simple file system protocol
186 struct efi_simple_file_system_protocol *
187 efi_fs_from_path(struct efi_device_path *full_path)
189 struct efi_object *efiobj;
190 struct efi_handler *handler;
191 struct efi_device_path *device_path;
192 struct efi_device_path *file_path;
195 /* Split the path into a device part and a file part */
196 ret = efi_dp_split_file_path(full_path, &device_path, &file_path);
197 if (ret != EFI_SUCCESS)
199 efi_free_pool(file_path);
201 /* Get the EFI object for the partition */
202 efiobj = efi_dp_find_obj(device_path, NULL);
203 efi_free_pool(device_path);
207 /* Find the simple file system protocol */
208 ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid,
210 if (ret != EFI_SUCCESS)
213 /* Return the simple file system protocol for the partition */
214 return handler->protocol_interface;
218 * Create a handle for a partition or disk
220 * @parent parent handle
221 * @dp_parent parent device path
222 * @if_typename interface name for block device
223 * @desc internal block device
224 * @dev_index device index for block device
225 * @offset offset into disk for simple partitions
226 * @return disk object
228 static efi_status_t efi_disk_add_dev(
230 struct efi_device_path *dp_parent,
231 const char *if_typename,
232 struct blk_desc *desc,
236 struct efi_disk_obj **disk)
238 struct efi_disk_obj *diskobj;
241 /* Don't add empty devices */
243 return EFI_NOT_READY;
245 diskobj = calloc(1, sizeof(*diskobj));
247 return EFI_OUT_OF_RESOURCES;
249 /* Hook up to the device list */
250 efi_add_handle(&diskobj->parent);
252 /* Fill in object data */
254 struct efi_device_path *node = efi_dp_part_node(desc, part);
256 diskobj->dp = efi_dp_append_node(dp_parent, node);
259 diskobj->dp = efi_dp_from_part(desc, part);
261 diskobj->part = part;
262 ret = efi_add_protocol(diskobj->parent.handle, &efi_block_io_guid,
264 if (ret != EFI_SUCCESS)
266 ret = efi_add_protocol(diskobj->parent.handle, &efi_guid_device_path,
268 if (ret != EFI_SUCCESS)
271 diskobj->volume = efi_simple_file_system(desc, part,
273 ret = efi_add_protocol(diskobj->parent.handle,
274 &efi_simple_file_system_protocol_guid,
276 if (ret != EFI_SUCCESS)
279 diskobj->ops = block_io_disk_template;
280 diskobj->ifname = if_typename;
281 diskobj->dev_index = dev_index;
282 diskobj->offset = offset;
283 diskobj->desc = desc;
285 /* Fill in EFI IO Media info (for read/write callbacks) */
286 diskobj->media.removable_media = desc->removable;
287 diskobj->media.media_present = 1;
288 diskobj->media.block_size = desc->blksz;
289 diskobj->media.io_align = desc->blksz;
290 diskobj->media.last_block = desc->lba - offset;
292 diskobj->media.logical_partition = 1;
293 diskobj->ops.media = &diskobj->media;
300 * Create handles and protocols for the partitions of a block device
302 * @parent handle of the parent disk
303 * @blk_desc block device
304 * @if_typename interface type
305 * @diskid device number
306 * @pdevname device name
307 * @return number of partitions created
309 int efi_disk_create_partitions(efi_handle_t parent, struct blk_desc *desc,
310 const char *if_typename, int diskid,
311 const char *pdevname)
314 char devname[32] = { 0 }; /* dp->str is u16[32] long */
315 disk_partition_t info;
317 struct efi_device_path *dp = NULL;
319 struct efi_handler *handler;
321 /* Get the device path of the parent */
322 ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
323 if (ret == EFI_SUCCESS)
324 dp = handler->protocol_interface;
326 /* Add devices for each partition */
327 for (part = 1; part <= MAX_SEARCH_PARTITIONS; part++) {
328 if (part_get_info(desc, part, &info))
330 snprintf(devname, sizeof(devname), "%s:%d", pdevname,
332 ret = efi_disk_add_dev(parent, dp, if_typename, desc, diskid,
333 info.start, part, NULL);
334 if (ret != EFI_SUCCESS) {
335 printf("Adding partition %s failed\n", pdevname);
345 * U-Boot doesn't have a list of all online disk devices. So when running our
346 * EFI payload, we scan through all of the potentially available ones and
347 * store them in our object pool.
349 * TODO(sjg@chromium.org): Actually with CONFIG_BLK, U-Boot does have this.
350 * Consider converting the code to look up devices as needed. The EFI device
351 * could be a child of the UCLASS_BLK block device, perhaps.
353 * This gets called from do_bootefi_exec().
355 efi_status_t efi_disk_register(void)
357 struct efi_disk_obj *disk;
363 for (uclass_first_device_check(UCLASS_BLK, &dev); dev;
364 uclass_next_device_check(&dev)) {
365 struct blk_desc *desc = dev_get_uclass_platdata(dev);
366 const char *if_typename = blk_get_if_type_name(desc->if_type);
368 /* Add block device for the full device */
369 printf("Scanning disk %s...\n", dev->name);
370 ret = efi_disk_add_dev(NULL, NULL, if_typename,
371 desc, desc->devnum, 0, 0, &disk);
372 if (ret == EFI_NOT_READY) {
373 printf("Disk %s not ready\n", dev->name);
377 printf("ERROR: failure to add disk device %s, r = %lu\n",
378 dev->name, ret & ~EFI_ERROR_MASK);
383 /* Partitions show up as block devices in EFI */
384 disks += efi_disk_create_partitions(
385 disk->parent.handle, desc, if_typename,
386 desc->devnum, dev->name);
391 /* Search for all available disk devices */
392 for (if_type = 0; if_type < IF_TYPE_COUNT; if_type++) {
393 const struct blk_driver *cur_drvr;
394 const char *if_typename;
396 cur_drvr = blk_driver_lookup_type(if_type);
400 if_typename = cur_drvr->if_typename;
401 printf("Scanning disks on %s...\n", if_typename);
402 for (i = 0; i < 4; i++) {
403 struct blk_desc *desc;
404 char devname[32] = { 0 }; /* dp->str is u16[32] long */
406 desc = blk_get_devnum_by_type(if_type, i);
409 if (desc->type == DEV_TYPE_UNKNOWN)
412 snprintf(devname, sizeof(devname), "%s%d",
415 /* Add block device for the full device */
416 ret = efi_disk_add_dev(NULL, NULL, if_typename, desc,
418 if (ret == EFI_NOT_READY) {
419 printf("Disk %s not ready\n", devname);
423 printf("ERROR: failure to add disk device %s, r = %lu\n",
424 devname, ret & ~EFI_ERROR_MASK);
429 /* Partitions show up as block devices in EFI */
430 disks += efi_disk_create_partitions(
431 disk->parent.handle, desc,
432 if_typename, i, devname);
436 printf("Found %d disks\n", disks);