RedfishLib.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. /** @file
  2. Provides a set of utility APIs that allow to create/read/update/delete
  3. (CRUD) Redfish resources and provide basic query.
  4. Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
  5. (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>
  6. SPDX-License-Identifier: BSD-2-Clause-Patent
  7. **/
  8. #include "RedfishMisc.h"
  9. /**
  10. This function uses REST EX protocol provided in RedfishConfigServiceInfo.
  11. The service enumerator will also handle the authentication flow automatically
  12. if HTTP basic auth or Redfish session login is configured to use.
  13. Callers are responsible for freeing the returned service by RedfishCleanupService().
  14. @param[in] RedfishConfigServiceInfo Redfish service information the EFI Redfish
  15. feature driver communicates with.
  16. @return New created Redfish Service, or NULL if error happens.
  17. **/
  18. REDFISH_SERVICE
  19. EFIAPI
  20. RedfishCreateService (
  21. IN REDFISH_CONFIG_SERVICE_INFORMATION *RedfishConfigServiceInfo
  22. )
  23. {
  24. REDFISH_SERVICE RedfishService;
  25. EDKII_REDFISH_AUTH_METHOD AuthMethod;
  26. CHAR8 *UserId;
  27. CHAR8 *Password;
  28. EFI_STATUS Status;
  29. RedfishService = NULL;
  30. UserId = NULL;
  31. Password = NULL;
  32. //
  33. // Check Input Parameters.
  34. //
  35. if (RedfishConfigServiceInfo == NULL) {
  36. return NULL;
  37. }
  38. //
  39. // Get Authentication Configuration.
  40. //
  41. Status = RedfishGetAuthInfo (&AuthMethod, &UserId, &Password);
  42. if (EFI_ERROR (Status)) {
  43. goto ON_EXIT;
  44. }
  45. //
  46. // Create a redfish service node based on Redfish network host interface.
  47. //
  48. RedfishService = RedfishCreateLibredfishService (
  49. RedfishConfigServiceInfo,
  50. AuthMethod,
  51. UserId,
  52. Password
  53. );
  54. ON_EXIT:
  55. if (UserId != NULL) {
  56. FreePool (UserId);
  57. }
  58. if (Password != NULL) {
  59. FreePool (Password);
  60. }
  61. return RedfishService;
  62. }
  63. /**
  64. Free the Service and all its related resources.
  65. @param[in] RedfishService The Service to access the Redfish resources.
  66. **/
  67. VOID
  68. EFIAPI
  69. RedfishCleanupService (
  70. IN REDFISH_SERVICE RedfishService
  71. )
  72. {
  73. if (RedfishService == NULL) {
  74. return;
  75. }
  76. cleanupServiceEnumerator (RedfishService);
  77. }
  78. /**
  79. Create REDFISH_PAYLOAD instance in local with JSON represented resource value and
  80. the Redfish Service.
  81. The returned REDFISH_PAYLOAD can be used to create or update Redfish resource in
  82. server side.
  83. Callers are responsible for freeing the returned payload by RedfishCleanupPayload().
  84. @param[in] Value JSON Value of the redfish resource.
  85. @param[in] RedfishService The Service to access the Redfish resources.
  86. @return REDFISH_PAYLOAD instance of the resource, or NULL if error happens.
  87. **/
  88. REDFISH_PAYLOAD
  89. EFIAPI
  90. RedfishCreatePayload (
  91. IN EDKII_JSON_VALUE Value,
  92. IN REDFISH_SERVICE RedfishService
  93. )
  94. {
  95. EDKII_JSON_VALUE CopyValue;
  96. CopyValue = JsonValueClone (Value);
  97. return createRedfishPayload (CopyValue, RedfishService);
  98. }
  99. /**
  100. Free the RedfishPayload and all its related resources.
  101. @param[in] Payload Payload to be freed.
  102. **/
  103. VOID
  104. EFIAPI
  105. RedfishCleanupPayload (
  106. IN REDFISH_PAYLOAD Payload
  107. )
  108. {
  109. if (Payload == NULL) {
  110. return;
  111. }
  112. cleanupPayload ((redfishPayload *)Payload);
  113. }
  114. /**
  115. This function returns the decoded JSON value of a REDFISH_PAYLOAD.
  116. Caller doesn't need to free the returned JSON value because it will be released
  117. in corresponding RedfishCleanupPayload() function.
  118. @param[in] Payload A REDFISH_PAYLOAD instance.
  119. @return Decoded JSON value of the payload.
  120. **/
  121. EDKII_JSON_VALUE
  122. EFIAPI
  123. RedfishJsonInPayload (
  124. IN REDFISH_PAYLOAD Payload
  125. )
  126. {
  127. if (Payload == NULL) {
  128. return NULL;
  129. }
  130. return ((redfishPayload *)Payload)->json;
  131. }
  132. /**
  133. Fill the input RedPath string with system UUID from SMBIOS table or use the customized
  134. ID if FromSmbios == FALSE.
  135. This is a helper function to build a RedPath string which can be used to address
  136. a Redfish resource for this computer system. The input PathString must have a Systems
  137. note in format of "Systems[UUID=%g]" or "Systems[UUID~%g]" to fill the UUID value.
  138. Example:
  139. Use "/v1/Systems[UUID=%g]/Bios" to build a RedPath to address the "Bios" resource
  140. for this computer system.
  141. @param[in] RedPath RedPath format to be build.
  142. @param[in] FromSmbios Get system UUID from SMBIOS as computer system instance ID.
  143. @param[in] IdString The computer system instance ID.
  144. @return Full RedPath with system UUID inside, or NULL if error happens.
  145. **/
  146. CHAR8 *
  147. EFIAPI
  148. RedfishBuildPathWithSystemUuid (
  149. IN CONST CHAR8 *RedPath,
  150. IN BOOLEAN FromSmbios,
  151. IN CHAR8 *IdString OPTIONAL
  152. )
  153. {
  154. UINTN BufSize;
  155. CHAR8 *RetRedPath;
  156. EFI_GUID SystemUuid;
  157. EFI_STATUS Status;
  158. if (RedPath == NULL) {
  159. return NULL;
  160. }
  161. //
  162. // Find system UUID from SMBIOS table.
  163. //
  164. if (FromSmbios) {
  165. Status = NetLibGetSystemGuid (&SystemUuid);
  166. if (EFI_ERROR (Status)) {
  167. return NULL;
  168. }
  169. // AsciiStrLen ("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx") = 36
  170. BufSize = AsciiStrSize (RedPath) + AsciiStrLen ("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");
  171. } else {
  172. BufSize = AsciiStrSize (RedPath) + AsciiStrLen (IdString);
  173. }
  174. RetRedPath = AllocateZeroPool (BufSize);
  175. if (RetRedPath == NULL) {
  176. return NULL;
  177. }
  178. if (FromSmbios) {
  179. AsciiSPrint (RetRedPath, BufSize, RedPath, &SystemUuid);
  180. } else {
  181. AsciiSPrint (RetRedPath, BufSize, RedPath, IdString);
  182. }
  183. return RetRedPath;
  184. }
  185. /**
  186. Get a redfish response addressed by a RedPath string, including HTTP StatusCode, Headers
  187. and Payload which record any HTTP response messages.
  188. Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in
  189. redfish response data.
  190. @param[in] RedfishService The Service to access the Redfish resources.
  191. @param[in] RedPath RedPath string to address a resource, must start
  192. from the root node.
  193. @param[out] RedResponse Pointer to the Redfish response data.
  194. @retval EFI_SUCCESS The opeartion is successful, indicates the HTTP StatusCode is not
  195. NULL and the value is 2XX. The corresponding redfish resource has
  196. been returned in Payload within RedResponse.
  197. @retval EFI_INVALID_PARAMETER RedfishService, RedPath, or RedResponse is NULL.
  198. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. Callers can get
  199. more error info from returned HTTP StatusCode, Headers and Payload
  200. within RedResponse:
  201. 1. If the returned Payload is NULL, indicates any error happen.
  202. 2. If the returned StatusCode is NULL, indicates any error happen.
  203. 3. If the returned StatusCode is not 2XX, indicates any error happen.
  204. **/
  205. EFI_STATUS
  206. EFIAPI
  207. RedfishGetByService (
  208. IN REDFISH_SERVICE RedfishService,
  209. IN CONST CHAR8 *RedPath,
  210. OUT REDFISH_RESPONSE *RedResponse
  211. )
  212. {
  213. if ((RedfishService == NULL) || (RedPath == NULL) || (RedResponse == NULL)) {
  214. return EFI_INVALID_PARAMETER;
  215. }
  216. ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE));
  217. RedResponse->Payload = (REDFISH_PAYLOAD)getPayloadByPath (RedfishService, RedPath, &(RedResponse->StatusCode));
  218. //
  219. // 1. If the returned Payload is NULL, indicates any error happen.
  220. // 2. If the returned StatusCode is NULL, indicates any error happen.
  221. //
  222. if ((RedResponse->Payload == NULL) || (RedResponse->StatusCode == NULL)) {
  223. return EFI_DEVICE_ERROR;
  224. }
  225. //
  226. // 3. If the returned StatusCode is not 2XX, indicates any error happen.
  227. // NOTE: If there is any error message returned from server, it will be returned in
  228. // Payload within RedResponse.
  229. //
  230. if ((*(RedResponse->StatusCode) < HTTP_STATUS_200_OK) || \
  231. (*(RedResponse->StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT))
  232. {
  233. return EFI_DEVICE_ERROR;
  234. }
  235. return EFI_SUCCESS;
  236. }
  237. /**
  238. Get a redfish response addressed by URI, including HTTP StatusCode, Headers
  239. and Payload which record any HTTP response messages.
  240. Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in
  241. redfish response data.
  242. @param[in] RedfishService The Service to access the URI resources.
  243. @param[in] Uri String to address a resource.
  244. @param[out] RedResponse Pointer to the Redfish response data.
  245. @retval EFI_SUCCESS The opeartion is successful, indicates the HTTP StatusCode is not
  246. NULL and the value is 2XX. The corresponding redfish resource has
  247. been returned in Payload within RedResponse.
  248. @retval EFI_INVALID_PARAMETER RedfishService, RedPath, or RedResponse is NULL.
  249. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. Callers can get
  250. more error info from returned HTTP StatusCode, Headers and Payload
  251. within RedResponse:
  252. 1. If the returned Payload is NULL, indicates any error happen.
  253. 2. If the returned StatusCode is NULL, indicates any error happen.
  254. 3. If the returned StatusCode is not 2XX, indicates any error happen.
  255. **/
  256. EFI_STATUS
  257. EFIAPI
  258. RedfishGetByUri (
  259. IN REDFISH_SERVICE RedfishService,
  260. IN CONST CHAR8 *Uri,
  261. OUT REDFISH_RESPONSE *RedResponse
  262. )
  263. {
  264. EDKII_JSON_VALUE JsonValue;
  265. if ((RedfishService == NULL) || (Uri == NULL) || (RedResponse == NULL)) {
  266. return EFI_INVALID_PARAMETER;
  267. }
  268. ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE));
  269. JsonValue = getUriFromService (RedfishService, Uri, &RedResponse->StatusCode);
  270. RedResponse->Payload = createRedfishPayload (JsonValue, RedfishService);
  271. //
  272. // 1. If the returned Payload is NULL, indicates any error happen.
  273. // 2. If the returned StatusCode is NULL, indicates any error happen.
  274. //
  275. if ((RedResponse->Payload == NULL) || (RedResponse->StatusCode == NULL)) {
  276. return EFI_DEVICE_ERROR;
  277. }
  278. //
  279. // 3. If the returned StatusCode is not 2XX, indicates any error happen.
  280. // NOTE: If there is any error message returned from server, it will be returned in
  281. // Payload within RedResponse.
  282. //
  283. if ((*(RedResponse->StatusCode) < HTTP_STATUS_200_OK) || \
  284. (*(RedResponse->StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT))
  285. {
  286. return EFI_DEVICE_ERROR;
  287. }
  288. return EFI_SUCCESS;
  289. }
  290. /**
  291. Get a redfish response addressed by the input Payload and relative RedPath string,
  292. including HTTP StatusCode, Headers and Payload which record any HTTP response messages.
  293. Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in
  294. redfish response data.
  295. @param[in] Payload A existing REDFISH_PAYLOAD instance.
  296. @param[in] RedPath Relative RedPath string to address a resource inside Payload.
  297. @param[out] RedResponse Pointer to the Redfish response data.
  298. @retval EFI_SUCCESS The opeartion is successful:
  299. 1. The HTTP StatusCode is NULL and the returned Payload in
  300. RedResponse is not NULL, indicates the Redfish resource has
  301. been parsed from the input payload directly.
  302. 2. The HTTP StatusCode is not NULL and the value is 2XX,
  303. indicates the corresponding redfish resource has been returned
  304. in Payload within RedResponse.
  305. @retval EFI_INVALID_PARAMETER Payload, RedPath, or RedResponse is NULL.
  306. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. Callers can get
  307. more error info from returned HTTP StatusCode, Headers and Payload
  308. within RedResponse:
  309. 1. If the returned Payload is NULL, indicates any error happen.
  310. 2. If StatusCode is not NULL and the returned value of StatusCode
  311. is not 2XX, indicates any error happen.
  312. **/
  313. EFI_STATUS
  314. EFIAPI
  315. RedfishGetByPayload (
  316. IN REDFISH_PAYLOAD Payload,
  317. IN CONST CHAR8 *RedPath,
  318. OUT REDFISH_RESPONSE *RedResponse
  319. )
  320. {
  321. if ((Payload == NULL) || (RedPath == NULL) || (RedResponse == NULL)) {
  322. return EFI_INVALID_PARAMETER;
  323. }
  324. ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE));
  325. RedResponse->Payload = (REDFISH_PAYLOAD)getPayloadForPathString (Payload, RedPath, &(RedResponse->StatusCode));
  326. //
  327. // 1. If the returned Payload is NULL, indicates any error happen.
  328. //
  329. if (RedResponse->Payload == NULL) {
  330. return EFI_DEVICE_ERROR;
  331. }
  332. //
  333. // 2. If StatusCode is not NULL and the returned value of StatusCode is not 2XX, indicates any
  334. // error happen.
  335. // NOTE: If there is any error message returned from server, it will be returned in
  336. // Payload within RedResponse.
  337. //
  338. if ((RedResponse->StatusCode != NULL) && \
  339. ((*(RedResponse->StatusCode) < HTTP_STATUS_200_OK) || \
  340. (*(RedResponse->StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT)
  341. ))
  342. {
  343. return EFI_DEVICE_ERROR;
  344. }
  345. return EFI_SUCCESS;
  346. }
  347. /**
  348. Use HTTP PATCH to perform updates on pre-existing Redfish resource.
  349. This function uses the RedfishService to patch a Redfish resource addressed by
  350. Uri (only the relative path is required). Changes to one or more properties within
  351. the target resource are represented in the input Content, properties not specified
  352. in Content won't be changed by this request. The corresponding redfish response will
  353. returned, including HTTP StatusCode, Headers and Payload which record any HTTP response
  354. messages.
  355. Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in
  356. redfish response data.
  357. @param[in] RedfishService The Service to access the Redfish resources.
  358. @param[in] Uri Relative path to address the resource.
  359. @param[in] Content JSON represented properties to be update.
  360. @param[out] RedResponse Pointer to the Redfish response data.
  361. @retval EFI_SUCCESS The opeartion is successful, indicates the HTTP StatusCode is not
  362. NULL and the value is 2XX. The Redfish resource will be returned
  363. in Payload within RedResponse if server send it back in the HTTP
  364. response message body.
  365. @retval EFI_INVALID_PARAMETER RedfishService, Uri, Content, or RedResponse is NULL.
  366. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. Callers can get
  367. more error info from returned HTTP StatusCode, Headers and Payload
  368. within RedResponse:
  369. 1. If the returned StatusCode is NULL, indicates any error happen.
  370. 2. If the returned StatusCode is not NULL and the value is not 2XX,
  371. indicates any error happen.
  372. **/
  373. EFI_STATUS
  374. EFIAPI
  375. RedfishPatchToUri (
  376. IN REDFISH_SERVICE RedfishService,
  377. IN CONST CHAR8 *Uri,
  378. IN CONST CHAR8 *Content,
  379. OUT REDFISH_RESPONSE *RedResponse
  380. )
  381. {
  382. EFI_STATUS Status;
  383. EDKII_JSON_VALUE JsonValue;
  384. Status = EFI_SUCCESS;
  385. JsonValue = NULL;
  386. if ((RedfishService == NULL) || (Uri == NULL) || (Content == NULL) || (RedResponse == NULL)) {
  387. return EFI_INVALID_PARAMETER;
  388. }
  389. ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE));
  390. JsonValue = (EDKII_JSON_VALUE)patchUriFromService (
  391. RedfishService,
  392. Uri,
  393. Content,
  394. &(RedResponse->StatusCode)
  395. );
  396. //
  397. // 1. If the returned StatusCode is NULL, indicates any error happen.
  398. //
  399. if (RedResponse->StatusCode == NULL) {
  400. Status = EFI_DEVICE_ERROR;
  401. goto ON_EXIT;
  402. }
  403. //
  404. // 2. If the returned StatusCode is not NULL and the value is not 2XX, indicates any error happen.
  405. // NOTE: If there is any error message returned from server, it will be returned in
  406. // Payload within RedResponse.
  407. //
  408. if ((*(RedResponse->StatusCode) < HTTP_STATUS_200_OK) || \
  409. (*(RedResponse->StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT))
  410. {
  411. Status = EFI_DEVICE_ERROR;
  412. }
  413. ON_EXIT:
  414. if (JsonValue != NULL) {
  415. RedResponse->Payload = createRedfishPayload (JsonValue, RedfishService);
  416. if (RedResponse->Payload == NULL) {
  417. //
  418. // Ignore the error when create RedfishPayload, just free the JsonValue since it's not what
  419. // we care about if the returned StatusCode is 2XX.
  420. //
  421. JsonValueFree (JsonValue);
  422. }
  423. }
  424. return Status;
  425. }
  426. /**
  427. Use HTTP PATCH to perform updates on target payload. Patch to odata.id in Payload directly.
  428. This function uses the Payload to patch the Target. Changes to one or more properties
  429. within the target resource are represented in the input Payload, properties not specified
  430. in Payload won't be changed by this request. The corresponding redfish response will
  431. returned, including HTTP StatusCode, Headers and Payload which record any HTTP response
  432. messages.
  433. Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in
  434. redfish response data.
  435. @param[in] Target The target payload to be updated.
  436. @param[in] Payload Palyoad with properties to be changed.
  437. @param[out] RedResponse Pointer to the Redfish response data.
  438. @retval EFI_SUCCESS The opeartion is successful, indicates the HTTP StatusCode is not
  439. NULL and the value is 2XX. The Redfish resource will be returned
  440. in Payload within RedResponse if server send it back in the HTTP
  441. response message body.
  442. @retval EFI_INVALID_PARAMETER Target, Payload, or RedResponse is NULL.
  443. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. Callers can get
  444. more error info from returned HTTP StatusCode, Headers and Payload
  445. within RedResponse:
  446. 1. If the returned StatusCode is NULL, indicates any error happen.
  447. 2. If the returned StatusCode is not NULL and the value is not 2XX,
  448. indicates any error happen.
  449. **/
  450. EFI_STATUS
  451. EFIAPI
  452. RedfishPatchToPayload (
  453. IN REDFISH_PAYLOAD Target,
  454. IN REDFISH_PAYLOAD Payload,
  455. OUT REDFISH_RESPONSE *RedResponse
  456. )
  457. {
  458. if ((Target == NULL) || (Payload == NULL) || (RedResponse == NULL)) {
  459. return EFI_INVALID_PARAMETER;
  460. }
  461. ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE));
  462. RedResponse->Payload = (REDFISH_PAYLOAD)patchPayload (
  463. Target,
  464. Payload,
  465. &(RedResponse->StatusCode)
  466. );
  467. //
  468. // 1. If the returned StatusCode is NULL, indicates any error happen.
  469. //
  470. if (RedResponse->StatusCode == NULL) {
  471. return EFI_DEVICE_ERROR;
  472. }
  473. //
  474. // 2. If the returned StatusCode is not NULL and the value is not 2XX, indicates any error happen.
  475. // NOTE: If there is any error message returned from server, it will be returned in
  476. // Payload within RedResponse.
  477. //
  478. if ((*(RedResponse->StatusCode) < HTTP_STATUS_200_OK) || \
  479. (*(RedResponse->StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT))
  480. {
  481. return EFI_DEVICE_ERROR;
  482. }
  483. return EFI_SUCCESS;
  484. }
  485. /**
  486. Use HTTP POST to create a new resource in target payload.
  487. The POST request should be submitted to the Resource Collection in which the new resource
  488. is to belong. The Resource Collection is addressed by Target payload. The Redfish may
  489. ignore any service controlled properties. The corresponding redfish response will returned,
  490. including HTTP StatusCode, Headers and Payload which record any HTTP response messages.
  491. Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in
  492. redfish response data.
  493. @param[in] Target Target payload of the Resource Collection.
  494. @param[in] Payload The new resource to be created.
  495. @param[out] RedResponse Pointer to the Redfish response data.
  496. @retval EFI_SUCCESS The opeartion is successful, indicates the HTTP StatusCode is not
  497. NULL and the value is 2XX. The Redfish resource will be returned
  498. in Payload within RedResponse if server send it back in the HTTP
  499. response message body.
  500. @retval EFI_INVALID_PARAMETER Target, Payload, or RedResponse is NULL.
  501. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. Callers can get
  502. more error info from returned HTTP StatusCode, Headers and Payload
  503. within RedResponse:
  504. 1. If the returned StatusCode is NULL, indicates any error happen.
  505. 2. If the returned StatusCode is not NULL and the value is not 2XX,
  506. indicates any error happen.
  507. **/
  508. EFI_STATUS
  509. EFIAPI
  510. RedfishPostToPayload (
  511. IN REDFISH_PAYLOAD Target,
  512. IN REDFISH_PAYLOAD Payload,
  513. OUT REDFISH_RESPONSE *RedResponse
  514. )
  515. {
  516. if ((Target == NULL) || (Payload == NULL) || (RedResponse == NULL)) {
  517. return EFI_INVALID_PARAMETER;
  518. }
  519. ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE));
  520. RedResponse->Payload = (REDFISH_PAYLOAD)postPayload (
  521. Target,
  522. Payload,
  523. &(RedResponse->StatusCode)
  524. );
  525. //
  526. // 1. If the returned StatusCode is NULL, indicates any error happen.
  527. //
  528. if (RedResponse->StatusCode == NULL) {
  529. return EFI_DEVICE_ERROR;
  530. }
  531. //
  532. // 2. If the returned StatusCode is not NULL and the value is not 2XX, indicates any error happen.
  533. // NOTE: If there is any error message returned from server, it will be returned in
  534. // Payload within RedResponse.
  535. //
  536. if ((*(RedResponse->StatusCode) < HTTP_STATUS_200_OK) || \
  537. (*(RedResponse->StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT))
  538. {
  539. return EFI_DEVICE_ERROR;
  540. }
  541. return EFI_SUCCESS;
  542. }
  543. /**
  544. Use HTTP DELETE to remove a resource.
  545. This function uses the RedfishService to remove a Redfish resource which is addressed
  546. by input Uri (only the relative path is required). The corresponding redfish response will
  547. returned, including HTTP StatusCode, Headers and Payload which record any HTTP response
  548. messages.
  549. Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in
  550. redfish response data.
  551. @param[in] RedfishService The Service to access the Redfish resources.
  552. @param[in] Uri Relative path to address the resource.
  553. @param[out] RedResponse Pointer to the Redfish response data.
  554. @retval EFI_SUCCESS The opeartion is successful, indicates the HTTP StatusCode is not
  555. NULL and the value is 2XX, the Redfish resource has been removed.
  556. If there is any message returned from server, it will be returned
  557. in Payload within RedResponse.
  558. @retval EFI_INVALID_PARAMETER RedfishService, Uri, or RedResponse is NULL.
  559. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. Callers can get
  560. more error info from returned HTTP StatusCode, Headers and Payload
  561. within RedResponse:
  562. 1. If the returned StatusCode is NULL, indicates any error happen.
  563. 2. If the returned StatusCode is not NULL and the value is not 2XX,
  564. indicates any error happen.
  565. **/
  566. EFI_STATUS
  567. EFIAPI
  568. RedfishDeleteByUri (
  569. IN REDFISH_SERVICE RedfishService,
  570. IN CONST CHAR8 *Uri,
  571. OUT REDFISH_RESPONSE *RedResponse
  572. )
  573. {
  574. EFI_STATUS Status;
  575. EDKII_JSON_VALUE JsonValue;
  576. Status = EFI_SUCCESS;
  577. JsonValue = NULL;
  578. if ((RedfishService == NULL) || (Uri == NULL) || (RedResponse == NULL)) {
  579. return EFI_INVALID_PARAMETER;
  580. }
  581. ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE));
  582. JsonValue = (EDKII_JSON_VALUE)deleteUriFromService (
  583. RedfishService,
  584. Uri,
  585. &(RedResponse->StatusCode)
  586. );
  587. //
  588. // 1. If the returned StatusCode is NULL, indicates any error happen.
  589. //
  590. if (RedResponse->StatusCode == NULL) {
  591. Status = EFI_DEVICE_ERROR;
  592. goto ON_EXIT;
  593. }
  594. //
  595. // 2. If the returned StatusCode is not NULL and the value is not 2XX, indicates any error happen.
  596. // NOTE: If there is any error message returned from server, it will be returned in
  597. // Payload within RedResponse.
  598. //
  599. if ((*(RedResponse->StatusCode) < HTTP_STATUS_200_OK) || \
  600. (*(RedResponse->StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT))
  601. {
  602. Status = EFI_DEVICE_ERROR;
  603. }
  604. ON_EXIT:
  605. if (JsonValue != NULL) {
  606. RedResponse->Payload = createRedfishPayload (JsonValue, RedfishService);
  607. if (RedResponse->Payload == NULL) {
  608. //
  609. // Ignore the error when create RedfishPayload, just free the JsonValue since it's not what
  610. // we care about if the returned StatusCode is 2XX.
  611. //
  612. JsonValueFree (JsonValue);
  613. }
  614. }
  615. return Status;
  616. }
  617. /**
  618. Dump text in fractions.
  619. @param[in] String ASCII string to dump.
  620. **/
  621. VOID
  622. RedfishDumpJsonStringFractions (
  623. IN CHAR8 *String
  624. )
  625. {
  626. CHAR8 *NextFraction;
  627. UINTN StringFractionSize;
  628. UINTN StrLen;
  629. UINTN Count;
  630. CHAR8 BackupChar;
  631. StringFractionSize = 200;
  632. if (String == NULL) {
  633. return;
  634. }
  635. DEBUG ((DEBUG_INFO, "JSON text:\n"));
  636. NextFraction = String;
  637. StrLen = AsciiStrLen (String);
  638. if (StrLen == 0) {
  639. return;
  640. }
  641. for (Count = 0; Count < (StrLen / StringFractionSize); Count++) {
  642. BackupChar = *(NextFraction + StringFractionSize);
  643. *(NextFraction + StringFractionSize) = 0;
  644. DEBUG ((DEBUG_INFO, "%a", NextFraction));
  645. *(NextFraction + StringFractionSize) = BackupChar;
  646. NextFraction += StringFractionSize;
  647. }
  648. if ((StrLen % StringFractionSize) != 0) {
  649. DEBUG ((DEBUG_INFO, "%a\n\n", NextFraction));
  650. }
  651. }
  652. /**
  653. Dump text in JSON value.
  654. @param[in] JsonValue The Redfish JSON value to dump.
  655. **/
  656. VOID
  657. RedfishDumpJson (
  658. IN EDKII_JSON_VALUE JsonValue
  659. )
  660. {
  661. CHAR8 *String;
  662. String = JsonDumpString (JsonValue, 0);
  663. if (String == NULL) {
  664. return;
  665. }
  666. RedfishDumpJsonStringFractions (String);
  667. FreePool (String);
  668. }
  669. /**
  670. Extract the JSON text content from REDFISH_PAYLOAD and dump to debug console.
  671. @param[in] Payload The Redfish payload to dump.
  672. **/
  673. VOID
  674. RedfishDumpPayload (
  675. IN REDFISH_PAYLOAD Payload
  676. )
  677. {
  678. EDKII_JSON_VALUE JsonValue;
  679. CHAR8 *String;
  680. JsonValue = NULL;
  681. String = NULL;
  682. if (Payload == NULL) {
  683. return;
  684. }
  685. JsonValue = RedfishJsonInPayload (Payload);
  686. if (JsonValue == NULL) {
  687. return;
  688. }
  689. String = JsonDumpString (JsonValue, 0);
  690. if (String == NULL) {
  691. return;
  692. }
  693. RedfishDumpJsonStringFractions (String);
  694. FreePool (String);
  695. }
  696. /**
  697. This function will cleanup the HTTP header and Redfish payload resources.
  698. @param[in] StatusCode The status code in HTTP response message.
  699. @param[in] HeaderCount Number of HTTP header structures in Headers list.
  700. @param[in] Headers Array containing list of HTTP headers.
  701. @param[in] Payload The Redfish payload to dump.
  702. **/
  703. VOID
  704. RedfishFreeResponse (
  705. IN EFI_HTTP_STATUS_CODE *StatusCode,
  706. IN UINTN HeaderCount,
  707. IN EFI_HTTP_HEADER *Headers,
  708. IN REDFISH_PAYLOAD Payload
  709. )
  710. {
  711. if (StatusCode != NULL) {
  712. FreePool (StatusCode);
  713. StatusCode = NULL;
  714. }
  715. if ((HeaderCount != 0) && (Headers != NULL)) {
  716. HttpFreeHeaderFields (Headers, HeaderCount);
  717. Headers = NULL;
  718. }
  719. if (Payload != NULL) {
  720. RedfishCleanupPayload (Payload);
  721. Payload = NULL;
  722. }
  723. }
  724. /**
  725. Check if the "@odata.type" in Payload is valid or not.
  726. @param[in] Payload The Redfish payload to be checked.
  727. @param[in] OdataTypeName OdataType will be retrived from mapping list.
  728. @param[in] OdataTypeMappingList The list of OdataType.
  729. @param[in] OdataTypeMappingListSize The number of mapping list
  730. @return TRUE if the "@odata.type" in Payload is valid, otherwise FALSE.
  731. **/
  732. BOOLEAN
  733. RedfishIsValidOdataType (
  734. IN REDFISH_PAYLOAD Payload,
  735. IN CONST CHAR8 *OdataTypeName,
  736. IN REDFISH_ODATA_TYPE_MAPPING *OdataTypeMappingList,
  737. IN UINTN OdataTypeMappingListSize
  738. )
  739. {
  740. UINTN Index;
  741. EDKII_JSON_VALUE OdataType;
  742. EDKII_JSON_VALUE JsonValue;
  743. if ((Payload == NULL) || (OdataTypeName == NULL)) {
  744. return FALSE;
  745. }
  746. JsonValue = RedfishJsonInPayload (Payload);
  747. if (!JsonValueIsObject (JsonValue)) {
  748. return FALSE;
  749. }
  750. OdataType = JsonObjectGetValue (JsonValueGetObject (JsonValue), "@odata.type");
  751. if (!JsonValueIsString (OdataType) || (JsonValueGetAsciiString (OdataType) == NULL)) {
  752. return FALSE;
  753. }
  754. for (Index = 0; Index < OdataTypeMappingListSize; Index++) {
  755. if ((AsciiStrCmp (OdataTypeMappingList[Index].OdataTypeName, OdataTypeName) == 0) &&
  756. (AsciiStrCmp (OdataTypeMappingList[Index].OdataType, JsonValueGetAsciiString (OdataType)) == 0))
  757. {
  758. return TRUE;
  759. }
  760. }
  761. DEBUG ((DEBUG_INFO, "%a: This Odata type is not in the list.\n", __FUNCTION__));
  762. return FALSE;
  763. }
  764. /**
  765. Check if the payload is collection
  766. @param[in] Payload The Redfish payload to be checked.
  767. @return TRUE if the payload is collection.
  768. **/
  769. BOOLEAN
  770. RedfishIsPayloadCollection (
  771. IN REDFISH_PAYLOAD Payload
  772. )
  773. {
  774. return isPayloadCollection (Payload);
  775. }
  776. /**
  777. Get collection size.
  778. @param[in] Payload The Redfish collection payload
  779. @param[in] CollectionSize Size of this collection
  780. @return EFI_SUCCESS Coolection size is returned in CollectionSize
  781. @return EFI_INVALID_PARAMETER The payload is not a collection.
  782. **/
  783. EFI_STATUS
  784. RedfishGetCollectionSize (
  785. IN REDFISH_PAYLOAD Payload,
  786. IN UINTN *CollectionSize
  787. )
  788. {
  789. if ((Payload == NULL) || (CollectionSize == NULL)) {
  790. return EFI_INVALID_PARAMETER;
  791. }
  792. if (!RedfishIsPayloadCollection (Payload)) {
  793. return EFI_INVALID_PARAMETER;
  794. }
  795. *CollectionSize = (UINTN)getCollectionSize (Payload);
  796. return EFI_SUCCESS;
  797. }
  798. /**
  799. Get Redfish payload of collection member
  800. @param[in] Payload The Redfish collection payload
  801. @param[in] Index Index of collection member
  802. @return NULL Fail to get collection member.
  803. @return Non NULL Payload is returned.
  804. **/
  805. REDFISH_PAYLOAD
  806. RedfishGetPayloadByIndex (
  807. IN REDFISH_PAYLOAD Payload,
  808. IN UINTN Index
  809. )
  810. {
  811. REDFISH_RESPONSE RedfishResponse;
  812. REDFISH_PAYLOAD PayloadReturn;
  813. PayloadReturn = (VOID *)getPayloadByIndex (Payload, Index, &RedfishResponse.StatusCode);
  814. if ((PayloadReturn == NULL) ||
  815. ((*(RedfishResponse.StatusCode) < HTTP_STATUS_200_OK) && (*(RedfishResponse.StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT)))
  816. {
  817. return NULL;
  818. }
  819. return PayloadReturn;
  820. }
  821. /**
  822. Check and return Redfish resource of the given Redpath.
  823. @param[in] RedfishService Pointer to REDFISH_SERVICE
  824. @param[in] Redpath Redpath of the resource.
  825. @param[in] Response Optional return the resource.
  826. @return EFI_STATUS
  827. **/
  828. EFI_STATUS
  829. RedfishCheckIfRedpathExist (
  830. IN REDFISH_SERVICE RedfishService,
  831. IN CHAR8 *Redpath,
  832. IN REDFISH_RESPONSE *Response OPTIONAL
  833. )
  834. {
  835. EFI_STATUS Status;
  836. REDFISH_RESPONSE TempResponse;
  837. if (Redpath == NULL) {
  838. return EFI_INVALID_PARAMETER;
  839. }
  840. Status = RedfishGetByService (RedfishService, Redpath, &TempResponse);
  841. if (EFI_ERROR (Status)) {
  842. return Status;
  843. }
  844. if (Response == NULL) {
  845. RedfishFreeResponse (
  846. TempResponse.StatusCode,
  847. TempResponse.HeaderCount,
  848. TempResponse.Headers,
  849. TempResponse.Payload
  850. );
  851. } else {
  852. CopyMem ((VOID *)Response, (VOID *)&TempResponse, sizeof (REDFISH_RESPONSE));
  853. }
  854. return EFI_SUCCESS;
  855. }