]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/IScsiDxe/IScsiMisc.c
Remove the old unused ValueToString code, which has been replaced by UnicodeValueToSt...
[mirror_edk2.git] / MdeModulePkg / Universal / Network / IScsiDxe / IScsiMisc.c
CommitLineData
12618416 1/** @file\r
55a64ae0 2 Miscellaneous routines for iSCSI driver.\r
6a690e23 3\r
894d038a 4Copyright (c) 2004 - 2009, Intel Corporation.<BR>\r
7a444476 5All rights reserved. This 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
6a690e23 12\r
12618416 13**/\r
6a690e23 14\r
15#include "IScsiImpl.h"\r
16\r
ac7e320c 17GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 IScsiHexString[] = "0123456789ABCDEFabcdef";\r
6a690e23 18\r
12618416 19/**\r
20 Removes (trims) specified leading and trailing characters from a string.\r
21\r
1055b68d 22 @param[in, out] Str Pointer to the null-terminated string to be trimmed. On return, \r
963dbb30 23 Str will hold the trimmed string. \r
12618416 24\r
1055b68d 25 @param[in] CharC Character will be trimmed from str.\r
12618416 26**/\r
6a690e23 27VOID\r
28StrTrim (\r
1055b68d 29 IN OUT CHAR16 *Str,\r
6a690e23 30 IN CHAR16 CharC\r
31 )\r
6a690e23 32{\r
1055b68d 33 CHAR16 *Pointer1;\r
34 CHAR16 *Pointer2;\r
6a690e23 35 \r
1055b68d 36 if (*Str == 0) {\r
6a690e23 37 return;\r
38 }\r
39 \r
40 //\r
41 // Trim off the leading and trailing characters c\r
42 //\r
1055b68d 43 for (Pointer1 = Str; (*Pointer1 != 0) && (*Pointer1 == CharC); Pointer1++) {\r
6a690e23 44 ;\r
45 }\r
46 \r
1055b68d 47 Pointer2 = Str;\r
48 if (Pointer2 == Pointer1) {\r
49 while (*Pointer1 != 0) {\r
50 Pointer2++;\r
51 Pointer1++;\r
6a690e23 52 }\r
53 } else {\r
1055b68d 54 while (*Pointer1 != 0) { \r
55 *Pointer2 = *Pointer1; \r
56 Pointer1++;\r
57 Pointer2++;\r
6a690e23 58 }\r
1055b68d 59 *Pointer2 = 0;\r
6a690e23 60 }\r
61 \r
62 \r
1055b68d 63 for (Pointer1 = Str + StrLen(Str) - 1; Pointer1 >= Str && *Pointer1 == CharC; Pointer1--) {\r
6a690e23 64 ;\r
65 }\r
1055b68d 66 if (Pointer1 != Str + StrLen(Str) - 1) { \r
67 *(Pointer1 + 1) = 0;\r
6a690e23 68 }\r
69}\r
70\r
12618416 71/**\r
6a690e23 72 Calculate the prefix length of the IPv4 subnet mask.\r
73\r
1055b68d 74 @param[in] SubnetMask The IPv4 subnet mask.\r
6a690e23 75\r
1055b68d 76 @return The prefix length of the subnet mask.\r
963dbb30 77 @retval 0 Other errors as indicated.\r
12618416 78**/\r
79UINT8\r
80IScsiGetSubnetMaskPrefixLength (\r
81 IN EFI_IPv4_ADDRESS *SubnetMask\r
82 )\r
6a690e23 83{\r
84 UINT8 Len;\r
85 UINT32 ReverseMask;\r
86\r
87 //\r
88 // The SubnetMask is in network byte order.\r
89 //\r
90 ReverseMask = (SubnetMask->Addr[0] << 24) | (SubnetMask->Addr[1] << 16) | (SubnetMask->Addr[2] << 8) | (SubnetMask->Addr[3]);\r
91\r
92 //\r
93 // Reverse it.\r
94 //\r
95 ReverseMask = ~ReverseMask;\r
96\r
d5893e0b 97 if ((ReverseMask & (ReverseMask + 1)) != 0) {\r
6a690e23 98 return 0;\r
99 }\r
100\r
101 Len = 0;\r
102\r
103 while (ReverseMask != 0) {\r
104 ReverseMask = ReverseMask >> 1;\r
105 Len++;\r
106 }\r
107\r
69b0882d 108 return (UINT8) (32 - Len);\r
6a690e23 109}\r
110\r
12618416 111/**\r
6a690e23 112 Convert the hexadecimal encoded LUN string into the 64-bit LUN. \r
113\r
1055b68d 114 @param[in] Str The hexadecimal encoded LUN string.\r
115 @param[out] Lun Storage to return the 64-bit LUN.\r
6a690e23 116\r
12618416 117 @retval EFI_SUCCESS The 64-bit LUN is stored in Lun.\r
12618416 118 @retval EFI_INVALID_PARAMETER The string is malformatted.\r
12618416 119**/\r
120EFI_STATUS\r
121IScsiAsciiStrToLun (\r
122 IN CHAR8 *Str,\r
123 OUT UINT8 *Lun\r
124 )\r
6a690e23 125{\r
63d55bb9
LG
126 UINTN Index, IndexValue, IndexNum, SizeStr;\r
127 CHAR8 TemStr[2];\r
128 UINT8 TemValue;\r
f6b7393c 129 UINT16 Value[4];\r
63d55bb9 130 \r
e48e37fc 131 ZeroMem (Lun, 8);\r
63d55bb9
LG
132 ZeroMem (TemStr, 2);\r
133 ZeroMem ((UINT8 *) Value, sizeof (Value));\r
134 SizeStr = AsciiStrLen (Str); \r
135 IndexValue = 0;\r
136 IndexNum = 0;\r
137\r
138 for (Index = 0; Index < SizeStr; Index ++) {\r
139 TemStr[0] = Str[Index];\r
140 TemValue = (UINT8) AsciiStrHexToUint64 (TemStr);\r
141 if (TemValue == 0 && TemStr[0] != '0') {\r
142 if ((TemStr[0] != '-') || (IndexNum == 0)) {\r
143 //\r
144 // Invalid Lun Char\r
145 //\r
146 return EFI_INVALID_PARAMETER;\r
6a690e23 147 }\r
6a690e23 148 }\r
63d55bb9
LG
149 \r
150 if ((TemValue == 0) && (TemStr[0] == '-')) {\r
151 //\r
152 // Next Lun value\r
153 //\r
154 if (++IndexValue >= 4) {\r
155 //\r
156 // Max 4 Lun value\r
157 //\r
158 return EFI_INVALID_PARAMETER;\r
159 }\r
160 //\r
161 // Restart str index for the next lun value\r
162 //\r
163 IndexNum = 0;\r
164 continue;\r
165 }\r
166 \r
167 if (++IndexNum > 4) {\r
168 // \r
169 // Each Lun Str can't exceed size 4, because it will be as UINT16 value\r
170 //\r
6a690e23 171 return EFI_INVALID_PARAMETER;\r
172 }\r
63d55bb9
LG
173 \r
174 //\r
175 // Combine UINT16 value\r
176 //\r
177 Value[IndexValue] = (UINT16) ((Value[IndexValue] << 4) + TemValue);\r
6a690e23 178 }\r
63d55bb9
LG
179 \r
180 for (Index = 0; Index <= IndexValue; Index ++) {\r
181 *((UINT16 *) &Lun[Index * 2]) = HTONS (Value[Index]);\r
182 }\r
183 \r
6a690e23 184 return EFI_SUCCESS;\r
185}\r
186\r
12618416 187/**\r
6a690e23 188 Convert the 64-bit LUN into the hexadecimal encoded LUN string.\r
189\r
1055b68d 190 @param[in] Lun The 64-bit LUN.\r
191 @param[out] Str The storage to return the hexadecimal encoded LUN string.\r
12618416 192**/\r
193VOID\r
194IScsiLunToUnicodeStr (\r
195 IN UINT8 *Lun,\r
196 OUT CHAR16 *Str\r
197 )\r
6a690e23 198{\r
199 UINTN Index;\r
200 CHAR16 *TempStr;\r
201\r
202 TempStr = Str;\r
203\r
204 for (Index = 0; Index < 4; Index++) {\r
205\r
206 if ((Lun[2 * Index] | Lun[2 * Index + 1]) == 0) {\r
207 StrCpy (TempStr, L"0-");\r
208 } else {\r
209 TempStr[0] = (CHAR16) IScsiHexString[Lun[2 * Index] >> 4];\r
ac7e320c 210 TempStr[1] = (CHAR16) IScsiHexString[Lun[2 * Index] & 0x0F];\r
6a690e23 211 TempStr[2] = (CHAR16) IScsiHexString[Lun[2 * Index + 1] >> 4];\r
ac7e320c 212 TempStr[3] = (CHAR16) IScsiHexString[Lun[2 * Index + 1] & 0x0F];\r
6a690e23 213 TempStr[4] = L'-';\r
214 TempStr[5] = 0;\r
215\r
216 StrTrim (TempStr, L'0');\r
217 }\r
218\r
219 TempStr += StrLen (TempStr);\r
220 }\r
221\r
222 Str[StrLen (Str) - 1] = 0;\r
223\r
224 for (Index = StrLen (Str) - 1; Index > 1; Index = Index - 2) {\r
225 if ((Str[Index] == L'0') && (Str[Index - 1] == L'-')) {\r
226 Str[Index - 1] = 0;\r
227 } else {\r
228 break;\r
229 }\r
230 }\r
231}\r
232\r
12618416 233/**\r
6a690e23 234 Convert the ASCII string into a UNICODE string.\r
235\r
1055b68d 236 @param[in] Source The ASCII string.\r
237 @param[out] Destination The storage to return the UNICODE string.\r
6a690e23 238\r
1055b68d 239 @return CHAR16 * Pointer to the UNICODE string.\r
12618416 240**/\r
241CHAR16 *\r
242IScsiAsciiStrToUnicodeStr (\r
243 IN CHAR8 *Source,\r
244 OUT CHAR16 *Destination\r
245 )\r
6a690e23 246{\r
247 ASSERT (Destination != NULL);\r
248 ASSERT (Source != NULL);\r
249\r
250 while (*Source != '\0') {\r
251 *(Destination++) = (CHAR16) *(Source++);\r
252 }\r
253\r
254 *Destination = '\0';\r
255\r
256 return Destination;\r
257}\r
258\r
12618416 259/**\r
6a690e23 260 Convert the UNICODE string into an ASCII string.\r
261\r
1055b68d 262 @param[in] Source The UNICODE string.\r
263 @param[out] Destination The storage to return the ASCII string.\r
6a690e23 264\r
1055b68d 265 @return CHAR8 * Pointer to the ASCII string.\r
266**/\r
12618416 267CHAR8 *\r
268IScsiUnicodeStrToAsciiStr (\r
269 IN CHAR16 *Source,\r
270 OUT CHAR8 *Destination\r
271 )\r
6a690e23 272{\r
273 ASSERT (Destination != NULL);\r
274 ASSERT (Source != NULL);\r
275\r
276 while (*Source != '\0') {\r
277 //\r
278 // If any Unicode characters in Source contain\r
279 // non-zero value in the upper 8 bits, then ASSERT().\r
280 //\r
281 ASSERT (*Source < 0x100);\r
282 *(Destination++) = (CHAR8) *(Source++);\r
283 }\r
284\r
285 *Destination = '\0';\r
286\r
287 return Destination;\r
288}\r
289\r
12618416 290/**\r
6a690e23 291 Convert the decimal dotted IPv4 address into the binary IPv4 address.\r
292\r
1055b68d 293 @param[in] Str The UNICODE string.\r
294 @param[out] Ip The storage to return the ASCII string.\r
6a690e23 295\r
12618416 296 @retval EFI_SUCCESS The binary IP address is returned in Ip.\r
12618416 297 @retval EFI_INVALID_PARAMETER The IP string is malformatted.\r
12618416 298**/\r
299EFI_STATUS\r
300IScsiAsciiStrToIp (\r
301 IN CHAR8 *Str,\r
302 OUT EFI_IPv4_ADDRESS *Ip\r
303 )\r
6a690e23 304{\r
305 UINTN Index;\r
306 UINTN Number;\r
307\r
308 Index = 0;\r
309\r
c5de0d55 310 while (*Str != 0) {\r
6a690e23 311\r
312 if (Index > 3) {\r
313 return EFI_INVALID_PARAMETER;\r
314 }\r
315\r
316 Number = 0;\r
317 while (NET_IS_DIGIT (*Str)) {\r
318 Number = Number * 10 + (*Str - '0');\r
319 Str++;\r
320 }\r
321\r
322 if (Number > 0xFF) {\r
323 return EFI_INVALID_PARAMETER;\r
324 }\r
325\r
326 Ip->Addr[Index] = (UINT8) Number;\r
327\r
328 if ((*Str != '\0') && (*Str != '.')) {\r
329 //\r
330 // The current character should be either the NULL terminator or\r
331 // the dot delimiter.\r
332 //\r
333 return EFI_INVALID_PARAMETER;\r
334 }\r
335\r
336 if (*Str == '.') {\r
337 //\r
338 // Skip the delimiter.\r
339 //\r
340 Str++;\r
341 }\r
342\r
343 Index++;\r
344 }\r
345\r
346 if (Index != 4) {\r
347 return EFI_INVALID_PARAMETER;\r
348 }\r
349\r
350 return EFI_SUCCESS;\r
351}\r
352\r
12618416 353/**\r
6a690e23 354 Convert the mac address into a hexadecimal encoded "-" seperated string.\r
355\r
1055b68d 356 @param[in] Mac The mac address.\r
357 @param[in] Len Length in bytes of the mac address.\r
358 @param[out] Str The storage to return the mac string.\r
12618416 359**/\r
360VOID\r
361IScsiMacAddrToStr (\r
362 IN EFI_MAC_ADDRESS *Mac,\r
363 IN UINT32 Len,\r
364 OUT CHAR16 *Str\r
365 )\r
6a690e23 366{\r
367 UINT32 Index;\r
368\r
369 for (Index = 0; Index < Len; Index++) {\r
ac7e320c
LG
370 Str[3 * Index] = (CHAR16) IScsiHexString[(Mac->Addr[Index] >> 4) & 0x0F];\r
371 Str[3 * Index + 1] = (CHAR16) IScsiHexString[Mac->Addr[Index] & 0x0F];\r
6a690e23 372 Str[3 * Index + 2] = L'-';\r
373 }\r
374\r
375 Str[3 * Index - 1] = L'\0';\r
376}\r
377\r
12618416 378/**\r
379 Convert the binary encoded buffer into a hexadecimal encoded string.\r
6a690e23 380\r
1055b68d 381 @param[in] BinBuffer The buffer containing the binary data.\r
382 @param[in] BinLength Length of the binary buffer.\r
383 @param[in, out] HexStr Pointer to the string.\r
384 @param[in, out] HexLength The length of the string.\r
6a690e23 385\r
12618416 386 @retval EFI_SUCCESS The binary data is converted to the hexadecimal string \r
387 and the length of the string is updated.\r
12618416 388 @retval EFI_BUFFER_TOO_SMALL The string is too small.\r
1055b68d 389 @retval EFI_INVALID_PARAMETER The IP string is malformatted.\r
12618416 390**/\r
391EFI_STATUS\r
392IScsiBinToHex (\r
393 IN UINT8 *BinBuffer,\r
394 IN UINT32 BinLength,\r
395 IN OUT CHAR8 *HexStr,\r
396 IN OUT UINT32 *HexLength\r
397 )\r
6a690e23 398{\r
399 UINTN Index;\r
400\r
401 if ((HexStr == NULL) || (BinBuffer == NULL) || (BinLength == 0)) {\r
402 return EFI_INVALID_PARAMETER;\r
403 }\r
404\r
405 if (((*HexLength) - 3) < BinLength * 2) {\r
406 *HexLength = BinLength * 2 + 3;\r
407 return EFI_BUFFER_TOO_SMALL;\r
408 }\r
409\r
410 *HexLength = BinLength * 2 + 3;\r
411 //\r
412 // Prefix for Hex String\r
413 //\r
414 HexStr[0] = '0';\r
415 HexStr[1] = 'x';\r
416\r
417 for (Index = 0; Index < BinLength; Index++) {\r
418 HexStr[Index * 2 + 2] = IScsiHexString[BinBuffer[Index] >> 4];\r
ac7e320c 419 HexStr[Index * 2 + 3] = IScsiHexString[BinBuffer[Index] & 0x0F];\r
6a690e23 420 }\r
421\r
422 HexStr[Index * 2 + 2] = '\0';\r
423\r
424 return EFI_SUCCESS;\r
425}\r
426\r
12618416 427/**\r
6a690e23 428 Convert the hexadecimal string into a binary encoded buffer.\r
429\r
1055b68d 430 @param[in, out] BinBuffer The binary buffer.\r
431 @param[in, out] BinLength Length of the binary buffer.\r
432 @param[in] HexStr The hexadecimal string.\r
6a690e23 433\r
12618416 434 @retval EFI_SUCCESS The hexadecimal string is converted into a binary\r
435 encoded buffer.\r
963dbb30 436 @retval EFI_BUFFER_TOO_SMALL The binary buffer is too small to hold the converted data.\r
12618416 437**/\r
438EFI_STATUS\r
439IScsiHexToBin (\r
440 IN OUT UINT8 *BinBuffer,\r
441 IN OUT UINT32 *BinLength,\r
442 IN CHAR8 *HexStr\r
443 )\r
6a690e23 444{\r
445 UINTN Index;\r
63d55bb9 446 UINTN Length;\r
6a690e23 447 UINT8 Digit;\r
63d55bb9
LG
448 CHAR8 TemStr[2];\r
449 \r
450 ZeroMem (TemStr, sizeof (TemStr));\r
7c3eaa27 451\r
6a690e23 452 //\r
453 // Find out how many hex characters the string has.\r
454 //\r
63d55bb9
LG
455 if ((HexStr[0] == '0') && ((HexStr[1] == 'x') || (HexStr[1] == 'X'))) {\r
456 HexStr += 2;\r
6a690e23 457 }\r
63d55bb9
LG
458 \r
459 Length = AsciiStrLen (HexStr);\r
6a690e23 460\r
63d55bb9
LG
461 for (Index = 0; Index < Length; Index ++) {\r
462 TemStr[0] = HexStr[Index];\r
463 Digit = (UINT8) AsciiStrHexToUint64 (TemStr);\r
464 if (Digit == 0 && TemStr[0] != '0') {\r
465 //\r
466 // Invalid Lun Char\r
467 //\r
468 break;\r
469 }\r
6a690e23 470 if ((Index & 1) == 0) {\r
63d55bb9 471 BinBuffer [Index/2] = Digit;\r
6a690e23 472 } else {\r
63d55bb9 473 BinBuffer [Index/2] = (UINT8) ((BinBuffer [Index/2] << 4) + Digit);\r
6a690e23 474 }\r
6a690e23 475 }\r
63d55bb9
LG
476 \r
477 *BinLength = (UINT32) ((Index + 1)/2);\r
6a690e23 478\r
479 return EFI_SUCCESS;\r
480}\r
481\r
12618416 482/**\r
6a690e23 483 Generate random numbers.\r
484\r
1055b68d 485 @param[in, out] Rand The buffer to contain random numbers.\r
486 @param[in] RandLength The length of the Rand buffer.\r
12618416 487**/\r
488VOID\r
489IScsiGenRandom (\r
490 IN OUT UINT8 *Rand,\r
491 IN UINTN RandLength\r
492 )\r
6a690e23 493{\r
494 UINT32 Random;\r
495\r
496 while (RandLength > 0) {\r
497 Random = NET_RANDOM (NetRandomInitSeed ());\r
498 *Rand++ = (UINT8) (Random);\r
499 RandLength--;\r
500 }\r
501}\r
502\r
12618416 503/**\r
6a690e23 504 Create the iSCSI driver data..\r
505\r
1055b68d 506 @param[in] Image The handle of the driver image.\r
507 @param[in] Controller The handle of the controller.\r
6a690e23 508\r
1055b68d 509 @return The iSCSI driver data created.\r
963dbb30 510 @retval NULL Other errors as indicated.\r
12618416 511**/\r
512ISCSI_DRIVER_DATA *\r
513IScsiCreateDriverData (\r
514 IN EFI_HANDLE Image,\r
515 IN EFI_HANDLE Controller\r
516 )\r
6a690e23 517{\r
518 ISCSI_DRIVER_DATA *Private;\r
519 EFI_STATUS Status;\r
520\r
e48e37fc 521 Private = AllocateZeroPool (sizeof (ISCSI_DRIVER_DATA));\r
6a690e23 522 if (Private == NULL) {\r
523 return NULL;\r
524 }\r
525\r
526 Private->Signature = ISCSI_DRIVER_DATA_SIGNATURE;\r
527 Private->Image = Image;\r
528 Private->Controller = Controller;\r
529\r
530 //\r
531 // Create an event to be signal when the BS to RT transition is triggerd so\r
532 // as to abort the iSCSI session.\r
533 //\r
01a5c994 534 Status = gBS->CreateEventEx (\r
535 EVT_NOTIFY_SIGNAL,\r
6a690e23 536 TPL_CALLBACK,\r
537 IScsiOnExitBootService,\r
538 Private,\r
01a5c994 539 &gEfiEventExitBootServicesGuid,\r
6a690e23 540 &Private->ExitBootServiceEvent\r
541 );\r
542 if (EFI_ERROR (Status)) {\r
e48e37fc 543 gBS->FreePool (Private);\r
6a690e23 544 return NULL;\r
545 }\r
546\r
e48e37fc 547 CopyMem(&Private->IScsiExtScsiPassThru, &gIScsiExtScsiPassThruProtocolTemplate, sizeof(EFI_EXT_SCSI_PASS_THRU_PROTOCOL));\r
6a690e23 548\r
549 //\r
550 // 0 is designated to the TargetId, so use another value for the AdapterId.\r
551 //\r
552 Private->ExtScsiPassThruMode.AdapterId = 2;\r
553 Private->ExtScsiPassThruMode.Attributes = EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_PHYSICAL | EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL;\r
554 Private->ExtScsiPassThruMode.IoAlign = 4;\r
555 Private->IScsiExtScsiPassThru.Mode = &Private->ExtScsiPassThruMode;\r
556\r
557 //\r
558 // Install the Ext SCSI PASS THRU protocol.\r
559 //\r
560 Status = gBS->InstallProtocolInterface (\r
561 &Private->ExtScsiPassThruHandle,\r
562 &gEfiExtScsiPassThruProtocolGuid,\r
563 EFI_NATIVE_INTERFACE,\r
564 &Private->IScsiExtScsiPassThru\r
565 );\r
566 if (EFI_ERROR (Status)) {\r
567 gBS->CloseEvent (Private->ExitBootServiceEvent);\r
e48e37fc 568 gBS->FreePool (Private);\r
6a690e23 569\r
570 return NULL;\r
571 }\r
572\r
573 IScsiSessionInit (&Private->Session, FALSE);\r
574\r
575 return Private;\r
576}\r
577\r
12618416 578/**\r
6a690e23 579 Clean the iSCSI driver data.\r
580\r
1055b68d 581 @param[in] Private The iSCSI driver data.\r
12618416 582**/\r
583VOID\r
584IScsiCleanDriverData (\r
585 IN ISCSI_DRIVER_DATA *Private\r
586 )\r
6a690e23 587{\r
588 if (Private->DevicePath != NULL) {\r
589 gBS->UninstallProtocolInterface (\r
590 Private->ExtScsiPassThruHandle,\r
591 &gEfiDevicePathProtocolGuid,\r
592 Private->DevicePath\r
593 );\r
594\r
e48e37fc 595 gBS->FreePool (Private->DevicePath);\r
6a690e23 596 }\r
597\r
598 if (Private->ExtScsiPassThruHandle != NULL) {\r
599 gBS->UninstallProtocolInterface (\r
600 Private->ExtScsiPassThruHandle,\r
601 &gEfiExtScsiPassThruProtocolGuid,\r
602 &Private->IScsiExtScsiPassThru\r
603 );\r
604 }\r
605\r
606 gBS->CloseEvent (Private->ExitBootServiceEvent);\r
607\r
e48e37fc 608 gBS->FreePool (Private);\r
6a690e23 609}\r
610\r
12618416 611/**\r
6a690e23 612 Get the various configuration data of this iSCSI instance.\r
613\r
1055b68d 614 @param[in] Private The iSCSI driver data.\r
6a690e23 615\r
12618416 616 @retval EFI_SUCCESS The configuration of this instance is got.\r
1055b68d 617 @retval EFI_ABORTED The operation was aborted.\r
963dbb30 618 @retval Others Other errors as indicated.\r
12618416 619**/\r
620EFI_STATUS\r
621IScsiGetConfigData (\r
622 IN ISCSI_DRIVER_DATA *Private\r
623 )\r
6a690e23 624{\r
625 EFI_STATUS Status;\r
626 ISCSI_SESSION *Session;\r
627 UINTN BufferSize;\r
628 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;\r
629 EFI_SIMPLE_NETWORK_MODE *Mode;\r
630 CHAR16 MacString[65];\r
631\r
632 //\r
633 // get the iSCSI Initiator Name\r
634 //\r
635 Session = &Private->Session;\r
636 Session->InitiatorNameLength = ISCSI_NAME_MAX_SIZE;\r
637 Status = gIScsiInitiatorName.Get (\r
638 &gIScsiInitiatorName,\r
639 &Session->InitiatorNameLength,\r
640 Session->InitiatorName\r
641 );\r
642 if (EFI_ERROR (Status)) {\r
643 return Status;\r
644 }\r
645\r
646 Status = gBS->HandleProtocol (\r
647 Private->Controller,\r
648 &gEfiSimpleNetworkProtocolGuid,\r
69b0882d 649 (VOID **)&Snp\r
6a690e23 650 );\r
651 if (EFI_ERROR (Status)) {\r
652 return Status;\r
653 }\r
654\r
655 Mode = Snp->Mode;\r
656\r
657 //\r
658 // Get the mac string, it's the name of various variable\r
659 //\r
660 IScsiMacAddrToStr (&Mode->PermanentAddress, Mode->HwAddressSize, MacString);\r
661\r
662 //\r
663 // Get the normal configuration.\r
664 //\r
665 BufferSize = sizeof (Session->ConfigData.NvData);\r
666 Status = gRT->GetVariable (\r
667 MacString,\r
668 &gEfiIScsiInitiatorNameProtocolGuid,\r
669 NULL,\r
670 &BufferSize,\r
671 &Session->ConfigData.NvData\r
672 );\r
673 if (EFI_ERROR (Status)) {\r
674 return Status;\r
675 }\r
676\r
677 if (!Session->ConfigData.NvData.Enabled) {\r
678 return EFI_ABORTED;\r
679 }\r
680 //\r
681 // Get the CHAP Auth information.\r
682 //\r
683 BufferSize = sizeof (Session->AuthData.AuthConfig);\r
684 Status = gRT->GetVariable (\r
685 MacString,\r
686 &mIScsiCHAPAuthInfoGuid,\r
687 NULL,\r
688 &BufferSize,\r
689 &Session->AuthData.AuthConfig\r
690 );\r
691\r
692 if (!EFI_ERROR (Status) && Session->ConfigData.NvData.InitiatorInfoFromDhcp) {\r
693 //\r
694 // Start dhcp.\r
695 //\r
696 Status = IScsiDoDhcp (Private->Image, Private->Controller, &Session->ConfigData);\r
697 }\r
698\r
699 return Status;\r
700}\r
701\r
12618416 702/**\r
6a690e23 703 Get the device path of the iSCSI tcp connection and update it.\r
704\r
1055b68d 705 @param[in] Private The iSCSI driver data.\r
6a690e23 706\r
1055b68d 707 @return The updated device path.\r
963dbb30 708 @retval NULL Other errors as indicated.\r
12618416 709**/\r
710EFI_DEVICE_PATH_PROTOCOL *\r
711IScsiGetTcpConnDevicePath (\r
712 IN ISCSI_DRIVER_DATA *Private\r
713 )\r
6a690e23 714{\r
715 ISCSI_SESSION *Session;\r
716 ISCSI_CONNECTION *Conn;\r
717 TCP4_IO *Tcp4Io;\r
718 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
719 EFI_STATUS Status;\r
720 EFI_DEV_PATH *DPathNode;\r
721\r
722 Session = &Private->Session;\r
723 if (Session->State != SESSION_STATE_LOGGED_IN) {\r
724 return NULL;\r
725 }\r
726\r
727 Conn = NET_LIST_USER_STRUCT_S (\r
728 Session->Conns.ForwardLink,\r
729 ISCSI_CONNECTION,\r
730 Link,\r
731 ISCSI_CONNECTION_SIGNATURE\r
732 );\r
733 Tcp4Io = &Conn->Tcp4Io;\r
734\r
735 Status = gBS->HandleProtocol (\r
736 Tcp4Io->Handle,\r
737 &gEfiDevicePathProtocolGuid,\r
69b0882d 738 (VOID **)&DevicePath\r
6a690e23 739 );\r
740 if (EFI_ERROR (Status)) {\r
741 return NULL;\r
742 }\r
743 //\r
744 // Duplicate it.\r
745 //\r
746 DevicePath = DuplicateDevicePath (DevicePath);\r
894d038a 747 if (DevicePath == NULL) {\r
748 return NULL;\r
749 }\r
6a690e23 750\r
751 DPathNode = (EFI_DEV_PATH *) DevicePath;\r
752\r
753 while (!IsDevicePathEnd (&DPathNode->DevPath)) {\r
754 if ((DevicePathType (&DPathNode->DevPath) == MESSAGING_DEVICE_PATH) &&\r
755 (DevicePathSubType (&DPathNode->DevPath) == MSG_IPv4_DP)\r
756 ) {\r
757\r
758 DPathNode->Ipv4.LocalPort = 0;\r
69b0882d 759 DPathNode->Ipv4.StaticIpAddress = (BOOLEAN) (!Session->ConfigData.NvData.InitiatorInfoFromDhcp);\r
6a690e23 760 break;\r
761 }\r
762\r
763 DPathNode = (EFI_DEV_PATH *) NextDevicePathNode (&DPathNode->DevPath);\r
764 }\r
765\r
766 return DevicePath;\r
767}\r
768\r
12618416 769/**\r
770 Abort the session when the transition from BS to RT is initiated.\r
771\r
1055b68d 772 @param[in] Event The event signaled.\r
773 @param[in] Context The iSCSI driver data.\r
12618416 774**/\r
6a690e23 775VOID\r
776EFIAPI\r
777IScsiOnExitBootService (\r
778 IN EFI_EVENT Event,\r
779 IN VOID *Context\r
780 )\r
6a690e23 781{\r
782 ISCSI_DRIVER_DATA *Private;\r
783\r
784 Private = (ISCSI_DRIVER_DATA *) Context;\r
785 gBS->CloseEvent (Private->ExitBootServiceEvent);\r
786\r
787 IScsiSessionAbort (&Private->Session);\r
788}\r