]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/UefiDevicePathLibDevicePathProtocol/UefiDevicePathLib.c
Roll back the changes in revision 14296 since it will cause iSCSI security authentica...
[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
771729c7 5 Copyright (c) 2006 - 2012, 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
c892d846 20\r
c7d265a9 21#include <Library/DevicePathLib.h>\r
22#include <Library/DebugLib.h>\r
23#include <Library/BaseLib.h>\r
24#include <Library/MemoryAllocationLib.h>\r
25#include <Library/BaseMemoryLib.h>\r
26#include <Library/UefiBootServicesTableLib.h>\r
771729c7 27#include <Library/PcdLib.h>\r
e386b444 28\r
fe467413 29EFI_DEVICE_PATH_UTILITIES_PROTOCOL *mDevicePathUtilities = NULL;\r
e386b444 30\r
3dc728fb 31//\r
32// Template for an end-of-device path node.\r
33//\r
34GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_DEVICE_PATH_PROTOCOL mUefiDevicePathLibEndDevicePath = {\r
35 END_DEVICE_PATH_TYPE,\r
36 END_ENTIRE_DEVICE_PATH_SUBTYPE,\r
37 {\r
38 END_DEVICE_PATH_LENGTH,\r
39 0\r
40 }\r
41};\r
42\r
e386b444 43/**\r
44 The constructor function caches the pointer to DevicePathUtilites protocol.\r
45 \r
46 The constructor function locates DevicePathUtilities protocol from protocol database.\r
47 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS. \r
48\r
49 @param ImageHandle The firmware allocated handle for the EFI image.\r
50 @param SystemTable A pointer to the EFI System Table.\r
51 \r
52 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r
53\r
54**/\r
55EFI_STATUS\r
56EFIAPI\r
57DevicePathLibConstructor (\r
58 IN EFI_HANDLE ImageHandle,\r
59 IN EFI_SYSTEM_TABLE *SystemTable\r
60 )\r
61{\r
62 EFI_STATUS Status;\r
63\r
64 Status = gBS->LocateProtocol (\r
65 &gEfiDevicePathUtilitiesProtocolGuid,\r
66 NULL,\r
67 (VOID**) &mDevicePathUtilities\r
68 );\r
69 ASSERT_EFI_ERROR (Status);\r
70 ASSERT (mDevicePathUtilities != NULL);\r
71\r
72 return Status;\r
73}\r
74\r
771729c7
RN
75/**\r
76 Determine whether a given device path is valid.\r
77 If DevicePath is NULL, then ASSERT().\r
78\r
79 @param DevicePath A pointer to a device path data structure.\r
80 @param MaxSize The maximum size of the device path data structure.\r
81\r
82 @retval TRUE DevicePath is valid.\r
83 @retval FALSE The length of any node node in the DevicePath is less\r
84 than sizeof (EFI_DEVICE_PATH_PROTOCOL).\r
85 @retval FALSE If MaxSize is not zero, the size of the DevicePath\r
86 exceeds MaxSize.\r
87 @retval FALSE If PcdMaximumDevicePathNodeCount is not zero, the node\r
88 count of the DevicePath exceeds PcdMaximumDevicePathNodeCount.\r
89**/\r
90BOOLEAN\r
91EFIAPI\r
92IsDevicePathValid (\r
93 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
94 IN UINTN MaxSize\r
95 )\r
96{\r
97 UINTN Count;\r
98 UINTN Size;\r
99 UINTN NodeLength;\r
100\r
101 ASSERT (DevicePath != NULL);\r
102\r
103 for (Count = 0, Size = 0; !IsDevicePathEnd (DevicePath); DevicePath = NextDevicePathNode (DevicePath)) {\r
104 NodeLength = DevicePathNodeLength (DevicePath);\r
105 if (NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {\r
106 return FALSE;\r
107 }\r
108\r
109 if (MaxSize > 0) {\r
110 Size += NodeLength;\r
111 if (Size + END_DEVICE_PATH_LENGTH > MaxSize) {\r
112 return FALSE;\r
113 }\r
114 }\r
115\r
116 if (PcdGet32 (PcdMaximumDevicePathNodeCount) > 0) {\r
117 Count++;\r
118 if (Count >= PcdGet32 (PcdMaximumDevicePathNodeCount)) {\r
119 return FALSE;\r
120 }\r
121 }\r
122 }\r
123\r
124 //\r
125 // Only return TRUE when the End Device Path node is valid.\r
126 //\r
127 return (BOOLEAN) (DevicePathNodeLength (DevicePath) == END_DEVICE_PATH_LENGTH);\r
128}\r
129\r
3dc728fb 130/**\r
131 Returns the Type field of a device path node.\r
132\r
133 Returns the Type field of the device path node specified by Node.\r
134\r
135 If Node is NULL, then ASSERT().\r
136\r
137 @param Node A pointer to a device path node data structure.\r
138\r
139 @return The Type field of the device path node specified by Node.\r
140\r
141**/\r
142UINT8\r
5cba121d 143EFIAPI\r
3dc728fb 144DevicePathType (\r
145 IN CONST VOID *Node\r
146 )\r
147{\r
148 ASSERT (Node != NULL);\r
149 return ((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Type;\r
150}\r
151\r
152/**\r
153 Returns the SubType field of a device path node.\r
154\r
155 Returns the SubType field of the device path node specified by Node.\r
156\r
157 If Node is NULL, then ASSERT().\r
158\r
159 @param Node A pointer to a device path node data structure.\r
160\r
161 @return The SubType field of the device path node specified by Node.\r
162\r
163**/\r
164UINT8\r
5cba121d 165EFIAPI\r
3dc728fb 166DevicePathSubType (\r
167 IN CONST VOID *Node\r
168 )\r
169{\r
170 ASSERT (Node != NULL);\r
171 return ((EFI_DEVICE_PATH_PROTOCOL *)(Node))->SubType;\r
172}\r
173\r
174/**\r
175 Returns the 16-bit Length field of a device path node.\r
176\r
177 Returns the 16-bit Length field of the device path node specified by Node. \r
178 Node is not required to be aligned on a 16-bit boundary, so it is recommended\r
179 that a function such as ReadUnaligned16() be used to extract the contents of \r
180 the Length field.\r
181\r
182 If Node is NULL, then ASSERT().\r
183\r
184 @param Node A pointer to a device path node data structure.\r
185\r
186 @return The 16-bit Length field of the device path node specified by Node.\r
187\r
188**/\r
189UINTN\r
5cba121d 190EFIAPI\r
3dc728fb 191DevicePathNodeLength (\r
192 IN CONST VOID *Node\r
193 )\r
194{\r
771729c7
RN
195 UINTN Length;\r
196\r
3dc728fb 197 ASSERT (Node != NULL);\r
771729c7
RN
198 Length = ReadUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0]);\r
199 ASSERT (Length >= sizeof (EFI_DEVICE_PATH_PROTOCOL));\r
200 return Length;\r
3dc728fb 201}\r
202\r
203/**\r
204 Returns a pointer to the next node in a device path.\r
205\r
58380e9c 206 Returns a pointer to the device path node that follows the device path node \r
207 specified by Node.\r
3dc728fb 208\r
209 If Node is NULL, then ASSERT().\r
210\r
211 @param Node A pointer to a device path node data structure.\r
212\r
58380e9c 213 @return a pointer to the device path node that follows the device path node \r
214 specified by Node.\r
3dc728fb 215\r
216**/\r
217EFI_DEVICE_PATH_PROTOCOL *\r
5cba121d 218EFIAPI\r
3dc728fb 219NextDevicePathNode (\r
220 IN CONST VOID *Node\r
221 )\r
222{\r
223 ASSERT (Node != NULL);\r
224 return (EFI_DEVICE_PATH_PROTOCOL *)((UINT8 *)(Node) + DevicePathNodeLength(Node));\r
225}\r
226\r
227/**\r
228 Determines if a device path node is an end node of a device path.\r
58380e9c 229 This includes nodes that are the end of a device path instance and nodes that \r
230 are the end of an entire device path.\r
3dc728fb 231\r
232 Determines if the device path node specified by Node is an end node of a device path. \r
233 This includes nodes that are the end of a device path instance and nodes that are the \r
234 end of an entire device path. If Node represents an end node of a device path, \r
235 then TRUE is returned. Otherwise, FALSE is returned.\r
236\r
237 If Node is NULL, then ASSERT().\r
238\r
239 @param Node A pointer to a device path node data structure.\r
240\r
241 @retval TRUE The device path node specified by Node is an end node of a device path.\r
58380e9c 242 @retval FALSE The device path node specified by Node is not an end node of \r
243 a device path.\r
3dc728fb 244 \r
245**/\r
246BOOLEAN\r
5cba121d 247EFIAPI\r
3dc728fb 248IsDevicePathEndType (\r
249 IN CONST VOID *Node\r
250 )\r
251{\r
252 ASSERT (Node != NULL);\r
a357faa3 253 return (BOOLEAN) (DevicePathType (Node) == END_DEVICE_PATH_TYPE);\r
3dc728fb 254}\r
255\r
256/**\r
257 Determines if a device path node is an end node of an entire device path.\r
258\r
58380e9c 259 Determines if a device path node specified by Node is an end node of an entire \r
260 device path.\r
261 If Node represents the end of an entire device path, then TRUE is returned. \r
262 Otherwise, FALSE is returned.\r
3dc728fb 263\r
264 If Node is NULL, then ASSERT().\r
265\r
266 @param Node A pointer to a device path node data structure.\r
267\r
268 @retval TRUE The device path node specified by Node is the end of an entire device path.\r
269 @retval FALSE The device path node specified by Node is not the end of an entire device path.\r
270\r
271**/\r
272BOOLEAN\r
5cba121d 273EFIAPI\r
3dc728fb 274IsDevicePathEnd (\r
275 IN CONST VOID *Node\r
276 )\r
277{\r
278 ASSERT (Node != NULL);\r
1bfc7438 279 return (BOOLEAN) (IsDevicePathEndType (Node) && DevicePathSubType(Node) == END_ENTIRE_DEVICE_PATH_SUBTYPE);\r
3dc728fb 280}\r
281\r
282/**\r
283 Determines if a device path node is an end node of a device path instance.\r
284\r
58380e9c 285 Determines if a device path node specified by Node is an end node of a device \r
286 path instance.\r
287 If Node represents the end of a device path instance, then TRUE is returned. \r
288 Otherwise, FALSE is returned.\r
3dc728fb 289\r
290 If Node is NULL, then ASSERT().\r
291\r
292 @param Node A pointer to a device path node data structure.\r
293\r
58380e9c 294 @retval TRUE The device path node specified by Node is the end of a device \r
295 path instance.\r
296 @retval FALSE The device path node specified by Node is not the end of a \r
297 device path instance.\r
3dc728fb 298\r
299**/\r
300BOOLEAN\r
5cba121d 301EFIAPI\r
3dc728fb 302IsDevicePathEndInstance (\r
303 IN CONST VOID *Node\r
304 )\r
305{\r
306 ASSERT (Node != NULL);\r
1bfc7438 307 return (BOOLEAN) (IsDevicePathEndType (Node) && DevicePathSubType(Node) == END_INSTANCE_DEVICE_PATH_SUBTYPE);\r
3dc728fb 308}\r
309\r
310/**\r
311 Sets the length, in bytes, of a device path node.\r
312\r
313 Sets the length of the device path node specified by Node to the value specified \r
314 by NodeLength. NodeLength is returned. Node is not required to be aligned on \r
315 a 16-bit boundary, so it is recommended that a function such as WriteUnaligned16()\r
316 be used to set the contents of the Length field.\r
317\r
318 If Node is NULL, then ASSERT().\r
771729c7
RN
319 If NodeLength >= SIZE_64KB, then ASSERT().\r
320 If NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL), then ASSERT().\r
3dc728fb 321\r
322 @param Node A pointer to a device path node data structure.\r
323 @param Length The length, in bytes, of the device path node.\r
324\r
325 @return Length\r
326\r
327**/\r
328UINT16\r
5cba121d 329EFIAPI\r
3dc728fb 330SetDevicePathNodeLength (\r
9bb407c6 331 IN OUT VOID *Node,\r
8f0dd97e 332 IN UINTN Length\r
3dc728fb 333 )\r
334{\r
335 ASSERT (Node != NULL);\r
771729c7 336 ASSERT ((Length >= sizeof (EFI_DEVICE_PATH_PROTOCOL)) && (Length < SIZE_64KB));\r
8f0dd97e 337 return WriteUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0], (UINT16)(Length));\r
3dc728fb 338}\r
339\r
340/**\r
341 Fills in all the fields of a device path node that is the end of an entire device path.\r
342\r
343 Fills in all the fields of a device path node specified by Node so Node represents \r
344 the end of an entire device path. The Type field of Node is set to \r
345 END_DEVICE_PATH_TYPE, the SubType field of Node is set to \r
346 END_ENTIRE_DEVICE_PATH_SUBTYPE, and the Length field of Node is set to \r
347 END_DEVICE_PATH_LENGTH. Node is not required to be aligned on a 16-bit boundary, \r
348 so it is recommended that a function such as WriteUnaligned16() be used to set \r
349 the contents of the Length field. \r
350\r
351 If Node is NULL, then ASSERT(). \r
352\r
353 @param Node A pointer to a device path node data structure.\r
354\r
355**/\r
356VOID\r
5cba121d 357EFIAPI\r
3dc728fb 358SetDevicePathEndNode (\r
9bb407c6 359 OUT VOID *Node\r
3dc728fb 360 )\r
361{\r
362 ASSERT (Node != NULL);\r
363 CopyMem (Node, &mUefiDevicePathLibEndDevicePath, sizeof (mUefiDevicePathLibEndDevicePath));\r
364}\r
365\r
e386b444 366/**\r
367 Returns the size of a device path in bytes.\r
368\r
771729c7
RN
369 This function returns the size, in bytes, of the device path data structure \r
370 specified by DevicePath including the end of device path node.\r
371 If DevicePath is NULL or invalid, then 0 is returned.\r
e386b444 372\r
771729c7
RN
373 @param DevicePath A pointer to a device path data structure.\r
374\r
375 @retval 0 If DevicePath is NULL or invalid.\r
376 @retval Others The size of a device path in bytes.\r
e386b444 377\r
378**/\r
379UINTN\r
380EFIAPI\r
381GetDevicePathSize (\r
382 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
383 )\r
384{\r
385 return mDevicePathUtilities->GetDevicePathSize (DevicePath);\r
386}\r
387\r
388/**\r
6a3f4ef9 389 Creates a new copy of an existing device path.\r
e386b444 390\r
58380e9c 391 This function allocates space for a new copy of the device path specified by \r
392 DevicePath. If DevicePath is NULL, then NULL is returned. \r
393 If the memory is successfully allocated, then the\r
e386b444 394 contents of DevicePath are copied to the newly allocated buffer, and a pointer to that buffer\r
395 is returned. Otherwise, NULL is returned. \r
3e5c3238 396 The memory for the new device path is allocated from EFI boot services memory. \r
397 It is the responsibility of the caller to free the memory allocated. \r
e386b444 398 \r
399 @param DevicePath A pointer to a device path data structure.\r
400\r
771729c7 401 @retval NULL If DevicePath is NULL or invalid.\r
3e5c3238 402 @retval Others A pointer to the duplicated device path.\r
403 \r
e386b444 404**/\r
405EFI_DEVICE_PATH_PROTOCOL *\r
406EFIAPI\r
407DuplicateDevicePath (\r
408 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
409 )\r
410{\r
411 return mDevicePathUtilities->DuplicateDevicePath (DevicePath);\r
412}\r
413\r
414/**\r
415 Creates a new device path by appending a second device path to a first device path.\r
416\r
417 This function creates a new device path by appending a copy of SecondDevicePath to a copy of\r
418 FirstDevicePath in a newly allocated buffer. Only the end-of-device-path device node from\r
419 SecondDevicePath is retained. The newly created device path is returned. \r
420 If FirstDevicePath is NULL, then it is ignored, and a duplicate of SecondDevicePath is returned. \r
421 If SecondDevicePath is NULL, then it is ignored, and a duplicate of FirstDevicePath is returned. \r
98a14db6 422 If both FirstDevicePath and SecondDevicePath are NULL, then a copy of an end-of-device-path is\r
423 returned. \r
e386b444 424 If there is not enough memory for the newly allocated buffer, then NULL is returned.\r
425 The memory for the new device path is allocated from EFI boot services memory. It is the\r
426 responsibility of the caller to free the memory allocated.\r
427\r
428 @param FirstDevicePath A pointer to a device path data structure.\r
429 @param SecondDevicePath A pointer to a device path data structure.\r
3e5c3238 430 \r
431 @retval NULL If there is not enough memory for the newly allocated buffer.\r
771729c7 432 @retval NULL If FirstDevicePath or SecondDevicePath is invalid.\r
3e5c3238 433 @retval Others A pointer to the new device path if success.\r
58380e9c 434 Or a copy an end-of-device-path if both FirstDevicePath and \r
435 SecondDevicePath are NULL.\r
e386b444 436\r
437**/\r
438EFI_DEVICE_PATH_PROTOCOL *\r
439EFIAPI\r
440AppendDevicePath (\r
441 IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath, OPTIONAL\r
442 IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath OPTIONAL\r
443 )\r
444{\r
445 return mDevicePathUtilities->AppendDevicePath (FirstDevicePath, SecondDevicePath);\r
446}\r
447\r
448/**\r
449 Creates a new path by appending the device node to the device path.\r
450\r
58380e9c 451 This function creates a new device path by appending a copy of the device node \r
452 specified by DevicePathNode to a copy of the device path specified by DevicePath \r
453 in an allocated buffer.\r
e386b444 454 The end-of-device-path device node is moved after the end of the appended device node.\r
98a14db6 455 If DevicePathNode is NULL then a copy of DevicePath is returned.\r
58380e9c 456 If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device \r
457 path device node is returned.\r
458 If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path \r
459 device node is returned.\r
460 If there is not enough memory to allocate space for the new device path, then \r
461 NULL is returned. \r
462 The memory is allocated from EFI boot services memory. It is the responsibility \r
463 of the caller to free the memory allocated.\r
e386b444 464\r
465 @param DevicePath A pointer to a device path data structure.\r
466 @param DevicePathNode A pointer to a single device path node.\r
467\r
3e5c3238 468 @retval NULL If there is not enough memory for the new device path.\r
469 @retval Others A pointer to the new device path if success.\r
470 A copy of DevicePathNode followed by an end-of-device-path node \r
471 if both FirstDevicePath and SecondDevicePath are NULL.\r
58380e9c 472 A copy of an end-of-device-path node if both FirstDevicePath \r
473 and SecondDevicePath are NULL.\r
e386b444 474\r
475**/\r
476EFI_DEVICE_PATH_PROTOCOL *\r
477EFIAPI\r
478AppendDevicePathNode (\r
479 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL\r
480 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathNode OPTIONAL\r
481 )\r
482{\r
483 return mDevicePathUtilities->AppendDeviceNode (DevicePath, DevicePathNode);\r
484}\r
485\r
486/**\r
58380e9c 487 Creates a new device path by appending the specified device path instance to \r
488 the specified device path.\r
e386b444 489 \r
58380e9c 490 This function creates a new device path by appending a copy of the device path \r
491 instance specified by DevicePathInstance to a copy of the device path specified \r
492 by DevicePath in a allocated buffer.\r
493 The end-of-device-path device node is moved after the end of the appended device \r
494 path instance and a new end-of-device-path-instance node is inserted between. \r
e386b444 495 If DevicePath is NULL, then a copy if DevicePathInstance is returned.\r
496 If DevicePathInstance is NULL, then NULL is returned.\r
771729c7 497 If DevicePath or DevicePathInstance is invalid, then NULL is returned.\r
58380e9c 498 If there is not enough memory to allocate space for the new device path, then \r
499 NULL is returned. \r
500 The memory is allocated from EFI boot services memory. It is the responsibility \r
501 of the caller to free the memory allocated.\r
e386b444 502 \r
503 @param DevicePath A pointer to a device path data structure.\r
504 @param DevicePathInstance A pointer to a device path instance.\r
505\r
506 @return A pointer to the new device path.\r
507\r
508**/\r
509EFI_DEVICE_PATH_PROTOCOL *\r
510EFIAPI\r
511AppendDevicePathInstance (\r
512 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL\r
513 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathInstance OPTIONAL\r
514 )\r
515{\r
516 return mDevicePathUtilities->AppendDevicePathInstance (DevicePath, DevicePathInstance);\r
517}\r
518\r
519/**\r
58380e9c 520 Creates a copy of the current device path instance and returns a pointer to the \r
521 next device path instance.\r
e386b444 522\r
58380e9c 523 This function creates a copy of the current device path instance. It also updates \r
524 DevicePath to point to the next device path instance in the device path (or NULL \r
525 if no more) and updates Size to hold the size of the device path instance copy.\r
e386b444 526 If DevicePath is NULL, then NULL is returned.\r
58380e9c 527 If there is not enough memory to allocate space for the new device path, then \r
528 NULL is returned. \r
529 The memory is allocated from EFI boot services memory. It is the responsibility \r
530 of the caller to free the memory allocated.\r
e386b444 531 If Size is NULL, then ASSERT().\r
532 \r
58380e9c 533 @param DevicePath On input, this holds the pointer to the current \r
534 device path instance. On output, this holds \r
535 the pointer to the next device path instance \r
536 or NULL if there are no more device path\r
537 instances in the device path pointer to a \r
538 device path data structure.\r
539 @param Size On output, this holds the size of the device \r
540 path instance, in bytes or zero, if DevicePath \r
541 is NULL.\r
e386b444 542\r
543 @return A pointer to the current device path instance.\r
544\r
545**/\r
546EFI_DEVICE_PATH_PROTOCOL *\r
547EFIAPI\r
548GetNextDevicePathInstance (\r
549 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath,\r
550 OUT UINTN *Size\r
551 )\r
552{\r
553 ASSERT (Size != NULL);\r
554 return mDevicePathUtilities->GetNextDevicePathInstance (DevicePath, Size);\r
555}\r
556\r
557/**\r
3e5c3238 558 Creates a device node.\r
e386b444 559\r
58380e9c 560 This function creates a new device node in a newly allocated buffer of size \r
561 NodeLength and initializes the device path node header with NodeType and NodeSubType. \r
562 The new device path node is returned.\r
e386b444 563 If NodeLength is smaller than a device path header, then NULL is returned. \r
58380e9c 564 If there is not enough memory to allocate space for the new device path, then \r
565 NULL is returned. \r
566 The memory is allocated from EFI boot services memory. It is the responsibility \r
567 of the caller to\r
e386b444 568 free the memory allocated.\r
569\r
570 @param NodeType The device node type for the new device node.\r
571 @param NodeSubType The device node sub-type for the new device node.\r
572 @param NodeLength The length of the new device node.\r
573\r
3e5c3238 574 @return The new device path.\r
e386b444 575\r
576**/\r
577EFI_DEVICE_PATH_PROTOCOL *\r
578EFIAPI\r
579CreateDeviceNode (\r
580 IN UINT8 NodeType,\r
581 IN UINT8 NodeSubType,\r
582 IN UINT16 NodeLength\r
583 )\r
584{\r
585 return mDevicePathUtilities->CreateDeviceNode (NodeType, NodeSubType, NodeLength);\r
586}\r
587\r
588/**\r
589 Determines if a device path is single or multi-instance.\r
590\r
771729c7 591 This function returns TRUE if the device path specified by DevicePath is\r
58380e9c 592 multi-instance.\r
771729c7
RN
593 Otherwise, FALSE is returned.\r
594 If DevicePath is NULL or invalid, then FALSE is returned.\r
e386b444 595\r
596 @param DevicePath A pointer to a device path data structure.\r
597\r
598 @retval TRUE DevicePath is multi-instance.\r
771729c7
RN
599 @retval FALSE DevicePath is not multi-instance, or DevicePath \r
600 is NULL or invalid.\r
e386b444 601\r
602**/\r
603BOOLEAN\r
604EFIAPI\r
605IsDevicePathMultiInstance (\r
606 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
607 )\r
608{\r
609 return mDevicePathUtilities->IsDevicePathMultiInstance (DevicePath);\r
610}\r
611\r
612/**\r
613 Retrieves the device path protocol from a handle.\r
614\r
58380e9c 615 This function returns the device path protocol from the handle specified by Handle. \r
616 If Handle is NULL or Handle does not contain a device path protocol, then NULL \r
617 is returned.\r
e386b444 618 \r
58380e9c 619 @param Handle The handle from which to retrieve the device \r
620 path protocol.\r
e386b444 621\r
622 @return The device path protocol from the handle specified by Handle.\r
623\r
624**/\r
625EFI_DEVICE_PATH_PROTOCOL *\r
626EFIAPI\r
627DevicePathFromHandle (\r
628 IN EFI_HANDLE Handle\r
629 )\r
630{\r
631 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
632 EFI_STATUS Status;\r
633\r
634 Status = gBS->HandleProtocol (\r
635 Handle,\r
636 &gEfiDevicePathProtocolGuid,\r
637 (VOID *) &DevicePath\r
638 );\r
639 if (EFI_ERROR (Status)) {\r
640 DevicePath = NULL;\r
641 }\r
642 return DevicePath;\r
643}\r
644\r
645/**\r
646 Allocates a device path for a file and appends it to an existing device path.\r
647\r
58380e9c 648 If Device is a valid device handle that contains a device path protocol, then \r
649 a device path for the file specified by FileName is allocated and appended to \r
650 the device path associated with the handle Device. The allocated device path \r
651 is returned. If Device is NULL or Device is a handle that does not support the \r
652 device path protocol, then a device path containing a single device path node \r
653 for the file specified by FileName is allocated and returned.\r
654 The memory for the new device path is allocated from EFI boot services memory. \r
655 It is the responsibility of the caller to free the memory allocated.\r
3e5c3238 656 \r
e386b444 657 If FileName is NULL, then ASSERT().\r
3e5c3238 658 If FileName is not aligned on a 16-bit boundary, then ASSERT().\r
e386b444 659\r
58380e9c 660 @param Device A pointer to a device handle. This parameter \r
661 is optional and may be NULL.\r
e386b444 662 @param FileName A pointer to a Null-terminated Unicode string.\r
663\r
3e5c3238 664 @return The allocated device path.\r
e386b444 665\r
666**/\r
667EFI_DEVICE_PATH_PROTOCOL *\r
668EFIAPI\r
669FileDevicePath (\r
670 IN EFI_HANDLE Device, OPTIONAL\r
671 IN CONST CHAR16 *FileName\r
672 )\r
673{\r
674 UINTN Size;\r
675 FILEPATH_DEVICE_PATH *FilePath;\r
676 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
677 EFI_DEVICE_PATH_PROTOCOL *FileDevicePath;\r
678\r
679 DevicePath = NULL;\r
680\r
681 Size = StrSize (FileName);\r
e5dab016 682 FileDevicePath = AllocatePool (Size + SIZE_OF_FILEPATH_DEVICE_PATH + END_DEVICE_PATH_LENGTH);\r
e386b444 683 if (FileDevicePath != NULL) {\r
684 FilePath = (FILEPATH_DEVICE_PATH *) FileDevicePath;\r
685 FilePath->Header.Type = MEDIA_DEVICE_PATH;\r
686 FilePath->Header.SubType = MEDIA_FILEPATH_DP;\r
687 CopyMem (&FilePath->PathName, FileName, Size);\r
688 SetDevicePathNodeLength (&FilePath->Header, Size + SIZE_OF_FILEPATH_DEVICE_PATH);\r
689 SetDevicePathEndNode (NextDevicePathNode (&FilePath->Header));\r
690\r
691 if (Device != NULL) {\r
692 DevicePath = DevicePathFromHandle (Device);\r
693 }\r
694\r
695 DevicePath = AppendDevicePath (DevicePath, FileDevicePath);\r
696 FreePool (FileDevicePath);\r
697 }\r
698\r
699 return DevicePath;\r
700}\r