xref: /openbmc/libcper/sections/cper-section-nvidia.c (revision 6c5d2f34188e568d8fb59d0666f3359c8e5197f8)
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 			add_uint(reg, "address", regPtr->Address);
87 			add_uint(reg, "value", regPtr->Value);
88 		} else {
89 			reg = json_object_new_null();
90 		}
91 
92 		json_object_array_add(regarr, reg);
93 	}
94 	json_object_object_add(section_ir, "registers", regarr);
95 }
96 
97 typedef struct {
98 	const char *ip_signature;
99 	void (*callback)(EFI_NVIDIA_REGISTER_DATA *, UINT8, size_t,
100 			 json_object *);
101 } NV_SECTION_CALLBACKS;
102 
103 NV_SECTION_CALLBACKS section_handlers[] = {
104 	{ "CMET-INFO\0", &parse_cmet_info },
105 	{ "FWERROR\0", &parse_fwerror },
106 	{ "", &parse_registers },
107 };
108 
get_index(const char * signature)109 int get_index(const char *signature)
110 {
111 	if (signature == NULL) {
112 		cper_print_log("Error: NULL signature\n");
113 		return -1;
114 	}
115 	int index = -1;
116 	// we're comparing i with section_handlers type
117 	size_t i = 0;
118 	for (i = 0; i < sizeof(section_handlers) / sizeof(section_handlers[0]);
119 	     i++) {
120 		const char *ip_signature = section_handlers[i].ip_signature;
121 		if (strncmp(signature, ip_signature, strlen(ip_signature)) ==
122 		    0) {
123 			// i is small so we won't overflow
124 			return (int)i;
125 		}
126 	}
127 // if no match was found, and fuzzing is enabled, pick one to get coverage
128 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
129 	index = (unsigned char)signature[0] %
130 		(sizeof(section_handlers) / sizeof(section_handlers[0]));
131 #endif
132 	return index;
133 }
134 
135 //Converts a single NVIDIA CPER section into JSON IR.
cper_section_nvidia_to_ir(const UINT8 * section,UINT32 size,char ** desc_string)136 json_object *cper_section_nvidia_to_ir(const UINT8 *section, UINT32 size,
137 				       char **desc_string)
138 {
139 	*desc_string = NULL;
140 	if (size < sizeof(EFI_NVIDIA_ERROR_DATA)) {
141 		cper_print_log("Error: NVIDIA section too small\n");
142 		return NULL;
143 	}
144 
145 	*desc_string = calloc(1, SECTION_DESC_STRING_SIZE);
146 	if (*desc_string == NULL) {
147 		cper_print_log(
148 			"Error: Failed to allocate NVIDIA desc string\n");
149 		return NULL;
150 	}
151 
152 	char *property_desc = calloc(1, EFI_ERROR_DESCRIPTION_STRING_LEN);
153 	if (property_desc == NULL) {
154 		free(*desc_string);
155 		*desc_string = NULL;
156 		cper_print_log(
157 			"Error: Failed to allocate NVIDIA property desc\n");
158 		return NULL;
159 	}
160 
161 	EFI_NVIDIA_ERROR_DATA *nvidia_error = (EFI_NVIDIA_ERROR_DATA *)section;
162 
163 	json_object *section_ir = json_object_new_object();
164 
165 	add_untrusted_string(section_ir, "signature", nvidia_error->Signature,
166 			     sizeof(nvidia_error->Signature));
167 
168 	json_object *severity = json_object_new_object();
169 	add_uint(severity, "code", nvidia_error->Severity);
170 	const char *severity_name = severity_to_string(nvidia_error->Severity);
171 	add_string(severity, "name", severity_name);
172 	int outstr_len = 0;
173 	char *signature = nvidia_error->Signature;
174 	int sig_len = cper_printable_string_length(
175 		nvidia_error->Signature, sizeof(nvidia_error->Signature));
176 	if (sig_len <= 0) {
177 		signature = "";
178 		sig_len = 0;
179 	}
180 
181 	outstr_len = snprintf(*desc_string, SECTION_DESC_STRING_SIZE,
182 			      "A %s %.*s NVIDIA Error occurred", severity_name,
183 			      sig_len, signature);
184 	if (outstr_len < 0) {
185 		cper_print_log(
186 			"Error: Could not write to description string\n");
187 	} else if (outstr_len > SECTION_DESC_STRING_SIZE) {
188 		cper_print_log("Error: Description string truncated: %s\n",
189 			       *desc_string);
190 	}
191 	json_object_object_add(section_ir, "severity", severity);
192 
193 	add_int(section_ir, "errorType", nvidia_error->ErrorType);
194 	add_int(section_ir, "errorInstance", nvidia_error->ErrorInstance);
195 	add_int(section_ir, "socket", nvidia_error->Socket);
196 
197 	outstr_len = snprintf(property_desc, EFI_ERROR_DESCRIPTION_STRING_LEN,
198 			      " on CPU %d", nvidia_error->Socket);
199 	if (outstr_len < 0) {
200 		cper_print_log("Error: Could not write to property string\n");
201 	} else if (outstr_len > EFI_ERROR_DESCRIPTION_STRING_LEN) {
202 		cper_print_log("Error: Property string truncated: %s\n",
203 			       property_desc);
204 	}
205 
206 	int property_desc_len = strlen(property_desc);
207 	strncat(*desc_string, property_desc,
208 		SECTION_DESC_STRING_SIZE - strlen(*desc_string) - 1);
209 	// We still want to get as much info as possible, just warn about truncation
210 	if (property_desc_len + strlen(*desc_string) >=
211 	    SECTION_DESC_STRING_SIZE) {
212 		cper_print_log("Error: Description string truncated: %s\n",
213 			       *desc_string);
214 	}
215 	free(property_desc);
216 
217 	add_int(section_ir, "registerCount", nvidia_error->NumberRegs);
218 	add_uint(section_ir, "instanceBase", nvidia_error->InstanceBase);
219 	int index = get_index(nvidia_error->Signature);
220 	if (index == -1) {
221 		cper_print_log("Error: Unknown NVIDIA section signature: %s\n",
222 			       nvidia_error->Signature);
223 		return NULL;
224 	}
225 	section_handlers[index].callback(&nvidia_error->Register[0],
226 					 nvidia_error->NumberRegs, size,
227 					 section_ir);
228 
229 	return section_ir;
230 }
231 
232 //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)233 void ir_section_nvidia_to_cper(json_object *section, FILE *out)
234 {
235 	json_object *regarr = json_object_object_get(section, "registers");
236 	int numRegs = json_object_array_length(regarr);
237 
238 	size_t section_sz = offsetof(EFI_NVIDIA_ERROR_DATA, Register) +
239 			    numRegs * sizeof(EFI_NVIDIA_REGISTER_DATA);
240 	EFI_NVIDIA_ERROR_DATA *section_cper =
241 		(EFI_NVIDIA_ERROR_DATA *)calloc(1, section_sz);
242 
243 	//Signature.
244 	strncpy(section_cper->Signature,
245 		json_object_get_string(
246 			json_object_object_get(section, "signature")),
247 		sizeof(section_cper->Signature) - 1);
248 	section_cper->Signature[sizeof(section_cper->Signature) - 1] = '\0';
249 
250 	//Fields.
251 	section_cper->ErrorType = json_object_get_int(
252 		json_object_object_get(section, "errorType"));
253 	section_cper->ErrorInstance = json_object_get_int(
254 		json_object_object_get(section, "errorInstance"));
255 	json_object *severity = json_object_object_get(section, "severity");
256 	section_cper->Severity = (UINT8)json_object_get_uint64(
257 		json_object_object_get(severity, "code"));
258 	section_cper->Socket =
259 		json_object_get_int(json_object_object_get(section, "socket"));
260 	section_cper->NumberRegs = json_object_get_int(
261 		json_object_object_get(section, "registerCount"));
262 	section_cper->InstanceBase = json_object_get_uint64(
263 		json_object_object_get(section, "instanceBase"));
264 
265 	// Registers (Address Value pairs).
266 	EFI_NVIDIA_REGISTER_DATA *regPtr = section_cper->Register;
267 	for (int i = 0; i < numRegs; i++, regPtr++) {
268 		json_object *reg = json_object_array_get_idx(regarr, i);
269 		regPtr->Address = json_object_get_uint64(
270 			json_object_object_get(reg, "address"));
271 		regPtr->Value = json_object_get_uint64(
272 			json_object_object_get(reg, "value"));
273 	}
274 
275 	//Write to stream, free resources.
276 	fwrite(section_cper, section_sz, 1, out);
277 	fflush(out);
278 	free(section_cper);
279 }
280