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