smbios.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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_ext2 |= BIOS_CHARACTERISTICS_EXT2_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. if (!t->manufacturer)
  231. t->manufacturer = smbios_add_string(ctx, "Unknown");
  232. t->product_name = smbios_add_prop(ctx, "product");
  233. if (!t->product_name)
  234. t->product_name = smbios_add_string(ctx, "Unknown Product");
  235. t->version = smbios_add_prop_si(ctx, "version",
  236. SYSINFO_ID_SMBIOS_SYSTEM_VERSION);
  237. if (serial_str) {
  238. t->serial_number = smbios_add_string(ctx, serial_str);
  239. strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
  240. } else {
  241. t->serial_number = smbios_add_prop(ctx, "serial");
  242. }
  243. t->sku_number = smbios_add_prop(ctx, "sku");
  244. t->family = smbios_add_prop(ctx, "family");
  245. len = t->length + smbios_string_table_len(ctx);
  246. *current += len;
  247. unmap_sysmem(t);
  248. return len;
  249. }
  250. static int smbios_write_type2(ulong *current, int handle,
  251. struct smbios_ctx *ctx)
  252. {
  253. struct smbios_type2 *t;
  254. int len = sizeof(struct smbios_type2);
  255. t = map_sysmem(*current, len);
  256. memset(t, 0, sizeof(struct smbios_type2));
  257. fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
  258. smbios_set_eos(ctx, t->eos);
  259. t->manufacturer = smbios_add_prop(ctx, "manufacturer");
  260. if (!t->manufacturer)
  261. t->manufacturer = smbios_add_string(ctx, "Unknown");
  262. t->product_name = smbios_add_prop(ctx, "product");
  263. if (!t->product_name)
  264. t->product_name = smbios_add_string(ctx, "Unknown Product");
  265. t->version = smbios_add_prop_si(ctx, "version",
  266. SYSINFO_ID_SMBIOS_BASEBOARD_VERSION);
  267. t->asset_tag_number = smbios_add_prop(ctx, "asset-tag");
  268. t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
  269. t->board_type = SMBIOS_BOARD_MOTHERBOARD;
  270. len = t->length + smbios_string_table_len(ctx);
  271. *current += len;
  272. unmap_sysmem(t);
  273. return len;
  274. }
  275. static int smbios_write_type3(ulong *current, int handle,
  276. struct smbios_ctx *ctx)
  277. {
  278. struct smbios_type3 *t;
  279. int len = sizeof(struct smbios_type3);
  280. t = map_sysmem(*current, len);
  281. memset(t, 0, sizeof(struct smbios_type3));
  282. fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
  283. smbios_set_eos(ctx, t->eos);
  284. t->manufacturer = smbios_add_prop(ctx, "manufacturer");
  285. if (!t->manufacturer)
  286. t->manufacturer = smbios_add_string(ctx, "Unknown");
  287. t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
  288. t->bootup_state = SMBIOS_STATE_SAFE;
  289. t->power_supply_state = SMBIOS_STATE_SAFE;
  290. t->thermal_state = SMBIOS_STATE_SAFE;
  291. t->security_status = SMBIOS_SECURITY_NONE;
  292. len = t->length + smbios_string_table_len(ctx);
  293. *current += len;
  294. unmap_sysmem(t);
  295. return len;
  296. }
  297. static void smbios_write_type4_dm(struct smbios_type4 *t,
  298. struct smbios_ctx *ctx)
  299. {
  300. u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
  301. const char *vendor = "Unknown";
  302. const char *name = "Unknown";
  303. #ifdef CONFIG_CPU
  304. char processor_name[49];
  305. char vendor_name[49];
  306. struct udevice *cpu = NULL;
  307. uclass_find_first_device(UCLASS_CPU, &cpu);
  308. if (cpu) {
  309. struct cpu_plat *plat = dev_get_parent_plat(cpu);
  310. if (plat->family)
  311. processor_family = plat->family;
  312. t->processor_id[0] = plat->id[0];
  313. t->processor_id[1] = plat->id[1];
  314. if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name)))
  315. vendor = vendor_name;
  316. if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name)))
  317. name = processor_name;
  318. }
  319. #endif
  320. t->processor_family = processor_family;
  321. t->processor_manufacturer = smbios_add_string(ctx, vendor);
  322. t->processor_version = smbios_add_string(ctx, name);
  323. }
  324. static int smbios_write_type4(ulong *current, int handle,
  325. struct smbios_ctx *ctx)
  326. {
  327. struct smbios_type4 *t;
  328. int len = sizeof(struct smbios_type4);
  329. t = map_sysmem(*current, len);
  330. memset(t, 0, sizeof(struct smbios_type4));
  331. fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
  332. smbios_set_eos(ctx, t->eos);
  333. t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
  334. smbios_write_type4_dm(t, ctx);
  335. t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
  336. t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
  337. t->l1_cache_handle = 0xffff;
  338. t->l2_cache_handle = 0xffff;
  339. t->l3_cache_handle = 0xffff;
  340. t->processor_family2 = t->processor_family;
  341. len = t->length + smbios_string_table_len(ctx);
  342. *current += len;
  343. unmap_sysmem(t);
  344. return len;
  345. }
  346. static int smbios_write_type32(ulong *current, int handle,
  347. struct smbios_ctx *ctx)
  348. {
  349. struct smbios_type32 *t;
  350. int len = sizeof(struct smbios_type32);
  351. t = map_sysmem(*current, len);
  352. memset(t, 0, sizeof(struct smbios_type32));
  353. fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
  354. smbios_set_eos(ctx, t->eos);
  355. *current += len;
  356. unmap_sysmem(t);
  357. return len;
  358. }
  359. static int smbios_write_type127(ulong *current, int handle,
  360. struct smbios_ctx *ctx)
  361. {
  362. struct smbios_type127 *t;
  363. int len = sizeof(struct smbios_type127);
  364. t = map_sysmem(*current, len);
  365. memset(t, 0, sizeof(struct smbios_type127));
  366. fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
  367. *current += len;
  368. unmap_sysmem(t);
  369. return len;
  370. }
  371. static struct smbios_write_method smbios_write_funcs[] = {
  372. { smbios_write_type0, "bios", },
  373. { smbios_write_type1, "system", },
  374. { smbios_write_type2, "baseboard", },
  375. { smbios_write_type3, "chassis", },
  376. { smbios_write_type4, },
  377. { smbios_write_type32, },
  378. { smbios_write_type127 },
  379. };
  380. ulong write_smbios_table(ulong addr)
  381. {
  382. ofnode parent_node = ofnode_null();
  383. struct smbios_entry *se;
  384. struct smbios_ctx ctx;
  385. ulong table_addr;
  386. ulong tables;
  387. int len = 0;
  388. int max_struct_size = 0;
  389. int handle = 0;
  390. char *istart;
  391. int isize;
  392. int i;
  393. ctx.node = ofnode_null();
  394. if (IS_ENABLED(CONFIG_OF_CONTROL)) {
  395. uclass_first_device(UCLASS_SYSINFO, &ctx.dev);
  396. if (ctx.dev)
  397. parent_node = dev_read_subnode(ctx.dev, "smbios");
  398. } else {
  399. ctx.dev = NULL;
  400. }
  401. /* 16 byte align the table address */
  402. addr = ALIGN(addr, 16);
  403. se = map_sysmem(addr, sizeof(struct smbios_entry));
  404. memset(se, 0, sizeof(struct smbios_entry));
  405. addr += sizeof(struct smbios_entry);
  406. addr = ALIGN(addr, 16);
  407. tables = addr;
  408. /* populate minimum required tables */
  409. for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
  410. const struct smbios_write_method *method;
  411. int tmp;
  412. method = &smbios_write_funcs[i];
  413. if (IS_ENABLED(CONFIG_OF_CONTROL) && method->subnode_name)
  414. ctx.node = ofnode_find_subnode(parent_node,
  415. method->subnode_name);
  416. tmp = method->write((ulong *)&addr, handle++, &ctx);
  417. max_struct_size = max(max_struct_size, tmp);
  418. len += tmp;
  419. }
  420. memcpy(se->anchor, "_SM_", 4);
  421. se->length = sizeof(struct smbios_entry);
  422. se->major_ver = SMBIOS_MAJOR_VER;
  423. se->minor_ver = SMBIOS_MINOR_VER;
  424. se->max_struct_size = max_struct_size;
  425. memcpy(se->intermediate_anchor, "_DMI_", 5);
  426. se->struct_table_length = len;
  427. /*
  428. * We must use a pointer here so things work correctly on sandbox. The
  429. * user of this table is not aware of the mapping of addresses to
  430. * sandbox's DRAM buffer.
  431. */
  432. table_addr = (ulong)map_sysmem(tables, 0);
  433. if (sizeof(table_addr) > sizeof(u32) && table_addr > (ulong)UINT_MAX) {
  434. /*
  435. * We need to put this >32-bit pointer into the table but the
  436. * field is only 32 bits wide.
  437. */
  438. printf("WARNING: SMBIOS table_address overflow %llx\n",
  439. (unsigned long long)table_addr);
  440. table_addr = 0;
  441. }
  442. se->struct_table_address = table_addr;
  443. se->struct_count = handle;
  444. /* calculate checksums */
  445. istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
  446. isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
  447. se->intermediate_checksum = table_compute_checksum(istart, isize);
  448. se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));
  449. unmap_sysmem(se);
  450. return addr;
  451. }