smbios.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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 <sysinfo.h>
  13. #include <tables_csum.h>
  14. #include <version.h>
  15. #ifdef CONFIG_CPU
  16. #include <cpu.h>
  17. #include <dm/uclass-internal.h>
  18. #endif
  19. DECLARE_GLOBAL_DATA_PTR;
  20. /**
  21. * struct smbios_ctx - context for writing SMBIOS tables
  22. *
  23. * @node: node containing the information to write (ofnode_null() if none)
  24. * @dev: sysinfo device to use (NULL if none)
  25. * @eos: end-of-string pointer for the table being processed. This is set
  26. * up when we start processing a table
  27. * @next_ptr: pointer to the start of the next string to be added. When the
  28. * table is nopt empty, this points to the byte after the \0 of the
  29. * previous string.
  30. * @last_str: points to the last string that was written to the table, or NULL
  31. * if none
  32. */
  33. struct smbios_ctx {
  34. ofnode node;
  35. struct udevice *dev;
  36. char *eos;
  37. char *next_ptr;
  38. char *last_str;
  39. };
  40. /**
  41. * Function prototype to write a specific type of SMBIOS structure
  42. *
  43. * @addr: start address to write the structure
  44. * @handle: the structure's handle, a unique 16-bit number
  45. * @ctx: context for writing the tables
  46. * @return: size of the structure
  47. */
  48. typedef int (*smbios_write_type)(ulong *addr, int handle,
  49. struct smbios_ctx *ctx);
  50. /**
  51. * struct smbios_write_method - Information about a table-writing function
  52. *
  53. * @write: Function to call
  54. * @subnode_name: Name of subnode which has the information for this function,
  55. * NULL if none
  56. */
  57. struct smbios_write_method {
  58. smbios_write_type write;
  59. const char *subnode_name;
  60. };
  61. /**
  62. * smbios_add_string() - add a string to the string area
  63. *
  64. * This adds a string to the string area which is appended directly after
  65. * the formatted portion of an SMBIOS structure.
  66. *
  67. * @ctx: SMBIOS context
  68. * @str: string to add
  69. * @return: string number in the string area (1 or more)
  70. */
  71. static int smbios_add_string(struct smbios_ctx *ctx, const char *str)
  72. {
  73. int i = 1;
  74. char *p = ctx->eos;
  75. if (!*str)
  76. str = "Unknown";
  77. for (;;) {
  78. if (!*p) {
  79. ctx->last_str = p;
  80. strcpy(p, str);
  81. p += strlen(str);
  82. *p++ = '\0';
  83. ctx->next_ptr = p;
  84. *p++ = '\0';
  85. return i;
  86. }
  87. if (!strcmp(p, str)) {
  88. ctx->last_str = p;
  89. return i;
  90. }
  91. p += strlen(p) + 1;
  92. i++;
  93. }
  94. }
  95. /**
  96. * smbios_add_prop_si() - Add a property from the devicetree or sysinfo
  97. *
  98. * Sysinfo is used if available, with a fallback to devicetree
  99. *
  100. * @ctx: context for writing the tables
  101. * @prop: property to write
  102. * @return 0 if not found, else SMBIOS string number (1 or more)
  103. */
  104. static int smbios_add_prop_si(struct smbios_ctx *ctx, const char *prop,
  105. int sysinfo_id)
  106. {
  107. if (sysinfo_id && ctx->dev) {
  108. char val[SMBIOS_STR_MAX];
  109. int ret;
  110. ret = sysinfo_get_str(ctx->dev, sysinfo_id, sizeof(val), val);
  111. if (!ret)
  112. return smbios_add_string(ctx, val);
  113. }
  114. if (IS_ENABLED(CONFIG_OF_CONTROL)) {
  115. const char *str;
  116. str = ofnode_read_string(ctx->node, prop);
  117. if (str)
  118. return smbios_add_string(ctx, str);
  119. }
  120. return 0;
  121. }
  122. /**
  123. * smbios_add_prop() - Add a property from the devicetree
  124. *
  125. * @prop: property to write
  126. * @return 0 if not found, else SMBIOS string number (1 or more)
  127. */
  128. static int smbios_add_prop(struct smbios_ctx *ctx, const char *prop)
  129. {
  130. return smbios_add_prop_si(ctx, prop, SYSINFO_ID_NONE);
  131. }
  132. static void smbios_set_eos(struct smbios_ctx *ctx, char *eos)
  133. {
  134. ctx->eos = eos;
  135. ctx->next_ptr = eos;
  136. ctx->last_str = NULL;
  137. }
  138. int smbios_update_version(const char *version)
  139. {
  140. char *ptr = gd->smbios_version;
  141. uint old_len, len;
  142. if (!ptr)
  143. return log_ret(-ENOENT);
  144. /*
  145. * This string is supposed to have at least enough bytes and is
  146. * padded with spaces. Update it, taking care not to move the
  147. * \0 terminator, so that other strings in the string table
  148. * are not disturbed. See smbios_add_string()
  149. */
  150. old_len = strnlen(ptr, SMBIOS_STR_MAX);
  151. len = strnlen(version, SMBIOS_STR_MAX);
  152. if (len > old_len)
  153. return log_ret(-ENOSPC);
  154. log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr);
  155. memcpy(ptr, version, len);
  156. #ifdef LOG_DEBUG
  157. print_buffer((ulong)ptr, ptr, 1, old_len + 1, 0);
  158. #endif
  159. return 0;
  160. }
  161. /**
  162. * smbios_string_table_len() - compute the string area size
  163. *
  164. * This computes the size of the string area including the string terminator.
  165. *
  166. * @ctx: SMBIOS context
  167. * @return: string area size
  168. */
  169. static int smbios_string_table_len(const struct smbios_ctx *ctx)
  170. {
  171. /* Allow for the final \0 after all strings */
  172. return (ctx->next_ptr + 1) - ctx->eos;
  173. }
  174. static int smbios_write_type0(ulong *current, int handle,
  175. struct smbios_ctx *ctx)
  176. {
  177. struct smbios_type0 *t;
  178. int len = sizeof(struct smbios_type0);
  179. t = map_sysmem(*current, len);
  180. memset(t, 0, sizeof(struct smbios_type0));
  181. fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
  182. smbios_set_eos(ctx, t->eos);
  183. t->vendor = smbios_add_string(ctx, "U-Boot");
  184. t->bios_ver = smbios_add_prop(ctx, "version");
  185. if (!t->bios_ver)
  186. t->bios_ver = smbios_add_string(ctx, PLAIN_VERSION);
  187. if (t->bios_ver)
  188. gd->smbios_version = ctx->last_str;
  189. log_debug("smbios_version = %p: '%s'\n", gd->smbios_version,
  190. gd->smbios_version);
  191. #ifdef LOG_DEBUG
  192. print_buffer((ulong)gd->smbios_version, gd->smbios_version,
  193. 1, strlen(gd->smbios_version) + 1, 0);
  194. #endif
  195. t->bios_release_date = smbios_add_string(ctx, U_BOOT_DMI_DATE);
  196. #ifdef CONFIG_ROM_SIZE
  197. t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
  198. #endif
  199. t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
  200. BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
  201. BIOS_CHARACTERISTICS_UPGRADEABLE;
  202. #ifdef CONFIG_GENERATE_ACPI_TABLE
  203. t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
  204. #endif
  205. #ifdef CONFIG_EFI_LOADER
  206. t->bios_characteristics_ext1 |= BIOS_CHARACTERISTICS_EXT1_UEFI;
  207. #endif
  208. t->bios_characteristics_ext2 = BIOS_CHARACTERISTICS_EXT2_TARGET;
  209. /* bios_major_release has only one byte, so drop century */
  210. t->bios_major_release = U_BOOT_VERSION_NUM % 100;
  211. t->bios_minor_release = U_BOOT_VERSION_NUM_PATCH;
  212. t->ec_major_release = 0xff;
  213. t->ec_minor_release = 0xff;
  214. len = t->length + smbios_string_table_len(ctx);
  215. *current += len;
  216. unmap_sysmem(t);
  217. return len;
  218. }
  219. static int smbios_write_type1(ulong *current, int handle,
  220. struct smbios_ctx *ctx)
  221. {
  222. struct smbios_type1 *t;
  223. int len = sizeof(struct smbios_type1);
  224. char *serial_str = env_get("serial#");
  225. t = map_sysmem(*current, len);
  226. memset(t, 0, sizeof(struct smbios_type1));
  227. fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
  228. smbios_set_eos(ctx, t->eos);
  229. t->manufacturer = smbios_add_prop(ctx, "manufacturer");
  230. t->product_name = smbios_add_prop(ctx, "product");
  231. t->version = smbios_add_prop_si(ctx, "version",
  232. SYSINFO_ID_SMBIOS_SYSTEM_VERSION);
  233. if (serial_str) {
  234. t->serial_number = smbios_add_string(ctx, serial_str);
  235. strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
  236. } else {
  237. t->serial_number = smbios_add_prop(ctx, "serial");
  238. }
  239. t->sku_number = smbios_add_prop(ctx, "sku");
  240. t->family = smbios_add_prop(ctx, "family");
  241. len = t->length + smbios_string_table_len(ctx);
  242. *current += len;
  243. unmap_sysmem(t);
  244. return len;
  245. }
  246. static int smbios_write_type2(ulong *current, int handle,
  247. struct smbios_ctx *ctx)
  248. {
  249. struct smbios_type2 *t;
  250. int len = sizeof(struct smbios_type2);
  251. t = map_sysmem(*current, len);
  252. memset(t, 0, sizeof(struct smbios_type2));
  253. fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
  254. smbios_set_eos(ctx, t->eos);
  255. t->manufacturer = smbios_add_prop(ctx, "manufacturer");
  256. t->product_name = smbios_add_prop(ctx, "product");
  257. t->version = smbios_add_prop_si(ctx, "version",
  258. SYSINFO_ID_SMBIOS_BASEBOARD_VERSION);
  259. t->asset_tag_number = smbios_add_prop(ctx, "asset-tag");
  260. t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
  261. t->board_type = SMBIOS_BOARD_MOTHERBOARD;
  262. len = t->length + smbios_string_table_len(ctx);
  263. *current += len;
  264. unmap_sysmem(t);
  265. return len;
  266. }
  267. static int smbios_write_type3(ulong *current, int handle,
  268. struct smbios_ctx *ctx)
  269. {
  270. struct smbios_type3 *t;
  271. int len = sizeof(struct smbios_type3);
  272. t = map_sysmem(*current, len);
  273. memset(t, 0, sizeof(struct smbios_type3));
  274. fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
  275. smbios_set_eos(ctx, t->eos);
  276. t->manufacturer = smbios_add_prop(ctx, "manufacturer");
  277. t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
  278. t->bootup_state = SMBIOS_STATE_SAFE;
  279. t->power_supply_state = SMBIOS_STATE_SAFE;
  280. t->thermal_state = SMBIOS_STATE_SAFE;
  281. t->security_status = SMBIOS_SECURITY_NONE;
  282. len = t->length + smbios_string_table_len(ctx);
  283. *current += len;
  284. unmap_sysmem(t);
  285. return len;
  286. }
  287. static void smbios_write_type4_dm(struct smbios_type4 *t,
  288. struct smbios_ctx *ctx)
  289. {
  290. u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
  291. const char *vendor = "Unknown";
  292. const char *name = "Unknown";
  293. #ifdef CONFIG_CPU
  294. char processor_name[49];
  295. char vendor_name[49];
  296. struct udevice *cpu = NULL;
  297. uclass_find_first_device(UCLASS_CPU, &cpu);
  298. if (cpu) {
  299. struct cpu_plat *plat = dev_get_parent_plat(cpu);
  300. if (plat->family)
  301. processor_family = plat->family;
  302. t->processor_id[0] = plat->id[0];
  303. t->processor_id[1] = plat->id[1];
  304. if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name)))
  305. vendor = vendor_name;
  306. if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name)))
  307. name = processor_name;
  308. }
  309. #endif
  310. t->processor_family = processor_family;
  311. t->processor_manufacturer = smbios_add_string(ctx, vendor);
  312. t->processor_version = smbios_add_string(ctx, name);
  313. }
  314. static int smbios_write_type4(ulong *current, int handle,
  315. struct smbios_ctx *ctx)
  316. {
  317. struct smbios_type4 *t;
  318. int len = sizeof(struct smbios_type4);
  319. t = map_sysmem(*current, len);
  320. memset(t, 0, sizeof(struct smbios_type4));
  321. fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
  322. smbios_set_eos(ctx, t->eos);
  323. t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
  324. smbios_write_type4_dm(t, ctx);
  325. t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
  326. t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
  327. t->l1_cache_handle = 0xffff;
  328. t->l2_cache_handle = 0xffff;
  329. t->l3_cache_handle = 0xffff;
  330. t->processor_family2 = t->processor_family;
  331. len = t->length + smbios_string_table_len(ctx);
  332. *current += len;
  333. unmap_sysmem(t);
  334. return len;
  335. }
  336. static int smbios_write_type32(ulong *current, int handle,
  337. struct smbios_ctx *ctx)
  338. {
  339. struct smbios_type32 *t;
  340. int len = sizeof(struct smbios_type32);
  341. t = map_sysmem(*current, len);
  342. memset(t, 0, sizeof(struct smbios_type32));
  343. fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
  344. smbios_set_eos(ctx, t->eos);
  345. *current += len;
  346. unmap_sysmem(t);
  347. return len;
  348. }
  349. static int smbios_write_type127(ulong *current, int handle,
  350. struct smbios_ctx *ctx)
  351. {
  352. struct smbios_type127 *t;
  353. int len = sizeof(struct smbios_type127);
  354. t = map_sysmem(*current, len);
  355. memset(t, 0, sizeof(struct smbios_type127));
  356. fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
  357. *current += len;
  358. unmap_sysmem(t);
  359. return len;
  360. }
  361. static struct smbios_write_method smbios_write_funcs[] = {
  362. { smbios_write_type0, "bios", },
  363. { smbios_write_type1, "system", },
  364. { smbios_write_type2, "baseboard", },
  365. { smbios_write_type3, "chassis", },
  366. { smbios_write_type4, },
  367. { smbios_write_type32, },
  368. { smbios_write_type127 },
  369. };
  370. ulong write_smbios_table(ulong addr)
  371. {
  372. ofnode parent_node = ofnode_null();
  373. struct smbios_entry *se;
  374. struct smbios_ctx ctx;
  375. ulong table_addr;
  376. ulong tables;
  377. int len = 0;
  378. int max_struct_size = 0;
  379. int handle = 0;
  380. char *istart;
  381. int isize;
  382. int i;
  383. ctx.node = ofnode_null();
  384. if (IS_ENABLED(CONFIG_OF_CONTROL)) {
  385. uclass_first_device(UCLASS_SYSINFO, &ctx.dev);
  386. if (ctx.dev)
  387. parent_node = dev_read_subnode(ctx.dev, "smbios");
  388. } else {
  389. ctx.dev = NULL;
  390. }
  391. /* 16 byte align the table address */
  392. addr = ALIGN(addr, 16);
  393. se = map_sysmem(addr, sizeof(struct smbios_entry));
  394. memset(se, 0, sizeof(struct smbios_entry));
  395. addr += sizeof(struct smbios_entry);
  396. addr = ALIGN(addr, 16);
  397. tables = addr;
  398. /* populate minimum required tables */
  399. for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
  400. const struct smbios_write_method *method;
  401. int tmp;
  402. method = &smbios_write_funcs[i];
  403. if (IS_ENABLED(CONFIG_OF_CONTROL) && method->subnode_name)
  404. ctx.node = ofnode_find_subnode(parent_node,
  405. method->subnode_name);
  406. tmp = method->write((ulong *)&addr, handle++, &ctx);
  407. max_struct_size = max(max_struct_size, tmp);
  408. len += tmp;
  409. }
  410. memcpy(se->anchor, "_SM_", 4);
  411. se->length = sizeof(struct smbios_entry);
  412. se->major_ver = SMBIOS_MAJOR_VER;
  413. se->minor_ver = SMBIOS_MINOR_VER;
  414. se->max_struct_size = max_struct_size;
  415. memcpy(se->intermediate_anchor, "_DMI_", 5);
  416. se->struct_table_length = len;
  417. /*
  418. * We must use a pointer here so things work correctly on sandbox. The
  419. * user of this table is not aware of the mapping of addresses to
  420. * sandbox's DRAM buffer.
  421. */
  422. table_addr = (ulong)map_sysmem(tables, 0);
  423. if (sizeof(table_addr) > sizeof(u32) && table_addr > (ulong)UINT_MAX) {
  424. /*
  425. * We need to put this >32-bit pointer into the table but the
  426. * field is only 32 bits wide.
  427. */
  428. printf("WARNING: SMBIOS table_address overflow %llx\n",
  429. (unsigned long long)table_addr);
  430. table_addr = 0;
  431. }
  432. se->struct_table_address = table_addr;
  433. se->struct_count = handle;
  434. /* calculate checksums */
  435. istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
  436. isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
  437. se->intermediate_checksum = table_compute_checksum(istart, isize);
  438. se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));
  439. unmap_sysmem(se);
  440. return addr;
  441. }