summaryrefslogtreecommitdiffstats
path: root/src/interface/efi/efi_service.c
blob: de73536522404d40b70a22ffb98094ca6a699632 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * Copyright (C) 2024 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * You can also choose to distribute this program under the terms of
 * the Unmodified Binary Distribution Licence (as given in the file
 * COPYING.UBDL), provided that you have satisfied its requirements.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

/** @file
 *
 * EFI service binding
 *
 */

#include <string.h>
#include <errno.h>
#include <ipxe/efi/efi.h>
#include <ipxe/efi/efi_service.h>
#include <ipxe/efi/Protocol/ServiceBinding.h>

/**
 * Add service to child handle
 *
 * @v service		Service binding handle
 * @v binding		Service binding protocol GUID
 * @v handle		Handle on which to install child
 * @ret rc		Return status code
 */
int efi_service_add ( EFI_HANDLE service, EFI_GUID *binding,
		      EFI_HANDLE *handle ) {
	union {
		EFI_SERVICE_BINDING_PROTOCOL *sb;
		void *interface;
	} u;
	EFI_STATUS efirc;
	int rc;

	/* Open service binding protocol */
	if ( ( rc = efi_open ( service, binding, &u.interface ) ) != 0 ) {
		DBGC ( service, "EFISVC %s cannot open %s binding: %s\n",
		       efi_handle_name ( service ), efi_guid_ntoa ( binding ),
		       strerror ( rc ) );
		return rc;
	}

	/* Create child handle */
	if ( ( efirc = u.sb->CreateChild ( u.sb, handle ) ) != 0 ) {
		rc = -EEFI ( efirc );
		DBGC ( service, "EFISVC %s could not create %s child: %s\n",
		       efi_handle_name ( service ), efi_guid_ntoa ( binding ),
		       strerror ( rc ) );
		return rc;
	}

	DBGC ( service, "EFISVC %s created %s child ",
	       efi_handle_name ( service ), efi_guid_ntoa ( binding ) );
	DBGC ( service, "%s\n", efi_handle_name ( *handle ) );
	return 0;
}

/**
 * Remove service from child handle
 *
 * @v service		Service binding handle
 * @v binding		Service binding protocol GUID
 * @v handle		Child handle
 * @ret rc		Return status code
 */
int efi_service_del ( EFI_HANDLE service, EFI_GUID *binding,
		      EFI_HANDLE handle ) {
	union {
		EFI_SERVICE_BINDING_PROTOCOL *sb;
		void *interface;
	} u;
	EFI_STATUS efirc;
	int rc;

	DBGC ( service, "EFISVC %s removing %s child ",
	       efi_handle_name ( service ), efi_guid_ntoa ( binding ) );
	DBGC ( service, "%s\n", efi_handle_name ( handle ) );

	/* Open service binding protocol */
	if ( ( rc = efi_open ( service, binding, &u.interface ) ) != 0 ) {
		DBGC ( service, "EFISVC %s cannot open %s binding: %s\n",
		       efi_handle_name ( service ), efi_guid_ntoa ( binding ),
		       strerror ( rc ) );
		return rc;
	}

	/* Destroy child handle */
	if ( ( efirc = u.sb->DestroyChild ( u.sb, handle ) ) != 0 ) {
		rc = -EEFI ( efirc );
		DBGC ( service, "EFISVC %s could not destroy %s child ",
		       efi_handle_name ( service ), efi_guid_ntoa ( binding ) );
		DBGC ( service, "%s: %s\n",
		       efi_handle_name ( handle ), strerror ( rc ) );
		return rc;
	}

	return 0;
}