smbios-parser.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2020, Bachmann electronic GmbH
  4. */
  5. #define LOG_CATEGORY LOGC_BOOT
  6. #include <common.h>
  7. #include <smbios.h>
  8. static inline int verify_checksum(const struct smbios_entry *e)
  9. {
  10. /*
  11. * Checksums for SMBIOS tables are calculated to have a value, so that
  12. * the sum over all bytes yields zero (using unsigned 8 bit arithmetic).
  13. */
  14. u8 *byte = (u8 *)e;
  15. u8 sum = 0;
  16. for (int i = 0; i < e->length; i++)
  17. sum += byte[i];
  18. return sum;
  19. }
  20. const struct smbios_entry *smbios_entry(u64 address, u32 size)
  21. {
  22. const struct smbios_entry *entry = (struct smbios_entry *)(uintptr_t)address;
  23. if (!address | !size)
  24. return NULL;
  25. if (memcmp(entry->anchor, "_SM_", 4))
  26. return NULL;
  27. if (verify_checksum(entry))
  28. return NULL;
  29. return entry;
  30. }
  31. static const struct smbios_header *next_header(const struct smbios_header *curr)
  32. {
  33. u8 *pos = ((u8 *)curr) + curr->length;
  34. /* search for _double_ NULL bytes */
  35. while (!((*pos == 0) && (*(pos + 1) == 0)))
  36. pos++;
  37. /* step behind the double NULL bytes */
  38. pos += 2;
  39. return (struct smbios_header *)pos;
  40. }
  41. const struct smbios_header *smbios_header(const struct smbios_entry *entry, int type)
  42. {
  43. const unsigned int num_header = entry->struct_count;
  44. const struct smbios_header *header = (struct smbios_header *)entry->struct_table_address;
  45. for (unsigned int i = 0; i < num_header; i++) {
  46. if (header->type == type)
  47. return header;
  48. header = next_header(header);
  49. }
  50. return NULL;
  51. }
  52. static const char *string_from_smbios_table(const struct smbios_header *header,
  53. int idx)
  54. {
  55. unsigned int i = 1;
  56. u8 *pos;
  57. if (!header)
  58. return NULL;
  59. pos = ((u8 *)header) + header->length;
  60. while (i < idx) {
  61. if (*pos == 0x0)
  62. i++;
  63. pos++;
  64. }
  65. return (const char *)pos;
  66. }
  67. const char *smbios_string(const struct smbios_header *header, int index)
  68. {
  69. if (!header)
  70. return NULL;
  71. return string_from_smbios_table(header, index);
  72. }
  73. int smbios_update_version_full(void *smbios_tab, const char *version)
  74. {
  75. const struct smbios_header *hdr;
  76. struct smbios_type0 *bios;
  77. uint old_len, len;
  78. char *ptr;
  79. log_info("Updating SMBIOS table at %p\n", smbios_tab);
  80. hdr = smbios_header(smbios_tab, SMBIOS_BIOS_INFORMATION);
  81. if (!hdr)
  82. return log_msg_ret("tab", -ENOENT);
  83. bios = (struct smbios_type0 *)hdr;
  84. ptr = (char *)smbios_string(hdr, bios->bios_ver);
  85. if (!ptr)
  86. return log_msg_ret("str", -ENOMEDIUM);
  87. /*
  88. * This string is supposed to have at least enough bytes and is
  89. * padded with spaces. Update it, taking care not to move the
  90. * \0 terminator, so that other strings in the string table
  91. * are not disturbed. See smbios_add_string()
  92. */
  93. old_len = strnlen(ptr, SMBIOS_STR_MAX);
  94. len = strnlen(version, SMBIOS_STR_MAX);
  95. if (len > old_len)
  96. return log_ret(-ENOSPC);
  97. log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr);
  98. memcpy(ptr, version, len);
  99. #ifdef LOG_DEBUG
  100. print_buffer((ulong)ptr, ptr, 1, old_len + 1, 0);
  101. #endif
  102. return 0;
  103. }