[coreboot-gerrit] Change in coreboot[master]: util/bincfg: don't use sym_table shared variable

Denis 'GNUtoo' Carikli (Code Review) gerrit at coreboot.org
Thu Aug 2 22:19:47 CEST 2018


Denis 'GNUtoo' Carikli has uploaded this change for review. ( https://review.coreboot.org/27810


Change subject: util/bincfg: don't use sym_table shared variable
......................................................................

util/bincfg: don't use sym_table shared variable

Change-Id: I652a8da75498f871a53eb7509f6145c4842e3373
Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo at no-log.org>
---
M util/bincfg/bincfg.h
M util/bincfg/bincfg.y
2 files changed, 82 insertions(+), 60 deletions(-)



  git pull ssh://review.coreboot.org:29418/coreboot refs/changes/10/27810/1

diff --git a/util/bincfg/bincfg.h b/util/bincfg/bincfg.h
index b8c6688..8e2038a 100644
--- a/util/bincfg/bincfg.h
+++ b/util/bincfg/bincfg.h
@@ -27,6 +27,8 @@
 	unsigned int value;
 	struct field *next;
 };
+typedef struct field* field_t;
+typedef struct field** field_ptr_t;
 
 /* Bit array intermediary representation */
 struct blob {
@@ -37,13 +39,12 @@
 	unsigned int lenactualblob;
 };
 
-static struct field *putsym (char const *, unsigned int);
-static struct field *getsym (char const *);
-static void yyerror (FILE* fp, char const *);
-int yylex (void);
+static field_t putsym(field_ptr_t sym_table_ptr, char const *, unsigned int);
+static field_t getsym(field_ptr_t sym_table_ptr, char const *);
+static void yyerror(FILE* fp, field_ptr_t, char const *);
+int yylex(void);
 
 static struct blob *binary;
-static struct field *sym_table;
-static struct field *sym_table_tail;
+static field_t sym_table_tail;
 
 #endif /* __BINCFG_H */
diff --git a/util/bincfg/bincfg.y b/util/bincfg/bincfg.y
index 8be11e9..851d9f5 100644
--- a/util/bincfg/bincfg.y
+++ b/util/bincfg/bincfg.y
@@ -59,10 +59,12 @@
 	binary->bloblen += w;
 }
 
-static void set_bitfield(char *name, unsigned int value)
+static void set_bitfield(field_ptr_t sym_table_ptr, char *name,
+			 unsigned int value)
 {
 	unsigned long long i;
-	struct field *bf = getsym (name);
+
+	struct field *bf = getsym(sym_table_ptr, name);
 	if (bf) {
 		bf->value = value & 0xffffffff;
 		i = (1 << bf->width) - 1;
@@ -78,28 +80,30 @@
 	}
 }
 
-static void set_bitfield_array(char *name, unsigned int n, unsigned int value)
+static void set_bitfield_array(field_ptr_t sym_table_ptr, char *name,
+			       unsigned int n, unsigned int value)
 {
 	unsigned int i;
 	unsigned long len = strlen (name);
 	char *namen = (char *) malloc ((len + 9) * sizeof (char));
+
 	check_pointer(namen);
 	for (i = 0; i < n; i++) {
 		snprintf (namen, len + 8, "%s%x", name, i);
-		set_bitfield (namen, value);
+		set_bitfield (sym_table_ptr, namen, value);
 	}
 	free(namen);
 }
 
-static void create_new_bitfield(char *name, unsigned int width)
+static void create_new_bitfield(field_ptr_t sym_table_ptr, char *name, unsigned int width)
 {
 	struct field *bf;
 
-	if (!(bf = putsym (name, width))) return;
+	if (!(bf = putsym (sym_table_ptr, name, width))) return;
 	//fprintf(stderr, "Added bitfield `%s` : %d\n", bf->name, width);
 }
 
