]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/UefiDevicePathLibDevicePathProtocol/UefiDevicePathLib.c
Add 4 APIs to DevicePathLib: ConvertDeviceNodeToText, ConvertDevicePathToText, Conver...
[mirror_edk2.git] / MdePkg / Library / UefiDevicePathLibDevicePathProtocol / UefiDevicePathLib.c
CommitLineData
e386b444 1/** @file\r
f008fc32 2 Library instance that implement UEFI Device Path Library class based on protocol\r
3 gEfiDevicePathUtilitiesProtocolGuid.\r
e386b444 4\r
4d0a30a4 5 Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>\r
19388d29 6 This program and the accompanying materials\r
e386b444 7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
2fc59a00 9 http://opensource.org/licenses/bsd-license.php.\r
e386b444 10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
e386b444 14**/\r
15\r
c892d846 16\r
c7d265a9 17#include <Uefi.h>\r
c892d846 18\r
c7d265a9 19#include <Protocol/DevicePathUtilities.h>\r
4d0a30a4
RN
20#include <Protocol/DevicePathToText.h>\r
21#include <Protocol/DevicePathFromText.h>\r
c892d846 22\r
c7d265a9 23#include <Library/DevicePathLib.h>\r
24#include <Library/DebugLib.h>\r
25#include <Library/BaseLib.h>\r
26#include <Library/MemoryAllocationLib.h>\r
27#include <Library/BaseMemoryLib.h>\r
28#include <Library/UefiBootServicesTableLib.h>\r
771729c7 29#include <Library/PcdLib.h>\r
e386b444 30\r
4d0a30a4
RN
31GLOBAL_REMOVE_IF_UNREFERENCED EFI_DEVICE_PATH_UTILITIES_PROTOCOL *mDevicePathLibDevicePathUtilities = NULL;\r
32GLOBAL_REMOVE_IF_UNREFERENCED EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *mDevicePathLibDevicePathToText = NULL;\r
33GLOBAL_REMOVE_IF_UNREFERENCED EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL *mDevicePathLibDevicePathFromText = NULL;\r
e386b444 34\r
3dc728fb 35//\r
36// Template for an end-of-device path node.\r
37//\r
38GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_DEVICE_PATH_PROTOCOL mUefiDevicePathLibEndDevicePath = {\r
39 END_DEVICE_PATH_TYPE,\r
40 END_ENTIRE_DEVICE_PATH_SUBTYPE,\r
41 {\r
42 END_DEVICE_PATH_LENGTH,\r
43 0\r
44 }\r
45};\r
46\r
e386b444 47/**\r
48 The constructor function caches the pointer to DevicePathUtilites protocol.\r
49 \r
50 The constructor function locates DevicePathUtilities protocol from protocol database.\r
51 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS. \r
52\r
53 @param ImageHandle The firmware allocated handle for the EFI image.\r
54 @param SystemTable A pointer to the EFI System Table.\r
55 \r
56 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r
57\r
58**/\r
59EFI_STATUS\r
60EFIAPI\r
61DevicePathLibConstructor (\r
62 IN EFI_HANDLE ImageHandle,\r
63 IN EFI_SYSTEM_TABLE *SystemTable\r
64 )\r
65{\r
66 EFI_STATUS Status;\r
67\r
68 Status = gBS->LocateProtocol (\r
69 &gEfiDevicePathUtilitiesProtocolGuid,\r
70 NULL,\r
4d0a30a4 71 (VOID**) &mDevicePathLibDevicePathUtilities\r
e386b444 72 );\r
73 ASSERT_EFI_ERROR (Status);\r
4d0a30a4 74 ASSERT (mDevicePathLibDevicePathUtilities != NULL);\r
e386b444 75 return Status;\r
76}\r
77\r
771729c7
RN
78/**\r
79 Determine whether a given device path is valid.\r
80 If DevicePath is NULL, then ASSERT().\r
81\r
82 @param DevicePath A pointer to a device path data structure.\r
83 @param MaxSize The maximum size of the device path data structure.\r
84\r
85 @retval TRUE DevicePath is valid.\r
86 @retval FALSE The length of any node node in the DevicePath is less\r
87 than sizeof (EFI_DEVICE_PATH_PROTOCOL).\r
88 @retval FALSE If MaxSize is not zero, the size of the DevicePath\r
89 exceeds MaxSize.\r
90 @retval FALSE If PcdMaximumDevicePathNodeCount is not zero, the node\r
91 count of the DevicePath exceeds PcdMaximumDevicePathNodeCount.\r
92**/\r
93BOOLEAN\r
94EFIAPI\r
95IsDevicePathValid (\r
96 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
97 IN UINTN MaxSize\r
98 )\r
99{\r
100 UINTN Count;\r
101 UINTN Size;\r
102 UINTN NodeLength;\r
103\r
104 ASSERT (DevicePath != NULL);\r
105\r
106 for (Count = 0, Size = 0; !IsDevicePathEnd (DevicePath); DevicePath = NextDevicePathNode (DevicePath)) {\r
107 NodeLength = DevicePathNodeLength (DevicePath);\r
108 if (NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {\r
109 return FALSE;\r
110 }\r
111\r
112 if (MaxSize > 0) {\r
113 Size += NodeLength;\r
114 if (Size + END_DEVICE_PATH_LENGTH > MaxSize) {\r
115 return FALSE;\r
116 }\r
117 }\r
118\r
119 if (PcdGet32 (PcdMaximumDevicePathNodeCount) > 0) {\r
120 Count++;\r
121 if (Count >= PcdGet32 (PcdMaximumDevicePathNodeCount)) {\r
122 return FALSE;\r
123 }\r
124 }\r
125 }\r
126\r
127 //\r
128 // Only return TRUE when the End Device Path node is valid.\r
129 //\r
130 return (BOOLEAN) (DevicePathNodeLength (DevicePath) == END_DEVICE_PATH_LENGTH);\r
131}\r
132\r
3dc728fb 133/**\r
134 Returns the Type field of a device path node.\r
135\r
136 Returns the Type field of the device path node specified by Node.\r
137\r
138 If Node is NULL, then ASSERT().\r
139\r
140 @param Node A pointer to a device path node data structure.\r
141\r
142 @return The Type field of the device path node specified by Node.\r
143\r
144**/\r
145UINT8\r
5cba121d 146EFIAPI\r
3dc728fb 147DevicePathType (\r
148 IN CONST VOID *Node\r
149 )\r
150{\r
151 ASSERT (Node != NULL);\r
152 return ((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Type;\r
153}\r
154\r
155/**\r
156 Returns the SubType field of a device path node.\r
157\r
158 Returns the SubType field of the device path node specified by Node.\r
159\r
160 If Node is NULL, then ASSERT().\r
161\r
162 @param Node A pointer to a device path node data structure.\r
163\r
164 @return The SubType field of the device path node specified by Node.\r
165\r
166**/\r
167UINT8\r
5cba121d 168EFIAPI\r
3dc728fb 169DevicePathSubType (\r
170 IN CONST VOID *Node\r
171 )\r
172{\r
173 ASSERT (Node != NULL);\r
174 return ((EFI_DEVICE_PATH_PROTOCOL *)(Node))->SubType;\r
175}\r
176\r
177/**\r
178 Returns the 16-bit Length field of a device path node.\r
179\r
180 Returns the 16-bit Length field of the device path node specified by Node. \r
181 Node is not required to be aligned on a 16-bit boundary, so it is recommended\r
182 that a function such as ReadUnaligned16() be used to extract the contents of \r
183 the Length field.\r
184\r
185 If Node is NULL, then ASSERT().\r
186\r
187 @param Node A pointer to a device path node data structure.\r
188\r
189 @return The 16-bit Length field of the device path node specified by Node.\r
190\r
191**/\r
192UINTN\r
5cba121d 193EFIAPI\r
3dc728fb 194DevicePathNodeLength (\r
195 IN CONST VOID *Node\r
196 )\r
197{\r
771729c7
RN
198 UINTN Length;\r
199\r
3dc728fb 200 ASSERT (Node != NULL);\r
771729c7
RN
201 Length = ReadUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0]);\r
202 ASSERT (Length >= sizeof (EFI_DEVICE_PATH_PROTOCOL));\r
203 return Length;\r
3dc728fb 204}\r
205\r
206/**\r
207 Returns a pointer to the next node in a device path.\r
208\r
58380e9c 209 Returns a pointer to the device path node that follows the device path node \r
210 specified by Node.\r
3dc728fb 211\r
212 If Node is NULL, then ASSERT().\r
213\r
214 @param Node A pointer to a device path node data structure.\r
215\r
58380e9c 216 @return a pointer to the device path node that follows the device path node \r
217 specified by Node.\r
3dc728fb 218\r
219**/\r
220EFI_DEVICE_PATH_PROTOCOL *\r
5cba121d 221EFIAPI\r
3dc728fb 222NextDevicePathNode (\r
223 IN CONST VOID *Node\r
224 )\r
225{\r
226 ASSERT (Node != NULL);\r
227 return (EFI_DEVICE_PATH_PROTOCOL *)((UINT8 *)(Node) + DevicePathNodeLength(Node));\r
228}\r
229\r
230/**\r
231 Determines if a device path node is an end node of a device path.\r
58380e9c 232 This includes nodes that are the end of a device path instance and nodes that \r
233 are the end of an entire device path.\r
3dc728fb 234\r
235 Determines if the device path node specified by Node is an end node of a device path. \r
236 This includes nodes that are the end of a device path instance and nodes that are the \r
237 end of an entire device path. If Node represents an end node of a device path, \r
238 then TRUE is returned. Otherwise, FALSE is returned.\r
239\r
240 If Node is NULL, then ASSERT().\r
241\r
242 @param Node A pointer to a device path node data structure.\r
243\r
244 @retval TRUE The device path node specified by Node is an end node of a device path.\r
58380e9c 245 @retval FALSE The device path node specified by Node is not an end node of \r
246 a device path.\r
3dc728fb 247 \r
248**/\r
249BOOLEAN\r
5cba121d 250EFIAPI\r
3dc728fb 251IsDevicePathEndType (\r
252 IN CONST VOID *Node\r
253 )\r
254{\r
255 ASSERT (Node != NULL);\r
a357faa3 256 return (BOOLEAN) (DevicePathType (Node) == END_DEVICE_PATH_TYPE);\r
3dc728fb 257}\r
258\r
259/**\r
260 Determines if a device path node is an end node of an entire device path.\r
261\r
58380e9c 262 Determines if a device path node specified by Node is an end node of an entire \r
263 device path.\r
264 If Node represents the end of an entire device path, then TRUE is returned. \r
265 Otherwise, FALSE is returned.\r
3dc728fb 266\r
267 If Node is NULL, then ASSERT().\r
268\r
269 @param Node A pointer to a device path node data structure.\r
270\r
271 @retval TRUE The device path node specified by Node is the end of an entire device path.\r
272 @retval FALSE The device path node specified by Node is not the end of an entire device path.\r
273\r
274**/\r
275BOOLEAN\r
5cba121d 276EFIAPI\r
3dc728fb 277IsDevicePathEnd (\r
278 IN CONST VOID *Node\r
279 )\r
280{\r
281 ASSERT (Node != NULL);\r
1bfc7438 282 return (BOOLEAN) (IsDevicePathEndType (Node) && DevicePathSubType(Node) == END_ENTIRE_DEVICE_PATH_SUBTYPE);\r
3dc728fb 283}\r
284\r
285/**\r
286 Determines if a device path node is an end node of a device path instance.\r
287\r
58380e9c 288 Determines if a device path node specified by Node is an end node of a device \r
289 path instance.\r
290 If Node represents the end of a device path instance, then TRUE is returned. \r
291 Otherwise, FALSE is returned.\r
3dc728fb 292\r
293 If Node is NULL, then ASSERT().\r
294\r
295 @param Node A pointer to a device path node data structure.\r
296\r
58380e9c 297 @retval TRUE The device path node specified by Node is the end of a device \r
298 path instance.\r
299 @retval FALSE The device path node specified by Node is not the end of a \r
300 device path instance.\r
3dc728fb 301\r
302**/\r
303BOOLEAN\r
5cba121d 304EFIAPI\r
3dc728fb 305IsDevicePathEndInstance (\r
306 IN CONST VOID *Node\r
307 )\r
308{\r
309 ASSERT (Node != NULL);\r
1bfc7438 310 return (BOOLEAN) (IsDevicePathEndType (Node) && DevicePathSubType(Node) == END_INSTANCE_DEVICE_PATH_SUBTYPE);\r
3dc728fb 311}\r
312\r
313/**\r
314 Sets the length, in bytes, of a device path node.\r
315\r
316 Sets the length of the device path node specified by Node to the value specified \r
317 by NodeLength. NodeLength is returned. Node is not required to be aligned on \r
318 a 16-bit boundary, so it is recommended that a function such as WriteUnaligned16()\r
319 be used to set the contents of the Length field.\r
320\r
321 If Node is NULL, then ASSERT().\r
771729c7
RN
322 If NodeLength >= SIZE_64KB, then ASSERT().\r
323 If NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL), then ASSERT().\r
3dc728fb 324\r
325 @param Node A pointer to a device path node data structure.\r
326 @param Length The length, in bytes, of the device path node.\r
327\r
328 @return Length\r
329\r
330**/\r
331UINT16\r
5cba121d 332EFIAPI\r
3dc728fb 333SetDevicePathNodeLength (\r
9bb407c6 334 IN OUT VOID *Node,\r
8f0dd97e 335 IN UINTN Length\r
3dc728fb 336 )\r
337{\r
338 ASSERT (Node != NULL);\r
771729c7 339 ASSERT ((Length >= sizeof (EFI_DEVICE_PATH_PROTOCOL)) && (Length < SIZE_64KB));\r
8f0dd97e 340 return WriteUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0], (UINT16)(Length));\r
3dc728fb 341}\r
342\r
343/**\r
344 Fills in all the fields of a device path node that is the end of an entire device path.\r
345\r
346 Fills in all the fields of a device path node specified by Node so Node represents \r
347 the end of an entire device path. The Type field of Node is set to \r
348 END_DEVICE_PATH_TYPE, the SubType field of Node is set to \r
349 END_ENTIRE_DEVICE_PATH_SUBTYPE, and the Length field of Node is set to \r
350 END_DEVICE_PATH_LENGTH. Node is not required to be aligned on a 16-bit boundary, \r
351 so it is recommended that a function such as WriteUnaligned16() be used to set \r
352 the contents of the Length field. \r
353\r
354 If Node is NULL, then ASSERT(). \r
355\r
356 @param Node A pointer to a device path node data structure.\r
357\r
358**/\r
359VOID\r
5cba121d 360EFIAPI\r
3dc728fb 361SetDevicePathEndNode (\r
9bb407c6 362 OUT VOID *Node\r
3dc728fb 363 )\r
364{\r
365 ASSERT (Node != NULL);\r
366 CopyMem (Node, &mUefiDevicePathLibEndDevicePath, sizeof (mUefiDevicePathLibEndDevicePath));\r
367}\r
368\r
e386b444 369/**\r
370 Returns the size of a device path in bytes.\r
371\r
771729c7
RN
372 This function returns the size, in bytes, of the device path data structure \r
373 specified by DevicePath including the end of device path node.\r
374 If DevicePath is NULL or invalid, then 0 is returned.\r
e386b444 375\r
771729c7
RN
376 @param DevicePath A pointer to a device path data structure.\r
377\r
378 @retval 0 If DevicePath is NULL or invalid.\r
379 @retval Others The size of a device path in bytes.\r
e386b444 380\r
381**/\r
382UINTN\r
383EFIAPI\r
384GetDevicePathSize (\r
385 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
386 )\r
387{\r
4d0a30a4 388 return mDevicePathLibDevicePathUtilities->GetDevicePathSize (DevicePath);\r
e386b444 389}\r
390\r
391/**\r
6a3f4ef9 392 Creates a new copy of an existing device path.\r
e386b444 393\r
58380e9c 394 This function allocates space for a new copy of the device path specified by \r
395 DevicePath. If DevicePath is NULL, then NULL is returned. \r
396 If the memory is successfully allocated, then the\r
e386b444 397 contents of DevicePath are copied to the newly allocated buffer, and a pointer to that buffer\r
398 is returned. Otherwise, NULL is returned. \r
3e5c3238 399 The memory for the new device path is allocated from EFI boot services memory. \r
400 It is the responsibility of the caller to free the memory allocated. \r
e386b444 401 \r
402 @param DevicePath A pointer to a device path data structure.\r
403\r
771729c7 404 @retval NULL If DevicePath is NULL or invalid.\r
3e5c3238 405 @retval Others A pointer to the duplicated device path.\r
406 \r
e386b444 407**/\r
408EFI_DEVICE_PATH_PROTOCOL *\r
409EFIAPI\r
410DuplicateDevicePath (\r
411 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
412 )\r
413{\r
4d0a30a4 414 return mDevicePathLibDevicePathUtilities->DuplicateDevicePath (DevicePath);\r
e386b444 415}\r
416\r
417/**\r
418 Creates a new device path by appending a second device path to a first device path.\r
419\r
420 This function creates a new device path by appending a copy of SecondDevicePath to a copy of\r
421 FirstDevicePath in a newly allocated buffer. Only the end-of-device-path device node from\r
422 SecondDevicePath is retained. The newly created device path is returned. \r
423 If FirstDevicePath is NULL, then it is ignored, and a duplicate of SecondDevicePath is returned. \r
424 If SecondDevicePath is NULL, then it is ignored, and a duplicate of FirstDevicePath is returned. \r
98a14db6 425 If both FirstDevicePath and SecondDevicePath are NULL, then a copy of an end-of-device-path is\r
426 returned. \r
e386b444 427 If there is not enough memory for the newly allocated buffer, then NULL is returned.\r
428 The memory for the new device path is allocated from EFI boot services memory. It is the\r
429 responsibility of the caller to free the memory allocated.\r
430\r
431 @param FirstDevicePath A pointer to a device path data structure.\r
432 @param SecondDevicePath A pointer to a device path data structure.\r
3e5c3238 433 \r
434 @retval NULL If there is not enough memory for the newly allocated buffer.\r
771729c7 435 @retval NULL If FirstDevicePath or SecondDevicePath is invalid.\r
3e5c3238 436 @retval Others A pointer to the new device path if success.\r
58380e9c 437 Or a copy an end-of-device-path if both FirstDevicePath and \r
438 SecondDevicePath are NULL.\r
e386b444 439\r
440**/\r
441EFI_DEVICE_PATH_PROTOCOL *\r
442EFIAPI\r
443AppendDevicePath (\r
444 IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath, OPTIONAL\r
445 IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath OPTIONAL\r
446 )\r
447{\r
4d0a30a4 448 return mDevicePathLibDevicePathUtilities->AppendDevicePath (FirstDevicePath, SecondDevicePath);\r
e386b444 449}\r
450\r
451/**\r
452 Creates a new path by appending the device node to the device path.\r
453\r
58380e9c 454 This function creates a new device path by appending a copy of the device node \r
455 specified by DevicePathNode to a copy of the device path specified by DevicePath \r
456 in an allocated buffer.\r
e386b444 457 The end-of-device-path device node is moved after the end of the appended device node.\r
98a14db6 458 If DevicePathNode is NULL then a copy of DevicePath is returned.\r
58380e9c 459 If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device \r
460 path device node is returned.\r
461 If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path \r
462 device node is returned.\r
463 If there is not enough memory to allocate space for the new device path, then \r
464 NULL is returned. \r
465 The memory is allocated from EFI boot services memory. It is the responsibility \r
466 of the caller to free the memory allocated.\r
e386b444 467\r
468 @param DevicePath A pointer to a device path data structure.\r
469 @param DevicePathNode A pointer to a single device path node.\r
470\r
3e5c3238 471 @retval NULL If there is not enough memory for the new device path.\r
472 @retval Others A pointer to the new device path if success.\r
473 A copy of DevicePathNode followed by an end-of-device-path node \r
474 if both FirstDevicePath and SecondDevicePath are NULL.\r
58380e9c 475 A copy of an end-of-device-path node if both FirstDevicePath \r
476 and SecondDevicePath are NULL.\r
e386b444 477\r
478**/\r
479EFI_DEVICE_PATH_PROTOCOL *\r
480EFIAPI\r
481AppendDevicePathNode (\r
482 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL\r
483 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathNode OPTIONAL\r
484 )\r
485{\r
4d0a30a4 486 return mDevicePathLibDevicePathUtilities->AppendDeviceNode (DevicePath, DevicePathNode);\r
e386b444 487}\r
488\r
489/**\r
58380e9c 490 Creates a new device path by appending the specified device path instance to \r
491 the specified device path.\r
e386b444 492 \r
58380e9c 493 This function creates a new device path by appending a copy of the device path \r
494 instance specified by DevicePathInstance to a copy of the device path specified \r
495 by DevicePath in a allocated buffer.\r
496 The end-of-device-path device node is moved after the end of the appended device \r
497 path instance and a new end-of-device-path-instance node is inserted between. \r
e386b444 498 If DevicePath is NULL, then a copy if DevicePathInstance is returned.\r
499 If DevicePathInstance is NULL, then NULL is returned.\r
771729c7 500 If DevicePath or DevicePathInstance is invalid, then NULL is returned.\r
58380e9c 501 If there is not enough memory to allocate space for the new device path, then \r
502 NULL is returned. \r
503 The memory is allocated from EFI boot services memory. It is the responsibility \r
504 of the caller to free the memory allocated.\r
e386b444 505 \r
506 @param DevicePath A pointer to a device path data structure.\r
507 @param DevicePathInstance A pointer to a device path instance.\r
508\r
509 @return A pointer to the new device path.\r
510\r
511**/\r
512EFI_DEVICE_PATH_PROTOCOL *\r
513EFIAPI\r
514AppendDevicePathInstance (\r
515 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL\r
516 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathInstance OPTIONAL\r
517 )\r
518{\r
4d0a30a4 519 return mDevicePathLibDevicePathUtilities->AppendDevicePathInstance (DevicePath, DevicePathInstance);\r
e386b444 520}\r
521\r
522/**\r
58380e9c 523 Creates a copy of the current device path instance and returns a pointer to the \r
524 next device path instance.\r
e386b444 525\r
58380e9c 526 This function creates a copy of the current device path instance. It also updates \r
527 DevicePath to point to the next device path instance in the device path (or NULL \r
528 if no more) and updates Size to hold the size of the device path instance copy.\r
e386b444 529 If DevicePath is NULL, then NULL is returned.\r
58380e9c 530 If there is not enough memory to allocate space for the new device path, then \r
531 NULL is returned. \r
532 The memory is allocated from EFI boot services memory. It is the responsibility \r
533 of the caller to free the memory allocated.\r
e386b444 534 If Size is NULL, then ASSERT().\r
535 \r
58380e9c 536 @param DevicePath On input, this holds the pointer to the current \r
537 device path instance. On output, this holds \r
538 the pointer to the next device path instance \r
539 or NULL if there are no more device path\r
540 instances in the device path pointer to a \r
541 device path data structure.\r
542 @param Size On output, this holds the size of the device \r
543 path instance, in bytes or zero, if DevicePath \r
544 is NULL.\r
e386b444 545\r
546 @return A pointer to the current device path instance.\r
547\r
548**/\r
549EFI_DEVICE_PATH_PROTOCOL *\r
550EFIAPI\r
551GetNextDevicePathInstance (\r
552 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath,\r
553 OUT UINTN *Size\r
554 )\r
555{\r
556 ASSERT (Size != NULL);\r
4d0a30a4 557 return mDevicePathLibDevicePathUtilities->GetNextDevicePathInstance (DevicePath, Size);\r
e386b444 558}\r
559\r
560/**\r
3e5c3238 561 Creates a device node.\r
e386b444 562\r
58380e9c 563 This function creates a new device node in a newly allocated buffer of size \r
564 NodeLength and initializes the device path node header with NodeType and NodeSubType. \r
565 The new device path node is returned.\r
e386b444 566 If NodeLength is smaller than a device path header, then NULL is returned. \r
58380e9c 567 If there is not enough memory to allocate space for the new device path, then \r
568 NULL is returned. \r
569 The memory is allocated from EFI boot services memory. It is the responsibility \r
570 of the caller to\r
e386b444 571 free the memory allocated.\r
572\r
573 @param NodeType The device node type for the new device node.\r
574 @param NodeSubType The device node sub-type for the new device node.\r
575 @param NodeLength The length of the new device node.\r
576\r
3e5c3238 577 @return The new device path.\r
e386b444 578\r
579**/\r
580EFI_DEVICE_PATH_PROTOCOL *\r
581EFIAPI\r
582CreateDeviceNode (\r
583 IN UINT8 NodeType,\r
584 IN UINT8 NodeSubType,\r
585 IN UINT16 NodeLength\r
586 )\r
587{\r
4d0a30a4 588 return mDevicePathLibDevicePathUtilities->CreateDeviceNode (NodeType, NodeSubType, NodeLength);\r
e386b444 589}\r
590\r
591/**\r
592 Determines if a device path is single or multi-instance.\r
593\r
771729c7 594 This function returns TRUE if the device path specified by DevicePath is\r
58380e9c 595 multi-instance.\r
771729c7
RN
596 Otherwise, FALSE is returned.\r
597 If DevicePath is NULL or invalid, then FALSE is returned.\r
e386b444 598\r
599 @param DevicePath A pointer to a device path data structure.\r
600\r
601 @retval TRUE DevicePath is multi-instance.\r
771729c7
RN
602 @retval FALSE DevicePath is not multi-instance, or DevicePath \r
603 is NULL or invalid.\r
e386b444 604\r
605**/\r
606BOOLEAN\r
607EFIAPI\r
608IsDevicePathMultiInstance (\r
609 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
610 )\r
611{\r
4d0a30a4 612 return mDevicePathLibDevicePathUtilities->IsDevicePathMultiInstance (DevicePath);\r
e386b444 613}\r
614\r
615/**\r
616 Retrieves the device path protocol from a handle.\r
617\r
58380e9c 618 This function returns the device path protocol from the handle specified by Handle. \r
619 If Handle is NULL or Handle does not contain a device path protocol, then NULL \r
620 is returned.\r
e386b444 621 \r
58380e9c 622 @param Handle The handle from which to retrieve the device \r
623 path protocol.\r
e386b444 624\r
625 @return The device path protocol from the handle specified by Handle.\r
626\r
627**/\r
628EFI_DEVICE_PATH_PROTOCOL *\r
629EFIAPI\r
630DevicePathFromHandle (\r
631 IN EFI_HANDLE Handle\r
632 )\r
633{\r
634 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
635 EFI_STATUS Status;\r
636\r
637 Status = gBS->HandleProtocol (\r
638 Handle,\r
639 &gEfiDevicePathProtocolGuid,\r
640 (VOID *) &DevicePath\r
641 );\r
642 if (EFI_ERROR (Status)) {\r
643 DevicePath = NULL;\r
644 }\r
645 return DevicePath;\r
646}\r
647\r
648/**\r
649 Allocates a device path for a file and appends it to an existing device path.\r
650\r
58380e9c 651 If Device is a valid device handle that contains a device path protocol, then \r
652 a device path for the file specified by FileName is allocated and appended to \r
653 the device path associated with the handle Device. The allocated device path \r
654 is returned. If Device is NULL or Device is a handle that does not support the \r
655 device path protocol, then a device path containing a single device path node \r
656 for the file specified by FileName is allocated and returned.\r
657 The memory for the new device path is allocated from EFI boot services memory. \r
658 It is the responsibility of the caller to free the memory allocated.\r
3e5c3238 659 \r
e386b444 660 If FileName is NULL, then ASSERT().\r
3e5c3238 661 If FileName is not aligned on a 16-bit boundary, then ASSERT().\r
e386b444 662\r
58380e9c 663 @param Device A pointer to a device handle. This parameter \r
664 is optional and may be NULL.\r
e386b444 665 @param FileName A pointer to a Null-terminated Unicode string.\r
666\r
3e5c3238 667 @return The allocated device path.\r
e386b444 668\r
669**/\r
670EFI_DEVICE_PATH_PROTOCOL *\r
671EFIAPI\r
672FileDevicePath (\r
673 IN EFI_HANDLE Device, OPTIONAL\r
674 IN CONST CHAR16 *FileName\r
675 )\r
676{\r
677 UINTN Size;\r
678 FILEPATH_DEVICE_PATH *FilePath;\r
679 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
680 EFI_DEVICE_PATH_PROTOCOL *FileDevicePath;\r
681\r
682 DevicePath = NULL;\r
683\r
684 Size = StrSize (FileName);\r
e5dab016 685 FileDevicePath = AllocatePool (Size + SIZE_OF_FILEPATH_DEVICE_PATH + END_DEVICE_PATH_LENGTH);\r
e386b444 686 if (FileDevicePath != NULL) {\r
687 FilePath = (FILEPATH_DEVICE_PATH *) FileDevicePath;\r
688 FilePath->Header.Type = MEDIA_DEVICE_PATH;\r
689 FilePath->Header.SubType = MEDIA_FILEPATH_DP;\r
690 CopyMem (&FilePath->PathName, FileName, Size);\r
691 SetDevicePathNodeLength (&FilePath->Header, Size + SIZE_OF_FILEPATH_DEVICE_PATH);\r
692 SetDevicePathEndNode (NextDevicePathNode (&FilePath->Header));\r
693\r
694 if (Device != NULL) {\r
695 DevicePath = DevicePathFromHandle (Device);\r
696 }\r
697\r
698 DevicePath = AppendDevicePath (DevicePath, FileDevicePath);\r
699 FreePool (FileDevicePath);\r
700 }\r
701\r
702 return DevicePath;\r
703}\r
4d0a30a4
RN
704\r
705/**\r
706 Locate and return the protocol instance identified by the ProtocolGuid.\r
707\r
708 @param ProtocolGuid The GUID of the protocol.\r
709\r
710 @return A pointer to the protocol instance or NULL when absent.\r
711**/\r
712VOID *\r
713UefiDevicePathLibLocateProtocol (\r
714 EFI_GUID *ProtocolGuid\r
715 )\r
716{\r
717 EFI_STATUS Status;\r
718 VOID *Protocol;\r
719 Status = gBS->LocateProtocol (\r
720 ProtocolGuid,\r
721 NULL,\r
722 (VOID**) &Protocol\r
723 );\r
724 if (EFI_ERROR (Status)) {\r
725 return NULL;\r
726 } else {\r
727 return Protocol;\r
728 }\r
729}\r
730\r
731/**\r
732 Converts a device node to its string representation.\r
733\r
734 @param DeviceNode A Pointer to the device node to be converted.\r
735 @param DisplayOnly If DisplayOnly is TRUE, then the shorter text representation\r
736 of the display node is used, where applicable. If DisplayOnly\r
737 is FALSE, then the longer text representation of the display node\r
738 is used.\r
739 @param AllowShortcuts If AllowShortcuts is TRUE, then the shortcut forms of text\r
740 representation for a device node can be used, where applicable.\r
741\r
742 @return A pointer to the allocated text representation of the device node or NULL if DeviceNode\r
743 is NULL or there was insufficient memory.\r
744\r
745**/\r
746CHAR16 *\r
747EFIAPI\r
748ConvertDeviceNodeToText (\r
749 IN CONST EFI_DEVICE_PATH_PROTOCOL *DeviceNode,\r
750 IN BOOLEAN DisplayOnly,\r
751 IN BOOLEAN AllowShortcuts\r
752 )\r
753{\r
754 if (mDevicePathLibDevicePathToText == NULL) {\r
755 mDevicePathLibDevicePathToText = UefiDevicePathLibLocateProtocol (&gEfiDevicePathToTextProtocolGuid);\r
756 }\r
757 if (mDevicePathLibDevicePathToText != NULL) {\r
758 return mDevicePathLibDevicePathToText->ConvertDeviceNodeToText (DeviceNode, DisplayOnly, AllowShortcuts);\r
759 } else {\r
760 return NULL;\r
761 }\r
762}\r
763\r
764/**\r
765 Converts a device path to its text representation.\r
766\r
767 @param DevicePath A Pointer to the device to be converted.\r
768 @param DisplayOnly If DisplayOnly is TRUE, then the shorter text representation\r
769 of the display node is used, where applicable. If DisplayOnly\r
770 is FALSE, then the longer text representation of the display node\r
771 is used.\r
772 @param AllowShortcuts If AllowShortcuts is TRUE, then the shortcut forms of text\r
773 representation for a device node can be used, where applicable.\r
774\r
775 @return A pointer to the allocated text representation of the device path or\r
776 NULL if DeviceNode is NULL or there was insufficient memory.\r
777\r
778**/\r
779CHAR16 *\r
780EFIAPI\r
781ConvertDevicePathToText (\r
782 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
783 IN BOOLEAN DisplayOnly,\r
784 IN BOOLEAN AllowShortcuts\r
785 )\r
786{\r
787 if (mDevicePathLibDevicePathToText == NULL) {\r
788 mDevicePathLibDevicePathToText = UefiDevicePathLibLocateProtocol (&gEfiDevicePathToTextProtocolGuid);\r
789 }\r
790 if (mDevicePathLibDevicePathToText != NULL) {\r
791 return mDevicePathLibDevicePathToText->ConvertDevicePathToText (DevicePath, DisplayOnly, AllowShortcuts);\r
792 } else {\r
793 return NULL;\r
794 }\r
795}\r
796\r
797/**\r
798 Convert text to the binary representation of a device node.\r
799\r
800 @param TextDeviceNode TextDeviceNode points to the text representation of a device\r
801 node. Conversion starts with the first character and continues\r
802 until the first non-device node character.\r
803\r
804 @return A pointer to the EFI device node or NULL if TextDeviceNode is NULL or there was\r
805 insufficient memory or text unsupported.\r
806\r
807**/\r
808EFI_DEVICE_PATH_PROTOCOL *\r
809EFIAPI\r
810ConvertTextToDeviceNode (\r
811 IN CONST CHAR16 *TextDeviceNode\r
812 )\r
813{\r
814 if (mDevicePathLibDevicePathFromText == NULL) {\r
815 mDevicePathLibDevicePathFromText = UefiDevicePathLibLocateProtocol (&gEfiDevicePathFromTextProtocolGuid);\r
816 }\r
817 if (mDevicePathLibDevicePathFromText != NULL) {\r
818 return mDevicePathLibDevicePathFromText->ConvertTextToDeviceNode (TextDeviceNode);\r
819 } else {\r
820 return NULL;\r
821 }\r
822}\r
823\r
824/**\r
825 Convert text to the binary representation of a device path.\r
826\r
827\r
828 @param TextDevicePath TextDevicePath points to the text representation of a device\r
829 path. Conversion starts with the first character and continues\r
830 until the first non-device node character.\r
831\r
832 @return A pointer to the allocated device path or NULL if TextDeviceNode is NULL or\r
833 there was insufficient memory.\r
834\r
835**/\r
836EFI_DEVICE_PATH_PROTOCOL *\r
837EFIAPI\r
838ConvertTextToDevicePath (\r
839 IN CONST CHAR16 *TextDevicePath\r
840 )\r
841{\r
842 if (mDevicePathLibDevicePathFromText == NULL) {\r
843 mDevicePathLibDevicePathFromText = UefiDevicePathLibLocateProtocol (&gEfiDevicePathFromTextProtocolGuid);\r
844 }\r
845 if (mDevicePathLibDevicePathFromText != NULL) {\r
846 return mDevicePathLibDevicePathFromText->ConvertTextToDevicePath (TextDevicePath);\r
847 } else {\r
848 return NULL;\r
849 }\r
850}\r
851\r