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