xref: /illumos-gate/usr/src/boot/efi/libefi/devpath.c (revision 533affcbc7fc4d0c8132976ea454aaa715fe2307)
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 
29 #include <efi.h>
30 #include <efilib.h>
31 
32 static EFI_GUID ImageDevicePathGUID =
33     EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL_GUID;
34 static EFI_GUID DevicePathGUID = DEVICE_PATH_PROTOCOL;
35 static EFI_GUID DevicePathToTextGUID = EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
36 static EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *textProtocol;
37 
38 EFI_DEVICE_PATH *
39 efi_lookup_image_devpath(EFI_HANDLE handle)
40 {
41 	EFI_DEVICE_PATH *devpath;
42 	EFI_STATUS status;
43 
44 	status = OpenProtocolByHandle(handle, &ImageDevicePathGUID,
45 	    (void **)&devpath);
46 	if (EFI_ERROR(status))
47 		devpath = NULL;
48 	return (devpath);
49 }
50 
51 EFI_DEVICE_PATH *
52 efi_lookup_devpath(EFI_HANDLE handle)
53 {
54 	EFI_DEVICE_PATH *devpath;
55 	EFI_STATUS status;
56 
57 	status = OpenProtocolByHandle(handle, &DevicePathGUID,
58 	    (void **)&devpath);
59 	if (EFI_ERROR(status))
60 		devpath = NULL;
61 	return (devpath);
62 }
63 
64 void
65 efi_close_devpath(EFI_HANDLE handle)
66 {
67 	EFI_STATUS status;
68 
69 	status = BS->CloseProtocol(handle, &DevicePathGUID, IH, NULL);
70 	if (EFI_ERROR(status))
71 		printf("CloseProtocol error: %lu\n", EFI_ERROR_CODE(status));
72 }
73 
74 CHAR16 *
75 efi_devpath_name(EFI_DEVICE_PATH *devpath)
76 {
77 	static int once = 1;
78 	EFI_STATUS status;
79 
80 	if (devpath == NULL)
81 		return (NULL);
82 	if (once) {
83 		status = BS->LocateProtocol(&DevicePathToTextGUID, NULL,
84 		    (VOID **)&textProtocol);
85 		if (EFI_ERROR(status))
86 			textProtocol = NULL;
87 		once = 0;
88 	}
89 	if (textProtocol == NULL)
90 		return (NULL);
91 
92 	return (textProtocol->ConvertDevicePathToText(devpath, TRUE, TRUE));
93 }
94 
95 void
96 efi_free_devpath_name(CHAR16 *text)
97 {
98 
99 	BS->FreePool(text);
100 }
101 
102 EFI_DEVICE_PATH *
103 efi_devpath_last_node(EFI_DEVICE_PATH *devpath)
104 {
105 
106 	if (IsDevicePathEnd(devpath))
107 		return (NULL);
108 	while (!IsDevicePathEnd(NextDevicePathNode(devpath)))
109 		devpath = NextDevicePathNode(devpath);
110 	return (devpath);
111 }
112 
113 /*
114  * Walk device path nodes, return next instance or end node.
115  */
116 EFI_DEVICE_PATH *
117 efi_devpath_next_instance(EFI_DEVICE_PATH *devpath)
118 {
119 	while (!IsDevicePathEnd(devpath)) {
120 		devpath = NextDevicePathNode(devpath);
121 		if (IsDevicePathEndType(devpath) &&
122 		    devpath->SubType == END_INSTANCE_DEVICE_PATH_SUBTYPE) {
123 			devpath = NextDevicePathNode(devpath);
124 			break;
125 		}
126 	}
127 	return (devpath);
128 }
129 
130 EFI_DEVICE_PATH *
131 efi_devpath_trim(EFI_DEVICE_PATH *devpath)
132 {
133 	EFI_DEVICE_PATH *node, *copy;
134 	size_t prefix, len;
135 
136 	if ((node = efi_devpath_last_node(devpath)) == NULL)
137 		return (NULL);
138 	prefix = (UINT8 *)node - (UINT8 *)devpath;
139 	if (prefix == 0)
140 		return (NULL);
141 	len = prefix + DevicePathNodeLength(NextDevicePathNode(node));
142 	copy = malloc(len);
143 	if (copy != NULL) {
144 		memcpy(copy, devpath, prefix);
145 		node = (EFI_DEVICE_PATH *)((UINT8 *)copy + prefix);
146 		SetDevicePathEndNode(node);
147 	}
148 	return (copy);
149 }
150 
151 EFI_HANDLE
152 efi_devpath_handle(EFI_DEVICE_PATH *devpath)
153 {
154 	EFI_STATUS status;
155 	EFI_HANDLE h;
156 
157 	/*
158 	 * There isn't a standard way to locate a handle for a given
159 	 * device path.  However, querying the EFI_DEVICE_PATH protocol
160 	 * for a given device path should give us a handle for the
161 	 * closest node in the path to the end that is valid.
162 	 */
163 	status = BS->LocateDevicePath(&DevicePathGUID, &devpath, &h);
164 	if (EFI_ERROR(status))
165 		return (NULL);
166 	return (h);
167 }
168 
169 bool
170 efi_devpath_match(EFI_DEVICE_PATH *devpath1, EFI_DEVICE_PATH *devpath2)
171 {
172 	size_t len;
173 
174 	if (devpath1 == NULL || devpath2 == NULL)
175 		return (false);
176 
177 	while (true) {
178 		if (DevicePathType(devpath1) != DevicePathType(devpath2) ||
179 		    DevicePathSubType(devpath1) != DevicePathSubType(devpath2))
180 			return (false);
181 
182 		len = DevicePathNodeLength(devpath1);
183 		if (len != DevicePathNodeLength(devpath2))
184 			return (false);
185 
186 		if (memcmp(devpath1, devpath2, len) != 0)
187 			return (false);
188 
189 		if (IsDevicePathEnd(devpath1))
190 			break;
191 		devpath1 = NextDevicePathNode(devpath1);
192 		devpath2 = NextDevicePathNode(devpath2);
193 	}
194 	return (true);
195 }
196 
197 bool
198 efi_devpath_is_prefix(EFI_DEVICE_PATH *prefix, EFI_DEVICE_PATH *path)
199 {
200 	size_t len;
201 
202 	if (prefix == NULL || path == NULL)
203 		return (false);
204 
205 	while (1) {
206 		if (IsDevicePathEnd(prefix))
207 			break;
208 
209 		if (DevicePathType(prefix) != DevicePathType(path) ||
210 		    DevicePathSubType(prefix) != DevicePathSubType(path))
211 			return (false);
212 
213 		len = DevicePathNodeLength(prefix);
214 		if (len != DevicePathNodeLength(path))
215 			return (false);
216 
217 		if (memcmp(prefix, path, len) != 0)
218 			return (false);
219 
220 		prefix = NextDevicePathNode(prefix);
221 		path = NextDevicePathNode(path);
222 	}
223 	return (true);
224 }
225