]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/IScsiDxe/IScsiMisc.h
NetworkPkg/IScsiDxe: fix IScsiHexToBin() buffer overflow
[mirror_edk2.git] / NetworkPkg / IScsiDxe / IScsiMisc.h
1 /** @file
2 Miscellaneous definitions for iSCSI driver.
3
4 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #ifndef _ISCSI_MISC_H_
10 #define _ISCSI_MISC_H_
11
12 typedef struct _ISCSI_DRIVER_DATA ISCSI_DRIVER_DATA;
13
14 ///
15 /// IPv4 Device Path Node Length
16 ///
17 #define IP4_NODE_LEN_NEW_VERSIONS 27
18
19 ///
20 /// IPv6 Device Path Node Length
21 ///
22 #define IP6_NODE_LEN_OLD_VERSIONS 43
23 #define IP6_NODE_LEN_NEW_VERSIONS 60
24
25 ///
26 /// The ignored field StaticIpAddress's offset in old IPv6 Device Path
27 ///
28 #define IP6_OLD_IPADDRESS_OFFSET 42
29
30
31 #pragma pack(1)
32 typedef struct _ISCSI_SESSION_CONFIG_NVDATA {
33 UINT16 TargetPort;
34 UINT8 Enabled;
35 UINT8 IpMode;
36
37 EFI_IP_ADDRESS LocalIp;
38 EFI_IPv4_ADDRESS SubnetMask;
39 EFI_IP_ADDRESS Gateway;
40
41 BOOLEAN InitiatorInfoFromDhcp;
42 BOOLEAN TargetInfoFromDhcp;
43
44 CHAR8 TargetName[ISCSI_NAME_MAX_SIZE];
45 EFI_IP_ADDRESS TargetIp;
46 UINT8 PrefixLength;
47 UINT8 BootLun[8];
48
49 UINT16 ConnectTimeout; ///< timeout value in milliseconds.
50 UINT8 ConnectRetryCount;
51 UINT8 IsId[6];
52
53 BOOLEAN RedirectFlag;
54 UINT16 OriginalTargetPort; // The port of proxy/virtual target.
55 EFI_IP_ADDRESS OriginalTargetIp; // The address of proxy/virtual target.
56
57 BOOLEAN DnsMode; // Flag indicate whether the Target address is expressed as URL format.
58 CHAR8 TargetUrl[ISCSI_TARGET_URI_MAX_SIZE];
59
60 } ISCSI_SESSION_CONFIG_NVDATA;
61 #pragma pack()
62
63 /**
64 Calculate the prefix length of the IPv4 subnet mask.
65
66 @param[in] SubnetMask The IPv4 subnet mask.
67
68 @return The prefix length of the subnet mask.
69 @retval 0 Other errors as indicated.
70
71 **/
72 UINT8
73 IScsiGetSubnetMaskPrefixLength (
74 IN EFI_IPv4_ADDRESS *SubnetMask
75 );
76
77 /**
78 Convert the hexadecimal encoded LUN string into the 64-bit LUN.
79
80 @param[in] Str The hexadecimal encoded LUN string.
81 @param[out] Lun Storage to return the 64-bit LUN.
82
83 @retval EFI_SUCCESS The 64-bit LUN is stored in Lun.
84 @retval EFI_INVALID_PARAMETER The string is malformatted.
85
86 **/
87 EFI_STATUS
88 IScsiAsciiStrToLun (
89 IN CHAR8 *Str,
90 OUT UINT8 *Lun
91 );
92
93 /**
94 Convert the 64-bit LUN into the hexadecimal encoded LUN string.
95
96 @param[in] Lun The 64-bit LUN.
97 @param[out] String The storage to return the hexadecimal encoded LUN string.
98
99 **/
100 VOID
101 IScsiLunToUnicodeStr (
102 IN UINT8 *Lun,
103 OUT CHAR16 *String
104 );
105
106 /**
107 Convert the mac address into a hexadecimal encoded "-" separated string.
108
109 @param[in] Mac The mac address.
110 @param[in] Len Length in bytes of the mac address.
111 @param[in] VlanId VLAN ID of the network device.
112 @param[out] Str The storage to return the mac string.
113
114 **/
115 VOID
116 IScsiMacAddrToStr (
117 IN EFI_MAC_ADDRESS *Mac,
118 IN UINT32 Len,
119 IN UINT16 VlanId,
120 OUT CHAR16 *Str
121 );
122
123 /**
124 Convert the formatted IP address into the binary IP address.
125
126 @param[in] Str The UNICODE string.
127 @param[in] IpMode Indicates whether the IP address is v4 or v6.
128 @param[out] Ip The storage to return the ASCII string.
129
130 @retval EFI_SUCCESS The binary IP address is returned in Ip.
131 @retval EFI_INVALID_PARAMETER The IP string is malformatted or IpMode is
132 invalid.
133
134 **/
135 EFI_STATUS
136 IScsiAsciiStrToIp (
137 IN CHAR8 *Str,
138 IN UINT8 IpMode,
139 OUT EFI_IP_ADDRESS *Ip
140 );
141
142 /**
143 Convert the binary encoded buffer into a hexadecimal encoded string.
144
145 @param[in] BinBuffer The buffer containing the binary data.
146 @param[in] BinLength Length of the binary buffer.
147 @param[in, out] HexStr Pointer to the string.
148 @param[in, out] HexLength The length of the string.
149
150 @retval EFI_SUCCESS The binary data is converted to the hexadecimal string
151 and the length of the string is updated.
152 @retval EFI_BUFFER_TOO_SMALL The string is too small.
153 @retval EFI_BAD_BUFFER_SIZE BinLength is too large for hex encoding.
154 @retval EFI_INVALID_PARAMETER The IP string is malformatted.
155
156 **/
157 EFI_STATUS
158 IScsiBinToHex (
159 IN UINT8 *BinBuffer,
160 IN UINT32 BinLength,
161 IN OUT CHAR8 *HexStr,
162 IN OUT UINT32 *HexLength
163 );
164
165 /**
166 Convert the hexadecimal string into a binary encoded buffer.
167
168 @param[in, out] BinBuffer The binary buffer.
169 @param[in, out] BinLength Length of the binary buffer.
170 @param[in] HexStr The hexadecimal string.
171
172 @retval EFI_SUCCESS The hexadecimal string is converted into a
173 binary encoded buffer.
174 @retval EFI_INVALID_PARAMETER Invalid hex encoding found in HexStr.
175 @retval EFI_BAD_BUFFER_SIZE The length of HexStr is too large for decoding:
176 the decoded size cannot be expressed in
177 BinLength on output.
178 @retval EFI_BUFFER_TOO_SMALL The binary buffer is too small to hold the
179 converted data.
180 **/
181 EFI_STATUS
182 IScsiHexToBin (
183 IN OUT UINT8 *BinBuffer,
184 IN OUT UINT32 *BinLength,
185 IN CHAR8 *HexStr
186 );
187
188
189 /**
190 Convert the decimal-constant string or hex-constant string into a numerical value.
191
192 @param[in] Str String in decimal or hex.
193
194 @return The numerical value.
195
196 **/
197 UINTN
198 IScsiNetNtoi (
199 IN CHAR8 *Str
200 );
201
202 /**
203 Generate random numbers.
204
205 @param[in, out] Rand The buffer to contain random numbers.
206 @param[in] RandLength The length of the Rand buffer.
207
208 **/
209 VOID
210 IScsiGenRandom (
211 IN OUT UINT8 *Rand,
212 IN UINTN RandLength
213 );
214
215 /**
216 Record the NIC information in a global structure.
217
218 @param[in] Controller The handle of the controller.
219 @param[in] Image Handle of the image.
220
221 @retval EFI_SUCCESS The operation is completed.
222 @retval EFI_OUT_OF_RESOURCES Do not have sufficient resource to finish this
223 operation.
224
225 **/
226 EFI_STATUS
227 IScsiAddNic (
228 IN EFI_HANDLE Controller,
229 IN EFI_HANDLE Image
230 );
231
232 /**
233 Delete the recorded NIC information from a global structure. Also delete corresponding
234 attempts.
235
236 @param[in] Controller The handle of the controller.
237
238 @retval EFI_SUCCESS The operation completed.
239 @retval EFI_NOT_FOUND The NIC information to be deleted is not recorded.
240
241 **/
242 EFI_STATUS
243 IScsiRemoveNic (
244 IN EFI_HANDLE Controller
245 );
246
247 /**
248 Create and initialize the Attempts.
249
250 @param[in] AttemptNum The number of Attempts will be created.
251
252 @retval EFI_SUCCESS The Attempts have been created successfully.
253 @retval Others Failed to create the Attempt.
254
255 **/
256 EFI_STATUS
257 IScsiCreateAttempts (
258 IN UINTN AttemptNum
259 );
260
261 /**
262 Create the iSCSI configuration Keywords for each attempt.
263
264 @param[in] KeywordNum The number Sets of Keywords will be created.
265
266 @retval EFI_SUCCESS The operation is completed.
267 @retval Others Failed to create the Keywords.
268
269 **/
270 EFI_STATUS
271 IScsiCreateKeywords (
272 IN UINTN KeywordNum
273 );
274
275 /**
276
277 Free the attempt configure data variable.
278
279 **/
280 VOID
281 IScsiCleanAttemptVariable (
282 IN VOID
283 );
284
285 /**
286 Get the recorded NIC information from a global structure by the Index.
287
288 @param[in] NicIndex The index indicates the position of NIC info.
289
290 @return Pointer to the NIC info or NULL if not found.
291
292 **/
293 ISCSI_NIC_INFO *
294 IScsiGetNicInfoByIndex (
295 IN UINT8 NicIndex
296 );
297
298
299 /**
300 Get the NIC's PCI location and return it according to the composited
301 format defined in iSCSI Boot Firmware Table.
302
303 @param[in] Controller The handle of the controller.
304 @param[out] Bus The bus number.
305 @param[out] Device The device number.
306 @param[out] Function The function number.
307
308 @return The composited representation of the NIC PCI location.
309
310 **/
311 UINT16
312 IScsiGetNICPciLocation (
313 IN EFI_HANDLE Controller,
314 OUT UINTN *Bus,
315 OUT UINTN *Device,
316 OUT UINTN *Function
317 );
318
319 /**
320 Read the EFI variable (VendorGuid/Name) and return a dynamically allocated
321 buffer, and the size of the buffer. If failure, return NULL.
322
323 @param[in] Name String part of EFI variable name.
324 @param[in] VendorGuid GUID part of EFI variable name.
325 @param[out] VariableSize Returns the size of the EFI variable that was read.
326
327 @return Dynamically allocated memory that contains a copy of the EFI variable.
328 @return Caller is responsible freeing the buffer.
329 @retval NULL Variable was not read.
330
331 **/
332 VOID *
333 IScsiGetVariableAndSize (
334 IN CHAR16 *Name,
335 IN EFI_GUID *VendorGuid,
336 OUT UINTN *VariableSize
337 );
338
339 /**
340 Create the iSCSI driver data.
341
342 @param[in] Image The handle of the driver image.
343 @param[in] Controller The handle of the controller.
344
345 @return The iSCSI driver data created.
346 @retval NULL Other errors as indicated.
347
348 **/
349 ISCSI_DRIVER_DATA *
350 IScsiCreateDriverData (
351 IN EFI_HANDLE Image,
352 IN EFI_HANDLE Controller
353 );
354
355 /**
356 Clean the iSCSI driver data.
357
358 @param[in] Private The iSCSI driver data.
359
360 @retval EFI_SUCCESS The clean operation is successful.
361 @retval Others Other errors as indicated.
362
363 **/
364 EFI_STATUS
365 IScsiCleanDriverData (
366 IN ISCSI_DRIVER_DATA *Private
367 );
368
369 /**
370 Check wheather the Controller handle is configured to use DHCP protocol.
371
372 @param[in] Controller The handle of the controller.
373 @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6.
374
375 @retval TRUE The handle of the controller need the Dhcp protocol.
376 @retval FALSE The handle of the controller does not need the Dhcp protocol.
377
378 **/
379 BOOLEAN
380 IScsiDhcpIsConfigured (
381 IN EFI_HANDLE Controller,
382 IN UINT8 IpVersion
383 );
384
385 /**
386 Check wheather the Controller handle is configured to use DNS protocol.
387
388 @param[in] Controller The handle of the controller.
389
390 @retval TRUE The handle of the controller need the DNS protocol.
391 @retval FALSE The handle of the controller does not need the DNS protocol.
392
393 **/
394 BOOLEAN
395 IScsiDnsIsConfigured (
396 IN EFI_HANDLE Controller
397 );
398
399 /**
400 Get the various configuration data of this iSCSI instance.
401
402 @param[in] Private The iSCSI driver data.
403
404 @retval EFI_SUCCESS Obtained the configuration of this instance.
405 @retval EFI_ABORTED The operation was aborted.
406 @retval Others Other errors as indicated.
407
408 **/
409 EFI_STATUS
410 IScsiGetConfigData (
411 IN ISCSI_DRIVER_DATA *Private
412 );
413
414 /**
415 Get the device path of the iSCSI tcp connection and update it.
416
417 @param[in] Session The iSCSI session data.
418
419 @return The updated device path.
420 @retval NULL Other errors as indicated.
421
422 **/
423 EFI_DEVICE_PATH_PROTOCOL *
424 IScsiGetTcpConnDevicePath (
425 IN ISCSI_SESSION *Session
426 );
427
428 /**
429 Abort the session when the transition from BS to RT is initiated.
430
431 @param[in] Event The event signaled.
432 @param[in] Context The iSCSI driver data.
433
434 **/
435 VOID
436 EFIAPI
437 IScsiOnExitBootService (
438 IN EFI_EVENT Event,
439 IN VOID *Context
440 );
441
442 /**
443 Tests whether a controller handle is being managed by IScsi driver.
444
445 This function tests whether the driver specified by DriverBindingHandle is
446 currently managing the controller specified by ControllerHandle. This test
447 is performed by evaluating if the protocol specified by ProtocolGuid is
448 present on ControllerHandle and is was opened by DriverBindingHandle and Nic
449 Device handle with an attribute of EFI_OPEN_PROTOCOL_BY_DRIVER.
450 If ProtocolGuid is NULL, then ASSERT().
451
452 @param ControllerHandle A handle for a controller to test.
453 @param DriverBindingHandle Specifies the driver binding handle for the
454 driver.
455 @param ProtocolGuid Specifies the protocol that the driver specified
456 by DriverBindingHandle opens in its Start()
457 function.
458
459 @retval EFI_SUCCESS ControllerHandle is managed by the driver
460 specified by DriverBindingHandle.
461 @retval EFI_UNSUPPORTED ControllerHandle is not managed by the driver
462 specified by DriverBindingHandle.
463
464 **/
465 EFI_STATUS
466 EFIAPI
467 IScsiTestManagedDevice (
468 IN EFI_HANDLE ControllerHandle,
469 IN EFI_HANDLE DriverBindingHandle,
470 IN EFI_GUID *ProtocolGuid
471 );
472 #endif