smbios.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 <version.h>
  10. #include <asm/cpu.h>
  11. #include <asm/smbios.h>
  12. #include <asm/tables.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. /**
  15. * smbios_add_string() - add a string to the string area
  16. *
  17. * This adds a string to the string area which is appended directly after
  18. * the formatted portion of an SMBIOS structure.
  19. *
  20. * @start: string area start address
  21. * @str: string to add
  22. * @return: string number in the string area
  23. */
  24. static int smbios_add_string(char *start, const char *str)
  25. {
  26. int i = 1;
  27. char *p = start;
  28. for (;;) {
  29. if (!*p) {
  30. strcpy(p, str);
  31. p += strlen(str);
  32. *p++ = '\0';
  33. *p++ = '\0';
  34. return i;
  35. }
  36. if (!strcmp(p, str))
  37. return i;
  38. p += strlen(p) + 1;
  39. i++;
  40. }
  41. }
  42. /**
  43. * smbios_string_table_len() - compute the string area size
  44. *
  45. * This computes the size of the string area including the string terminator.
  46. *
  47. * @start: string area start address
  48. * @return: string area size
  49. */
  50. static int smbios_string_table_len(char *start)
  51. {
  52. char *p = start;
  53. int i, len = 0;
  54. while (*p) {
  55. i = strlen(p) + 1;
  56. p += i;
  57. len += i;
  58. }
  59. return len + 1;
  60. }
  61. static int smbios_write_type0(u32 *current, int handle)
  62. {
  63. struct smbios_type0 *t = (struct smbios_type0 *)*current;
  64. int len = sizeof(struct smbios_type0);
  65. memset(t, 0, sizeof(struct smbios_type0));
  66. fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
  67. t->vendor = smbios_add_string(t->eos, "U-Boot");
  68. t->bios_ver = smbios_add_string(t->eos, PLAIN_VERSION);
  69. t->bios_release_date = smbios_add_string(t->eos, U_BOOT_DMI_DATE);
  70. t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
  71. t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
  72. BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
  73. BIOS_CHARACTERISTICS_UPGRADEABLE;
  74. #ifdef CONFIG_GENERATE_ACPI_TABLE
  75. t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
  76. #endif
  77. t->bios_characteristics_ext2 = BIOS_CHARACTERISTICS_EXT2_TARGET;
  78. t->bios_major_release = 0xff;
  79. t->bios_minor_release = 0xff;
  80. t->ec_major_release = 0xff;
  81. t->ec_minor_release = 0xff;
  82. len = t->length + smbios_string_table_len(t->eos);
  83. *current += len;
  84. return len;
  85. }
  86. static int smbios_write_type1(u32 *current, int handle)
  87. {
  88. struct smbios_type1 *t = (struct smbios_type1 *)*current;
  89. int len = sizeof(struct smbios_type1);
  90. memset(t, 0, sizeof(struct smbios_type1));
  91. fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
  92. t->manufacturer = smbios_add_string(t->eos, CONFIG_SYS_VENDOR);
  93. t->product_name = smbios_add_string(t->eos, CONFIG_SYS_BOARD);
  94. len = t->length + smbios_string_table_len(t->eos);
  95. *current += len;
  96. return len;
  97. }
  98. static int smbios_write_type2(u32 *current, int handle)
  99. {
  100. struct smbios_type2 *t = (struct smbios_type2 *)*current;
  101. int len = sizeof(struct smbios_type2);
  102. memset(t, 0, sizeof(struct smbios_type2));
  103. fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
  104. t->manufacturer = smbios_add_string(t->eos, CONFIG_SYS_VENDOR);
  105. t->product_name = smbios_add_string(t->eos, CONFIG_SYS_BOARD);
  106. t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
  107. t->board_type = SMBIOS_BOARD_MOTHERBOARD;
  108. len = t->length + smbios_string_table_len(t->eos);
  109. *current += len;
  110. return len;
  111. }
  112. static int smbios_write_type3(u32 *current, int handle)
  113. {
  114. struct smbios_type3 *t = (struct smbios_type3 *)*current;
  115. int len = sizeof(struct smbios_type3);
  116. memset(t, 0, sizeof(struct smbios_type3));
  117. fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
  118. t->manufacturer = smbios_add_string(t->eos, CONFIG_SYS_VENDOR);
  119. t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
  120. t->bootup_state = SMBIOS_STATE_SAFE;
  121. t->power_supply_state = SMBIOS_STATE_SAFE;
  122. t->thermal_state = SMBIOS_STATE_SAFE;
  123. t->security_status = SMBIOS_SECURITY_NONE;
  124. len = t->length + smbios_string_table_len(t->eos);
  125. *current += len;
  126. return len;
  127. }
  128. static int smbios_write_type4(u32 *current, int handle)
  129. {
  130. struct smbios_type4 *t = (struct smbios_type4 *)*current;
  131. int len = sizeof(struct smbios_type4);
  132. const char *vendor;
  133. char *name;
  134. char processor_name[CPU_MAX_NAME_LEN];
  135. struct cpuid_result res;
  136. memset(t, 0, sizeof(struct smbios_type4));
  137. fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
  138. t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
  139. t->processor_family = gd->arch.x86;
  140. vendor = cpu_vendor_name(gd->arch.x86_vendor);
  141. t->processor_manufacturer = smbios_add_string(t->eos, vendor);
  142. res = cpuid(1);
  143. t->processor_id[0] = res.eax;
  144. t->processor_id[1] = res.edx;
  145. name = cpu_get_name(processor_name);
  146. t->processor_version = smbios_add_string(t->eos, name);
  147. t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
  148. t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
  149. t->l1_cache_handle = 0xffff;
  150. t->l2_cache_handle = 0xffff;
  151. t->l3_cache_handle = 0xffff;
  152. t->processor_family2 = t->processor_family;
  153. len = t->length + smbios_string_table_len(t->eos);
  154. *current += len;
  155. return len;
  156. }
  157. static int smbios_write_type32(u32 *current, int handle)
  158. {
  159. struct smbios_type32 *t = (struct smbios_type32 *)*current;
  160. int len = sizeof(struct smbios_type32);
  161. memset(t, 0, sizeof(struct smbios_type32));
  162. fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
  163. *current += len;
  164. return len;
  165. }
  166. static int smbios_write_type127(u32 *current, int handle)
  167. {
  168. struct smbios_type127 *t = (struct smbios_type127 *)*current;
  169. int len = sizeof(struct smbios_type127);
  170. memset(t, 0, sizeof(struct smbios_type127));
  171. fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
  172. *current += len;
  173. return len;
  174. }
  175. static smbios_write_type smbios_write_funcs[] = {
  176. smbios_write_type0,
  177. smbios_write_type1,
  178. smbios_write_type2,
  179. smbios_write_type3,
  180. smbios_write_type4,
  181. smbios_write_type32,
  182. smbios_write_type127
  183. };
  184. u32 write_smbios_table(u32 addr)
  185. {
  186. struct smbios_entry *se;
  187. u32 tables;
  188. int len = 0;
  189. int max_struct_size = 0;
  190. int handle = 0;
  191. char *istart;
  192. int isize;
  193. int i;
  194. /* 16 byte align the table address */
  195. addr = ALIGN(addr, 16);
  196. se = (struct smbios_entry *)addr;
  197. memset(se, 0, sizeof(struct smbios_entry));
  198. addr += sizeof(struct smbios_entry);
  199. addr = ALIGN(addr, 16);
  200. tables = addr;
  201. /* populate minimum required tables */
  202. for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
  203. int tmp = smbios_write_funcs[i](&addr, handle++);
  204. max_struct_size = max(max_struct_size, tmp);
  205. len += tmp;
  206. }
  207. memcpy(se->anchor, "_SM_", 4);
  208. se->length = sizeof(struct smbios_entry);
  209. se->major_ver = SMBIOS_MAJOR_VER;
  210. se->minor_ver = SMBIOS_MINOR_VER;
  211. se->max_struct_size = max_struct_size;
  212. memcpy(se->intermediate_anchor, "_DMI_", 5);
  213. se->struct_table_length = len;
  214. se->struct_table_address = tables;
  215. se->struct_count = handle;
  216. /* calculate checksums */
  217. istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
  218. isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
  219. se->intermediate_checksum = table_compute_checksum(istart, isize);
  220. se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));
  221. return addr;
  222. }