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