]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/UefiDevicePathLibDevicePathProtocol/UefiDevicePathLib.c
MdePkg DevicePathLib: Rollback former change.
[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
d9c2c954 5 Copyright (c) 2006 - 2016, 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
1420143f 106 for (Count = 0, Size = 0; !IsDevicePathEnd (DevicePath); DevicePath = NextDevicePathNode (DevicePath)) {\r
771729c7
RN
107 NodeLength = DevicePathNodeLength (DevicePath);\r
108 if (NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {\r
109 return FALSE;\r
110 }\r
111\r
1420143f
ED
112 if (MaxSize > 0) {\r
113 Size += NodeLength;\r
114 if (Size + END_DEVICE_PATH_LENGTH > MaxSize) {\r
115 return FALSE;\r
116 }\r
771729c7
RN
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
198 ASSERT (Node != NULL);\r
0b13fe74 199 return ReadUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0]);\r
3dc728fb 200}\r
201\r
202/**\r
203 Returns a pointer to the next node in a device path.\r
204\r
58380e9c 205 Returns a pointer to the device path node that follows the device path node \r
206 specified by Node.\r
3dc728fb 207\r
208 If Node is NULL, then ASSERT().\r
209\r
210 @param Node A pointer to a device path node data structure.\r
211\r
58380e9c 212 @return a pointer to the device path node that follows the device path node \r
213 specified by Node.\r
3dc728fb 214\r
215**/\r
216EFI_DEVICE_PATH_PROTOCOL *\r
5cba121d 217EFIAPI\r
3dc728fb 218NextDevicePathNode (\r
219 IN CONST VOID *Node\r
220 )\r
221{\r
222 ASSERT (Node != NULL);\r
223 return (EFI_DEVICE_PATH_PROTOCOL *)((UINT8 *)(Node) + DevicePathNodeLength(Node));\r
224}\r
225\r
226/**\r
227 Determines if a device path node is an end node of a device path.\r
58380e9c 228 This includes nodes that are the end of a device path instance and nodes that \r
229 are the end of an entire device path.\r
3dc728fb 230\r
231 Determines if the device path node specified by Node is an end node of a device path. \r
232 This includes nodes that are the end of a device path instance and nodes that are the \r
233 end of an entire device path. If Node represents an end node of a device path, \r
234 then TRUE is returned. Otherwise, FALSE is returned.\r
235\r
236 If Node is NULL, then ASSERT().\r
237\r
238 @param Node A pointer to a device path node data structure.\r
239\r
240 @retval TRUE The device path node specified by Node is an end node of a device path.\r
58380e9c 241 @retval FALSE The device path node specified by Node is not an end node of \r
242 a device path.\r
3dc728fb 243 \r
244**/\r
245BOOLEAN\r
5cba121d 246EFIAPI\r
3dc728fb 247IsDevicePathEndType (\r
248 IN CONST VOID *Node\r
249 )\r
250{\r
251 ASSERT (Node != NULL);\r
a357faa3 252 return (BOOLEAN) (DevicePathType (Node) == END_DEVICE_PATH_TYPE);\r
3dc728fb 253}\r
254\r
255/**\r
256 Determines if a device path node is an end node of an entire device path.\r
257\r
58380e9c 258 Determines if a device path node specified by Node is an end node of an entire \r
259 device path.\r
260 If Node represents the end of an entire device path, then TRUE is returned. \r
261 Otherwise, FALSE is returned.\r
3dc728fb 262\r
263 If Node is NULL, then ASSERT().\r
264\r
265 @param Node A pointer to a device path node data structure.\r
266\r
267 @retval TRUE The device path node specified by Node is the end of an entire device path.\r
268 @retval FALSE The device path node specified by Node is not the end of an entire device path.\r
269\r
270**/\r
271BOOLEAN\r
5cba121d 272EFIAPI\r
3dc728fb 273IsDevicePathEnd (\r
274 IN CONST VOID *Node\r
275 )\r
276{\r
277 ASSERT (Node != NULL);\r
1bfc7438 278 return (BOOLEAN) (IsDevicePathEndType (Node) && DevicePathSubType(Node) == END_ENTIRE_DEVICE_PATH_SUBTYPE);\r
3dc728fb 279}\r
280\r
281/**\r
282 Determines if a device path node is an end node of a device path instance.\r
283\r
58380e9c 284 Determines if a device path node specified by Node is an end node of a device \r
285 path instance.\r
286 If Node represents the end of a device path instance, then TRUE is returned. \r
287 Otherwise, FALSE is returned.\r
3dc728fb 288\r
289 If Node is NULL, then ASSERT().\r
290\r
291 @param Node A pointer to a device path node data structure.\r
292\r
58380e9c 293 @retval TRUE The device path node specified by Node is the end of a device \r
294 path instance.\r
295 @retval FALSE The device path node specified by Node is not the end of a \r
296 device path instance.\r
3dc728fb 297\r
298**/\r
299BOOLEAN\r
5cba121d 300EFIAPI\r
3dc728fb 301IsDevicePathEndInstance (\r
302 IN CONST VOID *Node\r
303 )\r
304{\r
305 ASSERT (Node != NULL);\r
1bfc7438 306 return (BOOLEAN) (IsDevicePathEndType (Node) && DevicePathSubType(Node) == END_INSTANCE_DEVICE_PATH_SUBTYPE);\r
3dc728fb 307}\r
308\r
309/**\r
310 Sets the length, in bytes, of a device path node.\r
311\r
312 Sets the length of the device path node specified by Node to the value specified \r
313 by NodeLength. NodeLength is returned. Node is not required to be aligned on \r
314 a 16-bit boundary, so it is recommended that a function such as WriteUnaligned16()\r
315 be used to set the contents of the Length field.\r
316\r
317 If Node is NULL, then ASSERT().\r
771729c7
RN
318 If NodeLength >= SIZE_64KB, then ASSERT().\r
319 If NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL), then ASSERT().\r
3dc728fb 320\r
321 @param Node A pointer to a device path node data structure.\r
322 @param Length The length, in bytes, of the device path node.\r
323\r
324 @return Length\r
325\r
326**/\r
327UINT16\r
5cba121d 328EFIAPI\r
3dc728fb 329SetDevicePathNodeLength (\r
9bb407c6 330 IN OUT VOID *Node,\r
8f0dd97e 331 IN UINTN Length\r
3dc728fb 332 )\r
333{\r
334 ASSERT (Node != NULL);\r
771729c7 335 ASSERT ((Length >= sizeof (EFI_DEVICE_PATH_PROTOCOL)) && (Length < SIZE_64KB));\r
8f0dd97e 336 return WriteUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0], (UINT16)(Length));\r
3dc728fb 337}\r
338\r
339/**\r
340 Fills in all the fields of a device path node that is the end of an entire device path.\r
341\r
342 Fills in all the fields of a device path node specified by Node so Node represents \r
343 the end of an entire device path. The Type field of Node is set to \r
344 END_DEVICE_PATH_TYPE, the SubType field of Node is set to \r
345 END_ENTIRE_DEVICE_PATH_SUBTYPE, and the Length field of Node is set to \r
346 END_DEVICE_PATH_LENGTH. Node is not required to be aligned on a 16-bit boundary, \r
347 so it is recommended that a function such as WriteUnaligned16() be used to set \r
348 the contents of the Length field. \r
349\r
350 If Node is NULL, then ASSERT(). \r
351\r
352 @param Node A pointer to a device path node data structure.\r
353\r
354**/\r
355VOID\r
5cba121d 356EFIAPI\r
3dc728fb 357SetDevicePathEndNode (\r
9bb407c6 358 OUT VOID *Node\r
3dc728fb 359 )\r
360{\r
361 ASSERT (Node != NULL);\r
362 CopyMem (Node, &mUefiDevicePathLibEndDevicePath, sizeof (mUefiDevicePathLibEndDevicePath));\r
363}\r
364\r
e386b444 365/**\r
366 Returns the size of a device path in bytes.\r
367\r
771729c7
RN
368 This function returns the size, in bytes, of the device path data structure \r
369 specified by DevicePath including the end of device path node.\r
370 If DevicePath is NULL or invalid, then 0 is returned.\r
e386b444 371\r
771729c7
RN
372 @param DevicePath A pointer to a device path data structure.\r
373\r
374 @retval 0 If DevicePath is NULL or invalid.\r
375 @retval Others The size of a device path in bytes.\r
e386b444 376\r
377**/\r
378UINTN\r
379EFIAPI\r
380GetDevicePathSize (\r
381 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
382 )\r
383{\r
4d0a30a4 384 return mDevicePathLibDevicePathUtilities->GetDevicePathSize (DevicePath);\r
e386b444 385}\r
386\r
387/**\r
6a3f4ef9 388 Creates a new copy of an existing device path.\r
e386b444 389\r
58380e9c 390 This function allocates space for a new copy of the device path specified by \r
391 DevicePath. If DevicePath is NULL, then NULL is returned. \r
392 If the memory is successfully allocated, then the\r
e386b444 393 contents of DevicePath are copied to the newly allocated buffer, and a pointer to that buffer\r
394 is returned. Otherwise, NULL is returned. \r
3e5c3238 395 The memory for the new device path is allocated from EFI boot services memory. \r
396 It is the responsibility of the caller to free the memory allocated. \r
e386b444 397 \r
398 @param DevicePath A pointer to a device path data structure.\r
399\r
771729c7 400 @retval NULL If DevicePath is NULL or invalid.\r
3e5c3238 401 @retval Others A pointer to the duplicated device path.\r
402 \r
e386b444 403**/\r
404EFI_DEVICE_PATH_PROTOCOL *\r
405EFIAPI\r
406DuplicateDevicePath (\r
407 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
408 )\r
409{\r
4d0a30a4 410 return mDevicePathLibDevicePathUtilities->DuplicateDevicePath (DevicePath);\r
e386b444 411}\r
412\r
413/**\r
414 Creates a new device path by appending a second device path to a first device path.\r
415\r
416 This function creates a new device path by appending a copy of SecondDevicePath to a copy of\r
417 FirstDevicePath in a newly allocated buffer. Only the end-of-device-path device node from\r
418 SecondDevicePath is retained. The newly created device path is returned. \r
419 If FirstDevicePath is NULL, then it is ignored, and a duplicate of SecondDevicePath is returned. \r
420 If SecondDevicePath is NULL, then it is ignored, and a duplicate of FirstDevicePath is returned. \r
98a14db6 421 If both FirstDevicePath and SecondDevicePath are NULL, then a copy of an end-of-device-path is\r
422 returned. \r
e386b444 423 If there is not enough memory for the newly allocated buffer, then NULL is returned.\r
424 The memory for the new device path is allocated from EFI boot services memory. It is the\r
425 responsibility of the caller to free the memory allocated.\r
426\r
427 @param FirstDevicePath A pointer to a device path data structure.\r
428 @param SecondDevicePath A pointer to a device path data structure.\r
3e5c3238 429 \r
430 @retval NULL If there is not enough memory for the newly allocated buffer.\r
771729c7 431 @retval NULL If FirstDevicePath or SecondDevicePath is invalid.\r
3e5c3238 432 @retval Others A pointer to the new device path if success.\r
58380e9c 433 Or a copy an end-of-device-path if both FirstDevicePath and \r
434 SecondDevicePath are NULL.\r
e386b444 435\r
436**/\r
437EFI_DEVICE_PATH_PROTOCOL *\r
438EFIAPI\r
439AppendDevicePath (\r
440 IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath, OPTIONAL\r
441 IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath OPTIONAL\r
442 )\r
443{\r
4d0a30a4 444 return mDevicePathLibDevicePathUtilities->AppendDevicePath (FirstDevicePath, SecondDevicePath);\r
e386b444 445}\r
446\r
447/**\r
448 Creates a new path by appending the device node to the device path.\r
449\r
58380e9c 450 This function creates a new device path by appending a copy of the device node \r
451 specified by DevicePathNode to a copy of the device path specified by DevicePath \r
452 in an allocated buffer.\r
e386b444 453 The end-of-device-path device node is moved after the end of the appended device node.\r
98a14db6 454 If DevicePathNode is NULL then a copy of DevicePath is returned.\r
58380e9c 455 If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device \r
456 path device node is returned.\r
457 If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path \r
458 device node is returned.\r
459 If there is not enough memory to allocate space for the new device path, then \r
460 NULL is returned. \r
461 The memory is allocated from EFI boot services memory. It is the responsibility \r
462 of the caller to free the memory allocated.\r
e386b444 463\r
464 @param DevicePath A pointer to a device path data structure.\r
465 @param DevicePathNode A pointer to a single device path node.\r
466\r
3e5c3238 467 @retval NULL If there is not enough memory for the new device path.\r
468 @retval Others A pointer to the new device path if success.\r
469 A copy of DevicePathNode followed by an end-of-device-path node \r
470 if both FirstDevicePath and SecondDevicePath are NULL.\r
58380e9c 471 A copy of an end-of-device-path node if both FirstDevicePath \r
472 and SecondDevicePath are NULL.\r
e386b444 473\r
474**/\r
475EFI_DEVICE_PATH_PROTOCOL *\r
476EFIAPI\r
477AppendDevicePathNode (\r
478 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL\r
479 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathNode OPTIONAL\r
480 )\r
481{\r
4d0a30a4 482 return mDevicePathLibDevicePathUtilities->AppendDeviceNode (DevicePath, DevicePathNode);\r
e386b444 483}\r
484\r
485/**\r
58380e9c 486 Creates a new device path by appending the specified device path instance to \r
487 the specified device path.\r
e386b444 488 \r
58380e9c 489 This function creates a new device path by appending a copy of the device path \r
490 instance specified by DevicePathInstance to a copy of the device path specified \r
491 by DevicePath in a allocated buffer.\r
492 The end-of-device-path device node is moved after the end of the appended device \r
493 path instance and a new end-of-device-path-instance node is inserted between. \r
e386b444 494 If DevicePath is NULL, then a copy if DevicePathInstance is returned.\r
495 If DevicePathInstance is NULL, then NULL is returned.\r
771729c7 496 If DevicePath or DevicePathInstance is invalid, then NULL is returned.\r
58380e9c 497 If there is not enough memory to allocate space for the new device path, then \r
498 NULL is returned. \r
499 The memory is allocated from EFI boot services memory. It is the responsibility \r
500 of the caller to free the memory allocated.\r
e386b444 501 \r
502 @param DevicePath A pointer to a device path data structure.\r
503 @param DevicePathInstance A pointer to a device path instance.\r
504\r
505 @return A pointer to the new device path.\r
506\r
507**/\r
508EFI_DEVICE_PATH_PROTOCOL *\r
509EFIAPI\r
510AppendDevicePathInstance (\r
511 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL\r
512 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathInstance OPTIONAL\r
513 )\r
514{\r
4d0a30a4 515 return mDevicePathLibDevicePathUtilities->AppendDevicePathInstance (DevicePath, DevicePathInstance);\r
e386b444 516}\r
517\r
518/**\r
58380e9c 519 Creates a copy of the current device path instance and returns a pointer to the \r
520 next device path instance.\r
e386b444 521\r
58380e9c 522 This function creates a copy of the current device path instance. It also updates \r
523 DevicePath to point to the next device path instance in the device path (or NULL \r
524 if no more) and updates Size to hold the size of the device path instance copy.\r
e386b444 525 If DevicePath is NULL, then NULL is returned.\r
58380e9c 526 If there is not enough memory to allocate space for the new device path, then \r
527 NULL is returned. \r
528 The memory is allocated from EFI boot services memory. It is the responsibility \r
529 of the caller to free the memory allocated.\r
e386b444 530 If Size is NULL, then ASSERT().\r
531 \r
58380e9c 532 @param DevicePath On input, this holds the pointer to the current \r
533 device path instance. On output, this holds \r
534 the pointer to the next device path instance \r
535 or NULL if there are no more device path\r
536 instances in the device path pointer to a \r
537 device path data structure.\r
538 @param Size On output, this holds the size of the device \r
539 path instance, in bytes or zero, if DevicePath \r
540 is NULL.\r
e386b444 541\r
542 @return A pointer to the current device path instance.\r
543\r
544**/\r
545EFI_DEVICE_PATH_PROTOCOL *\r
546EFIAPI\r
547GetNextDevicePathInstance (\r
548 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath,\r
549 OUT UINTN *Size\r
550 )\r
551{\r
552 ASSERT (Size != NULL);\r
4d0a30a4 553 return mDevicePathLibDevicePathUtilities->GetNextDevicePathInstance (DevicePath, Size);\r
e386b444 554}\r
555\r
556/**\r
3e5c3238 557 Creates a device node.\r
e386b444 558\r
58380e9c 559 This function creates a new device node in a newly allocated buffer of size \r
560 NodeLength and initializes the device path node header with NodeType and NodeSubType. \r
561 The new device path node is returned.\r
e386b444 562 If NodeLength is smaller than a device path header, then NULL is returned. \r
58380e9c 563 If there is not enough memory to allocate space for the new device path, then \r
564 NULL is returned. \r
565 The memory is allocated from EFI boot services memory. It is the responsibility \r
0b13fe74 566 of the caller to free the memory allocated.\r
e386b444 567\r
568 @param NodeType The device node type for the new device node.\r
569 @param NodeSubType The device node sub-type for the new device node.\r
570 @param NodeLength The length of the new device node.\r
571\r
3e5c3238 572 @return The new device path.\r
e386b444 573\r
574**/\r
575EFI_DEVICE_PATH_PROTOCOL *\r
576EFIAPI\r
577CreateDeviceNode (\r
578 IN UINT8 NodeType,\r
579 IN UINT8 NodeSubType,\r
580 IN UINT16 NodeLength\r
581 )\r
582{\r
4d0a30a4 583 return mDevicePathLibDevicePathUtilities->CreateDeviceNode (NodeType, NodeSubType, NodeLength);\r
e386b444 584}\r
585\r
586/**\r
587 Determines if a device path is single or multi-instance.\r
588\r
771729c7 589 This function returns TRUE if the device path specified by DevicePath is\r
58380e9c 590 multi-instance.\r
771729c7
RN
591 Otherwise, FALSE is returned.\r
592 If DevicePath is NULL or invalid, then FALSE is returned.\r
e386b444 593\r
594 @param DevicePath A pointer to a device path data structure.\r
595\r
596 @retval TRUE DevicePath is multi-instance.\r
771729c7
RN
597 @retval FALSE DevicePath is not multi-instance, or DevicePath \r
598 is NULL or invalid.\r
e386b444 599\r
600**/\r
601BOOLEAN\r
602EFIAPI\r
603IsDevicePathMultiInstance (\r
604 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
605 )\r
606{\r
4d0a30a4 607 return mDevicePathLibDevicePathUtilities->IsDevicePathMultiInstance (DevicePath);\r
e386b444 608}\r
609\r
610/**\r
611 Retrieves the device path protocol from a handle.\r
612\r
58380e9c 613 This function returns the device path protocol from the handle specified by Handle. \r
614 If Handle is NULL or Handle does not contain a device path protocol, then NULL \r
615 is returned.\r
e386b444 616 \r
58380e9c 617 @param Handle The handle from which to retrieve the device \r
618 path protocol.\r
e386b444 619\r
620 @return The device path protocol from the handle specified by Handle.\r
621\r
622**/\r
623EFI_DEVICE_PATH_PROTOCOL *\r
624EFIAPI\r
625DevicePathFromHandle (\r
626 IN EFI_HANDLE Handle\r
627 )\r
628{\r
629 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
630 EFI_STATUS Status;\r
631\r
632 Status = gBS->HandleProtocol (\r
633 Handle,\r
634 &gEfiDevicePathProtocolGuid,\r
635 (VOID *) &DevicePath\r
636 );\r
637 if (EFI_ERROR (Status)) {\r
638 DevicePath = NULL;\r
639 }\r
640 return DevicePath;\r
641}\r
642\r
643/**\r
644 Allocates a device path for a file and appends it to an existing device path.\r
645\r
58380e9c 646 If Device is a valid device handle that contains a device path protocol, then \r
647 a device path for the file specified by FileName is allocated and appended to \r
648 the device path associated with the handle Device. The allocated device path \r
649 is returned. If Device is NULL or Device is a handle that does not support the \r
650 device path protocol, then a device path containing a single device path node \r
651 for the file specified by FileName is allocated and returned.\r
652 The memory for the new device path is allocated from EFI boot services memory. \r
653 It is the responsibility of the caller to free the memory allocated.\r
3e5c3238 654 \r
e386b444 655 If FileName is NULL, then ASSERT().\r
3e5c3238 656 If FileName is not aligned on a 16-bit boundary, then ASSERT().\r
e386b444 657\r
58380e9c 658 @param Device A pointer to a device handle. This parameter \r
659 is optional and may be NULL.\r
e386b444 660 @param FileName A pointer to a Null-terminated Unicode string.\r
661\r
3e5c3238 662 @return The allocated device path.\r
e386b444 663\r
664**/\r
665EFI_DEVICE_PATH_PROTOCOL *\r
666EFIAPI\r
667FileDevicePath (\r
668 IN EFI_HANDLE Device, OPTIONAL\r
669 IN CONST CHAR16 *FileName\r
670 )\r
671{\r
672 UINTN Size;\r
673 FILEPATH_DEVICE_PATH *FilePath;\r
674 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
675 EFI_DEVICE_PATH_PROTOCOL *FileDevicePath;\r
676\r
677 DevicePath = NULL;\r
678\r
679 Size = StrSize (FileName);\r
e5dab016 680 FileDevicePath = AllocatePool (Size + SIZE_OF_FILEPATH_DEVICE_PATH + END_DEVICE_PATH_LENGTH);\r
e386b444 681 if (FileDevicePath != NULL) {\r
682 FilePath = (FILEPATH_DEVICE_PATH *) FileDevicePath;\r
683 FilePath->Header.Type = MEDIA_DEVICE_PATH;\r
684 FilePath->Header.SubType = MEDIA_FILEPATH_DP;\r
685 CopyMem (&FilePath->PathName, FileName, Size);\r
686 SetDevicePathNodeLength (&FilePath->Header, Size + SIZE_OF_FILEPATH_DEVICE_PATH);\r
687 SetDevicePathEndNode (NextDevicePathNode (&FilePath->Header));\r
688\r
689 if (Device != NULL) {\r
690 DevicePath = DevicePathFromHandle (Device);\r
691 }\r
692\r
693 DevicePath = AppendDevicePath (DevicePath, FileDevicePath);\r
694 FreePool (FileDevicePath);\r
695 }\r
696\r
697 return DevicePath;\r
698}\r
4d0a30a4
RN
699\r
700/**\r
701 Locate and return the protocol instance identified by the ProtocolGuid.\r
702\r
703 @param ProtocolGuid The GUID of the protocol.\r
704\r
705 @return A pointer to the protocol instance or NULL when absent.\r
706**/\r
707VOID *\r
708UefiDevicePathLibLocateProtocol (\r
709 EFI_GUID *ProtocolGuid\r
710 )\r
711{\r
712 EFI_STATUS Status;\r
713 VOID *Protocol;\r
714 Status = gBS->LocateProtocol (\r
715 ProtocolGuid,\r
716 NULL,\r
717 (VOID**) &Protocol\r
718 );\r
719 if (EFI_ERROR (Status)) {\r
720 return NULL;\r
721 } else {\r
722 return Protocol;\r
723 }\r
724}\r
725\r
726/**\r
727 Converts a device node to its string representation.\r
728\r
729 @param DeviceNode A Pointer to the device node to be converted.\r
730 @param DisplayOnly If DisplayOnly is TRUE, then the shorter text representation\r
731 of the display node is used, where applicable. If DisplayOnly\r
732 is FALSE, then the longer text representation of the display node\r
733 is used.\r
734 @param AllowShortcuts If AllowShortcuts is TRUE, then the shortcut forms of text\r
735 representation for a device node can be used, where applicable.\r
736\r
737 @return A pointer to the allocated text representation of the device node or NULL if DeviceNode\r
738 is NULL or there was insufficient memory.\r
739\r
740**/\r
741CHAR16 *\r
742EFIAPI\r
743ConvertDeviceNodeToText (\r
744 IN CONST EFI_DEVICE_PATH_PROTOCOL *DeviceNode,\r
745 IN BOOLEAN DisplayOnly,\r
746 IN BOOLEAN AllowShortcuts\r
747 )\r
748{\r
749 if (mDevicePathLibDevicePathToText == NULL) {\r
750 mDevicePathLibDevicePathToText = UefiDevicePathLibLocateProtocol (&gEfiDevicePathToTextProtocolGuid);\r
751 }\r
752 if (mDevicePathLibDevicePathToText != NULL) {\r
753 return mDevicePathLibDevicePathToText->ConvertDeviceNodeToText (DeviceNode, DisplayOnly, AllowShortcuts);\r
754 } else {\r
755 return NULL;\r
756 }\r
757}\r
758\r
759/**\r
760 Converts a device path to its text representation.\r
761\r
762 @param DevicePath A Pointer to the device to be converted.\r
763 @param DisplayOnly If DisplayOnly is TRUE, then the shorter text representation\r
764 of the display node is used, where applicable. If DisplayOnly\r
765 is FALSE, then the longer text representation of the display node\r
766 is used.\r
767 @param AllowShortcuts If AllowShortcuts is TRUE, then the shortcut forms of text\r
768 representation for a device node can be used, where applicable.\r
769\r
770 @return A pointer to the allocated text representation of the device path or\r
771 NULL if DeviceNode is NULL or there was insufficient memory.\r
772\r
773**/\r
774CHAR16 *\r
775EFIAPI\r
776ConvertDevicePathToText (\r
777 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
778 IN BOOLEAN DisplayOnly,\r
779 IN BOOLEAN AllowShortcuts\r
780 )\r
781{\r
782 if (mDevicePathLibDevicePathToText == NULL) {\r
783 mDevicePathLibDevicePathToText = UefiDevicePathLibLocateProtocol (&gEfiDevicePathToTextProtocolGuid);\r
784 }\r
785 if (mDevicePathLibDevicePathToText != NULL) {\r
786 return mDevicePathLibDevicePathToText->ConvertDevicePathToText (DevicePath, DisplayOnly, AllowShortcuts);\r
787 } else {\r
788 return NULL;\r
789 }\r
790}\r
791\r
792/**\r
793 Convert text to the binary representation of a device node.\r
794\r
795 @param TextDeviceNode TextDeviceNode points to the text representation of a device\r
796 node. Conversion starts with the first character and continues\r
797 until the first non-device node character.\r
798\r
799 @return A pointer to the EFI device node or NULL if TextDeviceNode is NULL or there was\r
800 insufficient memory or text unsupported.\r
801\r
802**/\r
803EFI_DEVICE_PATH_PROTOCOL *\r
804EFIAPI\r
805ConvertTextToDeviceNode (\r
806 IN CONST CHAR16 *TextDeviceNode\r
807 )\r
808{\r
809 if (mDevicePathLibDevicePathFromText == NULL) {\r
810 mDevicePathLibDevicePathFromText = UefiDevicePathLibLocateProtocol (&gEfiDevicePathFromTextProtocolGuid);\r
811 }\r
812 if (mDevicePathLibDevicePathFromText != NULL) {\r
813 return mDevicePathLibDevicePathFromText->ConvertTextToDeviceNode (TextDeviceNode);\r
814 } else {\r
815 return NULL;\r
816 }\r
817}\r
818\r
819/**\r
820 Convert text to the binary representation of a device path.\r
821\r
822\r
823 @param TextDevicePath TextDevicePath points to the text representation of a device\r
824 path. Conversion starts with the first character and continues\r
825 until the first non-device node character.\r
826\r
827 @return A pointer to the allocated device path or NULL if TextDeviceNode is NULL or\r
828 there was insufficient memory.\r
829\r
830**/\r
831EFI_DEVICE_PATH_PROTOCOL *\r
832EFIAPI\r
833ConvertTextToDevicePath (\r
834 IN CONST CHAR16 *TextDevicePath\r
835 )\r
836{\r
837 if (mDevicePathLibDevicePathFromText == NULL) {\r
838 mDevicePathLibDevicePathFromText = UefiDevicePathLibLocateProtocol (&gEfiDevicePathFromTextProtocolGuid);\r
839 }\r
840 if (mDevicePathLibDevicePathFromText != NULL) {\r
841 return mDevicePathLibDevicePathFromText->ConvertTextToDevicePath (TextDevicePath);\r
842 } else {\r
843 return NULL;\r
844 }\r
845}\r
846\r