eeprom_field.c 6.0 KB

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