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>
This commit is contained in:
Denis 'GNUtoo' Carikli 2018-07-22 16:38:45 +02:00 committed by Patrick Georgi
parent 78d338ccb9
commit 48c24ce5ee
2 changed files with 61 additions and 41 deletions

View File

@ -37,13 +37,12 @@ struct blob {
unsigned int lenactualblob; unsigned int lenactualblob;
}; };
static struct field *putsym (char const *, unsigned int); static struct field *putsym(field_ptr_t, char const *, unsigned int);
static struct field *getsym (char const *); static struct field *getsym(field_ptr_t, char const *);
static void yyerror (FILE* fp, char const *); static void yyerror(FILE *, field_ptr_t, char const *);
int yylex (void); int yylex(void);
static struct blob *binary; static struct blob *binary;
static struct field *sym_table;
static struct field *sym_table_tail; static struct field *sym_table_tail;
#endif /* __BINCFG_H */ #endif /* __BINCFG_H */

View File

@ -59,10 +59,12 @@ static void append_field_to_blob (unsigned char b[], unsigned int w)
binary->bloblen += 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; unsigned long long i;
struct field *bf = getsym (name);
struct field *bf = getsym(sym_table_ptr, name);
if (bf) { if (bf) {
bf->value = value & 0xffffffff; bf->value = value & 0xffffffff;
i = (1 << bf->width) - 1; 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 int i;
unsigned long len = strlen (name); unsigned long len = strlen (name);
char *namen = (char *) malloc ((len + 9) * sizeof (char)); char *namen = (char *) malloc ((len + 9) * sizeof (char));
check_pointer(namen); check_pointer(namen);
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
snprintf (namen, len + 8, "%s%x", name, i); snprintf (namen, len + 8, "%s%x", name, i);
set_bitfield (namen, value); set_bitfield (sym_table_ptr, namen, value);
} }
free(namen); 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; 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); //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 int i;
unsigned long len = strlen (name); 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); check_pointer(namen);
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
snprintf (namen, len + 8, "%s%x", name, i); snprintf (namen, len + 8, "%s%x", name, i);
create_new_bitfield (namen, width); create_new_bitfield (sym_table_ptr, namen, width);
} }
free(namen); 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", fprintf(stderr, "Cannot add duplicate named bitfield `%s`\n",
sym_name); sym_name);
return 0; return 0;
} }
struct field *ptr = (struct field *) malloc (sizeof (struct field)); struct field *ptr = (field_t ) malloc (sizeof (struct field));
check_pointer(ptr); check_pointer(ptr);
ptr->name = (char *) malloc (strlen (sym_name) + 1); ptr->name = (char *) malloc (strlen (sym_name) + 1);
check_pointer(ptr->name); check_pointer(ptr->name);
strcpy (ptr->name, sym_name); strcpy (ptr->name, sym_name);
ptr->width = w; ptr->width = w;
ptr->value = 0; ptr->value = 0;
ptr->next = (struct field *)0; ptr->next = (field_t)0;
if (sym_table_tail) { if (sym_table_tail) {
sym_table_tail->next = ptr; sym_table_tail->next = ptr;
} else { } else {
sym_table = ptr; *sym_table_ptr = ptr;
} }
sym_table_tail = ptr; sym_table_tail = ptr;
return 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 *ptr;
struct field *sym_table = *sym_table_ptr;
for (ptr = sym_table; ptr != (struct field *) 0; for (ptr = sym_table; ptr != (struct field *) 0;
ptr = (struct field *)ptr->next) { ptr = (struct field *)ptr->next) {
if (strcmp (ptr->name, sym_name) == 0) if (strcmp (ptr->name, sym_name) == 0)
@ -147,9 +154,11 @@ static struct field *getsym (char const *sym_name)
return 0; return 0;
} }
static void dump_all_values (void) static void dump_all_values (struct field **sym_table_ptr)
{ {
struct field *ptr; struct field *ptr;
struct field *sym_table = *sym_table_ptr;
for (ptr = sym_table; ptr != (struct field *) 0; for (ptr = sym_table; ptr != (struct field *) 0;
ptr = (struct field *)ptr->next) { ptr = (struct field *)ptr->next) {
fprintf(stderr, "%s = %d (%d bits)\n", 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 *ptr;
struct field *ptrnext; struct field *ptrnext;
struct field *sym_table = *sym_table_ptr;
for (ptr = sym_table; ptr != (struct field *) 0; ptr = ptrnext) { for (ptr = sym_table; ptr != (struct field *) 0; ptr = ptrnext) {
if (ptr) { if (ptr) {
ptrnext = ptr->next; ptrnext = ptr->next;
@ -208,10 +219,12 @@ static void interpret_next_blob_value (struct field *f)
} }
/* {}%BIN -> {} */ /* {}%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; unsigned int i;
struct field *ptr; struct field *ptr;
struct field *sym_table = *sym_table_ptr;
/* Convert bytes to bit array */ /* Convert bytes to bit array */
for (i = 0; i < binary->lenactualblob; i++) { 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"); 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; int i;
unsigned short checksum; unsigned short checksum;
struct field *sym_table = *sym_table_ptr;
/* traverse spec, push to blob and add up for checksum */ /* traverse spec, push to blob and add up for checksum */
struct field *ptr; struct field *ptr;
unsigned int uptochksum = 0; unsigned int uptochksum = 0;
@ -277,7 +293,7 @@ static void generate_binary_with_gbe_checksum(FILE* fp)
checksum = (0xbaba - binary->checksum) & 0xffff; checksum = (0xbaba - binary->checksum) & 0xffff;
/* Now write checksum */ /* 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 & 0xff);
fprintf(fp, "%c", (checksum & 0xff00) >> 8); fprintf(fp, "%c", (checksum & 0xff00) >> 8);
@ -305,10 +321,11 @@ static void generate_binary_with_gbe_checksum(FILE* fp)
} }
/* {}{} -> BIN */ /* {}{} -> BIN */
static void generate_binary(FILE* fp) static void generate_binary(FILE* fp, struct field **sym_table_ptr)
{ {
unsigned int i; unsigned int i;
struct field *ptr; struct field *ptr;
struct field *sym_table = *sym_table_ptr;
if (binary->bloblen % 8) { if (binary->bloblen % 8) {
fprintf (stderr, fprintf (stderr,
@ -316,8 +333,8 @@ static void generate_binary(FILE* fp)
exit (1); exit (1);
} }
if (getsym ("checksum_gbe")) { if (getsym (sym_table_ptr, "checksum_gbe")) {
generate_binary_with_gbe_checksum(fp); generate_binary_with_gbe_checksum(fp, sym_table_ptr);
return; return;
} }
@ -353,7 +370,7 @@ static void generate_binary(FILE* fp)
unsigned char u8; unsigned char u8;
unsigned char *u8array; unsigned char *u8array;
} }
%parse-param {FILE* fp} %parse-param {FILE* fp} {struct field** sym_table_ptr}
%token <str> name %token <str> name
%token <u32> val %token <u32> val
@ -372,13 +389,13 @@ static void generate_binary(FILE* fp)
input: input:
/* empty */ /* 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"); | input spec blob { fprintf (stderr, "Parsed all bytes\n");
empty_field_table(); YYACCEPT;} empty_field_table(sym_table_ptr); YYACCEPT;}
; ;
blob: blob:
'%' eof { generate_setter_bitfields(fp, '%' eof { generate_setter_bitfields(fp, sym_table_ptr,
binary->actualblob); } binary->actualblob); }
; ;
@ -394,14 +411,15 @@ specmembers:
; ;
specpair: specpair:
name ':' val { create_new_bitfield($1, $3); } name ':' val { create_new_bitfield(sym_table_ptr, $1, $3); }
| name '[' val ']' ':' val { create_new_bitfields($1, $3, $6); } | name '[' val ']' ':' val { create_new_bitfields(sym_table_ptr, $1, $3,
$6); }
; ;
setter: setter:
'{' '}' { fprintf (stderr, "No values\n"); } '{' '}' { fprintf (stderr, "No values\n"); }
| '{' valuemembers '}' { fprintf (stderr, "Parsed all values\n"); | '{' valuemembers '}' { fprintf (stderr, "Parsed all values\n");
generate_binary(fp); } generate_binary(fp, sym_table_ptr); }
; ;
valuemembers: valuemembers:
@ -410,14 +428,15 @@ valuemembers:
; ;
setpair: setpair:
name '=' val { set_bitfield($1, $3); } name '=' val { set_bitfield(sym_table_ptr, $1, $3); }
| name '[' val ']' '=' val { set_bitfield_array($1, $3, $6); } | name '[' val ']' '=' val { set_bitfield_array(sym_table_ptr, $1, $3,
$6); }
; ;
%% %%
/* Called by yyparse on error. */ /* 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); fprintf (stderr, "yyerror: %s\n", s);
} }
@ -426,9 +445,10 @@ static void yyerror (FILE* fp, char const *s)
void set_input_string(char* in); void set_input_string(char* in);
/* This function parses a string */ /* 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); set_input_string ((char *)in);
return yyparse (fp); return yyparse (fp, sym_table_ptr);
} }
static unsigned int loadfile (FILE* fp, char *file, char *filetype, static unsigned int loadfile (FILE* fp, char *file, char *filetype,
@ -464,6 +484,7 @@ int main (int argc, char *argv[])
unsigned int pos = 0; unsigned int pos = 0;
int ret = 0; int ret = 0;
FILE* fp; FILE* fp;
struct field *sym_table;
#if YYDEBUG == 1 #if YYDEBUG == 1
yydebug = 1; yydebug = 1;
@ -484,7 +505,7 @@ int main (int argc, char *argv[])
argv[3]); argv[3]);
exit(1); exit(1);
} }
ret = parse_string(fp, parsestring); ret = parse_string(fp, &sym_table, parsestring);
free(parsestring); free(parsestring);
} else if (argc == 5 && strcmp (argv[1], "-d") == 0) { } else if (argc == 5 && strcmp (argv[1], "-d") == 0) {
/* Decompile mode */ /* Decompile mode */
@ -516,7 +537,7 @@ int main (int argc, char *argv[])
argv[4]); argv[4]);
exit(1); exit(1);
} }
ret = parse_string(fp, parsestring); ret = parse_string(fp, &sym_table, parsestring);
free(parsestring); free(parsestring);
free(binary->actualblob); free(binary->actualblob);
fclose(fp); fclose(fp);