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