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