]> git.proxmox.com Git - mirror_edk2.git/blame - EdkCompatibilityPkg/Foundation/Library/Dxe/EfiDriverLib/DevicePath.c
Sync all bug fixes between EDK1.04 and EDK1.06 into EdkCompatibilityPkg.
[mirror_edk2.git] / EdkCompatibilityPkg / Foundation / Library / Dxe / EfiDriverLib / DevicePath.c
CommitLineData
3eb9473e 1/*++\r
2\r
3e99020d 3Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>\r
4ea9375a 4This program and the accompanying materials \r
3eb9473e 5are licensed and made available under the terms and conditions of the BSD License \r
6which accompanies this distribution. The full text of the license may be found at \r
7http://opensource.org/licenses/bsd-license.php \r
8 \r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
11\r
12Module Name:\r
13\r
14 DevicePath.c\r
15\r
16Abstract:\r
17\r
18 Device Path services. The thing to remember is device paths are built out of\r
19 nodes. The device path is terminated by an end node that is length\r
20 sizeof(EFI_DEVICE_PATH_PROTOCOL). That would be why there is sizeof(EFI_DEVICE_PATH_PROTOCOL)\r
21 all over this file.\r
22\r
23 The only place where multi-instance device paths are supported is in\r
24 environment varibles. Multi-instance device paths should never be placed\r
25 on a Handle.\r
26\r
27--*/\r
28\r
29#include "Tiano.h"\r
30#include "EfiDriverLib.h"\r
31#include EFI_PROTOCOL_DEFINITION (DevicePath)\r
32\r
33EFI_DEVICE_PATH_PROTOCOL *\r
34EfiDevicePathInstance (\r
35 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath,\r
36 OUT UINTN *Size\r
37 )\r
38/*++\r
39\r
40Routine Description:\r
41 Function retrieves the next device path instance from a device path data structure.\r
42\r
43Arguments:\r
44 DevicePath - A pointer to a device path data structure.\r
45\r
46 Size - A pointer to the size of a device path instance in bytes.\r
47\r
48Returns:\r
49\r
50 This function returns a pointer to the current device path instance.\r
51 In addition, it returns the size in bytes of the current device path instance in Size,\r
52 and a pointer to the next device path instance in DevicePath.\r
53 If there are no more device path instances in DevicePath, then DevicePath will be set to NULL.\r
54\r
55--*/\r
56{\r
57 EFI_DEVICE_PATH_PROTOCOL *DevPath;\r
58 EFI_DEVICE_PATH_PROTOCOL *ReturnValue;\r
59 UINT8 Temp;\r
60\r
61 if (*DevicePath == NULL) {\r
62 if (Size != NULL) {\r
63 *Size = 0;\r
64 }\r
65\r
66 return NULL;\r
67 }\r
68\r
69 //\r
70 // Find the end of the device path instance\r
71 //\r
72 DevPath = *DevicePath;\r
73 while (!IsDevicePathEndType (DevPath)) {\r
74 DevPath = NextDevicePathNode (DevPath);\r
75 }\r
76\r
77 //\r
78 // Compute the size of the device path instance\r
79 //\r
80 if (Size != NULL) {\r
81 *Size = ((UINTN) DevPath - (UINTN) (*DevicePath)) + sizeof (EFI_DEVICE_PATH_PROTOCOL);\r
82 }\r
83\r
84 //\r
85 // Make a copy and return the device path instance\r
86 //\r
87 Temp = DevPath->SubType;\r
88 DevPath->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;\r
89 ReturnValue = EfiDuplicateDevicePath (*DevicePath);\r
90 DevPath->SubType = Temp;\r
91\r
92 //\r
93 // If DevPath is the end of an entire device path, then another instance\r
94 // does not follow, so *DevicePath is set to NULL.\r
95 //\r
96 if (DevicePathSubType (DevPath) == END_ENTIRE_DEVICE_PATH_SUBTYPE) {\r
97 *DevicePath = NULL;\r
98 } else {\r
99 *DevicePath = NextDevicePathNode (DevPath);\r
100 }\r
101\r
102 return ReturnValue;\r
103}\r
104\r
105BOOLEAN\r
106EfiIsDevicePathMultiInstance (\r
107 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
108 )\r
109/*++\r
110\r
111Routine Description:\r
112 Return TRUE is this is a multi instance device path.\r
113\r
114Arguments:\r
115 DevicePath - A pointer to a device path data structure.\r
116\r
117\r
118Returns:\r
119 TRUE - If DevicePath is multi instance. FALSE - If DevicePath is not multi\r
120 instance.\r
121\r
122--*/\r
123{\r
124 EFI_DEVICE_PATH_PROTOCOL *Node;\r
125\r
126 if (DevicePath == NULL) {\r
127 return FALSE;\r
128 }\r
129\r
130 Node = DevicePath;\r
131 while (!EfiIsDevicePathEnd (Node)) {\r
132 if (EfiIsDevicePathEndInstance (Node)) {\r
133 return TRUE;\r
134 }\r
135\r
136 Node = EfiNextDevicePathNode (Node);\r
137 }\r
138\r
139 return FALSE;\r
140}\r
141\r
142UINTN\r
143EfiDevicePathSize (\r
144 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
145 )\r
146/*++\r
147\r
148Routine Description:\r
149\r
150 Calculate the space size of a device path.\r
151\r
152Arguments:\r
153\r
154 DevicePath - A specified device path\r
155\r
156Returns:\r
157\r
158 The size.\r
159\r
160--*/\r
161{\r
162 EFI_DEVICE_PATH_PROTOCOL *Start;\r
163\r
164 if (DevicePath == NULL) {\r
165 return 0;\r
166 }\r
167\r
168 //\r
169 // Search for the end of the device path structure\r
170 //\r
171 Start = DevicePath;\r
172 while (!EfiIsDevicePathEnd (DevicePath)) {\r
173 DevicePath = EfiNextDevicePathNode (DevicePath);\r
174 }\r
175\r
176 //\r
177 // Compute the size and add back in the size of the end device path structure\r
178 //\r
179 return ((UINTN) DevicePath - (UINTN) Start) + sizeof (EFI_DEVICE_PATH_PROTOCOL);\r
180}\r
181\r
182EFI_DEVICE_PATH_PROTOCOL *\r
183EfiDevicePathFromHandle (\r
184 IN EFI_HANDLE Handle\r
185 )\r
186/*++\r
187\r
188Routine Description:\r
189\r
190 Get the device path protocol interface installed on a specified handle.\r
191\r
192Arguments:\r
193\r
194 Handle - a specified handle\r
195\r
196Returns:\r
197\r
198 The device path protocol interface installed on that handle.\r
199\r
200--*/\r
201{\r
202 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
203\r
204 DevicePath = NULL;\r
205 gBS->HandleProtocol (\r
206 Handle,\r
207 &gEfiDevicePathProtocolGuid,\r
208 (VOID *) &DevicePath\r
209 );\r
210 return DevicePath;\r
211}\r
212\r
213EFI_DEVICE_PATH_PROTOCOL *\r
214EfiDuplicateDevicePath (\r
215 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
216 )\r
217/*++\r
218\r
219Routine Description:\r
220\r
221 Duplicate a device path structure.\r
222\r
223Arguments:\r
224\r
225 DevicePath - The device path to duplicated.\r
226\r
227Returns:\r
228\r
229 The duplicated device path.\r
230\r
231--*/\r
232{\r
233 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
234 UINTN Size;\r
235\r
236 if (DevicePath == NULL) {\r
237 return NULL;\r
238 }\r
239\r
240 //\r
241 // Compute the size\r
242 //\r
243 Size = EfiDevicePathSize (DevicePath);\r
244 if (Size == 0) {\r
245 return NULL;\r
246 }\r
247\r
248 //\r
249 // Allocate space for duplicate device path\r
250 //\r
251 NewDevicePath = EfiLibAllocateCopyPool (Size, DevicePath);\r
252\r
253 return NewDevicePath;\r
254}\r
255\r
256EFI_DEVICE_PATH_PROTOCOL *\r
257EfiAppendDevicePath (\r
258 IN EFI_DEVICE_PATH_PROTOCOL *Src1,\r
259 IN EFI_DEVICE_PATH_PROTOCOL *Src2\r
260 )\r
261/*++\r
262\r
263Routine Description:\r
264 Function is used to append a Src1 and Src2 together.\r
265\r
266Arguments:\r
267 Src1 - A pointer to a device path data structure.\r
268\r
269 Src2 - A pointer to a device path data structure.\r
270\r
271Returns:\r
272\r
273 A pointer to the new device path is returned.\r
274 NULL is returned if space for the new device path could not be allocated from pool.\r
275 It is up to the caller to free the memory used by Src1 and Src2 if they are no longer needed.\r
276\r
277--*/\r
278{\r
279 UINTN Size;\r
280 UINTN Size1;\r
281 UINTN Size2;\r
282 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
283 EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath;\r
284\r
285 //\r
286 // If there's only 1 path, just duplicate it\r
287 //\r
288 if (!Src1) {\r
289 ASSERT (!IsDevicePathUnpacked (Src2));\r
290 return EfiDuplicateDevicePath (Src2);\r
291 }\r
292\r
293 if (!Src2) {\r
294 ASSERT (!IsDevicePathUnpacked (Src1));\r
295 return EfiDuplicateDevicePath (Src1);\r
296 }\r
297\r
298 //\r
299 // Allocate space for the combined device path. It only has one end node of\r
300 // length EFI_DEVICE_PATH_PROTOCOL\r
301 //\r
302 Size1 = EfiDevicePathSize (Src1);\r
303 Size2 = EfiDevicePathSize (Src2);\r
304 Size = Size1 + Size2 - sizeof (EFI_DEVICE_PATH_PROTOCOL);\r
305\r
306 NewDevicePath = EfiLibAllocateCopyPool (Size, Src1);\r
307\r
308 if (NewDevicePath != NULL) {\r
309\r
310 //\r
311 // Over write Src1 EndNode and do the copy\r
312 //\r
313 SecondDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) ((CHAR8 *) NewDevicePath + (Size1 - sizeof (EFI_DEVICE_PATH_PROTOCOL)));\r
314 EfiCopyMem (SecondDevicePath, Src2, Size2);\r
315 }\r
316\r
317 return NewDevicePath;\r
318}\r
319\r
320EFI_DEVICE_PATH_PROTOCOL *\r
321EfiAppendDevicePathNode (\r
322 IN EFI_DEVICE_PATH_PROTOCOL *Src1,\r
323 IN EFI_DEVICE_PATH_PROTOCOL *Node\r
324 )\r
325/*++\r
326\r
327Routine Description:\r
328 Function is used to append a device path node to the end of another device path.\r
329\r
330Arguments:\r
331 Src1 - A pointer to a device path data structure.\r
332\r
333 Node - A pointer to a device path data structure.\r
334\r
335Returns:\r
336 This function returns a pointer to the new device path.\r
337 If there is not enough temporary pool memory available to complete this function,\r
338 then NULL is returned.\r
339\r
340\r
341--*/\r
342{\r
343 EFI_DEVICE_PATH_PROTOCOL *Temp;\r
344 EFI_DEVICE_PATH_PROTOCOL *NextNode;\r
345 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
346 UINTN NodeLength;\r
347\r
348 //\r
349 // Build a Node that has a terminator on it\r
350 //\r
351 NodeLength = DevicePathNodeLength (Node);\r
352\r
353 Temp = EfiLibAllocateCopyPool (NodeLength + sizeof (EFI_DEVICE_PATH_PROTOCOL), Node);\r
354 if (Temp == NULL) {\r
355 return NULL;\r
356 }\r
357\r
358 //\r
359 // Add and end device path node to convert Node to device path\r
360 //\r
361 NextNode = NextDevicePathNode (Temp);\r
362 SetDevicePathEndNode (NextNode);\r
363\r
364 //\r
365 // Append device paths\r
366 //\r
367 NewDevicePath = EfiAppendDevicePath (Src1, Temp);\r
368 gBS->FreePool (Temp);\r
369 return NewDevicePath;\r
370}\r
371\r
372EFI_DEVICE_PATH_PROTOCOL *\r
373EfiFileDevicePath (\r
374 IN EFI_HANDLE Device OPTIONAL,\r
375 IN CHAR16 *FileName\r
376 )\r
377/*++\r
378\r
379Routine Description:\r
380\r
381 This function allocates a device path for a file and appends it to an existiong\r
382 device path.\r
383\r
384Arguments:\r
385 Device - A pointer to a device handle.\r
386\r
387 FileName - A pointer to a Null-terminated Unicodestring.\r
388\r
389Returns:\r
390 A device path contain the file name.\r
391\r
392--*/\r
393{\r
394 UINTN Size;\r
395 FILEPATH_DEVICE_PATH *FilePath;\r
396 EFI_DEVICE_PATH_PROTOCOL *Eop;\r
397 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
398\r
399 for (Size = 0; FileName[Size] != 0; Size++)\r
400 ;\r
401 Size = (Size + 1) * 2;\r
402\r
403 FilePath = EfiLibAllocateZeroPool (Size + SIZE_OF_FILEPATH_DEVICE_PATH + sizeof (EFI_DEVICE_PATH_PROTOCOL));\r
404\r
405 DevicePath = NULL;\r
406\r
407 if (FilePath != NULL) {\r
408\r
409 //\r
410 // Build a file path\r
411 //\r
412 FilePath->Header.Type = MEDIA_DEVICE_PATH;\r
413 FilePath->Header.SubType = MEDIA_FILEPATH_DP;\r
414 SetDevicePathNodeLength (&FilePath->Header, Size + SIZE_OF_FILEPATH_DEVICE_PATH);\r
415 EfiCopyMem (FilePath->PathName, FileName, Size);\r
416 Eop = NextDevicePathNode (&FilePath->Header);\r
417 SetDevicePathEndNode (Eop);\r
418\r
419 //\r
420 // Append file path to device's device path\r
421 //\r
422\r
423 DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) FilePath;\r
424 if (Device != NULL) {\r
425 DevicePath = EfiAppendDevicePath (\r
426 EfiDevicePathFromHandle (Device),\r
427 DevicePath\r
428 );\r
429\r
430 gBS->FreePool (FilePath);\r
431 }\r
432 }\r
433\r
434 return DevicePath;\r
435}\r
436\r
437EFI_DEVICE_PATH_PROTOCOL *\r
438EfiAppendDevicePathInstance (\r
439 IN EFI_DEVICE_PATH_PROTOCOL *Src,\r
440 IN EFI_DEVICE_PATH_PROTOCOL *Instance\r
441 )\r
442/*++\r
443\r
444Routine Description:\r
445\r
446 Append a device path instance to another.\r
447\r
448Arguments:\r
449\r
450 Src - The device path instance to be appended with.\r
451 Instance - The device path instance appending the other.\r
452\r
453Returns:\r
454\r
455 The contaction of these two.\r
456\r
457--*/\r
458{\r
459 UINT8 *Ptr;\r
460 EFI_DEVICE_PATH_PROTOCOL *DevPath;\r
461 UINTN SrcSize;\r
462 UINTN InstanceSize;\r
463\r
464 if (Src == NULL) {\r
465 return EfiDuplicateDevicePath (Instance);\r
466 }\r
467\r
468 SrcSize = EfiDevicePathSize (Src);\r
469 InstanceSize = EfiDevicePathSize (Instance);\r
470\r
471 Ptr = EfiLibAllocateCopyPool (SrcSize + InstanceSize, Src);\r
472 if (Ptr != NULL) {\r
473\r
474 DevPath = (EFI_DEVICE_PATH_PROTOCOL *) Ptr;\r
475\r
476 while (!IsDevicePathEnd (DevPath)) {\r
477 DevPath = NextDevicePathNode (DevPath);\r
478 }\r
479 //\r
480 // Convert the End to an End Instance, since we are\r
481 // appending another instacne after this one its a good\r
482 // idea.\r
483 //\r
484 DevPath->SubType = END_INSTANCE_DEVICE_PATH_SUBTYPE;\r
485\r
486 DevPath = NextDevicePathNode (DevPath);\r
487 EfiCopyMem (DevPath, Instance, InstanceSize);\r
488 }\r
489\r
490 return (EFI_DEVICE_PATH_PROTOCOL *) Ptr;\r
491}\r
492\r
493VOID\r
494EFIAPI\r
495EfiInitializeFwVolDevicepathNode (\r
496 IN MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *FvDevicePathNode,\r
497 IN EFI_GUID *NameGuid\r
498 )\r
499/*++\r
500\r
501Routine Description:\r
502\r
503 Initialize a Firmware Volume (FV) Media Device Path node.\r
504 \r
3eb9473e 505Arguments:\r
506\r
507 FvDevicePathNode - Pointer to a FV device path node to initialize\r
508 NameGuid - FV file name to use in FvDevicePathNode\r
509\r
510Returns:\r
511\r
512 None\r
513\r
514--*/\r
515{\r
3eb9473e 516 FvDevicePathNode->Header.Type = MEDIA_DEVICE_PATH;\r
517 FvDevicePathNode->Header.SubType = MEDIA_FV_FILEPATH_DP;\r
518 SetDevicePathNodeLength (&FvDevicePathNode->Header, sizeof (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH));\r
519\r
3eb9473e 520 EfiCopyMem (&FvDevicePathNode->NameGuid, NameGuid, sizeof(EFI_GUID));\r
521}\r
522\r
523EFI_GUID *\r
524EFIAPI\r
525EfiGetNameGuidFromFwVolDevicePathNode (\r
526 IN MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *FvDevicePathNode\r
527 )\r
528/*++\r
529\r
530Routine Description:\r
531\r
532 Check to see if the Firmware Volume (FV) Media Device Path is valid.\r
533 \r
3eb9473e 534Arguments:\r
535\r
536 FvDevicePathNode - Pointer to FV device path to check\r
537\r
538Returns:\r
539\r
540 NULL - FvDevicePathNode is not valid.\r
541 Other - FvDevicePathNode is valid and pointer to NameGuid was returned.\r
542\r
543--*/\r
544{\r
3eb9473e 545 if (DevicePathType (&FvDevicePathNode->Header) == MEDIA_DEVICE_PATH &&\r
546 DevicePathSubType (&FvDevicePathNode->Header) == MEDIA_FV_FILEPATH_DP) {\r
547 return &FvDevicePathNode->NameGuid;\r
548 }\r
549\r
3eb9473e 550 return NULL;\r
551}\r