]>
Commit | Line | Data |
---|---|---|
4d0a30a4 RN |
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 | |
9095d37b | 11 | Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r |
9344f092 | 12 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
4d0a30a4 RN |
13 | \r |
14 | **/\r | |
15 | \r | |
16 | #include "UefiDevicePathLib.h"\r | |
17 | \r | |
18 | //\r | |
19 | // Template for an end-of-device path node.\r | |
20 | //\r | |
21 | GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_DEVICE_PATH_PROTOCOL mUefiDevicePathLibEndDevicePath = {\r | |
22 | END_DEVICE_PATH_TYPE,\r | |
23 | END_ENTIRE_DEVICE_PATH_SUBTYPE,\r | |
24 | {\r | |
25 | END_DEVICE_PATH_LENGTH,\r | |
26 | 0\r | |
27 | }\r | |
28 | };\r | |
29 | \r | |
30 | /**\r | |
31 | Determine whether a given device path is valid.\r | |
4d0a30a4 RN |
32 | \r |
33 | @param DevicePath A pointer to a device path data structure.\r | |
34 | @param MaxSize The maximum size of the device path data structure.\r | |
35 | \r | |
36 | @retval TRUE DevicePath is valid.\r | |
fd023942 MT |
37 | @retval FALSE DevicePath is NULL.\r |
38 | @retval FALSE Maxsize is less than sizeof(EFI_DEVICE_PATH_PROTOCOL).\r | |
4d0a30a4 RN |
39 | @retval FALSE The length of any node node in the DevicePath is less\r |
40 | than sizeof (EFI_DEVICE_PATH_PROTOCOL).\r | |
41 | @retval FALSE If MaxSize is not zero, the size of the DevicePath\r | |
42 | exceeds MaxSize.\r | |
43 | @retval FALSE If PcdMaximumDevicePathNodeCount is not zero, the node\r | |
44 | count of the DevicePath exceeds PcdMaximumDevicePathNodeCount.\r | |
45 | **/\r | |
46 | BOOLEAN\r | |
47 | EFIAPI\r | |
48 | IsDevicePathValid (\r | |
2f88bd3a MK |
49 | IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r |
50 | IN UINTN MaxSize\r | |
4d0a30a4 RN |
51 | )\r |
52 | {\r | |
2f88bd3a MK |
53 | UINTN Count;\r |
54 | UINTN Size;\r | |
55 | UINTN NodeLength;\r | |
4d0a30a4 | 56 | \r |
c3c9892c | 57 | //\r |
2f88bd3a | 58 | // Validate the input whether exists and its size big enough to touch the first node\r |
c3c9892c | 59 | //\r |
2f88bd3a | 60 | if ((DevicePath == NULL) || ((MaxSize > 0) && (MaxSize < END_DEVICE_PATH_LENGTH))) {\r |
c3c9892c ED |
61 | return FALSE;\r |
62 | }\r | |
63 | \r | |
fd023942 MT |
64 | if (MaxSize == 0) {\r |
65 | MaxSize = MAX_UINTN;\r | |
66 | }\r | |
67 | \r | |
fb9405f9 | 68 | for (Count = 0, Size = 0; !IsDevicePathEnd (DevicePath); DevicePath = NextDevicePathNode (DevicePath)) {\r |
4d0a30a4 RN |
69 | NodeLength = DevicePathNodeLength (DevicePath);\r |
70 | if (NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {\r | |
71 | return FALSE;\r | |
72 | }\r | |
73 | \r | |
c3c9892c ED |
74 | if (NodeLength > MAX_UINTN - Size) {\r |
75 | return FALSE;\r | |
76 | }\r | |
2f88bd3a | 77 | \r |
c3c9892c ED |
78 | Size += NodeLength;\r |
79 | \r | |
80 | //\r | |
81 | // Validate next node before touch it.\r | |
82 | //\r | |
83 | if (Size > MaxSize - END_DEVICE_PATH_LENGTH ) {\r | |
84 | return FALSE;\r | |
4d0a30a4 RN |
85 | }\r |
86 | \r | |
87 | if (PcdGet32 (PcdMaximumDevicePathNodeCount) > 0) {\r | |
88 | Count++;\r | |
89 | if (Count >= PcdGet32 (PcdMaximumDevicePathNodeCount)) {\r | |
90 | return FALSE;\r | |
91 | }\r | |
92 | }\r | |
2f7a96d6 JW |
93 | \r |
94 | //\r | |
95 | // FilePath must be a NULL-terminated string.\r | |
96 | //\r | |
2f88bd3a MK |
97 | if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) &&\r |
98 | (DevicePathSubType (DevicePath) == MEDIA_FILEPATH_DP) &&\r | |
99 | (*(CHAR16 *)((UINT8 *)DevicePath + NodeLength - 2) != 0))\r | |
100 | {\r | |
2f7a96d6 JW |
101 | return FALSE;\r |
102 | }\r | |
4d0a30a4 RN |
103 | }\r |
104 | \r | |
105 | //\r | |
106 | // Only return TRUE when the End Device Path node is valid.\r | |
107 | //\r | |
2f88bd3a | 108 | return (BOOLEAN)(DevicePathNodeLength (DevicePath) == END_DEVICE_PATH_LENGTH);\r |
4d0a30a4 RN |
109 | }\r |
110 | \r | |
111 | /**\r | |
112 | Returns the Type field of a device path node.\r | |
113 | \r | |
114 | Returns the Type field of the device path node specified by Node.\r | |
115 | \r | |
116 | If Node is NULL, then ASSERT().\r | |
117 | \r | |
118 | @param Node A pointer to a device path node data structure.\r | |
119 | \r | |
120 | @return The Type field of the device path node specified by Node.\r | |
121 | \r | |
122 | **/\r | |
123 | UINT8\r | |
124 | EFIAPI\r | |
125 | DevicePathType (\r | |
126 | IN CONST VOID *Node\r | |
127 | )\r | |
128 | {\r | |
129 | ASSERT (Node != NULL);\r | |
130 | return ((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Type;\r | |
131 | }\r | |
132 | \r | |
133 | /**\r | |
134 | Returns the SubType field of a device path node.\r | |
135 | \r | |
136 | Returns the SubType field of the device path node specified by Node.\r | |
137 | \r | |
138 | If Node is NULL, then ASSERT().\r | |
139 | \r | |
140 | @param Node A pointer to a device path node data structure.\r | |
141 | \r | |
142 | @return The SubType field of the device path node specified by Node.\r | |
143 | \r | |
144 | **/\r | |
145 | UINT8\r | |
146 | EFIAPI\r | |
147 | DevicePathSubType (\r | |
148 | IN CONST VOID *Node\r | |
149 | )\r | |
150 | {\r | |
151 | ASSERT (Node != NULL);\r | |
152 | return ((EFI_DEVICE_PATH_PROTOCOL *)(Node))->SubType;\r | |
153 | }\r | |
154 | \r | |
155 | /**\r | |
156 | Returns the 16-bit Length field of a device path node.\r | |
157 | \r | |
9095d37b | 158 | Returns the 16-bit Length field of the device path node specified by Node.\r |
4d0a30a4 | 159 | Node is not required to be aligned on a 16-bit boundary, so it is recommended\r |
9095d37b | 160 | that a function such as ReadUnaligned16() be used to extract the contents of\r |
4d0a30a4 RN |
161 | the Length field.\r |
162 | \r | |
163 | If Node is NULL, then ASSERT().\r | |
164 | \r | |
165 | @param Node A pointer to a device path node data structure.\r | |
166 | \r | |
167 | @return The 16-bit Length field of the device path node specified by Node.\r | |
168 | \r | |
169 | **/\r | |
170 | UINTN\r | |
171 | EFIAPI\r | |
172 | DevicePathNodeLength (\r | |
173 | IN CONST VOID *Node\r | |
174 | )\r | |
175 | {\r | |
4d0a30a4 | 176 | ASSERT (Node != NULL);\r |
ed2867de | 177 | return ReadUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0]);\r |
4d0a30a4 RN |
178 | }\r |
179 | \r | |
180 | /**\r | |
181 | Returns a pointer to the next node in a device path.\r | |
182 | \r | |
9095d37b | 183 | Returns a pointer to the device path node that follows the device path node\r |
4d0a30a4 RN |
184 | specified by Node.\r |
185 | \r | |
186 | If Node is NULL, then ASSERT().\r | |
187 | \r | |
188 | @param Node A pointer to a device path node data structure.\r | |
189 | \r | |
9095d37b | 190 | @return a pointer to the device path node that follows the device path node\r |
4d0a30a4 RN |
191 | specified by Node.\r |
192 | \r | |
193 | **/\r | |
194 | EFI_DEVICE_PATH_PROTOCOL *\r | |
195 | EFIAPI\r | |
196 | NextDevicePathNode (\r | |
197 | IN CONST VOID *Node\r | |
198 | )\r | |
199 | {\r | |
200 | ASSERT (Node != NULL);\r | |
2f88bd3a | 201 | return (EFI_DEVICE_PATH_PROTOCOL *)((UINT8 *)(Node) + DevicePathNodeLength (Node));\r |
4d0a30a4 RN |
202 | }\r |
203 | \r | |
204 | /**\r | |
205 | Determines if a device path node is an end node of a device path.\r | |
9095d37b | 206 | This includes nodes that are the end of a device path instance and nodes that\r |
4d0a30a4 RN |
207 | are the end of an entire device path.\r |
208 | \r | |
9095d37b LG |
209 | Determines if the device path node specified by Node is an end node of a device path.\r |
210 | This includes nodes that are the end of a device path instance and nodes that are the\r | |
211 | end of an entire device path. If Node represents an end node of a device path,\r | |
4d0a30a4 RN |
212 | then TRUE is returned. Otherwise, FALSE is returned.\r |
213 | \r | |
214 | If Node is NULL, then ASSERT().\r | |
215 | \r | |
216 | @param Node A pointer to a device path node data structure.\r | |
217 | \r | |
9095d37b | 218 | @retval TRUE The device path node specified by Node is an end node of a\r |
4d0a30a4 | 219 | device path.\r |
9095d37b | 220 | @retval FALSE The device path node specified by Node is not an end node of\r |
4d0a30a4 | 221 | a device path.\r |
9095d37b | 222 | \r |
4d0a30a4 RN |
223 | **/\r |
224 | BOOLEAN\r | |
225 | EFIAPI\r | |
226 | IsDevicePathEndType (\r | |
227 | IN CONST VOID *Node\r | |
228 | )\r | |
229 | {\r | |
230 | ASSERT (Node != NULL);\r | |
2f88bd3a | 231 | return (BOOLEAN)(DevicePathType (Node) == END_DEVICE_PATH_TYPE);\r |
4d0a30a4 RN |
232 | }\r |
233 | \r | |
234 | /**\r | |
235 | Determines if a device path node is an end node of an entire device path.\r | |
236 | \r | |
9095d37b LG |
237 | Determines if a device path node specified by Node is an end node of an entire\r |
238 | device path. If Node represents the end of an entire device path, then TRUE is\r | |
4d0a30a4 RN |
239 | returned. Otherwise, FALSE is returned.\r |
240 | \r | |
241 | If Node is NULL, then ASSERT().\r | |
242 | \r | |
243 | @param Node A pointer to a device path node data structure.\r | |
244 | \r | |
9095d37b | 245 | @retval TRUE The device path node specified by Node is the end of an entire\r |
4d0a30a4 | 246 | device path.\r |
9095d37b | 247 | @retval FALSE The device path node specified by Node is not the end of an\r |
4d0a30a4 RN |
248 | entire device path.\r |
249 | \r | |
250 | **/\r | |
251 | BOOLEAN\r | |
252 | EFIAPI\r | |
253 | IsDevicePathEnd (\r | |
254 | IN CONST VOID *Node\r | |
255 | )\r | |
256 | {\r | |
257 | ASSERT (Node != NULL);\r | |
2f88bd3a | 258 | return (BOOLEAN)(IsDevicePathEndType (Node) && DevicePathSubType (Node) == END_ENTIRE_DEVICE_PATH_SUBTYPE);\r |
4d0a30a4 RN |
259 | }\r |
260 | \r | |
261 | /**\r | |
262 | Determines if a device path node is an end node of a device path instance.\r | |
263 | \r | |
9095d37b LG |
264 | Determines if a device path node specified by Node is an end node of a device\r |
265 | path instance. If Node represents the end of a device path instance, then TRUE\r | |
4d0a30a4 RN |
266 | is returned. Otherwise, FALSE is returned.\r |
267 | \r | |
268 | If Node is NULL, then ASSERT().\r | |
269 | \r | |
270 | @param Node A pointer to a device path node data structure.\r | |
271 | \r | |
9095d37b | 272 | @retval TRUE The device path node specified by Node is the end of a device\r |
4d0a30a4 | 273 | path instance.\r |
9095d37b | 274 | @retval FALSE The device path node specified by Node is not the end of a\r |
4d0a30a4 RN |
275 | device path instance.\r |
276 | \r | |
277 | **/\r | |
278 | BOOLEAN\r | |
279 | EFIAPI\r | |
280 | IsDevicePathEndInstance (\r | |
281 | IN CONST VOID *Node\r | |
282 | )\r | |
283 | {\r | |
284 | ASSERT (Node != NULL);\r | |
2f88bd3a | 285 | return (BOOLEAN)(IsDevicePathEndType (Node) && DevicePathSubType (Node) == END_INSTANCE_DEVICE_PATH_SUBTYPE);\r |
4d0a30a4 RN |
286 | }\r |
287 | \r | |
288 | /**\r | |
289 | Sets the length, in bytes, of a device path node.\r | |
290 | \r | |
9095d37b LG |
291 | Sets the length of the device path node specified by Node to the value specified\r |
292 | by NodeLength. NodeLength is returned. Node is not required to be aligned on\r | |
4d0a30a4 RN |
293 | a 16-bit boundary, so it is recommended that a function such as WriteUnaligned16()\r |
294 | be used to set the contents of the Length field.\r | |
295 | \r | |
296 | If Node is NULL, then ASSERT().\r | |
297 | If NodeLength >= SIZE_64KB, then ASSERT().\r | |
298 | If NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL), then ASSERT().\r | |
299 | \r | |
300 | @param Node A pointer to a device path node data structure.\r | |
301 | @param Length The length, in bytes, of the device path node.\r | |
302 | \r | |
303 | @return Length\r | |
304 | \r | |
305 | **/\r | |
306 | UINT16\r | |
307 | EFIAPI\r | |
308 | SetDevicePathNodeLength (\r | |
309 | IN OUT VOID *Node,\r | |
310 | IN UINTN Length\r | |
311 | )\r | |
312 | {\r | |
313 | ASSERT (Node != NULL);\r | |
314 | ASSERT ((Length >= sizeof (EFI_DEVICE_PATH_PROTOCOL)) && (Length < SIZE_64KB));\r | |
315 | return WriteUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0], (UINT16)(Length));\r | |
316 | }\r | |
317 | \r | |
318 | /**\r | |
319 | Fills in all the fields of a device path node that is the end of an entire device path.\r | |
320 | \r | |
9095d37b LG |
321 | Fills in all the fields of a device path node specified by Node so Node represents\r |
322 | the end of an entire device path. The Type field of Node is set to\r | |
323 | END_DEVICE_PATH_TYPE, the SubType field of Node is set to\r | |
324 | END_ENTIRE_DEVICE_PATH_SUBTYPE, and the Length field of Node is set to\r | |
325 | END_DEVICE_PATH_LENGTH. Node is not required to be aligned on a 16-bit boundary,\r | |
326 | so it is recommended that a function such as WriteUnaligned16() be used to set\r | |
327 | the contents of the Length field.\r | |
4d0a30a4 | 328 | \r |
9095d37b | 329 | If Node is NULL, then ASSERT().\r |
4d0a30a4 RN |
330 | \r |
331 | @param Node A pointer to a device path node data structure.\r | |
332 | \r | |
333 | **/\r | |
334 | VOID\r | |
335 | EFIAPI\r | |
336 | SetDevicePathEndNode (\r | |
337 | OUT VOID *Node\r | |
338 | )\r | |
339 | {\r | |
340 | ASSERT (Node != NULL);\r | |
341 | CopyMem (Node, &mUefiDevicePathLibEndDevicePath, sizeof (mUefiDevicePathLibEndDevicePath));\r | |
342 | }\r | |
343 | \r | |
344 | /**\r | |
345 | Returns the size of a device path in bytes.\r | |
346 | \r | |
9095d37b | 347 | This function returns the size, in bytes, of the device path data structure\r |
4d0a30a4 RN |
348 | specified by DevicePath including the end of device path node.\r |
349 | If DevicePath is NULL or invalid, then 0 is returned.\r | |
350 | \r | |
351 | @param DevicePath A pointer to a device path data structure.\r | |
352 | \r | |
353 | @retval 0 If DevicePath is NULL or invalid.\r | |
354 | @retval Others The size of a device path in bytes.\r | |
355 | \r | |
356 | **/\r | |
357 | UINTN\r | |
358 | EFIAPI\r | |
359 | UefiDevicePathLibGetDevicePathSize (\r | |
360 | IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r | |
361 | )\r | |
362 | {\r | |
363 | CONST EFI_DEVICE_PATH_PROTOCOL *Start;\r | |
364 | \r | |
365 | if (DevicePath == NULL) {\r | |
366 | return 0;\r | |
367 | }\r | |
368 | \r | |
369 | if (!IsDevicePathValid (DevicePath, 0)) {\r | |
370 | return 0;\r | |
371 | }\r | |
372 | \r | |
373 | //\r | |
374 | // Search for the end of the device path structure\r | |
375 | //\r | |
376 | Start = DevicePath;\r | |
377 | while (!IsDevicePathEnd (DevicePath)) {\r | |
378 | DevicePath = NextDevicePathNode (DevicePath);\r | |
379 | }\r | |
380 | \r | |
381 | //\r | |
382 | // Compute the size and add back in the size of the end device path structure\r | |
383 | //\r | |
2f88bd3a | 384 | return ((UINTN)DevicePath - (UINTN)Start) + DevicePathNodeLength (DevicePath);\r |
4d0a30a4 RN |
385 | }\r |
386 | \r | |
387 | /**\r | |
388 | Creates a new copy of an existing device path.\r | |
389 | \r | |
9095d37b LG |
390 | This function allocates space for a new copy of the device path specified by DevicePath.\r |
391 | If DevicePath is NULL, then NULL is returned. If the memory is successfully\r | |
392 | allocated, then the contents of DevicePath are copied to the newly allocated\r | |
393 | buffer, and a pointer to that buffer is returned. Otherwise, NULL is returned.\r | |
394 | The memory for the new device path is allocated from EFI boot services memory.\r | |
395 | It is the responsibility of the caller to free the memory allocated.\r | |
396 | \r | |
4d0a30a4 RN |
397 | @param DevicePath A pointer to a device path data structure.\r |
398 | \r | |
399 | @retval NULL DevicePath is NULL or invalid.\r | |
400 | @retval Others A pointer to the duplicated device path.\r | |
9095d37b | 401 | \r |
4d0a30a4 RN |
402 | **/\r |
403 | EFI_DEVICE_PATH_PROTOCOL *\r | |
404 | EFIAPI\r | |
405 | UefiDevicePathLibDuplicateDevicePath (\r | |
406 | IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r | |
407 | )\r | |
408 | {\r | |
2f88bd3a | 409 | UINTN Size;\r |
4d0a30a4 RN |
410 | \r |
411 | //\r | |
412 | // Compute the size\r | |
413 | //\r | |
414 | Size = GetDevicePathSize (DevicePath);\r | |
415 | if (Size == 0) {\r | |
416 | return NULL;\r | |
417 | }\r | |
418 | \r | |
419 | //\r | |
420 | // Allocate space for duplicate device path\r | |
421 | //\r | |
422 | \r | |
423 | return AllocateCopyPool (Size, DevicePath);\r | |
424 | }\r | |
425 | \r | |
426 | /**\r | |
427 | Creates a new device path by appending a second device path to a first device path.\r | |
428 | \r | |
9095d37b LG |
429 | This function creates a new device path by appending a copy of SecondDevicePath\r |
430 | to a copy of FirstDevicePath in a newly allocated buffer. Only the end-of-device-path\r | |
431 | device node from SecondDevicePath is retained. The newly created device path is\r | |
432 | returned. If FirstDevicePath is NULL, then it is ignored, and a duplicate of\r | |
433 | SecondDevicePath is returned. If SecondDevicePath is NULL, then it is ignored,\r | |
434 | and a duplicate of FirstDevicePath is returned. If both FirstDevicePath and\r | |
435 | SecondDevicePath are NULL, then a copy of an end-of-device-path is returned.\r | |
436 | \r | |
4d0a30a4 | 437 | If there is not enough memory for the newly allocated buffer, then NULL is returned.\r |
9095d37b | 438 | The memory for the new device path is allocated from EFI boot services memory.\r |
4d0a30a4 RN |
439 | It is the responsibility of the caller to free the memory allocated.\r |
440 | \r | |
441 | @param FirstDevicePath A pointer to a device path data structure.\r | |
442 | @param SecondDevicePath A pointer to a device path data structure.\r | |
9095d37b | 443 | \r |
4d0a30a4 RN |
444 | @retval NULL If there is not enough memory for the newly allocated buffer.\r |
445 | @retval NULL If FirstDevicePath or SecondDevicePath is invalid.\r | |
446 | @retval Others A pointer to the new device path if success.\r | |
447 | Or a copy an end-of-device-path if both FirstDevicePath and SecondDevicePath are NULL.\r | |
448 | \r | |
449 | **/\r | |
450 | EFI_DEVICE_PATH_PROTOCOL *\r | |
451 | EFIAPI\r | |
452 | UefiDevicePathLibAppendDevicePath (\r | |
d0e2f823 | 453 | IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath OPTIONAL,\r |
4d0a30a4 RN |
454 | IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath OPTIONAL\r |
455 | )\r | |
456 | {\r | |
457 | UINTN Size;\r | |
458 | UINTN Size1;\r | |
459 | UINTN Size2;\r | |
460 | EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r | |
461 | EFI_DEVICE_PATH_PROTOCOL *DevicePath2;\r | |
462 | \r | |
463 | //\r | |
464 | // If there's only 1 path, just duplicate it.\r | |
465 | //\r | |
466 | if (FirstDevicePath == NULL) {\r | |
467 | return DuplicateDevicePath ((SecondDevicePath != NULL) ? SecondDevicePath : &mUefiDevicePathLibEndDevicePath);\r | |
468 | }\r | |
469 | \r | |
470 | if (SecondDevicePath == NULL) {\r | |
471 | return DuplicateDevicePath (FirstDevicePath);\r | |
472 | }\r | |
473 | \r | |
474 | if (!IsDevicePathValid (FirstDevicePath, 0) || !IsDevicePathValid (SecondDevicePath, 0)) {\r | |
475 | return NULL;\r | |
476 | }\r | |
477 | \r | |
478 | //\r | |
479 | // Allocate space for the combined device path. It only has one end node of\r | |
480 | // length EFI_DEVICE_PATH_PROTOCOL.\r | |
481 | //\r | |
2f88bd3a MK |
482 | Size1 = GetDevicePathSize (FirstDevicePath);\r |
483 | Size2 = GetDevicePathSize (SecondDevicePath);\r | |
484 | Size = Size1 + Size2 - END_DEVICE_PATH_LENGTH;\r | |
4d0a30a4 RN |
485 | \r |
486 | NewDevicePath = AllocatePool (Size);\r | |
487 | \r | |
488 | if (NewDevicePath != NULL) {\r | |
489 | NewDevicePath = CopyMem (NewDevicePath, FirstDevicePath, Size1);\r | |
490 | //\r | |
491 | // Over write FirstDevicePath EndNode and do the copy\r | |
492 | //\r | |
2f88bd3a MK |
493 | DevicePath2 = (EFI_DEVICE_PATH_PROTOCOL *)((CHAR8 *)NewDevicePath +\r |
494 | (Size1 - END_DEVICE_PATH_LENGTH));\r | |
4d0a30a4 RN |
495 | CopyMem (DevicePath2, SecondDevicePath, Size2);\r |
496 | }\r | |
497 | \r | |
498 | return NewDevicePath;\r | |
499 | }\r | |
500 | \r | |
501 | /**\r | |
502 | Creates a new path by appending the device node to the device path.\r | |
503 | \r | |
9095d37b LG |
504 | This function creates a new device path by appending a copy of the device node\r |
505 | specified by DevicePathNode to a copy of the device path specified by DevicePath\r | |
506 | in an allocated buffer. The end-of-device-path device node is moved after the\r | |
4d0a30a4 RN |
507 | end of the appended device node.\r |
508 | If DevicePathNode is NULL then a copy of DevicePath is returned.\r | |
9095d37b | 509 | If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device\r |
4d0a30a4 | 510 | path device node is returned.\r |
9095d37b | 511 | If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path\r |
4d0a30a4 | 512 | device node is returned.\r |
9095d37b LG |
513 | If there is not enough memory to allocate space for the new device path, then\r |
514 | NULL is returned.\r | |
515 | The memory is allocated from EFI boot services memory. It is the responsibility\r | |
4d0a30a4 RN |
516 | of the caller to free the memory allocated.\r |
517 | \r | |
518 | @param DevicePath A pointer to a device path data structure.\r | |
519 | @param DevicePathNode A pointer to a single device path node.\r | |
520 | \r | |
521 | @retval NULL If there is not enough memory for the new device path.\r | |
522 | @retval Others A pointer to the new device path if success.\r | |
9095d37b | 523 | A copy of DevicePathNode followed by an end-of-device-path node\r |
4d0a30a4 | 524 | if both FirstDevicePath and SecondDevicePath are NULL.\r |
9095d37b | 525 | A copy of an end-of-device-path node if both FirstDevicePath\r |
4d0a30a4 RN |
526 | and SecondDevicePath are NULL.\r |
527 | \r | |
528 | **/\r | |
529 | EFI_DEVICE_PATH_PROTOCOL *\r | |
530 | EFIAPI\r | |
531 | UefiDevicePathLibAppendDevicePathNode (\r | |
d0e2f823 | 532 | IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath OPTIONAL,\r |
4d0a30a4 RN |
533 | IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathNode OPTIONAL\r |
534 | )\r | |
535 | {\r | |
536 | EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r | |
537 | EFI_DEVICE_PATH_PROTOCOL *NextNode;\r | |
538 | EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r | |
539 | UINTN NodeLength;\r | |
540 | \r | |
541 | if (DevicePathNode == NULL) {\r | |
542 | return DuplicateDevicePath ((DevicePath != NULL) ? DevicePath : &mUefiDevicePathLibEndDevicePath);\r | |
543 | }\r | |
2f88bd3a | 544 | \r |
4d0a30a4 RN |
545 | //\r |
546 | // Build a Node that has a terminator on it\r | |
547 | //\r | |
548 | NodeLength = DevicePathNodeLength (DevicePathNode);\r | |
549 | \r | |
550 | TempDevicePath = AllocatePool (NodeLength + END_DEVICE_PATH_LENGTH);\r | |
551 | if (TempDevicePath == NULL) {\r | |
552 | return NULL;\r | |
553 | }\r | |
2f88bd3a | 554 | \r |
4d0a30a4 RN |
555 | TempDevicePath = CopyMem (TempDevicePath, DevicePathNode, NodeLength);\r |
556 | //\r | |
557 | // Add and end device path node to convert Node to device path\r | |
558 | //\r | |
559 | NextNode = NextDevicePathNode (TempDevicePath);\r | |
560 | SetDevicePathEndNode (NextNode);\r | |
561 | //\r | |
562 | // Append device paths\r | |
563 | //\r | |
564 | NewDevicePath = AppendDevicePath (DevicePath, TempDevicePath);\r | |
565 | \r | |
566 | FreePool (TempDevicePath);\r | |
567 | \r | |
568 | return NewDevicePath;\r | |
569 | }\r | |
570 | \r | |
571 | /**\r | |
572 | Creates a new device path by appending the specified device path instance to the specified device\r | |
573 | path.\r | |
9095d37b LG |
574 | \r |
575 | This function creates a new device path by appending a copy of the device path\r | |
576 | instance specified by DevicePathInstance to a copy of the device path specified\r | |
4d0a30a4 | 577 | by DevicePath in a allocated buffer.\r |
9095d37b LG |
578 | The end-of-device-path device node is moved after the end of the appended device\r |
579 | path instance and a new end-of-device-path-instance node is inserted between.\r | |
4d0a30a4 RN |
580 | If DevicePath is NULL, then a copy if DevicePathInstance is returned.\r |
581 | If DevicePathInstance is NULL, then NULL is returned.\r | |
582 | If DevicePath or DevicePathInstance is invalid, then NULL is returned.\r | |
9095d37b LG |
583 | If there is not enough memory to allocate space for the new device path, then\r |
584 | NULL is returned.\r | |
585 | The memory is allocated from EFI boot services memory. It is the responsibility\r | |
4d0a30a4 | 586 | of the caller to free the memory allocated.\r |
9095d37b | 587 | \r |
4d0a30a4 RN |
588 | @param DevicePath A pointer to a device path data structure.\r |
589 | @param DevicePathInstance A pointer to a device path instance.\r | |
590 | \r | |
591 | @return A pointer to the new device path.\r | |
592 | \r | |
593 | **/\r | |
594 | EFI_DEVICE_PATH_PROTOCOL *\r | |
595 | EFIAPI\r | |
596 | UefiDevicePathLibAppendDevicePathInstance (\r | |
d0e2f823 | 597 | IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath OPTIONAL,\r |
4d0a30a4 RN |
598 | IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathInstance OPTIONAL\r |
599 | )\r | |
600 | {\r | |
601 | EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r | |
602 | EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r | |
603 | UINTN SrcSize;\r | |
604 | UINTN InstanceSize;\r | |
605 | \r | |
606 | if (DevicePath == NULL) {\r | |
607 | return DuplicateDevicePath (DevicePathInstance);\r | |
608 | }\r | |
609 | \r | |
610 | if (DevicePathInstance == NULL) {\r | |
611 | return NULL;\r | |
612 | }\r | |
613 | \r | |
614 | if (!IsDevicePathValid (DevicePath, 0) || !IsDevicePathValid (DevicePathInstance, 0)) {\r | |
615 | return NULL;\r | |
616 | }\r | |
617 | \r | |
2f88bd3a MK |
618 | SrcSize = GetDevicePathSize (DevicePath);\r |
619 | InstanceSize = GetDevicePathSize (DevicePathInstance);\r | |
4d0a30a4 RN |
620 | \r |
621 | NewDevicePath = AllocatePool (SrcSize + InstanceSize);\r | |
622 | if (NewDevicePath != NULL) {\r | |
2f88bd3a | 623 | TempDevicePath = CopyMem (NewDevicePath, DevicePath, SrcSize);\r |
9095d37b | 624 | \r |
4d0a30a4 RN |
625 | while (!IsDevicePathEnd (TempDevicePath)) {\r |
626 | TempDevicePath = NextDevicePathNode (TempDevicePath);\r | |
627 | }\r | |
9095d37b | 628 | \r |
2f88bd3a MK |
629 | TempDevicePath->SubType = END_INSTANCE_DEVICE_PATH_SUBTYPE;\r |
630 | TempDevicePath = NextDevicePathNode (TempDevicePath);\r | |
4d0a30a4 RN |
631 | CopyMem (TempDevicePath, DevicePathInstance, InstanceSize);\r |
632 | }\r | |
633 | \r | |
634 | return NewDevicePath;\r | |
635 | }\r | |
636 | \r | |
637 | /**\r | |
638 | Creates a copy of the current device path instance and returns a pointer to the next device path\r | |
639 | instance.\r | |
640 | \r | |
9095d37b LG |
641 | This function creates a copy of the current device path instance. It also updates\r |
642 | DevicePath to point to the next device path instance in the device path (or NULL\r | |
4d0a30a4 RN |
643 | if no more) and updates Size to hold the size of the device path instance copy.\r |
644 | If DevicePath is NULL, then NULL is returned.\r | |
645 | If DevicePath points to a invalid device path, then NULL is returned.\r | |
9095d37b LG |
646 | If there is not enough memory to allocate space for the new device path, then\r |
647 | NULL is returned.\r | |
648 | The memory is allocated from EFI boot services memory. It is the responsibility\r | |
4d0a30a4 RN |
649 | of the caller to free the memory allocated.\r |
650 | If Size is NULL, then ASSERT().\r | |
9095d37b LG |
651 | \r |
652 | @param DevicePath On input, this holds the pointer to the current\r | |
653 | device path instance. On output, this holds\r | |
654 | the pointer to the next device path instance\r | |
4d0a30a4 | 655 | or NULL if there are no more device path\r |
9095d37b | 656 | instances in the device path pointer to a\r |
4d0a30a4 | 657 | device path data structure.\r |
9095d37b LG |
658 | @param Size On output, this holds the size of the device\r |
659 | path instance, in bytes or zero, if DevicePath\r | |
4d0a30a4 RN |
660 | is NULL.\r |
661 | \r | |
662 | @return A pointer to the current device path instance.\r | |
663 | \r | |
664 | **/\r | |
665 | EFI_DEVICE_PATH_PROTOCOL *\r | |
666 | EFIAPI\r | |
667 | UefiDevicePathLibGetNextDevicePathInstance (\r | |
2f88bd3a MK |
668 | IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath,\r |
669 | OUT UINTN *Size\r | |
4d0a30a4 RN |
670 | )\r |
671 | {\r | |
672 | EFI_DEVICE_PATH_PROTOCOL *DevPath;\r | |
673 | EFI_DEVICE_PATH_PROTOCOL *ReturnValue;\r | |
674 | UINT8 Temp;\r | |
675 | \r | |
676 | ASSERT (Size != NULL);\r | |
677 | \r | |
2f88bd3a | 678 | if ((DevicePath == NULL) || (*DevicePath == NULL)) {\r |
4d0a30a4 RN |
679 | *Size = 0;\r |
680 | return NULL;\r | |
681 | }\r | |
682 | \r | |
683 | if (!IsDevicePathValid (*DevicePath, 0)) {\r | |
684 | return NULL;\r | |
685 | }\r | |
686 | \r | |
687 | //\r | |
688 | // Find the end of the device path instance\r | |
689 | //\r | |
690 | DevPath = *DevicePath;\r | |
691 | while (!IsDevicePathEndType (DevPath)) {\r | |
692 | DevPath = NextDevicePathNode (DevPath);\r | |
693 | }\r | |
694 | \r | |
695 | //\r | |
696 | // Compute the size of the device path instance\r | |
697 | //\r | |
2f88bd3a | 698 | *Size = ((UINTN)DevPath - (UINTN)(*DevicePath)) + sizeof (EFI_DEVICE_PATH_PROTOCOL);\r |
9095d37b | 699 | \r |
4d0a30a4 RN |
700 | //\r |
701 | // Make a copy and return the device path instance\r | |
702 | //\r | |
2f88bd3a MK |
703 | Temp = DevPath->SubType;\r |
704 | DevPath->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;\r | |
705 | ReturnValue = DuplicateDevicePath (*DevicePath);\r | |
706 | DevPath->SubType = Temp;\r | |
4d0a30a4 RN |
707 | \r |
708 | //\r | |
709 | // If DevPath is the end of an entire device path, then another instance\r | |
710 | // does not follow, so *DevicePath is set to NULL.\r | |
711 | //\r | |
712 | if (DevicePathSubType (DevPath) == END_ENTIRE_DEVICE_PATH_SUBTYPE) {\r | |
713 | *DevicePath = NULL;\r | |
714 | } else {\r | |
715 | *DevicePath = NextDevicePathNode (DevPath);\r | |
716 | }\r | |
717 | \r | |
718 | return ReturnValue;\r | |
719 | }\r | |
720 | \r | |
721 | /**\r | |
722 | Creates a device node.\r | |
723 | \r | |
9095d37b LG |
724 | This function creates a new device node in a newly allocated buffer of size\r |
725 | NodeLength and initializes the device path node header with NodeType and NodeSubType.\r | |
4d0a30a4 | 726 | The new device path node is returned.\r |
9095d37b LG |
727 | If NodeLength is smaller than a device path header, then NULL is returned.\r |
728 | If there is not enough memory to allocate space for the new device path, then\r | |
729 | NULL is returned.\r | |
730 | The memory is allocated from EFI boot services memory. It is the responsibility\r | |
4d0a30a4 RN |
731 | of the caller to free the memory allocated.\r |
732 | \r | |
733 | @param NodeType The device node type for the new device node.\r | |
734 | @param NodeSubType The device node sub-type for the new device node.\r | |
735 | @param NodeLength The length of the new device node.\r | |
736 | \r | |
737 | @return The new device path.\r | |
738 | \r | |
739 | **/\r | |
740 | EFI_DEVICE_PATH_PROTOCOL *\r | |
741 | EFIAPI\r | |
742 | UefiDevicePathLibCreateDeviceNode (\r | |
2f88bd3a MK |
743 | IN UINT8 NodeType,\r |
744 | IN UINT8 NodeSubType,\r | |
745 | IN UINT16 NodeLength\r | |
4d0a30a4 RN |
746 | )\r |
747 | {\r | |
2f88bd3a | 748 | EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r |
4d0a30a4 RN |
749 | \r |
750 | if (NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {\r | |
751 | //\r | |
752 | // NodeLength is less than the size of the header.\r | |
753 | //\r | |
754 | return NULL;\r | |
755 | }\r | |
9095d37b | 756 | \r |
4d0a30a4 RN |
757 | DevicePath = AllocateZeroPool (NodeLength);\r |
758 | if (DevicePath != NULL) {\r | |
2f88bd3a MK |
759 | DevicePath->Type = NodeType;\r |
760 | DevicePath->SubType = NodeSubType;\r | |
761 | SetDevicePathNodeLength (DevicePath, NodeLength);\r | |
4d0a30a4 RN |
762 | }\r |
763 | \r | |
764 | return DevicePath;\r | |
765 | }\r | |
766 | \r | |
767 | /**\r | |
768 | Determines if a device path is single or multi-instance.\r | |
769 | \r | |
770 | This function returns TRUE if the device path specified by DevicePath is\r | |
771 | multi-instance.\r | |
772 | Otherwise, FALSE is returned.\r | |
773 | If DevicePath is NULL or invalid, then FALSE is returned.\r | |
774 | \r | |
775 | @param DevicePath A pointer to a device path data structure.\r | |
776 | \r | |
777 | @retval TRUE DevicePath is multi-instance.\r | |
9095d37b | 778 | @retval FALSE DevicePath is not multi-instance, or DevicePath\r |
4d0a30a4 RN |
779 | is NULL or invalid.\r |
780 | \r | |
781 | **/\r | |
782 | BOOLEAN\r | |
783 | EFIAPI\r | |
784 | UefiDevicePathLibIsDevicePathMultiInstance (\r | |
785 | IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r | |
786 | )\r | |
787 | {\r | |
2f88bd3a | 788 | CONST EFI_DEVICE_PATH_PROTOCOL *Node;\r |
4d0a30a4 RN |
789 | \r |
790 | if (DevicePath == NULL) {\r | |
791 | return FALSE;\r | |
792 | }\r | |
793 | \r | |
794 | if (!IsDevicePathValid (DevicePath, 0)) {\r | |
795 | return FALSE;\r | |
796 | }\r | |
797 | \r | |
798 | Node = DevicePath;\r | |
799 | while (!IsDevicePathEnd (Node)) {\r | |
800 | if (IsDevicePathEndInstance (Node)) {\r | |
801 | return TRUE;\r | |
802 | }\r | |
803 | \r | |
804 | Node = NextDevicePathNode (Node);\r | |
805 | }\r | |
806 | \r | |
807 | return FALSE;\r | |
808 | }\r | |
809 | \r | |
4d0a30a4 RN |
810 | /**\r |
811 | Allocates a device path for a file and appends it to an existing device path.\r | |
812 | \r | |
813 | If Device is a valid device handle that contains a device path protocol, then a device path for\r | |
814 | the file specified by FileName is allocated and appended to the device path associated with the\r | |
815 | handle Device. The allocated device path is returned. If Device is NULL or Device is a handle\r | |
816 | that does not support the device path protocol, then a device path containing a single device\r | |
817 | path node for the file specified by FileName is allocated and returned.\r | |
818 | The memory for the new device path is allocated from EFI boot services memory. It is the responsibility\r | |
819 | of the caller to free the memory allocated.\r | |
9095d37b | 820 | \r |
4d0a30a4 RN |
821 | If FileName is NULL, then ASSERT().\r |
822 | If FileName is not aligned on a 16-bit boundary, then ASSERT().\r | |
823 | \r | |
9095d37b | 824 | @param Device A pointer to a device handle. This parameter\r |
4d0a30a4 RN |
825 | is optional and may be NULL.\r |
826 | @param FileName A pointer to a Null-terminated Unicode string.\r | |
827 | \r | |
828 | @return The allocated device path.\r | |
829 | \r | |
830 | **/\r | |
831 | EFI_DEVICE_PATH_PROTOCOL *\r | |
832 | EFIAPI\r | |
833 | FileDevicePath (\r | |
2f88bd3a MK |
834 | IN EFI_HANDLE Device OPTIONAL,\r |
835 | IN CONST CHAR16 *FileName\r | |
4d0a30a4 RN |
836 | )\r |
837 | {\r | |
838 | UINTN Size;\r | |
839 | FILEPATH_DEVICE_PATH *FilePath;\r | |
840 | EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r | |
841 | EFI_DEVICE_PATH_PROTOCOL *FileDevicePath;\r | |
842 | \r | |
843 | DevicePath = NULL;\r | |
844 | \r | |
2f88bd3a | 845 | Size = StrSize (FileName);\r |
4d0a30a4 RN |
846 | FileDevicePath = AllocatePool (Size + SIZE_OF_FILEPATH_DEVICE_PATH + END_DEVICE_PATH_LENGTH);\r |
847 | if (FileDevicePath != NULL) {\r | |
2f88bd3a | 848 | FilePath = (FILEPATH_DEVICE_PATH *)FileDevicePath;\r |
4d0a30a4 RN |
849 | FilePath->Header.Type = MEDIA_DEVICE_PATH;\r |
850 | FilePath->Header.SubType = MEDIA_FILEPATH_DP;\r | |
851 | CopyMem (&FilePath->PathName, FileName, Size);\r | |
852 | SetDevicePathNodeLength (&FilePath->Header, Size + SIZE_OF_FILEPATH_DEVICE_PATH);\r | |
853 | SetDevicePathEndNode (NextDevicePathNode (&FilePath->Header));\r | |
854 | \r | |
855 | if (Device != NULL) {\r | |
856 | DevicePath = DevicePathFromHandle (Device);\r | |
857 | }\r | |
858 | \r | |
859 | DevicePath = AppendDevicePath (DevicePath, FileDevicePath);\r | |
860 | FreePool (FileDevicePath);\r | |
861 | }\r | |
862 | \r | |
863 | return DevicePath;\r | |
864 | }\r |