smbios.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  1. /*
  2. * SMBIOS Support
  3. *
  4. * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
  5. * Copyright (C) 2013 Red Hat, Inc.
  6. *
  7. * Authors:
  8. * Alex Williamson <alex.williamson@hp.com>
  9. * Markus Armbruster <armbru@redhat.com>
  10. *
  11. * This work is licensed under the terms of the GNU GPL, version 2. See
  12. * the COPYING file in the top-level directory.
  13. *
  14. * Contributions after 2012-01-13 are licensed under the terms of the
  15. * GNU GPL, version 2 or (at your option) any later version.
  16. */
  17. #include "qemu/osdep.h"
  18. #include "qemu/units.h"
  19. #include "qapi/error.h"
  20. #include "qemu/config-file.h"
  21. #include "qemu/error-report.h"
  22. #include "qemu/module.h"
  23. #include "qemu/option.h"
  24. #include "sysemu/sysemu.h"
  25. #include "qemu/uuid.h"
  26. #include "hw/firmware/smbios.h"
  27. #include "hw/loader.h"
  28. #include "hw/boards.h"
  29. #include "smbios_build.h"
  30. /* legacy structures and constants for <= 2.0 machines */
  31. struct smbios_header {
  32. uint16_t length;
  33. uint8_t type;
  34. } QEMU_PACKED;
  35. struct smbios_field {
  36. struct smbios_header header;
  37. uint8_t type;
  38. uint16_t offset;
  39. uint8_t data[];
  40. } QEMU_PACKED;
  41. struct smbios_table {
  42. struct smbios_header header;
  43. uint8_t data[];
  44. } QEMU_PACKED;
  45. #define SMBIOS_FIELD_ENTRY 0
  46. #define SMBIOS_TABLE_ENTRY 1
  47. static uint8_t *smbios_entries;
  48. static size_t smbios_entries_len;
  49. static bool smbios_legacy = true;
  50. static bool smbios_uuid_encoded = true;
  51. /* end: legacy structures & constants for <= 2.0 machines */
  52. uint8_t *smbios_tables;
  53. size_t smbios_tables_len;
  54. unsigned smbios_table_max;
  55. unsigned smbios_table_cnt;
  56. static SmbiosEntryPointType smbios_ep_type = SMBIOS_ENTRY_POINT_21;
  57. static SmbiosEntryPoint ep;
  58. static int smbios_type4_count = 0;
  59. static bool smbios_immutable;
  60. static bool smbios_have_defaults;
  61. static uint32_t smbios_cpuid_version, smbios_cpuid_features, smbios_smp_sockets;
  62. static DECLARE_BITMAP(have_binfile_bitmap, SMBIOS_MAX_TYPE+1);
  63. static DECLARE_BITMAP(have_fields_bitmap, SMBIOS_MAX_TYPE+1);
  64. static struct {
  65. const char *vendor, *version, *date;
  66. bool have_major_minor, uefi;
  67. uint8_t major, minor;
  68. } type0;
  69. static struct {
  70. const char *manufacturer, *product, *version, *serial, *sku, *family;
  71. /* uuid is in qemu_uuid */
  72. } type1;
  73. static struct {
  74. const char *manufacturer, *product, *version, *serial, *asset, *location;
  75. } type2;
  76. static struct {
  77. const char *manufacturer, *version, *serial, *asset, *sku;
  78. } type3;
  79. static struct {
  80. const char *sock_pfx, *manufacturer, *version, *serial, *asset, *part;
  81. } type4;
  82. static struct {
  83. size_t nvalues;
  84. const char **values;
  85. } type11;
  86. static struct {
  87. const char *loc_pfx, *bank, *manufacturer, *serial, *asset, *part;
  88. uint16_t speed;
  89. } type17;
  90. static QemuOptsList qemu_smbios_opts = {
  91. .name = "smbios",
  92. .head = QTAILQ_HEAD_INITIALIZER(qemu_smbios_opts.head),
  93. .desc = {
  94. /*
  95. * no elements => accept any params
  96. * validation will happen later
  97. */
  98. { /* end of list */ }
  99. }
  100. };
  101. static const QemuOptDesc qemu_smbios_file_opts[] = {
  102. {
  103. .name = "file",
  104. .type = QEMU_OPT_STRING,
  105. .help = "binary file containing an SMBIOS element",
  106. },
  107. { /* end of list */ }
  108. };
  109. static const QemuOptDesc qemu_smbios_type0_opts[] = {
  110. {
  111. .name = "type",
  112. .type = QEMU_OPT_NUMBER,
  113. .help = "SMBIOS element type",
  114. },{
  115. .name = "vendor",
  116. .type = QEMU_OPT_STRING,
  117. .help = "vendor name",
  118. },{
  119. .name = "version",
  120. .type = QEMU_OPT_STRING,
  121. .help = "version number",
  122. },{
  123. .name = "date",
  124. .type = QEMU_OPT_STRING,
  125. .help = "release date",
  126. },{
  127. .name = "release",
  128. .type = QEMU_OPT_STRING,
  129. .help = "revision number",
  130. },{
  131. .name = "uefi",
  132. .type = QEMU_OPT_BOOL,
  133. .help = "uefi support",
  134. },
  135. { /* end of list */ }
  136. };
  137. static const QemuOptDesc qemu_smbios_type1_opts[] = {
  138. {
  139. .name = "type",
  140. .type = QEMU_OPT_NUMBER,
  141. .help = "SMBIOS element type",
  142. },{
  143. .name = "manufacturer",
  144. .type = QEMU_OPT_STRING,
  145. .help = "manufacturer name",
  146. },{
  147. .name = "product",
  148. .type = QEMU_OPT_STRING,
  149. .help = "product name",
  150. },{
  151. .name = "version",
  152. .type = QEMU_OPT_STRING,
  153. .help = "version number",
  154. },{
  155. .name = "serial",
  156. .type = QEMU_OPT_STRING,
  157. .help = "serial number",
  158. },{
  159. .name = "uuid",
  160. .type = QEMU_OPT_STRING,
  161. .help = "UUID",
  162. },{
  163. .name = "sku",
  164. .type = QEMU_OPT_STRING,
  165. .help = "SKU number",
  166. },{
  167. .name = "family",
  168. .type = QEMU_OPT_STRING,
  169. .help = "family name",
  170. },
  171. { /* end of list */ }
  172. };
  173. static const QemuOptDesc qemu_smbios_type2_opts[] = {
  174. {
  175. .name = "type",
  176. .type = QEMU_OPT_NUMBER,
  177. .help = "SMBIOS element type",
  178. },{
  179. .name = "manufacturer",
  180. .type = QEMU_OPT_STRING,
  181. .help = "manufacturer name",
  182. },{
  183. .name = "product",
  184. .type = QEMU_OPT_STRING,
  185. .help = "product name",
  186. },{
  187. .name = "version",
  188. .type = QEMU_OPT_STRING,
  189. .help = "version number",
  190. },{
  191. .name = "serial",
  192. .type = QEMU_OPT_STRING,
  193. .help = "serial number",
  194. },{
  195. .name = "asset",
  196. .type = QEMU_OPT_STRING,
  197. .help = "asset tag number",
  198. },{
  199. .name = "location",
  200. .type = QEMU_OPT_STRING,
  201. .help = "location in chassis",
  202. },
  203. { /* end of list */ }
  204. };
  205. static const QemuOptDesc qemu_smbios_type3_opts[] = {
  206. {
  207. .name = "type",
  208. .type = QEMU_OPT_NUMBER,
  209. .help = "SMBIOS element type",
  210. },{
  211. .name = "manufacturer",
  212. .type = QEMU_OPT_STRING,
  213. .help = "manufacturer name",
  214. },{
  215. .name = "version",
  216. .type = QEMU_OPT_STRING,
  217. .help = "version number",
  218. },{
  219. .name = "serial",
  220. .type = QEMU_OPT_STRING,
  221. .help = "serial number",
  222. },{
  223. .name = "asset",
  224. .type = QEMU_OPT_STRING,
  225. .help = "asset tag number",
  226. },{
  227. .name = "sku",
  228. .type = QEMU_OPT_STRING,
  229. .help = "SKU number",
  230. },
  231. { /* end of list */ }
  232. };
  233. static const QemuOptDesc qemu_smbios_type4_opts[] = {
  234. {
  235. .name = "type",
  236. .type = QEMU_OPT_NUMBER,
  237. .help = "SMBIOS element type",
  238. },{
  239. .name = "sock_pfx",
  240. .type = QEMU_OPT_STRING,
  241. .help = "socket designation string prefix",
  242. },{
  243. .name = "manufacturer",
  244. .type = QEMU_OPT_STRING,
  245. .help = "manufacturer name",
  246. },{
  247. .name = "version",
  248. .type = QEMU_OPT_STRING,
  249. .help = "version number",
  250. },{
  251. .name = "serial",
  252. .type = QEMU_OPT_STRING,
  253. .help = "serial number",
  254. },{
  255. .name = "asset",
  256. .type = QEMU_OPT_STRING,
  257. .help = "asset tag number",
  258. },{
  259. .name = "part",
  260. .type = QEMU_OPT_STRING,
  261. .help = "part number",
  262. },
  263. { /* end of list */ }
  264. };
  265. static const QemuOptDesc qemu_smbios_type11_opts[] = {
  266. {
  267. .name = "value",
  268. .type = QEMU_OPT_STRING,
  269. .help = "OEM string data",
  270. },
  271. };
  272. static const QemuOptDesc qemu_smbios_type17_opts[] = {
  273. {
  274. .name = "type",
  275. .type = QEMU_OPT_NUMBER,
  276. .help = "SMBIOS element type",
  277. },{
  278. .name = "loc_pfx",
  279. .type = QEMU_OPT_STRING,
  280. .help = "device locator string prefix",
  281. },{
  282. .name = "bank",
  283. .type = QEMU_OPT_STRING,
  284. .help = "bank locator string",
  285. },{
  286. .name = "manufacturer",
  287. .type = QEMU_OPT_STRING,
  288. .help = "manufacturer name",
  289. },{
  290. .name = "serial",
  291. .type = QEMU_OPT_STRING,
  292. .help = "serial number",
  293. },{
  294. .name = "asset",
  295. .type = QEMU_OPT_STRING,
  296. .help = "asset tag number",
  297. },{
  298. .name = "part",
  299. .type = QEMU_OPT_STRING,
  300. .help = "part number",
  301. },{
  302. .name = "speed",
  303. .type = QEMU_OPT_NUMBER,
  304. .help = "maximum capable speed",
  305. },
  306. { /* end of list */ }
  307. };
  308. static void smbios_register_config(void)
  309. {
  310. qemu_add_opts(&qemu_smbios_opts);
  311. }
  312. opts_init(smbios_register_config);
  313. static void smbios_validate_table(MachineState *ms)
  314. {
  315. uint32_t expect_t4_count = smbios_legacy ?
  316. ms->smp.cpus : smbios_smp_sockets;
  317. if (smbios_type4_count && smbios_type4_count != expect_t4_count) {
  318. error_report("Expected %d SMBIOS Type 4 tables, got %d instead",
  319. expect_t4_count, smbios_type4_count);
  320. exit(1);
  321. }
  322. }
  323. /* legacy setup functions for <= 2.0 machines */
  324. static void smbios_add_field(int type, int offset, const void *data, size_t len)
  325. {
  326. struct smbios_field *field;
  327. if (!smbios_entries) {
  328. smbios_entries_len = sizeof(uint16_t);
  329. smbios_entries = g_malloc0(smbios_entries_len);
  330. }
  331. smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
  332. sizeof(*field) + len);
  333. field = (struct smbios_field *)(smbios_entries + smbios_entries_len);
  334. field->header.type = SMBIOS_FIELD_ENTRY;
  335. field->header.length = cpu_to_le16(sizeof(*field) + len);
  336. field->type = type;
  337. field->offset = cpu_to_le16(offset);
  338. memcpy(field->data, data, len);
  339. smbios_entries_len += sizeof(*field) + len;
  340. (*(uint16_t *)smbios_entries) =
  341. cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
  342. }
  343. static void smbios_maybe_add_str(int type, int offset, const char *data)
  344. {
  345. if (data) {
  346. smbios_add_field(type, offset, data, strlen(data) + 1);
  347. }
  348. }
  349. static void smbios_build_type_0_fields(void)
  350. {
  351. smbios_maybe_add_str(0, offsetof(struct smbios_type_0, vendor_str),
  352. type0.vendor);
  353. smbios_maybe_add_str(0, offsetof(struct smbios_type_0, bios_version_str),
  354. type0.version);
  355. smbios_maybe_add_str(0, offsetof(struct smbios_type_0,
  356. bios_release_date_str),
  357. type0.date);
  358. if (type0.have_major_minor) {
  359. smbios_add_field(0, offsetof(struct smbios_type_0,
  360. system_bios_major_release),
  361. &type0.major, 1);
  362. smbios_add_field(0, offsetof(struct smbios_type_0,
  363. system_bios_minor_release),
  364. &type0.minor, 1);
  365. }
  366. }
  367. static void smbios_build_type_1_fields(void)
  368. {
  369. smbios_maybe_add_str(1, offsetof(struct smbios_type_1, manufacturer_str),
  370. type1.manufacturer);
  371. smbios_maybe_add_str(1, offsetof(struct smbios_type_1, product_name_str),
  372. type1.product);
  373. smbios_maybe_add_str(1, offsetof(struct smbios_type_1, version_str),
  374. type1.version);
  375. smbios_maybe_add_str(1, offsetof(struct smbios_type_1, serial_number_str),
  376. type1.serial);
  377. smbios_maybe_add_str(1, offsetof(struct smbios_type_1, sku_number_str),
  378. type1.sku);
  379. smbios_maybe_add_str(1, offsetof(struct smbios_type_1, family_str),
  380. type1.family);
  381. if (qemu_uuid_set) {
  382. /* We don't encode the UUID in the "wire format" here because this
  383. * function is for legacy mode and needs to keep the guest ABI, and
  384. * because we don't know what's the SMBIOS version advertised by the
  385. * BIOS.
  386. */
  387. smbios_add_field(1, offsetof(struct smbios_type_1, uuid),
  388. &qemu_uuid, 16);
  389. }
  390. }
  391. uint8_t *smbios_get_table_legacy(MachineState *ms, size_t *length)
  392. {
  393. if (!smbios_legacy) {
  394. *length = 0;
  395. return NULL;
  396. }
  397. if (!smbios_immutable) {
  398. smbios_build_type_0_fields();
  399. smbios_build_type_1_fields();
  400. smbios_validate_table(ms);
  401. smbios_immutable = true;
  402. }
  403. *length = smbios_entries_len;
  404. return smbios_entries;
  405. }
  406. /* end: legacy setup functions for <= 2.0 machines */
  407. bool smbios_skip_table(uint8_t type, bool required_table)
  408. {
  409. if (test_bit(type, have_binfile_bitmap)) {
  410. return true; /* user provided their own binary blob(s) */
  411. }
  412. if (test_bit(type, have_fields_bitmap)) {
  413. return false; /* user provided fields via command line */
  414. }
  415. if (smbios_have_defaults && required_table) {
  416. return false; /* we're building tables, and this one's required */
  417. }
  418. return true;
  419. }
  420. static void smbios_build_type_0_table(void)
  421. {
  422. SMBIOS_BUILD_TABLE_PRE(0, 0x000, false); /* optional, leave up to BIOS */
  423. SMBIOS_TABLE_SET_STR(0, vendor_str, type0.vendor);
  424. SMBIOS_TABLE_SET_STR(0, bios_version_str, type0.version);
  425. t->bios_starting_address_segment = cpu_to_le16(0xE800); /* from SeaBIOS */
  426. SMBIOS_TABLE_SET_STR(0, bios_release_date_str, type0.date);
  427. t->bios_rom_size = 0; /* hardcoded in SeaBIOS with FIXME comment */
  428. t->bios_characteristics = cpu_to_le64(0x08); /* Not supported */
  429. t->bios_characteristics_extension_bytes[0] = 0;
  430. t->bios_characteristics_extension_bytes[1] = 0x14; /* TCD/SVVP | VM */
  431. if (type0.uefi) {
  432. t->bios_characteristics_extension_bytes[1] |= 0x08; /* |= UEFI */
  433. }
  434. if (type0.have_major_minor) {
  435. t->system_bios_major_release = type0.major;
  436. t->system_bios_minor_release = type0.minor;
  437. } else {
  438. t->system_bios_major_release = 0;
  439. t->system_bios_minor_release = 0;
  440. }
  441. /* hardcoded in SeaBIOS */
  442. t->embedded_controller_major_release = 0xFF;
  443. t->embedded_controller_minor_release = 0xFF;
  444. SMBIOS_BUILD_TABLE_POST;
  445. }
  446. /* Encode UUID from the big endian encoding described on RFC4122 to the wire
  447. * format specified by SMBIOS version 2.6.
  448. */
  449. static void smbios_encode_uuid(struct smbios_uuid *uuid, QemuUUID *in)
  450. {
  451. memcpy(uuid, in, 16);
  452. if (smbios_uuid_encoded) {
  453. uuid->time_low = bswap32(uuid->time_low);
  454. uuid->time_mid = bswap16(uuid->time_mid);
  455. uuid->time_hi_and_version = bswap16(uuid->time_hi_and_version);
  456. }
  457. }
  458. static void smbios_build_type_1_table(void)
  459. {
  460. SMBIOS_BUILD_TABLE_PRE(1, 0x100, true); /* required */
  461. SMBIOS_TABLE_SET_STR(1, manufacturer_str, type1.manufacturer);
  462. SMBIOS_TABLE_SET_STR(1, product_name_str, type1.product);
  463. SMBIOS_TABLE_SET_STR(1, version_str, type1.version);
  464. SMBIOS_TABLE_SET_STR(1, serial_number_str, type1.serial);
  465. if (qemu_uuid_set) {
  466. smbios_encode_uuid(&t->uuid, &qemu_uuid);
  467. } else {
  468. memset(&t->uuid, 0, 16);
  469. }
  470. t->wake_up_type = 0x06; /* power switch */
  471. SMBIOS_TABLE_SET_STR(1, sku_number_str, type1.sku);
  472. SMBIOS_TABLE_SET_STR(1, family_str, type1.family);
  473. SMBIOS_BUILD_TABLE_POST;
  474. }
  475. static void smbios_build_type_2_table(void)
  476. {
  477. SMBIOS_BUILD_TABLE_PRE(2, 0x200, false); /* optional */
  478. SMBIOS_TABLE_SET_STR(2, manufacturer_str, type2.manufacturer);
  479. SMBIOS_TABLE_SET_STR(2, product_str, type2.product);
  480. SMBIOS_TABLE_SET_STR(2, version_str, type2.version);
  481. SMBIOS_TABLE_SET_STR(2, serial_number_str, type2.serial);
  482. SMBIOS_TABLE_SET_STR(2, asset_tag_number_str, type2.asset);
  483. t->feature_flags = 0x01; /* Motherboard */
  484. SMBIOS_TABLE_SET_STR(2, location_str, type2.location);
  485. t->chassis_handle = cpu_to_le16(0x300); /* Type 3 (System enclosure) */
  486. t->board_type = 0x0A; /* Motherboard */
  487. t->contained_element_count = 0;
  488. SMBIOS_BUILD_TABLE_POST;
  489. }
  490. static void smbios_build_type_3_table(void)
  491. {
  492. SMBIOS_BUILD_TABLE_PRE(3, 0x300, true); /* required */
  493. SMBIOS_TABLE_SET_STR(3, manufacturer_str, type3.manufacturer);
  494. t->type = 0x01; /* Other */
  495. SMBIOS_TABLE_SET_STR(3, version_str, type3.version);
  496. SMBIOS_TABLE_SET_STR(3, serial_number_str, type3.serial);
  497. SMBIOS_TABLE_SET_STR(3, asset_tag_number_str, type3.asset);
  498. t->boot_up_state = 0x03; /* Safe */
  499. t->power_supply_state = 0x03; /* Safe */
  500. t->thermal_state = 0x03; /* Safe */
  501. t->security_status = 0x02; /* Unknown */
  502. t->oem_defined = cpu_to_le32(0);
  503. t->height = 0;
  504. t->number_of_power_cords = 0;
  505. t->contained_element_count = 0;
  506. t->contained_element_record_length = 0;
  507. SMBIOS_TABLE_SET_STR(3, sku_number_str, type3.sku);
  508. SMBIOS_BUILD_TABLE_POST;
  509. }
  510. static void smbios_build_type_4_table(MachineState *ms, unsigned instance)
  511. {
  512. char sock_str[128];
  513. SMBIOS_BUILD_TABLE_PRE(4, 0x400 + instance, true); /* required */
  514. snprintf(sock_str, sizeof(sock_str), "%s%2x", type4.sock_pfx, instance);
  515. SMBIOS_TABLE_SET_STR(4, socket_designation_str, sock_str);
  516. t->processor_type = 0x03; /* CPU */
  517. t->processor_family = 0x01; /* Other */
  518. SMBIOS_TABLE_SET_STR(4, processor_manufacturer_str, type4.manufacturer);
  519. t->processor_id[0] = cpu_to_le32(smbios_cpuid_version);
  520. t->processor_id[1] = cpu_to_le32(smbios_cpuid_features);
  521. SMBIOS_TABLE_SET_STR(4, processor_version_str, type4.version);
  522. t->voltage = 0;
  523. t->external_clock = cpu_to_le16(0); /* Unknown */
  524. /* SVVP requires max_speed and current_speed to not be unknown. */
  525. t->max_speed = cpu_to_le16(2000); /* 2000 MHz */
  526. t->current_speed = cpu_to_le16(2000); /* 2000 MHz */
  527. t->status = 0x41; /* Socket populated, CPU enabled */
  528. t->processor_upgrade = 0x01; /* Other */
  529. t->l1_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
  530. t->l2_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
  531. t->l3_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
  532. SMBIOS_TABLE_SET_STR(4, serial_number_str, type4.serial);
  533. SMBIOS_TABLE_SET_STR(4, asset_tag_number_str, type4.asset);
  534. SMBIOS_TABLE_SET_STR(4, part_number_str, type4.part);
  535. t->core_count = t->core_enabled = ms->smp.cores;
  536. t->thread_count = ms->smp.threads;
  537. t->processor_characteristics = cpu_to_le16(0x02); /* Unknown */
  538. t->processor_family2 = cpu_to_le16(0x01); /* Other */
  539. SMBIOS_BUILD_TABLE_POST;
  540. smbios_type4_count++;
  541. }
  542. static void smbios_build_type_11_table(void)
  543. {
  544. char count_str[128];
  545. size_t i;
  546. if (type11.nvalues == 0) {
  547. return;
  548. }
  549. SMBIOS_BUILD_TABLE_PRE(11, 0xe00, true); /* required */
  550. snprintf(count_str, sizeof(count_str), "%zu", type11.nvalues);
  551. t->count = type11.nvalues;
  552. for (i = 0; i < type11.nvalues; i++) {
  553. SMBIOS_TABLE_SET_STR_LIST(11, type11.values[i]);
  554. }
  555. SMBIOS_BUILD_TABLE_POST;
  556. }
  557. #define MAX_T16_STD_SZ 0x80000000 /* 2T in Kilobytes */
  558. static void smbios_build_type_16_table(unsigned dimm_cnt)
  559. {
  560. uint64_t size_kb;
  561. SMBIOS_BUILD_TABLE_PRE(16, 0x1000, true); /* required */
  562. t->location = 0x01; /* Other */
  563. t->use = 0x03; /* System memory */
  564. t->error_correction = 0x06; /* Multi-bit ECC (for Microsoft, per SeaBIOS) */
  565. size_kb = QEMU_ALIGN_UP(ram_size, KiB) / KiB;
  566. if (size_kb < MAX_T16_STD_SZ) {
  567. t->maximum_capacity = cpu_to_le32(size_kb);
  568. t->extended_maximum_capacity = cpu_to_le64(0);
  569. } else {
  570. t->maximum_capacity = cpu_to_le32(MAX_T16_STD_SZ);
  571. t->extended_maximum_capacity = cpu_to_le64(ram_size);
  572. }
  573. t->memory_error_information_handle = cpu_to_le16(0xFFFE); /* Not provided */
  574. t->number_of_memory_devices = cpu_to_le16(dimm_cnt);
  575. SMBIOS_BUILD_TABLE_POST;
  576. }
  577. #define MAX_T17_STD_SZ 0x7FFF /* (32G - 1M), in Megabytes */
  578. #define MAX_T17_EXT_SZ 0x80000000 /* 2P, in Megabytes */
  579. static void smbios_build_type_17_table(unsigned instance, uint64_t size)
  580. {
  581. char loc_str[128];
  582. uint64_t size_mb;
  583. SMBIOS_BUILD_TABLE_PRE(17, 0x1100 + instance, true); /* required */
  584. t->physical_memory_array_handle = cpu_to_le16(0x1000); /* Type 16 above */
  585. t->memory_error_information_handle = cpu_to_le16(0xFFFE); /* Not provided */
  586. t->total_width = cpu_to_le16(0xFFFF); /* Unknown */
  587. t->data_width = cpu_to_le16(0xFFFF); /* Unknown */
  588. size_mb = QEMU_ALIGN_UP(size, MiB) / MiB;
  589. if (size_mb < MAX_T17_STD_SZ) {
  590. t->size = cpu_to_le16(size_mb);
  591. t->extended_size = cpu_to_le32(0);
  592. } else {
  593. assert(size_mb < MAX_T17_EXT_SZ);
  594. t->size = cpu_to_le16(MAX_T17_STD_SZ);
  595. t->extended_size = cpu_to_le32(size_mb);
  596. }
  597. t->form_factor = 0x09; /* DIMM */
  598. t->device_set = 0; /* Not in a set */
  599. snprintf(loc_str, sizeof(loc_str), "%s %d", type17.loc_pfx, instance);
  600. SMBIOS_TABLE_SET_STR(17, device_locator_str, loc_str);
  601. SMBIOS_TABLE_SET_STR(17, bank_locator_str, type17.bank);
  602. t->memory_type = 0x07; /* RAM */
  603. t->type_detail = cpu_to_le16(0x02); /* Other */
  604. t->speed = cpu_to_le16(type17.speed);
  605. SMBIOS_TABLE_SET_STR(17, manufacturer_str, type17.manufacturer);
  606. SMBIOS_TABLE_SET_STR(17, serial_number_str, type17.serial);
  607. SMBIOS_TABLE_SET_STR(17, asset_tag_number_str, type17.asset);
  608. SMBIOS_TABLE_SET_STR(17, part_number_str, type17.part);
  609. t->attributes = 0; /* Unknown */
  610. t->configured_clock_speed = t->speed; /* reuse value for max speed */
  611. t->minimum_voltage = cpu_to_le16(0); /* Unknown */
  612. t->maximum_voltage = cpu_to_le16(0); /* Unknown */
  613. t->configured_voltage = cpu_to_le16(0); /* Unknown */
  614. SMBIOS_BUILD_TABLE_POST;
  615. }
  616. static void smbios_build_type_19_table(unsigned instance,
  617. uint64_t start, uint64_t size)
  618. {
  619. uint64_t end, start_kb, end_kb;
  620. SMBIOS_BUILD_TABLE_PRE(19, 0x1300 + instance, true); /* required */
  621. end = start + size - 1;
  622. assert(end > start);
  623. start_kb = start / KiB;
  624. end_kb = end / KiB;
  625. if (start_kb < UINT32_MAX && end_kb < UINT32_MAX) {
  626. t->starting_address = cpu_to_le32(start_kb);
  627. t->ending_address = cpu_to_le32(end_kb);
  628. t->extended_starting_address =
  629. t->extended_ending_address = cpu_to_le64(0);
  630. } else {
  631. t->starting_address = t->ending_address = cpu_to_le32(UINT32_MAX);
  632. t->extended_starting_address = cpu_to_le64(start);
  633. t->extended_ending_address = cpu_to_le64(end);
  634. }
  635. t->memory_array_handle = cpu_to_le16(0x1000); /* Type 16 above */
  636. t->partition_width = 1; /* One device per row */
  637. SMBIOS_BUILD_TABLE_POST;
  638. }
  639. static void smbios_build_type_32_table(void)
  640. {
  641. SMBIOS_BUILD_TABLE_PRE(32, 0x2000, true); /* required */
  642. memset(t->reserved, 0, 6);
  643. t->boot_status = 0; /* No errors detected */
  644. SMBIOS_BUILD_TABLE_POST;
  645. }
  646. static void smbios_build_type_127_table(void)
  647. {
  648. SMBIOS_BUILD_TABLE_PRE(127, 0x7F00, true); /* required */
  649. SMBIOS_BUILD_TABLE_POST;
  650. }
  651. void smbios_set_cpuid(uint32_t version, uint32_t features)
  652. {
  653. smbios_cpuid_version = version;
  654. smbios_cpuid_features = features;
  655. }
  656. #define SMBIOS_SET_DEFAULT(field, value) \
  657. if (!field) { \
  658. field = value; \
  659. }
  660. void smbios_set_defaults(const char *manufacturer, const char *product,
  661. const char *version, bool legacy_mode,
  662. bool uuid_encoded, SmbiosEntryPointType ep_type)
  663. {
  664. smbios_have_defaults = true;
  665. smbios_legacy = legacy_mode;
  666. smbios_uuid_encoded = uuid_encoded;
  667. smbios_ep_type = ep_type;
  668. /* drop unwanted version of command-line file blob(s) */
  669. if (smbios_legacy) {
  670. g_free(smbios_tables);
  671. /* in legacy mode, also complain if fields were given for types > 1 */
  672. if (find_next_bit(have_fields_bitmap,
  673. SMBIOS_MAX_TYPE+1, 2) < SMBIOS_MAX_TYPE+1) {
  674. error_report("can't process fields for smbios "
  675. "types > 1 on machine versions < 2.1!");
  676. exit(1);
  677. }
  678. } else {
  679. g_free(smbios_entries);
  680. }
  681. SMBIOS_SET_DEFAULT(type1.manufacturer, manufacturer);
  682. SMBIOS_SET_DEFAULT(type1.product, product);
  683. SMBIOS_SET_DEFAULT(type1.version, version);
  684. SMBIOS_SET_DEFAULT(type2.manufacturer, manufacturer);
  685. SMBIOS_SET_DEFAULT(type2.product, product);
  686. SMBIOS_SET_DEFAULT(type2.version, version);
  687. SMBIOS_SET_DEFAULT(type3.manufacturer, manufacturer);
  688. SMBIOS_SET_DEFAULT(type3.version, version);
  689. SMBIOS_SET_DEFAULT(type4.sock_pfx, "CPU");
  690. SMBIOS_SET_DEFAULT(type4.manufacturer, manufacturer);
  691. SMBIOS_SET_DEFAULT(type4.version, version);
  692. SMBIOS_SET_DEFAULT(type17.loc_pfx, "DIMM");
  693. SMBIOS_SET_DEFAULT(type17.manufacturer, manufacturer);
  694. }
  695. static void smbios_entry_point_setup(void)
  696. {
  697. switch (smbios_ep_type) {
  698. case SMBIOS_ENTRY_POINT_21:
  699. memcpy(ep.ep21.anchor_string, "_SM_", 4);
  700. memcpy(ep.ep21.intermediate_anchor_string, "_DMI_", 5);
  701. ep.ep21.length = sizeof(struct smbios_21_entry_point);
  702. ep.ep21.entry_point_revision = 0; /* formatted_area reserved */
  703. memset(ep.ep21.formatted_area, 0, 5);
  704. /* compliant with smbios spec v2.8 */
  705. ep.ep21.smbios_major_version = 2;
  706. ep.ep21.smbios_minor_version = 8;
  707. ep.ep21.smbios_bcd_revision = 0x28;
  708. /* set during table construction, but BIOS may override: */
  709. ep.ep21.structure_table_length = cpu_to_le16(smbios_tables_len);
  710. ep.ep21.max_structure_size = cpu_to_le16(smbios_table_max);
  711. ep.ep21.number_of_structures = cpu_to_le16(smbios_table_cnt);
  712. /* BIOS must recalculate */
  713. ep.ep21.checksum = 0;
  714. ep.ep21.intermediate_checksum = 0;
  715. ep.ep21.structure_table_address = cpu_to_le32(0);
  716. break;
  717. case SMBIOS_ENTRY_POINT_30:
  718. memcpy(ep.ep30.anchor_string, "_SM3_", 5);
  719. ep.ep30.length = sizeof(struct smbios_30_entry_point);
  720. ep.ep30.entry_point_revision = 1;
  721. ep.ep30.reserved = 0;
  722. /* compliant with smbios spec 3.0 */
  723. ep.ep30.smbios_major_version = 3;
  724. ep.ep30.smbios_minor_version = 0;
  725. ep.ep30.smbios_doc_rev = 0;
  726. /* set during table construct, but BIOS might override */
  727. ep.ep30.structure_table_max_size = cpu_to_le32(smbios_tables_len);
  728. /* BIOS must recalculate */
  729. ep.ep30.checksum = 0;
  730. ep.ep30.structure_table_address = cpu_to_le64(0);
  731. break;
  732. default:
  733. abort();
  734. break;
  735. }
  736. }
  737. void smbios_get_tables(MachineState *ms,
  738. const struct smbios_phys_mem_area *mem_array,
  739. const unsigned int mem_array_size,
  740. uint8_t **tables, size_t *tables_len,
  741. uint8_t **anchor, size_t *anchor_len)
  742. {
  743. unsigned i, dimm_cnt;
  744. if (smbios_legacy) {
  745. *tables = *anchor = NULL;
  746. *tables_len = *anchor_len = 0;
  747. return;
  748. }
  749. if (!smbios_immutable) {
  750. smbios_build_type_0_table();
  751. smbios_build_type_1_table();
  752. smbios_build_type_2_table();
  753. smbios_build_type_3_table();
  754. smbios_smp_sockets = DIV_ROUND_UP(ms->smp.cpus,
  755. ms->smp.cores * ms->smp.threads);
  756. assert(smbios_smp_sockets >= 1);
  757. for (i = 0; i < smbios_smp_sockets; i++) {
  758. smbios_build_type_4_table(ms, i);
  759. }
  760. smbios_build_type_11_table();
  761. #define MAX_DIMM_SZ (16 * GiB)
  762. #define GET_DIMM_SZ ((i < dimm_cnt - 1) ? MAX_DIMM_SZ \
  763. : ((ram_size - 1) % MAX_DIMM_SZ) + 1)
  764. dimm_cnt = QEMU_ALIGN_UP(ram_size, MAX_DIMM_SZ) / MAX_DIMM_SZ;
  765. smbios_build_type_16_table(dimm_cnt);
  766. for (i = 0; i < dimm_cnt; i++) {
  767. smbios_build_type_17_table(i, GET_DIMM_SZ);
  768. }
  769. for (i = 0; i < mem_array_size; i++) {
  770. smbios_build_type_19_table(i, mem_array[i].address,
  771. mem_array[i].length);
  772. }
  773. smbios_build_type_32_table();
  774. smbios_build_type_38_table();
  775. smbios_build_type_127_table();
  776. smbios_validate_table(ms);
  777. smbios_entry_point_setup();
  778. smbios_immutable = true;
  779. }
  780. /* return tables blob and entry point (anchor), and their sizes */
  781. *tables = smbios_tables;
  782. *tables_len = smbios_tables_len;
  783. *anchor = (uint8_t *)&ep;
  784. /* calculate length based on anchor string */
  785. if (!strncmp((char *)&ep, "_SM_", 4)) {
  786. *anchor_len = sizeof(struct smbios_21_entry_point);
  787. } else if (!strncmp((char *)&ep, "_SM3_", 5)) {
  788. *anchor_len = sizeof(struct smbios_30_entry_point);
  789. } else {
  790. abort();
  791. }
  792. }
  793. static void save_opt(const char **dest, QemuOpts *opts, const char *name)
  794. {
  795. const char *val = qemu_opt_get(opts, name);
  796. if (val) {
  797. *dest = val;
  798. }
  799. }
  800. struct opt_list {
  801. const char *name;
  802. size_t *ndest;
  803. const char ***dest;
  804. };
  805. static int save_opt_one(void *opaque,
  806. const char *name, const char *value,
  807. Error **errp)
  808. {
  809. struct opt_list *opt = opaque;
  810. if (!g_str_equal(name, opt->name)) {
  811. return 0;
  812. }
  813. *opt->dest = g_renew(const char *, *opt->dest, (*opt->ndest) + 1);
  814. (*opt->dest)[*opt->ndest] = value;
  815. (*opt->ndest)++;
  816. return 0;
  817. }
  818. static void save_opt_list(size_t *ndest, const char ***dest,
  819. QemuOpts *opts, const char *name)
  820. {
  821. struct opt_list opt = {
  822. name, ndest, dest,
  823. };
  824. qemu_opt_foreach(opts, save_opt_one, &opt, NULL);
  825. }
  826. void smbios_entry_add(QemuOpts *opts, Error **errp)
  827. {
  828. const char *val;
  829. assert(!smbios_immutable);
  830. val = qemu_opt_get(opts, "file");
  831. if (val) {
  832. struct smbios_structure_header *header;
  833. int size;
  834. struct smbios_table *table; /* legacy mode only */
  835. if (!qemu_opts_validate(opts, qemu_smbios_file_opts, errp)) {
  836. return;
  837. }
  838. size = get_image_size(val);
  839. if (size == -1 || size < sizeof(struct smbios_structure_header)) {
  840. error_setg(errp, "Cannot read SMBIOS file %s", val);
  841. return;
  842. }
  843. /*
  844. * NOTE: standard double '\0' terminator expected, per smbios spec.
  845. * (except in legacy mode, where the second '\0' is implicit and
  846. * will be inserted by the BIOS).
  847. */
  848. smbios_tables = g_realloc(smbios_tables, smbios_tables_len + size);
  849. header = (struct smbios_structure_header *)(smbios_tables +
  850. smbios_tables_len);
  851. if (load_image_size(val, (uint8_t *)header, size) != size) {
  852. error_setg(errp, "Failed to load SMBIOS file %s", val);
  853. return;
  854. }
  855. if (test_bit(header->type, have_fields_bitmap)) {
  856. error_setg(errp,
  857. "can't load type %d struct, fields already specified!",
  858. header->type);
  859. return;
  860. }
  861. set_bit(header->type, have_binfile_bitmap);
  862. if (header->type == 4) {
  863. smbios_type4_count++;
  864. }
  865. smbios_tables_len += size;
  866. if (size > smbios_table_max) {
  867. smbios_table_max = size;
  868. }
  869. smbios_table_cnt++;
  870. /* add a copy of the newly loaded blob to legacy smbios_entries */
  871. /* NOTE: This code runs before smbios_set_defaults(), so we don't
  872. * yet know which mode (legacy vs. aggregate-table) will be
  873. * required. We therefore add the binary blob to both legacy
  874. * (smbios_entries) and aggregate (smbios_tables) tables, and
  875. * delete the one we don't need from smbios_set_defaults(),
  876. * once we know which machine version has been requested.
  877. */
  878. if (!smbios_entries) {
  879. smbios_entries_len = sizeof(uint16_t);
  880. smbios_entries = g_malloc0(smbios_entries_len);
  881. }
  882. smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
  883. size + sizeof(*table));
  884. table = (struct smbios_table *)(smbios_entries + smbios_entries_len);
  885. table->header.type = SMBIOS_TABLE_ENTRY;
  886. table->header.length = cpu_to_le16(sizeof(*table) + size);
  887. memcpy(table->data, header, size);
  888. smbios_entries_len += sizeof(*table) + size;
  889. (*(uint16_t *)smbios_entries) =
  890. cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
  891. /* end: add a copy of the newly loaded blob to legacy smbios_entries */
  892. return;
  893. }
  894. val = qemu_opt_get(opts, "type");
  895. if (val) {
  896. unsigned long type = strtoul(val, NULL, 0);
  897. if (type > SMBIOS_MAX_TYPE) {
  898. error_setg(errp, "out of range!");
  899. return;
  900. }
  901. if (test_bit(type, have_binfile_bitmap)) {
  902. error_setg(errp, "can't add fields, binary file already loaded!");
  903. return;
  904. }
  905. set_bit(type, have_fields_bitmap);
  906. switch (type) {
  907. case 0:
  908. if (!qemu_opts_validate(opts, qemu_smbios_type0_opts, errp)) {
  909. return;
  910. }
  911. save_opt(&type0.vendor, opts, "vendor");
  912. save_opt(&type0.version, opts, "version");
  913. save_opt(&type0.date, opts, "date");
  914. type0.uefi = qemu_opt_get_bool(opts, "uefi", false);
  915. val = qemu_opt_get(opts, "release");
  916. if (val) {
  917. if (sscanf(val, "%hhu.%hhu", &type0.major, &type0.minor) != 2) {
  918. error_setg(errp, "Invalid release");
  919. return;
  920. }
  921. type0.have_major_minor = true;
  922. }
  923. return;
  924. case 1:
  925. if (!qemu_opts_validate(opts, qemu_smbios_type1_opts, errp)) {
  926. return;
  927. }
  928. save_opt(&type1.manufacturer, opts, "manufacturer");
  929. save_opt(&type1.product, opts, "product");
  930. save_opt(&type1.version, opts, "version");
  931. save_opt(&type1.serial, opts, "serial");
  932. save_opt(&type1.sku, opts, "sku");
  933. save_opt(&type1.family, opts, "family");
  934. val = qemu_opt_get(opts, "uuid");
  935. if (val) {
  936. if (qemu_uuid_parse(val, &qemu_uuid) != 0) {
  937. error_setg(errp, "Invalid UUID");
  938. return;
  939. }
  940. qemu_uuid_set = true;
  941. }
  942. return;
  943. case 2:
  944. if (!qemu_opts_validate(opts, qemu_smbios_type2_opts, errp)) {
  945. return;
  946. }
  947. save_opt(&type2.manufacturer, opts, "manufacturer");
  948. save_opt(&type2.product, opts, "product");
  949. save_opt(&type2.version, opts, "version");
  950. save_opt(&type2.serial, opts, "serial");
  951. save_opt(&type2.asset, opts, "asset");
  952. save_opt(&type2.location, opts, "location");
  953. return;
  954. case 3:
  955. if (!qemu_opts_validate(opts, qemu_smbios_type3_opts, errp)) {
  956. return;
  957. }
  958. save_opt(&type3.manufacturer, opts, "manufacturer");
  959. save_opt(&type3.version, opts, "version");
  960. save_opt(&type3.serial, opts, "serial");
  961. save_opt(&type3.asset, opts, "asset");
  962. save_opt(&type3.sku, opts, "sku");
  963. return;
  964. case 4:
  965. if (!qemu_opts_validate(opts, qemu_smbios_type4_opts, errp)) {
  966. return;
  967. }
  968. save_opt(&type4.sock_pfx, opts, "sock_pfx");
  969. save_opt(&type4.manufacturer, opts, "manufacturer");
  970. save_opt(&type4.version, opts, "version");
  971. save_opt(&type4.serial, opts, "serial");
  972. save_opt(&type4.asset, opts, "asset");
  973. save_opt(&type4.part, opts, "part");
  974. return;
  975. case 11:
  976. if (!qemu_opts_validate(opts, qemu_smbios_type11_opts, errp)) {
  977. return;
  978. }
  979. save_opt_list(&type11.nvalues, &type11.values, opts, "value");
  980. return;
  981. case 17:
  982. if (!qemu_opts_validate(opts, qemu_smbios_type17_opts, errp)) {
  983. return;
  984. }
  985. save_opt(&type17.loc_pfx, opts, "loc_pfx");
  986. save_opt(&type17.bank, opts, "bank");
  987. save_opt(&type17.manufacturer, opts, "manufacturer");
  988. save_opt(&type17.serial, opts, "serial");
  989. save_opt(&type17.asset, opts, "asset");
  990. save_opt(&type17.part, opts, "part");
  991. type17.speed = qemu_opt_get_number(opts, "speed", 0);
  992. return;
  993. default:
  994. error_setg(errp,
  995. "Don't know how to build fields for SMBIOS type %ld",
  996. type);
  997. return;
  998. }
  999. }
  1000. error_setg(errp, "Must specify type= or file=");
  1001. }