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