IScsiMisc.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607
  1. /** @file
  2. Miscellaneous routines for iSCSI driver.
  3. Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "IScsiImpl.h"
  7. GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 IScsiHexString[] = "0123456789ABCDEFabcdef";
  8. /**
  9. Removes (trims) specified leading and trailing characters from a string.
  10. @param[in, out] Str Pointer to the null-terminated string to be trimmed.
  11. On return, Str will hold the trimmed string.
  12. @param[in] CharC Character will be trimmed from str.
  13. **/
  14. VOID
  15. IScsiStrTrim (
  16. IN OUT CHAR16 *Str,
  17. IN CHAR16 CharC
  18. )
  19. {
  20. CHAR16 *Pointer1;
  21. CHAR16 *Pointer2;
  22. if (*Str == 0) {
  23. return;
  24. }
  25. //
  26. // Trim off the leading and trailing characters c
  27. //
  28. for (Pointer1 = Str; (*Pointer1 != 0) && (*Pointer1 == CharC); Pointer1++) {
  29. }
  30. Pointer2 = Str;
  31. if (Pointer2 == Pointer1) {
  32. while (*Pointer1 != 0) {
  33. Pointer2++;
  34. Pointer1++;
  35. }
  36. } else {
  37. while (*Pointer1 != 0) {
  38. *Pointer2 = *Pointer1;
  39. Pointer1++;
  40. Pointer2++;
  41. }
  42. *Pointer2 = 0;
  43. }
  44. for (Pointer1 = Str + StrLen (Str) - 1; Pointer1 >= Str && *Pointer1 == CharC; Pointer1--) {
  45. }
  46. if (Pointer1 != Str + StrLen (Str) - 1) {
  47. *(Pointer1 + 1) = 0;
  48. }
  49. }
  50. /**
  51. Calculate the prefix length of the IPv4 subnet mask.
  52. @param[in] SubnetMask The IPv4 subnet mask.
  53. @return The prefix length of the subnet mask.
  54. @retval 0 Other errors as indicated.
  55. **/
  56. UINT8
  57. IScsiGetSubnetMaskPrefixLength (
  58. IN EFI_IPv4_ADDRESS *SubnetMask
  59. )
  60. {
  61. UINT8 Len;
  62. UINT32 ReverseMask;
  63. //
  64. // The SubnetMask is in network byte order.
  65. //
  66. ReverseMask = (SubnetMask->Addr[0] << 24) | (SubnetMask->Addr[1] << 16) | (SubnetMask->Addr[2] << 8) | (SubnetMask->Addr[3]);
  67. //
  68. // Reverse it.
  69. //
  70. ReverseMask = ~ReverseMask;
  71. if ((ReverseMask & (ReverseMask + 1)) != 0) {
  72. return 0;
  73. }
  74. Len = 0;
  75. while (ReverseMask != 0) {
  76. ReverseMask = ReverseMask >> 1;
  77. Len++;
  78. }
  79. return (UINT8)(32 - Len);
  80. }
  81. /**
  82. Convert the hexadecimal encoded LUN string into the 64-bit LUN.
  83. @param[in] Str The hexadecimal encoded LUN string.
  84. @param[out] Lun Storage to return the 64-bit LUN.
  85. @retval EFI_SUCCESS The 64-bit LUN is stored in Lun.
  86. @retval EFI_INVALID_PARAMETER The string is malformatted.
  87. **/
  88. EFI_STATUS
  89. IScsiAsciiStrToLun (
  90. IN CHAR8 *Str,
  91. OUT UINT8 *Lun
  92. )
  93. {
  94. UINTN Index, IndexValue, IndexNum, SizeStr;
  95. CHAR8 TemStr[2];
  96. UINT8 TemValue;
  97. UINT16 Value[4];
  98. ZeroMem (Lun, 8);
  99. ZeroMem (TemStr, 2);
  100. ZeroMem ((UINT8 *)Value, sizeof (Value));
  101. SizeStr = AsciiStrLen (Str);
  102. IndexValue = 0;
  103. IndexNum = 0;
  104. for (Index = 0; Index < SizeStr; Index++) {
  105. TemStr[0] = Str[Index];
  106. TemValue = (UINT8)AsciiStrHexToUint64 (TemStr);
  107. if ((TemValue == 0) && (TemStr[0] != '0')) {
  108. if ((TemStr[0] != '-') || (IndexNum == 0)) {
  109. //
  110. // Invalid Lun Char.
  111. //
  112. return EFI_INVALID_PARAMETER;
  113. }
  114. }
  115. if ((TemValue == 0) && (TemStr[0] == '-')) {
  116. //
  117. // Next Lun value.
  118. //
  119. if (++IndexValue >= 4) {
  120. //
  121. // Max 4 Lun value.
  122. //
  123. return EFI_INVALID_PARAMETER;
  124. }
  125. //
  126. // Restart str index for the next lun value.
  127. //
  128. IndexNum = 0;
  129. continue;
  130. }
  131. if (++IndexNum > 4) {
  132. //
  133. // Each Lun Str can't exceed size 4, because it will be as UINT16 value.
  134. //
  135. return EFI_INVALID_PARAMETER;
  136. }
  137. //
  138. // Combine UINT16 value.
  139. //
  140. Value[IndexValue] = (UINT16)((Value[IndexValue] << 4) + TemValue);
  141. }
  142. for (Index = 0; Index <= IndexValue; Index++) {
  143. *((UINT16 *)&Lun[Index * 2]) = HTONS (Value[Index]);
  144. }
  145. return EFI_SUCCESS;
  146. }
  147. /**
  148. Convert the 64-bit LUN into the hexadecimal encoded LUN string.
  149. @param[in] Lun The 64-bit LUN.
  150. @param[out] Str The storage to return the hexadecimal encoded LUN string.
  151. **/
  152. VOID
  153. IScsiLunToUnicodeStr (
  154. IN UINT8 *Lun,
  155. OUT CHAR16 *Str
  156. )
  157. {
  158. UINTN Index;
  159. CHAR16 *TempStr;
  160. TempStr = Str;
  161. for (Index = 0; Index < 4; Index++) {
  162. if ((Lun[2 * Index] | Lun[2 * Index + 1]) == 0) {
  163. CopyMem (TempStr, L"0-", sizeof (L"0-"));
  164. } else {
  165. TempStr[0] = (CHAR16)IScsiHexString[Lun[2 * Index] >> 4];
  166. TempStr[1] = (CHAR16)IScsiHexString[Lun[2 * Index] & 0x0F];
  167. TempStr[2] = (CHAR16)IScsiHexString[Lun[2 * Index + 1] >> 4];
  168. TempStr[3] = (CHAR16)IScsiHexString[Lun[2 * Index + 1] & 0x0F];
  169. TempStr[4] = L'-';
  170. TempStr[5] = 0;
  171. IScsiStrTrim (TempStr, L'0');
  172. }
  173. TempStr += StrLen (TempStr);
  174. }
  175. //
  176. // Remove the last '-'
  177. //
  178. ASSERT (StrLen (Str) >= 1);
  179. Str[StrLen (Str) - 1] = 0;
  180. for (Index = StrLen (Str) - 1; Index > 1; Index = Index - 2) {
  181. if ((Str[Index] == L'0') && (Str[Index - 1] == L'-')) {
  182. Str[Index - 1] = 0;
  183. } else {
  184. break;
  185. }
  186. }
  187. }
  188. /**
  189. Convert the formatted IP address into the binary IP address.
  190. @param[in] Str The UNICODE string.
  191. @param[in] IpMode Indicates whether the IP address is v4 or v6.
  192. @param[out] Ip The storage to return the ASCII string.
  193. @retval EFI_SUCCESS The binary IP address is returned in Ip.
  194. @retval EFI_INVALID_PARAMETER The IP string is malformatted or IpMode is
  195. invalid.
  196. **/
  197. EFI_STATUS
  198. IScsiAsciiStrToIp (
  199. IN CHAR8 *Str,
  200. IN UINT8 IpMode,
  201. OUT EFI_IP_ADDRESS *Ip
  202. )
  203. {
  204. EFI_STATUS Status;
  205. if ((IpMode == IP_MODE_IP4) || (IpMode == IP_MODE_AUTOCONFIG_IP4)) {
  206. return NetLibAsciiStrToIp4 (Str, &Ip->v4);
  207. } else if ((IpMode == IP_MODE_IP6) || (IpMode == IP_MODE_AUTOCONFIG_IP6)) {
  208. return NetLibAsciiStrToIp6 (Str, &Ip->v6);
  209. } else if (IpMode == IP_MODE_AUTOCONFIG) {
  210. Status = NetLibAsciiStrToIp4 (Str, &Ip->v4);
  211. if (!EFI_ERROR (Status)) {
  212. return Status;
  213. }
  214. return NetLibAsciiStrToIp6 (Str, &Ip->v6);
  215. }
  216. return EFI_INVALID_PARAMETER;
  217. }
  218. /**
  219. Convert the mac address into a hexadecimal encoded "-" separated string.
  220. @param[in] Mac The mac address.
  221. @param[in] Len Length in bytes of the mac address.
  222. @param[in] VlanId VLAN ID of the network device.
  223. @param[out] Str The storage to return the mac string.
  224. **/
  225. VOID
  226. IScsiMacAddrToStr (
  227. IN EFI_MAC_ADDRESS *Mac,
  228. IN UINT32 Len,
  229. IN UINT16 VlanId,
  230. OUT CHAR16 *Str
  231. )
  232. {
  233. UINT32 Index;
  234. CHAR16 *String;
  235. for (Index = 0; Index < Len; Index++) {
  236. Str[3 * Index] = (CHAR16)IScsiHexString[(Mac->Addr[Index] >> 4) & 0x0F];
  237. Str[3 * Index + 1] = (CHAR16)IScsiHexString[Mac->Addr[Index] & 0x0F];
  238. Str[3 * Index + 2] = L':';
  239. }
  240. String = &Str[3 * Index - 1];
  241. if (VlanId != 0) {
  242. String += UnicodeSPrint (String, 6 * sizeof (CHAR16), L"\\%04x", (UINTN)VlanId);
  243. }
  244. *String = L'\0';
  245. }
  246. /**
  247. Convert the binary encoded buffer into a hexadecimal encoded string.
  248. @param[in] BinBuffer The buffer containing the binary data.
  249. @param[in] BinLength Length of the binary buffer.
  250. @param[in, out] HexStr Pointer to the string.
  251. @param[in, out] HexLength The length of the string.
  252. @retval EFI_SUCCESS The binary data is converted to the hexadecimal string
  253. and the length of the string is updated.
  254. @retval EFI_BUFFER_TOO_SMALL The string is too small.
  255. @retval EFI_BAD_BUFFER_SIZE BinLength is too large for hex encoding.
  256. @retval EFI_INVALID_PARAMETER The IP string is malformatted.
  257. **/
  258. EFI_STATUS
  259. IScsiBinToHex (
  260. IN UINT8 *BinBuffer,
  261. IN UINT32 BinLength,
  262. IN OUT CHAR8 *HexStr,
  263. IN OUT UINT32 *HexLength
  264. )
  265. {
  266. UINT32 HexLengthMin;
  267. UINT32 HexLengthProvided;
  268. UINT32 Index;
  269. if ((HexStr == NULL) || (BinBuffer == NULL) || (BinLength == 0)) {
  270. return EFI_INVALID_PARAMETER;
  271. }
  272. //
  273. // Safely calculate: HexLengthMin := BinLength * 2 + 3.
  274. //
  275. if (RETURN_ERROR (SafeUint32Mult (BinLength, 2, &HexLengthMin)) ||
  276. RETURN_ERROR (SafeUint32Add (HexLengthMin, 3, &HexLengthMin)))
  277. {
  278. return EFI_BAD_BUFFER_SIZE;
  279. }
  280. HexLengthProvided = *HexLength;
  281. *HexLength = HexLengthMin;
  282. if (HexLengthProvided < HexLengthMin) {
  283. return EFI_BUFFER_TOO_SMALL;
  284. }
  285. //
  286. // Prefix for Hex String.
  287. //
  288. HexStr[0] = '0';
  289. HexStr[1] = 'x';
  290. for (Index = 0; Index < BinLength; Index++) {
  291. HexStr[Index * 2 + 2] = IScsiHexString[BinBuffer[Index] >> 4];
  292. HexStr[Index * 2 + 3] = IScsiHexString[BinBuffer[Index] & 0xf];
  293. }
  294. HexStr[Index * 2 + 2] = '\0';
  295. return EFI_SUCCESS;
  296. }
  297. /**
  298. Convert the hexadecimal string into a binary encoded buffer.
  299. @param[in, out] BinBuffer The binary buffer.
  300. @param[in, out] BinLength Length of the binary buffer.
  301. @param[in] HexStr The hexadecimal string.
  302. @retval EFI_SUCCESS The hexadecimal string is converted into a
  303. binary encoded buffer.
  304. @retval EFI_INVALID_PARAMETER Invalid hex encoding found in HexStr.
  305. @retval EFI_BAD_BUFFER_SIZE The length of HexStr is too large for decoding:
  306. the decoded size cannot be expressed in
  307. BinLength on output.
  308. @retval EFI_BUFFER_TOO_SMALL The binary buffer is too small to hold the
  309. converted data.
  310. **/
  311. EFI_STATUS
  312. IScsiHexToBin (
  313. IN OUT UINT8 *BinBuffer,
  314. IN OUT UINT32 *BinLength,
  315. IN CHAR8 *HexStr
  316. )
  317. {
  318. UINTN BinLengthMin;
  319. UINT32 BinLengthProvided;
  320. UINTN Index;
  321. UINTN Length;
  322. UINT8 Digit;
  323. CHAR8 TemStr[2];
  324. ZeroMem (TemStr, sizeof (TemStr));
  325. //
  326. // Find out how many hex characters the string has.
  327. //
  328. if ((HexStr[0] == '0') && ((HexStr[1] == 'x') || (HexStr[1] == 'X'))) {
  329. HexStr += 2;
  330. }
  331. Length = AsciiStrLen (HexStr);
  332. //
  333. // Reject an empty hex string; reject a stray nibble.
  334. //
  335. if ((Length == 0) || (Length % 2 != 0)) {
  336. return EFI_INVALID_PARAMETER;
  337. }
  338. //
  339. // Check if the caller provides enough room for the decoded blob.
  340. //
  341. BinLengthMin = Length / 2;
  342. if (BinLengthMin > MAX_UINT32) {
  343. return EFI_BAD_BUFFER_SIZE;
  344. }
  345. BinLengthProvided = *BinLength;
  346. *BinLength = (UINT32)BinLengthMin;
  347. if (BinLengthProvided < BinLengthMin) {
  348. return EFI_BUFFER_TOO_SMALL;
  349. }
  350. for (Index = 0; Index < Length; Index++) {
  351. TemStr[0] = HexStr[Index];
  352. Digit = (UINT8)AsciiStrHexToUint64 (TemStr);
  353. if ((Digit == 0) && (TemStr[0] != '0')) {
  354. //
  355. // Invalid Hex Char.
  356. //
  357. return EFI_INVALID_PARAMETER;
  358. }
  359. if ((Index & 1) == 0) {
  360. BinBuffer[Index/2] = Digit;
  361. } else {
  362. BinBuffer[Index/2] = (UINT8)((BinBuffer[Index/2] << 4) + Digit);
  363. }
  364. }
  365. return EFI_SUCCESS;
  366. }
  367. /**
  368. Convert the decimal-constant string or hex-constant string into a numerical value.
  369. @param[in] Str String in decimal or hex.
  370. @return The numerical value.
  371. **/
  372. UINTN
  373. IScsiNetNtoi (
  374. IN CHAR8 *Str
  375. )
  376. {
  377. if ((Str[0] == '0') && ((Str[1] == 'x') || (Str[1] == 'X'))) {
  378. Str += 2;
  379. return AsciiStrHexToUintn (Str);
  380. }
  381. return AsciiStrDecimalToUintn (Str);
  382. }
  383. /**
  384. Generate random numbers.
  385. @param[in, out] Rand The buffer to contain random numbers.
  386. @param[in] RandLength The length of the Rand buffer.
  387. **/
  388. VOID
  389. IScsiGenRandom (
  390. IN OUT UINT8 *Rand,
  391. IN UINTN RandLength
  392. )
  393. {
  394. UINT32 Random;
  395. while (RandLength > 0) {
  396. Random = NET_RANDOM (NetRandomInitSeed ());
  397. *Rand++ = (UINT8)(Random);
  398. RandLength--;
  399. }
  400. }
  401. /**
  402. Check whether UNDI protocol supports IPv6.
  403. @param[in] ControllerHandle Controller handle.
  404. @param[in] Image Handle of the image.
  405. @param[out] Ipv6Support TRUE if UNDI supports IPv6.
  406. @retval EFI_SUCCESS Get the result whether UNDI supports IPv6 by NII or AIP protocol successfully.
  407. @retval EFI_NOT_FOUND Don't know whether UNDI supports IPv6 since NII or AIP is not available.
  408. **/
  409. EFI_STATUS
  410. IScsiCheckIpv6Support (
  411. IN EFI_HANDLE ControllerHandle,
  412. IN EFI_HANDLE Image,
  413. OUT BOOLEAN *Ipv6Support
  414. )
  415. {
  416. EFI_HANDLE Handle;
  417. EFI_ADAPTER_INFORMATION_PROTOCOL *Aip;
  418. EFI_STATUS Status;
  419. EFI_GUID *InfoTypesBuffer;
  420. UINTN InfoTypeBufferCount;
  421. UINTN TypeIndex;
  422. BOOLEAN Supported;
  423. VOID *InfoBlock;
  424. UINTN InfoBlockSize;
  425. EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL *Nii;
  426. ASSERT (Ipv6Support != NULL);
  427. //
  428. // Check whether the UNDI supports IPv6 by NII protocol.
  429. //
  430. Status = gBS->OpenProtocol (
  431. ControllerHandle,
  432. &gEfiNetworkInterfaceIdentifierProtocolGuid_31,
  433. (VOID **)&Nii,
  434. Image,
  435. ControllerHandle,
  436. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  437. );
  438. if (Status == EFI_SUCCESS) {
  439. *Ipv6Support = Nii->Ipv6Supported;
  440. return EFI_SUCCESS;
  441. }
  442. //
  443. // Get the NIC handle by SNP protocol.
  444. //
  445. Handle = NetLibGetSnpHandle (ControllerHandle, NULL);
  446. if (Handle == NULL) {
  447. return EFI_NOT_FOUND;
  448. }
  449. Aip = NULL;
  450. Status = gBS->HandleProtocol (
  451. Handle,
  452. &gEfiAdapterInformationProtocolGuid,
  453. (VOID *)&Aip
  454. );
  455. if (EFI_ERROR (Status) || (Aip == NULL)) {
  456. return EFI_NOT_FOUND;
  457. }
  458. InfoTypesBuffer = NULL;
  459. InfoTypeBufferCount = 0;
  460. Status = Aip->GetSupportedTypes (Aip, &InfoTypesBuffer, &InfoTypeBufferCount);
  461. if (EFI_ERROR (Status) || (InfoTypesBuffer == NULL)) {
  462. FreePool (InfoTypesBuffer);
  463. return EFI_NOT_FOUND;
  464. }
  465. Supported = FALSE;
  466. for (TypeIndex = 0; TypeIndex < InfoTypeBufferCount; TypeIndex++) {
  467. if (CompareGuid (&InfoTypesBuffer[TypeIndex], &gEfiAdapterInfoUndiIpv6SupportGuid)) {
  468. Supported = TRUE;
  469. break;
  470. }
  471. }
  472. FreePool (InfoTypesBuffer);
  473. if (!Supported) {
  474. return EFI_NOT_FOUND;
  475. }
  476. //
  477. // We now have adapter information block.
  478. //
  479. InfoBlock = NULL;
  480. InfoBlockSize = 0;
  481. Status = Aip->GetInformation (Aip, &gEfiAdapterInfoUndiIpv6SupportGuid, &InfoBlock, &InfoBlockSize);
  482. if (EFI_ERROR (Status) || (InfoBlock == NULL)) {
  483. FreePool (InfoBlock);
  484. return EFI_NOT_FOUND;
  485. }
  486. *Ipv6Support = ((EFI_ADAPTER_INFO_UNDI_IPV6_SUPPORT *)InfoBlock)->Ipv6Support;
  487. FreePool (InfoBlock);
  488. return EFI_SUCCESS;
  489. }
  490. /**
  491. Record the NIC info in global structure.
  492. @param[in] Controller The handle of the controller.
  493. @param[in] Image Handle of the image.
  494. @retval EFI_SUCCESS The operation is completed.
  495. @retval EFI_OUT_OF_RESOURCES Do not have sufficient resources to finish this
  496. operation.
  497. **/
  498. EFI_STATUS
  499. IScsiAddNic (
  500. IN EFI_HANDLE Controller,
  501. IN EFI_HANDLE Image
  502. )
  503. {
  504. EFI_STATUS Status;
  505. ISCSI_NIC_INFO *NicInfo;
  506. LIST_ENTRY *Entry;
  507. EFI_MAC_ADDRESS MacAddr;
  508. UINTN HwAddressSize;
  509. UINT16 VlanId;
  510. //
  511. // Get MAC address of this network device.
  512. //
  513. Status = NetLibGetMacAddress (Controller, &MacAddr, &HwAddressSize);
  514. if (EFI_ERROR (Status)) {
  515. return Status;
  516. }
  517. //
  518. // Get VLAN ID of this network device.
  519. //
  520. VlanId = NetLibGetVlanId (Controller);
  521. //
  522. // Check whether the NIC info already exists. Return directly if so.
  523. //
  524. NET_LIST_FOR_EACH (Entry, &mPrivate->NicInfoList) {
  525. NicInfo = NET_LIST_USER_STRUCT (Entry, ISCSI_NIC_INFO, Link);
  526. if ((NicInfo->HwAddressSize == HwAddressSize) &&
  527. (CompareMem (&NicInfo->PermanentAddress, MacAddr.Addr, HwAddressSize) == 0) &&
  528. (NicInfo->VlanId == VlanId))
  529. {
  530. mPrivate->CurrentNic = NicInfo->NicIndex;
  531. //
  532. // Set IPv6 available flag.
  533. //
  534. Status = IScsiCheckIpv6Support (Controller, Image, &NicInfo->Ipv6Available);
  535. if (EFI_ERROR (Status)) {
  536. //
  537. // Fail to get the data whether UNDI supports IPv6.
  538. // Set default value to TRUE.
  539. //
  540. NicInfo->Ipv6Available = TRUE;
  541. }
  542. return EFI_SUCCESS;
  543. }
  544. if (mPrivate->MaxNic < NicInfo->NicIndex) {
  545. mPrivate->MaxNic = NicInfo->NicIndex;
  546. }
  547. }
  548. //
  549. // Record the NIC info in private structure.
  550. //
  551. NicInfo = AllocateZeroPool (sizeof (ISCSI_NIC_INFO));
  552. if (NicInfo == NULL) {
  553. return EFI_OUT_OF_RESOURCES;
  554. }
  555. CopyMem (&NicInfo->PermanentAddress, MacAddr.Addr, HwAddressSize);
  556. NicInfo->HwAddressSize = (UINT32)HwAddressSize;
  557. NicInfo->VlanId = VlanId;
  558. NicInfo->NicIndex = (UINT8)(mPrivate->MaxNic + 1);
  559. mPrivate->MaxNic = NicInfo->NicIndex;
  560. //
  561. // Set IPv6 available flag.
  562. //
  563. Status = IScsiCheckIpv6Support (Controller, Image, &NicInfo->Ipv6Available);
  564. if (EFI_ERROR (Status)) {
  565. //
  566. // Fail to get the data whether UNDI supports IPv6.
  567. // Set default value to TRUE.
  568. //
  569. NicInfo->Ipv6Available = TRUE;
  570. }
  571. //
  572. // Get the PCI location.
  573. //
  574. IScsiGetNICPciLocation (
  575. Controller,
  576. &NicInfo->BusNumber,
  577. &NicInfo->DeviceNumber,
  578. &NicInfo->FunctionNumber
  579. );
  580. InsertTailList (&mPrivate->NicInfoList, &NicInfo->Link);
  581. mPrivate->NicCount++;
  582. mPrivate->CurrentNic = NicInfo->NicIndex;
  583. return EFI_SUCCESS;
  584. }
  585. /**
  586. Delete the recorded NIC info from global structure. Also delete corresponding
  587. attempts.
  588. @param[in] Controller The handle of the controller.
  589. @retval EFI_SUCCESS The operation is completed.
  590. @retval EFI_NOT_FOUND The NIC info to be deleted is not recorded.
  591. **/
  592. EFI_STATUS
  593. IScsiRemoveNic (
  594. IN EFI_HANDLE Controller
  595. )
  596. {
  597. EFI_STATUS Status;
  598. ISCSI_NIC_INFO *NicInfo;
  599. LIST_ENTRY *Entry;
  600. LIST_ENTRY *NextEntry;
  601. ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData;
  602. ISCSI_NIC_INFO *ThisNic;
  603. EFI_MAC_ADDRESS MacAddr;
  604. UINTN HwAddressSize;
  605. UINT16 VlanId;
  606. //
  607. // Get MAC address of this network device.
  608. //
  609. Status = NetLibGetMacAddress (Controller, &MacAddr, &HwAddressSize);
  610. if (EFI_ERROR (Status)) {
  611. return Status;
  612. }
  613. //
  614. // Get VLAN ID of this network device.
  615. //
  616. VlanId = NetLibGetVlanId (Controller);
  617. //
  618. // Check whether the NIC information exists.
  619. //
  620. ThisNic = NULL;
  621. NET_LIST_FOR_EACH (Entry, &mPrivate->NicInfoList) {
  622. NicInfo = NET_LIST_USER_STRUCT (Entry, ISCSI_NIC_INFO, Link);
  623. if ((NicInfo->HwAddressSize == HwAddressSize) &&
  624. (CompareMem (&NicInfo->PermanentAddress, MacAddr.Addr, HwAddressSize) == 0) &&
  625. (NicInfo->VlanId == VlanId))
  626. {
  627. ThisNic = NicInfo;
  628. break;
  629. }
  630. }
  631. if (ThisNic == NULL) {
  632. return EFI_NOT_FOUND;
  633. }
  634. mPrivate->CurrentNic = ThisNic->NicIndex;
  635. RemoveEntryList (&ThisNic->Link);
  636. FreePool (ThisNic);
  637. mPrivate->NicCount--;
  638. //
  639. // Remove all attempts related to this NIC.
  640. //
  641. NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &mPrivate->AttemptConfigs) {
  642. AttemptConfigData = NET_LIST_USER_STRUCT (Entry, ISCSI_ATTEMPT_CONFIG_NVDATA, Link);
  643. if (AttemptConfigData->NicIndex == mPrivate->CurrentNic) {
  644. RemoveEntryList (&AttemptConfigData->Link);
  645. mPrivate->AttemptCount--;
  646. if ((AttemptConfigData->SessionConfigData.Enabled == ISCSI_ENABLED_FOR_MPIO) && (mPrivate->MpioCount > 0)) {
  647. if (--mPrivate->MpioCount == 0) {
  648. mPrivate->EnableMpio = FALSE;
  649. }
  650. if ((AttemptConfigData->AuthenticationType == ISCSI_AUTH_TYPE_KRB) && (mPrivate->Krb5MpioCount > 0)) {
  651. mPrivate->Krb5MpioCount--;
  652. }
  653. } else if ((AttemptConfigData->SessionConfigData.Enabled == ISCSI_ENABLED) && (mPrivate->SinglePathCount > 0)) {
  654. mPrivate->SinglePathCount--;
  655. if (mPrivate->ValidSinglePathCount > 0) {
  656. mPrivate->ValidSinglePathCount--;
  657. }
  658. }
  659. FreePool (AttemptConfigData);
  660. }
  661. }
  662. return EFI_SUCCESS;
  663. }
  664. /**
  665. Create and initialize the Attempts.
  666. @param[in] AttemptNum The number of Attempts will be created.
  667. @retval EFI_SUCCESS The Attempts have been created successfully.
  668. @retval Others Failed to create the Attempt.
  669. **/
  670. EFI_STATUS
  671. IScsiCreateAttempts (
  672. IN UINTN AttemptNum
  673. )
  674. {
  675. ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData;
  676. ISCSI_SESSION_CONFIG_NVDATA *ConfigData;
  677. UINT8 *AttemptConfigOrder;
  678. UINTN AttemptConfigOrderSize;
  679. UINT8 *AttemptOrderTmp;
  680. UINTN TotalNumber;
  681. UINT8 Index;
  682. EFI_STATUS Status;
  683. for (Index = 1; Index <= AttemptNum; Index++) {
  684. //
  685. // Get the initialized attempt order. This is used to essure creating attempts by order.
  686. //
  687. AttemptConfigOrder = IScsiGetVariableAndSize (
  688. L"InitialAttemptOrder",
  689. &gIScsiConfigGuid,
  690. &AttemptConfigOrderSize
  691. );
  692. TotalNumber = AttemptConfigOrderSize / sizeof (UINT8);
  693. if (TotalNumber == AttemptNum) {
  694. Status = EFI_SUCCESS;
  695. break;
  696. }
  697. TotalNumber++;
  698. //
  699. // Append the new created attempt to the end.
  700. //
  701. AttemptOrderTmp = AllocateZeroPool (TotalNumber * sizeof (UINT8));
  702. if (AttemptOrderTmp == NULL) {
  703. if (AttemptConfigOrder != NULL) {
  704. FreePool (AttemptConfigOrder);
  705. }
  706. return EFI_OUT_OF_RESOURCES;
  707. }
  708. if (AttemptConfigOrder != NULL) {
  709. CopyMem (AttemptOrderTmp, AttemptConfigOrder, AttemptConfigOrderSize);
  710. FreePool (AttemptConfigOrder);
  711. }
  712. AttemptOrderTmp[TotalNumber - 1] = Index;
  713. AttemptConfigOrder = AttemptOrderTmp;
  714. AttemptConfigOrderSize = TotalNumber * sizeof (UINT8);
  715. Status = gRT->SetVariable (
  716. L"InitialAttemptOrder",
  717. &gIScsiConfigGuid,
  718. EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,
  719. AttemptConfigOrderSize,
  720. AttemptConfigOrder
  721. );
  722. FreePool (AttemptConfigOrder);
  723. if (EFI_ERROR (Status)) {
  724. DEBUG ((
  725. DEBUG_ERROR,
  726. "%a: Failed to set 'InitialAttemptOrder' with Guid (%g): "
  727. "%r\n",
  728. __FUNCTION__,
  729. &gIScsiConfigGuid,
  730. Status
  731. ));
  732. return Status;
  733. }
  734. //
  735. // Create new Attempt
  736. //
  737. AttemptConfigData = AllocateZeroPool (sizeof (ISCSI_ATTEMPT_CONFIG_NVDATA));
  738. if (AttemptConfigData == NULL) {
  739. return EFI_OUT_OF_RESOURCES;
  740. }
  741. ConfigData = &AttemptConfigData->SessionConfigData;
  742. ConfigData->TargetPort = ISCSI_WELL_KNOWN_PORT;
  743. ConfigData->ConnectTimeout = CONNECT_DEFAULT_TIMEOUT;
  744. ConfigData->ConnectRetryCount = CONNECT_MIN_RETRY;
  745. AttemptConfigData->AuthenticationType = ISCSI_AUTH_TYPE_CHAP;
  746. AttemptConfigData->AuthConfigData.CHAP.CHAPType = ISCSI_CHAP_UNI;
  747. //
  748. // Configure the Attempt index and set variable.
  749. //
  750. AttemptConfigData->AttemptConfigIndex = Index;
  751. //
  752. // Set the attempt name according to the order.
  753. //
  754. UnicodeSPrint (
  755. mPrivate->PortString,
  756. (UINTN)ISCSI_NAME_IFR_MAX_SIZE,
  757. L"Attempt %d",
  758. (UINTN)AttemptConfigData->AttemptConfigIndex
  759. );
  760. UnicodeStrToAsciiStrS (mPrivate->PortString, AttemptConfigData->AttemptName, ATTEMPT_NAME_SIZE);
  761. Status = gRT->SetVariable (
  762. mPrivate->PortString,
  763. &gEfiIScsiInitiatorNameProtocolGuid,
  764. ISCSI_CONFIG_VAR_ATTR,
  765. sizeof (ISCSI_ATTEMPT_CONFIG_NVDATA),
  766. AttemptConfigData
  767. );
  768. FreePool (AttemptConfigData);
  769. if (EFI_ERROR (Status)) {
  770. DEBUG ((
  771. DEBUG_ERROR,
  772. "%a: Failed to set variable (mPrivate->PortString) with Guid (%g): "
  773. "%r\n",
  774. __FUNCTION__,
  775. &gEfiIScsiInitiatorNameProtocolGuid,
  776. Status
  777. ));
  778. return Status;
  779. }
  780. }
  781. return EFI_SUCCESS;
  782. }
  783. /**
  784. Create the iSCSI configuration Keywords for each attempt. You can find the keywords
  785. defined in the "x-UEFI-ns" namespace (http://www.uefi.org/confignamespace).
  786. @param[in] KeywordNum The number Sets of Keywords will be created.
  787. @retval EFI_SUCCESS The operation is completed.
  788. @retval Others Failed to create the Keywords.
  789. **/
  790. EFI_STATUS
  791. IScsiCreateKeywords (
  792. IN UINTN KeywordNum
  793. )
  794. {
  795. VOID *StartOpCodeHandle;
  796. EFI_IFR_GUID_LABEL *StartLabel;
  797. VOID *EndOpCodeHandle;
  798. EFI_IFR_GUID_LABEL *EndLabel;
  799. UINTN Index;
  800. EFI_STRING_ID StringToken;
  801. CHAR16 StringId[64];
  802. CHAR16 KeywordId[32];
  803. EFI_STATUS Status;
  804. Status = IScsiCreateOpCode (
  805. KEYWORD_ENTRY_LABEL,
  806. &StartOpCodeHandle,
  807. &StartLabel,
  808. &EndOpCodeHandle,
  809. &EndLabel
  810. );
  811. if (EFI_ERROR (Status)) {
  812. return EFI_OUT_OF_RESOURCES;
  813. }
  814. for (Index = 1; Index <= KeywordNum; Index++) {
  815. //
  816. // Create iSCSIAttemptName Keyword.
  817. //
  818. UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_ATTEMPTT_NAME_PROMPT%d", Index);
  819. StringToken = HiiSetString (
  820. mCallbackInfo->RegisteredHandle,
  821. 0,
  822. StringId,
  823. NULL
  824. );
  825. UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIAttemptName:%d", Index);
  826. HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns");
  827. HiiCreateStringOpCode (
  828. StartOpCodeHandle,
  829. (EFI_QUESTION_ID)(ATTEMPT_ATTEMPT_NAME_QUESTION_ID + (Index - 1)),
  830. CONFIGURATION_VARSTORE_ID,
  831. (UINT16)(ATTEMPT_ATTEMPT_NAME_VAR_OFFSET + ATTEMPT_NAME_SIZE * (Index - 1) * sizeof (CHAR16)),
  832. StringToken,
  833. StringToken,
  834. EFI_IFR_FLAG_READ_ONLY,
  835. 0,
  836. 0,
  837. ATTEMPT_NAME_SIZE,
  838. NULL
  839. );
  840. //
  841. // Create iSCSIBootEnable Keyword.
  842. //
  843. UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_MODE_PROMPT%d", Index);
  844. StringToken = HiiSetString (
  845. mCallbackInfo->RegisteredHandle,
  846. 0,
  847. StringId,
  848. NULL
  849. );
  850. UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIBootEnable:%d", Index);
  851. HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns");
  852. HiiCreateNumericOpCode (
  853. StartOpCodeHandle,
  854. (EFI_QUESTION_ID)(ATTEMPT_BOOTENABLE_QUESTION_ID + (Index - 1)),
  855. CONFIGURATION_VARSTORE_ID,
  856. (UINT16)(ATTEMPT_BOOTENABLE_VAR_OFFSET + (Index - 1)),
  857. StringToken,
  858. StringToken,
  859. 0,
  860. EFI_IFR_NUMERIC_SIZE_1,
  861. 0,
  862. 2,
  863. 0,
  864. NULL
  865. );
  866. //
  867. // Create iSCSIIpAddressType Keyword.
  868. //
  869. UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_IP_MODE_PROMPT%d", Index);
  870. StringToken = HiiSetString (
  871. mCallbackInfo->RegisteredHandle,
  872. 0,
  873. StringId,
  874. NULL
  875. );
  876. UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIIpAddressType:%d", Index);
  877. HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns");
  878. HiiCreateNumericOpCode (
  879. StartOpCodeHandle,
  880. (EFI_QUESTION_ID)(ATTEMPT_ADDRESS_TYPE_QUESTION_ID + (Index - 1)),
  881. CONFIGURATION_VARSTORE_ID,
  882. (UINT16)(ATTEMPT_ADDRESS_TYPE_VAR_OFFSET + (Index - 1)),
  883. StringToken,
  884. StringToken,
  885. 0,
  886. EFI_IFR_NUMERIC_SIZE_1,
  887. 0,
  888. 2,
  889. 0,
  890. NULL
  891. );
  892. //
  893. // Create iSCSIConnectRetry Keyword.
  894. //
  895. UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_CONNECT_RETRY_PROMPT%d", Index);
  896. StringToken = HiiSetString (
  897. mCallbackInfo->RegisteredHandle,
  898. 0,
  899. StringId,
  900. NULL
  901. );
  902. UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIConnectRetry:%d", Index);
  903. HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns");
  904. HiiCreateNumericOpCode (
  905. StartOpCodeHandle,
  906. (EFI_QUESTION_ID)(ATTEMPT_CONNECT_RETRY_QUESTION_ID + (Index - 1)),
  907. CONFIGURATION_VARSTORE_ID,
  908. (UINT16)(ATTEMPT_CONNECT_RETRY_VAR_OFFSET + (Index - 1)),
  909. StringToken,
  910. StringToken,
  911. 0,
  912. EFI_IFR_NUMERIC_SIZE_1,
  913. 0,
  914. 16,
  915. 0,
  916. NULL
  917. );
  918. //
  919. // Create iSCSIConnectTimeout Keyword.
  920. //
  921. UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_CONNECT_TIMEOUT_PROMPT%d", Index);
  922. StringToken = HiiSetString (
  923. mCallbackInfo->RegisteredHandle,
  924. 0,
  925. StringId,
  926. NULL
  927. );
  928. UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIConnectTimeout:%d", Index);
  929. HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns");
  930. HiiCreateNumericOpCode (
  931. StartOpCodeHandle,
  932. (EFI_QUESTION_ID)(ATTEMPT_CONNECT_TIMEOUT_QUESTION_ID + (Index - 1)),
  933. CONFIGURATION_VARSTORE_ID,
  934. (UINT16)(ATTEMPT_CONNECT_TIMEOUT_VAR_OFFSET + 2 * (Index - 1)),
  935. StringToken,
  936. StringToken,
  937. 0,
  938. EFI_IFR_NUMERIC_SIZE_2,
  939. CONNECT_MIN_TIMEOUT,
  940. CONNECT_MAX_TIMEOUT,
  941. 0,
  942. NULL
  943. );
  944. //
  945. // Create ISID Keyword.
  946. //
  947. UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_ISID_PROMPT%d", Index);
  948. StringToken = HiiSetString (
  949. mCallbackInfo->RegisteredHandle,
  950. 0,
  951. StringId,
  952. NULL
  953. );
  954. UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIISID:%d", Index);
  955. HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns");
  956. HiiCreateStringOpCode (
  957. StartOpCodeHandle,
  958. (EFI_QUESTION_ID)(ATTEMPT_ISID_QUESTION_ID + (Index - 1)),
  959. CONFIGURATION_VARSTORE_ID,
  960. (UINT16)(ATTEMPT_ISID_VAR_OFFSET + sizeof (KEYWORD_STR) * (Index - 1)),
  961. StringToken,
  962. STRING_TOKEN (STR_ISCSI_ISID_HELP),
  963. 0,
  964. 0,
  965. ISID_CONFIGURABLE_MIN_LEN,
  966. ISID_CONFIGURABLE_STORAGE,
  967. NULL
  968. );
  969. //
  970. // Create iSCSIInitiatorInfoViaDHCP Keyword.
  971. //
  972. UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_INITIATOR_VIA_DHCP_PROMPT%d", Index);
  973. StringToken = HiiSetString (
  974. mCallbackInfo->RegisteredHandle,
  975. 0,
  976. StringId,
  977. NULL
  978. );
  979. UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIInitiatorInfoViaDHCP:%d", Index);
  980. HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns");
  981. HiiCreateNumericOpCode (
  982. StartOpCodeHandle,
  983. (EFI_QUESTION_ID)(ATTEMPT_INITIATOR_VIA_DHCP_QUESTION_ID + (Index - 1)),
  984. CONFIGURATION_VARSTORE_ID,
  985. (UINT16)(ATTEMPT_INITIATOR_VIA_DHCP_VAR_OFFSET + (Index - 1)),
  986. StringToken,
  987. StringToken,
  988. 0,
  989. 0,
  990. 0,
  991. 1,
  992. 0,
  993. NULL
  994. );
  995. //
  996. // Create iSCSIInitiatorIpAddress Keyword.
  997. //
  998. UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_INITIATOR_IP_ADDRESS_PROMPT%d", Index);
  999. StringToken = HiiSetString (
  1000. mCallbackInfo->RegisteredHandle,
  1001. 0,
  1002. StringId,
  1003. NULL
  1004. );
  1005. UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIInitiatorIpAddress:%d", Index);
  1006. HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns");
  1007. HiiCreateStringOpCode (
  1008. StartOpCodeHandle,
  1009. (EFI_QUESTION_ID)(ATTEMPT_INITIATOR_IP_ADDRESS_QUESTION_ID + (Index - 1)),
  1010. CONFIGURATION_VARSTORE_ID,
  1011. (UINT16)(ATTEMPT_INITIATOR_IP_ADDRESS_VAR_OFFSET + sizeof (KEYWORD_STR) * (Index - 1)),
  1012. StringToken,
  1013. StringToken,
  1014. 0,
  1015. 0,
  1016. IP4_MIN_SIZE,
  1017. IP4_STR_MAX_SIZE,
  1018. NULL
  1019. );
  1020. //
  1021. // Create iSCSIInitiatorNetmask Keyword.
  1022. //
  1023. UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_INITIATOR_NET_MASK_PROMPT%d", Index);
  1024. StringToken = HiiSetString (
  1025. mCallbackInfo->RegisteredHandle,
  1026. 0,
  1027. StringId,
  1028. NULL
  1029. );
  1030. UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIInitiatorNetmask:%d", Index);
  1031. HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns");
  1032. HiiCreateStringOpCode (
  1033. StartOpCodeHandle,
  1034. (EFI_QUESTION_ID)(ATTEMPT_INITIATOR_NET_MASK_QUESTION_ID + (Index - 1)),
  1035. CONFIGURATION_VARSTORE_ID,
  1036. (UINT16)(ATTEMPT_INITIATOR_NET_MASK_VAR_OFFSET + sizeof (KEYWORD_STR) * (Index - 1)),
  1037. StringToken,
  1038. StringToken,
  1039. 0,
  1040. 0,
  1041. IP4_MIN_SIZE,
  1042. IP4_STR_MAX_SIZE,
  1043. NULL
  1044. );
  1045. //
  1046. // Create iSCSIInitiatorGateway Keyword.
  1047. //
  1048. UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_INITIATOR_GATE_PROMPT%d", Index);
  1049. StringToken = HiiSetString (
  1050. mCallbackInfo->RegisteredHandle,
  1051. 0,
  1052. StringId,
  1053. NULL
  1054. );
  1055. UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIInitiatorGateway:%d", Index);
  1056. HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns");
  1057. HiiCreateStringOpCode (
  1058. StartOpCodeHandle,
  1059. (EFI_QUESTION_ID)(ATTEMPT_INITIATOR_GATE_WAY_QUESTION_ID + (Index - 1)),
  1060. CONFIGURATION_VARSTORE_ID,
  1061. (UINT16)(ATTEMPT_INITIATOR_GATE_WAY_VAR_OFFSET + sizeof (KEYWORD_STR) * (Index - 1)),
  1062. StringToken,
  1063. StringToken,
  1064. 0,
  1065. 0,
  1066. IP4_MIN_SIZE,
  1067. IP4_STR_MAX_SIZE,
  1068. NULL
  1069. );
  1070. //
  1071. // Create iSCSITargetInfoViaDHCP Keyword.
  1072. //
  1073. UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_TARGET_VIA_DHCP_PROMPT%d", Index);
  1074. StringToken = HiiSetString (
  1075. mCallbackInfo->RegisteredHandle,
  1076. 0,
  1077. StringId,
  1078. NULL
  1079. );
  1080. UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSITargetInfoViaDHCP:%d", Index);
  1081. HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns");
  1082. HiiCreateNumericOpCode (
  1083. StartOpCodeHandle,
  1084. (EFI_QUESTION_ID)(ATTEMPT_TARGET_VIA_DHCP_QUESTION_ID + (Index - 1)),
  1085. CONFIGURATION_VARSTORE_ID,
  1086. (UINT16)(ATTEMPT_TARGET_VIA_DHCP_VAR_OFFSET + (Index - 1)),
  1087. StringToken,
  1088. StringToken,
  1089. 0,
  1090. 0,
  1091. 0,
  1092. 1,
  1093. 0,
  1094. NULL
  1095. );
  1096. //
  1097. // Create iSCSITargetTcpPort Keyword.
  1098. //
  1099. UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_TARGET_TCP_PORT_PROMPT%d", Index);
  1100. StringToken = HiiSetString (
  1101. mCallbackInfo->RegisteredHandle,
  1102. 0,
  1103. StringId,
  1104. NULL
  1105. );
  1106. UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSITargetTcpPort:%d", Index);
  1107. HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns");
  1108. HiiCreateNumericOpCode (
  1109. StartOpCodeHandle,
  1110. (EFI_QUESTION_ID)(ATTEMPT_TARGET_TCP_PORT_QUESTION_ID + (Index - 1)),
  1111. CONFIGURATION_VARSTORE_ID,
  1112. (UINT16)(ATTEMPT_TARGET_TCP_PORT_VAR_OFFSET + 2 * (Index - 1)),
  1113. StringToken,
  1114. StringToken,
  1115. 0,
  1116. EFI_IFR_NUMERIC_SIZE_2,
  1117. TARGET_PORT_MIN_NUM,
  1118. TARGET_PORT_MAX_NUM,
  1119. 0,
  1120. NULL
  1121. );
  1122. //
  1123. // Create iSCSITargetName Keyword.
  1124. //
  1125. UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_TARGET_NAME_PROMPT%d", Index);
  1126. StringToken = HiiSetString (
  1127. mCallbackInfo->RegisteredHandle,
  1128. 0,
  1129. StringId,
  1130. NULL
  1131. );
  1132. UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSITargetName:%d", Index);
  1133. HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns");
  1134. HiiCreateStringOpCode (
  1135. StartOpCodeHandle,
  1136. (EFI_QUESTION_ID)(ATTEMPT_TARGET_NAME_QUESTION_ID + (Index - 1)),
  1137. CONFIGURATION_VARSTORE_ID,
  1138. (UINT16)(ATTEMPT_TARGET_NAME_VAR_OFFSET + sizeof (KEYWORD_STR) * (Index - 1)),
  1139. StringToken,
  1140. StringToken,
  1141. 0,
  1142. 0,
  1143. ISCSI_NAME_IFR_MIN_SIZE,
  1144. ISCSI_NAME_IFR_MAX_SIZE,
  1145. NULL
  1146. );
  1147. //
  1148. // Create iSCSITargetIpAddress Keyword.
  1149. //
  1150. UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_TARGET_IP_ADDRESS_PROMPT%d", Index);
  1151. StringToken = HiiSetString (
  1152. mCallbackInfo->RegisteredHandle,
  1153. 0,
  1154. StringId,
  1155. NULL
  1156. );
  1157. UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSITargetIpAddress:%d", Index);
  1158. HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns");
  1159. HiiCreateStringOpCode (
  1160. StartOpCodeHandle,
  1161. (EFI_QUESTION_ID)(ATTEMPT_TARGET_IP_ADDRESS_QUESTION_ID + (Index - 1)),
  1162. CONFIGURATION_VARSTORE_ID,
  1163. (UINT16)(ATTEMPT_TARGET_IP_ADDRESS_VAR_OFFSET + sizeof (KEYWORD_STR) * (Index - 1)),
  1164. StringToken,
  1165. StringToken,
  1166. 0,
  1167. 0,
  1168. IP_MIN_SIZE,
  1169. IP_STR_MAX_SIZE,
  1170. NULL
  1171. );
  1172. //
  1173. // Create iSCSILUN Keyword.
  1174. //
  1175. UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_LUN_PROMPT%d", Index);
  1176. StringToken = HiiSetString (
  1177. mCallbackInfo->RegisteredHandle,
  1178. 0,
  1179. StringId,
  1180. NULL
  1181. );
  1182. UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSILUN:%d", Index);
  1183. HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns");
  1184. HiiCreateStringOpCode (
  1185. StartOpCodeHandle,
  1186. (EFI_QUESTION_ID)(ATTEMPT_LUN_QUESTION_ID + (Index - 1)),
  1187. CONFIGURATION_VARSTORE_ID,
  1188. (UINT16)(ATTEMPT_LUN_VAR_OFFSET + sizeof (KEYWORD_STR) * (Index - 1)),
  1189. StringToken,
  1190. StringToken,
  1191. 0,
  1192. 0,
  1193. LUN_MIN_SIZE,
  1194. LUN_MAX_SIZE,
  1195. NULL
  1196. );
  1197. //
  1198. // Create iSCSIAuthenticationMethod Keyword.
  1199. //
  1200. UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_AUTHENTICATION_METHOD_PROMPT%d", Index);
  1201. StringToken = HiiSetString (
  1202. mCallbackInfo->RegisteredHandle,
  1203. 0,
  1204. StringId,
  1205. NULL
  1206. );
  1207. UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIAuthenticationMethod:%d", Index);
  1208. HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns");
  1209. HiiCreateNumericOpCode (
  1210. StartOpCodeHandle,
  1211. (EFI_QUESTION_ID)(ATTEMPT_AUTHENTICATION_METHOD_QUESTION_ID + (Index - 1)),
  1212. CONFIGURATION_VARSTORE_ID,
  1213. (UINT16)(ATTEMPT_AUTHENTICATION_METHOD_VAR_OFFSET + (Index - 1)),
  1214. StringToken,
  1215. StringToken,
  1216. 0,
  1217. 0,
  1218. 0,
  1219. 1,
  1220. 0,
  1221. NULL
  1222. );
  1223. //
  1224. // Create iSCSIChapType Keyword.
  1225. //
  1226. UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_CHARTYPE_PROMPT%d", Index);
  1227. StringToken = HiiSetString (
  1228. mCallbackInfo->RegisteredHandle,
  1229. 0,
  1230. StringId,
  1231. NULL
  1232. );
  1233. UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIChapType:%d", Index);
  1234. HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns");
  1235. HiiCreateNumericOpCode (
  1236. StartOpCodeHandle,
  1237. (EFI_QUESTION_ID)(ATTEMPT_CHARTYPE_QUESTION_ID + (Index - 1)),
  1238. CONFIGURATION_VARSTORE_ID,
  1239. (UINT16)(ATTEMPT_CHARTYPE_VAR_OFFSET + (Index - 1)),
  1240. StringToken,
  1241. StringToken,
  1242. 0,
  1243. 0,
  1244. 0,
  1245. 1,
  1246. 0,
  1247. NULL
  1248. );
  1249. //
  1250. // Create iSCSIChapUsername Keyword.
  1251. //
  1252. UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_CHAR_USER_NAME_PROMPT%d", Index);
  1253. StringToken = HiiSetString (
  1254. mCallbackInfo->RegisteredHandle,
  1255. 0,
  1256. StringId,
  1257. NULL
  1258. );
  1259. UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIChapUsername:%d", Index);
  1260. HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns");
  1261. HiiCreateStringOpCode (
  1262. StartOpCodeHandle,
  1263. (EFI_QUESTION_ID)(ATTEMPT_CHAR_USER_NAME_QUESTION_ID + (Index - 1)),
  1264. CONFIGURATION_VARSTORE_ID,
  1265. (UINT16)(ATTEMPT_CHAR_USER_NAME_VAR_OFFSET + sizeof (KEYWORD_STR) * (Index - 1)),
  1266. StringToken,
  1267. StringToken,
  1268. 0,
  1269. 0,
  1270. 0,
  1271. ISCSI_CHAP_NAME_MAX_LEN,
  1272. NULL
  1273. );
  1274. //
  1275. // Create iSCSIChapSecret Keyword.
  1276. //
  1277. UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_CHAR_SECRET_PROMPT%d", Index);
  1278. StringToken = HiiSetString (
  1279. mCallbackInfo->RegisteredHandle,
  1280. 0,
  1281. StringId,
  1282. NULL
  1283. );
  1284. UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIChapSecret:%d", Index);
  1285. HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns");
  1286. HiiCreateStringOpCode (
  1287. StartOpCodeHandle,
  1288. (EFI_QUESTION_ID)(ATTEMPT_CHAR_SECRET_QUESTION_ID + (Index - 1)),
  1289. CONFIGURATION_VARSTORE_ID,
  1290. (UINT16)(ATTEMPT_CHAR_SECRET_VAR_OFFSET + sizeof (KEYWORD_STR) * (Index - 1)),
  1291. StringToken,
  1292. StringToken,
  1293. 0,
  1294. 0,
  1295. ISCSI_CHAP_SECRET_MIN_LEN,
  1296. ISCSI_CHAP_SECRET_MAX_LEN,
  1297. NULL
  1298. );
  1299. //
  1300. // Create iSCSIReverseChapUsername Keyword.
  1301. //
  1302. UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_CHAR_REVERSE_USER_NAME_PROMPT%d", Index);
  1303. StringToken = HiiSetString (
  1304. mCallbackInfo->RegisteredHandle,
  1305. 0,
  1306. StringId,
  1307. NULL
  1308. );
  1309. UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIReverseChapUsername:%d", Index);
  1310. HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns");
  1311. HiiCreateStringOpCode (
  1312. StartOpCodeHandle,
  1313. (EFI_QUESTION_ID)(ATTEMPT_CHAR_REVERSE_USER_NAME_QUESTION_ID + (Index - 1)),
  1314. CONFIGURATION_VARSTORE_ID,
  1315. (UINT16)(ATTEMPT_CHAR_REVERSE_USER_NAME_VAR_OFFSET + sizeof (KEYWORD_STR) * (Index - 1)),
  1316. StringToken,
  1317. StringToken,
  1318. 0,
  1319. 0,
  1320. 0,
  1321. ISCSI_CHAP_NAME_MAX_LEN,
  1322. NULL
  1323. );
  1324. //
  1325. // Create iSCSIReverseChapSecret Keyword.
  1326. //
  1327. UnicodeSPrint (StringId, sizeof (StringId), L"STR_ISCSI_CHAR_REVERSE_SECRET_PROMPT%d", Index);
  1328. StringToken = HiiSetString (
  1329. mCallbackInfo->RegisteredHandle,
  1330. 0,
  1331. StringId,
  1332. NULL
  1333. );
  1334. UnicodeSPrint (KeywordId, sizeof (KeywordId), L"iSCSIReverseChapSecret:%d", Index);
  1335. HiiSetString (mCallbackInfo->RegisteredHandle, StringToken, KeywordId, "x-UEFI-ns");
  1336. HiiCreateStringOpCode (
  1337. StartOpCodeHandle,
  1338. (EFI_QUESTION_ID)(ATTEMPT_CHAR_REVERSE_SECRET_QUESTION_ID + (Index - 1)),
  1339. CONFIGURATION_VARSTORE_ID,
  1340. (UINT16)(ATTEMPT_CHAR_REVERSE_SECRET_VAR_OFFSET + sizeof (KEYWORD_STR) * (Index - 1)),
  1341. StringToken,
  1342. StringToken,
  1343. 0,
  1344. 0,
  1345. ISCSI_CHAP_SECRET_MIN_LEN,
  1346. ISCSI_CHAP_SECRET_MAX_LEN,
  1347. NULL
  1348. );
  1349. }
  1350. Status = HiiUpdateForm (
  1351. mCallbackInfo->RegisteredHandle, // HII handle
  1352. &gIScsiConfigGuid, // Formset GUID
  1353. FORMID_ATTEMPT_FORM, // Form ID
  1354. StartOpCodeHandle, // Label for where to insert opcodes
  1355. EndOpCodeHandle // Replace data
  1356. );
  1357. HiiFreeOpCodeHandle (StartOpCodeHandle);
  1358. HiiFreeOpCodeHandle (EndOpCodeHandle);
  1359. return Status;
  1360. }
  1361. /**
  1362. Free the attempt configure data variable.
  1363. **/
  1364. VOID
  1365. IScsiCleanAttemptVariable (
  1366. IN VOID
  1367. )
  1368. {
  1369. ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData;
  1370. UINT8 *AttemptConfigOrder;
  1371. UINTN AttemptConfigOrderSize;
  1372. UINTN Index;
  1373. //
  1374. // Get the initialized attempt order.
  1375. //
  1376. AttemptConfigOrder = IScsiGetVariableAndSize (
  1377. L"InitialAttemptOrder",
  1378. &gIScsiConfigGuid,
  1379. &AttemptConfigOrderSize
  1380. );
  1381. if ((AttemptConfigOrder == NULL) || (AttemptConfigOrderSize == 0)) {
  1382. return;
  1383. }
  1384. for (Index = 1; Index < AttemptConfigOrderSize / sizeof (UINT8); Index++) {
  1385. UnicodeSPrint (
  1386. mPrivate->PortString,
  1387. (UINTN)ISCSI_NAME_IFR_MAX_SIZE,
  1388. L"Attempt %d",
  1389. Index
  1390. );
  1391. GetVariable2 (
  1392. mPrivate->PortString,
  1393. &gEfiIScsiInitiatorNameProtocolGuid,
  1394. (VOID **)&AttemptConfigData,
  1395. NULL
  1396. );
  1397. if (AttemptConfigData != NULL) {
  1398. gRT->SetVariable (
  1399. mPrivate->PortString,
  1400. &gEfiIScsiInitiatorNameProtocolGuid,
  1401. 0,
  1402. 0,
  1403. NULL
  1404. );
  1405. }
  1406. }
  1407. return;
  1408. }
  1409. /**
  1410. Get the recorded NIC info from global structure by the Index.
  1411. @param[in] NicIndex The index indicates the position of NIC info.
  1412. @return Pointer to the NIC info, or NULL if not found.
  1413. **/
  1414. ISCSI_NIC_INFO *
  1415. IScsiGetNicInfoByIndex (
  1416. IN UINT8 NicIndex
  1417. )
  1418. {
  1419. LIST_ENTRY *Entry;
  1420. ISCSI_NIC_INFO *NicInfo;
  1421. NET_LIST_FOR_EACH (Entry, &mPrivate->NicInfoList) {
  1422. NicInfo = NET_LIST_USER_STRUCT (Entry, ISCSI_NIC_INFO, Link);
  1423. if (NicInfo->NicIndex == NicIndex) {
  1424. return NicInfo;
  1425. }
  1426. }
  1427. return NULL;
  1428. }
  1429. /**
  1430. Get the NIC's PCI location and return it according to the composited
  1431. format defined in iSCSI Boot Firmware Table.
  1432. @param[in] Controller The handle of the controller.
  1433. @param[out] Bus The bus number.
  1434. @param[out] Device The device number.
  1435. @param[out] Function The function number.
  1436. @return The composited representation of the NIC PCI location.
  1437. **/
  1438. UINT16
  1439. IScsiGetNICPciLocation (
  1440. IN EFI_HANDLE Controller,
  1441. OUT UINTN *Bus,
  1442. OUT UINTN *Device,
  1443. OUT UINTN *Function
  1444. )
  1445. {
  1446. EFI_STATUS Status;
  1447. EFI_DEVICE_PATH_PROTOCOL *DevicePath;
  1448. EFI_HANDLE PciIoHandle;
  1449. EFI_PCI_IO_PROTOCOL *PciIo;
  1450. UINTN Segment;
  1451. Status = gBS->HandleProtocol (
  1452. Controller,
  1453. &gEfiDevicePathProtocolGuid,
  1454. (VOID **)&DevicePath
  1455. );
  1456. if (EFI_ERROR (Status)) {
  1457. return 0;
  1458. }
  1459. Status = gBS->LocateDevicePath (
  1460. &gEfiPciIoProtocolGuid,
  1461. &DevicePath,
  1462. &PciIoHandle
  1463. );
  1464. if (EFI_ERROR (Status)) {
  1465. return 0;
  1466. }
  1467. Status = gBS->HandleProtocol (PciIoHandle, &gEfiPciIoProtocolGuid, (VOID **)&PciIo);
  1468. if (EFI_ERROR (Status)) {
  1469. return 0;
  1470. }
  1471. Status = PciIo->GetLocation (PciIo, &Segment, Bus, Device, Function);
  1472. if (EFI_ERROR (Status)) {
  1473. return 0;
  1474. }
  1475. return (UINT16)((*Bus << 8) | (*Device << 3) | *Function);
  1476. }
  1477. /**
  1478. Read the EFI variable (VendorGuid/Name) and return a dynamically allocated
  1479. buffer, and the size of the buffer. If failure, return NULL.
  1480. @param[in] Name String part of EFI variable name.
  1481. @param[in] VendorGuid GUID part of EFI variable name.
  1482. @param[out] VariableSize Returns the size of the EFI variable that was read.
  1483. @return Dynamically allocated memory that contains a copy of the EFI variable.
  1484. @return Caller is responsible freeing the buffer.
  1485. @retval NULL Variable was not read.
  1486. **/
  1487. VOID *
  1488. IScsiGetVariableAndSize (
  1489. IN CHAR16 *Name,
  1490. IN EFI_GUID *VendorGuid,
  1491. OUT UINTN *VariableSize
  1492. )
  1493. {
  1494. EFI_STATUS Status;
  1495. UINTN BufferSize;
  1496. VOID *Buffer;
  1497. Buffer = NULL;
  1498. //
  1499. // Pass in a zero size buffer to find the required buffer size.
  1500. //
  1501. BufferSize = 0;
  1502. Status = gRT->GetVariable (Name, VendorGuid, NULL, &BufferSize, Buffer);
  1503. if (Status == EFI_BUFFER_TOO_SMALL) {
  1504. //
  1505. // Allocate the buffer to return
  1506. //
  1507. Buffer = AllocateZeroPool (BufferSize);
  1508. if (Buffer == NULL) {
  1509. return NULL;
  1510. }
  1511. //
  1512. // Read variable into the allocated buffer.
  1513. //
  1514. Status = gRT->GetVariable (Name, VendorGuid, NULL, &BufferSize, Buffer);
  1515. if (EFI_ERROR (Status)) {
  1516. BufferSize = 0;
  1517. }
  1518. }
  1519. *VariableSize = BufferSize;
  1520. return Buffer;
  1521. }
  1522. /**
  1523. Create the iSCSI driver data.
  1524. @param[in] Image The handle of the driver image.
  1525. @param[in] Controller The handle of the controller.
  1526. @return The iSCSI driver data created.
  1527. @retval NULL Other errors as indicated.
  1528. **/
  1529. ISCSI_DRIVER_DATA *
  1530. IScsiCreateDriverData (
  1531. IN EFI_HANDLE Image,
  1532. IN EFI_HANDLE Controller
  1533. )
  1534. {
  1535. ISCSI_DRIVER_DATA *Private;
  1536. EFI_STATUS Status;
  1537. Private = AllocateZeroPool (sizeof (ISCSI_DRIVER_DATA));
  1538. if (Private == NULL) {
  1539. return NULL;
  1540. }
  1541. Private->Signature = ISCSI_DRIVER_DATA_SIGNATURE;
  1542. Private->Image = Image;
  1543. Private->Controller = Controller;
  1544. Private->Session = NULL;
  1545. //
  1546. // Create an event to be signaled when the BS to RT transition is triggerd so
  1547. // as to abort the iSCSI session.
  1548. //
  1549. Status = gBS->CreateEventEx (
  1550. EVT_NOTIFY_SIGNAL,
  1551. TPL_CALLBACK,
  1552. IScsiOnExitBootService,
  1553. Private,
  1554. &gEfiEventExitBootServicesGuid,
  1555. &Private->ExitBootServiceEvent
  1556. );
  1557. if (EFI_ERROR (Status)) {
  1558. FreePool (Private);
  1559. return NULL;
  1560. }
  1561. Private->ExtScsiPassThruHandle = NULL;
  1562. CopyMem (&Private->IScsiExtScsiPassThru, &gIScsiExtScsiPassThruProtocolTemplate, sizeof (EFI_EXT_SCSI_PASS_THRU_PROTOCOL));
  1563. //
  1564. // 0 is designated to the TargetId, so use another value for the AdapterId.
  1565. //
  1566. Private->ExtScsiPassThruMode.AdapterId = 2;
  1567. Private->ExtScsiPassThruMode.Attributes = EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_PHYSICAL | EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL;
  1568. Private->ExtScsiPassThruMode.IoAlign = 4;
  1569. Private->IScsiExtScsiPassThru.Mode = &Private->ExtScsiPassThruMode;
  1570. return Private;
  1571. }
  1572. /**
  1573. Clean the iSCSI driver data.
  1574. @param[in] Private The iSCSI driver data.
  1575. @retval EFI_SUCCESS The clean operation is successful.
  1576. @retval Others Other errors as indicated.
  1577. **/
  1578. EFI_STATUS
  1579. IScsiCleanDriverData (
  1580. IN ISCSI_DRIVER_DATA *Private
  1581. )
  1582. {
  1583. EFI_STATUS Status;
  1584. Status = EFI_SUCCESS;
  1585. if (Private->DevicePath != NULL) {
  1586. Status = gBS->UninstallProtocolInterface (
  1587. Private->ExtScsiPassThruHandle,
  1588. &gEfiDevicePathProtocolGuid,
  1589. Private->DevicePath
  1590. );
  1591. if (EFI_ERROR (Status)) {
  1592. goto EXIT;
  1593. }
  1594. FreePool (Private->DevicePath);
  1595. }
  1596. if (Private->ExtScsiPassThruHandle != NULL) {
  1597. Status = gBS->UninstallProtocolInterface (
  1598. Private->ExtScsiPassThruHandle,
  1599. &gEfiExtScsiPassThruProtocolGuid,
  1600. &Private->IScsiExtScsiPassThru
  1601. );
  1602. if (!EFI_ERROR (Status)) {
  1603. mPrivate->OneSessionEstablished = FALSE;
  1604. }
  1605. }
  1606. EXIT:
  1607. if (Private->ExitBootServiceEvent != NULL) {
  1608. gBS->CloseEvent (Private->ExitBootServiceEvent);
  1609. }
  1610. mCallbackInfo->Current = NULL;
  1611. FreePool (Private);
  1612. return Status;
  1613. }
  1614. /**
  1615. Check wheather the Controller handle is configured to use DHCP protocol.
  1616. @param[in] Controller The handle of the controller.
  1617. @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6.
  1618. @retval TRUE The handle of the controller need the Dhcp protocol.
  1619. @retval FALSE The handle of the controller does not need the Dhcp protocol.
  1620. **/
  1621. BOOLEAN
  1622. IScsiDhcpIsConfigured (
  1623. IN EFI_HANDLE Controller,
  1624. IN UINT8 IpVersion
  1625. )
  1626. {
  1627. ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptTmp;
  1628. UINT8 *AttemptConfigOrder;
  1629. UINTN AttemptConfigOrderSize;
  1630. UINTN Index;
  1631. EFI_STATUS Status;
  1632. EFI_MAC_ADDRESS MacAddr;
  1633. UINTN HwAddressSize;
  1634. UINT16 VlanId;
  1635. CHAR16 MacString[ISCSI_MAX_MAC_STRING_LEN];
  1636. CHAR16 AttemptMacString[ISCSI_MAX_MAC_STRING_LEN];
  1637. CHAR16 AttemptName[ISCSI_NAME_IFR_MAX_SIZE];
  1638. AttemptConfigOrder = IScsiGetVariableAndSize (
  1639. L"AttemptOrder",
  1640. &gIScsiConfigGuid,
  1641. &AttemptConfigOrderSize
  1642. );
  1643. if ((AttemptConfigOrder == NULL) || (AttemptConfigOrderSize == 0)) {
  1644. return FALSE;
  1645. }
  1646. //
  1647. // Get MAC address of this network device.
  1648. //
  1649. Status = NetLibGetMacAddress (Controller, &MacAddr, &HwAddressSize);
  1650. if (EFI_ERROR (Status)) {
  1651. return FALSE;
  1652. }
  1653. //
  1654. // Get VLAN ID of this network device.
  1655. //
  1656. VlanId = NetLibGetVlanId (Controller);
  1657. IScsiMacAddrToStr (&MacAddr, (UINT32)HwAddressSize, VlanId, MacString);
  1658. for (Index = 0; Index < AttemptConfigOrderSize / sizeof (UINT8); Index++) {
  1659. UnicodeSPrint (
  1660. AttemptName,
  1661. (UINTN)128,
  1662. L"Attempt %d",
  1663. (UINTN)AttemptConfigOrder[Index]
  1664. );
  1665. Status = GetVariable2 (
  1666. AttemptName,
  1667. &gEfiIScsiInitiatorNameProtocolGuid,
  1668. (VOID **)&AttemptTmp,
  1669. NULL
  1670. );
  1671. if ((AttemptTmp == NULL) || EFI_ERROR (Status)) {
  1672. continue;
  1673. }
  1674. ASSERT (AttemptConfigOrder[Index] == AttemptTmp->AttemptConfigIndex);
  1675. if (AttemptTmp->SessionConfigData.Enabled == ISCSI_DISABLED) {
  1676. FreePool (AttemptTmp);
  1677. continue;
  1678. }
  1679. if ((AttemptTmp->SessionConfigData.IpMode != IP_MODE_AUTOCONFIG) &&
  1680. (AttemptTmp->SessionConfigData.IpMode != ((IpVersion == IP_VERSION_4) ? IP_MODE_IP4 : IP_MODE_IP6)))
  1681. {
  1682. FreePool (AttemptTmp);
  1683. continue;
  1684. }
  1685. AsciiStrToUnicodeStrS (AttemptTmp->MacString, AttemptMacString, sizeof (AttemptMacString) / sizeof (AttemptMacString[0]));
  1686. if ((AttemptTmp->Actived == ISCSI_ACTIVE_DISABLED) || StrCmp (MacString, AttemptMacString)) {
  1687. continue;
  1688. }
  1689. if ((AttemptTmp->SessionConfigData.IpMode == IP_MODE_AUTOCONFIG) ||
  1690. (AttemptTmp->SessionConfigData.InitiatorInfoFromDhcp == TRUE) ||
  1691. (AttemptTmp->SessionConfigData.TargetInfoFromDhcp == TRUE))
  1692. {
  1693. FreePool (AttemptTmp);
  1694. FreePool (AttemptConfigOrder);
  1695. return TRUE;
  1696. }
  1697. FreePool (AttemptTmp);
  1698. }
  1699. FreePool (AttemptConfigOrder);
  1700. return FALSE;
  1701. }
  1702. /**
  1703. Check whether the Controller handle is configured to use DNS protocol.
  1704. @param[in] Controller The handle of the controller.
  1705. @retval TRUE The handle of the controller need the Dns protocol.
  1706. @retval FALSE The handle of the controller does not need the Dns protocol.
  1707. **/
  1708. BOOLEAN
  1709. IScsiDnsIsConfigured (
  1710. IN EFI_HANDLE Controller
  1711. )
  1712. {
  1713. ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptTmp;
  1714. UINT8 *AttemptConfigOrder;
  1715. UINTN AttemptConfigOrderSize;
  1716. UINTN Index;
  1717. EFI_STATUS Status;
  1718. EFI_MAC_ADDRESS MacAddr;
  1719. UINTN HwAddressSize;
  1720. UINT16 VlanId;
  1721. CHAR16 AttemptMacString[ISCSI_MAX_MAC_STRING_LEN];
  1722. CHAR16 MacString[ISCSI_MAX_MAC_STRING_LEN];
  1723. CHAR16 AttemptName[ISCSI_NAME_IFR_MAX_SIZE];
  1724. AttemptConfigOrder = IScsiGetVariableAndSize (
  1725. L"AttemptOrder",
  1726. &gIScsiConfigGuid,
  1727. &AttemptConfigOrderSize
  1728. );
  1729. if ((AttemptConfigOrder == NULL) || (AttemptConfigOrderSize == 0)) {
  1730. return FALSE;
  1731. }
  1732. //
  1733. // Get MAC address of this network device.
  1734. //
  1735. Status = NetLibGetMacAddress (Controller, &MacAddr, &HwAddressSize);
  1736. if (EFI_ERROR (Status)) {
  1737. return FALSE;
  1738. }
  1739. //
  1740. // Get VLAN ID of this network device.
  1741. //
  1742. VlanId = NetLibGetVlanId (Controller);
  1743. IScsiMacAddrToStr (&MacAddr, (UINT32)HwAddressSize, VlanId, MacString);
  1744. for (Index = 0; Index < AttemptConfigOrderSize / sizeof (UINT8); Index++) {
  1745. UnicodeSPrint (
  1746. AttemptName,
  1747. (UINTN)128,
  1748. L"Attempt %d",
  1749. (UINTN)AttemptConfigOrder[Index]
  1750. );
  1751. Status = GetVariable2 (
  1752. AttemptName,
  1753. &gEfiIScsiInitiatorNameProtocolGuid,
  1754. (VOID **)&AttemptTmp,
  1755. NULL
  1756. );
  1757. if ((AttemptTmp == NULL) || EFI_ERROR (Status)) {
  1758. continue;
  1759. }
  1760. ASSERT (AttemptConfigOrder[Index] == AttemptTmp->AttemptConfigIndex);
  1761. AsciiStrToUnicodeStrS (AttemptTmp->MacString, AttemptMacString, sizeof (AttemptMacString) / sizeof (AttemptMacString[0]));
  1762. if ((AttemptTmp->SessionConfigData.Enabled == ISCSI_DISABLED) || StrCmp (MacString, AttemptMacString)) {
  1763. FreePool (AttemptTmp);
  1764. continue;
  1765. }
  1766. if (AttemptTmp->SessionConfigData.DnsMode || AttemptTmp->SessionConfigData.TargetInfoFromDhcp) {
  1767. FreePool (AttemptTmp);
  1768. FreePool (AttemptConfigOrder);
  1769. return TRUE;
  1770. } else {
  1771. FreePool (AttemptTmp);
  1772. continue;
  1773. }
  1774. }
  1775. FreePool (AttemptConfigOrder);
  1776. return FALSE;
  1777. }
  1778. /**
  1779. Get the various configuration data.
  1780. @param[in] Private The iSCSI driver data.
  1781. @retval EFI_SUCCESS The configuration data is retrieved.
  1782. @retval EFI_NOT_FOUND This iSCSI driver is not configured yet.
  1783. @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
  1784. **/
  1785. EFI_STATUS
  1786. IScsiGetConfigData (
  1787. IN ISCSI_DRIVER_DATA *Private
  1788. )
  1789. {
  1790. EFI_STATUS Status;
  1791. CHAR16 MacString[ISCSI_MAX_MAC_STRING_LEN];
  1792. CHAR16 AttemptMacString[ISCSI_MAX_MAC_STRING_LEN];
  1793. UINTN Index;
  1794. ISCSI_NIC_INFO *NicInfo;
  1795. ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData;
  1796. ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptTmp;
  1797. UINT8 *AttemptConfigOrder;
  1798. UINTN AttemptConfigOrderSize;
  1799. CHAR16 IScsiMode[64];
  1800. CHAR16 IpMode[64];
  1801. //
  1802. // There should be at least one attempt configured.
  1803. //
  1804. AttemptConfigOrder = IScsiGetVariableAndSize (
  1805. L"AttemptOrder",
  1806. &gIScsiConfigGuid,
  1807. &AttemptConfigOrderSize
  1808. );
  1809. if ((AttemptConfigOrder == NULL) || (AttemptConfigOrderSize == 0)) {
  1810. return EFI_NOT_FOUND;
  1811. }
  1812. //
  1813. // Get the iSCSI Initiator Name.
  1814. //
  1815. mPrivate->InitiatorNameLength = ISCSI_NAME_MAX_SIZE;
  1816. Status = gIScsiInitiatorName.Get (
  1817. &gIScsiInitiatorName,
  1818. &mPrivate->InitiatorNameLength,
  1819. mPrivate->InitiatorName
  1820. );
  1821. if (EFI_ERROR (Status)) {
  1822. return Status;
  1823. }
  1824. //
  1825. // Get the normal configuration.
  1826. //
  1827. for (Index = 0; Index < AttemptConfigOrderSize / sizeof (UINT8); Index++) {
  1828. //
  1829. // Check whether the attempt exists in AttemptConfig.
  1830. //
  1831. AttemptTmp = IScsiConfigGetAttemptByConfigIndex (AttemptConfigOrder[Index]);
  1832. if ((AttemptTmp != NULL) && (AttemptTmp->SessionConfigData.Enabled == ISCSI_DISABLED)) {
  1833. continue;
  1834. } else if ((AttemptTmp != NULL) && (AttemptTmp->SessionConfigData.Enabled != ISCSI_DISABLED)) {
  1835. //
  1836. // Check the autoconfig path to see whether it should be retried.
  1837. //
  1838. if ((AttemptTmp->SessionConfigData.IpMode == IP_MODE_AUTOCONFIG) &&
  1839. !AttemptTmp->AutoConfigureSuccess)
  1840. {
  1841. if (mPrivate->Ipv6Flag &&
  1842. (AttemptTmp->AutoConfigureMode == IP_MODE_AUTOCONFIG_IP6))
  1843. {
  1844. //
  1845. // Autoconfigure for IP6 already attempted but failed. Do not try again.
  1846. //
  1847. continue;
  1848. } else if (!mPrivate->Ipv6Flag &&
  1849. (AttemptTmp->AutoConfigureMode == IP_MODE_AUTOCONFIG_IP4))
  1850. {
  1851. //
  1852. // Autoconfigure for IP4 already attempted but failed. Do not try again.
  1853. //
  1854. continue;
  1855. } else {
  1856. //
  1857. // Try another approach for this autoconfigure path.
  1858. //
  1859. AttemptTmp->AutoConfigureMode =
  1860. (UINT8)(mPrivate->Ipv6Flag ? IP_MODE_AUTOCONFIG_IP6 : IP_MODE_AUTOCONFIG_IP4);
  1861. AttemptTmp->SessionConfigData.InitiatorInfoFromDhcp = TRUE;
  1862. AttemptTmp->SessionConfigData.TargetInfoFromDhcp = TRUE;
  1863. AttemptTmp->DhcpSuccess = FALSE;
  1864. //
  1865. // Get some information from the dhcp server.
  1866. //
  1867. if (!mPrivate->Ipv6Flag) {
  1868. Status = IScsiDoDhcp (Private->Image, Private->Controller, AttemptTmp);
  1869. if (!EFI_ERROR (Status)) {
  1870. AttemptTmp->DhcpSuccess = TRUE;
  1871. }
  1872. } else {
  1873. Status = IScsiDoDhcp6 (Private->Image, Private->Controller, AttemptTmp);
  1874. if (!EFI_ERROR (Status)) {
  1875. AttemptTmp->DhcpSuccess = TRUE;
  1876. }
  1877. }
  1878. //
  1879. // Refresh the state of this attempt to NVR.
  1880. //
  1881. UnicodeSPrint (
  1882. mPrivate->PortString,
  1883. (UINTN)ISCSI_NAME_IFR_MAX_SIZE,
  1884. L"Attempt %d",
  1885. (UINTN)AttemptTmp->AttemptConfigIndex
  1886. );
  1887. gRT->SetVariable (
  1888. mPrivate->PortString,
  1889. &gEfiIScsiInitiatorNameProtocolGuid,
  1890. ISCSI_CONFIG_VAR_ATTR,
  1891. sizeof (ISCSI_ATTEMPT_CONFIG_NVDATA),
  1892. AttemptTmp
  1893. );
  1894. continue;
  1895. }
  1896. } else if (AttemptTmp->SessionConfigData.InitiatorInfoFromDhcp &&
  1897. !AttemptTmp->ValidPath &&
  1898. (AttemptTmp->NicIndex == mPrivate->CurrentNic))
  1899. {
  1900. //
  1901. // If the attempt associates with the current NIC, we can
  1902. // get DHCP information for already added, but failed, attempt.
  1903. //
  1904. AttemptTmp->DhcpSuccess = FALSE;
  1905. if (!mPrivate->Ipv6Flag && (AttemptTmp->SessionConfigData.IpMode == IP_MODE_IP4)) {
  1906. Status = IScsiDoDhcp (Private->Image, Private->Controller, AttemptTmp);
  1907. if (!EFI_ERROR (Status)) {
  1908. AttemptTmp->DhcpSuccess = TRUE;
  1909. }
  1910. } else if (mPrivate->Ipv6Flag && (AttemptTmp->SessionConfigData.IpMode == IP_MODE_IP6)) {
  1911. Status = IScsiDoDhcp6 (Private->Image, Private->Controller, AttemptTmp);
  1912. if (!EFI_ERROR (Status)) {
  1913. AttemptTmp->DhcpSuccess = TRUE;
  1914. }
  1915. }
  1916. //
  1917. // Refresh the state of this attempt to NVR.
  1918. //
  1919. UnicodeSPrint (
  1920. mPrivate->PortString,
  1921. (UINTN)ISCSI_NAME_IFR_MAX_SIZE,
  1922. L"Attempt %d",
  1923. (UINTN)AttemptTmp->AttemptConfigIndex
  1924. );
  1925. gRT->SetVariable (
  1926. mPrivate->PortString,
  1927. &gEfiIScsiInitiatorNameProtocolGuid,
  1928. ISCSI_CONFIG_VAR_ATTR,
  1929. sizeof (ISCSI_ATTEMPT_CONFIG_NVDATA),
  1930. AttemptTmp
  1931. );
  1932. continue;
  1933. } else {
  1934. continue;
  1935. }
  1936. }
  1937. //
  1938. // This attempt does not exist in AttemptConfig. Try to add a new one.
  1939. //
  1940. NicInfo = IScsiGetNicInfoByIndex (mPrivate->CurrentNic);
  1941. ASSERT (NicInfo != NULL);
  1942. IScsiMacAddrToStr (&NicInfo->PermanentAddress, NicInfo->HwAddressSize, NicInfo->VlanId, MacString);
  1943. UnicodeSPrint (
  1944. mPrivate->PortString,
  1945. (UINTN)ISCSI_NAME_IFR_MAX_SIZE,
  1946. L"Attempt %d",
  1947. (UINTN)AttemptConfigOrder[Index]
  1948. );
  1949. GetVariable2 (
  1950. mPrivate->PortString,
  1951. &gEfiIScsiInitiatorNameProtocolGuid,
  1952. (VOID **)&AttemptConfigData,
  1953. NULL
  1954. );
  1955. AsciiStrToUnicodeStrS (AttemptConfigData->MacString, AttemptMacString, sizeof (AttemptMacString) / sizeof (AttemptMacString[0]));
  1956. if ((AttemptConfigData == NULL) || (AttemptConfigData->Actived == ISCSI_ACTIVE_DISABLED) ||
  1957. StrCmp (MacString, AttemptMacString))
  1958. {
  1959. continue;
  1960. }
  1961. ASSERT (AttemptConfigOrder[Index] == AttemptConfigData->AttemptConfigIndex);
  1962. AttemptConfigData->NicIndex = NicInfo->NicIndex;
  1963. AttemptConfigData->DhcpSuccess = FALSE;
  1964. AttemptConfigData->ValidiBFTPath = (BOOLEAN)(mPrivate->EnableMpio ? TRUE : FALSE);
  1965. AttemptConfigData->ValidPath = FALSE;
  1966. if (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_AUTOCONFIG) {
  1967. AttemptConfigData->SessionConfigData.InitiatorInfoFromDhcp = TRUE;
  1968. AttemptConfigData->SessionConfigData.TargetInfoFromDhcp = TRUE;
  1969. AttemptConfigData->AutoConfigureMode =
  1970. (UINT8)(mPrivate->Ipv6Flag ? IP_MODE_AUTOCONFIG_IP6 : IP_MODE_AUTOCONFIG_IP4);
  1971. AttemptConfigData->AutoConfigureSuccess = FALSE;
  1972. }
  1973. //
  1974. // Get some information from dhcp server.
  1975. //
  1976. if ((AttemptConfigData->SessionConfigData.Enabled != ISCSI_DISABLED) &&
  1977. AttemptConfigData->SessionConfigData.InitiatorInfoFromDhcp)
  1978. {
  1979. if (!mPrivate->Ipv6Flag &&
  1980. ((AttemptConfigData->SessionConfigData.IpMode == IP_MODE_IP4) ||
  1981. (AttemptConfigData->AutoConfigureMode == IP_MODE_AUTOCONFIG_IP4)))
  1982. {
  1983. Status = IScsiDoDhcp (Private->Image, Private->Controller, AttemptConfigData);
  1984. if (!EFI_ERROR (Status)) {
  1985. AttemptConfigData->DhcpSuccess = TRUE;
  1986. }
  1987. } else if (mPrivate->Ipv6Flag &&
  1988. ((AttemptConfigData->SessionConfigData.IpMode == IP_MODE_IP6) ||
  1989. (AttemptConfigData->AutoConfigureMode == IP_MODE_AUTOCONFIG_IP6)))
  1990. {
  1991. Status = IScsiDoDhcp6 (Private->Image, Private->Controller, AttemptConfigData);
  1992. if (!EFI_ERROR (Status)) {
  1993. AttemptConfigData->DhcpSuccess = TRUE;
  1994. }
  1995. }
  1996. //
  1997. // Refresh the state of this attempt to NVR.
  1998. //
  1999. UnicodeSPrint (
  2000. mPrivate->PortString,
  2001. (UINTN)ISCSI_NAME_IFR_MAX_SIZE,
  2002. L"Attempt %d",
  2003. (UINTN)AttemptConfigData->AttemptConfigIndex
  2004. );
  2005. gRT->SetVariable (
  2006. mPrivate->PortString,
  2007. &gEfiIScsiInitiatorNameProtocolGuid,
  2008. ISCSI_CONFIG_VAR_ATTR,
  2009. sizeof (ISCSI_ATTEMPT_CONFIG_NVDATA),
  2010. AttemptConfigData
  2011. );
  2012. }
  2013. //
  2014. // Update Attempt Help Info.
  2015. //
  2016. if (AttemptConfigData->SessionConfigData.Enabled == ISCSI_DISABLED) {
  2017. UnicodeSPrint (IScsiMode, 64, L"Disabled");
  2018. } else if (AttemptConfigData->SessionConfigData.Enabled == ISCSI_ENABLED) {
  2019. UnicodeSPrint (IScsiMode, 64, L"Enabled");
  2020. } else if (AttemptConfigData->SessionConfigData.Enabled == ISCSI_ENABLED_FOR_MPIO) {
  2021. UnicodeSPrint (IScsiMode, 64, L"Enabled for MPIO");
  2022. }
  2023. if (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_IP4) {
  2024. UnicodeSPrint (IpMode, 64, L"IP4");
  2025. } else if (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_IP6) {
  2026. UnicodeSPrint (IpMode, 64, L"IP6");
  2027. } else if (AttemptConfigData->SessionConfigData.IpMode == IP_MODE_AUTOCONFIG) {
  2028. UnicodeSPrint (IpMode, 64, L"Autoconfigure");
  2029. }
  2030. UnicodeSPrint (
  2031. mPrivate->PortString,
  2032. (UINTN)ISCSI_NAME_IFR_MAX_SIZE,
  2033. L"MAC: %s, PFA: Bus %d | Dev %d | Func %d, iSCSI mode: %s, IP version: %s",
  2034. MacString,
  2035. NicInfo->BusNumber,
  2036. NicInfo->DeviceNumber,
  2037. NicInfo->FunctionNumber,
  2038. IScsiMode,
  2039. IpMode
  2040. );
  2041. AttemptConfigData->AttemptTitleHelpToken = HiiSetString (
  2042. mCallbackInfo->RegisteredHandle,
  2043. 0,
  2044. mPrivate->PortString,
  2045. NULL
  2046. );
  2047. if (AttemptConfigData->AttemptTitleHelpToken == 0) {
  2048. return EFI_OUT_OF_RESOURCES;
  2049. }
  2050. //
  2051. // Record the attempt in global link list.
  2052. //
  2053. InsertTailList (&mPrivate->AttemptConfigs, &AttemptConfigData->Link);
  2054. mPrivate->AttemptCount++;
  2055. if (AttemptConfigData->SessionConfigData.Enabled == ISCSI_ENABLED_FOR_MPIO) {
  2056. mPrivate->MpioCount++;
  2057. mPrivate->EnableMpio = TRUE;
  2058. if (AttemptConfigData->AuthenticationType == ISCSI_AUTH_TYPE_KRB) {
  2059. mPrivate->Krb5MpioCount++;
  2060. }
  2061. } else if (AttemptConfigData->SessionConfigData.Enabled == ISCSI_ENABLED) {
  2062. mPrivate->SinglePathCount++;
  2063. }
  2064. }
  2065. //
  2066. // Reorder the AttemptConfig by the configured order.
  2067. //
  2068. for (Index = 0; Index < AttemptConfigOrderSize / sizeof (UINT8); Index++) {
  2069. AttemptConfigData = IScsiConfigGetAttemptByConfigIndex (AttemptConfigOrder[Index]);
  2070. if (AttemptConfigData == NULL) {
  2071. continue;
  2072. }
  2073. RemoveEntryList (&AttemptConfigData->Link);
  2074. InsertTailList (&mPrivate->AttemptConfigs, &AttemptConfigData->Link);
  2075. }
  2076. //
  2077. // Update the Main Form.
  2078. //
  2079. IScsiConfigUpdateAttempt ();
  2080. FreePool (AttemptConfigOrder);
  2081. //
  2082. // There should be at least one attempt configuration.
  2083. //
  2084. if (!mPrivate->EnableMpio) {
  2085. if (mPrivate->SinglePathCount == 0) {
  2086. return EFI_NOT_FOUND;
  2087. }
  2088. mPrivate->ValidSinglePathCount = mPrivate->SinglePathCount;
  2089. }
  2090. return EFI_SUCCESS;
  2091. }
  2092. /**
  2093. Get the device path of the iSCSI tcp connection and update it.
  2094. @param Session The iSCSI session.
  2095. @return The updated device path.
  2096. @retval NULL Other errors as indicated.
  2097. **/
  2098. EFI_DEVICE_PATH_PROTOCOL *
  2099. IScsiGetTcpConnDevicePath (
  2100. IN ISCSI_SESSION *Session
  2101. )
  2102. {
  2103. ISCSI_CONNECTION *Conn;
  2104. EFI_DEVICE_PATH_PROTOCOL *DevicePath;
  2105. EFI_STATUS Status;
  2106. EFI_DEV_PATH *DPathNode;
  2107. UINTN PathLen;
  2108. if (Session->State != SESSION_STATE_LOGGED_IN) {
  2109. return NULL;
  2110. }
  2111. Conn = NET_LIST_USER_STRUCT_S (
  2112. Session->Conns.ForwardLink,
  2113. ISCSI_CONNECTION,
  2114. Link,
  2115. ISCSI_CONNECTION_SIGNATURE
  2116. );
  2117. Status = gBS->HandleProtocol (
  2118. Conn->TcpIo.Handle,
  2119. &gEfiDevicePathProtocolGuid,
  2120. (VOID **)&DevicePath
  2121. );
  2122. if (EFI_ERROR (Status)) {
  2123. return NULL;
  2124. }
  2125. //
  2126. // Duplicate it.
  2127. //
  2128. DevicePath = DuplicateDevicePath (DevicePath);
  2129. if (DevicePath == NULL) {
  2130. return NULL;
  2131. }
  2132. DPathNode = (EFI_DEV_PATH *)DevicePath;
  2133. while (!IsDevicePathEnd (&DPathNode->DevPath)) {
  2134. if (DevicePathType (&DPathNode->DevPath) == MESSAGING_DEVICE_PATH) {
  2135. if (!Conn->Ipv6Flag && (DevicePathSubType (&DPathNode->DevPath) == MSG_IPv4_DP)) {
  2136. DPathNode->Ipv4.LocalPort = 0;
  2137. DPathNode->Ipv4.StaticIpAddress =
  2138. (BOOLEAN)(!Session->ConfigData->SessionConfigData.InitiatorInfoFromDhcp);
  2139. //
  2140. // Add a judgement here to support previous versions of IPv4_DEVICE_PATH.
  2141. // In previous versions of IPv4_DEVICE_PATH, GatewayIpAddress and SubnetMask
  2142. // do not exist.
  2143. // In new version of IPv4_DEVICE_PATH, structure length is 27.
  2144. //
  2145. PathLen = DevicePathNodeLength (&DPathNode->Ipv4);
  2146. if (PathLen == IP4_NODE_LEN_NEW_VERSIONS) {
  2147. IP4_COPY_ADDRESS (
  2148. &DPathNode->Ipv4.GatewayIpAddress,
  2149. &Session->ConfigData->SessionConfigData.Gateway
  2150. );
  2151. IP4_COPY_ADDRESS (
  2152. &DPathNode->Ipv4.SubnetMask,
  2153. &Session->ConfigData->SessionConfigData.SubnetMask
  2154. );
  2155. }
  2156. break;
  2157. } else if (Conn->Ipv6Flag && (DevicePathSubType (&DPathNode->DevPath) == MSG_IPv6_DP)) {
  2158. DPathNode->Ipv6.LocalPort = 0;
  2159. //
  2160. // Add a judgement here to support previous versions of IPv6_DEVICE_PATH.
  2161. // In previous versions of IPv6_DEVICE_PATH, IpAddressOrigin, PrefixLength
  2162. // and GatewayIpAddress do not exist.
  2163. // In new version of IPv6_DEVICE_PATH, structure length is 60, while in
  2164. // old versions, the length is 43.
  2165. //
  2166. PathLen = DevicePathNodeLength (&DPathNode->Ipv6);
  2167. if (PathLen == IP6_NODE_LEN_NEW_VERSIONS ) {
  2168. DPathNode->Ipv6.IpAddressOrigin = 0;
  2169. DPathNode->Ipv6.PrefixLength = IP6_PREFIX_LENGTH;
  2170. ZeroMem (&DPathNode->Ipv6.GatewayIpAddress, sizeof (EFI_IPv6_ADDRESS));
  2171. } else if (PathLen == IP6_NODE_LEN_OLD_VERSIONS) {
  2172. //
  2173. // StaticIPAddress is a field in old versions of IPv6_DEVICE_PATH, while ignored in new
  2174. // version. Set StaticIPAddress through its' offset in old IPv6_DEVICE_PATH.
  2175. //
  2176. *((UINT8 *)(&DPathNode->Ipv6) + IP6_OLD_IPADDRESS_OFFSET) =
  2177. (BOOLEAN)(!Session->ConfigData->SessionConfigData.InitiatorInfoFromDhcp);
  2178. }
  2179. break;
  2180. }
  2181. }
  2182. DPathNode = (EFI_DEV_PATH *)NextDevicePathNode (&DPathNode->DevPath);
  2183. }
  2184. return DevicePath;
  2185. }
  2186. /**
  2187. Abort the session when the transition from BS to RT is initiated.
  2188. @param[in] Event The event signaled.
  2189. @param[in] Context The iSCSI driver data.
  2190. **/
  2191. VOID
  2192. EFIAPI
  2193. IScsiOnExitBootService (
  2194. IN EFI_EVENT Event,
  2195. IN VOID *Context
  2196. )
  2197. {
  2198. ISCSI_DRIVER_DATA *Private;
  2199. Private = (ISCSI_DRIVER_DATA *)Context;
  2200. gBS->CloseEvent (Private->ExitBootServiceEvent);
  2201. Private->ExitBootServiceEvent = NULL;
  2202. if (Private->Session != NULL) {
  2203. IScsiSessionAbort (Private->Session);
  2204. }
  2205. }
  2206. /**
  2207. Tests whether a controller handle is being managed by IScsi driver.
  2208. This function tests whether the driver specified by DriverBindingHandle is
  2209. currently managing the controller specified by ControllerHandle. This test
  2210. is performed by evaluating if the protocol specified by ProtocolGuid is
  2211. present on ControllerHandle and is was opened by DriverBindingHandle and Nic
  2212. Device handle with an attribute of EFI_OPEN_PROTOCOL_BY_DRIVER.
  2213. If ProtocolGuid is NULL, then ASSERT().
  2214. @param ControllerHandle A handle for a controller to test.
  2215. @param DriverBindingHandle Specifies the driver binding handle for the
  2216. driver.
  2217. @param ProtocolGuid Specifies the protocol that the driver specified
  2218. by DriverBindingHandle opens in its Start()
  2219. function.
  2220. @retval EFI_SUCCESS ControllerHandle is managed by the driver
  2221. specified by DriverBindingHandle.
  2222. @retval EFI_UNSUPPORTED ControllerHandle is not managed by the driver
  2223. specified by DriverBindingHandle.
  2224. **/
  2225. EFI_STATUS
  2226. EFIAPI
  2227. IScsiTestManagedDevice (
  2228. IN EFI_HANDLE ControllerHandle,
  2229. IN EFI_HANDLE DriverBindingHandle,
  2230. IN EFI_GUID *ProtocolGuid
  2231. )
  2232. {
  2233. EFI_STATUS Status;
  2234. VOID *ManagedInterface;
  2235. EFI_HANDLE NicControllerHandle;
  2236. ASSERT (ProtocolGuid != NULL);
  2237. NicControllerHandle = NetLibGetNicHandle (ControllerHandle, ProtocolGuid);
  2238. if (NicControllerHandle == NULL) {
  2239. return EFI_UNSUPPORTED;
  2240. }
  2241. Status = gBS->OpenProtocol (
  2242. ControllerHandle,
  2243. (EFI_GUID *)ProtocolGuid,
  2244. &ManagedInterface,
  2245. DriverBindingHandle,
  2246. NicControllerHandle,
  2247. EFI_OPEN_PROTOCOL_BY_DRIVER
  2248. );
  2249. if (!EFI_ERROR (Status)) {
  2250. gBS->CloseProtocol (
  2251. ControllerHandle,
  2252. (EFI_GUID *)ProtocolGuid,
  2253. DriverBindingHandle,
  2254. NicControllerHandle
  2255. );
  2256. return EFI_UNSUPPORTED;
  2257. }
  2258. if (Status != EFI_ALREADY_STARTED) {
  2259. return EFI_UNSUPPORTED;
  2260. }
  2261. return EFI_SUCCESS;
  2262. }