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