]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.c
fixed typo.
[mirror_edk2.git] / MdePkg / Library / UefiDevicePathLib / UefiDevicePathLib.c
CommitLineData
e386b444 1/** @file\r
2 Device Path services. The thing to remember is device paths are built out of\r
3 nodes. The device path is terminated by an end node that is length\r
4 sizeof(EFI_DEVICE_PATH_PROTOCOL). That would be why there is sizeof(EFI_DEVICE_PATH_PROTOCOL)\r
5 all over this file.\r
6\r
7 The only place where multi-instance device paths are supported is in\r
8 environment varibles. Multi-instance device paths should never be placed\r
9 on a Handle.\r
10\r
11 Copyright (c) 2006, Intel Corporation \r
12 All rights reserved. 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
e386b444 20**/\r
21\r
c892d846 22\r
c7d265a9 23#include <Uefi.h>\r
c892d846 24\r
c7d265a9 25#include <Protocol/DevicePath.h>\r
c892d846 26\r
c7d265a9 27#include <Library/DevicePathLib.h>\r
28#include <Library/BaseMemoryLib.h>\r
29#include <Library/DebugLib.h>\r
30#include <Library/MemoryAllocationLib.h>\r
31#include <Library/UefiBootServicesTableLib.h>\r
32#include <Library/BaseLib.h>\r
e386b444 33\r
98a14db6 34//\r
35// Template for an end-of-device path node.\r
36//\r
697f4d59 37STATIC EFI_DEVICE_PATH_PROTOCOL mEndDevicePath = {\r
98a14db6 38 END_DEVICE_PATH_TYPE,\r
39 END_ENTIRE_DEVICE_PATH_SUBTYPE,\r
697f4d59 40 {\r
41 END_DEVICE_PATH_LENGTH,\r
42 0\r
43 }\r
98a14db6 44};\r
45\r
e386b444 46/**\r
47 Returns the size of a device path in bytes.\r
48\r
49 This function returns the size, in bytes, of the device path data structure specified by\r
50 DevicePath including the end of device path node. If DevicePath is NULL, then 0 is returned.\r
51\r
52 @param DevicePath A pointer to a device path data structure.\r
53\r
54 @return The size of a device path in bytes.\r
55\r
56**/\r
57UINTN\r
58EFIAPI\r
59GetDevicePathSize (\r
60 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
61 )\r
62{\r
63 CONST EFI_DEVICE_PATH_PROTOCOL *Start;\r
64\r
65 if (DevicePath == NULL) {\r
66 return 0;\r
67 }\r
68\r
69 //\r
70 // Search for the end of the device path structure\r
71 //\r
72 Start = DevicePath;\r
73 while (!EfiIsDevicePathEnd (DevicePath)) {\r
74 DevicePath = EfiNextDevicePathNode (DevicePath);\r
75 }\r
76\r
77 //\r
78 // Compute the size and add back in the size of the end device path structure\r
79 //\r
80 return ((UINTN) DevicePath - (UINTN) Start) + sizeof (EFI_DEVICE_PATH_PROTOCOL);\r
81}\r
82\r
83/**\r
84 Creates a new device path by appending a second device path to a first device path.\r
85\r
86 This function allocates space for a new copy of the device path specified by DevicePath. If\r
87 DevicePath is NULL, then NULL is returned. If the memory is successfully allocated, then the\r
88 contents of DevicePath are copied to the newly allocated buffer, and a pointer to that buffer\r
89 is returned. Otherwise, NULL is returned. \r
90 \r
91 @param DevicePath A pointer to a device path data structure.\r
92\r
93 @return A pointer to the duplicated device path.\r
94\r
95**/\r
96EFI_DEVICE_PATH_PROTOCOL *\r
97EFIAPI\r
98DuplicateDevicePath (\r
99 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
100 )\r
101{\r
102 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
103 UINTN Size;\r
104\r
105 //\r
106 // Compute the size\r
107 //\r
108 Size = GetDevicePathSize (DevicePath);\r
109 if (Size == 0) {\r
110 return NULL;\r
111 }\r
112\r
113 //\r
114 // Allocate space for duplicate device path\r
115 //\r
116 NewDevicePath = AllocateCopyPool (Size, DevicePath);\r
117\r
118 return NewDevicePath;\r
119}\r
120\r
121/**\r
122 Creates a new device path by appending a second device path to a first device path.\r
123\r
124 This function creates a new device path by appending a copy of SecondDevicePath to a copy of\r
125 FirstDevicePath in a newly allocated buffer. Only the end-of-device-path device node from\r
126 SecondDevicePath is retained. The newly created device path is returned. \r
127 If FirstDevicePath is NULL, then it is ignored, and a duplicate of SecondDevicePath is returned. \r
128 If SecondDevicePath is NULL, then it is ignored, and a duplicate of FirstDevicePath is returned. \r
98a14db6 129 If both FirstDevicePath and SecondDevicePath are NULL, then a copy of an end-of-device-path is\r
130 returned. \r
e386b444 131 If there is not enough memory for the newly allocated buffer, then NULL is returned.\r
132 The memory for the new device path is allocated from EFI boot services memory. It is the\r
133 responsibility of the caller to free the memory allocated.\r
134\r
135 @param FirstDevicePath A pointer to a device path data structure.\r
136 @param SecondDevicePath A pointer to a device path data structure.\r
137\r
138 @return A pointer to the new device path.\r
139\r
140**/\r
141EFI_DEVICE_PATH_PROTOCOL *\r
142EFIAPI\r
143AppendDevicePath (\r
144 IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath, OPTIONAL\r
145 IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath OPTIONAL\r
146 )\r
147{\r
148 UINTN Size;\r
149 UINTN Size1;\r
150 UINTN Size2;\r
151 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
152 EFI_DEVICE_PATH_PROTOCOL *DevicePath2;\r
153\r
154 //\r
155 // If there's only 1 path, just duplicate it.\r
156 //\r
157 if (FirstDevicePath == NULL) {\r
697f4d59 158 return DuplicateDevicePath ((SecondDevicePath != NULL) ? SecondDevicePath : &mEndDevicePath);\r
e386b444 159 }\r
160\r
161 if (SecondDevicePath == NULL) {\r
162 return DuplicateDevicePath (FirstDevicePath);\r
163 }\r
164\r
165 //\r
166 // Allocate space for the combined device path. It only has one end node of\r
167 // length EFI_DEVICE_PATH_PROTOCOL.\r
168 //\r
169 Size1 = GetDevicePathSize (FirstDevicePath);\r
170 Size2 = GetDevicePathSize (SecondDevicePath);\r
171 Size = Size1 + Size2 - sizeof (EFI_DEVICE_PATH_PROTOCOL);\r
172\r
173 NewDevicePath = AllocatePool (Size);\r
174\r
175 if (NewDevicePath != NULL) {\r
176 NewDevicePath = CopyMem (NewDevicePath, FirstDevicePath, Size1);\r
177 //\r
178 // Over write FirstDevicePath EndNode and do the copy\r
179 //\r
180 DevicePath2 = (EFI_DEVICE_PATH_PROTOCOL *) ((CHAR8 *) NewDevicePath +\r
181 (Size1 - sizeof (EFI_DEVICE_PATH_PROTOCOL)));\r
182 CopyMem (DevicePath2, SecondDevicePath, Size2);\r
183 }\r
184\r
185 return NewDevicePath;\r
186}\r
187\r
188/**\r
189 Creates a new path by appending the device node to the device path.\r
190\r
191 This function creates a new device path by appending a copy of the device node specified by\r
192 DevicePathNode to a copy of the device path specified by DevicePath in an allocated buffer.\r
193 The end-of-device-path device node is moved after the end of the appended device node.\r
98a14db6 194 If DevicePathNode is NULL then a copy of DevicePath is returned.\r
6336a895 195 If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device path device\r
196 node is returned.\r
98a14db6 197 If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path device node\r
198 is returned.\r
e386b444 199 If there is not enough memory to allocate space for the new device path, then NULL is returned. \r
200 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to\r
201 free the memory allocated.\r
202\r
203 @param DevicePath A pointer to a device path data structure.\r
204 @param DevicePathNode A pointer to a single device path node.\r
205\r
206 @return A pointer to the new device path.\r
207\r
208**/\r
209EFI_DEVICE_PATH_PROTOCOL *\r
210EFIAPI\r
211AppendDevicePathNode (\r
212 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL\r
213 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathNode OPTIONAL\r
214 )\r
215{\r
216 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
217 EFI_DEVICE_PATH_PROTOCOL *NextNode;\r
218 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
219 UINTN NodeLength;\r
220\r
98a14db6 221 if (DevicePathNode == NULL) {\r
697f4d59 222 return DuplicateDevicePath ((DevicePath != NULL) ? DevicePath : &mEndDevicePath);\r
e386b444 223 }\r
224 //\r
225 // Build a Node that has a terminator on it\r
226 //\r
227 NodeLength = DevicePathNodeLength (DevicePathNode);\r
228\r
229 TempDevicePath = AllocatePool (NodeLength + sizeof (EFI_DEVICE_PATH_PROTOCOL));\r
230 if (TempDevicePath == NULL) {\r
231 return NULL;\r
232 }\r
233 TempDevicePath = CopyMem (TempDevicePath, DevicePathNode, NodeLength);\r
234 //\r
235 // Add and end device path node to convert Node to device path\r
236 //\r
237 NextNode = NextDevicePathNode (TempDevicePath);\r
238 SetDevicePathEndNode (NextNode);\r
239 //\r
240 // Append device paths\r
241 //\r
242 NewDevicePath = AppendDevicePath (DevicePath, TempDevicePath);\r
243\r
244 FreePool (TempDevicePath);\r
245\r
246 return NewDevicePath;\r
247}\r
248\r
249/**\r
250 Creates a new device path by appending the specified device path instance to the specified device\r
251 path.\r
252 \r
253 This function creates a new device path by appending a copy of the device path instance specified\r
254 by DevicePathInstance to a copy of the device path secified by DevicePath in a allocated buffer.\r
255 The end-of-device-path device node is moved after the end of the appended device path instance\r
256 and a new end-of-device-path-instance node is inserted between. \r
257 If DevicePath is NULL, then a copy if DevicePathInstance is returned.\r
258 If DevicePathInstance is NULL, then NULL is returned.\r
259 If there is not enough memory to allocate space for the new device path, then NULL is returned. \r
260 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to\r
261 free the memory allocated.\r
262 \r
263 @param DevicePath A pointer to a device path data structure.\r
264 @param DevicePathInstance A pointer to a device path instance.\r
265\r
266 @return A pointer to the new device path.\r
267\r
268**/\r
269EFI_DEVICE_PATH_PROTOCOL *\r
270EFIAPI\r
271AppendDevicePathInstance (\r
272 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL\r
273 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathInstance OPTIONAL\r
274 )\r
275{\r
276 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
277 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
278 UINTN SrcSize;\r
279 UINTN InstanceSize;\r
280\r
281 if (DevicePath == NULL) {\r
282 return DuplicateDevicePath (DevicePathInstance);\r
283 }\r
284\r
285 if (DevicePathInstance == NULL) {\r
286 return NULL;\r
287 }\r
288\r
289 SrcSize = GetDevicePathSize (DevicePath);\r
290 InstanceSize = GetDevicePathSize (DevicePathInstance);\r
291\r
292 NewDevicePath = AllocatePool (SrcSize + InstanceSize);\r
293 if (NewDevicePath != NULL) {\r
294 \r
295 TempDevicePath = CopyMem (NewDevicePath, DevicePath, SrcSize);;\r
296 \r
297 while (!IsDevicePathEnd (TempDevicePath)) {\r
298 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
299 }\r
300 \r
301 TempDevicePath->SubType = END_INSTANCE_DEVICE_PATH_SUBTYPE;\r
302 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
303 CopyMem (TempDevicePath, DevicePathInstance, InstanceSize);\r
304 }\r
305\r
306 return NewDevicePath;\r
307}\r
308\r
309/**\r
310 Creates a copy of the current device path instance and returns a pointer to the next device path\r
311 instance.\r
312\r
313 This function creates a copy of the current device path instance. It also updates DevicePath to\r
314 point to the next device path instance in the device path (or NULL if no more) and updates Size\r
315 to hold the size of the device path instance copy.\r
316 If DevicePath is NULL, then NULL is returned.\r
317 If there is not enough memory to allocate space for the new device path, then NULL is returned. \r
318 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to\r
319 free the memory allocated.\r
320 If Size is NULL, then ASSERT().\r
321 \r
322 @param DevicePath On input, this holds the pointer to the current device path\r
323 instance. On output, this holds the pointer to the next device\r
324 path instance or NULL if there are no more device path\r
325 instances in the device path pointer to a device path data\r
326 structure.\r
327 @param Size On output, this holds the size of the device path instance, in\r
328 bytes or zero, if DevicePath is NULL.\r
329\r
330 @return A pointer to the current device path instance.\r
331\r
332**/\r
333EFI_DEVICE_PATH_PROTOCOL *\r
334EFIAPI\r
335GetNextDevicePathInstance (\r
336 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath,\r
337 OUT UINTN *Size\r
338 )\r
339{\r
340 EFI_DEVICE_PATH_PROTOCOL *DevPath;\r
341 EFI_DEVICE_PATH_PROTOCOL *ReturnValue;\r
342 UINT8 Temp;\r
343\r
344 ASSERT (Size != NULL);\r
345\r
346 if (DevicePath == NULL || *DevicePath == NULL) {\r
347 *Size = 0;\r
348 return NULL;\r
349 }\r
350\r
351 //\r
352 // Find the end of the device path instance\r
353 //\r
354 DevPath = *DevicePath;\r
355 while (!IsDevicePathEndType (DevPath)) {\r
356 DevPath = NextDevicePathNode (DevPath);\r
357 }\r
358\r
359 //\r
360 // Compute the size of the device path instance\r
361 //\r
362 *Size = ((UINTN) DevPath - (UINTN) (*DevicePath)) + sizeof (EFI_DEVICE_PATH_PROTOCOL);\r
363 \r
364 //\r
365 // Make a copy and return the device path instance\r
366 //\r
367 Temp = DevPath->SubType;\r
368 DevPath->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;\r
369 ReturnValue = DuplicateDevicePath (*DevicePath);\r
370 DevPath->SubType = Temp;\r
371\r
372 //\r
373 // If DevPath is the end of an entire device path, then another instance\r
374 // does not follow, so *DevicePath is set to NULL.\r
375 //\r
376 if (DevicePathSubType (DevPath) == END_ENTIRE_DEVICE_PATH_SUBTYPE) {\r
377 *DevicePath = NULL;\r
378 } else {\r
379 *DevicePath = NextDevicePathNode (DevPath);\r
380 }\r
381\r
382 return ReturnValue;\r
383}\r
384\r
385/**\r
386 Creates a copy of the current device path instance and returns a pointer to the next device path\r
387 instance.\r
388\r
389 This function creates a new device node in a newly allocated buffer of size NodeLength and\r
390 initializes the device path node header with NodeType and NodeSubType. The new device path node\r
391 is returned.\r
392 If NodeLength is smaller than a device path header, then NULL is returned. \r
393 If there is not enough memory to allocate space for the new device path, then NULL is returned. \r
394 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to\r
395 free the memory allocated.\r
396\r
397 @param NodeType The device node type for the new device node.\r
398 @param NodeSubType The device node sub-type for the new device node.\r
399 @param NodeLength The length of the new device node.\r
400\r
401 @return The new device path.\r
402\r
403**/\r
404EFI_DEVICE_PATH_PROTOCOL *\r
405EFIAPI\r
406CreateDeviceNode (\r
407 IN UINT8 NodeType,\r
408 IN UINT8 NodeSubType,\r
409 IN UINT16 NodeLength\r
410 )\r
411{\r
412 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
413\r
414 if (NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {\r
415 //\r
416 // NodeLength is less than the size of the header.\r
417 //\r
418 return NULL;\r
419 }\r
420 \r
6577541d 421 DevicePath = AllocateZeroPool (NodeLength);\r
e386b444 422 if (DevicePath != NULL) {\r
423 DevicePath->Type = NodeType;\r
424 DevicePath->SubType = NodeSubType;\r
425 SetDevicePathNodeLength (DevicePath, NodeLength);\r
426 }\r
427\r
428 return DevicePath;\r
429}\r
430\r
431/**\r
432 Determines if a device path is single or multi-instance.\r
433\r
434 This function returns TRUE if the device path specified by DevicePath is multi-instance.\r
435 Otherwise, FALSE is returned. If DevicePath is NULL, then FALSE is returned.\r
436\r
437 @param DevicePath A pointer to a device path data structure.\r
438\r
439 @retval TRUE DevicePath is multi-instance.\r
440 @retval FALSE DevicePath is not multi-instance or DevicePath is NULL.\r
441\r
442**/\r
443BOOLEAN\r
444EFIAPI\r
445IsDevicePathMultiInstance (\r
446 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
447 )\r
448{\r
449 CONST EFI_DEVICE_PATH_PROTOCOL *Node;\r
450\r
451 if (DevicePath == NULL) {\r
452 return FALSE;\r
453 }\r
454\r
455 Node = DevicePath;\r
456 while (!EfiIsDevicePathEnd (Node)) {\r
457 if (EfiIsDevicePathEndInstance (Node)) {\r
458 return TRUE;\r
459 }\r
460\r
461 Node = EfiNextDevicePathNode (Node);\r
462 }\r
463\r
464 return FALSE;\r
465}\r
466\r
467\r
468/**\r
469 Retrieves the device path protocol from a handle.\r
470\r
471 This function returns the device path protocol from the handle specified by Handle. If Handle is\r
472 NULL or Handle does not contain a device path protocol, then NULL is returned.\r
473 \r
474 @param Handle The handle from which to retrieve the device path protocol.\r
475\r
476 @return The device path protocol from the handle specified by Handle.\r
477\r
478**/\r
479EFI_DEVICE_PATH_PROTOCOL *\r
480EFIAPI\r
481DevicePathFromHandle (\r
482 IN EFI_HANDLE Handle\r
483 )\r
484{\r
485 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
486 EFI_STATUS Status;\r
487\r
488 Status = gBS->HandleProtocol (\r
489 Handle,\r
490 &gEfiDevicePathProtocolGuid,\r
491 (VOID *) &DevicePath\r
492 );\r
493 if (EFI_ERROR (Status)) {\r
494 DevicePath = NULL;\r
495 }\r
496 return DevicePath;\r
497}\r
498\r
499/**\r
500 Allocates a device path for a file and appends it to an existing device path.\r
501\r
502 If Device is a valid device handle that contains a device path protocol, then a device path for\r
503 the file specified by FileName is allocated and appended to the device path associated with the\r
504 handle Device. The allocated device path is returned. If Device is NULL or Device is a handle\r
505 that does not support the device path protocol, then a device path containing a single device\r
506 path node for the file specified by FileName is allocated and returned.\r
507 If FileName is NULL, then ASSERT().\r
508\r
509 @param Device A pointer to a device handle. This parameter is optional and\r
510 may be NULL.\r
511 @param FileName A pointer to a Null-terminated Unicode string.\r
512\r
513 @return The allocated device path.\r
514\r
515**/\r
516EFI_DEVICE_PATH_PROTOCOL *\r
517EFIAPI\r
518FileDevicePath (\r
519 IN EFI_HANDLE Device, OPTIONAL\r
520 IN CONST CHAR16 *FileName\r
521 )\r
522{\r
523 UINTN Size;\r
524 FILEPATH_DEVICE_PATH *FilePath;\r
525 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
526 EFI_DEVICE_PATH_PROTOCOL *FileDevicePath;\r
527\r
528 DevicePath = NULL;\r
529\r
530 Size = StrSize (FileName);\r
531 FileDevicePath = AllocatePool (Size + SIZE_OF_FILEPATH_DEVICE_PATH + EFI_END_DEVICE_PATH_LENGTH);\r
532 if (FileDevicePath != NULL) {\r
533 FilePath = (FILEPATH_DEVICE_PATH *) FileDevicePath;\r
534 FilePath->Header.Type = MEDIA_DEVICE_PATH;\r
535 FilePath->Header.SubType = MEDIA_FILEPATH_DP;\r
536 CopyMem (&FilePath->PathName, FileName, Size);\r
537 SetDevicePathNodeLength (&FilePath->Header, Size + SIZE_OF_FILEPATH_DEVICE_PATH);\r
538 SetDevicePathEndNode (NextDevicePathNode (&FilePath->Header));\r
539\r
540 if (Device != NULL) {\r
541 DevicePath = DevicePathFromHandle (Device);\r
542 }\r
543\r
544 DevicePath = AppendDevicePath (DevicePath, FileDevicePath);\r
545 FreePool (FileDevicePath);\r
546 }\r
547\r
548 return DevicePath;\r
549}\r
550\r