VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/Firmware/RedfishPkg/Library/RedfishDebugLib/RedfishDebugLib.c

Last change on this file was 108794, checked in by vboxsync, 7 weeks ago

Devices/EFI/FirmwareNew: Merge edk2-stable202502 from the vendor branch and make it build for the important platforms, bugref:4643

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette