eeprom_layout.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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/kernel.h>
  11. #include <eeprom_layout.h>
  12. #include <eeprom_field.h>
  13. #define NO_LAYOUT_FIELDS "Unknown layout. Dumping raw data\n"
  14. struct eeprom_field layout_unknown[1] = {
  15. { NO_LAYOUT_FIELDS, 256, NULL, eeprom_field_print_bin,
  16. eeprom_field_update_bin },
  17. };
  18. /*
  19. * eeprom_layout_detect() - detect layout based on the contents of the data.
  20. * @data: Pointer to the data to be analyzed.
  21. *
  22. * Returns: the detected layout version.
  23. */
  24. __weak int eeprom_layout_detect(unsigned char *data)
  25. {
  26. return LAYOUT_VERSION_UNRECOGNIZED;
  27. }
  28. /*
  29. * __eeprom_layout_assign() - set the layout fields
  30. * @layout: A pointer to an existing struct layout.
  31. * @layout_version: The version number of the desired layout
  32. */
  33. __weak void __eeprom_layout_assign(struct eeprom_layout *layout,
  34. int layout_version)
  35. {
  36. layout->fields = layout_unknown;
  37. layout->num_of_fields = ARRAY_SIZE(layout_unknown);
  38. }
  39. void eeprom_layout_assign(struct eeprom_layout *layout, int layout_version) \
  40. __attribute__((weak, alias("__eeprom_layout_assign")));
  41. /*
  42. * eeprom_layout_print() - print the layout and the data which is assigned to it
  43. * @layout: A pointer to an existing struct layout.
  44. */
  45. static void eeprom_layout_print(const struct eeprom_layout *layout)
  46. {
  47. int i;
  48. struct eeprom_field *fields = layout->fields;
  49. for (i = 0; i < layout->num_of_fields; i++)
  50. fields[i].print(&fields[i]);
  51. }
  52. /*
  53. * eeprom_layout_update_field() - update a single field in the layout data.
  54. * @layout: A pointer to an existing struct layout.
  55. * @field_name: The name of the field to update.
  56. * @new_data: The new field data (a string. Format depends on the field)
  57. *
  58. * Returns: 0 on success, negative error value on failure.
  59. */
  60. static int eeprom_layout_update_field(struct eeprom_layout *layout,
  61. char *field_name, char *new_data)
  62. {
  63. int i, err;
  64. struct eeprom_field *fields = layout->fields;
  65. if (new_data == NULL)
  66. return 0;
  67. if (field_name == NULL)
  68. return -1;
  69. for (i = 0; i < layout->num_of_fields; i++) {
  70. if (fields[i].name == RESERVED_FIELDS ||
  71. strcmp(fields[i].name, field_name))
  72. continue;
  73. err = fields[i].update(&fields[i], new_data);
  74. if (err)
  75. printf("Invalid data for field %s\n", field_name);
  76. return err;
  77. }
  78. printf("No such field '%s'\n", field_name);
  79. return -1;
  80. }
  81. /*
  82. * eeprom_layout_setup() - setup layout struct with the layout data and
  83. * metadata as dictated by layout_version
  84. * @layout: A pointer to an existing struct layout.
  85. * @buf: A buffer initialized with the eeprom data.
  86. * @buf_size: Size of buf in bytes.
  87. * @layout version: The version number of the layout.
  88. */
  89. void eeprom_layout_setup(struct eeprom_layout *layout, unsigned char *buf,
  90. unsigned int buf_size, int layout_version)
  91. {
  92. int i;
  93. if (layout_version == LAYOUT_VERSION_AUTODETECT)
  94. layout->layout_version = eeprom_layout_detect(buf);
  95. else
  96. layout->layout_version = layout_version;
  97. eeprom_layout_assign(layout, layout_version);
  98. layout->data = buf;
  99. for (i = 0; i < layout->num_of_fields; i++) {
  100. layout->fields[i].buf = buf;
  101. buf += layout->fields[i].size;
  102. }
  103. layout->data_size = buf_size;
  104. layout->print = eeprom_layout_print;
  105. layout->update = eeprom_layout_update_field;
  106. }