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