]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/IScsiDxe/IScsiMisc.c
Update network stack code to use StrnCpy instead of StrCpy.
[mirror_edk2.git] / NetworkPkg / IScsiDxe / IScsiMisc.c
CommitLineData
4c5a5e0c 1/** @file\r
2 Miscellaneous routines for iSCSI driver.\r
3\r
18b24f92 4Copyright (c) 2004 - 2014, Intel Corporation. All rights reserved.<BR>\r
4c5a5e0c 5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "IScsiImpl.h"\r
16\r
17GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 IScsiHexString[] = "0123456789ABCDEFabcdef";\r
18\r
19/**\r
20 Removes (trims) specified leading and trailing characters from a string.\r
21\r
22 @param[in, out] Str Pointer to the null-terminated string to be trimmed.\r
23 On return, Str will hold the trimmed string. \r
24\r
25 @param[in] CharC Character will be trimmed from str.\r
26\r
27**/\r
28VOID\r
29IScsiStrTrim (\r
30 IN OUT CHAR16 *Str,\r
31 IN CHAR16 CharC\r
32 )\r
33{\r
34 CHAR16 *Pointer1;\r
35 CHAR16 *Pointer2;\r
36 \r
37 if (*Str == 0) {\r
38 return ;\r
39 }\r
40 \r
41 //\r
42 // Trim off the leading and trailing characters c\r
43 //\r
44 for (Pointer1 = Str; (*Pointer1 != 0) && (*Pointer1 == CharC); Pointer1++) {\r
45 ;\r
46 }\r
47 \r
48 Pointer2 = Str;\r
49 if (Pointer2 == Pointer1) {\r
50 while (*Pointer1 != 0) {\r
51 Pointer2++;\r
52 Pointer1++;\r
53 }\r
54 } else {\r
55 while (*Pointer1 != 0) { \r
56 *Pointer2 = *Pointer1; \r
57 Pointer1++;\r
58 Pointer2++;\r
59 }\r
60 *Pointer2 = 0;\r
61 }\r
62 \r
63 \r
64 for (Pointer1 = Str + StrLen(Str) - 1; Pointer1 >= Str && *Pointer1 == CharC; Pointer1--) {\r
65 ;\r
66 }\r
67 if (Pointer1 != Str + StrLen(Str) - 1) { \r
68 *(Pointer1 + 1) = 0;\r
69 }\r
70}\r
71\r
72/**\r
73 Calculate the prefix length of the IPv4 subnet mask.\r
74\r
75 @param[in] SubnetMask The IPv4 subnet mask.\r
76\r
77 @return The prefix length of the subnet mask.\r
78 @retval 0 Other errors as indicated.\r
79\r
80**/\r
81UINT8\r
82IScsiGetSubnetMaskPrefixLength (\r
83 IN EFI_IPv4_ADDRESS *SubnetMask\r
84 )\r
85{\r
86 UINT8 Len;\r
87 UINT32 ReverseMask;\r
88\r
89 //\r
90 // The SubnetMask is in network byte order.\r
91 //\r
92 ReverseMask = (SubnetMask->Addr[0] << 24) | (SubnetMask->Addr[1] << 16) | (SubnetMask->Addr[2] << 8) | (SubnetMask->Addr[3]);\r
93\r
94 //\r
95 // Reverse it.\r
96 //\r
97 ReverseMask = ~ReverseMask;\r
98\r
99 if ((ReverseMask & (ReverseMask + 1)) != 0) {\r
100 return 0;\r
101 }\r
102\r
103 Len = 0;\r
104\r
105 while (ReverseMask != 0) {\r
106 ReverseMask = ReverseMask >> 1;\r
107 Len++;\r
108 }\r
109\r
110 return (UINT8) (32 - Len);\r
111}\r
112\r
113\r
114/**\r
115 Convert the hexadecimal encoded LUN string into the 64-bit LUN.\r
116\r
117 @param[in] Str The hexadecimal encoded LUN string.\r
118 @param[out] Lun Storage to return the 64-bit LUN.\r
119\r
120 @retval EFI_SUCCESS The 64-bit LUN is stored in Lun.\r
121 @retval EFI_INVALID_PARAMETER The string is malformatted.\r
122\r
123**/\r
124EFI_STATUS\r
125IScsiAsciiStrToLun (\r
126 IN CHAR8 *Str,\r
127 OUT UINT8 *Lun\r
128 )\r
129{\r
130 UINTN Index, IndexValue, IndexNum, SizeStr;\r
131 CHAR8 TemStr[2];\r
132 UINT8 TemValue;\r
133 UINT16 Value[4];\r
134 \r
135 ZeroMem (Lun, 8);\r
136 ZeroMem (TemStr, 2);\r
137 ZeroMem ((UINT8 *) Value, sizeof (Value));\r
138 SizeStr = AsciiStrLen (Str); \r
139 IndexValue = 0;\r
140 IndexNum = 0;\r
141\r
142 for (Index = 0; Index < SizeStr; Index ++) {\r
143 TemStr[0] = Str[Index];\r
144 TemValue = (UINT8) AsciiStrHexToUint64 (TemStr);\r
145 if (TemValue == 0 && TemStr[0] != '0') {\r
146 if ((TemStr[0] != '-') || (IndexNum == 0)) {\r
147 //\r
148 // Invalid Lun Char.\r
149 //\r
150 return EFI_INVALID_PARAMETER;\r
151 }\r
152 }\r
153 \r
154 if ((TemValue == 0) && (TemStr[0] == '-')) {\r
155 //\r
156 // Next Lun value.\r
157 //\r
158 if (++IndexValue >= 4) {\r
159 //\r
160 // Max 4 Lun value.\r
161 //\r
162 return EFI_INVALID_PARAMETER;\r
163 }\r
164 //\r
165 // Restart str index for the next lun value.\r
166 //\r
167 IndexNum = 0;\r
168 continue;\r
169 }\r
170 \r
171 if (++IndexNum > 4) {\r
172 // \r
173 // Each Lun Str can't exceed size 4, because it will be as UINT16 value.\r
174 //\r
175 return EFI_INVALID_PARAMETER;\r
176 }\r
177 \r
178 //\r
179 // Combine UINT16 value.\r
180 //\r
181 Value[IndexValue] = (UINT16) ((Value[IndexValue] << 4) + TemValue);\r
182 }\r
183 \r
184 for (Index = 0; Index <= IndexValue; Index ++) {\r
185 *((UINT16 *) &Lun[Index * 2]) = HTONS (Value[Index]);\r
186 }\r
187 \r
188 return EFI_SUCCESS;\r
189}\r
190\r
191/**\r
192 Convert the 64-bit LUN into the hexadecimal encoded LUN string.\r
193\r
194 @param[in] Lun The 64-bit LUN.\r
195 @param[out] Str The storage to return the hexadecimal encoded LUN string.\r
196\r
197**/\r
198VOID\r
199IScsiLunToUnicodeStr (\r
200 IN UINT8 *Lun,\r
201 OUT CHAR16 *Str\r
202 )\r
203{\r
204 UINTN Index;\r
205 CHAR16 *TempStr;\r
206\r
207 TempStr = Str;\r
208\r
209 for (Index = 0; Index < 4; Index++) {\r
210\r
211 if ((Lun[2 * Index] | Lun[2 * Index + 1]) == 0) {\r
2922e29a 212 StrnCpy (TempStr, L"0-", StrLen (L"0-"));\r
4c5a5e0c 213 } else {\r
214 TempStr[0] = (CHAR16) IScsiHexString[Lun[2 * Index] >> 4];\r
215 TempStr[1] = (CHAR16) IScsiHexString[Lun[2 * Index] & 0x0F];\r
216 TempStr[2] = (CHAR16) IScsiHexString[Lun[2 * Index + 1] >> 4];\r
217 TempStr[3] = (CHAR16) IScsiHexString[Lun[2 * Index + 1] & 0x0F];\r
218 TempStr[4] = L'-';\r
219 TempStr[5] = 0;\r
220\r
221 IScsiStrTrim (TempStr, L'0');\r
222 }\r
223\r
224 TempStr += StrLen (TempStr);\r
225 }\r
226\r
227 Str[StrLen (Str) - 1] = 0;\r
228\r
229 for (Index = StrLen (Str) - 1; Index > 1; Index = Index - 2) {\r
230 if ((Str[Index] == L'0') && (Str[Index - 1] == L'-')) {\r
231 Str[Index - 1] = 0;\r
232 } else {\r
233 break;\r
234 }\r
235 }\r
236}\r
237\r
238/**\r
239 Convert the formatted IP address into the binary IP address.\r
240\r
241 @param[in] Str The UNICODE string.\r
242 @param[in] IpMode Indicates whether the IP address is v4 or v6.\r
243 @param[out] Ip The storage to return the ASCII string.\r
244\r
245 @retval EFI_SUCCESS The binary IP address is returned in Ip.\r
246 @retval EFI_INVALID_PARAMETER The IP string is malformatted or IpMode is\r
247 invalid.\r
248\r
249**/\r
250EFI_STATUS\r
251IScsiAsciiStrToIp (\r
252 IN CHAR8 *Str,\r
253 IN UINT8 IpMode,\r
254 OUT EFI_IP_ADDRESS *Ip\r
255 )\r
256{\r
257 EFI_STATUS Status;\r
258\r
259 if (IpMode == IP_MODE_IP4 || IpMode == IP_MODE_AUTOCONFIG_IP4) {\r
260 return NetLibAsciiStrToIp4 (Str, &Ip->v4);\r
261\r
262 } else if (IpMode == IP_MODE_IP6 || IpMode == IP_MODE_AUTOCONFIG_IP6) {\r
263 return NetLibAsciiStrToIp6 (Str, &Ip->v6);\r
264\r
265 } else if (IpMode == IP_MODE_AUTOCONFIG) {\r
266 Status = NetLibAsciiStrToIp4 (Str, &Ip->v4);\r
267 if (!EFI_ERROR (Status)) {\r
268 return Status;\r
269 }\r
270 return NetLibAsciiStrToIp6 (Str, &Ip->v6);\r
271\r
272 }\r
273\r
274 return EFI_INVALID_PARAMETER;\r
275}\r
276\r
277/**\r
278 Convert the mac address into a hexadecimal encoded "-" seperated string.\r
279\r
280 @param[in] Mac The mac address.\r
281 @param[in] Len Length in bytes of the mac address.\r
282 @param[in] VlanId VLAN ID of the network device.\r
283 @param[out] Str The storage to return the mac string.\r
284\r
285**/\r
286VOID\r
287IScsiMacAddrToStr (\r
288 IN EFI_MAC_ADDRESS *Mac,\r
289 IN UINT32 Len,\r
290 IN UINT16 VlanId,\r
291 OUT CHAR16 *Str\r
292 )\r
293{\r
294 UINT32 Index;\r
295 CHAR16 *String;\r
296\r
297 for (Index = 0; Index < Len; Index++) {\r
298 Str[3 * Index] = (CHAR16) IScsiHexString[(Mac->Addr[Index] >> 4) & 0x0F];\r
299 Str[3 * Index + 1] = (CHAR16) IScsiHexString[Mac->Addr[Index] & 0x0F];\r
c0d494b5 300 Str[3 * Index + 2] = L':';\r
4c5a5e0c 301 }\r
302\r
303 String = &Str[3 * Index - 1] ;\r
304 if (VlanId != 0) {\r
305 String += UnicodeSPrint (String, 6 * sizeof (CHAR16), L"\\%04x", (UINTN) VlanId);\r
306 }\r
307\r
308 *String = L'\0';\r
309}\r
310\r
311/**\r
312 Convert the binary encoded buffer into a hexadecimal encoded string.\r
313\r
314 @param[in] BinBuffer The buffer containing the binary data.\r
315 @param[in] BinLength Length of the binary buffer.\r
316 @param[in, out] HexStr Pointer to the string.\r
317 @param[in, out] HexLength The length of the string.\r
318\r
319 @retval EFI_SUCCESS The binary data is converted to the hexadecimal string \r
320 and the length of the string is updated.\r
321 @retval EFI_BUFFER_TOO_SMALL The string is too small.\r
322 @retval EFI_INVALID_PARAMETER The IP string is malformatted.\r
323\r
324**/\r
325EFI_STATUS\r
326IScsiBinToHex (\r
327 IN UINT8 *BinBuffer,\r
328 IN UINT32 BinLength,\r
329 IN OUT CHAR8 *HexStr,\r
330 IN OUT UINT32 *HexLength\r
331 )\r
332{\r
333 UINTN Index;\r
334\r
335 if ((HexStr == NULL) || (BinBuffer == NULL) || (BinLength == 0)) {\r
336 return EFI_INVALID_PARAMETER;\r
337 }\r
338\r
339 if (((*HexLength) - 3) < BinLength * 2) {\r
340 *HexLength = BinLength * 2 + 3;\r
341 return EFI_BUFFER_TOO_SMALL;\r
342 }\r
343\r
344 *HexLength = BinLength * 2 + 3;\r
345 //\r
346 // Prefix for Hex String.\r
347 //\r
348 HexStr[0] = '0';\r
349 HexStr[1] = 'x';\r
350\r
351 for (Index = 0; Index < BinLength; Index++) {\r
352 HexStr[Index * 2 + 2] = IScsiHexString[BinBuffer[Index] >> 4];\r
353 HexStr[Index * 2 + 3] = IScsiHexString[BinBuffer[Index] & 0xf];\r
354 }\r
355\r
356 HexStr[Index * 2 + 2] = '\0';\r
357\r
358 return EFI_SUCCESS;\r
359}\r
360\r
361\r
362/**\r
363 Convert the hexadecimal string into a binary encoded buffer.\r
364\r
365 @param[in, out] BinBuffer The binary buffer.\r
366 @param[in, out] BinLength Length of the binary buffer.\r
367 @param[in] HexStr The hexadecimal string.\r
368\r
369 @retval EFI_SUCCESS The hexadecimal string is converted into a binary\r
370 encoded buffer.\r
371 @retval EFI_BUFFER_TOO_SMALL The binary buffer is too small to hold the converted data.\r
372\r
373**/\r
374EFI_STATUS\r
375IScsiHexToBin (\r
376 IN OUT UINT8 *BinBuffer,\r
377 IN OUT UINT32 *BinLength,\r
378 IN CHAR8 *HexStr\r
379 )\r
380{\r
381 UINTN Index;\r
382 UINTN Length;\r
383 UINT8 Digit;\r
384 CHAR8 TemStr[2];\r
385 \r
386 ZeroMem (TemStr, sizeof (TemStr));\r
387\r
388 //\r
389 // Find out how many hex characters the string has.\r
390 //\r
391 if ((HexStr[0] == '0') && ((HexStr[1] == 'x') || (HexStr[1] == 'X'))) {\r
392 HexStr += 2;\r
393 }\r
394 \r
395 Length = AsciiStrLen (HexStr);\r
396\r
397 for (Index = 0; Index < Length; Index ++) {\r
398 TemStr[0] = HexStr[Index];\r
399 Digit = (UINT8) AsciiStrHexToUint64 (TemStr);\r
400 if (Digit == 0 && TemStr[0] != '0') {\r
401 //\r
402 // Invalid Lun Char.\r
403 //\r
404 break;\r
405 }\r
406 if ((Index & 1) == 0) {\r
407 BinBuffer [Index/2] = Digit;\r
408 } else {\r
409 BinBuffer [Index/2] = (UINT8) ((BinBuffer [Index/2] << 4) + Digit);\r
410 }\r
411 }\r
412 \r
413 *BinLength = (UINT32) ((Index + 1)/2);\r
414\r
415 return EFI_SUCCESS;\r
416}\r
417\r
418\r
419/**\r
420 Convert the decimal-constant string or hex-constant string into a numerical value.\r
421\r
422 @param[in] Str String in decimal or hex.\r
423\r
424 @return The numerical value.\r
425\r
426**/\r
427UINTN\r
428IScsiNetNtoi (\r
429 IN CHAR8 *Str\r
430 )\r
431{\r
432 if ((Str[0] == '0') && ((Str[1] == 'x') || (Str[1] == 'X'))) {\r
433 Str += 2;\r
434\r
435 return AsciiStrHexToUintn (Str);\r
436 }\r
437\r
438 return AsciiStrDecimalToUintn (Str);\r
439}\r
440\r
441\r
442/**\r
443 Generate random numbers.\r
444\r
445 @param[in, out] Rand The buffer to contain random numbers.\r
446 @param[in] RandLength The length of the Rand buffer.\r
447\r
448**/\r
449VOID\r
450IScsiGenRandom (\r
451 IN OUT UINT8 *Rand,\r
452 IN UINTN RandLength\r
453 )\r
454{\r
455 UINT32 Random;\r
456\r
457 while (RandLength > 0) {\r
458 Random = NET_RANDOM (NetRandomInitSeed ());\r
459 *Rand++ = (UINT8) (Random);\r
460 RandLength--;\r
461 }\r
462}\r
463\r
464\r
465/**\r
466 Record the NIC info in global structure.\r
467\r
468 @param[in] Controller The handle of the controller.\r
469\r
470 @retval EFI_SUCCESS The operation is completed.\r
471 @retval EFI_OUT_OF_RESOURCES Do not have sufficient resources to finish this\r
472 operation.\r
473\r
474**/\r
475EFI_STATUS\r
476IScsiAddNic (\r
477 IN EFI_HANDLE Controller\r
478 )\r
479{\r
480 EFI_STATUS Status;\r
481 ISCSI_NIC_INFO *NicInfo;\r
482 LIST_ENTRY *Entry;\r
483 EFI_MAC_ADDRESS MacAddr;\r
484 UINTN HwAddressSize;\r
485 UINT16 VlanId;\r
486\r
487 //\r
488 // Get MAC address of this network device.\r
489 //\r
490 Status = NetLibGetMacAddress (Controller, &MacAddr, &HwAddressSize);\r
491 if (EFI_ERROR (Status)) {\r
492 return Status;\r
493 }\r
494\r
495 //\r
496 // Get VLAN ID of this network device.\r
497 //\r
498 VlanId = NetLibGetVlanId (Controller);\r
499\r
500 //\r
501 // Check whether the NIC info already exists. Return directly if so.\r
502 //\r
503 NET_LIST_FOR_EACH (Entry, &mPrivate->NicInfoList) {\r
504 NicInfo = NET_LIST_USER_STRUCT (Entry, ISCSI_NIC_INFO, Link);\r
505 if (NicInfo->HwAddressSize == HwAddressSize &&\r
506 CompareMem (&NicInfo->PermanentAddress, MacAddr.Addr, HwAddressSize) == 0 &&\r
507 NicInfo->VlanId == VlanId) {\r
508 mPrivate->CurrentNic = NicInfo->NicIndex;\r
509 return EFI_SUCCESS;\r
510 }\r
511\r
512 if (mPrivate->MaxNic < NicInfo->NicIndex) {\r
513 mPrivate->MaxNic = NicInfo->NicIndex;\r
514 }\r
515 }\r
516\r
517 //\r
518 // Record the NIC info in private structure.\r
519 //\r
520 NicInfo = AllocateZeroPool (sizeof (ISCSI_NIC_INFO));\r
521 if (NicInfo == NULL) {\r
522 return EFI_OUT_OF_RESOURCES;\r
523 }\r
524\r
525 CopyMem (&NicInfo->PermanentAddress, MacAddr.Addr, HwAddressSize);\r
526 NicInfo->HwAddressSize = (UINT32) HwAddressSize;\r
527 NicInfo->VlanId = VlanId;\r
528 NicInfo->NicIndex = (UINT8) (mPrivate->MaxNic + 1);\r
529 mPrivate->MaxNic = NicInfo->NicIndex;\r
530\r
531 //\r
532 // Get the PCI location.\r
533 //\r
534 IScsiGetNICPciLocation (\r
535 Controller,\r
536 &NicInfo->BusNumber,\r
537 &NicInfo->DeviceNumber,\r
538 &NicInfo->FunctionNumber\r
539 );\r
540\r
541 InsertTailList (&mPrivate->NicInfoList, &NicInfo->Link);\r
542 mPrivate->NicCount++;\r
543\r
544 mPrivate->CurrentNic = NicInfo->NicIndex;\r
545 return EFI_SUCCESS;\r
546}\r
547\r
548\r
549/**\r
550 Delete the recorded NIC info from global structure. Also delete corresponding\r
551 attempts.\r
552\r
553 @param[in] Controller The handle of the controller.\r
554\r
555 @retval EFI_SUCCESS The operation is completed.\r
556 @retval EFI_NOT_FOUND The NIC info to be deleted is not recorded.\r
557\r
558**/\r
559EFI_STATUS\r
560IScsiRemoveNic (\r
561 IN EFI_HANDLE Controller\r
562 )\r
563{\r
564 EFI_STATUS Status;\r
565 ISCSI_NIC_INFO *NicInfo;\r
566 LIST_ENTRY *Entry;\r
567 LIST_ENTRY *NextEntry;\r
568 ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData;\r
569 ISCSI_NIC_INFO *ThisNic;\r
570 EFI_MAC_ADDRESS MacAddr;\r
571 UINTN HwAddressSize;\r
572 UINT16 VlanId;\r
573\r
574 //\r
575 // Get MAC address of this network device.\r
576 //\r
577 Status = NetLibGetMacAddress (Controller, &MacAddr, &HwAddressSize);\r
578 if (EFI_ERROR (Status)) {\r
579 return Status;\r
580 }\r
581\r
582 //\r
583 // Get VLAN ID of this network device.\r
584 //\r
585 VlanId = NetLibGetVlanId (Controller);\r
586\r
587 //\r
588 // Check whether the NIC information exists.\r
589 //\r
590 ThisNic = NULL;\r
591\r
592 NET_LIST_FOR_EACH (Entry, &mPrivate->NicInfoList) {\r
593 NicInfo = NET_LIST_USER_STRUCT (Entry, ISCSI_NIC_INFO, Link);\r
594 if (NicInfo->HwAddressSize == HwAddressSize &&\r
595 CompareMem (&NicInfo->PermanentAddress, MacAddr.Addr, HwAddressSize) == 0 &&\r
596 NicInfo->VlanId == VlanId) {\r
597\r
598 ThisNic = NicInfo;\r
599 break;\r
600 }\r
601 }\r
602\r
603 if (ThisNic == NULL) {\r
604 return EFI_NOT_FOUND;\r
605 }\r
606\r
607 mPrivate->CurrentNic = ThisNic->NicIndex;\r
608\r
609 RemoveEntryList (&ThisNic->Link);\r
610 FreePool (ThisNic);\r
611 mPrivate->NicCount--;\r
612\r
613 //\r
614 // Remove all attempts related to this NIC.\r
615 //\r
616 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &mPrivate->AttemptConfigs) {\r
617 AttemptConfigData = NET_LIST_USER_STRUCT (Entry, ISCSI_ATTEMPT_CONFIG_NVDATA, Link);\r
618 if (AttemptConfigData->NicIndex == mPrivate->CurrentNic) {\r
619 RemoveEntryList (&AttemptConfigData->Link);\r
620 mPrivate->AttemptCount--;\r
621\r
622 if (AttemptConfigData->SessionConfigData.Enabled == ISCSI_ENABLED_FOR_MPIO && mPrivate->MpioCount > 0) {\r
623 if (--mPrivate->MpioCount == 0) {\r
624 mPrivate->EnableMpio = FALSE;\r
625 }\r
626\r
627 if (AttemptConfigData->AuthenticationType == ISCSI_AUTH_TYPE_KRB && mPrivate->Krb5MpioCount > 0) {\r
628 mPrivate->Krb5MpioCount--;\r
629 }\r
630\r
631 } else if (AttemptConfigData->SessionConfigData.Enabled == ISCSI_ENABLED && mPrivate->SinglePathCount > 0) {\r
632 mPrivate->SinglePathCount--;\r
633\r
634 if (mPrivate->ValidSinglePathCount > 0) {\r
635 mPrivate->ValidSinglePathCount--;\r
636 }\r
637 }\r
638\r
639 FreePool (AttemptConfigData);\r
640 }\r
641 }\r
642\r
c0d494b5 643 //\r
644 // Free attempt is created but not saved to system.\r
645 //\r
646 if (mPrivate->NewAttempt != NULL) {\r
647 FreePool (mPrivate->NewAttempt);\r
648 mPrivate->NewAttempt = NULL;\r
649 }\r
650\r
4c5a5e0c 651 return EFI_SUCCESS;\r
652}\r
653\r
654\r
655/**\r
656 Get the recorded NIC info from global structure by the Index.\r
657\r
658 @param[in] NicIndex The index indicates the position of NIC info.\r
659\r
660 @return Pointer to the NIC info, or NULL if not found.\r
661\r
662**/\r
663ISCSI_NIC_INFO *\r
664IScsiGetNicInfoByIndex (\r
665 IN UINT8 NicIndex\r
666 )\r
667{\r
668 LIST_ENTRY *Entry;\r
669 ISCSI_NIC_INFO *NicInfo;\r
670\r
671 NET_LIST_FOR_EACH (Entry, &mPrivate->NicInfoList) {\r
672 NicInfo = NET_LIST_USER_STRUCT (Entry, ISCSI_NIC_INFO, Link);\r
673 if (NicInfo->NicIndex == NicIndex) {\r
674 return NicInfo;\r
675 }\r
676 }\r
677\r
678 return NULL;\r
679}\r
680\r
681\r
682/**\r
683 Get the NIC's PCI location and return it accroding to the composited\r
684 format defined in iSCSI Boot Firmware Table.\r
685\r
686 @param[in] Controller The handle of the controller.\r
687 @param[out] Bus The bus number.\r
688 @param[out] Device The device number.\r
689 @param[out] Function The function number.\r
690\r
691 @return The composited representation of the NIC PCI location.\r
692\r
693**/\r
694UINT16\r
695IScsiGetNICPciLocation (\r
696 IN EFI_HANDLE Controller,\r
697 OUT UINTN *Bus,\r
698 OUT UINTN *Device,\r
699 OUT UINTN *Function\r
700 )\r
701{\r
702 EFI_STATUS Status;\r
703 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
704 EFI_HANDLE PciIoHandle;\r
705 EFI_PCI_IO_PROTOCOL *PciIo;\r
706 UINTN Segment;\r
707\r
708 Status = gBS->HandleProtocol (\r
709 Controller,\r
710 &gEfiDevicePathProtocolGuid,\r
711 (VOID **) &DevicePath\r
712 );\r
713 if (EFI_ERROR (Status)) {\r
714 return 0;\r
715 }\r
716\r
717 Status = gBS->LocateDevicePath (\r
718 &gEfiPciIoProtocolGuid,\r
719 &DevicePath,\r
720 &PciIoHandle\r
721 );\r
722 if (EFI_ERROR (Status)) {\r
723 return 0;\r
724 }\r
725\r
726 Status = gBS->HandleProtocol (PciIoHandle, &gEfiPciIoProtocolGuid, (VOID **) &PciIo);\r
727 if (EFI_ERROR (Status)) {\r
728 return 0;\r
729 }\r
730\r
731 Status = PciIo->GetLocation (PciIo, &Segment, Bus, Device, Function);\r
732 if (EFI_ERROR (Status)) {\r
733 return 0;\r
734 }\r
735\r
736 return (UINT16) ((*Bus << 8) | (*Device << 3) | *Function);\r
737}\r
738\r
739\r
740/**\r
741 Read the EFI variable (VendorGuid/Name) and return a dynamically allocated\r
742 buffer, and the size of the buffer. If failure, return NULL.\r
743\r
744 @param[in] Name String part of EFI variable name.\r
745 @param[in] VendorGuid GUID part of EFI variable name.\r
746 @param[out] VariableSize Returns the size of the EFI variable that was read.\r
747\r
748 @return Dynamically allocated memory that contains a copy of the EFI variable.\r
749 @return Caller is responsible freeing the buffer.\r
750 @retval NULL Variable was not read.\r
751\r
752**/\r
753VOID *\r
754IScsiGetVariableAndSize (\r
755 IN CHAR16 *Name,\r
756 IN EFI_GUID *VendorGuid,\r
757 OUT UINTN *VariableSize\r
758 )\r
759{\r
760 EFI_STATUS Status;\r
761 UINTN BufferSize;\r
762 VOID *Buffer;\r
763\r
764 Buffer = NULL;\r
765\r
766 //\r
767 // Pass in a zero size buffer to find the required buffer size.\r
768 //\r
769 BufferSize = 0;\r
770 Status = gRT->GetVariable (Name, VendorGuid, NULL, &BufferSize, Buffer);\r
771 if (Status == EFI_BUFFER_TOO_SMALL) {\r
772 //\r
773 // Allocate the buffer to return\r
774 //\r
775 Buffer = AllocateZeroPool (BufferSize);\r
776 if (Buffer == NULL) {\r
777 return NULL;\r
778 }\r
779 //\r
780 // Read variable into the allocated buffer.\r
781 //\r
782 Status = gRT->GetVariable (Name, VendorGuid, NULL, &BufferSize, Buffer);\r
783 if (EFI_ERROR (Status)) {\r
784 BufferSize = 0;\r
785 }\r
786 }\r
787\r
788 *VariableSize = BufferSize;\r
789 return Buffer;\r
790}\r
791\r
792\r
793/**\r
794 Create the iSCSI driver data.\r
795\r
796 @param[in] Image The handle of the driver image.\r
797 @param[in] Controller The handle of the controller.\r
798\r
799 @return The iSCSI driver data created.\r
800 @retval NULL Other errors as indicated.\r
801\r
802**/\r
803ISCSI_DRIVER_DATA *\r
804IScsiCreateDriverData (\r
805 IN EFI_HANDLE Image,\r
806 IN EFI_HANDLE Controller\r
807 )\r
808{\r
809 ISCSI_DRIVER_DATA *Private;\r
810 EFI_STATUS Status;\r
811\r
812 Private = AllocateZeroPool (sizeof (ISCSI_DRIVER_DATA));\r
813 if (Private == NULL) {\r
814 return NULL;\r
815 }\r
816\r
817 Private->Signature = ISCSI_DRIVER_DATA_SIGNATURE;\r
818 Private->Image = Image;\r
819 Private->Controller = Controller;\r
820 Private->Session = NULL;\r
821\r
822 //\r
823 // Create an event to be signaled when the BS to RT transition is triggerd so\r
824 // as to abort the iSCSI session.\r
825 //\r
826 Status = gBS->CreateEventEx (\r
827 EVT_NOTIFY_SIGNAL,\r
828 TPL_CALLBACK,\r
829 IScsiOnExitBootService,\r
830 Private,\r
831 &gEfiEventExitBootServicesGuid,\r
832 &Private->ExitBootServiceEvent\r
833 );\r
834 if (EFI_ERROR (Status)) {\r
835 FreePool (Private);\r
836 return NULL;\r
837 }\r
838\r
839 Private->ExtScsiPassThruHandle = NULL;\r
840 CopyMem(&Private->IScsiExtScsiPassThru, &gIScsiExtScsiPassThruProtocolTemplate, sizeof(EFI_EXT_SCSI_PASS_THRU_PROTOCOL));\r
841\r
842 //\r
843 // 0 is designated to the TargetId, so use another value for the AdapterId.\r
844 //\r
845 Private->ExtScsiPassThruMode.AdapterId = 2;\r
846 Private->ExtScsiPassThruMode.Attributes = EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_PHYSICAL | EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL;\r
847 Private->ExtScsiPassThruMode.IoAlign = 4;\r
848 Private->IScsiExtScsiPassThru.Mode = &Private->ExtScsiPassThruMode;\r
849\r
850 return Private;\r
851}\r
852\r
853\r
854/**\r
855 Clean the iSCSI driver data.\r
856\r
857 @param[in] Private The iSCSI driver data.\r
858\r
859**/\r
860VOID\r
861IScsiCleanDriverData (\r
862 IN ISCSI_DRIVER_DATA *Private\r
863 )\r
864{\r
865 EFI_STATUS Status;\r
866\r
867 if (Private->DevicePath != NULL) {\r
868 gBS->UninstallProtocolInterface (\r
869 Private->ExtScsiPassThruHandle,\r
870 &gEfiDevicePathProtocolGuid,\r
871 Private->DevicePath\r
872 );\r
873\r
874 FreePool (Private->DevicePath);\r
875 }\r
876\r
877 if (Private->ExtScsiPassThruHandle != NULL) {\r
878 Status = gBS->UninstallProtocolInterface (\r
879 Private->ExtScsiPassThruHandle,\r
880 &gEfiExtScsiPassThruProtocolGuid,\r
881 &Private->IScsiExtScsiPassThru\r
882 );\r
883 if (!EFI_ERROR (Status)) {\r
884 mPrivate->OneSessionEstablished = FALSE;\r
885 }\r
886 }\r
887\r
888 gBS->CloseEvent (Private->ExitBootServiceEvent);\r
889\r
890 FreePool (Private);\r
891}\r
892\r
b7cc5bf1
WJ
893/**\r
894 Check wheather the Controller handle is configured to use DHCP protocol.\r
895\r
896 @param[in] Controller The handle of the controller.\r
897 @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6.\r
898 \r
899 @retval TRUE The handle of the controller need the Dhcp protocol.\r
900 @retval FALSE The handle of the controller does not need the Dhcp protocol.\r
901 \r
902**/\r
903BOOLEAN\r
904IScsiDhcpIsConfigured (\r
905 IN EFI_HANDLE Controller,\r
906 IN UINT8 IpVersion\r
907 )\r
908{\r
909 ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptTmp;\r
910 UINT8 *AttemptConfigOrder;\r
911 UINTN AttemptConfigOrderSize;\r
912 UINTN Index;\r
913 EFI_STATUS Status;\r
914 EFI_MAC_ADDRESS MacAddr;\r
915 UINTN HwAddressSize;\r
916 UINT16 VlanId;\r
917 CHAR16 MacString[ISCSI_MAX_MAC_STRING_LEN];\r
918 CHAR16 AttemptName[ISCSI_NAME_IFR_MAX_SIZE];\r
919 \r
920 AttemptConfigOrder = IScsiGetVariableAndSize (\r
921 L"AttemptOrder",\r
922 &gIScsiConfigGuid,\r
923 &AttemptConfigOrderSize\r
924 );\r
925 if (AttemptConfigOrder == NULL || AttemptConfigOrderSize == 0) {\r
926 return FALSE;\r
927 }\r
928 \r
929 //\r
930 // Get MAC address of this network device.\r
931 //\r
932 Status = NetLibGetMacAddress (Controller, &MacAddr, &HwAddressSize);\r
933 if(EFI_ERROR (Status)) {\r
934 return FALSE;\r
935 }\r
936 //\r
937 // Get VLAN ID of this network device.\r
938 //\r
939 VlanId = NetLibGetVlanId (Controller);\r
940 IScsiMacAddrToStr (&MacAddr, (UINT32) HwAddressSize, VlanId, MacString);\r
941 \r
942 for (Index = 0; Index < AttemptConfigOrderSize / sizeof (UINT8); Index++) {\r
943 UnicodeSPrint (\r
944 AttemptName,\r
945 (UINTN) 128,\r
946 L"%s%d",\r
947 MacString,\r
948 (UINTN) AttemptConfigOrder[Index]\r
949 );\r
950 Status = GetVariable2 (\r
951 AttemptName,\r
952 &gEfiIScsiInitiatorNameProtocolGuid,\r
953 (VOID**)&AttemptTmp,\r
954 NULL\r
955 );\r
107e3d7a 956 if(AttemptTmp == NULL || EFI_ERROR (Status)) {\r
b7cc5bf1
WJ
957 continue;\r
958 }\r
107e3d7a 959 \r
b7cc5bf1
WJ
960 ASSERT (AttemptConfigOrder[Index] == AttemptTmp->AttemptConfigIndex);\r
961\r
962 if (AttemptTmp->SessionConfigData.Enabled == ISCSI_DISABLED) {\r
963 FreePool (AttemptTmp);\r
964 continue;\r
965 }\r
966\r
967 if (AttemptTmp->SessionConfigData.IpMode != IP_MODE_AUTOCONFIG && \r
968 AttemptTmp->SessionConfigData.IpMode != ((IpVersion == IP_VERSION_4) ? IP_MODE_IP4 : IP_MODE_IP6)) {\r
969 FreePool (AttemptTmp);\r
970 continue;\r
971 }\r
972 \r
973 if(AttemptTmp->SessionConfigData.IpMode == IP_MODE_AUTOCONFIG ||\r
974 AttemptTmp->SessionConfigData.InitiatorInfoFromDhcp == TRUE ||\r
975 AttemptTmp->SessionConfigData.TargetInfoFromDhcp == TRUE) { \r
976 FreePool (AttemptTmp);\r
977 FreePool (AttemptConfigOrder);\r
978 return TRUE;\r
979 }\r
980\r
981 FreePool (AttemptTmp);\r
982 }\r
983 \r
984 FreePool (AttemptConfigOrder);\r
985 return FALSE;\r
986}\r
4c5a5e0c 987\r
988/**\r
989 Get the various configuration data.\r
990\r
991 @param[in] Private The iSCSI driver data.\r
992\r
993 @retval EFI_SUCCESS The configuration data is retrieved.\r
994 @retval EFI_NOT_FOUND This iSCSI driver is not configured yet.\r
995\r
996**/\r
997EFI_STATUS\r
998IScsiGetConfigData (\r
999 IN ISCSI_DRIVER_DATA *Private\r
1000 )\r
1001{\r
1002 EFI_STATUS Status;\r
1003 CHAR16 MacString[ISCSI_MAX_MAC_STRING_LEN];\r
1004 UINTN Index;\r
1005 ISCSI_NIC_INFO *NicInfo;\r
1006 ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData;\r
1007 ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptTmp;\r
1008 UINT8 *AttemptConfigOrder;\r
1009 UINTN AttemptConfigOrderSize;\r
1010 CHAR16 IScsiMode[64];\r
1011 CHAR16 IpMode[64];\r
1012\r
1013 //\r
1014 // There should be at least one attempt configured.\r
1015 //\r
1016 AttemptConfigOrder = IScsiGetVariableAndSize (\r
1017 L"AttemptOrder",\r
9bdc6592 1018 &gIScsiConfigGuid,\r
4c5a5e0c 1019 &AttemptConfigOrderSize\r
1020 );\r
1021 if (AttemptConfigOrder == NULL || AttemptConfigOrderSize == 0) {\r
1022 return EFI_NOT_FOUND;\r
1023 }\r
1024\r
1025 //\r
1026 // Get the iSCSI Initiator Name.\r
1027 //\r
1028 mPrivate->InitiatorNameLength = ISCSI_NAME_MAX_SIZE;\r
1029 Status = gIScsiInitiatorName.Get (\r
1030 &gIScsiInitiatorName,\r
1031 &mPrivate->InitiatorNameLength,\r
1032 mPrivate->InitiatorName\r
1033 );\r
1034 if (EFI_ERROR (Status)) {\r
1035 return Status;\r
1036 }\r
1037\r
1038 //\r
1039 // Get the normal configuration.\r
1040 //\r
1041 for (Index = 0; Index < AttemptConfigOrderSize / sizeof (UINT8); Index++) {\r
1042\r
1043 //\r
1044 // Check whether the attempt exists in AttemptConfig.\r
1045 //\r
1046 AttemptTmp = IScsiConfigGetAttemptByConfigIndex (AttemptConfigOrder[Index]); \r
1047 if (AttemptTmp != NULL && AttemptTmp->SessionConfigData.Enabled == ISCSI_DISABLED) {\r
1048 continue;\r
1049 } else if (AttemptTmp != NULL && AttemptTmp->SessionConfigData.Enabled != ISCSI_DISABLED) {\r
1050 //\r
1051 // Check the autoconfig path to see whether it should be retried.\r
1052 //\r
1053 if (AttemptTmp->SessionConfigData.IpMode == IP_MODE_AUTOCONFIG &&\r
1054 AttemptTmp->AutoConfigureMode != IP_MODE_AUTOCONFIG_SUCCESS) {\r
1055 if (mPrivate->Ipv6Flag &&\r
1056 AttemptTmp->AutoConfigureMode == IP_MODE_AUTOCONFIG_IP6) {\r
1057 //\r
1058 // Autoconfigure for IP6 already attempted but failed. Do not try again.\r
1059 //\r
1060 continue;\r
1061 } else if (!mPrivate->Ipv6Flag &&\r
1062 AttemptTmp->AutoConfigureMode == IP_MODE_AUTOCONFIG_IP4) {\r
1063 //\r
1064 // Autoconfigure for IP4 already attempted but failed. Do not try again.\r
1065 //\r
1066 continue;\r
1067 } else {\r
1068 //\r
1069 // Try another approach for this autoconfigure path.\r
1070 //\r
1071 AttemptTmp->AutoConfigureMode =\r
1072 (UINT8) (mPrivate->Ipv6Flag ? IP_MODE_AUTOCONFIG_IP6 : IP_MODE_AUTOCONFIG_IP4);\r
1073 AttemptTmp->SessionConfigData.InitiatorInfoFromDhcp = TRUE;\r
1074 AttemptTmp->SessionConfigData.TargetInfoFromDhcp = TRUE;\r
1075 AttemptTmp->DhcpSuccess = FALSE;\r
1076\r
1077 //\r
1078 // Get some information from the dhcp server.\r
1079 //\r
1080 if (!mPrivate->Ipv6Flag) {\r
1081 Status = IScsiDoDhcp (Private->Image, Private->Controller, AttemptTmp);\r
1082 if (!EFI_ERROR (Status)) {\r
1083 AttemptTmp->DhcpSuccess = TRUE;\r
1084 }\r
1085 } else {\r
1086 Status = IScsiDoDhcp6 (Private->Image, Private->Controller, AttemptTmp);\r
1087 if (!EFI_ERROR (Status)) {\r
1088 AttemptTmp->DhcpSuccess = TRUE;\r
1089 }\r
1090 }\r
1091\r
1092 //\r
1093 // Refresh the state of this attempt to NVR.\r
1094 //\r
1095 AsciiStrToUnicodeStr (AttemptTmp->MacString, MacString);\r
1096 UnicodeSPrint (\r
1097 mPrivate->PortString,\r
1098 (UINTN) ISCSI_NAME_IFR_MAX_SIZE,\r
1099 L"%s%d",\r
1100 MacString,\r
1101 (UINTN) AttemptTmp->AttemptConfigIndex\r
1102 );\r
1103\r
1104 gRT->SetVariable (\r
1105 mPrivate->PortString,\r
1106 &gEfiIScsiInitiatorNameProtocolGuid,\r
1107 ISCSI_CONFIG_VAR_ATTR,\r
1108 sizeof (ISCSI_ATTEMPT_CONFIG_NVDATA),\r
1109 AttemptTmp\r
1110 );\r
1111\r
1112 continue;\r
1113 }\r
1114 } else if (AttemptTmp->SessionConfigData.InitiatorInfoFromDhcp && !AttemptTmp->ValidPath) {\r
1115 //\r
1116 // Get DHCP information for already added, but failed, attempt.\r
1117 //\r
1118 AttemptTmp->DhcpSuccess = FALSE;\r
1119 if (!mPrivate->Ipv6Flag && (AttemptTmp->SessionConfigData.IpMode == IP_MODE_IP4)) {\r
1120 Status = IScsiDoDhcp (Private->Image, Private->Controller, AttemptTmp);\r
1121 if (!EFI_ERROR (Status)) {\r
1122 AttemptTmp->DhcpSuccess = TRUE;\r
1123 }\r
1124 } else if (mPrivate->Ipv6Flag && (AttemptTmp->SessionConfigData.IpMode == IP_MODE_IP6)) {\r
1125 Status = IScsiDoDhcp6 (Private->Image, Private->Controller, AttemptTmp);\r
1126 if (!EFI_ERROR (Status)) {\r
1127 AttemptTmp->DhcpSuccess = TRUE;\r
1128 }\r
1129 }\r
1130\r
1131 //\r
1132 // Refresh the state of this attempt to NVR.\r
1133 //\r
1134 AsciiStrToUnicodeStr (AttemptTmp->MacString, MacString);\r
1135 UnicodeSPrint (\r
1136 mPrivate->PortString,\r
1137 (UINTN) ISCSI_NAME_IFR_MAX_SIZE,\r
1138 L"%s%d",\r
1139 MacString,\r
1140 (UINTN) AttemptTmp->AttemptConfigIndex\r
1141 );\r
1142\r
1143 gRT->SetVariable (\r
1144 mPrivate->PortString,\r
1145 &gEfiIScsiInitiatorNameProtocolGuid,\r
1146 ISCSI_CONFIG_VAR_ATTR,\r
1147 sizeof (ISCSI_ATTEMPT_CONFIG_NVDATA),\r
1148 AttemptTmp\r
1149 );\r
1150\r
1151 continue;\r
1152\r
1153 } else {\r
1154 continue;\r
1155 }\r
1156 }\r
1157\r
1158 //\r
1159 // This attempt does not exist in AttemptConfig. Try to add a new one.\r
1160 //\r
1161\r
1162 NicInfo = IScsiGetNicInfoByIndex (mPrivate->CurrentNic);\r
1163 ASSERT (NicInfo != NULL);\r
1164 IScsiMacAddrToStr (&NicInfo->PermanentAddress, NicInfo->HwAddressSize, NicInfo->VlanId, MacString);\r
1165 UnicodeSPrint (\r
1166 mPrivate->PortString,\r
1167 (UINTN) 128,\r
1168 L"%s%d",\r
1169 MacString,\r
1170 (UINTN) AttemptConfigOrder[Index]\r
1171 );\r
1172\r
bf4a3dbd
ED
1173 GetVariable2 (\r
1174 mPrivate->PortString,\r
1175 &gEfiIScsiInitiatorNameProtocolGuid,\r
f01b91ae 1176 (VOID**)&AttemptConfigData,\r
bf4a3dbd
ED
1177 NULL\r
1178 );\r
4c5a5e0c 1179\r
1180 if (AttemptConfigData == NULL) {\r
1181 continue;\r
1182 }\r
1183\r
1184 ASSERT (AttemptConfigOrder[Index] == AttemptConfigData->AttemptConfigIndex);\r
1185\r
1186 AttemptConfigData->NicIndex = NicInfo->NicIndex;\r
1187 AttemptConfigData->DhcpSuccess = FALSE;\r
1188 AttemptConfigData->ValidiBFTPath = (BOOLEAN) (mPrivate->EnableMpio ? TRUE : FALSE);\r
1189 AttemptConfigData->ValidPath = FALSE;\r
1190\r
1191 if (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_AUTOCONFIG) {\r
1192 AttemptConfigData->SessionConfigData.InitiatorInfoFromDhcp = TRUE;\r
1193 AttemptConfigData->SessionConfigData.TargetInfoFromDhcp = TRUE;\r
1194\r
1195 AttemptConfigData->AutoConfigureMode =\r
1196 (UINT8) (mPrivate->Ipv6Flag ? IP_MODE_AUTOCONFIG_IP6 : IP_MODE_AUTOCONFIG_IP4);\r
1197 }\r
1198 \r
1199 //\r
1200 // Get some information from dhcp server.\r
1201 //\r
1202 if (AttemptConfigData->SessionConfigData.Enabled != ISCSI_DISABLED &&\r
1203 AttemptConfigData->SessionConfigData.InitiatorInfoFromDhcp) {\r
1204\r
1205 if (!mPrivate->Ipv6Flag &&\r
1206 (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_IP4 ||\r
1207 AttemptConfigData->AutoConfigureMode == IP_MODE_AUTOCONFIG_IP4)) {\r
1208 Status = IScsiDoDhcp (Private->Image, Private->Controller, AttemptConfigData);\r
1209 if (!EFI_ERROR (Status)) {\r
1210 AttemptConfigData->DhcpSuccess = TRUE;\r
1211 }\r
1212 } else if (mPrivate->Ipv6Flag &&\r
1213 (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_IP6 ||\r
1214 AttemptConfigData->AutoConfigureMode == IP_MODE_AUTOCONFIG_IP6)) {\r
1215 Status = IScsiDoDhcp6 (Private->Image, Private->Controller, AttemptConfigData);\r
1216 if (!EFI_ERROR (Status)) {\r
1217 AttemptConfigData->DhcpSuccess = TRUE;\r
1218 }\r
1219 }\r
1220\r
1221 //\r
1222 // Refresh the state of this attempt to NVR.\r
1223 //\r
1224 AsciiStrToUnicodeStr (AttemptConfigData->MacString, MacString);\r
1225 UnicodeSPrint (\r
1226 mPrivate->PortString,\r
1227 (UINTN) ISCSI_NAME_IFR_MAX_SIZE,\r
1228 L"%s%d",\r
1229 MacString,\r
1230 (UINTN) AttemptConfigData->AttemptConfigIndex\r
1231 );\r
1232\r
1233 gRT->SetVariable (\r
1234 mPrivate->PortString,\r
1235 &gEfiIScsiInitiatorNameProtocolGuid,\r
1236 ISCSI_CONFIG_VAR_ATTR,\r
1237 sizeof (ISCSI_ATTEMPT_CONFIG_NVDATA),\r
1238 AttemptConfigData\r
1239 );\r
1240 }\r
1241\r
1242 //\r
1243 // Update Attempt Help Info.\r
1244 //\r
1245\r
1246 if (AttemptConfigData->SessionConfigData.Enabled == ISCSI_DISABLED) {\r
1247 UnicodeSPrint (IScsiMode, 64, L"Disabled");\r
1248 } else if (AttemptConfigData->SessionConfigData.Enabled == ISCSI_ENABLED) {\r
1249 UnicodeSPrint (IScsiMode, 64, L"Enabled");\r
1250 } else if (AttemptConfigData->SessionConfigData.Enabled == ISCSI_ENABLED_FOR_MPIO) {\r
1251 UnicodeSPrint (IScsiMode, 64, L"Enabled for MPIO");\r
1252 }\r
1253\r
1254 if (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_IP4) {\r
1255 UnicodeSPrint (IpMode, 64, L"IP4");\r
1256 } else if (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_IP6) {\r
1257 UnicodeSPrint (IpMode, 64, L"IP6");\r
1258 } else if (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_AUTOCONFIG) {\r
1259 UnicodeSPrint (IpMode, 64, L"Autoconfigure");\r
1260 }\r
1261\r
1262 UnicodeSPrint (\r
1263 mPrivate->PortString,\r
1264 (UINTN) ISCSI_NAME_IFR_MAX_SIZE,\r
1265 L"MAC: %s, PFA: Bus %d | Dev %d | Func %d, iSCSI mode: %s, IP version: %s",\r
1266 MacString,\r
1267 NicInfo->BusNumber,\r
1268 NicInfo->DeviceNumber,\r
1269 NicInfo->FunctionNumber,\r
1270 IScsiMode,\r
1271 IpMode\r
1272 );\r
1273\r
1274 AttemptConfigData->AttemptTitleHelpToken = HiiSetString (\r
1275 mCallbackInfo->RegisteredHandle,\r
1276 0,\r
1277 mPrivate->PortString,\r
1278 NULL\r
1279 );\r
1280 ASSERT (AttemptConfigData->AttemptTitleHelpToken != 0);\r
1281\r
1282 //\r
1283 // Record the attempt in global link list.\r
1284 //\r
1285 InsertTailList (&mPrivate->AttemptConfigs, &AttemptConfigData->Link);\r
1286 mPrivate->AttemptCount++;\r
1287\r
1288 if (AttemptConfigData->SessionConfigData.Enabled == ISCSI_ENABLED_FOR_MPIO) {\r
1289 mPrivate->MpioCount++;\r
1290 mPrivate->EnableMpio = TRUE;\r
1291\r
1292 if (AttemptConfigData->AuthenticationType == ISCSI_AUTH_TYPE_KRB) {\r
1293 mPrivate->Krb5MpioCount++;\r
1294 }\r
1295 } else if (AttemptConfigData->SessionConfigData.Enabled == ISCSI_ENABLED) {\r
1296 mPrivate->SinglePathCount++;\r
1297 }\r
1298 }\r
1299\r
1300 //\r
1301 // Reorder the AttemptConfig by the configured order.\r
1302 //\r
1303 for (Index = 0; Index < AttemptConfigOrderSize / sizeof (UINT8); Index++) {\r
1304 AttemptConfigData = IScsiConfigGetAttemptByConfigIndex (AttemptConfigOrder[Index]);\r
1305 if (AttemptConfigData == NULL) {\r
1306 continue;\r
1307 }\r
1308\r
1309 RemoveEntryList (&AttemptConfigData->Link);\r
1310 InsertTailList (&mPrivate->AttemptConfigs, &AttemptConfigData->Link);\r
1311 }\r
1312\r
1313 //\r
1314 // Update the Main Form.\r
1315 //\r
1316 IScsiConfigUpdateAttempt ();\r
1317\r
1318 FreePool (AttemptConfigOrder);\r
1319\r
1320 //\r
1321 // There should be at least one attempt configuration.\r
1322 //\r
1323 if (!mPrivate->EnableMpio) {\r
1324 if (mPrivate->SinglePathCount == 0) {\r
1325 return EFI_NOT_FOUND;\r
1326 }\r
1327 mPrivate->ValidSinglePathCount = mPrivate->SinglePathCount;\r
1328 }\r
1329\r
1330 return EFI_SUCCESS;\r
1331}\r
1332\r
1333\r
1334/**\r
1335 Get the device path of the iSCSI tcp connection and update it.\r
1336\r
1337 @param Session The iSCSI session.\r
1338\r
1339 @return The updated device path.\r
1340 @retval NULL Other errors as indicated.\r
1341\r
1342**/\r
1343EFI_DEVICE_PATH_PROTOCOL *\r
1344IScsiGetTcpConnDevicePath (\r
1345 IN ISCSI_SESSION *Session\r
1346 )\r
1347{\r
1348 ISCSI_CONNECTION *Conn;\r
1349 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
1350 EFI_STATUS Status;\r
1351 EFI_DEV_PATH *DPathNode;\r
1352\r
1353 if (Session->State != SESSION_STATE_LOGGED_IN) {\r
1354 return NULL;\r
1355 }\r
1356\r
1357 Conn = NET_LIST_USER_STRUCT_S (\r
1358 Session->Conns.ForwardLink,\r
1359 ISCSI_CONNECTION,\r
1360 Link,\r
1361 ISCSI_CONNECTION_SIGNATURE\r
1362 );\r
1363\r
1364 Status = gBS->HandleProtocol (\r
1365 Conn->TcpIo.Handle,\r
1366 &gEfiDevicePathProtocolGuid,\r
1367 (VOID **) &DevicePath\r
1368 ); \r
1369 if (EFI_ERROR (Status)) {\r
1370 return NULL;\r
1371 }\r
1372 //\r
1373 // Duplicate it.\r
1374 //\r
1375 DevicePath = DuplicateDevicePath (DevicePath);\r
1376 if (DevicePath == NULL) {\r
1377 return NULL;\r
1378 }\r
1379\r
1380 DPathNode = (EFI_DEV_PATH *) DevicePath;\r
1381\r
1382 while (!IsDevicePathEnd (&DPathNode->DevPath)) {\r
1383 if (DevicePathType (&DPathNode->DevPath) == MESSAGING_DEVICE_PATH) {\r
1384 if (!Conn->Ipv6Flag && DevicePathSubType (&DPathNode->DevPath) == MSG_IPv4_DP) {\r
1385 DPathNode->Ipv4.LocalPort = 0;\r
501793fa
RN
1386\r
1387 DPathNode->Ipv4.StaticIpAddress = \r
1388 (BOOLEAN) (!Session->ConfigData->SessionConfigData.InitiatorInfoFromDhcp);\r
1389\r
1390 IP4_COPY_ADDRESS (\r
1391 &DPathNode->Ipv4.GatewayIpAddress,\r
1392 &Session->ConfigData->SessionConfigData.Gateway\r
1393 );\r
1394\r
1395 IP4_COPY_ADDRESS (\r
1396 &DPathNode->Ipv4.SubnetMask,\r
1397 &Session->ConfigData->SessionConfigData.SubnetMask\r
1398 );\r
4c5a5e0c 1399 break;\r
1400 } else if (Conn->Ipv6Flag && DevicePathSubType (&DPathNode->DevPath) == MSG_IPv6_DP) {\r
1401 DPathNode->Ipv6.LocalPort = 0;\r
501793fa
RN
1402 DPathNode->Ipv6.IpAddressOrigin = 0;\r
1403 DPathNode->Ipv6.PrefixLength = IP6_PREFIX_LENGTH;\r
1404 ZeroMem (&DPathNode->Ipv6.GatewayIpAddress, sizeof (EFI_IPv6_ADDRESS));\r
4c5a5e0c 1405 break;\r
1406 }\r
1407 }\r
1408\r
1409 DPathNode = (EFI_DEV_PATH *) NextDevicePathNode (&DPathNode->DevPath);\r
1410 }\r
1411\r
1412 return DevicePath;\r
1413}\r
1414\r
1415\r
1416/**\r
1417 Abort the session when the transition from BS to RT is initiated.\r
1418\r
1419 @param[in] Event The event signaled.\r
1420 @param[in] Context The iSCSI driver data.\r
1421\r
1422**/\r
1423VOID\r
1424EFIAPI\r
1425IScsiOnExitBootService (\r
1426 IN EFI_EVENT Event,\r
1427 IN VOID *Context\r
1428 )\r
1429{\r
1430 ISCSI_DRIVER_DATA *Private;\r
1431\r
1432 Private = (ISCSI_DRIVER_DATA *) Context;\r
1433 gBS->CloseEvent (Private->ExitBootServiceEvent);\r
1434\r
1435 if (Private->Session != NULL) {\r
1436 IScsiSessionAbort (Private->Session);\r
1437 }\r
1438}\r
18b24f92
FS
1439\r
1440/**\r
1441 Tests whether a controller handle is being managed by IScsi driver.\r
1442\r
1443 This function tests whether the driver specified by DriverBindingHandle is\r
1444 currently managing the controller specified by ControllerHandle. This test\r
1445 is performed by evaluating if the the protocol specified by ProtocolGuid is\r
1446 present on ControllerHandle and is was opened by DriverBindingHandle and Nic\r
1447 Device handle with an attribute of EFI_OPEN_PROTOCOL_BY_DRIVER. \r
1448 If ProtocolGuid is NULL, then ASSERT().\r
1449\r
1450 @param ControllerHandle A handle for a controller to test.\r
1451 @param DriverBindingHandle Specifies the driver binding handle for the\r
1452 driver.\r
1453 @param ProtocolGuid Specifies the protocol that the driver specified\r
1454 by DriverBindingHandle opens in its Start()\r
1455 function.\r
1456\r
1457 @retval EFI_SUCCESS ControllerHandle is managed by the driver\r
1458 specified by DriverBindingHandle.\r
1459 @retval EFI_UNSUPPORTED ControllerHandle is not managed by the driver\r
1460 specified by DriverBindingHandle.\r
1461\r
1462**/\r
1463EFI_STATUS\r
1464EFIAPI\r
1465IScsiTestManagedDevice (\r
1466 IN EFI_HANDLE ControllerHandle,\r
1467 IN EFI_HANDLE DriverBindingHandle,\r
1468 IN EFI_GUID *ProtocolGuid\r
1469 )\r
1470{\r
1471 EFI_STATUS Status;\r
1472 VOID *ManagedInterface;\r
1473 EFI_HANDLE NicControllerHandle;\r
1474\r
1475 ASSERT (ProtocolGuid != NULL);\r
1476\r
1477 NicControllerHandle = NetLibGetNicHandle (ControllerHandle, ProtocolGuid);\r
1478 if (NicControllerHandle == NULL) {\r
1479 return EFI_UNSUPPORTED;\r
1480 }\r
1481\r
1482 Status = gBS->OpenProtocol (\r
1483 ControllerHandle,\r
1484 (EFI_GUID *) ProtocolGuid,\r
1485 &ManagedInterface,\r
1486 DriverBindingHandle,\r
1487 NicControllerHandle,\r
1488 EFI_OPEN_PROTOCOL_BY_DRIVER\r
1489 );\r
1490 if (!EFI_ERROR (Status)) {\r
1491 gBS->CloseProtocol (\r
1492 ControllerHandle,\r
1493 (EFI_GUID *) ProtocolGuid,\r
1494 DriverBindingHandle,\r
1495 NicControllerHandle\r
1496 );\r
1497 return EFI_UNSUPPORTED;\r
1498 }\r
1499\r
1500 if (Status != EFI_ALREADY_STARTED) {\r
1501 return EFI_UNSUPPORTED;\r
1502 }\r
1503\r
1504 return EFI_SUCCESS;\r
1505}\r