1 | /** @file
|
---|
2 | Redfish debug library to debug Redfish application.
|
---|
3 |
|
---|
4 | Copyright (c) 2023-2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
---|
5 | Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.<BR>
|
---|
6 |
|
---|
7 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
8 |
|
---|
9 | **/
|
---|
10 |
|
---|
11 | #include <Uefi.h>
|
---|
12 |
|
---|
13 | #include <Library/BaseLib.h>
|
---|
14 | #include <Library/DebugLib.h>
|
---|
15 | #include <Library/MemoryAllocationLib.h>
|
---|
16 | #include <Library/RedfishDebugLib.h>
|
---|
17 | #include <Library/RedfishHttpLib.h>
|
---|
18 | #include <Library/UefiLib.h>
|
---|
19 |
|
---|
20 | #ifndef IS_EMPTY_STRING
|
---|
21 | #define IS_EMPTY_STRING(a) ((a) == NULL || (a)[0] == '\0')
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | #define REDFISH_JSON_STRING_LENGTH 200
|
---|
25 | #define REDFISH_JSON_OUTPUT_FORMAT (EDKII_JSON_COMPACT | EDKII_JSON_INDENT(2))
|
---|
26 | #define REDFISH_PRINT_BUFFER_BYTES_PER_ROW 16
|
---|
27 |
|
---|
28 | /**
|
---|
29 | Determine whether the Redfish debug category is enabled in
|
---|
30 | gEfiRedfishPkgTokenSpaceGuid.PcdRedfishDebugCategory.
|
---|
31 |
|
---|
32 | @param[in] RedfishDebugCategory Redfish debug category.
|
---|
33 |
|
---|
34 | @retval TRUE This debug category is enabled.
|
---|
35 | @retval FALSE This debug category is disabled..
|
---|
36 | **/
|
---|
37 | BOOLEAN
|
---|
38 | DebugRedfishComponentEnabled (
|
---|
39 | IN UINT64 RedfishDebugCategory
|
---|
40 | )
|
---|
41 | {
|
---|
42 | UINT64 DebugCategory;
|
---|
43 |
|
---|
44 | DebugCategory = FixedPcdGet64 (PcdRedfishDebugCategory);
|
---|
45 | return ((DebugCategory & RedfishDebugCategory) != 0);
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | Debug print the value of RedfishValue.
|
---|
50 |
|
---|
51 | @param[in] ErrorLevel DEBUG macro error level.
|
---|
52 | @param[in] RedfishValue The statement value to print.
|
---|
53 |
|
---|
54 | @retval EFI_SUCCESS RedfishValue is printed.
|
---|
55 | @retval EFI_INVALID_PARAMETER RedfishValue is NULL.
|
---|
56 | **/
|
---|
57 | EFI_STATUS
|
---|
58 | DumpRedfishValue (
|
---|
59 | IN UINTN ErrorLevel,
|
---|
60 | IN EDKII_REDFISH_VALUE *RedfishValue
|
---|
61 | )
|
---|
62 | {
|
---|
63 | UINTN Index;
|
---|
64 |
|
---|
65 | if (RedfishValue == NULL) {
|
---|
66 | return EFI_INVALID_PARAMETER;
|
---|
67 | }
|
---|
68 |
|
---|
69 | DEBUG ((ErrorLevel, "Type: 0x%x\n", RedfishValue->Type));
|
---|
70 | DEBUG ((ErrorLevel, "ArrayCount: 0x%x\n", RedfishValue->ArrayCount));
|
---|
71 |
|
---|
72 | switch (RedfishValue->Type) {
|
---|
73 | case RedfishValueTypeInteger:
|
---|
74 | DEBUG ((ErrorLevel, "Value: 0x%x\n", RedfishValue->Value.Integer));
|
---|
75 | break;
|
---|
76 | case RedfishValueTypeBoolean:
|
---|
77 | DEBUG ((ErrorLevel, "Value: %a\n", (RedfishValue->Value.Boolean ? "true" : "false")));
|
---|
78 | break;
|
---|
79 | case RedfishValueTypeString:
|
---|
80 | DEBUG ((ErrorLevel, "Value: %a\n", RedfishValue->Value.Buffer));
|
---|
81 | break;
|
---|
82 | case RedfishValueTypeStringArray:
|
---|
83 | for (Index = 0; Index < RedfishValue->ArrayCount; Index++) {
|
---|
84 | DEBUG ((ErrorLevel, "Value[%d]: %a\n", Index, RedfishValue->Value.StringArray[Index]));
|
---|
85 | }
|
---|
86 |
|
---|
87 | break;
|
---|
88 | case RedfishValueTypeIntegerArray:
|
---|
89 | for (Index = 0; Index < RedfishValue->ArrayCount; Index++) {
|
---|
90 | DEBUG ((ErrorLevel, "Value[%d]: 0x%x\n", Index, RedfishValue->Value.IntegerArray[Index]));
|
---|
91 | }
|
---|
92 |
|
---|
93 | break;
|
---|
94 | case RedfishValueTypeBooleanArray:
|
---|
95 | for (Index = 0; Index < RedfishValue->ArrayCount; Index++) {
|
---|
96 | DEBUG ((ErrorLevel, "Value[%d]: %a\n", Index, (RedfishValue->Value.BooleanArray[Index] ? "true" : "false")));
|
---|
97 | }
|
---|
98 |
|
---|
99 | break;
|
---|
100 | case RedfishValueTypeUnknown:
|
---|
101 | case RedfishValueTypeMax:
|
---|
102 | default:
|
---|
103 | break;
|
---|
104 | }
|
---|
105 |
|
---|
106 | return EFI_SUCCESS;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 |
|
---|
111 | This function dump the Json string in given error level.
|
---|
112 |
|
---|
113 | @param[in] ErrorLevel DEBUG macro error level
|
---|
114 | @param[in] JsonValue Json value to dump.
|
---|
115 |
|
---|
116 | @retval EFI_SUCCESS Json string is printed.
|
---|
117 | @retval Others Errors occur.
|
---|
118 |
|
---|
119 | **/
|
---|
120 | EFI_STATUS
|
---|
121 | DumpJsonValue (
|
---|
122 | IN UINTN ErrorLevel,
|
---|
123 | IN EDKII_JSON_VALUE JsonValue
|
---|
124 | )
|
---|
125 | {
|
---|
126 | CHAR8 *String;
|
---|
127 | CHAR8 *Runner;
|
---|
128 | CHAR8 Buffer[REDFISH_JSON_STRING_LENGTH + 1];
|
---|
129 | UINTN StrLen;
|
---|
130 | UINTN Count;
|
---|
131 | UINTN Index;
|
---|
132 |
|
---|
133 | if (JsonValue == NULL) {
|
---|
134 | return EFI_INVALID_PARAMETER;
|
---|
135 | }
|
---|
136 |
|
---|
137 | String = JsonDumpString (JsonValue, REDFISH_JSON_OUTPUT_FORMAT);
|
---|
138 | if (String == NULL) {
|
---|
139 | return EFI_UNSUPPORTED;
|
---|
140 | }
|
---|
141 |
|
---|
142 | StrLen = AsciiStrLen (String);
|
---|
143 | if (StrLen == 0) {
|
---|
144 | return EFI_UNSUPPORTED;
|
---|
145 | }
|
---|
146 |
|
---|
147 | Count = StrLen / REDFISH_JSON_STRING_LENGTH;
|
---|
148 | Runner = String;
|
---|
149 | for (Index = 0; Index < Count; Index++) {
|
---|
150 | AsciiStrnCpyS (Buffer, (REDFISH_JSON_STRING_LENGTH + 1), Runner, REDFISH_JSON_STRING_LENGTH);
|
---|
151 | Buffer[REDFISH_JSON_STRING_LENGTH] = '\0';
|
---|
152 | DEBUG ((ErrorLevel, "%a", Buffer));
|
---|
153 | Runner += REDFISH_JSON_STRING_LENGTH;
|
---|
154 | }
|
---|
155 |
|
---|
156 | Count = StrLen % REDFISH_JSON_STRING_LENGTH;
|
---|
157 | if (Count > 0) {
|
---|
158 | DEBUG ((ErrorLevel, "%a", Runner));
|
---|
159 | }
|
---|
160 |
|
---|
161 | DEBUG ((ErrorLevel, "\n"));
|
---|
162 |
|
---|
163 | FreePool (String);
|
---|
164 | return EFI_SUCCESS;
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 |
|
---|
169 | This function dump the status code, header and body in given
|
---|
170 | Redfish payload.
|
---|
171 |
|
---|
172 | @param[in] ErrorLevel DEBUG macro error level
|
---|
173 | @param[in] Payload Redfish payload to dump
|
---|
174 |
|
---|
175 | @retval EFI_SUCCESS Redfish payload is printed.
|
---|
176 | @retval Others Errors occur.
|
---|
177 |
|
---|
178 | **/
|
---|
179 | EFI_STATUS
|
---|
180 | DumpRedfishPayload (
|
---|
181 | IN UINTN ErrorLevel,
|
---|
182 | IN REDFISH_PAYLOAD Payload
|
---|
183 | )
|
---|
184 | {
|
---|
185 | EDKII_JSON_VALUE JsonValue;
|
---|
186 |
|
---|
187 | if (Payload == NULL) {
|
---|
188 | return EFI_INVALID_PARAMETER;
|
---|
189 | }
|
---|
190 |
|
---|
191 | JsonValue = RedfishJsonInPayload (Payload);
|
---|
192 | if (JsonValue != NULL) {
|
---|
193 | DEBUG ((ErrorLevel, "Payload:\n"));
|
---|
194 | DumpJsonValue (ErrorLevel, JsonValue);
|
---|
195 | }
|
---|
196 |
|
---|
197 | return EFI_SUCCESS;
|
---|
198 | }
|
---|
199 |
|
---|
200 | /**
|
---|
201 |
|
---|
202 | This function dump the HTTP status code.
|
---|
203 |
|
---|
204 | @param[in] ErrorLevel DEBUG macro error level
|
---|
205 | @param[in] HttpStatusCode HTTP status code
|
---|
206 |
|
---|
207 | @retval EFI_SUCCESS HTTP status code is printed
|
---|
208 |
|
---|
209 | **/
|
---|
210 | EFI_STATUS
|
---|
211 | DumpHttpStatusCode (
|
---|
212 | IN UINTN ErrorLevel,
|
---|
213 | IN EFI_HTTP_STATUS_CODE HttpStatusCode
|
---|
214 | )
|
---|
215 | {
|
---|
216 | switch (HttpStatusCode) {
|
---|
217 | case HTTP_STATUS_100_CONTINUE:
|
---|
218 | DEBUG ((ErrorLevel, "Status code: 100 CONTINUE\n"));
|
---|
219 | break;
|
---|
220 | case HTTP_STATUS_200_OK:
|
---|
221 | DEBUG ((ErrorLevel, "Status code: 200 OK\n"));
|
---|
222 | break;
|
---|
223 | case HTTP_STATUS_201_CREATED:
|
---|
224 | DEBUG ((ErrorLevel, "Status code: 201 CREATED\n"));
|
---|
225 | break;
|
---|
226 | case HTTP_STATUS_202_ACCEPTED:
|
---|
227 | DEBUG ((ErrorLevel, "Status code: 202 ACCEPTED\n"));
|
---|
228 | break;
|
---|
229 | case HTTP_STATUS_304_NOT_MODIFIED:
|
---|
230 | DEBUG ((ErrorLevel, "Status code: 304 NOT MODIFIED\n"));
|
---|
231 | break;
|
---|
232 | case HTTP_STATUS_400_BAD_REQUEST:
|
---|
233 | DEBUG ((ErrorLevel, "Status code: 400 BAD REQUEST\n"));
|
---|
234 | break;
|
---|
235 | case HTTP_STATUS_401_UNAUTHORIZED:
|
---|
236 | DEBUG ((ErrorLevel, "Status code: 401 UNAUTHORIZED\n"));
|
---|
237 | break;
|
---|
238 | case HTTP_STATUS_403_FORBIDDEN:
|
---|
239 | DEBUG ((ErrorLevel, "Status code: 403 FORBIDDEN\n"));
|
---|
240 | break;
|
---|
241 | case HTTP_STATUS_404_NOT_FOUND:
|
---|
242 | DEBUG ((ErrorLevel, "Status code: 404 NOT FOUND\n"));
|
---|
243 | break;
|
---|
244 | case HTTP_STATUS_405_METHOD_NOT_ALLOWED:
|
---|
245 | DEBUG ((ErrorLevel, "Status code: 405 METHOD NOT ALLOWED\n"));
|
---|
246 | break;
|
---|
247 | case HTTP_STATUS_500_INTERNAL_SERVER_ERROR:
|
---|
248 | DEBUG ((ErrorLevel, "Status code: 500 INTERNAL SERVER ERROR\n"));
|
---|
249 | break;
|
---|
250 | default:
|
---|
251 | DEBUG ((ErrorLevel, "Status code: 0x%x\n", HttpStatusCode));
|
---|
252 | break;
|
---|
253 | }
|
---|
254 |
|
---|
255 | return EFI_SUCCESS;
|
---|
256 | }
|
---|
257 |
|
---|
258 | /**
|
---|
259 |
|
---|
260 | This function dump the status code, header and body in given
|
---|
261 | Redfish response.
|
---|
262 |
|
---|
263 | @param[in] Message Message string
|
---|
264 | @param[in] ErrorLevel DEBUG macro error level
|
---|
265 | @param[in] Response Redfish response to dump
|
---|
266 |
|
---|
267 | @retval EFI_SUCCESS Redfish response is printed.
|
---|
268 | @retval Others Errors occur.
|
---|
269 |
|
---|
270 | **/
|
---|
271 | EFI_STATUS
|
---|
272 | DumpRedfishResponse (
|
---|
273 | IN CONST CHAR8 *Message,
|
---|
274 | IN UINTN ErrorLevel,
|
---|
275 | IN REDFISH_RESPONSE *Response
|
---|
276 | )
|
---|
277 | {
|
---|
278 | UINTN Index;
|
---|
279 |
|
---|
280 | if (Response == NULL) {
|
---|
281 | return EFI_INVALID_PARAMETER;
|
---|
282 | }
|
---|
283 |
|
---|
284 | if (!IS_EMPTY_STRING (Message)) {
|
---|
285 | DEBUG ((ErrorLevel, "%a\n", Message));
|
---|
286 | }
|
---|
287 |
|
---|
288 | //
|
---|
289 | // status code
|
---|
290 | //
|
---|
291 | if (Response->StatusCode != NULL) {
|
---|
292 | DumpHttpStatusCode (ErrorLevel, *(Response->StatusCode));
|
---|
293 | }
|
---|
294 |
|
---|
295 | //
|
---|
296 | // header
|
---|
297 | //
|
---|
298 | if (Response->HeaderCount > 0) {
|
---|
299 | DEBUG ((ErrorLevel, "Header: %d\n", Response->HeaderCount));
|
---|
300 | for (Index = 0; Index < Response->HeaderCount; Index++) {
|
---|
301 | DEBUG ((ErrorLevel, " %a: %a\n", Response->Headers[Index].FieldName, Response->Headers[Index].FieldValue));
|
---|
302 | }
|
---|
303 | }
|
---|
304 |
|
---|
305 | //
|
---|
306 | // Body
|
---|
307 | //
|
---|
308 | if (Response->Payload != NULL) {
|
---|
309 | DumpRedfishPayload (ErrorLevel, Response->Payload);
|
---|
310 | }
|
---|
311 |
|
---|
312 | return EFI_SUCCESS;
|
---|
313 | }
|
---|
314 |
|
---|
315 | /**
|
---|
316 |
|
---|
317 | This function dump the IPv4 address in given error level.
|
---|
318 |
|
---|
319 | @param[in] ErrorLevel DEBUG macro error level
|
---|
320 | @param[in] Ipv4Address IPv4 address to dump
|
---|
321 |
|
---|
322 | @retval EFI_SUCCESS IPv4 address string is printed.
|
---|
323 | @retval Others Errors occur.
|
---|
324 |
|
---|
325 | **/
|
---|
326 | EFI_STATUS
|
---|
327 | DumpIpv4Address (
|
---|
328 | IN UINTN ErrorLevel,
|
---|
329 | IN EFI_IPv4_ADDRESS *Ipv4Address
|
---|
330 | )
|
---|
331 | {
|
---|
332 | if (Ipv4Address == NULL) {
|
---|
333 | return EFI_INVALID_PARAMETER;
|
---|
334 | }
|
---|
335 |
|
---|
336 | DEBUG ((ErrorLevel, "%d.%d.%d.%d\n", Ipv4Address->Addr[0], Ipv4Address->Addr[1], Ipv4Address->Addr[2], Ipv4Address->Addr[3]));
|
---|
337 |
|
---|
338 | return EFI_SUCCESS;
|
---|
339 | }
|
---|
340 |
|
---|
341 | /**
|
---|
342 | Debug output raw data buffer.
|
---|
343 |
|
---|
344 | @param[in] ErrorLevel DEBUG macro error level
|
---|
345 | @param[in] Buffer Debug output data buffer.
|
---|
346 | @param[in] BufferSize The size of Buffer in byte.
|
---|
347 |
|
---|
348 | @retval EFI_SUCCESS Debug dump finished.
|
---|
349 | @retval EFI_INVALID_PARAMETER Buffer is NULL.
|
---|
350 |
|
---|
351 | **/
|
---|
352 | EFI_STATUS
|
---|
353 | DumpBuffer (
|
---|
354 | IN UINTN ErrorLevel,
|
---|
355 | IN UINT8 *Buffer,
|
---|
356 | IN UINTN BufferSize
|
---|
357 | )
|
---|
358 | {
|
---|
359 | UINTN Index;
|
---|
360 |
|
---|
361 | if (Buffer == NULL) {
|
---|
362 | return EFI_INVALID_PARAMETER;
|
---|
363 | }
|
---|
364 |
|
---|
365 | DEBUG ((ErrorLevel, "Address: 0x%p size: %d\n", Buffer, BufferSize));
|
---|
366 | for (Index = 0; Index < BufferSize; Index++) {
|
---|
367 | if (Index % REDFISH_PRINT_BUFFER_BYTES_PER_ROW == 0) {
|
---|
368 | DEBUG ((ErrorLevel, "\n%04X: ", Index));
|
---|
369 | }
|
---|
370 |
|
---|
371 | DEBUG ((ErrorLevel, "%02X ", Buffer[Index]));
|
---|
372 | }
|
---|
373 |
|
---|
374 | DEBUG ((ErrorLevel, "\n"));
|
---|
375 |
|
---|
376 | return EFI_SUCCESS;
|
---|
377 | }
|
---|