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