smbios-parser.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 u8 *find_next_header(u8 *pos)
  32. {
  33. /* search for _double_ NULL bytes */
  34. while (!((*pos == 0) && (*(pos + 1) == 0)))
  35. pos++;
  36. /* step behind the double NULL bytes */
  37. pos += 2;
  38. return pos;
  39. }
  40. static struct smbios_header *get_next_header(struct smbios_header *curr)
  41. {
  42. u8 *pos = ((u8 *)curr) + curr->length;
  43. return (struct smbios_header *)find_next_header(pos);
  44. }
  45. static const struct smbios_header *next_header(const struct smbios_header *curr)
  46. {
  47. u8 *pos = ((u8 *)curr) + curr->length;
  48. return (struct smbios_header *)find_next_header(pos);
  49. }
  50. const struct smbios_header *smbios_header(const struct smbios_entry *entry, int type)
  51. {
  52. const unsigned int num_header = entry->struct_count;
  53. const struct smbios_header *header = (struct smbios_header *)((uintptr_t)entry->struct_table_address);
  54. for (unsigned int i = 0; i < num_header; i++) {
  55. if (header->type == type)
  56. return header;
  57. header = next_header(header);
  58. }
  59. return NULL;
  60. }
  61. static char *string_from_smbios_table(const struct smbios_header *header,
  62. int idx)
  63. {
  64. unsigned int i = 1;
  65. u8 *pos;
  66. if (!header)
  67. return NULL;
  68. pos = ((u8 *)header) + header->length;
  69. while (i < idx) {
  70. if (*pos == 0x0)
  71. i++;
  72. pos++;
  73. }
  74. return (char *)pos;
  75. }
  76. char *smbios_string(const struct smbios_header *header, int index)
  77. {
  78. if (!header)
  79. return NULL;
  80. return string_from_smbios_table(header, index);
  81. }
  82. int smbios_update_version_full(void *smbios_tab, const char *version)
  83. {
  84. const struct smbios_header *hdr;
  85. struct smbios_type0 *bios;
  86. uint old_len, len;
  87. char *ptr;
  88. log_info("Updating SMBIOS table at %p\n", smbios_tab);
  89. hdr = smbios_header(smbios_tab, SMBIOS_BIOS_INFORMATION);
  90. if (!hdr)
  91. return log_msg_ret("tab", -ENOENT);
  92. bios = (struct smbios_type0 *)hdr;
  93. ptr = smbios_string(hdr, bios->bios_ver);
  94. if (!ptr)
  95. return log_msg_ret("str", -ENOMEDIUM);
  96. /*
  97. * This string is supposed to have at least enough bytes and is
  98. * padded with spaces. Update it, taking care not to move the
  99. * \0 terminator, so that other strings in the string table
  100. * are not disturbed. See smbios_add_string()
  101. */
  102. old_len = strnlen(ptr, SMBIOS_STR_MAX);
  103. len = strnlen(version, SMBIOS_STR_MAX);
  104. if (len > old_len)
  105. return log_ret(-ENOSPC);
  106. log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr);
  107. memcpy(ptr, version, len);
  108. #ifdef LOG_DEBUG
  109. print_buffer((ulong)ptr, ptr, 1, old_len + 1, 0);
  110. #endif
  111. return 0;
  112. }
  113. struct smbios_filter_param {
  114. u32 offset;
  115. u32 size;
  116. bool is_string;
  117. };
  118. struct smbios_filter_table {
  119. int type;
  120. struct smbios_filter_param *params;
  121. u32 count;
  122. };
  123. struct smbios_filter_param smbios_type1_filter_params[] = {
  124. {offsetof(struct smbios_type1, serial_number),
  125. FIELD_SIZEOF(struct smbios_type1, serial_number), true},
  126. {offsetof(struct smbios_type1, uuid),
  127. FIELD_SIZEOF(struct smbios_type1, uuid), false},
  128. {offsetof(struct smbios_type1, wakeup_type),
  129. FIELD_SIZEOF(struct smbios_type1, wakeup_type), false},
  130. };
  131. struct smbios_filter_param smbios_type2_filter_params[] = {
  132. {offsetof(struct smbios_type2, serial_number),
  133. FIELD_SIZEOF(struct smbios_type2, serial_number), true},
  134. {offsetof(struct smbios_type2, chassis_location),
  135. FIELD_SIZEOF(struct smbios_type2, chassis_location), false},
  136. };
  137. struct smbios_filter_param smbios_type3_filter_params[] = {
  138. {offsetof(struct smbios_type3, serial_number),
  139. FIELD_SIZEOF(struct smbios_type3, serial_number), true},
  140. {offsetof(struct smbios_type3, asset_tag_number),
  141. FIELD_SIZEOF(struct smbios_type3, asset_tag_number), true},
  142. };
  143. struct smbios_filter_param smbios_type4_filter_params[] = {
  144. {offsetof(struct smbios_type4, serial_number),
  145. FIELD_SIZEOF(struct smbios_type4, serial_number), true},
  146. {offsetof(struct smbios_type4, asset_tag),
  147. FIELD_SIZEOF(struct smbios_type4, asset_tag), true},
  148. {offsetof(struct smbios_type4, part_number),
  149. FIELD_SIZEOF(struct smbios_type4, part_number), true},
  150. {offsetof(struct smbios_type4, core_count),
  151. FIELD_SIZEOF(struct smbios_type4, core_count), false},
  152. {offsetof(struct smbios_type4, core_enabled),
  153. FIELD_SIZEOF(struct smbios_type4, core_enabled), false},
  154. {offsetof(struct smbios_type4, thread_count),
  155. FIELD_SIZEOF(struct smbios_type4, thread_count), false},
  156. {offsetof(struct smbios_type4, core_count2),
  157. FIELD_SIZEOF(struct smbios_type4, core_count2), false},
  158. {offsetof(struct smbios_type4, core_enabled2),
  159. FIELD_SIZEOF(struct smbios_type4, core_enabled2), false},
  160. {offsetof(struct smbios_type4, thread_count2),
  161. FIELD_SIZEOF(struct smbios_type4, thread_count2), false},
  162. {offsetof(struct smbios_type4, voltage),
  163. FIELD_SIZEOF(struct smbios_type4, voltage), false},
  164. };
  165. struct smbios_filter_table smbios_filter_tables[] = {
  166. {SMBIOS_SYSTEM_INFORMATION, smbios_type1_filter_params,
  167. ARRAY_SIZE(smbios_type1_filter_params)},
  168. {SMBIOS_BOARD_INFORMATION, smbios_type2_filter_params,
  169. ARRAY_SIZE(smbios_type2_filter_params)},
  170. {SMBIOS_SYSTEM_ENCLOSURE, smbios_type3_filter_params,
  171. ARRAY_SIZE(smbios_type3_filter_params)},
  172. {SMBIOS_PROCESSOR_INFORMATION, smbios_type4_filter_params,
  173. ARRAY_SIZE(smbios_type4_filter_params)},
  174. };
  175. static void clear_smbios_table(struct smbios_header *header,
  176. struct smbios_filter_param *filter,
  177. u32 count)
  178. {
  179. u32 i;
  180. char *str;
  181. u8 string_id;
  182. for (i = 0; i < count; i++) {
  183. if (filter[i].is_string) {
  184. string_id = *((u8 *)header + filter[i].offset);
  185. if (string_id == 0) /* string is empty */
  186. continue;
  187. str = smbios_string(header, string_id);
  188. if (!str)
  189. continue;
  190. /* string is cleared to space, keep '\0' terminator */
  191. memset(str, ' ', strlen(str));
  192. } else {
  193. memset((void *)((u8 *)header + filter[i].offset),
  194. 0, filter[i].size);
  195. }
  196. }
  197. }
  198. void smbios_prepare_measurement(const struct smbios_entry *entry,
  199. struct smbios_header *smbios_copy)
  200. {
  201. u32 i, j;
  202. struct smbios_header *header;
  203. for (i = 0; i < ARRAY_SIZE(smbios_filter_tables); i++) {
  204. header = smbios_copy;
  205. for (j = 0; j < entry->struct_count; j++) {
  206. if (header->type == smbios_filter_tables[i].type)
  207. break;
  208. header = get_next_header(header);
  209. }
  210. if (j >= entry->struct_count)
  211. continue;
  212. clear_smbios_table(header,
  213. smbios_filter_tables[i].params,
  214. smbios_filter_tables[i].count);
  215. }
  216. }