2
0

smbios.c 36 KB

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