blob: 2cb8de0fa62dcc1134c4f43a317cd043f0696c97 [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");
40 int amt_descriptors = json_object_array_length(section_descriptors);
41 EFI_ERROR_SECTION_DESCRIPTOR *descriptors[amt_descriptors];
42 for (int i = 0; i < amt_descriptors; i++) {
43 descriptors[i] = (EFI_ERROR_SECTION_DESCRIPTOR *)calloc(
44 1, sizeof(EFI_ERROR_SECTION_DESCRIPTOR));
45 ir_section_descriptor_to_cper(
46 json_object_array_get_idx(section_descriptors, i),
47 descriptors[i]);
48 fwrite(descriptors[i], sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1,
49 out);
50 fflush(out);
51 }
Lawrence Tang0cb33792022-07-13 13:51:39 +010052
Lawrence Tange407b4c2022-07-21 13:54:01 +010053 //Run through each section in turn.
54 json_object *sections = json_object_object_get(ir, "sections");
55 int amt_sections = json_object_array_length(sections);
John Chungf8fc7052024-05-03 20:05:29 +080056 if (amt_sections == amt_descriptors) {
57 for (int i = 0; i < amt_sections; i++) {
58 //Get the section itself from the IR.
59 json_object *section =
60 json_object_array_get_idx(sections, i);
Lawrence Tang0cb33792022-07-13 13:51:39 +010061
John Chungf8fc7052024-05-03 20:05:29 +080062 //Convert.
63 ir_section_to_cper(section, descriptors[i], out);
64 }
Lawrence Tange407b4c2022-07-21 13:54:01 +010065 }
66
67 //Free all remaining resources.
68 free(header);
John Chungf8fc7052024-05-03 20:05:29 +080069 for (int i = 0; i < amt_descriptors; i++) {
Lawrence Tange407b4c2022-07-21 13:54:01 +010070 free(descriptors[i]);
John Chungf8fc7052024-05-03 20:05:29 +080071 }
Lawrence Tangb44314c2022-07-13 11:45:22 +010072}
73
74//Converts a CPER-JSON IR header to a CPER header structure.
Lawrence Tange407b4c2022-07-21 13:54:01 +010075void ir_header_to_cper(json_object *header_ir,
76 EFI_COMMON_ERROR_RECORD_HEADER *header)
Lawrence Tangb44314c2022-07-13 11:45:22 +010077{
Lawrence Tange407b4c2022-07-21 13:54:01 +010078 header->SignatureStart = 0x52455043; //CPER
Lawrence Tangb44314c2022-07-13 11:45:22 +010079
Lawrence Tange407b4c2022-07-21 13:54:01 +010080 //Revision.
81 json_object *revision = json_object_object_get(header_ir, "revision");
82 int minor =
83 json_object_get_int(json_object_object_get(revision, "minor"));
84 int major =
85 json_object_get_int(json_object_object_get(revision, "major"));
86 header->Revision = minor + (major << 8);
Lawrence Tangb44314c2022-07-13 11:45:22 +010087
Lawrence Tange407b4c2022-07-21 13:54:01 +010088 header->SignatureEnd = 0xFFFFFFFF;
Lawrence Tangb44314c2022-07-13 11:45:22 +010089
Lawrence Tange407b4c2022-07-21 13:54:01 +010090 //Section count.
91 int section_count = json_object_get_int(
92 json_object_object_get(header_ir, "sectionCount"));
93 header->SectionCount = (UINT16)section_count;
Lawrence Tangb44314c2022-07-13 11:45:22 +010094
Lawrence Tange407b4c2022-07-21 13:54:01 +010095 //Error severity.
96 json_object *severity = json_object_object_get(header_ir, "severity");
97 header->ErrorSeverity = (UINT32)json_object_get_uint64(
98 json_object_object_get(severity, "code"));
Lawrence Tangb44314c2022-07-13 11:45:22 +010099
Lawrence Tange407b4c2022-07-21 13:54:01 +0100100 //Validation bits.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800101 ValidationTypes ui32Type = { UINT_32T, .value.ui32 = 0 };
102 struct json_object *obj = NULL;
Lawrence Tangb44314c2022-07-13 11:45:22 +0100103
Lawrence Tange407b4c2022-07-21 13:54:01 +0100104 //Record length.
105 header->RecordLength = (UINT32)json_object_get_uint64(
106 json_object_object_get(header_ir, "recordLength"));
Lawrence Tangb44314c2022-07-13 11:45:22 +0100107
Lawrence Tange407b4c2022-07-21 13:54:01 +0100108 //Timestamp, if present.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800109 if (json_object_object_get_ex(header_ir, "timestamp", &obj)) {
110 json_object *timestamp = obj;
111 if (timestamp != NULL) {
112 string_to_timestamp(&header->TimeStamp,
113 json_object_get_string(timestamp));
114 header->TimeStamp.Flag = json_object_get_boolean(
115 json_object_object_get(header_ir,
116 "timestampIsPrecise"));
117 }
118 add_to_valid_bitfield(&ui32Type, 1);
Lawrence Tange407b4c2022-07-21 13:54:01 +0100119 }
Lawrence Tangb44314c2022-07-13 11:45:22 +0100120
Lawrence Tange407b4c2022-07-21 13:54:01 +0100121 //Various GUIDs.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800122 json_object *platform_id;
123 json_object_object_get_ex(header_ir, "platformID", &platform_id);
124 json_object *partition_id;
125 json_object_object_get_ex(header_ir, "partitionID", &partition_id);
John Chungf8fc7052024-05-03 20:05:29 +0800126 if (platform_id != NULL) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100127 string_to_guid(&header->PlatformID,
128 json_object_get_string(platform_id));
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800129 add_to_valid_bitfield(&ui32Type, 0);
John Chungf8fc7052024-05-03 20:05:29 +0800130 }
131 if (partition_id != NULL) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100132 string_to_guid(&header->PartitionID,
133 json_object_get_string(partition_id));
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800134 add_to_valid_bitfield(&ui32Type, 2);
John Chungf8fc7052024-05-03 20:05:29 +0800135 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100136 string_to_guid(&header->CreatorID,
137 json_object_get_string(
138 json_object_object_get(header_ir, "creatorID")));
Lawrence Tangb44314c2022-07-13 11:45:22 +0100139
Lawrence Tange407b4c2022-07-21 13:54:01 +0100140 //Notification type.
141 json_object *notification_type =
142 json_object_object_get(header_ir, "notificationType");
143 string_to_guid(&header->NotificationType,
144 json_object_get_string(json_object_object_get(
145 notification_type, "guid")));
Lawrence Tangb44314c2022-07-13 11:45:22 +0100146
Lawrence Tange407b4c2022-07-21 13:54:01 +0100147 //Record ID, persistence info.
148 header->RecordID = json_object_get_uint64(
149 json_object_object_get(header_ir, "recordID"));
150 header->PersistenceInfo = json_object_get_uint64(
151 json_object_object_get(header_ir, "persistenceInfo"));
Lawrence Tangb44314c2022-07-13 11:45:22 +0100152
Lawrence Tange407b4c2022-07-21 13:54:01 +0100153 //Flags.
154 json_object *flags = json_object_object_get(header_ir, "flags");
155 header->Flags = (UINT32)json_object_get_uint64(
156 json_object_object_get(flags, "value"));
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800157
158 header->ValidationBits = ui32Type.value.ui32;
Lawrence Tang0cb33792022-07-13 13:51:39 +0100159}
160
Lawrence Tang617949e2022-08-08 14:21:42 +0100161//Converts a single given IR section into CPER, outputting to the given stream.
162void ir_section_to_cper(json_object *section,
163 EFI_ERROR_SECTION_DESCRIPTOR *descriptor, FILE *out)
164{
Ed Tanousb07061a2024-09-22 10:33:29 -0700165 json_object *ir = NULL;
166
Lawrence Tang617949e2022-08-08 14:21:42 +0100167 //Find the correct section type, and parse.
Lawrence Tang580423f2022-08-24 09:37:53 +0100168 int section_converted = 0;
John Chungf8fc7052024-05-03 20:05:29 +0800169 for (size_t i = 0; i < section_definitions_len; i++) {
Lawrence Tangf1f3b832022-08-24 09:42:39 +0100170 if (guid_equal(section_definitions[i].Guid,
171 &descriptor->SectionType) &&
172 section_definitions[i].ToCPER != NULL) {
Ed Tanousb07061a2024-09-22 10:33:29 -0700173 ir = json_object_object_get(
174 section, section_definitions[i].ShortName);
175 section_definitions[i].ToCPER(ir, out);
Lawrence Tang580423f2022-08-24 09:37:53 +0100176 section_converted = 1;
177 break;
178 }
179 }
180
181 //If unknown GUID, so read as a base64 unknown section.
182 if (!section_converted) {
Ed Tanousb07061a2024-09-22 10:33:29 -0700183 ir = json_object_object_get(section, "Unknown");
184 json_object *encoded = json_object_object_get(ir, "data");
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700185
186 int32_t decoded_len = 0;
187
188 UINT8 *decoded = base64_decode(
189 json_object_get_string(encoded),
190 json_object_get_string_len(encoded), &decoded_len);
191 if (decoded == NULL) {
John Chungf8fc7052024-05-03 20:05:29 +0800192 printf("Failed to allocate decode output buffer. \n");
193 } else {
John Chungf8fc7052024-05-03 20:05:29 +0800194 fwrite(decoded, decoded_len, 1, out);
John Chungf8fc7052024-05-03 20:05:29 +0800195 free(decoded);
196 }
Lawrence Tang617949e2022-08-08 14:21:42 +0100197 }
198}
199
Lawrence Tang0cb33792022-07-13 13:51:39 +0100200//Converts a single CPER-JSON IR section descriptor into a CPER structure.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100201void ir_section_descriptor_to_cper(json_object *section_descriptor_ir,
202 EFI_ERROR_SECTION_DESCRIPTOR *descriptor)
Lawrence Tang0cb33792022-07-13 13:51:39 +0100203{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100204 //Section offset, length.
205 descriptor->SectionOffset = (UINT32)json_object_get_uint64(
206 json_object_object_get(section_descriptor_ir, "sectionOffset"));
207 descriptor->SectionLength = (UINT32)json_object_get_uint64(
208 json_object_object_get(section_descriptor_ir, "sectionLength"));
Lawrence Tang0cb33792022-07-13 13:51:39 +0100209
Lawrence Tange407b4c2022-07-21 13:54:01 +0100210 //Revision.
211 json_object *revision =
212 json_object_object_get(section_descriptor_ir, "revision");
213 int minor =
214 json_object_get_int(json_object_object_get(revision, "minor"));
215 int major =
216 json_object_get_int(json_object_object_get(revision, "major"));
217 descriptor->Revision = minor + (major << 8);
Lawrence Tang0cb33792022-07-13 13:51:39 +0100218
Lawrence Tange407b4c2022-07-21 13:54:01 +0100219 //Validation bits, flags.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800220 ValidationTypes ui8Type = { UINT_8T, .value.ui8 = 0 };
221 struct json_object *obj = NULL;
222
Lawrence Tange407b4c2022-07-21 13:54:01 +0100223 descriptor->SectionFlags = ir_to_bitfield(
224 json_object_object_get(section_descriptor_ir, "flags"), 8,
225 CPER_SECTION_DESCRIPTOR_FLAGS_BITFIELD_NAMES);
Lawrence Tang0cb33792022-07-13 13:51:39 +0100226
Lawrence Tange407b4c2022-07-21 13:54:01 +0100227 //Section type.
228 json_object *section_type =
229 json_object_object_get(section_descriptor_ir, "sectionType");
230 string_to_guid(&descriptor->SectionType,
231 json_object_get_string(
232 json_object_object_get(section_type, "data")));
Lawrence Tang0cb33792022-07-13 13:51:39 +0100233
Lawrence Tange407b4c2022-07-21 13:54:01 +0100234 //FRU ID, if present.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800235 if (json_object_object_get_ex(section_descriptor_ir, "fruID", &obj)) {
236 json_object *fru_id = obj;
237 if (fru_id != NULL) {
238 string_to_guid(&descriptor->FruId,
239 json_object_get_string(fru_id));
240 add_to_valid_bitfield(&ui8Type, 0);
241 }
John Chungf8fc7052024-05-03 20:05:29 +0800242 }
Lawrence Tang0cb33792022-07-13 13:51:39 +0100243
Lawrence Tange407b4c2022-07-21 13:54:01 +0100244 //Severity code.
245 json_object *severity =
246 json_object_object_get(section_descriptor_ir, "severity");
247 descriptor->Severity = (UINT32)json_object_get_uint64(
248 json_object_object_get(severity, "code"));
Lawrence Tang0cb33792022-07-13 13:51:39 +0100249
Lawrence Tange407b4c2022-07-21 13:54:01 +0100250 //FRU text, if present.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800251 if (json_object_object_get_ex(section_descriptor_ir, "fruText", &obj)) {
252 json_object *fru_text = obj;
253 if (fru_text != NULL) {
254 strncpy(descriptor->FruString,
255 json_object_get_string(fru_text),
256 sizeof(descriptor->FruString) - 1);
257 descriptor
258 ->FruString[sizeof(descriptor->FruString) - 1] =
259 '\0';
260 add_to_valid_bitfield(&ui8Type, 1);
261 }
John Chungf8fc7052024-05-03 20:05:29 +0800262 }
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800263 descriptor->SecValidMask = ui8Type.value.ui8;
Lawrence Tang617949e2022-08-08 14:21:42 +0100264}
265
266//Converts IR for a given single section format CPER record into CPER binary.
267void ir_single_section_to_cper(json_object *ir, FILE *out)
268{
269 //Create & write a section descriptor to file.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800270 EFI_ERROR_SECTION_DESCRIPTOR section_descriptor;
271 memset(&section_descriptor, 0, sizeof(section_descriptor));
272
Lawrence Tang617949e2022-08-08 14:21:42 +0100273 ir_section_descriptor_to_cper(
274 json_object_object_get(ir, "sectionDescriptor"),
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800275 &section_descriptor);
276 fwrite(&section_descriptor, sizeof(section_descriptor), 1, out);
Lawrence Tang617949e2022-08-08 14:21:42 +0100277
278 //Write section to file.
279 ir_section_to_cper(json_object_object_get(ir, "section"),
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800280 &section_descriptor, out);
Lawrence Tang617949e2022-08-08 14:21:42 +0100281
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800282 fflush(out);
John Chungf8fc7052024-05-03 20:05:29 +0800283}