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