1 /** @file
2   Miscellaneous definitions for iSCSI driver.
3 
4 Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution.  The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9 
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 
13 **/
14 
15 #ifndef _ISCSI_MISC_H_
16 #define _ISCSI_MISC_H_
17 
18 #include <Library/BaseLib.h>
19 
20 typedef struct _ISCSI_SESSION_CONFIG_DATA ISCSI_SESSION_CONFIG_DATA;
21 
22 ///
23 /// IPv4 Device Path Node Length
24 ///
25 #define IP4_NODE_LEN_NEW_VERSIONS    27
26 
27 #pragma pack(1)
28 typedef struct {
29   BOOLEAN           Enabled;
30 
31   BOOLEAN           InitiatorInfoFromDhcp;
32   EFI_IPv4_ADDRESS  LocalIp;
33   EFI_IPv4_ADDRESS  SubnetMask;
34   EFI_IPv4_ADDRESS  Gateway;
35 
36   BOOLEAN           TargetInfoFromDhcp;
37   CHAR8             TargetName[ISCSI_NAME_MAX_SIZE];
38   EFI_IPv4_ADDRESS  TargetIp;
39   UINT16            TargetPort;
40   UINT8             BootLun[8];
41 
42   UINT8             IsId[6];
43 } ISCSI_SESSION_CONFIG_NVDATA;
44 #pragma pack()
45 
46 struct _ISCSI_SESSION_CONFIG_DATA {
47   ISCSI_SESSION_CONFIG_NVDATA NvData;
48 
49   EFI_IPv4_ADDRESS            PrimaryDns;
50   EFI_IPv4_ADDRESS            SecondaryDns;
51   EFI_IPv4_ADDRESS            DhcpServer;
52 };
53 
54 /**
55   Calculate the prefix length of the IPv4 subnet mask.
56 
57   @param[in]  SubnetMask The IPv4 subnet mask.
58 
59   @return The prefix length of the subnet mask.
60   @retval 0 Other errors as indicated.
61 **/
62 UINT8
63 IScsiGetSubnetMaskPrefixLength (
64   IN EFI_IPv4_ADDRESS  *SubnetMask
65   );
66 
67 /**
68   Convert the hexadecimal encoded LUN string into the 64-bit LUN.
69 
70   @param[in]   Str             The hexadecimal encoded LUN string.
71   @param[out]  Lun             Storage to return the 64-bit LUN.
72 
73   @retval EFI_SUCCESS           The 64-bit LUN is stored in Lun.
74   @retval EFI_INVALID_PARAMETER The string is malformatted.
75 **/
76 EFI_STATUS
77 IScsiAsciiStrToLun (
78   IN  CHAR8  *Str,
79   OUT UINT8  *Lun
80   );
81 
82 /**
83   Convert the 64-bit LUN into the hexadecimal encoded LUN string.
84 
85   @param[in]   Lun The 64-bit LUN.
86   @param[out]  Str The storage to return the hexadecimal encoded LUN string.
87 **/
88 VOID
89 IScsiLunToUnicodeStr (
90   IN UINT8    *Lun,
91   OUT CHAR16  *Str
92   );
93 
94 /**
95   Convert the ASCII string into a UNICODE string.
96 
97   @param[in]   Source      The ASCII string.
98   @param[out]  Destination The storage to return the UNICODE string.
99 
100   @return CHAR16 *         Pointer to the UNICODE string.
101 **/
102 CHAR16 *
103 IScsiAsciiStrToUnicodeStr (
104   IN  CHAR8   *Source,
105   OUT CHAR16  *Destination
106   );
107 
108 /**
109   Convert the UNICODE string into an ASCII string.
110 
111   @param[in]  Source       The UNICODE string.
112   @param[out] Destination  The storage to return the ASCII string.
113 
114   @return CHAR8 *          Pointer to the ASCII string.
115 **/
116 CHAR8 *
117 IScsiUnicodeStrToAsciiStr (
118   IN  CHAR16  *Source,
119   OUT CHAR8   *Destination
120   );
121 
122 /**
123   Convert the mac address into a hexadecimal encoded "-" seperated string.
124 
125   @param[in]  Mac     The mac address.
126   @param[in]  Len     Length in bytes of the mac address.
127   @param[in]  VlanId  VLAN ID of the network device.
128   @param[out] Str     The storage to return the mac string.
129 **/
130 VOID
131 IScsiMacAddrToStr (
132   IN  EFI_MAC_ADDRESS  *Mac,
133   IN  UINT32           Len,
134   IN  UINT16           VlanId,
135   OUT CHAR16           *Str
136   );
137 
138 /**
139   Convert the decimal dotted IPv4 address into the binary IPv4 address.
140 
141   @param[in]   Str             The UNICODE string.
142   @param[out]  Ip              The storage to return the ASCII string.
143 
144   @retval EFI_SUCCESS           The binary IP address is returned in Ip.
145   @retval EFI_INVALID_PARAMETER The IP string is malformatted.
146 **/
147 EFI_STATUS
148 IScsiAsciiStrToIp (
149   IN  CHAR8             *Str,
150   OUT EFI_IPv4_ADDRESS  *Ip
151   );
152 
153 /**
154   Convert the binary encoded buffer into a hexadecimal encoded string.
155 
156   @param[in]       BinBuffer   The buffer containing the binary data.
157   @param[in]       BinLength   Length of the binary buffer.
158   @param[in, out]  HexStr      Pointer to the string.
159   @param[in, out]  HexLength   The length of the string.
160 
161   @retval EFI_SUCCESS          The binary data is converted to the hexadecimal string
162                                and the length of the string is updated.
163   @retval EFI_BUFFER_TOO_SMALL The string is too small.
164   @retval EFI_INVALID_PARAMETER The IP string is malformatted.
165 **/
166 EFI_STATUS
167 IScsiBinToHex (
168   IN     UINT8  *BinBuffer,
169   IN     UINT32 BinLength,
170   IN OUT CHAR8  *HexStr,
171   IN OUT UINT32 *HexLength
172   );
173 
174 /**
175   Convert the hexadecimal string into a binary encoded buffer.
176 
177   @param[in, out]  BinBuffer   The binary buffer.
178   @param[in, out]  BinLength   Length of the binary buffer.
179   @param[in]       HexStr      The hexadecimal string.
180 
181   @retval EFI_SUCCESS          The hexadecimal string is converted into a binary
182                                encoded buffer.
183   @retval EFI_BUFFER_TOO_SMALL The binary buffer is too small to hold the converted data.
184 **/
185 EFI_STATUS
186 IScsiHexToBin (
187   IN OUT UINT8  *BinBuffer,
188   IN OUT UINT32 *BinLength,
189   IN     CHAR8  *HexStr
190   );
191 
192 /**
193   Generate random numbers.
194 
195   @param[in, out]  Rand       The buffer to contain random numbers.
196   @param[in]       RandLength The length of the Rand buffer.
197 **/
198 VOID
199 IScsiGenRandom (
200   IN OUT UINT8  *Rand,
201   IN     UINTN  RandLength
202   );
203 
204 /**
205   Create the iSCSI driver data..
206 
207   @param[in] Image      The handle of the driver image.
208   @param[in] Controller The handle of the controller.
209 
210   @return The iSCSI driver data created.
211   @retval NULL Other errors as indicated.
212 **/
213 ISCSI_DRIVER_DATA *
214 IScsiCreateDriverData (
215   IN EFI_HANDLE  Image,
216   IN EFI_HANDLE  Controller
217   );
218 
219 /**
220   Clean the iSCSI driver data.
221 
222   @param[in]               Private The iSCSI driver data.
223 
224   @retval EFI_SUCCES       The clean operation is successful.
225   @retval Others           Other errors as indicated.
226 **/
227 EFI_STATUS
228 IScsiCleanDriverData (
229   IN ISCSI_DRIVER_DATA  *Private
230   );
231 
232 /**
233   Check wheather the Controller is configured to use DHCP protocol.
234 
235   @param[in]  Controller           The handle of the controller.
236 
237   @retval TRUE                     The handle of the controller need the Dhcp protocol.
238   @retval FALSE                    The handle of the controller does not need the Dhcp protocol.
239 
240 **/
241 BOOLEAN
242 IScsiDhcpIsConfigured (
243   IN EFI_HANDLE  Controller
244   );
245 
246 /**
247   Get the various configuration data of this iSCSI instance.
248 
249   @param[in]  Private   The iSCSI driver data.
250 
251   @retval EFI_SUCCESS   The configuration of this instance is got.
252   @retval EFI_ABORTED   The operation was aborted.
253   @retval Others        Other errors as indicated.
254 **/
255 EFI_STATUS
256 IScsiGetConfigData (
257   IN ISCSI_DRIVER_DATA  *Private
258   );
259 
260 /**
261   Get the device path of the iSCSI tcp connection and update it.
262 
263   @param[in]  Private The iSCSI driver data.
264 
265   @return The updated device path.
266   @retval NULL Other errors as indicated.
267 **/
268 EFI_DEVICE_PATH_PROTOCOL *
269 IScsiGetTcpConnDevicePath (
270   IN ISCSI_DRIVER_DATA  *Private
271   );
272 
273 /**
274   Abort the session when the transition from BS to RT is initiated.
275 
276   @param[in]   Event  The event signaled.
277   @param[in]  Context The iSCSI driver data.
278 **/
279 VOID
280 EFIAPI
281 IScsiOnExitBootService (
282   IN EFI_EVENT  Event,
283   IN VOID       *Context
284   );
285 
286 /**
287   Tests whether a controller handle is being managed by IScsi driver.
288 
289   This function tests whether the driver specified by DriverBindingHandle is
290   currently managing the controller specified by ControllerHandle.  This test
291   is performed by evaluating if the the protocol specified by ProtocolGuid is
292   present on ControllerHandle and is was opened by DriverBindingHandle and Nic
293   Device handle with an attribute of EFI_OPEN_PROTOCOL_BY_DRIVER.
294   If ProtocolGuid is NULL, then ASSERT().
295 
296   @param  ControllerHandle     A handle for a controller to test.
297   @param  DriverBindingHandle  Specifies the driver binding handle for the
298                                driver.
299   @param  ProtocolGuid         Specifies the protocol that the driver specified
300                                by DriverBindingHandle opens in its Start()
301                                function.
302 
303   @retval EFI_SUCCESS          ControllerHandle is managed by the driver
304                                specified by DriverBindingHandle.
305   @retval EFI_UNSUPPORTED      ControllerHandle is not managed by the driver
306                                specified by DriverBindingHandle.
307 
308 **/
309 EFI_STATUS
310 EFIAPI
311 IScsiTestManagedDevice (
312   IN  EFI_HANDLE       ControllerHandle,
313   IN  EFI_HANDLE       DriverBindingHandle,
314   IN  EFI_GUID         *ProtocolGuid
315   );
316 
317 #endif
318