xref: /openbmc/libcper/sections/cper-section-nvidia.c (revision 680875cbca6901c5de3aa0c9f5443b9c5790efb2)
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>
7 #include <stddef.h>
8 #include <string.h>
9 #include <json.h>
10 #include <libcper/Cper.h>
11 #include <libcper/cper-utils.h>
12 #include <libcper/sections/cper-section-nvidia.h>
13 #include <libcper/log.h>
14 #include <string.h>
15 
parse_cmet_info(EFI_NVIDIA_REGISTER_DATA * regPtr,UINT8 NumberRegs,size_t size,json_object * section_ir)16 void parse_cmet_info(EFI_NVIDIA_REGISTER_DATA *regPtr, UINT8 NumberRegs,
17 		     size_t size, json_object *section_ir)
18 {
19 	json_object *regarr = json_object_new_array();
20 	for (int i = 0; i < NumberRegs; i++, regPtr++) {
21 		json_object *reg = NULL;
22 		if (sizeof(EFI_NVIDIA_ERROR_DATA) +
23 			    (i + 1) * sizeof(EFI_NVIDIA_REGISTER_DATA) <=
24 		    size) {
25 			reg = json_object_new_object();
26 			add_int_hex_64(reg, "ChannelAddress", regPtr->Address);
27 			add_int(reg, "ErrorCount", regPtr->CmetInfo.ErrorCount);
28 			add_bool(reg, "ChannelEnabled",
29 				 regPtr->CmetInfo.ChannelEnabled);
30 			add_bool(reg, "ChannelIsSpare",
31 				 regPtr->CmetInfo.ChannelIsSpare);
32 			add_dict(reg, "DisabledReason",
33 				 regPtr->CmetInfo.DisabledReason,
34 				 channel_disable_reason_dict,
35 				 channel_disable_reason_dict_size);
36 		} else {
37 			reg = json_object_new_null();
38 		}
39 
40 		json_object_array_add(regarr, reg);
41 	}
42 
43 	json_object_object_add(section_ir, "CMETInfo", regarr);
44 }
45 
parse_fwerror(EFI_NVIDIA_REGISTER_DATA * regPtr,UINT8 NumberRegs,size_t size,json_object * section_ir)46 void parse_fwerror(EFI_NVIDIA_REGISTER_DATA *regPtr, UINT8 NumberRegs,
47 		   size_t size, json_object *section_ir)
48 {
49 	(void)NumberRegs;
50 	json_object *fwinfo;
51 	if (sizeof(EFI_NVIDIA_ERROR_DATA) + sizeof(EFI_NVIDIA_FWERROR) > size) {
52 		fwinfo = json_object_new_null();
53 	} else {
54 		fwinfo = json_object_new_object();
55 		EFI_NVIDIA_FWERROR *fwerror = (EFI_NVIDIA_FWERROR *)regPtr;
56 		add_untrusted_string(fwinfo, "initiating_firmware",
57 				     fwerror->initiating_firmware,
58 				     sizeof(fwerror->initiating_firmware));
59 		add_int_hex_64(fwinfo, "task_checkpoint",
60 			       fwerror->task_checkpoint);
61 		add_int_hex_64(fwinfo, "mb1_error_code",
62 			       fwerror->mb1_error_code);
63 		add_untrusted_string(fwinfo, "mb1_version_string",
64 				     fwerror->mb1_version_string,
65 				     sizeof(fwerror->mb1_version_string));
66 		add_int_hex_64(fwinfo, "bad_pages_retired_mask",
67 			       fwerror->bad_pages_retired_mask);
68 		add_int_hex_64(fwinfo, "training_or_alias_check_retired_mask",
69 			       fwerror->training_or_alias_check_retired_mask);
70 	}
71 
72 	json_object_object_add(section_ir, "FWErrorInfo", fwinfo);
73 }
74 
parse_registers(EFI_NVIDIA_REGISTER_DATA * regPtr,UINT8 NumberRegs,size_t size,json_object * section_ir)75 void parse_registers(EFI_NVIDIA_REGISTER_DATA *regPtr, UINT8 NumberRegs,
76 		     size_t size, json_object *section_ir)
77 {
78 	// Registers (Address Value pairs).
79 	json_object *regarr = json_object_new_array();
80 	for (int i = 0; i < NumberRegs; i++, regPtr++) {
81 		json_object *reg = NULL;
82 		if (sizeof(EFI_NVIDIA_ERROR_DATA) +
83 			    (i + 1) * sizeof(EFI_NVIDIA_REGISTER_DATA) <=
84 		    size) {
85 			reg = json_object_new_object();
86 			json_object_object_add(
87 				reg, "address",
88 				json_object_new_uint64(regPtr->Address));
89 			json_object_object_add(
90 				reg, "value",
91 				json_object_new_uint64(regPtr->Value));
92 		} else {
93 			reg = json_object_new_null();
94 		}
95 
96 		json_object_array_add(regarr, reg);
97 	}
98 	json_object_object_add(section_ir, "registers", regarr);
99 }
100 
101 typedef struct {
102 	const char *ip_signature;
103 	void (*callback)(EFI_NVIDIA_REGISTER_DATA *, UINT8, size_t,
104 			 json_object *);
105 } NV_SECTION_CALLBACKS;
106 
107 NV_SECTION_CALLBACKS section_handlers[] = {
108 	{ "CMET-INFO\0", &parse_cmet_info },
109 	{ "FWERROR\0", &parse_fwerror },
110 	{ "", &parse_registers },
111 };
112 
113 //Converts a single NVIDIA CPER section into JSON IR.
cper_section_nvidia_to_ir(const UINT8 * section,UINT32 size,char ** desc_string)114 json_object *cper_section_nvidia_to_ir(const UINT8 *section, UINT32 size,
115 				       char **desc_string)
116 {
117 	*desc_string = NULL;
118 	if (size < sizeof(EFI_NVIDIA_ERROR_DATA)) {
119 		cper_print_log("Error: NVIDIA section too small\n");
120 		return NULL;
121 	}
122 
123 	*desc_string = calloc(1, SECTION_DESC_STRING_SIZE);
124 	if (*desc_string == NULL) {
125 		cper_print_log(
126 			"Error: Failed to allocate NVIDIA desc string\n");
127 		return NULL;
128 	}
129 
130 	char *property_desc = calloc(1, EFI_ERROR_DESCRIPTION_STRING_LEN);
131 	if (property_desc == NULL) {
132 		free(*desc_string);
133 		*desc_string = NULL;
134 		cper_print_log(
135 			"Error: Failed to allocate NVIDIA property desc\n");
136 		return NULL;
137 	}
138 
139 	EFI_NVIDIA_ERROR_DATA *nvidia_error = (EFI_NVIDIA_ERROR_DATA *)section;
140 
141 	json_object *section_ir = json_object_new_object();
142 
143 	const char *signature = nvidia_error->Signature;
144 	add_untrusted_string(section_ir, "signature", signature,
145 			     strlen(signature));
146 
147 	json_object *severity = json_object_new_object();
148 	json_object_object_add(severity, "code",
149 			       json_object_new_uint64(nvidia_error->Severity));
150 	const char *severity_name = severity_to_string(nvidia_error->Severity);
151 	json_object_object_add(severity, "name",
152 			       json_object_new_string(severity_name));
153 	int outstr_len = 0;
154 	outstr_len = snprintf(*desc_string, SECTION_DESC_STRING_SIZE,
155 			      "A %s %s NVIDIA Error occurred", severity_name,
156 			      signature);
157 	if (outstr_len < 0) {
158 		cper_print_log(
159 			"Error: Could not write to description string\n");
160 	} else if (outstr_len > SECTION_DESC_STRING_SIZE) {
161 		cper_print_log("Error: Description string truncated: %s\n",
162 			       *desc_string);
163 	}
164 	json_object_object_add(section_ir, "severity", severity);
165 
166 	json_object_object_add(section_ir, "errorType",
167 			       json_object_new_int(nvidia_error->ErrorType));
168 	json_object_object_add(
169 		section_ir, "errorInstance",
170 		json_object_new_int(nvidia_error->ErrorInstance));
171 	json_object_object_add(section_ir, "socket",
172 			       json_object_new_int(nvidia_error->Socket));
173 
174 	outstr_len = snprintf(property_desc, EFI_ERROR_DESCRIPTION_STRING_LEN,
175 			      " on CPU %d", nvidia_error->Socket);
176 	if (outstr_len < 0) {
177 		cper_print_log("Error: Could not write to property string\n");
178 	} else if (outstr_len > EFI_ERROR_DESCRIPTION_STRING_LEN) {
179 		cper_print_log("Error: Property string truncated: %s\n",
180 			       property_desc);
181 	}
182 
183 	int property_desc_len = strlen(property_desc);
184 	strncat(*desc_string, property_desc,
185 		SECTION_DESC_STRING_SIZE - strlen(*desc_string) - 1);
186 	// We still want to get as much info as possible, just warn about truncation
187 	if (property_desc_len + strlen(*desc_string) >=
188 	    SECTION_DESC_STRING_SIZE) {
189 		cper_print_log("Error: Description string truncated: %s\n",
190 			       *desc_string);
191 	}
192 	free(property_desc);
193 
194 	json_object_object_add(section_ir, "registerCount",
195 			       json_object_new_int(nvidia_error->NumberRegs));
196 	json_object_object_add(
197 		section_ir, "instanceBase",
198 		json_object_new_uint64(nvidia_error->InstanceBase));
199 
200 	for (long unsigned int i = 0;
201 	     i < sizeof(section_handlers) / sizeof(section_handlers[0]); i++) {
202 		const char *ip_signature = section_handlers[i].ip_signature;
203 		if (strncmp(nvidia_error->Signature, ip_signature,
204 			    strlen(ip_signature)) == 0) {
205 			section_handlers[i].callback(&nvidia_error->Register[0],
206 						     nvidia_error->NumberRegs,
207 						     size, section_ir);
208 			break;
209 		}
210 	}
211 	return section_ir;
212 }
213 
214 //Converts a single NVIDIA CPER-JSON section into CPER binary, outputting to the given stream.
ir_section_nvidia_to_cper(json_object * section,FILE * out)215 void ir_section_nvidia_to_cper(json_object *section, FILE *out)
216 {
217 	json_object *regarr = json_object_object_get(section, "registers");
218 	int numRegs = json_object_array_length(regarr);
219 
220 	size_t section_sz = offsetof(EFI_NVIDIA_ERROR_DATA, Register) +
221 			    numRegs * sizeof(EFI_NVIDIA_REGISTER_DATA);
222 	EFI_NVIDIA_ERROR_DATA *section_cper =
223 		(EFI_NVIDIA_ERROR_DATA *)calloc(1, section_sz);
224 
225 	//Signature.
226 	strncpy(section_cper->Signature,
227 		json_object_get_string(
228 			json_object_object_get(section, "signature")),
229 		sizeof(section_cper->Signature) - 1);
230 	section_cper->Signature[sizeof(section_cper->Signature) - 1] = '\0';
231 
232 	//Fields.
233 	section_cper->ErrorType = json_object_get_int(
234 		json_object_object_get(section, "errorType"));
235 	section_cper->ErrorInstance = json_object_get_int(
236 		json_object_object_get(section, "errorInstance"));
237 	json_object *severity = json_object_object_get(section, "severity");
238 	section_cper->Severity = (UINT8)json_object_get_uint64(
239 		json_object_object_get(severity, "code"));
240 	section_cper->Socket =
241 		json_object_get_int(json_object_object_get(section, "socket"));
242 	section_cper->NumberRegs = json_object_get_int(
243 		json_object_object_get(section, "registerCount"));
244 	section_cper->InstanceBase = json_object_get_uint64(
245 		json_object_object_get(section, "instanceBase"));
246 
247 	// Registers (Address Value pairs).
248 	EFI_NVIDIA_REGISTER_DATA *regPtr = section_cper->Register;
249 	for (int i = 0; i < numRegs; i++, regPtr++) {
250 		json_object *reg = json_object_array_get_idx(regarr, i);
251 		regPtr->Address = json_object_get_uint64(
252 			json_object_object_get(reg, "address"));
253 		regPtr->Value = json_object_get_uint64(
254 			json_object_object_get(reg, "value"));
255 	}
256 
257 	//Write to stream, free resources.
258 	fwrite(section_cper, section_sz, 1, out);
259 	fflush(out);
260 	free(section_cper);
261 }
262