blob: e9c396658495996fd73f7e0a3a94de0e2d749173 [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>
Ed Tanous50b966f2025-03-11 09:06:19 -070010#include <libcper/log.h>
Thu Nguyene42fb482024-10-15 14:43:11 +000011#include <libcper/base64.h>
12#include <libcper/Cper.h>
13#include <libcper/cper-parse.h>
14#include <libcper/cper-utils.h>
15#include <libcper/sections/cper-section.h>
Lawrence Tangb44314c2022-07-13 11:45:22 +010016
17//Private pre-declarations.
Lawrence Tange407b4c2022-07-21 13:54:01 +010018void ir_header_to_cper(json_object *header_ir,
19 EFI_COMMON_ERROR_RECORD_HEADER *header);
20void ir_section_descriptor_to_cper(json_object *section_descriptor_ir,
21 EFI_ERROR_SECTION_DESCRIPTOR *descriptor);
Lawrence Tang617949e2022-08-08 14:21:42 +010022void ir_section_to_cper(json_object *section,
23 EFI_ERROR_SECTION_DESCRIPTOR *descriptor, FILE *out);
Lawrence Tangf0f95572022-07-07 16:56:22 +010024
25//Converts the given JSON IR CPER representation into CPER binary format, piped to the provided file stream.
Lawrence Tang0cb33792022-07-13 13:51:39 +010026//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 +010027//use validate_schema() from json-schema.h before attempting to call this function.
28void ir_to_cper(json_object *ir, FILE *out)
Lawrence Tangf0f95572022-07-07 16:56:22 +010029{
Lawrence Tange407b4c2022-07-21 13:54:01 +010030 //Create the CPER header.
31 EFI_COMMON_ERROR_RECORD_HEADER *header =
32 (EFI_COMMON_ERROR_RECORD_HEADER *)calloc(
33 1, sizeof(EFI_COMMON_ERROR_RECORD_HEADER));
34 ir_header_to_cper(json_object_object_get(ir, "header"), header);
35 fwrite(header, sizeof(EFI_COMMON_ERROR_RECORD_HEADER), 1, out);
36 fflush(out);
Lawrence Tang0cb33792022-07-13 13:51:39 +010037
Lawrence Tange407b4c2022-07-21 13:54:01 +010038 //Create the CPER section descriptors.
39 json_object *section_descriptors =
40 json_object_object_get(ir, "sectionDescriptors");
Ed Tanous8121f7e2025-03-06 14:39:07 -080041 if (section_descriptors == NULL) {
Ed Tanous50b966f2025-03-11 09:06:19 -070042 cper_print_log("Invalid CPER file: No section descriptors.\n");
Ed Tanous8121f7e2025-03-06 14:39:07 -080043 return;
44 }
Lawrence Tange407b4c2022-07-21 13:54:01 +010045 int amt_descriptors = json_object_array_length(section_descriptors);
46 EFI_ERROR_SECTION_DESCRIPTOR *descriptors[amt_descriptors];
47 for (int i = 0; i < amt_descriptors; i++) {
48 descriptors[i] = (EFI_ERROR_SECTION_DESCRIPTOR *)calloc(
49 1, sizeof(EFI_ERROR_SECTION_DESCRIPTOR));
50 ir_section_descriptor_to_cper(
51 json_object_array_get_idx(section_descriptors, i),
52 descriptors[i]);
53 fwrite(descriptors[i], sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1,
54 out);
55 fflush(out);
56 }
Lawrence Tang0cb33792022-07-13 13:51:39 +010057
Lawrence Tange407b4c2022-07-21 13:54:01 +010058 //Run through each section in turn.
59 json_object *sections = json_object_object_get(ir, "sections");
Ed Tanous8121f7e2025-03-06 14:39:07 -080060 if (sections == NULL) {
Ed Tanous50b966f2025-03-11 09:06:19 -070061 cper_print_log("Invalid CPER file: No sections.\n");
Ed Tanous8121f7e2025-03-06 14:39:07 -080062 return;
63 }
Lawrence Tange407b4c2022-07-21 13:54:01 +010064 int amt_sections = json_object_array_length(sections);
John Chungf8fc7052024-05-03 20:05:29 +080065 if (amt_sections == amt_descriptors) {
66 for (int i = 0; i < amt_sections; i++) {
67 //Get the section itself from the IR.
68 json_object *section =
69 json_object_array_get_idx(sections, i);
Lawrence Tang0cb33792022-07-13 13:51:39 +010070
John Chungf8fc7052024-05-03 20:05:29 +080071 //Convert.
72 ir_section_to_cper(section, descriptors[i], out);
73 }
Lawrence Tange407b4c2022-07-21 13:54:01 +010074 }
75
76 //Free all remaining resources.
77 free(header);
John Chungf8fc7052024-05-03 20:05:29 +080078 for (int i = 0; i < amt_descriptors; i++) {
Lawrence Tange407b4c2022-07-21 13:54:01 +010079 free(descriptors[i]);
John Chungf8fc7052024-05-03 20:05:29 +080080 }
Lawrence Tangb44314c2022-07-13 11:45:22 +010081}
82
83//Converts a CPER-JSON IR header to a CPER header structure.
Lawrence Tange407b4c2022-07-21 13:54:01 +010084void ir_header_to_cper(json_object *header_ir,
85 EFI_COMMON_ERROR_RECORD_HEADER *header)
Lawrence Tangb44314c2022-07-13 11:45:22 +010086{
Lawrence Tange407b4c2022-07-21 13:54:01 +010087 header->SignatureStart = 0x52455043; //CPER
Lawrence Tangb44314c2022-07-13 11:45:22 +010088
Lawrence Tange407b4c2022-07-21 13:54:01 +010089 //Revision.
90 json_object *revision = json_object_object_get(header_ir, "revision");
91 int minor =
92 json_object_get_int(json_object_object_get(revision, "minor"));
93 int major =
94 json_object_get_int(json_object_object_get(revision, "major"));
95 header->Revision = minor + (major << 8);
Lawrence Tangb44314c2022-07-13 11:45:22 +010096
Lawrence Tange407b4c2022-07-21 13:54:01 +010097 header->SignatureEnd = 0xFFFFFFFF;
Lawrence Tangb44314c2022-07-13 11:45:22 +010098
Lawrence Tange407b4c2022-07-21 13:54:01 +010099 //Section count.
100 int section_count = json_object_get_int(
101 json_object_object_get(header_ir, "sectionCount"));
102 header->SectionCount = (UINT16)section_count;
Lawrence Tangb44314c2022-07-13 11:45:22 +0100103
Lawrence Tange407b4c2022-07-21 13:54:01 +0100104 //Error severity.
105 json_object *severity = json_object_object_get(header_ir, "severity");
106 header->ErrorSeverity = (UINT32)json_object_get_uint64(
107 json_object_object_get(severity, "code"));
Lawrence Tangb44314c2022-07-13 11:45:22 +0100108
Lawrence Tange407b4c2022-07-21 13:54:01 +0100109 //Validation bits.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800110 ValidationTypes ui32Type = { UINT_32T, .value.ui32 = 0 };
111 struct json_object *obj = NULL;
Lawrence Tangb44314c2022-07-13 11:45:22 +0100112
Lawrence Tange407b4c2022-07-21 13:54:01 +0100113 //Record length.
114 header->RecordLength = (UINT32)json_object_get_uint64(
115 json_object_object_get(header_ir, "recordLength"));
Lawrence Tangb44314c2022-07-13 11:45:22 +0100116
Lawrence Tange407b4c2022-07-21 13:54:01 +0100117 //Timestamp, if present.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800118 if (json_object_object_get_ex(header_ir, "timestamp", &obj)) {
119 json_object *timestamp = obj;
120 if (timestamp != NULL) {
121 string_to_timestamp(&header->TimeStamp,
122 json_object_get_string(timestamp));
123 header->TimeStamp.Flag = json_object_get_boolean(
124 json_object_object_get(header_ir,
125 "timestampIsPrecise"));
126 }
127 add_to_valid_bitfield(&ui32Type, 1);
Lawrence Tange407b4c2022-07-21 13:54:01 +0100128 }
Lawrence Tangb44314c2022-07-13 11:45:22 +0100129
Lawrence Tange407b4c2022-07-21 13:54:01 +0100130 //Various GUIDs.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800131 json_object *platform_id;
132 json_object_object_get_ex(header_ir, "platformID", &platform_id);
133 json_object *partition_id;
134 json_object_object_get_ex(header_ir, "partitionID", &partition_id);
John Chungf8fc7052024-05-03 20:05:29 +0800135 if (platform_id != NULL) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100136 string_to_guid(&header->PlatformID,
137 json_object_get_string(platform_id));
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800138 add_to_valid_bitfield(&ui32Type, 0);
John Chungf8fc7052024-05-03 20:05:29 +0800139 }
140 if (partition_id != NULL) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100141 string_to_guid(&header->PartitionID,
142 json_object_get_string(partition_id));
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800143 add_to_valid_bitfield(&ui32Type, 2);
John Chungf8fc7052024-05-03 20:05:29 +0800144 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100145 string_to_guid(&header->CreatorID,
146 json_object_get_string(
147 json_object_object_get(header_ir, "creatorID")));
Lawrence Tangb44314c2022-07-13 11:45:22 +0100148
Lawrence Tange407b4c2022-07-21 13:54:01 +0100149 //Notification type.
150 json_object *notification_type =
151 json_object_object_get(header_ir, "notificationType");
152 string_to_guid(&header->NotificationType,
153 json_object_get_string(json_object_object_get(
154 notification_type, "guid")));
Lawrence Tangb44314c2022-07-13 11:45:22 +0100155
Lawrence Tange407b4c2022-07-21 13:54:01 +0100156 //Record ID, persistence info.
157 header->RecordID = json_object_get_uint64(
158 json_object_object_get(header_ir, "recordID"));
159 header->PersistenceInfo = json_object_get_uint64(
160 json_object_object_get(header_ir, "persistenceInfo"));
Lawrence Tangb44314c2022-07-13 11:45:22 +0100161
Lawrence Tange407b4c2022-07-21 13:54:01 +0100162 //Flags.
163 json_object *flags = json_object_object_get(header_ir, "flags");
164 header->Flags = (UINT32)json_object_get_uint64(
165 json_object_object_get(flags, "value"));
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800166
167 header->ValidationBits = ui32Type.value.ui32;
Lawrence Tang0cb33792022-07-13 13:51:39 +0100168}
169
Lawrence Tang617949e2022-08-08 14:21:42 +0100170//Converts a single given IR section into CPER, outputting to the given stream.
171void ir_section_to_cper(json_object *section,
172 EFI_ERROR_SECTION_DESCRIPTOR *descriptor, FILE *out)
173{
Ed Tanousb07061a2024-09-22 10:33:29 -0700174 json_object *ir = NULL;
175
Lawrence Tang617949e2022-08-08 14:21:42 +0100176 //Find the correct section type, and parse.
Ed Tanous1a648562025-03-10 15:23:38 -0700177 CPER_SECTION_DEFINITION *definition =
178 select_section_by_guid(&descriptor->SectionType);
Ed Tanousd6b62632025-03-14 15:30:07 -0700179 if (definition == NULL) {
180 cper_print_log("Unknown section type guid\n");
181 } else {
Ed Tanous1a648562025-03-10 15:23:38 -0700182 ir = json_object_object_get(section, definition->ShortName);
183 definition->ToCPER(ir, out);
Lawrence Tang580423f2022-08-24 09:37:53 +0100184 }
185
186 //If unknown GUID, so read as a base64 unknown section.
Ed Tanousd6b62632025-03-14 15:30:07 -0700187 if (ir == NULL) {
Ed Tanousb07061a2024-09-22 10:33:29 -0700188 ir = json_object_object_get(section, "Unknown");
189 json_object *encoded = json_object_object_get(ir, "data");
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700190
191 int32_t decoded_len = 0;
192
193 UINT8 *decoded = base64_decode(
194 json_object_get_string(encoded),
195 json_object_get_string_len(encoded), &decoded_len);
196 if (decoded == NULL) {
Ed Tanous50b966f2025-03-11 09:06:19 -0700197 cper_print_log(
198 "Failed to allocate decode output buffer. \n");
John Chungf8fc7052024-05-03 20:05:29 +0800199 } else {
John Chungf8fc7052024-05-03 20:05:29 +0800200 fwrite(decoded, decoded_len, 1, out);
John Chungf8fc7052024-05-03 20:05:29 +0800201 free(decoded);
202 }
Lawrence Tang617949e2022-08-08 14:21:42 +0100203 }
204}
205
Lawrence Tang0cb33792022-07-13 13:51:39 +0100206//Converts a single CPER-JSON IR section descriptor into a CPER structure.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100207void ir_section_descriptor_to_cper(json_object *section_descriptor_ir,
208 EFI_ERROR_SECTION_DESCRIPTOR *descriptor)
Lawrence Tang0cb33792022-07-13 13:51:39 +0100209{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100210 //Section offset, length.
211 descriptor->SectionOffset = (UINT32)json_object_get_uint64(
212 json_object_object_get(section_descriptor_ir, "sectionOffset"));
213 descriptor->SectionLength = (UINT32)json_object_get_uint64(
214 json_object_object_get(section_descriptor_ir, "sectionLength"));
Lawrence Tang0cb33792022-07-13 13:51:39 +0100215
Lawrence Tange407b4c2022-07-21 13:54:01 +0100216 //Revision.
217 json_object *revision =
218 json_object_object_get(section_descriptor_ir, "revision");
219 int minor =
220 json_object_get_int(json_object_object_get(revision, "minor"));
221 int major =
222 json_object_get_int(json_object_object_get(revision, "major"));
223 descriptor->Revision = minor + (major << 8);
Lawrence Tang0cb33792022-07-13 13:51:39 +0100224
Lawrence Tange407b4c2022-07-21 13:54:01 +0100225 //Validation bits, flags.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800226 ValidationTypes ui8Type = { UINT_8T, .value.ui8 = 0 };
227 struct json_object *obj = NULL;
228
Lawrence Tange407b4c2022-07-21 13:54:01 +0100229 descriptor->SectionFlags = ir_to_bitfield(
230 json_object_object_get(section_descriptor_ir, "flags"), 8,
231 CPER_SECTION_DESCRIPTOR_FLAGS_BITFIELD_NAMES);
Lawrence Tang0cb33792022-07-13 13:51:39 +0100232
Lawrence Tange407b4c2022-07-21 13:54:01 +0100233 //Section type.
234 json_object *section_type =
235 json_object_object_get(section_descriptor_ir, "sectionType");
236 string_to_guid(&descriptor->SectionType,
237 json_object_get_string(
238 json_object_object_get(section_type, "data")));
Lawrence Tang0cb33792022-07-13 13:51:39 +0100239
Lawrence Tange407b4c2022-07-21 13:54:01 +0100240 //FRU ID, if present.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800241 if (json_object_object_get_ex(section_descriptor_ir, "fruID", &obj)) {
242 json_object *fru_id = obj;
243 if (fru_id != NULL) {
244 string_to_guid(&descriptor->FruId,
245 json_object_get_string(fru_id));
246 add_to_valid_bitfield(&ui8Type, 0);
247 }
John Chungf8fc7052024-05-03 20:05:29 +0800248 }
Lawrence Tang0cb33792022-07-13 13:51:39 +0100249
Lawrence Tange407b4c2022-07-21 13:54:01 +0100250 //Severity code.
251 json_object *severity =
252 json_object_object_get(section_descriptor_ir, "severity");
253 descriptor->Severity = (UINT32)json_object_get_uint64(
254 json_object_object_get(severity, "code"));
Lawrence Tang0cb33792022-07-13 13:51:39 +0100255
Lawrence Tange407b4c2022-07-21 13:54:01 +0100256 //FRU text, if present.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800257 if (json_object_object_get_ex(section_descriptor_ir, "fruText", &obj)) {
258 json_object *fru_text = obj;
259 if (fru_text != NULL) {
260 strncpy(descriptor->FruString,
261 json_object_get_string(fru_text),
262 sizeof(descriptor->FruString) - 1);
263 descriptor
264 ->FruString[sizeof(descriptor->FruString) - 1] =
265 '\0';
266 add_to_valid_bitfield(&ui8Type, 1);
267 }
John Chungf8fc7052024-05-03 20:05:29 +0800268 }
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800269 descriptor->SecValidMask = ui8Type.value.ui8;
Lawrence Tang617949e2022-08-08 14:21:42 +0100270}
271
272//Converts IR for a given single section format CPER record into CPER binary.
273void ir_single_section_to_cper(json_object *ir, FILE *out)
274{
275 //Create & write a section descriptor to file.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800276 EFI_ERROR_SECTION_DESCRIPTOR section_descriptor;
277 memset(&section_descriptor, 0, sizeof(section_descriptor));
278
Lawrence Tang617949e2022-08-08 14:21:42 +0100279 ir_section_descriptor_to_cper(
280 json_object_object_get(ir, "sectionDescriptor"),
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800281 &section_descriptor);
282 fwrite(&section_descriptor, sizeof(section_descriptor), 1, out);
Lawrence Tang617949e2022-08-08 14:21:42 +0100283
284 //Write section to file.
285 ir_section_to_cper(json_object_object_get(ir, "section"),
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800286 &section_descriptor, out);
Lawrence Tang617949e2022-08-08 14:21:42 +0100287
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800288 fflush(out);
John Chungf8fc7052024-05-03 20:05:29 +0800289}