RedfishDiscoverDxe.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073
  1. /** @file
  2. The implementation of EFI Redfidh Discover Protocol.
  3. (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>
  4. Copyright (c) 2022, AMD Incorporated. All rights reserved.
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include "RedfishDiscoverInternal.h"
  8. LIST_ENTRY mRedfishDiscoverList;
  9. LIST_ENTRY mRedfishInstanceList;
  10. EFI_SMBIOS_PROTOCOL *mSmbios = NULL;
  11. UINTN mNumNetworkInterface = 0;
  12. UINTN mNumRestExInstance = 0;
  13. LIST_ENTRY mEfiRedfishDiscoverNetworkInterface;
  14. LIST_ENTRY mEfiRedfishDiscoverRestExInstance;
  15. EFI_GUID mRedfishDiscoverTcp4InstanceGuid = EFI_REDFISH_DISCOVER_TCP4_INSTANCE_GUID;
  16. EFI_GUID mRedfishDiscoverTcp6InstanceGuid = EFI_REDFISH_DISCOVER_TCP6_INSTANCE_GUID;
  17. EFI_GUID mRedfishDiscoverRestExInstanceGuid = EFI_REDFISH_DISCOVER_REST_EX_INSTANCE_GUID;
  18. EFI_STATUS
  19. EFIAPI
  20. Tcp4GetSubnetInfo (
  21. IN EFI_HANDLE ImageHandle,
  22. IN EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *Instance
  23. );
  24. EFI_STATUS
  25. EFIAPI
  26. Tcp6GetSubnetInfo (
  27. IN EFI_HANDLE ImageHandle,
  28. IN EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *Instance
  29. );
  30. static REDFISH_DISCOVER_REQUIRED_PROTOCOL gRequiredProtocol[] = {
  31. {
  32. ProtocolTypeTcp4,
  33. L"TCP4 Service Binding Protocol",
  34. &gEfiTcp4ProtocolGuid,
  35. &gEfiTcp4ServiceBindingProtocolGuid,
  36. &mRedfishDiscoverTcp4InstanceGuid,
  37. Tcp4GetSubnetInfo
  38. },
  39. {
  40. ProtocolTypeTcp6,
  41. L"TCP6 Service Binding Protocol",
  42. &gEfiTcp6ProtocolGuid,
  43. &gEfiTcp6ServiceBindingProtocolGuid,
  44. &mRedfishDiscoverTcp6InstanceGuid,
  45. Tcp6GetSubnetInfo
  46. },
  47. {
  48. ProtocolTypeRestEx,
  49. L"REST EX Service Binding Protocol",
  50. &gEfiRestExProtocolGuid,
  51. &gEfiRestExServiceBindingProtocolGuid,
  52. &mRedfishDiscoverRestExInstanceGuid,
  53. NULL
  54. }
  55. };
  56. /**
  57. This function creates REST EX instance for the found Resfish service.
  58. by known owner handle.
  59. @param[in] Instance EFI_REDFISH_DISCOVERED_INTERNAL_INSTANCE
  60. @param[in] Token Client token.
  61. @retval NULL Instance not found.
  62. @retval EFI_REDFISH_DISCOVERED_INTERNAL_INSTANCE The instance owned by this owner.
  63. **/
  64. EFI_STATUS
  65. CreateRestExInstance (
  66. IN EFI_REDFISH_DISCOVERED_INTERNAL_INSTANCE *Instance,
  67. IN EFI_REDFISH_DISCOVERED_TOKEN *Token
  68. )
  69. {
  70. EFI_STATUS Status;
  71. Status = RestExLibCreateChild (
  72. Instance->Owner,
  73. FixedPcdGetBool (PcdRedfishDiscoverAccessModeInBand) ? EfiRestExServiceInBandAccess : EfiRestExServiceOutOfBandAccess,
  74. EfiRestExConfigHttp,
  75. EfiRestExServiceRedfish,
  76. &Token->DiscoverList.RedfishInstances->Information.RedfishRestExHandle
  77. );
  78. return Status;
  79. }
  80. /**
  81. This function gets EFI_REDFISH_DISCOVERED_INTERNAL_INSTANCE
  82. by known owner handle.
  83. @param[in] ImageHandle Image handle owns EFI_REDFISH_DISCOVERED_INTERNAL_INSTANCE.
  84. @param[in] TargetNetworkInterface Target network interface used by this EFI_REDFISH_DISCOVERED_INTERNAL_INSTANCE.
  85. @param[in] DiscoverFlags EFI_REDFISH_DISCOVER_FLAG
  86. @retval NULL Instance not found.
  87. @retval EFI_REDFISH_DISCOVERED_INTERNAL_INSTANCE The instance owned by this owner.
  88. **/
  89. EFI_REDFISH_DISCOVERED_INTERNAL_INSTANCE *
  90. GetInstanceByOwner (
  91. IN EFI_HANDLE ImageHandle,
  92. IN EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *TargetNetworkInterface,
  93. IN EFI_REDFISH_DISCOVER_FLAG DiscoverFlags
  94. )
  95. {
  96. EFI_REDFISH_DISCOVERED_INTERNAL_INSTANCE *ThisInstance;
  97. if (IsListEmpty (&mRedfishDiscoverList)) {
  98. return NULL;
  99. }
  100. ThisInstance =
  101. (EFI_REDFISH_DISCOVERED_INTERNAL_INSTANCE *)GetFirstNode (&mRedfishDiscoverList);
  102. while (TRUE) {
  103. if ((ThisInstance->Owner == ImageHandle) &&
  104. (ThisInstance->DiscoverFlags == DiscoverFlags) &&
  105. (ThisInstance->NetworkInterface == TargetNetworkInterface))
  106. {
  107. return ThisInstance;
  108. }
  109. if (IsNodeAtEnd (&mRedfishDiscoverList, &ThisInstance->Entry)) {
  110. break;
  111. }
  112. ThisInstance =
  113. (EFI_REDFISH_DISCOVERED_INTERNAL_INSTANCE *)GetNextNode (&mRedfishDiscoverList, &ThisInstance->Entry);
  114. }
  115. return NULL;
  116. }
  117. /**
  118. This function gets the subnet information of this TCP4 instance.
  119. @param[in] ImageHandle EFI handle with this image.
  120. @param[in] Instance Instance of Network interface.
  121. @retval EFI_STATUS Get subnet information successfully.
  122. @retval Otherwise Fail to get subnet information.
  123. **/
  124. EFI_STATUS
  125. EFIAPI
  126. Tcp4GetSubnetInfo (
  127. IN EFI_HANDLE ImageHandle,
  128. IN EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *Instance
  129. )
  130. {
  131. EFI_STATUS Status;
  132. EFI_TCP4_PROTOCOL *Tcp4;
  133. EFI_TCP4_CONFIG_DATA Tcp4CfgData;
  134. EFI_TCP4_OPTION Tcp4Option;
  135. EFI_IP4_MODE_DATA IpModedata;
  136. UINT8 SubnetMaskIndex;
  137. UINT8 BitMask;
  138. UINT8 PrefixLength;
  139. BOOLEAN GotPrefixLength;
  140. if (Instance == NULL) {
  141. return EFI_INVALID_PARAMETER;
  142. }
  143. Tcp4 = (EFI_TCP4_PROTOCOL *)Instance->NetworkInterfaceProtocolInfo.NetworkProtocolInterface;
  144. ZeroMem ((VOID *)&Tcp4CfgData, sizeof (EFI_TCP4_CONFIG_DATA));
  145. ZeroMem ((VOID *)&Tcp4Option, sizeof (EFI_TCP4_OPTION));
  146. // Give a local host IP address just for getting subnet information.
  147. Tcp4CfgData.AccessPoint.UseDefaultAddress = TRUE;
  148. Tcp4CfgData.AccessPoint.RemoteAddress.Addr[0] = 127;
  149. Tcp4CfgData.AccessPoint.RemoteAddress.Addr[1] = 0;
  150. Tcp4CfgData.AccessPoint.RemoteAddress.Addr[2] = 0;
  151. Tcp4CfgData.AccessPoint.RemoteAddress.Addr[3] = 1;
  152. Tcp4CfgData.AccessPoint.RemotePort = 80;
  153. Tcp4CfgData.AccessPoint.ActiveFlag = TRUE;
  154. Tcp4CfgData.ControlOption = &Tcp4Option;
  155. Tcp4Option.ReceiveBufferSize = 65535;
  156. Tcp4Option.SendBufferSize = 65535;
  157. Tcp4Option.MaxSynBackLog = 5;
  158. Tcp4Option.ConnectionTimeout = 60;
  159. Tcp4Option.DataRetries = 12;
  160. Tcp4Option.FinTimeout = 2;
  161. Tcp4Option.KeepAliveProbes = 6;
  162. Tcp4Option.KeepAliveTime = 7200;
  163. Tcp4Option.KeepAliveInterval = 30;
  164. Tcp4Option.EnableNagle = TRUE;
  165. Status = Tcp4->Configure (Tcp4, &Tcp4CfgData);
  166. if (EFI_ERROR (Status)) {
  167. DEBUG ((DEBUG_ERROR, "%a: Can't get subnet information\n", __FUNCTION__));
  168. return Status;
  169. }
  170. Status = Tcp4->GetModeData (Tcp4, NULL, NULL, &IpModedata, NULL, NULL);
  171. if (EFI_ERROR (Status)) {
  172. DEBUG ((DEBUG_ERROR, "%a: Can't get IP mode data information\n", __FUNCTION__));
  173. return Status;
  174. }
  175. IP4_COPY_ADDRESS (&Instance->SubnetMask, &IpModedata.ConfigData.SubnetMask);
  176. Instance->SubnetAddr.v4.Addr[0] = IpModedata.ConfigData.StationAddress.Addr[0] & Instance->SubnetMask.v4.Addr[0];
  177. Instance->SubnetAddr.v4.Addr[1] = IpModedata.ConfigData.StationAddress.Addr[1] & Instance->SubnetMask.v4.Addr[1];
  178. Instance->SubnetAddr.v4.Addr[2] = IpModedata.ConfigData.StationAddress.Addr[2] & Instance->SubnetMask.v4.Addr[2];
  179. Instance->SubnetAddr.v4.Addr[3] = IpModedata.ConfigData.StationAddress.Addr[3] & Instance->SubnetMask.v4.Addr[3];
  180. //
  181. // Calculate the subnet mask prefix.
  182. //
  183. GotPrefixLength = FALSE;
  184. PrefixLength = 0;
  185. SubnetMaskIndex = 0;
  186. while (GotPrefixLength == FALSE && SubnetMaskIndex < 4) {
  187. BitMask = 0x80;
  188. while (BitMask != 0) {
  189. if ((Instance->SubnetMask.v4.Addr[SubnetMaskIndex] & BitMask) != 0) {
  190. PrefixLength++;
  191. } else {
  192. GotPrefixLength = TRUE;
  193. break;
  194. }
  195. BitMask = BitMask >> 1;
  196. }
  197. SubnetMaskIndex++;
  198. }
  199. Instance->SubnetPrefixLength = PrefixLength;
  200. return EFI_SUCCESS;
  201. }
  202. /**
  203. This function gets the subnet information of this TCP6 instance.
  204. @param[in] ImageHandle EFI handle with this image.
  205. @param[in] Instance Instance of Network interface.
  206. @retval EFI_STATUS Get subnet information successfully.
  207. @retval Otherwise Fail to get subnet information.
  208. **/
  209. EFI_STATUS
  210. EFIAPI
  211. Tcp6GetSubnetInfo (
  212. IN EFI_HANDLE ImageHandle,
  213. IN EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *Instance
  214. )
  215. {
  216. EFI_STATUS Status;
  217. EFI_TCP6_PROTOCOL *Tcp6;
  218. EFI_IP6_MODE_DATA IpModedata;
  219. if (Instance == NULL) {
  220. return EFI_INVALID_PARAMETER;
  221. }
  222. Tcp6 = (EFI_TCP6_PROTOCOL *)Instance->NetworkInterfaceProtocolInfo.NetworkProtocolInterface;
  223. Status = Tcp6->GetModeData (Tcp6, NULL, NULL, &IpModedata, NULL, NULL);
  224. if (EFI_ERROR (Status)) {
  225. DEBUG ((DEBUG_ERROR, "%a: Can't get IP mode data information\n", __FUNCTION__));
  226. return Status;
  227. }
  228. if (IpModedata.AddressCount == 0) {
  229. DEBUG ((DEBUG_INFO, "%a: No IPv6 address configured.\n", __FUNCTION__));
  230. }
  231. if (Instance->SubnetAddrInfoIPv6 != NULL) {
  232. FreePool (Instance->SubnetAddrInfoIPv6);
  233. }
  234. Instance->SubnetAddrInfoIPv6 = AllocateZeroPool (IpModedata.AddressCount * sizeof (EFI_IP6_ADDRESS_INFO));
  235. if (Instance->SubnetAddrInfoIPv6 == NULL) {
  236. DEBUG ((DEBUG_ERROR, "%a: Failed to allocate memory for IPv6 subnet address information\n", __FUNCTION__));
  237. return EFI_OUT_OF_RESOURCES;
  238. }
  239. Instance->SubnetAddrInfoIPv6Number = IpModedata.AddressCount;
  240. CopyMem (
  241. (VOID *)Instance->SubnetAddrInfoIPv6,
  242. (VOID *)&IpModedata.AddressList,
  243. IpModedata.AddressCount * sizeof (EFI_IP6_ADDRESS_INFO)
  244. );
  245. FreePool (IpModedata.AddressList);
  246. return EFI_SUCCESS;
  247. }
  248. /**
  249. This function searches EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL
  250. instance with the given EFI_REDFISH_DISCOVER_NETWORK_INTERFACE.
  251. @param[in] TargetNetworkInterface EFI_REDFISH_DISCOVER_NETWORK_INTERFACE.
  252. NULL for all EFI_REDFISH_DISCOVER_NETWORK_INTERFACEs.
  253. @retval Non-NULL EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL is returned.
  254. @retval NULL Non of EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL instance is returned.
  255. **/
  256. EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *
  257. GetTargetNetworkInterfaceInternal (
  258. IN EFI_REDFISH_DISCOVER_NETWORK_INTERFACE *TargetNetworkInterface
  259. )
  260. {
  261. EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *ThisNetworkInterface;
  262. ThisNetworkInterface = (EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *)GetFirstNode (&mEfiRedfishDiscoverNetworkInterface);
  263. while (TRUE) {
  264. if (CompareMem ((VOID *)&ThisNetworkInterface->MacAddress, &TargetNetworkInterface->MacAddress, ThisNetworkInterface->HwAddressSize) == 0) {
  265. return ThisNetworkInterface;
  266. }
  267. if (IsNodeAtEnd (&mEfiRedfishDiscoverNetworkInterface, &ThisNetworkInterface->Entry)) {
  268. return NULL;
  269. }
  270. ThisNetworkInterface = (EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *)GetNextNode (&mEfiRedfishDiscoverNetworkInterface, &ThisNetworkInterface->Entry);
  271. }
  272. return NULL;
  273. }
  274. /**
  275. This function searches EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL
  276. instance with the given Controller handle.
  277. @param[in] ControllerHandle The controller handle associated with network interface.
  278. @retval Non-NULL EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL is returned.
  279. @retval NULL Non of EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL instance is returned.
  280. **/
  281. EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *
  282. GetTargetNetworkInterfaceInternalByController (
  283. IN EFI_HANDLE ControllerHandle
  284. )
  285. {
  286. EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *ThisNetworkInterface;
  287. ThisNetworkInterface = (EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *)GetFirstNode (&mEfiRedfishDiscoverNetworkInterface);
  288. while (TRUE) {
  289. if (ThisNetworkInterface->OpenDriverControllerHandle == ControllerHandle) {
  290. return ThisNetworkInterface;
  291. }
  292. if (IsNodeAtEnd (&mEfiRedfishDiscoverNetworkInterface, &ThisNetworkInterface->Entry)) {
  293. return NULL;
  294. }
  295. ThisNetworkInterface = (EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *)GetNextNode (&mEfiRedfishDiscoverNetworkInterface, &ThisNetworkInterface->Entry);
  296. }
  297. return NULL;
  298. }
  299. /**
  300. This function validate if target network interface is ready for discovering
  301. Redfish service.
  302. @param[in] TargetNetworkInterface EFI_REDFISH_DISCOVER_NETWORK_INTERFACE.
  303. NULL for all EFI_REDFISH_DISCOVER_NETWORK_INTERFACEs.
  304. @param[in] Flags EFI_REDFISH_DISCOVER_FLAG
  305. @retval EFI_SUCCESS Target network interface is ready to use.
  306. @retval EFI_UNSUPPORTED Target network interface is not ready to use.
  307. **/
  308. EFI_STATUS
  309. ValidateTargetNetworkInterface (
  310. IN EFI_REDFISH_DISCOVER_NETWORK_INTERFACE *TargetNetworkInterface,
  311. IN EFI_REDFISH_DISCOVER_FLAG Flags
  312. )
  313. {
  314. EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *ThisNetworkInterface;
  315. if (IsListEmpty (&mEfiRedfishDiscoverNetworkInterface) && (TargetNetworkInterface == NULL)) {
  316. return EFI_UNSUPPORTED;
  317. }
  318. if (TargetNetworkInterface == NULL) {
  319. return EFI_SUCCESS; // Return EFI_SUCCESS if no network interface is specified.
  320. }
  321. ThisNetworkInterface = (EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *)GetFirstNode (&mEfiRedfishDiscoverNetworkInterface);
  322. while (TRUE) {
  323. if (CompareMem ((VOID *)&ThisNetworkInterface->MacAddress, &TargetNetworkInterface->MacAddress, ThisNetworkInterface->HwAddressSize) == 0) {
  324. break;
  325. }
  326. if (IsNodeAtEnd (&mEfiRedfishDiscoverNetworkInterface, &ThisNetworkInterface->Entry)) {
  327. return EFI_UNSUPPORTED;
  328. }
  329. ThisNetworkInterface = (EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *)GetNextNode (&mEfiRedfishDiscoverNetworkInterface, &ThisNetworkInterface->Entry);
  330. }
  331. if ((Flags & EFI_REDFISH_DISCOVER_SSDP) != 0) {
  332. // Validate if UDP4/6 is supported on the given network interface.
  333. // SSDP is not supported.
  334. return EFI_SUCCESS;
  335. }
  336. if (ThisNetworkInterface->NetworkInterfaceProtocolInfo.ProtocolControllerHandle == NULL) {
  337. return EFI_UNSUPPORTED; // The required protocol on this network interface is not found.
  338. }
  339. return EFI_SUCCESS;
  340. }
  341. /**
  342. This function returns number of network interface instance.
  343. @retval UINTN Number of network interface instances.
  344. **/
  345. UINTN
  346. NumberOfNetworkInterface (
  347. VOID
  348. )
  349. {
  350. UINTN Num;
  351. EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *ThisNetworkInterface;
  352. if (IsListEmpty (&mEfiRedfishDiscoverNetworkInterface)) {
  353. return 0;
  354. }
  355. Num = 1;
  356. ThisNetworkInterface = (EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *)GetFirstNode (&mEfiRedfishDiscoverNetworkInterface);
  357. while (TRUE) {
  358. if (IsNodeAtEnd (&mEfiRedfishDiscoverNetworkInterface, &ThisNetworkInterface->Entry)) {
  359. break;
  360. }
  361. ThisNetworkInterface = (EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *)GetNextNode (&mEfiRedfishDiscoverNetworkInterface, &ThisNetworkInterface->Entry);
  362. Num++;
  363. }
  364. return Num;
  365. }
  366. /**
  367. This function checks the IP version supported on this
  368. netwoek interface.
  369. @param[in] ThisNetworkInterface EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL
  370. @retval TRUE Is IPv6, otherwise IPv4.
  371. **/
  372. BOOLEAN
  373. CheckIsIpVersion6 (
  374. IN EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *ThisNetworkInterface
  375. )
  376. {
  377. if (ThisNetworkInterface->NetworkProtocolType == ProtocolTypeTcp6) {
  378. return TRUE;
  379. }
  380. return FALSE;
  381. }
  382. /**
  383. This function discover Redfish service through SMBIOS host interface.
  384. @param[in] Instance EFI_REDFISH_DISCOVERED_INTERNAL_INSTANCE
  385. @retval EFI_SUCCESS Redfish service is discovered through SMBIOS Host interface.
  386. @retval Others Fail to discover Redfish service throught SMBIOS host interface
  387. **/
  388. EFI_STATUS
  389. DiscoverRedfishHostInterface (
  390. IN EFI_REDFISH_DISCOVERED_INTERNAL_INSTANCE *Instance
  391. )
  392. {
  393. EFI_STATUS Status;
  394. REDFISH_OVER_IP_PROTOCOL_DATA *Data;
  395. REDFISH_INTERFACE_DATA *DeviceDescriptor;
  396. CHAR8 UuidStr[sizeof "00000000-0000-0000-0000-000000000000" + 1];
  397. CHAR16 Ipv6Str[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" + 1];
  398. CHAR8 RedfishServiceLocateStr[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" + 1];
  399. UINTN StrSize;
  400. UINTN MacCompareStstus;
  401. BOOLEAN IsHttps;
  402. Data = NULL;
  403. DeviceDescriptor = NULL;
  404. if (mSmbios == NULL) {
  405. Status = gBS->LocateProtocol (&gEfiSmbiosProtocolGuid, NULL, (VOID **)&mSmbios);
  406. if (EFI_ERROR (Status)) {
  407. return Status;
  408. }
  409. }
  410. Status = RedfishGetHostInterfaceProtocolData (mSmbios, &DeviceDescriptor, &Data); // Search for SMBIOS type 42h
  411. if (!EFI_ERROR (Status) && (Data != NULL) && (DeviceDescriptor != NULL)) {
  412. //
  413. // Chceck if we can reach out Redfish service using this network interface.
  414. // Check with MAC address using Device Descroptor Data Device Type 04 and Type 05.
  415. // Those two types of Redfish host interface device has MAC information.
  416. //
  417. if (DeviceDescriptor->DeviceType == REDFISH_HOST_INTERFACE_DEVICE_TYPE_PCI_PCIE_V2) {
  418. MacCompareStstus = CompareMem (&Instance->NetworkInterface->MacAddress, &DeviceDescriptor->DeviceDescriptor.PciPcieDeviceV2.MacAddress, 6);
  419. } else if (DeviceDescriptor->DeviceType == REDFISH_HOST_INTERFACE_DEVICE_TYPE_USB_V2) {
  420. MacCompareStstus = CompareMem (&Instance->NetworkInterface->MacAddress, &DeviceDescriptor->DeviceDescriptor.UsbDeviceV2.MacAddress, 6);
  421. } else {
  422. return EFI_UNSUPPORTED;
  423. }
  424. if (MacCompareStstus != 0) {
  425. return EFI_UNSUPPORTED;
  426. }
  427. if (Data->RedfishServiceIpAddressFormat == 1) {
  428. IP4_COPY_ADDRESS ((VOID *)&Instance->TargetIpAddress.v4, (VOID *)Data->RedfishServiceIpAddress);
  429. } else {
  430. IP6_COPY_ADDRESS ((VOID *)&Instance->TargetIpAddress.v6, (VOID *)Data->RedfishServiceIpAddress);
  431. }
  432. if (Instance->HostIntfValidation) {
  433. DEBUG ((DEBUG_ERROR, "%a:Send UPnP unicast SSDP to validate this Redfish Host Interface is not supported.\n", __FUNCTION__));
  434. Status = EFI_UNSUPPORTED;
  435. } else {
  436. //
  437. // Add this istance to list without detial information of Redfish
  438. // service.
  439. //
  440. IsHttps = FALSE;
  441. if (Data->RedfishServiceIpPort == 443) {
  442. IsHttps = TRUE;
  443. }
  444. StrSize = sizeof (UuidStr);
  445. AsciiSPrint (UuidStr, StrSize, "%g", &Data->ServiceUuid);
  446. //
  447. // Generate Redfish service location string.
  448. //
  449. if (Data->RedfishServiceIpAddressFormat == REDFISH_HOST_INTERFACE_HOST_IP_ADDRESS_FORMAT_IP6) {
  450. NetLibIp6ToStr ((IPv6_ADDRESS *)&Data->RedfishServiceIpAddress, Ipv6Str, sizeof (Ipv6Str));
  451. if ((Data->RedfishServiceIpPort == 0) || (IsHttps == TRUE)) {
  452. AsciiSPrintUnicodeFormat (
  453. RedfishServiceLocateStr,
  454. sizeof (RedfishServiceLocateStr),
  455. L"%s",
  456. Ipv6Str
  457. );
  458. } else {
  459. AsciiSPrintUnicodeFormat (
  460. RedfishServiceLocateStr,
  461. sizeof (RedfishServiceLocateStr),
  462. L"[%s]:%d",
  463. Ipv6Str,
  464. Data->RedfishServiceIpPort
  465. );
  466. }
  467. } else {
  468. if ((Data->RedfishServiceIpPort == 0) || (IsHttps == TRUE)) {
  469. AsciiSPrint (
  470. RedfishServiceLocateStr,
  471. sizeof (RedfishServiceLocateStr),
  472. "%d.%d.%d.%d",
  473. Data->RedfishServiceIpAddress[0],
  474. Data->RedfishServiceIpAddress[1],
  475. Data->RedfishServiceIpAddress[2],
  476. Data->RedfishServiceIpAddress[3]
  477. );
  478. } else {
  479. AsciiSPrint (
  480. RedfishServiceLocateStr,
  481. sizeof (RedfishServiceLocateStr),
  482. "%d.%d.%d.%d:%d",
  483. Data->RedfishServiceIpAddress[0],
  484. Data->RedfishServiceIpAddress[1],
  485. Data->RedfishServiceIpAddress[2],
  486. Data->RedfishServiceIpAddress[3],
  487. Data->RedfishServiceIpPort
  488. );
  489. }
  490. }
  491. Status = AddAndSignalNewRedfishService (
  492. Instance,
  493. NULL,
  494. RedfishServiceLocateStr,
  495. UuidStr,
  496. NULL,
  497. NULL,
  498. NULL,
  499. NULL,
  500. IsHttps
  501. );
  502. }
  503. }
  504. return Status;
  505. }
  506. /**
  507. The function adds a new found Redfish service to internal list and
  508. notify client.
  509. @param[in] Instance EFI_REDFISH_DISCOVERED_INTERNAL_INSTANCE.
  510. @param[in] RedfishVersion Redfish version.
  511. @param[in] RedfishLocation Redfish location.
  512. @param[in] Uuid Service UUID string.
  513. @param[in] Os OS string.
  514. @param[in] OsVer OS version string.
  515. @param[in] Product Product string.
  516. @param[in] ProductVer Product verison string.
  517. @param[in] UseHttps Redfish service requires secured connection.
  518. @retval EFI_SUCCESS Redfish service is added to list successfully.
  519. **/
  520. EFI_STATUS
  521. AddAndSignalNewRedfishService (
  522. IN EFI_REDFISH_DISCOVERED_INTERNAL_INSTANCE *Instance,
  523. IN UINTN *RedfishVersion OPTIONAL,
  524. IN CHAR8 *RedfishLocation OPTIONAL,
  525. IN CHAR8 *Uuid OPTIONAL,
  526. IN CHAR8 *Os OPTIONAL,
  527. IN CHAR8 *OsVer OPTIONAL,
  528. IN CHAR8 *Product OPTIONAL,
  529. IN CHAR8 *ProductVer OPTIONAL,
  530. IN BOOLEAN UseHttps
  531. )
  532. {
  533. BOOLEAN NewFound;
  534. BOOLEAN InfoRefresh;
  535. BOOLEAN RestExOpened;
  536. BOOLEAN DeleteRestEx;
  537. EFI_STATUS Status;
  538. EFI_REDFISH_DISCOVERED_INTERNAL_LIST *DiscoveredList;
  539. EFI_REDFISH_DISCOVERED_INSTANCE *DiscoveredInstance;
  540. CHAR16 *Char16Uuid;
  541. EFI_REST_EX_PROTOCOL *RestEx;
  542. EFI_REST_EX_HTTP_CONFIG_DATA *RestExHttpConfigData;
  543. EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *NetworkInterface;
  544. NewFound = TRUE;
  545. InfoRefresh = FALSE;
  546. Char16Uuid = NULL;
  547. RestExOpened = FALSE;
  548. DeleteRestEx = FALSE;
  549. DEBUG ((DEBUG_INFO, "%a:Add this instance to Redfish instance list.\n", __FUNCTION__));
  550. if (Uuid != NULL) {
  551. Char16Uuid = (CHAR16 *)AllocateZeroPool (AsciiStrSize ((const CHAR8 *)Uuid) * sizeof (CHAR16));
  552. AsciiStrToUnicodeStrS ((const CHAR8 *)Uuid, Char16Uuid, AsciiStrSize ((const CHAR8 *)Uuid) * sizeof (CHAR16));
  553. }
  554. DiscoveredList = NULL;
  555. DiscoveredInstance = NULL;
  556. RestExHttpConfigData = NULL;
  557. NetworkInterface = Instance->NetworkInterface;
  558. if (!IsListEmpty (&mRedfishInstanceList)) {
  559. //
  560. // Is this a duplicate redfish service.
  561. //
  562. DiscoveredList = (EFI_REDFISH_DISCOVERED_INTERNAL_LIST *)GetFirstNode (&mRedfishInstanceList);
  563. NewFound = FALSE;
  564. do {
  565. if ((Char16Uuid == NULL) || (DiscoveredList->Instance->Information.Uuid == NULL)) {
  566. //
  567. // Check if this Redfish instance already found using IP addrress.
  568. //
  569. if (!CheckIsIpVersion6 (NetworkInterface)) {
  570. if (CompareMem (
  571. (VOID *)&Instance->TargetIpAddress.v4,
  572. (VOID *)&DiscoveredList->Instance->Information.RedfishHostIpAddress.v4,
  573. sizeof (EFI_IPv4_ADDRESS)
  574. ) == 0)
  575. {
  576. DiscoveredInstance = DiscoveredList->Instance;
  577. if ((DiscoveredList->Instance->Information.Uuid == NULL) &&
  578. (Char16Uuid != NULL))
  579. {
  580. InfoRefresh = TRUE;
  581. DiscoveredInstance = DiscoveredList->Instance;
  582. DEBUG ((DEBUG_INFO, "*** This Redfish Service information refresh ***\n"));
  583. }
  584. break;
  585. }
  586. } else {
  587. if (CompareMem (
  588. (VOID *)&Instance->TargetIpAddress.v6,
  589. (VOID *)&DiscoveredList->Instance->Information.RedfishHostIpAddress.v6,
  590. sizeof (EFI_IPv6_ADDRESS)
  591. ) == 0)
  592. {
  593. DiscoveredInstance = DiscoveredList->Instance;
  594. break;
  595. }
  596. }
  597. } else {
  598. //
  599. // Check if this Redfish instance already found using UUID.
  600. //
  601. if (StrCmp ((const CHAR16 *)Char16Uuid, (const CHAR16 *)DiscoveredList->Instance->Information.Uuid) == 0) {
  602. DiscoveredInstance = DiscoveredList->Instance;
  603. break;
  604. }
  605. }
  606. if (IsNodeAtEnd (&mRedfishInstanceList, &DiscoveredList->NextInstance)) {
  607. NewFound = TRUE;
  608. break;
  609. }
  610. DiscoveredList = (EFI_REDFISH_DISCOVERED_INTERNAL_LIST *)GetNextNode (&mRedfishInstanceList, &DiscoveredList->NextInstance);
  611. } while (TRUE);
  612. }
  613. if (NewFound || InfoRefresh) {
  614. if (!InfoRefresh) {
  615. DiscoveredList = (EFI_REDFISH_DISCOVERED_INTERNAL_LIST *)AllocateZeroPool (sizeof (EFI_REDFISH_DISCOVERED_INTERNAL_LIST));
  616. if (DiscoveredList == NULL) {
  617. return EFI_OUT_OF_RESOURCES;
  618. }
  619. InitializeListHead (&DiscoveredList->NextInstance);
  620. DiscoveredInstance = (EFI_REDFISH_DISCOVERED_INSTANCE *)AllocateZeroPool (sizeof (EFI_REDFISH_DISCOVERED_INSTANCE));
  621. if (DiscoveredInstance == NULL) {
  622. FreePool ((VOID *)DiscoveredList);
  623. return EFI_OUT_OF_RESOURCES;
  624. }
  625. }
  626. DEBUG ((DEBUG_INFO, "*** Redfish Service Information ***\n"));
  627. DiscoveredInstance->Information.UseHttps = UseHttps;
  628. if (RedfishVersion != NULL) {
  629. DiscoveredInstance->Information.RedfishVersion = *RedfishVersion;
  630. DEBUG ((DEBUG_INFO, "Redfish service version: %d.\n", DiscoveredInstance->Information.RedfishVersion));
  631. }
  632. if (RedfishLocation != NULL) {
  633. DiscoveredInstance->Information.Location = (CHAR16 *)AllocatePool (AsciiStrSize ((const CHAR8 *)RedfishLocation) * sizeof (CHAR16));
  634. AsciiStrToUnicodeStrS ((const CHAR8 *)RedfishLocation, DiscoveredInstance->Information.Location, AsciiStrSize ((const CHAR8 *)RedfishLocation) * sizeof (CHAR16));
  635. DEBUG ((DEBUG_INFO, "Redfish service location: %s.\n", DiscoveredInstance->Information.Location));
  636. }
  637. if (Uuid != NULL) {
  638. DiscoveredInstance->Information.Uuid = (CHAR16 *)AllocatePool (AsciiStrSize ((const CHAR8 *)Uuid) * sizeof (CHAR16));
  639. AsciiStrToUnicodeStrS ((const CHAR8 *)Uuid, DiscoveredInstance->Information.Uuid, AsciiStrSize ((const CHAR8 *)Uuid) * sizeof (CHAR16));
  640. DEBUG ((DEBUG_INFO, "Service UUID: %s.\n", DiscoveredInstance->Information.Uuid));
  641. }
  642. if (Os != NULL) {
  643. DiscoveredInstance->Information.Os = (CHAR16 *)AllocatePool (AsciiStrSize ((const CHAR8 *)Os) * sizeof (CHAR16));
  644. AsciiStrToUnicodeStrS ((const CHAR8 *)Os, DiscoveredInstance->Information.Os, AsciiStrSize ((const CHAR8 *)Os) * sizeof (CHAR16));
  645. DEBUG ((DEBUG_INFO, "Redfish service OS: %s, Version:%s.\n", DiscoveredInstance->Information.Os, DiscoveredInstance->Information.OsVersion));
  646. }
  647. if (OsVer != NULL) {
  648. DiscoveredInstance->Information.OsVersion = (CHAR16 *)AllocatePool (AsciiStrSize ((const CHAR8 *)OsVer) * sizeof (CHAR16));
  649. AsciiStrToUnicodeStrS ((const CHAR8 *)OsVer, DiscoveredInstance->Information.OsVersion, AsciiStrSize ((const CHAR8 *)OsVer) * sizeof (CHAR16));
  650. }
  651. if ((Product != NULL) && (ProductVer != NULL)) {
  652. DiscoveredInstance->Information.Product = (CHAR16 *)AllocatePool (AsciiStrSize ((const CHAR8 *)Product) * sizeof (CHAR16));
  653. AsciiStrToUnicodeStrS ((const CHAR8 *)Product, DiscoveredInstance->Information.Product, AsciiStrSize ((const CHAR8 *)Product) * sizeof (CHAR16));
  654. DiscoveredInstance->Information.ProductVer = (CHAR16 *)AllocatePool (AsciiStrSize ((const CHAR8 *)ProductVer) * sizeof (CHAR16));
  655. AsciiStrToUnicodeStrS ((const CHAR8 *)ProductVer, DiscoveredInstance->Information.ProductVer, AsciiStrSize ((const CHAR8 *)ProductVer) * sizeof (CHAR16));
  656. DEBUG ((DEBUG_INFO, "Redfish service product: %s, Version:%s.\n", DiscoveredInstance->Information.Product, DiscoveredInstance->Information.ProductVer));
  657. }
  658. if (RedfishLocation == NULL) {
  659. // This is the Redfish reported from SMBIOS 42h
  660. // without validation.
  661. IP4_COPY_ADDRESS ((VOID *)&DiscoveredInstance->Information.RedfishHostIpAddress.v4, (VOID *)&Instance->TargetIpAddress.v4);
  662. }
  663. if (!InfoRefresh) {
  664. DiscoveredList->Instance = DiscoveredInstance;
  665. InsertTailList (&mRedfishInstanceList, &DiscoveredList->NextInstance);
  666. }
  667. DiscoveredInstance->Status = EFI_SUCCESS;
  668. } else {
  669. if (DiscoveredList != NULL) {
  670. DEBUG ((DEBUG_INFO, "*** This Redfish Service was already found ***\n"));
  671. if (DiscoveredInstance->Information.Uuid != NULL) {
  672. DEBUG ((DEBUG_INFO, "Service UUID: %s.\n", DiscoveredInstance->Information.Uuid));
  673. } else {
  674. DEBUG ((DEBUG_INFO, "Service UUID: unknown.\n"));
  675. }
  676. }
  677. }
  678. if (Char16Uuid != NULL) {
  679. FreePool ((VOID *)Char16Uuid);
  680. }
  681. Status = EFI_SUCCESS;
  682. if (NewFound || InfoRefresh) {
  683. //
  684. // Build up EFI_REDFISH_DISCOVERED_LIST in token.
  685. //
  686. Instance->DiscoverToken->DiscoverList.NumberOfServiceFound = 1;
  687. Instance->DiscoverToken->DiscoverList.RedfishInstances = DiscoveredInstance;
  688. DiscoveredInstance->Status = EFI_SUCCESS;
  689. if (!InfoRefresh) {
  690. Status = CreateRestExInstance (Instance, Instance->DiscoverToken); // Create REST EX child.
  691. if (EFI_ERROR (Status)) {
  692. DEBUG ((DEBUG_ERROR, "%a:Can't create REST EX child instance.\n", __FUNCTION__));
  693. goto ON_EXIT;
  694. }
  695. Status = gBS->OpenProtocol (
  696. // Configure local host information.
  697. Instance->DiscoverToken->DiscoverList.RedfishInstances->Information.RedfishRestExHandle,
  698. &gEfiRestExProtocolGuid,
  699. (VOID **)&RestEx,
  700. Instance->NetworkInterface->OpenDriverAgentHandle,
  701. Instance->NetworkInterface->OpenDriverControllerHandle,
  702. EFI_OPEN_PROTOCOL_BY_DRIVER
  703. );
  704. if (EFI_ERROR (Status)) {
  705. DeleteRestEx = TRUE;
  706. goto ERROR_EXIT;
  707. }
  708. RestExOpened = TRUE;
  709. RestExHttpConfigData = AllocateZeroPool (sizeof (EFI_REST_EX_HTTP_CONFIG_DATA));
  710. if (RestExHttpConfigData == NULL) {
  711. Status = EFI_OUT_OF_RESOURCES;
  712. DeleteRestEx = TRUE;
  713. goto EXIT_FREE_CONFIG_DATA;
  714. }
  715. RestExHttpConfigData->SendReceiveTimeout = 5000;
  716. RestExHttpConfigData->HttpConfigData.HttpVersion = HttpVersion11;
  717. RestExHttpConfigData->HttpConfigData.LocalAddressIsIPv6 = CheckIsIpVersion6 (NetworkInterface);
  718. if (RestExHttpConfigData->HttpConfigData.LocalAddressIsIPv6) {
  719. RestExHttpConfigData->HttpConfigData.AccessPoint.IPv6Node = AllocateZeroPool (sizeof (EFI_HTTPv6_ACCESS_POINT));
  720. if (RestExHttpConfigData->HttpConfigData.AccessPoint.IPv6Node == NULL) {
  721. Status = EFI_OUT_OF_RESOURCES;
  722. goto EXIT_FREE_CONFIG_DATA;
  723. }
  724. } else {
  725. RestExHttpConfigData->HttpConfigData.AccessPoint.IPv4Node = AllocateZeroPool (sizeof (EFI_HTTPv4_ACCESS_POINT));
  726. if (RestExHttpConfigData->HttpConfigData.AccessPoint.IPv4Node == NULL) {
  727. Status = EFI_OUT_OF_RESOURCES;
  728. goto EXIT_FREE_CONFIG_DATA;
  729. }
  730. RestExHttpConfigData->HttpConfigData.AccessPoint.IPv4Node->UseDefaultAddress = TRUE;
  731. }
  732. Status = RestEx->Configure (
  733. RestEx,
  734. (EFI_REST_EX_CONFIG_DATA)(UINT8 *)RestExHttpConfigData
  735. );
  736. if (EFI_ERROR (Status)) {
  737. DEBUG ((DEBUG_ERROR, "%a:REST EX configured..\n", __FUNCTION__));
  738. DeleteRestEx = TRUE;
  739. goto EXIT_FREE_ALL;
  740. }
  741. //
  742. // Signal client, close REST EX before signaling client.
  743. //
  744. if (RestExOpened) {
  745. gBS->CloseProtocol (
  746. Instance->DiscoverToken->DiscoverList.RedfishInstances->Information.RedfishRestExHandle,
  747. &gEfiRestExProtocolGuid,
  748. Instance->NetworkInterface->OpenDriverAgentHandle,
  749. Instance->NetworkInterface->OpenDriverControllerHandle
  750. );
  751. RestExOpened = FALSE;
  752. }
  753. }
  754. Status = gBS->SignalEvent (Instance->DiscoverToken->Event);
  755. if (!EFI_ERROR (Status)) {
  756. DEBUG ((DEBUG_ERROR, "%a:No event to signal!\n", __FUNCTION__));
  757. }
  758. }
  759. EXIT_FREE_ALL:;
  760. if ((RestExHttpConfigData != NULL) && (RestExHttpConfigData->HttpConfigData.AccessPoint.IPv4Node != NULL)) {
  761. FreePool (RestExHttpConfigData->HttpConfigData.AccessPoint.IPv4Node);
  762. }
  763. EXIT_FREE_CONFIG_DATA:;
  764. if (RestExHttpConfigData != NULL) {
  765. FreePool ((VOID *)RestExHttpConfigData);
  766. }
  767. if (RestExOpened) {
  768. gBS->CloseProtocol (
  769. Instance->DiscoverToken->DiscoverList.RedfishInstances->Information.RedfishRestExHandle,
  770. &gEfiRestExProtocolGuid,
  771. Instance->NetworkInterface->OpenDriverAgentHandle,
  772. Instance->NetworkInterface->OpenDriverControllerHandle
  773. );
  774. }
  775. ERROR_EXIT:;
  776. if (DeleteRestEx && RestExOpened) {
  777. gBS->CloseProtocol (
  778. Instance->DiscoverToken->DiscoverList.RedfishInstances->Information.RedfishRestExHandle,
  779. &gEfiRestExProtocolGuid,
  780. Instance->NetworkInterface->OpenDriverAgentHandle,
  781. Instance->NetworkInterface->OpenDriverControllerHandle
  782. );
  783. }
  784. ON_EXIT:;
  785. return Status;
  786. }
  787. /**
  788. This function gets the subnet information of this network interface instance.
  789. can discover Redfish service on it.
  790. @param[in] Instance EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL instance.
  791. @param[in] ImageHandle EFI Image handle request the network interface list.
  792. @retval EFI_SUCCESS
  793. **/
  794. EFI_STATUS
  795. NetworkInterfaceGetSubnetInfo (
  796. IN EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *Instance,
  797. IN EFI_HANDLE ImageHandle
  798. )
  799. {
  800. EFI_STATUS Status;
  801. UINT32 ProtocolType;
  802. UINT32 IPv6InfoIndex;
  803. EFI_IP6_ADDRESS_INFO *ThisSubnetAddrInfoIPv6;
  804. EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *NewNetworkInterface;
  805. if (Instance->GotSubnetInfo) {
  806. return EFI_SUCCESS;
  807. }
  808. ProtocolType = Instance->NetworkProtocolType;
  809. if ((gRequiredProtocol[ProtocolType].GetSubnetInfo != NULL) && (Instance->GotSubnetInfo == FALSE)) {
  810. Status = gRequiredProtocol[ProtocolType].GetSubnetInfo (
  811. ImageHandle,
  812. Instance
  813. );
  814. if (EFI_ERROR (Status)) {
  815. DEBUG ((DEBUG_ERROR, "%a:Faile to get Subnet infomation.\n", __FUNCTION__));
  816. return Status;
  817. } else {
  818. DEBUG ((DEBUG_INFO, "%a:MAC address: %s\n", __FUNCTION__, Instance->StrMacAddr));
  819. if (CheckIsIpVersion6 (Instance)) {
  820. if (Instance->SubnetAddrInfoIPv6Number == 0) {
  821. DEBUG ((DEBUG_ERROR, "%a: There is no Subnet infomation for IPv6 network interface.\n", __FUNCTION__));
  822. return EFI_NOT_FOUND;
  823. }
  824. ThisSubnetAddrInfoIPv6 = Instance->SubnetAddrInfoIPv6; // First IPv6 address information.
  825. IP6_COPY_ADDRESS (&Instance->SubnetAddr.v6, &ThisSubnetAddrInfoIPv6->Address);
  826. Instance->SubnetPrefixLength = ThisSubnetAddrInfoIPv6->PrefixLength;
  827. DEBUG ((
  828. DEBUG_INFO,
  829. " IPv6 Subnet ID:%d, Prefix length: %d.\n",
  830. ThisSubnetAddrInfoIPv6->Address.Addr[7] + (UINT16)ThisSubnetAddrInfoIPv6->Address.Addr[6] * 256,
  831. ThisSubnetAddrInfoIPv6->PrefixLength
  832. )
  833. );
  834. //
  835. // If this is IPv6, then we may have to propagate network interface for IPv6 network scopes
  836. // according to the Ipv6 address information.
  837. //
  838. ThisSubnetAddrInfoIPv6++;
  839. for (IPv6InfoIndex = 0; IPv6InfoIndex < Instance->SubnetAddrInfoIPv6Number - 1; IPv6InfoIndex++) {
  840. //
  841. // Build up addtional EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL instances.
  842. //
  843. NewNetworkInterface = (EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *)AllocateZeroPool (sizeof (EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL));
  844. if (NewNetworkInterface != NULL) {
  845. CopyMem ((VOID *)NewNetworkInterface, (VOID *)Instance, sizeof (EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL)); // Clone information of first instance.
  846. IP6_COPY_ADDRESS (&NewNetworkInterface->SubnetAddr.v6, &ThisSubnetAddrInfoIPv6->Address);
  847. NewNetworkInterface->SubnetPrefixLength = ThisSubnetAddrInfoIPv6->PrefixLength;
  848. NewNetworkInterface->GotSubnetInfo = TRUE;
  849. InsertTailList (&mEfiRedfishDiscoverNetworkInterface, &NewNetworkInterface->Entry);
  850. ThisSubnetAddrInfoIPv6++;
  851. mNumNetworkInterface++;
  852. DEBUG ((
  853. DEBUG_INFO,
  854. " IPv6 Subnet ID:%d, Prefix length: %d.\n",
  855. ThisSubnetAddrInfoIPv6->Address.Addr[7] + (UINT16)ThisSubnetAddrInfoIPv6->Address.Addr[6] * 256,
  856. ThisSubnetAddrInfoIPv6->PrefixLength
  857. )
  858. );
  859. } else {
  860. return EFI_OUT_OF_RESOURCES;
  861. }
  862. }
  863. } else {
  864. DEBUG ((
  865. DEBUG_INFO,
  866. " IPv4 Subnet:%d.%d.%d.%d Subnet mask: %d.%d.%d.%d.\n",
  867. Instance->SubnetAddr.v4.Addr[0],
  868. Instance->SubnetAddr.v4.Addr[1],
  869. Instance->SubnetAddr.v4.Addr[2],
  870. Instance->SubnetAddr.v4.Addr[3],
  871. Instance->SubnetMask.v4.Addr[0],
  872. Instance->SubnetMask.v4.Addr[1],
  873. Instance->SubnetMask.v4.Addr[2],
  874. Instance->SubnetMask.v4.Addr[3]
  875. ));
  876. }
  877. }
  878. }
  879. Instance->GotSubnetInfo = TRUE; // Only try to get Subnet Info once.
  880. return EFI_SUCCESS;
  881. }
  882. /**
  883. This function gets the network interface list which Redfish discover protocol
  884. can discover Redfish service on it.
  885. @param[in] This EFI_REDFISH_DISCOVER_PROTOCOL instance.
  886. @param[in] ImageHandle EFI Image handle request the network interface list,
  887. @param[out] NumberOfNetworkIntfs Number of network interfaces can do Redfish service discovery.
  888. @param[out] NetworkIntfInstances Network interface instances. It's an array of instance. The number of entries
  889. in array is indicated by NumberOfNetworkIntfs.
  890. Caller has to release the memory
  891. allocated by Redfish discover protocol.
  892. @retval EFI_SUCCESS The information of network interface is returned in NumberOfNetworkIntfs and
  893. NetworkIntfInstances.
  894. @retval Others Fail to return the information of network interface.
  895. **/
  896. EFI_STATUS
  897. EFIAPI
  898. RedfishServiceGetNetworkInterface (
  899. IN EFI_REDFISH_DISCOVER_PROTOCOL *This,
  900. IN EFI_HANDLE ImageHandle,
  901. OUT UINTN *NumberOfNetworkIntfs,
  902. OUT EFI_REDFISH_DISCOVER_NETWORK_INTERFACE **NetworkIntfInstances
  903. )
  904. {
  905. EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *ThisNetworkInterfaceIntn;
  906. EFI_REDFISH_DISCOVER_NETWORK_INTERFACE *ThisNetworkInterface;
  907. if ((NetworkIntfInstances == NULL) || (NumberOfNetworkIntfs == NULL) || (ImageHandle == NULL)) {
  908. return EFI_INVALID_PARAMETER;
  909. }
  910. *NumberOfNetworkIntfs = 0;
  911. *NetworkIntfInstances = NULL;
  912. if (IsListEmpty ((const LIST_ENTRY *)&mEfiRedfishDiscoverNetworkInterface)) {
  913. return EFI_NOT_FOUND;
  914. }
  915. ThisNetworkInterface = (EFI_REDFISH_DISCOVER_NETWORK_INTERFACE *)AllocateZeroPool (sizeof (EFI_REDFISH_DISCOVER_NETWORK_INTERFACE) * mNumNetworkInterface);
  916. if (ThisNetworkInterface == NULL) {
  917. return EFI_OUT_OF_RESOURCES;
  918. }
  919. *NetworkIntfInstances = ThisNetworkInterface;
  920. ThisNetworkInterfaceIntn = (EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *)GetFirstNode (&mEfiRedfishDiscoverNetworkInterface);
  921. while (TRUE) {
  922. ThisNetworkInterface->IsIpv6 = FALSE;
  923. if (CheckIsIpVersion6 (ThisNetworkInterfaceIntn)) {
  924. ThisNetworkInterface->IsIpv6 = TRUE;
  925. }
  926. CopyMem ((VOID *)&ThisNetworkInterface->MacAddress, &ThisNetworkInterfaceIntn->MacAddress, ThisNetworkInterfaceIntn->HwAddressSize);
  927. NetworkInterfaceGetSubnetInfo (ThisNetworkInterfaceIntn, ImageHandle); // Get subnet info.
  928. if (!ThisNetworkInterface->IsIpv6) {
  929. IP4_COPY_ADDRESS (&ThisNetworkInterface->SubnetId.v4, &ThisNetworkInterfaceIntn->SubnetAddr.v4); // IPv4 subnet information.
  930. } else {
  931. IP6_COPY_ADDRESS (&ThisNetworkInterface->SubnetId.v6, &ThisNetworkInterfaceIntn->SubnetAddr.v6); // IPv6 subnet information in IPv6 address information.
  932. }
  933. ThisNetworkInterface->SubnetPrefixLength = ThisNetworkInterfaceIntn->SubnetPrefixLength;
  934. ThisNetworkInterface->VlanId = ThisNetworkInterfaceIntn->VlanId;
  935. (*NumberOfNetworkIntfs)++;
  936. if (IsNodeAtEnd (&mEfiRedfishDiscoverNetworkInterface, &ThisNetworkInterfaceIntn->Entry)) {
  937. break;
  938. }
  939. ThisNetworkInterfaceIntn = (EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *)GetNextNode (&mEfiRedfishDiscoverNetworkInterface, &ThisNetworkInterfaceIntn->Entry);
  940. ThisNetworkInterface++;
  941. }
  942. return EFI_SUCCESS;
  943. }
  944. /**
  945. This function acquires Redfish services by discovering static Redfish setting
  946. according to Redfish Host Interface or through SSDP. Returns a list of EFI
  947. handles in EFI_REDFISH_DISCOVERED_LIST. Each of EFI handle has cooresponding
  948. EFI REST EX instance installed on it. Each REST EX isntance is a child instance which
  949. created through EFI REST EX serivce protoocl for communicating with specific
  950. Redfish service.
  951. @param[in] This EFI_REDFISH_DISCOVER_PROTOCOL instance.
  952. @param[in] ImageHandle EFI image owns these Redfish service instances.
  953. @param[in] TargetNetworkInterface Target network interface to do the discovery.
  954. NULL means discover Redfish service on all network interfaces on platform.
  955. @param[in] Flags Redfish service discover flags.
  956. @param[in] Token EFI_REDFISH_DISCOVERED_TOKEN instance.
  957. The memory of EFI_REDFISH_DISCOVERED_LIST and the strings in
  958. EFI_REDFISH_DISCOVERED_INFORMATION are all allocated by Acquire()
  959. and must be freed when caller invoke Release().
  960. @retval EFI_SUCCESS REST EX instance of discovered Redfish services are returned.
  961. @retval EFI_INVALID_PARAMETERS ImageHandle == NULL, Flags == 0, Token == NULL, Token->Timeout > 5,
  962. or Token->Event == NULL.
  963. @retval Others Fail acquire Redfish services.
  964. **/
  965. EFI_STATUS
  966. EFIAPI
  967. RedfishServiceAcquireService (
  968. IN EFI_REDFISH_DISCOVER_PROTOCOL *This,
  969. IN EFI_HANDLE ImageHandle,
  970. IN EFI_REDFISH_DISCOVER_NETWORK_INTERFACE *TargetNetworkInterface,
  971. IN EFI_REDFISH_DISCOVER_FLAG Flags,
  972. IN EFI_REDFISH_DISCOVERED_TOKEN *Token
  973. )
  974. {
  975. EFI_REDFISH_DISCOVERED_INTERNAL_INSTANCE *Instance;
  976. EFI_STATUS Status1;
  977. EFI_STATUS Status2;
  978. BOOLEAN NewInstance;
  979. UINTN NumNetworkInterfaces;
  980. UINTN NetworkInterfacesIndex;
  981. EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *TargetNetworkInterfaceInternal;
  982. DEBUG ((DEBUG_INFO, "%a:Entry.\n", __FUNCTION__));
  983. //
  984. // Validate parameters.
  985. //
  986. if ((ImageHandle == NULL) || (Token == NULL) || ((Flags & ~EFI_REDFISH_DISCOVER_VALIDATION) == 0)) {
  987. DEBUG ((DEBUG_ERROR, "%a:Invalid parameters.\n", __FUNCTION__));
  988. return EFI_INVALID_PARAMETER;
  989. }
  990. //
  991. // Validate target network interface.
  992. //
  993. if (EFI_ERROR (ValidateTargetNetworkInterface (TargetNetworkInterface, Flags))) {
  994. return EFI_UNSUPPORTED;
  995. }
  996. if (TargetNetworkInterface != NULL) {
  997. TargetNetworkInterfaceInternal = GetTargetNetworkInterfaceInternal (TargetNetworkInterface);
  998. NumNetworkInterfaces = 1;
  999. } else {
  1000. TargetNetworkInterfaceInternal = (EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *)GetFirstNode (&mEfiRedfishDiscoverNetworkInterface);
  1001. NumNetworkInterfaces = NumberOfNetworkInterface ();
  1002. if (NumNetworkInterfaces == 0) {
  1003. DEBUG ((DEBUG_ERROR, "%a:No network interface on platform.\n", __FUNCTION__));
  1004. return EFI_UNSUPPORTED;
  1005. }
  1006. }
  1007. for (NetworkInterfacesIndex = 0; NetworkInterfacesIndex < NumNetworkInterfaces; NetworkInterfacesIndex++) {
  1008. Status1 = EFI_SUCCESS;
  1009. Status2 = EFI_SUCCESS;
  1010. NewInstance = FALSE;
  1011. Instance = GetInstanceByOwner (ImageHandle, TargetNetworkInterfaceInternal, Flags & ~EFI_REDFISH_DISCOVER_VALIDATION); // Check if we can re-use previous instance.
  1012. if (Instance == NULL) {
  1013. DEBUG ((DEBUG_INFO, "%a:Create new EFI_REDFISH_DISCOVERED_INTERNAL_INSTANCE.\n", __FUNCTION__));
  1014. Instance = (EFI_REDFISH_DISCOVERED_INTERNAL_INSTANCE *)AllocateZeroPool (sizeof (EFI_REDFISH_DISCOVERED_INTERNAL_INSTANCE));
  1015. if (Instance == NULL) {
  1016. DEBUG ((DEBUG_ERROR, "%a:Memory allocation fail.\n", __FUNCTION__));
  1017. }
  1018. InitializeListHead (&Instance->Entry);
  1019. Instance->Owner = ImageHandle;
  1020. Instance->DiscoverFlags = Flags & ~EFI_REDFISH_DISCOVER_VALIDATION;
  1021. Instance->NetworkInterface = TargetNetworkInterfaceInternal;
  1022. //
  1023. // Get subnet information in case subnet information is not set because
  1024. // RedfishServiceGetNetworkInterfaces hasn't been called yet.
  1025. //
  1026. NetworkInterfaceGetSubnetInfo (TargetNetworkInterfaceInternal, ImageHandle);
  1027. NewInstance = TRUE;
  1028. }
  1029. if (TargetNetworkInterfaceInternal->StrMacAddr != NULL) {
  1030. DEBUG ((DEBUG_INFO, "%a:Acquire Redfish service on network interface MAC address:%s.\n", __FUNCTION__, TargetNetworkInterfaceInternal->StrMacAddr));
  1031. } else {
  1032. DEBUG ((DEBUG_INFO, "%a:WARNING: No MAC address on this network interface.\n", __FUNCTION__));
  1033. }
  1034. Instance->DiscoverToken = Token; // Always use the latest Token passed by caller.
  1035. if ((Flags & EFI_REDFISH_DISCOVER_HOST_INTERFACE) != 0) {
  1036. DEBUG ((DEBUG_INFO, "%a:Redfish HOST interface discovery.\n", __FUNCTION__));
  1037. Instance->HostIntfValidation = FALSE;
  1038. if ((Flags & EFI_REDFISH_DISCOVER_VALIDATION) != 0) {
  1039. Instance->HostIntfValidation = TRUE;
  1040. }
  1041. Status1 = DiscoverRedfishHostInterface (Instance); // Discover Redfish service through Redfish Host Interface.
  1042. }
  1043. if ((Flags & EFI_REDFISH_DISCOVER_SSDP) != 0) {
  1044. DEBUG ((DEBUG_ERROR, "%a:Redfish service discovery through SSDP is not supported\n", __FUNCTION__));
  1045. return EFI_UNSUPPORTED;
  1046. } else {
  1047. if (EFI_ERROR (Status1) && EFI_ERROR (Status2)) {
  1048. FreePool ((VOID *)Instance);
  1049. DEBUG ((DEBUG_ERROR, "%a:Something wrong on Redfish service discovery Status1=%x, Status2=%x.\n", __FUNCTION__, Status1, Status2));
  1050. } else {
  1051. if (NewInstance) {
  1052. InsertTailList (&mRedfishDiscoverList, &Instance->Entry);
  1053. }
  1054. }
  1055. }
  1056. if (TargetNetworkInterface == NULL) {
  1057. //
  1058. // Discover Redfish services on all of network interfaces.
  1059. //
  1060. TargetNetworkInterfaceInternal = (EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *)GetNextNode (&mEfiRedfishDiscoverNetworkInterface, &TargetNetworkInterfaceInternal->Entry);
  1061. }
  1062. }
  1063. return EFI_SUCCESS;
  1064. }
  1065. /**
  1066. This function aborts Redfish service discovery on the given network interface.
  1067. @param[in] This EFI_REDFISH_DISCOVER_PROTOCOL instance.
  1068. @param[in] TargetNetworkInterface Target network interface to do the discovery.
  1069. @retval EFI_SUCCESS REST EX instance of discovered Redfish services are returned.
  1070. @retval Others Fail to abort Redfish service discovery.
  1071. **/
  1072. EFI_STATUS
  1073. EFIAPI
  1074. RedfishServiceAbortAcquire (
  1075. IN EFI_REDFISH_DISCOVER_PROTOCOL *This,
  1076. IN EFI_REDFISH_DISCOVER_NETWORK_INTERFACE *TargetNetworkInterface OPTIONAL
  1077. )
  1078. {
  1079. // This function is used to abort Redfish service discovery through SSDP
  1080. // on the network interface. SSDP is optionally supprted by EFI_REDFISH_DISCOVER_PROTOCOL,
  1081. // we dont have implementation for SSDP now.
  1082. return EFI_UNSUPPORTED;
  1083. }
  1084. /**
  1085. This function releases Redfish services found by RedfishServiceAcquire().
  1086. @param[in] This EFI_REDFISH_DISCOVER_PROTOCOL instance.
  1087. @param[in] InstanceList The Redfish service to release.
  1088. @retval EFI_SUCCESS REST EX instances of discovered Redfish are released.
  1089. @retval Others Fail to remove the entry
  1090. **/
  1091. EFI_STATUS
  1092. EFIAPI
  1093. RedfishServiceReleaseService (
  1094. IN EFI_REDFISH_DISCOVER_PROTOCOL *This,
  1095. IN EFI_REDFISH_DISCOVERED_LIST *InstanceList
  1096. )
  1097. {
  1098. UINTN NumService;
  1099. BOOLEAN AnyFailRelease;
  1100. EFI_REDFISH_DISCOVERED_INSTANCE *ThisRedfishInstance;
  1101. EFI_REDFISH_DISCOVERED_INTERNAL_LIST *DiscoveredRedfishInstance;
  1102. if (IsListEmpty (&mRedfishInstanceList)) {
  1103. DEBUG ((DEBUG_ERROR, "%a:No any discovered Redfish service.\n", __FUNCTION__));
  1104. return EFI_NOT_FOUND;
  1105. }
  1106. AnyFailRelease = FALSE;
  1107. ThisRedfishInstance = InstanceList->RedfishInstances;
  1108. for (NumService = 0; NumService < InstanceList->NumberOfServiceFound; NumService++) {
  1109. DiscoveredRedfishInstance = (EFI_REDFISH_DISCOVERED_INTERNAL_LIST *)GetFirstNode (&mRedfishInstanceList);
  1110. do {
  1111. if (DiscoveredRedfishInstance->Instance == ThisRedfishInstance) {
  1112. RemoveEntryList (&DiscoveredRedfishInstance->NextInstance);
  1113. if (ThisRedfishInstance->Information.Location != NULL) {
  1114. FreePool (ThisRedfishInstance->Information.Location);
  1115. }
  1116. if (ThisRedfishInstance->Information.Uuid != NULL) {
  1117. FreePool (ThisRedfishInstance->Information.Uuid);
  1118. }
  1119. if (ThisRedfishInstance->Information.Os != NULL) {
  1120. FreePool (ThisRedfishInstance->Information.Os);
  1121. }
  1122. if (ThisRedfishInstance->Information.OsVersion != NULL) {
  1123. FreePool (ThisRedfishInstance->Information.OsVersion);
  1124. }
  1125. if (ThisRedfishInstance->Information.Product != NULL) {
  1126. FreePool (ThisRedfishInstance->Information.Product);
  1127. }
  1128. if (ThisRedfishInstance->Information.ProductVer != NULL) {
  1129. FreePool (ThisRedfishInstance->Information.ProductVer);
  1130. }
  1131. FreePool ((VOID *)ThisRedfishInstance);
  1132. goto ReleaseNext;
  1133. }
  1134. if (IsNodeAtEnd (&mRedfishInstanceList, &DiscoveredRedfishInstance->NextInstance)) {
  1135. break;
  1136. }
  1137. DiscoveredRedfishInstance = (EFI_REDFISH_DISCOVERED_INTERNAL_LIST *)GetNextNode (&mRedfishInstanceList, &DiscoveredRedfishInstance->NextInstance);
  1138. } while (TRUE);
  1139. AnyFailRelease = TRUE;
  1140. ReleaseNext:;
  1141. //
  1142. // Release next discovered Redfish Service.
  1143. //
  1144. ThisRedfishInstance = (EFI_REDFISH_DISCOVERED_INSTANCE *)((UINT8 *)ThisRedfishInstance + sizeof (EFI_REDFISH_DISCOVERED_INSTANCE));
  1145. }
  1146. if (AnyFailRelease) {
  1147. return EFI_NOT_FOUND;
  1148. } else {
  1149. return EFI_SUCCESS;
  1150. }
  1151. }
  1152. EFI_REDFISH_DISCOVER_PROTOCOL mRedfishDiscover = {
  1153. RedfishServiceGetNetworkInterface,
  1154. RedfishServiceAcquireService,
  1155. RedfishServiceAbortAcquire,
  1156. RedfishServiceReleaseService
  1157. };
  1158. /**
  1159. This function create an EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL for the
  1160. given network interface.
  1161. @param[in] ControllerHandle MAC address of this network interface.
  1162. @param[in] NetworkProtocolType Network protocol type.
  1163. @param[out] IsNewInstance BOOLEAN means new instance or not.
  1164. @param[out] NetworkInterface Pointer to to EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL.
  1165. @retval EFI_STATUS
  1166. **/
  1167. EFI_STATUS
  1168. CreateRedfishDiscoverNetworkInterface (
  1169. IN EFI_HANDLE ControllerHandle,
  1170. IN UINT32 NetworkProtocolType,
  1171. OUT BOOLEAN *IsNewInstance,
  1172. OUT EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL **NetworkInterface
  1173. )
  1174. {
  1175. EFI_MAC_ADDRESS MacAddress;
  1176. UINTN HwAddressSize;
  1177. EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *ThisNetworkInterface;
  1178. EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *NewNetworkInterface;
  1179. NetLibGetMacAddress (ControllerHandle, &MacAddress, &HwAddressSize);
  1180. NewNetworkInterface = NULL;
  1181. *IsNewInstance = TRUE;
  1182. if (!IsListEmpty ((const LIST_ENTRY *)&mEfiRedfishDiscoverNetworkInterface)) {
  1183. //
  1184. // Check if this instance already exist.
  1185. //
  1186. ThisNetworkInterface = (EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *)GetFirstNode (&mEfiRedfishDiscoverNetworkInterface);
  1187. if (ThisNetworkInterface != NULL) {
  1188. while (TRUE) {
  1189. if ((CompareMem ((CONST VOID *)&ThisNetworkInterface->MacAddress.Addr, (CONST VOID *)&MacAddress.Addr, HwAddressSize) == 0) &&
  1190. (ThisNetworkInterface->NetworkProtocolType == NetworkProtocolType))
  1191. {
  1192. NewNetworkInterface = ThisNetworkInterface;
  1193. *IsNewInstance = FALSE;
  1194. break;
  1195. }
  1196. if (IsNodeAtEnd (&mEfiRedfishDiscoverNetworkInterface, &ThisNetworkInterface->Entry)) {
  1197. NewNetworkInterface = NULL;
  1198. break;
  1199. }
  1200. ThisNetworkInterface = (EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *)GetNextNode (&mEfiRedfishDiscoverNetworkInterface, &ThisNetworkInterface->Entry);
  1201. }
  1202. }
  1203. }
  1204. if (NewNetworkInterface == NULL) {
  1205. //
  1206. // Create a new instance.
  1207. //
  1208. NewNetworkInterface = (EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *)AllocateZeroPool (sizeof (EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL));
  1209. if (NewNetworkInterface == NULL) {
  1210. return EFI_OUT_OF_RESOURCES;
  1211. }
  1212. NewNetworkInterface->HwAddressSize = HwAddressSize;
  1213. CopyMem (&NewNetworkInterface->MacAddress.Addr, &MacAddress.Addr, NewNetworkInterface->HwAddressSize);
  1214. NetLibGetMacString (ControllerHandle, NULL, &NewNetworkInterface->StrMacAddr);
  1215. NewNetworkInterface->VlanId = NetLibGetVlanId (ControllerHandle);
  1216. }
  1217. *NetworkInterface = NewNetworkInterface;
  1218. return EFI_SUCCESS;
  1219. }
  1220. /**
  1221. This function destory network interface
  1222. @param[in] ThisNetworkInterface EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL instance.
  1223. @retval EFI_STATUS
  1224. **/
  1225. EFI_STATUS
  1226. DestroyRedfishNetwrokInterface (
  1227. IN EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *ThisNetworkInterface
  1228. )
  1229. {
  1230. EFI_STATUS Status;
  1231. Status = gBS->UninstallProtocolInterface (
  1232. ThisNetworkInterface->OpenDriverControllerHandle,
  1233. gRequiredProtocol[ThisNetworkInterface->NetworkProtocolType].DiscoveredProtocolGuid,
  1234. &ThisNetworkInterface->NetworkInterfaceProtocolInfo.ProtocolDiscoverId
  1235. );
  1236. RemoveEntryList (&ThisNetworkInterface->Entry);
  1237. mNumNetworkInterface--;
  1238. FreePool (ThisNetworkInterface);
  1239. return Status;
  1240. }
  1241. /**
  1242. Tests to see if the required protocols are provided on the given
  1243. controller handle.
  1244. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
  1245. @param[in] ControllerHandle The handle of the controller to test. This handle
  1246. must support a protocol interface that supplies
  1247. an I/O abstraction to the driver.
  1248. @retval EFI_SUCCESS One of required protocol is found.
  1249. @retval EFI_UNSUPPORTED None of required protocol is found.
  1250. **/
  1251. EFI_STATUS
  1252. TestForRequiredProtocols (
  1253. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  1254. IN EFI_HANDLE ControllerHandle
  1255. )
  1256. {
  1257. UINT32 Id;
  1258. UINTN Index;
  1259. EFI_STATUS Status;
  1260. for (Index = 0; Index < (sizeof (gRequiredProtocol) / sizeof (REDFISH_DISCOVER_REQUIRED_PROTOCOL)); Index++) {
  1261. Status = gBS->OpenProtocol (
  1262. ControllerHandle,
  1263. gRequiredProtocol[Index].RequiredServiceBindingProtocolGuid,
  1264. NULL,
  1265. This->DriverBindingHandle,
  1266. ControllerHandle,
  1267. EFI_OPEN_PROTOCOL_TEST_PROTOCOL
  1268. );
  1269. if (!EFI_ERROR (Status)) {
  1270. Status = gBS->OpenProtocol (
  1271. ControllerHandle,
  1272. gRequiredProtocol[Index].DiscoveredProtocolGuid,
  1273. (VOID **)&Id,
  1274. This->DriverBindingHandle,
  1275. ControllerHandle,
  1276. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  1277. );
  1278. if (EFI_ERROR (Status)) {
  1279. DEBUG ((DEBUG_ERROR, "%a: %s is found on this controller handle.\n", __FUNCTION__, gRequiredProtocol[Index].ProtocolName));
  1280. return EFI_SUCCESS;
  1281. }
  1282. }
  1283. }
  1284. return EFI_UNSUPPORTED;
  1285. }
  1286. /**
  1287. Build up network interface and create corresponding service through the given
  1288. controller handle.
  1289. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
  1290. @param[in] ControllerHandle The handle of the controller to test. This handle
  1291. must support a protocol interface that supplies
  1292. an I/O abstraction to the driver.
  1293. @retval EFI_SUCCESS One of required protocol is found.
  1294. @retval EFI_UNSUPPORTED None of required protocol is found.
  1295. @retval EFI_UNSUPPORTED Failed to build up network interface.
  1296. **/
  1297. EFI_STATUS
  1298. BuildupNetworkInterface (
  1299. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  1300. IN EFI_HANDLE ControllerHandle
  1301. )
  1302. {
  1303. UINT32 Id;
  1304. UINT32 Index;
  1305. EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *NetworkInterface;
  1306. BOOLEAN IsNew;
  1307. EFI_STATUS Status;
  1308. VOID *TempInterface;
  1309. VOID **Interface;
  1310. UINT32 *ProtocolDiscoverIdPtr;
  1311. EFI_HANDLE OpenDriverAgentHandle;
  1312. EFI_HANDLE OpenDriverControllerHandle;
  1313. EFI_HANDLE *HandleOfProtocolInterfacePtr;
  1314. EFI_REDFISH_DISCOVER_REST_EX_INSTANCE_INTERNAL *RestExInstance;
  1315. EFI_TPL OldTpl;
  1316. BOOLEAN NewNetworkInterfaceInstalled;
  1317. NewNetworkInterfaceInstalled = FALSE;
  1318. Index = 0;
  1319. do {
  1320. Status = gBS->OpenProtocol (
  1321. // Already in list?
  1322. ControllerHandle,
  1323. gRequiredProtocol[Index].DiscoveredProtocolGuid,
  1324. (VOID **)&Id,
  1325. This->DriverBindingHandle,
  1326. ControllerHandle,
  1327. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  1328. );
  1329. if (!EFI_ERROR (Status)) {
  1330. Index++;
  1331. if (Index == (sizeof (gRequiredProtocol) / sizeof (REDFISH_DISCOVER_REQUIRED_PROTOCOL))) {
  1332. break;
  1333. }
  1334. continue;
  1335. }
  1336. Status = gBS->OpenProtocol (
  1337. ControllerHandle,
  1338. gRequiredProtocol[Index].RequiredServiceBindingProtocolGuid,
  1339. &TempInterface,
  1340. This->DriverBindingHandle,
  1341. ControllerHandle,
  1342. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  1343. );
  1344. if (EFI_ERROR (Status)) {
  1345. Index++;
  1346. if (Index == (sizeof (gRequiredProtocol) / sizeof (REDFISH_DISCOVER_REQUIRED_PROTOCOL))) {
  1347. break;
  1348. }
  1349. continue;
  1350. }
  1351. if (gRequiredProtocol[Index].ProtocolType != ProtocolTypeRestEx) {
  1352. OldTpl = gBS->RaiseTPL (EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_TPL);
  1353. Status = CreateRedfishDiscoverNetworkInterface (ControllerHandle, gRequiredProtocol[Index].ProtocolType, &IsNew, &NetworkInterface);
  1354. if (EFI_ERROR (Status)) {
  1355. gBS->RestoreTPL (OldTpl);
  1356. return Status;
  1357. }
  1358. NetworkInterface->NetworkProtocolType = gRequiredProtocol[Index].ProtocolType;
  1359. NetworkInterface->OpenDriverAgentHandle = This->DriverBindingHandle;
  1360. NetworkInterface->OpenDriverControllerHandle = ControllerHandle;
  1361. NetworkInterface->NetworkInterfaceProtocolInfo.ProtocolGuid = \
  1362. *gRequiredProtocol[Index].RequiredProtocolGuid;
  1363. NetworkInterface->NetworkInterfaceProtocolInfo.ProtocolServiceGuid = \
  1364. *gRequiredProtocol[Index].RequiredServiceBindingProtocolGuid;
  1365. ProtocolDiscoverIdPtr = &NetworkInterface->NetworkInterfaceProtocolInfo.ProtocolDiscoverId;
  1366. OpenDriverAgentHandle = NetworkInterface->OpenDriverAgentHandle;
  1367. OpenDriverControllerHandle = NetworkInterface->OpenDriverControllerHandle;
  1368. HandleOfProtocolInterfacePtr = &NetworkInterface->NetworkInterfaceProtocolInfo.ProtocolControllerHandle;
  1369. Interface = &NetworkInterface->NetworkInterfaceProtocolInfo.NetworkProtocolInterface;
  1370. NewNetworkInterfaceInstalled = TRUE;
  1371. if (IsNew) {
  1372. InsertTailList (&mEfiRedfishDiscoverNetworkInterface, &NetworkInterface->Entry);
  1373. mNumNetworkInterface++;
  1374. }
  1375. gBS->RestoreTPL (OldTpl);
  1376. } else {
  1377. // Record REST_EX instance. REST_EX is created when clinet asks for Redfish service discovery.
  1378. // Redfish Service Discover protocol will match REST EX to the corresponding EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL
  1379. // when discovery.
  1380. RestExInstance = (EFI_REDFISH_DISCOVER_REST_EX_INSTANCE_INTERNAL *)AllocateZeroPool (sizeof (EFI_REDFISH_DISCOVER_REST_EX_INSTANCE_INTERNAL));
  1381. if (RestExInstance == NULL) {
  1382. return EFI_OUT_OF_RESOURCES;
  1383. }
  1384. RestExInstance->OpenDriverAgentHandle = This->DriverBindingHandle;
  1385. RestExInstance->OpenDriverControllerHandle = ControllerHandle;
  1386. RestExInstance->RestExControllerHandle = ControllerHandle;
  1387. InitializeListHead (&RestExInstance->Entry);
  1388. InsertTailList (&mEfiRedfishDiscoverRestExInstance, &RestExInstance->Entry);
  1389. mNumRestExInstance++;
  1390. ProtocolDiscoverIdPtr = &RestExInstance->RestExId;
  1391. OpenDriverAgentHandle = RestExInstance->OpenDriverAgentHandle;
  1392. OpenDriverControllerHandle = RestExInstance->OpenDriverControllerHandle;
  1393. HandleOfProtocolInterfacePtr = &RestExInstance->RestExChildHandle;
  1394. Interface = (VOID **)&RestExInstance->RestExProtocolInterface;
  1395. }
  1396. Status = gBS->InstallProtocolInterface (
  1397. &ControllerHandle,
  1398. gRequiredProtocol[Index].DiscoveredProtocolGuid,
  1399. EFI_NATIVE_INTERFACE,
  1400. ProtocolDiscoverIdPtr
  1401. );
  1402. if (EFI_ERROR (Status)) {
  1403. Index++;
  1404. if (Index == (sizeof (gRequiredProtocol) / sizeof (REDFISH_DISCOVER_REQUIRED_PROTOCOL))) {
  1405. break;
  1406. }
  1407. continue;
  1408. }
  1409. //
  1410. // Create service binding child and open it BY_DRIVER.
  1411. //
  1412. Status = NetLibCreateServiceChild (
  1413. ControllerHandle,
  1414. This->ImageHandle,
  1415. gRequiredProtocol[Index].RequiredServiceBindingProtocolGuid,
  1416. HandleOfProtocolInterfacePtr
  1417. );
  1418. if (!EFI_ERROR (Status)) {
  1419. Status = gBS->OpenProtocol (
  1420. *HandleOfProtocolInterfacePtr,
  1421. gRequiredProtocol[Index].RequiredProtocolGuid,
  1422. Interface,
  1423. OpenDriverAgentHandle,
  1424. OpenDriverControllerHandle,
  1425. EFI_OPEN_PROTOCOL_BY_DRIVER
  1426. );
  1427. if (!EFI_ERROR (Status)) {
  1428. if ((gRequiredProtocol[Index].ProtocolType == ProtocolTypeRestEx)) {
  1429. // Install Redfish Discover Protocol when EFI REST EX protcol is discovered.
  1430. // This ensures EFI REST EX is ready while the consumer of EFI_REDFISH_DISCOVER_PROTOCOL
  1431. // acquires Redfish serivce over network interface.
  1432. if (!NewNetworkInterfaceInstalled) {
  1433. NetworkInterface = GetTargetNetworkInterfaceInternalByController (ControllerHandle);
  1434. if (NetworkInterface == NULL) {
  1435. DEBUG ((DEBUG_ERROR, "%a: Can't find network interface by ControllerHandle\n", __FUNCTION__));
  1436. return Status;
  1437. }
  1438. }
  1439. NewNetworkInterfaceInstalled = FALSE;
  1440. NetworkInterface->EfiRedfishDiscoverProtocolHandle = NULL;
  1441. Status = gBS->InstallProtocolInterface (
  1442. &NetworkInterface->EfiRedfishDiscoverProtocolHandle,
  1443. &gEfiRedfishDiscoverProtocolGuid,
  1444. EFI_NATIVE_INTERFACE,
  1445. (VOID *)&mRedfishDiscover
  1446. );
  1447. if (EFI_ERROR (Status)) {
  1448. DEBUG ((DEBUG_ERROR, "%a: Fail to install EFI_REDFISH_DISCOVER_PROTOCOL\n", __FUNCTION__));
  1449. }
  1450. }
  1451. }
  1452. return Status;
  1453. } else {
  1454. Index++;
  1455. if (Index == (sizeof (gRequiredProtocol) / sizeof (REDFISH_DISCOVER_REQUIRED_PROTOCOL))) {
  1456. break;
  1457. }
  1458. continue;
  1459. }
  1460. } while (Index < (sizeof (gRequiredProtocol) / sizeof (REDFISH_DISCOVER_REQUIRED_PROTOCOL)));
  1461. return EFI_UNSUPPORTED;
  1462. }
  1463. /**
  1464. Close the protocol opened for Redfish discovery. This function also destories
  1465. the network services.
  1466. @param[in] ThisBindingProtocol A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
  1467. @param[in] ControllerHandle The handle of the controller to test. This handle
  1468. must support a protocol interface that supplies
  1469. an I/O abstraction to the driver.
  1470. @param[in] ThisRequiredProtocol Pointer to the instance of REDFISH_DISCOVER_REQUIRED_PROTOCOL.
  1471. @param[in] DriverAgentHandle Driver agent handle which used to open protocol earlier.
  1472. @param[in] DriverControllerHandle Driver controller handle which used to open protocol earlier.
  1473. @retval EFI_SUCCESS Prorocol is closed successfully.
  1474. @retval Others Prorocol is closed unsuccessfully.
  1475. **/
  1476. EFI_STATUS
  1477. CloseProtocolService (
  1478. IN EFI_DRIVER_BINDING_PROTOCOL *ThisBindingProtocol,
  1479. IN EFI_HANDLE ControllerHandle,
  1480. IN REDFISH_DISCOVER_REQUIRED_PROTOCOL *ThisRequiredProtocol,
  1481. IN EFI_HANDLE DriverAgentHandle,
  1482. IN EFI_HANDLE DriverControllerHandle
  1483. )
  1484. {
  1485. EFI_STATUS Status;
  1486. Status = gBS->CloseProtocol (
  1487. ControllerHandle,
  1488. ThisRequiredProtocol->RequiredProtocolGuid,
  1489. DriverAgentHandle,
  1490. DriverControllerHandle
  1491. );
  1492. if (!EFI_ERROR (Status)) {
  1493. NetLibDestroyServiceChild (
  1494. ControllerHandle,
  1495. ThisBindingProtocol->ImageHandle,
  1496. ThisRequiredProtocol->RequiredServiceBindingProtocolGuid,
  1497. ControllerHandle
  1498. );
  1499. }
  1500. return Status;
  1501. }
  1502. /**
  1503. Stop the services on network interface.
  1504. @param[in] ThisBindingProtocol A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
  1505. @param[in] ControllerHandle The handle of the controller to test. This handle
  1506. must support a protocol interface that supplies
  1507. an I/O abstraction to the driver.
  1508. @retval EFI_SUCCESS One of required protocol is found.
  1509. @retval Others Faile to stop the services on network interface.
  1510. **/
  1511. EFI_STATUS
  1512. StopServiceOnNetworkInterface (
  1513. IN EFI_DRIVER_BINDING_PROTOCOL *ThisBindingProtocol,
  1514. IN EFI_HANDLE ControllerHandle
  1515. )
  1516. {
  1517. UINT32 Index;
  1518. EFI_STATUS Status;
  1519. VOID *Interface;
  1520. EFI_TPL OldTpl;
  1521. EFI_HANDLE DiscoverProtocolHandle;
  1522. EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *ThisNetworkInterface;
  1523. EFI_REDFISH_DISCOVER_REST_EX_INSTANCE_INTERNAL *RestExInstance;
  1524. for (Index = 0; Index < (sizeof (gRequiredProtocol) / sizeof (REDFISH_DISCOVER_REQUIRED_PROTOCOL)); Index++) {
  1525. Status = gBS->HandleProtocol (
  1526. ControllerHandle,
  1527. gRequiredProtocol[Index].RequiredProtocolGuid,
  1528. (VOID **)&Interface
  1529. );
  1530. if (!EFI_ERROR (Status)) {
  1531. if (gRequiredProtocol[Index].ProtocolType != ProtocolTypeRestEx) {
  1532. if (IsListEmpty (&mEfiRedfishDiscoverNetworkInterface)) {
  1533. return EFI_NOT_FOUND;
  1534. }
  1535. OldTpl = gBS->RaiseTPL (EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_TPL);
  1536. ThisNetworkInterface = (EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *)GetFirstNode (&mEfiRedfishDiscoverNetworkInterface);
  1537. while (TRUE) {
  1538. if (ThisNetworkInterface->NetworkInterfaceProtocolInfo.ProtocolControllerHandle == ControllerHandle) {
  1539. DiscoverProtocolHandle = ThisNetworkInterface->EfiRedfishDiscoverProtocolHandle;
  1540. //
  1541. // Close protocol and destroy service.
  1542. //
  1543. Status = CloseProtocolService (
  1544. ThisBindingProtocol,
  1545. ControllerHandle,
  1546. &gRequiredProtocol[Index],
  1547. ThisNetworkInterface->OpenDriverAgentHandle,
  1548. ThisNetworkInterface->OpenDriverControllerHandle
  1549. );
  1550. if (!EFI_ERROR (Status)) {
  1551. Status = DestroyRedfishNetwrokInterface (ThisNetworkInterface);
  1552. }
  1553. gBS->RestoreTPL (OldTpl);
  1554. //
  1555. // Disconnect EFI Redfish discover driver controller to notify the
  1556. // clinet which uses .EFI Redfish discover protocol.
  1557. //
  1558. if (DiscoverProtocolHandle != NULL) {
  1559. gBS->DisconnectController (DiscoverProtocolHandle, NULL, NULL);
  1560. Status = gBS->UninstallProtocolInterface (
  1561. DiscoverProtocolHandle,
  1562. &gEfiRedfishDiscoverProtocolGuid,
  1563. (VOID *)&mRedfishDiscover
  1564. );
  1565. }
  1566. return Status;
  1567. }
  1568. if (IsNodeAtEnd (&mEfiRedfishDiscoverNetworkInterface, &ThisNetworkInterface->Entry)) {
  1569. break;
  1570. }
  1571. ThisNetworkInterface = (EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *)GetNextNode (&mEfiRedfishDiscoverNetworkInterface, &ThisNetworkInterface->Entry);
  1572. }
  1573. gBS->RestoreTPL (OldTpl);
  1574. } else {
  1575. if (IsListEmpty (&mEfiRedfishDiscoverRestExInstance)) {
  1576. return EFI_NOT_FOUND;
  1577. }
  1578. OldTpl = gBS->RaiseTPL (EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_TPL);
  1579. RestExInstance = (EFI_REDFISH_DISCOVER_REST_EX_INSTANCE_INTERNAL *)GetFirstNode (&mEfiRedfishDiscoverRestExInstance);
  1580. while (TRUE) {
  1581. if (RestExInstance->RestExChildHandle == ControllerHandle) {
  1582. Status = CloseProtocolService (
  1583. // Close REST_EX protocol.
  1584. ThisBindingProtocol,
  1585. ControllerHandle,
  1586. &gRequiredProtocol[Index],
  1587. RestExInstance->OpenDriverAgentHandle,
  1588. RestExInstance->OpenDriverControllerHandle
  1589. );
  1590. RemoveEntryList (&RestExInstance->Entry);
  1591. FreePool ((VOID *)RestExInstance);
  1592. mNumRestExInstance--;
  1593. gBS->RestoreTPL (OldTpl);
  1594. return Status;
  1595. }
  1596. if (IsNodeAtEnd (&mEfiRedfishDiscoverRestExInstance, &RestExInstance->Entry)) {
  1597. break;
  1598. }
  1599. RestExInstance = (EFI_REDFISH_DISCOVER_REST_EX_INSTANCE_INTERNAL *)GetNextNode (&mEfiRedfishDiscoverRestExInstance, &RestExInstance->Entry);
  1600. }
  1601. gBS->RestoreTPL (OldTpl);
  1602. }
  1603. }
  1604. }
  1605. return EFI_NOT_FOUND;
  1606. }
  1607. /**
  1608. Tests to see if this driver supports a given controller. If a child device is provided,
  1609. it further tests to see if this driver supports creating a handle for the specified child device.
  1610. This function checks to see if the driver specified by This supports the device specified by
  1611. ControllerHandle. Drivers will typically use the device path attached to
  1612. ControllerHandle and/or the services from the bus I/O abstraction attached to
  1613. ControllerHandle to determine if the driver supports ControllerHandle. This function
  1614. may be called many times during platform initialization. In order to reduce boot times, the tests
  1615. performed by this function must be very small, and take as little time as possible to execute. This
  1616. function must not change the state of any hardware devices, and this function must be aware that the
  1617. device specified by ControllerHandle may already be managed by the same driver or a
  1618. different driver. This function must match its calls to AllocatePages() with FreePages(),
  1619. AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
  1620. Because ControllerHandle may have been previously started by the same driver, if a protocol is
  1621. already in the opened state, then it must not be closed with CloseProtocol(). This is required
  1622. to guarantee the state of ControllerHandle is not modified by this function.
  1623. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
  1624. @param[in] ControllerHandle The handle of the controller to test. This handle
  1625. must support a protocol interface that supplies
  1626. an I/O abstraction to the driver.
  1627. @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
  1628. parameter is ignored by device drivers, and is optional for bus
  1629. drivers. For bus drivers, if this parameter is not NULL, then
  1630. the bus driver must determine if the bus controller specified
  1631. by ControllerHandle and the child controller specified
  1632. by RemainingDevicePath are both supported by this
  1633. bus driver.
  1634. @retval EFI_SUCCESS The device specified by ControllerHandle and
  1635. RemainingDevicePath is supported by the driver specified by This.
  1636. @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
  1637. RemainingDevicePath is already being managed by the driver
  1638. specified by This.
  1639. @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
  1640. RemainingDevicePath is already being managed by a different
  1641. driver or an application that requires exclusive access.
  1642. Currently not implemented.
  1643. @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
  1644. RemainingDevicePath is not supported by the driver specified by This.
  1645. **/
  1646. EFI_STATUS
  1647. EFIAPI
  1648. RedfishDiscoverDriverBindingSupported (
  1649. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  1650. IN EFI_HANDLE ControllerHandle,
  1651. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
  1652. )
  1653. {
  1654. return TestForRequiredProtocols (This, ControllerHandle);
  1655. }
  1656. /**
  1657. Starts a device controller or a bus controller.
  1658. The Start() function is designed to be invoked from the EFI boot service ConnectController().
  1659. As a result, much of the error checking on the parameters to Start() has been moved into this
  1660. common boot service. It is legal to call Start() from other locations,
  1661. but the following calling restrictions must be followed, or the system behavior will not be deterministic.
  1662. 1. ControllerHandle must be a valid EFI_HANDLE.
  1663. 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
  1664. EFI_DEVICE_PATH_PROTOCOL.
  1665. 3. Prior to calling Start(), the Supported() function for the driver specified by This must
  1666. have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
  1667. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
  1668. @param[in] ControllerHandle The handle of the controller to start. This handle
  1669. must support a protocol interface that supplies
  1670. an I/O abstraction to the driver.
  1671. @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
  1672. parameter is ignored by device drivers, and is optional for bus
  1673. drivers. For a bus driver, if this parameter is NULL, then handles
  1674. for all the children of Controller are created by this driver.
  1675. If this parameter is not NULL and the first Device Path Node is
  1676. not the End of Device Path Node, then only the handle for the
  1677. child device specified by the first Device Path Node of
  1678. RemainingDevicePath is created by this driver.
  1679. If the first Device Path Node of RemainingDevicePath is
  1680. the End of Device Path Node, no child handle is created by this
  1681. driver.
  1682. @retval EFI_SUCCESS The device was started.
  1683. @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
  1684. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
  1685. @retval Others The driver failded to start the device.
  1686. **/
  1687. EFI_STATUS
  1688. EFIAPI
  1689. RedfishDiscoverDriverBindingStart (
  1690. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  1691. IN EFI_HANDLE ControllerHandle,
  1692. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
  1693. )
  1694. {
  1695. return BuildupNetworkInterface (This, ControllerHandle);
  1696. }
  1697. /**
  1698. Stops a device controller or a bus controller.
  1699. The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
  1700. As a result, much of the error checking on the parameters to Stop() has been moved
  1701. into this common boot service. It is legal to call Stop() from other locations,
  1702. but the following calling restrictions must be followed, or the system behavior will not be deterministic.
  1703. 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
  1704. same driver's Start() function.
  1705. 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
  1706. EFI_HANDLE. In addition, all of these handles must have been created in this driver's
  1707. Start() function, and the Start() function must have called OpenProtocol() on
  1708. ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
  1709. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
  1710. @param[in] ControllerHandle A handle to the device being stopped. The handle must
  1711. support a bus specific I/O protocol for the driver
  1712. to use to stop the device.
  1713. @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
  1714. @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
  1715. if NumberOfChildren is 0.
  1716. @retval EFI_SUCCESS The device was stopped.
  1717. @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
  1718. **/
  1719. EFI_STATUS
  1720. EFIAPI
  1721. RedfishDiscoverDriverBindingStop (
  1722. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  1723. IN EFI_HANDLE ControllerHandle,
  1724. IN UINTN NumberOfChildren,
  1725. IN EFI_HANDLE *ChildHandleBuffer OPTIONAL
  1726. )
  1727. {
  1728. return StopServiceOnNetworkInterface (This, ControllerHandle);
  1729. }
  1730. EFI_DRIVER_BINDING_PROTOCOL gRedfishDiscoverDriverBinding = {
  1731. RedfishDiscoverDriverBindingSupported,
  1732. RedfishDiscoverDriverBindingStart,
  1733. RedfishDiscoverDriverBindingStop,
  1734. REDFISH_DISCOVER_VERSION,
  1735. NULL,
  1736. NULL
  1737. };
  1738. /**
  1739. This is the declaration of an EFI image entry point.
  1740. @param ImageHandle The firmware allocated handle for the UEFI image.
  1741. @param SystemTable A pointer to the EFI System Table.
  1742. @retval EFI_SUCCESS The operation completed successfully.
  1743. @retval Others An unexpected error occurred.
  1744. **/
  1745. EFI_STATUS
  1746. EFIAPI
  1747. RedfishDiscoverEntryPoint (
  1748. IN EFI_HANDLE ImageHandle,
  1749. IN EFI_SYSTEM_TABLE *SystemTable
  1750. )
  1751. {
  1752. EFI_STATUS Status;
  1753. Status = EFI_SUCCESS;
  1754. InitializeListHead (&mRedfishDiscoverList);
  1755. InitializeListHead (&mRedfishInstanceList);
  1756. InitializeListHead (&mEfiRedfishDiscoverNetworkInterface);
  1757. InitializeListHead (&mEfiRedfishDiscoverRestExInstance);
  1758. //
  1759. // Install binding protoocl to obtain UDP and REST EX protocol.
  1760. //
  1761. Status = EfiLibInstallDriverBindingComponentName2 (
  1762. ImageHandle,
  1763. SystemTable,
  1764. &gRedfishDiscoverDriverBinding,
  1765. ImageHandle,
  1766. &gRedfishDiscoverComponentName,
  1767. &gRedfishDiscoverComponentName2
  1768. );
  1769. return Status;
  1770. }
  1771. /**
  1772. This is the unload handle for Redfish discover module.
  1773. Disconnect the driver specified by ImageHandle from all the devices in the handle database.
  1774. Uninstall all the protocols installed in the driver entry point.
  1775. @param[in] ImageHandle The drivers' driver image.
  1776. @retval EFI_SUCCESS The image is unloaded.
  1777. @retval Others Failed to unload the image.
  1778. **/
  1779. EFI_STATUS
  1780. EFIAPI
  1781. RedfishDiscoverUnload (
  1782. IN EFI_HANDLE ImageHandle
  1783. )
  1784. {
  1785. EFI_STATUS Status;
  1786. EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *ThisNetworkInterface;
  1787. Status = EFI_SUCCESS;
  1788. // Destroy all network interfaces found by EFI Redfish Discover driver and
  1789. // stop services created for Redfish Discover.
  1790. while (!IsListEmpty (&mEfiRedfishDiscoverNetworkInterface)) {
  1791. ThisNetworkInterface = (EFI_REDFISH_DISCOVER_NETWORK_INTERFACE_INTERNAL *)GetFirstNode (&mEfiRedfishDiscoverNetworkInterface);
  1792. StopServiceOnNetworkInterface (&gRedfishDiscoverDriverBinding, ThisNetworkInterface->NetworkInterfaceProtocolInfo.ProtocolControllerHandle);
  1793. }
  1794. return Status;
  1795. }