]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/UefiDevicePathLibDevicePathProtocol/UefiDevicePathLib.c
Fix bug in backward copy for x64
[mirror_edk2.git] / MdePkg / Library / UefiDevicePathLibDevicePathProtocol / UefiDevicePathLib.c
CommitLineData
e386b444 1/** @file\r
2 UEFI Device Path Library.\r
3\r
4 Copyright (c) 2006, Intel Corporation<BR>\r
5 All rights reserved. This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
e386b444 13**/\r
14\r
c892d846 15\r
c7d265a9 16#include <Uefi.h>\r
c892d846 17\r
c7d265a9 18#include <Protocol/DevicePathUtilities.h>\r
19#include <Protocol/DevicePath.h>\r
c892d846 20\r
c7d265a9 21#include <Library/DevicePathLib.h>\r
22#include <Library/DebugLib.h>\r
23#include <Library/BaseLib.h>\r
24#include <Library/MemoryAllocationLib.h>\r
25#include <Library/BaseMemoryLib.h>\r
26#include <Library/UefiBootServicesTableLib.h>\r
e386b444 27\r
a73480f6 28#include "UefiDevicePathLibInternal.h"\r
29\r
e386b444 30STATIC EFI_DEVICE_PATH_UTILITIES_PROTOCOL *mDevicePathUtilities = NULL;\r
31\r
32/**\r
33 The constructor function caches the pointer to DevicePathUtilites protocol.\r
34 \r
35 The constructor function locates DevicePathUtilities protocol from protocol database.\r
36 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS. \r
37\r
38 @param ImageHandle The firmware allocated handle for the EFI image.\r
39 @param SystemTable A pointer to the EFI System Table.\r
40 \r
41 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r
42\r
43**/\r
44EFI_STATUS\r
45EFIAPI\r
46DevicePathLibConstructor (\r
47 IN EFI_HANDLE ImageHandle,\r
48 IN EFI_SYSTEM_TABLE *SystemTable\r
49 )\r
50{\r
51 EFI_STATUS Status;\r
52\r
53 Status = gBS->LocateProtocol (\r
54 &gEfiDevicePathUtilitiesProtocolGuid,\r
55 NULL,\r
56 (VOID**) &mDevicePathUtilities\r
57 );\r
58 ASSERT_EFI_ERROR (Status);\r
59 ASSERT (mDevicePathUtilities != NULL);\r
60\r
61 return Status;\r
62}\r
63\r
64/**\r
65 Returns the size of a device path in bytes.\r
66\r
67 This function returns the size, in bytes, of the device path data structure specified by\r
68 DevicePath including the end of device path node. If DevicePath is NULL, then 0 is returned.\r
69\r
70 @param DevicePath A pointer to a device path data structure.\r
71\r
72 @return The size of a device path in bytes.\r
73\r
74**/\r
75UINTN\r
76EFIAPI\r
77GetDevicePathSize (\r
78 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
79 )\r
80{\r
81 return mDevicePathUtilities->GetDevicePathSize (DevicePath);\r
82}\r
83\r
84/**\r
85 Creates a new device path by appending a second device path to a first device path.\r
86\r
87 This function allocates space for a new copy of the device path specified by DevicePath. If\r
88 DevicePath is NULL, then NULL is returned. If the memory is successfully allocated, then the\r
89 contents of DevicePath are copied to the newly allocated buffer, and a pointer to that buffer\r
90 is returned. Otherwise, NULL is returned. \r
91 \r
92 @param DevicePath A pointer to a device path data structure.\r
93\r
94 @return A pointer to the duplicated device path.\r
95\r
96**/\r
97EFI_DEVICE_PATH_PROTOCOL *\r
98EFIAPI\r
99DuplicateDevicePath (\r
100 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
101 )\r
102{\r
103 return mDevicePathUtilities->DuplicateDevicePath (DevicePath);\r
104}\r
105\r
106/**\r
107 Creates a new device path by appending a second device path to a first device path.\r
108\r
109 This function creates a new device path by appending a copy of SecondDevicePath to a copy of\r
110 FirstDevicePath in a newly allocated buffer. Only the end-of-device-path device node from\r
111 SecondDevicePath is retained. The newly created device path is returned. \r
112 If FirstDevicePath is NULL, then it is ignored, and a duplicate of SecondDevicePath is returned. \r
113 If SecondDevicePath is NULL, then it is ignored, and a duplicate of FirstDevicePath is returned. \r
114 If both FirstDevicePath and SecondDevicePath are NULL, then NULL is returned. \r
115 If there is not enough memory for the newly allocated buffer, then NULL is returned.\r
116 The memory for the new device path is allocated from EFI boot services memory. It is the\r
117 responsibility of the caller to free the memory allocated.\r
118\r
119 @param FirstDevicePath A pointer to a device path data structure.\r
120 @param SecondDevicePath A pointer to a device path data structure.\r
121\r
122 @return A pointer to the new device path.\r
123\r
124**/\r
125EFI_DEVICE_PATH_PROTOCOL *\r
126EFIAPI\r
127AppendDevicePath (\r
128 IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath, OPTIONAL\r
129 IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath OPTIONAL\r
130 )\r
131{\r
132 return mDevicePathUtilities->AppendDevicePath (FirstDevicePath, SecondDevicePath);\r
133}\r
134\r
135/**\r
136 Creates a new path by appending the device node to the device path.\r
137\r
138 This function creates a new device path by appending a copy of the device node specified by\r
139 DevicePathNode to a copy of the device path specified by DevicePath in an allocated buffer.\r
140 The end-of-device-path device node is moved after the end of the appended device node.\r
141 If DevicePath is NULL, then NULL is returned.\r
142 If DevicePathNode is NULL, then NULL is returned.\r
143 If there is not enough memory to allocate space for the new device path, then NULL is returned. \r
144 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to\r
145 free the memory allocated.\r
146\r
147 @param DevicePath A pointer to a device path data structure.\r
148 @param DevicePathNode A pointer to a single device path node.\r
149\r
150 @return A pointer to the new device path.\r
151\r
152**/\r
153EFI_DEVICE_PATH_PROTOCOL *\r
154EFIAPI\r
155AppendDevicePathNode (\r
156 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL\r
157 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathNode OPTIONAL\r
158 )\r
159{\r
160 return mDevicePathUtilities->AppendDeviceNode (DevicePath, DevicePathNode);\r
161}\r
162\r
163/**\r
164 Creates a new device path by appending the specified device path instance to the specified device\r
165 path.\r
166 \r
167 This function creates a new device path by appending a copy of the device path instance specified\r
168 by DevicePathInstance to a copy of the device path secified by DevicePath in a allocated buffer.\r
169 The end-of-device-path device node is moved after the end of the appended device path instance\r
170 and a new end-of-device-path-instance node is inserted between. \r
171 If DevicePath is NULL, then a copy if DevicePathInstance is returned.\r
172 If DevicePathInstance is NULL, then NULL is returned.\r
173 If there is not enough memory to allocate space for the new device path, then NULL is returned. \r
174 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to\r
175 free the memory allocated.\r
176 \r
177 @param DevicePath A pointer to a device path data structure.\r
178 @param DevicePathInstance A pointer to a device path instance.\r
179\r
180 @return A pointer to the new device path.\r
181\r
182**/\r
183EFI_DEVICE_PATH_PROTOCOL *\r
184EFIAPI\r
185AppendDevicePathInstance (\r
186 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL\r
187 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathInstance OPTIONAL\r
188 )\r
189{\r
190 return mDevicePathUtilities->AppendDevicePathInstance (DevicePath, DevicePathInstance);\r
191}\r
192\r
193/**\r
194 Creates a copy of the current device path instance and returns a pointer to the next device path\r
195 instance.\r
196\r
197 This function creates a copy of the current device path instance. It also updates DevicePath to\r
198 point to the next device path instance in the device path (or NULL if no more) and updates Size\r
199 to hold the size of the device path instance copy.\r
200 If DevicePath is NULL, then NULL is returned.\r
201 If there is not enough memory to allocate space for the new device path, then NULL is returned. \r
202 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to\r
203 free the memory allocated.\r
204 If Size is NULL, then ASSERT().\r
205 \r
206 @param DevicePath On input, this holds the pointer to the current device path\r
207 instance. On output, this holds the pointer to the next device\r
208 path instance or NULL if there are no more device path\r
209 instances in the device path pointer to a device path data\r
210 structure.\r
211 @param Size On output, this holds the size of the device path instance, in\r
212 bytes or zero, if DevicePath is NULL.\r
213\r
214 @return A pointer to the current device path instance.\r
215\r
216**/\r
217EFI_DEVICE_PATH_PROTOCOL *\r
218EFIAPI\r
219GetNextDevicePathInstance (\r
220 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath,\r
221 OUT UINTN *Size\r
222 )\r
223{\r
224 ASSERT (Size != NULL);\r
225 return mDevicePathUtilities->GetNextDevicePathInstance (DevicePath, Size);\r
226}\r
227\r
228/**\r
229 Creates a copy of the current device path instance and returns a pointer to the next device path\r
230 instance.\r
231\r
232 This function creates a new device node in a newly allocated buffer of size NodeLength and\r
233 initializes the device path node header with NodeType and NodeSubType. The new device path node\r
234 is returned.\r
235 If NodeLength is smaller than a device path header, then NULL is returned. \r
236 If there is not enough memory to allocate space for the new device path, then NULL is returned. \r
237 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to\r
238 free the memory allocated.\r
239\r
240 @param NodeType The device node type for the new device node.\r
241 @param NodeSubType The device node sub-type for the new device node.\r
242 @param NodeLength The length of the new device node.\r
243\r
244 @return The new device path.\r
245\r
246**/\r
247EFI_DEVICE_PATH_PROTOCOL *\r
248EFIAPI\r
249CreateDeviceNode (\r
250 IN UINT8 NodeType,\r
251 IN UINT8 NodeSubType,\r
252 IN UINT16 NodeLength\r
253 )\r
254{\r
255 return mDevicePathUtilities->CreateDeviceNode (NodeType, NodeSubType, NodeLength);\r
256}\r
257\r
258/**\r
259 Determines if a device path is single or multi-instance.\r
260\r
261 This function returns TRUE if the device path specified by DevicePath is multi-instance.\r
262 Otherwise, FALSE is returned. If DevicePath is NULL, then FALSE is returned.\r
263\r
264 @param DevicePath A pointer to a device path data structure.\r
265\r
266 @retval TRUE DevicePath is multi-instance.\r
267 @retval FALSE DevicePath is not multi-instance or DevicePath is NULL.\r
268\r
269**/\r
270BOOLEAN\r
271EFIAPI\r
272IsDevicePathMultiInstance (\r
273 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
274 )\r
275{\r
276 return mDevicePathUtilities->IsDevicePathMultiInstance (DevicePath);\r
277}\r
278\r
279/**\r
280 Retrieves the device path protocol from a handle.\r
281\r
282 This function returns the device path protocol from the handle specified by Handle. If Handle is\r
283 NULL or Handle does not contain a device path protocol, then NULL is returned.\r
284 \r
285 @param Handle The handle from which to retrieve the device path protocol.\r
286\r
287 @return The device path protocol from the handle specified by Handle.\r
288\r
289**/\r
290EFI_DEVICE_PATH_PROTOCOL *\r
291EFIAPI\r
292DevicePathFromHandle (\r
293 IN EFI_HANDLE Handle\r
294 )\r
295{\r
296 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
297 EFI_STATUS Status;\r
298\r
299 Status = gBS->HandleProtocol (\r
300 Handle,\r
301 &gEfiDevicePathProtocolGuid,\r
302 (VOID *) &DevicePath\r
303 );\r
304 if (EFI_ERROR (Status)) {\r
305 DevicePath = NULL;\r
306 }\r
307 return DevicePath;\r
308}\r
309\r
310/**\r
311 Allocates a device path for a file and appends it to an existing device path.\r
312\r
313 If Device is a valid device handle that contains a device path protocol, then a device path for\r
314 the file specified by FileName is allocated and appended to the device path associated with the\r
315 handle Device. The allocated device path is returned. If Device is NULL or Device is a handle\r
316 that does not support the device path protocol, then a device path containing a single device\r
317 path node for the file specified by FileName is allocated and returned.\r
318 If FileName is NULL, then ASSERT().\r
319\r
320 @param Device A pointer to a device handle. This parameter is optional and\r
321 may be NULL.\r
322 @param FileName A pointer to a Null-terminated Unicode string.\r
323\r
324 @return The allocated device path.\r
325\r
326**/\r
327EFI_DEVICE_PATH_PROTOCOL *\r
328EFIAPI\r
329FileDevicePath (\r
330 IN EFI_HANDLE Device, OPTIONAL\r
331 IN CONST CHAR16 *FileName\r
332 )\r
333{\r
334 UINTN Size;\r
335 FILEPATH_DEVICE_PATH *FilePath;\r
336 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
337 EFI_DEVICE_PATH_PROTOCOL *FileDevicePath;\r
338\r
339 DevicePath = NULL;\r
340\r
341 Size = StrSize (FileName);\r
342 FileDevicePath = AllocatePool (Size + SIZE_OF_FILEPATH_DEVICE_PATH + EFI_END_DEVICE_PATH_LENGTH);\r
343 if (FileDevicePath != NULL) {\r
344 FilePath = (FILEPATH_DEVICE_PATH *) FileDevicePath;\r
345 FilePath->Header.Type = MEDIA_DEVICE_PATH;\r
346 FilePath->Header.SubType = MEDIA_FILEPATH_DP;\r
347 CopyMem (&FilePath->PathName, FileName, Size);\r
348 SetDevicePathNodeLength (&FilePath->Header, Size + SIZE_OF_FILEPATH_DEVICE_PATH);\r
349 SetDevicePathEndNode (NextDevicePathNode (&FilePath->Header));\r
350\r
351 if (Device != NULL) {\r
352 DevicePath = DevicePathFromHandle (Device);\r
353 }\r
354\r
355 DevicePath = AppendDevicePath (DevicePath, FileDevicePath);\r
356 FreePool (FileDevicePath);\r
357 }\r
358\r
359 return DevicePath;\r
360}\r
361\r