summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorDenis 'GNUtoo' Carikli <GNUtoo@no-log.org>2018-07-22 16:38:45 +0200
committerPatrick Georgi <pgeorgi@google.com>2018-10-24 10:02:06 +0000
commit48c24ce5eefe64b1aa82237f0a4be2772c686ef6 (patch)
tree6995e4fbef1c62ec342536cd103d04c2d189493b /util
parent78d338ccb9ca9d03e516c27beb3724be38d38ecf (diff)
downloadcoreboot-48c24ce5eefe64b1aa82237f0a4be2772c686ef6.tar.xz
util/bincfg: code cleanup: convert sym_table to a local variable
Global variables are considered a bad practice. Change-Id: I652a8da75498f871a53eb7509f6145c4842e3373 Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo@no-log.org> Reviewed-on: https://review.coreboot.org/27810 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org>
Diffstat (limited to 'util')
-rw-r--r--util/bincfg/bincfg.h9
-rw-r--r--util/bincfg/bincfg.y93
2 files changed, 61 insertions, 41 deletions
diff --git a/util/bincfg/bincfg.h b/util/bincfg/bincfg.h
index b8c668889e..c0e1242f83 100644
--- a/util/bincfg/bincfg.h
+++ b/util/bincfg/bincfg.h
@@ -37,13 +37,12 @@ struct blob {
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 struct field *putsym(field_ptr_t, char const *, unsigned int);
+static struct field *getsym(field_ptr_t, char const *);
+static void yyerror(FILE *, field_ptr_t, char const *);
+int yylex(void);
static struct blob *binary;
-static struct field *sym_table;
static struct field *sym_table_tail;
#endif /* __BINCFG_H */
diff --git a/util/bincfg/bincfg.y b/util/bincfg/bincfg.y
index 8be11e9a92..ac65d0ffbe 100644
--- a/util/bincfg/bincfg.y
+++ b/util/bincfg/bincfg.y
@@ -59,10 +59,12 @@ static void append_field_to_blob (unsigned char b[], unsigned int w)
binary->bloblen += w;
}
-static void set_bitfield(char *name, unsigned int value)
+static void set_bitfield(struct field **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(char *name, unsigned int value)
}
}
-static void set_bitfield_array(char *name, unsigned int n, unsigned int value)
+static void set_bitfield_array(struct field **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(struct field **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(struct field **sym_table_ptr, char *name, unsigned int n, unsigned int width)
{
unsigned int i;
unsigned long len = strlen (name);
@@ -107,38 +111,41 @@ static void create_new_bitfields(char *name, unsigned int n, unsigned int width)
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 struct field *putsym (struct field **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));
+ struct field *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 struct field *getsym (struct field **sym_table_ptr, char const *sym_name)
{
struct field *ptr;
+ struct field *sym_table = *sym_table_ptr;
+
for (ptr = sym_table; ptr != (struct field *) 0;
ptr = (struct field *)ptr->next) {
if (strcmp (ptr->name, sym_name) == 0)
@@ -147,9 +154,11 @@ static struct field *getsym (char const *sym_name)
return 0;
}
-static void dump_all_values (void)
+static void dump_all_values (struct field **sym_table_ptr)
{
struct field *ptr;
+ struct field *sym_table = *sym_table_ptr;
+
for (ptr = sym_table; ptr != (struct field *) 0;
ptr = (struct field *)ptr->next) {
fprintf(stderr, "%s = %d (%d bits)\n",
@@ -159,11 +168,13 @@ static void dump_all_values (void)
}
}
-static void empty_field_table(void)
+static void empty_field_table(struct field **sym_table_ptr)
{
struct field *ptr;
struct field *ptrnext;
+ struct field *sym_table = *sym_table_ptr;
+
for (ptr = sym_table; ptr != (struct field *) 0; ptr = ptrnext) {
if (ptr) {
ptrnext = ptr->next;
@@ -208,10 +219,12 @@ static void interpret_next_blob_value (struct field *f)
}
/* {}%BIN -> {} */
-static void generate_setter_bitfields(FILE* fp, unsigned char *bin)
+static void generate_setter_bitfields(FILE* fp, struct field **sym_table_ptr,
+ unsigned char *bin)
{
unsigned int i;
struct field *ptr;
+ struct field *sym_table = *sym_table_ptr;
/* Convert bytes to bit array */
for (i = 0; i < binary->lenactualblob; i++) {
@@ -233,11 +246,14 @@ static void generate_setter_bitfields(FILE* fp, unsigned char *bin)
fprintf (fp, "\n}\n");
}
-static void generate_binary_with_gbe_checksum(FILE* fp)
+static void generate_binary_with_gbe_checksum(FILE* fp,
+ struct field **sym_table_ptr)
{
int i;
unsigned short checksum;
+ struct field *sym_table = *sym_table_ptr;
+
/* traverse spec, push to blob and add up for checksum */
struct field *ptr;
unsigned int uptochksum = 0;
@@ -277,7 +293,7 @@ static void generate_binary_with_gbe_checksum(FILE* fp)
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);
@@ -305,10 +321,11 @@ static void generate_binary_with_gbe_checksum(FILE* fp)
}
/* {}{} -> BIN */
-static void generate_binary(FILE* fp)
+static void generate_binary(FILE* fp, struct field **sym_table_ptr)
{
unsigned int i;
struct field *ptr;
+ struct field *sym_table = *sym_table_ptr;
if (binary->bloblen % 8) {
fprintf (stderr,
@@ -316,8 +333,8 @@ static void generate_binary(FILE* fp)
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;
}
@@ -353,7 +370,7 @@ static void generate_binary(FILE* fp)
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 @@ static void generate_binary(FILE* fp)
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 @@ specmembers:
;
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 @@ valuemembers:
;
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, struct field **sym_table_ptr, char const *s)
{
fprintf (stderr, "yyerror: %s\n", s);
}
@@ -426,9 +445,10 @@ static void yyerror (FILE* fp, char const *s)
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, struct field **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 @@ int main (int argc, char *argv[])
unsigned int pos = 0;
int ret = 0;
FILE* fp;
+ struct field *sym_table;
#if YYDEBUG == 1
yydebug = 1;
@@ -484,7 +505,7 @@ int main (int argc, char *argv[])
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 @@ int main (int argc, char *argv[])
argv[4]);
exit(1);
}
- ret = parse_string(fp, parsestring);
+ ret = parse_string(fp, &sym_table, parsestring);
free(parsestring);
free(binary->actualblob);
fclose(fp);