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