smbios.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
  3. *
  4. * Adapted from coreboot src/arch/x86/smbios.c
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <smbios.h>
  10. #include <tables_csum.h>
  11. #include <version.h>
  12. #ifdef CONFIG_CPU
  13. #include <cpu.h>
  14. #include <dm.h>
  15. #include <dm/uclass-internal.h>
  16. #endif
  17. DECLARE_GLOBAL_DATA_PTR;
  18. /**
  19. * smbios_add_string() - add a string to the string area
  20. *
  21. * This adds a string to the string area which is appended directly after
  22. * the formatted portion of an SMBIOS structure.
  23. *
  24. * @start: string area start address
  25. * @str: string to add
  26. * @return: string number in the string area
  27. */
  28. static int smbios_add_string(char *start, const char *str)
  29. {
  30. int i = 1;
  31. char *p = start;
  32. for (;;) {
  33. if (!*p) {
  34. strcpy(p, str);
  35. p += strlen(str);
  36. *p++ = '\0';
  37. *p++ = '\0';
  38. return i;
  39. }
  40. if (!strcmp(p, str))
  41. return i;
  42. p += strlen(p) + 1;
  43. i++;
  44. }
  45. }
  46. /**
  47. * smbios_string_table_len() - compute the string area size
  48. *
  49. * This computes the size of the string area including the string terminator.
  50. *
  51. * @start: string area start address
  52. * @return: string area size
  53. */
  54. static int smbios_string_table_len(char *start)
  55. {
  56. char *p = start;
  57. int i, len = 0;
  58. while (*p) {
  59. i = strlen(p) + 1;
  60. p += i;
  61. len += i;
  62. }
  63. return len + 1;
  64. }
  65. static int smbios_write_type0(uintptr_t *current, int handle)
  66. {
  67. struct smbios_type0 *t = (struct smbios_type0 *)*current;
  68. int len = sizeof(struct smbios_type0);
  69. memset(t, 0, sizeof(struct smbios_type0));
  70. fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
  71. t->vendor = smbios_add_string(t->eos, "U-Boot");
  72. t->bios_ver = smbios_add_string(t->eos, PLAIN_VERSION);
  73. t->bios_release_date = smbios_add_string(t->eos, U_BOOT_DMI_DATE);
  74. #ifdef CONFIG_ROM_SIZE
  75. t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
  76. #endif
  77. t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
  78. BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
  79. BIOS_CHARACTERISTICS_UPGRADEABLE;
  80. #ifdef CONFIG_GENERATE_ACPI_TABLE
  81. t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
  82. #endif
  83. #ifdef CONFIG_EFI_LOADER
  84. t->bios_characteristics_ext1 |= BIOS_CHARACTERISTICS_EXT1_UEFI;
  85. #endif
  86. t->bios_characteristics_ext2 = BIOS_CHARACTERISTICS_EXT2_TARGET;
  87. t->bios_major_release = 0xff;
  88. t->bios_minor_release = 0xff;
  89. t->ec_major_release = 0xff;
  90. t->ec_minor_release = 0xff;
  91. len = t->length + smbios_string_table_len(t->eos);
  92. *current += len;
  93. return len;
  94. }
  95. static int smbios_write_type1(uintptr_t *current, int handle)
  96. {
  97. struct smbios_type1 *t = (struct smbios_type1 *)*current;
  98. int len = sizeof(struct smbios_type1);
  99. char *serial_str = getenv("serial#");
  100. memset(t, 0, sizeof(struct smbios_type1));
  101. fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
  102. t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
  103. t->product_name = smbios_add_string(t->eos, CONFIG_SMBIOS_PRODUCT_NAME);
  104. if (serial_str) {
  105. strncpy((char*)t->uuid, serial_str, sizeof(t->uuid));
  106. t->serial_number = smbios_add_string(t->eos, serial_str);
  107. }
  108. len = t->length + smbios_string_table_len(t->eos);
  109. *current += len;
  110. return len;
  111. }
  112. static int smbios_write_type2(uintptr_t *current, int handle)
  113. {
  114. struct smbios_type2 *t = (struct smbios_type2 *)*current;
  115. int len = sizeof(struct smbios_type2);
  116. memset(t, 0, sizeof(struct smbios_type2));
  117. fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
  118. t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
  119. t->product_name = smbios_add_string(t->eos, CONFIG_SMBIOS_PRODUCT_NAME);
  120. t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
  121. t->board_type = SMBIOS_BOARD_MOTHERBOARD;
  122. len = t->length + smbios_string_table_len(t->eos);
  123. *current += len;
  124. return len;
  125. }
  126. static int smbios_write_type3(uintptr_t *current, int handle)
  127. {
  128. struct smbios_type3 *t = (struct smbios_type3 *)*current;
  129. int len = sizeof(struct smbios_type3);
  130. memset(t, 0, sizeof(struct smbios_type3));
  131. fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
  132. t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
  133. t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
  134. t->bootup_state = SMBIOS_STATE_SAFE;
  135. t->power_supply_state = SMBIOS_STATE_SAFE;
  136. t->thermal_state = SMBIOS_STATE_SAFE;
  137. t->security_status = SMBIOS_SECURITY_NONE;
  138. len = t->length + smbios_string_table_len(t->eos);
  139. *current += len;
  140. return len;
  141. }
  142. static void smbios_write_type4_dm(struct smbios_type4 *t)
  143. {
  144. u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
  145. const char *vendor = "Unknown";
  146. const char *name = "Unknown";
  147. #ifdef CONFIG_CPU
  148. char processor_name[49];
  149. char vendor_name[49];
  150. struct udevice *dev = NULL;
  151. uclass_find_first_device(UCLASS_CPU, &dev);
  152. if (dev) {
  153. struct cpu_platdata *plat = dev_get_parent_platdata(dev);
  154. if (plat->family)
  155. processor_family = plat->family;
  156. t->processor_id[0] = plat->id[0];
  157. t->processor_id[1] = plat->id[1];
  158. if (!cpu_get_vendor(dev, vendor_name, sizeof(vendor_name)))
  159. vendor = vendor_name;
  160. if (!cpu_get_desc(dev, processor_name, sizeof(processor_name)))
  161. name = processor_name;
  162. }
  163. #endif
  164. t->processor_family = processor_family;
  165. t->processor_manufacturer = smbios_add_string(t->eos, vendor);
  166. t->processor_version = smbios_add_string(t->eos, name);
  167. }
  168. static int smbios_write_type4(uintptr_t *current, int handle)
  169. {
  170. struct smbios_type4 *t = (struct smbios_type4 *)*current;
  171. int len = sizeof(struct smbios_type4);
  172. memset(t, 0, sizeof(struct smbios_type4));
  173. fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
  174. t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
  175. smbios_write_type4_dm(t);
  176. t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
  177. t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
  178. t->l1_cache_handle = 0xffff;
  179. t->l2_cache_handle = 0xffff;
  180. t->l3_cache_handle = 0xffff;
  181. t->processor_family2 = t->processor_family;
  182. len = t->length + smbios_string_table_len(t->eos);
  183. *current += len;
  184. return len;
  185. }
  186. static int smbios_write_type32(uintptr_t *current, int handle)
  187. {
  188. struct smbios_type32 *t = (struct smbios_type32 *)*current;
  189. int len = sizeof(struct smbios_type32);
  190. memset(t, 0, sizeof(struct smbios_type32));
  191. fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
  192. *current += len;
  193. return len;
  194. }
  195. static int smbios_write_type127(uintptr_t *current, int handle)
  196. {
  197. struct smbios_type127 *t = (struct smbios_type127 *)*current;
  198. int len = sizeof(struct smbios_type127);
  199. memset(t, 0, sizeof(struct smbios_type127));
  200. fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
  201. *current += len;
  202. return len;
  203. }
  204. static smbios_write_type smbios_write_funcs[] = {
  205. smbios_write_type0,
  206. smbios_write_type1,
  207. smbios_write_type2,
  208. smbios_write_type3,
  209. smbios_write_type4,
  210. smbios_write_type32,
  211. smbios_write_type127
  212. };
  213. uintptr_t write_smbios_table(uintptr_t addr)
  214. {
  215. struct smbios_entry *se;
  216. u32 tables;
  217. int len = 0;
  218. int max_struct_size = 0;
  219. int handle = 0;
  220. char *istart;
  221. int isize;
  222. int i;
  223. /* 16 byte align the table address */
  224. addr = ALIGN(addr, 16);
  225. se = (struct smbios_entry *)addr;
  226. memset(se, 0, sizeof(struct smbios_entry));
  227. addr += sizeof(struct smbios_entry);
  228. addr = ALIGN(addr, 16);
  229. tables = addr;
  230. /* populate minimum required tables */
  231. for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
  232. int tmp = smbios_write_funcs[i](&addr, handle++);
  233. max_struct_size = max(max_struct_size, tmp);
  234. len += tmp;
  235. }
  236. memcpy(se->anchor, "_SM_", 4);
  237. se->length = sizeof(struct smbios_entry);
  238. se->major_ver = SMBIOS_MAJOR_VER;
  239. se->minor_ver = SMBIOS_MINOR_VER;
  240. se->max_struct_size = max_struct_size;
  241. memcpy(se->intermediate_anchor, "_DMI_", 5);
  242. se->struct_table_length = len;
  243. se->struct_table_address = tables;
  244. se->struct_count = handle;
  245. /* calculate checksums */
  246. istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
  247. isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
  248. se->intermediate_checksum = table_compute_checksum(istart, isize);
  249. se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));
  250. return addr;
  251. }