]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.c
Initial import.
[mirror_edk2.git] / MdePkg / Library / UefiDevicePathLib / UefiDevicePathLib.c
CommitLineData
878ddf1f 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
25 This function returns the size, in bytes, \r
26 of the device path data structure specified by DevicePath.\r
27 If DevicePath is NULL, then 0 is returned.\r
28\r
29 @param DevicePath A pointer to a device path data structure.\r
30\r
31 @return The size of a device path in bytes.\r
32\r
33**/\r
34UINTN\r
35EFIAPI\r
36GetDevicePathSize (\r
37 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
38 )\r
39{\r
40 CONST EFI_DEVICE_PATH_PROTOCOL *Start;\r
41\r
42 if (DevicePath == NULL) {\r
43 return 0;\r
44 }\r
45\r
46 //\r
47 // Search for the end of the device path structure\r
48 //\r
49 Start = DevicePath;\r
50 while (!EfiIsDevicePathEnd (DevicePath)) {\r
51 DevicePath = EfiNextDevicePathNode (DevicePath);\r
52 }\r
53\r
54 //\r
55 // Compute the size and add back in the size of the end device path structure\r
56 //\r
57 return ((UINTN) DevicePath - (UINTN) Start) + sizeof (EFI_DEVICE_PATH_PROTOCOL);\r
58}\r
59\r
60/**\r
61 This function allocates space for a new copy of the device path\r
62 specified by DevicePath.\r
63\r
64 @param DevicePath A pointer to a device path data structure.\r
65\r
66 @return The duplicated device path.\r
67\r
68**/\r
69EFI_DEVICE_PATH_PROTOCOL *\r
70EFIAPI\r
71DuplicateDevicePath (\r
72 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
73 )\r
74{\r
75 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
76 UINTN Size;\r
77\r
78 //\r
79 // Compute the size\r
80 //\r
81 Size = GetDevicePathSize (DevicePath);\r
82 if (Size == 0) {\r
83 return NULL;\r
84 }\r
85\r
86 //\r
87 // Allocate space for duplicate device path\r
88 //\r
89 NewDevicePath = AllocateCopyPool (Size, DevicePath);\r
90\r
91 return NewDevicePath;\r
92}\r
93\r
94/**\r
95 This function appends the device path SecondDevicePath\r
96 to every device path instance in FirstDevicePath. \r
97\r
98 @param FirstDevicePath A pointer to a device path data structure.\r
99 \r
100 @param SecondDevicePath A pointer to a device path data structure.\r
101\r
102 @return A pointer to the new device path is returned.\r
103 NULL is returned if space for the new device path could not be allocated from pool.\r
104 It is up to the caller to free the memory used by FirstDevicePath and SecondDevicePath\r
105 if they are no longer needed.\r
106\r
107**/\r
108EFI_DEVICE_PATH_PROTOCOL *\r
109EFIAPI\r
110AppendDevicePath (\r
111 IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath,\r
112 IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath\r
113 )\r
114{\r
115 UINTN Size;\r
116 UINTN Size1;\r
117 UINTN Size2;\r
118 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
119 EFI_DEVICE_PATH_PROTOCOL *DevicePath2;\r
120\r
121 //\r
122 // If there's only 1 path, just duplicate it\r
123 //\r
124 if (FirstDevicePath == NULL) {\r
125 return DuplicateDevicePath (SecondDevicePath);\r
126 }\r
127\r
128 if (SecondDevicePath == NULL) {\r
129 return DuplicateDevicePath (FirstDevicePath);\r
130 }\r
131\r
132 //\r
133 // Allocate space for the combined device path. It only has one end node of\r
134 // length EFI_DEVICE_PATH_PROTOCOL\r
135 //\r
136 Size1 = GetDevicePathSize (FirstDevicePath);\r
137 Size2 = GetDevicePathSize (SecondDevicePath);\r
138 Size = Size1 + Size2 - sizeof (EFI_DEVICE_PATH_PROTOCOL);\r
139\r
140 NewDevicePath = AllocatePool (Size);\r
141\r
142 if (NewDevicePath != NULL) {\r
143 NewDevicePath = CopyMem (NewDevicePath, FirstDevicePath, Size1);\r
144 //\r
145 // Over write Src1 EndNode and do the copy\r
146 //\r
147 DevicePath2 = (EFI_DEVICE_PATH_PROTOCOL *) ((CHAR8 *) NewDevicePath + (Size1 - sizeof (EFI_DEVICE_PATH_PROTOCOL)));\r
148 CopyMem (DevicePath2, SecondDevicePath, Size2);\r
149 }\r
150\r
151 return NewDevicePath;\r
152}\r
153\r
154/**\r
155 This function appends the device path node SecondDevicePath\r
156 to every device path instance in FirstDevicePath.\r
157\r
158 @param FirstDevicePath A pointer to a device path data structure.\r
159 \r
160 @param SecondDevicePath A pointer to a single device path node.\r
161\r
162 @return A pointer to the new device path.\r
163 If there is not enough temporary pool memory available to complete this function,\r
164 then NULL is returned.\r
165\r
166**/\r
167EFI_DEVICE_PATH_PROTOCOL *\r
168EFIAPI\r
169AppendDevicePathNode (\r
170 IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath,\r
171 IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath\r
172 )\r
173{\r
174 EFI_DEVICE_PATH_PROTOCOL *NextNode;\r
175 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
176 UINTN NodeLength;\r
177 UINTN Size1;\r
178\r
179 //\r
180 // Build a Node that has a terminator on it\r
181 //\r
182 NodeLength = DevicePathNodeLength (SecondDevicePath);\r
183 Size1 = GetDevicePathSize (FirstDevicePath);\r
184 \r
185 NewDevicePath = AllocatePool (NodeLength + Size1);\r
186 if (NewDevicePath != NULL) {\r
187 //\r
188 // Copy the first device path to the new device path\r
189 //\r
190 NewDevicePath = CopyMem (NewDevicePath, FirstDevicePath, Size1);\r
191\r
192 //\r
193 // Copy the device path node to the new device path\r
194 //\r
195 NextNode = (EFI_DEVICE_PATH_PROTOCOL *) ((CHAR8 *) NewDevicePath + (Size1 - sizeof (EFI_DEVICE_PATH_PROTOCOL)));\r
196 NextNode = CopyMem (NextNode, SecondDevicePath, NodeLength);\r
197\r
198 //\r
199 // Terminate the whole device path\r
200 //\r
201 NextNode = NextDevicePathNode (NextNode);\r
202 SetDevicePathEndNode (NextNode);\r
203 }\r
204 return NewDevicePath;\r
205}\r
206\r
207/**\r
208 This function appends the device path instance Instance to the device path Source.\r
209 If Source is NULL, then a new device path with one instance is created. \r
210\r
211 @param Source A pointer to a device path data structure.\r
212 @param Instance A pointer to a device path instance.\r
213\r
214 @return A pointer to the new device path.\r
215 If there is not enough temporary pool memory available to complete this function,\r
216 then NULL is returned.\r
217\r
218**/\r
219EFI_DEVICE_PATH_PROTOCOL *\r
220EFIAPI\r
221AppendDevicePathInstance (\r
222 IN CONST EFI_DEVICE_PATH_PROTOCOL *Source,\r
223 IN CONST EFI_DEVICE_PATH_PROTOCOL *Instance\r
224 )\r
225{\r
226 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
227 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
228 UINTN SrcSize;\r
229 UINTN InstanceSize;\r
230\r
231 if (Source == NULL) {\r
232 return DuplicateDevicePath (Instance);\r
233 }\r
234\r
235 SrcSize = GetDevicePathSize (Source);\r
236 InstanceSize = GetDevicePathSize (Instance);\r
237\r
238 NewDevicePath = AllocatePool (SrcSize + InstanceSize);\r
239 if (NewDevicePath != NULL) {\r
240 \r
241 DevicePath = CopyMem (NewDevicePath, Source, SrcSize);;\r
242 \r
243 while (!IsDevicePathEnd (DevicePath)) {\r
244 DevicePath = NextDevicePathNode (DevicePath);\r
245 }\r
246 \r
247 DevicePath->SubType = END_INSTANCE_DEVICE_PATH_SUBTYPE;\r
248\r
249 DevicePath = NextDevicePathNode (DevicePath);\r
250 CopyMem (DevicePath, Instance, InstanceSize);\r
251 }\r
252\r
253 return NewDevicePath;\r
254}\r
255\r
256/**\r
257 Function retrieves the next device path instance from a device path data structure.\r
258\r
259 @param DevicePath A pointer to a device path data structure.\r
260 \r
261 @param Size A pointer to the size of a device path instance in bytes.\r
262\r
263 @return This function returns a pointer to the current device path instance.\r
264 In addition, it returns the size in bytes of the current device path instance in Size,\r
265 and a pointer to the next device path instance in DevicePath.\r
266 If there are no more device path instances in DevicePath, then DevicePath will be set to NULL.\r
267\r
268**/\r
269EFI_DEVICE_PATH_PROTOCOL *\r
270EFIAPI\r
271GetNextDevicePathInstance (\r
272 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath,\r
273 OUT UINTN *Size\r
274 )\r
275{\r
276 EFI_DEVICE_PATH_PROTOCOL *DevPath;\r
277 EFI_DEVICE_PATH_PROTOCOL *ReturnValue;\r
278 UINT8 Temp;\r
279\r
280 ASSERT (DevicePath != NULL);\r
281 ASSERT (Size != NULL);\r
282 if (*DevicePath == NULL) {\r
283 *Size = 0;\r
284 return NULL;\r
285 }\r
286\r
287 //\r
288 // Find the end of the device path instance\r
289 //\r
290 DevPath = *DevicePath;\r
291 while (!IsDevicePathEndType (DevPath)) {\r
292 DevPath = NextDevicePathNode (DevPath);\r
293 }\r
294\r
295 //\r
296 // Compute the size of the device path instance\r
297 //\r
298 *Size = ((UINTN) DevPath - (UINTN) (*DevicePath)) + sizeof (EFI_DEVICE_PATH_PROTOCOL);\r
299 \r
300 //\r
301 // Make a copy and return the device path instance\r
302 //\r
303 Temp = DevPath->SubType;\r
304 DevPath->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;\r
305 ReturnValue = DuplicateDevicePath (*DevicePath);\r
306 DevPath->SubType = Temp;\r
307\r
308 //\r
309 // If DevPath is the end of an entire device path, then another instance\r
310 // does not follow, so *DevicePath is set to NULL.\r
311 //\r
312 if (DevicePathSubType (DevPath) == END_ENTIRE_DEVICE_PATH_SUBTYPE) {\r
313 *DevicePath = NULL;\r
314 } else {\r
315 *DevicePath = NextDevicePathNode (DevPath);\r
316 }\r
317\r
318 return ReturnValue;\r
319}\r
320\r
321/**\r
322 Return TRUE is this is a multi instance device path.\r
323\r
324 @param DevicePath A pointer to a device path data structure.\r
325\r
326 @retval TRUE If DevicePath is multi-instance.\r
327 @retval FALSE If DevicePath is not multi-instance or DevicePath is NULL.\r
328\r
329**/\r
330BOOLEAN\r
331EFIAPI\r
332IsDevicePathMultiInstance (\r
333 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
334 )\r
335{\r
336 CONST EFI_DEVICE_PATH_PROTOCOL *Node;\r
337\r
338 if (DevicePath == NULL) {\r
339 return FALSE;\r
340 }\r
341\r
342 Node = DevicePath;\r
343 while (!EfiIsDevicePathEnd (Node)) {\r
344 if (EfiIsDevicePathEndInstance (Node)) {\r
345 return TRUE;\r
346 }\r
347\r
348 Node = EfiNextDevicePathNode (Node);\r
349 }\r
350\r
351 return FALSE;\r
352}\r
353\r
354/**\r
355 This function retrieves the device path protocol from a handle.\r
356\r
357 @param Handle The handle from which to retrieve the device path protocol.\r
358\r
359 @return This function returns the device path protocol from the handle specified by Handle.\r
360 If Handle is NULL or Handle does not contain a device path protocol, then NULL is returned.\r
361\r
362**/\r
363EFI_DEVICE_PATH_PROTOCOL *\r
364EFIAPI\r
365DevicePathFromHandle (\r
366 IN EFI_HANDLE Handle\r
367 )\r
368{\r
369 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
370 EFI_STATUS Status;\r
371\r
372 Status = gBS->HandleProtocol (\r
373 Handle,\r
374 &gEfiDevicePathProtocolGuid,\r
375 (VOID *) &DevicePath\r
376 );\r
377 if (EFI_ERROR (Status)) {\r
378 DevicePath = NULL;\r
379 }\r
380 return DevicePath;\r
381}\r
382\r
383/**\r
384 This function allocates a device path for a file and appends it to an existing device path.\r
385\r
386 @param Device A pointer to a device handle. This parameter is optional and may be NULL.\r
387 @param FileName A pointer to a Null-terminated Unicode string.\r
388\r
389 @return If Device is a valid device handle that contains a device path protocol,\r
390 then a device path for the file specified by FileName is allocated\r
391 and appended to the device path associated with the handle Device. The allocated device path is returned.\r
392 If Device is NULL or Device is a handle that does not support the device path protocol,\r
393 then a device path containing a single device path node for the file specified by FileName\r
394 is allocated and returned.\r
395\r
396**/\r
397EFI_DEVICE_PATH_PROTOCOL *\r
398EFIAPI\r
399FileDevicePath (\r
400 IN EFI_HANDLE Device, OPTIONAL\r
401 IN CONST CHAR16 *FileName\r
402 )\r
403{\r
404 UINTN FileNameSize;\r
405 UINTN FilePathNodeSize;\r
406 FILEPATH_DEVICE_PATH *FilePathNode;\r
407 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
408\r
409 DevicePath = NULL;\r
410\r
411 FileNameSize = StrSize (FileName);\r
412 FilePathNodeSize = FileNameSize + SIZE_OF_FILEPATH_DEVICE_PATH;\r
413 FilePathNode = AllocatePool (FilePathNodeSize);\r
414 if (FilePathNode != NULL) {\r
415 //\r
416 // Build a file path node\r
417 //\r
418 FilePathNode->Header.Type = MEDIA_DEVICE_PATH;\r
419 FilePathNode->Header.SubType = MEDIA_FILEPATH_DP;\r
420 SetDevicePathNodeLength (&FilePathNode->Header, FilePathNodeSize);\r
421 CopyMem (FilePathNode->PathName, FileName, FileNameSize);\r
422 \r
423 //\r
424 // Append file path node to device's device path\r
425 //\r
426 if (Device != NULL) {\r
427 DevicePath = DevicePathFromHandle (Device);\r
428 }\r
429 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *) FilePathNode);\r
430 FreePool (FilePathNode);\r
431 }\r
432 return DevicePath;\r
433}\r
434\r