Karthik Rajagopalan | 683e055 | 2024-03-07 12:30:43 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Describes functions for converting NVIDIA CPER sections from binary and JSON format |
| 3 | * into an intermediate format. |
| 4 | **/ |
| 5 | |
| 6 | #include <stdio.h> |
Ed Tanous | 2d17ace | 2024-08-27 14:45:38 -0700 | [diff] [blame] | 7 | #include <stddef.h> |
Karthik Rajagopalan | 683e055 | 2024-03-07 12:30:43 -0800 | [diff] [blame] | 8 | #include <string.h> |
| 9 | #include <json.h> |
Ed Tanous | a3b7f8a | 2024-11-04 16:38:59 -0800 | [diff] [blame^] | 10 | #include "Cper.h" |
Karthik Rajagopalan | 683e055 | 2024-03-07 12:30:43 -0800 | [diff] [blame] | 11 | #include "../cper-utils.h" |
| 12 | #include "cper-section-nvidia.h" |
| 13 | |
| 14 | //Converts a single NVIDIA CPER section into JSON IR. |
| 15 | json_object *cper_section_nvidia_to_ir(void *section) |
| 16 | { |
| 17 | EFI_NVIDIA_ERROR_DATA *nvidia_error = (EFI_NVIDIA_ERROR_DATA *)section; |
| 18 | json_object *section_ir = json_object_new_object(); |
| 19 | |
Karthik Rajagopalan | 683e055 | 2024-03-07 12:30:43 -0800 | [diff] [blame] | 20 | json_object_object_add(section_ir, "signature", |
| 21 | json_object_new_string(nvidia_error->Signature)); |
| 22 | |
Ed Tanous | 2d17ace | 2024-08-27 14:45:38 -0700 | [diff] [blame] | 23 | json_object *severity = json_object_new_object(); |
| 24 | json_object_object_add(severity, "code", |
| 25 | json_object_new_uint64(nvidia_error->Severity)); |
| 26 | json_object_object_add(severity, "name", |
| 27 | json_object_new_string(severity_to_string( |
| 28 | nvidia_error->Severity))); |
| 29 | json_object_object_add(section_ir, "severity", severity); |
| 30 | |
Karthik Rajagopalan | 683e055 | 2024-03-07 12:30:43 -0800 | [diff] [blame] | 31 | json_object_object_add(section_ir, "errorType", |
| 32 | json_object_new_int(nvidia_error->ErrorType)); |
| 33 | json_object_object_add( |
| 34 | section_ir, "errorInstance", |
| 35 | json_object_new_int(nvidia_error->ErrorInstance)); |
Karthik Rajagopalan | 683e055 | 2024-03-07 12:30:43 -0800 | [diff] [blame] | 36 | json_object_object_add(section_ir, "socket", |
| 37 | json_object_new_int(nvidia_error->Socket)); |
Ed Tanous | 2d17ace | 2024-08-27 14:45:38 -0700 | [diff] [blame] | 38 | json_object_object_add(section_ir, "registerCount", |
Karthik Rajagopalan | 683e055 | 2024-03-07 12:30:43 -0800 | [diff] [blame] | 39 | json_object_new_int(nvidia_error->NumberRegs)); |
| 40 | json_object_object_add( |
| 41 | section_ir, "instanceBase", |
| 42 | json_object_new_uint64(nvidia_error->InstanceBase)); |
| 43 | |
| 44 | // Registers (Address Value pairs). |
| 45 | json_object *regarr = json_object_new_array(); |
Ed Tanous | 2d17ace | 2024-08-27 14:45:38 -0700 | [diff] [blame] | 46 | EFI_NVIDIA_REGISTER_DATA *regPtr = nvidia_error->Register; |
| 47 | for (int i = 0; i < nvidia_error->NumberRegs; i++, regPtr++) { |
Karthik Rajagopalan | 683e055 | 2024-03-07 12:30:43 -0800 | [diff] [blame] | 48 | json_object *reg = json_object_new_object(); |
| 49 | json_object_object_add(reg, "address", |
Ed Tanous | 2d17ace | 2024-08-27 14:45:38 -0700 | [diff] [blame] | 50 | json_object_new_uint64(regPtr->Address)); |
Karthik Rajagopalan | 683e055 | 2024-03-07 12:30:43 -0800 | [diff] [blame] | 51 | json_object_object_add(reg, "value", |
Ed Tanous | 2d17ace | 2024-08-27 14:45:38 -0700 | [diff] [blame] | 52 | json_object_new_uint64(regPtr->Value)); |
Karthik Rajagopalan | 683e055 | 2024-03-07 12:30:43 -0800 | [diff] [blame] | 53 | json_object_array_add(regarr, reg); |
| 54 | } |
| 55 | json_object_object_add(section_ir, "registers", regarr); |
| 56 | |
| 57 | return section_ir; |
| 58 | } |
| 59 | |
| 60 | //Converts a single NVIDIA CPER-JSON section into CPER binary, outputting to the given stream. |
| 61 | void ir_section_nvidia_to_cper(json_object *section, FILE *out) |
| 62 | { |
| 63 | json_object *regarr = json_object_object_get(section, "registers"); |
| 64 | int numRegs = json_object_array_length(regarr); |
| 65 | |
Ed Tanous | 2d17ace | 2024-08-27 14:45:38 -0700 | [diff] [blame] | 66 | size_t section_sz = offsetof(EFI_NVIDIA_ERROR_DATA, Register) + |
| 67 | numRegs * sizeof(EFI_NVIDIA_REGISTER_DATA); |
Karthik Rajagopalan | 683e055 | 2024-03-07 12:30:43 -0800 | [diff] [blame] | 68 | EFI_NVIDIA_ERROR_DATA *section_cper = |
| 69 | (EFI_NVIDIA_ERROR_DATA *)calloc(1, section_sz); |
| 70 | |
| 71 | //Signature. |
| 72 | strncpy(section_cper->Signature, |
| 73 | json_object_get_string( |
| 74 | json_object_object_get(section, "signature")), |
Patrick Williams | 379e492 | 2024-08-28 11:14:34 -0400 | [diff] [blame] | 75 | sizeof(section_cper->Signature) - 1); |
| 76 | section_cper->Signature[sizeof(section_cper->Signature) - 1] = '\0'; |
Karthik Rajagopalan | 683e055 | 2024-03-07 12:30:43 -0800 | [diff] [blame] | 77 | |
| 78 | //Fields. |
| 79 | section_cper->ErrorType = json_object_get_int( |
| 80 | json_object_object_get(section, "errorType")); |
| 81 | section_cper->ErrorInstance = json_object_get_int( |
| 82 | json_object_object_get(section, "errorInstance")); |
Ed Tanous | 2d17ace | 2024-08-27 14:45:38 -0700 | [diff] [blame] | 83 | json_object *severity = json_object_object_get(section, "severity"); |
| 84 | section_cper->Severity = (UINT8)json_object_get_uint64( |
| 85 | json_object_object_get(severity, "code")); |
Karthik Rajagopalan | 683e055 | 2024-03-07 12:30:43 -0800 | [diff] [blame] | 86 | section_cper->Socket = |
| 87 | json_object_get_int(json_object_object_get(section, "socket")); |
| 88 | section_cper->NumberRegs = json_object_get_int( |
Ed Tanous | 2d17ace | 2024-08-27 14:45:38 -0700 | [diff] [blame] | 89 | json_object_object_get(section, "registerCount")); |
Karthik Rajagopalan | 683e055 | 2024-03-07 12:30:43 -0800 | [diff] [blame] | 90 | section_cper->InstanceBase = json_object_get_uint64( |
| 91 | json_object_object_get(section, "instanceBase")); |
| 92 | |
| 93 | // Registers (Address Value pairs). |
Ed Tanous | 2d17ace | 2024-08-27 14:45:38 -0700 | [diff] [blame] | 94 | EFI_NVIDIA_REGISTER_DATA *regPtr = section_cper->Register; |
| 95 | for (int i = 0; i < numRegs; i++, regPtr++) { |
Karthik Rajagopalan | 683e055 | 2024-03-07 12:30:43 -0800 | [diff] [blame] | 96 | json_object *reg = json_object_array_get_idx(regarr, i); |
Ed Tanous | 2d17ace | 2024-08-27 14:45:38 -0700 | [diff] [blame] | 97 | regPtr->Address = json_object_get_uint64( |
Karthik Rajagopalan | 683e055 | 2024-03-07 12:30:43 -0800 | [diff] [blame] | 98 | json_object_object_get(reg, "address")); |
Ed Tanous | 2d17ace | 2024-08-27 14:45:38 -0700 | [diff] [blame] | 99 | regPtr->Value = json_object_get_uint64( |
Karthik Rajagopalan | 683e055 | 2024-03-07 12:30:43 -0800 | [diff] [blame] | 100 | json_object_object_get(reg, "value")); |
| 101 | } |
| 102 | |
| 103 | //Write to stream, free resources. |
| 104 | fwrite(section_cper, section_sz, 1, out); |
| 105 | fflush(out); |
| 106 | free(section_cper); |
| 107 | } |