efi_selftest_devicepath.c 11 KB
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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
/*
 * efi_selftest_devicepath
 *
 * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
 *
 * SPDX-License-Identifier:     GPL-2.0+
 *
 * This unit test checks the following protocol services:
 * DevicePathToText
 */

#include <efi_selftest.h>

static struct efi_boot_services *boottime;

static efi_handle_t handle1;
static efi_handle_t handle2;
static efi_handle_t handle3;

struct interface {
	void (EFIAPI * inc)(void);
} interface;

static efi_guid_t guid_device_path = DEVICE_PATH_GUID;

static efi_guid_t guid_device_path_to_text_protocol =
	EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;

static efi_guid_t guid_protocol =
	EFI_GUID(0xdbca4c98, 0x6cb0, 0x694d,
		 0x08, 0x72, 0x81, 0x9c, 0x65, 0x0c, 0xbb, 0x7d);

static efi_guid_t guid_vendor1 =
	EFI_GUID(0xdbca4c98, 0x6cb0, 0x694d,
		 0x08, 0x72, 0x81, 0x9c, 0x65, 0x0c, 0xbb, 0xb1);

static efi_guid_t guid_vendor2 =
	EFI_GUID(0xdbca4c98, 0x6cb0, 0x694d,
		 0x08, 0x72, 0x81, 0x9c, 0x65, 0x0c, 0xbb, 0xa2);

static efi_guid_t guid_vendor3 =
	EFI_GUID(0xdbca4c98, 0x6cb0, 0x694d,
		 0x08, 0x72, 0x81, 0x9c, 0x65, 0x0c, 0xbb, 0xc3);

static u8 *dp1;
static u8 *dp2;
static u8 *dp3;

struct efi_device_path_to_text_protocol *device_path_to_text;

/*
 * Setup unit test.
 *
 * Create three handles. Install a new protocol on two of them and
 * provice device paths.
 *
 * handle1
 *   guid interface
 * handle2
 *   guid interface
 * handle3
 *
 * @handle:	handle of the loaded image
 * @systable:	system table
 */
static int setup(const efi_handle_t img_handle,
		 const struct efi_system_table *systable)
{
	struct efi_device_path_vendor vendor_node;
	struct efi_device_path end_node;
	efi_status_t ret;

	boottime = systable->boottime;

	ret = boottime->locate_protocol(&guid_device_path_to_text_protocol,
					NULL, (void **)&device_path_to_text);
	if (ret != EFI_SUCCESS) {
		device_path_to_text = NULL;
		efi_st_error(
			"Device path to text protocol is not available.\n");
		return EFI_ST_FAILURE;
	}

	ret = boottime->allocate_pool(EFI_LOADER_DATA,
				      sizeof(struct efi_device_path_vendor) +
				      sizeof(struct efi_device_path),
				      (void **)&dp1);
	if (ret != EFI_SUCCESS)
		goto out_of_memory;

	ret = boottime->allocate_pool(EFI_LOADER_DATA, 2 *
				      sizeof(struct efi_device_path_vendor) +
				      sizeof(struct efi_device_path),
				      (void **)&dp2);
	if (ret != EFI_SUCCESS)
		goto out_of_memory;

	ret = boottime->allocate_pool(EFI_LOADER_DATA, 3 *
				      sizeof(struct efi_device_path_vendor) +
				      sizeof(struct efi_device_path),
				      (void **)&dp3);
	if (ret != EFI_SUCCESS)
		goto out_of_memory;

	vendor_node.dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
	vendor_node.dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
	vendor_node.dp.length = sizeof(struct efi_device_path_vendor);

	boottime->copy_mem(&vendor_node.guid, &guid_vendor1,
			   sizeof(efi_guid_t));
	boottime->copy_mem(dp1, &vendor_node,
			   sizeof(struct efi_device_path_vendor));
	boottime->copy_mem(dp2, &vendor_node,
			   sizeof(struct efi_device_path_vendor));
	boottime->copy_mem(dp3, &vendor_node,
			   sizeof(struct efi_device_path_vendor));

	boottime->copy_mem(&vendor_node.guid, &guid_vendor2,
			   sizeof(efi_guid_t));
	boottime->copy_mem(dp2 + sizeof(struct efi_device_path_vendor),
			   &vendor_node, sizeof(struct efi_device_path_vendor));
	boottime->copy_mem(dp3 + sizeof(struct efi_device_path_vendor),
			   &vendor_node, sizeof(struct efi_device_path_vendor));

	boottime->copy_mem(&vendor_node.guid, &guid_vendor3,
			   sizeof(efi_guid_t));
	boottime->copy_mem(dp3 + 2 * sizeof(struct efi_device_path_vendor),
			   &vendor_node, sizeof(struct efi_device_path_vendor));

	end_node.type = DEVICE_PATH_TYPE_END;
	end_node.sub_type = DEVICE_PATH_SUB_TYPE_END;
	end_node.length = sizeof(struct efi_device_path);
	boottime->copy_mem(dp1 + sizeof(struct efi_device_path_vendor),
			   &end_node, sizeof(struct efi_device_path));
	boottime->copy_mem(dp2 + 2 * sizeof(struct efi_device_path_vendor),
			   &end_node, sizeof(struct efi_device_path));
	boottime->copy_mem(dp3 + 3 * sizeof(struct efi_device_path_vendor),
			   &end_node, sizeof(struct efi_device_path));

	ret = boottime->install_protocol_interface(&handle1,
						   &guid_device_path,
						   EFI_NATIVE_INTERFACE,
						   dp1);
	if (ret != EFI_SUCCESS) {
		efi_st_error("InstallProtocolInterface failed\n");
		return EFI_ST_FAILURE;
	}
	ret = boottime->install_protocol_interface(&handle1,
						   &guid_protocol,
						   EFI_NATIVE_INTERFACE,
						   &interface);
	if (ret != EFI_SUCCESS) {
		efi_st_error("InstallProtocolInterface failed\n");
		return EFI_ST_FAILURE;
	}
	ret = boottime->install_protocol_interface(&handle2,
						   &guid_device_path,
						   EFI_NATIVE_INTERFACE,
						   dp2);
	if (ret != EFI_SUCCESS) {
		efi_st_error("InstallProtocolInterface failed\n");
		return EFI_ST_FAILURE;
	}
	ret = boottime->install_protocol_interface(&handle2,
						   &guid_protocol,
						   EFI_NATIVE_INTERFACE,
						   &interface);
	if (ret != EFI_SUCCESS) {
		efi_st_error("InstallProtocolInterface failed\n");
		return EFI_ST_FAILURE;
	}
	ret = boottime->install_protocol_interface(&handle3,
						   &guid_device_path,
						   EFI_NATIVE_INTERFACE,
						   dp3);
	if (ret != EFI_SUCCESS) {
		efi_st_error("InstallProtocolInterface failed\n");
		return EFI_ST_FAILURE;
	}
	return EFI_ST_SUCCESS;

out_of_memory:
	efi_st_error("Out of memory\n");
	return EFI_ST_FAILURE;
}

