smbios.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 <env.h>
  9. #include <mapmem.h>
  10. #include <smbios.h>
  11. #include <tables_csum.h>
  12. #include <version.h>
  13. #ifdef CONFIG_CPU
  14. #include <cpu.h>
  15. #include <dm.h>
  16. #include <dm/uclass-internal.h>
  17. #endif
  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(ulong *current, int handle)
  66. {
  67. struct smbios_type0 *t;
  68. int len = sizeof(struct smbios_type0);
  69. t = map_sysmem(*current, len);
  70. memset(t, 0, sizeof(struct smbios_type0));
  71. fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
  72. t->vendor = smbios_add_string(t->eos, "U-Boot");
  73. t->bios_ver = smbios_add_string(t->eos, PLAIN_VERSION);
  74. t->bios_release_date = smbios_add_string(t->eos, U_BOOT_DMI_DATE);
  75. #ifdef CONFIG_ROM_SIZE
  76. t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
  77. #endif
  78. t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
  79. BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
  80. BIOS_CHARACTERISTICS_UPGRADEABLE;
  81. #ifdef CONFIG_GENERATE_ACPI_TABLE
  82. t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
  83. #endif
  84. #ifdef CONFIG_EFI_LOADER
  85. t->bios_characteristics_ext1 |= BIOS_CHARACTERISTICS_EXT1_UEFI;
  86. #endif
  87. t->bios_characteristics_ext2 = BIOS_CHARACTERISTICS_EXT2_TARGET;
  88. t->bios_major_release = 0xff;
  89. t->bios_minor_release = 0xff;
  90. t->ec_major_release = 0xff;
  91. t->ec_minor_release = 0xff;
  92. len = t->length + smbios_string_table_len(t->eos);
  93. *current += len;
  94. unmap_sysmem(t);
  95. return len;
  96. }
  97. static int smbios_write_type1(ulong *current, int handle)
  98. {
  99. struct smbios_type1 *t;
  100. int len = sizeof(struct smbios_type1);
  101. char *serial_str = env_get("serial#");
  102. t = map_sysmem(*current, len);
  103. memset(t, 0, sizeof(struct smbios_type1));
  104. fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
  105. t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
  106. t->product_name = smbios_add_string(t->eos, CONFIG_SMBIOS_PRODUCT_NAME);
  107. if (serial_str) {
  108. strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
  109. t->serial_number = smbios_add_string(t->eos, serial_str);
  110. }
  111. len = t->length + smbios_string_table_len(t->eos);
  112. *current += len;
  113. unmap_sysmem(t);
  114. return len;
  115. }
  116. static int smbios_write_type2(ulong *current, int handle)
  117. {
  118. struct smbios_type2 *t;
  119. int len = sizeof(struct smbios_type2);
  120. t = map_sysmem(*current, len);
  121. memset(t, 0, sizeof(struct smbios_type2));
  122. fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
  123. t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
  124. t->product_name = smbios_add_string(t->eos, CONFIG_SMBIOS_PRODUCT_NAME);
  125. t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
  126. t->board_type = SMBIOS_BOARD_MOTHERBOARD;
  127. len = t->length + smbios_string_table_len(t->eos);
  128. *current += len;
  129. unmap_sysmem(t);
  130. return len;
  131. }
  132. static int smbios_write_type3(ulong *current, int handle)
  133. {
  134. struct smbios_type3 *t;
  135. int len = sizeof(struct smbios_type3);
  136. t = map_sysmem(*current, len);
  137. memset(t, 0, sizeof(struct smbios_type3));
  138. fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
  139. t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
  140. t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
  141. t->bootup_state = SMBIOS_STATE_SAFE;
  142. t->power_supply_state = SMBIOS_STATE_SAFE;
  143. t->thermal_state = SMBIOS_STATE_SAFE;
  144. t->security_status = SMBIOS_SECURITY_NONE;
  145. len = t->length + smbios_string_table_len(t->eos);
  146. *current += len;
  147. unmap_sysmem(t);
  148. return len;
  149. }
  150. static void smbios_write_type4_dm(struct smbios_type4 *t)
  151. {
  152. u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
  153. const char *vendor = "Unknown";
  154. const char *name = "Unknown";
  155. #ifdef CONFIG_CPU
  156. char processor_name[49];
  157. char vendor_name[49];
  158. struct udevice *dev = NULL;
  159. uclass_find_first_device(UCLASS_CPU, &dev);
  160. if (dev) {
  161. struct cpu_platdata *plat = dev_get_parent_platdata(dev);
  162. if (plat->family)
  163. processor_family = plat->family;
  164. t->processor_id[0] = plat->id[0];
  165. t->processor_id[1] = plat->id[1];
  166. if (!cpu_get_vendor(dev, vendor_name, sizeof(vendor_name)))
  167. vendor = vendor_name;
  168. if (!cpu_get_desc(dev, processor_name, sizeof(processor_name)))
  169. name = processor_name;
  170. }
  171. #endif
  172. t->processor_family = processor_family;
  173. t->processor_manufacturer = smbios_add_string(t->eos, vendor);
  174. t->processor_version = smbios_add_string(t->eos, name);
  175. }
  176. static int smbios_write_type4(ulong *current, int handle)
  177. {
  178. struct smbios_type4 *t;
  179. int len = sizeof(struct smbios_type4);
  180. t = map_sysmem(*current, len);
  181. memset(t, 0, sizeof(struct smbios_type4));
  182. fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
  183. t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
  184. smbios_write_type4_dm(t);
  185. t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
  186. t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
  187. t->l1_cache_handle = 0xffff;
  188. t->l2_cache_handle = 0xffff;
  189. t->l3_cache_handle = 0xffff;
  190. t->processor_family2 = t->processor_family;
  191. len = t->length + smbios_string_table_len(t->eos);
  192. *current += len;
  193. unmap_sysmem(t);
  194. return len;
  195. }
  196. static int smbios_write_type32(ulong *current, int handle)
  197. {
  198. struct smbios_type32 *t;
  199. int len = sizeof(struct smbios_type32);
  200. t = map_sysmem(*current, len);
  201. memset(t, 0, sizeof(struct smbios_type32));
  202. fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
  203. *current += len;
  204. unmap_sysmem(t);
  205. return len;
  206. }
  207. static int smbios_write_type127(ulong *current, int handle)
  208. {
  209. struct smbios_type127 *t;
  210. int len = sizeof(struct smbios_type127);
  211. t = map_sysmem(*current, len);
  212. memset(t, 0, sizeof(struct smbios_type127));
  213. fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
  214. *current += len;
  215. unmap_sysmem(t);
  216. return len;
  217. }
  218. static smbios_write_type smbios_write_funcs[] = {
  219. smbios_write_type0,
  220. smbios_write_type1,
  221. smbios_write_type2,
  222. smbios_write_type3,
  223. smbios_write_type4,
  224. smbios_write_type32,
  225. smbios_write_type127
  226. };
  227. ulong write_smbios_table(ulong addr)
  228. {
  229. struct smbios_entry *se;
  230. ulong table_addr;
  231. ulong tables;
  232. int len = 0;
  233. int max_struct_size = 0;
  234. int handle = 0;
  235. char *istart;
  236. int isize;
  237. int i;
  238. /* 16 byte align the table address */
  239. addr = ALIGN(addr, 16);
  240. se = map_sysmem(addr, sizeof(struct smbios_entry));
  241. memset(se, 0, sizeof(struct smbios_entry));
  242. addr += sizeof(struct smbios_entry);
  243. addr = ALIGN(addr, 16);
  244. tables = addr;
  245. /* populate minimum required tables */
  246. for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
  247. int tmp = smbios_write_funcs[i]((ulong *)&addr, handle++);
  248. max_struct_size = max(max_struct_size, tmp);
  249. len += tmp;
  250. }
  251. memcpy(se->anchor, "_SM_", 4);
  252. se->length = sizeof(struct smbios_entry);
  253. se->major_ver = SMBIOS_MAJOR_VER;
  254. se->minor_ver = SMBIOS_MINOR_VER;
  255. se->max_struct_size = max_struct_size;
  256. memcpy(se->intermediate_anchor, "_DMI_", 5);
  257. se->struct_table_length = len;
  258. /*
  259. * We must use a pointer here so things work correctly on sandbox. The
  260. * user of this table is not aware of the mapping of addresses to
  261. * sandbox's DRAM buffer.
  262. */
  263. table_addr = (ulong)map_sysmem(tables, 0);
  264. if (sizeof(table_addr) > sizeof(u32) && table_addr > (ulong)UINT_MAX) {
  265. /*
  266. * We need to put this >32-bit pointer into the table but the
  267. * field is only 32 bits wide.
  268. */
  269. printf("WARNING: SMBIOS table_address overflow %llx\n",
  270. (unsigned long long)table_addr);
  271. table_addr = 0;
  272. }
  273. se->struct_table_address = table_addr;
  274. se->struct_count = handle;
  275. /* calculate checksums */
  276. istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
  277. isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
  278. se->intermediate_checksum = table_compute_checksum(istart, isize);
  279. se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));
  280. unmap_sysmem(se);
  281. return addr;
  282. }