-static void create_new_bitfields(char *name, unsigned int n, unsigned int width)
+static void create_new_bitfields(field_ptr_t sym_table_ptr, char *name, unsigned int n, unsigned int width)
 {
 	unsigned int i;
 	unsigned long len = strlen (name);
@@ -107,51 +111,56 @@
 	check_pointer(namen);
 	for (i = 0; i < n; i++) {
 		snprintf (namen, len + 8, "%s%x", name, i);
-		create_new_bitfield (namen, width);
+		create_new_bitfield (sym_table_ptr, namen, width);
 	}
 	free(namen);
 }
 
-static struct field *putsym (char const *sym_name, unsigned int w)
+static field_t putsym (field_ptr_t sym_table_ptr,  char const *sym_name,
+		       unsigned int w)
 {
-	if (getsym(sym_name)) {
+	if (getsym(sym_table_ptr, sym_name)) {
 		fprintf(stderr, "Cannot add duplicate named bitfield `%s`\n",
 			sym_name);
 		return 0;
 	}
-	struct field *ptr = (struct field *) malloc (sizeof (struct field));
+	field_t ptr = (field_t ) malloc (sizeof (struct field));
 	check_pointer(ptr);
 	ptr->name = (char *) malloc (strlen (sym_name) + 1);
 	check_pointer(ptr->name);
 	strcpy (ptr->name, sym_name);
 	ptr->width = w;
 	ptr->value = 0;
-	ptr->next = (struct field *)0;
+	ptr->next = (field_t)0;
 	if (sym_table_tail) {
 		sym_table_tail->next = ptr;
 	} else {
-		sym_table = ptr;
+		*sym_table_ptr = ptr;
 	}
 	sym_table_tail = ptr;
 	return ptr;
 }
 
-static struct field *getsym (char const *sym_name)
+static field_t getsym (field_ptr_t sym_table_ptr, char const *sym_name)
 {
-	struct field *ptr;
-	for (ptr = sym_table; ptr != (struct field *) 0;
-			ptr = (struct field *)ptr->next) {
+	field_t ptr;
+	field_t sym_table = *sym_table_ptr;
+
+	for (ptr = sym_table; ptr != (field_t ) 0;
+			ptr = (field_t )ptr->next) {
 		if (strcmp (ptr->name, sym_name) == 0)
 			return ptr;
 	}
 	return 0;
 }
 
-static void dump_all_values (void)
+static void dump_all_values (field_ptr_t sym_table_ptr)
 {
-	struct field *ptr;
-	for (ptr = sym_table; ptr != (struct field *) 0;
-			ptr = (struct field *)ptr->next) {
+	field_t ptr;
+	field_t sym_table = *sym_table_ptr;
+
+	for (ptr = sym_table; ptr != (field_t ) 0;
+			ptr = (field_t )ptr->next) {
 		fprintf(stderr, "%s = %d (%d bits)\n",
 				ptr->name,
 				ptr->value,
@@ -159,17 +168,19 @@
 	}
 }
 
-static void empty_field_table(void)
+static void empty_field_table(field_ptr_t sym_table_ptr)
 {
-	struct field *ptr;
-	struct field *ptrnext;
+	field_t ptr;
+	field_t ptrnext;
 
-	for (ptr = sym_table; ptr != (struct field *) 0; ptr = ptrnext) {
+	field_t sym_table = *sym_table_ptr;
+
+	for (ptr = sym_table; ptr != (field_t ) 0; ptr = ptrnext) {
 		if (ptr) {
 			ptrnext = ptr->next;
 			free(ptr);
 		} else {
-			ptrnext = (struct field *) 0;
+			ptrnext = (field_t ) 0;
 		}
 	}
 	sym_table = 0;
@@ -190,7 +201,7 @@
 	binary->blb[0] = VALID_BIT;
 }
 
-static void interpret_next_blob_value (struct field *f)
+static void interpret_next_blob_value (field_t f)
 {
 	unsigned int i;
 	unsigned int v = 0;
@@ -208,10 +219,12 @@
 }
 
 /* {}%BIN -> {} */
-static void generate_setter_bitfields(FILE* fp, unsigned char *bin)
+static void generate_setter_bitfields(FILE* fp, field_ptr_t sym_table_ptr,
+				      unsigned char *bin)
 {
 	unsigned int i;
-	struct field *ptr;
+	field_t ptr;
+	field_t sym_table = *sym_table_ptr;
 
 	/* Convert bytes to bit array */
 	for (i = 0; i < binary->lenactualblob; i++) {
@@ -224,7 +237,7 @@
 	fprintf (fp, "# AUTOGENERATED SETTER BY BINCFG\n{\n");
 
 	/* Traverse spec and output bitfield setters based on blob values */
-	for (ptr = sym_table; ptr != (struct field *) 0; ptr = ptr->next) {
+	for (ptr = sym_table; ptr != (field_t ) 0; ptr = ptr->next) {
 
 		interpret_next_blob_value(ptr);
 		fprintf (fp, "\t\"%s\" = 0x%x,\n", ptr->name, ptr->value);
@@ -233,15 +246,18 @@
 	fprintf (fp, "\n}\n");
 }
 
-static void generate_binary_with_gbe_checksum(FILE* fp)
+static void generate_binary_with_gbe_checksum(FILE* fp,
+					      field_ptr_t sym_table_ptr)
 {
 	int i;
 	unsigned short checksum;
 
+	field_t sym_table = *sym_table_ptr;
+
 	/* traverse spec, push to blob and add up for checksum */
-	struct field *ptr;
+	field_t ptr;
 	unsigned int uptochksum = 0;
-	for (ptr = sym_table; ptr != (struct field *) 0; ptr = ptr->next) {
+	for (ptr = sym_table; ptr != (field_t ) 0; ptr = ptr->next) {
 		if (strcmp (ptr->name, "checksum_gbe") == 0) {
 			/* Stop traversing because we hit checksum */
 			ptr = ptr->next;
@@ -277,14 +293,14 @@
 	checksum = (0xbaba - binary->checksum) & 0xffff;
 
 	/* Now write checksum */
-	set_bitfield ("checksum_gbe", checksum);
+	set_bitfield (sym_table_ptr, "checksum_gbe", checksum);
 
 	fprintf(fp, "%c", checksum & 0xff);
 	fprintf(fp, "%c", (checksum & 0xff00) >> 8);
 
 	append_field_to_blob (value_to_bits(checksum, 16), 16);
 
-	for (; ptr != (struct field *) 0; ptr = ptr->next) {
+	for (; ptr != (field_t ) 0; ptr = ptr->next) {
 		append_field_to_blob (
 			value_to_bits(ptr->value, ptr->width), ptr->width);
 	}
@@ -305,10 +321,11 @@
 }
 
 /* {}{} -> BIN */
-static void generate_binary(FILE* fp)
+static void generate_binary(FILE* fp, field_ptr_t sym_table_ptr)
 {
 	unsigned int i;
-	struct field *ptr;
+	field_t ptr;
+	field_t sym_table = *sym_table_ptr;
 
 	if (binary->bloblen % 8) {
 		fprintf (stderr,
@@ -316,13 +333,13 @@
 		exit (1);
 	}
 
-	if (getsym ("checksum_gbe")) {
-		generate_binary_with_gbe_checksum(fp);
+	if (getsym (sym_table_ptr, "checksum_gbe")) {
+		generate_binary_with_gbe_checksum(fp, sym_table_ptr);
 		return;
 	}
 
 	/* traverse spec, push to blob */
-	for (ptr = sym_table; ptr != (struct field *) 0; ptr = ptr->next) {
+	for (ptr = sym_table; ptr != (field_t ) 0; ptr = ptr->next) {
 		append_field_to_blob (
 			value_to_bits(ptr->value, ptr->width),
 			ptr->width);
@@ -353,7 +370,7 @@
 	unsigned char u8;
 	unsigned char *u8array;
 }
-%parse-param {FILE* fp}
+%parse-param {FILE* fp} {struct field** sym_table_ptr}
 
 %token <str> name
 %token <u32> val
@@ -372,13 +389,13 @@
 
 input:
   /* empty */
-| input spec setter eof		{ empty_field_table(); YYACCEPT;}
+| input spec setter eof		{ empty_field_table(sym_table_ptr); YYACCEPT;}
 | input spec blob		{ fprintf (stderr, "Parsed all bytes\n");
-				  empty_field_table(); YYACCEPT;}
+				  empty_field_table(sym_table_ptr); YYACCEPT;}
 ;
 
 blob:
-  '%' eof			{ generate_setter_bitfields(fp,
+  '%' eof			{ generate_setter_bitfields(fp, sym_table_ptr,
 				  binary->actualblob); }
 ;
 
@@ -394,14 +411,15 @@
 ;
 
 specpair:
-  name ':' val		{	create_new_bitfield($1, $3); }
-| name '[' val ']' ':' val	{ create_new_bitfields($1, $3, $6); }
+  name ':' val		{	create_new_bitfield(sym_table_ptr, $1, $3); }
+| name '[' val ']' ':' val	{ create_new_bitfields(sym_table_ptr, $1, $3,
+						       $6); }
 ;
 
 setter:
   '{' '}'		{	fprintf (stderr, "No values\n"); }
 | '{' valuemembers '}'	{	fprintf (stderr, "Parsed all values\n");
-				generate_binary(fp); }
+				generate_binary(fp, sym_table_ptr); }
 ;
 
 valuemembers:
@@ -410,14 +428,15 @@
 ;
 
 setpair:
-  name '=' val		{	set_bitfield($1, $3); }
-| name '[' val ']' '=' val	{ set_bitfield_array($1, $3, $6); }
+name '=' val		{	set_bitfield(sym_table_ptr, $1, $3); }
+| name '[' val ']' '=' val	{ set_bitfield_array(sym_table_ptr, $1, $3,
+						     $6); }
 ;
 
 %%
 
 /* Called by yyparse on error.  */
-static void yyerror (FILE* fp, char const *s)
+static void yyerror (FILE* fp, field_ptr_t sym_table_ptr, char const *s)
 {
 	fprintf (stderr, "yyerror: %s\n", s);
 }
@@ -426,9 +445,10 @@
 void set_input_string(char* in);
 
 /* This function parses a string */
-static int parse_string(FILE* fp, unsigned char* in) {
+static int parse_string(FILE* fp, field_ptr_t sym_table_ptr, unsigned char* in)
+{
 	set_input_string ((char *)in);
-	return yyparse (fp);
+	return yyparse (fp, sym_table_ptr);
 }
 
 static unsigned int loadfile (FILE* fp, char *file, char *filetype,
@@ -464,6 +484,7 @@
 	unsigned int pos = 0;
 	int ret = 0;
 	FILE* fp;
+	field_t sym_table;
 
 #if YYDEBUG == 1
 	yydebug = 1;
@@ -484,7 +505,7 @@
 			       argv[3]);
 			exit(1);
 		}
-		ret = parse_string(fp, parsestring);
+		ret = parse_string(fp, &sym_table, parsestring);
 		free(parsestring);
 	} else if (argc == 5 && strcmp (argv[1], "-d") == 0) {
 		/* Decompile mode */
@@ -516,7 +537,7 @@
 			       argv[4]);
 			exit(1);
 		}
-		ret = parse_string(fp, parsestring);
+		ret = parse_string(fp, &sym_table, parsestring);
 		free(parsestring);
 		free(binary->actualblob);
 		fclose(fp);

-- 
To view, visit https://review.coreboot.org/27810
To unsubscribe, or for help writing mail filters, visit https://review.coreboot.org/settings

Gerrit-Project: coreboot
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I652a8da75498f871a53eb7509f6145c4842e3373
Gerrit-Change-Number: 27810
Gerrit-PatchSet: 1
Gerrit-Owner: Denis 'GNUtoo' Carikli <GNUtoo at no-log.org>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.coreboot.org/pipermail/coreboot-gerrit/attachments/20180802/46a1212b/attachment-0001.html>


More information about the coreboot-gerrit mailing list