From: "Marek Behún" <kabel@kernel.org>
To: Tom Rini <trini@konsulko.com>,
u-boot@lists.denx.de, Stefan Roese <sr@denx.de>
Cc: "Simon Glass" <sjg@chromium.org>,
"Ilias Apalodimas" <ilias.apalodimas@linaro.org>,
"Nikita Kiryanov" <nikita@compulab.co.il>,
"Marek Behún" <kabel@kernel.org>
Subject: [PATCH 02/11] common: eeprom_layout: Split field finding code from the field update function
Date: Tue, 21 May 2024 09:13:26 +0200 [thread overview]
Message-ID: <20240521071335.4193-3-kabel@kernel.org> (raw)
In-Reply-To: <20240521071335.4193-1-kabel@kernel.org>
Split the eeprom layout field finding code from the
eeprom_layout_update_field() function in order to make it usable in
alternative implementations of update method.
Signed-off-by: Marek Behún <kabel@kernel.org>
---
common/eeprom/eeprom_layout.c | 46 +++++++++++++++++++++++------------
include/eeprom_layout.h | 4 +++
2 files changed, 35 insertions(+), 15 deletions(-)
diff --git a/common/eeprom/eeprom_layout.c b/common/eeprom/eeprom_layout.c
index 406db3f7d1..801e90d38d 100644
--- a/common/eeprom/eeprom_layout.c
+++ b/common/eeprom/eeprom_layout.c
@@ -56,6 +56,28 @@ static void eeprom_layout_print(const struct eeprom_layout *layout)
fields[i].print(&fields[i]);
}
+/*
+ * eeprom_layout_find_field() - finds a layout field by name
+ * @layout: A pointer to an existing struct layout.
+ * @field_name: The name of the field to update.
+ * @warn: Whether to print a warning if the field is not found.
+ *
+ * Returns: a pointer to the found field or NULL on failure.
+ */
+struct eeprom_field *eeprom_layout_find_field(struct eeprom_layout *layout,
+ char *field_name, bool warn)
+{
+ for (int i = 0; i < layout->num_of_fields; i++)
+ if (layout->fields[i].name != RESERVED_FIELDS &&
+ !strcmp(layout->fields[i].name, field_name))
+ return &layout->fields[i];
+
+ if (warn)
+ printf("No such field '%s'\n", field_name);
+
+ return NULL;
+}
+
/*
* eeprom_layout_update_field() - update a single field in the layout data.
* @layout: A pointer to an existing struct layout.
@@ -67,8 +89,8 @@ static void eeprom_layout_print(const struct eeprom_layout *layout)
static int eeprom_layout_update_field(struct eeprom_layout *layout,
char *field_name, char *new_data)
{
- int i, err;
- struct eeprom_field *fields = layout->fields;
+ struct eeprom_field *field;
+ int err;
if (new_data == NULL)
return 0;
@@ -76,21 +98,15 @@ static int eeprom_layout_update_field(struct eeprom_layout *layout,
if (field_name == NULL)
return -1;
- for (i = 0; i < layout->num_of_fields; i++) {
- if (fields[i].name == RESERVED_FIELDS ||
- strcmp(fields[i].name, field_name))
- continue;
-
- err = fields[i].update(&fields[i], new_data);
- if (err)
- printf("Invalid data for field %s\n", field_name);
-
- return err;
- }
+ field = eeprom_layout_find_field(layout, field_name, true);
+ if (field == NULL)
+ return -1;
- printf("No such field '%s'\n", field_name);
+ err = field->update(field, new_data);
+ if (err)
+ printf("Invalid data for field %s\n", field_name);
- return -1;
+ return err;
}
/*
diff --git a/include/eeprom_layout.h b/include/eeprom_layout.h
index 730d963ab9..b1d6220595 100644
--- a/include/eeprom_layout.h
+++ b/include/eeprom_layout.h
@@ -9,6 +9,8 @@
#ifndef _LAYOUT_
#define _LAYOUT_
+#include <eeprom_field.h>
+
#define RESERVED_FIELDS NULL
#define LAYOUT_VERSION_UNRECOGNIZED -1
#define LAYOUT_VERSION_AUTODETECT -2
@@ -24,6 +26,8 @@ struct eeprom_layout {
char *new_data);
};
+struct eeprom_field *eeprom_layout_find_field(struct eeprom_layout *layout,
+ char *field_name, bool warn);
void eeprom_layout_setup(struct eeprom_layout *layout, unsigned char *buf,
unsigned int buf_size, int layout_version);
__weak void __eeprom_layout_assign(struct eeprom_layout *layout,
--
2.44.1
next prev parent reply other threads:[~2024-05-21 7:14 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-21 7:13 [PATCH 00/11] 'eeprom' command improvements Marek Behún
2024-05-21 7:13 ` [PATCH 01/11] common: eeprom_layout: Assign default layout methods and parameters before specific ones Marek Behún
2024-05-21 7:13 ` Marek Behún [this message]
2024-05-21 7:13 ` [PATCH 03/11] common: eeprom_field: Fix updating binary field Marek Behún
2024-05-21 7:13 ` [PATCH 04/11] common: eeprom_field: Drop unnecessary comparison Marek Behún
2024-05-21 7:13 ` [PATCH 05/11] cmd: eeprom: Fix usage help for the eeprom command Marek Behún
2024-05-21 7:13 ` [PATCH 06/11] cmd: eeprom: Hide eeprom layout versioning behind a Kconfig option Marek Behún
2024-05-21 7:13 ` [PATCH 07/11] cmd: eeprom: Deduplicate parse_i2c_bus_addr() calls Marek Behún
2024-05-21 7:13 ` [PATCH 08/11] cmd: eeprom: Refactor eeprom device specifier parsing Marek Behún
2024-05-21 7:13 ` [PATCH 09/11] cmd: eeprom: Refactor command execution into function by action Marek Behún
2024-05-21 7:13 ` [PATCH 10/11] cmd: eeprom: Don't read/write whole EEPROM if not necessary Marek Behún
2024-05-21 7:13 ` [PATCH 11/11] cmd: eeprom: Extend to EEPROMs probed via driver model Marek Behún
2024-06-07 18:57 ` [PATCH 00/11] 'eeprom' command improvements Tom Rini
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=20240521071335.4193-3-kabel@kernel.org \
--to=kabel@kernel.org \
--cc=ilias.apalodimas@linaro.org \
--cc=nikita@compulab.co.il \
--cc=sjg@chromium.org \
--cc=sr@denx.de \
--cc=trini@konsulko.com \
--cc=u-boot@lists.denx.de \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.