Replace open coded list handling with Linux list implementation. Signed-off-by: Sascha Hauer <s.hauer@xxxxxxxxxxxxxx> --- commands/fiptool.c | 16 ++++++++-------- include/fiptool.h | 16 ++++++++++++---- lib/fip.c | 40 +++++++++++++++++++++++----------------- 3 files changed, 43 insertions(+), 29 deletions(-) diff --git a/commands/fiptool.c b/commands/fiptool.c index a4370c9eef..45d534e470 100644 --- a/commands/fiptool.c +++ b/commands/fiptool.c @@ -73,7 +73,7 @@ static int info_cmd(struct fip_state *fip, int argc, char *argv[]) pr_verbose("toc_header[flags]: 0x%llX\n", (unsigned long long)toc_header.flags); - for (desc = fip->image_desc_head; desc != NULL; desc = desc->next) { + fip_for_each_desc(fip, desc) { struct fip_image *image = desc->image; if (image == NULL) @@ -409,7 +409,7 @@ static int unpack_cmd(struct fip_state *fip, int argc, char *argv[]) } /* Unpack all specified images. */ - for (desc = fip->image_desc_head; desc != NULL; desc = desc->next) { + fip_for_each_desc(fip, desc) { char file[PATH_MAX]; struct fip_image *image = desc->image; @@ -517,7 +517,7 @@ static __maybe_unused int remove_cmd(struct fip_state *fip, int argc, char *argv if (ret) return ret; - for (desc = fip->image_desc_head; desc != NULL; desc = desc->next) { + fip_for_each_desc(fip, desc) { if (desc->action != DO_REMOVE) continue; @@ -548,7 +548,7 @@ static cmd_t cmds[] = { static int do_fiptool(int argc, char *argv[]) { int i, opt, ret = 0; - struct fip_state fip = {}; + struct fip_state *fip = fip_new(); /* * Set POSIX mode so getopt stops at the first non-option @@ -557,7 +557,7 @@ static int do_fiptool(int argc, char *argv[]) while ((opt = getopt(argc, argv, "+v")) > 0) { switch (opt) { case 'v': - fip.verbose = 1; + fip->verbose = 1; break; default: return COMMAND_ERROR_USAGE; @@ -569,14 +569,14 @@ static int do_fiptool(int argc, char *argv[]) if (argc == 0) return COMMAND_ERROR_USAGE; - fill_image_descs(&fip); + fill_image_descs(fip); for (i = 0; i < ARRAY_SIZE(cmds); i++) { if (strcmp(cmds[i].name, argv[0]) == 0) { struct getopt_context gc; getopt_context_store(&gc); - ret = cmds[i].handler(&fip, argc, argv); + ret = cmds[i].handler(fip, argc, argv); getopt_context_restore(&gc); break; @@ -585,7 +585,7 @@ static int do_fiptool(int argc, char *argv[]) if (i == ARRAY_SIZE(cmds)) return COMMAND_ERROR_USAGE; - free_image_descs(&fip); + fip_free(fip); return ret; } diff --git a/include/fiptool.h b/include/fiptool.h index 6f1d69d178..5a3fdee6e0 100644 --- a/include/fiptool.h +++ b/include/fiptool.h @@ -8,6 +8,7 @@ #include <linux/uuid.h> #include <fip.h> +#include <linux/list.h> enum { DO_UNSPEC = 0, @@ -23,7 +24,7 @@ struct fip_image_desc { int action; char *action_arg; struct fip_image *image; - struct fip_image_desc *next; + struct list_head list; }; struct fip_image { @@ -32,7 +33,7 @@ struct fip_image { }; struct fip_state { - struct fip_image_desc *image_desc_head; + struct list_head descs; size_t nr_image_descs; int verbose; }; @@ -45,6 +46,9 @@ struct fip_state { } \ } while (0) +struct fip_state *fip_new(void); +void fip_free(struct fip_state *fip); + struct fip_image_desc *new_image_desc(const uuid_t *uuid, const char *name, const char *cmdline_name); @@ -55,8 +59,6 @@ void free_image_desc(struct fip_image_desc *desc); void add_image_desc(struct fip_state *fip, struct fip_image_desc *desc); -void free_image_descs(struct fip_state *fip); - void fill_image_descs(struct fip_state *fip); struct fip_image_desc *lookup_image_desc_from_uuid(struct fip_state *fip, @@ -84,4 +86,10 @@ typedef struct toc_entry { extern toc_entry_t toc_entries[]; extern toc_entry_t plat_def_toc_entries[]; +#define fip_for_each_desc(fip, e) \ + list_for_each_entry(e, &(fip)->descs, list) + +#define fip_for_each_desc_safe(fip, e, tmp) \ + list_for_each_entry_safe(e, tmp, &(fip)->descs, list) + #endif /* FIPTOOL_H */ diff --git a/lib/fip.c b/lib/fip.c index e7589ff4c2..63f1469086 100644 --- a/lib/fip.c +++ b/lib/fip.c @@ -66,27 +66,33 @@ void free_image_desc(struct fip_image_desc *desc) void add_image_desc(struct fip_state *fip, struct fip_image_desc *desc) { - struct fip_image_desc **p = &fip->image_desc_head; + list_add_tail(&desc->list, &fip->descs); + fip->nr_image_descs++; +} - while (*p) - p = &(*p)->next; +struct fip_state *fip_new(void) +{ + struct fip_state *fip; - ASSERT(*p == NULL); - *p = desc; - fip->nr_image_descs++; + fip = xzalloc(sizeof(*fip)); + + INIT_LIST_HEAD(&fip->descs); + + return fip; } -void free_image_descs(struct fip_state *fip) +void fip_free(struct fip_state *fip) { - struct fip_image_desc *desc = fip->image_desc_head, *tmp; + struct fip_image_desc *desc, *tmp; - while (desc != NULL) { - tmp = desc->next; + fip_for_each_desc_safe(fip, desc, tmp) { free_image_desc(desc); - desc = tmp; fip->nr_image_descs--; } + ASSERT(fip->nr_image_descs == 0); + + free(fip); } void fill_image_descs(struct fip_state *fip) @@ -120,7 +126,7 @@ struct fip_image_desc *lookup_image_desc_from_uuid(struct fip_state *fip, { struct fip_image_desc *desc; - for (desc = fip->image_desc_head; desc != NULL; desc = desc->next) + fip_for_each_desc(fip, desc) if (memcmp(&desc->uuid, uuid, sizeof(uuid_t)) == 0) return desc; return NULL; @@ -135,7 +141,7 @@ struct fip_image_desc *lookup_image_desc_from_opt(struct fip_state *fip, char ** eq = strchrnul(*arg, '='); len = eq - *arg; - for (desc = fip->image_desc_head; desc != NULL; desc = desc->next) { + fip_for_each_desc(fip, desc) { if (strncmp(desc->cmdline_name, *arg, len) == 0) { if (*eq) *arg = eq + 1; @@ -305,7 +311,7 @@ int pack_images(struct fip_state *fip, uint64_t entry_offset, buf_size, payload_size = 0, pad_size; size_t nr_images = 0; - for (desc = fip->image_desc_head; desc != NULL; desc = desc->next) + fip_for_each_desc(fip, desc) if (desc->image != NULL) nr_images++; @@ -324,7 +330,7 @@ int pack_images(struct fip_state *fip, toc_entry = (fip_toc_entry_t *)(toc_header + 1); entry_offset = buf_size; - for (desc = fip->image_desc_head; desc != NULL; desc = desc->next) { + fip_for_each_desc(fip, desc) { struct fip_image *image = desc->image; if (image == NULL || (image->toc_e.size == 0ULL)) @@ -360,7 +366,7 @@ int pack_images(struct fip_state *fip, pr_verbose("Payload size: %llu bytes\n", payload_size); - for (desc = fip->image_desc_head; desc != NULL; desc = desc->next) { + fip_for_each_desc(fip, desc) { struct fip_image *image = desc->image; if (image == NULL) @@ -399,7 +405,7 @@ int update_fip(struct fip_state *fip) struct fip_image_desc *desc; /* Add or replace images in the FIP file. */ - for (desc = fip->image_desc_head; desc != NULL; desc = desc->next) { + fip_for_each_desc(fip, desc) { struct fip_image *image; if (desc->action != DO_PACK) -- 2.39.5