/*
 * Tear down unit test.
 *
 */
static int teardown(void)
{
	efi_status_t ret;

	ret = boottime->uninstall_protocol_interface(handle1,
						     &guid_device_path,
						     dp1);
	if (ret != EFI_SUCCESS) {
		efi_st_error("UninstallProtocolInterface failed\n");
		return EFI_ST_FAILURE;
	}
	ret = boottime->uninstall_protocol_interface(handle1,
						     &guid_protocol,
						     &interface);
	if (ret != EFI_SUCCESS) {
		efi_st_error("UninstallProtocolInterface failed\n");
		return EFI_ST_FAILURE;
	}
	ret = boottime->uninstall_protocol_interface(handle2,
						     &guid_device_path,
						     dp2);
	if (ret != EFI_SUCCESS) {
		efi_st_error("UninstallProtocolInterface failed\n");
		return EFI_ST_FAILURE;
	}
	ret = boottime->uninstall_protocol_interface(handle2,
						     &guid_protocol,
						     &interface);
	if (ret != EFI_SUCCESS) {
		efi_st_error("UninstallProtocolInterface failed\n");
		return EFI_ST_FAILURE;
	}
	ret = boottime->uninstall_protocol_interface(handle3,
						     &guid_device_path,
						     dp3);
	if (ret != EFI_SUCCESS) {
		efi_st_error("UninstallProtocolInterface failed\n");
		return EFI_ST_FAILURE;
	}
	if (dp1) {
		ret = boottime->free_pool(dp1);
		if (ret != EFI_SUCCESS) {
			efi_st_error("FreePool failed\n");
			return EFI_ST_FAILURE;
		}
	}
	if (dp2) {
		ret = boottime->free_pool(dp2);
		if (ret != EFI_SUCCESS) {
			efi_st_error("FreePool failed\n");
			return EFI_ST_FAILURE;
		}
	}
	if (dp3) {
		ret = boottime->free_pool(dp3);
		if (ret != EFI_SUCCESS) {
			efi_st_error("FreePool failed\n");
			return EFI_ST_FAILURE;
		}
	}
	return EFI_ST_SUCCESS;
}

/*
 * Execute unit test.
 *
 */
