1
0
mirror of https://review.coreboot.org/flashrom.git synced 2025-07-02 06:23:18 +02:00

layout.c: Don't use global variables for included regions

This removes the use of global variables for included region arguments
and also uses a linked list to store the arguments.

Change-Id: I6534cc58b8dcc6256c2730c809286d8083669a6c
Signed-off-by: Arthur Heymans <arthur@aheymans.xyz>
Reviewed-on: https://review.coreboot.org/c/flashrom/+/31247
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Nico Huber <nico.h@gmx.de>
This commit is contained in:
Arthur Heymans
2019-02-05 17:35:05 +01:00
committed by Nico Huber
parent ee13d0c8fa
commit b04fef91c1
4 changed files with 58 additions and 51 deletions

View File

@ -151,6 +151,7 @@ int main(int argc, char *argv[])
#endif /* !STANDALONE */ #endif /* !STANDALONE */
char *tempstr = NULL; char *tempstr = NULL;
char *pparam = NULL; char *pparam = NULL;
struct layout_include_args *include_args = NULL;
flashrom_set_log_callback((flashrom_log_callback *)&flashrom_print_cb); flashrom_set_log_callback((flashrom_log_callback *)&flashrom_print_cb);
@ -290,7 +291,7 @@ int main(int argc, char *argv[])
break; break;
case 'i': case 'i':
tempstr = strdup(optarg); tempstr = strdup(optarg);
if (register_include_arg(tempstr)) { if (register_include_arg(&include_args, tempstr)) {
free(tempstr); free(tempstr);
cli_classic_abort_usage(); cli_classic_abort_usage();
} }
@ -452,7 +453,7 @@ int main(int argc, char *argv[])
goto out; goto out;
} }
if (!ifd && !fmap && process_include_args(get_global_layout())) { if (!ifd && !fmap && process_include_args(get_global_layout(), include_args)) {
ret = 1; ret = 1;
goto out; goto out;
} }
@ -608,7 +609,7 @@ int main(int argc, char *argv[])
if (layoutfile) { if (layoutfile) {
layout = get_global_layout(); layout = get_global_layout();
} else if (ifd && (flashrom_layout_read_from_ifd(&layout, fill_flash, NULL, 0) || } else if (ifd && (flashrom_layout_read_from_ifd(&layout, fill_flash, NULL, 0) ||
process_include_args(layout))) { process_include_args(layout, include_args))) {
ret = 1; ret = 1;
goto out_shutdown; goto out_shutdown;
} else if (fmap && fmapfile) { } else if (fmap && fmapfile) {
@ -633,14 +634,14 @@ int main(int argc, char *argv[])
} }
if (flashrom_layout_read_fmap_from_buffer(&layout, fill_flash, fmapfile_buffer, fmapfile_size) || if (flashrom_layout_read_fmap_from_buffer(&layout, fill_flash, fmapfile_buffer, fmapfile_size) ||
process_include_args(layout)) { process_include_args(layout, include_args)) {
ret = 1; ret = 1;
free(fmapfile_buffer); free(fmapfile_buffer);
goto out_shutdown; goto out_shutdown;
} }
free(fmapfile_buffer); free(fmapfile_buffer);
} else if (fmap && (flashrom_layout_read_fmap_from_rom(&layout, fill_flash, 0, } else if (fmap && (flashrom_layout_read_fmap_from_rom(&layout, fill_flash, 0,
fill_flash->chip->total_size * 1024) || process_include_args(layout))) { fill_flash->chip->total_size * 1024) || process_include_args(layout, include_args))) {
ret = 1; ret = 1;
goto out_shutdown; goto out_shutdown;
} }
@ -675,7 +676,7 @@ out:
for (i = 0; i < chipcount; i++) for (i = 0; i < chipcount; i++)
free(flashes[i].chip); free(flashes[i].chip);
layout_cleanup(); layout_cleanup(&include_args);
free(filename); free(filename);
free(fmapfile); free(fmapfile);
free(referencefile); free(referencefile);

View File

