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