1
0
mirror of https://review.coreboot.org/flashrom.git synced 2025-07-01 06:01:16 +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 */
char *tempstr = NULL;
char *pparam = NULL;
struct layout_include_args *include_args = NULL;
flashrom_set_log_callback((flashrom_log_callback *)&flashrom_print_cb);
@ -290,7 +291,7 @@ int main(int argc, char *argv[])
break;
case 'i':
tempstr = strdup(optarg);
if (register_include_arg(tempstr)) {
if (register_include_arg(&include_args, tempstr)) {
free(tempstr);
cli_classic_abort_usage();
}
@ -452,7 +453,7 @@ int main(int argc, char *argv[])
goto out;
}
if (!ifd && !fmap && process_include_args(get_global_layout())) {
if (!ifd && !fmap && process_include_args(get_global_layout(), include_args)) {
ret = 1;
goto out;
}
@ -608,7 +609,7 @@ int main(int argc, char *argv[])
if (layoutfile) {
layout = get_global_layout();
} 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;
goto out_shutdown;
} 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) ||
process_include_args(layout)) {
process_include_args(layout, include_args)) {
ret = 1;
free(fmapfile_buffer);
goto out_shutdown;
}
free(fmapfile_buffer);
} 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;
goto out_shutdown;
}
@ -675,7 +676,7 @@ out:
for (i = 0; i < chipcount; i++)
free(flashes[i].chip);
layout_cleanup();
layout_cleanup(&include_args);
free(filename);
free(fmapfile);
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 */
/* 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 normalize_romentries(const struct flashctx *flash);
void layout_cleanup(void);
void layout_cleanup(struct layout_include_args **args);
/* spi.c */
struct spi_command {

View File

@ -26,11 +26,6 @@
struct romentry entries[MAX_ROMLAYOUT];
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)
{
return &layout;
@ -101,37 +96,34 @@ int read_romlayout(const char *name)
}
#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 */
int register_include_arg(char *name)
int register_include_arg(struct layout_include_args **args, char *name)
{
if (num_include_args >= MAX_ROMLAYOUT) {
msg_gerr("Too many regions included (%i).\n", num_include_args);
return 1;
}
struct layout_include_args *tmp;
if (name == NULL) {
msg_gerr("<NULL> is a bad region name.\n");
return 1;
}
if (find_include_arg(name) != -1) {
tmp = *args;
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;
}
tmp->name = name;
tmp->next = *args;
*args = tmp;
include_args[num_include_args] = name;
num_include_args++;
return 0;
}
@ -153,45 +145,54 @@ static int find_romentry(struct flashrom_layout *const l, char *name)
/* process -i arguments
* 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;
/* User has specified an area, but no layout file is loaded. */
if (l->num_entries == 0) {
msg_gerr("Region requested (with -i \"%s\"), "
"but no layout data is available.\n",
include_args[0]);
args->name);
return 1;
}
for (i = 0; i < num_include_args; i++) {
if (find_romentry(l, include_args[i]) < 0) {
tmp = args;
while (tmp) {
if (find_romentry(l, tmp->name) < 0) {
msg_gerr("Invalid region specified: \"%s\".\n",
include_args[i]);
tmp->name);
return 1;
}
tmp = tmp->next;
found++;
}
msg_ginfo("Using region%s: \"%s\"", num_include_args > 1 ? "s" : "",
include_args[0]);
for (i = 1; i < num_include_args; i++)
msg_ginfo(", \"%s\"", include_args[i]);
msg_ginfo("Using region%s:", found > 1 ? "s" : "");
tmp = args;
while (tmp) {
msg_ginfo(" \"%s\"%s", tmp->name, found > 1 ? "," : "");
found--;
tmp = tmp->next;
}
msg_ginfo(".\n");
return 0;
}
void layout_cleanup(void)
void layout_cleanup(struct layout_include_args **args)
{
int i;
for (i = 0; i < num_include_args; i++) {
free(include_args[i]);
include_args[i] = NULL;
struct layout_include_args *tmp;
while (*args) {
tmp = (*args)->next;
free(*args);
*args = tmp;
}
num_include_args = 0;
for (i = 0; i < layout.num_entries; i++) {
layout.entries[i].included = 0;

View File

@ -54,11 +54,16 @@ struct single_layout {
struct romentry entry;
};
struct layout_include_args {
char *name;
struct layout_include_args *next;
};
struct flashrom_layout *get_global_layout(void);
struct flashrom_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);
#endif /* !__LAYOUT_H__ */