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