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