static int execute(void)
{
	struct efi_device_path *remaining_dp;
	void *handle;
	/*
	 * This device path node ends with the letter 't' of 'u-boot'.
	 * The following '.bin' does not belong to the node but is
	 * helps to test the correct truncation.
	 */
	struct {
		struct efi_device_path dp;
		u16 text[12];
	} __packed dp_node = {
			{ DEVICE_PATH_TYPE_MEDIA_DEVICE,
			  DEVICE_PATH_SUB_TYPE_FILE_PATH,
			  sizeof(struct efi_device_path) + 12},
			L"u-boot.bin",
		};
	u16 *string;
	efi_status_t ret;
	efi_uintn_t i, no_handles;
	efi_handle_t *handles;
	struct efi_device_path *dp;

	/* Display all available device paths */
	ret = boottime->locate_handle_buffer(BY_PROTOCOL,
					     &guid_device_path,
					     NULL, &no_handles, &handles);
	if (ret != EFI_SUCCESS) {
		efi_st_error("Cannot retrieve device path protocols.\n");
		return EFI_ST_FAILURE;
	}

	efi_st_printf("Installed device path protocols:\n");
	for (i = 0; i < no_handles; ++i) {
		ret = boottime->open_protocol(handles[i], &guid_device_path,
					      (void **)&dp, NULL, NULL,
					      EFI_OPEN_PROTOCOL_GET_PROTOCOL);
		if (ret != EFI_SUCCESS) {
			efi_st_error("Cannot open device path protocol.\n");
			return EFI_ST_FAILURE;
		}
		string = device_path_to_text->convert_device_path_to_text(
					dp, true, false);
		if (!string) {
			efi_st_error("ConvertDevicePathToText failed\n");
			return EFI_ST_FAILURE;
		}
		efi_st_printf("%ps\n", string);
		ret = boottime->free_pool(string);
		if (ret != EFI_SUCCESS) {
			efi_st_error("FreePool failed\n");
			return EFI_ST_FAILURE;
		}
		/*
		 * CloseProtocol cannot be called without agent handle.
		 * There is no need to close the device path protocol.
		 */
	}
	ret = boottime->free_pool(handles);
	if (ret != EFI_SUCCESS) {
		efi_st_error("FreePool failed\n");
		return EFI_ST_FAILURE;
	}

	/* Test ConvertDevicePathToText */
	string = device_path_to_text->convert_device_path_to_text(
			(struct efi_device_path *)dp2, true, false);
	if (!string) {
		efi_st_error("ConvertDevicePathToText failed\n");
		return EFI_ST_FAILURE;
	}
	if (efi_st_strcmp_16_8(
		string,
		"/VenHw(dbca4c98-6cb0-694d-0872-819c650cbbb1)/VenHw(dbca4c98-6cb0-694d-0872-819c650cbba2)")
	    ) {
		efi_st_printf("dp2: %ps\n", string);
		efi_st_error("Incorrect text from ConvertDevicePathToText\n");
		return EFI_ST_FAILURE;
	}
	ret = boottime->free_pool(string);
	if (ret != EFI_SUCCESS) {
		efi_st_error("FreePool failed\n");
		return EFI_ST_FAILURE;
	}

	/* Test ConvertDeviceNodeToText */
	string = device_path_to_text->convert_device_node_to_text(
			(struct efi_device_path *)&dp_node, true, false);
	if (!string) {
		efi_st_error("ConvertDeviceNodeToText failed\n");
		return EFI_ST_FAILURE;
	}
	if (efi_st_strcmp_16_8(string, "u-boot")) {
		efi_st_printf("dp_node: %ps\n", string);
		efi_st_error(
			"Incorrect conversion by ConvertDeviceNodeToText\n");
		return EFI_ST_FAILURE;
	}
	ret = boottime->free_pool(string);
	if (ret != EFI_SUCCESS) {
		efi_st_error("FreePool failed\n");
		return EFI_ST_FAILURE;
	}

	/* Test LocateDevicePath */
	remaining_dp = (struct efi_device_path *)dp3;
	ret = boottime->locate_device_path(&guid_protocol, &remaining_dp,
					   &handle);
	if (ret != EFI_SUCCESS) {
		efi_st_error("LocateDevicePath failed\n");
		return EFI_ST_FAILURE;
	}
	if (handle != handle2) {
		efi_st_error("LocateDevicePath returned wrong handle\n");
		return EFI_ST_FAILURE;
	}
	string = device_path_to_text->convert_device_path_to_text(remaining_dp,
								  true, false);
	if (!string) {
		efi_st_error("ConvertDevicePathToText failed\n");
		return EFI_ST_FAILURE;
	}
	if (efi_st_strcmp_16_8(string,
			       "/VenHw(dbca4c98-6cb0-694d-0872-819c650cbbc3)")
	    ) {
		efi_st_printf("remaining device path: %ps\n", string);
		efi_st_error("LocateDevicePath: wrong remaining device path\n");
		return EFI_ST_FAILURE;
	}
	ret = boottime->free_pool(string);
	if (ret != EFI_SUCCESS) {
		efi_st_error("FreePool failed\n");
		return EFI_ST_FAILURE;
	}

	return EFI_ST_SUCCESS;
}

EFI_UNIT_TEST(devicepath) = {
	.name = "device path",
	.phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
	.setup = setup,
	.execute = execute,
	.teardown = teardown,
};