eeprom_layout.c 3.3 KB

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