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