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