]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - common/eeprom/eeprom_field.c
1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2009-2016 CompuLab, Ltd.
5 * Authors: Nikita Kiryanov <nikita@compulab.co.il>
6 * Igor Grinberg <grinberg@compulab.co.il>
10 #include <linux/string.h>
11 #include <eeprom_field.h>
13 static void __eeprom_field_print_bin(const struct eeprom_field
*field
,
14 char *delimiter
, bool reverse
)
17 int from
= reverse
? field
->size
- 1 : 0;
18 int to
= reverse
? 0 : field
->size
- 1;
20 printf(PRINT_FIELD_SEGMENT
, field
->name
);
21 for (i
= from
; i
!= to
; reverse
? i
-- : i
++)
22 printf("%02x%s", field
->buf
[i
], delimiter
);
24 printf("%02x\n", field
->buf
[i
]);
27 static int __eeprom_field_update_bin(struct eeprom_field
*field
,
28 const char *value
, bool reverse
)
30 int len
= strlen(value
);
31 int k
, j
, i
= reverse
? len
- 1 : 0;
35 /* each two characters in the string fit in one byte */
36 if (len
> field
->size
* 2)
39 memset(field
->buf
, 0, field
->size
);
41 /* i - string iterator, j - buf iterator */
42 for (j
= 0; j
< field
->size
; j
++) {
44 char tmp
[3] = { 0, 0, 0 };
46 if ((reverse
&& i
< 0) || (!reverse
&& i
>= len
))
49 for (k
= 0; k
< 2; k
++) {
50 if (reverse
&& i
== 0) {
55 tmp
[k
] = value
[reverse
? i
- 1 + k
: i
+ k
];
58 byte
= simple_strtoul(tmp
, &endptr
, 0);
59 if (*endptr
!= '\0' || byte
< 0)
63 i
= reverse
? i
- 2 : i
+ 2;
69 static int __eeprom_field_update_bin_delim(struct eeprom_field
*field
,
70 char *value
, char *delimiter
)
74 const char *tmp
= value
;
78 tmp
= strstr(tmp
, delimiter
);
82 tmp
= strstr(tmp
, delimiter
);
85 if (count
> field
->size
)
88 tok
= strtok(value
, delimiter
);
89 for (i
= 0; tok
&& i
< field
->size
; i
++) {
90 val
= simple_strtoul(tok
, &endptr
, 0);
94 /* here we assume that each tok is no more than byte long */
95 field
->buf
[i
] = (unsigned char)val
;
96 tok
= strtok(NULL
, delimiter
);
103 * eeprom_field_print_bin() - print a field which contains binary data
105 * Treat the field data as simple binary data, and print it as two digit
106 * hexadecimal values.
108 * Field Name 0102030405060708090a
110 * @field: an initialized field to print
112 void eeprom_field_print_bin(const struct eeprom_field
*field
)
114 __eeprom_field_print_bin(field
, "", false);
118 * eeprom_field_update_bin() - Update field with new data in binary form
120 * @field: an initialized field
121 * @value: a string of values (i.e. "10b234a")
123 int eeprom_field_update_bin(struct eeprom_field
*field
, char *value
)
125 return __eeprom_field_update_bin(field
, value
, false);
129 * eeprom_field_update_reserved() - Update reserved field with new data in
132 * @field: an initialized field
133 * @value: a space delimited string of byte values (i.e. "1 02 3 0x4")
135 int eeprom_field_update_reserved(struct eeprom_field
*field
, char *value
)
137 return __eeprom_field_update_bin_delim(field
, value
, " ");
141 * eeprom_field_print_bin_rev() - print a field which contains binary data in
144 * Treat the field data as simple binary data, and print it in reverse order
145 * as two digit hexadecimal values.
148 * 0102030405060708090a
150 * Field Name 0a090807060504030201
152 * @field: an initialized field to print
154 void eeprom_field_print_bin_rev(const struct eeprom_field
*field
)
156 __eeprom_field_print_bin(field
, "", true);
160 * eeprom_field_update_bin_rev() - Update field with new data in binary form,
161 * storing it in reverse
163 * This function takes a string of byte values, and stores them
164 * in the field in the reverse order. i.e. if the input string was "1234",
165 * "3412" will be written to the field.
167 * @field: an initialized field
168 * @value: a string of byte values
170 int eeprom_field_update_bin_rev(struct eeprom_field
*field
, char *value
)
172 return __eeprom_field_update_bin(field
, value
, true);
176 * eeprom_field_print_mac_addr() - print a field which contains a mac address
178 * Treat the field data as simple binary data, and print it formatted as a MAC
181 * Field Name 01:02:03:04:05:06
183 * @field: an initialized field to print
185 void eeprom_field_print_mac(const struct eeprom_field
*field
)
187 __eeprom_field_print_bin(field
, ":", false);
191 * eeprom_field_update_mac() - Update a mac address field which contains binary
194 * @field: an initialized field
195 * @value: a colon delimited string of byte values (i.e. "1:02:3:ff")
197 int eeprom_field_update_mac(struct eeprom_field
*field
, char *value
)
199 return __eeprom_field_update_bin_delim(field
, value
, ":");
203 * eeprom_field_print_ascii() - print a field which contains ASCII data
204 * @field: an initialized field to print
206 void eeprom_field_print_ascii(const struct eeprom_field
*field
)
210 sprintf(format
, "%%.%ds\n", field
->size
);
211 printf(PRINT_FIELD_SEGMENT
, field
->name
);
212 printf(format
, field
->buf
);
216 * eeprom_field_update_ascii() - Update field with new data in ASCII form
217 * @field: an initialized field
218 * @value: the new string data
220 * Returns 0 on success, -1 of failure (new string too long).
222 int eeprom_field_update_ascii(struct eeprom_field
*field
, char *value
)
224 if (strlen(value
) >= field
->size
) {
225 printf("%s: new data too long\n", field
->name
);
229 strncpy((char *)field
->buf
, value
, field
->size
- 1);
230 field
->buf
[field
->size
- 1] = '\0';
236 * eeprom_field_print_reserved() - print the "Reserved fields" field
238 * Print a notice that the following field_size bytes are reserved.
241 * Reserved fields (64 bytes)
243 * @field: an initialized field to print
245 void eeprom_field_print_reserved(const struct eeprom_field
*field
)
247 printf(PRINT_FIELD_SEGMENT
, "Reserved fields\t");
248 printf("(%d bytes)\n", field
->size
);