]> git.ipfire.org Git - people/ms/u-boot.git/blob - common/eeprom/eeprom_layout.c
binman: Add binman support to spl_ram.c
[people/ms/u-boot.git] / common / eeprom / eeprom_layout.c
1 /*
2 * (C) Copyright 2009-2016 CompuLab, Ltd.
3 *
4 * Authors: Nikita Kiryanov <nikita@compulab.co.il>
5 * Igor Grinberg <grinberg@compulab.co.il>
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 */
9
10 #include <common.h>
11 #include <linux/kernel.h>
12 #include <eeprom_layout.h>
13 #include <eeprom_field.h>
14
15 #define NO_LAYOUT_FIELDS "Unknown layout. Dumping raw data\n"
16
17 struct eeprom_field layout_unknown[1] = {
18 { NO_LAYOUT_FIELDS, 256, NULL, eeprom_field_print_bin,
19 eeprom_field_update_bin },
20 };
21
22 /*
23 * eeprom_layout_detect() - detect layout based on the contents of the data.
24 * @data: Pointer to the data to be analyzed.
25 *
26 * Returns: the detected layout version.
27 */
28 __weak int eeprom_layout_detect(unsigned char *data)
29 {
30 return LAYOUT_VERSION_UNRECOGNIZED;
31 }
32
33 /*
34 * __eeprom_layout_assign() - set the layout fields
35 * @layout: A pointer to an existing struct layout.
36 * @layout_version: The version number of the desired layout
37 */
38 __weak void __eeprom_layout_assign(struct eeprom_layout *layout,
39 int layout_version)
40 {
41 layout->fields = layout_unknown;
42 layout->num_of_fields = ARRAY_SIZE(layout_unknown);
43 }
44 void eeprom_layout_assign(struct eeprom_layout *layout, int layout_version) \
45 __attribute__((weak, alias("__eeprom_layout_assign")));
46
47 /*
48 * eeprom_layout_print() - print the layout and the data which is assigned to it
49 * @layout: A pointer to an existing struct layout.
50 */
51 static void eeprom_layout_print(const struct eeprom_layout *layout)
52 {
53 int i;
54 struct eeprom_field *fields = layout->fields;
55
56 for (i = 0; i < layout->num_of_fields; i++)
57 fields[i].print(&fields[i]);
58 }
59
60 /*
61 * eeprom_layout_update_field() - update a single field in the layout data.
62 * @layout: A pointer to an existing struct layout.
63 * @field_name: The name of the field to update.
64 * @new_data: The new field data (a string. Format depends on the field)
65 *
66 * Returns: 0 on success, negative error value on failure.
67 */
68 static int eeprom_layout_update_field(struct eeprom_layout *layout,
69 char *field_name, char *new_data)
70 {
71 int i, err;
72 struct eeprom_field *fields = layout->fields;
73
74 if (new_data == NULL)
75 return 0;
76
77 if (field_name == NULL)
78 return -1;
79
80 for (i = 0; i < layout->num_of_fields; i++) {
81 if (fields[i].name == RESERVED_FIELDS ||
82 strcmp(fields[i].name, field_name))
83 continue;
84
85 err = fields[i].update(&fields[i], new_data);
86 if (err)
87 printf("Invalid data for field %s\n", field_name);
88
89 return err;
90 }
91
92 printf("No such field '%s'\n", field_name);
93
94 return -1;
95 }
96
97 /*
98 * eeprom_layout_setup() - setup layout struct with the layout data and
99 * metadata as dictated by layout_version
100 * @layout: A pointer to an existing struct layout.
101 * @buf: A buffer initialized with the eeprom data.
102 * @buf_size: Size of buf in bytes.
103 * @layout version: The version number of the layout.
104 */
105 void eeprom_layout_setup(struct eeprom_layout *layout, unsigned char *buf,
106 unsigned int buf_size, int layout_version)
107 {
108 int i;
109
110 if (layout_version == LAYOUT_VERSION_AUTODETECT)
111 layout->layout_version = eeprom_layout_detect(buf);
112 else
113 layout->layout_version = layout_version;
114
115 eeprom_layout_assign(layout, layout_version);
116 layout->data = buf;
117 for (i = 0; i < layout->num_of_fields; i++) {
118 layout->fields[i].buf = buf;
119 buf += layout->fields[i].size;
120 }
121
122 layout->data_size = buf_size;
123 layout->print = eeprom_layout_print;
124 layout->update = eeprom_layout_update_field;
125 }