2 * (C) Copyright 2009-2016 CompuLab, Ltd.
4 * Authors: Nikita Kiryanov <nikita@compulab.co.il>
5 * Igor Grinberg <grinberg@compulab.co.il>
7 * SPDX-License-Identifier: GPL-2.0+
11 #include <linux/string.h>
12 #include <eeprom_field.h>
14 static void __eeprom_field_print_bin(const struct eeprom_field *field,
15 char *delimiter, bool reverse)
18 int from = reverse ? field->size - 1 : 0;
19 int to = reverse ? 0 : field->size - 1;
21 printf(PRINT_FIELD_SEGMENT, field->name);
22 for (i = from; i != to; reverse ? i-- : i++)
23 printf("%02x%s", field->buf[i], delimiter);
25 printf("%02x\n", field->buf[i]);
28 static int __eeprom_field_update_bin(struct eeprom_field *field,
29 const char *value, bool reverse)
31 int len = strlen(value);
32 int k, j, i = reverse ? len - 1 : 0;
36 /* each two characters in the string fit in one byte */
37 if (len > field->size * 2)
40 memset(field->buf, 0, field->size);
42 /* i - string iterator, j - buf iterator */
43 for (j = 0; j < field->size; j++) {
45 char tmp[3] = { 0, 0, 0 };
47 if ((reverse && i < 0) || (!reverse && i >= len))
50 for (k = 0; k < 2; k++) {
51 if (reverse && i == 0) {
56 tmp[k] = value[reverse ? i - 1 + k : i + k];
59 byte = simple_strtoul(tmp, &endptr, 0);
60 if (*endptr != '\0' || byte < 0)
64 i = reverse ? i - 2 : i + 2;
70 static int __eeprom_field_update_bin_delim(struct eeprom_field *field,
71 char *value, char *delimiter)
75 const char *tmp = value;
79 tmp = strstr(tmp, delimiter);
83 tmp = strstr(tmp, delimiter);
86 if (count > field->size)
89 tok = strtok(value, delimiter);
90 for (i = 0; tok && i < field->size; i++) {
91 val = simple_strtoul(tok, &endptr, 0);
95 /* here we assume that each tok is no more than byte long */
96 field->buf[i] = (unsigned char)val;
97 tok = strtok(NULL, delimiter);
104 * eeprom_field_print_bin() - print a field which contains binary data
106 * Treat the field data as simple binary data, and print it as two digit
107 * hexadecimal values.
109 * Field Name 0102030405060708090a
111 * @field: an initialized field to print
113 void eeprom_field_print_bin(const struct eeprom_field *field)
115 __eeprom_field_print_bin(field, "", false);
119 * eeprom_field_update_bin() - Update field with new data in binary form
121 * @field: an initialized field
122 * @value: a string of values (i.e. "10b234a")
124 int eeprom_field_update_bin(struct eeprom_field *field, char *value)
126 return __eeprom_field_update_bin(field, value, false);
130 * eeprom_field_update_reserved() - Update reserved field with new data in
133 * @field: an initialized field
134 * @value: a space delimited string of byte values (i.e. "1 02 3 0x4")
136 int eeprom_field_update_reserved(struct eeprom_field *field, char *value)
138 return __eeprom_field_update_bin_delim(field, value, " ");
142 * eeprom_field_print_bin_rev() - print a field which contains binary data in
145 * Treat the field data as simple binary data, and print it in reverse order
146 * as two digit hexadecimal values.
149 * 0102030405060708090a
151 * Field Name 0a090807060504030201
153 * @field: an initialized field to print
155 void eeprom_field_print_bin_rev(const struct eeprom_field *field)
157 __eeprom_field_print_bin(field, "", true);
161 * eeprom_field_update_bin_rev() - Update field with new data in binary form,
162 * storing it in reverse
164 * This function takes a string of byte values, and stores them
165 * in the field in the reverse order. i.e. if the input string was "1234",
166 * "3412" will be written to the field.
168 * @field: an initialized field
169 * @value: a string of byte values
171 int eeprom_field_update_bin_rev(struct eeprom_field *field, char *value)
173 return __eeprom_field_update_bin(field, value, true);
177 * eeprom_field_print_mac_addr() - print a field which contains a mac address
179 * Treat the field data as simple binary data, and print it formatted as a MAC
182 * Field Name 01:02:03:04:05:06
184 * @field: an initialized field to print
186 void eeprom_field_print_mac(const struct eeprom_field *field)
188 __eeprom_field_print_bin(field, ":", false);
192 * eeprom_field_update_mac() - Update a mac address field which contains binary
195 * @field: an initialized field
196 * @value: a colon delimited string of byte values (i.e. "1:02:3:ff")
198 int eeprom_field_update_mac(struct eeprom_field *field, char *value)
200 return __eeprom_field_update_bin_delim(field, value, ":");
204 * eeprom_field_print_ascii() - print a field which contains ASCII data
205 * @field: an initialized field to print
207 void eeprom_field_print_ascii(const struct eeprom_field *field)
211 sprintf(format, "%%.%ds\n", field->size);
212 printf(PRINT_FIELD_SEGMENT, field->name);
213 printf(format, field->buf);
217 * eeprom_field_update_ascii() - Update field with new data in ASCII form
218 * @field: an initialized field
219 * @value: the new string data
221 * Returns 0 on success, -1 of failure (new string too long).
223 int eeprom_field_update_ascii(struct eeprom_field *field, char *value)
225 if (strlen(value) >= field->size) {
226 printf("%s: new data too long\n", field->name);
230 strncpy((char *)field->buf, value, field->size - 1);
231 field->buf[field->size - 1] = '\0';
237 * eeprom_field_print_reserved() - print the "Reserved fields" field
239 * Print a notice that the following field_size bytes are reserved.
242 * Reserved fields (64 bytes)
244 * @field: an initialized field to print
246 void eeprom_field_print_reserved(const struct eeprom_field *field)
248 printf(PRINT_FIELD_SEGMENT, "Reserved fields\t");
249 printf("(%d bytes)\n", field->size);