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