2 Helper functions for configuring or getting the parameters relating to iSCSI.
4 Copyright (c) 2004 - 2011, 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
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 EFI_GUID mVendorGuid
= ISCSI_CONFIG_GUID
;
18 CHAR16 mVendorStorageName
[] = L
"ISCSI_CONFIG_IFR_NVDATA";
19 BOOLEAN mIScsiDeviceListUpdated
= FALSE
;
20 UINTN mNumberOfIScsiDevices
= 0;
21 ISCSI_FORM_CALLBACK_INFO
*mCallbackInfo
= NULL
;
23 LIST_ENTRY mIScsiConfigFormList
= {
24 &mIScsiConfigFormList
,
28 HII_VENDOR_DEVICE_PATH mIScsiHiiVendorDevicePath
= {
34 (UINT8
) (sizeof (VENDOR_DEVICE_PATH
)),
35 (UINT8
) ((sizeof (VENDOR_DEVICE_PATH
)) >> 8)
39 // {49D7B73E-143D-4716-977B-C45F1CB038CC}
41 { 0x49d7b73e, 0x143d, 0x4716, { 0x97, 0x7b, 0xc4, 0x5f, 0x1c, 0xb0, 0x38, 0xcc } }
45 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
47 (UINT8
) (END_DEVICE_PATH_LENGTH
),
48 (UINT8
) ((END_DEVICE_PATH_LENGTH
) >> 8)
54 Convert the IPv4 address into a dotted string.
56 @param[in] Ip The IPv4 address.
57 @param[out] Str The dotted IP string.
61 IN EFI_IPv4_ADDRESS
*Ip
,
65 UnicodeSPrint ( Str
, 2 * IP4_STR_MAX_SIZE
, L
"%d.%d.%d.%d", Ip
->Addr
[0], Ip
->Addr
[1], Ip
->Addr
[2], Ip
->Addr
[3]);
70 Parse IsId in string format and convert it to binary.
72 @param[in] String The buffer of the string to be parsed.
73 @param[in, out] IsId The buffer to store IsId.
75 @retval EFI_SUCCESS The operation finished successfully.
76 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
80 IScsiParseIsIdFromString (
81 IN CONST CHAR16
*String
,
89 CHAR16 PortString
[ISCSI_NAME_IFR_MAX_SIZE
];
92 if ((String
== NULL
) || (IsId
== NULL
)) {
93 return EFI_INVALID_PARAMETER
;
96 IsIdStr
= (CHAR16
*) String
;
98 if (StrLen (IsIdStr
) != 6) {
101 (UINTN
) ISCSI_NAME_IFR_MAX_SIZE
,
102 L
"Error! Input is incorrect, please input 6 hex numbers!\n"
106 EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
,
112 return EFI_INVALID_PARAMETER
;
115 for (Index
= 3; Index
< 6; Index
++) {
116 CopyMem (TempStr
, IsIdStr
, sizeof (TempStr
));
120 // Convert the string to IsId. StrHexToUintn stops at the first character
121 // that is not a valid hex character, '\0' here.
123 NodeVal
= StrHexToUintn (TempStr
);
125 IsId
[Index
] = (UINT8
) NodeVal
;
127 IsIdStr
= IsIdStr
+ 2;
134 Convert IsId from binary to string format.
136 @param[out] String The buffer to store the converted string.
137 @param[in] IsId The buffer to store IsId.
139 @retval EFI_SUCCESS The string converted successfully.
140 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
144 IScsiConvertIsIdToString (
152 if ((String
== NULL
) || (IsId
== NULL
)) {
153 return EFI_INVALID_PARAMETER
;
156 for (Index
= 0; Index
< 6; Index
++) {
157 if (IsId
[Index
] <= 0xF) {
158 Number
= UnicodeSPrint (
160 2 * ISID_CONFIGURABLE_STORAGE
,
165 Number
= UnicodeSPrint (
167 2 * ISID_CONFIGURABLE_STORAGE
,
174 String
= String
+ Number
;
184 Update the list of iSCSI devices the iSCSI driver is controlling.
186 @retval EFI_SUCCESS The callback successfully handled the action.
187 @retval Others Other errors as indicated.
190 IScsiUpdateDeviceList (
195 ISCSI_DEVICE_LIST
*DeviceList
;
201 UINTN LastDeviceIndex
;
202 EFI_MAC_ADDRESS MacAddress
;
205 ISCSI_MAC_INFO
*CurMacInfo
;
206 ISCSI_MAC_INFO TempMacInfo
;
207 CHAR16 MacString
[70];
208 UINTN DeviceListSize
;
211 // Dump all the handles the Managed Network Service Binding Protocol is installed on.
213 Status
= gBS
->LocateHandleBuffer (
215 &gEfiManagedNetworkServiceBindingProtocolGuid
,
220 if (EFI_ERROR (Status
)) {
225 Status
= gRT
->GetVariable (
232 if (Status
== EFI_BUFFER_TOO_SMALL
) {
233 DeviceList
= (ISCSI_DEVICE_LIST
*) AllocatePool (DataSize
);
245 for (HandleIndex
= 0; HandleIndex
< NumHandles
; HandleIndex
++) {
246 Status
= NetLibGetMacAddress (Handles
[HandleIndex
], &MacAddress
, &HwAddressSize
);
247 ASSERT (Status
== EFI_SUCCESS
);
248 VlanId
= NetLibGetVlanId (Handles
[HandleIndex
]);
250 for (Index
= LastDeviceIndex
; Index
< DeviceList
->NumDevice
; Index
++) {
251 CurMacInfo
= &DeviceList
->MacInfo
[Index
];
252 if ((CurMacInfo
->Len
== HwAddressSize
) &&
253 (CurMacInfo
->VlanId
== VlanId
) &&
254 (NET_MAC_EQUAL (&CurMacInfo
->Mac
, MacAddress
.Addr
, HwAddressSize
))
257 // The previous configured NIC is still here.
259 if (Index
!= LastDeviceIndex
) {
261 // Swap the current MAC address entry with the one indexed by
264 CopyMem (&TempMacInfo
, CurMacInfo
, sizeof (ISCSI_MAC_INFO
));
265 CopyMem (CurMacInfo
, &DeviceList
->MacInfo
[LastDeviceIndex
], sizeof (ISCSI_MAC_INFO
));
266 CopyMem (&DeviceList
->MacInfo
[LastDeviceIndex
], &TempMacInfo
, sizeof (ISCSI_MAC_INFO
));
273 if (LastDeviceIndex
== DeviceList
->NumDevice
) {
278 for (Index
= LastDeviceIndex
; Index
< DeviceList
->NumDevice
; Index
++) {
280 // delete the variables
282 CurMacInfo
= &DeviceList
->MacInfo
[Index
];
283 IScsiMacAddrToStr (&CurMacInfo
->Mac
, CurMacInfo
->Len
, CurMacInfo
->VlanId
, MacString
);
284 gRT
->SetVariable (MacString
, &gEfiIScsiInitiatorNameProtocolGuid
, 0, 0, NULL
);
285 gRT
->SetVariable (MacString
, &mIScsiCHAPAuthInfoGuid
, 0, 0, NULL
);
288 FreePool (DeviceList
);
289 } else if (Status
!= EFI_NOT_FOUND
) {
294 // Construct the new iSCSI device list.
296 DeviceListSize
= sizeof (ISCSI_DEVICE_LIST
) + (NumHandles
- 1) * sizeof (ISCSI_MAC_INFO
);
297 DeviceList
= (ISCSI_DEVICE_LIST
*) AllocatePool (DeviceListSize
);
298 DeviceList
->NumDevice
= (UINT8
) NumHandles
;
300 for (Index
= 0; Index
< NumHandles
; Index
++) {
301 NetLibGetMacAddress (Handles
[Index
], &MacAddress
, &HwAddressSize
);
303 CurMacInfo
= &DeviceList
->MacInfo
[Index
];
304 CopyMem (&CurMacInfo
->Mac
, MacAddress
.Addr
, HwAddressSize
);
305 CurMacInfo
->Len
= (UINT8
) HwAddressSize
;
306 CurMacInfo
->VlanId
= NetLibGetVlanId (Handles
[Index
]);
312 ISCSI_CONFIG_VAR_ATTR
,
317 FreePool (DeviceList
);
324 Get the iSCSI configuration form entry by the index of the goto opcode actived.
326 @param[in] Index The 0-based index of the goto opcode actived.
328 @return The iSCSI configuration form entry found.
330 ISCSI_CONFIG_FORM_ENTRY
*
331 IScsiGetConfigFormEntryByIndex (
337 ISCSI_CONFIG_FORM_ENTRY
*ConfigFormEntry
;
340 ConfigFormEntry
= NULL
;
342 NET_LIST_FOR_EACH (Entry
, &mIScsiConfigFormList
) {
343 if (CurrentIndex
== Index
) {
344 ConfigFormEntry
= NET_LIST_USER_STRUCT (Entry
, ISCSI_CONFIG_FORM_ENTRY
, Link
);
351 return ConfigFormEntry
;
355 Convert the iSCSI configuration data into the IFR data.
357 @param[in] ConfigFormEntry The iSCSI configuration form entry.
358 @param[out] IfrNvData The IFR nv data.
362 IScsiConvertDeviceConfigDataToIfrNvData (
363 IN ISCSI_CONFIG_FORM_ENTRY
*ConfigFormEntry
,
364 OUT ISCSI_CONFIG_IFR_NVDATA
*IfrNvData
367 ISCSI_SESSION_CONFIG_NVDATA
*SessionConfigData
;
368 ISCSI_CHAP_AUTH_CONFIG_NVDATA
*AuthConfigData
;
371 // Normal session configuration parameters.
373 SessionConfigData
= &ConfigFormEntry
->SessionConfigData
;
374 IfrNvData
->Enabled
= SessionConfigData
->Enabled
;
376 IfrNvData
->InitiatorInfoFromDhcp
= SessionConfigData
->InitiatorInfoFromDhcp
;
377 IfrNvData
->TargetInfoFromDhcp
= SessionConfigData
->TargetInfoFromDhcp
;
378 IfrNvData
->TargetPort
= SessionConfigData
->TargetPort
;
380 IScsiIpToStr (&SessionConfigData
->LocalIp
, IfrNvData
->LocalIp
);
381 IScsiIpToStr (&SessionConfigData
->SubnetMask
, IfrNvData
->SubnetMask
);
382 IScsiIpToStr (&SessionConfigData
->Gateway
, IfrNvData
->Gateway
);
383 IScsiIpToStr (&SessionConfigData
->TargetIp
, IfrNvData
->TargetIp
);
385 IScsiAsciiStrToUnicodeStr (SessionConfigData
->TargetName
, IfrNvData
->TargetName
);
387 IScsiLunToUnicodeStr (SessionConfigData
->BootLun
, IfrNvData
->BootLun
);
389 IScsiConvertIsIdToString (IfrNvData
->IsId
, SessionConfigData
->IsId
);
392 // CHAP authentication parameters.
394 AuthConfigData
= &ConfigFormEntry
->AuthConfigData
;
396 IfrNvData
->CHAPType
= AuthConfigData
->CHAPType
;
398 IScsiAsciiStrToUnicodeStr (AuthConfigData
->CHAPName
, IfrNvData
->CHAPName
);
399 IScsiAsciiStrToUnicodeStr (AuthConfigData
->CHAPSecret
, IfrNvData
->CHAPSecret
);
400 IScsiAsciiStrToUnicodeStr (AuthConfigData
->ReverseCHAPName
, IfrNvData
->ReverseCHAPName
);
401 IScsiAsciiStrToUnicodeStr (AuthConfigData
->ReverseCHAPSecret
, IfrNvData
->ReverseCHAPSecret
);
405 This function allows the caller to request the current
406 configuration for one or more named elements. The resulting
407 string is in <ConfigAltResp> format. Any and all alternative
408 configuration strings shall also be appended to the end of the
409 current configuration string. If they are, they must appear
410 after the current configuration. They must contain the same
411 routing (GUID, NAME, PATH) as the current configuration string.
412 They must have an additional description indicating the type of
413 alternative configuration the string represents,
414 "ALTCFG=<StringToken>". That <StringToken> (when
415 converted from Hex UNICODE to binary) is a reference to a
416 string in the associated string pack.
418 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
419 @param[in] Request A null-terminated Unicode string in
420 <ConfigRequest> format. Note that this
421 includes the routing information as well as
422 the configurable name / value pairs. It is
423 invalid for this string to be in
424 <MultiConfigRequest> format.
425 @param[out] Progress On return, points to a character in the
426 Request string. Points to the string's null
427 terminator if request was successful. Points
428 to the most recent "&" before the first
429 failing name / value pair (or the beginning
430 of the string if the failure is in the first
431 name / value pair) if the request was not
433 @param[out] Results A null-terminated Unicode string in
434 <ConfigAltResp> format which has all values
435 filled in for the names in the Request string.
436 String to be allocated by the called function.
438 @retval EFI_SUCCESS The Results string is filled with the
439 values corresponding to all requested
441 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the
442 parts of the results that must be
443 stored awaiting possible future
445 @retval EFI_INVALID_PARAMETER For example, passing in a NULL
446 for the Request parameter
447 would result in this type of
448 error. In this case, the
449 Progress parameter would be
451 @retval EFI_NOT_FOUND Routing data doesn't match any
452 known driver. Progress set to the
453 first character in the routing header.
454 Note: There is no requirement that the
455 driver validate the routing data. It
456 must skip the <ConfigHdr> in order to
458 @retval EFI_INVALID_PARAMETER Illegal syntax. Progress set
459 to most recent & before the
460 error or the beginning of the
462 @retval EFI_INVALID_PARAMETER Unknown name. Progress points
463 to the & before the name in
464 question.Currently not implemented.
468 IScsiFormExtractConfig (
469 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
470 IN CONST EFI_STRING Request
,
471 OUT EFI_STRING
*Progress
,
472 OUT EFI_STRING
*Results
476 CHAR8 InitiatorName
[ISCSI_NAME_MAX_SIZE
];
478 ISCSI_CONFIG_IFR_NVDATA
*IfrNvData
;
479 ISCSI_FORM_CALLBACK_INFO
*Private
;
480 EFI_HII_CONFIG_ROUTING_PROTOCOL
*HiiConfigRouting
;
481 EFI_STRING ConfigRequestHdr
;
482 EFI_STRING ConfigRequest
;
483 BOOLEAN AllocatedRequest
;
486 if (Progress
== NULL
|| Results
== NULL
) {
487 return EFI_INVALID_PARAMETER
;
491 if ((Request
!= NULL
) && !HiiIsConfigHdrMatch (Request
, &mVendorGuid
, mVendorStorageName
)) {
492 return EFI_NOT_FOUND
;
495 ConfigRequestHdr
= NULL
;
496 ConfigRequest
= NULL
;
497 AllocatedRequest
= FALSE
;
500 if (!mIScsiDeviceListUpdated
) {
502 // Update the device list.
504 IScsiUpdateDeviceList ();
505 mIScsiDeviceListUpdated
= TRUE
;
508 Private
= ISCSI_FORM_CALLBACK_INFO_FROM_FORM_CALLBACK (This
);
509 IfrNvData
= AllocateZeroPool (sizeof (ISCSI_CONFIG_IFR_NVDATA
));
510 ASSERT (IfrNvData
!= NULL
);
511 if (Private
->Current
!= NULL
) {
512 IScsiConvertDeviceConfigDataToIfrNvData (Private
->Current
, IfrNvData
);
515 BufferSize
= ISCSI_NAME_MAX_SIZE
;
516 Status
= gIScsiInitiatorName
.Get (&gIScsiInitiatorName
, &BufferSize
, InitiatorName
);
517 if (EFI_ERROR (Status
)) {
518 IfrNvData
->InitiatorName
[0] = L
'\0';
520 IScsiAsciiStrToUnicodeStr (InitiatorName
, IfrNvData
->InitiatorName
);
524 // Convert buffer data to <ConfigResp> by helper function BlockToConfig()
526 HiiConfigRouting
= Private
->ConfigRouting
;
527 BufferSize
= sizeof (ISCSI_CONFIG_IFR_NVDATA
);
528 ConfigRequest
= Request
;
529 if ((Request
== NULL
) || (StrStr (Request
, L
"OFFSET") == NULL
)) {
531 // Request has no request element, construct full request string.
532 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
533 // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator
535 ConfigRequestHdr
= HiiConstructConfigHdr (&mVendorGuid
, mVendorStorageName
, Private
->DriverHandle
);
536 Size
= (StrLen (ConfigRequestHdr
) + 32 + 1) * sizeof (CHAR16
);
537 ConfigRequest
= AllocateZeroPool (Size
);
538 ASSERT (ConfigRequest
!= NULL
);
539 AllocatedRequest
= TRUE
;
540 UnicodeSPrint (ConfigRequest
, Size
, L
"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr
, (UINT64
)BufferSize
);
541 FreePool (ConfigRequestHdr
);
543 Status
= HiiConfigRouting
->BlockToConfig (
551 FreePool (IfrNvData
);
553 // Free the allocated config request string.
555 if (AllocatedRequest
) {
556 FreePool (ConfigRequest
);
557 ConfigRequest
= NULL
;
561 // Set Progress string to the original request string.
563 if (Request
== NULL
) {
565 } else if (StrStr (Request
, L
"OFFSET") == NULL
) {
566 *Progress
= Request
+ StrLen (Request
);
573 This function applies changes in a driver's configuration.
574 Input is a Configuration, which has the routing data for this
575 driver followed by name / value configuration pairs. The driver
576 must apply those pairs to its configurable storage. If the
577 driver's configuration is stored in a linear block of data
578 and the driver's name / value pairs are in <BlockConfig>
579 format, it may use the ConfigToBlock helper function (above) to
580 simplify the job. Currently not implemented.
582 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
583 @param[in] Configuration A null-terminated Unicode string in
584 <ConfigString> format.
585 @param[out] Progress A pointer to a string filled in with the
586 offset of the most recent '&' before the
587 first failing name / value pair (or the
588 beginn ing of the string if the failure
589 is in the first name / value pair) or
590 the terminating NULL if all was
593 @retval EFI_SUCCESS The results have been distributed or are
594 awaiting distribution.
595 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the
596 parts of the results that must be
597 stored awaiting possible future
599 @retval EFI_INVALID_PARAMETERS Passing in a NULL for the
600 Results parameter would result
601 in this type of error.
602 @retval EFI_NOT_FOUND Target for the specified routing data
607 IScsiFormRouteConfig (
608 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
609 IN CONST EFI_STRING Configuration
,
610 OUT EFI_STRING
*Progress
613 if (Configuration
== NULL
|| Progress
== NULL
) {
614 return EFI_INVALID_PARAMETER
;
618 // Check routing data in <ConfigHdr>.
619 // Note: if only one Storage is used, then this checking could be skipped.
621 if (!HiiIsConfigHdrMatch (Configuration
, &mVendorGuid
, mVendorStorageName
)) {
622 *Progress
= Configuration
;
623 return EFI_NOT_FOUND
;
626 *Progress
= Configuration
+ StrLen (Configuration
);
631 This function is called to provide results data to the driver.
632 This data consists of a unique key that is used to identify
633 which data is either being passed back or being asked for.
635 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
636 @param[in] Action Specifies the type of action taken by the browser.
637 @param[in] QuestionId A unique value which is sent to the original
638 exporting driver so that it can identify the type
639 of data to expect. The format of the data tends to
640 vary based on the opcode that enerated the callback.
641 @param[in] Type The type of value for the question.
642 @param[in] Value A pointer to the data being sent to the original
644 @param[out] ActionRequest On return, points to the action requested by the
647 @retval EFI_SUCCESS The callback successfully handled the action.
648 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the
649 variable and its data.
650 @retval EFI_DEVICE_ERROR The variable could not be saved.
651 @retval EFI_UNSUPPORTED The specified Action is not supported by the
652 callback.Currently not implemented.
653 @retval EFI_INVALID_PARAMETERS Passing in wrong parameter.
654 @retval Others Other errors as indicated.
659 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
660 IN EFI_BROWSER_ACTION Action
,
661 IN EFI_QUESTION_ID QuestionId
,
663 IN EFI_IFR_TYPE_VALUE
*Value
,
664 OUT EFI_BROWSER_ACTION_REQUEST
*ActionRequest
667 ISCSI_FORM_CALLBACK_INFO
*Private
;
669 CHAR8 IScsiName
[ISCSI_NAME_MAX_SIZE
];
670 CHAR16 PortString
[128];
671 CHAR8 Ip4String
[IP4_STR_MAX_SIZE
];
672 CHAR8 LunString
[ISCSI_LUN_STR_MAX_LEN
];
674 EFI_STRING_ID DeviceFormTitleToken
;
675 ISCSI_CONFIG_IFR_NVDATA
*IfrNvData
;
676 ISCSI_CONFIG_FORM_ENTRY
*ConfigFormEntry
;
677 EFI_IP_ADDRESS HostIp
;
678 EFI_IP_ADDRESS SubnetMask
;
679 EFI_IP_ADDRESS Gateway
;
683 if (Action
== EFI_BROWSER_ACTION_CHANGING
) {
684 Private
= ISCSI_FORM_CALLBACK_INFO_FROM_FORM_CALLBACK (This
);
686 // Retrive uncommitted data from Browser
688 IfrNvData
= AllocateZeroPool (sizeof (ISCSI_CONFIG_IFR_NVDATA
));
689 ASSERT (IfrNvData
!= NULL
);
690 if (!HiiGetBrowserData (&mVendorGuid
, mVendorStorageName
, sizeof (ISCSI_CONFIG_IFR_NVDATA
), (UINT8
*) IfrNvData
)) {
691 FreePool (IfrNvData
);
692 return EFI_NOT_FOUND
;
694 Status
= EFI_SUCCESS
;
696 switch (QuestionId
) {
697 case KEY_INITIATOR_NAME
:
698 IScsiUnicodeStrToAsciiStr (IfrNvData
->InitiatorName
, IScsiName
);
699 BufferSize
= AsciiStrSize (IScsiName
);
701 Status
= gIScsiInitiatorName
.Set (&gIScsiInitiatorName
, &BufferSize
, IScsiName
);
702 if (EFI_ERROR (Status
)) {
703 CreatePopUp (EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
, &Key
, L
"Invalid iSCSI Name!", NULL
);
709 IScsiUnicodeStrToAsciiStr (IfrNvData
->LocalIp
, Ip4String
);
710 Status
= IScsiAsciiStrToIp (Ip4String
, &HostIp
.v4
);
711 if (EFI_ERROR (Status
) || !NetIp4IsUnicast (NTOHL (HostIp
.Addr
[0]), 0)) {
712 CreatePopUp (EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
, &Key
, L
"Invalid IP address!", NULL
);
713 Status
= EFI_INVALID_PARAMETER
;
715 CopyMem (&Private
->Current
->SessionConfigData
.LocalIp
, &HostIp
.v4
, sizeof (HostIp
.v4
));
720 case KEY_SUBNET_MASK
:
721 IScsiUnicodeStrToAsciiStr (IfrNvData
->SubnetMask
, Ip4String
);
722 Status
= IScsiAsciiStrToIp (Ip4String
, &SubnetMask
.v4
);
723 if (EFI_ERROR (Status
) || ((SubnetMask
.Addr
[0] != 0) && (IScsiGetSubnetMaskPrefixLength (&SubnetMask
.v4
) == 0))) {
724 CreatePopUp (EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
, &Key
, L
"Invalid Subnet Mask!", NULL
);
725 Status
= EFI_INVALID_PARAMETER
;
727 CopyMem (&Private
->Current
->SessionConfigData
.SubnetMask
, &SubnetMask
.v4
, sizeof (SubnetMask
.v4
));
733 IScsiUnicodeStrToAsciiStr (IfrNvData
->Gateway
, Ip4String
);
734 Status
= IScsiAsciiStrToIp (Ip4String
, &Gateway
.v4
);
735 if (EFI_ERROR (Status
) || ((Gateway
.Addr
[0] != 0) && !NetIp4IsUnicast (NTOHL (Gateway
.Addr
[0]), 0))) {
736 CreatePopUp (EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
, &Key
, L
"Invalid Gateway!", NULL
);
737 Status
= EFI_INVALID_PARAMETER
;
739 CopyMem (&Private
->Current
->SessionConfigData
.Gateway
, &Gateway
.v4
, sizeof (Gateway
.v4
));
745 IScsiUnicodeStrToAsciiStr (IfrNvData
->TargetIp
, Ip4String
);
746 Status
= IScsiAsciiStrToIp (Ip4String
, &HostIp
.v4
);
747 if (EFI_ERROR (Status
) || !NetIp4IsUnicast (NTOHL (HostIp
.Addr
[0]), 0)) {
748 CreatePopUp (EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
, &Key
, L
"Invalid IP address!", NULL
);
749 Status
= EFI_INVALID_PARAMETER
;
751 CopyMem (&Private
->Current
->SessionConfigData
.TargetIp
, &HostIp
.v4
, sizeof (HostIp
.v4
));
756 case KEY_TARGET_NAME
:
757 IScsiUnicodeStrToAsciiStr (IfrNvData
->TargetName
, IScsiName
);
758 Status
= IScsiNormalizeName (IScsiName
, AsciiStrLen (IScsiName
));
759 if (EFI_ERROR (Status
)) {
760 CreatePopUp (EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
, &Key
, L
"Invalid iSCSI Name!", NULL
);
762 AsciiStrCpy (Private
->Current
->SessionConfigData
.TargetName
, IScsiName
);
767 case KEY_DHCP_ENABLE
:
768 if (IfrNvData
->InitiatorInfoFromDhcp
== 0) {
769 IfrNvData
->TargetInfoFromDhcp
= 0;
775 IScsiUnicodeStrToAsciiStr (IfrNvData
->BootLun
, LunString
);
776 Status
= IScsiAsciiStrToLun (LunString
, (UINT8
*) &Lun
);
777 if (EFI_ERROR (Status
)) {
778 CreatePopUp (EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
, &Key
, L
"Invalid LUN string!", NULL
);
780 CopyMem (Private
->Current
->SessionConfigData
.BootLun
, &Lun
, sizeof (Lun
));
786 IScsiUnicodeStrToAsciiStr (IfrNvData
->CHAPName
, Private
->Current
->AuthConfigData
.CHAPName
);
789 case KEY_CHAP_SECRET
:
790 IScsiUnicodeStrToAsciiStr (IfrNvData
->CHAPSecret
, Private
->Current
->AuthConfigData
.CHAPSecret
);
793 case KEY_REVERSE_CHAP_NAME
:
794 IScsiUnicodeStrToAsciiStr (IfrNvData
->ReverseCHAPName
, Private
->Current
->AuthConfigData
.ReverseCHAPName
);
797 case KEY_REVERSE_CHAP_SECRET
:
798 IScsiUnicodeStrToAsciiStr (IfrNvData
->ReverseCHAPSecret
, Private
->Current
->AuthConfigData
.ReverseCHAPSecret
);
801 case KEY_CONFIG_ISID
:
802 IScsiParseIsIdFromString (IfrNvData
->IsId
, Private
->Current
->SessionConfigData
.IsId
);
803 IScsiConvertIsIdToString (IfrNvData
->IsId
, Private
->Current
->SessionConfigData
.IsId
);
807 case KEY_SAVE_CHANGES
:
809 // First, update those fields which don't have INTERACTIVE set.
811 Private
->Current
->SessionConfigData
.Enabled
= IfrNvData
->Enabled
;
812 Private
->Current
->SessionConfigData
.InitiatorInfoFromDhcp
= IfrNvData
->InitiatorInfoFromDhcp
;
813 Private
->Current
->SessionConfigData
.TargetPort
= IfrNvData
->TargetPort
;
814 if (Private
->Current
->SessionConfigData
.TargetPort
== 0) {
815 Private
->Current
->SessionConfigData
.TargetPort
= ISCSI_WELL_KNOWN_PORT
;
818 Private
->Current
->SessionConfigData
.TargetInfoFromDhcp
= IfrNvData
->TargetInfoFromDhcp
;
819 Private
->Current
->AuthConfigData
.CHAPType
= IfrNvData
->CHAPType
;
822 // Only do full parameter validation if iSCSI is enabled on this device.
824 if (Private
->Current
->SessionConfigData
.Enabled
) {
826 // Validate the address configuration of the Initiator if DHCP isn't
829 if (!Private
->Current
->SessionConfigData
.InitiatorInfoFromDhcp
) {
830 CopyMem (&HostIp
.v4
, &Private
->Current
->SessionConfigData
.LocalIp
, sizeof (HostIp
.v4
));
831 CopyMem (&SubnetMask
.v4
, &Private
->Current
->SessionConfigData
.SubnetMask
, sizeof (SubnetMask
.v4
));
832 CopyMem (&Gateway
.v4
, &Private
->Current
->SessionConfigData
.Gateway
, sizeof (Gateway
.v4
));
834 if ((Gateway
.Addr
[0] != 0)) {
835 if (SubnetMask
.Addr
[0] == 0) {
836 CreatePopUp (EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
, &Key
, L
"Gateway address is set but subnet mask is zero.", NULL
);
837 Status
= EFI_INVALID_PARAMETER
;
839 } else if (!IP4_NET_EQUAL (HostIp
.Addr
[0], Gateway
.Addr
[0], SubnetMask
.Addr
[0])) {
840 CreatePopUp (EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
, &Key
, L
"Local IP and Gateway are not in the same subnet.", NULL
);
841 Status
= EFI_INVALID_PARAMETER
;
847 // Validate target configuration if DHCP isn't deployed.
849 if (!Private
->Current
->SessionConfigData
.TargetInfoFromDhcp
) {
850 CopyMem (&HostIp
.v4
, &Private
->Current
->SessionConfigData
.TargetIp
, sizeof (HostIp
.v4
));
851 if (!NetIp4IsUnicast (NTOHL (HostIp
.Addr
[0]), 0)) {
852 CreatePopUp (EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
, &Key
, L
"Target IP is invalid!", NULL
);
853 Status
= EFI_INVALID_PARAMETER
;
858 if (IfrNvData
->CHAPType
!= ISCSI_CHAP_NONE
) {
859 if ((IfrNvData
->CHAPName
[0] == '\0') || (IfrNvData
->CHAPSecret
[0] == '\0')) {
860 CreatePopUp (EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
, &Key
, L
"CHAP Name or CHAP Secret is invalid!", NULL
);
861 Status
= EFI_INVALID_PARAMETER
;
865 if ((IfrNvData
->CHAPType
== ISCSI_CHAP_MUTUAL
) &&
866 ((IfrNvData
->ReverseCHAPName
[0] == '\0') || (IfrNvData
->ReverseCHAPSecret
[0] == '\0'))
868 CreatePopUp (EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
, &Key
, L
"Reverse CHAP Name or Reverse CHAP Secret is invalid!", NULL
);
869 Status
= EFI_INVALID_PARAMETER
;
875 BufferSize
= sizeof (Private
->Current
->SessionConfigData
);
877 Private
->Current
->MacString
,
878 &gEfiIScsiInitiatorNameProtocolGuid
,
879 ISCSI_CONFIG_VAR_ATTR
,
881 &Private
->Current
->SessionConfigData
884 BufferSize
= sizeof (Private
->Current
->AuthConfigData
);
886 Private
->Current
->MacString
,
887 &mIScsiCHAPAuthInfoGuid
,
888 ISCSI_CONFIG_VAR_ATTR
,
890 &Private
->Current
->AuthConfigData
892 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_APPLY
;
896 if ((QuestionId
>= KEY_DEVICE_ENTRY_BASE
) && (QuestionId
< (mNumberOfIScsiDevices
+ KEY_DEVICE_ENTRY_BASE
))) {
898 // In case goto the device configuration form, update the device form title.
900 ConfigFormEntry
= IScsiGetConfigFormEntryByIndex ((UINT32
) (QuestionId
- KEY_DEVICE_ENTRY_BASE
));
901 ASSERT (ConfigFormEntry
!= NULL
);
903 UnicodeSPrint (PortString
, (UINTN
) 128, L
"Port %s", ConfigFormEntry
->MacString
);
904 DeviceFormTitleToken
= (EFI_STRING_ID
) STR_ISCSI_DEVICE_FORM_TITLE
;
905 HiiSetString (Private
->RegisteredHandle
, DeviceFormTitleToken
, PortString
, NULL
);
907 IScsiConvertDeviceConfigDataToIfrNvData (ConfigFormEntry
, IfrNvData
);
909 Private
->Current
= ConfigFormEntry
;
915 if (!EFI_ERROR (Status
)) {
917 // Pass changed uncommitted data back to Form Browser
919 HiiSetBrowserData (&mVendorGuid
, mVendorStorageName
, sizeof (ISCSI_CONFIG_IFR_NVDATA
), (UINT8
*) IfrNvData
, NULL
);
922 FreePool (IfrNvData
);
928 // All other action return unsupported.
930 return EFI_UNSUPPORTED
;
934 Updates the iSCSI configuration form to add/delete an entry for the iSCSI
935 device specified by the Controller.
937 @param[in] DriverBindingHandle The driverbinding handle.
938 @param[in] Controller The controller handle of the iSCSI device.
939 @param[in] AddForm Whether to add or delete a form entry.
941 @retval EFI_SUCCESS The iSCSI configuration form is updated.
942 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
943 @retval Others Other errors as indicated.
946 IScsiConfigUpdateForm (
947 IN EFI_HANDLE DriverBindingHandle
,
948 IN EFI_HANDLE Controller
,
953 ISCSI_CONFIG_FORM_ENTRY
*ConfigFormEntry
;
954 BOOLEAN EntryExisted
;
956 EFI_MAC_ADDRESS MacAddress
;
959 CHAR16 PortString
[128];
962 VOID
*StartOpCodeHandle
;
963 VOID
*EndOpCodeHandle
;
964 EFI_IFR_GUID_LABEL
*StartLabel
;
965 EFI_IFR_GUID_LABEL
*EndLabel
;
967 ConfigFormEntry
= NULL
;
968 EntryExisted
= FALSE
;
970 NET_LIST_FOR_EACH (Entry
, &mIScsiConfigFormList
) {
971 ConfigFormEntry
= NET_LIST_USER_STRUCT (Entry
, ISCSI_CONFIG_FORM_ENTRY
, Link
);
973 if (ConfigFormEntry
->Controller
== Controller
) {
986 ConfigFormEntry
= (ISCSI_CONFIG_FORM_ENTRY
*) AllocateZeroPool (sizeof (ISCSI_CONFIG_FORM_ENTRY
));
987 if (ConfigFormEntry
== NULL
) {
988 return EFI_OUT_OF_RESOURCES
;
991 InitializeListHead (&ConfigFormEntry
->Link
);
992 ConfigFormEntry
->Controller
= Controller
;
995 // Get the MAC address and convert it into the formatted string.
997 Status
= NetLibGetMacAddress (Controller
, &MacAddress
, &HwAddressSize
);
998 ASSERT (Status
== EFI_SUCCESS
);
999 VlanId
= NetLibGetVlanId (Controller
);
1001 IScsiMacAddrToStr (&MacAddress
, (UINT32
) HwAddressSize
, VlanId
, ConfigFormEntry
->MacString
);
1004 // Get the normal session configuration data.
1006 BufferSize
= sizeof (ConfigFormEntry
->SessionConfigData
);
1007 Status
= gRT
->GetVariable (
1008 ConfigFormEntry
->MacString
,
1009 &gEfiIScsiInitiatorNameProtocolGuid
,
1012 &ConfigFormEntry
->SessionConfigData
1014 if (EFI_ERROR (Status
)) {
1015 ZeroMem (&ConfigFormEntry
->SessionConfigData
, sizeof (ConfigFormEntry
->SessionConfigData
));
1018 // Generate OUI-format ISID based on MAC address.
1020 CopyMem (ConfigFormEntry
->SessionConfigData
.IsId
, &MacAddress
, 6);
1021 ConfigFormEntry
->SessionConfigData
.IsId
[0] =
1022 (UINT8
) (ConfigFormEntry
->SessionConfigData
.IsId
[0] & 0x3F);
1025 // Get the CHAP authentication configuration data.
1027 BufferSize
= sizeof (ConfigFormEntry
->AuthConfigData
);
1028 Status
= gRT
->GetVariable (
1029 ConfigFormEntry
->MacString
,
1030 &mIScsiCHAPAuthInfoGuid
,
1033 &ConfigFormEntry
->AuthConfigData
1035 if (EFI_ERROR (Status
)) {
1036 ZeroMem (&ConfigFormEntry
->AuthConfigData
, sizeof (ConfigFormEntry
->AuthConfigData
));
1039 // Compose the Port string and create a new EFI_STRING_ID.
1041 UnicodeSPrint (PortString
, 128, L
"Port %s", ConfigFormEntry
->MacString
);
1042 ConfigFormEntry
->PortTitleToken
= HiiSetString (mCallbackInfo
->RegisteredHandle
, 0, PortString
, NULL
);
1045 // Compose the help string of this port and create a new EFI_STRING_ID.
1047 UnicodeSPrint (PortString
, 128, L
"Set the iSCSI parameters on port %s", ConfigFormEntry
->MacString
);
1048 ConfigFormEntry
->PortTitleHelpToken
= HiiSetString (mCallbackInfo
->RegisteredHandle
, 0, PortString
, NULL
);
1050 InsertTailList (&mIScsiConfigFormList
, &ConfigFormEntry
->Link
);
1051 mNumberOfIScsiDevices
++;
1054 ASSERT (EntryExisted
);
1056 mNumberOfIScsiDevices
--;
1057 RemoveEntryList (&ConfigFormEntry
->Link
);
1058 FreePool (ConfigFormEntry
);
1061 // Allocate space for creation of Buffer
1065 // Init OpCode Handle
1067 StartOpCodeHandle
= HiiAllocateOpCodeHandle ();
1068 ASSERT (StartOpCodeHandle
!= NULL
);
1070 EndOpCodeHandle
= HiiAllocateOpCodeHandle ();
1071 ASSERT (EndOpCodeHandle
!= NULL
);
1074 // Create Hii Extend Label OpCode as the start opcode
1076 StartLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (StartOpCodeHandle
, &gEfiIfrTianoGuid
, NULL
, sizeof (EFI_IFR_GUID_LABEL
));
1077 StartLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
1078 StartLabel
->Number
= DEVICE_ENTRY_LABEL
;
1081 // Create Hii Extend Label OpCode as the end opcode
1083 EndLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (EndOpCodeHandle
, &gEfiIfrTianoGuid
, NULL
, sizeof (EFI_IFR_GUID_LABEL
));
1084 EndLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
1085 EndLabel
->Number
= LABEL_END
;
1088 NET_LIST_FOR_EACH (Entry
, &mIScsiConfigFormList
) {
1089 ConfigFormEntry
= NET_LIST_USER_STRUCT (Entry
, ISCSI_CONFIG_FORM_ENTRY
, Link
);
1091 HiiCreateGotoOpCode (
1092 StartOpCodeHandle
, // Container for dynamic created opcodes
1093 FORMID_DEVICE_FORM
, // Target Form ID
1094 ConfigFormEntry
->PortTitleToken
, // Prompt text
1095 ConfigFormEntry
->PortTitleHelpToken
, // Help text
1096 EFI_IFR_FLAG_CALLBACK
, // Question flag
1097 (UINT16
)(KEY_DEVICE_ENTRY_BASE
+ FormIndex
) // Question ID
1104 mCallbackInfo
->RegisteredHandle
,
1107 StartOpCodeHandle
, // Label DEVICE_ENTRY_LABEL
1108 EndOpCodeHandle
// LABEL_END
1111 HiiFreeOpCodeHandle (StartOpCodeHandle
);
1112 HiiFreeOpCodeHandle (EndOpCodeHandle
);
1118 Initialize the iSCSI configuration form.
1120 @param[in] DriverBindingHandle The iSCSI driverbinding handle.
1122 @retval EFI_SUCCESS The iSCSI configuration form is initialized.
1123 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1124 @retval Others Other errors as indicated.
1127 IScsiConfigFormInit (
1132 EFI_HII_DATABASE_PROTOCOL
*HiiDatabase
;
1133 ISCSI_FORM_CALLBACK_INFO
*CallbackInfo
;
1135 Status
= gBS
->LocateProtocol (&gEfiHiiDatabaseProtocolGuid
, NULL
, (VOID
**)&HiiDatabase
);
1136 if (EFI_ERROR (Status
)) {
1140 CallbackInfo
= (ISCSI_FORM_CALLBACK_INFO
*) AllocateZeroPool (sizeof (ISCSI_FORM_CALLBACK_INFO
));
1141 if (CallbackInfo
== NULL
) {
1142 return EFI_OUT_OF_RESOURCES
;
1145 CallbackInfo
->Signature
= ISCSI_FORM_CALLBACK_INFO_SIGNATURE
;
1146 CallbackInfo
->HiiDatabase
= HiiDatabase
;
1147 CallbackInfo
->Current
= NULL
;
1149 CallbackInfo
->ConfigAccess
.ExtractConfig
= IScsiFormExtractConfig
;
1150 CallbackInfo
->ConfigAccess
.RouteConfig
= IScsiFormRouteConfig
;
1151 CallbackInfo
->ConfigAccess
.Callback
= IScsiFormCallback
;
1153 Status
= gBS
->LocateProtocol (&gEfiHiiConfigRoutingProtocolGuid
, NULL
, (VOID
**)&CallbackInfo
->ConfigRouting
);
1154 if (EFI_ERROR (Status
)) {
1155 FreePool(CallbackInfo
);
1160 // Install Device Path Protocol and Config Access protocol to driver handle
1162 Status
= gBS
->InstallMultipleProtocolInterfaces (
1163 &CallbackInfo
->DriverHandle
,
1164 &gEfiDevicePathProtocolGuid
,
1165 &mIScsiHiiVendorDevicePath
,
1166 &gEfiHiiConfigAccessProtocolGuid
,
1167 &CallbackInfo
->ConfigAccess
,
1170 ASSERT_EFI_ERROR (Status
);
1173 // Publish our HII data
1175 CallbackInfo
->RegisteredHandle
= HiiAddPackages (
1177 CallbackInfo
->DriverHandle
,
1182 if (CallbackInfo
->RegisteredHandle
== NULL
) {
1183 FreePool(CallbackInfo
);
1184 return EFI_OUT_OF_RESOURCES
;
1187 mCallbackInfo
= CallbackInfo
;
1193 Unload the iSCSI configuration form, this includes: delete all the iSCSI
1194 device configuration entries, uninstall the form callback protocol and
1195 free the resources used.
1197 @param[in] DriverBindingHandle The iSCSI driverbinding handle.
1199 @retval EFI_SUCCESS The iSCSI configuration form is unloaded.
1200 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1203 IScsiConfigFormUnload (
1204 IN EFI_HANDLE DriverBindingHandle
1207 ISCSI_CONFIG_FORM_ENTRY
*ConfigFormEntry
;
1209 while (!IsListEmpty (&mIScsiConfigFormList
)) {
1211 // Uninstall the device forms as the iSCSI driver instance may fail to
1212 // control the controller but still install the device configuration form.
1213 // In such case, upon driver unloading, the driver instance's driverbinding.
1214 // stop () won't be called, so we have to take this chance here to uninstall
1217 ConfigFormEntry
= NET_LIST_USER_STRUCT (mIScsiConfigFormList
.ForwardLink
, ISCSI_CONFIG_FORM_ENTRY
, Link
);
1218 IScsiConfigUpdateForm (DriverBindingHandle
, ConfigFormEntry
->Controller
, FALSE
);
1222 // Remove HII package list
1224 mCallbackInfo
->HiiDatabase
->RemovePackageList (
1225 mCallbackInfo
->HiiDatabase
,
1226 mCallbackInfo
->RegisteredHandle
1230 // Uninstall EFI_HII_CONFIG_ACCESS_PROTOCOL
1232 gBS
->UninstallMultipleProtocolInterfaces (
1233 mCallbackInfo
->DriverHandle
,
1234 &gEfiDevicePathProtocolGuid
,
1235 &mIScsiHiiVendorDevicePath
,
1236 &gEfiHiiConfigAccessProtocolGuid
,
1237 &mCallbackInfo
->ConfigAccess
,
1240 FreePool (mCallbackInfo
);