]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/IScsiDxe/IScsiConfig.c
fixed typo. EFI_OUT_OF_MEMORY should be EFI_OUT_OF_RESOURCES.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / IScsiDxe / IScsiConfig.c
1 /** @file
2 Helper functions for configuring or getting the parameters relating to iSCSI.
3
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
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 #include "IScsiImpl.h"
16
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;
22
23 LIST_ENTRY mIScsiConfigFormList = {
24 &mIScsiConfigFormList,
25 &mIScsiConfigFormList
26 };
27
28 HII_VENDOR_DEVICE_PATH mIScsiHiiVendorDevicePath = {
29 {
30 {
31 HARDWARE_DEVICE_PATH,
32 HW_VENDOR_DP,
33 {
34 (UINT8) (sizeof (VENDOR_DEVICE_PATH)),
35 (UINT8) ((sizeof (VENDOR_DEVICE_PATH)) >> 8)
36 }
37 },
38 //
39 // {49D7B73E-143D-4716-977B-C45F1CB038CC}
40 //
41 { 0x49d7b73e, 0x143d, 0x4716, { 0x97, 0x7b, 0xc4, 0x5f, 0x1c, 0xb0, 0x38, 0xcc } }
42 },
43 {
44 END_DEVICE_PATH_TYPE,
45 END_ENTIRE_DEVICE_PATH_SUBTYPE,
46 {
47 (UINT8) (END_DEVICE_PATH_LENGTH),
48 (UINT8) ((END_DEVICE_PATH_LENGTH) >> 8)
49 }
50 }
51 };
52
53 /**
54 Convert the IPv4 address into a dotted string.
55
56 @param[in] Ip The IPv4 address.
57 @param[out] Str The dotted IP string.
58 **/
59 VOID
60 IScsiIpToStr (
61 IN EFI_IPv4_ADDRESS *Ip,
62 OUT CHAR16 *Str
63 )
64 {
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]);
66 }
67
68 /**
69 Update the list of iSCSI devices the iSCSI driver is controlling.
70
71 @retval EFI_SUCCESS The callback successfully handled the action.
72 @retval Others Other errors as indicated.
73 **/
74 EFI_STATUS
75 IScsiUpdateDeviceList (
76 VOID
77 )
78 {
79 EFI_STATUS Status;
80 ISCSI_DEVICE_LIST *DeviceList;
81 UINTN DataSize;
82 UINTN NumHandles;
83 EFI_HANDLE *Handles;
84 UINTN HandleIndex;
85 UINTN Index;
86 UINTN LastDeviceIndex;
87 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
88 EFI_SIMPLE_NETWORK_MODE *Mode;
89 ISCSI_MAC_INFO *CurMacInfo;
90 ISCSI_MAC_INFO TempMacInfo;
91 CHAR16 MacString[65];
92 UINTN DeviceListSize;
93
94 //
95 // Dump all the handles the Simple Network Protocol is installed on.
96 //
97 Status = gBS->LocateHandleBuffer (
98 ByProtocol,
99 &gEfiSimpleNetworkProtocolGuid,
100 NULL,
101 &NumHandles,
102 &Handles
103 );
104 if (EFI_ERROR (Status)) {
105 return Status;
106 }
107
108 DataSize = 0;
109 Status = gRT->GetVariable (
110 L"iSCSIDeviceList",
111 &mVendorGuid,
112 NULL,
113 &DataSize,
114 NULL
115 );
116 if (Status == EFI_BUFFER_TOO_SMALL) {
117 DeviceList = (ISCSI_DEVICE_LIST *) AllocatePool (DataSize);
118
119 gRT->GetVariable (
120 L"iSCSIDeviceList",
121 &mVendorGuid,
122 NULL,
123 &DataSize,
124 DeviceList
125 );
126
127 LastDeviceIndex = 0;
128
129 for (HandleIndex = 0; HandleIndex < NumHandles; HandleIndex++) {
130 gBS->HandleProtocol (Handles[HandleIndex], &gEfiSimpleNetworkProtocolGuid, (VOID **)&Snp);
131
132 Mode = Snp->Mode;
133
134 for (Index = LastDeviceIndex; Index < DeviceList->NumDevice; Index++) {
135 CurMacInfo = &DeviceList->MacInfo[Index];
136 if ((CurMacInfo->Len == Mode->HwAddressSize) &&
137 (NET_MAC_EQUAL (&CurMacInfo->Mac, &Mode->PermanentAddress, Mode->HwAddressSize))
138 ) {
139 //
140 // The previous configured NIC is still here.
141 //
142 if (Index != LastDeviceIndex) {
143 //
144 // Swap the current MAC address entry with the one indexed by
145 // LastDeviceIndex.
146 //
147 CopyMem (&TempMacInfo, CurMacInfo, sizeof (ISCSI_MAC_INFO));
148 CopyMem (CurMacInfo, &DeviceList->MacInfo[LastDeviceIndex], sizeof (ISCSI_MAC_INFO));
149 CopyMem (&DeviceList->MacInfo[LastDeviceIndex], &TempMacInfo, sizeof (ISCSI_MAC_INFO));
150 }
151
152 LastDeviceIndex++;
153 }
154 }
155
156 if (LastDeviceIndex == DeviceList->NumDevice) {
157 break;
158 }
159 }
160
161 for (Index = LastDeviceIndex; Index < DeviceList->NumDevice; Index++) {
162 //
163 // delete the variables
164 //
165 CurMacInfo = &DeviceList->MacInfo[Index];
166 IScsiMacAddrToStr (&CurMacInfo->Mac, CurMacInfo->Len, MacString);
167 gRT->SetVariable (MacString, &gEfiIScsiInitiatorNameProtocolGuid, 0, 0, NULL);
168 gRT->SetVariable (MacString, &mIScsiCHAPAuthInfoGuid, 0, 0, NULL);
169 }
170
171 gBS->FreePool (DeviceList);
172 } else if (Status != EFI_NOT_FOUND) {
173 gBS->FreePool (Handles);
174 return Status;
175 }
176 //
177 // Construct the new iSCSI device list.
178 //
179 DeviceListSize = sizeof (ISCSI_DEVICE_LIST) + (NumHandles - 1) * sizeof (ISCSI_MAC_INFO);
180 DeviceList = (ISCSI_DEVICE_LIST *) AllocatePool (DeviceListSize);
181 DeviceList->NumDevice = (UINT8) NumHandles;
182
183 for (Index = 0; Index < NumHandles; Index++) {
184 gBS->HandleProtocol (Handles[Index], &gEfiSimpleNetworkProtocolGuid, (VOID **)&Snp);
185 Mode = Snp->Mode;
186
187 CurMacInfo = &DeviceList->MacInfo[Index];
188 CopyMem (&CurMacInfo->Mac, &Mode->PermanentAddress, Mode->HwAddressSize);
189 CurMacInfo->Len = (UINT8) Mode->HwAddressSize;
190 }
191
192 gRT->SetVariable (
193 L"iSCSIDeviceList",
194 &mVendorGuid,
195 ISCSI_CONFIG_VAR_ATTR,
196 DeviceListSize,
197 DeviceList
198 );
199
200 gBS->FreePool (DeviceList);
201 gBS->FreePool (Handles);
202
203 return Status;
204 }
205
206 /**
207 Get the iSCSI configuration form entry by the index of the goto opcode actived.
208
209 @param[in] Index The 0-based index of the goto opcode actived.
210
211 @return The iSCSI configuration form entry found.
212 **/
213 ISCSI_CONFIG_FORM_ENTRY *
214 IScsiGetConfigFormEntryByIndex (
215 IN UINT32 Index
216 )
217 {
218 UINT32 CurrentIndex;
219 LIST_ENTRY *Entry;
220 ISCSI_CONFIG_FORM_ENTRY *ConfigFormEntry;
221
222 CurrentIndex = 0;
223 ConfigFormEntry = NULL;
224
225 NET_LIST_FOR_EACH (Entry, &mIScsiConfigFormList) {
226 if (CurrentIndex == Index) {
227 ConfigFormEntry = NET_LIST_USER_STRUCT (Entry, ISCSI_CONFIG_FORM_ENTRY, Link);
228 break;
229 }
230
231 CurrentIndex++;
232 }
233
234 return ConfigFormEntry;
235 }
236
237 /**
238 Convert the iSCSI configuration data into the IFR data.
239
240 @param[in] ConfigFormEntry The iSCSI configuration form entry.
241 @param[out] IfrNvData The IFR nv data.
242 **/
243 VOID
244 IScsiConvertDeviceConfigDataToIfrNvData (
245 IN ISCSI_CONFIG_FORM_ENTRY *ConfigFormEntry,
246 OUT ISCSI_CONFIG_IFR_NVDATA *IfrNvData
247 )
248 {
249 ISCSI_SESSION_CONFIG_NVDATA *SessionConfigData;
250 ISCSI_CHAP_AUTH_CONFIG_NVDATA *AuthConfigData;
251
252 //
253 // Normal session configuration parameters.
254 //
255 SessionConfigData = &ConfigFormEntry->SessionConfigData;
256 IfrNvData->Enabled = SessionConfigData->Enabled;
257
258 IfrNvData->InitiatorInfoFromDhcp = SessionConfigData->InitiatorInfoFromDhcp;
259 IfrNvData->TargetInfoFromDhcp = SessionConfigData->TargetInfoFromDhcp;
260 IfrNvData->TargetPort = SessionConfigData->TargetPort;
261
262 IScsiIpToStr (&SessionConfigData->LocalIp, IfrNvData->LocalIp);
263 IScsiIpToStr (&SessionConfigData->SubnetMask, IfrNvData->SubnetMask);
264 IScsiIpToStr (&SessionConfigData->Gateway, IfrNvData->Gateway);
265 IScsiIpToStr (&SessionConfigData->TargetIp, IfrNvData->TargetIp);
266
267 IScsiAsciiStrToUnicodeStr (SessionConfigData->TargetName, IfrNvData->TargetName);
268
269 IScsiLunToUnicodeStr (SessionConfigData->BootLun, IfrNvData->BootLun);
270
271 //
272 // CHAP authentication parameters.
273 //
274 AuthConfigData = &ConfigFormEntry->AuthConfigData;
275
276 IfrNvData->CHAPType = AuthConfigData->CHAPType;
277
278 IScsiAsciiStrToUnicodeStr (AuthConfigData->CHAPName, IfrNvData->CHAPName);
279 IScsiAsciiStrToUnicodeStr (AuthConfigData->CHAPSecret, IfrNvData->CHAPSecret);
280 IScsiAsciiStrToUnicodeStr (AuthConfigData->ReverseCHAPName, IfrNvData->ReverseCHAPName);
281 IScsiAsciiStrToUnicodeStr (AuthConfigData->ReverseCHAPSecret, IfrNvData->ReverseCHAPSecret);
282 }
283
284 /**
285 This function allows the caller to request the current
286 configuration for one or more named elements. The resulting
287 string is in <ConfigAltResp> format. Any and all alternative
288 configuration strings shall also be appended to the end of the
289 current configuration string. If they are, they must appear
290 after the current configuration. They must contain the same
291 routing (GUID, NAME, PATH) as the current configuration string.
292 They must have an additional description indicating the type of
293 alternative configuration the string represents,
294 "ALTCFG=<StringToken>". That <StringToken> (when
295 converted from Hex UNICODE to binary) is a reference to a
296 string in the associated string pack.
297
298 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
299 @param[in] Request A null-terminated Unicode string in
300 <ConfigRequest> format. Note that this
301 includes the routing information as well as
302 the configurable name / value pairs. It is
303 invalid for this string to be in
304 <MultiConfigRequest> format.
305 @param[out] Progress On return, points to a character in the
306 Request string. Points to the string's null
307 terminator if request was successful. Points
308 to the most recent "&" before the first
309 failing name / value pair (or the beginning
310 of the string if the failure is in the first
311 name / value pair) if the request was not
312 successful.
313 @param[out] Results A null-terminated Unicode string in
314 <ConfigAltResp> format which has all values
315 filled in for the names in the Request string.
316 String to be allocated by the called function.
317
318 @retval EFI_SUCCESS The Results string is filled with the
319 values corresponding to all requested
320 names.
321 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the
322 parts of the results that must be
323 stored awaiting possible future
324 protocols.
325 @retval EFI_INVALID_PARAMETER For example, passing in a NULL
326 for the Request parameter
327 would result in this type of
328 error. In this case, the
329 Progress parameter would be
330 set to NULL.
331 @retval EFI_NOT_FOUND Routing data doesn't match any
332 known driver. Progress set to the
333 first character in the routing header.
334 Note: There is no requirement that the
335 driver validate the routing data. It
336 must skip the <ConfigHdr> in order to
337 process the names.
338 @retval EFI_INVALID_PARAMETER Illegal syntax. Progress set
339 to most recent & before the
340 error or the beginning of the
341 string.
342 @retval EFI_INVALID_PARAMETER Unknown name. Progress points
343 to the & before the name in
344 question.Currently not implemented.
345 **/
346 EFI_STATUS
347 EFIAPI
348 IScsiFormExtractConfig (
349 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
350 IN CONST EFI_STRING Request,
351 OUT EFI_STRING *Progress,
352 OUT EFI_STRING *Results
353 )
354 {
355 EFI_STATUS Status;
356 CHAR8 InitiatorName[ISCSI_NAME_IFR_MAX_SIZE];
357 UINTN BufferSize;
358 ISCSI_CONFIG_IFR_NVDATA *IfrNvData;
359 ISCSI_FORM_CALLBACK_INFO *Private;
360 EFI_HII_CONFIG_ROUTING_PROTOCOL *HiiConfigRouting;
361
362 if (Request == NULL) {
363 return EFI_INVALID_PARAMETER;
364 }
365
366 if (!mIScsiDeviceListUpdated) {
367 //
368 // Update the device list.
369 //
370 IScsiUpdateDeviceList ();
371 mIScsiDeviceListUpdated = TRUE;
372 }
373
374 Private = ISCSI_FORM_CALLBACK_INFO_FROM_FORM_CALLBACK (This);
375 IfrNvData = AllocateZeroPool (sizeof (ISCSI_CONFIG_IFR_NVDATA));
376 ASSERT (IfrNvData != NULL);
377 if (Private->Current != NULL) {
378 IScsiConvertDeviceConfigDataToIfrNvData (Private->Current, IfrNvData);
379 }
380
381 BufferSize = ISCSI_NAME_IFR_MAX_SIZE;
382 Status = gIScsiInitiatorName.Get (&gIScsiInitiatorName, &BufferSize, InitiatorName);
383 if (EFI_ERROR (Status)) {
384 IfrNvData->InitiatorName[0] = L'\0';
385 } else {
386 IScsiAsciiStrToUnicodeStr (InitiatorName, IfrNvData->InitiatorName);
387 }
388
389 //
390 // Convert buffer data to <ConfigResp> by helper function BlockToConfig()
391 //
392 HiiConfigRouting = Private->ConfigRouting;
393 BufferSize = sizeof (ISCSI_CONFIG_IFR_NVDATA);
394 Status = HiiConfigRouting->BlockToConfig (
395 HiiConfigRouting,
396 Request,
397 (UINT8 *) IfrNvData,
398 BufferSize,
399 Results,
400 Progress
401 );
402 gBS->FreePool (IfrNvData);
403 return Status;
404 }
405
406 /**
407 This function applies changes in a driver's configuration.
408 Input is a Configuration, which has the routing data for this
409 driver followed by name / value configuration pairs. The driver
410 must apply those pairs to its configurable storage. If the
411 driver's configuration is stored in a linear block of data
412 and the driver's name / value pairs are in <BlockConfig>
413 format, it may use the ConfigToBlock helper function (above) to
414 simplify the job. Currently not implemented.
415
416 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
417 @param[in] Configuration A null-terminated Unicode string in
418 <ConfigString> format.
419 @param[out] Progress A pointer to a string filled in with the
420 offset of the most recent '&' before the
421 first failing name / value pair (or the
422 beginn ing of the string if the failure
423 is in the first name / value pair) or
424 the terminating NULL if all was
425 successful.
426
427 @retval EFI_SUCCESS The results have been distributed or are
428 awaiting distribution.
429 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the
430 parts of the results that must be
431 stored awaiting possible future
432 protocols.
433 @retval EFI_INVALID_PARAMETERS Passing in a NULL for the
434 Results parameter would result
435 in this type of error.
436 @retval EFI_NOT_FOUND Target for the specified routing data
437 was not found.
438 **/
439 EFI_STATUS
440 EFIAPI
441 IScsiFormRouteConfig (
442 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
443 IN CONST EFI_STRING Configuration,
444 OUT EFI_STRING *Progress
445 )
446 {
447 return EFI_SUCCESS;
448 }
449
450 /**
451 This function is called to provide results data to the driver.
452 This data consists of a unique key that is used to identify
453 which data is either being passed back or being asked for.
454
455 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
456 @param[in] Action Specifies the type of action taken by the browser.
457 @param[in] QuestionId A unique value which is sent to the original
458 exporting driver so that it can identify the type
459 of data to expect. The format of the data tends to
460 vary based on the opcode that enerated the callback.
461 @param[in] Type The type of value for the question.
462 @param[in] Value A pointer to the data being sent to the original
463 exporting driver.
464 @param[out] ActionRequest On return, points to the action requested by the
465 callback function.
466
467 @retval EFI_SUCCESS The callback successfully handled the action.
468 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the
469 variable and its data.
470 @retval EFI_DEVICE_ERROR The variable could not be saved.
471 @retval EFI_UNSUPPORTED The specified Action is not supported by the
472 callback.Currently not implemented.
473 @retval EFI_INVALID_PARAMETERS Passing in wrong parameter.
474 @retval Others Other errors as indicated.
475 **/
476 EFI_STATUS
477 EFIAPI
478 IScsiFormCallback (
479 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
480 IN EFI_BROWSER_ACTION Action,
481 IN EFI_QUESTION_ID QuestionId,
482 IN UINT8 Type,
483 IN EFI_IFR_TYPE_VALUE *Value,
484 OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest
485 )
486 {
487 ISCSI_FORM_CALLBACK_INFO *Private;
488 UINTN BufferSize;
489 CHAR8 IScsiName[ISCSI_NAME_IFR_MAX_SIZE];
490 CHAR16 PortString[128];
491 CHAR8 Ip4String[IP4_STR_MAX_SIZE];
492 CHAR8 LunString[ISCSI_LUN_STR_MAX_LEN];
493 UINT64 Lun;
494 EFI_STRING_ID DeviceFormTitleToken;
495 ISCSI_CONFIG_IFR_NVDATA *IfrNvData;
496 ISCSI_CONFIG_FORM_ENTRY *ConfigFormEntry;
497 EFI_IP_ADDRESS HostIp;
498 EFI_IP_ADDRESS SubnetMask;
499 EFI_IP_ADDRESS Gateway;
500 EFI_STATUS Status;
501 EFI_INPUT_KEY Key;
502
503 Private = ISCSI_FORM_CALLBACK_INFO_FROM_FORM_CALLBACK (This);
504
505 //
506 // Retrive uncommitted data from Browser
507 //
508 IfrNvData = AllocateZeroPool (sizeof (ISCSI_CONFIG_IFR_NVDATA));
509 ASSERT (IfrNvData != NULL);
510 if (!HiiGetBrowserData (&mVendorGuid, mVendorStorageName, sizeof (ISCSI_CONFIG_IFR_NVDATA), (UINT8 *) IfrNvData)) {
511 FreePool (IfrNvData);
512 return EFI_NOT_FOUND;
513 }
514
515 Status = EFI_SUCCESS;
516
517 switch (QuestionId) {
518 case KEY_INITIATOR_NAME:
519 IScsiUnicodeStrToAsciiStr (IfrNvData->InitiatorName, IScsiName);
520 BufferSize = AsciiStrLen (IScsiName) + 1;
521
522 Status = gIScsiInitiatorName.Set (&gIScsiInitiatorName, &BufferSize, IScsiName);
523 if (EFI_ERROR (Status)) {
524 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Invalid iSCSI Name!", NULL);
525 }
526
527 break;
528
529 case KEY_LOCAL_IP:
530 IScsiUnicodeStrToAsciiStr (IfrNvData->LocalIp, Ip4String);
531 Status = IScsiAsciiStrToIp (Ip4String, &HostIp.v4);
532 if (EFI_ERROR (Status) || !Ip4IsUnicast (NTOHL (HostIp.Addr[0]), 0)) {
533 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Invalid IP address!", NULL);
534 Status = EFI_INVALID_PARAMETER;
535 } else {
536 CopyMem (&Private->Current->SessionConfigData.LocalIp, &HostIp.v4, sizeof (HostIp.v4));
537 }
538
539 break;
540
541 case KEY_SUBNET_MASK:
542 IScsiUnicodeStrToAsciiStr (IfrNvData->SubnetMask, Ip4String);
543 Status = IScsiAsciiStrToIp (Ip4String, &SubnetMask.v4);
544 if (EFI_ERROR (Status) || ((SubnetMask.Addr[0] != 0) && (IScsiGetSubnetMaskPrefixLength (&SubnetMask.v4) == 0))) {
545 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Invalid Subnet Mask!", NULL);
546 Status = EFI_INVALID_PARAMETER;
547 } else {
548 CopyMem (&Private->Current->SessionConfigData.SubnetMask, &SubnetMask.v4, sizeof (SubnetMask.v4));
549 }
550
551 break;
552
553 case KEY_GATE_WAY:
554 IScsiUnicodeStrToAsciiStr (IfrNvData->Gateway, Ip4String);
555 Status = IScsiAsciiStrToIp (Ip4String, &Gateway.v4);
556 if (EFI_ERROR (Status) || ((Gateway.Addr[0] != 0) && !Ip4IsUnicast (NTOHL (Gateway.Addr[0]), 0))) {
557 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Invalid Gateway!", NULL);
558 Status = EFI_INVALID_PARAMETER;
559 } else {
560 CopyMem (&Private->Current->SessionConfigData.Gateway, &Gateway.v4, sizeof (Gateway.v4));
561 }
562
563 break;
564
565 case KEY_TARGET_IP:
566 IScsiUnicodeStrToAsciiStr (IfrNvData->TargetIp, Ip4String);
567 Status = IScsiAsciiStrToIp (Ip4String, &HostIp.v4);
568 if (EFI_ERROR (Status) || !Ip4IsUnicast (NTOHL (HostIp.Addr[0]), 0)) {
569 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Invalid IP address!", NULL);
570 Status = EFI_INVALID_PARAMETER;
571 } else {
572 CopyMem (&Private->Current->SessionConfigData.TargetIp, &HostIp.v4, sizeof (HostIp.v4));
573 }
574
575 break;
576
577 case KEY_TARGET_NAME:
578 IScsiUnicodeStrToAsciiStr (IfrNvData->TargetName, IScsiName);
579 Status = IScsiNormalizeName (IScsiName, AsciiStrLen (IScsiName));
580 if (EFI_ERROR (Status)) {
581 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Invalid iSCSI Name!", NULL);
582 } else {
583 AsciiStrCpy (Private->Current->SessionConfigData.TargetName, IScsiName);
584 }
585
586 break;
587
588 case KEY_DHCP_ENABLE:
589 if (IfrNvData->InitiatorInfoFromDhcp == 0) {
590 IfrNvData->TargetInfoFromDhcp = 0;
591 }
592
593 break;
594
595 case KEY_BOOT_LUN:
596 IScsiUnicodeStrToAsciiStr (IfrNvData->BootLun, LunString);
597 Status = IScsiAsciiStrToLun (LunString, (UINT8 *) &Lun);
598 if (EFI_ERROR (Status)) {
599 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Invalid LUN string!", NULL);
600 } else {
601 CopyMem (Private->Current->SessionConfigData.BootLun, &Lun, sizeof (Lun));
602 }
603
604 break;
605
606 case KEY_CHAP_NAME:
607 IScsiUnicodeStrToAsciiStr (IfrNvData->CHAPName, Private->Current->AuthConfigData.CHAPName);
608 break;
609
610 case KEY_CHAP_SECRET:
611 IScsiUnicodeStrToAsciiStr (IfrNvData->CHAPSecret, Private->Current->AuthConfigData.CHAPSecret);
612 break;
613
614 case KEY_REVERSE_CHAP_NAME:
615 IScsiUnicodeStrToAsciiStr (IfrNvData->ReverseCHAPName, Private->Current->AuthConfigData.ReverseCHAPName);
616 break;
617
618 case KEY_REVERSE_CHAP_SECRET:
619 IScsiUnicodeStrToAsciiStr (IfrNvData->ReverseCHAPSecret, Private->Current->AuthConfigData.ReverseCHAPSecret);
620 break;
621
622 case KEY_SAVE_CHANGES:
623 //
624 // First, update those fields which don't have INTERACTIVE set.
625 //
626 Private->Current->SessionConfigData.Enabled = IfrNvData->Enabled;
627 Private->Current->SessionConfigData.InitiatorInfoFromDhcp = IfrNvData->InitiatorInfoFromDhcp;
628 Private->Current->SessionConfigData.TargetPort = IfrNvData->TargetPort;
629 if (Private->Current->SessionConfigData.TargetPort == 0) {
630 Private->Current->SessionConfigData.TargetPort = ISCSI_WELL_KNOWN_PORT;
631 }
632
633 Private->Current->SessionConfigData.TargetInfoFromDhcp = IfrNvData->TargetInfoFromDhcp;
634 Private->Current->AuthConfigData.CHAPType = IfrNvData->CHAPType;
635
636 //
637 // Only do full parameter validation if iSCSI is enabled on this device.
638 //
639 if (Private->Current->SessionConfigData.Enabled) {
640 //
641 // Validate the address configuration of the Initiator if DHCP isn't
642 // deployed.
643 //
644 if (!Private->Current->SessionConfigData.InitiatorInfoFromDhcp) {
645 CopyMem (&HostIp.v4, &Private->Current->SessionConfigData.LocalIp, sizeof (HostIp.v4));
646 CopyMem (&SubnetMask.v4, &Private->Current->SessionConfigData.SubnetMask, sizeof (SubnetMask.v4));
647 CopyMem (&Gateway.v4, &Private->Current->SessionConfigData.Gateway, sizeof (Gateway.v4));
648
649 if ((Gateway.Addr[0] != 0)) {
650 if (SubnetMask.Addr[0] == 0) {
651 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Gateway address is set but subnet mask is zero.", NULL);
652 Status = EFI_INVALID_PARAMETER;
653 break;
654 } else if (!IP4_NET_EQUAL (HostIp.Addr[0], Gateway.Addr[0], SubnetMask.Addr[0])) {
655 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Local IP and Gateway are not in the same subnet.", NULL);
656 Status = EFI_INVALID_PARAMETER;
657 break;
658 }
659 }
660 }
661 //
662 // Validate target configuration if DHCP isn't deployed.
663 //
664 if (!Private->Current->SessionConfigData.TargetInfoFromDhcp) {
665 CopyMem (&HostIp.v4, &Private->Current->SessionConfigData.TargetIp, sizeof (HostIp.v4));
666 if (!Ip4IsUnicast (NTOHL (HostIp.Addr[0]), 0)) {
667 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Target IP is invalid!", NULL);
668 Status = EFI_INVALID_PARAMETER;
669 break;
670 }
671 }
672
673 if (IfrNvData->CHAPType != ISCSI_CHAP_NONE) {
674 if ((IfrNvData->CHAPName[0] == '\0') || (IfrNvData->CHAPSecret[0] == '\0')) {
675 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"CHAP Name or CHAP Secret is invalid!", NULL);
676 Status = EFI_INVALID_PARAMETER;
677 break;
678 }
679
680 if ((IfrNvData->CHAPType == ISCSI_CHAP_MUTUAL) &&
681 ((IfrNvData->ReverseCHAPName[0] == '\0') || (IfrNvData->ReverseCHAPSecret[0] == '\0'))
682 ) {
683 CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Reverse CHAP Name or Reverse CHAP Secret is invalid!", NULL);
684 Status = EFI_INVALID_PARAMETER;
685 break;
686 }
687 }
688 }
689
690 BufferSize = sizeof (Private->Current->SessionConfigData);
691 gRT->SetVariable (
692 Private->Current->MacString,
693 &gEfiIScsiInitiatorNameProtocolGuid,
694 ISCSI_CONFIG_VAR_ATTR,
695 BufferSize,
696 &Private->Current->SessionConfigData
697 );
698
699 BufferSize = sizeof (Private->Current->AuthConfigData);
700 gRT->SetVariable (
701 Private->Current->MacString,
702 &mIScsiCHAPAuthInfoGuid,
703 ISCSI_CONFIG_VAR_ATTR,
704 BufferSize,
705 &Private->Current->AuthConfigData
706 );
707 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_SUBMIT;
708 break;
709
710 default:
711 if ((QuestionId >= KEY_DEVICE_ENTRY_BASE) && (QuestionId < (mNumberOfIScsiDevices + KEY_DEVICE_ENTRY_BASE))) {
712 //
713 // In case goto the device configuration form, update the device form title.
714 //
715 ConfigFormEntry = IScsiGetConfigFormEntryByIndex ((UINT32) (QuestionId - KEY_DEVICE_ENTRY_BASE));
716 ASSERT (ConfigFormEntry != NULL);
717
718 UnicodeSPrint (PortString, (UINTN) 128, L"Port %s", ConfigFormEntry->MacString);
719 DeviceFormTitleToken = (EFI_STRING_ID) STR_ISCSI_DEVICE_FORM_TITLE;
720 HiiSetString (Private->RegisteredHandle, DeviceFormTitleToken, PortString, NULL);
721
722 IScsiConvertDeviceConfigDataToIfrNvData (ConfigFormEntry, IfrNvData);
723
724 Private->Current = ConfigFormEntry;
725 }
726
727 break;
728 }
729
730 if (!EFI_ERROR (Status)) {
731 //
732 // Pass changed uncommitted data back to Form Browser
733 //
734 HiiSetBrowserData (&mVendorGuid, mVendorStorageName, sizeof (ISCSI_CONFIG_IFR_NVDATA), (UINT8 *) IfrNvData, NULL);
735 }
736
737 FreePool (IfrNvData);
738 return Status;
739 }
740
741 /**
742 Updates the iSCSI configuration form to add/delete an entry for the iSCSI
743 device specified by the Controller.
744
745 @param[in] DriverBindingHandle The driverbinding handle.
746 @param[in] Controller The controller handle of the iSCSI device.
747 @param[in] AddForm Whether to add or delete a form entry.
748
749 @retval EFI_SUCCESS The iSCSI configuration form is updated.
750 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
751 @retval Others Other errors as indicated.
752 **/
753 EFI_STATUS
754 IScsiConfigUpdateForm (
755 IN EFI_HANDLE DriverBindingHandle,
756 IN EFI_HANDLE Controller,
757 IN BOOLEAN AddForm
758 )
759 {
760 LIST_ENTRY *Entry;
761 ISCSI_CONFIG_FORM_ENTRY *ConfigFormEntry;
762 BOOLEAN EntryExisted;
763 EFI_STATUS Status;
764 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
765 CHAR16 PortString[128];
766 UINT16 FormIndex;
767 UINTN BufferSize;
768 VOID *StartOpCodeHandle;
769 VOID *EndOpCodeHandle;
770 EFI_IFR_GUID_LABEL *StartLabel;
771 EFI_IFR_GUID_LABEL *EndLabel;
772
773 ConfigFormEntry = NULL;
774 EntryExisted = FALSE;
775
776 NET_LIST_FOR_EACH (Entry, &mIScsiConfigFormList) {
777 ConfigFormEntry = NET_LIST_USER_STRUCT (Entry, ISCSI_CONFIG_FORM_ENTRY, Link);
778
779 if (ConfigFormEntry->Controller == Controller) {
780 EntryExisted = TRUE;
781 break;
782 }
783 }
784
785 if (AddForm) {
786 if (EntryExisted) {
787 return EFI_SUCCESS;
788 } else {
789 //
790 // Add a new form.
791 //
792 ConfigFormEntry = (ISCSI_CONFIG_FORM_ENTRY *) AllocateZeroPool (sizeof (ISCSI_CONFIG_FORM_ENTRY));
793 if (ConfigFormEntry == NULL) {
794 return EFI_OUT_OF_RESOURCES;
795 }
796
797 InitializeListHead (&ConfigFormEntry->Link);
798 ConfigFormEntry->Controller = Controller;
799
800 //
801 // Get the simple network protocol and convert the MAC address into
802 // the formatted string.
803 //
804 Status = gBS->HandleProtocol (
805 Controller,
806 &gEfiSimpleNetworkProtocolGuid,
807 (VOID **)&Snp
808 );
809 ASSERT (Status == EFI_SUCCESS);
810
811 IScsiMacAddrToStr (&Snp->Mode->PermanentAddress, Snp->Mode->HwAddressSize, ConfigFormEntry->MacString);
812
813 //
814 // Get the normal session configuration data.
815 //
816 BufferSize = sizeof (ConfigFormEntry->SessionConfigData);
817 Status = gRT->GetVariable (
818 ConfigFormEntry->MacString,
819 &gEfiIScsiInitiatorNameProtocolGuid,
820 NULL,
821 &BufferSize,
822 &ConfigFormEntry->SessionConfigData
823 );
824 if (EFI_ERROR (Status)) {
825 ZeroMem (&ConfigFormEntry->SessionConfigData, sizeof (ConfigFormEntry->SessionConfigData));
826 }
827 //
828 // Get the CHAP authentication configuration data.
829 //
830 BufferSize = sizeof (ConfigFormEntry->AuthConfigData);
831 Status = gRT->GetVariable (
832 ConfigFormEntry->MacString,
833 &mIScsiCHAPAuthInfoGuid,
834 NULL,
835 &BufferSize,
836 &ConfigFormEntry->AuthConfigData
837 );
838 if (EFI_ERROR (Status)) {
839 ZeroMem (&ConfigFormEntry->AuthConfigData, sizeof (ConfigFormEntry->AuthConfigData));
840 }
841 //
842 // Compose the Port string and create a new EFI_STRING_ID.
843 //
844 UnicodeSPrint (PortString, 128, L"Port %s", ConfigFormEntry->MacString);
845 ConfigFormEntry->PortTitleToken = HiiSetString (mCallbackInfo->RegisteredHandle, 0, PortString, NULL);
846
847 //
848 // Compose the help string of this port and create a new EFI_STRING_ID.
849 //
850 UnicodeSPrint (PortString, 128, L"Set the iSCSI parameters on port %s", ConfigFormEntry->MacString);
851 ConfigFormEntry->PortTitleHelpToken = HiiSetString (mCallbackInfo->RegisteredHandle, 0, PortString, NULL);
852
853 InsertTailList (&mIScsiConfigFormList, &ConfigFormEntry->Link);
854 mNumberOfIScsiDevices++;
855 }
856 } else {
857 ASSERT (EntryExisted);
858
859 mNumberOfIScsiDevices--;
860 RemoveEntryList (&ConfigFormEntry->Link);
861 gBS->FreePool (ConfigFormEntry);
862 }
863 //
864 // Allocate space for creation of Buffer
865 //
866
867 //
868 // Init OpCode Handle
869 //
870 StartOpCodeHandle = HiiAllocateOpCodeHandle ();
871 ASSERT (StartOpCodeHandle != NULL);
872
873 EndOpCodeHandle = HiiAllocateOpCodeHandle ();
874 ASSERT (EndOpCodeHandle != NULL);
875
876 //
877 // Create Hii Extend Label OpCode as the start opcode
878 //
879 StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (StartOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));
880 StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
881 StartLabel->Number = DEVICE_ENTRY_LABEL;
882
883 //
884 // Create Hii Extend Label OpCode as the end opcode
885 //
886 EndLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (EndOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));
887 EndLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
888 EndLabel->Number = LABEL_END;
889
890 FormIndex = 0;
891 NET_LIST_FOR_EACH (Entry, &mIScsiConfigFormList) {
892 ConfigFormEntry = NET_LIST_USER_STRUCT (Entry, ISCSI_CONFIG_FORM_ENTRY, Link);
893
894 HiiCreateGotoOpCode (
895 StartOpCodeHandle, // Container for dynamic created opcodes
896 FORMID_DEVICE_FORM, // Target Form ID
897 ConfigFormEntry->PortTitleToken, // Prompt text
898 ConfigFormEntry->PortTitleHelpToken, // Help text
899 EFI_IFR_FLAG_CALLBACK, // Question flag
900 (UINT16)(KEY_DEVICE_ENTRY_BASE + FormIndex) // Question ID
901 );
902
903 FormIndex++;
904 }
905
906 HiiUpdateForm (
907 mCallbackInfo->RegisteredHandle,
908 &mVendorGuid,
909 FORMID_MAIN_FORM,
910 StartOpCodeHandle, // Label DEVICE_ENTRY_LABEL
911 EndOpCodeHandle // LABEL_END
912 );
913
914 HiiFreeOpCodeHandle (StartOpCodeHandle);
915 HiiFreeOpCodeHandle (EndOpCodeHandle);
916
917 return EFI_SUCCESS;
918 }
919
920 /**
921 Initialize the iSCSI configuration form.
922
923 @param[in] DriverBindingHandle The iSCSI driverbinding handle.
924
925 @retval EFI_SUCCESS The iSCSI configuration form is initialized.
926 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
927 @retval Others Other errors as indicated.
928 **/
929 EFI_STATUS
930 IScsiConfigFormInit (
931 VOID
932 )
933 {
934 EFI_STATUS Status;
935 EFI_HII_DATABASE_PROTOCOL *HiiDatabase;
936 ISCSI_FORM_CALLBACK_INFO *CallbackInfo;
937
938 Status = gBS->LocateProtocol (&gEfiHiiDatabaseProtocolGuid, NULL, (VOID **)&HiiDatabase);
939 if (EFI_ERROR (Status)) {
940 return Status;
941 }
942
943 CallbackInfo = (ISCSI_FORM_CALLBACK_INFO *) AllocateZeroPool (sizeof (ISCSI_FORM_CALLBACK_INFO));
944 if (CallbackInfo == NULL) {
945 return EFI_OUT_OF_RESOURCES;
946 }
947
948 CallbackInfo->Signature = ISCSI_FORM_CALLBACK_INFO_SIGNATURE;
949 CallbackInfo->HiiDatabase = HiiDatabase;
950 CallbackInfo->Current = NULL;
951
952 CallbackInfo->ConfigAccess.ExtractConfig = IScsiFormExtractConfig;
953 CallbackInfo->ConfigAccess.RouteConfig = IScsiFormRouteConfig;
954 CallbackInfo->ConfigAccess.Callback = IScsiFormCallback;
955
956 Status = gBS->LocateProtocol (&gEfiHiiConfigRoutingProtocolGuid, NULL, (VOID **)&CallbackInfo->ConfigRouting);
957 if (EFI_ERROR (Status)) {
958 FreePool(CallbackInfo);
959 return Status;
960 }
961
962 //
963 // Install Device Path Protocol and Config Access protocol to driver handle
964 //
965 Status = gBS->InstallMultipleProtocolInterfaces (
966 &CallbackInfo->DriverHandle,
967 &gEfiDevicePathProtocolGuid,
968 &mIScsiHiiVendorDevicePath,
969 &gEfiHiiConfigAccessProtocolGuid,
970 &CallbackInfo->ConfigAccess,
971 NULL
972 );
973 ASSERT_EFI_ERROR (Status);
974
975 //
976 // Publish our HII data
977 //
978 CallbackInfo->RegisteredHandle = HiiAddPackages (
979 &mVendorGuid,
980 CallbackInfo->DriverHandle,
981 IScsiDxeStrings,
982 IScsiConfigDxeBin,
983 NULL
984 );
985 if (CallbackInfo->RegisteredHandle == NULL) {
986 FreePool(CallbackInfo);
987 return EFI_OUT_OF_RESOURCES;
988 }
989
990 mCallbackInfo = CallbackInfo;
991
992 return Status;
993 }
994
995 /**
996 Unload the iSCSI configuration form, this includes: delete all the iSCSI
997 device configuration entries, uninstall the form callback protocol and
998 free the resources used.
999
1000 @param[in] DriverBindingHandle The iSCSI driverbinding handle.
1001
1002 @retval EFI_SUCCESS The iSCSI configuration form is unloaded.
1003 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1004 **/
1005 EFI_STATUS
1006 IScsiConfigFormUnload (
1007 IN EFI_HANDLE DriverBindingHandle
1008 )
1009 {
1010 ISCSI_CONFIG_FORM_ENTRY *ConfigFormEntry;
1011
1012 while (!IsListEmpty (&mIScsiConfigFormList)) {
1013 //
1014 // Uninstall the device forms as the iSCSI driver instance may fail to
1015 // control the controller but still install the device configuration form.
1016 // In such case, upon driver unloading, the driver instance's driverbinding.
1017 // stop () won't be called, so we have to take this chance here to uninstall
1018 // the device form.
1019 //
1020 ConfigFormEntry = NET_LIST_USER_STRUCT (mIScsiConfigFormList.ForwardLink, ISCSI_CONFIG_FORM_ENTRY, Link);
1021 IScsiConfigUpdateForm (DriverBindingHandle, ConfigFormEntry->Controller, FALSE);
1022 }
1023
1024 //
1025 // Remove HII package list
1026 //
1027 mCallbackInfo->HiiDatabase->RemovePackageList (
1028 mCallbackInfo->HiiDatabase,
1029 mCallbackInfo->RegisteredHandle
1030 );
1031
1032 //
1033 // Uninstall EFI_HII_CONFIG_ACCESS_PROTOCOL
1034 //
1035 gBS->UninstallMultipleProtocolInterfaces (
1036 mCallbackInfo->DriverHandle,
1037 &gEfiDevicePathProtocolGuid,
1038 &mIScsiHiiVendorDevicePath,
1039 &gEfiHiiConfigAccessProtocolGuid,
1040 &mCallbackInfo->ConfigAccess,
1041 NULL
1042 );
1043 gBS->FreePool (mCallbackInfo);
1044
1045 return EFI_SUCCESS;
1046 }