2 Device Path services. The thing to remember is device paths are built out of
3 nodes. The device path is terminated by an end node that is length
4 sizeof(EFI_DEVICE_PATH_PROTOCOL). That would be why there is sizeof(EFI_DEVICE_PATH_PROTOCOL)
7 The only place where multi-instance device paths are supported is in
8 environment varibles. Multi-instance device paths should never be placed
11 Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
12 SPDX-License-Identifier: BSD-2-Clause-Patent
17 #include "UefiDevicePathLib.h"
19 GLOBAL_REMOVE_IF_UNREFERENCED EFI_DEVICE_PATH_UTILITIES_PROTOCOL
*mDevicePathLibDevicePathUtilities
= NULL
;
20 GLOBAL_REMOVE_IF_UNREFERENCED EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
*mDevicePathLibDevicePathToText
= NULL
;
21 GLOBAL_REMOVE_IF_UNREFERENCED EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL
*mDevicePathLibDevicePathFromText
= NULL
;
24 The constructor function caches the pointer to DevicePathUtilites protocol,
25 DevicePathToText protocol and DevicePathFromText protocol.
27 The constructor function locates these three protocols from protocol database.
28 It will caches the pointer to local protocol instance if that operation fails
29 and it will always return EFI_SUCCESS.
31 @param ImageHandle The firmware allocated handle for the EFI image.
32 @param SystemTable A pointer to the EFI System Table.
34 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
39 UefiDevicePathLibOptionalDevicePathProtocolConstructor (
40 IN EFI_HANDLE ImageHandle
,
41 IN EFI_SYSTEM_TABLE
*SystemTable
46 Status
= gBS
->LocateProtocol (
47 &gEfiDevicePathUtilitiesProtocolGuid
,
49 (VOID
**) &mDevicePathLibDevicePathUtilities
51 ASSERT_EFI_ERROR (Status
);
52 ASSERT (mDevicePathLibDevicePathUtilities
!= NULL
);
57 Returns the size of a device path in bytes.
59 This function returns the size, in bytes, of the device path data structure
60 specified by DevicePath including the end of device path node.
61 If DevicePath is NULL or invalid, then 0 is returned.
63 @param DevicePath A pointer to a device path data structure.
65 @retval 0 If DevicePath is NULL or invalid.
66 @retval Others The size of a device path in bytes.
72 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
75 if (mDevicePathLibDevicePathUtilities
!= NULL
) {
76 return mDevicePathLibDevicePathUtilities
->GetDevicePathSize (DevicePath
);
78 return UefiDevicePathLibGetDevicePathSize (DevicePath
);
83 Creates a new copy of an existing device path.
85 This function allocates space for a new copy of the device path specified by DevicePath.
86 If DevicePath is NULL, then NULL is returned. If the memory is successfully
87 allocated, then the contents of DevicePath are copied to the newly allocated
88 buffer, and a pointer to that buffer is returned. Otherwise, NULL is returned.
89 The memory for the new device path is allocated from EFI boot services memory.
90 It is the responsibility of the caller to free the memory allocated.
92 @param DevicePath A pointer to a device path data structure.
94 @retval NULL DevicePath is NULL or invalid.
95 @retval Others A pointer to the duplicated device path.
98 EFI_DEVICE_PATH_PROTOCOL
*
100 DuplicateDevicePath (
101 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
104 if (mDevicePathLibDevicePathUtilities
!= NULL
) {
105 return mDevicePathLibDevicePathUtilities
->DuplicateDevicePath (DevicePath
);
107 return UefiDevicePathLibDuplicateDevicePath (DevicePath
);
112 Creates a new device path by appending a second device path to a first device path.
114 This function creates a new device path by appending a copy of SecondDevicePath
115 to a copy of FirstDevicePath in a newly allocated buffer. Only the end-of-device-path
116 device node from SecondDevicePath is retained. The newly created device path is
117 returned. If FirstDevicePath is NULL, then it is ignored, and a duplicate of
118 SecondDevicePath is returned. If SecondDevicePath is NULL, then it is ignored,
119 and a duplicate of FirstDevicePath is returned. If both FirstDevicePath and
120 SecondDevicePath are NULL, then a copy of an end-of-device-path is returned.
122 If there is not enough memory for the newly allocated buffer, then NULL is returned.
123 The memory for the new device path is allocated from EFI boot services memory.
124 It is the responsibility of the caller to free the memory allocated.
126 @param FirstDevicePath A pointer to a device path data structure.
127 @param SecondDevicePath A pointer to a device path data structure.
129 @retval NULL If there is not enough memory for the newly allocated buffer.
130 @retval NULL If FirstDevicePath or SecondDevicePath is invalid.
131 @retval Others A pointer to the new device path if success.
132 Or a copy an end-of-device-path if both FirstDevicePath and SecondDevicePath are NULL.
135 EFI_DEVICE_PATH_PROTOCOL
*
138 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FirstDevicePath
, OPTIONAL
139 IN CONST EFI_DEVICE_PATH_PROTOCOL
*SecondDevicePath OPTIONAL
142 if (mDevicePathLibDevicePathUtilities
!= NULL
) {
143 return mDevicePathLibDevicePathUtilities
->AppendDevicePath (FirstDevicePath
, SecondDevicePath
);
145 return UefiDevicePathLibAppendDevicePath (FirstDevicePath
, SecondDevicePath
);
150 Creates a new path by appending the device node to the device path.
152 This function creates a new device path by appending a copy of the device node
153 specified by DevicePathNode to a copy of the device path specified by DevicePath
154 in an allocated buffer. The end-of-device-path device node is moved after the
155 end of the appended device node.
156 If DevicePathNode is NULL then a copy of DevicePath is returned.
157 If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device
158 path device node is returned.
159 If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path
160 device node is returned.
161 If there is not enough memory to allocate space for the new device path, then
163 The memory is allocated from EFI boot services memory. It is the responsibility
164 of the caller to free the memory allocated.
166 @param DevicePath A pointer to a device path data structure.
167 @param DevicePathNode A pointer to a single device path node.
169 @retval NULL If there is not enough memory for the new device path.
170 @retval Others A pointer to the new device path if success.
171 A copy of DevicePathNode followed by an end-of-device-path node
172 if both FirstDevicePath and SecondDevicePath are NULL.
173 A copy of an end-of-device-path node if both FirstDevicePath
174 and SecondDevicePath are NULL.
177 EFI_DEVICE_PATH_PROTOCOL
*
179 AppendDevicePathNode (
180 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
181 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode OPTIONAL
184 if (mDevicePathLibDevicePathUtilities
!= NULL
) {
185 return mDevicePathLibDevicePathUtilities
->AppendDeviceNode (DevicePath
, DevicePathNode
);
187 return UefiDevicePathLibAppendDevicePathNode (DevicePath
, DevicePathNode
);
192 Creates a new device path by appending the specified device path instance to the specified device
195 This function creates a new device path by appending a copy of the device path
196 instance specified by DevicePathInstance to a copy of the device path specified
197 by DevicePath in a allocated buffer.
198 The end-of-device-path device node is moved after the end of the appended device
199 path instance and a new end-of-device-path-instance node is inserted between.
200 If DevicePath is NULL, then a copy if DevicePathInstance is returned.
201 If DevicePathInstance is NULL, then NULL is returned.
202 If DevicePath or DevicePathInstance is invalid, then NULL is returned.
203 If there is not enough memory to allocate space for the new device path, then
205 The memory is allocated from EFI boot services memory. It is the responsibility
206 of the caller to free the memory allocated.
208 @param DevicePath A pointer to a device path data structure.
209 @param DevicePathInstance A pointer to a device path instance.
211 @return A pointer to the new device path.
214 EFI_DEVICE_PATH_PROTOCOL
*
216 AppendDevicePathInstance (
217 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
, OPTIONAL
218 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePathInstance OPTIONAL
221 if (mDevicePathLibDevicePathUtilities
!= NULL
) {
222 return mDevicePathLibDevicePathUtilities
->AppendDevicePathInstance (DevicePath
, DevicePathInstance
);
224 return UefiDevicePathLibAppendDevicePathInstance (DevicePath
, DevicePathInstance
);
229 Creates a copy of the current device path instance and returns a pointer to the next device path
232 This function creates a copy of the current device path instance. It also updates
233 DevicePath to point to the next device path instance in the device path (or NULL
234 if no more) and updates Size to hold the size of the device path instance copy.
235 If DevicePath is NULL, then NULL is returned.
236 If DevicePath points to a invalid device path, then NULL is returned.
237 If there is not enough memory to allocate space for the new device path, then
239 The memory is allocated from EFI boot services memory. It is the responsibility
240 of the caller to free the memory allocated.
241 If Size is NULL, then ASSERT().
243 @param DevicePath On input, this holds the pointer to the current
244 device path instance. On output, this holds
245 the pointer to the next device path instance
246 or NULL if there are no more device path
247 instances in the device path pointer to a
248 device path data structure.
249 @param Size On output, this holds the size of the device
250 path instance, in bytes or zero, if DevicePath
253 @return A pointer to the current device path instance.
256 EFI_DEVICE_PATH_PROTOCOL
*
258 GetNextDevicePathInstance (
259 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
,
263 if (mDevicePathLibDevicePathUtilities
!= NULL
) {
264 return mDevicePathLibDevicePathUtilities
->GetNextDevicePathInstance (DevicePath
, Size
);
266 return UefiDevicePathLibGetNextDevicePathInstance (DevicePath
, Size
);
271 Creates a device node.
273 This function creates a new device node in a newly allocated buffer of size
274 NodeLength and initializes the device path node header with NodeType and NodeSubType.
275 The new device path node is returned.
276 If NodeLength is smaller than a device path header, then NULL is returned.
277 If there is not enough memory to allocate space for the new device path, then
279 The memory is allocated from EFI boot services memory. It is the responsibility
280 of the caller to free the memory allocated.
282 @param NodeType The device node type for the new device node.
283 @param NodeSubType The device node sub-type for the new device node.
284 @param NodeLength The length of the new device node.
286 @return The new device path.
289 EFI_DEVICE_PATH_PROTOCOL
*
293 IN UINT8 NodeSubType
,
297 if (mDevicePathLibDevicePathUtilities
!= NULL
) {
298 return mDevicePathLibDevicePathUtilities
->CreateDeviceNode (NodeType
, NodeSubType
, NodeLength
);
300 return UefiDevicePathLibCreateDeviceNode (NodeType
, NodeSubType
, NodeLength
);
305 Determines if a device path is single or multi-instance.
307 This function returns TRUE if the device path specified by DevicePath is
309 Otherwise, FALSE is returned.
310 If DevicePath is NULL or invalid, then FALSE is returned.
312 @param DevicePath A pointer to a device path data structure.
314 @retval TRUE DevicePath is multi-instance.
315 @retval FALSE DevicePath is not multi-instance, or DevicePath
321 IsDevicePathMultiInstance (
322 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
325 if (mDevicePathLibDevicePathUtilities
!= NULL
) {
326 return mDevicePathLibDevicePathUtilities
->IsDevicePathMultiInstance (DevicePath
);
328 return UefiDevicePathLibIsDevicePathMultiInstance (DevicePath
);
333 Locate and return the protocol instance identified by the ProtocolGuid.
335 @param ProtocolGuid The GUID of the protocol.
337 @return A pointer to the protocol instance or NULL when absent.
340 UefiDevicePathLibLocateProtocol (
341 EFI_GUID
*ProtocolGuid
346 Status
= gBS
->LocateProtocol (
351 if (EFI_ERROR (Status
)) {
359 Converts a device node to its string representation.
361 @param DeviceNode A Pointer to the device node to be converted.
362 @param DisplayOnly If DisplayOnly is TRUE, then the shorter text representation
363 of the display node is used, where applicable. If DisplayOnly
364 is FALSE, then the longer text representation of the display node
366 @param AllowShortcuts If AllowShortcuts is TRUE, then the shortcut forms of text
367 representation for a device node can be used, where applicable.
369 @return A pointer to the allocated text representation of the device node or NULL if DeviceNode
370 is NULL or there was insufficient memory.
375 ConvertDeviceNodeToText (
376 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DeviceNode
,
377 IN BOOLEAN DisplayOnly
,
378 IN BOOLEAN AllowShortcuts
381 if (mDevicePathLibDevicePathToText
== NULL
) {
382 mDevicePathLibDevicePathToText
= UefiDevicePathLibLocateProtocol (&gEfiDevicePathToTextProtocolGuid
);
384 if (mDevicePathLibDevicePathToText
!= NULL
) {
385 return mDevicePathLibDevicePathToText
->ConvertDeviceNodeToText (DeviceNode
, DisplayOnly
, AllowShortcuts
);
388 return UefiDevicePathLibConvertDeviceNodeToText (DeviceNode
, DisplayOnly
, AllowShortcuts
);
392 Converts a device path to its text representation.
394 @param DevicePath A Pointer to the device to be converted.
395 @param DisplayOnly If DisplayOnly is TRUE, then the shorter text representation
396 of the display node is used, where applicable. If DisplayOnly
397 is FALSE, then the longer text representation of the display node
399 @param AllowShortcuts If AllowShortcuts is TRUE, then the shortcut forms of text
400 representation for a device node can be used, where applicable.
402 @return A pointer to the allocated text representation of the device path or
403 NULL if DeviceNode is NULL or there was insufficient memory.
408 ConvertDevicePathToText (
409 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
410 IN BOOLEAN DisplayOnly
,
411 IN BOOLEAN AllowShortcuts
414 if (mDevicePathLibDevicePathToText
== NULL
) {
415 mDevicePathLibDevicePathToText
= UefiDevicePathLibLocateProtocol (&gEfiDevicePathToTextProtocolGuid
);
417 if (mDevicePathLibDevicePathToText
!= NULL
) {
418 return mDevicePathLibDevicePathToText
->ConvertDevicePathToText (DevicePath
, DisplayOnly
, AllowShortcuts
);
421 return UefiDevicePathLibConvertDevicePathToText (DevicePath
, DisplayOnly
, AllowShortcuts
);
425 Convert text to the binary representation of a device node.
427 @param TextDeviceNode TextDeviceNode points to the text representation of a device
428 node. Conversion starts with the first character and continues
429 until the first non-device node character.
431 @return A pointer to the EFI device node or NULL if TextDeviceNode is NULL or there was
432 insufficient memory or text unsupported.
435 EFI_DEVICE_PATH_PROTOCOL
*
437 ConvertTextToDeviceNode (
438 IN CONST CHAR16
*TextDeviceNode
441 if (mDevicePathLibDevicePathFromText
== NULL
) {
442 mDevicePathLibDevicePathFromText
= UefiDevicePathLibLocateProtocol (&gEfiDevicePathFromTextProtocolGuid
);
444 if (mDevicePathLibDevicePathFromText
!= NULL
) {
445 return mDevicePathLibDevicePathFromText
->ConvertTextToDeviceNode (TextDeviceNode
);
448 return UefiDevicePathLibConvertTextToDeviceNode (TextDeviceNode
);
452 Convert text to the binary representation of a device path.
455 @param TextDevicePath TextDevicePath points to the text representation of a device
456 path. Conversion starts with the first character and continues
457 until the first non-device node character.
459 @return A pointer to the allocated device path or NULL if TextDeviceNode is NULL or
460 there was insufficient memory.
463 EFI_DEVICE_PATH_PROTOCOL
*
465 ConvertTextToDevicePath (
466 IN CONST CHAR16
*TextDevicePath
469 if (mDevicePathLibDevicePathFromText
== NULL
) {
470 mDevicePathLibDevicePathFromText
= UefiDevicePathLibLocateProtocol (&gEfiDevicePathFromTextProtocolGuid
);
472 if (mDevicePathLibDevicePathFromText
!= NULL
) {
473 return mDevicePathLibDevicePathFromText
->ConvertTextToDevicePath (TextDevicePath
);
476 return UefiDevicePathLibConvertTextToDevicePath (TextDevicePath
);