1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application disk support
5 * Copyright (c) 2016 Alexander Graf
8 #define LOG_CATEGORY LOGC_EFI
13 #include <dm/device-internal.h>
16 #include <efi_driver.h>
17 #include <efi_loader.h>
23 struct efi_system_partition efi_system_partition = {
24 .uclass_id = UCLASS_INVALID,
27 const efi_guid_t efi_block_io_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
28 const efi_guid_t efi_system_partition_guid = PARTITION_SYSTEM_GUID;
31 * struct efi_disk_obj - EFI disk object
33 * @header: EFI object header
34 * @ops: EFI disk I/O protocol interface
35 * @dev_index: device index of block device
36 * @media: block I/O media information
37 * @dp: device path to the block device
39 * @volume: simple file system protocol of the partition
40 * @dev: associated DM device
43 struct efi_object header;
44 struct efi_block_io ops;
46 struct efi_block_io_media media;
47 struct efi_device_path *dp;
49 struct efi_simple_file_system_protocol *volume;
53 * efi_disk_reset() - reset block device
55 * This function implements the Reset service of the EFI_BLOCK_IO_PROTOCOL.
57 * As U-Boot's block devices do not have a reset function simply return
60 * See the Unified Extensible Firmware Interface (UEFI) specification for
63 * @this: pointer to the BLOCK_IO_PROTOCOL
64 * @extended_verification: extended verification
67 static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
68 char extended_verification)
70 EFI_ENTRY("%p, %x", this, extended_verification);
71 return EFI_EXIT(EFI_SUCCESS);
75 * efi_disk_is_removable() - check if the device is removable media
76 * @handle: efi object handle;
78 * Examine the device and determine if the device is a local block device
79 * and removable media.
81 * Return: true if removable, false otherwise
83 bool efi_disk_is_removable(efi_handle_t handle)
85 struct efi_handler *handler;
86 struct efi_block_io *io;
89 ret = efi_search_protocol(handle, &efi_block_io_guid, &handler);
90 if (ret != EFI_SUCCESS)
93 io = handler->protocol_interface;
95 if (!io || !io->media)
98 return (bool)io->media->removable_media;
101 enum efi_disk_direction {
106 static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
107 u32 media_id, u64 lba, unsigned long buffer_size,
108 void *buffer, enum efi_disk_direction direction)
110 struct efi_disk_obj *diskobj;
115 diskobj = container_of(this, struct efi_disk_obj, ops);
116 blksz = diskobj->media.block_size;
117 blocks = buffer_size / blksz;
119 EFI_PRINT("blocks=%x lba=%llx blksz=%x dir=%d\n",
120 blocks, lba, blksz, direction);
122 /* We only support full block access */
123 if (buffer_size & (blksz - 1))
124 return EFI_BAD_BUFFER_SIZE;
126 if (CONFIG_IS_ENABLED(PARTITIONS) &&
127 device_get_uclass_id(diskobj->header.dev) == UCLASS_PARTITION) {
128 if (direction == EFI_DISK_READ)
129 n = disk_blk_read(diskobj->header.dev, lba, blocks,
132 n = disk_blk_write(diskobj->header.dev, lba, blocks,
135 /* dev is a block device (UCLASS_BLK) */
136 struct blk_desc *desc;
138 desc = dev_get_uclass_plat(diskobj->header.dev);
139 if (direction == EFI_DISK_READ)
140 n = blk_dread(desc, lba, blocks, buffer);
142 n = blk_dwrite(desc, lba, blocks, buffer);
145 /* We don't do interrupts, so check for timers cooperatively */
148 EFI_PRINT("n=%lx blocks=%x\n", n, blocks);
151 return EFI_DEVICE_ERROR;
157 * efi_disk_read_blocks() - reads blocks from device
159 * This function implements the ReadBlocks service of the EFI_BLOCK_IO_PROTOCOL.
161 * See the Unified Extensible Firmware Interface (UEFI) specification for
164 * @this: pointer to the BLOCK_IO_PROTOCOL
165 * @media_id: id of the medium to be read from
166 * @lba: starting logical block for reading
167 * @buffer_size: size of the read buffer
168 * @buffer: pointer to the destination buffer
169 * Return: status code
171 static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
172 u32 media_id, u64 lba, efi_uintn_t buffer_size,
175 void *real_buffer = buffer;
179 return EFI_INVALID_PARAMETER;
180 /* TODO: check for media changes */
181 if (media_id != this->media->media_id)
182 return EFI_MEDIA_CHANGED;
183 if (!this->media->media_present)
185 /* media->io_align is a power of 2 or 0 */
186 if (this->media->io_align &&
187 (uintptr_t)buffer & (this->media->io_align - 1))
188 return EFI_INVALID_PARAMETER;
189 if (lba * this->media->block_size + buffer_size >
190 (this->media->last_block + 1) * this->media->block_size)
191 return EFI_INVALID_PARAMETER;
193 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
194 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
195 r = efi_disk_read_blocks(this, media_id, lba,
196 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
197 if (r != EFI_SUCCESS)
199 return efi_disk_read_blocks(this, media_id, lba +
200 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
201 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
202 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
205 real_buffer = efi_bounce_buffer;
208 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
209 buffer_size, buffer);
211 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
214 /* Copy from bounce buffer to real buffer if necessary */
215 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
216 memcpy(buffer, real_buffer, buffer_size);
222 * efi_disk_write_blocks() - writes blocks to device
224 * This function implements the WriteBlocks service of the
225 * EFI_BLOCK_IO_PROTOCOL.
227 * See the Unified Extensible Firmware Interface (UEFI) specification for
230 * @this: pointer to the BLOCK_IO_PROTOCOL
231 * @media_id: id of the medium to be written to
232 * @lba: starting logical block for writing
233 * @buffer_size: size of the write buffer
234 * @buffer: pointer to the source buffer
235 * Return: status code
237 static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
238 u32 media_id, u64 lba, efi_uintn_t buffer_size,
241 void *real_buffer = buffer;
245 return EFI_INVALID_PARAMETER;
246 if (this->media->read_only)
247 return EFI_WRITE_PROTECTED;
248 /* TODO: check for media changes */
249 if (media_id != this->media->media_id)
250 return EFI_MEDIA_CHANGED;
251 if (!this->media->media_present)
253 /* media->io_align is a power of 2 or 0 */
254 if (this->media->io_align &&
255 (uintptr_t)buffer & (this->media->io_align - 1))
256 return EFI_INVALID_PARAMETER;
257 if (lba * this->media->block_size + buffer_size >
258 (this->media->last_block + 1) * this->media->block_size)
259 return EFI_INVALID_PARAMETER;
261 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
262 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
263 r = efi_disk_write_blocks(this, media_id, lba,
264 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
265 if (r != EFI_SUCCESS)
267 return efi_disk_write_blocks(this, media_id, lba +
268 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
269 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
270 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
273 real_buffer = efi_bounce_buffer;
276 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
277 buffer_size, buffer);
279 /* Populate bounce buffer if necessary */
280 if (real_buffer != buffer)
281 memcpy(real_buffer, buffer, buffer_size);
283 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
290 * efi_disk_flush_blocks() - flushes modified data to the device
292 * This function implements the FlushBlocks service of the
293 * EFI_BLOCK_IO_PROTOCOL.
295 * As we always write synchronously nothing is done here.
297 * See the Unified Extensible Firmware Interface (UEFI) specification for
300 * @this: pointer to the BLOCK_IO_PROTOCOL
301 * Return: status code
303 static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
305 EFI_ENTRY("%p", this);
306 return EFI_EXIT(EFI_SUCCESS);
309 static const struct efi_block_io block_io_disk_template = {
310 .reset = &efi_disk_reset,
311 .read_blocks = &efi_disk_read_blocks,
312 .write_blocks = &efi_disk_write_blocks,
313 .flush_blocks = &efi_disk_flush_blocks,
317 * efi_fs_from_path() - retrieve simple file system protocol
319 * Gets the simple file system protocol for a file device path.
321 * The full path provided is split into device part and into a file
322 * part. The device part is used to find the handle on which the
323 * simple file system protocol is installed.
325 * @full_path: device path including device and file
326 * Return: simple file system protocol
328 struct efi_simple_file_system_protocol *
329 efi_fs_from_path(struct efi_device_path *full_path)
331 struct efi_object *efiobj;
332 struct efi_handler *handler;
333 struct efi_device_path *device_path;
334 struct efi_device_path *file_path;
337 /* Split the path into a device part and a file part */
338 ret = efi_dp_split_file_path(full_path, &device_path, &file_path);
339 if (ret != EFI_SUCCESS)
341 efi_free_pool(file_path);
343 /* Get the EFI object for the partition */
344 efiobj = efi_dp_find_obj(device_path, NULL, NULL);
345 efi_free_pool(device_path);
349 /* Find the simple file system protocol */
350 ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid,
352 if (ret != EFI_SUCCESS)
355 /* Return the simple file system protocol for the partition */
356 return handler->protocol_interface;
360 * efi_fs_exists() - check if a partition bears a file system
362 * @desc: block device descriptor
363 * @part: partition number
364 * Return: 1 if a file system exists on the partition
367 static int efi_fs_exists(struct blk_desc *desc, int part)
369 if (fs_set_blk_dev_with_part(desc, part))
372 if (fs_get_type() == FS_TYPE_ANY)
381 * efi_disk_add_dev() - create a handle for a partition or disk
383 * @parent: parent handle
384 * @dp_parent: parent device path
385 * @desc: internal block device
386 * @dev_index: device index for block device
387 * @part_info: partition info
389 * @disk: pointer to receive the created handle
390 * @agent_handle: handle of the EFI block driver
391 * Return: disk object
393 static efi_status_t efi_disk_add_dev(
395 struct efi_device_path *dp_parent,
396 struct blk_desc *desc,
398 struct disk_partition *part_info,
400 struct efi_disk_obj **disk,
401 efi_handle_t agent_handle)
403 struct efi_disk_obj *diskobj;
404 struct efi_object *handle;
405 const efi_guid_t *esp_guid = NULL;
408 /* Don't add empty devices */
410 return EFI_NOT_READY;
412 diskobj = calloc(1, sizeof(*diskobj));
414 return EFI_OUT_OF_RESOURCES;
416 /* Hook up to the device list */
417 efi_add_handle(&diskobj->header);
419 /* Fill in object data */
421 struct efi_device_path *node = efi_dp_part_node(desc, part);
422 struct efi_handler *handler;
423 void *protocol_interface;
426 ret = EFI_OUT_OF_RESOURCES;
427 log_debug("no node\n");
431 /* Parent must expose EFI_BLOCK_IO_PROTOCOL */
432 ret = efi_search_protocol(parent, &efi_block_io_guid, &handler);
433 if (ret != EFI_SUCCESS) {
434 log_debug("search failed\n");
439 * Link the partition (child controller) to the block device
442 ret = efi_protocol_open(handler, &protocol_interface, NULL,
444 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER);
445 if (ret != EFI_SUCCESS) {
446 log_debug("prot open failed\n");
450 diskobj->dp = efi_dp_append_node(dp_parent, node);
452 diskobj->media.last_block = part_info->size - 1;
453 if (part_info->bootable & PART_EFI_SYSTEM_PARTITION)
454 esp_guid = &efi_system_partition_guid;
456 diskobj->dp = efi_dp_from_part(desc, part);
457 diskobj->media.last_block = desc->lba - 1;
459 diskobj->part = part;
462 * Install the device path and the block IO protocol.
464 * InstallMultipleProtocolInterfaces() checks if the device path is
465 * already installed on an other handle and returns EFI_ALREADY_STARTED
468 handle = &diskobj->header;
469 ret = efi_install_multiple_protocol_interfaces(
471 &efi_guid_device_path, diskobj->dp,
472 &efi_block_io_guid, &diskobj->ops,
474 * esp_guid must be last entry as it
475 * can be NULL. Its interface is NULL.
479 if (ret != EFI_SUCCESS) {
480 log_debug("install failed %lx\n", ret);
485 * On partitions or whole disks without partitions install the
486 * simple file system protocol if a file system is available.
488 if ((part || desc->part_type == PART_TYPE_UNKNOWN) &&
489 efi_fs_exists(desc, part)) {
490 ret = efi_create_simple_file_system(desc, part, diskobj->dp,
492 if (ret != EFI_SUCCESS)
495 ret = efi_add_protocol(&diskobj->header,
496 &efi_simple_file_system_protocol_guid,
498 if (ret != EFI_SUCCESS)
501 diskobj->ops = block_io_disk_template;
502 diskobj->dev_index = dev_index;
504 /* Fill in EFI IO Media info (for read/write callbacks) */
505 diskobj->media.removable_media = desc->removable;
506 diskobj->media.media_present = 1;
508 * MediaID is just an arbitrary counter.
509 * We have to change it if the medium is removed or changed.
511 diskobj->media.media_id = 1;
512 diskobj->media.block_size = desc->blksz;
513 diskobj->media.io_align = desc->blksz;
515 diskobj->media.logical_partition = 1;
516 diskobj->ops.media = &diskobj->media;
520 EFI_PRINT("BlockIO: part %u, present %d, logical %d, removable %d"
521 ", last_block %llu\n",
523 diskobj->media.media_present,
524 diskobj->media.logical_partition,
525 diskobj->media.removable_media,
526 diskobj->media.last_block);
528 /* Store first EFI system partition */
529 if (part && efi_system_partition.uclass_id == UCLASS_INVALID) {
530 if (part_info->bootable & PART_EFI_SYSTEM_PARTITION) {
531 efi_system_partition.uclass_id = desc->uclass_id;
532 efi_system_partition.devnum = desc->devnum;
533 efi_system_partition.part = part;
534 EFI_PRINT("EFI system partition: %s %x:%x\n",
535 blk_get_uclass_name(desc->uclass_id),
541 efi_delete_handle(&diskobj->header);
542 free(diskobj->volume);
548 * efi_disk_create_raw() - create a handle for a whole raw disk
550 * @dev: udevice (UCLASS_BLK)
551 * @agent_handle: handle of the EFI block driver
553 * Create an efi_disk object which is associated with @dev.
554 * The type of @dev must be UCLASS_BLK.
556 * Return: 0 on success, -1 otherwise
558 static int efi_disk_create_raw(struct udevice *dev, efi_handle_t agent_handle)
560 struct efi_disk_obj *disk;
561 struct blk_desc *desc;
565 desc = dev_get_uclass_plat(dev);
566 diskid = desc->devnum;
568 ret = efi_disk_add_dev(NULL, NULL, desc,
569 diskid, NULL, 0, &disk, agent_handle);
570 if (ret != EFI_SUCCESS) {
571 if (ret == EFI_NOT_READY) {
572 log_notice("Disk %s not ready\n", dev->name);
575 log_err("Adding disk for %s failed (err=%ld/%#lx)\n", dev->name, ret, ret);
581 if (efi_link_dev(&disk->header, dev)) {
582 efi_free_pool(disk->dp);
583 efi_delete_handle(&disk->header);
592 * efi_disk_create_part() - create a handle for a disk partition
594 * @dev: udevice (UCLASS_PARTITION)
595 * @agent_handle: handle of the EFI block driver
597 * Create an efi_disk object which is associated with @dev.
598 * The type of @dev must be UCLASS_PARTITION.
600 * Return: 0 on success, -1 otherwise
602 static int efi_disk_create_part(struct udevice *dev, efi_handle_t agent_handle)
605 struct blk_desc *desc;
606 struct disk_part *part_data;
607 struct disk_partition *info;
610 struct efi_handler *handler;
611 struct efi_device_path *dp_parent;
612 struct efi_disk_obj *disk;
615 if (dev_tag_get_ptr(dev_get_parent(dev), DM_TAG_EFI, (void **)&parent))
618 desc = dev_get_uclass_plat(dev_get_parent(dev));
619 diskid = desc->devnum;
621 part_data = dev_get_uclass_plat(dev);
622 part = part_data->partnum;
623 info = &part_data->gpt_part_info;
625 ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
626 if (ret != EFI_SUCCESS)
628 dp_parent = (struct efi_device_path *)handler->protocol_interface;
630 ret = efi_disk_add_dev(parent, dp_parent, desc, diskid,
631 info, part, &disk, agent_handle);
632 if (ret != EFI_SUCCESS) {
633 log_err("Adding partition for %s failed\n", dev->name);
636 if (efi_link_dev(&disk->header, dev)) {
637 efi_free_pool(disk->dp);
638 efi_delete_handle(&disk->header);
647 * efi_disk_probe() - create efi_disk objects for a block device
649 * @ctx: event context - driver binding protocol
650 * @event: EV_PM_POST_PROBE event
652 * Create efi_disk objects for partitions as well as a raw disk
653 * which is associated with @dev.
654 * The type of @dev must be UCLASS_BLK.
655 * This function is expected to be called at EV_PM_POST_PROBE.
657 * Return: 0 on success, -1 otherwise
659 int efi_disk_probe(void *ctx, struct event *event)
663 struct blk_desc *desc;
664 struct udevice *child;
665 struct efi_driver_binding_extended_protocol *db_prot = ctx;
666 efi_handle_t agent_handle = db_prot->bp.driver_binding_handle;
669 dev = event->data.dm.dev;
670 id = device_get_uclass_id(dev);
672 /* We won't support partitions in a partition */
673 if (id != UCLASS_BLK)
677 * Avoid creating duplicated objects now that efi_driver
678 * has already created an efi_disk at this moment.
680 desc = dev_get_uclass_plat(dev);
681 if (desc->uclass_id != UCLASS_EFI_LOADER) {
682 ret = efi_disk_create_raw(dev, agent_handle);
687 device_foreach_child(child, dev) {
688 ret = efi_disk_create_part(child, agent_handle);
697 * efi_disk_remove - delete an efi_disk object for a block device or partition
699 * @ctx: event context: driver binding protocol
700 * @event: EV_PM_PRE_REMOVE event
702 * Delete an efi_disk object which is associated with the UCLASS_BLK or
703 * UCLASS_PARTITION device for which the EV_PM_PRE_REMOVE event is raised.
705 * Return: 0 on success, -1 otherwise
707 int efi_disk_remove(void *ctx, struct event *event)
710 struct udevice *dev = event->data.dm.dev;
712 struct blk_desc *desc;
713 struct efi_disk_obj *diskobj = NULL;
716 if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle))
719 id = device_get_uclass_id(dev);
722 desc = dev_get_uclass_plat(dev);
723 if (desc && desc->uclass_id != UCLASS_EFI_LOADER)
724 diskobj = container_of(handle, struct efi_disk_obj,
727 case UCLASS_PARTITION:
728 diskobj = container_of(handle, struct efi_disk_obj, header);
734 ret = efi_delete_handle(handle);
735 /* Do not delete DM device if there are still EFI drivers attached. */
736 if (ret != EFI_SUCCESS)
740 efi_free_pool(diskobj->dp);
742 dev_tag_del(dev, DM_TAG_EFI);
748 * efi_disk_get_device_name() - get U-Boot device name associated with EFI handle
750 * @handle: pointer to the EFI handle
751 * @buf: pointer to the buffer to store the string
752 * @size: size of buffer
753 * Return: status code
755 efi_status_t efi_disk_get_device_name(const efi_handle_t handle, char *buf, int size)
762 struct blk_desc *desc;
763 const char *if_typename;
764 bool is_partition = false;
765 struct disk_part *part_data;
767 if (!handle || !buf || !size)
768 return EFI_INVALID_PARAMETER;
771 id = device_get_uclass_id(dev);
772 if (id == UCLASS_BLK) {
773 desc = dev_get_uclass_plat(dev);
774 } else if (id == UCLASS_PARTITION) {
775 desc = dev_get_uclass_plat(dev_get_parent(dev));
778 return EFI_INVALID_PARAMETER;
780 if_typename = blk_get_uclass_name(desc->uclass_id);
781 diskid = desc->devnum;
784 part_data = dev_get_uclass_plat(dev);
785 part = part_data->partnum;
786 count = snprintf(buf, size, "%s %d:%u", if_typename, diskid,
789 count = snprintf(buf, size, "%s %d", if_typename, diskid);
792 if (count < 0 || (count + 1) > size)
793 return EFI_INVALID_PARAMETER;
799 * efi_disks_register() - ensure all block devices are available in UEFI
801 * The function probes all block devices. As we store UEFI variables on the
802 * EFI system partition this function has to be called before enabling
805 efi_status_t efi_disks_register(void)
809 uclass_foreach_dev_probe(UCLASS_BLK, dev) {