3 Copyright (c) 2007 Intel Corporation. All rights reserved
4 This software and associated documentation (if any) is furnished
5 under a license and may only be used or copied in accordance
6 with the terms of the license. Except as permitted by such
7 license, no part of this software or documentation may be
8 reproduced, stored in a retrieval system, or transmitted in any
9 form or by any means without the express written consent of
18 Miscellaneous routines for iSCSI driver.
22 #include "IScsiImpl.h"
24 STATIC CONST CHAR8 IScsiHexString
[] = "0123456789ABCDEFabcdef";
35 Determines if a Unicode character is a hexadecimal digit.
36 The test is case insensitive.
39 Digit - Pointer to byte that receives the value of the hex character.
40 Char - Unicode character to test.
43 TRUE - If the character is a hexadecimal digit.
48 if ((Char
>= L
'0') && (Char
<= L
'9')) {
49 *Digit
= (UINT8
) (Char
- L
'0');
53 if ((Char
>= L
'A') && (Char
<= L
'F')) {
54 *Digit
= (UINT8
) (Char
- L
'A' + 0x0A);
58 if ((Char
>= L
'a') && (Char
<= L
'f')) {
59 *Digit
= (UINT8
) (Char
- L
'a' + 0x0A);
76 Removes (trims) specified leading and trailing characters from a string.
80 str - Pointer to the null-terminated string to be trimmed. On return,
81 str will hold the trimmed string.
82 CharC - Character will be trimmed from str.
96 // Trim off the leading and trailing characters c
98 for (p1
= str
; *p1
&& *p1
== CharC
; p1
++) {
118 for (p1
= str
+ StrLen(str
) - 1; p1
>= str
&& *p1
== CharC
; p1
--) {
121 if (p1
!= str
+ StrLen(str
) - 1) {
127 IScsiGetSubnetMaskPrefixLength (
128 IN EFI_IPv4_ADDRESS
*SubnetMask
134 Calculate the prefix length of the IPv4 subnet mask.
138 SubnetMask - The IPv4 subnet mask.
142 The prefix length of the subnet mask.
150 // The SubnetMask is in network byte order.
152 ReverseMask
= (SubnetMask
->Addr
[0] << 24) | (SubnetMask
->Addr
[1] << 16) | (SubnetMask
->Addr
[2] << 8) | (SubnetMask
->Addr
[3]);
157 ReverseMask
= ~ReverseMask
;
159 if (ReverseMask
& (ReverseMask
+ 1)) {
165 while (ReverseMask
!= 0) {
166 ReverseMask
= ReverseMask
>> 1;
182 Convert the hexadecimal encoded LUN string into the 64-bit LUN.
186 Str - The hexadecimal encoded LUN string.
187 Lun - Storage to return the 64-bit LUN.
191 EFI_SUCCESS - The 64-bit LUN is stored in Lun.
192 EFI_INVALID_PARAMETER - The string is malformatted.
197 CHAR8
*LunUnitStr
[4];
201 NetZeroMem (LunUnitStr
, sizeof (LunUnitStr
));
206 if (!IsHexDigit (&Digit
, *Str
)) {
207 return EFI_INVALID_PARAMETER
;
210 while (*Str
!= '\0') {
212 // Legal representations of LUN:
213 // 4752-3A4F-6b7e-2F99,
221 if (*(Str
+ 1) != '\0') {
222 if (!IsHexDigit (&Digit
, *(Str
+ 1))) {
223 return EFI_INVALID_PARAMETER
;
226 LunUnitStr
[Index
] = Str
+ 1;
228 } else if (!IsHexDigit (&Digit
, *Str
)) {
229 return EFI_INVALID_PARAMETER
;
235 for (Index
= 0; (Index
< 4) && (LunUnitStr
[Index
] != NULL
); Index
++) {
236 if (AsciiStrLen (LunUnitStr
[Index
]) > 4) {
237 return EFI_INVALID_PARAMETER
;
240 *((UINT16
*) &Lun
[Index
* 2]) = HTONS (AsciiStrHexToUintn (LunUnitStr
[Index
]));
247 IScsiLunToUnicodeStr (
255 Convert the 64-bit LUN into the hexadecimal encoded LUN string.
259 Lun - The 64-bit LUN.
260 Str - The storage to return the hexadecimal encoded LUN string.
273 for (Index
= 0; Index
< 4; Index
++) {
275 if ((Lun
[2 * Index
] | Lun
[2 * Index
+ 1]) == 0) {
276 StrCpy (TempStr
, L
"0-");
278 TempStr
[0] = (CHAR16
) IScsiHexString
[Lun
[2 * Index
] >> 4];
279 TempStr
[1] = (CHAR16
) IScsiHexString
[Lun
[2 * Index
] & 0xf];
280 TempStr
[2] = (CHAR16
) IScsiHexString
[Lun
[2 * Index
+ 1] >> 4];
281 TempStr
[3] = (CHAR16
) IScsiHexString
[Lun
[2 * Index
+ 1] & 0xf];
285 StrTrim (TempStr
, L
'0');
288 TempStr
+= StrLen (TempStr
);
291 Str
[StrLen (Str
) - 1] = 0;
293 for (Index
= StrLen (Str
) - 1; Index
> 1; Index
= Index
- 2) {
294 if ((Str
[Index
] == L
'0') && (Str
[Index
- 1] == L
'-')) {
303 IScsiAsciiStrToUnicodeStr (
305 OUT CHAR16
*Destination
311 Convert the ASCII string into a UNICODE string.
315 Source - The ASCII string.
316 Destination - The storage to return the UNICODE string.
320 Pointer to the UNICODE string.
324 ASSERT (Destination
!= NULL
);
325 ASSERT (Source
!= NULL
);
327 while (*Source
!= '\0') {
328 *(Destination
++) = (CHAR16
) *(Source
++);
337 IScsiUnicodeStrToAsciiStr (
339 OUT CHAR8
*Destination
345 Convert the UNICODE string into an ASCII string.
349 Source - The UNICODE string.
350 Destination - The storage to return the ASCII string.
354 Pointer to the ASCII string.
358 ASSERT (Destination
!= NULL
);
359 ASSERT (Source
!= NULL
);
361 while (*Source
!= '\0') {
363 // If any Unicode characters in Source contain
364 // non-zero value in the upper 8 bits, then ASSERT().
366 ASSERT (*Source
< 0x100);
367 *(Destination
++) = (CHAR8
) *(Source
++);
378 OUT EFI_IPv4_ADDRESS
*Ip
384 Convert the decimal dotted IPv4 address into the binary IPv4 address.
388 Str - The UNICODE string.
389 Ip - The storage to return the ASCII string.
393 EFI_SUCCESS - The binary IP address is returned in Ip.
394 EFI_INVALID_PARAMETER - The IP string is malformatted.
406 return EFI_INVALID_PARAMETER
;
410 while (NET_IS_DIGIT (*Str
)) {
411 Number
= Number
* 10 + (*Str
- '0');
416 return EFI_INVALID_PARAMETER
;
419 Ip
->Addr
[Index
] = (UINT8
) Number
;
421 if ((*Str
!= '\0') && (*Str
!= '.')) {
423 // The current character should be either the NULL terminator or
424 // the dot delimiter.
426 return EFI_INVALID_PARAMETER
;
431 // Skip the delimiter.
440 return EFI_INVALID_PARAMETER
;
448 IN EFI_MAC_ADDRESS
*Mac
,
456 Convert the mac address into a hexadecimal encoded "-" seperated string.
460 Mac - The mac address.
461 Len - Length in bytes of the mac address.
462 Str - The storage to return the mac string.
472 for (Index
= 0; Index
< Len
; Index
++) {
473 Str
[3 * Index
] = NibbleToHexChar (Mac
->Addr
[Index
] >> 4);
474 Str
[3 * Index
+ 1] = NibbleToHexChar (Mac
->Addr
[Index
]);
475 Str
[3 * Index
+ 2] = L
'-';
478 Str
[3 * Index
- 1] = L
'\0';
485 IN OUT CHAR8
*HexStr
,
486 IN OUT UINT32
*HexLength
492 Convert the binary encoded buffer into a hexadecimal encoded string.
496 BinBuffer - The buffer containing the binary data.
497 BinLength - Length of the binary buffer.
498 HexStr - Pointer to the string.
499 HexLength - The length of the string.
503 EFI_SUCCESS - The binary data is converted to the hexadecimal string
504 and the length of the string is updated.
505 EFI_BUFFER_TOO_SMALL - The string is too small.
511 if ((HexStr
== NULL
) || (BinBuffer
== NULL
) || (BinLength
== 0)) {
512 return EFI_INVALID_PARAMETER
;
515 if (((*HexLength
) - 3) < BinLength
* 2) {
516 *HexLength
= BinLength
* 2 + 3;
517 return EFI_BUFFER_TOO_SMALL
;
520 *HexLength
= BinLength
* 2 + 3;
522 // Prefix for Hex String
527 for (Index
= 0; Index
< BinLength
; Index
++) {
528 HexStr
[Index
* 2 + 2] = IScsiHexString
[BinBuffer
[Index
] >> 4];
529 HexStr
[Index
* 2 + 3] = IScsiHexString
[BinBuffer
[Index
] & 0xf];
532 HexStr
[Index
* 2 + 2] = '\0';
539 IN OUT UINT8
*BinBuffer
,
540 IN OUT UINT32
*BinLength
,
547 Convert the hexadecimal string into a binary encoded buffer.
551 BinBuffer - The binary buffer.
552 BinLength - Length of the binary buffer.
553 HexStr - The hexadecimal string.
557 EFI_SUCCESS - The hexadecimal string is converted into a binary
559 EFI_BUFFER_TOO_SMALL - The binary buffer is too small to hold the converted data.s
570 // Find out how many hex characters the string has.
573 if ((HexBuf
[0] == '0') && ((HexBuf
[1] == 'x') || (HexBuf
[1] == 'X'))) {
577 for (Index
= 0, HexCount
= 0; IsHexDigit (&Digit
, HexBuf
[Index
]); Index
++, HexCount
++)
585 // Test if buffer is passed enough.
587 if (((HexCount
+ 1) / 2) > *BinLength
) {
588 *BinLength
= (HexCount
+ 1) / 2;
589 return EFI_BUFFER_TOO_SMALL
;
592 *BinLength
= (HexCount
+ 1) / 2;
594 for (Index
= 0; Index
< HexCount
; Index
++) {
596 IsHexDigit (&Digit
, HexBuf
[HexCount
- 1 - Index
]);
598 if ((Index
& 1) == 0) {
601 Byte
= BinBuffer
[*BinLength
- 1 - Index
/ 2];
606 BinBuffer
[*BinLength
- 1 - Index
/ 2] = Byte
;
621 Generate random numbers.
625 Rand - The buffer to contain random numbers.
626 RandLength - The length of the Rand buffer.
636 while (RandLength
> 0) {
637 Random
= NET_RANDOM (NetRandomInitSeed ());
638 *Rand
++ = (UINT8
) (Random
);
644 IScsiCreateDriverData (
646 IN EFI_HANDLE Controller
652 Create the iSCSI driver data..
656 Image - The handle of the driver image.
657 Controller - The handle of the controller.
661 The iSCSI driver data created.
665 ISCSI_DRIVER_DATA
*Private
;
668 Private
= NetAllocateZeroPool (sizeof (ISCSI_DRIVER_DATA
));
669 if (Private
== NULL
) {
673 Private
->Signature
= ISCSI_DRIVER_DATA_SIGNATURE
;
674 Private
->Image
= Image
;
675 Private
->Controller
= Controller
;
678 // Create an event to be signal when the BS to RT transition is triggerd so
679 // as to abort the iSCSI session.
681 Status
= gBS
->CreateEvent (
682 EFI_EVENT_SIGNAL_EXIT_BOOT_SERVICES
,
684 IScsiOnExitBootService
,
686 &Private
->ExitBootServiceEvent
688 if (EFI_ERROR (Status
)) {
689 NetFreePool (Private
);
693 NetCopyMem(&Private
->IScsiExtScsiPassThru
, &gIScsiExtScsiPassThruProtocolTemplate
, sizeof(EFI_EXT_SCSI_PASS_THRU_PROTOCOL
));
696 // 0 is designated to the TargetId, so use another value for the AdapterId.
698 Private
->ExtScsiPassThruMode
.AdapterId
= 2;
699 Private
->ExtScsiPassThruMode
.Attributes
= EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_PHYSICAL
| EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL
;
700 Private
->ExtScsiPassThruMode
.IoAlign
= 4;
701 Private
->IScsiExtScsiPassThru
.Mode
= &Private
->ExtScsiPassThruMode
;
704 // Install the Ext SCSI PASS THRU protocol.
706 Status
= gBS
->InstallProtocolInterface (
707 &Private
->ExtScsiPassThruHandle
,
708 &gEfiExtScsiPassThruProtocolGuid
,
709 EFI_NATIVE_INTERFACE
,
710 &Private
->IScsiExtScsiPassThru
712 if (EFI_ERROR (Status
)) {
713 gBS
->CloseEvent (Private
->ExitBootServiceEvent
);
714 NetFreePool (Private
);
719 IScsiSessionInit (&Private
->Session
, FALSE
);
725 IScsiCleanDriverData (
726 IN ISCSI_DRIVER_DATA
*Private
732 Clean the iSCSI driver data.
736 Private - The iSCSI driver data.
744 if (Private
->DevicePath
!= NULL
) {
745 gBS
->UninstallProtocolInterface (
746 Private
->ExtScsiPassThruHandle
,
747 &gEfiDevicePathProtocolGuid
,
751 NetFreePool (Private
->DevicePath
);
754 if (Private
->ExtScsiPassThruHandle
!= NULL
) {
755 gBS
->UninstallProtocolInterface (
756 Private
->ExtScsiPassThruHandle
,
757 &gEfiExtScsiPassThruProtocolGuid
,
758 &Private
->IScsiExtScsiPassThru
762 gBS
->CloseEvent (Private
->ExitBootServiceEvent
);
764 NetFreePool (Private
);
769 IN ISCSI_DRIVER_DATA
*Private
775 Get the various configuration data of this iSCSI instance.
779 Private - The iSCSI driver data.
783 EFI_SUCCESS - The configuration of this instance is got.
784 EFI_NOT_FOUND - This iSCSI instance is not configured yet.
789 ISCSI_SESSION
*Session
;
791 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
792 EFI_SIMPLE_NETWORK_MODE
*Mode
;
793 CHAR16 MacString
[65];
796 // get the iSCSI Initiator Name
798 Session
= &Private
->Session
;
799 Session
->InitiatorNameLength
= ISCSI_NAME_MAX_SIZE
;
800 Status
= gIScsiInitiatorName
.Get (
801 &gIScsiInitiatorName
,
802 &Session
->InitiatorNameLength
,
803 Session
->InitiatorName
805 if (EFI_ERROR (Status
)) {
809 Status
= gBS
->HandleProtocol (
811 &gEfiSimpleNetworkProtocolGuid
,
814 if (EFI_ERROR (Status
)) {
821 // Get the mac string, it's the name of various variable
823 IScsiMacAddrToStr (&Mode
->PermanentAddress
, Mode
->HwAddressSize
, MacString
);
826 // Get the normal configuration.
828 BufferSize
= sizeof (Session
->ConfigData
.NvData
);
829 Status
= gRT
->GetVariable (
831 &gEfiIScsiInitiatorNameProtocolGuid
,
834 &Session
->ConfigData
.NvData
836 if (EFI_ERROR (Status
)) {
840 if (!Session
->ConfigData
.NvData
.Enabled
) {
844 // Get the CHAP Auth information.
846 BufferSize
= sizeof (Session
->AuthData
.AuthConfig
);
847 Status
= gRT
->GetVariable (
849 &mIScsiCHAPAuthInfoGuid
,
852 &Session
->AuthData
.AuthConfig
855 if (!EFI_ERROR (Status
) && Session
->ConfigData
.NvData
.InitiatorInfoFromDhcp
) {
859 Status
= IScsiDoDhcp (Private
->Image
, Private
->Controller
, &Session
->ConfigData
);
865 EFI_DEVICE_PATH_PROTOCOL
*
866 IScsiGetTcpConnDevicePath (
867 IN ISCSI_DRIVER_DATA
*Private
873 Get the device path of the iSCSI tcp connection and update it.
877 Private - The iSCSI driver data.
881 The updated device path.
885 ISCSI_SESSION
*Session
;
886 ISCSI_CONNECTION
*Conn
;
888 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
890 EFI_DEV_PATH
*DPathNode
;
892 Session
= &Private
->Session
;
893 if (Session
->State
!= SESSION_STATE_LOGGED_IN
) {
897 Conn
= NET_LIST_USER_STRUCT_S (
898 Session
->Conns
.ForwardLink
,
901 ISCSI_CONNECTION_SIGNATURE
903 Tcp4Io
= &Conn
->Tcp4Io
;
905 Status
= gBS
->HandleProtocol (
907 &gEfiDevicePathProtocolGuid
,
910 if (EFI_ERROR (Status
)) {
916 DevicePath
= DuplicateDevicePath (DevicePath
);
918 DPathNode
= (EFI_DEV_PATH
*) DevicePath
;
920 while (!IsDevicePathEnd (&DPathNode
->DevPath
)) {
921 if ((DevicePathType (&DPathNode
->DevPath
) == MESSAGING_DEVICE_PATH
) &&
922 (DevicePathSubType (&DPathNode
->DevPath
) == MSG_IPv4_DP
)
925 DPathNode
->Ipv4
.LocalPort
= 0;
926 DPathNode
->Ipv4
.StaticIpAddress
= !Session
->ConfigData
.NvData
.InitiatorInfoFromDhcp
;
930 DPathNode
= (EFI_DEV_PATH
*) NextDevicePathNode (&DPathNode
->DevPath
);
938 IScsiOnExitBootService (
946 Abort the session when the transition from BS to RT is initiated.
950 Event - The event signaled.
951 Context - The iSCSI driver data.
959 ISCSI_DRIVER_DATA
*Private
;
961 Private
= (ISCSI_DRIVER_DATA
*) Context
;
962 gBS
->CloseEvent (Private
->ExitBootServiceEvent
);
964 IScsiSessionAbort (&Private
->Session
);