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