]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/UefiDevicePathLibDevicePathProtocol/UefiDevicePathLib.c
Update the copyright notice format
[mirror_edk2.git] / MdePkg / Library / UefiDevicePathLibDevicePathProtocol / UefiDevicePathLib.c
CommitLineData
e386b444 1/** @file\r
f008fc32 2 Library instance that implement UEFI Device Path Library class based on protocol\r
3 gEfiDevicePathUtilitiesProtocolGuid.\r
e386b444 4\r
5cba121d 5 Copyright (c) 2006 - 2009, Intel Corporation<BR>\r
e386b444 6 All rights reserved. This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
e386b444 14**/\r
15\r
c892d846 16\r
c7d265a9 17#include <Uefi.h>\r
c892d846 18\r
c7d265a9 19#include <Protocol/DevicePathUtilities.h>\r
c892d846 20\r
c7d265a9 21#include <Library/DevicePathLib.h>\r
22#include <Library/DebugLib.h>\r
23#include <Library/BaseLib.h>\r
24#include <Library/MemoryAllocationLib.h>\r
25#include <Library/BaseMemoryLib.h>\r
26#include <Library/UefiBootServicesTableLib.h>\r
e386b444 27\r
fe467413 28EFI_DEVICE_PATH_UTILITIES_PROTOCOL *mDevicePathUtilities = NULL;\r
e386b444 29\r
3dc728fb 30//\r
31// Template for an end-of-device path node.\r
32//\r
33GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_DEVICE_PATH_PROTOCOL mUefiDevicePathLibEndDevicePath = {\r
34 END_DEVICE_PATH_TYPE,\r
35 END_ENTIRE_DEVICE_PATH_SUBTYPE,\r
36 {\r
37 END_DEVICE_PATH_LENGTH,\r
38 0\r
39 }\r
40};\r
41\r
e386b444 42/**\r
43 The constructor function caches the pointer to DevicePathUtilites protocol.\r
44 \r
45 The constructor function locates DevicePathUtilities protocol from protocol database.\r
46 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS. \r
47\r
48 @param ImageHandle The firmware allocated handle for the EFI image.\r
49 @param SystemTable A pointer to the EFI System Table.\r
50 \r
51 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r
52\r
53**/\r
54EFI_STATUS\r
55EFIAPI\r
56DevicePathLibConstructor (\r
57 IN EFI_HANDLE ImageHandle,\r
58 IN EFI_SYSTEM_TABLE *SystemTable\r
59 )\r
60{\r
61 EFI_STATUS Status;\r
62\r
63 Status = gBS->LocateProtocol (\r
64 &gEfiDevicePathUtilitiesProtocolGuid,\r
65 NULL,\r
66 (VOID**) &mDevicePathUtilities\r
67 );\r
68 ASSERT_EFI_ERROR (Status);\r
69 ASSERT (mDevicePathUtilities != NULL);\r
70\r
71 return Status;\r
72}\r
73\r
3dc728fb 74/**\r
75 Returns the Type field of a device path node.\r
76\r
77 Returns the Type field of the device path node specified by Node.\r
78\r
79 If Node is NULL, then ASSERT().\r
80\r
81 @param Node A pointer to a device path node data structure.\r
82\r
83 @return The Type field of the device path node specified by Node.\r
84\r
85**/\r
86UINT8\r
5cba121d 87EFIAPI\r
3dc728fb 88DevicePathType (\r
89 IN CONST VOID *Node\r
90 )\r
91{\r
92 ASSERT (Node != NULL);\r
93 return ((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Type;\r
94}\r
95\r
96/**\r
97 Returns the SubType field of a device path node.\r
98\r
99 Returns the SubType field of the device path node specified by Node.\r
100\r
101 If Node is NULL, then ASSERT().\r
102\r
103 @param Node A pointer to a device path node data structure.\r
104\r
105 @return The SubType field of the device path node specified by Node.\r
106\r
107**/\r
108UINT8\r
5cba121d 109EFIAPI\r
3dc728fb 110DevicePathSubType (\r
111 IN CONST VOID *Node\r
112 )\r
113{\r
114 ASSERT (Node != NULL);\r
115 return ((EFI_DEVICE_PATH_PROTOCOL *)(Node))->SubType;\r
116}\r
117\r
118/**\r
119 Returns the 16-bit Length field of a device path node.\r
120\r
121 Returns the 16-bit Length field of the device path node specified by Node. \r
122 Node is not required to be aligned on a 16-bit boundary, so it is recommended\r
123 that a function such as ReadUnaligned16() be used to extract the contents of \r
124 the Length field.\r
125\r
126 If Node is NULL, then ASSERT().\r
127\r
128 @param Node A pointer to a device path node data structure.\r
129\r
130 @return The 16-bit Length field of the device path node specified by Node.\r
131\r
132**/\r
133UINTN\r
5cba121d 134EFIAPI\r
3dc728fb 135DevicePathNodeLength (\r
136 IN CONST VOID *Node\r
137 )\r
138{\r
139 ASSERT (Node != NULL);\r
140 return ReadUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0]);\r
141}\r
142\r
143/**\r
144 Returns a pointer to the next node in a device path.\r
145\r
146 Returns a pointer to the device path node that follows the device path node specified by Node.\r
147\r
148 If Node is NULL, then ASSERT().\r
149\r
150 @param Node A pointer to a device path node data structure.\r
151\r
152 @return a pointer to the device path node that follows the device path node specified by Node.\r
153\r
154**/\r
155EFI_DEVICE_PATH_PROTOCOL *\r
5cba121d 156EFIAPI\r
3dc728fb 157NextDevicePathNode (\r
158 IN CONST VOID *Node\r
159 )\r
160{\r
161 ASSERT (Node != NULL);\r
162 return (EFI_DEVICE_PATH_PROTOCOL *)((UINT8 *)(Node) + DevicePathNodeLength(Node));\r
163}\r
164\r
165/**\r
166 Determines if a device path node is an end node of a device path.\r
167 This includes nodes that are the end of a device path instance and nodes that are the end of an entire device path.\r
168\r
169 Determines if the device path node specified by Node is an end node of a device path. \r
170 This includes nodes that are the end of a device path instance and nodes that are the \r
171 end of an entire device path. If Node represents an end node of a device path, \r
172 then TRUE is returned. Otherwise, FALSE is returned.\r
173\r
174 If Node is NULL, then ASSERT().\r
175\r
176 @param Node A pointer to a device path node data structure.\r
177\r
178 @retval TRUE The device path node specified by Node is an end node of a device path.\r
179 @retval FALSE The device path node specified by Node is not an end node of a device path.\r
180 \r
181**/\r
182BOOLEAN\r
5cba121d 183EFIAPI\r
3dc728fb 184IsDevicePathEndType (\r
185 IN CONST VOID *Node\r
186 )\r
187{\r
188 ASSERT (Node != NULL);\r
a357faa3 189 return (BOOLEAN) (DevicePathType (Node) == END_DEVICE_PATH_TYPE);\r
3dc728fb 190}\r
191\r
192/**\r
193 Determines if a device path node is an end node of an entire device path.\r
194\r
195 Determines if a device path node specified by Node is an end node of an entire device path.\r
196 If Node represents the end of an entire device path, then TRUE is returned. Otherwise, FALSE is returned.\r
197\r
198 If Node is NULL, then ASSERT().\r
199\r
200 @param Node A pointer to a device path node data structure.\r
201\r
202 @retval TRUE The device path node specified by Node is the end of an entire device path.\r
203 @retval FALSE The device path node specified by Node is not the end of an entire device path.\r
204\r
205**/\r
206BOOLEAN\r
5cba121d 207EFIAPI\r
3dc728fb 208IsDevicePathEnd (\r
209 IN CONST VOID *Node\r
210 )\r
211{\r
212 ASSERT (Node != NULL);\r
1bfc7438 213 return (BOOLEAN) (IsDevicePathEndType (Node) && DevicePathSubType(Node) == END_ENTIRE_DEVICE_PATH_SUBTYPE);\r
3dc728fb 214}\r
215\r
216/**\r
217 Determines if a device path node is an end node of a device path instance.\r
218\r
219 Determines if a device path node specified by Node is an end node of a device path instance.\r
220 If Node represents the end of a device path instance, then TRUE is 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 a device path instance.\r
227 @retval FALSE The device path node specified by Node is not the end of a device path instance.\r
228\r
229**/\r
230BOOLEAN\r
5cba121d 231EFIAPI\r
3dc728fb 232IsDevicePathEndInstance (\r
233 IN CONST VOID *Node\r
234 )\r
235{\r
236 ASSERT (Node != NULL);\r
1bfc7438 237 return (BOOLEAN) (IsDevicePathEndType (Node) && DevicePathSubType(Node) == END_INSTANCE_DEVICE_PATH_SUBTYPE);\r
3dc728fb 238}\r
239\r
240/**\r
241 Sets the length, in bytes, of a device path node.\r
242\r
243 Sets the length of the device path node specified by Node to the value specified \r
244 by NodeLength. NodeLength is returned. Node is not required to be aligned on \r
245 a 16-bit boundary, so it is recommended that a function such as WriteUnaligned16()\r
246 be used to set the contents of the Length field.\r
247\r
248 If Node is NULL, then ASSERT().\r
249 If NodeLength >= 0x10000, then ASSERT().\r
250\r
251 @param Node A pointer to a device path node data structure.\r
252 @param Length The length, in bytes, of the device path node.\r
253\r
254 @return Length\r
255\r
256**/\r
257UINT16\r
5cba121d 258EFIAPI\r
3dc728fb 259SetDevicePathNodeLength (\r
9bb407c6 260 IN OUT VOID *Node,\r
8f0dd97e 261 IN UINTN Length\r
3dc728fb 262 )\r
263{\r
264 ASSERT (Node != NULL);\r
8f0dd97e 265 ASSERT (Length < 0x10000);\r
266 return WriteUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0], (UINT16)(Length));\r
3dc728fb 267}\r
268\r
269/**\r
270 Fills in all the fields of a device path node that is the end of an entire device path.\r
271\r
272 Fills in all the fields of a device path node specified by Node so Node represents \r
273 the end of an entire device path. The Type field of Node is set to \r
274 END_DEVICE_PATH_TYPE, the SubType field of Node is set to \r
275 END_ENTIRE_DEVICE_PATH_SUBTYPE, and the Length field of Node is set to \r
276 END_DEVICE_PATH_LENGTH. Node is not required to be aligned on a 16-bit boundary, \r
277 so it is recommended that a function such as WriteUnaligned16() be used to set \r
278 the contents of the Length field. \r
279\r
280 If Node is NULL, then ASSERT(). \r
281\r
282 @param Node A pointer to a device path node data structure.\r
283\r
284**/\r
285VOID\r
5cba121d 286EFIAPI\r
3dc728fb 287SetDevicePathEndNode (\r
9bb407c6 288 OUT VOID *Node\r
3dc728fb 289 )\r
290{\r
291 ASSERT (Node != NULL);\r
292 CopyMem (Node, &mUefiDevicePathLibEndDevicePath, sizeof (mUefiDevicePathLibEndDevicePath));\r
293}\r
294\r
e386b444 295/**\r
296 Returns the size of a device path in bytes.\r
297\r
298 This function returns the size, in bytes, of the device path data structure specified by\r
299 DevicePath including the end of device path node. If DevicePath is NULL, then 0 is returned.\r
300\r
301 @param DevicePath A pointer to a device path data structure.\r
3e5c3238 302 \r
303 @retval 0 If DevicePath is NULL.\r
304 @retval Others The size of a device path in bytes.\r
e386b444 305\r
306**/\r
307UINTN\r
308EFIAPI\r
309GetDevicePathSize (\r
310 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
311 )\r
312{\r
313 return mDevicePathUtilities->GetDevicePathSize (DevicePath);\r
314}\r
315\r
316/**\r
6a3f4ef9 317 Creates a new copy of an existing device path.\r
e386b444 318\r
319 This function allocates space for a new copy of the device path specified by DevicePath. If\r
320 DevicePath is NULL, then NULL is returned. If the memory is successfully allocated, then the\r
321 contents of DevicePath are copied to the newly allocated buffer, and a pointer to that buffer\r
322 is returned. Otherwise, NULL is returned. \r
3e5c3238 323 The memory for the new device path is allocated from EFI boot services memory. \r
324 It is the responsibility of the caller to free the memory allocated. \r
e386b444 325 \r
326 @param DevicePath A pointer to a device path data structure.\r
327\r
3e5c3238 328 @retval NULL If DevicePath is NULL.\r
329 @retval Others A pointer to the duplicated device path.\r
330 \r
e386b444 331**/\r
332EFI_DEVICE_PATH_PROTOCOL *\r
333EFIAPI\r
334DuplicateDevicePath (\r
335 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
336 )\r
337{\r
338 return mDevicePathUtilities->DuplicateDevicePath (DevicePath);\r
339}\r
340\r
341/**\r
342 Creates a new device path by appending a second device path to a first device path.\r
343\r
344 This function creates a new device path by appending a copy of SecondDevicePath to a copy of\r
345 FirstDevicePath in a newly allocated buffer. Only the end-of-device-path device node from\r
346 SecondDevicePath is retained. The newly created device path is returned. \r
347 If FirstDevicePath is NULL, then it is ignored, and a duplicate of SecondDevicePath is returned. \r
348 If SecondDevicePath is NULL, then it is ignored, and a duplicate of FirstDevicePath is returned. \r
98a14db6 349 If both FirstDevicePath and SecondDevicePath are NULL, then a copy of an end-of-device-path is\r
350 returned. \r
e386b444 351 If there is not enough memory for the newly allocated buffer, then NULL is returned.\r
352 The memory for the new device path is allocated from EFI boot services memory. It is the\r
353 responsibility of the caller to free the memory allocated.\r
354\r
355 @param FirstDevicePath A pointer to a device path data structure.\r
356 @param SecondDevicePath A pointer to a device path data structure.\r
3e5c3238 357 \r
358 @retval NULL If there is not enough memory for the newly allocated buffer.\r
359 @retval Others A pointer to the new device path if success.\r
360 Or a copy an end-of-device-path if both FirstDevicePath and SecondDevicePath are NULL.\r
e386b444 361\r
362**/\r
363EFI_DEVICE_PATH_PROTOCOL *\r
364EFIAPI\r
365AppendDevicePath (\r
366 IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath, OPTIONAL\r
367 IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath OPTIONAL\r
368 )\r
369{\r
370 return mDevicePathUtilities->AppendDevicePath (FirstDevicePath, SecondDevicePath);\r
371}\r
372\r
373/**\r
374 Creates a new path by appending the device node to the device path.\r
375\r
376 This function creates a new device path by appending a copy of the device node specified by\r
377 DevicePathNode to a copy of the device path specified by DevicePath in an allocated buffer.\r
378 The end-of-device-path device node is moved after the end of the appended device node.\r
98a14db6 379 If DevicePathNode is NULL then a copy of DevicePath is returned.\r
6336a895 380 If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device path device\r
381 node is returned.\r
98a14db6 382 If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path device node\r
383 is returned.\r
e386b444 384 If there is not enough memory to allocate space for the new device path, then NULL is returned. \r
385 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to\r
386 free the memory allocated.\r
387\r
388 @param DevicePath A pointer to a device path data structure.\r
389 @param DevicePathNode A pointer to a single device path node.\r
390\r
3e5c3238 391 @retval NULL If there is not enough memory for the new device path.\r
392 @retval Others A pointer to the new device path if success.\r
393 A copy of DevicePathNode followed by an end-of-device-path node \r
394 if both FirstDevicePath and SecondDevicePath are NULL.\r
395 A copy of an end-of-device-path node if both FirstDevicePath and SecondDevicePath are NULL.\r
e386b444 396\r
397**/\r
398EFI_DEVICE_PATH_PROTOCOL *\r
399EFIAPI\r
400AppendDevicePathNode (\r
401 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL\r
402 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathNode OPTIONAL\r
403 )\r
404{\r
405 return mDevicePathUtilities->AppendDeviceNode (DevicePath, DevicePathNode);\r
406}\r
407\r
408/**\r
409 Creates a new device path by appending the specified device path instance to the specified device\r
410 path.\r
411 \r
412 This function creates a new device path by appending a copy of the device path instance specified\r
413 by DevicePathInstance to a copy of the device path secified by DevicePath in a allocated buffer.\r
414 The end-of-device-path device node is moved after the end of the appended device path instance\r
415 and a new end-of-device-path-instance node is inserted between. \r
416 If DevicePath is NULL, then a copy if DevicePathInstance is returned.\r
417 If DevicePathInstance is NULL, then NULL is returned.\r
418 If there is not enough memory to allocate space for the new device path, then NULL is returned. \r
419 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to\r
420 free the memory allocated.\r
421 \r
422 @param DevicePath A pointer to a device path data structure.\r
423 @param DevicePathInstance A pointer to a device path instance.\r
424\r
425 @return A pointer to the new device path.\r
426\r
427**/\r
428EFI_DEVICE_PATH_PROTOCOL *\r
429EFIAPI\r
430AppendDevicePathInstance (\r
431 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL\r
432 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathInstance OPTIONAL\r
433 )\r
434{\r
435 return mDevicePathUtilities->AppendDevicePathInstance (DevicePath, DevicePathInstance);\r
436}\r
437\r
438/**\r
439 Creates a copy of the current device path instance and returns a pointer to the next device path\r
440 instance.\r
441\r
442 This function creates a copy of the current device path instance. It also updates DevicePath to\r
443 point to the next device path instance in the device path (or NULL if no more) and updates Size\r
444 to hold the size of the device path instance copy.\r
445 If DevicePath is NULL, then NULL is returned.\r
446 If there is not enough memory to allocate space for the new device path, then NULL is returned. \r
447 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to\r
448 free the memory allocated.\r
449 If Size is NULL, then ASSERT().\r
450 \r
451 @param DevicePath On input, this holds the pointer to the current device path\r
452 instance. On output, this holds the pointer to the next device\r
453 path instance or NULL if there are no more device path\r
454 instances in the device path pointer to a device path data\r
455 structure.\r
456 @param Size On output, this holds the size of the device path instance, in\r
457 bytes or zero, if DevicePath is NULL.\r
458\r
459 @return A pointer to the current device path instance.\r
460\r
461**/\r
462EFI_DEVICE_PATH_PROTOCOL *\r
463EFIAPI\r
464GetNextDevicePathInstance (\r
465 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath,\r
466 OUT UINTN *Size\r
467 )\r
468{\r
469 ASSERT (Size != NULL);\r
470 return mDevicePathUtilities->GetNextDevicePathInstance (DevicePath, Size);\r
471}\r
472\r
473/**\r
3e5c3238 474 Creates a device node.\r
e386b444 475\r
476 This function creates a new device node in a newly allocated buffer of size NodeLength and\r
477 initializes the device path node header with NodeType and NodeSubType. The new device path node\r
478 is returned.\r
479 If NodeLength is smaller than a device path header, then NULL is returned. \r
480 If there is not enough memory to allocate space for the new device path, then NULL is returned. \r
481 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to\r
482 free the memory allocated.\r
483\r
484 @param NodeType The device node type for the new device node.\r
485 @param NodeSubType The device node sub-type for the new device node.\r
486 @param NodeLength The length of the new device node.\r
487\r
3e5c3238 488 @return The new device path.\r
e386b444 489\r
490**/\r
491EFI_DEVICE_PATH_PROTOCOL *\r
492EFIAPI\r
493CreateDeviceNode (\r
494 IN UINT8 NodeType,\r
495 IN UINT8 NodeSubType,\r
496 IN UINT16 NodeLength\r
497 )\r
498{\r
499 return mDevicePathUtilities->CreateDeviceNode (NodeType, NodeSubType, NodeLength);\r
500}\r
501\r
502/**\r
503 Determines if a device path is single or multi-instance.\r
504\r
505 This function returns TRUE if the device path specified by DevicePath is multi-instance.\r
506 Otherwise, FALSE is returned. If DevicePath is NULL, then FALSE is returned.\r
507\r
508 @param DevicePath A pointer to a device path data structure.\r
509\r
510 @retval TRUE DevicePath is multi-instance.\r
511 @retval FALSE DevicePath is not multi-instance or DevicePath is NULL.\r
512\r
513**/\r
514BOOLEAN\r
515EFIAPI\r
516IsDevicePathMultiInstance (\r
517 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
518 )\r
519{\r
520 return mDevicePathUtilities->IsDevicePathMultiInstance (DevicePath);\r
521}\r
522\r
523/**\r
524 Retrieves the device path protocol from a handle.\r
525\r
526 This function returns the device path protocol from the handle specified by Handle. If Handle is\r
527 NULL or Handle does not contain a device path protocol, then NULL is returned.\r
528 \r
529 @param Handle The handle from which to retrieve the device path protocol.\r
530\r
531 @return The device path protocol from the handle specified by Handle.\r
532\r
533**/\r
534EFI_DEVICE_PATH_PROTOCOL *\r
535EFIAPI\r
536DevicePathFromHandle (\r
537 IN EFI_HANDLE Handle\r
538 )\r
539{\r
540 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
541 EFI_STATUS Status;\r
542\r
543 Status = gBS->HandleProtocol (\r
544 Handle,\r
545 &gEfiDevicePathProtocolGuid,\r
546 (VOID *) &DevicePath\r
547 );\r
548 if (EFI_ERROR (Status)) {\r
549 DevicePath = NULL;\r
550 }\r
551 return DevicePath;\r
552}\r
553\r
554/**\r
555 Allocates a device path for a file and appends it to an existing device path.\r
556\r
557 If Device is a valid device handle that contains a device path protocol, then a device path for\r
558 the file specified by FileName is allocated and appended to the device path associated with the\r
559 handle Device. The allocated device path is returned. If Device is NULL or Device is a handle\r
560 that does not support the device path protocol, then a device path containing a single device\r
561 path node for the file specified by FileName is allocated and returned.\r
3e5c3238 562 The memory for the new device path is allocated from EFI boot services memory. It is the responsibility\r
563 of the caller to free the memory allocated.\r
564 \r
e386b444 565 If FileName is NULL, then ASSERT().\r
3e5c3238 566 If FileName is not aligned on a 16-bit boundary, then ASSERT().\r
e386b444 567\r
568 @param Device A pointer to a device handle. This parameter is optional and\r
569 may be NULL.\r
570 @param FileName A pointer to a Null-terminated Unicode string.\r
571\r
3e5c3238 572 @return The allocated device path.\r
e386b444 573\r
574**/\r
575EFI_DEVICE_PATH_PROTOCOL *\r
576EFIAPI\r
577FileDevicePath (\r
578 IN EFI_HANDLE Device, OPTIONAL\r
579 IN CONST CHAR16 *FileName\r
580 )\r
581{\r
582 UINTN Size;\r
583 FILEPATH_DEVICE_PATH *FilePath;\r
584 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
585 EFI_DEVICE_PATH_PROTOCOL *FileDevicePath;\r
586\r
587 DevicePath = NULL;\r
588\r
589 Size = StrSize (FileName);\r
e5dab016 590 FileDevicePath = AllocatePool (Size + SIZE_OF_FILEPATH_DEVICE_PATH + END_DEVICE_PATH_LENGTH);\r
e386b444 591 if (FileDevicePath != NULL) {\r
592 FilePath = (FILEPATH_DEVICE_PATH *) FileDevicePath;\r
593 FilePath->Header.Type = MEDIA_DEVICE_PATH;\r
594 FilePath->Header.SubType = MEDIA_FILEPATH_DP;\r
595 CopyMem (&FilePath->PathName, FileName, Size);\r
596 SetDevicePathNodeLength (&FilePath->Header, Size + SIZE_OF_FILEPATH_DEVICE_PATH);\r
597 SetDevicePathEndNode (NextDevicePathNode (&FilePath->Header));\r
598\r
599 if (Device != NULL) {\r
600 DevicePath = DevicePathFromHandle (Device);\r
601 }\r
602\r
603 DevicePath = AppendDevicePath (DevicePath, FileDevicePath);\r
604 FreePool (FileDevicePath);\r
605 }\r
606\r
607 return DevicePath;\r
608}\r