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