2 Miscellaneous routines for IScsi driver.
4 Copyright (c) 2004 - 2008, Intel Corporation
5 All rights reserved. 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
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.
19 Miscellaneous routines for IScsi driver.
23 #include "IScsiImpl.h"
25 CONST CHAR8 IScsiHexString
[] = "0123456789ABCDEFabcdef";
28 Removes (trims) specified leading and trailing characters from a string.
30 @param str[in][out] Pointer to the null-terminated string to be trimmed. On return,
31 str will hold the trimmed string.
33 @param CharC[in] Character will be trimmed from str.
52 // Trim off the leading and trailing characters c
54 for (p1
= str
; *p1
&& *p1
== CharC
; p1
++) {
74 for (p1
= str
+ StrLen(str
) - 1; p1
>= str
&& *p1
== CharC
; p1
--) {
77 if (p1
!= str
+ StrLen(str
) - 1) {
83 Calculate the prefix length of the IPv4 subnet mask.
85 @param SubnetMask[in] The IPv4 subnet mask.
87 @retval The prefix length of the subnet mask.
91 IScsiGetSubnetMaskPrefixLength (
92 IN EFI_IPv4_ADDRESS
*SubnetMask
99 // The SubnetMask is in network byte order.
101 ReverseMask
= (SubnetMask
->Addr
[0] << 24) | (SubnetMask
->Addr
[1] << 16) | (SubnetMask
->Addr
[2] << 8) | (SubnetMask
->Addr
[3]);
106 ReverseMask
= ~ReverseMask
;
108 if (ReverseMask
& (ReverseMask
+ 1)) {
114 while (ReverseMask
!= 0) {
115 ReverseMask
= ReverseMask
>> 1;
119 return (UINT8
) (32 - Len
);
123 Convert the hexadecimal encoded LUN string into the 64-bit LUN.
125 @param Str[in] The hexadecimal encoded LUN string.
127 @param Lun[out] Storage to return the 64-bit LUN.
129 @retval EFI_SUCCESS The 64-bit LUN is stored in Lun.
131 @retval EFI_INVALID_PARAMETER The string is malformatted.
141 CHAR8
*LunUnitStr
[4];
146 ZeroMem (LunUnitStr
, sizeof (LunUnitStr
));
151 if (!IsHexDigit ((UINT8
*) &Digit
, *Str
)) {
152 return EFI_INVALID_PARAMETER
;
155 while (*Str
!= '\0') {
157 // Legal representations of LUN:
158 // 4752-3A4F-6b7e-2F99,
166 if (*(Str
+ 1) != '\0') {
167 if (!IsHexDigit ((UINT8
*) &Digit
, *(Str
+ 1))) {
168 return EFI_INVALID_PARAMETER
;
171 LunUnitStr
[Index
] = Str
+ 1;
173 } else if (!IsHexDigit ((UINT8
*) &Digit
, *Str
)) {
174 return EFI_INVALID_PARAMETER
;
180 for (Index
= 0; (Index
< 4) && (LunUnitStr
[Index
] != NULL
); Index
++) {
181 if (AsciiStrLen (LunUnitStr
[Index
]) > 4) {
182 return EFI_INVALID_PARAMETER
;
185 Temp
= AsciiStrHexToUintn (LunUnitStr
[Index
]);
186 *((UINT16
*) &Lun
[Index
* 2]) = HTONS (Temp
);
193 Convert the 64-bit LUN into the hexadecimal encoded LUN string.
195 @param Lun[in] The 64-bit LUN.
197 @param Str[out] The storage to return the hexadecimal encoded LUN string.
203 IScsiLunToUnicodeStr (
213 for (Index
= 0; Index
< 4; Index
++) {
215 if ((Lun
[2 * Index
] | Lun
[2 * Index
+ 1]) == 0) {
216 StrCpy (TempStr
, L
"0-");
218 TempStr
[0] = (CHAR16
) IScsiHexString
[Lun
[2 * Index
] >> 4];
219 TempStr
[1] = (CHAR16
) IScsiHexString
[Lun
[2 * Index
] & 0xf];
220 TempStr
[2] = (CHAR16
) IScsiHexString
[Lun
[2 * Index
+ 1] >> 4];
221 TempStr
[3] = (CHAR16
) IScsiHexString
[Lun
[2 * Index
+ 1] & 0xf];
225 StrTrim (TempStr
, L
'0');
228 TempStr
+= StrLen (TempStr
);
231 Str
[StrLen (Str
) - 1] = 0;
233 for (Index
= StrLen (Str
) - 1; Index
> 1; Index
= Index
- 2) {
234 if ((Str
[Index
] == L
'0') && (Str
[Index
- 1] == L
'-')) {
243 Convert the ASCII string into a UNICODE string.
245 @param Source[out] The ASCII string.
247 @param Destination[out] The storage to return the UNICODE string.
249 @retval CHAR16 * Pointer to the UNICODE string.
253 IScsiAsciiStrToUnicodeStr (
255 OUT CHAR16
*Destination
258 ASSERT (Destination
!= NULL
);
259 ASSERT (Source
!= NULL
);
261 while (*Source
!= '\0') {
262 *(Destination
++) = (CHAR16
) *(Source
++);
271 Convert the UNICODE string into an ASCII string.
273 @param Source[in] The UNICODE string.
275 @param Destination[out] The storage to return the ASCII string.
277 @retval CHAR8 * Pointer to the ASCII string.
281 IScsiUnicodeStrToAsciiStr (
283 OUT CHAR8
*Destination
286 ASSERT (Destination
!= NULL
);
287 ASSERT (Source
!= NULL
);
289 while (*Source
!= '\0') {
291 // If any Unicode characters in Source contain
292 // non-zero value in the upper 8 bits, then ASSERT().
294 ASSERT (*Source
< 0x100);
295 *(Destination
++) = (CHAR8
) *(Source
++);
304 Convert the decimal dotted IPv4 address into the binary IPv4 address.
306 @param Str[in] The UNICODE string.
308 @param Ip[out] The storage to return the ASCII string.
310 @retval EFI_SUCCESS The binary IP address is returned in Ip.
312 @retval EFI_INVALID_PARAMETER The IP string is malformatted.
318 OUT EFI_IPv4_ADDRESS
*Ip
329 return EFI_INVALID_PARAMETER
;
333 while (NET_IS_DIGIT (*Str
)) {
334 Number
= Number
* 10 + (*Str
- '0');
339 return EFI_INVALID_PARAMETER
;
342 Ip
->Addr
[Index
] = (UINT8
) Number
;
344 if ((*Str
!= '\0') && (*Str
!= '.')) {
346 // The current character should be either the NULL terminator or
347 // the dot delimiter.
349 return EFI_INVALID_PARAMETER
;
354 // Skip the delimiter.
363 return EFI_INVALID_PARAMETER
;
370 Convert the mac address into a hexadecimal encoded "-" seperated string.
372 @param Mac[in] The mac address.
374 @param Len[in] Length in bytes of the mac address.
376 @param Str[out] The storage to return the mac string.
383 IN EFI_MAC_ADDRESS
*Mac
,
390 for (Index
= 0; Index
< Len
; Index
++) {
391 Str
[3 * Index
] = NibbleToHexChar ((UINT8
) (Mac
->Addr
[Index
] >> 4));
392 Str
[3 * Index
+ 1] = NibbleToHexChar (Mac
->Addr
[Index
]);
393 Str
[3 * Index
+ 2] = L
'-';
396 Str
[3 * Index
- 1] = L
'\0';
400 Convert the binary encoded buffer into a hexadecimal encoded string.
402 @param BinBuffer[in] The buffer containing the binary data.
404 @param BinLength[in] Length of the binary buffer.
406 @param HexStr[in][out] Pointer to the string.
408 @param HexLength[in][out] The length of the string.
410 @retval EFI_SUCCESS The binary data is converted to the hexadecimal string
411 and the length of the string is updated.
413 @retval EFI_BUFFER_TOO_SMALL The string is too small.
420 IN OUT CHAR8
*HexStr
,
421 IN OUT UINT32
*HexLength
426 if ((HexStr
== NULL
) || (BinBuffer
== NULL
) || (BinLength
== 0)) {
427 return EFI_INVALID_PARAMETER
;
430 if (((*HexLength
) - 3) < BinLength
* 2) {
431 *HexLength
= BinLength
* 2 + 3;
432 return EFI_BUFFER_TOO_SMALL
;
435 *HexLength
= BinLength
* 2 + 3;
437 // Prefix for Hex String
442 for (Index
= 0; Index
< BinLength
; Index
++) {
443 HexStr
[Index
* 2 + 2] = IScsiHexString
[BinBuffer
[Index
] >> 4];
444 HexStr
[Index
* 2 + 3] = IScsiHexString
[BinBuffer
[Index
] & 0xf];
447 HexStr
[Index
* 2 + 2] = '\0';
453 Convert the hexadecimal string into a binary encoded buffer.
455 @param BinBuffer[in][out] The binary buffer.
457 @param BinLength[in][out] Length of the binary buffer.
459 @param HexStr[in] The hexadecimal string.
461 @retval EFI_SUCCESS The hexadecimal string is converted into a binary
464 @retval EFI_BUFFER_TOO_SMALL The binary buffer is too small to hold the converted data.s
469 IN OUT UINT8
*BinBuffer
,
470 IN OUT UINT32
*BinLength
,
483 // Find out how many hex characters the string has.
486 if ((HexBuf
[0] == '0') && ((HexBuf
[1] == 'x') || (HexBuf
[1] == 'X'))) {
490 for (Index
= 0, HexCount
= 0; IsHexDigit (&Digit
, HexBuf
[Index
]); Index
++, HexCount
++)
498 // Test if buffer is passed enough.
500 if (((HexCount
+ 1) / 2) > *BinLength
) {
501 *BinLength
= (HexCount
+ 1) / 2;
502 return EFI_BUFFER_TOO_SMALL
;
505 *BinLength
= (HexCount
+ 1) / 2;
507 for (Index
= 0; Index
< HexCount
; Index
++) {
509 IsHexDigit (&Digit
, HexBuf
[HexCount
- 1 - Index
]);
511 if ((Index
& 1) == 0) {
514 Byte
= BinBuffer
[*BinLength
- 1 - Index
/ 2];
516 Byte
= (UINT8
) (Byte
| (Digit
<< 4));
519 BinBuffer
[*BinLength
- 1 - Index
/ 2] = Byte
;
526 Generate random numbers.
528 @param Rand[in][out] The buffer to contain random numbers.
530 @param RandLength[in] The length of the Rand buffer.
543 while (RandLength
> 0) {
544 Random
= NET_RANDOM (NetRandomInitSeed ());
545 *Rand
++ = (UINT8
) (Random
);
551 Create the iSCSI driver data..
553 @param Image[in] The handle of the driver image.
555 @param Controller[in] The handle of the controller.
557 @retval The iSCSI driver data created.
561 IScsiCreateDriverData (
563 IN EFI_HANDLE Controller
566 ISCSI_DRIVER_DATA
*Private
;
569 Private
= AllocateZeroPool (sizeof (ISCSI_DRIVER_DATA
));
570 if (Private
== NULL
) {
574 Private
->Signature
= ISCSI_DRIVER_DATA_SIGNATURE
;
575 Private
->Image
= Image
;
576 Private
->Controller
= Controller
;
579 // Create an event to be signal when the BS to RT transition is triggerd so
580 // as to abort the iSCSI session.
582 Status
= gBS
->CreateEvent (
583 EVT_SIGNAL_EXIT_BOOT_SERVICES
,
585 IScsiOnExitBootService
,
587 &Private
->ExitBootServiceEvent
589 if (EFI_ERROR (Status
)) {
590 gBS
->FreePool (Private
);
594 CopyMem(&Private
->IScsiExtScsiPassThru
, &gIScsiExtScsiPassThruProtocolTemplate
, sizeof(EFI_EXT_SCSI_PASS_THRU_PROTOCOL
));
597 // 0 is designated to the TargetId, so use another value for the AdapterId.
599 Private
->ExtScsiPassThruMode
.AdapterId
= 2;
600 Private
->ExtScsiPassThruMode
.Attributes
= EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_PHYSICAL
| EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL
;
601 Private
->ExtScsiPassThruMode
.IoAlign
= 4;
602 Private
->IScsiExtScsiPassThru
.Mode
= &Private
->ExtScsiPassThruMode
;
605 // Install the Ext SCSI PASS THRU protocol.
607 Status
= gBS
->InstallProtocolInterface (
608 &Private
->ExtScsiPassThruHandle
,
609 &gEfiExtScsiPassThruProtocolGuid
,
610 EFI_NATIVE_INTERFACE
,
611 &Private
->IScsiExtScsiPassThru
613 if (EFI_ERROR (Status
)) {
614 gBS
->CloseEvent (Private
->ExitBootServiceEvent
);
615 gBS
->FreePool (Private
);
620 IScsiSessionInit (&Private
->Session
, FALSE
);
626 Clean the iSCSI driver data.
628 @param Private[in] The iSCSI driver data.
634 IScsiCleanDriverData (
635 IN ISCSI_DRIVER_DATA
*Private
638 if (Private
->DevicePath
!= NULL
) {
639 gBS
->UninstallProtocolInterface (
640 Private
->ExtScsiPassThruHandle
,
641 &gEfiDevicePathProtocolGuid
,
645 gBS
->FreePool (Private
->DevicePath
);
648 if (Private
->ExtScsiPassThruHandle
!= NULL
) {
649 gBS
->UninstallProtocolInterface (
650 Private
->ExtScsiPassThruHandle
,
651 &gEfiExtScsiPassThruProtocolGuid
,
652 &Private
->IScsiExtScsiPassThru
656 gBS
->CloseEvent (Private
->ExitBootServiceEvent
);
658 gBS
->FreePool (Private
);
663 Get the various configuration data of this iSCSI instance.
665 @param Private[in] The iSCSI driver data.
667 @retval EFI_SUCCESS The configuration of this instance is got.
669 @retval EFI_NOT_FOUND This iSCSI instance is not configured yet.
674 IN ISCSI_DRIVER_DATA
*Private
678 ISCSI_SESSION
*Session
;
680 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
681 EFI_SIMPLE_NETWORK_MODE
*Mode
;
682 CHAR16 MacString
[65];
685 // get the iSCSI Initiator Name
687 Session
= &Private
->Session
;
688 Session
->InitiatorNameLength
= ISCSI_NAME_MAX_SIZE
;
689 Status
= gIScsiInitiatorName
.Get (
690 &gIScsiInitiatorName
,
691 &Session
->InitiatorNameLength
,
692 Session
->InitiatorName
694 if (EFI_ERROR (Status
)) {
698 Status
= gBS
->HandleProtocol (
700 &gEfiSimpleNetworkProtocolGuid
,
703 if (EFI_ERROR (Status
)) {
710 // Get the mac string, it's the name of various variable
712 IScsiMacAddrToStr (&Mode
->PermanentAddress
, Mode
->HwAddressSize
, MacString
);
715 // Get the normal configuration.
717 BufferSize
= sizeof (Session
->ConfigData
.NvData
);
718 Status
= gRT
->GetVariable (
720 &gEfiIScsiInitiatorNameProtocolGuid
,
723 &Session
->ConfigData
.NvData
725 if (EFI_ERROR (Status
)) {
729 if (!Session
->ConfigData
.NvData
.Enabled
) {
733 // Get the CHAP Auth information.
735 BufferSize
= sizeof (Session
->AuthData
.AuthConfig
);
736 Status
= gRT
->GetVariable (
738 &mIScsiCHAPAuthInfoGuid
,
741 &Session
->AuthData
.AuthConfig
744 if (!EFI_ERROR (Status
) && Session
->ConfigData
.NvData
.InitiatorInfoFromDhcp
) {
748 Status
= IScsiDoDhcp (Private
->Image
, Private
->Controller
, &Session
->ConfigData
);
755 Get the device path of the iSCSI tcp connection and update it.
757 @param Private[in] The iSCSI driver data.
759 @retval The updated device path.
762 EFI_DEVICE_PATH_PROTOCOL
*
763 IScsiGetTcpConnDevicePath (
764 IN ISCSI_DRIVER_DATA
*Private
767 ISCSI_SESSION
*Session
;
768 ISCSI_CONNECTION
*Conn
;
770 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
772 EFI_DEV_PATH
*DPathNode
;
774 Session
= &Private
->Session
;
775 if (Session
->State
!= SESSION_STATE_LOGGED_IN
) {
779 Conn
= NET_LIST_USER_STRUCT_S (
780 Session
->Conns
.ForwardLink
,
783 ISCSI_CONNECTION_SIGNATURE
785 Tcp4Io
= &Conn
->Tcp4Io
;
787 Status
= gBS
->HandleProtocol (
789 &gEfiDevicePathProtocolGuid
,
792 if (EFI_ERROR (Status
)) {
798 DevicePath
= DuplicateDevicePath (DevicePath
);
800 DPathNode
= (EFI_DEV_PATH
*) DevicePath
;
802 while (!IsDevicePathEnd (&DPathNode
->DevPath
)) {
803 if ((DevicePathType (&DPathNode
->DevPath
) == MESSAGING_DEVICE_PATH
) &&
804 (DevicePathSubType (&DPathNode
->DevPath
) == MSG_IPv4_DP
)
807 DPathNode
->Ipv4
.LocalPort
= 0;
808 DPathNode
->Ipv4
.StaticIpAddress
= (BOOLEAN
) (!Session
->ConfigData
.NvData
.InitiatorInfoFromDhcp
);
812 DPathNode
= (EFI_DEV_PATH
*) NextDevicePathNode (&DPathNode
->DevPath
);
819 Abort the session when the transition from BS to RT is initiated.
821 @param Event[in] The event signaled.
823 @param Context[in] The iSCSI driver data.
830 IScsiOnExitBootService (
835 ISCSI_DRIVER_DATA
*Private
;
837 Private
= (ISCSI_DRIVER_DATA
*) Context
;
838 gBS
->CloseEvent (Private
->ExitBootServiceEvent
);
840 IScsiSessionAbort (&Private
->Session
);