[PATCH 3/3] ratp: make ratp commands const

Sascha Hauer s.hauer at pengutronix.de
Mon Apr 7 00:29:55 PDT 2025


The ratp commands are placed in the readonly data section, but are
written to due to the list entry in the command.

Make the ratp commands actually const by moving the list entry to a
separate container struct.

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 common/ratp/ratp.c | 34 ++++++++++++++++++++++------------
 include/ratp_bb.h  |  3 +--
 2 files changed, 23 insertions(+), 14 deletions(-)

diff --git a/common/ratp/ratp.c b/common/ratp/ratp.c
index 925e1d637f..a59dd56265 100644
--- a/common/ratp/ratp.c
+++ b/common/ratp/ratp.c
@@ -30,8 +30,6 @@
 
 static LIST_HEAD(ratp_command_list);
 
-#define for_each_ratp_command(cmd) list_for_each_entry(cmd, &ratp_command_list, list)
-
 struct ratp_bb_command_return {
 	uint32_t errno;
 };
@@ -56,30 +54,42 @@ struct ratp_ctx {
 	bool wq_registered;
 };
 
+struct ratp_command_entry {
+	struct list_head list;
+	const struct ratp_command *cmd;
+};
+
 static int compare_ratp_command(struct list_head *a, struct list_head *b)
 {
-	int id_a = list_entry(a, struct ratp_command, list)->request_id;
-	int id_b = list_entry(b, struct ratp_command, list)->request_id;
+	int id_a = list_entry(a, const struct ratp_command_entry, list)->cmd->request_id;
+	int id_b = list_entry(b, const struct ratp_command_entry, list)->cmd->request_id;
 
 	return (id_a - id_b);
 }
 
-int register_ratp_command(struct ratp_command *cmd)
+int register_ratp_command(const struct ratp_command *cmd)
 {
+	struct ratp_command_entry *entry;
+
 	debug("register ratp command: request %hu, response %hu\n",
 	      cmd->request_id, cmd->response_id);
-	list_add_sort(&cmd->list, &ratp_command_list, compare_ratp_command);
+
+	entry = xzalloc(sizeof(*entry));
+	entry->cmd = cmd;
+
+	list_add_sort(&entry->list, &ratp_command_list, compare_ratp_command);
+
 	return 0;
 }
 EXPORT_SYMBOL(register_ratp_command);
 
-static struct ratp_command *find_ratp_request(uint16_t request_id)
+static const struct ratp_command *find_ratp_request(uint16_t request_id)
 {
-	struct ratp_command *cmdtp;
+	struct ratp_command_entry *entry;
 
-	for_each_ratp_command(cmdtp)
-		if (request_id == cmdtp->request_id)
-			return cmdtp;
+	list_for_each_entry(entry, &ratp_command_list, list)
+		if (request_id == entry->cmd->request_id)
+			return entry->cmd;
 
 	return NULL;	/* not found */
 }
@@ -210,7 +220,7 @@ static int ratp_bb_dispatch(struct ratp_ctx *ctx, const void *buf, int len)
 	int dlen = len - sizeof(struct ratp_bb);
 	int ret = 0;
 	uint16_t type = be16_to_cpu(rbb->type);
-	struct ratp_command *cmd;
+	const struct ratp_command *cmd;
 	struct ratp_work *rw;
 
 	/* See if there's a command registered to this type */
diff --git a/include/ratp_bb.h b/include/ratp_bb.h
index c6c7c4bc23..746f6155dd 100644
--- a/include/ratp_bb.h
+++ b/include/ratp_bb.h
@@ -50,7 +50,6 @@ int  barebox_ratp_fs_mount(const char *path);
  */
 
 struct ratp_command {
-	struct list_head  list;
 	uint16_t          request_id;
 	uint16_t          response_id;
 	int		(*cmd)(const struct ratp_bb *req,
@@ -67,6 +66,6 @@ const struct ratp_command __barebox_ratp_cmd_##_name					\
 #define BAREBOX_RATP_CMD_END								\
 };
 
-int register_ratp_command(struct ratp_command *cmd);
+int register_ratp_command(const struct ratp_command *cmd);
 
 #endif /* __RATP_BB_H */

-- 
2.39.5




More information about the barebox mailing list