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