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