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