2 Miscellaneous routines for iSCSI driver.
4 Copyright (c) 2004 - 2008, Intel Corporation.<BR>
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.
15 #include "IScsiImpl.h"
17 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 IScsiHexString
[] = "0123456789ABCDEFabcdef";
20 Removes (trims) specified leading and trailing characters from a string.
22 @param[in, out] Str Pointer to the null-terminated string to be trimmed. On return,
23 Str will hold the trimmed string.
25 @param[in] CharC Character will be trimmed from str.
41 // Trim off the leading and trailing characters c
43 for (Pointer1
= Str
; (*Pointer1
!= 0) && (*Pointer1
== CharC
); Pointer1
++) {
48 if (Pointer2
== Pointer1
) {
49 while (*Pointer1
!= 0) {
54 while (*Pointer1
!= 0) {
55 *Pointer2
= *Pointer1
;
63 for (Pointer1
= Str
+ StrLen(Str
) - 1; Pointer1
>= Str
&& *Pointer1
== CharC
; Pointer1
--) {
66 if (Pointer1
!= Str
+ StrLen(Str
) - 1) {
72 Calculate the prefix length of the IPv4 subnet mask.
74 @param[in] SubnetMask The IPv4 subnet mask.
76 @return The prefix length of the subnet mask.
77 @retval 0 Other errors as indicated.
80 IScsiGetSubnetMaskPrefixLength (
81 IN EFI_IPv4_ADDRESS
*SubnetMask
88 // The SubnetMask is in network byte order.
90 ReverseMask
= (SubnetMask
->Addr
[0] << 24) | (SubnetMask
->Addr
[1] << 16) | (SubnetMask
->Addr
[2] << 8) | (SubnetMask
->Addr
[3]);
95 ReverseMask
= ~ReverseMask
;
97 if ((ReverseMask
& (ReverseMask
+ 1)) != 0) {
103 while (ReverseMask
!= 0) {
104 ReverseMask
= ReverseMask
>> 1;
108 return (UINT8
) (32 - Len
);
112 Convert the hexadecimal encoded LUN string into the 64-bit LUN.
114 @param[in] Str The hexadecimal encoded LUN string.
115 @param[out] Lun Storage to return the 64-bit LUN.
117 @retval EFI_SUCCESS The 64-bit LUN is stored in Lun.
118 @retval EFI_INVALID_PARAMETER The string is malformatted.
126 UINTN Index
, IndexValue
, IndexNum
, SizeStr
;
133 ZeroMem ((UINT8
*) Value
, sizeof (Value
));
134 SizeStr
= AsciiStrLen (Str
);
138 for (Index
= 0; Index
< SizeStr
; Index
++) {
139 TemStr
[0] = Str
[Index
];
140 TemValue
= (UINT8
) AsciiStrHexToUint64 (TemStr
);
141 if (TemValue
== 0 && TemStr
[0] != '0') {
142 if ((TemStr
[0] != '-') || (IndexNum
== 0)) {
146 return EFI_INVALID_PARAMETER
;
150 if ((TemValue
== 0) && (TemStr
[0] == '-')) {
154 if (++IndexValue
>= 4) {
158 return EFI_INVALID_PARAMETER
;
161 // Restart str index for the next lun value
167 if (++IndexNum
> 4) {
169 // Each Lun Str can't exceed size 4, because it will be as UINT16 value
171 return EFI_INVALID_PARAMETER
;
175 // Combine UINT16 value
177 Value
[IndexValue
] = (UINT16
) ((Value
[IndexValue
] << 4) + TemValue
);
180 for (Index
= 0; Index
<= IndexValue
; Index
++) {
181 *((UINT16
*) &Lun
[Index
* 2]) = HTONS (Value
[Index
]);
188 Convert the 64-bit LUN into the hexadecimal encoded LUN string.
190 @param[in] Lun The 64-bit LUN.
191 @param[out] Str The storage to return the hexadecimal encoded LUN string.
194 IScsiLunToUnicodeStr (
204 for (Index
= 0; Index
< 4; Index
++) {
206 if ((Lun
[2 * Index
] | Lun
[2 * Index
+ 1]) == 0) {
207 StrCpy (TempStr
, L
"0-");
209 TempStr
[0] = (CHAR16
) IScsiHexString
[Lun
[2 * Index
] >> 4];
210 TempStr
[1] = (CHAR16
) IScsiHexString
[Lun
[2 * Index
] & 0x0F];
211 TempStr
[2] = (CHAR16
) IScsiHexString
[Lun
[2 * Index
+ 1] >> 4];
212 TempStr
[3] = (CHAR16
) IScsiHexString
[Lun
[2 * Index
+ 1] & 0x0F];
216 StrTrim (TempStr
, L
'0');
219 TempStr
+= StrLen (TempStr
);
222 Str
[StrLen (Str
) - 1] = 0;
224 for (Index
= StrLen (Str
) - 1; Index
> 1; Index
= Index
- 2) {
225 if ((Str
[Index
] == L
'0') && (Str
[Index
- 1] == L
'-')) {
234 Convert the ASCII string into a UNICODE string.
236 @param[in] Source The ASCII string.
237 @param[out] Destination The storage to return the UNICODE string.
239 @return CHAR16 * Pointer to the UNICODE string.
242 IScsiAsciiStrToUnicodeStr (
244 OUT CHAR16
*Destination
247 ASSERT (Destination
!= NULL
);
248 ASSERT (Source
!= NULL
);
250 while (*Source
!= '\0') {
251 *(Destination
++) = (CHAR16
) *(Source
++);
260 Convert the UNICODE string into an ASCII string.
262 @param[in] Source The UNICODE string.
263 @param[out] Destination The storage to return the ASCII string.
265 @return CHAR8 * Pointer to the ASCII string.
268 IScsiUnicodeStrToAsciiStr (
270 OUT CHAR8
*Destination
273 ASSERT (Destination
!= NULL
);
274 ASSERT (Source
!= NULL
);
276 while (*Source
!= '\0') {
278 // If any Unicode characters in Source contain
279 // non-zero value in the upper 8 bits, then ASSERT().
281 ASSERT (*Source
< 0x100);
282 *(Destination
++) = (CHAR8
) *(Source
++);
291 Convert the decimal dotted IPv4 address into the binary IPv4 address.
293 @param[in] Str The UNICODE string.
294 @param[out] Ip The storage to return the ASCII string.
296 @retval EFI_SUCCESS The binary IP address is returned in Ip.
297 @retval EFI_INVALID_PARAMETER The IP string is malformatted.
302 OUT EFI_IPv4_ADDRESS
*Ip
313 return EFI_INVALID_PARAMETER
;
317 while (NET_IS_DIGIT (*Str
)) {
318 Number
= Number
* 10 + (*Str
- '0');
323 return EFI_INVALID_PARAMETER
;
326 Ip
->Addr
[Index
] = (UINT8
) Number
;
328 if ((*Str
!= '\0') && (*Str
!= '.')) {
330 // The current character should be either the NULL terminator or
331 // the dot delimiter.
333 return EFI_INVALID_PARAMETER
;
338 // Skip the delimiter.
347 return EFI_INVALID_PARAMETER
;
354 Convert the mac address into a hexadecimal encoded "-" seperated string.
356 @param[in] Mac The mac address.
357 @param[in] Len Length in bytes of the mac address.
358 @param[out] Str The storage to return the mac string.
362 IN EFI_MAC_ADDRESS
*Mac
,
369 for (Index
= 0; Index
< Len
; Index
++) {
370 Str
[3 * Index
] = (CHAR16
) IScsiHexString
[(Mac
->Addr
[Index
] >> 4) & 0x0F];
371 Str
[3 * Index
+ 1] = (CHAR16
) IScsiHexString
[Mac
->Addr
[Index
] & 0x0F];
372 Str
[3 * Index
+ 2] = L
'-';
375 Str
[3 * Index
- 1] = L
'\0';
379 Convert the binary encoded buffer into a hexadecimal encoded string.
381 @param[in] BinBuffer The buffer containing the binary data.
382 @param[in] BinLength Length of the binary buffer.
383 @param[in, out] HexStr Pointer to the string.
384 @param[in, out] HexLength The length of the string.
386 @retval EFI_SUCCESS The binary data is converted to the hexadecimal string
387 and the length of the string is updated.
388 @retval EFI_BUFFER_TOO_SMALL The string is too small.
389 @retval EFI_INVALID_PARAMETER The IP string is malformatted.
395 IN OUT CHAR8
*HexStr
,
396 IN OUT UINT32
*HexLength
401 if ((HexStr
== NULL
) || (BinBuffer
== NULL
) || (BinLength
== 0)) {
402 return EFI_INVALID_PARAMETER
;
405 if (((*HexLength
) - 3) < BinLength
* 2) {
406 *HexLength
= BinLength
* 2 + 3;
407 return EFI_BUFFER_TOO_SMALL
;
410 *HexLength
= BinLength
* 2 + 3;
412 // Prefix for Hex String
417 for (Index
= 0; Index
< BinLength
; Index
++) {
418 HexStr
[Index
* 2 + 2] = IScsiHexString
[BinBuffer
[Index
] >> 4];
419 HexStr
[Index
* 2 + 3] = IScsiHexString
[BinBuffer
[Index
] & 0x0F];
422 HexStr
[Index
* 2 + 2] = '\0';
428 Convert the hexadecimal string into a binary encoded buffer.
430 @param[in, out] BinBuffer The binary buffer.
431 @param[in, out] BinLength Length of the binary buffer.
432 @param[in] HexStr The hexadecimal string.
434 @retval EFI_SUCCESS The hexadecimal string is converted into a binary
436 @retval EFI_BUFFER_TOO_SMALL The binary buffer is too small to hold the converted data.
440 IN OUT UINT8
*BinBuffer
,
441 IN OUT UINT32
*BinLength
,
450 ZeroMem (TemStr
, sizeof (TemStr
));
453 // Find out how many hex characters the string has.
455 if ((HexStr
[0] == '0') && ((HexStr
[1] == 'x') || (HexStr
[1] == 'X'))) {
459 Length
= AsciiStrLen (HexStr
);
461 for (Index
= 0; Index
< Length
; Index
++) {
462 TemStr
[0] = HexStr
[Index
];
463 Digit
= (UINT8
) AsciiStrHexToUint64 (TemStr
);
464 if (Digit
== 0 && TemStr
[0] != '0') {
470 if ((Index
& 1) == 0) {
471 BinBuffer
[Index
/2] = Digit
;
473 BinBuffer
[Index
/2] = (UINT8
) ((BinBuffer
[Index
/2] << 4) + Digit
);
477 *BinLength
= (UINT32
) ((Index
+ 1)/2);
483 Generate random numbers.
485 @param[in, out] Rand The buffer to contain random numbers.
486 @param[in] RandLength The length of the Rand buffer.
496 while (RandLength
> 0) {
497 Random
= NET_RANDOM (NetRandomInitSeed ());
498 *Rand
++ = (UINT8
) (Random
);
504 Create the iSCSI driver data..
506 @param[in] Image The handle of the driver image.
507 @param[in] Controller The handle of the controller.
509 @return The iSCSI driver data created.
510 @retval NULL Other errors as indicated.
513 IScsiCreateDriverData (
515 IN EFI_HANDLE Controller
518 ISCSI_DRIVER_DATA
*Private
;
521 Private
= AllocateZeroPool (sizeof (ISCSI_DRIVER_DATA
));
522 if (Private
== NULL
) {
526 Private
->Signature
= ISCSI_DRIVER_DATA_SIGNATURE
;
527 Private
->Image
= Image
;
528 Private
->Controller
= Controller
;
531 // Create an event to be signal when the BS to RT transition is triggerd so
532 // as to abort the iSCSI session.
534 Status
= gBS
->CreateEventEx (
537 IScsiOnExitBootService
,
539 &gEfiEventExitBootServicesGuid
,
540 &Private
->ExitBootServiceEvent
542 if (EFI_ERROR (Status
)) {
543 gBS
->FreePool (Private
);
547 CopyMem(&Private
->IScsiExtScsiPassThru
, &gIScsiExtScsiPassThruProtocolTemplate
, sizeof(EFI_EXT_SCSI_PASS_THRU_PROTOCOL
));
550 // 0 is designated to the TargetId, so use another value for the AdapterId.
552 Private
->ExtScsiPassThruMode
.AdapterId
= 2;
553 Private
->ExtScsiPassThruMode
.Attributes
= EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_PHYSICAL
| EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL
;
554 Private
->ExtScsiPassThruMode
.IoAlign
= 4;
555 Private
->IScsiExtScsiPassThru
.Mode
= &Private
->ExtScsiPassThruMode
;
558 // Install the Ext SCSI PASS THRU protocol.
560 Status
= gBS
->InstallProtocolInterface (
561 &Private
->ExtScsiPassThruHandle
,
562 &gEfiExtScsiPassThruProtocolGuid
,
563 EFI_NATIVE_INTERFACE
,
564 &Private
->IScsiExtScsiPassThru
566 if (EFI_ERROR (Status
)) {
567 gBS
->CloseEvent (Private
->ExitBootServiceEvent
);
568 gBS
->FreePool (Private
);
573 IScsiSessionInit (&Private
->Session
, FALSE
);
579 Clean the iSCSI driver data.
581 @param[in] Private The iSCSI driver data.
584 IScsiCleanDriverData (
585 IN ISCSI_DRIVER_DATA
*Private
588 if (Private
->DevicePath
!= NULL
) {
589 gBS
->UninstallProtocolInterface (
590 Private
->ExtScsiPassThruHandle
,
591 &gEfiDevicePathProtocolGuid
,
595 gBS
->FreePool (Private
->DevicePath
);
598 if (Private
->ExtScsiPassThruHandle
!= NULL
) {
599 gBS
->UninstallProtocolInterface (
600 Private
->ExtScsiPassThruHandle
,
601 &gEfiExtScsiPassThruProtocolGuid
,
602 &Private
->IScsiExtScsiPassThru
606 gBS
->CloseEvent (Private
->ExitBootServiceEvent
);
608 gBS
->FreePool (Private
);
612 Get the various configuration data of this iSCSI instance.
614 @param[in] Private The iSCSI driver data.
616 @retval EFI_SUCCESS The configuration of this instance is got.
617 @retval EFI_ABORTED The operation was aborted.
618 @retval Others Other errors as indicated.
622 IN ISCSI_DRIVER_DATA
*Private
626 ISCSI_SESSION
*Session
;
628 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
629 EFI_SIMPLE_NETWORK_MODE
*Mode
;
630 CHAR16 MacString
[65];
633 // get the iSCSI Initiator Name
635 Session
= &Private
->Session
;
636 Session
->InitiatorNameLength
= ISCSI_NAME_MAX_SIZE
;
637 Status
= gIScsiInitiatorName
.Get (
638 &gIScsiInitiatorName
,
639 &Session
->InitiatorNameLength
,
640 Session
->InitiatorName
642 if (EFI_ERROR (Status
)) {
646 Status
= gBS
->HandleProtocol (
648 &gEfiSimpleNetworkProtocolGuid
,
651 if (EFI_ERROR (Status
)) {
658 // Get the mac string, it's the name of various variable
660 IScsiMacAddrToStr (&Mode
->PermanentAddress
, Mode
->HwAddressSize
, MacString
);
663 // Get the normal configuration.
665 BufferSize
= sizeof (Session
->ConfigData
.NvData
);
666 Status
= gRT
->GetVariable (
668 &gEfiIScsiInitiatorNameProtocolGuid
,
671 &Session
->ConfigData
.NvData
673 if (EFI_ERROR (Status
)) {
677 if (!Session
->ConfigData
.NvData
.Enabled
) {
681 // Get the CHAP Auth information.
683 BufferSize
= sizeof (Session
->AuthData
.AuthConfig
);
684 Status
= gRT
->GetVariable (
686 &mIScsiCHAPAuthInfoGuid
,
689 &Session
->AuthData
.AuthConfig
692 if (!EFI_ERROR (Status
) && Session
->ConfigData
.NvData
.InitiatorInfoFromDhcp
) {
696 Status
= IScsiDoDhcp (Private
->Image
, Private
->Controller
, &Session
->ConfigData
);
703 Get the device path of the iSCSI tcp connection and update it.
705 @param[in] Private The iSCSI driver data.
707 @return The updated device path.
708 @retval NULL Other errors as indicated.
710 EFI_DEVICE_PATH_PROTOCOL
*
711 IScsiGetTcpConnDevicePath (
712 IN ISCSI_DRIVER_DATA
*Private
715 ISCSI_SESSION
*Session
;
716 ISCSI_CONNECTION
*Conn
;
718 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
720 EFI_DEV_PATH
*DPathNode
;
722 Session
= &Private
->Session
;
723 if (Session
->State
!= SESSION_STATE_LOGGED_IN
) {
727 Conn
= NET_LIST_USER_STRUCT_S (
728 Session
->Conns
.ForwardLink
,
731 ISCSI_CONNECTION_SIGNATURE
733 Tcp4Io
= &Conn
->Tcp4Io
;
735 Status
= gBS
->HandleProtocol (
737 &gEfiDevicePathProtocolGuid
,
740 if (EFI_ERROR (Status
)) {
746 DevicePath
= DuplicateDevicePath (DevicePath
);
748 DPathNode
= (EFI_DEV_PATH
*) DevicePath
;
750 while (!IsDevicePathEnd (&DPathNode
->DevPath
)) {
751 if ((DevicePathType (&DPathNode
->DevPath
) == MESSAGING_DEVICE_PATH
) &&
752 (DevicePathSubType (&DPathNode
->DevPath
) == MSG_IPv4_DP
)
755 DPathNode
->Ipv4
.LocalPort
= 0;
756 DPathNode
->Ipv4
.StaticIpAddress
= (BOOLEAN
) (!Session
->ConfigData
.NvData
.InitiatorInfoFromDhcp
);
760 DPathNode
= (EFI_DEV_PATH
*) NextDevicePathNode (&DPathNode
->DevPath
);
767 Abort the session when the transition from BS to RT is initiated.
769 @param[in] Event The event signaled.
770 @param[in] Context The iSCSI driver data.
774 IScsiOnExitBootService (
779 ISCSI_DRIVER_DATA
*Private
;
781 Private
= (ISCSI_DRIVER_DATA
*) Context
;
782 gBS
->CloseEvent (Private
->ExitBootServiceEvent
);
784 IScsiSessionAbort (&Private
->Session
);