xref: /aosp_15_r20/external/coreboot/util/nvramtool/accessors/layout-bin.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <string.h>
4 #ifndef __MINGW32__
5 #include <sys/mman.h>
6 #endif
7 #include "common.h"
8 #include "coreboot_tables.h"
9 #include "ip_checksum.h"
10 #include "lbtable.h"
11 #include "layout.h"
12 #include "cmos_lowlevel.h"
13 #include "hexdump.h"
14 #include "cbfs.h"
15 #include "layout-text.h"
16 
17 static void process_cmos_table(void);
18 static void get_cmos_checksum_info(void);
19 static void try_convert_checksum_layout(cmos_checksum_layout_t * layout);
20 static void try_add_cmos_table_enum(cmos_enum_t * cmos_enum);
21 static void try_add_cmos_table_entry(cmos_entry_t * cmos_entry);
22 static const struct cmos_entries *first_cmos_table_entry(void);
23 static const struct cmos_entries *next_cmos_table_entry(const struct
24 							cmos_entries *last);
25 static const struct cmos_enums *first_cmos_table_enum(void);
26 static const struct cmos_enums *next_cmos_table_enum
27     (const struct cmos_enums *last);
28 static const struct lb_record *first_cmos_rec(uint32_t tag);
29 static const struct lb_record *next_cmos_rec(const struct lb_record *last,
30 					     uint32_t tag);
31 
32 /* The CMOS option table is located within the coreboot table.  It tells us
33  * where the CMOS parameters are located in the nonvolatile RAM.
34  */
35 static const struct cmos_option_table *cmos_table = NULL;
36 
37 #define ROUNDUP4(x)	(x += (4 - (x % 4)))
38 
process_layout(void)39 void process_layout(void)
40 {
41 	if ((cmos_table) == NULL) {
42 		fprintf(stderr,
43 			"%s: CMOS option table not found in coreboot table.  "
44 			"Apparently, the coreboot installed on this system was "
45 			"built without selecting CONFIG_USE_OPTION_TABLE.\n",
46 			prog_name);
47 		exit(1);
48 	}
49 
50 	process_cmos_table();
51 	get_cmos_checksum_info();
52 }
53 
get_layout_from_cbfs_file(void)54 void get_layout_from_cbfs_file(void)
55 {
56 	uint32_t len;
57 	cmos_table = cbfs_find_file("cmos_layout.bin", CBFS_COMPONENT_CMOS_LAYOUT, &len);
58 	process_layout();
59 }
60 
write_cmos_layout_bin(FILE * f)61 static int write_cmos_layout_bin(FILE *f)
62 {
63 	const cmos_entry_t *cmos_entry;
64 	const cmos_enum_t *cmos_enum;
65 	cmos_checksum_layout_t layout;
66 	struct cmos_option_table table;
67 	struct cmos_entries entry;
68 	struct cmos_enums cenum;
69 	struct cmos_checksum csum;
70 	size_t sum = 0;
71 	int len;
72 
73 	for (cmos_entry = first_cmos_entry(); cmos_entry != NULL;
74 			cmos_entry = next_cmos_entry(cmos_entry)) {
75 
76 		if (cmos_entry == first_cmos_entry()) {
77 			sum += sizeof(table);
78 			table.header_length = sizeof(table);
79 			table.tag = LB_TAG_CMOS_OPTION_TABLE;
80 			table.size = 0;
81 
82 			if (fwrite((char *)&table, sizeof(table), 1, f) != 1) {
83 				perror("Error writing image file");
84 				goto err;
85 			}
86 		}
87 
88 		memset(&entry, 0, sizeof(entry));
89 		entry.tag = LB_TAG_OPTION;
90 		entry.config = cmos_entry->config;
91 		entry.config_id = (uint32_t)cmos_entry->config_id;
92 		entry.bit = cmos_entry->bit;
93 		entry.length = cmos_entry->length;
94 
95 		if (!is_ident((char *)cmos_entry->name)) {
96 			fprintf(stderr,
97 				"Error - Name %s is an invalid identifier\n",
98 				cmos_entry->name);
99 			goto err;
100 		}
101 
102 		memcpy(entry.name, cmos_entry->name, strlen(cmos_entry->name));
103 		entry.name[strlen(cmos_entry->name)] = '\0';
104 		len = strlen(cmos_entry->name) + 1;
105 
106 		if (len % 4)
107 			ROUNDUP4(len);
108 
109 		entry.size = sizeof(entry) - CMOS_MAX_NAME_LENGTH + len;
110 		sum += entry.size;
111 		if (fwrite((char *)&entry, entry.size, 1, f) != 1) {
112 			perror("Error writing image file");
113 			goto err;
114 		}
115 	}
116 
117 	for (cmos_enum = first_cmos_enum();
118 			cmos_enum != NULL; cmos_enum = next_cmos_enum(cmos_enum)) {
119 		memset(&cenum, 0, sizeof(cenum));
120 		cenum.tag = LB_TAG_OPTION_ENUM;
121 		memcpy(cenum.text, cmos_enum->text, strlen(cmos_enum->text));
122 		cenum.text[strlen(cmos_enum->text)] = '\0';
123 		len = strlen((char *)cenum.text) + 1;
124 
125 		if (len % 4)
126 			ROUNDUP4(len);
127 
128 		cenum.config_id = cmos_enum->config_id;
129 		cenum.value = cmos_enum->value;
130 		cenum.size = sizeof(cenum) - CMOS_MAX_TEXT_LENGTH + len;
131 		sum += cenum.size;
132 		if (fwrite((char *)&cenum, cenum.size, 1, f) != 1) {
133 			perror("Error writing image file");
134 			goto err;
135 		}
136 	}
137 
138 	layout.summed_area_start = cmos_checksum_start;
139 	layout.summed_area_end = cmos_checksum_end;
140 	layout.checksum_at = cmos_checksum_index;
141 	checksum_layout_to_bits(&layout);
142 
143 	csum.tag = LB_TAG_OPTION_CHECKSUM;
144 	csum.size = sizeof(csum);
145 	csum.range_start = layout.summed_area_start;
146 	csum.range_end = layout.summed_area_end;
147 	csum.location = layout.checksum_at;
148 	csum.type = CHECKSUM_PCBIOS;
149 	sum += csum.size;
150 
151 	if (fwrite((char *)&csum, csum.size, 1, f) != 1) {
152 		perror("Error writing image file");
153 		goto err;
154 	}
155 
156 	if (fseek(f, 0, SEEK_SET) != 0) {
157 		perror("Error while seeking");
158 		goto err;
159 	}
160 
161 	table.size = sum;
162 	if (fwrite((char *)&table, sizeof(table), 1, f) != 1) {
163 		perror("Error writing image file");
164 		goto err;
165 	}
166 	return sum;
167 
168 err:
169 	fclose(f);
170 	exit(1);
171 }
172 
write_cmos_output_bin(const char * binary_filename)173 void write_cmos_output_bin(const char *binary_filename)
174 {
175 	FILE *fp;
176 
177 	if ((fp = fopen(binary_filename, "wb")) == NULL) {
178 		fprintf(stderr,
179 			"%s: Can not open file %s for writing: "
180 			"%s\n", prog_name, binary_filename, strerror(errno));
181 		exit(1);
182 	}
183 	write_cmos_layout_bin(fp);
184 	fclose(fp);
185 }
186 
187 /****************************************************************************
188  * get_layout_from_cmos_table
189  *
190  * Find the CMOS table which is stored within the coreboot table and set the
191  * global variable cmos_table to point to it.
192  ****************************************************************************/
get_layout_from_cmos_table(void)193 void get_layout_from_cmos_table(void)
194 {
195 	get_lbtable();
196 	cmos_table = (const struct cmos_option_table *)
197 	    find_lbrec(LB_TAG_CMOS_OPTION_TABLE);
198 	process_layout();
199 }
200 
201 /****************************************************************************
202  * process_cmos_table
203  *
204  * Extract layout information from the CMOS option table and store it in our
205  * internal repository.
206  ****************************************************************************/
process_cmos_table(void)207 static void process_cmos_table(void)
208 {
209 	const struct cmos_enums *p;
210 	const struct cmos_entries *q;
211 	cmos_enum_t cmos_enum;
212 	cmos_entry_t cmos_entry;
213 
214 	/* First add the enums. */
215 	for (p = first_cmos_table_enum(); p != NULL;
216 	     p = next_cmos_table_enum(p)) {
217 		cmos_enum.config_id = p->config_id;
218 		cmos_enum.value = p->value;
219 		strncpy(cmos_enum.text, (char *)p->text, CMOS_MAX_TEXT_LENGTH);
220 		cmos_enum.text[CMOS_MAX_TEXT_LENGTH] = '\0';
221 		try_add_cmos_table_enum(&cmos_enum);
222 	}
223 
224 	/* Now add the entries.  We must add the entries after the enums because
225 	 * the entries are sanity checked against the enums as they are added.
226 	 */
227 	for (q = first_cmos_table_entry(); q != NULL;
228 	     q = next_cmos_table_entry(q)) {
229 		cmos_entry.bit = q->bit;
230 		cmos_entry.length = q->length;
231 
232 		switch (q->config) {
233 		case 'e':
234 			cmos_entry.config = CMOS_ENTRY_ENUM;
235 			break;
236 
237 		case 'h':
238 			cmos_entry.config = CMOS_ENTRY_HEX;
239 			break;
240 
241 		case 'r':
242 			cmos_entry.config = CMOS_ENTRY_RESERVED;
243 			break;
244 
245 		case 's':
246 			cmos_entry.config = CMOS_ENTRY_STRING;
247 			break;
248 
249 		default:
250 			fprintf(stderr,
251 				"%s: Entry in CMOS option table has unknown config "
252 				"value.\n", prog_name);
253 			exit(1);
254 		}
255 
256 		cmos_entry.config_id = q->config_id;
257 		strncpy(cmos_entry.name, (char *)q->name, CMOS_MAX_NAME_LENGTH);
258 		cmos_entry.name[CMOS_MAX_NAME_LENGTH] = '\0';
259 		try_add_cmos_table_entry(&cmos_entry);
260 	}
261 }
262 
263 /****************************************************************************
264  * get_cmos_checksum_info
265  *
266  * Get layout information for CMOS checksum.
267  ****************************************************************************/
get_cmos_checksum_info(void)268 static void get_cmos_checksum_info(void)
269 {
270 	const cmos_entry_t *e;
271 	struct cmos_checksum *checksum;
272 	cmos_checksum_layout_t layout;
273 	unsigned index, index2;
274 
275 	checksum = (struct cmos_checksum *)next_cmos_rec((const struct lb_record *)first_cmos_table_enum(), LB_TAG_OPTION_CHECKSUM);
276 
277 	if (checksum != NULL) {	/* We are lucky.  The coreboot table hints us to the checksum.
278 				 * We might have to check the type field here though.
279 				 */
280 		layout.summed_area_start = checksum->range_start;
281 		layout.summed_area_end = checksum->range_end;
282 		layout.checksum_at = checksum->location;
283 		try_convert_checksum_layout(&layout);
284 		cmos_checksum_start = layout.summed_area_start;
285 		cmos_checksum_end = layout.summed_area_end;
286 		cmos_checksum_index = layout.checksum_at;
287 		return;
288 	}
289 
290 	if ((e = find_cmos_entry(checksum_param_name)) == NULL)
291 		return;
292 
293 	/* If we get here, we are unlucky.  The CMOS option table contains the
294 	 * location of the CMOS checksum.  However, there is no information
295 	 * regarding which bytes of the CMOS area the checksum is computed over.
296 	 * Thus we have to hope our presets will be fine.
297 	 */
298 
299 	if (e->bit % 8) {
300 		fprintf(stderr,
301 			"%s: Error: CMOS checksum is not byte-aligned.\n",
302 			prog_name);
303 		exit(1);
304 	}
305 
306 	index = e->bit / 8;
307 	index2 = index + 1;	/* The CMOS checksum occupies 16 bits. */
308 
309 	if (verify_cmos_byte_index(index) || verify_cmos_byte_index(index2)) {
310 		fprintf(stderr,
311 			"%s: Error: CMOS checksum location out of range.\n",
312 			prog_name);
313 		exit(1);
314 	}
315 
316 	if (((index >= cmos_checksum_start) && (index <= cmos_checksum_end)) ||
317 	    (((index2) >= cmos_checksum_start)
318 	     && ((index2) <= cmos_checksum_end))) {
319 		fprintf(stderr,
320 			"%s: Error: CMOS checksum overlaps checksummed area.\n",
321 			prog_name);
322 		exit(1);
323 	}
324 
325 	cmos_checksum_index = index;
326 }
327 
328 /****************************************************************************
329  * try_convert_checksum_layout
330  *
331  * Perform sanity checking on CMOS checksum layout information and attempt to
332  * convert information from bit positions to byte positions.  Return OK on
333  * success or an error code on failure.
334  ****************************************************************************/
try_convert_checksum_layout(cmos_checksum_layout_t * layout)335 static void try_convert_checksum_layout(cmos_checksum_layout_t * layout)
336 {
337 	switch (checksum_layout_to_bytes(layout)) {
338 	case OK:
339 		return;
340 
341 	case LAYOUT_SUMMED_AREA_START_NOT_ALIGNED:
342 		fprintf(stderr,
343 			"%s: CMOS checksummed area start is not byte-aligned.\n",
344 			prog_name);
345 		break;
346 
347 	case LAYOUT_SUMMED_AREA_END_NOT_ALIGNED:
348 		fprintf(stderr,
349 			"%s: CMOS checksummed area end is not byte-aligned.\n",
350 			prog_name);
351 		break;
352 
353 	case LAYOUT_CHECKSUM_LOCATION_NOT_ALIGNED:
354 		fprintf(stderr,
355 			"%s: CMOS checksum location is not byte-aligned.\n",
356 			prog_name);
357 		break;
358 
359 	case LAYOUT_INVALID_SUMMED_AREA:
360 		fprintf(stderr,
361 			"%s: CMOS checksummed area end must be greater than "
362 			"CMOS checksummed area start.\n", prog_name);
363 		break;
364 
365 	case LAYOUT_CHECKSUM_OVERLAPS_SUMMED_AREA:
366 		fprintf(stderr,
367 			"%s: CMOS checksum overlaps checksummed area.\n",
368 			prog_name);
369 		break;
370 
371 	case LAYOUT_SUMMED_AREA_OUT_OF_RANGE:
372 		fprintf(stderr,
373 			"%s: CMOS checksummed area out of range.\n", prog_name);
374 		break;
375 
376 	case LAYOUT_CHECKSUM_LOCATION_OUT_OF_RANGE:
377 		fprintf(stderr,
378 			"%s: CMOS checksum location out of range.\n",
379 			prog_name);
380 		break;
381 
382 	default:
383 		BUG();
384 	}
385 
386 	exit(1);
387 }
388 
389 /****************************************************************************
390  * try_add_cmos_table_enum
391  *
392  * Attempt to add a CMOS enum to our internal repository.  Exit with an error
393  * message on failure.
394  ****************************************************************************/
try_add_cmos_table_enum(cmos_enum_t * cmos_enum)395 static void try_add_cmos_table_enum(cmos_enum_t * cmos_enum)
396 {
397 	switch (add_cmos_enum(cmos_enum)) {
398 	case OK:
399 		return;
400 
401 	case LAYOUT_DUPLICATE_ENUM:
402 		fprintf(stderr, "%s: Duplicate enum %s found in CMOS option "
403 			"table.\n", prog_name, cmos_enum->text);
404 		break;
405 
406 	default:
407 		BUG();
408 	}
409 
410 	exit(1);
411 }
412 
413 /****************************************************************************
414  * try_add_cmos_table_entry
415  *
416  * Attempt to add a CMOS entry to our internal repository.  Exit with an
417  * error message on failure.
418  ****************************************************************************/
try_add_cmos_table_entry(cmos_entry_t * cmos_entry)419 static void try_add_cmos_table_entry(cmos_entry_t * cmos_entry)
420 {
421 	const cmos_entry_t *conflict;
422 
423 	switch (add_cmos_entry(cmos_entry, &conflict)) {
424 	case OK:
425 		return;
426 
427 	case CMOS_AREA_OUT_OF_RANGE:
428 		fprintf(stderr,
429 			"%s: Bad CMOS option layout in CMOS option table entry "
430 			"%s.\n", prog_name, cmos_entry->name);
431 		break;
432 
433 	case CMOS_AREA_TOO_WIDE:
434 		fprintf(stderr,
435 			"%s: Area too wide for CMOS option table entry %s.\n",
436 			prog_name, cmos_entry->name);
437 		break;
438 
439 	case LAYOUT_ENTRY_OVERLAP:
440 		fprintf(stderr,
441 			"%s: CMOS option table entries %s and %s have overlapping "
442 			"layouts.\n", prog_name, cmos_entry->name,
443 			conflict->name);
444 		break;
445 
446 	case LAYOUT_ENTRY_BAD_LENGTH:
447 		/* Silently ignore entries with zero length.  Although this should
448 		 * never happen in practice, we should handle the case in a
449 		 * reasonable manner just to be safe.
450 		 */
451 		return;
452 
453 	case LAYOUT_MULTIBYTE_ENTRY_NOT_ALIGNED:
454 		fprintf(stderr,
455 			"%s: Unaligned CMOS option table entry %s "
456 			"spans multiple bytes.\n", prog_name, cmos_entry->name);
457 		break;
458 
459 	default:
460 		BUG();
461 	}
462 
463 	exit(1);
464 }
465 
466 /****************************************************************************
467  * first_cmos_table_entry
468  *
469  * Return a pointer to the first entry in the CMOS table that represents a
470  * CMOS parameter.  Return NULL if CMOS table is empty.
471  ****************************************************************************/
first_cmos_table_entry(void)472 static const struct cmos_entries *first_cmos_table_entry(void)
473 {
474 	return (const struct cmos_entries *)first_cmos_rec(LB_TAG_OPTION);
475 }
476 
477 /****************************************************************************
478  * next_cmos_table_entry
479  *
480  * Return a pointer to the next entry after 'last' in the CMOS table that
481  * represents a CMOS parameter.  Return NULL if there are no more parameters.
482  ****************************************************************************/
next_cmos_table_entry(const struct cmos_entries * last)483 static const struct cmos_entries *next_cmos_table_entry(const struct
484 							cmos_entries *last)
485 {
486 	return (const struct cmos_entries *)
487 	    next_cmos_rec((const struct lb_record *)last, LB_TAG_OPTION);
488 }
489 
490 /****************************************************************************
491  * first_cmos_table_enum
492  *
493  * Return a pointer to the first entry in the CMOS table that represents a
494  * possible CMOS parameter value.  Return NULL if the table does not contain
495  * any such entries.
496  ****************************************************************************/
first_cmos_table_enum(void)497 static const struct cmos_enums *first_cmos_table_enum(void)
498 {
499 	return (const struct cmos_enums *)first_cmos_rec(LB_TAG_OPTION_ENUM);
500 }
501 
502 /****************************************************************************
503  * next_cmos_table_enum
504  *
505  * Return a pointer to the next entry after 'last' in the CMOS table that
506  * represents a possible CMOS parameter value.  Return NULL if there are no
507  * more parameter values.
508  ****************************************************************************/
next_cmos_table_enum(const struct cmos_enums * last)509 static const struct cmos_enums *next_cmos_table_enum
510     (const struct cmos_enums *last) {
511 	return (const struct cmos_enums *)
512 	    next_cmos_rec((const struct lb_record *)last, LB_TAG_OPTION_ENUM);
513 }
514 
515 /****************************************************************************
516  * first_cmos_rec
517  *
518  * Return a pointer to the first entry in the CMOS table whose type matches
519  * 'tag'.  Return NULL if CMOS table contains no such entry.
520  *
521  * Possible values for 'tag' are as follows:
522  *
523  *     LB_TAG_OPTION:      The entry represents a CMOS parameter.
524  *     LB_TAG_OPTION_ENUM: The entry represents a possible value for a CMOS
525  *                         parameter of type 'enum'.
526  *
527  * The CMOS table tells us where in the nonvolatile RAM to look for CMOS
528  * parameter values and specifies their types as 'enum', 'hex', or
529  * 'reserved'.
530  ****************************************************************************/
first_cmos_rec(uint32_t tag)531 static const struct lb_record *first_cmos_rec(uint32_t tag)
532 {
533 	const char *p;
534 	uint32_t bytes_processed, bytes_for_entries;
535 	const struct lb_record *lbrec;
536 
537 	p = ((const char *)cmos_table) + cmos_table->header_length;
538 	bytes_for_entries = cmos_table->size - cmos_table->header_length;
539 
540 	for (bytes_processed = 0;
541 	     bytes_processed < bytes_for_entries;
542 	     bytes_processed += lbrec->size) {
543 		lbrec = (const struct lb_record *)&p[bytes_processed];
544 
545 		if (lbrec->tag == tag)
546 			return lbrec;
547 	}
548 
549 	return NULL;
550 }
551 
552 /****************************************************************************
553  * next_cmos_rec
554  *
555  * Return a pointer to the next entry after 'last' in the CMOS table whose
556  * type matches 'tag'.  Return NULL if the table contains no more entries of
557  * this type.
558  ****************************************************************************/
next_cmos_rec(const struct lb_record * last,uint32_t tag)559 static const struct lb_record *next_cmos_rec(const struct lb_record *last,
560 					     uint32_t tag)
561 {
562 	const char *p;
563 	uint32_t bytes_processed, bytes_for_entries, last_offset;
564 	const struct lb_record *lbrec;
565 
566 	p = ((const char *)cmos_table) + cmos_table->header_length;
567 	bytes_for_entries = cmos_table->size - cmos_table->header_length;
568 	last_offset = ((const char *)last) - p;
569 
570 	for (bytes_processed = last_offset + last->size;
571 	     bytes_processed < bytes_for_entries;
572 	     bytes_processed += lbrec->size) {
573 		lbrec = (const struct lb_record *)&p[bytes_processed];
574 
575 		if (lbrec->tag == tag)
576 			return lbrec;
577 	}
578 
579 	return NULL;
580 }
581