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