blob: 8c0b7e0b393c9d3ed2d6b3badf14f2e345acda59 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Nikita Kiryanovaa9e6042016-04-16 17:55:03 +03002/*
3 * (C) Copyright 2009-2016 CompuLab, Ltd.
4 *
5 * Authors: Nikita Kiryanov <nikita@compulab.co.il>
6 * Igor Grinberg <grinberg@compulab.co.il>
Nikita Kiryanovaa9e6042016-04-16 17:55:03 +03007 */
8
Nikita Kiryanovaa9e6042016-04-16 17:55:03 +03009#include <linux/kernel.h>
Tom Rini03de3052024-05-20 13:35:03 -060010#include <linux/string.h>
Nikita Kiryanovaa9e6042016-04-16 17:55:03 +030011#include <eeprom_layout.h>
12#include <eeprom_field.h>
13
14#define NO_LAYOUT_FIELDS "Unknown layout. Dumping raw data\n"
15
16struct eeprom_field layout_unknown[1] = {
17 { NO_LAYOUT_FIELDS, 256, NULL, eeprom_field_print_bin,
18 eeprom_field_update_bin },
19};
20
21/*
22 * eeprom_layout_detect() - detect layout based on the contents of the data.
23 * @data: Pointer to the data to be analyzed.
24 *
25 * Returns: the detected layout version.
26 */
27__weak int eeprom_layout_detect(unsigned char *data)
28{
29 return LAYOUT_VERSION_UNRECOGNIZED;
30}
31
32/*
33 * __eeprom_layout_assign() - set the layout fields
34 * @layout: A pointer to an existing struct layout.
35 * @layout_version: The version number of the desired layout
36 */
37__weak void __eeprom_layout_assign(struct eeprom_layout *layout,
38 int layout_version)
39{
40 layout->fields = layout_unknown;
41 layout->num_of_fields = ARRAY_SIZE(layout_unknown);
42}
43void eeprom_layout_assign(struct eeprom_layout *layout, int layout_version) \
44 __attribute__((weak, alias("__eeprom_layout_assign")));
45
46/*
47 * eeprom_layout_print() - print the layout and the data which is assigned to it
48 * @layout: A pointer to an existing struct layout.
49 */
50static void eeprom_layout_print(const struct eeprom_layout *layout)
51{
52 int i;
53 struct eeprom_field *fields = layout->fields;
54
55 for (i = 0; i < layout->num_of_fields; i++)
56 fields[i].print(&fields[i]);
57}
58
59/*
Marek Behún15378a32024-05-21 09:13:26 +020060 * eeprom_layout_find_field() - finds a layout field by name
61 * @layout: A pointer to an existing struct layout.
62 * @field_name: The name of the field to update.
63 * @warn: Whether to print a warning if the field is not found.
64 *
65 * Returns: a pointer to the found field or NULL on failure.
66 */
67struct eeprom_field *eeprom_layout_find_field(struct eeprom_layout *layout,
68 char *field_name, bool warn)
69{
70 for (int i = 0; i < layout->num_of_fields; i++)
71 if (layout->fields[i].name != RESERVED_FIELDS &&
72 !strcmp(layout->fields[i].name, field_name))
73 return &layout->fields[i];
74
75 if (warn)
76 printf("No such field '%s'\n", field_name);
77
78 return NULL;
79}
80
81/*
Nikita Kiryanovaa9e6042016-04-16 17:55:03 +030082 * eeprom_layout_update_field() - update a single field in the layout data.
83 * @layout: A pointer to an existing struct layout.
84 * @field_name: The name of the field to update.
85 * @new_data: The new field data (a string. Format depends on the field)
86 *
87 * Returns: 0 on success, negative error value on failure.
88 */
89static int eeprom_layout_update_field(struct eeprom_layout *layout,
90 char *field_name, char *new_data)
91{
Marek Behún15378a32024-05-21 09:13:26 +020092 struct eeprom_field *field;
93 int err;
Nikita Kiryanovaa9e6042016-04-16 17:55:03 +030094
95 if (new_data == NULL)
96 return 0;
97
98 if (field_name == NULL)
99 return -1;
100
Marek Behún15378a32024-05-21 09:13:26 +0200101 field = eeprom_layout_find_field(layout, field_name, true);
102 if (field == NULL)
103 return -1;
Nikita Kiryanovaa9e6042016-04-16 17:55:03 +0300104
Marek Behún15378a32024-05-21 09:13:26 +0200105 err = field->update(field, new_data);
106 if (err)
107 printf("Invalid data for field %s\n", field_name);
Nikita Kiryanovaa9e6042016-04-16 17:55:03 +0300108
Marek Behún15378a32024-05-21 09:13:26 +0200109 return err;
Nikita Kiryanovaa9e6042016-04-16 17:55:03 +0300110}
111
112/*
113 * eeprom_layout_setup() - setup layout struct with the layout data and
114 * metadata as dictated by layout_version
115 * @layout: A pointer to an existing struct layout.
116 * @buf: A buffer initialized with the eeprom data.
117 * @buf_size: Size of buf in bytes.
118 * @layout version: The version number of the layout.
119 */
120void eeprom_layout_setup(struct eeprom_layout *layout, unsigned char *buf,
121 unsigned int buf_size, int layout_version)
122{
123 int i;
124
125 if (layout_version == LAYOUT_VERSION_AUTODETECT)
126 layout->layout_version = eeprom_layout_detect(buf);
127 else
128 layout->layout_version = layout_version;
129
Marek Behúna804c8d2024-05-21 09:13:25 +0200130 layout->data_size = buf_size;
131 layout->print = eeprom_layout_print;
132 layout->update = eeprom_layout_update_field;
133
Nikita Kiryanovaa9e6042016-04-16 17:55:03 +0300134 eeprom_layout_assign(layout, layout_version);
135 layout->data = buf;
136 for (i = 0; i < layout->num_of_fields; i++) {
137 layout->fields[i].buf = buf;
138 buf += layout->fields[i].size;
139 }
Nikita Kiryanovaa9e6042016-04-16 17:55:03 +0300140}