blob: 35c35c8846d2b6cf31e5c460ea96b8fe4af4373d [file] [log] [blame]
Lawrence Tangf0f95572022-07-07 16:56:22 +01001/**
2 * Describes functions for parsing JSON IR CPER data into binary CPER format.
3 *
4 * 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>
Lawrence Tang0cb33792022-07-13 13:51:39 +010010#include "b64.h"
Lawrence Tangb44314c2022-07-13 11:45:22 +010011#include "edk/Cper.h"
Lawrence Tangf0f95572022-07-07 16:56:22 +010012#include "cper-parse.h"
Lawrence Tangb44314c2022-07-13 11:45:22 +010013#include "cper-utils.h"
Lawrence Tang580423f2022-08-24 09:37:53 +010014#include "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);
56 for (int i = 0; i < amt_sections; i++) {
57 //Get the section itself from the IR.
58 json_object *section = json_object_array_get_idx(sections, i);
Lawrence Tang0cb33792022-07-13 13:51:39 +010059
Lawrence Tang617949e2022-08-08 14:21:42 +010060 //Convert.
61 ir_section_to_cper(section, descriptors[i], out);
Lawrence Tange407b4c2022-07-21 13:54:01 +010062 }
63
64 //Free all remaining resources.
65 free(header);
66 for (int i = 0; i < amt_descriptors; i++)
67 free(descriptors[i]);
Lawrence Tangb44314c2022-07-13 11:45:22 +010068}
69
70//Converts a CPER-JSON IR header to a CPER header structure.
Lawrence Tange407b4c2022-07-21 13:54:01 +010071void ir_header_to_cper(json_object *header_ir,
72 EFI_COMMON_ERROR_RECORD_HEADER *header)
Lawrence Tangb44314c2022-07-13 11:45:22 +010073{
Lawrence Tange407b4c2022-07-21 13:54:01 +010074 header->SignatureStart = 0x52455043; //CPER
Lawrence Tangb44314c2022-07-13 11:45:22 +010075
Lawrence Tange407b4c2022-07-21 13:54:01 +010076 //Revision.
77 json_object *revision = json_object_object_get(header_ir, "revision");
78 int minor =
79 json_object_get_int(json_object_object_get(revision, "minor"));
80 int major =
81 json_object_get_int(json_object_object_get(revision, "major"));
82 header->Revision = minor + (major << 8);
Lawrence Tangb44314c2022-07-13 11:45:22 +010083
Lawrence Tange407b4c2022-07-21 13:54:01 +010084 header->SignatureEnd = 0xFFFFFFFF;
Lawrence Tangb44314c2022-07-13 11:45:22 +010085
Lawrence Tange407b4c2022-07-21 13:54:01 +010086 //Section count.
87 int section_count = json_object_get_int(
88 json_object_object_get(header_ir, "sectionCount"));
89 header->SectionCount = (UINT16)section_count;
Lawrence Tangb44314c2022-07-13 11:45:22 +010090
Lawrence Tange407b4c2022-07-21 13:54:01 +010091 //Error severity.
92 json_object *severity = json_object_object_get(header_ir, "severity");
93 header->ErrorSeverity = (UINT32)json_object_get_uint64(
94 json_object_object_get(severity, "code"));
Lawrence Tangb44314c2022-07-13 11:45:22 +010095
Lawrence Tange407b4c2022-07-21 13:54:01 +010096 //Validation bits.
97 header->ValidationBits = ir_to_bitfield(
98 json_object_object_get(header_ir, "validationBits"), 3,
99 CPER_HEADER_VALID_BITFIELD_NAMES);
Lawrence Tangb44314c2022-07-13 11:45:22 +0100100
Lawrence Tange407b4c2022-07-21 13:54:01 +0100101 //Record length.
102 header->RecordLength = (UINT32)json_object_get_uint64(
103 json_object_object_get(header_ir, "recordLength"));
Lawrence Tangb44314c2022-07-13 11:45:22 +0100104
Lawrence Tange407b4c2022-07-21 13:54:01 +0100105 //Timestamp, if present.
106 json_object *timestamp = json_object_object_get(header_ir, "timestamp");
107 if (timestamp != NULL) {
108 string_to_timestamp(&header->TimeStamp,
109 json_object_get_string(timestamp));
110 header->TimeStamp.Flag = json_object_get_boolean(
111 json_object_object_get(header_ir,
112 "timestampIsPrecise"));
113 }
Lawrence Tangb44314c2022-07-13 11:45:22 +0100114
Lawrence Tange407b4c2022-07-21 13:54:01 +0100115 //Various GUIDs.
116 json_object *platform_id =
117 json_object_object_get(header_ir, "platformID");
118 json_object *partition_id =
119 json_object_object_get(header_ir, "partitionID");
120 if (platform_id != NULL)
121 string_to_guid(&header->PlatformID,
122 json_object_get_string(platform_id));
123 if (partition_id != NULL)
124 string_to_guid(&header->PartitionID,
125 json_object_get_string(partition_id));
126 string_to_guid(&header->CreatorID,
127 json_object_get_string(
128 json_object_object_get(header_ir, "creatorID")));
Lawrence Tangb44314c2022-07-13 11:45:22 +0100129
Lawrence Tange407b4c2022-07-21 13:54:01 +0100130 //Notification type.
131 json_object *notification_type =
132 json_object_object_get(header_ir, "notificationType");
133 string_to_guid(&header->NotificationType,
134 json_object_get_string(json_object_object_get(
135 notification_type, "guid")));
Lawrence Tangb44314c2022-07-13 11:45:22 +0100136
Lawrence Tange407b4c2022-07-21 13:54:01 +0100137 //Record ID, persistence info.
138 header->RecordID = json_object_get_uint64(
139 json_object_object_get(header_ir, "recordID"));
140 header->PersistenceInfo = json_object_get_uint64(
141 json_object_object_get(header_ir, "persistenceInfo"));
Lawrence Tangb44314c2022-07-13 11:45:22 +0100142
Lawrence Tange407b4c2022-07-21 13:54:01 +0100143 //Flags.
144 json_object *flags = json_object_object_get(header_ir, "flags");
145 header->Flags = (UINT32)json_object_get_uint64(
146 json_object_object_get(flags, "value"));
Lawrence Tang0cb33792022-07-13 13:51:39 +0100147}
148
Lawrence Tang617949e2022-08-08 14:21:42 +0100149//Converts a single given IR section into CPER, outputting to the given stream.
150void ir_section_to_cper(json_object *section,
151 EFI_ERROR_SECTION_DESCRIPTOR *descriptor, FILE *out)
152{
153 //Find the correct section type, and parse.
Lawrence Tang580423f2022-08-24 09:37:53 +0100154 int section_converted = 0;
Lawrence Tangf1f3b832022-08-24 09:42:39 +0100155 for (int i = 0; i < section_definitions_len; i++) {
156 if (guid_equal(section_definitions[i].Guid,
157 &descriptor->SectionType) &&
158 section_definitions[i].ToCPER != NULL) {
Lawrence Tang580423f2022-08-24 09:37:53 +0100159 section_definitions[i].ToCPER(section, out);
160 section_converted = 1;
161 break;
162 }
163 }
164
165 //If unknown GUID, so read as a base64 unknown section.
166 if (!section_converted) {
Lawrence Tang617949e2022-08-08 14:21:42 +0100167 json_object *encoded = json_object_object_get(section, "data");
168 UINT8 *decoded =
169 b64_decode(json_object_get_string(encoded),
170 json_object_get_string_len(encoded));
171 fwrite(decoded, descriptor->SectionLength, 1, out);
172 fflush(out);
173 free(decoded);
174 }
175}
176
Lawrence Tang0cb33792022-07-13 13:51:39 +0100177//Converts a single CPER-JSON IR section descriptor into a CPER structure.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100178void ir_section_descriptor_to_cper(json_object *section_descriptor_ir,
179 EFI_ERROR_SECTION_DESCRIPTOR *descriptor)
Lawrence Tang0cb33792022-07-13 13:51:39 +0100180{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100181 //Section offset, length.
182 descriptor->SectionOffset = (UINT32)json_object_get_uint64(
183 json_object_object_get(section_descriptor_ir, "sectionOffset"));
184 descriptor->SectionLength = (UINT32)json_object_get_uint64(
185 json_object_object_get(section_descriptor_ir, "sectionLength"));
Lawrence Tang0cb33792022-07-13 13:51:39 +0100186
Lawrence Tange407b4c2022-07-21 13:54:01 +0100187 //Revision.
188 json_object *revision =
189 json_object_object_get(section_descriptor_ir, "revision");
190 int minor =
191 json_object_get_int(json_object_object_get(revision, "minor"));
192 int major =
193 json_object_get_int(json_object_object_get(revision, "major"));
194 descriptor->Revision = minor + (major << 8);
Lawrence Tang0cb33792022-07-13 13:51:39 +0100195
Lawrence Tange407b4c2022-07-21 13:54:01 +0100196 //Validation bits, flags.
197 descriptor->SecValidMask = ir_to_bitfield(
198 json_object_object_get(section_descriptor_ir, "validationBits"),
199 2, CPER_SECTION_DESCRIPTOR_VALID_BITFIELD_NAMES);
200 descriptor->SectionFlags = ir_to_bitfield(
201 json_object_object_get(section_descriptor_ir, "flags"), 8,
202 CPER_SECTION_DESCRIPTOR_FLAGS_BITFIELD_NAMES);
Lawrence Tang0cb33792022-07-13 13:51:39 +0100203
Lawrence Tange407b4c2022-07-21 13:54:01 +0100204 //Section type.
205 json_object *section_type =
206 json_object_object_get(section_descriptor_ir, "sectionType");
207 string_to_guid(&descriptor->SectionType,
208 json_object_get_string(
209 json_object_object_get(section_type, "data")));
Lawrence Tang0cb33792022-07-13 13:51:39 +0100210
Lawrence Tange407b4c2022-07-21 13:54:01 +0100211 //FRU ID, if present.
212 json_object *fru_id =
213 json_object_object_get(section_descriptor_ir, "fruID");
214 if (fru_id != NULL)
215 string_to_guid(&descriptor->FruId,
216 json_object_get_string(fru_id));
Lawrence Tang0cb33792022-07-13 13:51:39 +0100217
Lawrence Tange407b4c2022-07-21 13:54:01 +0100218 //Severity code.
219 json_object *severity =
220 json_object_object_get(section_descriptor_ir, "severity");
221 descriptor->Severity = (UINT32)json_object_get_uint64(
222 json_object_object_get(severity, "code"));
Lawrence Tang0cb33792022-07-13 13:51:39 +0100223
Lawrence Tange407b4c2022-07-21 13:54:01 +0100224 //FRU text, if present.
225 json_object *fru_text =
226 json_object_object_get(section_descriptor_ir, "fruText");
227 if (fru_text != NULL)
228 strncpy(descriptor->FruString, json_object_get_string(fru_text),
229 20);
Lawrence Tang617949e2022-08-08 14:21:42 +0100230}
231
232//Converts IR for a given single section format CPER record into CPER binary.
233void ir_single_section_to_cper(json_object *ir, FILE *out)
234{
235 //Create & write a section descriptor to file.
236 EFI_ERROR_SECTION_DESCRIPTOR *section_descriptor =
237 (EFI_ERROR_SECTION_DESCRIPTOR *)calloc(
238 1, sizeof(EFI_ERROR_SECTION_DESCRIPTOR));
239 ir_section_descriptor_to_cper(
240 json_object_object_get(ir, "sectionDescriptor"),
241 section_descriptor);
242 fwrite(section_descriptor, sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1,
243 out);
244 fflush(out);
245
246 //Write section to file.
247 ir_section_to_cper(json_object_object_get(ir, "section"),
248 section_descriptor, out);
249
250 //Free remaining resources.
251 free(section_descriptor);
Lawrence Tangf0f95572022-07-07 16:56:22 +0100252}