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