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