smbios.c 15 KB

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