RedfishDiscoverDxe.c 84 KB

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