Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 1 | /** |
Ed Tanous | fedd457 | 2024-07-12 13:56:00 -0700 | [diff] [blame] | 2 | * Describes high level functions for converting an entire CPER log, and functions for parsing |
Lawrence Tang | 2800cd8 | 2022-07-05 16:08:20 +0100 | [diff] [blame] | 3 | * CPER headers and section descriptions into an intermediate JSON format. |
Ed Tanous | fedd457 | 2024-07-12 13:56:00 -0700 | [diff] [blame] | 4 | * |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 5 | * Author: Lawrence.Tang@arm.com |
| 6 | **/ |
| 7 | |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 8 | #include <limits.h> |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 9 | #include <stdio.h> |
Karthik Rajagopalan | 5220c9b | 2024-08-08 00:24:44 -0700 | [diff] [blame] | 10 | #include <string.h> |
Lawrence Tang | 5202bbb | 2022-08-12 14:54:36 +0100 | [diff] [blame] | 11 | #include <json.h> |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 12 | |
Thu Nguyen | e42fb48 | 2024-10-15 14:43:11 +0000 | [diff] [blame] | 13 | #include <libcper/base64.h> |
| 14 | #include <libcper/Cper.h> |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 15 | #include <libcper/log.h> |
Thu Nguyen | e42fb48 | 2024-10-15 14:43:11 +0000 | [diff] [blame] | 16 | #include <libcper/cper-parse.h> |
| 17 | #include <libcper/cper-parse-str.h> |
| 18 | #include <libcper/cper-utils.h> |
| 19 | #include <libcper/sections/cper-section.h> |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 20 | |
| 21 | //Private pre-definitions. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 22 | json_object *cper_header_to_ir(EFI_COMMON_ERROR_RECORD_HEADER *header); |
| 23 | json_object * |
| 24 | cper_section_descriptor_to_ir(EFI_ERROR_SECTION_DESCRIPTOR *section_descriptor); |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 25 | |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 26 | json_object *cper_buf_section_to_ir(const void *cper_section_buf, size_t size, |
| 27 | EFI_ERROR_SECTION_DESCRIPTOR *descriptor); |
| 28 | |
| 29 | json_object *cper_buf_to_ir(const unsigned char *cper_buf, size_t size) |
Ed Tanous | 8d47a37 | 2025-03-05 15:55:36 -0800 | [diff] [blame] | 30 | { |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 31 | json_object *parent = NULL; |
| 32 | json_object *header_ir = NULL; |
| 33 | json_object *section_descriptors_ir = NULL; |
| 34 | json_object *sections_ir = NULL; |
| 35 | |
| 36 | const unsigned char *pos = cper_buf; |
| 37 | unsigned int remaining = size; |
| 38 | |
| 39 | if (remaining < sizeof(EFI_COMMON_ERROR_RECORD_HEADER)) { |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 40 | cper_print_log( |
| 41 | "Invalid CPER file: Invalid header (incorrect signature).\n"); |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 42 | goto fail; |
Ed Tanous | 8d47a37 | 2025-03-05 15:55:36 -0800 | [diff] [blame] | 43 | } |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 44 | |
| 45 | EFI_COMMON_ERROR_RECORD_HEADER *header = NULL; |
| 46 | header = (EFI_COMMON_ERROR_RECORD_HEADER *)cper_buf; |
| 47 | pos += sizeof(EFI_COMMON_ERROR_RECORD_HEADER); |
| 48 | remaining -= sizeof(EFI_COMMON_ERROR_RECORD_HEADER); |
| 49 | if (header->SignatureStart != EFI_ERROR_RECORD_SIGNATURE_START) { |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 50 | cper_print_log( |
| 51 | "Invalid CPER file: Invalid header (incorrect signature).\n"); |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 52 | goto fail; |
| 53 | } |
| 54 | if (header->SectionCount == 0) { |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 55 | cper_print_log( |
| 56 | "Invalid CPER file: Invalid section count (0).\n"); |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 57 | goto fail; |
| 58 | } |
| 59 | if (remaining < sizeof(EFI_ERROR_SECTION_DESCRIPTOR)) { |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 60 | cper_print_log( |
| 61 | "Invalid CPER file: Invalid section descriptor (section offset + length > size).\n"); |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 62 | goto fail; |
| 63 | } |
| 64 | |
| 65 | //Create the header JSON object from the read bytes. |
| 66 | parent = json_object_new_object(); |
| 67 | header_ir = cper_header_to_ir(header); |
| 68 | |
| 69 | json_object_object_add(parent, "header", header_ir); |
| 70 | |
| 71 | //Read the appropriate number of section descriptors & sections, and convert them into IR format. |
| 72 | section_descriptors_ir = json_object_new_array(); |
| 73 | sections_ir = json_object_new_array(); |
| 74 | for (int i = 0; i < header->SectionCount; i++) { |
| 75 | //Create the section descriptor. |
| 76 | if (remaining < sizeof(EFI_ERROR_SECTION_DESCRIPTOR)) { |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 77 | cper_print_log( |
| 78 | "Invalid number of section headers: Header states %d sections, could not read section %d.\n", |
| 79 | header->SectionCount, i + 1); |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 80 | goto fail; |
| 81 | } |
| 82 | |
| 83 | EFI_ERROR_SECTION_DESCRIPTOR *section_descriptor; |
| 84 | section_descriptor = (EFI_ERROR_SECTION_DESCRIPTOR *)(pos); |
| 85 | pos += sizeof(EFI_ERROR_SECTION_DESCRIPTOR); |
| 86 | remaining -= sizeof(EFI_ERROR_SECTION_DESCRIPTOR); |
| 87 | |
| 88 | if (section_descriptor->SectionOffset > size) { |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 89 | cper_print_log( |
| 90 | "Invalid section descriptor: Section offset > size.\n"); |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 91 | goto fail; |
| 92 | } |
| 93 | |
| 94 | if (section_descriptor->SectionLength <= 0) { |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 95 | cper_print_log( |
| 96 | "Invalid section descriptor: Section length <= 0.\n"); |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 97 | goto fail; |
| 98 | } |
| 99 | |
| 100 | if (section_descriptor->SectionOffset > |
| 101 | UINT_MAX - section_descriptor->SectionLength) { |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 102 | cper_print_log( |
| 103 | "Invalid section descriptor: Section offset + length would overflow.\n"); |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 104 | goto fail; |
| 105 | } |
| 106 | |
| 107 | if (section_descriptor->SectionOffset + |
| 108 | section_descriptor->SectionLength > |
| 109 | size) { |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 110 | cper_print_log( |
| 111 | "Invalid section descriptor: Section offset + length > size.\n"); |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 112 | goto fail; |
| 113 | } |
| 114 | |
| 115 | const unsigned char *section_begin = |
| 116 | cper_buf + section_descriptor->SectionOffset; |
Ed Tanous | d6b6263 | 2025-03-14 15:30:07 -0700 | [diff] [blame^] | 117 | json_object *section_descriptor_ir = |
| 118 | cper_section_descriptor_to_ir(section_descriptor); |
| 119 | json_object_array_add(section_descriptors_ir, |
| 120 | section_descriptor_ir); |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 121 | |
| 122 | //Read the section itself. |
| 123 | json_object *section_ir = cper_buf_section_to_ir( |
| 124 | section_begin, section_descriptor->SectionLength, |
| 125 | section_descriptor); |
Ed Tanous | d6b6263 | 2025-03-14 15:30:07 -0700 | [diff] [blame^] | 126 | if (section_ir == NULL) { |
| 127 | cper_print_log("Failed to parse section %d\n", i); |
| 128 | section_ir = json_object_new_object(); |
| 129 | } |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 130 | json_object_array_add(sections_ir, section_ir); |
| 131 | } |
| 132 | |
| 133 | //Add the header, section descriptors, and sections to a parent object. |
| 134 | json_object_object_add(parent, "sectionDescriptors", |
| 135 | section_descriptors_ir); |
| 136 | json_object_object_add(parent, "sections", sections_ir); |
| 137 | |
| 138 | return parent; |
| 139 | |
| 140 | fail: |
| 141 | json_object_put(sections_ir); |
| 142 | json_object_put(section_descriptors_ir); |
| 143 | json_object_put(parent); |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 144 | cper_print_log("Failed to parse CPER file.\n"); |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 145 | return NULL; |
Ed Tanous | 8d47a37 | 2025-03-05 15:55:36 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 148 | //Reads a CPER log file at the given file location, and returns an intermediate |
| 149 | //JSON representation of this CPER record. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 150 | json_object *cper_to_ir(FILE *cper_file) |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 151 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 152 | //Ensure this is really a CPER log. |
| 153 | EFI_COMMON_ERROR_RECORD_HEADER header; |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 154 | if (fread(&header, sizeof(EFI_COMMON_ERROR_RECORD_HEADER), 1, |
| 155 | cper_file) != 1) { |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 156 | cper_print_log( |
| 157 | "Invalid CPER file: Invalid length (log too short).\n"); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 158 | return NULL; |
| 159 | } |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 160 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 161 | //Check if the header contains the magic bytes ("CPER"). |
| 162 | if (header.SignatureStart != EFI_ERROR_RECORD_SIGNATURE_START) { |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 163 | cper_print_log( |
| 164 | "Invalid CPER file: Invalid header (incorrect signature).\n"); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 165 | return NULL; |
| 166 | } |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 167 | fseek(cper_file, -sizeof(EFI_COMMON_ERROR_RECORD_HEADER), SEEK_CUR); |
| 168 | unsigned char *cper_buf = malloc(header.RecordLength); |
| 169 | if (fread(cper_buf, header.RecordLength, 1, cper_file) != 1) { |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 170 | cper_print_log("File read failed\n"); |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 171 | free(cper_buf); |
| 172 | return NULL; |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 173 | } |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 174 | |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 175 | json_object *ir = cper_buf_to_ir(cper_buf, header.RecordLength); |
| 176 | free(cper_buf); |
| 177 | return ir; |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Karthik Rajagopalan | 5220c9b | 2024-08-08 00:24:44 -0700 | [diff] [blame] | 180 | char *cper_to_str_ir(FILE *cper_file) |
| 181 | { |
| 182 | json_object *jobj = cper_to_ir(cper_file); |
| 183 | char *str = jobj ? strdup(json_object_to_json_string(jobj)) : NULL; |
| 184 | |
| 185 | json_object_put(jobj); |
| 186 | return str; |
| 187 | } |
| 188 | |
| 189 | char *cperbuf_to_str_ir(const unsigned char *cper, size_t size) |
| 190 | { |
| 191 | FILE *cper_file = fmemopen((void *)cper, size, "r"); |
| 192 | |
| 193 | return cper_file ? cper_to_str_ir(cper_file) : NULL; |
| 194 | } |
| 195 | |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 196 | //Converts a parsed CPER record header into intermediate JSON object format. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 197 | json_object *cper_header_to_ir(EFI_COMMON_ERROR_RECORD_HEADER *header) |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 198 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 199 | json_object *header_ir = json_object_new_object(); |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 200 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 201 | //Revision/version information. |
| 202 | json_object_object_add(header_ir, "revision", |
| 203 | revision_to_ir(header->Revision)); |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 204 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 205 | //Section count. |
| 206 | json_object_object_add(header_ir, "sectionCount", |
| 207 | json_object_new_int(header->SectionCount)); |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 208 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 209 | //Error severity (with interpreted string version). |
| 210 | json_object *error_severity = json_object_new_object(); |
| 211 | json_object_object_add(error_severity, "code", |
| 212 | json_object_new_uint64(header->ErrorSeverity)); |
| 213 | json_object_object_add(error_severity, "name", |
| 214 | json_object_new_string(severity_to_string( |
| 215 | header->ErrorSeverity))); |
| 216 | json_object_object_add(header_ir, "severity", error_severity); |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 217 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 218 | //Total length of the record (including headers) in bytes. |
| 219 | json_object_object_add(header_ir, "recordLength", |
| 220 | json_object_new_uint64(header->RecordLength)); |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 221 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 222 | //If a timestamp exists according to validation bits, then add it. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 223 | if (header->ValidationBits & 0x2) { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 224 | char timestamp_string[TIMESTAMP_LENGTH]; |
Ed Tanous | 596c59e | 2025-03-10 13:15:58 -0700 | [diff] [blame] | 225 | if (timestamp_to_string(timestamp_string, TIMESTAMP_LENGTH, |
Ed Tanous | d6b6263 | 2025-03-14 15:30:07 -0700 | [diff] [blame^] | 226 | &header->TimeStamp) >= 0) { |
| 227 | json_object_object_add( |
| 228 | header_ir, "timestamp", |
| 229 | json_object_new_string(timestamp_string)); |
Ed Tanous | 596c59e | 2025-03-10 13:15:58 -0700 | [diff] [blame] | 230 | |
Ed Tanous | d6b6263 | 2025-03-14 15:30:07 -0700 | [diff] [blame^] | 231 | json_object_object_add(header_ir, "timestampIsPrecise", |
| 232 | json_object_new_boolean( |
| 233 | header->TimeStamp.Flag)); |
| 234 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 235 | } |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 236 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 237 | //If a platform ID exists according to the validation bits, then add it. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 238 | if (header->ValidationBits & 0x1) { |
Ed Tanous | c2ebddd | 2025-03-09 10:07:01 -0700 | [diff] [blame] | 239 | add_guid(header_ir, "platformID", &header->PlatformID); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 240 | } |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 241 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 242 | //If a partition ID exists according to the validation bits, then add it. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 243 | if (header->ValidationBits & 0x4) { |
Ed Tanous | c2ebddd | 2025-03-09 10:07:01 -0700 | [diff] [blame] | 244 | add_guid(header_ir, "partitionID", &header->PartitionID); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 245 | } |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 246 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 247 | //Creator ID of the header. |
Ed Tanous | c2ebddd | 2025-03-09 10:07:01 -0700 | [diff] [blame] | 248 | add_guid(header_ir, "creatorID", &header->CreatorID); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 249 | //Notification type for the header. Some defined types are available. |
| 250 | json_object *notification_type = json_object_new_object(); |
Ed Tanous | c2ebddd | 2025-03-09 10:07:01 -0700 | [diff] [blame] | 251 | add_guid(notification_type, "guid", &header->NotificationType); |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 252 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 253 | //Add the human readable notification type if possible. |
Ed Tanous | c2ebddd | 2025-03-09 10:07:01 -0700 | [diff] [blame] | 254 | const char *notification_type_readable = "Unknown"; |
Ed Tanous | 1a64856 | 2025-03-10 15:23:38 -0700 | [diff] [blame] | 255 | |
| 256 | EFI_GUID *guids[] = { |
| 257 | &gEfiEventNotificationTypeCmcGuid, |
| 258 | &gEfiEventNotificationTypeCpeGuid, |
| 259 | &gEfiEventNotificationTypeMceGuid, |
| 260 | &gEfiEventNotificationTypePcieGuid, |
| 261 | &gEfiEventNotificationTypeInitGuid, |
| 262 | &gEfiEventNotificationTypeNmiGuid, |
| 263 | &gEfiEventNotificationTypeBootGuid, |
| 264 | &gEfiEventNotificationTypeDmarGuid, |
| 265 | &gEfiEventNotificationTypeSeaGuid, |
| 266 | &gEfiEventNotificationTypeSeiGuid, |
| 267 | &gEfiEventNotificationTypePeiGuid, |
| 268 | &gEfiEventNotificationTypeCxlGuid, |
| 269 | }; |
| 270 | |
| 271 | const char *readable_names[] = { |
| 272 | "CMC", "CPE", "MCE", "PCIe", "INIT", "NMI", |
| 273 | "Boot", "DMAr", "SEA", "SEI", "PEI", "CXL Component" |
| 274 | }; |
| 275 | |
| 276 | int index = select_guid_from_list(&header->NotificationType, guids, |
| 277 | sizeof(guids) / sizeof(EFI_GUID *)); |
| 278 | if (index < (int)(sizeof(readable_names) / sizeof(char *))) { |
| 279 | notification_type_readable = readable_names[index]; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 280 | } |
Ed Tanous | 1a64856 | 2025-03-10 15:23:38 -0700 | [diff] [blame] | 281 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 282 | json_object_object_add( |
| 283 | notification_type, "type", |
| 284 | json_object_new_string(notification_type_readable)); |
| 285 | json_object_object_add(header_ir, "notificationType", |
| 286 | notification_type); |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 287 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 288 | //The record ID for this record, unique on a given system. |
| 289 | json_object_object_add(header_ir, "recordID", |
| 290 | json_object_new_uint64(header->RecordID)); |
| 291 | |
| 292 | //Flag for the record, and a human readable form. |
| 293 | json_object *flags = integer_to_readable_pair( |
| 294 | header->Flags, |
| 295 | sizeof(CPER_HEADER_FLAG_TYPES_KEYS) / sizeof(int), |
| 296 | CPER_HEADER_FLAG_TYPES_KEYS, CPER_HEADER_FLAG_TYPES_VALUES, |
| 297 | "Unknown"); |
| 298 | json_object_object_add(header_ir, "flags", flags); |
| 299 | |
| 300 | //Persistence information. Outside the scope of specification, so just a uint32 here. |
| 301 | json_object_object_add(header_ir, "persistenceInfo", |
| 302 | json_object_new_uint64(header->PersistenceInfo)); |
| 303 | return header_ir; |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | //Converts the given EFI section descriptor into JSON IR format. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 307 | json_object * |
| 308 | cper_section_descriptor_to_ir(EFI_ERROR_SECTION_DESCRIPTOR *section_descriptor) |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 309 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 310 | json_object *section_descriptor_ir = json_object_new_object(); |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 311 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 312 | //The offset of the section from the base of the record header, length. |
| 313 | json_object_object_add( |
| 314 | section_descriptor_ir, "sectionOffset", |
| 315 | json_object_new_uint64(section_descriptor->SectionOffset)); |
| 316 | json_object_object_add( |
| 317 | section_descriptor_ir, "sectionLength", |
| 318 | json_object_new_uint64(section_descriptor->SectionLength)); |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 319 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 320 | //Revision. |
| 321 | json_object_object_add(section_descriptor_ir, "revision", |
| 322 | revision_to_ir(section_descriptor->Revision)); |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 323 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 324 | //Flag bits. |
| 325 | json_object *flags = |
| 326 | bitfield_to_ir(section_descriptor->SectionFlags, 8, |
| 327 | CPER_SECTION_DESCRIPTOR_FLAGS_BITFIELD_NAMES); |
| 328 | json_object_object_add(section_descriptor_ir, "flags", flags); |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 329 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 330 | //Section type (GUID). |
| 331 | json_object *section_type = json_object_new_object(); |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 332 | |
Ed Tanous | c2ebddd | 2025-03-09 10:07:01 -0700 | [diff] [blame] | 333 | add_guid(section_type, "data", §ion_descriptor->SectionType); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 334 | //Readable section type, if possible. |
Lawrence Tang | 580423f | 2022-08-24 09:37:53 +0100 | [diff] [blame] | 335 | const char *section_type_readable = "Unknown"; |
Ed Tanous | 1a64856 | 2025-03-10 15:23:38 -0700 | [diff] [blame] | 336 | |
| 337 | CPER_SECTION_DEFINITION *section = |
| 338 | select_section_by_guid(§ion_descriptor->SectionType); |
| 339 | if (section != NULL) { |
| 340 | section_type_readable = section->ReadableName; |
Lawrence Tang | 580423f | 2022-08-24 09:37:53 +0100 | [diff] [blame] | 341 | } |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 342 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 343 | json_object_object_add(section_type, "type", |
| 344 | json_object_new_string(section_type_readable)); |
| 345 | json_object_object_add(section_descriptor_ir, "sectionType", |
| 346 | section_type); |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 347 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 348 | //If validation bits indicate it exists, add FRU ID. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 349 | if (section_descriptor->SecValidMask & 0x1) { |
Ed Tanous | c2ebddd | 2025-03-09 10:07:01 -0700 | [diff] [blame] | 350 | add_guid(section_descriptor_ir, "fruID", |
| 351 | §ion_descriptor->FruId); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 352 | } |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 353 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 354 | //If validation bits indicate it exists, add FRU text. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 355 | if ((section_descriptor->SecValidMask & 0x2) >> 1) { |
Ed Tanous | 8121f7e | 2025-03-06 14:39:07 -0800 | [diff] [blame] | 356 | int fru_text_len = 0; |
| 357 | for (; |
| 358 | fru_text_len < (int)sizeof(section_descriptor->FruString); |
| 359 | fru_text_len++) { |
| 360 | char c = section_descriptor->FruString[fru_text_len]; |
| 361 | if (c < 0) { |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 362 | //cper_print_log("Fru text contains non-ASCII character\n"); |
Ed Tanous | 8121f7e | 2025-03-06 14:39:07 -0800 | [diff] [blame] | 363 | fru_text_len = -1; |
| 364 | break; |
| 365 | } |
| 366 | if (c == '\0') { |
| 367 | break; |
| 368 | } |
| 369 | } |
| 370 | if (fru_text_len >= 0) { |
| 371 | json_object_object_add( |
| 372 | section_descriptor_ir, "fruText", |
| 373 | json_object_new_string_len( |
| 374 | section_descriptor->FruString, |
| 375 | fru_text_len)); |
| 376 | } |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 377 | } |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 378 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 379 | //Section severity. |
| 380 | json_object *section_severity = json_object_new_object(); |
| 381 | json_object_object_add( |
| 382 | section_severity, "code", |
| 383 | json_object_new_uint64(section_descriptor->Severity)); |
| 384 | json_object_object_add(section_severity, "name", |
| 385 | json_object_new_string(severity_to_string( |
| 386 | section_descriptor->Severity))); |
| 387 | json_object_object_add(section_descriptor_ir, "severity", |
| 388 | section_severity); |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 389 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 390 | return section_descriptor_ir; |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 391 | } |
| 392 | |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame] | 393 | json_object *read_section(const unsigned char *cper_section_buf, size_t size, |
| 394 | CPER_SECTION_DEFINITION *definition) |
Ed Tanous | d759a18 | 2025-03-07 15:51:45 -0800 | [diff] [blame] | 395 | { |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame] | 396 | if (definition->ToIR == NULL) { |
| 397 | return NULL; |
| 398 | } |
| 399 | json_object *section_ir = definition->ToIR(cper_section_buf, size); |
Ed Tanous | d6b6263 | 2025-03-14 15:30:07 -0700 | [diff] [blame^] | 400 | if (section_ir == NULL) { |
| 401 | return NULL; |
| 402 | } |
Ed Tanous | d759a18 | 2025-03-07 15:51:45 -0800 | [diff] [blame] | 403 | json_object *result = json_object_new_object(); |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame] | 404 | json_object_object_add(result, definition->ShortName, section_ir); |
Ed Tanous | d759a18 | 2025-03-07 15:51:45 -0800 | [diff] [blame] | 405 | return result; |
| 406 | } |
| 407 | |
Ed Tanous | 1a64856 | 2025-03-10 15:23:38 -0700 | [diff] [blame] | 408 | CPER_SECTION_DEFINITION *select_section_by_guid(EFI_GUID *guid) |
| 409 | { |
| 410 | size_t i = 0; |
| 411 | for (; i < section_definitions_len; i++) { |
| 412 | if (guid_equal(guid, section_definitions[i].Guid)) { |
| 413 | break; |
| 414 | } |
| 415 | } |
| 416 | // It's unlikely fuzzing can reliably come up with a correct guid, given how |
| 417 | // much entropy there is. If we're in fuzzing mode, and if we haven't found |
| 418 | // a match, try to force a match so we get some coverage. Note, we still |
| 419 | // want coverage of the section failed to convert code, so treat index == |
| 420 | // size as section failed to convert. |
| 421 | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
| 422 | if (i == section_definitions_len) { |
| 423 | i = guid->Data1 % (section_definitions_len + 1); |
| 424 | } |
| 425 | #endif |
| 426 | if (i < section_definitions_len) { |
| 427 | return §ion_definitions[i]; |
| 428 | } |
| 429 | |
| 430 | return NULL; |
| 431 | } |
| 432 | |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 433 | //Converts the section described by a single given section descriptor. |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 434 | json_object *cper_buf_section_to_ir(const void *cper_section_buf, size_t size, |
| 435 | EFI_ERROR_SECTION_DESCRIPTOR *descriptor) |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 436 | { |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 437 | if (descriptor->SectionLength > size) { |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 438 | cper_print_log( |
| 439 | "Invalid CPER file: Invalid header (incorrect signature).\n"); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 440 | return NULL; |
| 441 | } |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 442 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 443 | //Parse section to IR based on GUID. |
| 444 | json_object *result = NULL; |
Ed Tanous | b07061a | 2024-09-22 10:33:29 -0700 | [diff] [blame] | 445 | json_object *section_ir = NULL; |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame] | 446 | |
Ed Tanous | 1a64856 | 2025-03-10 15:23:38 -0700 | [diff] [blame] | 447 | CPER_SECTION_DEFINITION *section = |
| 448 | select_section_by_guid(&descriptor->SectionType); |
Ed Tanous | d6b6263 | 2025-03-14 15:30:07 -0700 | [diff] [blame^] | 449 | if (section == NULL) { |
| 450 | cper_print_log("Unknown section type guid\n"); |
| 451 | } else { |
Ed Tanous | 1a64856 | 2025-03-10 15:23:38 -0700 | [diff] [blame] | 452 | result = read_section(cper_section_buf, size, section); |
Ed Tanous | d759a18 | 2025-03-07 15:51:45 -0800 | [diff] [blame] | 453 | } |
Ed Tanous | b07061a | 2024-09-22 10:33:29 -0700 | [diff] [blame] | 454 | |
Lawrence Tang | 580423f | 2022-08-24 09:37:53 +0100 | [diff] [blame] | 455 | //Was it an unknown GUID/failed read? |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame] | 456 | if (result == NULL) { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 457 | //Output the data as formatted base64. |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 458 | int32_t encoded_len = 0; |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 459 | char *encoded = base64_encode(cper_section_buf, |
| 460 | descriptor->SectionLength, |
| 461 | &encoded_len); |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 462 | if (encoded == NULL) { |
Ed Tanous | d6b6263 | 2025-03-14 15:30:07 -0700 | [diff] [blame^] | 463 | cper_print_log( |
| 464 | "Failed to allocate encode output buffer. \n"); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 465 | } else { |
Ed Tanous | b07061a | 2024-09-22 10:33:29 -0700 | [diff] [blame] | 466 | section_ir = json_object_new_object(); |
| 467 | json_object_object_add(section_ir, "data", |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 468 | json_object_new_string_len( |
| 469 | encoded, encoded_len)); |
| 470 | free(encoded); |
Ed Tanous | b07061a | 2024-09-22 10:33:29 -0700 | [diff] [blame] | 471 | |
| 472 | result = json_object_new_object(); |
| 473 | json_object_object_add(result, "Unknown", section_ir); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 474 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 475 | } |
Ed Tanous | d6b6263 | 2025-03-14 15:30:07 -0700 | [diff] [blame^] | 476 | if (result == NULL) { |
| 477 | cper_print_log("RETURNING NULL!! !!\n"); |
| 478 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 479 | return result; |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 480 | } |
| 481 | |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 482 | json_object *cper_buf_single_section_to_ir(const unsigned char *cper_buf, |
| 483 | size_t size) |
Ed Tanous | 8d47a37 | 2025-03-05 15:55:36 -0800 | [diff] [blame] | 484 | { |
Ed Tanous | 8121f7e | 2025-03-06 14:39:07 -0800 | [diff] [blame] | 485 | const unsigned char *cper_end; |
| 486 | const unsigned char *section_begin; |
| 487 | json_object *ir; |
| 488 | |
| 489 | cper_end = cper_buf + size; |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 490 | |
| 491 | //Read the section descriptor out. |
| 492 | EFI_ERROR_SECTION_DESCRIPTOR *section_descriptor; |
| 493 | if (sizeof(EFI_ERROR_SECTION_DESCRIPTOR) > size) { |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 494 | cper_print_log( |
| 495 | "Size of cper buffer was too small to read section descriptor %zu\n", |
| 496 | size); |
Ed Tanous | 8d47a37 | 2025-03-05 15:55:36 -0800 | [diff] [blame] | 497 | return NULL; |
| 498 | } |
Ed Tanous | 8121f7e | 2025-03-06 14:39:07 -0800 | [diff] [blame] | 499 | |
| 500 | ir = json_object_new_object(); |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 501 | section_descriptor = (EFI_ERROR_SECTION_DESCRIPTOR *)cper_buf; |
| 502 | //Convert the section descriptor to IR. |
| 503 | json_object *section_descriptor_ir = |
| 504 | cper_section_descriptor_to_ir(section_descriptor); |
| 505 | json_object_object_add(ir, "sectionDescriptor", section_descriptor_ir); |
Ed Tanous | 8121f7e | 2025-03-06 14:39:07 -0800 | [diff] [blame] | 506 | section_begin = cper_buf + section_descriptor->SectionOffset; |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 507 | |
Ed Tanous | 8121f7e | 2025-03-06 14:39:07 -0800 | [diff] [blame] | 508 | if (section_begin + section_descriptor->SectionLength >= cper_end) { |
| 509 | json_object_put(ir); |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 510 | //cper_print_log("Invalid CPER file: Invalid section descriptor (section offset + length > size).\n"); |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 511 | return NULL; |
| 512 | } |
| 513 | |
| 514 | const unsigned char *section = |
| 515 | cper_buf + section_descriptor->SectionOffset; |
| 516 | |
| 517 | //Parse the single section. |
| 518 | json_object *section_ir = cper_buf_section_to_ir( |
| 519 | section, section_descriptor->SectionLength, section_descriptor); |
Ed Tanous | d6b6263 | 2025-03-14 15:30:07 -0700 | [diff] [blame^] | 520 | if (section_ir == NULL) { |
| 521 | cper_print_log("RETURNING NULL2!! !!\n"); |
| 522 | } |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 523 | json_object_object_add(ir, "section", section_ir); |
Ed Tanous | 8d47a37 | 2025-03-05 15:55:36 -0800 | [diff] [blame] | 524 | return ir; |
| 525 | } |
| 526 | |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 527 | //Converts a single CPER section, without a header but with a section descriptor, to JSON. |
| 528 | json_object *cper_single_section_to_ir(FILE *cper_section_file) |
| 529 | { |
| 530 | json_object *ir = json_object_new_object(); |
| 531 | |
Lawrence Tang | 9415349 | 2022-09-05 13:07:54 +0100 | [diff] [blame] | 532 | //Read the current file pointer location as base record position. |
| 533 | long base_pos = ftell(cper_section_file); |
| 534 | |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 535 | //Read the section descriptor out. |
| 536 | EFI_ERROR_SECTION_DESCRIPTOR section_descriptor; |
| 537 | if (fread(§ion_descriptor, sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1, |
| 538 | cper_section_file) != 1) { |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 539 | cper_print_log( |
| 540 | "Failed to read section descriptor for CPER single section (fread() returned an unexpected value).\n"); |
Ed Tanous | 8121f7e | 2025-03-06 14:39:07 -0800 | [diff] [blame] | 541 | json_object_put(ir); |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 542 | return NULL; |
| 543 | } |
| 544 | |
| 545 | //Convert the section descriptor to IR. |
| 546 | json_object *section_descriptor_ir = |
| 547 | cper_section_descriptor_to_ir(§ion_descriptor); |
| 548 | json_object_object_add(ir, "sectionDescriptor", section_descriptor_ir); |
| 549 | |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 550 | //Save our current position in the stream. |
| 551 | long position = ftell(cper_section_file); |
| 552 | |
| 553 | //Read section as described by the section descriptor. |
| 554 | fseek(cper_section_file, base_pos + section_descriptor.SectionOffset, |
| 555 | SEEK_SET); |
| 556 | void *section = malloc(section_descriptor.SectionLength); |
| 557 | if (fread(section, section_descriptor.SectionLength, 1, |
| 558 | cper_section_file) != 1) { |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 559 | cper_print_log( |
| 560 | "Section read failed: Could not read %u bytes from global offset %d.\n", |
| 561 | section_descriptor.SectionLength, |
| 562 | section_descriptor.SectionOffset); |
Ed Tanous | 8121f7e | 2025-03-06 14:39:07 -0800 | [diff] [blame] | 563 | json_object_put(ir); |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 564 | free(section); |
| 565 | return NULL; |
| 566 | } |
| 567 | |
| 568 | //Seek back to our original position. |
| 569 | fseek(cper_section_file, position, SEEK_SET); |
| 570 | |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 571 | //Parse the single section. |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 572 | json_object *section_ir = cper_buf_section_to_ir( |
| 573 | section, section_descriptor.SectionLength, §ion_descriptor); |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 574 | json_object_object_add(ir, "section", section_ir); |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 575 | free(section); |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 576 | return ir; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 577 | } |
Karthik Rajagopalan | 5220c9b | 2024-08-08 00:24:44 -0700 | [diff] [blame] | 578 | |
Karthik Rajagopalan | 5220c9b | 2024-08-08 00:24:44 -0700 | [diff] [blame] | 579 | char *cperbuf_single_section_to_str_ir(const unsigned char *cper_section, |
| 580 | size_t size) |
| 581 | { |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 582 | json_object *jobj = cper_buf_single_section_to_ir(cper_section, size); |
| 583 | char *str = jobj ? strdup(json_object_to_json_string(jobj)) : NULL; |
Karthik Rajagopalan | 5220c9b | 2024-08-08 00:24:44 -0700 | [diff] [blame] | 584 | |
Ed Tanous | 73498f6 | 2025-03-05 18:03:36 -0800 | [diff] [blame] | 585 | json_object_put(jobj); |
| 586 | return str; |
Karthik Rajagopalan | 5220c9b | 2024-08-08 00:24:44 -0700 | [diff] [blame] | 587 | } |