"Das U-Boot" Source Tree
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * EFI application disk support
4 *
5 * Copyright (c) 2016 Alexander Graf
6 */
7
8#define LOG_CATEGORY LOGC_EFI
9
10#include <blk.h>
11#include <dm.h>
12#include <dm/device-internal.h>
13#include <dm/tag.h>
14#include <event.h>
15#include <efi_driver.h>
16#include <efi_loader.h>
17#include <fs.h>
18#include <log.h>
19#include <part.h>
20#include <malloc.h>
21
22struct efi_system_partition efi_system_partition = {
23 .uclass_id = UCLASS_INVALID,
24};
25
26const efi_guid_t efi_block_io_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
27const efi_guid_t efi_system_partition_guid = PARTITION_SYSTEM_GUID;
28
29/**
30 * struct efi_disk_obj - EFI disk object
31 *
32 * @header: EFI object header
33 * @ops: EFI disk I/O protocol interface
34 * @media: block I/O media information
35 * @dp: device path to the block device
36 * @volume: simple file system protocol of the partition
37 */
38struct efi_disk_obj {
39 struct efi_object header;
40 struct efi_block_io ops;
41 struct efi_block_io_media media;
42 struct efi_device_path *dp;
43 struct efi_simple_file_system_protocol *volume;
44};
45
46/**
47 * efi_disk_reset() - reset block device
48 *
49 * This function implements the Reset service of the EFI_BLOCK_IO_PROTOCOL.
50 *
51 * As U-Boot's block devices do not have a reset function simply return
52 * EFI_SUCCESS.
53 *
54 * See the Unified Extensible Firmware Interface (UEFI) specification for
55 * details.
56 *
57 * @this: pointer to the BLOCK_IO_PROTOCOL
58 * @extended_verification: extended verification
59 * Return: status code
60 */
61static efi_status_t EFIAPI efi_disk_reset(struct efi_block_io *this,
62 char extended_verification)
63{
64 EFI_ENTRY("%p, %x", this, extended_verification);
65 return EFI_EXIT(EFI_SUCCESS);
66}
67
68/**
69 * efi_disk_is_removable() - check if the device is removable media
70 * @handle: efi object handle;
71 *
72 * Examine the device and determine if the device is a local block device
73 * and removable media.
74 *
75 * Return: true if removable, false otherwise
76 */
77bool efi_disk_is_removable(efi_handle_t handle)
78{
79 struct efi_handler *handler;
80 struct efi_block_io *io;
81 efi_status_t ret;
82
83 ret = efi_search_protocol(handle, &efi_block_io_guid, &handler);
84 if (ret != EFI_SUCCESS)
85 return false;
86
87 io = handler->protocol_interface;
88
89 if (!io || !io->media)
90 return false;
91
92 return (bool)io->media->removable_media;
93}
94
95enum efi_disk_direction {
96 EFI_DISK_READ,
97 EFI_DISK_WRITE,
98};
99
100static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
101 u32 media_id, u64 lba, unsigned long buffer_size,
102 void *buffer, enum efi_disk_direction direction)
103{
104 struct efi_disk_obj *diskobj;
105 int blksz;
106 int blocks;
107 unsigned long n;
108
109 diskobj = container_of(this, struct efi_disk_obj, ops);
110 blksz = diskobj->media.block_size;
111 blocks = buffer_size / blksz;
112
113 EFI_PRINT("blocks=%x lba=%llx blksz=%x dir=%d\n",
114 blocks, lba, blksz, direction);
115
116 /* We only support full block access */
117 if (buffer_size & (blksz - 1))
118 return EFI_BAD_BUFFER_SIZE;
119
120 if (CONFIG_IS_ENABLED(PARTITIONS) &&
121 device_get_uclass_id(diskobj->header.dev) == UCLASS_PARTITION) {
122 if (direction == EFI_DISK_READ)
123 n = disk_blk_read(diskobj->header.dev, lba, blocks,
124 buffer);
125 else
126 n = disk_blk_write(diskobj->header.dev, lba, blocks,
127 buffer);
128 } else {
129 /* dev is a block device (UCLASS_BLK) */
130 struct blk_desc *desc;
131
132 desc = dev_get_uclass_plat(diskobj->header.dev);
133 if (direction == EFI_DISK_READ)
134 n = blk_dread(desc, lba, blocks, buffer);
135 else
136 n = blk_dwrite(desc, lba, blocks, buffer);
137 }
138
139 /* We don't do interrupts, so check for timers cooperatively */
140 efi_timer_check();
141
142 EFI_PRINT("n=%lx blocks=%x\n", n, blocks);
143
144 if (n != blocks)
145 return EFI_DEVICE_ERROR;
146
147 return EFI_SUCCESS;
148}
149
150/**
151 * efi_disk_read_blocks() - reads blocks from device
152 *
153 * This function implements the ReadBlocks service of the EFI_BLOCK_IO_PROTOCOL.
154 *
155 * See the Unified Extensible Firmware Interface (UEFI) specification for
156 * details.
157 *
158 * @this: pointer to the BLOCK_IO_PROTOCOL
159 * @media_id: id of the medium to be read from
160 * @lba: starting logical block for reading
161 * @buffer_size: size of the read buffer
162 * @buffer: pointer to the destination buffer
163 * Return: status code
164 */
165static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
166 u32 media_id, u64 lba, efi_uintn_t buffer_size,
167 void *buffer)
168{
169 void *real_buffer = buffer;
170 efi_status_t r;
171
172 if (!this)
173 return EFI_INVALID_PARAMETER;
174 /* TODO: check for media changes */
175 if (media_id != this->media->media_id)
176 return EFI_MEDIA_CHANGED;
177 if (!this->media->media_present)
178 return EFI_NO_MEDIA;
179 /* media->io_align is a power of 2 or 0 */
180 if (this->media->io_align &&
181 (uintptr_t)buffer & (this->media->io_align - 1))
182 return EFI_INVALID_PARAMETER;
183 if (lba * this->media->block_size + buffer_size >
184 (this->media->last_block + 1) * this->media->block_size)
185 return EFI_INVALID_PARAMETER;
186
187#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
188 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
189 r = efi_disk_read_blocks(this, media_id, lba,
190 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
191 if (r != EFI_SUCCESS)
192 return r;
193 return efi_disk_read_blocks(this, media_id, lba +
194 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
195 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
196 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
197 }
198
199 real_buffer = efi_bounce_buffer;
200#endif
201
202 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
203 buffer_size, buffer);
204
205 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
206 EFI_DISK_READ);
207
208 /* Copy from bounce buffer to real buffer if necessary */
209 if ((r == EFI_SUCCESS) && (real_buffer != buffer))
210 memcpy(buffer, real_buffer, buffer_size);
211
212 return EFI_EXIT(r);
213}
214
215/**
216 * efi_disk_write_blocks() - writes blocks to device
217 *
218 * This function implements the WriteBlocks service of the
219 * EFI_BLOCK_IO_PROTOCOL.
220 *
221 * See the Unified Extensible Firmware Interface (UEFI) specification for
222 * details.
223 *
224 * @this: pointer to the BLOCK_IO_PROTOCOL
225 * @media_id: id of the medium to be written to
226 * @lba: starting logical block for writing
227 * @buffer_size: size of the write buffer
228 * @buffer: pointer to the source buffer
229 * Return: status code
230 */
231static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
232 u32 media_id, u64 lba, efi_uintn_t buffer_size,
233 void *buffer)
234{
235 void *real_buffer = buffer;
236 efi_status_t r;
237
238 if (!this)
239 return EFI_INVALID_PARAMETER;
240 if (this->media->read_only)
241 return EFI_WRITE_PROTECTED;
242 /* TODO: check for media changes */
243 if (media_id != this->media->media_id)
244 return EFI_MEDIA_CHANGED;
245 if (!this->media->media_present)
246 return EFI_NO_MEDIA;
247 /* media->io_align is a power of 2 or 0 */
248 if (this->media->io_align &&
249 (uintptr_t)buffer & (this->media->io_align - 1))
250 return EFI_INVALID_PARAMETER;
251 if (lba * this->media->block_size + buffer_size >
252 (this->media->last_block + 1) * this->media->block_size)
253 return EFI_INVALID_PARAMETER;
254
255#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
256 if (buffer_size > EFI_LOADER_BOUNCE_BUFFER_SIZE) {
257 r = efi_disk_write_blocks(this, media_id, lba,
258 EFI_LOADER_BOUNCE_BUFFER_SIZE, buffer);
259 if (r != EFI_SUCCESS)
260 return r;
261 return efi_disk_write_blocks(this, media_id, lba +
262 EFI_LOADER_BOUNCE_BUFFER_SIZE / this->media->block_size,
263 buffer_size - EFI_LOADER_BOUNCE_BUFFER_SIZE,
264 buffer + EFI_LOADER_BOUNCE_BUFFER_SIZE);
265 }
266
267 real_buffer = efi_bounce_buffer;
268#endif
269
270 EFI_ENTRY("%p, %x, %llx, %zx, %p", this, media_id, lba,
271 buffer_size, buffer);
272
273 /* Populate bounce buffer if necessary */
274 if (real_buffer != buffer)
275 memcpy(real_buffer, buffer, buffer_size);
276
277 r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
278 EFI_DISK_WRITE);
279
280 return EFI_EXIT(r);
281}
282
283/**
284 * efi_disk_flush_blocks() - flushes modified data to the device
285 *
286 * This function implements the FlushBlocks service of the
287 * EFI_BLOCK_IO_PROTOCOL.
288 *
289 * As we always write synchronously nothing is done here.
290 *
291 * See the Unified Extensible Firmware Interface (UEFI) specification for
292 * details.
293 *
294 * @this: pointer to the BLOCK_IO_PROTOCOL
295 * Return: status code
296 */
297static efi_status_t EFIAPI efi_disk_flush_blocks(struct efi_block_io *this)
298{
299 EFI_ENTRY("%p", this);
300 return EFI_EXIT(EFI_SUCCESS);
301}
302
303static const struct efi_block_io block_io_disk_template = {
304 .reset = &efi_disk_reset,
305 .read_blocks = &efi_disk_read_blocks,
306 .write_blocks = &efi_disk_write_blocks,
307 .flush_blocks = &efi_disk_flush_blocks,
308};
309
310/**
311 * efi_fs_from_path() - retrieve simple file system protocol
312 *
313 * Gets the simple file system protocol for a file device path.
314 *
315 * The full path provided is split into device part and into a file
316 * part. The device part is used to find the handle on which the
317 * simple file system protocol is installed.
318 *
319 * @full_path: device path including device and file
320 * Return: simple file system protocol
321 */
322struct efi_simple_file_system_protocol *
323efi_fs_from_path(struct efi_device_path *full_path)
324{
325 struct efi_object *efiobj;
326 struct efi_handler *handler;
327 struct efi_device_path *device_path;
328 struct efi_device_path *file_path;
329 efi_status_t ret;
330
331 /* Split the path into a device part and a file part */
332 ret = efi_dp_split_file_path(full_path, &device_path, &file_path);
333 if (ret != EFI_SUCCESS)
334 return NULL;
335 efi_free_pool(file_path);
336
337 /* Get the EFI object for the partition */
338 efiobj = efi_dp_find_obj(device_path, NULL, NULL);
339 efi_free_pool(device_path);
340 if (!efiobj)
341 return NULL;
342
343 /* Find the simple file system protocol */
344 ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid,
345 &handler);
346 if (ret != EFI_SUCCESS)
347 return NULL;
348
349 /* Return the simple file system protocol for the partition */
350 return handler->protocol_interface;
351}
352
353/**
354 * efi_fs_exists() - check if a partition bears a file system
355 *
356 * @desc: block device descriptor
357 * @part: partition number
358 * Return: 1 if a file system exists on the partition
359 * 0 otherwise
360 */
361static int efi_fs_exists(struct blk_desc *desc, int part)
362{
363 if (fs_set_blk_dev_with_part(desc, part))
364 return 0;
365
366 if (fs_get_type() == FS_TYPE_ANY)
367 return 0;
368
369 fs_close();
370
371 return 1;
372}
373
374static void efi_disk_free_diskobj(struct efi_disk_obj *diskobj)
375{
376 struct efi_device_path *dp = diskobj->dp;
377 struct efi_simple_file_system_protocol *volume = diskobj->volume;
378
379 /*
380 * ignore error of efi_delete_handle() since this function
381 * is expected to be called in error path.
382 */
383 efi_delete_handle(&diskobj->header);
384 efi_free_pool(dp);
385 free(volume);
386}
387
388/**
389 * efi_disk_add_dev() - create a handle for a partition or disk
390 *
391 * @parent: parent handle
392 * @dp_parent: parent device path
393 * @desc: internal block device
394 * @part_info: partition info
395 * @part: partition
396 * @disk: pointer to receive the created handle
397 * @agent_handle: handle of the EFI block driver
398 * Return: disk object
399 */
400static efi_status_t efi_disk_add_dev(
401 efi_handle_t parent,
402 struct efi_device_path *dp_parent,
403 struct blk_desc *desc,
404 struct disk_partition *part_info,
405 unsigned int part,
406 struct efi_disk_obj **disk,
407 efi_handle_t agent_handle)
408{
409 struct efi_disk_obj *diskobj;
410 struct efi_object *handle;
411 const efi_guid_t *esp_guid = NULL;
412 efi_status_t ret;
413
414 /* Don't add empty devices */
415 if (!desc->lba)
416 return EFI_NOT_READY;
417
418 diskobj = calloc(1, sizeof(*diskobj));
419 if (!diskobj)
420 return EFI_OUT_OF_RESOURCES;
421
422 /* Hook up to the device list */
423 efi_add_handle(&diskobj->header);
424
425 /* Fill in object data */
426 if (part_info) {
427 struct efi_device_path *node = efi_dp_part_node(desc, part);
428 struct efi_handler *handler;
429 void *protocol_interface;
430
431 if (!node) {
432 ret = EFI_OUT_OF_RESOURCES;
433 log_debug("no node\n");
434 goto error;
435 }
436
437 /* Parent must expose EFI_BLOCK_IO_PROTOCOL */
438 ret = efi_search_protocol(parent, &efi_block_io_guid, &handler);
439 if (ret != EFI_SUCCESS) {
440 log_debug("search failed\n");
441 goto error;
442 }
443
444 /*
445 * Link the partition (child controller) to the block device
446 * (controller).
447 */
448 ret = efi_protocol_open(handler, &protocol_interface, NULL,
449 &diskobj->header,
450 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER);
451 if (ret != EFI_SUCCESS) {
452 log_debug("prot open failed\n");
453 goto error;
454 }
455
456 diskobj->dp = efi_dp_append_node(dp_parent, node);
457 efi_free_pool(node);
458 diskobj->media.last_block = part_info->size - 1;
459 if (part_info->bootable & PART_EFI_SYSTEM_PARTITION)
460 esp_guid = &efi_system_partition_guid;
461 } else {
462 diskobj->dp = efi_dp_from_part(desc, part);
463 diskobj->media.last_block = desc->lba - 1;
464 }
465
466 /*
467 * Install the device path and the block IO protocol.
468 *
469 * InstallMultipleProtocolInterfaces() checks if the device path is
470 * already installed on an other handle and returns EFI_ALREADY_STARTED
471 * in this case.
472 */
473 handle = &diskobj->header;
474 ret = efi_install_multiple_protocol_interfaces(
475 &handle,
476 &efi_guid_device_path, diskobj->dp,
477 &efi_block_io_guid, &diskobj->ops,
478 /*
479 * esp_guid must be last entry as it
480 * can be NULL. Its interface is NULL.
481 */
482 esp_guid, NULL,
483 NULL);
484 if (ret != EFI_SUCCESS) {
485 log_debug("install failed %lx\n", ret);
486 goto error;
487 }
488
489 /*
490 * On partitions or whole disks without partitions install the
491 * simple file system protocol if a file system is available.
492 */
493 if ((part || desc->part_type == PART_TYPE_UNKNOWN) &&
494 efi_fs_exists(desc, part)) {
495 ret = efi_create_simple_file_system(desc, part, diskobj->dp,
496 &diskobj->volume);
497 if (ret != EFI_SUCCESS)
498 goto error;
499
500 ret = efi_add_protocol(&diskobj->header,
501 &efi_simple_file_system_protocol_guid,
502 diskobj->volume);
503 if (ret != EFI_SUCCESS)
504 goto error;
505 }
506 diskobj->ops = block_io_disk_template;
507
508 /* Fill in EFI IO Media info (for read/write callbacks) */
509 diskobj->media.removable_media = desc->removable;
510 diskobj->media.media_present = 1;
511 /*
512 * MediaID is just an arbitrary counter.
513 * We have to change it if the medium is removed or changed.
514 */
515 diskobj->media.media_id = 1;
516 diskobj->media.block_size = desc->blksz;
517 diskobj->media.io_align = desc->blksz;
518 if (part)
519 diskobj->media.logical_partition = 1;
520 diskobj->ops.media = &diskobj->media;
521 if (disk)
522 *disk = diskobj;
523
524 EFI_PRINT("BlockIO: part %u, present %d, logical %d, removable %d"
525 ", last_block %llu\n",
526 part,
527 diskobj->media.media_present,
528 diskobj->media.logical_partition,
529 diskobj->media.removable_media,
530 diskobj->media.last_block);
531
532 /* Store first EFI system partition */
533 if (part && efi_system_partition.uclass_id == UCLASS_INVALID) {
534 if (part_info &&
535 part_info->bootable & PART_EFI_SYSTEM_PARTITION) {
536 efi_system_partition.uclass_id = desc->uclass_id;
537 efi_system_partition.devnum = desc->devnum;
538 efi_system_partition.part = part;
539 EFI_PRINT("EFI system partition: %s %x:%x\n",
540 blk_get_uclass_name(desc->uclass_id),
541 desc->devnum, part);
542 }
543 }
544 return EFI_SUCCESS;
545error:
546 efi_disk_free_diskobj(diskobj);
547 return ret;
548}
549
550/**
551 * efi_disk_create_raw() - create a handle for a whole raw disk
552 *
553 * @dev: udevice (UCLASS_BLK)
554 * @agent_handle: handle of the EFI block driver
555 *
556 * Create an efi_disk object which is associated with @dev.
557 * The type of @dev must be UCLASS_BLK.
558 *
559 * Return: 0 on success, -1 otherwise
560 */
561static int efi_disk_create_raw(struct udevice *dev, efi_handle_t agent_handle)
562{
563 struct efi_disk_obj *disk;
564 struct blk_desc *desc;
565 efi_status_t ret;
566
567 desc = dev_get_uclass_plat(dev);
568
569 ret = efi_disk_add_dev(NULL, NULL, desc,
570 NULL, 0, &disk, agent_handle);
571 if (ret != EFI_SUCCESS) {
572 if (ret == EFI_NOT_READY) {
573 log_notice("Disk %s not ready\n", dev->name);
574 ret = -EBUSY;
575 } else {
576 log_err("Adding block device %s failed, r = %lu\n",
577 dev->name, ret & ~EFI_ERROR_MASK);
578 ret = -ENOENT;
579 }
580
581 return ret;
582 }
583 if (efi_link_dev(&disk->header, dev)) {
584 efi_disk_free_diskobj(disk);
585
586 return -EINVAL;
587 }
588
589 return 0;
590}
591
592/**
593 * efi_disk_create_part() - create a handle for a disk partition
594 *
595 * @dev: udevice (UCLASS_PARTITION)
596 * @agent_handle: handle of the EFI block driver
597 *
598 * Create an efi_disk object which is associated with @dev.
599 * The type of @dev must be UCLASS_PARTITION.
600 *
601 * Return: 0 on success, -1 otherwise
602 */
603static int efi_disk_create_part(struct udevice *dev, efi_handle_t agent_handle)
604{
605 efi_handle_t parent;
606 struct blk_desc *desc;
607 struct disk_part *part_data;
608 struct disk_partition *info;
609 unsigned int part;
610 struct efi_handler *handler;
611 struct efi_device_path *dp_parent;
612 struct efi_disk_obj *disk;
613 efi_status_t ret;
614
615 if (dev_tag_get_ptr(dev_get_parent(dev), DM_TAG_EFI, (void **)&parent))
616 return -1;
617
618 desc = dev_get_uclass_plat(dev_get_parent(dev));
619
620 part_data = dev_get_uclass_plat(dev);
621 part = part_data->partnum;
622 info = &part_data->gpt_part_info;
623
624 ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
625 if (ret != EFI_SUCCESS)
626 return -1;
627 dp_parent = (struct efi_device_path *)handler->protocol_interface;
628
629 ret = efi_disk_add_dev(parent, dp_parent, desc,
630 info, part, &disk, agent_handle);
631 if (ret != EFI_SUCCESS) {
632 log_err("Adding partition for %s failed\n", dev->name);
633 return -1;
634 }
635 if (efi_link_dev(&disk->header, dev)) {
636 efi_disk_free_diskobj(disk);
637
638 /* TODO: closing the parent EFI_BLOCK_IO_PROTOCOL is missing. */
639
640 return -1;
641 }
642
643 return 0;
644}
645
646/**
647 * efi_disk_probe() - create efi_disk objects for a block device
648 *
649 * @ctx: event context - driver binding protocol
650 * @event: EV_PM_POST_PROBE event
651 *
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.
656 *
657 * Return: 0 on success, -1 otherwise
658 */
659int efi_disk_probe(void *ctx, struct event *event)
660{
661 struct udevice *dev;
662 enum uclass_id id;
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;
667 int ret;
668
669 dev = event->data.dm.dev;
670 id = device_get_uclass_id(dev);
671
672 /* We won't support partitions in a partition */
673 if (id != UCLASS_BLK)
674 return 0;
675
676 /*
677 * Avoid creating duplicated objects now that efi_driver
678 * has already created an efi_disk at this moment.
679 */
680 desc = dev_get_uclass_plat(dev);
681 if (desc->uclass_id != UCLASS_EFI_LOADER) {
682 ret = efi_disk_create_raw(dev, agent_handle);
683 if (ret)
684 return -1;
685 }
686
687 device_foreach_child(child, dev) {
688 ret = efi_disk_create_part(child, agent_handle);
689 if (ret)
690 return -1;
691 }
692
693 /* only do the boot option management when UEFI sub-system is initialized */
694 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR) && efi_obj_list_initialized == EFI_SUCCESS) {
695 ret = efi_bootmgr_update_media_device_boot_option();
696 if (ret != EFI_SUCCESS)
697 return -1;
698 }
699
700 return 0;
701}
702
703/**
704 * efi_disk_remove - delete an efi_disk object for a block device or partition
705 *
706 * @ctx: event context: driver binding protocol
707 * @event: EV_PM_PRE_REMOVE event
708 *
709 * Delete an efi_disk object which is associated with the UCLASS_BLK or
710 * UCLASS_PARTITION device for which the EV_PM_PRE_REMOVE event is raised.
711 *
712 * Return: 0 on success, -1 otherwise
713 */
714int efi_disk_remove(void *ctx, struct event *event)
715{
716 enum uclass_id id;
717 struct udevice *dev = event->data.dm.dev;
718 efi_handle_t handle;
719 struct blk_desc *desc;
720 struct efi_device_path *dp = NULL;
721 struct efi_disk_obj *diskobj = NULL;
722 struct efi_simple_file_system_protocol *volume = NULL;
723 efi_status_t ret;
724
725 if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle))
726 return 0;
727
728 id = device_get_uclass_id(dev);
729 switch (id) {
730 case UCLASS_BLK:
731 desc = dev_get_uclass_plat(dev);
732 if (desc && desc->uclass_id == UCLASS_EFI_LOADER)
733 /*
734 * EFI application/driver manages the EFI handle,
735 * no need to delete EFI handle.
736 */
737 return 0;
738
739 diskobj = (struct efi_disk_obj *)handle;
740 break;
741 case UCLASS_PARTITION:
742 diskobj = (struct efi_disk_obj *)handle;
743
744 /* TODO: closing the parent EFI_BLOCK_IO_PROTOCOL is missing. */
745
746 break;
747 default:
748 return 0;
749 }
750
751 dp = diskobj->dp;
752 volume = diskobj->volume;
753
754 ret = efi_delete_handle(handle);
755 /* Do not delete DM device if there are still EFI drivers attached. */
756 if (ret != EFI_SUCCESS)
757 return -1;
758
759 efi_free_pool(dp);
760 free(volume);
761 dev_tag_del(dev, DM_TAG_EFI);
762
763 return 0;
764
765 /*
766 * TODO A flag to distinguish below 2 different scenarios of this
767 * function call is needed:
768 * a) Unplugging of a removable media under U-Boot
769 * b) U-Boot exiting and booting an OS
770 * In case of scenario a), efi_bootmgr_update_media_device_boot_option()
771 * needs to be invoked here to update the boot options and remove the
772 * unnecessary ones.
773 */
774
775}
776
777/**
778 * efi_disk_get_device_name() - get U-Boot device name associated with EFI handle
779 *
780 * @handle: pointer to the EFI handle
781 * @buf: pointer to the buffer to store the string
782 * @size: size of buffer
783 * Return: status code
784 */
785efi_status_t efi_disk_get_device_name(const efi_handle_t handle, char *buf, int size)
786{
787 int count;
788 int diskid;
789 enum uclass_id id;
790 unsigned int part;
791 struct udevice *dev;
792 struct blk_desc *desc;
793 const char *if_typename;
794 bool is_partition = false;
795 struct disk_part *part_data;
796
797 if (!handle || !buf || !size)
798 return EFI_INVALID_PARAMETER;
799
800 dev = handle->dev;
801 id = device_get_uclass_id(dev);
802 if (id == UCLASS_BLK) {
803 desc = dev_get_uclass_plat(dev);
804 } else if (id == UCLASS_PARTITION) {
805 desc = dev_get_uclass_plat(dev_get_parent(dev));
806 is_partition = true;
807 } else {
808 return EFI_INVALID_PARAMETER;
809 }
810 if_typename = blk_get_uclass_name(desc->uclass_id);
811 diskid = desc->devnum;
812
813 if (is_partition) {
814 part_data = dev_get_uclass_plat(dev);
815 part = part_data->partnum;
816 count = snprintf(buf, size, "%s %d:%u", if_typename, diskid,
817 part);
818 } else {
819 count = snprintf(buf, size, "%s %d", if_typename, diskid);
820 }
821
822 if (count < 0 || (count + 1) > size)
823 return EFI_INVALID_PARAMETER;
824
825 return EFI_SUCCESS;
826}
827
828/**
829 * efi_disks_register() - ensure all block devices are available in UEFI
830 *
831 * The function probes all block devices. As we store UEFI variables on the
832 * EFI system partition this function has to be called before enabling
833 * variable services.
834 */
835efi_status_t efi_disks_register(void)
836{
837 struct udevice *dev;
838
839 uclass_foreach_dev_probe(UCLASS_BLK, dev) {
840 }
841
842 return EFI_SUCCESS;
843}