IScsiConfig.c 129 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928
  1. /** @file
  2. Helper functions for configuring or getting the parameters relating to iSCSI.
  3. Copyright (c) 2004 - 2019, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "IScsiImpl.h"
  7. CHAR16 mVendorStorageName[] = L"ISCSI_CONFIG_IFR_NVDATA";
  8. ISCSI_FORM_CALLBACK_INFO *mCallbackInfo = NULL;
  9. HII_VENDOR_DEVICE_PATH mIScsiHiiVendorDevicePath = {
  10. {
  11. {
  12. HARDWARE_DEVICE_PATH,
  13. HW_VENDOR_DP,
  14. {
  15. (UINT8)(sizeof (VENDOR_DEVICE_PATH)),
  16. (UINT8)((sizeof (VENDOR_DEVICE_PATH)) >> 8)
  17. }
  18. },
  19. ISCSI_CONFIG_GUID
  20. },
  21. {
  22. END_DEVICE_PATH_TYPE,
  23. END_ENTIRE_DEVICE_PATH_SUBTYPE,
  24. {
  25. (UINT8)(END_DEVICE_PATH_LENGTH),
  26. (UINT8)((END_DEVICE_PATH_LENGTH) >> 8)
  27. }
  28. }
  29. };
  30. /**
  31. Convert the IP address into a dotted string.
  32. @param[in] Ip The IP address.
  33. @param[in] Ipv6Flag Indicates whether the IP address is version 4 or version 6.
  34. @param[out] Str The formatted IP string.
  35. **/
  36. VOID
  37. IScsiIpToStr (
  38. IN EFI_IP_ADDRESS *Ip,
  39. IN BOOLEAN Ipv6Flag,
  40. OUT CHAR16 *Str
  41. )
  42. {
  43. EFI_IPv4_ADDRESS *Ip4;
  44. EFI_IPv6_ADDRESS *Ip6;
  45. UINTN Index;
  46. BOOLEAN Short;
  47. UINTN Number;
  48. CHAR16 FormatString[8];
  49. if (!Ipv6Flag) {
  50. Ip4 = &Ip->v4;
  51. UnicodeSPrint (
  52. Str,
  53. (UINTN)2 * IP4_STR_MAX_SIZE,
  54. L"%d.%d.%d.%d",
  55. (UINTN)Ip4->Addr[0],
  56. (UINTN)Ip4->Addr[1],
  57. (UINTN)Ip4->Addr[2],
  58. (UINTN)Ip4->Addr[3]
  59. );
  60. return;
  61. }
  62. Ip6 = &Ip->v6;
  63. Short = FALSE;
  64. for (Index = 0; Index < 15; Index = Index + 2) {
  65. if (!Short &&
  66. (Index % 2 == 0) &&
  67. (Ip6->Addr[Index] == 0) &&
  68. (Ip6->Addr[Index + 1] == 0)
  69. )
  70. {
  71. //
  72. // Deal with the case of ::.
  73. //
  74. if (Index == 0) {
  75. *Str = L':';
  76. *(Str + 1) = L':';
  77. Str = Str + 2;
  78. } else {
  79. *Str = L':';
  80. Str = Str + 1;
  81. }
  82. while ((Index < 15) && (Ip6->Addr[Index] == 0) && (Ip6->Addr[Index + 1] == 0)) {
  83. Index = Index + 2;
  84. }
  85. Short = TRUE;
  86. if (Index == 16) {
  87. //
  88. // :: is at the end of the address.
  89. //
  90. *Str = L'\0';
  91. break;
  92. }
  93. }
  94. ASSERT (Index < 15);
  95. if (Ip6->Addr[Index] == 0) {
  96. Number = UnicodeSPrint (Str, 2 * IP_STR_MAX_SIZE, L"%x:", (UINTN)Ip6->Addr[Index + 1]);
  97. } else {
  98. if (Ip6->Addr[Index + 1] < 0x10) {
  99. CopyMem (FormatString, L"%x0%x:", StrSize (L"%x0%x:"));
  100. } else {
  101. CopyMem (FormatString, L"%x%x:", StrSize (L"%x%x:"));
  102. }
  103. Number = UnicodeSPrint (
  104. Str,
  105. 2 * IP_STR_MAX_SIZE,
  106. (CONST CHAR16 *)FormatString,
  107. (UINTN)Ip6->Addr[Index],
  108. (UINTN)Ip6->Addr[Index + 1]
  109. );
  110. }
  111. Str = Str + Number;
  112. if (Index + 2 == 16) {
  113. *Str = L'\0';
  114. if (*(Str - 1) == L':') {
  115. *(Str - 1) = L'\0';
  116. }
  117. }
  118. }
  119. }
  120. /**
  121. Check whether the input IP address is valid.
  122. @param[in] Ip The IP address.
  123. @param[in] IpMode Indicates iSCSI running on IP4 or IP6 stack.
  124. @retval TRUE The input IP address is valid.
  125. @retval FALSE Otherwise
  126. **/
  127. BOOLEAN
  128. IpIsUnicast (
  129. IN EFI_IP_ADDRESS *Ip,
  130. IN UINT8 IpMode
  131. )
  132. {
  133. if (IpMode == IP_MODE_IP4) {
  134. if (IP4_IS_UNSPECIFIED (NTOHL (Ip->Addr[0])) || IP4_IS_LOCAL_BROADCAST (NTOHL (Ip->Addr[0]))) {
  135. return FALSE;
  136. }
  137. return TRUE;
  138. } else if (IpMode == IP_MODE_IP6) {
  139. return NetIp6IsValidUnicast (&Ip->v6);
  140. } else {
  141. DEBUG ((DEBUG_ERROR, "IpMode %d is invalid when configuring the iSCSI target IP!\n", IpMode));
  142. return FALSE;
  143. }
  144. }
  145. /**
  146. Parse IsId in string format and convert it to binary.
  147. @param[in] String The buffer of the string to be parsed.
  148. @param[in, out] IsId The buffer to store IsId.
  149. @retval EFI_SUCCESS The operation finished successfully.
  150. @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
  151. **/
  152. EFI_STATUS
  153. IScsiParseIsIdFromString (
  154. IN CONST CHAR16 *String,
  155. IN OUT UINT8 *IsId
  156. )
  157. {
  158. UINT8 Index;
  159. CHAR16 *IsIdStr;
  160. CHAR16 TempStr[3];
  161. UINTN NodeVal;
  162. CHAR16 PortString[ISCSI_NAME_IFR_MAX_SIZE];
  163. EFI_INPUT_KEY Key;
  164. if ((String == NULL) || (IsId == NULL)) {
  165. return EFI_INVALID_PARAMETER;
  166. }
  167. IsIdStr = (CHAR16 *)String;
  168. if ((StrLen (IsIdStr) != 6) && (StrLen (IsIdStr) != 12)) {
  169. UnicodeSPrint (
  170. PortString,
  171. (UINTN)ISCSI_NAME_IFR_MAX_SIZE,
  172. L"Error! Only last 3 bytes are configurable, please input 6 hex numbers for last 3 bytes only or 12 hex numbers for full SSID!\n"
  173. );
  174. CreatePopUp (
  175. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  176. &Key,
  177. PortString,
  178. NULL
  179. );
  180. return EFI_INVALID_PARAMETER;
  181. }
  182. if (StrLen (IsIdStr) == 12) {
  183. IsIdStr += 6;
  184. }
  185. for (Index = 3; Index < 6; Index++) {
  186. CopyMem (TempStr, IsIdStr, sizeof (TempStr));
  187. TempStr[2] = L'\0';
  188. //
  189. // Convert the string to IsId. StrHexToUintn stops at the first character
  190. // that is not a valid hex character, '\0' here.
  191. //
  192. NodeVal = StrHexToUintn (TempStr);
  193. IsId[Index] = (UINT8)NodeVal;
  194. IsIdStr = IsIdStr + 2;
  195. }
  196. return EFI_SUCCESS;
  197. }
  198. /**
  199. Convert IsId from binary to string format.
  200. @param[out] String The buffer to store the converted string.
  201. @param[in] IsId The buffer to store IsId.
  202. @retval EFI_SUCCESS The string converted successfully.
  203. @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
  204. **/
  205. EFI_STATUS
  206. IScsiConvertIsIdToString (
  207. OUT CHAR16 *String,
  208. IN UINT8 *IsId
  209. )
  210. {
  211. UINT8 Index;
  212. UINTN Number;
  213. if ((String == NULL) || (IsId == NULL)) {
  214. return EFI_INVALID_PARAMETER;
  215. }
  216. for (Index = 0; Index < 6; Index++) {
  217. if (IsId[Index] <= 0xF) {
  218. Number = UnicodeSPrint (
  219. String,
  220. 2 * ISID_CONFIGURABLE_STORAGE,
  221. L"0%X",
  222. (UINTN)IsId[Index]
  223. );
  224. } else {
  225. Number = UnicodeSPrint (
  226. String,
  227. 2 * ISID_CONFIGURABLE_STORAGE,
  228. L"%X",
  229. (UINTN)IsId[Index]
  230. );
  231. }
  232. String = String + Number;
  233. }
  234. *String = L'\0';
  235. return EFI_SUCCESS;
  236. }
  237. /**
  238. Get the Offset value specified by the input String.
  239. @param[in] Configuration A null-terminated Unicode string in
  240. <ConfigString> format.
  241. @param[in] String The string is "&OFFSET=".
  242. @param[out] Value The Offset value.
  243. @retval EFI_OUT_OF_RESOURCES Insufficient resources to store necessary
  244. structures.
  245. @retval EFI_SUCCESS Value of <Number> is outputted in Number
  246. successfully.
  247. **/
  248. EFI_STATUS
  249. IScsiGetValue (
  250. IN CONST EFI_STRING Configuration,
  251. IN CHAR16 *String,
  252. OUT UINTN *Value
  253. )
  254. {
  255. CHAR16 *StringPtr;
  256. CHAR16 *TmpPtr;
  257. CHAR16 *Str;
  258. CHAR16 TmpStr[2];
  259. UINTN Length;
  260. UINTN Len;
  261. UINTN Index;
  262. UINT8 *Buf;
  263. UINT8 DigitUint8;
  264. EFI_STATUS Status;
  265. //
  266. // Get Value.
  267. //
  268. Buf = NULL;
  269. StringPtr = StrStr (Configuration, String);
  270. ASSERT (StringPtr != NULL);
  271. StringPtr += StrLen (String);
  272. TmpPtr = StringPtr;
  273. while (*StringPtr != L'\0' && *StringPtr != L'&') {
  274. StringPtr++;
  275. }
  276. Length = StringPtr - TmpPtr;
  277. Len = Length + 1;
  278. Str = AllocateZeroPool (Len * sizeof (CHAR16));
  279. if (Str == NULL) {
  280. Status = EFI_OUT_OF_RESOURCES;
  281. goto Exit;
  282. }
  283. CopyMem (Str, TmpPtr, Len * sizeof (CHAR16));
  284. *(Str + Length) = L'\0';
  285. Len = (Len + 1) / 2;
  286. Buf = (UINT8 *)AllocateZeroPool (Len);
  287. if (Buf == NULL) {
  288. Status = EFI_OUT_OF_RESOURCES;
  289. goto Exit;
  290. }
  291. ZeroMem (TmpStr, sizeof (TmpStr));
  292. for (Index = 0; Index < Length; Index++) {
  293. TmpStr[0] = Str[Length - Index - 1];
  294. DigitUint8 = (UINT8)StrHexToUint64 (TmpStr);
  295. if ((Index & 1) == 0) {
  296. Buf[Index/2] = DigitUint8;
  297. } else {
  298. Buf[Index/2] = (UINT8)((DigitUint8 << 4) + Buf[Index/2]);
  299. }
  300. }
  301. *Value = 0;
  302. CopyMem (
  303. Value,
  304. Buf,
  305. (((Length + 1) / 2) < sizeof (UINTN)) ? ((Length + 1) / 2) : sizeof (UINTN)
  306. );
  307. FreePool (Buf);
  308. Status = EFI_SUCCESS;
  309. Exit:
  310. if (Str != NULL) {
  311. FreePool (Str);
  312. }
  313. return Status;
  314. }
  315. /**
  316. Get the attempt config data from global structure by the ConfigIndex.
  317. @param[in] AttemptConfigIndex The unique index indicates the attempt.
  318. @return Pointer to the attempt config data.
  319. @retval NULL The attempt configuration data cannot be found.
  320. **/
  321. ISCSI_ATTEMPT_CONFIG_NVDATA *
  322. IScsiConfigGetAttemptByConfigIndex (
  323. IN UINT8 AttemptConfigIndex
  324. )
  325. {
  326. LIST_ENTRY *Entry;
  327. ISCSI_ATTEMPT_CONFIG_NVDATA *Attempt;
  328. NET_LIST_FOR_EACH (Entry, &mPrivate->AttemptConfigs) {
  329. Attempt = NET_LIST_USER_STRUCT (Entry, ISCSI_ATTEMPT_CONFIG_NVDATA, Link);
  330. if (Attempt->AttemptConfigIndex == AttemptConfigIndex) {
  331. return Attempt;
  332. }
  333. }
  334. return NULL;
  335. }
  336. /**
  337. Get the existing attempt config data from global structure by the NicIndex.
  338. @param[in] NewAttempt The created new attempt
  339. @param[in] IScsiMode The IScsi Mode of the new attempt, Enabled or
  340. Enabled for MPIO.
  341. @return Pointer to the existing attempt config data which
  342. has the same NICIndex as the new created attempt.
  343. @retval NULL The attempt with NicIndex does not exist.
  344. **/
  345. ISCSI_ATTEMPT_CONFIG_NVDATA *
  346. IScsiConfigGetAttemptByNic (
  347. IN ISCSI_ATTEMPT_CONFIG_NVDATA *NewAttempt,
  348. IN UINT8 IScsiMode
  349. )
  350. {
  351. LIST_ENTRY *Entry;
  352. ISCSI_ATTEMPT_CONFIG_NVDATA *Attempt;
  353. NET_LIST_FOR_EACH (Entry, &mPrivate->AttemptConfigs) {
  354. Attempt = NET_LIST_USER_STRUCT (Entry, ISCSI_ATTEMPT_CONFIG_NVDATA, Link);
  355. if ((Attempt != NewAttempt) && (Attempt->NicIndex == NewAttempt->NicIndex) &&
  356. (Attempt->SessionConfigData.Enabled == IScsiMode))
  357. {
  358. return Attempt;
  359. }
  360. }
  361. return NULL;
  362. }
  363. /**
  364. Extract the Index of the attempt list.
  365. @param[in] AttemptNameList The Name list of the Attempts.
  366. @param[out] AttemptIndexList The Index list of the Attempts.
  367. @param[in] IsAddAttempts If TRUE, Indicates add one or more attempts.
  368. If FALSE, Indicates delete attempts or change attempt order.
  369. @retval EFI_SUCCESS The Attempt list is valid.
  370. @retval EFI_INVALID_PARAMETERS The Attempt List is invalid.
  371. **/
  372. EFI_STATUS
  373. IScsiGetAttemptIndexList (
  374. IN CHAR16 *AttemptNameList,
  375. OUT UINT8 *AttemptIndexList,
  376. IN BOOLEAN IsAddAttempts
  377. )
  378. {
  379. ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData;
  380. CHAR16 *AttemptStr;
  381. UINT8 AttemptIndex;
  382. UINTN Len;
  383. UINTN Index;
  384. Index = 0;
  385. if ((AttemptNameList == NULL) || (*AttemptNameList == L'\0')) {
  386. return EFI_INVALID_PARAMETER;
  387. }
  388. AttemptStr = AttemptNameList;
  389. Len = StrLen (L"attempt:");
  390. while (*AttemptStr != L'\0') {
  391. AttemptStr = StrStr (AttemptStr, L"attempt:");
  392. if (AttemptStr == NULL) {
  393. return EFI_INVALID_PARAMETER;
  394. }
  395. AttemptStr += Len;
  396. AttemptIndex = (UINT8)(*AttemptStr - L'0');
  397. AttemptConfigData = IScsiConfigGetAttemptByConfigIndex (AttemptIndex);
  398. if (IsAddAttempts) {
  399. if ((AttemptConfigData != NULL) || ((AttemptIndex) > PcdGet8 (PcdMaxIScsiAttemptNumber))) {
  400. return EFI_INVALID_PARAMETER;
  401. }
  402. } else {
  403. if (AttemptConfigData == NULL) {
  404. return EFI_INVALID_PARAMETER;
  405. }
  406. }
  407. AttemptIndexList[Index] = AttemptIndex;
  408. Index++;
  409. AttemptStr += 2;
  410. }
  411. return EFI_SUCCESS;
  412. }
  413. /**
  414. Convert the iSCSI configuration data into the IFR data.
  415. @param[in] Attempt The iSCSI attempt config data.
  416. @param[in, out] IfrNvData The IFR nv data.
  417. **/
  418. VOID
  419. IScsiConvertAttemptConfigDataToIfrNvData (
  420. IN ISCSI_ATTEMPT_CONFIG_NVDATA *Attempt,
  421. IN OUT ISCSI_CONFIG_IFR_NVDATA *IfrNvData
  422. )
  423. {
  424. ISCSI_SESSION_CONFIG_NVDATA *SessionConfigData;
  425. ISCSI_CHAP_AUTH_CONFIG_NVDATA *AuthConfigData;
  426. EFI_IP_ADDRESS Ip;
  427. BOOLEAN DnsMode;
  428. //
  429. // Normal session configuration parameters.
  430. //
  431. SessionConfigData = &Attempt->SessionConfigData;
  432. IfrNvData->Enabled = SessionConfigData->Enabled;
  433. IfrNvData->IpMode = SessionConfigData->IpMode;
  434. DnsMode = SessionConfigData->DnsMode;
  435. IfrNvData->InitiatorInfoFromDhcp = SessionConfigData->InitiatorInfoFromDhcp;
  436. IfrNvData->TargetInfoFromDhcp = SessionConfigData->TargetInfoFromDhcp;
  437. IfrNvData->TargetPort = SessionConfigData->TargetPort;
  438. if (IfrNvData->IpMode == IP_MODE_IP4) {
  439. CopyMem (&Ip.v4, &SessionConfigData->LocalIp, sizeof (EFI_IPv4_ADDRESS));
  440. IScsiIpToStr (&Ip, FALSE, IfrNvData->LocalIp);
  441. CopyMem (&Ip.v4, &SessionConfigData->SubnetMask, sizeof (EFI_IPv4_ADDRESS));
  442. IScsiIpToStr (&Ip, FALSE, IfrNvData->SubnetMask);
  443. CopyMem (&Ip.v4, &SessionConfigData->Gateway, sizeof (EFI_IPv4_ADDRESS));
  444. IScsiIpToStr (&Ip, FALSE, IfrNvData->Gateway);
  445. ZeroMem (IfrNvData->TargetIp, sizeof (IfrNvData->TargetIp));
  446. if (SessionConfigData->TargetIp.v4.Addr[0] != '\0') {
  447. CopyMem (&Ip.v4, &SessionConfigData->TargetIp, sizeof (EFI_IPv4_ADDRESS));
  448. IScsiIpToStr (&Ip, FALSE, IfrNvData->TargetIp);
  449. }
  450. } else if (IfrNvData->IpMode == IP_MODE_IP6) {
  451. ZeroMem (IfrNvData->TargetIp, sizeof (IfrNvData->TargetIp));
  452. if (SessionConfigData->TargetIp.v6.Addr[0] != '\0') {
  453. IP6_COPY_ADDRESS (&Ip.v6, &SessionConfigData->TargetIp);
  454. IScsiIpToStr (&Ip, TRUE, IfrNvData->TargetIp);
  455. }
  456. }
  457. AsciiStrToUnicodeStrS (
  458. SessionConfigData->TargetName,
  459. IfrNvData->TargetName,
  460. sizeof (IfrNvData->TargetName) / sizeof (IfrNvData->TargetName[0])
  461. );
  462. if (DnsMode) {
  463. AsciiStrToUnicodeStrS (
  464. SessionConfigData->TargetUrl,
  465. IfrNvData->TargetIp,
  466. sizeof (IfrNvData->TargetIp) / sizeof (IfrNvData->TargetIp[0])
  467. );
  468. }
  469. IScsiLunToUnicodeStr (SessionConfigData->BootLun, IfrNvData->BootLun);
  470. IScsiConvertIsIdToString (IfrNvData->IsId, SessionConfigData->IsId);
  471. IfrNvData->ConnectRetryCount = SessionConfigData->ConnectRetryCount;
  472. IfrNvData->ConnectTimeout = SessionConfigData->ConnectTimeout;
  473. //
  474. // Authentication parameters.
  475. //
  476. IfrNvData->AuthenticationType = Attempt->AuthenticationType;
  477. if (IfrNvData->AuthenticationType == ISCSI_AUTH_TYPE_CHAP) {
  478. AuthConfigData = &Attempt->AuthConfigData.CHAP;
  479. IfrNvData->CHAPType = AuthConfigData->CHAPType;
  480. AsciiStrToUnicodeStrS (
  481. AuthConfigData->CHAPName,
  482. IfrNvData->CHAPName,
  483. sizeof (IfrNvData->CHAPName) / sizeof (IfrNvData->CHAPName[0])
  484. );
  485. AsciiStrToUnicodeStrS (
  486. AuthConfigData->CHAPSecret,
  487. IfrNvData->CHAPSecret,
  488. sizeof (IfrNvData->CHAPSecret) / sizeof (IfrNvData->CHAPSecret[0])
  489. );
  490. AsciiStrToUnicodeStrS (
  491. AuthConfigData->ReverseCHAPName,
  492. IfrNvData->ReverseCHAPName,
  493. sizeof (IfrNvData->ReverseCHAPName) / sizeof (IfrNvData->ReverseCHAPName[0])
  494. );
  495. AsciiStrToUnicodeStrS (
  496. AuthConfigData->ReverseCHAPSecret,
  497. IfrNvData->ReverseCHAPSecret,
  498. sizeof (IfrNvData->ReverseCHAPSecret) / sizeof (IfrNvData->ReverseCHAPSecret[0])
  499. );
  500. }
  501. //
  502. // Other parameters.
  503. //
  504. AsciiStrToUnicodeStrS (
  505. Attempt->AttemptName,
  506. IfrNvData->AttemptName,
  507. sizeof (IfrNvData->AttemptName) / sizeof (IfrNvData->AttemptName[0])
  508. );
  509. }
  510. /**
  511. Convert the iSCSI configuration data into the IFR data Which will be used
  512. to extract the iSCSI Keyword configuration in <ConfigAltResp> format.
  513. @param[in, out] IfrNvData The IFR nv data.
  514. **/
  515. VOID
  516. EFIAPI
  517. IScsiConvertAttemptConfigDataToIfrNvDataByKeyword (
  518. IN OUT ISCSI_CONFIG_IFR_NVDATA *IfrNvData
  519. )
  520. {
  521. LIST_ENTRY *Entry;
  522. ISCSI_ATTEMPT_CONFIG_NVDATA *Attempt;
  523. ISCSI_SESSION_CONFIG_NVDATA *SessionConfigData;
  524. ISCSI_CHAP_AUTH_CONFIG_NVDATA *AuthConfigData;
  525. CHAR16 AttemptNameList[ATTEMPT_NAME_LIST_SIZE];
  526. ISCSI_NIC_INFO *NicInfo;
  527. CHAR16 MacString[ISCSI_MAX_MAC_STRING_LEN];
  528. EFI_IP_ADDRESS Ip;
  529. UINTN Index;
  530. UINTN StringLen;
  531. NicInfo = NULL;
  532. ZeroMem (AttemptNameList, sizeof (AttemptNameList));
  533. if ((mPrivate != NULL) && (mPrivate->AttemptCount != 0)) {
  534. NET_LIST_FOR_EACH (Entry, &mPrivate->AttemptConfigs) {
  535. Attempt = NET_LIST_USER_STRUCT (Entry, ISCSI_ATTEMPT_CONFIG_NVDATA, Link);
  536. //
  537. // Normal session configuration parameters.
  538. //
  539. SessionConfigData = &Attempt->SessionConfigData;
  540. ASSERT ((Attempt->AttemptConfigIndex > 0) && (Attempt->AttemptConfigIndex <= FixedPcdGet8 (PcdMaxIScsiAttemptNumber)));
  541. Index = Attempt->AttemptConfigIndex - 1;
  542. //
  543. // Save the attempt to AttemptNameList as Attempt:1 Attempt:2
  544. //
  545. AsciiStrToUnicodeStrS (
  546. Attempt->AttemptName,
  547. AttemptNameList + StrLen (AttemptNameList),
  548. ATTEMPT_NAME_LIST_SIZE - StrLen (AttemptNameList)
  549. );
  550. StringLen = StrLen (AttemptNameList);
  551. ASSERT (StringLen > 2);
  552. *(AttemptNameList + StringLen - 2) = L':';
  553. *(AttemptNameList + StringLen) = L' ';
  554. AsciiStrToUnicodeStrS (
  555. Attempt->AttemptName,
  556. IfrNvData->ISCSIAttemptName + ATTEMPT_NAME_SIZE * Index,
  557. ATTEMPT_NAME_LIST_SIZE - ATTEMPT_NAME_SIZE * Index
  558. );
  559. IfrNvData->ISCSIBootEnableList[Index] = SessionConfigData->Enabled;
  560. IfrNvData->ISCSIIpAddressTypeList[Index] = SessionConfigData->IpMode;
  561. IfrNvData->ISCSIInitiatorInfoViaDHCP[Index] = SessionConfigData->InitiatorInfoFromDhcp;
  562. IfrNvData->ISCSITargetInfoViaDHCP[Index] = SessionConfigData->TargetInfoFromDhcp;
  563. IfrNvData->ISCSIConnectRetry[Index] = SessionConfigData->ConnectRetryCount;
  564. IfrNvData->ISCSIConnectTimeout[Index] = SessionConfigData->ConnectTimeout;
  565. IfrNvData->ISCSITargetTcpPort[Index] = SessionConfigData->TargetPort;
  566. if (SessionConfigData->IpMode == IP_MODE_IP4) {
  567. CopyMem (&Ip.v4, &SessionConfigData->LocalIp, sizeof (EFI_IPv4_ADDRESS));
  568. IScsiIpToStr (&Ip, FALSE, IfrNvData->Keyword[Index].ISCSIInitiatorIpAddress);
  569. CopyMem (&Ip.v4, &SessionConfigData->SubnetMask, sizeof (EFI_IPv4_ADDRESS));
  570. IScsiIpToStr (&Ip, FALSE, IfrNvData->Keyword[Index].ISCSIInitiatorNetmask);
  571. CopyMem (&Ip.v4, &SessionConfigData->Gateway, sizeof (EFI_IPv4_ADDRESS));
  572. IScsiIpToStr (&Ip, FALSE, IfrNvData->Keyword[Index].ISCSIInitiatorGateway);
  573. if (SessionConfigData->TargetIp.v4.Addr[0] != '\0') {
  574. CopyMem (&Ip.v4, &SessionConfigData->TargetIp, sizeof (EFI_IPv4_ADDRESS));
  575. IScsiIpToStr (&Ip, FALSE, IfrNvData->Keyword[Index].ISCSITargetIpAddress);
  576. }
  577. } else if (SessionConfigData->IpMode == IP_MODE_IP6) {
  578. ZeroMem (IfrNvData->Keyword[Index].ISCSITargetIpAddress, sizeof (IfrNvData->TargetIp));
  579. if (SessionConfigData->TargetIp.v6.Addr[0] != '\0') {
  580. IP6_COPY_ADDRESS (&Ip.v6, &SessionConfigData->TargetIp);
  581. IScsiIpToStr (&Ip, TRUE, IfrNvData->Keyword[Index].ISCSITargetIpAddress);
  582. }
  583. }
  584. AsciiStrToUnicodeStrS (
  585. SessionConfigData->TargetName,
  586. IfrNvData->Keyword[Index].ISCSITargetName,
  587. ISCSI_NAME_MAX_SIZE
  588. );
  589. if (SessionConfigData->DnsMode) {
  590. AsciiStrToUnicodeStrS (
  591. SessionConfigData->TargetUrl,
  592. IfrNvData->Keyword[Index].ISCSITargetIpAddress,
  593. sizeof (IfrNvData->Keyword[Index].ISCSITargetIpAddress) / sizeof (IfrNvData->Keyword[Index].ISCSITargetIpAddress[0])
  594. );
  595. }
  596. IScsiLunToUnicodeStr (SessionConfigData->BootLun, IfrNvData->Keyword[Index].ISCSILun);
  597. IScsiConvertIsIdToString (IfrNvData->Keyword[Index].ISCSIIsId, SessionConfigData->IsId);
  598. IfrNvData->ISCSIAuthenticationMethod[Index] = Attempt->AuthenticationType;
  599. if (Attempt->AuthenticationType == ISCSI_AUTH_TYPE_CHAP) {
  600. AuthConfigData = &Attempt->AuthConfigData.CHAP;
  601. IfrNvData->ISCSIChapType[Index] = AuthConfigData->CHAPType;
  602. AsciiStrToUnicodeStrS (
  603. AuthConfigData->CHAPName,
  604. IfrNvData->Keyword[Index].ISCSIChapUsername,
  605. ISCSI_CHAP_NAME_STORAGE
  606. );
  607. AsciiStrToUnicodeStrS (
  608. AuthConfigData->CHAPSecret,
  609. IfrNvData->Keyword[Index].ISCSIChapSecret,
  610. ISCSI_CHAP_SECRET_STORAGE
  611. );
  612. AsciiStrToUnicodeStrS (
  613. AuthConfigData->ReverseCHAPName,
  614. IfrNvData->Keyword[Index].ISCSIReverseChapUsername,
  615. ISCSI_CHAP_NAME_STORAGE
  616. );
  617. AsciiStrToUnicodeStrS (
  618. AuthConfigData->ReverseCHAPSecret,
  619. IfrNvData->Keyword[Index].ISCSIReverseChapSecret,
  620. ISCSI_CHAP_SECRET_STORAGE
  621. );
  622. }
  623. }
  624. CopyMem (IfrNvData->ISCSIDisplayAttemptList, AttemptNameList, ATTEMPT_NAME_LIST_SIZE);
  625. ZeroMem (IfrNvData->ISCSIMacAddr, sizeof (IfrNvData->ISCSIMacAddr));
  626. NET_LIST_FOR_EACH (Entry, &mPrivate->NicInfoList) {
  627. NicInfo = NET_LIST_USER_STRUCT (Entry, ISCSI_NIC_INFO, Link);
  628. IScsiMacAddrToStr (
  629. &NicInfo->PermanentAddress,
  630. NicInfo->HwAddressSize,
  631. NicInfo->VlanId,
  632. MacString
  633. );
  634. CopyMem (
  635. IfrNvData->ISCSIMacAddr + StrLen (IfrNvData->ISCSIMacAddr),
  636. MacString,
  637. StrLen (MacString) * sizeof (CHAR16)
  638. );
  639. *(IfrNvData->ISCSIMacAddr + StrLen (IfrNvData->ISCSIMacAddr)) = L'/';
  640. }
  641. StringLen = StrLen (IfrNvData->ISCSIMacAddr);
  642. if (StringLen > 0) {
  643. *(IfrNvData->ISCSIMacAddr + StringLen - 1) = L'\0';
  644. }
  645. }
  646. }
  647. /**
  648. Convert the IFR data to iSCSI configuration data.
  649. @param[in] IfrNvData Point to ISCSI_CONFIG_IFR_NVDATA.
  650. @param[in, out] Attempt The iSCSI attempt config data.
  651. @retval EFI_INVALID_PARAMETER Any input or configured parameter is invalid.
  652. @retval EFI_NOT_FOUND Cannot find the corresponding variable.
  653. @retval EFI_OUT_OF_RESOURCES The operation is failed due to lack of resources.
  654. @retval EFI_ABORTED The operation is aborted.
  655. @retval EFI_SUCCESS The operation is completed successfully.
  656. **/
  657. EFI_STATUS
  658. IScsiConvertIfrNvDataToAttemptConfigData (
  659. IN ISCSI_CONFIG_IFR_NVDATA *IfrNvData,
  660. IN OUT ISCSI_ATTEMPT_CONFIG_NVDATA *Attempt
  661. )
  662. {
  663. EFI_IP_ADDRESS HostIp;
  664. EFI_IP_ADDRESS SubnetMask;
  665. EFI_IP_ADDRESS Gateway;
  666. CHAR16 *MacString;
  667. CHAR16 *AttemptName1;
  668. CHAR16 *AttemptName2;
  669. ISCSI_ATTEMPT_CONFIG_NVDATA *ExistAttempt;
  670. ISCSI_ATTEMPT_CONFIG_NVDATA *SameNicAttempt;
  671. CHAR16 IScsiMode[64];
  672. CHAR16 IpMode[64];
  673. ISCSI_NIC_INFO *NicInfo;
  674. EFI_INPUT_KEY Key;
  675. UINT8 *AttemptConfigOrder;
  676. UINTN AttemptConfigOrderSize;
  677. UINT8 *AttemptOrderTmp;
  678. UINTN TotalNumber;
  679. EFI_STATUS Status;
  680. if ((IfrNvData == NULL) || (Attempt == NULL)) {
  681. return EFI_INVALID_PARAMETER;
  682. }
  683. //
  684. // Update those fields which don't have INTERACTIVE attribute.
  685. //
  686. Attempt->SessionConfigData.ConnectRetryCount = IfrNvData->ConnectRetryCount;
  687. Attempt->SessionConfigData.ConnectTimeout = IfrNvData->ConnectTimeout;
  688. Attempt->SessionConfigData.IpMode = IfrNvData->IpMode;
  689. if (IfrNvData->IpMode < IP_MODE_AUTOCONFIG) {
  690. Attempt->SessionConfigData.InitiatorInfoFromDhcp = IfrNvData->InitiatorInfoFromDhcp;
  691. Attempt->SessionConfigData.TargetPort = IfrNvData->TargetPort;
  692. if (Attempt->SessionConfigData.TargetPort == 0) {
  693. Attempt->SessionConfigData.TargetPort = ISCSI_WELL_KNOWN_PORT;
  694. }
  695. Attempt->SessionConfigData.TargetInfoFromDhcp = IfrNvData->TargetInfoFromDhcp;
  696. }
  697. Attempt->AuthenticationType = IfrNvData->AuthenticationType;
  698. if (Attempt->AuthenticationType == ISCSI_AUTH_TYPE_CHAP) {
  699. Attempt->AuthConfigData.CHAP.CHAPType = IfrNvData->CHAPType;
  700. }
  701. //
  702. // Only do full parameter validation if iSCSI is enabled on this device.
  703. //
  704. if (IfrNvData->Enabled != ISCSI_DISABLED) {
  705. if (Attempt->SessionConfigData.ConnectTimeout < CONNECT_MIN_TIMEOUT) {
  706. CreatePopUp (
  707. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  708. &Key,
  709. L"Connection Establishing Timeout is less than minimum value 100ms.",
  710. NULL
  711. );
  712. return EFI_INVALID_PARAMETER;
  713. }
  714. //
  715. // Validate the address configuration of the Initiator if DHCP isn't
  716. // deployed.
  717. //
  718. if (!Attempt->SessionConfigData.InitiatorInfoFromDhcp) {
  719. CopyMem (&HostIp.v4, &Attempt->SessionConfigData.LocalIp, sizeof (HostIp.v4));
  720. CopyMem (&SubnetMask.v4, &Attempt->SessionConfigData.SubnetMask, sizeof (SubnetMask.v4));
  721. CopyMem (&Gateway.v4, &Attempt->SessionConfigData.Gateway, sizeof (Gateway.v4));
  722. if ((Gateway.Addr[0] != 0)) {
  723. if (SubnetMask.Addr[0] == 0) {
  724. CreatePopUp (
  725. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  726. &Key,
  727. L"Gateway address is set but subnet mask is zero.",
  728. NULL
  729. );
  730. return EFI_INVALID_PARAMETER;
  731. } else if (!IP4_NET_EQUAL (HostIp.Addr[0], Gateway.Addr[0], SubnetMask.Addr[0])) {
  732. CreatePopUp (
  733. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  734. &Key,
  735. L"Local IP and Gateway are not in the same subnet.",
  736. NULL
  737. );
  738. return EFI_INVALID_PARAMETER;
  739. }
  740. }
  741. }
  742. //
  743. // Validate target configuration if DHCP isn't deployed.
  744. //
  745. if (!Attempt->SessionConfigData.TargetInfoFromDhcp && (Attempt->SessionConfigData.IpMode < IP_MODE_AUTOCONFIG)) {
  746. if (!Attempt->SessionConfigData.DnsMode) {
  747. if (!IpIsUnicast (&Attempt->SessionConfigData.TargetIp, IfrNvData->IpMode)) {
  748. CreatePopUp (
  749. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  750. &Key,
  751. L"Target IP is invalid!",
  752. NULL
  753. );
  754. return EFI_INVALID_PARAMETER;
  755. }
  756. } else {
  757. if (Attempt->SessionConfigData.TargetUrl[0] == '\0') {
  758. CreatePopUp (
  759. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  760. &Key,
  761. L"iSCSI target Url should not be NULL!",
  762. NULL
  763. );
  764. return EFI_INVALID_PARAMETER;
  765. }
  766. }
  767. //
  768. // Validate iSCSI target name configuration again:
  769. // The format of iSCSI target name is already verified in IScsiFormCallback() when
  770. // user input the name; here we only check the case user does not input the name.
  771. //
  772. if (Attempt->SessionConfigData.TargetName[0] == '\0') {
  773. CreatePopUp (
  774. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  775. &Key,
  776. L"iSCSI target name is NULL!",
  777. NULL
  778. );
  779. return EFI_INVALID_PARAMETER;
  780. }
  781. }
  782. //
  783. // Validate the authentication info.
  784. //
  785. if (IfrNvData->AuthenticationType == ISCSI_AUTH_TYPE_CHAP) {
  786. if ((IfrNvData->CHAPName[0] == '\0') || (IfrNvData->CHAPSecret[0] == '\0')) {
  787. CreatePopUp (
  788. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  789. &Key,
  790. L"CHAP Name or CHAP Secret is invalid!",
  791. NULL
  792. );
  793. return EFI_INVALID_PARAMETER;
  794. }
  795. if ((IfrNvData->CHAPType == ISCSI_CHAP_MUTUAL) &&
  796. ((IfrNvData->ReverseCHAPName[0] == '\0') || (IfrNvData->ReverseCHAPSecret[0] == '\0'))
  797. )
  798. {
  799. CreatePopUp (
  800. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  801. &Key,
  802. L"Reverse CHAP Name or Reverse CHAP Secret is invalid!",
  803. NULL
  804. );
  805. return EFI_INVALID_PARAMETER;
  806. }
  807. }
  808. //
  809. // Check whether this attempt uses NIC which is already used by existing attempt.
  810. //
  811. SameNicAttempt = IScsiConfigGetAttemptByNic (Attempt, IfrNvData->Enabled);
  812. if (SameNicAttempt != NULL) {
  813. AttemptName1 = (CHAR16 *)AllocateZeroPool (ATTEMPT_NAME_SIZE * sizeof (CHAR16));
  814. if (AttemptName1 == NULL) {
  815. return EFI_OUT_OF_RESOURCES;
  816. }
  817. AttemptName2 = (CHAR16 *)AllocateZeroPool (ATTEMPT_NAME_SIZE * sizeof (CHAR16));
  818. if (AttemptName2 == NULL) {
  819. FreePool (AttemptName1);
  820. return EFI_OUT_OF_RESOURCES;
  821. }
  822. AsciiStrToUnicodeStrS (Attempt->AttemptName, AttemptName1, ATTEMPT_NAME_SIZE);
  823. AsciiStrToUnicodeStrS (SameNicAttempt->AttemptName, AttemptName2, ATTEMPT_NAME_SIZE);
  824. UnicodeSPrint (
  825. mPrivate->PortString,
  826. (UINTN)ISCSI_NAME_IFR_MAX_SIZE,
  827. L"Warning! Attempt \"%s\" uses same NIC as Attempt \"%s\".",
  828. AttemptName1,
  829. AttemptName2
  830. );
  831. CreatePopUp (
  832. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  833. &Key,
  834. mPrivate->PortString,
  835. NULL
  836. );
  837. FreePool (AttemptName1);
  838. FreePool (AttemptName2);
  839. }
  840. }
  841. //
  842. // Update the iSCSI Mode data and record it in attempt help info.
  843. //
  844. if (IfrNvData->Enabled == ISCSI_DISABLED) {
  845. UnicodeSPrint (IScsiMode, 64, L"Disabled");
  846. } else if (IfrNvData->Enabled == ISCSI_ENABLED) {
  847. UnicodeSPrint (IScsiMode, 64, L"Enabled");
  848. } else if (IfrNvData->Enabled == ISCSI_ENABLED_FOR_MPIO) {
  849. UnicodeSPrint (IScsiMode, 64, L"Enabled for MPIO");
  850. }
  851. if (IfrNvData->IpMode == IP_MODE_IP4) {
  852. UnicodeSPrint (IpMode, 64, L"IP4");
  853. } else if (IfrNvData->IpMode == IP_MODE_IP6) {
  854. UnicodeSPrint (IpMode, 64, L"IP6");
  855. } else if (IfrNvData->IpMode == IP_MODE_AUTOCONFIG) {
  856. UnicodeSPrint (IpMode, 64, L"Autoconfigure");
  857. }
  858. NicInfo = IScsiGetNicInfoByIndex (Attempt->NicIndex);
  859. if (NicInfo == NULL) {
  860. return EFI_NOT_FOUND;
  861. }
  862. MacString = (CHAR16 *)AllocateZeroPool (ISCSI_MAX_MAC_STRING_LEN * sizeof (CHAR16));
  863. if (MacString == NULL) {
  864. return EFI_OUT_OF_RESOURCES;
  865. }
  866. AsciiStrToUnicodeStrS (Attempt->MacString, MacString, ISCSI_MAX_MAC_STRING_LEN);
  867. UnicodeSPrint (
  868. mPrivate->PortString,
  869. (UINTN)ISCSI_NAME_IFR_MAX_SIZE,
  870. L"MAC: %s, PFA: Bus %d | Dev %d | Func %d, iSCSI mode: %s, IP version: %s",
  871. MacString,
  872. NicInfo->BusNumber,
  873. NicInfo->DeviceNumber,
  874. NicInfo->FunctionNumber,
  875. IScsiMode,
  876. IpMode
  877. );
  878. Attempt->AttemptTitleHelpToken = HiiSetString (
  879. mCallbackInfo->RegisteredHandle,
  880. Attempt->AttemptTitleHelpToken,
  881. mPrivate->PortString,
  882. NULL
  883. );
  884. if (Attempt->AttemptTitleHelpToken == 0) {
  885. FreePool (MacString);
  886. return EFI_OUT_OF_RESOURCES;
  887. }
  888. //
  889. // Check whether this attempt is an existing one.
  890. //
  891. ExistAttempt = IScsiConfigGetAttemptByConfigIndex (Attempt->AttemptConfigIndex);
  892. if (ExistAttempt != NULL) {
  893. ASSERT (ExistAttempt == Attempt);
  894. if ((IfrNvData->Enabled == ISCSI_DISABLED) &&
  895. (Attempt->SessionConfigData.Enabled != ISCSI_DISABLED))
  896. {
  897. //
  898. // User updates the Attempt from "Enabled"/"Enabled for MPIO" to "Disabled".
  899. //
  900. if (Attempt->SessionConfigData.Enabled == ISCSI_ENABLED_FOR_MPIO) {
  901. if (mPrivate->MpioCount < 1) {
  902. return EFI_ABORTED;
  903. }
  904. if (--mPrivate->MpioCount == 0) {
  905. mPrivate->EnableMpio = FALSE;
  906. }
  907. } else if (Attempt->SessionConfigData.Enabled == ISCSI_ENABLED) {
  908. if (mPrivate->SinglePathCount < 1) {
  909. return EFI_ABORTED;
  910. }
  911. mPrivate->SinglePathCount--;
  912. }
  913. } else if ((IfrNvData->Enabled == ISCSI_ENABLED_FOR_MPIO) &&
  914. (Attempt->SessionConfigData.Enabled == ISCSI_ENABLED))
  915. {
  916. //
  917. // User updates the Attempt from "Enabled" to "Enabled for MPIO".
  918. //
  919. if (mPrivate->SinglePathCount < 1) {
  920. return EFI_ABORTED;
  921. }
  922. mPrivate->EnableMpio = TRUE;
  923. mPrivate->MpioCount++;
  924. mPrivate->SinglePathCount--;
  925. } else if ((IfrNvData->Enabled == ISCSI_ENABLED) &&
  926. (Attempt->SessionConfigData.Enabled == ISCSI_ENABLED_FOR_MPIO))
  927. {
  928. //
  929. // User updates the Attempt from "Enabled for MPIO" to "Enabled".
  930. //
  931. if (mPrivate->MpioCount < 1) {
  932. return EFI_ABORTED;
  933. }
  934. if (--mPrivate->MpioCount == 0) {
  935. mPrivate->EnableMpio = FALSE;
  936. }
  937. mPrivate->SinglePathCount++;
  938. } else if ((IfrNvData->Enabled != ISCSI_DISABLED) &&
  939. (Attempt->SessionConfigData.Enabled == ISCSI_DISABLED))
  940. {
  941. //
  942. // User updates the Attempt from "Disabled" to "Enabled"/"Enabled for MPIO".
  943. //
  944. if (IfrNvData->Enabled == ISCSI_ENABLED_FOR_MPIO) {
  945. mPrivate->EnableMpio = TRUE;
  946. mPrivate->MpioCount++;
  947. } else if (IfrNvData->Enabled == ISCSI_ENABLED) {
  948. mPrivate->SinglePathCount++;
  949. }
  950. }
  951. } else if (ExistAttempt == NULL) {
  952. //
  953. // When a new attempt is created, pointer of the attempt is saved to
  954. // mCallbackInfo->Current in IScsiConfigProcessDefault. If input Attempt
  955. // does not match any existing attempt, it should be a new created attempt.
  956. // Save it to system now.
  957. //
  958. //
  959. // Save current order number for this attempt.
  960. //
  961. AttemptConfigOrder = IScsiGetVariableAndSize (
  962. L"AttemptOrder",
  963. &gIScsiConfigGuid,
  964. &AttemptConfigOrderSize
  965. );
  966. TotalNumber = AttemptConfigOrderSize / sizeof (UINT8);
  967. TotalNumber++;
  968. //
  969. // Append the new created attempt order to the end.
  970. //
  971. AttemptOrderTmp = AllocateZeroPool (TotalNumber * sizeof (UINT8));
  972. if (AttemptOrderTmp == NULL) {
  973. if (AttemptConfigOrder != NULL) {
  974. FreePool (AttemptConfigOrder);
  975. }
  976. return EFI_OUT_OF_RESOURCES;
  977. }
  978. if (AttemptConfigOrder != NULL) {
  979. CopyMem (AttemptOrderTmp, AttemptConfigOrder, AttemptConfigOrderSize);
  980. FreePool (AttemptConfigOrder);
  981. }
  982. AttemptOrderTmp[TotalNumber - 1] = Attempt->AttemptConfigIndex;
  983. AttemptConfigOrder = AttemptOrderTmp;
  984. AttemptConfigOrderSize = TotalNumber * sizeof (UINT8);
  985. Status = gRT->SetVariable (
  986. L"AttemptOrder",
  987. &gIScsiConfigGuid,
  988. EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,
  989. AttemptConfigOrderSize,
  990. AttemptConfigOrder
  991. );
  992. FreePool (AttemptConfigOrder);
  993. if (EFI_ERROR (Status)) {
  994. return Status;
  995. }
  996. //
  997. // Insert new created attempt to array.
  998. //
  999. InsertTailList (&mPrivate->AttemptConfigs, &Attempt->Link);
  1000. mPrivate->AttemptCount++;
  1001. if (IfrNvData->Enabled == ISCSI_ENABLED_FOR_MPIO) {
  1002. //
  1003. // This new Attempt is enabled for MPIO; enable the multipath mode.
  1004. //
  1005. mPrivate->EnableMpio = TRUE;
  1006. mPrivate->MpioCount++;
  1007. } else if (IfrNvData->Enabled == ISCSI_ENABLED) {
  1008. mPrivate->SinglePathCount++;
  1009. }
  1010. IScsiConfigUpdateAttempt ();
  1011. }
  1012. Attempt->SessionConfigData.Enabled = IfrNvData->Enabled;
  1013. //
  1014. // Record the user configuration information in NVR.
  1015. //
  1016. UnicodeSPrint (mPrivate->PortString, (UINTN)ISCSI_NAME_IFR_MAX_SIZE, L"Attempt %d", Attempt->AttemptConfigIndex);
  1017. FreePool (MacString);
  1018. return gRT->SetVariable (
  1019. mPrivate->PortString,
  1020. &gEfiIScsiInitiatorNameProtocolGuid,
  1021. ISCSI_CONFIG_VAR_ATTR,
  1022. sizeof (ISCSI_ATTEMPT_CONFIG_NVDATA),
  1023. Attempt
  1024. );
  1025. }
  1026. /**
  1027. Convert the IFR data configured by keyword to iSCSI configuration data.
  1028. @param[in] IfrNvData Point to ISCSI_CONFIG_IFR_NVDATA.
  1029. @param[in] OffSet The offset of the variable to the configuration structure.
  1030. @retval EFI_INVALID_PARAMETER Any input or configured parameter is invalid.
  1031. @retval EFI_SUCCESS The operation is completed successfully.
  1032. **/
  1033. EFI_STATUS
  1034. IScsiConvertlfrNvDataToAttemptConfigDataByKeyword (
  1035. IN ISCSI_CONFIG_IFR_NVDATA *IfrNvData,
  1036. IN UINTN OffSet
  1037. )
  1038. {
  1039. ISCSI_ATTEMPT_CONFIG_NVDATA *Attempt;
  1040. UINT8 AttemptIndex;
  1041. UINT8 Index;
  1042. UINT8 ChapSecretLen;
  1043. UINT8 ReverseChapSecretLen;
  1044. CHAR16 *AttemptName1;
  1045. CHAR16 *AttemptName2;
  1046. ISCSI_ATTEMPT_CONFIG_NVDATA *SameNicAttempt;
  1047. CHAR8 LunString[ISCSI_LUN_STR_MAX_LEN];
  1048. CHAR8 IScsiName[ISCSI_NAME_MAX_SIZE];
  1049. CHAR8 IpString[IP_STR_MAX_SIZE];
  1050. EFI_IP_ADDRESS HostIp;
  1051. EFI_IP_ADDRESS SubnetMask;
  1052. EFI_IP_ADDRESS Gateway;
  1053. EFI_INPUT_KEY Key;
  1054. UINT64 Lun;
  1055. EFI_STATUS Status;
  1056. Attempt = NULL;
  1057. ZeroMem (IScsiName, sizeof (IScsiName));
  1058. if (OffSet < ATTEMPT_BOOTENABLE_VAR_OFFSET) {
  1059. return EFI_SUCCESS;
  1060. } else if ((OffSet >= ATTEMPT_BOOTENABLE_VAR_OFFSET) && (OffSet < ATTEMPT_ADDRESS_TYPE_VAR_OFFSET)) {
  1061. AttemptIndex = (UINT8)((OffSet - ATTEMPT_BOOTENABLE_VAR_OFFSET) + 1);
  1062. Attempt = IScsiConfigGetAttemptByConfigIndex (AttemptIndex);
  1063. if (Attempt == NULL) {
  1064. return EFI_INVALID_PARAMETER;
  1065. }
  1066. IfrNvData->Enabled = IfrNvData->ISCSIBootEnableList[AttemptIndex - 1];
  1067. //
  1068. // Validate the configuration of attempt.
  1069. //
  1070. if (IfrNvData->Enabled != ISCSI_DISABLED) {
  1071. //
  1072. // Check whether this attempt uses NIC which is already used by existing attempt.
  1073. //
  1074. SameNicAttempt = IScsiConfigGetAttemptByNic (Attempt, IfrNvData->Enabled);
  1075. if (SameNicAttempt != NULL) {
  1076. AttemptName1 = (CHAR16 *)AllocateZeroPool (ATTEMPT_NAME_SIZE * sizeof (CHAR16));
  1077. if (AttemptName1 == NULL) {
  1078. return EFI_OUT_OF_RESOURCES;
  1079. }
  1080. AttemptName2 = (CHAR16 *)AllocateZeroPool (ATTEMPT_NAME_SIZE * sizeof (CHAR16));
  1081. if (AttemptName2 == NULL) {
  1082. FreePool (AttemptName1);
  1083. return EFI_OUT_OF_RESOURCES;
  1084. }
  1085. AsciiStrToUnicodeStrS (Attempt->AttemptName, AttemptName1, ATTEMPT_NAME_SIZE);
  1086. AsciiStrToUnicodeStrS (SameNicAttempt->AttemptName, AttemptName2, ATTEMPT_NAME_SIZE);
  1087. UnicodeSPrint (
  1088. mPrivate->PortString,
  1089. (UINTN)ISCSI_NAME_IFR_MAX_SIZE,
  1090. L"Warning! \"%s\" uses same NIC as Attempt \"%s\".",
  1091. AttemptName1,
  1092. AttemptName2
  1093. );
  1094. CreatePopUp (
  1095. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  1096. &Key,
  1097. mPrivate->PortString,
  1098. NULL
  1099. );
  1100. FreePool (AttemptName1);
  1101. FreePool (AttemptName2);
  1102. }
  1103. }
  1104. if ((IfrNvData->Enabled == ISCSI_DISABLED) &&
  1105. (Attempt->SessionConfigData.Enabled != ISCSI_DISABLED))
  1106. {
  1107. //
  1108. // User updates the Attempt from "Enabled"/"Enabled for MPIO" to "Disabled".
  1109. //
  1110. if (Attempt->SessionConfigData.Enabled == ISCSI_ENABLED_FOR_MPIO) {
  1111. if (mPrivate->MpioCount < 1) {
  1112. return EFI_ABORTED;
  1113. }
  1114. if (--mPrivate->MpioCount == 0) {
  1115. mPrivate->EnableMpio = FALSE;
  1116. }
  1117. } else if (Attempt->SessionConfigData.Enabled == ISCSI_ENABLED) {
  1118. if (mPrivate->SinglePathCount < 1) {
  1119. return EFI_ABORTED;
  1120. }
  1121. mPrivate->SinglePathCount--;
  1122. }
  1123. } else if ((IfrNvData->Enabled == ISCSI_ENABLED_FOR_MPIO) &&
  1124. (Attempt->SessionConfigData.Enabled == ISCSI_ENABLED))
  1125. {
  1126. //
  1127. // User updates the Attempt from "Enabled" to "Enabled for MPIO".
  1128. //
  1129. if (mPrivate->SinglePathCount < 1) {
  1130. return EFI_ABORTED;
  1131. }
  1132. mPrivate->EnableMpio = TRUE;
  1133. mPrivate->MpioCount++;
  1134. mPrivate->SinglePathCount--;
  1135. } else if ((IfrNvData->Enabled == ISCSI_ENABLED) &&
  1136. (Attempt->SessionConfigData.Enabled == ISCSI_ENABLED_FOR_MPIO))
  1137. {
  1138. //
  1139. // User updates the Attempt from "Enabled for MPIO" to "Enabled".
  1140. //
  1141. if (mPrivate->MpioCount < 1) {
  1142. return EFI_ABORTED;
  1143. }
  1144. if (--mPrivate->MpioCount == 0) {
  1145. mPrivate->EnableMpio = FALSE;
  1146. }
  1147. mPrivate->SinglePathCount++;
  1148. } else if ((IfrNvData->Enabled != ISCSI_DISABLED) &&
  1149. (Attempt->SessionConfigData.Enabled == ISCSI_DISABLED))
  1150. {
  1151. //
  1152. // User updates the Attempt from "Disabled" to "Enabled"/"Enabled for MPIO".
  1153. //
  1154. if (IfrNvData->Enabled == ISCSI_ENABLED_FOR_MPIO) {
  1155. mPrivate->EnableMpio = TRUE;
  1156. mPrivate->MpioCount++;
  1157. } else if (IfrNvData->Enabled == ISCSI_ENABLED) {
  1158. mPrivate->SinglePathCount++;
  1159. }
  1160. }
  1161. Attempt->SessionConfigData.Enabled = IfrNvData->Enabled;
  1162. } else if ((OffSet >= ATTEMPT_ADDRESS_TYPE_VAR_OFFSET) && (OffSet < ATTEMPT_CONNECT_RETRY_VAR_OFFSET)) {
  1163. AttemptIndex = (UINT8)((OffSet - ATTEMPT_ADDRESS_TYPE_VAR_OFFSET) + 1);
  1164. Attempt = IScsiConfigGetAttemptByConfigIndex (AttemptIndex);
  1165. if (Attempt == NULL) {
  1166. return EFI_INVALID_PARAMETER;
  1167. }
  1168. Attempt->SessionConfigData.IpMode = IfrNvData->ISCSIIpAddressTypeList[AttemptIndex - 1];
  1169. if (Attempt->SessionConfigData.IpMode < IP_MODE_AUTOCONFIG) {
  1170. Attempt->AutoConfigureMode = 0;
  1171. }
  1172. } else if ((OffSet >= ATTEMPT_CONNECT_RETRY_VAR_OFFSET) && (OffSet < ATTEMPT_CONNECT_TIMEOUT_VAR_OFFSET)) {
  1173. AttemptIndex = (UINT8)((OffSet - ATTEMPT_CONNECT_RETRY_VAR_OFFSET) + 1);
  1174. Attempt = IScsiConfigGetAttemptByConfigIndex (AttemptIndex);
  1175. if (Attempt == NULL) {
  1176. return EFI_INVALID_PARAMETER;
  1177. }
  1178. if (IfrNvData->ISCSIConnectRetry[AttemptIndex - 1] > CONNECT_MAX_RETRY) {
  1179. CreatePopUp (
  1180. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  1181. &Key,
  1182. L"The minimum value is 0 and the maximum is 16. 0 means no retry.",
  1183. NULL
  1184. );
  1185. return EFI_INVALID_PARAMETER;
  1186. }
  1187. Attempt->SessionConfigData.ConnectRetryCount = IfrNvData->ISCSIConnectRetry[AttemptIndex - 1];
  1188. } else if ((OffSet >= ATTEMPT_CONNECT_TIMEOUT_VAR_OFFSET) && (OffSet < ATTEMPT_INITIATOR_VIA_DHCP_VAR_OFFSET)) {
  1189. AttemptIndex = (UINT8)((OffSet - ATTEMPT_CONNECT_TIMEOUT_VAR_OFFSET) / 2 + 1);
  1190. Attempt = IScsiConfigGetAttemptByConfigIndex (AttemptIndex);
  1191. if (Attempt == NULL) {
  1192. return EFI_INVALID_PARAMETER;
  1193. }
  1194. if ((IfrNvData->ISCSIConnectTimeout[AttemptIndex - 1] < CONNECT_MIN_TIMEOUT) ||
  1195. (IfrNvData->ISCSIConnectTimeout[AttemptIndex - 1] > CONNECT_MAX_TIMEOUT))
  1196. {
  1197. CreatePopUp (
  1198. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  1199. &Key,
  1200. L"The minimum value is 100 milliseconds and the maximum is 20 seconds.",
  1201. NULL
  1202. );
  1203. return EFI_INVALID_PARAMETER;
  1204. }
  1205. Attempt->SessionConfigData.ConnectTimeout = IfrNvData->ISCSIConnectTimeout[AttemptIndex - 1];
  1206. if (Attempt->SessionConfigData.ConnectTimeout == 0) {
  1207. Attempt->SessionConfigData.ConnectTimeout = CONNECT_DEFAULT_TIMEOUT;
  1208. }
  1209. } else if ((OffSet >= ATTEMPT_INITIATOR_VIA_DHCP_VAR_OFFSET) && (OffSet < ATTEMPT_TARGET_VIA_DHCP_VAR_OFFSET)) {
  1210. AttemptIndex = (UINT8)((OffSet - ATTEMPT_INITIATOR_VIA_DHCP_VAR_OFFSET) + 1);
  1211. Attempt = IScsiConfigGetAttemptByConfigIndex (AttemptIndex);
  1212. if (Attempt == NULL) {
  1213. return EFI_INVALID_PARAMETER;
  1214. }
  1215. Attempt->SessionConfigData.InitiatorInfoFromDhcp = IfrNvData->ISCSIInitiatorInfoViaDHCP[AttemptIndex - 1];
  1216. } else if ((OffSet >= ATTEMPT_TARGET_VIA_DHCP_VAR_OFFSET) && (OffSet < ATTEMPT_TARGET_TCP_PORT_VAR_OFFSET)) {
  1217. AttemptIndex = (UINT8)((OffSet - ATTEMPT_TARGET_VIA_DHCP_VAR_OFFSET) + 1);
  1218. Attempt = IScsiConfigGetAttemptByConfigIndex (AttemptIndex);
  1219. if (Attempt == NULL) {
  1220. return EFI_INVALID_PARAMETER;
  1221. }
  1222. if ((Attempt->SessionConfigData.IpMode < IP_MODE_AUTOCONFIG) && (Attempt->SessionConfigData.InitiatorInfoFromDhcp)) {
  1223. Attempt->SessionConfigData.TargetInfoFromDhcp = IfrNvData->ISCSITargetInfoViaDHCP[AttemptIndex - 1];
  1224. } else {
  1225. CreatePopUp (
  1226. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  1227. &Key,
  1228. L"Invalid Configuration, Check value of IpMode or Enable DHCP!",
  1229. NULL
  1230. );
  1231. return EFI_INVALID_PARAMETER;
  1232. }
  1233. } else if ((OffSet >= ATTEMPT_TARGET_TCP_PORT_VAR_OFFSET) && (OffSet < ATTEMPT_AUTHENTICATION_METHOD_VAR_OFFSET)) {
  1234. AttemptIndex = (UINT8)((OffSet - ATTEMPT_TARGET_TCP_PORT_VAR_OFFSET) / 2 + 1);
  1235. Attempt = IScsiConfigGetAttemptByConfigIndex (AttemptIndex);
  1236. if (Attempt == NULL) {
  1237. return EFI_INVALID_PARAMETER;
  1238. }
  1239. if ((Attempt->SessionConfigData.IpMode < IP_MODE_AUTOCONFIG) && (!Attempt->SessionConfigData.TargetInfoFromDhcp)) {
  1240. Attempt->SessionConfigData.TargetPort = IfrNvData->ISCSITargetTcpPort[AttemptIndex - 1];
  1241. if (Attempt->SessionConfigData.TargetPort == 0) {
  1242. Attempt->SessionConfigData.TargetPort = ISCSI_WELL_KNOWN_PORT;
  1243. }
  1244. } else {
  1245. CreatePopUp (
  1246. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  1247. &Key,
  1248. L"Invalid Configuration, Check value of IpMode or Target Via DHCP!",
  1249. NULL
  1250. );
  1251. return EFI_INVALID_PARAMETER;
  1252. }
  1253. } else if ((OffSet >= ATTEMPT_AUTHENTICATION_METHOD_VAR_OFFSET) && (OffSet < ATTEMPT_CHARTYPE_VAR_OFFSET)) {
  1254. AttemptIndex = (UINT8)((OffSet - ATTEMPT_AUTHENTICATION_METHOD_VAR_OFFSET) + 1);
  1255. Attempt = IScsiConfigGetAttemptByConfigIndex (AttemptIndex);
  1256. if (Attempt == NULL) {
  1257. return EFI_INVALID_PARAMETER;
  1258. }
  1259. Attempt->AuthenticationType = IfrNvData->ISCSIAuthenticationMethod[AttemptIndex - 1];
  1260. } else if ((OffSet >= ATTEMPT_CHARTYPE_VAR_OFFSET) && (OffSet < ATTEMPT_ISID_VAR_OFFSET)) {
  1261. AttemptIndex = (UINT8)((OffSet - ATTEMPT_CHARTYPE_VAR_OFFSET) + 1);
  1262. Attempt = IScsiConfigGetAttemptByConfigIndex (AttemptIndex);
  1263. if (Attempt == NULL) {
  1264. return EFI_INVALID_PARAMETER;
  1265. }
  1266. if (Attempt->AuthenticationType == ISCSI_AUTH_TYPE_CHAP) {
  1267. Attempt->AuthConfigData.CHAP.CHAPType = IfrNvData->ISCSIChapType[AttemptIndex - 1];
  1268. }
  1269. } else if (OffSet >= ATTEMPT_ISID_VAR_OFFSET) {
  1270. Index = (UINT8)((OffSet - ATTEMPT_ISID_VAR_OFFSET) / sizeof (KEYWORD_STR));
  1271. AttemptIndex = Index + 1;
  1272. Attempt = IScsiConfigGetAttemptByConfigIndex (AttemptIndex);
  1273. if (Attempt == NULL) {
  1274. return EFI_INVALID_PARAMETER;
  1275. }
  1276. OffSet = OffSet - Index * sizeof (KEYWORD_STR);
  1277. if ((OffSet >= ATTEMPT_ISID_VAR_OFFSET) && (OffSet < ATTEMPT_INITIATOR_IP_ADDRESS_VAR_OFFSET)) {
  1278. IScsiParseIsIdFromString (IfrNvData->Keyword[Index].ISCSIIsId, Attempt->SessionConfigData.IsId);
  1279. } else if ((OffSet >= ATTEMPT_INITIATOR_IP_ADDRESS_VAR_OFFSET) && (OffSet < ATTEMPT_INITIATOR_NET_MASK_VAR_OFFSET)) {
  1280. if ((Attempt->SessionConfigData.IpMode == IP_MODE_IP4) && (!Attempt->SessionConfigData.InitiatorInfoFromDhcp)) {
  1281. //
  1282. // Config Local ip
  1283. //
  1284. Status = NetLibStrToIp4 (IfrNvData->Keyword[Index].ISCSIInitiatorIpAddress, &HostIp.v4);
  1285. if (EFI_ERROR (Status) || ((Attempt->SessionConfigData.SubnetMask.Addr[0] != 0) &&
  1286. !NetIp4IsUnicast (NTOHL (HostIp.Addr[0]), NTOHL (*(UINT32 *)Attempt->SessionConfigData.SubnetMask.Addr))))
  1287. {
  1288. CreatePopUp (
  1289. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  1290. &Key,
  1291. L"Invalid IP address!",
  1292. NULL
  1293. );
  1294. return EFI_INVALID_PARAMETER;
  1295. } else {
  1296. CopyMem (&Attempt->SessionConfigData.LocalIp, &HostIp.v4, sizeof (HostIp.v4));
  1297. }
  1298. } else {
  1299. CreatePopUp (
  1300. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  1301. &Key,
  1302. L"Invalid Configuration, Check value of IpMode or Enable DHCP!",
  1303. NULL
  1304. );
  1305. return EFI_INVALID_PARAMETER;
  1306. }
  1307. } else if ((OffSet >= ATTEMPT_INITIATOR_NET_MASK_VAR_OFFSET) && (OffSet < ATTEMPT_INITIATOR_GATE_WAY_VAR_OFFSET)) {
  1308. if ((Attempt->SessionConfigData.IpMode == IP_MODE_IP4) && (!Attempt->SessionConfigData.InitiatorInfoFromDhcp)) {
  1309. Status = NetLibStrToIp4 (IfrNvData->Keyword[Index].ISCSIInitiatorNetmask, &SubnetMask.v4);
  1310. if (EFI_ERROR (Status) || ((SubnetMask.Addr[0] != 0) && (IScsiGetSubnetMaskPrefixLength (&SubnetMask.v4) == 0))) {
  1311. CreatePopUp (
  1312. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  1313. &Key,
  1314. L"Invalid Subnet Mask!",
  1315. NULL
  1316. );
  1317. return EFI_INVALID_PARAMETER;
  1318. } else {
  1319. CopyMem (&Attempt->SessionConfigData.SubnetMask, &SubnetMask.v4, sizeof (SubnetMask.v4));
  1320. }
  1321. } else {
  1322. CreatePopUp (
  1323. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  1324. &Key,
  1325. L"Invalid Configuration, Check value of IpMode or Enable DHCP!",
  1326. NULL
  1327. );
  1328. return EFI_INVALID_PARAMETER;
  1329. }
  1330. } else if ((OffSet >= ATTEMPT_INITIATOR_GATE_WAY_VAR_OFFSET) && (OffSet < ATTEMPT_TARGET_NAME_VAR_OFFSET)) {
  1331. if ((Attempt->SessionConfigData.IpMode == IP_MODE_IP4) && (!Attempt->SessionConfigData.InitiatorInfoFromDhcp)) {
  1332. Status = NetLibStrToIp4 (IfrNvData->Keyword[Index].ISCSIInitiatorGateway, &Gateway.v4);
  1333. if (EFI_ERROR (Status) ||
  1334. ((Gateway.Addr[0] != 0) && (Attempt->SessionConfigData.SubnetMask.Addr[0] != 0) &&
  1335. !NetIp4IsUnicast (NTOHL (Gateway.Addr[0]), NTOHL (*(UINT32 *)Attempt->SessionConfigData.SubnetMask.Addr))))
  1336. {
  1337. CreatePopUp (
  1338. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  1339. &Key,
  1340. L"Invalid Gateway!",
  1341. NULL
  1342. );
  1343. return EFI_INVALID_PARAMETER;
  1344. } else {
  1345. CopyMem (&Attempt->SessionConfigData.Gateway, &Gateway.v4, sizeof (Gateway.v4));
  1346. }
  1347. } else {
  1348. CreatePopUp (
  1349. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  1350. &Key,
  1351. L"Invalid Configuration, Check value of IpMode or Enable DHCP!",
  1352. NULL
  1353. );
  1354. return EFI_INVALID_PARAMETER;
  1355. }
  1356. } else if ((OffSet >= ATTEMPT_TARGET_NAME_VAR_OFFSET) && (OffSet < ATTEMPT_TARGET_IP_ADDRESS_VAR_OFFSET)) {
  1357. if ((Attempt->SessionConfigData.IpMode < IP_MODE_AUTOCONFIG) && (!Attempt->SessionConfigData.TargetInfoFromDhcp)) {
  1358. UnicodeStrToAsciiStrS (IfrNvData->Keyword[Index].ISCSITargetName, IScsiName, ISCSI_NAME_MAX_SIZE);
  1359. Status = IScsiNormalizeName (IScsiName, AsciiStrLen (IScsiName));
  1360. if (EFI_ERROR (Status)) {
  1361. CreatePopUp (
  1362. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  1363. &Key,
  1364. L"Invalid iSCSI Name!",
  1365. NULL
  1366. );
  1367. } else {
  1368. AsciiStrCpyS (Attempt->SessionConfigData.TargetName, ISCSI_NAME_MAX_SIZE, IScsiName);
  1369. }
  1370. if (Attempt->SessionConfigData.Enabled != ISCSI_DISABLED) {
  1371. if (Attempt->SessionConfigData.TargetName[0] == L'\0') {
  1372. CreatePopUp (
  1373. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  1374. &Key,
  1375. L"iSCSI target name is NULL!",
  1376. NULL
  1377. );
  1378. return EFI_INVALID_PARAMETER;
  1379. }
  1380. }
  1381. } else {
  1382. CreatePopUp (
  1383. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  1384. &Key,
  1385. L"Invalid Configuration, Check value of IpMode or Target Via DHCP!",
  1386. NULL
  1387. );
  1388. return EFI_INVALID_PARAMETER;
  1389. }
  1390. } else if ((OffSet >= ATTEMPT_TARGET_IP_ADDRESS_VAR_OFFSET) && (OffSet < ATTEMPT_LUN_VAR_OFFSET)) {
  1391. if ((Attempt->SessionConfigData.IpMode < IP_MODE_AUTOCONFIG) && (!Attempt->SessionConfigData.TargetInfoFromDhcp)) {
  1392. UnicodeStrToAsciiStrS (IfrNvData->Keyword[Index].ISCSITargetIpAddress, IpString, sizeof (IpString));
  1393. Status = IScsiAsciiStrToIp (IpString, Attempt->SessionConfigData.IpMode, &HostIp);
  1394. if (EFI_ERROR (Status) || !IpIsUnicast (&HostIp, Attempt->SessionConfigData.IpMode)) {
  1395. Attempt->SessionConfigData.DnsMode = TRUE;
  1396. ZeroMem (&Attempt->SessionConfigData.TargetIp, sizeof (Attempt->SessionConfigData.TargetIp));
  1397. UnicodeStrToAsciiStrS (IfrNvData->Keyword[Index].ISCSITargetIpAddress, Attempt->SessionConfigData.TargetUrl, ISCSI_NAME_MAX_SIZE);
  1398. } else {
  1399. Attempt->SessionConfigData.DnsMode = FALSE;
  1400. CopyMem (&Attempt->SessionConfigData.TargetIp, &HostIp, sizeof (HostIp));
  1401. }
  1402. } else {
  1403. CreatePopUp (
  1404. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  1405. &Key,
  1406. L"Invalid Configuration, Check value of IpMode or Target Via DHCP!",
  1407. NULL
  1408. );
  1409. return EFI_INVALID_PARAMETER;
  1410. }
  1411. } else if ((OffSet >= ATTEMPT_LUN_VAR_OFFSET) && (OffSet < ATTEMPT_CHAR_USER_NAME_VAR_OFFSET)) {
  1412. if ((Attempt->SessionConfigData.IpMode < IP_MODE_AUTOCONFIG) && (Attempt->SessionConfigData.TargetInfoFromDhcp == 0)) {
  1413. //
  1414. // Config LUN.
  1415. //
  1416. UnicodeStrToAsciiStrS (IfrNvData->Keyword[Index].ISCSILun, LunString, ISCSI_LUN_STR_MAX_LEN);
  1417. Status = IScsiAsciiStrToLun (LunString, (UINT8 *)&Lun);
  1418. if (EFI_ERROR (Status)) {
  1419. CreatePopUp (
  1420. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  1421. &Key,
  1422. L"Invalid LUN string, Examples are: 4752-3A4F-6b7e-2F99, 6734-9-156f-127, 4186-9!",
  1423. NULL
  1424. );
  1425. } else {
  1426. CopyMem (&Attempt->SessionConfigData.BootLun, &Lun, sizeof (Lun));
  1427. }
  1428. } else {
  1429. CreatePopUp (
  1430. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  1431. &Key,
  1432. L"Invalid Configuration, Check value of IpMode or Target Via DHCP!",
  1433. NULL
  1434. );
  1435. return EFI_INVALID_PARAMETER;
  1436. }
  1437. } else if ((OffSet >= ATTEMPT_CHAR_USER_NAME_VAR_OFFSET) && (OffSet < ATTEMPT_CHAR_SECRET_VAR_OFFSET)) {
  1438. if (Attempt->AuthenticationType == ISCSI_AUTH_TYPE_CHAP) {
  1439. UnicodeStrToAsciiStrS (
  1440. IfrNvData->Keyword[Index].ISCSIChapUsername,
  1441. Attempt->AuthConfigData.CHAP.CHAPName,
  1442. ISCSI_CHAP_NAME_STORAGE
  1443. );
  1444. if (Attempt->SessionConfigData.Enabled != ISCSI_DISABLED) {
  1445. if (IfrNvData->Keyword[Index].ISCSIChapUsername[0] == L'\0') {
  1446. CreatePopUp (
  1447. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  1448. &Key,
  1449. L"CHAP Name is invalid!",
  1450. NULL
  1451. );
  1452. return EFI_INVALID_PARAMETER;
  1453. }
  1454. }
  1455. } else {
  1456. CreatePopUp (
  1457. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  1458. &Key,
  1459. L"Invalid Configuration, Check value of AuthenticationType!",
  1460. NULL
  1461. );
  1462. return EFI_INVALID_PARAMETER;
  1463. }
  1464. } else if ((OffSet >= ATTEMPT_CHAR_SECRET_VAR_OFFSET) && (OffSet < ATTEMPT_CHAR_REVERSE_USER_NAME_VAR_OFFSET)) {
  1465. if (Attempt->AuthenticationType == ISCSI_AUTH_TYPE_CHAP) {
  1466. ChapSecretLen = (UINT8)StrLen (IfrNvData->Keyword[Index].ISCSIChapSecret);
  1467. UnicodeStrToAsciiStrS (
  1468. IfrNvData->Keyword[Index].ISCSIChapSecret,
  1469. Attempt->AuthConfigData.CHAP.CHAPSecret,
  1470. ISCSI_CHAP_SECRET_STORAGE
  1471. );
  1472. if (Attempt->SessionConfigData.Enabled != ISCSI_DISABLED) {
  1473. if ((ChapSecretLen < ISCSI_CHAP_SECRET_MIN_LEN) || (ChapSecretLen > ISCSI_CHAP_SECRET_MAX_LEN)) {
  1474. CreatePopUp (
  1475. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  1476. &Key,
  1477. L"The Chap Secret minimum length is 12 bytes and the maximum length is 16 bytes.",
  1478. NULL
  1479. );
  1480. return EFI_INVALID_PARAMETER;
  1481. }
  1482. }
  1483. } else {
  1484. CreatePopUp (
  1485. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  1486. &Key,
  1487. L"Invalid Configuration, Check value of AuthenticationType!",
  1488. NULL
  1489. );
  1490. return EFI_INVALID_PARAMETER;
  1491. }
  1492. } else if ((OffSet >= ATTEMPT_CHAR_REVERSE_USER_NAME_VAR_OFFSET) && (OffSet < ATTEMPT_CHAR_REVERSE_SECRET_VAR_OFFSET)) {
  1493. if (Attempt->AuthConfigData.CHAP.CHAPType == ISCSI_CHAP_MUTUAL) {
  1494. UnicodeStrToAsciiStrS (
  1495. IfrNvData->Keyword[Index].ISCSIReverseChapUsername,
  1496. Attempt->AuthConfigData.CHAP.ReverseCHAPName,
  1497. ISCSI_CHAP_NAME_STORAGE
  1498. );
  1499. if (Attempt->SessionConfigData.Enabled != ISCSI_DISABLED) {
  1500. if (IfrNvData->Keyword[Index].ISCSIReverseChapUsername[0] == L'\0') {
  1501. CreatePopUp (
  1502. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  1503. &Key,
  1504. L"Reverse CHAP Name is invalid!",
  1505. NULL
  1506. );
  1507. return EFI_INVALID_PARAMETER;
  1508. }
  1509. }
  1510. } else {
  1511. CreatePopUp (
  1512. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  1513. &Key,
  1514. L"Invalid Configuration, Check value of AuthenticationType or Chap Type!",
  1515. NULL
  1516. );
  1517. return EFI_INVALID_PARAMETER;
  1518. }
  1519. } else if (OffSet >= ATTEMPT_CHAR_REVERSE_SECRET_VAR_OFFSET) {
  1520. if (Attempt->AuthConfigData.CHAP.CHAPType == ISCSI_CHAP_MUTUAL) {
  1521. ReverseChapSecretLen = (UINT8)StrLen (IfrNvData->Keyword[Index].ISCSIReverseChapSecret);
  1522. UnicodeStrToAsciiStrS (
  1523. IfrNvData->Keyword[Index].ISCSIReverseChapSecret,
  1524. Attempt->AuthConfigData.CHAP.ReverseCHAPSecret,
  1525. ISCSI_CHAP_SECRET_STORAGE
  1526. );
  1527. if (Attempt->SessionConfigData.Enabled != ISCSI_DISABLED) {
  1528. if ((ReverseChapSecretLen < ISCSI_CHAP_SECRET_MIN_LEN) || (ReverseChapSecretLen > ISCSI_CHAP_SECRET_MAX_LEN)) {
  1529. CreatePopUp (
  1530. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  1531. &Key,
  1532. L"The Reverse CHAP Secret minimum length is 12 bytes and the maximum length is 16 bytes.",
  1533. NULL
  1534. );
  1535. return EFI_INVALID_PARAMETER;
  1536. }
  1537. }
  1538. } else {
  1539. CreatePopUp (
  1540. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  1541. &Key,
  1542. L"Invalid Configuration, Check value of AuthenticationType or Chap Type!",
  1543. NULL
  1544. );
  1545. return EFI_INVALID_PARAMETER;
  1546. }
  1547. }
  1548. }
  1549. //
  1550. // Record the user configuration information in NVR.
  1551. //
  1552. ASSERT (Attempt != NULL);
  1553. UnicodeSPrint (mPrivate->PortString, (UINTN)ISCSI_NAME_IFR_MAX_SIZE, L"Attempt %d", Attempt->AttemptConfigIndex);
  1554. return gRT->SetVariable (
  1555. mPrivate->PortString,
  1556. &gEfiIScsiInitiatorNameProtocolGuid,
  1557. ISCSI_CONFIG_VAR_ATTR,
  1558. sizeof (ISCSI_ATTEMPT_CONFIG_NVDATA),
  1559. Attempt
  1560. );
  1561. }
  1562. /**
  1563. Create Hii Extend Label OpCode as the start opcode and end opcode. It is
  1564. a help function.
  1565. @param[in] StartLabelNumber The number of start label.
  1566. @param[out] StartOpCodeHandle Points to the start opcode handle.
  1567. @param[out] StartLabel Points to the created start opcode.
  1568. @param[out] EndOpCodeHandle Points to the end opcode handle.
  1569. @param[out] EndLabel Points to the created end opcode.
  1570. @retval EFI_OUT_OF_RESOURCES Do not have sufficient resource to finish this
  1571. operation.
  1572. @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
  1573. @retval EFI_SUCCESS The operation is completed successfully.
  1574. **/
  1575. EFI_STATUS
  1576. IScsiCreateOpCode (
  1577. IN UINT16 StartLabelNumber,
  1578. OUT VOID **StartOpCodeHandle,
  1579. OUT EFI_IFR_GUID_LABEL **StartLabel,
  1580. OUT VOID **EndOpCodeHandle,
  1581. OUT EFI_IFR_GUID_LABEL **EndLabel
  1582. )
  1583. {
  1584. EFI_STATUS Status;
  1585. EFI_IFR_GUID_LABEL *InternalStartLabel;
  1586. EFI_IFR_GUID_LABEL *InternalEndLabel;
  1587. if ((StartOpCodeHandle == NULL) || (StartLabel == NULL) || (EndOpCodeHandle == NULL) || (EndLabel == NULL)) {
  1588. return EFI_INVALID_PARAMETER;
  1589. }
  1590. *StartOpCodeHandle = NULL;
  1591. *EndOpCodeHandle = NULL;
  1592. Status = EFI_OUT_OF_RESOURCES;
  1593. //
  1594. // Initialize the container for dynamic opcodes.
  1595. //
  1596. *StartOpCodeHandle = HiiAllocateOpCodeHandle ();
  1597. if (*StartOpCodeHandle == NULL) {
  1598. return Status;
  1599. }
  1600. *EndOpCodeHandle = HiiAllocateOpCodeHandle ();
  1601. if (*EndOpCodeHandle == NULL) {
  1602. goto Exit;
  1603. }
  1604. //
  1605. // Create Hii Extend Label OpCode as the start opcode.
  1606. //
  1607. InternalStartLabel = (EFI_IFR_GUID_LABEL *)HiiCreateGuidOpCode (
  1608. *StartOpCodeHandle,
  1609. &gEfiIfrTianoGuid,
  1610. NULL,
  1611. sizeof (EFI_IFR_GUID_LABEL)
  1612. );
  1613. if (InternalStartLabel == NULL) {
  1614. goto Exit;
  1615. }
  1616. InternalStartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
  1617. InternalStartLabel->Number = StartLabelNumber;
  1618. //
  1619. // Create Hii Extend Label OpCode as the end opcode.
  1620. //
  1621. InternalEndLabel = (EFI_IFR_GUID_LABEL *)HiiCreateGuidOpCode (
  1622. *EndOpCodeHandle,
  1623. &gEfiIfrTianoGuid,
  1624. NULL,
  1625. sizeof (EFI_IFR_GUID_LABEL)
  1626. );
  1627. if (InternalEndLabel == NULL) {
  1628. goto Exit;
  1629. }
  1630. InternalEndLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;
  1631. InternalEndLabel->Number = LABEL_END;
  1632. *StartLabel = InternalStartLabel;
  1633. *EndLabel = InternalEndLabel;
  1634. return EFI_SUCCESS;
  1635. Exit:
  1636. if (*StartOpCodeHandle != NULL) {
  1637. HiiFreeOpCodeHandle (*StartOpCodeHandle);
  1638. }
  1639. if (*EndOpCodeHandle != NULL) {
  1640. HiiFreeOpCodeHandle (*EndOpCodeHandle);
  1641. }
  1642. return Status;
  1643. }
  1644. /**
  1645. Update the MAIN form to display the configured attempts.
  1646. **/
  1647. VOID
  1648. IScsiConfigUpdateAttempt (
  1649. VOID
  1650. )
  1651. {
  1652. LIST_ENTRY *Entry;
  1653. ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData;
  1654. VOID *StartOpCodeHandle;
  1655. EFI_IFR_GUID_LABEL *StartLabel;
  1656. VOID *EndOpCodeHandle;
  1657. EFI_IFR_GUID_LABEL *EndLabel;
  1658. EFI_STATUS Status;
  1659. Status = IScsiCreateOpCode (
  1660. ATTEMPT_ENTRY_LABEL,
  1661. &StartOpCodeHandle,
  1662. &StartLabel,
  1663. &EndOpCodeHandle,
  1664. &EndLabel
  1665. );
  1666. if (EFI_ERROR (Status)) {
  1667. return;
  1668. }
  1669. NET_LIST_FOR_EACH (Entry, &mPrivate->AttemptConfigs) {
  1670. AttemptConfigData = NET_LIST_USER_STRUCT (Entry, ISCSI_ATTEMPT_CONFIG_NVDATA, Link);
  1671. if (AttemptConfigData->Actived == ISCSI_ACTIVE_ENABLED) {
  1672. //
  1673. // Update Attempt Help Info.
  1674. //
  1675. UnicodeSPrint (mPrivate->PortString, (UINTN)ISCSI_NAME_IFR_MAX_SIZE, L"Attempt %d", (UINTN)AttemptConfigData->AttemptConfigIndex);
  1676. AttemptConfigData->AttemptTitleToken = HiiSetString (
  1677. mCallbackInfo->RegisteredHandle,
  1678. 0,
  1679. mPrivate->PortString,
  1680. NULL
  1681. );
  1682. if (AttemptConfigData->AttemptTitleToken == 0) {
  1683. return;
  1684. }
  1685. HiiCreateGotoOpCode (
  1686. StartOpCodeHandle, // Container for dynamic created opcodes
  1687. FORMID_ATTEMPT_FORM, // Form ID
  1688. AttemptConfigData->AttemptTitleToken, // Prompt text
  1689. AttemptConfigData->AttemptTitleHelpToken, // Help text
  1690. EFI_IFR_FLAG_CALLBACK, // Question flag
  1691. (UINT16)(KEY_ATTEMPT_ENTRY_BASE + AttemptConfigData->AttemptConfigIndex) // Question ID
  1692. );
  1693. }
  1694. }
  1695. HiiUpdateForm (
  1696. mCallbackInfo->RegisteredHandle, // HII handle
  1697. &gIScsiConfigGuid, // Formset GUID
  1698. FORMID_MAIN_FORM, // Form ID
  1699. StartOpCodeHandle, // Label for where to insert opcodes
  1700. EndOpCodeHandle // Replace data
  1701. );
  1702. HiiFreeOpCodeHandle (StartOpCodeHandle);
  1703. HiiFreeOpCodeHandle (EndOpCodeHandle);
  1704. }
  1705. /**
  1706. Callback function when user presses "Add an Attempt".
  1707. @retval EFI_OUT_OF_RESOURCES Does not have sufficient resources to finish this
  1708. operation.
  1709. @retval EFI_SUCCESS The operation is completed successfully.
  1710. **/
  1711. EFI_STATUS
  1712. IScsiConfigAddAttempt (
  1713. VOID
  1714. )
  1715. {
  1716. LIST_ENTRY *Entry;
  1717. ISCSI_NIC_INFO *NicInfo;
  1718. EFI_STRING_ID PortTitleToken;
  1719. EFI_STRING_ID PortTitleHelpToken;
  1720. CHAR16 MacString[ISCSI_MAX_MAC_STRING_LEN];
  1721. EFI_STATUS Status;
  1722. VOID *StartOpCodeHandle;
  1723. EFI_IFR_GUID_LABEL *StartLabel;
  1724. VOID *EndOpCodeHandle;
  1725. EFI_IFR_GUID_LABEL *EndLabel;
  1726. Status = IScsiCreateOpCode (
  1727. MAC_ENTRY_LABEL,
  1728. &StartOpCodeHandle,
  1729. &StartLabel,
  1730. &EndOpCodeHandle,
  1731. &EndLabel
  1732. );
  1733. if (EFI_ERROR (Status)) {
  1734. return Status;
  1735. }
  1736. //
  1737. // Ask user to select a MAC for this attempt.
  1738. //
  1739. NET_LIST_FOR_EACH (Entry, &mPrivate->NicInfoList) {
  1740. NicInfo = NET_LIST_USER_STRUCT (Entry, ISCSI_NIC_INFO, Link);
  1741. IScsiMacAddrToStr (
  1742. &NicInfo->PermanentAddress,
  1743. NicInfo->HwAddressSize,
  1744. NicInfo->VlanId,
  1745. MacString
  1746. );
  1747. UnicodeSPrint (mPrivate->PortString, (UINTN)ISCSI_NAME_IFR_MAX_SIZE, L"MAC %s", MacString);
  1748. PortTitleToken = HiiSetString (
  1749. mCallbackInfo->RegisteredHandle,
  1750. 0,
  1751. mPrivate->PortString,
  1752. NULL
  1753. );
  1754. if (PortTitleToken == 0) {
  1755. Status = EFI_INVALID_PARAMETER;
  1756. goto Exit;
  1757. }
  1758. UnicodeSPrint (
  1759. mPrivate->PortString,
  1760. (UINTN)ISCSI_NAME_IFR_MAX_SIZE,
  1761. L"PFA: Bus %d | Dev %d | Func %d",
  1762. NicInfo->BusNumber,
  1763. NicInfo->DeviceNumber,
  1764. NicInfo->FunctionNumber
  1765. );
  1766. PortTitleHelpToken = HiiSetString (mCallbackInfo->RegisteredHandle, 0, mPrivate->PortString, NULL);
  1767. if (PortTitleHelpToken == 0) {
  1768. Status = EFI_INVALID_PARAMETER;
  1769. goto Exit;
  1770. }
  1771. HiiCreateGotoOpCode (
  1772. StartOpCodeHandle, // Container for dynamic created opcodes
  1773. FORMID_ATTEMPT_FORM,
  1774. PortTitleToken,
  1775. PortTitleHelpToken,
  1776. EFI_IFR_FLAG_CALLBACK, // Question flag
  1777. (UINT16)(KEY_MAC_ENTRY_BASE + NicInfo->NicIndex)
  1778. );
  1779. }
  1780. Status = HiiUpdateForm (
  1781. mCallbackInfo->RegisteredHandle, // HII handle
  1782. &gIScsiConfigGuid, // Formset GUID
  1783. FORMID_MAC_FORM, // Form ID
  1784. StartOpCodeHandle, // Label for where to insert opcodes
  1785. EndOpCodeHandle // Replace data
  1786. );
  1787. Exit:
  1788. HiiFreeOpCodeHandle (StartOpCodeHandle);
  1789. HiiFreeOpCodeHandle (EndOpCodeHandle);
  1790. return Status;
  1791. }
  1792. /**
  1793. Add the attempts by keyword 'iSCSIAddAttempts', you can use this keyword with
  1794. value 'attempt:1 attempt:2' etc to add one or more attempts once. This is different
  1795. with IScsiConfigAddAttempt function which is used to add attempt by UI configuration.
  1796. @param[in] AttemptList The new attempt List will be added.
  1797. @retval EFI_SUCCESS The operation to add attempt list successfully.
  1798. @retval EFI_INVALID_PARAMETER Any parameter is invalid.
  1799. @retval EFI_NOT_FOUND Cannot find the corresponding variable.
  1800. @retval EFI_OUT_OF_RESOURCES Fail to finish the operation due to lack of
  1801. resources.
  1802. **/
  1803. EFI_STATUS
  1804. IScsiConfigAddAttemptsByKeywords (
  1805. IN UINT8 *AttemptList
  1806. )
  1807. {
  1808. UINT8 Index;
  1809. UINT8 Number;
  1810. UINTN TotalNumber;
  1811. UINT8 Nic;
  1812. UINT8 *AttemptConfigOrder;
  1813. UINTN AttemptConfigOrderSize;
  1814. UINT8 *AttemptConfigOrderTmp;
  1815. ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData;
  1816. ISCSI_NIC_INFO *NicInfo;
  1817. CHAR16 MacString[ISCSI_MAX_MAC_STRING_LEN];
  1818. CHAR16 IScsiMode[64];
  1819. CHAR16 IpMode[64];
  1820. EFI_STATUS Status;
  1821. Nic = mPrivate->CurrentNic;
  1822. NicInfo = IScsiGetNicInfoByIndex (Nic);
  1823. if (NicInfo == NULL) {
  1824. return EFI_NOT_FOUND;
  1825. }
  1826. //
  1827. // The MAC info will be recorded in Config Data.
  1828. //
  1829. IScsiMacAddrToStr (
  1830. &NicInfo->PermanentAddress,
  1831. NicInfo->HwAddressSize,
  1832. NicInfo->VlanId,
  1833. MacString
  1834. );
  1835. for (Index = 0; Index < PcdGet8 (PcdMaxIScsiAttemptNumber); Index++) {
  1836. if (AttemptList[Index] == 0) {
  1837. continue;
  1838. }
  1839. //
  1840. // Add the attempt.
  1841. //
  1842. Number = AttemptList[Index];
  1843. UnicodeSPrint (
  1844. mPrivate->PortString,
  1845. (UINTN)ISCSI_NAME_IFR_MAX_SIZE,
  1846. L"Attempt %d",
  1847. Number
  1848. );
  1849. GetVariable2 (
  1850. mPrivate->PortString,
  1851. &gEfiIScsiInitiatorNameProtocolGuid,
  1852. (VOID **)&AttemptConfigData,
  1853. NULL
  1854. );
  1855. if ((AttemptConfigData == NULL) || (AttemptConfigData->Actived == ISCSI_ACTIVE_ENABLED)) {
  1856. return EFI_INVALID_PARAMETER;
  1857. }
  1858. AttemptConfigData->Actived = ISCSI_ACTIVE_ENABLED;
  1859. AttemptConfigData->NicIndex = NicInfo->NicIndex;
  1860. UnicodeStrToAsciiStrS (MacString, AttemptConfigData->MacString, ISCSI_MAX_MAC_STRING_LEN);
  1861. //
  1862. // Generate OUI-format ISID based on MAC address.
  1863. //
  1864. CopyMem (AttemptConfigData->SessionConfigData.IsId, &NicInfo->PermanentAddress, 6);
  1865. AttemptConfigData->SessionConfigData.IsId[0] =
  1866. (UINT8)(AttemptConfigData->SessionConfigData.IsId[0] & 0x3F);
  1867. //
  1868. // Configure the iSCSI Mode and IpMode to default.
  1869. // Add Attempt Help Info.
  1870. //
  1871. UnicodeSPrint (IScsiMode, 64, L"Disabled");
  1872. UnicodeSPrint (IpMode, 64, L"IP4");
  1873. UnicodeSPrint (
  1874. mPrivate->PortString,
  1875. (UINTN)ISCSI_NAME_IFR_MAX_SIZE,
  1876. L"MAC: %s, PFA: Bus %d | Dev %d | Func %d, iSCSI mode: %s, IP version: %s",
  1877. MacString,
  1878. NicInfo->BusNumber,
  1879. NicInfo->DeviceNumber,
  1880. NicInfo->FunctionNumber,
  1881. IScsiMode,
  1882. IpMode
  1883. );
  1884. AttemptConfigData->AttemptTitleHelpToken = HiiSetString (
  1885. mCallbackInfo->RegisteredHandle,
  1886. 0,
  1887. mPrivate->PortString,
  1888. NULL
  1889. );
  1890. if (AttemptConfigData->AttemptTitleHelpToken == 0) {
  1891. return EFI_OUT_OF_RESOURCES;
  1892. }
  1893. //
  1894. // Get current Attempt order and number.
  1895. //
  1896. AttemptConfigOrder = IScsiGetVariableAndSize (
  1897. L"AttemptOrder",
  1898. &gIScsiConfigGuid,
  1899. &AttemptConfigOrderSize
  1900. );
  1901. TotalNumber = AttemptConfigOrderSize / sizeof (UINT8);
  1902. TotalNumber++;
  1903. //
  1904. // Append the new created attempt order to the end.
  1905. //
  1906. AttemptConfigOrderTmp = AllocateZeroPool (TotalNumber * sizeof (UINT8));
  1907. if (AttemptConfigOrderTmp == NULL) {
  1908. if (AttemptConfigOrder != NULL) {
  1909. FreePool (AttemptConfigOrder);
  1910. }
  1911. return EFI_OUT_OF_RESOURCES;
  1912. }
  1913. if (AttemptConfigOrder != NULL) {
  1914. CopyMem (AttemptConfigOrderTmp, AttemptConfigOrder, AttemptConfigOrderSize);
  1915. FreePool (AttemptConfigOrder);
  1916. }
  1917. AttemptConfigOrderTmp[TotalNumber - 1] = Number;
  1918. AttemptConfigOrder = AttemptConfigOrderTmp;
  1919. AttemptConfigOrderSize = TotalNumber * sizeof (UINT8);
  1920. Status = gRT->SetVariable (
  1921. L"AttemptOrder",
  1922. &gIScsiConfigGuid,
  1923. EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,
  1924. AttemptConfigOrderSize,
  1925. AttemptConfigOrder
  1926. );
  1927. FreePool (AttemptConfigOrder);
  1928. if (EFI_ERROR (Status)) {
  1929. return Status;
  1930. }
  1931. //
  1932. // Record the attempt in global link list.
  1933. //
  1934. InsertTailList (&mPrivate->AttemptConfigs, &AttemptConfigData->Link);
  1935. mPrivate->AttemptCount++;
  1936. UnicodeSPrint (mPrivate->PortString, (UINTN)ISCSI_NAME_IFR_MAX_SIZE, L"Attempt %d", AttemptConfigData->AttemptConfigIndex);
  1937. gRT->SetVariable (
  1938. mPrivate->PortString,
  1939. &gEfiIScsiInitiatorNameProtocolGuid,
  1940. ISCSI_CONFIG_VAR_ATTR,
  1941. sizeof (ISCSI_ATTEMPT_CONFIG_NVDATA),
  1942. AttemptConfigData
  1943. );
  1944. }
  1945. return EFI_SUCCESS;
  1946. }
  1947. /**
  1948. Callback function when user presses "Commit Changes and Exit" in Delete Attempts or Delete Attempts by Keyword.
  1949. @param[in] IfrNvData The IFR NV data.
  1950. @retval EFI_NOT_FOUND Cannot find the corresponding variable.
  1951. @retval EFI_SUCCESS The operation is completed successfully.
  1952. @retval EFI_ABORTED This operation is aborted cause of error
  1953. configuration.
  1954. @retval EFI_OUT_OF_RESOURCES Fail to finish the operation due to lack of
  1955. resources.
  1956. **/
  1957. EFI_STATUS
  1958. IScsiConfigDeleteAttempts (
  1959. IN ISCSI_CONFIG_IFR_NVDATA *IfrNvData
  1960. )
  1961. {
  1962. EFI_STATUS Status;
  1963. UINTN Index;
  1964. UINTN NewIndex;
  1965. ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData;
  1966. UINT8 *AttemptConfigOrder;
  1967. UINTN AttemptConfigOrderSize;
  1968. UINT8 *AttemptNewOrder;
  1969. UINT8 AttemptConfigIndex;
  1970. UINT32 Attribute;
  1971. UINTN Total;
  1972. UINTN NewTotal;
  1973. LIST_ENTRY *Entry;
  1974. LIST_ENTRY *NextEntry;
  1975. ISCSI_SESSION_CONFIG_NVDATA *ConfigData;
  1976. Index = 0;
  1977. AttemptConfigOrder = IScsiGetVariableAndSize (
  1978. L"AttemptOrder",
  1979. &gIScsiConfigGuid,
  1980. &AttemptConfigOrderSize
  1981. );
  1982. if ((AttemptConfigOrder == NULL) || (AttemptConfigOrderSize == 0)) {
  1983. return EFI_NOT_FOUND;
  1984. }
  1985. AttemptNewOrder = AllocateZeroPool (AttemptConfigOrderSize);
  1986. if (AttemptNewOrder == NULL) {
  1987. Status = EFI_OUT_OF_RESOURCES;
  1988. goto Error;
  1989. }
  1990. Total = AttemptConfigOrderSize / sizeof (UINT8);
  1991. NewTotal = Total;
  1992. NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &mPrivate->AttemptConfigs) {
  1993. if (IfrNvData->DeleteAttemptList[Index] == 0) {
  1994. Index++;
  1995. continue;
  1996. }
  1997. //
  1998. // Delete the attempt.
  1999. //
  2000. AttemptConfigData = NET_LIST_USER_STRUCT (Entry, ISCSI_ATTEMPT_CONFIG_NVDATA, Link);
  2001. //
  2002. // Remove this attempt from UI configured attempt list.
  2003. //
  2004. RemoveEntryList (&AttemptConfigData->Link);
  2005. mPrivate->AttemptCount--;
  2006. if (AttemptConfigData->SessionConfigData.Enabled == ISCSI_ENABLED_FOR_MPIO) {
  2007. if (mPrivate->MpioCount < 1) {
  2008. Status = EFI_ABORTED;
  2009. goto Error;
  2010. }
  2011. //
  2012. // No more attempt is enabled for MPIO. Transit the iSCSI mode to single path.
  2013. //
  2014. if (--mPrivate->MpioCount == 0) {
  2015. mPrivate->EnableMpio = FALSE;
  2016. }
  2017. } else if (AttemptConfigData->SessionConfigData.Enabled == ISCSI_ENABLED) {
  2018. if (mPrivate->SinglePathCount < 1) {
  2019. Status = EFI_ABORTED;
  2020. goto Error;
  2021. }
  2022. mPrivate->SinglePathCount--;
  2023. }
  2024. AttemptConfigIndex = AttemptConfigData->AttemptConfigIndex;
  2025. FreePool (AttemptConfigData);
  2026. //
  2027. // Create a new Attempt
  2028. //
  2029. AttemptConfigData = AllocateZeroPool (sizeof (ISCSI_ATTEMPT_CONFIG_NVDATA));
  2030. if (AttemptConfigData == NULL) {
  2031. return EFI_OUT_OF_RESOURCES;
  2032. }
  2033. ConfigData = &AttemptConfigData->SessionConfigData;
  2034. ConfigData->TargetPort = ISCSI_WELL_KNOWN_PORT;
  2035. ConfigData->ConnectTimeout = CONNECT_DEFAULT_TIMEOUT;
  2036. ConfigData->ConnectRetryCount = CONNECT_MIN_RETRY;
  2037. AttemptConfigData->AuthenticationType = ISCSI_AUTH_TYPE_CHAP;
  2038. AttemptConfigData->AuthConfigData.CHAP.CHAPType = ISCSI_CHAP_UNI;
  2039. //
  2040. // Configure the Attempt index and set variable.
  2041. //
  2042. AttemptConfigData->AttemptConfigIndex = AttemptConfigIndex;
  2043. //
  2044. // Set the attempt name to default.
  2045. //
  2046. UnicodeSPrint (
  2047. mPrivate->PortString,
  2048. (UINTN)ISCSI_NAME_IFR_MAX_SIZE,
  2049. L"Attempt %d",
  2050. (UINTN)AttemptConfigData->AttemptConfigIndex
  2051. );
  2052. UnicodeStrToAsciiStrS (mPrivate->PortString, AttemptConfigData->AttemptName, ATTEMPT_NAME_SIZE);
  2053. gRT->SetVariable (
  2054. mPrivate->PortString,
  2055. &gEfiIScsiInitiatorNameProtocolGuid,
  2056. ISCSI_CONFIG_VAR_ATTR,
  2057. sizeof (ISCSI_ATTEMPT_CONFIG_NVDATA),
  2058. AttemptConfigData
  2059. );
  2060. //
  2061. // Mark the attempt order in NVR to be deleted - 0.
  2062. //
  2063. for (NewIndex = 0; NewIndex < Total; NewIndex++) {
  2064. if (AttemptConfigOrder[NewIndex] == AttemptConfigData->AttemptConfigIndex) {
  2065. AttemptConfigOrder[NewIndex] = 0;
  2066. break;
  2067. }
  2068. }
  2069. NewTotal--;
  2070. if (mCallbackInfo->Current == AttemptConfigData) {
  2071. mCallbackInfo->Current = NULL;
  2072. }
  2073. FreePool (AttemptConfigData);
  2074. //
  2075. // Check next Attempt.
  2076. //
  2077. Index++;
  2078. }
  2079. //
  2080. // Construct AttemptNewOrder.
  2081. //
  2082. for (Index = 0, NewIndex = 0; Index < Total; Index++) {
  2083. if (AttemptConfigOrder[Index] != 0) {
  2084. AttemptNewOrder[NewIndex] = AttemptConfigOrder[Index];
  2085. NewIndex++;
  2086. }
  2087. }
  2088. Attribute = EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE;
  2089. //
  2090. // Update AttemptOrder in NVR.
  2091. //
  2092. Status = gRT->SetVariable (
  2093. L"AttemptOrder",
  2094. &gIScsiConfigGuid,
  2095. Attribute,
  2096. NewTotal * sizeof (UINT8),
  2097. AttemptNewOrder
  2098. );
  2099. Error:
  2100. if (AttemptConfigOrder != NULL) {
  2101. FreePool (AttemptConfigOrder);
  2102. }
  2103. if (AttemptNewOrder != NULL) {
  2104. FreePool (AttemptNewOrder);
  2105. }
  2106. return Status;
  2107. }
  2108. /**
  2109. Callback function when user presses "Delete Attempts".
  2110. @param[in] IfrNvData The IFR nv data.
  2111. @retval EFI_INVALID_PARAMETER Any parameter is invalid.
  2112. @retval EFI_BUFFER_TOO_SMALL The buffer in UpdateData is too small.
  2113. @retval EFI_SUCCESS The operation is completed successfully.
  2114. **/
  2115. EFI_STATUS
  2116. IScsiConfigDisplayDeleteAttempts (
  2117. IN ISCSI_CONFIG_IFR_NVDATA *IfrNvData
  2118. )
  2119. {
  2120. UINT8 *AttemptConfigOrder;
  2121. UINTN AttemptConfigOrderSize;
  2122. LIST_ENTRY *Entry;
  2123. ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData;
  2124. UINT8 Index;
  2125. VOID *StartOpCodeHandle;
  2126. EFI_IFR_GUID_LABEL *StartLabel;
  2127. VOID *EndOpCodeHandle;
  2128. EFI_IFR_GUID_LABEL *EndLabel;
  2129. EFI_STATUS Status;
  2130. Status = IScsiCreateOpCode (
  2131. DELETE_ENTRY_LABEL,
  2132. &StartOpCodeHandle,
  2133. &StartLabel,
  2134. &EndOpCodeHandle,
  2135. &EndLabel
  2136. );
  2137. if (EFI_ERROR (Status)) {
  2138. return Status;
  2139. }
  2140. AttemptConfigOrder = IScsiGetVariableAndSize (
  2141. L"AttemptOrder",
  2142. &gIScsiConfigGuid,
  2143. &AttemptConfigOrderSize
  2144. );
  2145. if (AttemptConfigOrder != NULL) {
  2146. //
  2147. // Create the check box opcode to be deleted.
  2148. //
  2149. Index = 0;
  2150. NET_LIST_FOR_EACH (Entry, &mPrivate->AttemptConfigs) {
  2151. AttemptConfigData = NET_LIST_USER_STRUCT (Entry, ISCSI_ATTEMPT_CONFIG_NVDATA, Link);
  2152. IfrNvData->DeleteAttemptList[Index] = 0x00;
  2153. HiiCreateCheckBoxOpCode (
  2154. StartOpCodeHandle,
  2155. (EFI_QUESTION_ID)(ATTEMPT_DEL_QUESTION_ID + Index),
  2156. CONFIGURATION_VARSTORE_ID,
  2157. (UINT16)(ATTEMPT_DEL_VAR_OFFSET + Index),
  2158. AttemptConfigData->AttemptTitleToken,
  2159. AttemptConfigData->AttemptTitleHelpToken,
  2160. 0,
  2161. 0,
  2162. NULL
  2163. );
  2164. Index++;
  2165. if (Index == ISCSI_MAX_ATTEMPTS_NUM) {
  2166. break;
  2167. }
  2168. }
  2169. FreePool (AttemptConfigOrder);
  2170. }
  2171. Status = HiiUpdateForm (
  2172. mCallbackInfo->RegisteredHandle, // HII handle
  2173. &gIScsiConfigGuid, // Formset GUID
  2174. FORMID_DELETE_FORM, // Form ID
  2175. StartOpCodeHandle, // Label for where to insert opcodes
  2176. EndOpCodeHandle // Replace data
  2177. );
  2178. HiiFreeOpCodeHandle (StartOpCodeHandle);
  2179. HiiFreeOpCodeHandle (EndOpCodeHandle);
  2180. return Status;
  2181. }
  2182. /**
  2183. Callback function when user presses "Change Attempt Order".
  2184. @retval EFI_INVALID_PARAMETER Any parameter is invalid.
  2185. @retval EFI_OUT_OF_RESOURCES Does not have sufficient resources to finish this
  2186. operation.
  2187. @retval EFI_SUCCESS The operation is completed successfully.
  2188. **/
  2189. EFI_STATUS
  2190. IScsiConfigDisplayOrderAttempts (
  2191. VOID
  2192. )
  2193. {
  2194. EFI_STATUS Status;
  2195. UINT8 Index;
  2196. LIST_ENTRY *Entry;
  2197. ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData;
  2198. VOID *StartOpCodeHandle;
  2199. EFI_IFR_GUID_LABEL *StartLabel;
  2200. VOID *EndOpCodeHandle;
  2201. EFI_IFR_GUID_LABEL *EndLabel;
  2202. VOID *OptionsOpCodeHandle;
  2203. Status = IScsiCreateOpCode (
  2204. ORDER_ENTRY_LABEL,
  2205. &StartOpCodeHandle,
  2206. &StartLabel,
  2207. &EndOpCodeHandle,
  2208. &EndLabel
  2209. );
  2210. if (EFI_ERROR (Status)) {
  2211. return Status;
  2212. }
  2213. ASSERT (StartOpCodeHandle != NULL);
  2214. OptionsOpCodeHandle = NULL;
  2215. //
  2216. // If no attempt to be ordered, update the original form and exit.
  2217. //
  2218. if (mPrivate->AttemptCount == 0) {
  2219. goto Exit;
  2220. }
  2221. //
  2222. // Create Option OpCode.
  2223. //
  2224. OptionsOpCodeHandle = HiiAllocateOpCodeHandle ();
  2225. if (OptionsOpCodeHandle == NULL) {
  2226. Status = EFI_OUT_OF_RESOURCES;
  2227. goto Error;
  2228. }
  2229. Index = 0;
  2230. NET_LIST_FOR_EACH (Entry, &mPrivate->AttemptConfigs) {
  2231. AttemptConfigData = NET_LIST_USER_STRUCT (Entry, ISCSI_ATTEMPT_CONFIG_NVDATA, Link);
  2232. HiiCreateOneOfOptionOpCode (
  2233. OptionsOpCodeHandle,
  2234. AttemptConfigData->AttemptTitleToken,
  2235. 0,
  2236. EFI_IFR_NUMERIC_SIZE_1,
  2237. AttemptConfigData->AttemptConfigIndex
  2238. );
  2239. Index++;
  2240. }
  2241. ASSERT (Index == mPrivate->AttemptCount);
  2242. HiiCreateOrderedListOpCode (
  2243. StartOpCodeHandle, // Container for dynamic created opcodes
  2244. DYNAMIC_ORDERED_LIST_QUESTION_ID, // Question ID
  2245. CONFIGURATION_VARSTORE_ID, // VarStore ID
  2246. DYNAMIC_ORDERED_LIST_VAR_OFFSET, // Offset in Buffer Storage
  2247. STRING_TOKEN (STR_ORDER_ATTEMPT_ENTRY), // Question prompt text
  2248. STRING_TOKEN (STR_ORDER_ATTEMPT_ENTRY), // Question help text
  2249. 0, // Question flag
  2250. EFI_IFR_UNIQUE_SET, // Ordered list flag, e.g. EFI_IFR_UNIQUE_SET
  2251. EFI_IFR_NUMERIC_SIZE_1, // Data type of Question value
  2252. ISCSI_MAX_ATTEMPTS_NUM, // Maximum container
  2253. OptionsOpCodeHandle, // Option Opcode list
  2254. NULL // Default Opcode is NULL
  2255. );
  2256. Exit:
  2257. Status = HiiUpdateForm (
  2258. mCallbackInfo->RegisteredHandle, // HII handle
  2259. &gIScsiConfigGuid, // Formset GUID
  2260. FORMID_ORDER_FORM, // Form ID
  2261. StartOpCodeHandle, // Label for where to insert opcodes
  2262. EndOpCodeHandle // Replace data
  2263. );
  2264. Error:
  2265. HiiFreeOpCodeHandle (StartOpCodeHandle);
  2266. HiiFreeOpCodeHandle (EndOpCodeHandle);
  2267. if (OptionsOpCodeHandle != NULL) {
  2268. HiiFreeOpCodeHandle (OptionsOpCodeHandle);
  2269. }
  2270. return Status;
  2271. }
  2272. /**
  2273. Callback function when user presses "Commit Changes and Exit" in Change Attempt Order or Change Attempt Order by Keyword.
  2274. @param[in] IfrNvData The IFR nv data.
  2275. @retval EFI_OUT_OF_RESOURCES Does not have sufficient resources to finish this
  2276. operation.
  2277. @retval EFI_NOT_FOUND Cannot find the corresponding variable.
  2278. @retval EFI_SUCCESS The operation is completed successfully.
  2279. **/
  2280. EFI_STATUS
  2281. IScsiConfigOrderAttempts (
  2282. IN ISCSI_CONFIG_IFR_NVDATA *IfrNvData
  2283. )
  2284. {
  2285. EFI_STATUS Status;
  2286. UINTN Index;
  2287. UINTN Indexj;
  2288. UINT8 AttemptConfigIndex;
  2289. ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData;
  2290. UINT8 *AttemptConfigOrder;
  2291. UINT8 *AttemptConfigOrderTmp;
  2292. UINTN AttemptConfigOrderSize;
  2293. AttemptConfigOrder = IScsiGetVariableAndSize (
  2294. L"AttemptOrder",
  2295. &gIScsiConfigGuid,
  2296. &AttemptConfigOrderSize
  2297. );
  2298. if (AttemptConfigOrder == NULL) {
  2299. return EFI_NOT_FOUND;
  2300. }
  2301. AttemptConfigOrderTmp = AllocateZeroPool (AttemptConfigOrderSize);
  2302. if (AttemptConfigOrderTmp == NULL) {
  2303. Status = EFI_OUT_OF_RESOURCES;
  2304. goto Exit;
  2305. }
  2306. for (Index = 0; Index < ISCSI_MAX_ATTEMPTS_NUM; Index++) {
  2307. //
  2308. // The real content ends with 0.
  2309. //
  2310. if (IfrNvData->DynamicOrderedList[Index] == 0) {
  2311. break;
  2312. }
  2313. AttemptConfigIndex = IfrNvData->DynamicOrderedList[Index];
  2314. AttemptConfigData = IScsiConfigGetAttemptByConfigIndex (AttemptConfigIndex);
  2315. if (AttemptConfigData == NULL) {
  2316. Status = EFI_NOT_FOUND;
  2317. goto Exit;
  2318. }
  2319. //
  2320. // Reorder the Attempt List.
  2321. //
  2322. RemoveEntryList (&AttemptConfigData->Link);
  2323. InsertTailList (&mPrivate->AttemptConfigs, &AttemptConfigData->Link);
  2324. AttemptConfigOrderTmp[Index] = AttemptConfigIndex;
  2325. //
  2326. // Mark it to be deleted - 0.
  2327. //
  2328. for (Indexj = 0; Indexj < AttemptConfigOrderSize / sizeof (UINT8); Indexj++) {
  2329. if (AttemptConfigOrder[Indexj] == AttemptConfigIndex) {
  2330. AttemptConfigOrder[Indexj] = 0;
  2331. break;
  2332. }
  2333. }
  2334. }
  2335. //
  2336. // Adjust the attempt order in NVR.
  2337. //
  2338. for ( ; Index < AttemptConfigOrderSize / sizeof (UINT8); Index++) {
  2339. for (Indexj = 0; Indexj < AttemptConfigOrderSize / sizeof (UINT8); Indexj++) {
  2340. if (AttemptConfigOrder[Indexj] != 0) {
  2341. AttemptConfigOrderTmp[Index] = AttemptConfigOrder[Indexj];
  2342. AttemptConfigOrder[Indexj] = 0;
  2343. continue;
  2344. }
  2345. }
  2346. }
  2347. Status = gRT->SetVariable (
  2348. L"AttemptOrder",
  2349. &gIScsiConfigGuid,
  2350. EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,
  2351. AttemptConfigOrderSize,
  2352. AttemptConfigOrderTmp
  2353. );
  2354. Exit:
  2355. if (AttemptConfigOrderTmp != NULL) {
  2356. FreePool (AttemptConfigOrderTmp);
  2357. }
  2358. FreePool (AttemptConfigOrder);
  2359. return Status;
  2360. }
  2361. /**
  2362. Callback function when a user presses "Attempt *" or when a user selects a NIC to
  2363. create the new attempt.
  2364. @param[in] KeyValue A unique value which is sent to the original
  2365. exporting driver so that it can identify the type
  2366. of data to expect.
  2367. @param[in] IfrNvData The IFR nv data.
  2368. @retval EFI_OUT_OF_RESOURCES Does not have sufficient resources to finish this
  2369. operation.
  2370. @retval EFI_NOT_FOUND Cannot find the corresponding variable.
  2371. @retval EFI_UNSUPPORTED Can not create more attempts.
  2372. @retval EFI_SUCCESS The operation is completed successfully.
  2373. **/
  2374. EFI_STATUS
  2375. IScsiConfigProcessDefault (
  2376. IN EFI_QUESTION_ID KeyValue,
  2377. IN ISCSI_CONFIG_IFR_NVDATA *IfrNvData
  2378. )
  2379. {
  2380. BOOLEAN NewAttempt;
  2381. ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData;
  2382. UINT8 CurrentAttemptConfigIndex;
  2383. ISCSI_NIC_INFO *NicInfo;
  2384. UINT8 NicIndex;
  2385. CHAR16 MacString[ISCSI_MAX_MAC_STRING_LEN];
  2386. UINT8 *AttemptConfigOrder;
  2387. UINTN AttemptConfigOrderSize;
  2388. UINTN Index;
  2389. EFI_INPUT_KEY Key;
  2390. AttemptConfigData = NULL;
  2391. //
  2392. // Is User creating a new attempt?
  2393. //
  2394. NewAttempt = FALSE;
  2395. if ((KeyValue >= KEY_MAC_ENTRY_BASE) &&
  2396. (KeyValue <= (UINT16)(mPrivate->MaxNic + KEY_MAC_ENTRY_BASE)))
  2397. {
  2398. //
  2399. // User has pressed "Add an Attempt" and then selects a NIC.
  2400. //
  2401. NewAttempt = TRUE;
  2402. } else if ((KeyValue >= KEY_ATTEMPT_ENTRY_BASE) &&
  2403. (KeyValue < (ISCSI_MAX_ATTEMPTS_NUM + KEY_ATTEMPT_ENTRY_BASE)))
  2404. {
  2405. //
  2406. // User has pressed "Attempt *".
  2407. //
  2408. NewAttempt = FALSE;
  2409. } else {
  2410. //
  2411. // Don't process anything.
  2412. //
  2413. return EFI_SUCCESS;
  2414. }
  2415. if (NewAttempt) {
  2416. //
  2417. // Determine which NIC user has selected for the new created attempt.
  2418. //
  2419. NicIndex = (UINT8)(KeyValue - KEY_MAC_ENTRY_BASE);
  2420. NicInfo = IScsiGetNicInfoByIndex (NicIndex);
  2421. if (NicInfo == NULL) {
  2422. return EFI_NOT_FOUND;
  2423. }
  2424. //
  2425. // Create an attempt following the initialized attempt order.
  2426. //
  2427. AttemptConfigOrder = IScsiGetVariableAndSize (
  2428. L"InitialAttemptOrder",
  2429. &gIScsiConfigGuid,
  2430. &AttemptConfigOrderSize
  2431. );
  2432. if (AttemptConfigOrder == NULL) {
  2433. return EFI_NOT_FOUND;
  2434. }
  2435. for (Index = 0; Index < AttemptConfigOrderSize / sizeof (UINT8); Index++) {
  2436. UnicodeSPrint (
  2437. mPrivate->PortString,
  2438. (UINTN)ISCSI_NAME_IFR_MAX_SIZE,
  2439. L"Attempt %d",
  2440. (UINTN)AttemptConfigOrder[Index]
  2441. );
  2442. GetVariable2 (
  2443. mPrivate->PortString,
  2444. &gEfiIScsiInitiatorNameProtocolGuid,
  2445. (VOID **)&AttemptConfigData,
  2446. NULL
  2447. );
  2448. if ((AttemptConfigData == NULL) || (AttemptConfigData->Actived == ISCSI_ACTIVE_ENABLED)) {
  2449. continue;
  2450. }
  2451. break;
  2452. }
  2453. if (Index > PcdGet8 (PcdMaxIScsiAttemptNumber)) {
  2454. CreatePopUp (
  2455. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  2456. &Key,
  2457. L"Can not create more attempts, Please configure the PcdMaxIScsiAttemptNumber if needed!",
  2458. NULL
  2459. );
  2460. return EFI_UNSUPPORTED;
  2461. }
  2462. if (AttemptConfigOrder != NULL) {
  2463. FreePool (AttemptConfigOrder);
  2464. }
  2465. //
  2466. // Record the MAC info in Config Data.
  2467. //
  2468. IScsiMacAddrToStr (
  2469. &NicInfo->PermanentAddress,
  2470. NicInfo->HwAddressSize,
  2471. NicInfo->VlanId,
  2472. MacString
  2473. );
  2474. ASSERT (AttemptConfigData != NULL);
  2475. UnicodeStrToAsciiStrS (MacString, AttemptConfigData->MacString, sizeof (AttemptConfigData->MacString));
  2476. AttemptConfigData->NicIndex = NicIndex;
  2477. AttemptConfigData->Actived = ISCSI_ACTIVE_ENABLED;
  2478. //
  2479. // Generate OUI-format ISID based on MAC address.
  2480. //
  2481. CopyMem (AttemptConfigData->SessionConfigData.IsId, &NicInfo->PermanentAddress, 6);
  2482. AttemptConfigData->SessionConfigData.IsId[0] =
  2483. (UINT8)(AttemptConfigData->SessionConfigData.IsId[0] & 0x3F);
  2484. //
  2485. // Add the help info for the new attempt.
  2486. //
  2487. UnicodeSPrint (
  2488. mPrivate->PortString,
  2489. (UINTN)ISCSI_NAME_IFR_MAX_SIZE,
  2490. L"MAC: %s, PFA: Bus %d | Dev %d | Func %d",
  2491. MacString,
  2492. NicInfo->BusNumber,
  2493. NicInfo->DeviceNumber,
  2494. NicInfo->FunctionNumber
  2495. );
  2496. AttemptConfigData->AttemptTitleHelpToken = HiiSetString (
  2497. mCallbackInfo->RegisteredHandle,
  2498. 0,
  2499. mPrivate->PortString,
  2500. NULL
  2501. );
  2502. if (AttemptConfigData->AttemptTitleHelpToken == 0) {
  2503. FreePool (AttemptConfigData);
  2504. return EFI_OUT_OF_RESOURCES;
  2505. }
  2506. } else {
  2507. //
  2508. // Determine which Attempt user has selected to configure.
  2509. // Get the attempt configuration data.
  2510. //
  2511. CurrentAttemptConfigIndex = (UINT8)(KeyValue - KEY_ATTEMPT_ENTRY_BASE);
  2512. AttemptConfigData = IScsiConfigGetAttemptByConfigIndex (CurrentAttemptConfigIndex);
  2513. if (AttemptConfigData == NULL) {
  2514. DEBUG ((DEBUG_ERROR, "Corresponding configuration data can not be retrieved!\n"));
  2515. return EFI_NOT_FOUND;
  2516. }
  2517. }
  2518. //
  2519. // Clear the old IFR data to avoid sharing it with other attempts.
  2520. //
  2521. if (IfrNvData->AuthenticationType == ISCSI_AUTH_TYPE_CHAP) {
  2522. ZeroMem (IfrNvData->CHAPName, sizeof (IfrNvData->CHAPName));
  2523. ZeroMem (IfrNvData->CHAPSecret, sizeof (IfrNvData->CHAPSecret));
  2524. ZeroMem (IfrNvData->ReverseCHAPName, sizeof (IfrNvData->ReverseCHAPName));
  2525. ZeroMem (IfrNvData->ReverseCHAPSecret, sizeof (IfrNvData->ReverseCHAPSecret));
  2526. }
  2527. IScsiConvertAttemptConfigDataToIfrNvData (AttemptConfigData, IfrNvData);
  2528. //
  2529. // Update current attempt to be a new created attempt or an existing attempt.
  2530. //
  2531. mCallbackInfo->Current = AttemptConfigData;
  2532. return EFI_SUCCESS;
  2533. }
  2534. /**
  2535. This function allows the caller to request the current
  2536. configuration for one or more named elements. The resulting
  2537. string is in <ConfigAltResp> format. Also, any and all alternative
  2538. configuration strings shall be appended to the end of the
  2539. current configuration string. If they are, they must appear
  2540. after the current configuration. They must contain the same
  2541. routing (GUID, NAME, PATH) as the current configuration string.
  2542. They must have an additional description indicating the type of
  2543. alternative configuration the string represents,
  2544. "ALTCFG=<StringToken>". That <StringToken> (when
  2545. converted from Hex UNICODE to binary) is a reference to a
  2546. string in the associated string pack.
  2547. @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
  2548. @param[in] Request A null-terminated Unicode string in
  2549. <ConfigRequest> format. Note that this
  2550. includes the routing information as well as
  2551. the configurable name / value pairs. It is
  2552. invalid for this string to be in
  2553. <MultiConfigRequest> format.
  2554. @param[out] Progress On return, points to a character in the
  2555. Request string. Points to the string's null
  2556. terminator if request was successful. Points
  2557. to the most recent "&" before the first
  2558. failing name / value pair (or the beginning
  2559. of the string if the failure is in the first
  2560. name / value pair) if the request was not successful.
  2561. @param[out] Results A null-terminated Unicode string in
  2562. <ConfigAltResp> format which has all values
  2563. filled in for the names in the Request string.
  2564. String to be allocated by the called function.
  2565. @retval EFI_SUCCESS The Results string is filled with the
  2566. values corresponding to all requested
  2567. names.
  2568. @retval EFI_OUT_OF_RESOURCES Not enough memory to store the
  2569. parts of the results that must be
  2570. stored awaiting possible future
  2571. protocols.
  2572. @retval EFI_INVALID_PARAMETER For example, passing in a NULL
  2573. for the Request parameter
  2574. would result in this type of
  2575. error. In this case, the
  2576. Progress parameter would be
  2577. set to NULL.
  2578. @retval EFI_NOT_FOUND Routing data doesn't match any
  2579. known driver. Progress set to the
  2580. first character in the routing header.
  2581. Note: There is no requirement that the
  2582. driver validate the routing data. It
  2583. must skip the <ConfigHdr> in order to
  2584. process the names.
  2585. @retval EFI_INVALID_PARAMETER Illegal syntax. Progress set
  2586. to most recent "&" before the
  2587. error or the beginning of the
  2588. string.
  2589. @retval EFI_INVALID_PARAMETER Unknown name. Progress points
  2590. to the & before the name in
  2591. question.
  2592. **/
  2593. EFI_STATUS
  2594. EFIAPI
  2595. IScsiFormExtractConfig (
  2596. IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
  2597. IN CONST EFI_STRING Request,
  2598. OUT EFI_STRING *Progress,
  2599. OUT EFI_STRING *Results
  2600. )
  2601. {
  2602. EFI_STATUS Status;
  2603. CHAR8 *InitiatorName;
  2604. UINTN BufferSize;
  2605. ISCSI_CONFIG_IFR_NVDATA *IfrNvData;
  2606. ISCSI_FORM_CALLBACK_INFO *Private;
  2607. EFI_STRING ConfigRequestHdr;
  2608. EFI_STRING ConfigRequest;
  2609. BOOLEAN AllocatedRequest;
  2610. UINTN Size;
  2611. if ((This == NULL) || (Progress == NULL) || (Results == NULL)) {
  2612. return EFI_INVALID_PARAMETER;
  2613. }
  2614. *Progress = Request;
  2615. if ((Request != NULL) && !HiiIsConfigHdrMatch (Request, &gIScsiConfigGuid, mVendorStorageName)) {
  2616. return EFI_NOT_FOUND;
  2617. }
  2618. ConfigRequestHdr = NULL;
  2619. ConfigRequest = NULL;
  2620. AllocatedRequest = FALSE;
  2621. Size = 0;
  2622. Private = ISCSI_FORM_CALLBACK_INFO_FROM_FORM_CALLBACK (This);
  2623. IfrNvData = AllocateZeroPool (sizeof (ISCSI_CONFIG_IFR_NVDATA));
  2624. if (IfrNvData == NULL) {
  2625. return EFI_OUT_OF_RESOURCES;
  2626. }
  2627. if (Private->Current != NULL) {
  2628. IScsiConvertAttemptConfigDataToIfrNvData (Private->Current, IfrNvData);
  2629. }
  2630. //
  2631. // Extract all AttemptConfigData to Keyword storage of IfrNvData.
  2632. //
  2633. IScsiConvertAttemptConfigDataToIfrNvDataByKeyword (IfrNvData);
  2634. BufferSize = ISCSI_NAME_MAX_SIZE;
  2635. InitiatorName = (CHAR8 *)AllocateZeroPool (BufferSize);
  2636. if (InitiatorName == NULL) {
  2637. FreePool (IfrNvData);
  2638. return EFI_OUT_OF_RESOURCES;
  2639. }
  2640. Status = gIScsiInitiatorName.Get (&gIScsiInitiatorName, &BufferSize, InitiatorName);
  2641. if (EFI_ERROR (Status)) {
  2642. IfrNvData->InitiatorName[0] = L'\0';
  2643. } else {
  2644. AsciiStrToUnicodeStrS (
  2645. InitiatorName,
  2646. IfrNvData->InitiatorName,
  2647. sizeof (IfrNvData->InitiatorName) / sizeof (IfrNvData->InitiatorName[0])
  2648. );
  2649. }
  2650. //
  2651. // Convert buffer data to <ConfigResp> by helper function BlockToConfig().
  2652. //
  2653. BufferSize = sizeof (ISCSI_CONFIG_IFR_NVDATA);
  2654. ConfigRequest = Request;
  2655. if ((Request == NULL) || (StrStr (Request, L"OFFSET") == NULL)) {
  2656. //
  2657. // Request has no request element, construct full request string.
  2658. // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
  2659. // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator
  2660. //
  2661. ConfigRequestHdr = HiiConstructConfigHdr (&gIScsiConfigGuid, mVendorStorageName, Private->DriverHandle);
  2662. Size = (StrLen (ConfigRequestHdr) + 32 + 1) * sizeof (CHAR16);
  2663. ConfigRequest = AllocateZeroPool (Size);
  2664. if (ConfigRequest == NULL) {
  2665. FreePool (IfrNvData);
  2666. FreePool (InitiatorName);
  2667. return EFI_OUT_OF_RESOURCES;
  2668. }
  2669. AllocatedRequest = TRUE;
  2670. UnicodeSPrint (ConfigRequest, Size, L"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr, (UINT64)BufferSize);
  2671. FreePool (ConfigRequestHdr);
  2672. }
  2673. Status = gHiiConfigRouting->BlockToConfig (
  2674. gHiiConfigRouting,
  2675. ConfigRequest,
  2676. (UINT8 *)IfrNvData,
  2677. BufferSize,
  2678. Results,
  2679. Progress
  2680. );
  2681. FreePool (IfrNvData);
  2682. FreePool (InitiatorName);
  2683. //
  2684. // Free the allocated config request string.
  2685. //
  2686. if (AllocatedRequest) {
  2687. FreePool (ConfigRequest);
  2688. ConfigRequest = NULL;
  2689. }
  2690. //
  2691. // Set Progress string to the original request string.
  2692. //
  2693. if (Request == NULL) {
  2694. *Progress = NULL;
  2695. } else if (StrStr (Request, L"OFFSET") == NULL) {
  2696. *Progress = Request + StrLen (Request);
  2697. }
  2698. return Status;
  2699. }
  2700. /**
  2701. This function applies changes in a driver's configuration.
  2702. Input is a Configuration, which has the routing data for this
  2703. driver followed by name / value configuration pairs. The driver
  2704. must apply those pairs to its configurable storage. If the
  2705. driver's configuration is stored in a linear block of data
  2706. and the driver's name / value pairs are in <BlockConfig>
  2707. format, it may use the ConfigToBlock helper function (above) to
  2708. simplify the job.
  2709. @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
  2710. @param[in] Configuration A null-terminated Unicode string in
  2711. <ConfigString> format.
  2712. @param[out] Progress A pointer to a string filled in with the
  2713. offset of the most recent '&' before the
  2714. first failing name / value pair (or the
  2715. beginning of the string if the failure
  2716. is in the first name / value pair) or
  2717. the terminating NULL if all was
  2718. successful.
  2719. @retval EFI_SUCCESS The results have been distributed or are
  2720. awaiting distribution.
  2721. @retval EFI_OUT_OF_RESOURCES Not enough memory to store the
  2722. parts of the results that must be
  2723. stored awaiting possible future
  2724. protocols.
  2725. @retval EFI_INVALID_PARAMETERS Passing in a NULL for the
  2726. Results parameter would result
  2727. in this type of error.
  2728. @retval EFI_NOT_FOUND Target for the specified routing data
  2729. was not found.
  2730. **/
  2731. EFI_STATUS
  2732. EFIAPI
  2733. IScsiFormRouteConfig (
  2734. IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
  2735. IN CONST EFI_STRING Configuration,
  2736. OUT EFI_STRING *Progress
  2737. )
  2738. {
  2739. EFI_STATUS Status;
  2740. ISCSI_CONFIG_IFR_NVDATA *IfrNvData;
  2741. ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData;
  2742. LIST_ENTRY *Entry;
  2743. LIST_ENTRY *NextEntry;
  2744. ISCSI_NIC_INFO *NicInfo;
  2745. EFI_INPUT_KEY Key;
  2746. CHAR16 MacString[ISCSI_MAX_MAC_STRING_LEN];
  2747. CHAR8 *InitiatorName;
  2748. UINT8 *AttemptList;
  2749. UINTN BufferSize;
  2750. UINTN OffSet;
  2751. UINTN Index;
  2752. UINTN Index2;
  2753. Index = 0;
  2754. Index2 = 0;
  2755. NicInfo = NULL;
  2756. AttemptList = NULL;
  2757. Status = EFI_SUCCESS;
  2758. if ((This == NULL) || (Configuration == NULL) || (Progress == NULL)) {
  2759. return EFI_INVALID_PARAMETER;
  2760. }
  2761. //
  2762. // Check routing data in <ConfigHdr>.
  2763. // Note: if only one Storage is used, then this checking could be skipped.
  2764. //
  2765. if (!HiiIsConfigHdrMatch (Configuration, &gIScsiConfigGuid, mVendorStorageName)) {
  2766. *Progress = Configuration;
  2767. return EFI_NOT_FOUND;
  2768. }
  2769. IfrNvData = AllocateZeroPool (sizeof (ISCSI_CONFIG_IFR_NVDATA));
  2770. if (IfrNvData == NULL) {
  2771. return EFI_OUT_OF_RESOURCES;
  2772. }
  2773. BufferSize = ISCSI_NAME_MAX_SIZE;
  2774. InitiatorName = (CHAR8 *)AllocateZeroPool (BufferSize);
  2775. if (InitiatorName == NULL) {
  2776. Status = EFI_OUT_OF_RESOURCES;
  2777. goto Exit;
  2778. }
  2779. //
  2780. // Convert <ConfigResp> to buffer data by helper function ConfigToBlock().
  2781. //
  2782. BufferSize = sizeof (ISCSI_CONFIG_IFR_NVDATA);
  2783. Status = gHiiConfigRouting->ConfigToBlock (
  2784. gHiiConfigRouting,
  2785. Configuration,
  2786. (UINT8 *)IfrNvData,
  2787. &BufferSize,
  2788. Progress
  2789. );
  2790. if (EFI_ERROR (Status)) {
  2791. goto Exit;
  2792. }
  2793. if (IfrNvData->InitiatorName[0] != L'\0') {
  2794. UnicodeStrToAsciiStrS (IfrNvData->InitiatorName, InitiatorName, ISCSI_NAME_MAX_SIZE);
  2795. BufferSize = AsciiStrSize (InitiatorName);
  2796. Status = gIScsiInitiatorName.Set (&gIScsiInitiatorName, &BufferSize, InitiatorName);
  2797. if (EFI_ERROR (Status)) {
  2798. CreatePopUp (
  2799. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  2800. &Key,
  2801. L"Invalid iSCSI Name!",
  2802. NULL
  2803. );
  2804. goto Exit;
  2805. }
  2806. } else {
  2807. Status = IScsiGetValue (Configuration, L"&OFFSET=", &OffSet);
  2808. if (EFI_ERROR (Status)) {
  2809. goto Exit;
  2810. }
  2811. if (OffSet >= ATTEMPT_MAC_ADDR_VAR_OFFSET) {
  2812. Status = gIScsiInitiatorName.Get (&gIScsiInitiatorName, &BufferSize, InitiatorName);
  2813. if (EFI_ERROR (Status)) {
  2814. CreatePopUp (
  2815. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  2816. &Key,
  2817. L"Error: please configure iSCSI initiator name first!",
  2818. NULL
  2819. );
  2820. goto Exit;
  2821. }
  2822. } else {
  2823. goto Exit;
  2824. }
  2825. if (IfrNvData->ISCSIAddAttemptList[0] != L'\0') {
  2826. Status = IScsiGetAttemptIndexList (IfrNvData->ISCSIAddAttemptList, IfrNvData->AddAttemptList, TRUE);
  2827. if (EFI_ERROR (Status)) {
  2828. CreatePopUp (
  2829. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  2830. &Key,
  2831. L"Error: The add attempt list is invalid",
  2832. NULL
  2833. );
  2834. goto Exit;
  2835. }
  2836. Status = IScsiConfigAddAttemptsByKeywords (IfrNvData->AddAttemptList);
  2837. if (EFI_ERROR (Status)) {
  2838. goto Exit;
  2839. }
  2840. } else if (IfrNvData->ISCSIDeleteAttemptList[0] != L'\0') {
  2841. AttemptList = (UINT8 *)AllocateZeroPool ((ISCSI_MAX_ATTEMPTS_NUM + 1) * sizeof (UINT8));
  2842. if (AttemptList == NULL) {
  2843. Status = EFI_OUT_OF_RESOURCES;
  2844. goto Exit;
  2845. }
  2846. Status = IScsiGetAttemptIndexList (IfrNvData->ISCSIDeleteAttemptList, AttemptList, FALSE);
  2847. if (EFI_ERROR (Status)) {
  2848. CreatePopUp (
  2849. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  2850. &Key,
  2851. L"Error: The delete attempt list is invalid",
  2852. NULL
  2853. );
  2854. goto Exit;
  2855. }
  2856. //
  2857. // Mark the attempt which will be delete in the global list.
  2858. //
  2859. NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &mPrivate->AttemptConfigs) {
  2860. AttemptConfigData = NET_LIST_USER_STRUCT (Entry, ISCSI_ATTEMPT_CONFIG_NVDATA, Link);
  2861. while (AttemptList[Index] != 0) {
  2862. if (AttemptConfigData->AttemptConfigIndex == AttemptList[Index]) {
  2863. IfrNvData->DeleteAttemptList[Index2] = 1;
  2864. break;
  2865. }
  2866. Index++;
  2867. }
  2868. Index2++;
  2869. Index = 0;
  2870. }
  2871. Status = IScsiConfigDeleteAttempts (IfrNvData);
  2872. if (EFI_ERROR (Status)) {
  2873. goto Exit;
  2874. }
  2875. FreePool (AttemptList);
  2876. } else if (IfrNvData->ISCSIAttemptOrder[0] != L'\0') {
  2877. Status = IScsiGetAttemptIndexList (IfrNvData->ISCSIAttemptOrder, IfrNvData->DynamicOrderedList, FALSE);
  2878. if (EFI_ERROR (Status)) {
  2879. CreatePopUp (
  2880. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  2881. &Key,
  2882. L"Error: The new attempt order list is invalid",
  2883. NULL
  2884. );
  2885. goto Exit;
  2886. }
  2887. Status = IScsiConfigOrderAttempts (IfrNvData);
  2888. if (EFI_ERROR (Status)) {
  2889. goto Exit;
  2890. }
  2891. } else if (IfrNvData->ISCSIMacAddr[0] != L'\0') {
  2892. NET_LIST_FOR_EACH (Entry, &mPrivate->NicInfoList) {
  2893. NicInfo = NET_LIST_USER_STRUCT (Entry, ISCSI_NIC_INFO, Link);
  2894. IScsiMacAddrToStr (
  2895. &NicInfo->PermanentAddress,
  2896. NicInfo->HwAddressSize,
  2897. NicInfo->VlanId,
  2898. MacString
  2899. );
  2900. if (!StrCmp (MacString, IfrNvData->ISCSIMacAddr)) {
  2901. mPrivate->CurrentNic = NicInfo->NicIndex;
  2902. break;
  2903. }
  2904. }
  2905. if ((NicInfo == NULL) || (NicInfo->NicIndex == 0)) {
  2906. Status = EFI_NOT_FOUND;
  2907. goto Exit;
  2908. }
  2909. } else {
  2910. Status = IScsiConvertlfrNvDataToAttemptConfigDataByKeyword (IfrNvData, OffSet);
  2911. if (EFI_ERROR (Status)) {
  2912. goto Exit;
  2913. }
  2914. }
  2915. }
  2916. IScsiConfigUpdateAttempt ();
  2917. Exit:
  2918. if (InitiatorName != NULL) {
  2919. FreePool (InitiatorName);
  2920. }
  2921. if (IfrNvData != NULL) {
  2922. FreePool (IfrNvData);
  2923. }
  2924. return Status;
  2925. }
  2926. /**
  2927. This function is called to provide results data to the driver.
  2928. This data consists of a unique key that is used to identify
  2929. which data is either being passed back or being asked for.
  2930. @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
  2931. @param[in] Action Specifies the type of action taken by the browser.
  2932. @param[in] QuestionId A unique value which is sent to the original
  2933. exporting driver so that it can identify the type
  2934. of data to expect. The format of the data tends to
  2935. vary based on the opcode that generated the callback.
  2936. @param[in] Type The type of value for the question.
  2937. @param[in, out] Value A pointer to the data being sent to the original
  2938. exporting driver.
  2939. @param[out] ActionRequest On return, points to the action requested by the
  2940. callback function.
  2941. @retval EFI_SUCCESS The callback successfully handled the action.
  2942. @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the
  2943. variable and its data.
  2944. @retval EFI_DEVICE_ERROR The variable could not be saved.
  2945. @retval EFI_UNSUPPORTED The specified Action is not supported by the
  2946. callback.
  2947. **/
  2948. EFI_STATUS
  2949. EFIAPI
  2950. IScsiFormCallback (
  2951. IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
  2952. IN EFI_BROWSER_ACTION Action,
  2953. IN EFI_QUESTION_ID QuestionId,
  2954. IN UINT8 Type,
  2955. IN OUT EFI_IFR_TYPE_VALUE *Value,
  2956. OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest
  2957. )
  2958. {
  2959. ISCSI_FORM_CALLBACK_INFO *Private;
  2960. UINTN BufferSize;
  2961. CHAR8 *IScsiName;
  2962. CHAR8 IpString[ISCSI_NAME_MAX_SIZE];
  2963. CHAR8 LunString[ISCSI_LUN_STR_MAX_LEN];
  2964. UINT64 Lun;
  2965. EFI_IP_ADDRESS HostIp;
  2966. EFI_IP_ADDRESS SubnetMask;
  2967. EFI_IP_ADDRESS Gateway;
  2968. ISCSI_CONFIG_IFR_NVDATA *IfrNvData;
  2969. ISCSI_CONFIG_IFR_NVDATA OldIfrNvData;
  2970. EFI_STATUS Status;
  2971. EFI_INPUT_KEY Key;
  2972. ISCSI_NIC_INFO *NicInfo;
  2973. NicInfo = NULL;
  2974. if ((Action == EFI_BROWSER_ACTION_FORM_OPEN) || (Action == EFI_BROWSER_ACTION_FORM_CLOSE)) {
  2975. //
  2976. // Do nothing for UEFI OPEN/CLOSE Action
  2977. //
  2978. return EFI_SUCCESS;
  2979. }
  2980. if ((Action != EFI_BROWSER_ACTION_CHANGING) && (Action != EFI_BROWSER_ACTION_CHANGED)) {
  2981. //
  2982. // All other type return unsupported.
  2983. //
  2984. return EFI_UNSUPPORTED;
  2985. }
  2986. if ((Value == NULL) || (ActionRequest == NULL)) {
  2987. return EFI_INVALID_PARAMETER;
  2988. }
  2989. Private = ISCSI_FORM_CALLBACK_INFO_FROM_FORM_CALLBACK (This);
  2990. //
  2991. // Retrieve uncommitted data from Browser
  2992. //
  2993. BufferSize = sizeof (ISCSI_CONFIG_IFR_NVDATA);
  2994. IfrNvData = AllocateZeroPool (BufferSize);
  2995. if (IfrNvData == NULL) {
  2996. return EFI_OUT_OF_RESOURCES;
  2997. }
  2998. IScsiName = (CHAR8 *)AllocateZeroPool (ISCSI_NAME_MAX_SIZE);
  2999. if (IScsiName == NULL) {
  3000. FreePool (IfrNvData);
  3001. return EFI_OUT_OF_RESOURCES;
  3002. }
  3003. Status = EFI_SUCCESS;
  3004. ZeroMem (&OldIfrNvData, BufferSize);
  3005. HiiGetBrowserData (NULL, NULL, BufferSize, (UINT8 *)IfrNvData);
  3006. CopyMem (&OldIfrNvData, IfrNvData, BufferSize);
  3007. if (Action == EFI_BROWSER_ACTION_CHANGING) {
  3008. switch (QuestionId) {
  3009. case KEY_ADD_ATTEMPT:
  3010. //
  3011. // Check whether iSCSI initiator name is configured already.
  3012. //
  3013. mPrivate->InitiatorNameLength = ISCSI_NAME_MAX_SIZE;
  3014. Status = gIScsiInitiatorName.Get (
  3015. &gIScsiInitiatorName,
  3016. &mPrivate->InitiatorNameLength,
  3017. mPrivate->InitiatorName
  3018. );
  3019. if (EFI_ERROR (Status)) {
  3020. CreatePopUp (
  3021. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  3022. &Key,
  3023. L"Error: please configure iSCSI initiator name first!",
  3024. NULL
  3025. );
  3026. break;
  3027. }
  3028. Status = IScsiConfigAddAttempt ();
  3029. break;
  3030. case KEY_DELETE_ATTEMPT:
  3031. CopyMem (
  3032. OldIfrNvData.DeleteAttemptList,
  3033. IfrNvData->DeleteAttemptList,
  3034. sizeof (IfrNvData->DeleteAttemptList)
  3035. );
  3036. Status = IScsiConfigDisplayDeleteAttempts (IfrNvData);
  3037. break;
  3038. case KEY_ORDER_ATTEMPT_CONFIG:
  3039. //
  3040. // Order the attempt according to user input.
  3041. //
  3042. CopyMem (
  3043. OldIfrNvData.DynamicOrderedList,
  3044. IfrNvData->DynamicOrderedList,
  3045. sizeof (IfrNvData->DynamicOrderedList)
  3046. );
  3047. IScsiConfigDisplayOrderAttempts ();
  3048. break;
  3049. default:
  3050. Status = IScsiConfigProcessDefault (QuestionId, IfrNvData);
  3051. break;
  3052. }
  3053. } else if (Action == EFI_BROWSER_ACTION_CHANGED) {
  3054. switch (QuestionId) {
  3055. case KEY_INITIATOR_NAME:
  3056. UnicodeStrToAsciiStrS (IfrNvData->InitiatorName, IScsiName, ISCSI_NAME_MAX_SIZE);
  3057. BufferSize = AsciiStrSize (IScsiName);
  3058. Status = gIScsiInitiatorName.Set (&gIScsiInitiatorName, &BufferSize, IScsiName);
  3059. if (EFI_ERROR (Status)) {
  3060. CreatePopUp (
  3061. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  3062. &Key,
  3063. L"Invalid iSCSI Name!",
  3064. NULL
  3065. );
  3066. }
  3067. *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_APPLY;
  3068. break;
  3069. case KEY_SAVE_ATTEMPT_CONFIG:
  3070. Status = IScsiConvertIfrNvDataToAttemptConfigData (IfrNvData, Private->Current);
  3071. if (EFI_ERROR (Status)) {
  3072. break;
  3073. }
  3074. *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_SUBMIT_EXIT;
  3075. break;
  3076. case KEY_SAVE_ORDER_CHANGES:
  3077. //
  3078. // Sync the Attempt Order to NVR.
  3079. //
  3080. Status = IScsiConfigOrderAttempts (IfrNvData);
  3081. if (EFI_ERROR (Status)) {
  3082. break;
  3083. }
  3084. IScsiConfigUpdateAttempt ();
  3085. *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_SUBMIT_EXIT;
  3086. break;
  3087. case KEY_IGNORE_ORDER_CHANGES:
  3088. CopyMem (
  3089. IfrNvData->DynamicOrderedList,
  3090. OldIfrNvData.DynamicOrderedList,
  3091. sizeof (IfrNvData->DynamicOrderedList)
  3092. );
  3093. *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_DISCARD_EXIT;
  3094. break;
  3095. case KEY_SAVE_DELETE_ATTEMPT:
  3096. //
  3097. // Delete the Attempt Order from NVR
  3098. //
  3099. Status = IScsiConfigDeleteAttempts (IfrNvData);
  3100. if (EFI_ERROR (Status)) {
  3101. break;
  3102. }
  3103. IScsiConfigUpdateAttempt ();
  3104. *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_SUBMIT_EXIT;
  3105. break;
  3106. case KEY_IGNORE_DELETE_ATTEMPT:
  3107. CopyMem (
  3108. IfrNvData->DeleteAttemptList,
  3109. OldIfrNvData.DeleteAttemptList,
  3110. sizeof (IfrNvData->DeleteAttemptList)
  3111. );
  3112. *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_DISCARD_EXIT;
  3113. break;
  3114. case KEY_IP_MODE:
  3115. switch (Value->u8) {
  3116. case IP_MODE_IP6:
  3117. NicInfo = IScsiGetNicInfoByIndex (Private->Current->NicIndex);
  3118. if (NicInfo == NULL) {
  3119. break;
  3120. }
  3121. if (!NicInfo->Ipv6Available) {
  3122. //
  3123. // Current NIC doesn't Support IPv6, hence use IPv4.
  3124. //
  3125. IfrNvData->IpMode = IP_MODE_IP4;
  3126. CreatePopUp (
  3127. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  3128. &Key,
  3129. L"Current NIC doesn't Support IPv6!",
  3130. NULL
  3131. );
  3132. }
  3133. case IP_MODE_IP4:
  3134. ZeroMem (IfrNvData->LocalIp, sizeof (IfrNvData->LocalIp));
  3135. ZeroMem (IfrNvData->SubnetMask, sizeof (IfrNvData->SubnetMask));
  3136. ZeroMem (IfrNvData->Gateway, sizeof (IfrNvData->Gateway));
  3137. ZeroMem (IfrNvData->TargetIp, sizeof (IfrNvData->TargetIp));
  3138. Private->Current->AutoConfigureMode = 0;
  3139. ZeroMem (&Private->Current->SessionConfigData.LocalIp, sizeof (EFI_IP_ADDRESS));
  3140. ZeroMem (&Private->Current->SessionConfigData.SubnetMask, sizeof (EFI_IPv4_ADDRESS));
  3141. ZeroMem (&Private->Current->SessionConfigData.Gateway, sizeof (EFI_IP_ADDRESS));
  3142. ZeroMem (&Private->Current->SessionConfigData.TargetIp, sizeof (EFI_IP_ADDRESS));
  3143. break;
  3144. }
  3145. break;
  3146. case KEY_LOCAL_IP:
  3147. Status = NetLibStrToIp4 (IfrNvData->LocalIp, &HostIp.v4);
  3148. if (EFI_ERROR (Status) ||
  3149. ((Private->Current->SessionConfigData.SubnetMask.Addr[0] != 0) &&
  3150. !NetIp4IsUnicast (NTOHL (HostIp.Addr[0]), NTOHL (*(UINT32 *)Private->Current->SessionConfigData.SubnetMask.Addr))))
  3151. {
  3152. CreatePopUp (
  3153. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  3154. &Key,
  3155. L"Invalid IP address!",
  3156. NULL
  3157. );
  3158. Status = EFI_INVALID_PARAMETER;
  3159. } else {
  3160. CopyMem (&Private->Current->SessionConfigData.LocalIp, &HostIp.v4, sizeof (HostIp.v4));
  3161. }
  3162. break;
  3163. case KEY_SUBNET_MASK:
  3164. Status = NetLibStrToIp4 (IfrNvData->SubnetMask, &SubnetMask.v4);
  3165. if (EFI_ERROR (Status) || ((SubnetMask.Addr[0] != 0) && (IScsiGetSubnetMaskPrefixLength (&SubnetMask.v4) == 0))) {
  3166. CreatePopUp (
  3167. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  3168. &Key,
  3169. L"Invalid Subnet Mask!",
  3170. NULL
  3171. );
  3172. Status = EFI_INVALID_PARAMETER;
  3173. } else {
  3174. CopyMem (&Private->Current->SessionConfigData.SubnetMask, &SubnetMask.v4, sizeof (SubnetMask.v4));
  3175. }
  3176. break;
  3177. case KEY_GATE_WAY:
  3178. Status = NetLibStrToIp4 (IfrNvData->Gateway, &Gateway.v4);
  3179. if (EFI_ERROR (Status) ||
  3180. ((Gateway.Addr[0] != 0) &&
  3181. (Private->Current->SessionConfigData.SubnetMask.Addr[0] != 0) &&
  3182. !NetIp4IsUnicast (NTOHL (Gateway.Addr[0]), NTOHL (*(UINT32 *)Private->Current->SessionConfigData.SubnetMask.Addr))))
  3183. {
  3184. CreatePopUp (
  3185. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  3186. &Key,
  3187. L"Invalid Gateway!",
  3188. NULL
  3189. );
  3190. Status = EFI_INVALID_PARAMETER;
  3191. } else {
  3192. CopyMem (&Private->Current->SessionConfigData.Gateway, &Gateway.v4, sizeof (Gateway.v4));
  3193. }
  3194. break;
  3195. case KEY_TARGET_IP:
  3196. UnicodeStrToAsciiStrS (IfrNvData->TargetIp, IpString, sizeof (IpString));
  3197. Status = IScsiAsciiStrToIp (IpString, IfrNvData->IpMode, &HostIp);
  3198. if (EFI_ERROR (Status) || !IpIsUnicast (&HostIp, IfrNvData->IpMode)) {
  3199. //
  3200. // The target is expressed in URL format or an invalid Ip address, just save.
  3201. //
  3202. Private->Current->SessionConfigData.DnsMode = TRUE;
  3203. ZeroMem (&Private->Current->SessionConfigData.TargetIp, sizeof (Private->Current->SessionConfigData.TargetIp));
  3204. UnicodeStrToAsciiStrS (IfrNvData->TargetIp, Private->Current->SessionConfigData.TargetUrl, ISCSI_NAME_MAX_SIZE);
  3205. } else {
  3206. Private->Current->SessionConfigData.DnsMode = FALSE;
  3207. CopyMem (&Private->Current->SessionConfigData.TargetIp, &HostIp, sizeof (HostIp));
  3208. }
  3209. break;
  3210. case KEY_TARGET_NAME:
  3211. UnicodeStrToAsciiStrS (IfrNvData->TargetName, IScsiName, ISCSI_NAME_MAX_SIZE);
  3212. Status = IScsiNormalizeName (IScsiName, AsciiStrLen (IScsiName));
  3213. if (EFI_ERROR (Status)) {
  3214. CreatePopUp (
  3215. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  3216. &Key,
  3217. L"Invalid iSCSI Name!",
  3218. NULL
  3219. );
  3220. } else {
  3221. AsciiStrCpyS (Private->Current->SessionConfigData.TargetName, ISCSI_NAME_MAX_SIZE, IScsiName);
  3222. }
  3223. break;
  3224. case KEY_DHCP_ENABLE:
  3225. if (IfrNvData->InitiatorInfoFromDhcp == 0) {
  3226. IfrNvData->TargetInfoFromDhcp = 0;
  3227. }
  3228. break;
  3229. case KEY_BOOT_LUN:
  3230. UnicodeStrToAsciiStrS (IfrNvData->BootLun, LunString, sizeof (LunString));
  3231. Status = IScsiAsciiStrToLun (LunString, (UINT8 *)&Lun);
  3232. if (EFI_ERROR (Status)) {
  3233. CreatePopUp (
  3234. EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
  3235. &Key,
  3236. L"Invalid LUN string!",
  3237. NULL
  3238. );
  3239. } else {
  3240. CopyMem (Private->Current->SessionConfigData.BootLun, &Lun, sizeof (Lun));
  3241. }
  3242. break;
  3243. case KEY_AUTH_TYPE:
  3244. switch (Value->u8) {
  3245. case ISCSI_AUTH_TYPE_CHAP:
  3246. IfrNvData->CHAPType = ISCSI_CHAP_UNI;
  3247. break;
  3248. default:
  3249. break;
  3250. }
  3251. break;
  3252. case KEY_CHAP_NAME:
  3253. UnicodeStrToAsciiStrS (
  3254. IfrNvData->CHAPName,
  3255. Private->Current->AuthConfigData.CHAP.CHAPName,
  3256. sizeof (Private->Current->AuthConfigData.CHAP.CHAPName)
  3257. );
  3258. break;
  3259. case KEY_CHAP_SECRET:
  3260. UnicodeStrToAsciiStrS (
  3261. IfrNvData->CHAPSecret,
  3262. Private->Current->AuthConfigData.CHAP.CHAPSecret,
  3263. sizeof (Private->Current->AuthConfigData.CHAP.CHAPSecret)
  3264. );
  3265. break;
  3266. case KEY_REVERSE_CHAP_NAME:
  3267. UnicodeStrToAsciiStrS (
  3268. IfrNvData->ReverseCHAPName,
  3269. Private->Current->AuthConfigData.CHAP.ReverseCHAPName,
  3270. sizeof (Private->Current->AuthConfigData.CHAP.ReverseCHAPName)
  3271. );
  3272. break;
  3273. case KEY_REVERSE_CHAP_SECRET:
  3274. UnicodeStrToAsciiStrS (
  3275. IfrNvData->ReverseCHAPSecret,
  3276. Private->Current->AuthConfigData.CHAP.ReverseCHAPSecret,
  3277. sizeof (Private->Current->AuthConfigData.CHAP.ReverseCHAPSecret)
  3278. );
  3279. break;
  3280. case KEY_CONFIG_ISID:
  3281. IScsiParseIsIdFromString (IfrNvData->IsId, Private->Current->SessionConfigData.IsId);
  3282. IScsiConvertIsIdToString (IfrNvData->IsId, Private->Current->SessionConfigData.IsId);
  3283. break;
  3284. default:
  3285. break;
  3286. }
  3287. }
  3288. if (!EFI_ERROR (Status)) {
  3289. //
  3290. // Pass changed uncommitted data back to Form Browser.
  3291. //
  3292. BufferSize = sizeof (ISCSI_CONFIG_IFR_NVDATA);
  3293. HiiSetBrowserData (NULL, NULL, BufferSize, (UINT8 *)IfrNvData, NULL);
  3294. }
  3295. FreePool (IfrNvData);
  3296. FreePool (IScsiName);
  3297. return Status;
  3298. }
  3299. /**
  3300. Initialize the iSCSI configuration form.
  3301. @param[in] DriverBindingHandle The iSCSI driverbinding handle.
  3302. @retval EFI_SUCCESS The iSCSI configuration form is initialized.
  3303. @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
  3304. **/
  3305. EFI_STATUS
  3306. IScsiConfigFormInit (
  3307. IN EFI_HANDLE DriverBindingHandle
  3308. )
  3309. {
  3310. EFI_STATUS Status;
  3311. ISCSI_FORM_CALLBACK_INFO *CallbackInfo;
  3312. CallbackInfo = (ISCSI_FORM_CALLBACK_INFO *)AllocateZeroPool (sizeof (ISCSI_FORM_CALLBACK_INFO));
  3313. if (CallbackInfo == NULL) {
  3314. return EFI_OUT_OF_RESOURCES;
  3315. }
  3316. CallbackInfo->Signature = ISCSI_FORM_CALLBACK_INFO_SIGNATURE;
  3317. CallbackInfo->Current = NULL;
  3318. CallbackInfo->ConfigAccess.ExtractConfig = IScsiFormExtractConfig;
  3319. CallbackInfo->ConfigAccess.RouteConfig = IScsiFormRouteConfig;
  3320. CallbackInfo->ConfigAccess.Callback = IScsiFormCallback;
  3321. //
  3322. // Install Device Path Protocol and Config Access protocol to driver handle.
  3323. //
  3324. Status = gBS->InstallMultipleProtocolInterfaces (
  3325. &CallbackInfo->DriverHandle,
  3326. &gEfiDevicePathProtocolGuid,
  3327. &mIScsiHiiVendorDevicePath,
  3328. &gEfiHiiConfigAccessProtocolGuid,
  3329. &CallbackInfo->ConfigAccess,
  3330. NULL
  3331. );
  3332. ASSERT_EFI_ERROR (Status);
  3333. //
  3334. // Publish our HII data.
  3335. //
  3336. CallbackInfo->RegisteredHandle = HiiAddPackages (
  3337. &gIScsiConfigGuid,
  3338. CallbackInfo->DriverHandle,
  3339. IScsiDxeStrings,
  3340. IScsiConfigVfrBin,
  3341. NULL
  3342. );
  3343. if (CallbackInfo->RegisteredHandle == NULL) {
  3344. gBS->UninstallMultipleProtocolInterfaces (
  3345. CallbackInfo->DriverHandle,
  3346. &gEfiDevicePathProtocolGuid,
  3347. &mIScsiHiiVendorDevicePath,
  3348. &gEfiHiiConfigAccessProtocolGuid,
  3349. &CallbackInfo->ConfigAccess,
  3350. NULL
  3351. );
  3352. FreePool (CallbackInfo);
  3353. return EFI_OUT_OF_RESOURCES;
  3354. }
  3355. mCallbackInfo = CallbackInfo;
  3356. return EFI_SUCCESS;
  3357. }
  3358. /**
  3359. Unload the iSCSI configuration form, this includes: delete all the iSCSI
  3360. configuration entries, uninstall the form callback protocol, and
  3361. free the resources used.
  3362. @param[in] DriverBindingHandle The iSCSI driverbinding handle.
  3363. @retval EFI_SUCCESS The iSCSI configuration form is unloaded.
  3364. @retval Others Failed to unload the form.
  3365. **/
  3366. EFI_STATUS
  3367. IScsiConfigFormUnload (
  3368. IN EFI_HANDLE DriverBindingHandle
  3369. )
  3370. {
  3371. ISCSI_ATTEMPT_CONFIG_NVDATA *AttemptConfigData;
  3372. ISCSI_NIC_INFO *NicInfo;
  3373. LIST_ENTRY *Entry;
  3374. EFI_STATUS Status;
  3375. while (!IsListEmpty (&mPrivate->AttemptConfigs)) {
  3376. Entry = NetListRemoveHead (&mPrivate->AttemptConfigs);
  3377. AttemptConfigData = NET_LIST_USER_STRUCT (Entry, ISCSI_ATTEMPT_CONFIG_NVDATA, Link);
  3378. FreePool (AttemptConfigData);
  3379. mPrivate->AttemptCount--;
  3380. }
  3381. ASSERT (mPrivate->AttemptCount == 0);
  3382. while (!IsListEmpty (&mPrivate->NicInfoList)) {
  3383. Entry = NetListRemoveHead (&mPrivate->NicInfoList);
  3384. NicInfo = NET_LIST_USER_STRUCT (Entry, ISCSI_NIC_INFO, Link);
  3385. FreePool (NicInfo);
  3386. mPrivate->NicCount--;
  3387. }
  3388. ASSERT (mPrivate->NicCount == 0);
  3389. FreePool (mPrivate);
  3390. mPrivate = NULL;
  3391. //
  3392. // Remove HII package list.
  3393. //
  3394. HiiRemovePackages (mCallbackInfo->RegisteredHandle);
  3395. //
  3396. // Uninstall Device Path Protocol and Config Access protocol.
  3397. //
  3398. Status = gBS->UninstallMultipleProtocolInterfaces (
  3399. mCallbackInfo->DriverHandle,
  3400. &gEfiDevicePathProtocolGuid,
  3401. &mIScsiHiiVendorDevicePath,
  3402. &gEfiHiiConfigAccessProtocolGuid,
  3403. &mCallbackInfo->ConfigAccess,
  3404. NULL
  3405. );
  3406. FreePool (mCallbackInfo);
  3407. return Status;
  3408. }