smbios.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 <dm.h>
  9. #include <env.h>
  10. #include <mapmem.h>
  11. #include <smbios.h>
  12. #include <tables_csum.h>
  13. #include <version.h>
  14. #ifdef CONFIG_CPU
  15. #include <cpu.h>
  16. #include <dm/uclass-internal.h>
  17. #endif
  18. /**
  19. * struct smbios_write_method - Information about a table-writing function
  20. *
  21. * @write: Function to call
  22. * @subnode_name: Name of subnode which has the information for this function,
  23. * NULL if none
  24. */
  25. struct smbios_write_method {
  26. smbios_write_type write;
  27. const char *subnode_name;
  28. };
  29. /**
  30. * smbios_add_string() - add a string to the string area
  31. *
  32. * This adds a string to the string area which is appended directly after
  33. * the formatted portion of an SMBIOS structure.
  34. *
  35. * @start: string area start address
  36. * @str: string to add
  37. * @return: string number in the string area (1 or more)
  38. */
  39. static int smbios_add_string(char *start, const char *str)
  40. {
  41. int i = 1;
  42. char *p = start;
  43. if (!*str)
  44. str = "Unknown";
  45. for (;;) {
  46. if (!*p) {
  47. strcpy(p, str);
  48. p += strlen(str);
  49. *p++ = '\0';
  50. *p++ = '\0';
  51. return i;
  52. }
  53. if (!strcmp(p, str))
  54. return i;
  55. p += strlen(p) + 1;
  56. i++;
  57. }
  58. }
  59. /**
  60. * smbios_add_prop() - Add a property from the device tree
  61. *
  62. * @start: string area start address
  63. * @node: node containing the information to write (ofnode_null() if none)
  64. * @prop: property to write
  65. * @return 0 if not found, else SMBIOS string number (1 or more)
  66. */
  67. static int smbios_add_prop(char *start, ofnode node, const char *prop)
  68. {
  69. if (IS_ENABLED(CONFIG_OF_CONTROL)) {
  70. const char *str;
  71. str = ofnode_read_string(node, prop);
  72. if (str)
  73. return smbios_add_string(start, str);
  74. }
  75. return 0;
  76. }
  77. /**
  78. * smbios_string_table_len() - compute the string area size
  79. *
  80. * This computes the size of the string area including the string terminator.
  81. *
  82. * @start: string area start address
  83. * @return: string area size
  84. */
  85. static int smbios_string_table_len(char *start)
  86. {
  87. char *p = start;
  88. int i, len = 0;
  89. while (*p) {
  90. i = strlen(p) + 1;
  91. p += i;
  92. len += i;
  93. }
  94. return len + 1;
  95. }
  96. static int smbios_write_type0(ulong *current, int handle, ofnode node)
  97. {
  98. struct smbios_type0 *t;
  99. int len = sizeof(struct smbios_type0);
  100. t = map_sysmem(*current, len);
  101. memset(t, 0, sizeof(struct smbios_type0));
  102. fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
  103. t->vendor = smbios_add_string(t->eos, "U-Boot");
  104. t->bios_ver = smbios_add_string(t->eos, PLAIN_VERSION);
  105. t->bios_release_date = smbios_add_string(t->eos, U_BOOT_DMI_DATE);
  106. #ifdef CONFIG_ROM_SIZE
  107. t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
  108. #endif
  109. t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
  110. BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
  111. BIOS_CHARACTERISTICS_UPGRADEABLE;
  112. #ifdef CONFIG_GENERATE_ACPI_TABLE
  113. t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
  114. #endif
  115. #ifdef CONFIG_EFI_LOADER
  116. t->bios_characteristics_ext1 |= BIOS_CHARACTERISTICS_EXT1_UEFI;
  117. #endif
  118. t->bios_characteristics_ext2 = BIOS_CHARACTERISTICS_EXT2_TARGET;
  119. t->bios_major_release = 0xff;
  120. t->bios_minor_release = 0xff;
  121. t->ec_major_release = 0xff;
  122. t->ec_minor_release = 0xff;
  123. len = t->length + smbios_string_table_len(t->eos);
  124. *current += len;
  125. unmap_sysmem(t);
  126. return len;
  127. }
  128. static int smbios_write_type1(ulong *current, int handle, ofnode node)
  129. {
  130. struct smbios_type1 *t;
  131. int len = sizeof(struct smbios_type1);
  132. char *serial_str = env_get("serial#");
  133. t = map_sysmem(*current, len);
  134. memset(t, 0, sizeof(struct smbios_type1));
  135. fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
  136. t->manufacturer = smbios_add_prop(t->eos, node, "manufacturer");
  137. t->product_name = smbios_add_prop(t->eos, node, "product");
  138. t->version = smbios_add_prop(t->eos, node, "version");
  139. if (serial_str) {
  140. t->serial_number = smbios_add_string(t->eos, serial_str);
  141. strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
  142. } else {
  143. t->serial_number = smbios_add_prop(t->eos, node, "serial");
  144. }
  145. t->sku_number = smbios_add_prop(t->eos, node, "sku");
  146. t->family = smbios_add_prop(t->eos, node, "family");
  147. len = t->length + smbios_string_table_len(t->eos);
  148. *current += len;
  149. unmap_sysmem(t);
  150. return len;
  151. }
  152. static int smbios_write_type2(ulong *current, int handle, ofnode node)
  153. {
  154. struct smbios_type2 *t;
  155. int len = sizeof(struct smbios_type2);
  156. t = map_sysmem(*current, len);
  157. memset(t, 0, sizeof(struct smbios_type2));
  158. fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
  159. t->manufacturer = smbios_add_prop(t->eos, node, "manufacturer");
  160. t->product_name = smbios_add_prop(t->eos, node, "product");
  161. t->asset_tag_number = smbios_add_prop(t->eos, node, "asset-tag");
  162. t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
  163. t->board_type = SMBIOS_BOARD_MOTHERBOARD;
  164. len = t->length + smbios_string_table_len(t->eos);
  165. *current += len;
  166. unmap_sysmem(t);
  167. return len;
  168. }
  169. static int smbios_write_type3(ulong *current, int handle, ofnode node)
  170. {
  171. struct smbios_type3 *t;
  172. int len = sizeof(struct smbios_type3);
  173. t = map_sysmem(*current, len);
  174. memset(t, 0, sizeof(struct smbios_type3));
  175. fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
  176. t->manufacturer = smbios_add_prop(t->eos, node, "manufacturer");
  177. t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
  178. t->bootup_state = SMBIOS_STATE_SAFE;
  179. t->power_supply_state = SMBIOS_STATE_SAFE;
  180. t->thermal_state = SMBIOS_STATE_SAFE;
  181. t->security_status = SMBIOS_SECURITY_NONE;
  182. len = t->length + smbios_string_table_len(t->eos);
  183. *current += len;
  184. unmap_sysmem(t);
  185. return len;
  186. }
  187. static void smbios_write_type4_dm(struct smbios_type4 *t, ofnode node)
  188. {
  189. u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
  190. const char *vendor = "Unknown";
  191. const char *name = "Unknown";
  192. #ifdef CONFIG_CPU
  193. char processor_name[49];
  194. char vendor_name[49];
  195. struct udevice *cpu = NULL;
  196. uclass_find_first_device(UCLASS_CPU, &cpu);
  197. if (cpu) {
  198. struct cpu_platdata *plat = dev_get_parent_platdata(cpu);
  199. if (plat->family)
  200. processor_family = plat->family;
  201. t->processor_id[0] = plat->id[0];
  202. t->processor_id[1] = plat->id[1];
  203. if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name)))
  204. vendor = vendor_name;
  205. if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name)))
  206. name = processor_name;
  207. }
  208. #endif
  209. t->processor_family = processor_family;
  210. t->processor_manufacturer = smbios_add_string(t->eos, vendor);
  211. t->processor_version = smbios_add_string(t->eos, name);
  212. }
  213. static int smbios_write_type4(ulong *current, int handle, ofnode node)
  214. {
  215. struct smbios_type4 *t;
  216. int len = sizeof(struct smbios_type4);
  217. t = map_sysmem(*current, len);
  218. memset(t, 0, sizeof(struct smbios_type4));
  219. fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
  220. t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
  221. smbios_write_type4_dm(t, node);
  222. t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
  223. t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
  224. t->l1_cache_handle = 0xffff;
  225. t->l2_cache_handle = 0xffff;
  226. t->l3_cache_handle = 0xffff;
  227. t->processor_family2 = t->processor_family;
  228. len = t->length + smbios_string_table_len(t->eos);
  229. *current += len;
  230. unmap_sysmem(t);
  231. return len;
  232. }
  233. static int smbios_write_type32(ulong *current, int handle, ofnode node)
  234. {
  235. struct smbios_type32 *t;
  236. int len = sizeof(struct smbios_type32);
  237. t = map_sysmem(*current, len);
  238. memset(t, 0, sizeof(struct smbios_type32));
  239. fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
  240. *current += len;
  241. unmap_sysmem(t);
  242. return len;
  243. }
  244. static int smbios_write_type127(ulong *current, int handle, ofnode node)
  245. {
  246. struct smbios_type127 *t;
  247. int len = sizeof(struct smbios_type127);
  248. t = map_sysmem(*current, len);
  249. memset(t, 0, sizeof(struct smbios_type127));
  250. fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
  251. *current += len;
  252. unmap_sysmem(t);
  253. return len;
  254. }
  255. static struct smbios_write_method smbios_write_funcs[] = {
  256. { smbios_write_type0, },
  257. { smbios_write_type1, "system", },
  258. { smbios_write_type2, "baseboard", },
  259. { smbios_write_type3, "chassis", },
  260. { smbios_write_type4, },
  261. { smbios_write_type32, },
  262. { smbios_write_type127 },
  263. };
  264. ulong write_smbios_table(ulong addr)
  265. {
  266. ofnode parent_node = ofnode_null();
  267. struct smbios_entry *se;
  268. struct udevice *dev;
  269. ulong table_addr;
  270. ulong tables;
  271. int len = 0;
  272. int max_struct_size = 0;
  273. int handle = 0;
  274. char *istart;
  275. int isize;
  276. int i;
  277. if (IS_ENABLED(CONFIG_OF_CONTROL)) {
  278. uclass_first_device(UCLASS_SYSINFO, &dev);
  279. if (dev)
  280. parent_node = dev_read_subnode(dev, "smbios");
  281. }
  282. /* 16 byte align the table address */
  283. addr = ALIGN(addr, 16);
  284. se = map_sysmem(addr, sizeof(struct smbios_entry));
  285. memset(se, 0, sizeof(struct smbios_entry));
  286. addr += sizeof(struct smbios_entry);
  287. addr = ALIGN(addr, 16);
  288. tables = addr;
  289. /* populate minimum required tables */
  290. for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
  291. const struct smbios_write_method *method;
  292. ofnode node = ofnode_null();
  293. int tmp;
  294. method = &smbios_write_funcs[i];
  295. if (IS_ENABLED(CONFIG_OF_CONTROL) && method->subnode_name)
  296. node = ofnode_find_subnode(parent_node,
  297. method->subnode_name);
  298. tmp = method->write((ulong *)&addr, handle++, node);
  299. max_struct_size = max(max_struct_size, tmp);
  300. len += tmp;
  301. }
  302. memcpy(se->anchor, "_SM_", 4);
  303. se->length = sizeof(struct smbios_entry);
  304. se->major_ver = SMBIOS_MAJOR_VER;
  305. se->minor_ver = SMBIOS_MINOR_VER;
  306. se->max_struct_size = max_struct_size;
  307. memcpy(se->intermediate_anchor, "_DMI_", 5);
  308. se->struct_table_length = len;
  309. /*
  310. * We must use a pointer here so things work correctly on sandbox. The
  311. * user of this table is not aware of the mapping of addresses to
  312. * sandbox's DRAM buffer.
  313. */
  314. table_addr = (ulong)map_sysmem(tables, 0);
  315. if (sizeof(table_addr) > sizeof(u32) && table_addr > (ulong)UINT_MAX) {
  316. /*
  317. * We need to put this >32-bit pointer into the table but the
  318. * field is only 32 bits wide.
  319. */
  320. printf("WARNING: SMBIOS table_address overflow %llx\n",
  321. (unsigned long long)table_addr);
  322. table_addr = 0;
  323. }
  324. se->struct_table_address = table_addr;
  325. se->struct_count = handle;
  326. /* calculate checksums */
  327. istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
  328. isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
  329. se->intermediate_checksum = table_compute_checksum(istart, isize);
  330. se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));
  331. unmap_sysmem(se);
  332. return addr;
  333. }