]>
Commit | Line | Data |
---|---|---|
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 | |
34 | UINTN\r | |
35 | EFIAPI\r | |
36 | GetDevicePathSize (\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 | |
69 | EFI_DEVICE_PATH_PROTOCOL *\r | |
70 | EFIAPI\r | |
71 | DuplicateDevicePath (\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 | |
108 | EFI_DEVICE_PATH_PROTOCOL *\r | |
109 | EFIAPI\r | |
110 | AppendDevicePath (\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 | |
5f10fa01 | 158 | @param DevicePath A pointer to a device path data structure.\r |
878ddf1f | 159 | \r |
5f10fa01 | 160 | @param DevicePathNode A pointer to a single device path node.\r |
878ddf1f | 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 | |
167 | EFI_DEVICE_PATH_PROTOCOL *\r | |
168 | EFIAPI\r | |
169 | AppendDevicePathNode (\r | |
5f10fa01 | 170 | IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r |
171 | IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathNode\r | |
878ddf1f | 172 | )\r |
173 | {\r | |
5f10fa01 | 174 | EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r |
878ddf1f | 175 | EFI_DEVICE_PATH_PROTOCOL *NextNode;\r |
176 | EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r | |
177 | UINTN NodeLength;\r | |
878ddf1f | 178 | \r |
179 | //\r | |
180 | // Build a Node that has a terminator on it\r | |
181 | //\r | |
5f10fa01 | 182 | NodeLength = DevicePathNodeLength (DevicePathNode);\r |
878ddf1f | 183 | \r |
5f10fa01 | 184 | TempDevicePath = AllocatePool (NodeLength + sizeof (EFI_DEVICE_PATH_PROTOCOL));\r |
185 | if (TempDevicePath == NULL) {\r | |
186 | return NULL;\r | |
878ddf1f | 187 | }\r |
5f10fa01 | 188 | TempDevicePath = CopyMem (TempDevicePath, DevicePathNode, NodeLength);\r |
189 | //\r | |
190 | // Add and end device path node to convert Node to device path\r | |
191 | //\r | |
192 | NextNode = NextDevicePathNode (TempDevicePath);\r | |
193 | SetDevicePathEndNode (NextNode);\r | |
194 | //\r | |
195 | // Append device paths\r | |
196 | //\r | |
197 | NewDevicePath = AppendDevicePath (DevicePath, TempDevicePath);\r | |
198 | \r | |
199 | FreePool (TempDevicePath);\r | |
200 | \r | |
878ddf1f | 201 | return NewDevicePath;\r |
202 | }\r | |
203 | \r | |
204 | /**\r | |
205 | This function appends the device path instance Instance to the device path Source.\r | |
206 | If Source is NULL, then a new device path with one instance is created. \r | |
207 | \r | |
208 | @param Source A pointer to a device path data structure.\r | |
209 | @param Instance A pointer to a device path instance.\r | |
210 | \r | |
211 | @return A pointer to the new device path.\r | |
212 | If there is not enough temporary pool memory available to complete this function,\r | |
213 | then NULL is returned.\r | |
214 | \r | |
215 | **/\r | |
216 | EFI_DEVICE_PATH_PROTOCOL *\r | |
217 | EFIAPI\r | |
218 | AppendDevicePathInstance (\r | |
219 | IN CONST EFI_DEVICE_PATH_PROTOCOL *Source,\r | |
220 | IN CONST EFI_DEVICE_PATH_PROTOCOL *Instance\r | |
221 | )\r | |
222 | {\r | |
223 | EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r | |
224 | EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r | |
225 | UINTN SrcSize;\r | |
226 | UINTN InstanceSize;\r | |
227 | \r | |
228 | if (Source == NULL) {\r | |
229 | return DuplicateDevicePath (Instance);\r | |
230 | }\r | |
231 | \r | |
232 | SrcSize = GetDevicePathSize (Source);\r | |
233 | InstanceSize = GetDevicePathSize (Instance);\r | |
234 | \r | |
235 | NewDevicePath = AllocatePool (SrcSize + InstanceSize);\r | |
236 | if (NewDevicePath != NULL) {\r | |
237 | \r | |
238 | DevicePath = CopyMem (NewDevicePath, Source, SrcSize);;\r | |
239 | \r | |
240 | while (!IsDevicePathEnd (DevicePath)) {\r | |
241 | DevicePath = NextDevicePathNode (DevicePath);\r | |
242 | }\r | |
243 | \r | |
244 | DevicePath->SubType = END_INSTANCE_DEVICE_PATH_SUBTYPE;\r | |
245 | \r | |
246 | DevicePath = NextDevicePathNode (DevicePath);\r | |
247 | CopyMem (DevicePath, Instance, InstanceSize);\r | |
248 | }\r | |
249 | \r | |
250 | return NewDevicePath;\r | |
251 | }\r | |
252 | \r | |
253 | /**\r | |
254 | Function retrieves the next device path instance from a device path data structure.\r | |
255 | \r | |
256 | @param DevicePath A pointer to a device path data structure.\r | |
257 | \r | |
258 | @param Size A pointer to the size of a device path instance in bytes.\r | |
259 | \r | |
260 | @return This function returns a pointer to the current device path instance.\r | |
261 | In addition, it returns the size in bytes of the current device path instance in Size,\r | |
262 | and a pointer to the next device path instance in DevicePath.\r | |
263 | If there are no more device path instances in DevicePath, then DevicePath will be set to NULL.\r | |
264 | \r | |
265 | **/\r | |
266 | EFI_DEVICE_PATH_PROTOCOL *\r | |
267 | EFIAPI\r | |
268 | GetNextDevicePathInstance (\r | |
269 | IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath,\r | |
270 | OUT UINTN *Size\r | |
271 | )\r | |
272 | {\r | |
273 | EFI_DEVICE_PATH_PROTOCOL *DevPath;\r | |
274 | EFI_DEVICE_PATH_PROTOCOL *ReturnValue;\r | |
275 | UINT8 Temp;\r | |
276 | \r | |
277 | ASSERT (DevicePath != NULL);\r | |
278 | ASSERT (Size != NULL);\r | |
279 | if (*DevicePath == NULL) {\r | |
280 | *Size = 0;\r | |
281 | return NULL;\r | |
282 | }\r | |
283 | \r | |
284 | //\r | |
285 | // Find the end of the device path instance\r | |
286 | //\r | |
287 | DevPath = *DevicePath;\r | |
288 | while (!IsDevicePathEndType (DevPath)) {\r | |
289 | DevPath = NextDevicePathNode (DevPath);\r | |
290 | }\r | |
291 | \r | |
292 | //\r | |
293 | // Compute the size of the device path instance\r | |
294 | //\r | |
295 | *Size = ((UINTN) DevPath - (UINTN) (*DevicePath)) + sizeof (EFI_DEVICE_PATH_PROTOCOL);\r | |
296 | \r | |
297 | //\r | |
298 | // Make a copy and return the device path instance\r | |
299 | //\r | |
300 | Temp = DevPath->SubType;\r | |
301 | DevPath->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;\r | |
302 | ReturnValue = DuplicateDevicePath (*DevicePath);\r | |
303 | DevPath->SubType = Temp;\r | |
304 | \r | |
305 | //\r | |
306 | // If DevPath is the end of an entire device path, then another instance\r | |
307 | // does not follow, so *DevicePath is set to NULL.\r | |
308 | //\r | |
309 | if (DevicePathSubType (DevPath) == END_ENTIRE_DEVICE_PATH_SUBTYPE) {\r | |
310 | *DevicePath = NULL;\r | |
311 | } else {\r | |
312 | *DevicePath = NextDevicePathNode (DevPath);\r | |
313 | }\r | |
314 | \r | |
315 | return ReturnValue;\r | |
316 | }\r | |
317 | \r | |
318 | /**\r | |
319 | Return TRUE is this is a multi instance device path.\r | |
320 | \r | |
321 | @param DevicePath A pointer to a device path data structure.\r | |
322 | \r | |
323 | @retval TRUE If DevicePath is multi-instance.\r | |
324 | @retval FALSE If DevicePath is not multi-instance or DevicePath is NULL.\r | |
325 | \r | |
326 | **/\r | |
327 | BOOLEAN\r | |
328 | EFIAPI\r | |
329 | IsDevicePathMultiInstance (\r | |
330 | IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r | |
331 | )\r | |
332 | {\r | |
333 | CONST EFI_DEVICE_PATH_PROTOCOL *Node;\r | |
334 | \r | |
335 | if (DevicePath == NULL) {\r | |
336 | return FALSE;\r | |
337 | }\r | |
338 | \r | |
339 | Node = DevicePath;\r | |
340 | while (!EfiIsDevicePathEnd (Node)) {\r | |
341 | if (EfiIsDevicePathEndInstance (Node)) {\r | |
342 | return TRUE;\r | |
343 | }\r | |
344 | \r | |
345 | Node = EfiNextDevicePathNode (Node);\r | |
346 | }\r | |
347 | \r | |
348 | return FALSE;\r | |
349 | }\r | |
350 | \r | |
351 | /**\r | |
352 | This function retrieves the device path protocol from a handle.\r | |
353 | \r | |
354 | @param Handle The handle from which to retrieve the device path protocol.\r | |
355 | \r | |
356 | @return This function returns the device path protocol from the handle specified by Handle.\r | |
357 | If Handle is NULL or Handle does not contain a device path protocol, then NULL is returned.\r | |
358 | \r | |
359 | **/\r | |
360 | EFI_DEVICE_PATH_PROTOCOL *\r | |
361 | EFIAPI\r | |
362 | DevicePathFromHandle (\r | |
363 | IN EFI_HANDLE Handle\r | |
364 | )\r | |
365 | {\r | |
366 | EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r | |
367 | EFI_STATUS Status;\r | |
368 | \r | |
369 | Status = gBS->HandleProtocol (\r | |
370 | Handle,\r | |
371 | &gEfiDevicePathProtocolGuid,\r | |
372 | (VOID *) &DevicePath\r | |
373 | );\r | |
374 | if (EFI_ERROR (Status)) {\r | |
375 | DevicePath = NULL;\r | |
376 | }\r | |
377 | return DevicePath;\r | |
378 | }\r | |
379 | \r | |
380 | /**\r | |
381 | This function allocates a device path for a file and appends it to an existing device path.\r | |
382 | \r | |
383 | @param Device A pointer to a device handle. This parameter is optional and may be NULL.\r | |
384 | @param FileName A pointer to a Null-terminated Unicode string.\r | |
385 | \r | |
386 | @return If Device is a valid device handle that contains a device path protocol,\r | |
387 | then a device path for the file specified by FileName is allocated\r | |
388 | and appended to the device path associated with the handle Device. The allocated device path is returned.\r | |
389 | If Device is NULL or Device is a handle that does not support the device path protocol,\r | |
390 | then a device path containing a single device path node for the file specified by FileName\r | |
391 | is allocated and returned.\r | |
392 | \r | |
393 | **/\r | |
394 | EFI_DEVICE_PATH_PROTOCOL *\r | |
395 | EFIAPI\r | |
396 | FileDevicePath (\r | |
397 | IN EFI_HANDLE Device, OPTIONAL\r | |
398 | IN CONST CHAR16 *FileName\r | |
399 | )\r | |
400 | {\r | |
401 | UINTN FileNameSize;\r | |
402 | UINTN FilePathNodeSize;\r | |
403 | FILEPATH_DEVICE_PATH *FilePathNode;\r | |
404 | EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r | |
405 | \r | |
406 | DevicePath = NULL;\r | |
407 | \r | |
408 | FileNameSize = StrSize (FileName);\r | |
409 | FilePathNodeSize = FileNameSize + SIZE_OF_FILEPATH_DEVICE_PATH;\r | |
410 | FilePathNode = AllocatePool (FilePathNodeSize);\r | |
411 | if (FilePathNode != NULL) {\r | |
412 | //\r | |
413 | // Build a file path node\r | |
414 | //\r | |
415 | FilePathNode->Header.Type = MEDIA_DEVICE_PATH;\r | |
416 | FilePathNode->Header.SubType = MEDIA_FILEPATH_DP;\r | |
417 | SetDevicePathNodeLength (&FilePathNode->Header, FilePathNodeSize);\r | |
418 | CopyMem (FilePathNode->PathName, FileName, FileNameSize);\r | |
419 | \r | |
420 | //\r | |
421 | // Append file path node to device's device path\r | |
422 | //\r | |
423 | if (Device != NULL) {\r | |
424 | DevicePath = DevicePathFromHandle (Device);\r | |
425 | }\r | |
426 | DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *) FilePathNode);\r | |
427 | FreePool (FilePathNode);\r | |
428 | }\r | |
429 | return DevicePath;\r | |
430 | }\r | |
431 | \r |