eeprom_field.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2009-2016 CompuLab, Ltd.
  4. *
  5. * Authors: Nikita Kiryanov <nikita@compulab.co.il>
  6. * Igor Grinberg <grinberg@compulab.co.il>
  7. */
  8. #include <common.h>
  9. #include <linux/string.h>
  10. #include <eeprom_field.h>
  11. static void __eeprom_field_print_bin(const struct eeprom_field *field,
  12. char *delimiter, bool reverse)
  13. {
  14. int i;
  15. int from = reverse ? field->size - 1 : 0;
  16. int to = reverse ? 0 : field->size - 1;
  17. printf(PRINT_FIELD_SEGMENT, field->name);
  18. for (i = from; i != to; reverse ? i-- : i++)
  19. printf("%02x%s", field->buf[i], delimiter);
  20. printf("%02x\n", field->buf[i]);
  21. }
  22. static int __eeprom_field_update_bin(struct eeprom_field *field,
  23. const char *value, bool reverse)
  24. {
  25. int len = strlen(value);
  26. int k, j, i = reverse ? len - 1 : 0;
  27. unsigned char byte;
  28. char *endptr;
  29. /* each two characters in the string fit in one byte */
  30. if (len > field->size * 2)
  31. return -1;
  32. memset(field->buf, 0, field->size);
  33. /* i - string iterator, j - buf iterator */
  34. for (j = 0; j < field->size; j++) {
  35. byte = 0;
  36. char tmp[3] = { 0, 0, 0 };
  37. if ((reverse && i < 0) || (!reverse && i >= len))
  38. break;
  39. for (k = 0; k < 2; k++) {
  40. if (reverse && i == 0) {
  41. tmp[k] = value[i];
  42. break;
  43. }
  44. tmp[k] = value[reverse ? i - 1 + k : i + k];
  45. }
  46. byte = simple_strtoul(tmp, &endptr, 0);
  47. if (*endptr != '\0' || byte < 0)
  48. return -1;
  49. field->buf[j] = byte;
  50. i = reverse ? i - 2 : i + 2;
  51. }
  52. return 0;
  53. }
  54. static int __eeprom_field_update_bin_delim(struct eeprom_field *field,
  55. char *value, char *delimiter)
  56. {
  57. int count = 0;
  58. int i, val;
  59. const char *tmp = value;
  60. char *tok;
  61. char *endptr;
  62. tmp = strstr(tmp, delimiter);
  63. while (tmp != NULL) {
  64. count++;
  65. tmp++;
  66. tmp = strstr(tmp, delimiter);
  67. }
  68. if (count > field->size)
  69. return -1;
  70. tok = strtok(value, delimiter);
  71. for (i = 0; tok && i < field->size; i++) {
  72. val = simple_strtoul(tok, &endptr, 0);
  73. if (*endptr != '\0')
  74. return -1;
  75. /* here we assume that each tok is no more than byte long */
  76. field->buf[i] = (unsigned char)val;
  77. tok = strtok(NULL, delimiter);
  78. }
  79. return 0;
  80. }
  81. /**
  82. * eeprom_field_print_bin() - print a field which contains binary data
  83. *
  84. * Treat the field data as simple binary data, and print it as two digit
  85. * hexadecimal values.
  86. * Sample output:
  87. * Field Name 0102030405060708090a
  88. *
  89. * @field: an initialized field to print
  90. */
  91. void eeprom_field_print_bin(const struct eeprom_field *field)
  92. {
  93. __eeprom_field_print_bin(field, "", false);
  94. }
  95. /**
  96. * eeprom_field_update_bin() - Update field with new data in binary form
  97. *
  98. * @field: an initialized field
  99. * @value: a string of values (i.e. "10b234a")
  100. */
  101. int eeprom_field_update_bin(struct eeprom_field *field, char *value)
  102. {
  103. return __eeprom_field_update_bin(field, value, false);
  104. }
  105. /**
  106. * eeprom_field_update_reserved() - Update reserved field with new data in
  107. * binary form
  108. *
  109. * @field: an initialized field
  110. * @value: a space delimited string of byte values (i.e. "1 02 3 0x4")
  111. */
  112. int eeprom_field_update_reserved(struct eeprom_field *field, char *value)
  113. {
  114. return __eeprom_field_update_bin_delim(field, value, " ");
  115. }
  116. /**
  117. * eeprom_field_print_bin_rev() - print a field which contains binary data in
  118. * reverse order
  119. *
  120. * Treat the field data as simple binary data, and print it in reverse order
  121. * as two digit hexadecimal values.
  122. *
  123. * Data in field:
  124. * 0102030405060708090a
  125. * Sample output:
  126. * Field Name 0a090807060504030201
  127. *
  128. * @field: an initialized field to print
  129. */
  130. void eeprom_field_print_bin_rev(const struct eeprom_field *field)
  131. {
  132. __eeprom_field_print_bin(field, "", true);
  133. }
  134. /**
  135. * eeprom_field_update_bin_rev() - Update field with new data in binary form,
  136. * storing it in reverse
  137. *
  138. * This function takes a string of byte values, and stores them
  139. * in the field in the reverse order. i.e. if the input string was "1234",
  140. * "3412" will be written to the field.
  141. *
  142. * @field: an initialized field
  143. * @value: a string of byte values
  144. */
  145. int eeprom_field_update_bin_rev(struct eeprom_field *field, char *value)
  146. {
  147. return __eeprom_field_update_bin(field, value, true);
  148. }
  149. /**
  150. * eeprom_field_print_mac_addr() - print a field which contains a mac address
  151. *
  152. * Treat the field data as simple binary data, and print it formatted as a MAC
  153. * address.
  154. * Sample output:
  155. * Field Name 01:02:03:04:05:06
  156. *
  157. * @field: an initialized field to print
  158. */
  159. void eeprom_field_print_mac(const struct eeprom_field *field)
  160. {
  161. __eeprom_field_print_bin(field, ":", false);
  162. }
  163. /**
  164. * eeprom_field_update_mac() - Update a mac address field which contains binary
  165. * data
  166. *
  167. * @field: an initialized field
  168. * @value: a colon delimited string of byte values (i.e. "1:02:3:ff")
  169. */
  170. int eeprom_field_update_mac(struct eeprom_field *field, char *value)
  171. {
  172. return __eeprom_field_update_bin_delim(field, value, ":");
  173. }
  174. /**
  175. * eeprom_field_print_ascii() - print a field which contains ASCII data
  176. * @field: an initialized field to print
  177. */
  178. void eeprom_field_print_ascii(const struct eeprom_field *field)
  179. {
  180. char format[8];
  181. sprintf(format, "%%.%ds\n", field->size);
  182. printf(PRINT_FIELD_SEGMENT, field->name);
  183. printf(format, field->buf);
  184. }
  185. /**
  186. * eeprom_field_update_ascii() - Update field with new data in ASCII form
  187. * @field: an initialized field
  188. * @value: the new string data
  189. *
  190. * Returns 0 on success, -1 of failure (new string too long).
  191. */
  192. int eeprom_field_update_ascii(struct eeprom_field *field, char *value)
  193. {
  194. if (strlen(value) >= field->size) {
  195. printf("%s: new data too long\n", field->name);
  196. return -1;
  197. }
  198. strncpy((char *)field->buf, value, field->size - 1);
  199. field->buf[field->size - 1] = '\0';
  200. return 0;
  201. }
  202. /**
  203. * eeprom_field_print_reserved() - print the "Reserved fields" field
  204. *
  205. * Print a notice that the following field_size bytes are reserved.
  206. *
  207. * Sample output:
  208. * Reserved fields (64 bytes)
  209. *
  210. * @field: an initialized field to print
  211. */
  212. void eeprom_field_print_reserved(const struct eeprom_field *field)
  213. {
  214. printf(PRINT_FIELD_SEGMENT, "Reserved fields\t");
  215. printf("(%d bytes)\n", field->size);
  216. }