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 <efi_loader.h>
19 struct efi_system_partition efi_system_partition;
21 const efi_guid_t efi_block_io_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
22 const efi_guid_t efi_system_partition_guid = PARTITION_SYSTEM_GUID;
25 * struct efi_disk_obj - EFI disk object
27 * @header: EFI object header
28 * @ops: EFI disk I/O protocol interface
29 * @ifname: interface name for block device
30 * @dev_index: device index of block device
31 * @media: block I/O media information
32 * @dp: device path to the block device
34 * @volume: simple file system protocol of the partition
35 * @offset: offset into disk for simple partition
36 * @desc: internal block device descriptor
39 struct efi_object header;
40 struct efi_block_io ops;
43 struct efi_block_io_media media;
44 struct efi_device_path *dp;
46 struct efi_simple_file_system_protocol *volume;
48 struct blk_desc *desc;
52 * efi_disk_reset() - reset block device
54 * This function implements the Reset service of the EFI_BLOCK_IO_PROTOCOL.
56 * As U-Boot's block devices do not have a reset function simply return
59 * See the Unified Extensible Firmware Interface (UEFI) specification for
62 * @this: pointer to the BLOCK_IO_PROTOCOL
63 * @extended_verification: extended verification
66 static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
67 char extended_verification)
69 EFI_ENTRY("%p, %x", this, extended_verification);
70 return EFI_EXIT(EFI_SUCCESS);
73 enum efi_disk_direction {
78 static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
79 u32 media_id, u64 lba, unsigned long buffer_size,
80 void *buffer, enum efi_disk_direction direction)
82 struct efi_disk_obj *diskobj;
83 struct blk_desc *desc;
88 diskobj = container_of(this, struct efi_disk_obj, ops);
89 desc = (struct blk_desc *) diskobj->desc;
91 blocks = buffer_size / blksz;
92 lba += diskobj->offset;
94 EFI_PRINT("blocks=%x lba=%llx blksz=%x dir=%d\n",
95 blocks, lba, blksz, direction);
97 /* We only support full block access */
98 if (buffer_size & (blksz - 1))
99 return EFI_BAD_BUFFER_SIZE;
101 if (direction == EFI_DISK_READ)
102 n = blk_dread(desc, lba, blocks, buffer);
104 n = blk_dwrite(desc, lba, blocks, buffer);
106 /* We don't do interrupts, so check for timers cooperatively */
109 EFI_PRINT("n=%lx blocks=%x\n", n, blocks);
112 return EFI_DEVICE_ERROR;
118 * efi_disk_read_blocks() - reads blocks from device
120 * This function implements the ReadBlocks service of the EFI_BLOCK_IO_PROTOCOL.
122 * See the Unified Extensible Firmware Interface (UEFI) specification for
125 * @this: pointer to the BLOCK_IO_PROTOCOL
126 * @media_id: id of the medium to be read from
127 * @lba: starting logical block for reading
128 * @buffer_size: size of the read buffer
129 * @buffer: pointer to the destination buffer
130 * Return: status code
132 static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
133 u32 media_id, u64 lba, efi_uintn_t buffer_size,
136 void *real_buffer = buffer;
140 return EFI_INVALID_PARAMETER;
141 /* TODO: check for media changes */
142 if (media_id != this->media->media_id)
143 return EFI_MEDIA_CHANGED;
144 if (!this->media->media_present)
146 /* media->io_align is a power of 2 or 0 */
147 if (this->media->io_align &&
148 (uintptr_t)buffer & (this->media->io_align - 1))
149 return EFI_INVALID_PARAMETER;
150 if (lba * this->media->block_size + buffer_size >
151 (this->media->last_block + 1) * this->media->block_size)
152 return EFI_INVALID_PARAMETER;
154 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
155 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
156 r = efi_disk_read_blocks(this, media_id, lba,
157 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
158 if (r != EFI_SUCCESS)
160 return efi_disk_read_blocks(this, media_id, lba +
161 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
162 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
163 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
166 real_buffer = efi_bounce_buffer;
169 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
170 buffer_size, buffer);
172 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
175 /* Copy from bounce buffer to real buffer if necessary */
176 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
177 memcpy(buffer, real_buffer, buffer_size);
183 * efi_disk_write_blocks() - writes blocks to device
185 * This function implements the WriteBlocks service of the
186 * EFI_BLOCK_IO_PROTOCOL.
188 * See the Unified Extensible Firmware Interface (UEFI) specification for
191 * @this: pointer to the BLOCK_IO_PROTOCOL
192 * @media_id: id of the medium to be written to
193 * @lba: starting logical block for writing
194 * @buffer_size: size of the write buffer
195 * @buffer: pointer to the source buffer
196 * Return: status code
198 static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
199 u32 media_id, u64 lba, efi_uintn_t buffer_size,
202 void *real_buffer = buffer;
206 return EFI_INVALID_PARAMETER;
207 if (this->media->read_only)
208 return EFI_WRITE_PROTECTED;
209 /* TODO: check for media changes */
210 if (media_id != this->media->media_id)
211 return EFI_MEDIA_CHANGED;
212 if (!this->media->media_present)
214 /* media->io_align is a power of 2 or 0 */
215 if (this->media->io_align &&
216 (uintptr_t)buffer & (this->media->io_align - 1))
217 return EFI_INVALID_PARAMETER;
218 if (lba * this->media->block_size + buffer_size >
219 (this->media->last_block + 1) * this->media->block_size)
220 return EFI_INVALID_PARAMETER;
222 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
223 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
224 r = efi_disk_write_blocks(this, media_id, lba,
225 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
226 if (r != EFI_SUCCESS)
228 return efi_disk_write_blocks(this, media_id, lba +
229 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
230 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
231 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
234 real_buffer = efi_bounce_buffer;
237 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
238 buffer_size, buffer);
240 /* Populate bounce buffer if necessary */
241 if (real_buffer != buffer)
242 memcpy(real_buffer, buffer, buffer_size);
244 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
251 * efi_disk_flush_blocks() - flushes modified data to the device
253 * This function implements the FlushBlocks service of the
254 * EFI_BLOCK_IO_PROTOCOL.
256 * As we always write synchronously nothing is done here.
258 * See the Unified Extensible Firmware Interface (UEFI) specification for
261 * @this: pointer to the BLOCK_IO_PROTOCOL
262 * Return: status code
264 static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
266 EFI_ENTRY("%p", this);
267 return EFI_EXIT(EFI_SUCCESS);
270 static const struct efi_block_io block_io_disk_template = {
271 .reset = &efi_disk_reset,
272 .read_blocks = &efi_disk_read_blocks,
273 .write_blocks = &efi_disk_write_blocks,
274 .flush_blocks = &efi_disk_flush_blocks,
278 * efi_fs_from_path() - retrieve simple file system protocol
280 * Gets the simple file system protocol for a file device path.
282 * The full path provided is split into device part and into a file
283 * part. The device part is used to find the handle on which the
284 * simple file system protocol is installed.
286 * @full_path: device path including device and file
287 * Return: simple file system protocol
289 struct efi_simple_file_system_protocol *
290 efi_fs_from_path(struct efi_device_path *full_path)
292 struct efi_object *efiobj;
293 struct efi_handler *handler;
294 struct efi_device_path *device_path;
295 struct efi_device_path *file_path;
298 /* Split the path into a device part and a file part */
299 ret = efi_dp_split_file_path(full_path, &device_path, &file_path);
300 if (ret != EFI_SUCCESS)
302 efi_free_pool(file_path);
304 /* Get the EFI object for the partition */
305 efiobj = efi_dp_find_obj(device_path, NULL);
306 efi_free_pool(device_path);
310 /* Find the simple file system protocol */
311 ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid,
313 if (ret != EFI_SUCCESS)
316 /* Return the simple file system protocol for the partition */
317 return handler->protocol_interface;
321 * efi_fs_exists() - check if a partition bears a file system
323 * @desc: block device descriptor
324 * @part: partition number
325 * Return: 1 if a file system exists on the partition
328 static int efi_fs_exists(struct blk_desc *desc, int part)
330 if (fs_set_blk_dev_with_part(desc, part))
333 if (fs_get_type() == FS_TYPE_ANY)
342 * efi_disk_add_dev() - create a handle for a partition or disk
344 * @parent: parent handle
345 * @dp_parent: parent device path
346 * @if_typename: interface name for block device
347 * @desc: internal block device
348 * @dev_index: device index for block device
349 * @part_info: partition info
351 * @disk: pointer to receive the created handle
352 * Return: disk object
354 static efi_status_t efi_disk_add_dev(
356 struct efi_device_path *dp_parent,
357 const char *if_typename,
358 struct blk_desc *desc,
360 struct disk_partition *part_info,
362 struct efi_disk_obj **disk)
364 struct efi_disk_obj *diskobj;
365 struct efi_object *handle;
366 const efi_guid_t *guid = NULL;
369 /* Don't add empty devices */
371 return EFI_NOT_READY;
373 diskobj = calloc(1, sizeof(*diskobj));
375 return EFI_OUT_OF_RESOURCES;
377 /* Hook up to the device list */
378 efi_add_handle(&diskobj->header);
380 /* Fill in object data */
382 struct efi_device_path *node = efi_dp_part_node(desc, part);
383 struct efi_handler *handler;
384 void *protocol_interface;
386 /* Parent must expose EFI_BLOCK_IO_PROTOCOL */
387 ret = efi_search_protocol(parent, &efi_block_io_guid, &handler);
388 if (ret != EFI_SUCCESS)
392 * Link the partition (child controller) to the block device
395 ret = efi_protocol_open(handler, &protocol_interface, NULL,
397 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER);
398 if (ret != EFI_SUCCESS)
401 diskobj->dp = efi_dp_append_node(dp_parent, node);
403 diskobj->offset = part_info->start;
404 diskobj->media.last_block = part_info->size - 1;
405 if (part_info->bootable & PART_EFI_SYSTEM_PARTITION)
406 guid = &efi_system_partition_guid;
408 diskobj->dp = efi_dp_from_part(desc, part);
410 diskobj->media.last_block = desc->lba - 1;
412 diskobj->part = part;
415 * Install the device path and the block IO protocol.
417 * InstallMultipleProtocolInterfaces() checks if the device path is
418 * already installed on an other handle and returns EFI_ALREADY_STARTED
421 handle = &diskobj->header;
422 ret = EFI_CALL(efi_install_multiple_protocol_interfaces(
423 &handle, &efi_guid_device_path, diskobj->dp,
424 &efi_block_io_guid, &diskobj->ops,
426 if (ret != EFI_SUCCESS)
430 * On partitions or whole disks without partitions install the
431 * simple file system protocol if a file system is available.
433 if ((part || desc->part_type == PART_TYPE_UNKNOWN) &&
434 efi_fs_exists(desc, part)) {
435 diskobj->volume = efi_simple_file_system(desc, part,
437 ret = efi_add_protocol(&diskobj->header,
438 &efi_simple_file_system_protocol_guid,
440 if (ret != EFI_SUCCESS)
443 diskobj->ops = block_io_disk_template;
444 diskobj->ifname = if_typename;
445 diskobj->dev_index = dev_index;
446 diskobj->desc = desc;
448 /* Fill in EFI IO Media info (for read/write callbacks) */
449 diskobj->media.removable_media = desc->removable;
450 diskobj->media.media_present = 1;
452 * MediaID is just an arbitrary counter.
453 * We have to change it if the medium is removed or changed.
455 diskobj->media.media_id = 1;
456 diskobj->media.block_size = desc->blksz;
457 diskobj->media.io_align = desc->blksz;
459 diskobj->media.logical_partition = 1;
460 diskobj->ops.media = &diskobj->media;
464 EFI_PRINT("BlockIO: part %u, present %d, logical %d, removable %d"
465 ", offset " LBAF ", last_block %llu\n",
467 diskobj->media.media_present,
468 diskobj->media.logical_partition,
469 diskobj->media.removable_media,
471 diskobj->media.last_block);
473 /* Store first EFI system partition */
474 if (part && !efi_system_partition.if_type) {
475 if (part_info->bootable & PART_EFI_SYSTEM_PARTITION) {
476 efi_system_partition.if_type = desc->if_type;
477 efi_system_partition.devnum = desc->devnum;
478 efi_system_partition.part = part;
479 EFI_PRINT("EFI system partition: %s %x:%x\n",
480 blk_get_if_type_name(desc->if_type),
486 efi_delete_handle(&diskobj->header);
491 * efi_disk_create_partitions() - create handles and protocols for partitions
493 * Create handles and protocols for the partitions of a block device.
495 * @parent: handle of the parent disk
496 * @desc: block device
497 * @if_typename: interface type
498 * @diskid: device number
499 * @pdevname: device name
500 * Return: number of partitions created
502 int efi_disk_create_partitions(efi_handle_t parent, struct blk_desc *desc,
503 const char *if_typename, int diskid,
504 const char *pdevname)
507 char devname[32] = { 0 }; /* dp->str is u16[32] long */
509 struct efi_device_path *dp = NULL;
511 struct efi_handler *handler;
513 /* Get the device path of the parent */
514 ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
515 if (ret == EFI_SUCCESS)
516 dp = handler->protocol_interface;
518 /* Add devices for each partition */
519 for (part = 1; part <= MAX_SEARCH_PARTITIONS; part++) {
520 struct disk_partition info;
522 if (part_get_info(desc, part, &info))
524 snprintf(devname, sizeof(devname), "%s:%x", pdevname,
526 ret = efi_disk_add_dev(parent, dp, if_typename, desc, diskid,
528 if (ret != EFI_SUCCESS) {
529 log_err("Adding partition %s failed\n", pdevname);
539 * efi_disk_register() - register block devices
541 * U-Boot doesn't have a list of all online disk devices. So when running our
542 * EFI payload, we scan through all of the potentially available ones and
543 * store them in our object pool.
545 * This function is called in efi_init_obj_list().
548 * Consider converting the code to look up devices as needed. The EFI device
549 * could be a child of the UCLASS_BLK block device, perhaps.
551 * Return: status code
553 efi_status_t efi_disk_register(void)
555 struct efi_disk_obj *disk;
560 for (uclass_first_device_check(UCLASS_BLK, &dev); dev;
561 uclass_next_device_check(&dev)) {
562 struct blk_desc *desc = dev_get_uclass_plat(dev);
563 const char *if_typename = blk_get_if_type_name(desc->if_type);
565 /* Add block device for the full device */
566 log_info("Scanning disk %s...\n", dev->name);
567 ret = efi_disk_add_dev(NULL, NULL, if_typename,
568 desc, desc->devnum, NULL, 0, &disk);
569 if (ret == EFI_NOT_READY) {
570 log_notice("Disk %s not ready\n", dev->name);
574 log_err("ERROR: failure to add disk device %s, r = %lu\n",
575 dev->name, ret & ~EFI_ERROR_MASK);
580 /* Partitions show up as block devices in EFI */
581 disks += efi_disk_create_partitions(
582 &disk->header, desc, if_typename,
583 desc->devnum, dev->name);
586 log_info("Found %d disks\n", disks);
592 * efi_disk_is_system_part() - check if handle refers to an EFI system partition
594 * @handle: handle of partition
596 * Return: true if handle refers to an EFI system partition
598 bool efi_disk_is_system_part(efi_handle_t handle)
600 struct efi_handler *handler;
601 struct efi_disk_obj *diskobj;
602 struct disk_partition info;
606 /* check if this is a block device */
607 ret = efi_search_protocol(handle, &efi_block_io_guid, &handler);
608 if (ret != EFI_SUCCESS)
611 diskobj = container_of(handle, struct efi_disk_obj, header);
613 r = part_get_info(diskobj->desc, diskobj->part, &info);
617 return !!(info.bootable & PART_EFI_SYSTEM_PARTITION);