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