summaryrefslogtreecommitdiffstats
path: root/src/interface/efi/efi_blacklist.c
blob: 292b28e8c0318cebc43e0268097e45b257a0620c (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * Copyright (C) 2019 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.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <stddef.h>
#include <string.h>
#include <errno.h>
#include <ipxe/settings.h>
#include <ipxe/efi/efi.h>
#include <ipxe/efi/Protocol/DriverBinding.h>
#include <ipxe/efi/Protocol/LoadedImage.h>
#include <ipxe/efi/Protocol/ComponentName.h>
#include <ipxe/efi/efi_blacklist.h>

/** @file
 *
 * EFI driver blacklist
 *
 */

/** A blacklisted driver */
struct efi_blacklist {
	/** Name */
	const char *name;
	/**
	 * Check if driver is blacklisted
	 *
	 * @v binding		Driver binding protocol
	 * @v loaded		Loaded image protocol
	 * @v wtf		Component name protocol, if present
	 * @ret blacklisted	Driver is the blacklisted driver
	 */
	int ( * blacklist ) ( EFI_DRIVER_BINDING_PROTOCOL *binding,
			      EFI_LOADED_IMAGE_PROTOCOL *loaded,
			      EFI_COMPONENT_NAME_PROTOCOL *wtf );
};

/**
 * Blacklist Dell Ip4ConfigDxe driver
 *
 * @v binding		Driver binding protocol
 * @v loaded		Loaded image protocol
 * @v wtf		Component name protocol, if present
 * @ret blacklisted	Driver is the blacklisted driver
 */
static int
efi_blacklist_dell_ip4config ( EFI_DRIVER_BINDING_PROTOCOL *binding __unused,
			       EFI_LOADED_IMAGE_PROTOCOL *loaded __unused,
			       EFI_COMPONENT_NAME_PROTOCOL *wtf ) {
	static const CHAR16 ip4cfg[] = L"IP4 CONFIG Network Service Driver";
	static const char dell[] = "Dell Inc.";
	char manufacturer[ sizeof ( dell ) ];
	CHAR16 *name;

	/* Check driver name */
	if ( ! wtf )
		return 0;
	if ( wtf->GetDriverName ( wtf, "eng", &name ) != 0 )
		return 0;
	if ( memcmp ( name, ip4cfg, sizeof ( ip4cfg ) ) != 0 )
		return 0;

	/* Check manufacturer */
	fetch_string_setting ( NULL, &manufacturer_setting, manufacturer,
			       sizeof ( manufacturer ) );
	if ( strcmp ( manufacturer, dell ) != 0 )
		return 0;

	return 1;
}

/** Blacklisted drivers */
static struct efi_blacklist efi_blacklists[] = {
	{
		.name = "Dell Ip4Config",
		.blacklist = efi_blacklist_dell_ip4config,
	},
};

/**
 * Find driver blacklisting, if any
 *
 * @v driver		Driver binding handle
 * @ret blacklist	Driver blacklisting, or NULL
 * @ret rc		Return status code
 */
static int efi_blacklist ( EFI_HANDLE driver,
			   struct efi_blacklist **blacklist ) {
	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
	union {
		EFI_DRIVER_BINDING_PROTOCOL *binding;
		void *interface;
	} binding;
	union {
		EFI_LOADED_IMAGE_PROTOCOL *loaded;
		void *interface;
	} loaded;
	union {
		EFI_COMPONENT_NAME_PROTOCOL *wtf;
		void *interface;
	} wtf;
	unsigned int i;
	EFI_HANDLE image;
	EFI_STATUS efirc;
	int rc;

	DBGC2 ( &efi_blacklists, "EFIBL checking %s\n",
		efi_handle_name ( driver ) );

	/* Mark as not blacklisted */
	*blacklist = NULL;

	/* Open driver binding protocol */
	if ( ( efirc = bs->OpenProtocol (
			driver, &efi_driver_binding_protocol_guid,
			&binding.interface, efi_image_handle, driver,
			EFI_OPEN_PROTOCOL_GET_PROTOCOL ) ) != 0 ) {
		rc = -EEFI ( efirc );
		DBGC ( driver, "EFIBL %s could not open driver binding "
		       "protocol: %s\n", efi_handle_name ( driver ),
		       strerror ( rc ) );
		goto err_binding;
	}
	image = binding.binding->ImageHandle;

	/* Open loaded image protocol */
	if ( ( efirc = bs->OpenProtocol (
			image, &efi_loaded_image_protocol_guid,
			&loaded.interface, efi_image_handle, image,
			EFI_OPEN_PROTOCOL_GET_PROTOCOL ) ) != 0 ) {
		rc = -EEFI ( efirc );
		DBGC ( driver, "EFIBL %s could not open",
		       efi_handle_name ( driver ) );
		DBGC ( driver, " %s loaded image protocol: %s\n",
		       efi_handle_name ( image ), strerror ( rc ) );
		goto err_loaded;
	}

	/* Open component name protocol, if present*/
	if ( ( efirc = bs->OpenProtocol (
			driver, &efi_component_name_protocol_guid,
			&wtf.interface, efi_image_handle, driver,
			EFI_OPEN_PROTOCOL_GET_PROTOCOL ) ) != 0 ) {
		/* Ignore failure; is not required to be present */
		wtf.interface = NULL;
	}

	/* Check blacklistings */
	for ( i = 0 ; i < ( sizeof ( efi_blacklists ) /
			    sizeof ( efi_blacklists[0] ) ) ; i++ ) {
		if ( efi_blacklists[i].blacklist ( binding.binding,
						   loaded.loaded, wtf.wtf ) ) {
			*blacklist = &efi_blacklists[i];
			break;
		}
	}

	/* Success */
	rc = 0;

	/* Close protocols */
	if ( wtf.wtf ) {
		bs->CloseProtocol ( driver, &efi_component_name_protocol_guid,
				    efi_image_handle, driver );
	}
	bs->CloseProtocol ( image, &efi_loaded_image_protocol_guid,
			    efi_image_handle, image );
 err_loaded:
	bs->CloseProtocol ( driver, &efi_driver_binding_protocol_guid,
			    efi_image_handle, driver );
 err_binding:
	return rc;
}

/**
 * Unload any blacklisted drivers
 *
 */
void efi_unload_blacklist ( void ) {
	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
	struct efi_blacklist *blacklist;
	EFI_HANDLE *drivers;
	EFI_HANDLE driver;
	UINTN num_drivers;
	unsigned int i;
	EFI_STATUS efirc;
	int rc;

	/* Locate all driver binding protocol handles */
	if ( ( efirc = bs->LocateHandleBuffer (
			ByProtocol, &efi_driver_binding_protocol_guid,
			NULL, &num_drivers, &drivers ) ) != 0 ) {
		rc = -EEFI ( efirc );
		DBGC ( &efi_blacklists, "EFIBL could not list all drivers: "
		       "%s\n", strerror ( rc ) );
		return;
	}

	/* Unload any blacklisted drivers */
	for ( i = 0 ; i < num_drivers ; i++ ) {
		driver = drivers[i];
		if ( ( rc = efi_blacklist ( driver, &blacklist ) ) != 0 ) {
			DBGC ( driver, "EFIBL could not determine "
			       "blacklisting for %s: %s\n",
			       efi_handle_name ( driver ), strerror ( rc ) );
			continue;
		}
		if ( ! blacklist )
			continue;
		DBGC ( driver, "EFIBL unloading %s (%s)\n",
		       efi_handle_name ( driver ), blacklist->name );
		if ( ( efirc = bs->UnloadImage ( driver ) ) != 0 ) {
			DBGC ( driver, "EFIBL could not unload %s: %s\n",
			       efi_handle_name ( driver ), strerror ( rc ) );
		}
	}

	/* Free handle list */
	bs->FreePool ( drivers );
}