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