]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/UefiDevicePathLib/DevicePathUtilities.c
Add 'file not found' debug message to MTFTP.
[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
11 Copyright (c) 2006 - 2013, 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\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
156 UINTN Length;\r
157\r
158 ASSERT (Node != NULL);\r
159 Length = ReadUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0]);\r
160 ASSERT (Length >= sizeof (EFI_DEVICE_PATH_PROTOCOL));\r
161 return Length;\r
162}\r
163\r
164/**\r
165 Returns a pointer to the next node in a device path.\r
166\r
167 Returns a pointer to the device path node that follows the device path node \r
168 specified by Node.\r
169\r
170 If Node is NULL, then ASSERT().\r
171\r
172 @param Node A pointer to a device path node data structure.\r
173\r
174 @return a pointer to the device path node that follows the device path node \r
175 specified by Node.\r
176\r
177**/\r
178EFI_DEVICE_PATH_PROTOCOL *\r
179EFIAPI\r
180NextDevicePathNode (\r
181 IN CONST VOID *Node\r
182 )\r
183{\r
184 ASSERT (Node != NULL);\r
185 return (EFI_DEVICE_PATH_PROTOCOL *)((UINT8 *)(Node) + DevicePathNodeLength(Node));\r
186}\r
187\r
188/**\r
189 Determines if a device path 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 \r
191 are the end of an entire device path.\r
192\r
193 Determines if the device path node specified by Node is an end node of a device path. \r
194 This includes nodes that are the end of a device path instance and nodes that are the \r
195 end of an entire device path. If Node represents an end node of a device path, \r
196 then TRUE is returned. Otherwise, FALSE is returned.\r
197\r
198 If Node is NULL, then ASSERT().\r
199\r
200 @param Node A pointer to a device path node data structure.\r
201\r
202 @retval TRUE The device path node specified by Node is an end node of a \r
203 device path.\r
204 @retval FALSE The device path node specified by Node is not an end node of \r
205 a device path.\r
206 \r
207**/\r
208BOOLEAN\r
209EFIAPI\r
210IsDevicePathEndType (\r
211 IN 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
236EFIAPI\r
237IsDevicePathEnd (\r
238 IN CONST VOID *Node\r
239 )\r
240{\r
241 ASSERT (Node != NULL);\r
242 return (BOOLEAN) (IsDevicePathEndType (Node) && DevicePathSubType(Node) == END_ENTIRE_DEVICE_PATH_SUBTYPE);\r
243}\r
244\r
245/**\r
246 Determines if a device path node is an end node of a device path instance.\r
247\r
248 Determines if a device path node specified by Node is an end node of a device \r
249 path instance. If Node represents the end of a device path instance, then TRUE \r
250 is returned. Otherwise, FALSE is returned.\r
251\r
252 If Node is NULL, then ASSERT().\r
253\r
254 @param Node A pointer to a device path node data structure.\r
255\r
256 @retval TRUE The device path node specified by Node is the end of a device \r
257 path instance.\r
258 @retval FALSE The device path node specified by Node is not the end of a \r
259 device path instance.\r
260\r
261**/\r
262BOOLEAN\r
263EFIAPI\r
264IsDevicePathEndInstance (\r
265 IN CONST VOID *Node\r
266 )\r
267{\r
268 ASSERT (Node != NULL);\r
269 return (BOOLEAN) (IsDevicePathEndType (Node) && DevicePathSubType(Node) == END_INSTANCE_DEVICE_PATH_SUBTYPE);\r
270}\r
271\r
272/**\r
273 Sets the length, in bytes, of a device path node.\r
274\r
275 Sets the length of the device path node specified by Node to the value specified \r
276 by NodeLength. NodeLength is returned. Node is not required to be aligned on \r
277 a 16-bit boundary, so it is recommended that a function such as WriteUnaligned16()\r
278 be used to set the contents of the Length field.\r
279\r
280 If Node is NULL, then ASSERT().\r
281 If NodeLength >= SIZE_64KB, then ASSERT().\r
282 If NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL), then ASSERT().\r
283\r
284 @param Node A pointer to a device path node data structure.\r
285 @param Length The length, in bytes, of the device path node.\r
286\r
287 @return Length\r
288\r
289**/\r
290UINT16\r
291EFIAPI\r
292SetDevicePathNodeLength (\r
293 IN OUT VOID *Node,\r
294 IN UINTN Length\r
295 )\r
296{\r
297 ASSERT (Node != NULL);\r
298 ASSERT ((Length >= sizeof (EFI_DEVICE_PATH_PROTOCOL)) && (Length < SIZE_64KB));\r
299 return WriteUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0], (UINT16)(Length));\r
300}\r
301\r
302/**\r
303 Fills in all the fields of a device path node that is the end of an entire device path.\r
304\r
305 Fills in all the fields of a device path node specified by Node so Node represents \r
306 the end of an entire device path. The Type field of Node is set to \r
307 END_DEVICE_PATH_TYPE, the SubType field of Node is set to \r
308 END_ENTIRE_DEVICE_PATH_SUBTYPE, and the Length field of Node is set to \r
309 END_DEVICE_PATH_LENGTH. Node is not required to be aligned on a 16-bit boundary, \r
310 so it is recommended that a function such as WriteUnaligned16() be used to set \r
311 the contents of the Length field. \r
312\r
313 If Node is NULL, then ASSERT(). \r
314\r
315 @param Node A pointer to a device path node data structure.\r
316\r
317**/\r
318VOID\r
319EFIAPI\r
320SetDevicePathEndNode (\r
321 OUT VOID *Node\r
322 )\r
323{\r
324 ASSERT (Node != NULL);\r
325 CopyMem (Node, &mUefiDevicePathLibEndDevicePath, sizeof (mUefiDevicePathLibEndDevicePath));\r
326}\r
327\r
328/**\r
329 Returns the size of a device path in bytes.\r
330\r
331 This function returns the size, in bytes, of the device path data structure \r
332 specified by DevicePath including the end of device path node.\r
333 If DevicePath is NULL or invalid, then 0 is returned.\r
334\r
335 @param DevicePath A pointer to a device path data structure.\r
336\r
337 @retval 0 If DevicePath is NULL or invalid.\r
338 @retval Others The size of a device path in bytes.\r
339\r
340**/\r
341UINTN\r
342EFIAPI\r
343UefiDevicePathLibGetDevicePathSize (\r
344 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
345 )\r
346{\r
347 CONST EFI_DEVICE_PATH_PROTOCOL *Start;\r
348\r
349 if (DevicePath == NULL) {\r
350 return 0;\r
351 }\r
352\r
353 if (!IsDevicePathValid (DevicePath, 0)) {\r
354 return 0;\r
355 }\r
356\r
357 //\r
358 // Search for the end of the device path structure\r
359 //\r
360 Start = DevicePath;\r
361 while (!IsDevicePathEnd (DevicePath)) {\r
362 DevicePath = NextDevicePathNode (DevicePath);\r
363 }\r
364\r
365 //\r
366 // Compute the size and add back in the size of the end device path structure\r
367 //\r
368 return ((UINTN) DevicePath - (UINTN) Start) + DevicePathNodeLength (DevicePath);\r
369}\r
370\r
371/**\r
372 Creates a new copy of an existing device path.\r
373\r
374 This function allocates space for a new copy of the device path specified by DevicePath. \r
375 If DevicePath is NULL, then NULL is returned. If the memory is successfully \r
376 allocated, then the contents of DevicePath are copied to the newly allocated \r
377 buffer, and a pointer to that buffer is returned. Otherwise, NULL is returned. \r
378 The memory for the new device path is allocated from EFI boot services memory. \r
379 It is the responsibility of the caller to free the memory allocated. \r
380 \r
381 @param DevicePath A pointer to a device path data structure.\r
382\r
383 @retval NULL DevicePath is NULL or invalid.\r
384 @retval Others A pointer to the duplicated device path.\r
385 \r
386**/\r
387EFI_DEVICE_PATH_PROTOCOL *\r
388EFIAPI\r
389UefiDevicePathLibDuplicateDevicePath (\r
390 IN 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
435EFIAPI\r
436UefiDevicePathLibAppendDevicePath (\r
437 IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath, OPTIONAL\r
438 IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath OPTIONAL\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 = CopyMem (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 CopyMem (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
514EFIAPI\r
515UefiDevicePathLibAppendDevicePathNode (\r
516 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL\r
517 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathNode OPTIONAL\r
518 )\r
519{\r
520 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
521 EFI_DEVICE_PATH_PROTOCOL *NextNode;\r
522 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
523 UINTN NodeLength;\r
524\r
525 if (DevicePathNode == NULL) {\r
526 return DuplicateDevicePath ((DevicePath != NULL) ? DevicePath : &mUefiDevicePathLibEndDevicePath);\r
527 }\r
528 //\r
529 // Build a Node that has a terminator on it\r
530 //\r
531 NodeLength = DevicePathNodeLength (DevicePathNode);\r
532\r
533 TempDevicePath = AllocatePool (NodeLength + END_DEVICE_PATH_LENGTH);\r
534 if (TempDevicePath == NULL) {\r
535 return NULL;\r
536 }\r
537 TempDevicePath = CopyMem (TempDevicePath, DevicePathNode, NodeLength);\r
538 //\r
539 // Add and end device path node to convert Node to device path\r
540 //\r
541 NextNode = NextDevicePathNode (TempDevicePath);\r
542 SetDevicePathEndNode (NextNode);\r
543 //\r
544 // Append device paths\r
545 //\r
546 NewDevicePath = AppendDevicePath (DevicePath, TempDevicePath);\r
547\r
548 FreePool (TempDevicePath);\r
549\r
550 return NewDevicePath;\r
551}\r
552\r
553/**\r
554 Creates a new device path by appending the specified device path instance to the specified device\r
555 path.\r
556 \r
557 This function creates a new device path by appending a copy of the device path \r
558 instance specified by DevicePathInstance to a copy of the device path specified \r
559 by DevicePath in a allocated buffer.\r
560 The end-of-device-path device node is moved after the end of the appended device \r
561 path instance and a new end-of-device-path-instance node is inserted between. \r
562 If DevicePath is NULL, then a copy if DevicePathInstance is returned.\r
563 If DevicePathInstance is NULL, then NULL is returned.\r
564 If DevicePath or DevicePathInstance is invalid, then NULL is returned.\r
565 If there is not enough memory to allocate space for the new device path, then \r
566 NULL is returned. \r
567 The memory is allocated from EFI boot services memory. It is the responsibility \r
568 of the caller to free the memory allocated.\r
569 \r
570 @param DevicePath A pointer to a device path data structure.\r
571 @param DevicePathInstance A pointer to a device path instance.\r
572\r
573 @return A pointer to the new device path.\r
574\r
575**/\r
576EFI_DEVICE_PATH_PROTOCOL *\r
577EFIAPI\r
578UefiDevicePathLibAppendDevicePathInstance (\r
579 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL\r
580 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathInstance OPTIONAL\r
581 )\r
582{\r
583 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
584 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
585 UINTN SrcSize;\r
586 UINTN InstanceSize;\r
587\r
588 if (DevicePath == NULL) {\r
589 return DuplicateDevicePath (DevicePathInstance);\r
590 }\r
591\r
592 if (DevicePathInstance == NULL) {\r
593 return NULL;\r
594 }\r
595\r
596 if (!IsDevicePathValid (DevicePath, 0) || !IsDevicePathValid (DevicePathInstance, 0)) {\r
597 return NULL;\r
598 }\r
599\r
600 SrcSize = GetDevicePathSize (DevicePath);\r
601 InstanceSize = GetDevicePathSize (DevicePathInstance);\r
602\r
603 NewDevicePath = AllocatePool (SrcSize + InstanceSize);\r
604 if (NewDevicePath != NULL) {\r
605 \r
606 TempDevicePath = CopyMem (NewDevicePath, DevicePath, SrcSize);;\r
607 \r
608 while (!IsDevicePathEnd (TempDevicePath)) {\r
609 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
610 }\r
611 \r
612 TempDevicePath->SubType = END_INSTANCE_DEVICE_PATH_SUBTYPE;\r
613 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
614 CopyMem (TempDevicePath, DevicePathInstance, InstanceSize);\r
615 }\r
616\r
617 return NewDevicePath;\r
618}\r
619\r
620/**\r
621 Creates a copy of the current device path instance and returns a pointer to the next device path\r
622 instance.\r
623\r
624 This function creates a copy of the current device path instance. It also updates \r
625 DevicePath to point to the next device path instance in the device path (or NULL \r
626 if no more) and updates Size to hold the size of the device path instance copy.\r
627 If DevicePath is NULL, then NULL is returned.\r
628 If DevicePath points to a invalid device path, then NULL is returned.\r
629 If there is not enough memory to allocate space for the new device path, then \r
630 NULL is returned. \r
631 The memory is allocated from EFI boot services memory. It is the responsibility \r
632 of the caller to free the memory allocated.\r
633 If Size is NULL, then ASSERT().\r
634 \r
635 @param DevicePath On input, this holds the pointer to the current \r
636 device path instance. On output, this holds \r
637 the pointer to the next device path instance \r
638 or NULL if there are no more device path\r
639 instances in the device path pointer to a \r
640 device path data structure.\r
641 @param Size On output, this holds the size of the device \r
642 path instance, in bytes or zero, if DevicePath \r
643 is NULL.\r
644\r
645 @return A pointer to the current device path instance.\r
646\r
647**/\r
648EFI_DEVICE_PATH_PROTOCOL *\r
649EFIAPI\r
650UefiDevicePathLibGetNextDevicePathInstance (\r
651 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath,\r
652 OUT UINTN *Size\r
653 )\r
654{\r
655 EFI_DEVICE_PATH_PROTOCOL *DevPath;\r
656 EFI_DEVICE_PATH_PROTOCOL *ReturnValue;\r
657 UINT8 Temp;\r
658\r
659 ASSERT (Size != NULL);\r
660\r
661 if (DevicePath == NULL || *DevicePath == NULL) {\r
662 *Size = 0;\r
663 return NULL;\r
664 }\r
665\r
666 if (!IsDevicePathValid (*DevicePath, 0)) {\r
667 return NULL;\r
668 }\r
669\r
670 //\r
671 // Find the end of the device path instance\r
672 //\r
673 DevPath = *DevicePath;\r
674 while (!IsDevicePathEndType (DevPath)) {\r
675 DevPath = NextDevicePathNode (DevPath);\r
676 }\r
677\r
678 //\r
679 // Compute the size of the device path instance\r
680 //\r
681 *Size = ((UINTN) DevPath - (UINTN) (*DevicePath)) + sizeof (EFI_DEVICE_PATH_PROTOCOL);\r
682 \r
683 //\r
684 // Make a copy and return the device path instance\r
685 //\r
686 Temp = DevPath->SubType;\r
687 DevPath->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;\r
688 ReturnValue = DuplicateDevicePath (*DevicePath);\r
689 DevPath->SubType = Temp;\r
690\r
691 //\r
692 // If DevPath is the end of an entire device path, then another instance\r
693 // does not follow, so *DevicePath is set to NULL.\r
694 //\r
695 if (DevicePathSubType (DevPath) == END_ENTIRE_DEVICE_PATH_SUBTYPE) {\r
696 *DevicePath = NULL;\r
697 } else {\r
698 *DevicePath = NextDevicePathNode (DevPath);\r
699 }\r
700\r
701 return ReturnValue;\r
702}\r
703\r
704/**\r
705 Creates a device node.\r
706\r
707 This function creates a new device node in a newly allocated buffer of size \r
708 NodeLength and initializes the device path node header with NodeType and NodeSubType. \r
709 The new device path node is returned.\r
710 If NodeLength is smaller than a device path header, then NULL is returned. \r
711 If there is not enough memory to allocate space for the new device path, then \r
712 NULL is returned. \r
713 The memory is allocated from EFI boot services memory. It is the responsibility \r
714 of the caller to free the memory allocated.\r
715\r
716 @param NodeType The device node type for the new device node.\r
717 @param NodeSubType The device node sub-type for the new device node.\r
718 @param NodeLength The length of the new device node.\r
719\r
720 @return The new device path.\r
721\r
722**/\r
723EFI_DEVICE_PATH_PROTOCOL *\r
724EFIAPI\r
725UefiDevicePathLibCreateDeviceNode (\r
726 IN UINT8 NodeType,\r
727 IN UINT8 NodeSubType,\r
728 IN UINT16 NodeLength\r
729 )\r
730{\r
731 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
732\r
733 if (NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {\r
734 //\r
735 // NodeLength is less than the size of the header.\r
736 //\r
737 return NULL;\r
738 }\r
739 \r
740 DevicePath = AllocateZeroPool (NodeLength);\r
741 if (DevicePath != NULL) {\r
742 DevicePath->Type = NodeType;\r
743 DevicePath->SubType = NodeSubType;\r
744 SetDevicePathNodeLength (DevicePath, NodeLength);\r
745 }\r
746\r
747 return DevicePath;\r
748}\r
749\r
750/**\r
751 Determines if a device path is single or multi-instance.\r
752\r
753 This function returns TRUE if the device path specified by DevicePath is\r
754 multi-instance.\r
755 Otherwise, FALSE is returned.\r
756 If DevicePath is NULL or invalid, then FALSE is returned.\r
757\r
758 @param DevicePath A pointer to a device path data structure.\r
759\r
760 @retval TRUE DevicePath is multi-instance.\r
761 @retval FALSE DevicePath is not multi-instance, or DevicePath \r
762 is NULL or invalid.\r
763\r
764**/\r
765BOOLEAN\r
766EFIAPI\r
767UefiDevicePathLibIsDevicePathMultiInstance (\r
768 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
769 )\r
770{\r
771 CONST EFI_DEVICE_PATH_PROTOCOL *Node;\r
772\r
773 if (DevicePath == NULL) {\r
774 return FALSE;\r
775 }\r
776\r
777 if (!IsDevicePathValid (DevicePath, 0)) {\r
778 return FALSE;\r
779 }\r
780\r
781 Node = DevicePath;\r
782 while (!IsDevicePathEnd (Node)) {\r
783 if (IsDevicePathEndInstance (Node)) {\r
784 return TRUE;\r
785 }\r
786\r
787 Node = NextDevicePathNode (Node);\r
788 }\r
789\r
790 return FALSE;\r
791}\r
792\r
793\r
794/**\r
795 Retrieves the device path protocol from a handle.\r
796\r
797 This function returns the device path protocol from the handle specified by Handle. \r
798 If Handle is NULL or Handle does not contain a device path protocol, then NULL \r
799 is returned.\r
800 \r
801 @param Handle The handle from which to retrieve the device \r
802 path protocol.\r
803\r
804 @return The device path protocol from the handle specified by Handle.\r
805\r
806**/\r
807EFI_DEVICE_PATH_PROTOCOL *\r
808EFIAPI\r
809DevicePathFromHandle (\r
810 IN EFI_HANDLE Handle\r
811 )\r
812{\r
813 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
814 EFI_STATUS Status;\r
815\r
816 Status = gBS->HandleProtocol (\r
817 Handle,\r
818 &gEfiDevicePathProtocolGuid,\r
819 (VOID *) &DevicePath\r
820 );\r
821 if (EFI_ERROR (Status)) {\r
822 DevicePath = NULL;\r
823 }\r
824 return DevicePath;\r
825}\r
826\r
827/**\r
828 Allocates a device path for a file and appends it to an existing device path.\r
829\r
830 If Device is a valid device handle that contains a device path protocol, then a device path for\r
831 the file specified by FileName is allocated and appended to the device path associated with the\r
832 handle Device. The allocated device path is returned. If Device is NULL or Device is a handle\r
833 that does not support the device path protocol, then a device path containing a single device\r
834 path node for the file specified by FileName is allocated and returned.\r
835 The memory for the new device path is allocated from EFI boot services memory. It is the responsibility\r
836 of the caller to free the memory allocated.\r
837 \r
838 If FileName is NULL, then ASSERT().\r
839 If FileName is not aligned on a 16-bit boundary, then ASSERT().\r
840\r
841 @param Device A pointer to a device handle. This parameter \r
842 is optional and may be NULL.\r
843 @param FileName A pointer to a Null-terminated Unicode string.\r
844\r
845 @return The allocated device path.\r
846\r
847**/\r
848EFI_DEVICE_PATH_PROTOCOL *\r
849EFIAPI\r
850FileDevicePath (\r
851 IN EFI_HANDLE Device, OPTIONAL\r
852 IN CONST CHAR16 *FileName\r
853 )\r
854{\r
855 UINTN Size;\r
856 FILEPATH_DEVICE_PATH *FilePath;\r
857 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
858 EFI_DEVICE_PATH_PROTOCOL *FileDevicePath;\r
859\r
860 DevicePath = NULL;\r
861\r
862 Size = StrSize (FileName);\r
863 FileDevicePath = AllocatePool (Size + SIZE_OF_FILEPATH_DEVICE_PATH + END_DEVICE_PATH_LENGTH);\r
864 if (FileDevicePath != NULL) {\r
865 FilePath = (FILEPATH_DEVICE_PATH *) FileDevicePath;\r
866 FilePath->Header.Type = MEDIA_DEVICE_PATH;\r
867 FilePath->Header.SubType = MEDIA_FILEPATH_DP;\r
868 CopyMem (&FilePath->PathName, FileName, Size);\r
869 SetDevicePathNodeLength (&FilePath->Header, Size + SIZE_OF_FILEPATH_DEVICE_PATH);\r
870 SetDevicePathEndNode (NextDevicePathNode (&FilePath->Header));\r
871\r
872 if (Device != NULL) {\r
873 DevicePath = DevicePathFromHandle (Device);\r
874 }\r
875\r
876 DevicePath = AppendDevicePath (DevicePath, FileDevicePath);\r
877 FreePool (FileDevicePath);\r
878 }\r
879\r
880 return DevicePath;\r
881}\r
882\r