RedfishRestExDriver.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. /** @file
  2. The driver binding and service binding protocol for Redfish RestExDxe driver.
  3. Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
  4. (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include <Uefi.h>
  8. #include "RedfishRestExDriver.h"
  9. EFI_DRIVER_BINDING_PROTOCOL gRedfishRestExDriverBinding = {
  10. RedfishRestExDriverBindingSupported,
  11. RedfishRestExDriverBindingStart,
  12. RedfishRestExDriverBindingStop,
  13. REDFISH_RESTEX_DRIVER_VERSION,
  14. NULL,
  15. NULL
  16. };
  17. EFI_SERVICE_BINDING_PROTOCOL mRedfishRestExServiceBinding = {
  18. RedfishRestExServiceBindingCreateChild,
  19. RedfishRestExServiceBindingDestroyChild
  20. };
  21. /**
  22. Callback function which provided by user to remove one node in NetDestroyLinkList process.
  23. @param[in] Entry The entry to be removed.
  24. @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.
  25. @retval EFI_SUCCESS The entry has been removed successfully.
  26. @retval Others Fail to remove the entry.
  27. **/
  28. EFI_STATUS
  29. EFIAPI
  30. RestExDestroyChildEntryInHandleBuffer (
  31. IN LIST_ENTRY *Entry,
  32. IN VOID *Context
  33. )
  34. {
  35. RESTEX_INSTANCE *Instance;
  36. EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
  37. UINTN NumberOfChildren;
  38. EFI_HANDLE *ChildHandleBuffer;
  39. if ((Entry == NULL) || (Context == NULL)) {
  40. return EFI_INVALID_PARAMETER;
  41. }
  42. Instance = NET_LIST_USER_STRUCT_S (Entry, RESTEX_INSTANCE, Link, RESTEX_INSTANCE_SIGNATURE);
  43. ServiceBinding = ((RESTEX_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *)Context)->ServiceBinding;
  44. NumberOfChildren = ((RESTEX_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *)Context)->NumberOfChildren;
  45. ChildHandleBuffer = ((RESTEX_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT *)Context)->ChildHandleBuffer;
  46. if (!NetIsInHandleBuffer (Instance->ChildHandle, NumberOfChildren, ChildHandleBuffer)) {
  47. return EFI_SUCCESS;
  48. }
  49. return ServiceBinding->DestroyChild (ServiceBinding, Instance->ChildHandle);
  50. }
  51. /**
  52. Destroy the RestEx instance and recycle the resources.
  53. @param[in] Instance The pointer to the RestEx instance.
  54. **/
  55. VOID
  56. RestExDestroyInstance (
  57. IN RESTEX_INSTANCE *Instance
  58. )
  59. {
  60. HttpIoDestroyIo (&(Instance->HttpIo));
  61. FreePool (Instance);
  62. }
  63. /**
  64. Create the RestEx instance and initialize it.
  65. @param[in] Service The pointer to the RestEx service.
  66. @param[out] Instance The pointer to the RestEx instance.
  67. @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
  68. @retval EFI_SUCCESS The RestEx instance is created.
  69. **/
  70. EFI_STATUS
  71. RestExCreateInstance (
  72. IN RESTEX_SERVICE *Service,
  73. OUT RESTEX_INSTANCE **Instance
  74. )
  75. {
  76. RESTEX_INSTANCE *RestExIns;
  77. EFI_STATUS Status;
  78. *Instance = NULL;
  79. Status = EFI_SUCCESS;
  80. RestExIns = AllocateZeroPool (sizeof (RESTEX_INSTANCE));
  81. if (RestExIns == NULL) {
  82. return EFI_OUT_OF_RESOURCES;
  83. }
  84. RestExIns->Signature = RESTEX_INSTANCE_SIGNATURE;
  85. InitializeListHead (&RestExIns->Link);
  86. RestExIns->InDestroy = FALSE;
  87. RestExIns->Service = Service;
  88. CopyMem (&RestExIns->RestEx, &mRedfishRestExProtocol, sizeof (RestExIns->RestEx));
  89. //
  90. // Create a HTTP_IO to access the HTTP service.
  91. //
  92. Status = HttpIoCreateIo (
  93. RestExIns->Service->ImageHandle,
  94. RestExIns->Service->ControllerHandle,
  95. IP_VERSION_4,
  96. NULL,
  97. NULL,
  98. NULL,
  99. &(RestExIns->HttpIo)
  100. );
  101. if (EFI_ERROR (Status)) {
  102. FreePool (RestExIns);
  103. return Status;
  104. }
  105. *Instance = RestExIns;
  106. return EFI_SUCCESS;
  107. }
  108. /**
  109. Release all the resource used the RestEx service binding instance.
  110. @param[in] RestExSb The RestEx service binding instance.
  111. **/
  112. VOID
  113. RestExDestroyService (
  114. IN RESTEX_SERVICE *RestExSb
  115. )
  116. {
  117. if (RestExSb->HttpChildHandle != NULL) {
  118. gBS->CloseProtocol (
  119. RestExSb->HttpChildHandle,
  120. &gEfiHttpProtocolGuid,
  121. RestExSb->ImageHandle,
  122. RestExSb->ControllerHandle
  123. );
  124. NetLibDestroyServiceChild (
  125. RestExSb->ControllerHandle,
  126. RestExSb->ImageHandle,
  127. &gEfiHttpServiceBindingProtocolGuid,
  128. RestExSb->HttpChildHandle
  129. );
  130. RestExSb->HttpChildHandle = NULL;
  131. }
  132. gBS->UninstallProtocolInterface (
  133. RestExSb->ControllerHandle,
  134. &gEfiCallerIdGuid,
  135. &RestExSb->Id
  136. );
  137. FreePool (RestExSb);
  138. }
  139. /**
  140. Check the NIC controller handle represents an in-band or out-of-band Redfish host
  141. interface device. If not in-band, treat it as out-of-band interface device.
  142. @param[in] Controller The NIC controller handle needs to be checked.
  143. @return EFI_REST_EX_SERVICE_ACCESS_MODE of the device.
  144. **/
  145. EFI_REST_EX_SERVICE_ACCESS_MODE
  146. RestExServiceAccessMode (
  147. IN EFI_HANDLE Controller
  148. )
  149. {
  150. //
  151. // This is EFI REST EX driver instance to connect
  152. // to Redfish service using HTTP in out of band.
  153. //
  154. if (FixedPcdGetBool (PcdRedfishRestExServiceAccessModeInBand)) {
  155. return EfiRestExServiceInBandAccess;
  156. } else {
  157. return EfiRestExServiceOutOfBandAccess;
  158. }
  159. }
  160. /**
  161. Create then initialize a RestEx service binding instance.
  162. @param[in] Controller The controller to install the RestEx service
  163. binding on.
  164. @param[in] Image The driver binding image of the RestEx driver.
  165. @param[out] Service The variable to receive the created service
  166. binding instance.
  167. @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to create the instance.
  168. @retval EFI_SUCCESS The service instance is created for the controller.
  169. **/
  170. EFI_STATUS
  171. RestExCreateService (
  172. IN EFI_HANDLE Controller,
  173. IN EFI_HANDLE Image,
  174. OUT RESTEX_SERVICE **Service
  175. )
  176. {
  177. EFI_STATUS Status;
  178. RESTEX_SERVICE *RestExSb;
  179. Status = EFI_SUCCESS;
  180. RestExSb = NULL;
  181. *Service = NULL;
  182. RestExSb = AllocateZeroPool (sizeof (RESTEX_SERVICE));
  183. if (RestExSb == NULL) {
  184. return EFI_OUT_OF_RESOURCES;
  185. }
  186. RestExSb->Signature = RESTEX_SERVICE_SIGNATURE;
  187. RestExSb->ServiceBinding = mRedfishRestExServiceBinding;
  188. RestExSb->RestExChildrenNum = 0;
  189. InitializeListHead (&RestExSb->RestExChildrenList);
  190. RestExSb->ControllerHandle = Controller;
  191. RestExSb->ImageHandle = Image;
  192. RestExSb->RestExServiceInfo.EfiRestExServiceInfoV10.EfiRestExServiceInfoHeader.Length = sizeof (EFI_REST_EX_SERVICE_INFO);
  193. RestExSb->RestExServiceInfo.EfiRestExServiceInfoV10.EfiRestExServiceInfoHeader.RestServiceInfoVer.Major = 1;
  194. RestExSb->RestExServiceInfo.EfiRestExServiceInfoV10.EfiRestExServiceInfoHeader.RestServiceInfoVer.Minor = 0;
  195. RestExSb->RestExServiceInfo.EfiRestExServiceInfoV10.RestServiceType = EfiRestExServiceRedfish;
  196. RestExSb->RestExServiceInfo.EfiRestExServiceInfoV10.RestServiceAccessMode = RestExServiceAccessMode (Controller);
  197. RestExSb->RestExServiceInfo.EfiRestExServiceInfoV10.RestExConfigType = EfiRestExConfigHttp;
  198. RestExSb->RestExServiceInfo.EfiRestExServiceInfoV10.RestExConfigDataLength = sizeof (EFI_REST_EX_HTTP_CONFIG_DATA);
  199. Status = gBS->InstallProtocolInterface (
  200. &Controller,
  201. &gEfiCallerIdGuid,
  202. EFI_NATIVE_INTERFACE,
  203. &RestExSb->Id
  204. );
  205. if (EFI_ERROR (Status)) {
  206. FreePool (RestExSb);
  207. RestExSb = NULL;
  208. }
  209. *Service = RestExSb;
  210. return Status;
  211. }
  212. /**
  213. This is the declaration of an EFI image entry point. This entry point is
  214. the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
  215. both device drivers and bus drivers.
  216. @param[in] ImageHandle The firmware allocated handle for the UEFI image.
  217. @param[in] SystemTable A pointer to the EFI System Table.
  218. @retval EFI_SUCCESS The operation completed successfully.
  219. @retval Others An unexpected error occurred.
  220. **/
  221. EFI_STATUS
  222. EFIAPI
  223. RedfishRestExDriverEntryPoint (
  224. IN EFI_HANDLE ImageHandle,
  225. IN EFI_SYSTEM_TABLE *SystemTable
  226. )
  227. {
  228. EFI_STATUS Status;
  229. Status = EFI_SUCCESS;
  230. //
  231. // Install the RestEx Driver Binding Protocol.
  232. //
  233. Status = EfiLibInstallDriverBindingComponentName2 (
  234. ImageHandle,
  235. SystemTable,
  236. &gRedfishRestExDriverBinding,
  237. ImageHandle,
  238. &gRedfishRestExComponentName,
  239. &gRedfishRestExComponentName2
  240. );
  241. if (EFI_ERROR (Status)) {
  242. return Status;
  243. }
  244. return Status;
  245. }
  246. /**
  247. Tests to see if this driver supports a given controller. If a child device is provided,
  248. it further tests to see if this driver supports creating a handle for the specified child device.
  249. This function checks to see if the driver specified by This supports the device specified by
  250. ControllerHandle. Drivers will typically use the device path attached to
  251. ControllerHandle and/or the services from the bus I/O abstraction attached to
  252. ControllerHandle to determine if the driver supports ControllerHandle. This function
  253. may be called many times during platform initialization. In order to reduce boot times, the tests
  254. performed by this function must be very small, and take as little time as possible to execute. This
  255. function must not change the state of any hardware devices, and this function must be aware that the
  256. device specified by ControllerHandle may already be managed by the same driver or a
  257. different driver. This function must match its calls to AllocatePages() with FreePages(),
  258. AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
  259. Because ControllerHandle may have been previously started by the same driver, if a protocol is
  260. already in the opened state, then it must not be closed with CloseProtocol(). This is required
  261. to guarantee the state of ControllerHandle is not modified by this function.
  262. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
  263. @param[in] ControllerHandle The handle of the controller to test. This handle
  264. must support a protocol interface that supplies
  265. an I/O abstraction to the driver.
  266. @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
  267. parameter is ignored by device drivers, and is optional for bus
  268. drivers. For bus drivers, if this parameter is not NULL, then
  269. the bus driver must determine if the bus controller specified
  270. by ControllerHandle and the child controller specified
  271. by RemainingDevicePath are both supported by this
  272. bus driver.
  273. @retval EFI_SUCCESS The device specified by ControllerHandle and
  274. RemainingDevicePath is supported by the driver specified by This.
  275. @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
  276. RemainingDevicePath is already being managed by the driver
  277. specified by This.
  278. @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
  279. RemainingDevicePath is already being managed by a different
  280. driver or an application that requires exclusive access.
  281. Currently not implemented.
  282. @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
  283. RemainingDevicePath is not supported by the driver specified by This.
  284. **/
  285. EFI_STATUS
  286. EFIAPI
  287. RedfishRestExDriverBindingSupported (
  288. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  289. IN EFI_HANDLE ControllerHandle,
  290. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
  291. )
  292. {
  293. //
  294. // Test for the HttpServiceBinding Protocol.
  295. //
  296. return gBS->OpenProtocol (
  297. ControllerHandle,
  298. &gEfiHttpServiceBindingProtocolGuid,
  299. NULL,
  300. This->DriverBindingHandle,
  301. ControllerHandle,
  302. EFI_OPEN_PROTOCOL_TEST_PROTOCOL
  303. );
  304. }
  305. /**
  306. Starts a device controller or a bus controller.
  307. The Start() function is designed to be invoked from the EFI boot service ConnectController().
  308. As a result, much of the error checking on the parameters to Start() has been moved into this
  309. common boot service. It is legal to call Start() from other locations,
  310. but the following calling restrictions must be followed, or the system behavior will not be deterministic.
  311. 1. ControllerHandle must be a valid EFI_HANDLE.
  312. 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
  313. EFI_DEVICE_PATH_PROTOCOL.
  314. 3. Prior to calling Start(), the Supported() function for the driver specified by This must
  315. have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
  316. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
  317. @param[in] ControllerHandle The handle of the controller to start. This handle
  318. must support a protocol interface that supplies
  319. an I/O abstraction to the driver.
  320. @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
  321. parameter is ignored by device drivers, and is optional for bus
  322. drivers. For a bus driver, if this parameter is NULL, then handles
  323. for all the children of Controller are created by this driver.
  324. If this parameter is not NULL and the first Device Path Node is
  325. not the End of Device Path Node, then only the handle for the
  326. child device specified by the first Device Path Node of
  327. RemainingDevicePath is created by this driver.
  328. If the first Device Path Node of RemainingDevicePath is
  329. the End of Device Path Node, no child handle is created by this
  330. driver.
  331. @retval EFI_SUCCESS The device was started.
  332. @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
  333. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
  334. @retval Others The driver failded to start the device.
  335. **/
  336. EFI_STATUS
  337. EFIAPI
  338. RedfishRestExDriverBindingStart (
  339. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  340. IN EFI_HANDLE ControllerHandle,
  341. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
  342. )
  343. {
  344. RESTEX_SERVICE *RestExSb;
  345. EFI_STATUS Status;
  346. UINT32 *Id;
  347. VOID *Interface;
  348. Status = gBS->OpenProtocol (
  349. ControllerHandle,
  350. &gEfiCallerIdGuid,
  351. (VOID **)&Id,
  352. This->DriverBindingHandle,
  353. ControllerHandle,
  354. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  355. );
  356. if (!EFI_ERROR (Status)) {
  357. return EFI_ALREADY_STARTED;
  358. }
  359. Status = RestExCreateService (ControllerHandle, This->DriverBindingHandle, &RestExSb);
  360. if (EFI_ERROR (Status)) {
  361. return Status;
  362. }
  363. ASSERT (RestExSb != NULL);
  364. //
  365. // Create a Http child instance, but do not configure it.
  366. // This will establish the parent-child relationship.
  367. //
  368. Status = NetLibCreateServiceChild (
  369. ControllerHandle,
  370. This->DriverBindingHandle,
  371. &gEfiHttpServiceBindingProtocolGuid,
  372. &RestExSb->HttpChildHandle
  373. );
  374. if (EFI_ERROR (Status)) {
  375. goto ON_ERROR;
  376. }
  377. Status = gBS->OpenProtocol (
  378. RestExSb->HttpChildHandle,
  379. &gEfiHttpProtocolGuid,
  380. &Interface,
  381. This->DriverBindingHandle,
  382. ControllerHandle,
  383. EFI_OPEN_PROTOCOL_BY_DRIVER
  384. );
  385. if (EFI_ERROR (Status)) {
  386. goto ON_ERROR;
  387. }
  388. //
  389. // Install the RestEx ServiceBinding Protocol onto ControllerHandle.
  390. //
  391. Status = gBS->InstallMultipleProtocolInterfaces (
  392. &ControllerHandle,
  393. &gEfiRestExServiceBindingProtocolGuid,
  394. &RestExSb->ServiceBinding,
  395. NULL
  396. );
  397. if (EFI_ERROR (Status)) {
  398. goto ON_ERROR;
  399. }
  400. return EFI_SUCCESS;
  401. ON_ERROR:
  402. RestExDestroyService (RestExSb);
  403. return Status;
  404. }
  405. /**
  406. Stops a device controller or a bus controller.
  407. The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
  408. As a result, much of the error checking on the parameters to Stop() has been moved
  409. into this common boot service. It is legal to call Stop() from other locations,
  410. but the following calling restrictions must be followed, or the system behavior will not be deterministic.
  411. 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
  412. same driver's Start() function.
  413. 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
  414. EFI_HANDLE. In addition, all of these handles must have been created in this driver's
  415. Start() function, and the Start() function must have called OpenProtocol() on
  416. ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
  417. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
  418. @param[in] ControllerHandle A handle to the device being stopped. The handle must
  419. support a bus specific I/O protocol for the driver
  420. to use to stop the device.
  421. @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
  422. @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
  423. if NumberOfChildren is 0.
  424. @retval EFI_SUCCESS The device was stopped.
  425. @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
  426. **/
  427. EFI_STATUS
  428. EFIAPI
  429. RedfishRestExDriverBindingStop (
  430. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  431. IN EFI_HANDLE ControllerHandle,
  432. IN UINTN NumberOfChildren,
  433. IN EFI_HANDLE *ChildHandleBuffer OPTIONAL
  434. )
  435. {
  436. EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
  437. RESTEX_SERVICE *RestExSb;
  438. EFI_HANDLE NicHandle;
  439. EFI_STATUS Status;
  440. LIST_ENTRY *List;
  441. RESTEX_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT Context;
  442. //
  443. // RestEx driver opens HTTP child, So, Controller is a HTTP
  444. // child handle. Locate the Nic handle first. Then get the
  445. // RestEx private data back.
  446. //
  447. NicHandle = NetLibGetNicHandle (ControllerHandle, &gEfiHttpProtocolGuid);
  448. if (NicHandle == NULL) {
  449. return EFI_SUCCESS;
  450. }
  451. Status = gBS->OpenProtocol (
  452. NicHandle,
  453. &gEfiRestExServiceBindingProtocolGuid,
  454. (VOID **)&ServiceBinding,
  455. This->DriverBindingHandle,
  456. NicHandle,
  457. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  458. );
  459. if (EFI_ERROR (Status)) {
  460. return EFI_DEVICE_ERROR;
  461. }
  462. RestExSb = RESTEX_SERVICE_FROM_THIS (ServiceBinding);
  463. if (!IsListEmpty (&RestExSb->RestExChildrenList)) {
  464. //
  465. // Destroy the RestEx child instance in ChildHandleBuffer.
  466. //
  467. List = &RestExSb->RestExChildrenList;
  468. Context.ServiceBinding = ServiceBinding;
  469. Context.NumberOfChildren = NumberOfChildren;
  470. Context.ChildHandleBuffer = ChildHandleBuffer;
  471. Status = NetDestroyLinkList (
  472. List,
  473. RestExDestroyChildEntryInHandleBuffer,
  474. &Context,
  475. NULL
  476. );
  477. }
  478. if ((NumberOfChildren == 0) && IsListEmpty (&RestExSb->RestExChildrenList)) {
  479. gBS->UninstallProtocolInterface (
  480. NicHandle,
  481. &gEfiRestExServiceBindingProtocolGuid,
  482. ServiceBinding
  483. );
  484. RestExDestroyService (RestExSb);
  485. if (gRedfishRestExControllerNameTable != NULL) {
  486. FreeUnicodeStringTable (gRedfishRestExControllerNameTable);
  487. gRedfishRestExControllerNameTable = NULL;
  488. }
  489. Status = EFI_SUCCESS;
  490. }
  491. return Status;
  492. }
  493. /**
  494. Creates a child handle and installs a protocol.
  495. The CreateChild() function installs a protocol on ChildHandle.
  496. If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
  497. If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
  498. @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
  499. @param[in] ChildHandle Pointer to the handle of the child to create. If it is NULL,
  500. then a new handle is created. If it is a pointer to an existing UEFI handle,
  501. then the protocol is added to the existing UEFI handle.
  502. @retval EFI_SUCCES The protocol was added to ChildHandle.
  503. @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
  504. @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create
  505. the child
  506. @retval other The child handle was not created
  507. **/
  508. EFI_STATUS
  509. EFIAPI
  510. RedfishRestExServiceBindingCreateChild (
  511. IN EFI_SERVICE_BINDING_PROTOCOL *This,
  512. IN EFI_HANDLE *ChildHandle
  513. )
  514. {
  515. RESTEX_SERVICE *RestExSb;
  516. RESTEX_INSTANCE *Instance;
  517. EFI_STATUS Status;
  518. EFI_TPL OldTpl;
  519. VOID *Http;
  520. if ((This == NULL) || (ChildHandle == NULL)) {
  521. return EFI_INVALID_PARAMETER;
  522. }
  523. RestExSb = RESTEX_SERVICE_FROM_THIS (This);
  524. Status = RestExCreateInstance (RestExSb, &Instance);
  525. if (EFI_ERROR (Status)) {
  526. return Status;
  527. }
  528. ASSERT (Instance != NULL);
  529. //
  530. // Install the RestEx protocol onto ChildHandle
  531. //
  532. Status = gBS->InstallMultipleProtocolInterfaces (
  533. ChildHandle,
  534. &gEfiRestExProtocolGuid,
  535. &Instance->RestEx,
  536. NULL
  537. );
  538. if (EFI_ERROR (Status)) {
  539. goto ON_ERROR;
  540. }
  541. Instance->ChildHandle = *ChildHandle;
  542. //
  543. // Open the Http protocol BY_CHILD.
  544. //
  545. Status = gBS->OpenProtocol (
  546. RestExSb->HttpChildHandle,
  547. &gEfiHttpProtocolGuid,
  548. (VOID **)&Http,
  549. gRedfishRestExDriverBinding.DriverBindingHandle,
  550. Instance->ChildHandle,
  551. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
  552. );
  553. if (EFI_ERROR (Status)) {
  554. gBS->UninstallMultipleProtocolInterfaces (
  555. Instance->ChildHandle,
  556. &gEfiRestExProtocolGuid,
  557. &Instance->RestEx,
  558. NULL
  559. );
  560. goto ON_ERROR;
  561. }
  562. //
  563. // Open the Http protocol by child.
  564. //
  565. Status = gBS->OpenProtocol (
  566. Instance->HttpIo.Handle,
  567. &gEfiHttpProtocolGuid,
  568. (VOID **)&Http,
  569. gRedfishRestExDriverBinding.DriverBindingHandle,
  570. Instance->ChildHandle,
  571. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
  572. );
  573. if (EFI_ERROR (Status)) {
  574. //
  575. // Close the Http protocol.
  576. //
  577. gBS->CloseProtocol (
  578. RestExSb->HttpChildHandle,
  579. &gEfiHttpProtocolGuid,
  580. gRedfishRestExDriverBinding.DriverBindingHandle,
  581. ChildHandle
  582. );
  583. gBS->UninstallMultipleProtocolInterfaces (
  584. Instance->ChildHandle,
  585. &gEfiRestExProtocolGuid,
  586. &Instance->RestEx,
  587. NULL
  588. );
  589. goto ON_ERROR;
  590. }
  591. //
  592. // Add it to the parent's child list.
  593. //
  594. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  595. InsertTailList (&RestExSb->RestExChildrenList, &Instance->Link);
  596. RestExSb->RestExChildrenNum++;
  597. gBS->RestoreTPL (OldTpl);
  598. return EFI_SUCCESS;
  599. ON_ERROR:
  600. RestExDestroyInstance (Instance);
  601. return Status;
  602. }
  603. /**
  604. Destroys a child handle with a protocol installed on it.
  605. The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
  606. that was installed by CreateChild() from ChildHandle. If the removed protocol is the
  607. last protocol on ChildHandle, then ChildHandle is destroyed.
  608. @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
  609. @param[in] ChildHandle Handle of the child to destroy
  610. @retval EFI_SUCCES The protocol was removed from ChildHandle.
  611. @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
  612. @retval EFI_INVALID_PARAMETER Child handle is NULL.
  613. @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
  614. because its services are being used.
  615. @retval other The child handle was not destroyed
  616. **/
  617. EFI_STATUS
  618. EFIAPI
  619. RedfishRestExServiceBindingDestroyChild (
  620. IN EFI_SERVICE_BINDING_PROTOCOL *This,
  621. IN EFI_HANDLE ChildHandle
  622. )
  623. {
  624. RESTEX_SERVICE *RestExSb;
  625. RESTEX_INSTANCE *Instance;
  626. EFI_REST_EX_PROTOCOL *RestEx;
  627. EFI_STATUS Status;
  628. EFI_TPL OldTpl;
  629. if ((This == NULL) || (ChildHandle == NULL)) {
  630. return EFI_INVALID_PARAMETER;
  631. }
  632. //
  633. // Retrieve the private context data structures
  634. //
  635. Status = gBS->OpenProtocol (
  636. ChildHandle,
  637. &gEfiRestExProtocolGuid,
  638. (VOID **)&RestEx,
  639. NULL,
  640. NULL,
  641. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  642. );
  643. if (EFI_ERROR (Status)) {
  644. return EFI_UNSUPPORTED;
  645. }
  646. Instance = RESTEX_INSTANCE_FROM_THIS (RestEx);
  647. RestExSb = RESTEX_SERVICE_FROM_THIS (This);
  648. if (Instance->Service != RestExSb) {
  649. return EFI_INVALID_PARAMETER;
  650. }
  651. if (Instance->InDestroy) {
  652. return EFI_SUCCESS;
  653. }
  654. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  655. Instance->InDestroy = TRUE;
  656. //
  657. // Close the Http protocol.
  658. //
  659. gBS->CloseProtocol (
  660. RestExSb->HttpChildHandle,
  661. &gEfiHttpProtocolGuid,
  662. gRedfishRestExDriverBinding.DriverBindingHandle,
  663. ChildHandle
  664. );
  665. gBS->CloseProtocol (
  666. Instance->HttpIo.Handle,
  667. &gEfiHttpProtocolGuid,
  668. gRedfishRestExDriverBinding.DriverBindingHandle,
  669. ChildHandle
  670. );
  671. gBS->RestoreTPL (OldTpl);
  672. //
  673. // Uninstall the RestEx protocol first to enable a top down destruction.
  674. //
  675. Status = gBS->UninstallProtocolInterface (
  676. ChildHandle,
  677. &gEfiRestExProtocolGuid,
  678. RestEx
  679. );
  680. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  681. if (EFI_ERROR (Status)) {
  682. Instance->InDestroy = FALSE;
  683. gBS->RestoreTPL (OldTpl);
  684. return Status;
  685. }
  686. RemoveEntryList (&Instance->Link);
  687. RestExSb->RestExChildrenNum--;
  688. gBS->RestoreTPL (OldTpl);
  689. RestExDestroyInstance (Instance);
  690. return EFI_SUCCESS;
  691. }