@ -391,10 +391,10 @@ __attribute__((format(printf, 2, 3)));
#define msg_cspew(...) print(FLASHROM_MSG_SPEW, __VA_ARGS__) /* chip debug spew */ #define msg_cspew(...) print(FLASHROM_MSG_SPEW, __VA_ARGS__) /* chip debug spew */
/* layout.c */ /* layout.c */
int register_include_arg(char *name); int register_include_arg(struct layout_include_args **args, char *name);
int read_romlayout(const char *name); int read_romlayout(const char *name);
int normalize_romentries(const struct flashctx *flash); int normalize_romentries(const struct flashctx *flash);
void layout_cleanup(void); void layout_cleanup(struct layout_include_args **args);
/* spi.c */ /* spi.c */
struct spi_command { struct spi_command {

View File

@ -26,11 +26,6 @@
struct romentry entries[MAX_ROMLAYOUT]; struct romentry entries[MAX_ROMLAYOUT];
static struct flashrom_layout layout = { entries, 0 }; static struct flashrom_layout layout = { entries, 0 };
/* include_args holds the arguments specified at the command line with -i. They must be processed at some point
* so that desired regions are marked as "included" in the layout. */
static char *include_args[MAX_ROMLAYOUT];
static int num_include_args = 0; /* the number of valid include_args. */
struct flashrom_layout *get_global_layout(void) struct flashrom_layout *get_global_layout(void)
{ {
return &layout; return &layout;
@ -101,37 +96,34 @@ int read_romlayout(const char *name)
} }
#endif #endif
/* returns the index of the entry (or a negative value if it is not found) */
static int find_include_arg(const char *const name)
{
unsigned int i;
for (i = 0; i < num_include_args; i++) {
if (!strcmp(include_args[i], name))
return i;
}
return -1;
}
/* register an include argument (-i) for later processing */ /* register an include argument (-i) for later processing */
int register_include_arg(char *name) int register_include_arg(struct layout_include_args **args, char *name)
{ {
if (num_include_args >= MAX_ROMLAYOUT) { struct layout_include_args *tmp;
msg_gerr("Too many regions included (%i).\n", num_include_args);
return 1;
}
if (name == NULL) { if (name == NULL) {
msg_gerr("<NULL> is a bad region name.\n"); msg_gerr("<NULL> is a bad region name.\n");
return 1; return 1;
} }
if (find_include_arg(name) != -1) { tmp = *args;
msg_gerr("Duplicate region name: \"%s\".\n", name); while (tmp) {
if (!strcmp(tmp->name, name)) {
msg_gerr("Duplicate region name: \"%s\".\n", name);
return 1;
}
tmp = tmp->next;
}
tmp = malloc(sizeof(struct layout_include_args));
if (tmp == NULL) {
msg_gerr("Could not allocate memory");
return 1; return 1;
} }
include_args[num_include_args] = name; tmp->name = name;
num_include_args++; tmp->next = *args;
*args = tmp;
return 0; return 0;
} }
@ -153,45 +145,54 @@ static int find_romentry(struct flashrom_layout *const l, char *name)
/* process -i arguments /* process -i arguments
* returns 0 to indicate success, >0 to indicate failure * returns 0 to indicate success, >0 to indicate failure
*/ */
int process_include_args(struct flashrom_layout *const l) int process_include_args(struct flashrom_layout *l, const struct layout_include_args *const args)
{ {
int i; int found = 0;
const struct layout_include_args *tmp;
if (num_include_args == 0) if (args == NULL)
return 0; return 0;
/* User has specified an area, but no layout file is loaded. */ /* User has specified an area, but no layout file is loaded. */
if (l->num_entries == 0) { if (l->num_entries == 0) {
msg_gerr("Region requested (with -i \"%s\"), " msg_gerr("Region requested (with -i \"%s\"), "
"but no layout data is available.\n", "but no layout data is available.\n",
include_args[0]); args->name);
return 1; return 1;
} }
for (i = 0; i < num_include_args; i++) { tmp = args;
if (find_romentry(l, include_args[i]) < 0) { while (tmp) {
if (find_romentry(l, tmp->name) < 0) {
msg_gerr("Invalid region specified: \"%s\".\n", msg_gerr("Invalid region specified: \"%s\".\n",
include_args[i]); tmp->name);
return 1; return 1;
} }
tmp = tmp->next;
found++;
} }
msg_ginfo("Using region%s: \"%s\"", num_include_args > 1 ? "s" : "", msg_ginfo("Using region%s:", found > 1 ? "s" : "");
include_args[0]); tmp = args;
for (i = 1; i < num_include_args; i++) while (tmp) {
msg_ginfo(", \"%s\"", include_args[i]); msg_ginfo(" \"%s\"%s", tmp->name, found > 1 ? "," : "");
found--;
tmp = tmp->next;
}
msg_ginfo(".\n"); msg_ginfo(".\n");
return 0; return 0;
} }
void layout_cleanup(void) void layout_cleanup(struct layout_include_args **args)
{ {
int i; int i;
for (i = 0; i < num_include_args; i++) { struct layout_include_args *tmp;
free(include_args[i]);
include_args[i] = NULL; while (*args) {
tmp = (*args)->next;
free(*args);
*args = tmp;
} }
num_include_args = 0;
for (i = 0; i < layout.num_entries; i++) { for (i = 0; i < layout.num_entries; i++) {
layout.entries[i].included = 0; layout.entries[i].included = 0;

View File

@ -54,11 +54,16 @@ struct single_layout {
struct romentry entry; struct romentry entry;
}; };
struct layout_include_args {
char *name;
struct layout_include_args *next;
};
struct flashrom_layout *get_global_layout(void); struct flashrom_layout *get_global_layout(void);
struct flashrom_flashctx; struct flashrom_flashctx;
const struct flashrom_layout *get_layout(const struct flashrom_flashctx *const flashctx); const struct flashrom_layout *get_layout(const struct flashrom_flashctx *const flashctx);
int process_include_args(struct flashrom_layout *); int process_include_args(struct flashrom_layout *l, const struct layout_include_args *const args);
const struct romentry *layout_next_included_region(const struct flashrom_layout *, chipoff_t); const struct romentry *layout_next_included_region(const struct flashrom_layout *, chipoff_t);
#endif /* !__LAYOUT_H__ */ #endif /* !__LAYOUT_H__ */