blob: e599b68b26eaa48e38a1cdaf28fba387b91a1f90 [file] [log] [blame]
Lawrence Tangf0f95572022-07-07 16:56:22 +01001/**
2 * Describes functions for parsing JSON IR CPER data into binary CPER format.
Ed Tanousfedd4572024-07-12 13:56:00 -07003 *
Lawrence Tangf0f95572022-07-07 16:56:22 +01004 * Author: Lawrence.Tang@arm.com
5 **/
6
7#include <stdio.h>
Lawrence Tang0cb33792022-07-13 13:51:39 +01008#include <string.h>
Lawrence Tang5202bbb2022-08-12 14:54:36 +01009#include <json.h>
Thu Nguyene42fb482024-10-15 14:43:11 +000010#include <libcper/base64.h>
11#include <libcper/Cper.h>
12#include <libcper/cper-parse.h>
13#include <libcper/cper-utils.h>
14#include <libcper/sections/cper-section.h>
Lawrence Tangb44314c2022-07-13 11:45:22 +010015
16//Private pre-declarations.
Lawrence Tange407b4c2022-07-21 13:54:01 +010017void ir_header_to_cper(json_object *header_ir,
18 EFI_COMMON_ERROR_RECORD_HEADER *header);
19void ir_section_descriptor_to_cper(json_object *section_descriptor_ir,
20 EFI_ERROR_SECTION_DESCRIPTOR *descriptor);
Lawrence Tang617949e2022-08-08 14:21:42 +010021void ir_section_to_cper(json_object *section,
22 EFI_ERROR_SECTION_DESCRIPTOR *descriptor, FILE *out);
Lawrence Tangf0f95572022-07-07 16:56:22 +010023
24//Converts the given JSON IR CPER representation into CPER binary format, piped to the provided file stream.
Lawrence Tang0cb33792022-07-13 13:51:39 +010025//This function performs no validation of the IR against the CPER-JSON specification. To ensure a safe call,
Lawrence Tange407b4c2022-07-21 13:54:01 +010026//use validate_schema() from json-schema.h before attempting to call this function.
27void ir_to_cper(json_object *ir, FILE *out)
Lawrence Tangf0f95572022-07-07 16:56:22 +010028{
Lawrence Tange407b4c2022-07-21 13:54:01 +010029 //Create the CPER header.
30 EFI_COMMON_ERROR_RECORD_HEADER *header =
31 (EFI_COMMON_ERROR_RECORD_HEADER *)calloc(
32 1, sizeof(EFI_COMMON_ERROR_RECORD_HEADER));
33 ir_header_to_cper(json_object_object_get(ir, "header"), header);
34 fwrite(header, sizeof(EFI_COMMON_ERROR_RECORD_HEADER), 1, out);
35 fflush(out);
Lawrence Tang0cb33792022-07-13 13:51:39 +010036
Lawrence Tange407b4c2022-07-21 13:54:01 +010037 //Create the CPER section descriptors.
38 json_object *section_descriptors =
39 json_object_object_get(ir, "sectionDescriptors");
Ed Tanous8121f7e2025-03-06 14:39:07 -080040 if (section_descriptors == NULL) {
41 printf("Invalid CPER file: No section descriptors.\n");
42 return;
43 }
Lawrence Tange407b4c2022-07-21 13:54:01 +010044 int amt_descriptors = json_object_array_length(section_descriptors);
45 EFI_ERROR_SECTION_DESCRIPTOR *descriptors[amt_descriptors];
46 for (int i = 0; i < amt_descriptors; i++) {
47 descriptors[i] = (EFI_ERROR_SECTION_DESCRIPTOR *)calloc(
48 1, sizeof(EFI_ERROR_SECTION_DESCRIPTOR));
49 ir_section_descriptor_to_cper(
50 json_object_array_get_idx(section_descriptors, i),
51 descriptors[i]);
52 fwrite(descriptors[i], sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1,
53 out);
54 fflush(out);
55 }
Lawrence Tang0cb33792022-07-13 13:51:39 +010056
Lawrence Tange407b4c2022-07-21 13:54:01 +010057 //Run through each section in turn.
58 json_object *sections = json_object_object_get(ir, "sections");
Ed Tanous8121f7e2025-03-06 14:39:07 -080059 if (sections == NULL) {
60 printf("Invalid CPER file: No sections.\n");
61 return;
62 }
Lawrence Tange407b4c2022-07-21 13:54:01 +010063 int amt_sections = json_object_array_length(sections);
John Chungf8fc7052024-05-03 20:05:29 +080064 if (amt_sections == amt_descriptors) {
65 for (int i = 0; i < amt_sections; i++) {
66 //Get the section itself from the IR.
67 json_object *section =
68 json_object_array_get_idx(sections, i);
Lawrence Tang0cb33792022-07-13 13:51:39 +010069
John Chungf8fc7052024-05-03 20:05:29 +080070 //Convert.
71 ir_section_to_cper(section, descriptors[i], out);
72 }
Lawrence Tange407b4c2022-07-21 13:54:01 +010073 }
74
75 //Free all remaining resources.
76 free(header);
John Chungf8fc7052024-05-03 20:05:29 +080077 for (int i = 0; i < amt_descriptors; i++) {
Lawrence Tange407b4c2022-07-21 13:54:01 +010078 free(descriptors[i]);
John Chungf8fc7052024-05-03 20:05:29 +080079 }
Lawrence Tangb44314c2022-07-13 11:45:22 +010080}
81
82//Converts a CPER-JSON IR header to a CPER header structure.
Lawrence Tange407b4c2022-07-21 13:54:01 +010083void ir_header_to_cper(json_object *header_ir,
84 EFI_COMMON_ERROR_RECORD_HEADER *header)
Lawrence Tangb44314c2022-07-13 11:45:22 +010085{
Lawrence Tange407b4c2022-07-21 13:54:01 +010086 header->SignatureStart = 0x52455043; //CPER
Lawrence Tangb44314c2022-07-13 11:45:22 +010087
Lawrence Tange407b4c2022-07-21 13:54:01 +010088 //Revision.
89 json_object *revision = json_object_object_get(header_ir, "revision");
90 int minor =
91 json_object_get_int(json_object_object_get(revision, "minor"));
92 int major =
93 json_object_get_int(json_object_object_get(revision, "major"));
94 header->Revision = minor + (major << 8);
Lawrence Tangb44314c2022-07-13 11:45:22 +010095
Lawrence Tange407b4c2022-07-21 13:54:01 +010096 header->SignatureEnd = 0xFFFFFFFF;
Lawrence Tangb44314c2022-07-13 11:45:22 +010097
Lawrence Tange407b4c2022-07-21 13:54:01 +010098 //Section count.
99 int section_count = json_object_get_int(
100 json_object_object_get(header_ir, "sectionCount"));
101 header->SectionCount = (UINT16)section_count;
Lawrence Tangb44314c2022-07-13 11:45:22 +0100102
Lawrence Tange407b4c2022-07-21 13:54:01 +0100103 //Error severity.
104 json_object *severity = json_object_object_get(header_ir, "severity");
105 header->ErrorSeverity = (UINT32)json_object_get_uint64(
106 json_object_object_get(severity, "code"));
Lawrence Tangb44314c2022-07-13 11:45:22 +0100107
Lawrence Tange407b4c2022-07-21 13:54:01 +0100108 //Validation bits.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800109 ValidationTypes ui32Type = { UINT_32T, .value.ui32 = 0 };
110 struct json_object *obj = NULL;
Lawrence Tangb44314c2022-07-13 11:45:22 +0100111
Lawrence Tange407b4c2022-07-21 13:54:01 +0100112 //Record length.
113 header->RecordLength = (UINT32)json_object_get_uint64(
114 json_object_object_get(header_ir, "recordLength"));
Lawrence Tangb44314c2022-07-13 11:45:22 +0100115
Lawrence Tange407b4c2022-07-21 13:54:01 +0100116 //Timestamp, if present.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800117 if (json_object_object_get_ex(header_ir, "timestamp", &obj)) {
118 json_object *timestamp = obj;
119 if (timestamp != NULL) {
120 string_to_timestamp(&header->TimeStamp,
121 json_object_get_string(timestamp));
122 header->TimeStamp.Flag = json_object_get_boolean(
123 json_object_object_get(header_ir,
124 "timestampIsPrecise"));
125 }
126 add_to_valid_bitfield(&ui32Type, 1);
Lawrence Tange407b4c2022-07-21 13:54:01 +0100127 }
Lawrence Tangb44314c2022-07-13 11:45:22 +0100128
Lawrence Tange407b4c2022-07-21 13:54:01 +0100129 //Various GUIDs.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800130 json_object *platform_id;
131 json_object_object_get_ex(header_ir, "platformID", &platform_id);
132 json_object *partition_id;
133 json_object_object_get_ex(header_ir, "partitionID", &partition_id);
John Chungf8fc7052024-05-03 20:05:29 +0800134 if (platform_id != NULL) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100135 string_to_guid(&header->PlatformID,
136 json_object_get_string(platform_id));
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800137 add_to_valid_bitfield(&ui32Type, 0);
John Chungf8fc7052024-05-03 20:05:29 +0800138 }
139 if (partition_id != NULL) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100140 string_to_guid(&header->PartitionID,
141 json_object_get_string(partition_id));
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800142 add_to_valid_bitfield(&ui32Type, 2);
John Chungf8fc7052024-05-03 20:05:29 +0800143 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100144 string_to_guid(&header->CreatorID,
145 json_object_get_string(
146 json_object_object_get(header_ir, "creatorID")));
Lawrence Tangb44314c2022-07-13 11:45:22 +0100147
Lawrence Tange407b4c2022-07-21 13:54:01 +0100148 //Notification type.
149 json_object *notification_type =
150 json_object_object_get(header_ir, "notificationType");
151 string_to_guid(&header->NotificationType,
152 json_object_get_string(json_object_object_get(
153 notification_type, "guid")));
Lawrence Tangb44314c2022-07-13 11:45:22 +0100154
Lawrence Tange407b4c2022-07-21 13:54:01 +0100155 //Record ID, persistence info.
156 header->RecordID = json_object_get_uint64(
157 json_object_object_get(header_ir, "recordID"));
158 header->PersistenceInfo = json_object_get_uint64(
159 json_object_object_get(header_ir, "persistenceInfo"));
Lawrence Tangb44314c2022-07-13 11:45:22 +0100160
Lawrence Tange407b4c2022-07-21 13:54:01 +0100161 //Flags.
162 json_object *flags = json_object_object_get(header_ir, "flags");
163 header->Flags = (UINT32)json_object_get_uint64(
164 json_object_object_get(flags, "value"));
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800165
166 header->ValidationBits = ui32Type.value.ui32;
Lawrence Tang0cb33792022-07-13 13:51:39 +0100167}
168
Lawrence Tang617949e2022-08-08 14:21:42 +0100169//Converts a single given IR section into CPER, outputting to the given stream.
170void ir_section_to_cper(json_object *section,
171 EFI_ERROR_SECTION_DESCRIPTOR *descriptor, FILE *out)
172{
Ed Tanousb07061a2024-09-22 10:33:29 -0700173 json_object *ir = NULL;
174
Lawrence Tang617949e2022-08-08 14:21:42 +0100175 //Find the correct section type, and parse.
Lawrence Tang580423f2022-08-24 09:37:53 +0100176 int section_converted = 0;
Ed Tanous1a648562025-03-10 15:23:38 -0700177 CPER_SECTION_DEFINITION *definition =
178 select_section_by_guid(&descriptor->SectionType);
179 if (definition != NULL) {
180 ir = json_object_object_get(section, definition->ShortName);
181 definition->ToCPER(ir, out);
182 section_converted = 1;
Lawrence Tang580423f2022-08-24 09:37:53 +0100183 }
184
185 //If unknown GUID, so read as a base64 unknown section.
186 if (!section_converted) {
Ed Tanousb07061a2024-09-22 10:33:29 -0700187 ir = json_object_object_get(section, "Unknown");
188 json_object *encoded = json_object_object_get(ir, "data");
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700189
190 int32_t decoded_len = 0;
191
192 UINT8 *decoded = base64_decode(
193 json_object_get_string(encoded),
194 json_object_get_string_len(encoded), &decoded_len);
195 if (decoded == NULL) {
John Chungf8fc7052024-05-03 20:05:29 +0800196 printf("Failed to allocate decode output buffer. \n");
197 } else {
John Chungf8fc7052024-05-03 20:05:29 +0800198 fwrite(decoded, decoded_len, 1, out);
John Chungf8fc7052024-05-03 20:05:29 +0800199 free(decoded);
200 }
Lawrence Tang617949e2022-08-08 14:21:42 +0100201 }
202}
203
Lawrence Tang0cb33792022-07-13 13:51:39 +0100204//Converts a single CPER-JSON IR section descriptor into a CPER structure.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100205void ir_section_descriptor_to_cper(json_object *section_descriptor_ir,
206 EFI_ERROR_SECTION_DESCRIPTOR *descriptor)
Lawrence Tang0cb33792022-07-13 13:51:39 +0100207{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100208 //Section offset, length.
209 descriptor->SectionOffset = (UINT32)json_object_get_uint64(
210 json_object_object_get(section_descriptor_ir, "sectionOffset"));
211 descriptor->SectionLength = (UINT32)json_object_get_uint64(
212 json_object_object_get(section_descriptor_ir, "sectionLength"));
Lawrence Tang0cb33792022-07-13 13:51:39 +0100213
Lawrence Tange407b4c2022-07-21 13:54:01 +0100214 //Revision.
215 json_object *revision =
216 json_object_object_get(section_descriptor_ir, "revision");
217 int minor =
218 json_object_get_int(json_object_object_get(revision, "minor"));
219 int major =
220 json_object_get_int(json_object_object_get(revision, "major"));
221 descriptor->Revision = minor + (major << 8);
Lawrence Tang0cb33792022-07-13 13:51:39 +0100222
Lawrence Tange407b4c2022-07-21 13:54:01 +0100223 //Validation bits, flags.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800224 ValidationTypes ui8Type = { UINT_8T, .value.ui8 = 0 };
225 struct json_object *obj = NULL;
226
Lawrence Tange407b4c2022-07-21 13:54:01 +0100227 descriptor->SectionFlags = ir_to_bitfield(
228 json_object_object_get(section_descriptor_ir, "flags"), 8,
229 CPER_SECTION_DESCRIPTOR_FLAGS_BITFIELD_NAMES);
Lawrence Tang0cb33792022-07-13 13:51:39 +0100230
Lawrence Tange407b4c2022-07-21 13:54:01 +0100231 //Section type.
232 json_object *section_type =
233 json_object_object_get(section_descriptor_ir, "sectionType");
234 string_to_guid(&descriptor->SectionType,
235 json_object_get_string(
236 json_object_object_get(section_type, "data")));
Lawrence Tang0cb33792022-07-13 13:51:39 +0100237
Lawrence Tange407b4c2022-07-21 13:54:01 +0100238 //FRU ID, if present.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800239 if (json_object_object_get_ex(section_descriptor_ir, "fruID", &obj)) {
240 json_object *fru_id = obj;
241 if (fru_id != NULL) {
242 string_to_guid(&descriptor->FruId,
243 json_object_get_string(fru_id));
244 add_to_valid_bitfield(&ui8Type, 0);
245 }
John Chungf8fc7052024-05-03 20:05:29 +0800246 }
Lawrence Tang0cb33792022-07-13 13:51:39 +0100247
Lawrence Tange407b4c2022-07-21 13:54:01 +0100248 //Severity code.
249 json_object *severity =
250 json_object_object_get(section_descriptor_ir, "severity");
251 descriptor->Severity = (UINT32)json_object_get_uint64(
252 json_object_object_get(severity, "code"));
Lawrence Tang0cb33792022-07-13 13:51:39 +0100253
Lawrence Tange407b4c2022-07-21 13:54:01 +0100254 //FRU text, if present.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800255 if (json_object_object_get_ex(section_descriptor_ir, "fruText", &obj)) {
256 json_object *fru_text = obj;
257 if (fru_text != NULL) {
258 strncpy(descriptor->FruString,
259 json_object_get_string(fru_text),
260 sizeof(descriptor->FruString) - 1);
261 descriptor
262 ->FruString[sizeof(descriptor->FruString) - 1] =
263 '\0';
264 add_to_valid_bitfield(&ui8Type, 1);
265 }
John Chungf8fc7052024-05-03 20:05:29 +0800266 }
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800267 descriptor->SecValidMask = ui8Type.value.ui8;
Lawrence Tang617949e2022-08-08 14:21:42 +0100268}
269
270//Converts IR for a given single section format CPER record into CPER binary.
271void ir_single_section_to_cper(json_object *ir, FILE *out)
272{
273 //Create & write a section descriptor to file.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800274 EFI_ERROR_SECTION_DESCRIPTOR section_descriptor;
275 memset(&section_descriptor, 0, sizeof(section_descriptor));
276
Lawrence Tang617949e2022-08-08 14:21:42 +0100277 ir_section_descriptor_to_cper(
278 json_object_object_get(ir, "sectionDescriptor"),
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800279 &section_descriptor);
280 fwrite(&section_descriptor, sizeof(section_descriptor), 1, out);
Lawrence Tang617949e2022-08-08 14:21:42 +0100281
282 //Write section to file.
283 ir_section_to_cper(json_object_object_get(ir, "section"),
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800284 &section_descriptor, out);
Lawrence Tang617949e2022-08-08 14:21:42 +0100285
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800286 fflush(out);
John Chungf8fc7052024-05-03 20:05:29 +0800287}