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