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