2 * Copyright (c) 2016 John Baldwin <jhb@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
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
;
39 efi_lookup_image_devpath(EFI_HANDLE handle
)
41 EFI_DEVICE_PATH
*devpath
;
44 status
= BS
->HandleProtocol(handle
, &ImageDevicePathGUID
,
46 if (EFI_ERROR(status
))
52 efi_lookup_devpath(EFI_HANDLE handle
)
54 EFI_DEVICE_PATH
*devpath
;
57 status
= BS
->HandleProtocol(handle
, &DevicePathGUID
, (VOID
**)&devpath
);
58 if (EFI_ERROR(status
))
64 efi_devpath_name(EFI_DEVICE_PATH
*devpath
)
72 status
= BS
->LocateProtocol(&DevicePathToTextGUID
, NULL
,
73 (VOID
**)&textProtocol
);
74 if (EFI_ERROR(status
))
78 if (textProtocol
== NULL
)
81 return (textProtocol
->ConvertDevicePathToText(devpath
, TRUE
, TRUE
));
85 efi_free_devpath_name(CHAR16
*text
)
92 efi_devpath_last_node(EFI_DEVICE_PATH
*devpath
)
95 if (IsDevicePathEnd(devpath
))
97 while (!IsDevicePathEnd(NextDevicePathNode(devpath
)))
98 devpath
= NextDevicePathNode(devpath
);
103 efi_devpath_trim(EFI_DEVICE_PATH
*devpath
)
105 EFI_DEVICE_PATH
*node
, *copy
;
108 if ((node
= efi_devpath_last_node(devpath
)) == NULL
)
110 prefix
= (UINT8
*)node
- (UINT8
*)devpath
;
113 len
= prefix
+ DevicePathNodeLength(NextDevicePathNode(node
));
116 memcpy(copy
, devpath
, prefix
);
117 node
= (EFI_DEVICE_PATH
*)((UINT8
*)copy
+ prefix
);
118 SetDevicePathEndNode(node
);
124 efi_devpath_handle(EFI_DEVICE_PATH
*devpath
)
130 * There isn't a standard way to locate a handle for a given
131 * device path. However, querying the EFI_DEVICE_PATH protocol
132 * for a given device path should give us a handle for the
133 * closest node in the path to the end that is valid.
135 status
= BS
->LocateDevicePath(&DevicePathGUID
, &devpath
, &h
);
136 if (EFI_ERROR(status
))
142 efi_devpath_match(EFI_DEVICE_PATH
*devpath1
, EFI_DEVICE_PATH
*devpath2
)
146 if (devpath1
== NULL
|| devpath2
== NULL
)
150 if (DevicePathType(devpath1
) != DevicePathType(devpath2
) ||
151 DevicePathSubType(devpath1
) != DevicePathSubType(devpath2
))
154 len
= DevicePathNodeLength(devpath1
);
155 if (len
!= DevicePathNodeLength(devpath2
))
158 if (memcmp(devpath1
, devpath2
, len
) != 0)
161 if (IsDevicePathEnd(devpath1
))
163 devpath1
= NextDevicePathNode(devpath1
);
164 devpath2
= NextDevicePathNode(devpath2
);