1ab8c1d40SToomas Soome /*-
2ab8c1d40SToomas Soome  * Copyright (c) 2016 John Baldwin <jhb@FreeBSD.org>
3ab8c1d40SToomas Soome  * All rights reserved.
4ab8c1d40SToomas Soome  *
5ab8c1d40SToomas Soome  * Redistribution and use in source and binary forms, with or without
6ab8c1d40SToomas Soome  * modification, are permitted provided that the following conditions
7ab8c1d40SToomas Soome  * are met:
8ab8c1d40SToomas Soome  * 1. Redistributions of source code must retain the above copyright
9ab8c1d40SToomas Soome  *    notice, this list of conditions and the following disclaimer.
10ab8c1d40SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
11ab8c1d40SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
12ab8c1d40SToomas Soome  *    documentation and/or other materials provided with the distribution.
13ab8c1d40SToomas Soome  *
14ab8c1d40SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15ab8c1d40SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16ab8c1d40SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17ab8c1d40SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18ab8c1d40SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19ab8c1d40SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20ab8c1d40SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21ab8c1d40SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22ab8c1d40SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23ab8c1d40SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24ab8c1d40SToomas Soome  * SUCH DAMAGE.
25ab8c1d40SToomas Soome  */
26ab8c1d40SToomas Soome 
27ab8c1d40SToomas Soome #include <sys/cdefs.h>
28ab8c1d40SToomas Soome 
29ab8c1d40SToomas Soome #include <efi.h>
30ab8c1d40SToomas Soome #include <efilib.h>
31ab8c1d40SToomas Soome 
32ab8c1d40SToomas Soome static EFI_GUID ImageDevicePathGUID =
33ab8c1d40SToomas Soome     EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL_GUID;
34ab8c1d40SToomas Soome static EFI_GUID DevicePathGUID = DEVICE_PATH_PROTOCOL;
35ab8c1d40SToomas Soome static EFI_GUID DevicePathToTextGUID = EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
36ab8c1d40SToomas Soome static EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *textProtocol;
37ab8c1d40SToomas Soome 
38ab8c1d40SToomas Soome EFI_DEVICE_PATH *
efi_lookup_image_devpath(EFI_HANDLE handle)39ab8c1d40SToomas Soome efi_lookup_image_devpath(EFI_HANDLE handle)
40ab8c1d40SToomas Soome {
41ab8c1d40SToomas Soome 	EFI_DEVICE_PATH *devpath;
42ab8c1d40SToomas Soome 	EFI_STATUS status;
43ab8c1d40SToomas Soome 
44bf693dc9SToomas Soome 	status = OpenProtocolByHandle(handle, &ImageDevicePathGUID,
45bf693dc9SToomas Soome 	    (void **)&devpath);
46ab8c1d40SToomas Soome 	if (EFI_ERROR(status))
47ab8c1d40SToomas Soome 		devpath = NULL;
48ab8c1d40SToomas Soome 	return (devpath);
49ab8c1d40SToomas Soome }
50ab8c1d40SToomas Soome 
51ab8c1d40SToomas Soome EFI_DEVICE_PATH *
efi_lookup_devpath(EFI_HANDLE handle)52ab8c1d40SToomas Soome efi_lookup_devpath(EFI_HANDLE handle)
53ab8c1d40SToomas Soome {
54ab8c1d40SToomas Soome 	EFI_DEVICE_PATH *devpath;
55ab8c1d40SToomas Soome 	EFI_STATUS status;
56ab8c1d40SToomas Soome 
57bf693dc9SToomas Soome 	status = OpenProtocolByHandle(handle, &DevicePathGUID,
58bf693dc9SToomas Soome 	    (void **)&devpath);
59ab8c1d40SToomas Soome 	if (EFI_ERROR(status))
60ab8c1d40SToomas Soome 		devpath = NULL;
61ab8c1d40SToomas Soome 	return (devpath);
62ab8c1d40SToomas Soome }
63ab8c1d40SToomas Soome 
64*c54162e4SToomas Soome void
efi_close_devpath(EFI_HANDLE handle)65*c54162e4SToomas Soome efi_close_devpath(EFI_HANDLE handle)
66*c54162e4SToomas Soome {
67*c54162e4SToomas Soome 	EFI_STATUS status;
68*c54162e4SToomas Soome 
69*c54162e4SToomas Soome 	status = BS->CloseProtocol(handle, &DevicePathGUID, IH, NULL);
70*c54162e4SToomas Soome 	if (EFI_ERROR(status))
71*c54162e4SToomas Soome 		printf("CloseProtocol error: %lu\n", EFI_ERROR_CODE(status));
72*c54162e4SToomas Soome }
73*c54162e4SToomas Soome 
74ab8c1d40SToomas Soome CHAR16 *
efi_devpath_name(EFI_DEVICE_PATH * devpath)75ab8c1d40SToomas Soome efi_devpath_name(EFI_DEVICE_PATH *devpath)
76ab8c1d40SToomas Soome {
77ab8c1d40SToomas Soome 	static int once = 1;
78ab8c1d40SToomas Soome 	EFI_STATUS status;
79ab8c1d40SToomas Soome 
80ab8c1d40SToomas Soome 	if (devpath == NULL)
81ab8c1d40SToomas Soome 		return (NULL);
82ab8c1d40SToomas Soome 	if (once) {
83ab8c1d40SToomas Soome 		status = BS->LocateProtocol(&DevicePathToTextGUID, NULL,
84ab8c1d40SToomas Soome 		    (VOID **)&textProtocol);
85ab8c1d40SToomas Soome 		if (EFI_ERROR(status))
86ab8c1d40SToomas Soome 			textProtocol = NULL;
87ab8c1d40SToomas Soome 		once = 0;
88ab8c1d40SToomas Soome 	}
89ab8c1d40SToomas Soome 	if (textProtocol == NULL)
90ab8c1d40SToomas Soome 		return (NULL);
91ab8c1d40SToomas Soome 
92ab8c1d40SToomas Soome 	return (textProtocol->ConvertDevicePathToText(devpath, TRUE, TRUE));
93ab8c1d40SToomas Soome }
94ab8c1d40SToomas Soome 
95ab8c1d40SToomas Soome void
efi_free_devpath_name(CHAR16 * text)96ab8c1d40SToomas Soome efi_free_devpath_name(CHAR16 *text)
97ab8c1d40SToomas Soome {
98ab8c1d40SToomas Soome 
99ab8c1d40SToomas Soome 	BS->FreePool(text);
100ab8c1d40SToomas Soome }
101ab8c1d40SToomas Soome 
102ab8c1d40SToomas Soome EFI_DEVICE_PATH *
efi_devpath_last_node(EFI_DEVICE_PATH * devpath)103ab8c1d40SToomas Soome efi_devpath_last_node(EFI_DEVICE_PATH *devpath)
104ab8c1d40SToomas Soome {
105ab8c1d40SToomas Soome 
106ab8c1d40SToomas Soome 	if (IsDevicePathEnd(devpath))
107ab8c1d40SToomas Soome 		return (NULL);
108ab8c1d40SToomas Soome 	while (!IsDevicePathEnd(NextDevicePathNode(devpath)))
109ab8c1d40SToomas Soome 		devpath = NextDevicePathNode(devpath);
110ab8c1d40SToomas Soome 	return (devpath);
111ab8c1d40SToomas Soome }
112ab8c1d40SToomas Soome 
113ab8c1d40SToomas Soome EFI_DEVICE_PATH *
efi_devpath_trim(EFI_DEVICE_PATH * devpath)114ab8c1d40SToomas Soome efi_devpath_trim(EFI_DEVICE_PATH *devpath)
115ab8c1d40SToomas Soome {
116ab8c1d40SToomas Soome 	EFI_DEVICE_PATH *node, *copy;
117ab8c1d40SToomas Soome 	size_t prefix, len;
118ab8c1d40SToomas Soome 
119ab8c1d40SToomas Soome 	if ((node = efi_devpath_last_node(devpath)) == NULL)
120ab8c1d40SToomas Soome 		return (NULL);
121ab8c1d40SToomas Soome 	prefix = (UINT8 *)node - (UINT8 *)devpath;
122ab8c1d40SToomas Soome 	if (prefix == 0)
123ab8c1d40SToomas Soome 		return (NULL);
124ab8c1d40SToomas Soome 	len = prefix + DevicePathNodeLength(NextDevicePathNode(node));
125ab8c1d40SToomas Soome 	copy = malloc(len);
126ab8c1d40SToomas Soome 	if (copy != NULL) {
127ab8c1d40SToomas Soome 		memcpy(copy, devpath, prefix);
128ab8c1d40SToomas Soome 		node = (EFI_DEVICE_PATH *)((UINT8 *)copy + prefix);
129ab8c1d40SToomas Soome 		SetDevicePathEndNode(node);
130ab8c1d40SToomas Soome 	}
131ab8c1d40SToomas Soome 	return (copy);
132ab8c1d40SToomas Soome }
133ab8c1d40SToomas Soome 
134ab8c1d40SToomas Soome EFI_HANDLE
efi_devpath_handle(EFI_DEVICE_PATH * devpath)135ab8c1d40SToomas Soome efi_devpath_handle(EFI_DEVICE_PATH *devpath)
136ab8c1d40SToomas Soome {
137ab8c1d40SToomas Soome 	EFI_STATUS status;
138ab8c1d40SToomas Soome 	EFI_HANDLE h;
139ab8c1d40SToomas Soome 
140ab8c1d40SToomas Soome 	/*
141ab8c1d40SToomas Soome 	 * There isn't a standard way to locate a handle for a given
142ab8c1d40SToomas Soome 	 * device path.  However, querying the EFI_DEVICE_PATH protocol
143ab8c1d40SToomas Soome 	 * for a given device path should give us a handle for the
144ab8c1d40SToomas Soome 	 * closest node in the path to the end that is valid.
145ab8c1d40SToomas Soome 	 */
146ab8c1d40SToomas Soome 	status = BS->LocateDevicePath(&DevicePathGUID, &devpath, &h);
147ab8c1d40SToomas Soome 	if (EFI_ERROR(status))
148ab8c1d40SToomas Soome 		return (NULL);
149ab8c1d40SToomas Soome 	return (h);
150ab8c1d40SToomas Soome }
151dbacaf56SToomas Soome 
152dbacaf56SToomas Soome bool
efi_devpath_match(EFI_DEVICE_PATH * devpath1,EFI_DEVICE_PATH * devpath2)153dbacaf56SToomas Soome efi_devpath_match(EFI_DEVICE_PATH *devpath1, EFI_DEVICE_PATH *devpath2)
154dbacaf56SToomas Soome {
1555b5fb153SToomas Soome 	size_t len;
156dbacaf56SToomas Soome 
157dbacaf56SToomas Soome 	if (devpath1 == NULL || devpath2 == NULL)
158dbacaf56SToomas Soome 		return (false);
159dbacaf56SToomas Soome 
160dbacaf56SToomas Soome 	while (true) {
161dbacaf56SToomas Soome 		if (DevicePathType(devpath1) != DevicePathType(devpath2) ||
162dbacaf56SToomas Soome 		    DevicePathSubType(devpath1) != DevicePathSubType(devpath2))
163dbacaf56SToomas Soome 			return (false);
164dbacaf56SToomas Soome 
165dbacaf56SToomas Soome 		len = DevicePathNodeLength(devpath1);
166dbacaf56SToomas Soome 		if (len != DevicePathNodeLength(devpath2))
167dbacaf56SToomas Soome 			return (false);
168dbacaf56SToomas Soome 
169dbacaf56SToomas Soome 		if (memcmp(devpath1, devpath2, len) != 0)
170dbacaf56SToomas Soome 			return (false);
171dbacaf56SToomas Soome 
172dbacaf56SToomas Soome 		if (IsDevicePathEnd(devpath1))
173dbacaf56SToomas Soome 			break;
174dbacaf56SToomas Soome 		devpath1 = NextDevicePathNode(devpath1);
175dbacaf56SToomas Soome 		devpath2 = NextDevicePathNode(devpath2);
176dbacaf56SToomas Soome 	}
177dbacaf56SToomas Soome 	return (true);
178dbacaf56SToomas Soome }
1795b5fb153SToomas Soome 
1805b5fb153SToomas Soome bool
efi_devpath_is_prefix(EFI_DEVICE_PATH * prefix,EFI_DEVICE_PATH * path)1815b5fb153SToomas Soome efi_devpath_is_prefix(EFI_DEVICE_PATH *prefix, EFI_DEVICE_PATH *path)
1825b5fb153SToomas Soome {
1835b5fb153SToomas Soome 	size_t len;
1845b5fb153SToomas Soome 
1855b5fb153SToomas Soome 	if (prefix == NULL || path == NULL)
1865b5fb153SToomas Soome 		return (false);
1875b5fb153SToomas Soome 
1885b5fb153SToomas Soome 	while (1) {
1895b5fb153SToomas Soome 		if (IsDevicePathEnd(prefix))
1905b5fb153SToomas Soome 			break;
1915b5fb153SToomas Soome 
1925b5fb153SToomas Soome 		if (DevicePathType(prefix) != DevicePathType(path) ||
1935b5fb153SToomas Soome 		    DevicePathSubType(prefix) != DevicePathSubType(path))
1945b5fb153SToomas Soome 			return (false);
1955b5fb153SToomas Soome 
1965b5fb153SToomas Soome 		len = DevicePathNodeLength(prefix);
1975b5fb153SToomas Soome 		if (len != DevicePathNodeLength(path))
1985b5fb153SToomas Soome 			return (false);
1995b5fb153SToomas Soome 
2005b5fb153SToomas Soome 		if (memcmp(prefix, path, len) != 0)
2015b5fb153SToomas Soome 			return (false);
2025b5fb153SToomas Soome 
2035b5fb153SToomas Soome 		prefix = NextDevicePathNode(prefix);
2045b5fb153SToomas Soome 		path = NextDevicePathNode(path);
2055b5fb153SToomas Soome 	}
2065b5fb153SToomas Soome 	return (true);
2075b5fb153SToomas Soome }
208