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