smbios.c 8.5 KB

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