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