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