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