From: Sascha Hauer <s.hauer@pengutronix.de>
To: "open list:BAREBOX" <barebox@lists.infradead.org>
Subject: [PATCH v2 05/20] fip: use linux list implementation
Date: Wed, 12 Feb 2025 15:09:18 +0100 [thread overview]
Message-ID: <20250212-k3-emmc-v2-5-8dd1bb0ce60a@pengutronix.de> (raw)
In-Reply-To: <20250212-k3-emmc-v2-0-8dd1bb0ce60a@pengutronix.de>
Replace open coded list handling with Linux list implementation.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
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
next prev parent reply other threads:[~2025-02-12 14:10 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-12 14:09 [PATCH v2 00/20] ARM: K3 updates Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 01/20] ARM: k3: Add function to enable 32k crystal Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 02/20] ARM: k3: add function to detect eMMC boot Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 03/20] ARM: k3: do not mount /boot when booting from eMMC Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 04/20] fip: drop typedefs Sascha Hauer
2025-02-12 14:09 ` Sascha Hauer [this message]
2025-02-12 14:09 ` [PATCH v2 06/20] fip: use uuid_equal() and uuid_is_null() Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 07/20] fiptool: do not typedef structs Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 08/20] fip: add fip_ prefix Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 09/20] fip: add fip_image_open() Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 10/20] ARM: k3: r5: add USB DFU and eMMC boot support Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 11/20] ARM: am625-sk: enable 32k crystal Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 12/20] mci: am654: parse generic mmc node properties Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 13/20] ARM: k3: limit eMMC frequency to 26MHz Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 14/20] ARM: k3: add eMMC barebox update handler Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 15/20] ARM: am625-sk: put environment on eMMC when booting from it Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 16/20] serial: omap: Use ttyS as Linux console name Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 17/20] ARM: k3: remove beagleplay FIT image Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 18/20] ARM: am625-sk: cleanup board entry Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 19/20] ARM: beagleplay: " Sascha Hauer
2025-02-12 14:09 ` [PATCH v2 20/20] ARM: k3: Add k3-r5_defconfig Sascha Hauer
2025-02-17 11:14 ` [PATCH v2 00/20] ARM: K3 updates Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250212-k3-emmc-v2-5-8dd1bb0ce60a@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox