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