common: eeprom_layout: Split field finding code from the field update function

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>
This commit is contained in:
Marek Behún 2024-05-21 09:13:26 +02:00 committed by Tom Rini
parent a804c8dc5f
commit 15378a3fe1
2 changed files with 35 additions and 15 deletions

View file

@ -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;
field = eeprom_layout_find_field(layout, field_name, true);
if (field == NULL)
return -1;
err = fields[i].update(&fields[i], new_data);
if (err)
printf("Invalid data for field %s\n", field_name);
err = field->update(field, new_data);
if (err)
printf("Invalid data for field %s\n", field_name);
return err;
}
printf("No such field '%s'\n", field_name);
return -1;
return err;
}
/*

View file

@ -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,