1
2
3
4
5
6
7
8
9
10#include <common.h>
11#include <linux/string.h>
12#include <eeprom_field.h>
13
14static void __eeprom_field_print_bin(const struct eeprom_field *field,
15 char *delimiter, bool reverse)
16{
17 int i;
18 int from = reverse ? field->size - 1 : 0;
19 int to = reverse ? 0 : field->size - 1;
20
21 printf(PRINT_FIELD_SEGMENT, field->name);
22 for (i = from; i != to; reverse ? i-- : i++)
23 printf("%02x%s", field->buf[i], delimiter);
24
25 printf("%02x\n", field->buf[i]);
26}
27
28static int __eeprom_field_update_bin(struct eeprom_field *field,
29 const char *value, bool reverse)
30{
31 int len = strlen(value);
32 int k, j, i = reverse ? len - 1 : 0;
33 unsigned char byte;
34 char *endptr;
35
36
37 if (len > field->size * 2)
38 return -1;
39
40 memset(field->buf, 0, field->size);
41
42
43 for (j = 0; j < field->size; j++) {
44 byte = 0;
45 char tmp[3] = { 0, 0, 0 };
46
47 if ((reverse && i < 0) || (!reverse && i >= len))
48 break;
49
50 for (k = 0; k < 2; k++) {
51 if (reverse && i == 0) {
52 tmp[k] = value[i];
53 break;
54 }
55
56 tmp[k] = value[reverse ? i - 1 + k : i + k];
57 }
58
59 byte = simple_strtoul(tmp, &endptr, 0);
60 if (*endptr != '\0' || byte < 0)
61 return -1;
62
63 field->buf[j] = byte;
64 i = reverse ? i - 2 : i + 2;
65 }
66
67 return 0;
68}
69
70static int __eeprom_field_update_bin_delim(struct eeprom_field *field,
71 char *value, char *delimiter)
72{
73 int count = 0;
74 int i, val;
75 const char *tmp = value;
76 char *tok;
77 char *endptr;
78
79 tmp = strstr(tmp, delimiter);
80 while (tmp != NULL) {
81 count++;
82 tmp++;
83 tmp = strstr(tmp, delimiter);
84 }
85
86 if (count > field->size)
87 return -1;
88
89 tok = strtok(value, delimiter);
90 for (i = 0; tok && i < field->size; i++) {
91 val = simple_strtoul(tok, &endptr, 0);
92 if (*endptr != '\0')
93 return -1;
94
95
96 field->buf[i] = (unsigned char)val;
97 tok = strtok(NULL, delimiter);
98 }
99
100 return 0;
101}
102
103
104
105
106
107
108
109
110
111
112
113void eeprom_field_print_bin(const struct eeprom_field *field)
114{
115 __eeprom_field_print_bin(field, "", false);
116}
117
118
119
120
121
122
123
124int eeprom_field_update_bin(struct eeprom_field *field, char *value)
125{
126 return __eeprom_field_update_bin(field, value, false);
127}
128
129
130
131
132
133
134
135
136int eeprom_field_update_reserved(struct eeprom_field *field, char *value)
137{
138 return __eeprom_field_update_bin_delim(field, value, " ");
139}
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155void eeprom_field_print_bin_rev(const struct eeprom_field *field)
156{
157 __eeprom_field_print_bin(field, "", true);
158}
159
160
161
162
163
164
165
166
167
168
169
170
171int eeprom_field_update_bin_rev(struct eeprom_field *field, char *value)
172{
173 return __eeprom_field_update_bin(field, value, true);
174}
175
176
177
178
179
180
181
182
183
184
185
186void eeprom_field_print_mac(const struct eeprom_field *field)
187{
188 __eeprom_field_print_bin(field, ":", false);
189}
190
191
192
193
194
195
196
197
198int eeprom_field_update_mac(struct eeprom_field *field, char *value)
199{
200 return __eeprom_field_update_bin_delim(field, value, ":");
201}
202
203
204
205
206
207void eeprom_field_print_ascii(const struct eeprom_field *field)
208{
209 char format[8];
210
211 sprintf(format, "%%.%ds\n", field->size);
212 printf(PRINT_FIELD_SEGMENT, field->name);
213 printf(format, field->buf);
214}
215
216
217
218
219
220
221
222
223int eeprom_field_update_ascii(struct eeprom_field *field, char *value)
224{
225 if (strlen(value) >= field->size) {
226 printf("%s: new data too long\n", field->name);
227 return -1;
228 }
229
230 strncpy((char *)field->buf, value, field->size - 1);
231 field->buf[field->size - 1] = '\0';
232
233 return 0;
234}
235
236
237
238
239
240
241
242
243
244
245
246void eeprom_field_print_reserved(const struct eeprom_field *field)
247{
248 printf(PRINT_FIELD_SEGMENT, "Reserved fields\t");
249 printf("(%d bytes)\n", field->size);
250}
251