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