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