smbios.c 7.8 KB

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