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