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