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