Dung Cao | 04f5771 | 2024-08-29 08:03:59 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <json.h> |
Thu Nguyen | e42fb48 | 2024-10-15 14:43:11 +0000 | [diff] [blame] | 3 | #include <libcper/Cper.h> |
| 4 | #include <libcper/cper-utils.h> |
| 5 | #include <libcper/sections/cper-section-ampere.h> |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 6 | #include <libcper/log.h> |
Aushim Nagarkatti | ad6c880 | 2025-06-18 16:45:28 -0700 | [diff] [blame^] | 7 | #include <string.h> |
Dung Cao | 04f5771 | 2024-08-29 08:03:59 +0000 | [diff] [blame] | 8 | |
| 9 | //Converts the given processor-generic CPER section into JSON IR. |
Aushim Nagarkatti | ad6c880 | 2025-06-18 16:45:28 -0700 | [diff] [blame^] | 10 | json_object *cper_section_ampere_to_ir(const UINT8 *section, UINT32 size, |
| 11 | char **desc_string) |
Dung Cao | 04f5771 | 2024-08-29 08:03:59 +0000 | [diff] [blame] | 12 | { |
Aushim Nagarkatti | ad6c880 | 2025-06-18 16:45:28 -0700 | [diff] [blame^] | 13 | int outstr_len = 0; |
| 14 | *desc_string = malloc(SECTION_DESC_STRING_SIZE); |
| 15 | outstr_len = snprintf(*desc_string, SECTION_DESC_STRING_SIZE, |
| 16 | "An Ampere Error occurred"); |
| 17 | if (outstr_len < 0) { |
| 18 | cper_print_log( |
| 19 | "Error: Could not write to Ampere description string\n"); |
| 20 | } else if (outstr_len > SECTION_DESC_STRING_SIZE) { |
| 21 | cper_print_log("Error: Ampere description string truncated\n"); |
| 22 | } |
| 23 | |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame] | 24 | if (size < sizeof(EFI_AMPERE_ERROR_DATA)) { |
| 25 | return NULL; |
| 26 | } |
| 27 | |
Dung Cao | 04f5771 | 2024-08-29 08:03:59 +0000 | [diff] [blame] | 28 | EFI_AMPERE_ERROR_DATA *record = (EFI_AMPERE_ERROR_DATA *)section; |
| 29 | json_object *section_ir = json_object_new_object(); |
| 30 | |
| 31 | json_object_object_add(section_ir, "typeId", |
| 32 | json_object_new_int(record->TypeId)); |
| 33 | json_object_object_add(section_ir, "subTypeId", |
| 34 | json_object_new_int(record->SubtypeId)); |
| 35 | json_object_object_add(section_ir, "instanceId", |
| 36 | json_object_new_int(record->InstanceId)); |
| 37 | |
| 38 | return section_ir; |
| 39 | } |
| 40 | |
| 41 | //Converts a single CPER-JSON ARM error section into CPER binary, outputting to the given stream. |
| 42 | void ir_section_ampere_to_cper(json_object *section, FILE *out) |
| 43 | { |
| 44 | EFI_AMPERE_ERROR_DATA *section_cper = (EFI_AMPERE_ERROR_DATA *)calloc( |
| 45 | 1, sizeof(EFI_AMPERE_ERROR_DATA)); |
| 46 | |
| 47 | //Count of error/context info structures. |
| 48 | section_cper->TypeId = |
| 49 | json_object_get_int(json_object_object_get(section, "typeId")); |
| 50 | section_cper->SubtypeId = json_object_get_int( |
| 51 | json_object_object_get(section, "subTypeId")); |
| 52 | section_cper->InstanceId = json_object_get_int( |
| 53 | json_object_object_get(section, "instanceId")); |
| 54 | |
| 55 | //Flush header to stream. |
| 56 | fwrite(section_cper, sizeof(EFI_AMPERE_ERROR_DATA), 1, out); |
| 57 | fflush(out); |
| 58 | free(section_cper); |
| 59 | } |