2
0

smbios.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571
  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/module.h"
  22. #include "qemu/option.h"
  23. #include "system/system.h"
  24. #include "qemu/uuid.h"
  25. #include "hw/firmware/smbios.h"
  26. #include "hw/loader.h"
  27. #include "hw/boards.h"
  28. #include "hw/pci/pci_bus.h"
  29. #include "hw/pci/pci_device.h"
  30. #include "smbios_build.h"
  31. /*
  32. * SMBIOS tables provided by user with '-smbios file=<foo>' option
  33. */
  34. uint8_t *usr_blobs;
  35. size_t usr_blobs_len;
  36. static unsigned usr_table_max;
  37. static unsigned usr_table_cnt;
  38. uint8_t *smbios_tables;
  39. size_t smbios_tables_len;
  40. unsigned smbios_table_max;
  41. unsigned smbios_table_cnt;
  42. static SmbiosEntryPoint ep;
  43. static int smbios_type4_count = 0;
  44. static bool smbios_have_defaults;
  45. static uint32_t smbios_cpuid_version, smbios_cpuid_features;
  46. DECLARE_BITMAP(smbios_have_binfile_bitmap, SMBIOS_MAX_TYPE + 1);
  47. DECLARE_BITMAP(smbios_have_fields_bitmap, SMBIOS_MAX_TYPE + 1);
  48. smbios_type0_t smbios_type0;
  49. smbios_type1_t smbios_type1;
  50. static struct {
  51. const char *manufacturer, *product, *version, *serial, *asset, *location;
  52. } type2;
  53. static struct {
  54. const char *manufacturer, *version, *serial, *asset, *sku;
  55. } type3;
  56. /*
  57. * SVVP requires max_speed and current_speed to be set and not being
  58. * 0 which counts as unknown (SMBIOS 3.1.0/Table 21). Set the
  59. * default value to 2000MHz as we did before.
  60. */
  61. #define DEFAULT_CPU_SPEED 2000
  62. static struct {
  63. uint16_t processor_family;
  64. const char *sock_pfx, *manufacturer, *version, *serial, *asset, *part;
  65. uint64_t max_speed;
  66. uint64_t current_speed;
  67. uint64_t processor_id;
  68. } type4 = {
  69. .max_speed = DEFAULT_CPU_SPEED,
  70. .current_speed = DEFAULT_CPU_SPEED,
  71. .processor_id = 0,
  72. .processor_family = 0x01, /* Other */
  73. };
  74. struct type8_instance {
  75. const char *internal_reference, *external_reference;
  76. uint8_t connector_type, port_type;
  77. QTAILQ_ENTRY(type8_instance) next;
  78. };
  79. static QTAILQ_HEAD(, type8_instance) type8 = QTAILQ_HEAD_INITIALIZER(type8);
  80. /* type 9 instance for parsing */
  81. struct type9_instance {
  82. const char *slot_designation, *pcidev;
  83. uint8_t slot_type, slot_data_bus_width, current_usage, slot_length,
  84. slot_characteristics1, slot_characteristics2;
  85. uint16_t slot_id;
  86. QTAILQ_ENTRY(type9_instance) next;
  87. };
  88. static QTAILQ_HEAD(, type9_instance) type9 = QTAILQ_HEAD_INITIALIZER(type9);
  89. static struct {
  90. size_t nvalues;
  91. char **values;
  92. } type11;
  93. static struct {
  94. const char *loc_pfx, *bank, *manufacturer, *serial, *asset, *part;
  95. uint16_t speed;
  96. } type17;
  97. static QEnumLookup type41_kind_lookup = {
  98. .array = (const char *const[]) {
  99. "other",
  100. "unknown",
  101. "video",
  102. "scsi",
  103. "ethernet",
  104. "tokenring",
  105. "sound",
  106. "pata",
  107. "sata",
  108. "sas",
  109. },
  110. .size = 10
  111. };
  112. struct type41_instance {
  113. const char *designation, *pcidev;
  114. uint8_t instance, kind;
  115. QTAILQ_ENTRY(type41_instance) next;
  116. };
  117. static QTAILQ_HEAD(, type41_instance) type41 = QTAILQ_HEAD_INITIALIZER(type41);
  118. static QemuOptsList qemu_smbios_opts = {
  119. .name = "smbios",
  120. .head = QTAILQ_HEAD_INITIALIZER(qemu_smbios_opts.head),
  121. .desc = {
  122. /*
  123. * no elements => accept any params
  124. * validation will happen later
  125. */
  126. { /* end of list */ }
  127. }
  128. };
  129. static const QemuOptDesc qemu_smbios_file_opts[] = {
  130. {
  131. .name = "file",
  132. .type = QEMU_OPT_STRING,
  133. .help = "binary file containing an SMBIOS element",
  134. },
  135. { /* end of list */ }
  136. };
  137. static const QemuOptDesc qemu_smbios_type0_opts[] = {
  138. {
  139. .name = "type",
  140. .type = QEMU_OPT_NUMBER,
  141. .help = "SMBIOS element type",
  142. },{
  143. .name = "vendor",
  144. .type = QEMU_OPT_STRING,
  145. .help = "vendor name",
  146. },{
  147. .name = "version",
  148. .type = QEMU_OPT_STRING,
  149. .help = "version number",
  150. },{
  151. .name = "date",
  152. .type = QEMU_OPT_STRING,
  153. .help = "release date",
  154. },{
  155. .name = "release",
  156. .type = QEMU_OPT_STRING,
  157. .help = "revision number",
  158. },{
  159. .name = "uefi",
  160. .type = QEMU_OPT_BOOL,
  161. .help = "uefi support",
  162. },
  163. { /* end of list */ }
  164. };
  165. static const QemuOptDesc qemu_smbios_type1_opts[] = {
  166. {
  167. .name = "type",
  168. .type = QEMU_OPT_NUMBER,
  169. .help = "SMBIOS element type",
  170. },{
  171. .name = "manufacturer",
  172. .type = QEMU_OPT_STRING,
  173. .help = "manufacturer name",
  174. },{
  175. .name = "product",
  176. .type = QEMU_OPT_STRING,
  177. .help = "product name",
  178. },{
  179. .name = "version",
  180. .type = QEMU_OPT_STRING,
  181. .help = "version number",
  182. },{
  183. .name = "serial",
  184. .type = QEMU_OPT_STRING,
  185. .help = "serial number",
  186. },{
  187. .name = "uuid",
  188. .type = QEMU_OPT_STRING,
  189. .help = "UUID",
  190. },{
  191. .name = "sku",
  192. .type = QEMU_OPT_STRING,
  193. .help = "SKU number",
  194. },{
  195. .name = "family",
  196. .type = QEMU_OPT_STRING,
  197. .help = "family name",
  198. },
  199. { /* end of list */ }
  200. };
  201. static const QemuOptDesc qemu_smbios_type2_opts[] = {
  202. {
  203. .name = "type",
  204. .type = QEMU_OPT_NUMBER,
  205. .help = "SMBIOS element type",
  206. },{
  207. .name = "manufacturer",
  208. .type = QEMU_OPT_STRING,
  209. .help = "manufacturer name",
  210. },{
  211. .name = "product",
  212. .type = QEMU_OPT_STRING,
  213. .help = "product 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 = "location",
  228. .type = QEMU_OPT_STRING,
  229. .help = "location in chassis",
  230. },
  231. { /* end of list */ }
  232. };
  233. static const QemuOptDesc qemu_smbios_type3_opts[] = {
  234. {
  235. .name = "type",
  236. .type = QEMU_OPT_NUMBER,
  237. .help = "SMBIOS element type",
  238. },{
  239. .name = "manufacturer",
  240. .type = QEMU_OPT_STRING,
  241. .help = "manufacturer name",
  242. },{
  243. .name = "version",
  244. .type = QEMU_OPT_STRING,
  245. .help = "version number",
  246. },{
  247. .name = "serial",
  248. .type = QEMU_OPT_STRING,
  249. .help = "serial number",
  250. },{
  251. .name = "asset",
  252. .type = QEMU_OPT_STRING,
  253. .help = "asset tag number",
  254. },{
  255. .name = "sku",
  256. .type = QEMU_OPT_STRING,
  257. .help = "SKU number",
  258. },
  259. { /* end of list */ }
  260. };
  261. static const QemuOptDesc qemu_smbios_type4_opts[] = {
  262. {
  263. .name = "type",
  264. .type = QEMU_OPT_NUMBER,
  265. .help = "SMBIOS element type",
  266. },{
  267. .name = "sock_pfx",
  268. .type = QEMU_OPT_STRING,
  269. .help = "socket designation string prefix",
  270. },{
  271. .name = "manufacturer",
  272. .type = QEMU_OPT_STRING,
  273. .help = "manufacturer name",
  274. },{
  275. .name = "version",
  276. .type = QEMU_OPT_STRING,
  277. .help = "version number",
  278. },{
  279. .name = "max-speed",
  280. .type = QEMU_OPT_NUMBER,
  281. .help = "max speed in MHz",
  282. },{
  283. .name = "current-speed",
  284. .type = QEMU_OPT_NUMBER,
  285. .help = "speed at system boot in MHz",
  286. },{
  287. .name = "serial",
  288. .type = QEMU_OPT_STRING,
  289. .help = "serial number",
  290. },{
  291. .name = "asset",
  292. .type = QEMU_OPT_STRING,
  293. .help = "asset tag number",
  294. },{
  295. .name = "part",
  296. .type = QEMU_OPT_STRING,
  297. .help = "part number",
  298. }, {
  299. .name = "processor-family",
  300. .type = QEMU_OPT_NUMBER,
  301. .help = "processor family",
  302. }, {
  303. .name = "processor-id",
  304. .type = QEMU_OPT_NUMBER,
  305. .help = "processor id",
  306. },
  307. { /* end of list */ }
  308. };
  309. static const QemuOptDesc qemu_smbios_type8_opts[] = {
  310. {
  311. .name = "type",
  312. .type = QEMU_OPT_NUMBER,
  313. .help = "SMBIOS element type",
  314. },
  315. {
  316. .name = "internal_reference",
  317. .type = QEMU_OPT_STRING,
  318. .help = "internal reference designator",
  319. },
  320. {
  321. .name = "external_reference",
  322. .type = QEMU_OPT_STRING,
  323. .help = "external reference designator",
  324. },
  325. {
  326. .name = "connector_type",
  327. .type = QEMU_OPT_NUMBER,
  328. .help = "connector type",
  329. },
  330. {
  331. .name = "port_type",
  332. .type = QEMU_OPT_NUMBER,
  333. .help = "port type",
  334. },
  335. { /* end of list */ }
  336. };
  337. static const QemuOptDesc qemu_smbios_type9_opts[] = {
  338. {
  339. .name = "type",
  340. .type = QEMU_OPT_NUMBER,
  341. .help = "SMBIOS element type",
  342. },
  343. {
  344. .name = "slot_designation",
  345. .type = QEMU_OPT_STRING,
  346. .help = "string number for reference designation",
  347. },
  348. {
  349. .name = "slot_type",
  350. .type = QEMU_OPT_NUMBER,
  351. .help = "connector type",
  352. },
  353. {
  354. .name = "slot_data_bus_width",
  355. .type = QEMU_OPT_NUMBER,
  356. .help = "port type",
  357. },
  358. {
  359. .name = "current_usage",
  360. .type = QEMU_OPT_NUMBER,
  361. .help = "current usage",
  362. },
  363. {
  364. .name = "slot_length",
  365. .type = QEMU_OPT_NUMBER,
  366. .help = "system slot length",
  367. },
  368. {
  369. .name = "slot_id",
  370. .type = QEMU_OPT_NUMBER,
  371. .help = "system slot id",
  372. },
  373. {
  374. .name = "slot_characteristics1",
  375. .type = QEMU_OPT_NUMBER,
  376. .help = "slot characteristics1, see the spec",
  377. },
  378. {
  379. .name = "slot_characteristics2",
  380. .type = QEMU_OPT_NUMBER,
  381. .help = "slot characteristics2, see the spec",
  382. },
  383. {
  384. .name = "pci_device",
  385. .type = QEMU_OPT_STRING,
  386. .help = "PCI device, if provided."
  387. }
  388. };
  389. static const QemuOptDesc qemu_smbios_type11_opts[] = {
  390. {
  391. .name = "type",
  392. .type = QEMU_OPT_NUMBER,
  393. .help = "SMBIOS element type",
  394. },
  395. {
  396. .name = "value",
  397. .type = QEMU_OPT_STRING,
  398. .help = "OEM string data",
  399. },
  400. {
  401. .name = "path",
  402. .type = QEMU_OPT_STRING,
  403. .help = "OEM string data from file",
  404. },
  405. { /* end of list */ }
  406. };
  407. static const QemuOptDesc qemu_smbios_type17_opts[] = {
  408. {
  409. .name = "type",
  410. .type = QEMU_OPT_NUMBER,
  411. .help = "SMBIOS element type",
  412. },{
  413. .name = "loc_pfx",
  414. .type = QEMU_OPT_STRING,
  415. .help = "device locator string prefix",
  416. },{
  417. .name = "bank",
  418. .type = QEMU_OPT_STRING,
  419. .help = "bank locator string",
  420. },{
  421. .name = "manufacturer",
  422. .type = QEMU_OPT_STRING,
  423. .help = "manufacturer name",
  424. },{
  425. .name = "serial",
  426. .type = QEMU_OPT_STRING,
  427. .help = "serial number",
  428. },{
  429. .name = "asset",
  430. .type = QEMU_OPT_STRING,
  431. .help = "asset tag number",
  432. },{
  433. .name = "part",
  434. .type = QEMU_OPT_STRING,
  435. .help = "part number",
  436. },{
  437. .name = "speed",
  438. .type = QEMU_OPT_NUMBER,
  439. .help = "maximum capable speed",
  440. },
  441. { /* end of list */ }
  442. };
  443. static const QemuOptDesc qemu_smbios_type41_opts[] = {
  444. {
  445. .name = "type",
  446. .type = QEMU_OPT_NUMBER,
  447. .help = "SMBIOS element type",
  448. },{
  449. .name = "designation",
  450. .type = QEMU_OPT_STRING,
  451. .help = "reference designation string",
  452. },{
  453. .name = "kind",
  454. .type = QEMU_OPT_STRING,
  455. .help = "device type",
  456. .def_value_str = "other",
  457. },{
  458. .name = "instance",
  459. .type = QEMU_OPT_NUMBER,
  460. .help = "device type instance",
  461. },{
  462. .name = "pcidev",
  463. .type = QEMU_OPT_STRING,
  464. .help = "PCI device",
  465. },
  466. { /* end of list */ }
  467. };
  468. static void smbios_register_config(void)
  469. {
  470. qemu_add_opts(&qemu_smbios_opts);
  471. }
  472. opts_init(smbios_register_config);
  473. /*
  474. * The SMBIOS 2.1 "structure table length" field in the
  475. * entry point uses a 16-bit integer, so we're limited
  476. * in total table size
  477. */
  478. #define SMBIOS_21_MAX_TABLES_LEN 0xffff
  479. static bool smbios_check_type4_count(uint32_t expected_t4_count, Error **errp)
  480. {
  481. if (smbios_type4_count && smbios_type4_count != expected_t4_count) {
  482. error_setg(errp, "Expected %d SMBIOS Type 4 tables, got %d instead",
  483. expected_t4_count, smbios_type4_count);
  484. return false;
  485. }
  486. return true;
  487. }
  488. bool smbios_validate_table(SmbiosEntryPointType ep_type, Error **errp)
  489. {
  490. if (ep_type == SMBIOS_ENTRY_POINT_TYPE_32 &&
  491. smbios_tables_len > SMBIOS_21_MAX_TABLES_LEN) {
  492. error_setg(errp, "SMBIOS 2.1 table length %zu exceeds %d",
  493. smbios_tables_len, SMBIOS_21_MAX_TABLES_LEN);
  494. return false;
  495. }
  496. return true;
  497. }
  498. bool smbios_skip_table(uint8_t type, bool required_table)
  499. {
  500. if (test_bit(type, smbios_have_binfile_bitmap)) {
  501. return true; /* user provided their own binary blob(s) */
  502. }
  503. if (test_bit(type, smbios_have_fields_bitmap)) {
  504. return false; /* user provided fields via command line */
  505. }
  506. if (smbios_have_defaults && required_table) {
  507. return false; /* we're building tables, and this one's required */
  508. }
  509. return true;
  510. }
  511. #define T0_BASE 0x000
  512. #define T1_BASE 0x100
  513. #define T2_BASE 0x200
  514. #define T3_BASE 0x300
  515. #define T4_BASE 0x400
  516. #define T9_BASE 0x900
  517. #define T11_BASE 0xe00
  518. #define T16_BASE 0x1000
  519. #define T17_BASE 0x1100
  520. #define T19_BASE 0x1300
  521. #define T32_BASE 0x2000
  522. #define T41_BASE 0x2900
  523. #define T127_BASE 0x7F00
  524. static void smbios_build_type_0_table(void)
  525. {
  526. SMBIOS_BUILD_TABLE_PRE(0, T0_BASE, false); /* optional, leave up to BIOS */
  527. SMBIOS_TABLE_SET_STR(0, vendor_str, smbios_type0.vendor);
  528. SMBIOS_TABLE_SET_STR(0, bios_version_str, smbios_type0.version);
  529. t->bios_starting_address_segment = cpu_to_le16(0xE800); /* from SeaBIOS */
  530. SMBIOS_TABLE_SET_STR(0, bios_release_date_str, smbios_type0.date);
  531. t->bios_rom_size = 0; /* hardcoded in SeaBIOS with FIXME comment */
  532. t->bios_characteristics = cpu_to_le64(0x08); /* Not supported */
  533. t->bios_characteristics_extension_bytes[0] = 0;
  534. t->bios_characteristics_extension_bytes[1] = 0x14; /* TCD/SVVP | VM */
  535. if (smbios_type0.uefi) {
  536. t->bios_characteristics_extension_bytes[1] |= 0x08; /* |= UEFI */
  537. }
  538. if (smbios_type0.have_major_minor) {
  539. t->system_bios_major_release = smbios_type0.major;
  540. t->system_bios_minor_release = smbios_type0.minor;
  541. } else {
  542. t->system_bios_major_release = 0;
  543. t->system_bios_minor_release = 0;
  544. }
  545. /* hardcoded in SeaBIOS */
  546. t->embedded_controller_major_release = 0xFF;
  547. t->embedded_controller_minor_release = 0xFF;
  548. SMBIOS_BUILD_TABLE_POST;
  549. }
  550. /* Encode UUID from the big endian encoding described on RFC4122 to the wire
  551. * format specified by SMBIOS version 2.6.
  552. */
  553. static void smbios_encode_uuid(struct smbios_uuid *uuid, QemuUUID *in)
  554. {
  555. memcpy(uuid, in, 16);
  556. uuid->time_low = bswap32(uuid->time_low);
  557. uuid->time_mid = bswap16(uuid->time_mid);
  558. uuid->time_hi_and_version = bswap16(uuid->time_hi_and_version);
  559. }
  560. static void smbios_build_type_1_table(void)
  561. {
  562. SMBIOS_BUILD_TABLE_PRE(1, T1_BASE, true); /* required */
  563. SMBIOS_TABLE_SET_STR(1, manufacturer_str, smbios_type1.manufacturer);
  564. SMBIOS_TABLE_SET_STR(1, product_name_str, smbios_type1.product);
  565. SMBIOS_TABLE_SET_STR(1, version_str, smbios_type1.version);
  566. SMBIOS_TABLE_SET_STR(1, serial_number_str, smbios_type1.serial);
  567. if (qemu_uuid_set) {
  568. smbios_encode_uuid(&t->uuid, &qemu_uuid);
  569. } else {
  570. memset(&t->uuid, 0, 16);
  571. }
  572. t->wake_up_type = 0x06; /* power switch */
  573. SMBIOS_TABLE_SET_STR(1, sku_number_str, smbios_type1.sku);
  574. SMBIOS_TABLE_SET_STR(1, family_str, smbios_type1.family);
  575. SMBIOS_BUILD_TABLE_POST;
  576. }
  577. static void smbios_build_type_2_table(void)
  578. {
  579. SMBIOS_BUILD_TABLE_PRE(2, T2_BASE, false); /* optional */
  580. SMBIOS_TABLE_SET_STR(2, manufacturer_str, type2.manufacturer);
  581. SMBIOS_TABLE_SET_STR(2, product_str, type2.product);
  582. SMBIOS_TABLE_SET_STR(2, version_str, type2.version);
  583. SMBIOS_TABLE_SET_STR(2, serial_number_str, type2.serial);
  584. SMBIOS_TABLE_SET_STR(2, asset_tag_number_str, type2.asset);
  585. t->feature_flags = 0x01; /* Motherboard */
  586. SMBIOS_TABLE_SET_STR(2, location_str, type2.location);
  587. t->chassis_handle = cpu_to_le16(0x300); /* Type 3 (System enclosure) */
  588. t->board_type = 0x0A; /* Motherboard */
  589. t->contained_element_count = 0;
  590. SMBIOS_BUILD_TABLE_POST;
  591. }
  592. static void smbios_build_type_3_table(void)
  593. {
  594. SMBIOS_BUILD_TABLE_PRE(3, T3_BASE, true); /* required */
  595. SMBIOS_TABLE_SET_STR(3, manufacturer_str, type3.manufacturer);
  596. t->type = 0x01; /* Other */
  597. SMBIOS_TABLE_SET_STR(3, version_str, type3.version);
  598. SMBIOS_TABLE_SET_STR(3, serial_number_str, type3.serial);
  599. SMBIOS_TABLE_SET_STR(3, asset_tag_number_str, type3.asset);
  600. t->boot_up_state = 0x03; /* Safe */
  601. t->power_supply_state = 0x03; /* Safe */
  602. t->thermal_state = 0x03; /* Safe */
  603. t->security_status = 0x02; /* Unknown */
  604. t->oem_defined = cpu_to_le32(0);
  605. t->height = 0;
  606. t->number_of_power_cords = 0;
  607. t->contained_element_count = 0;
  608. t->contained_element_record_length = 0;
  609. SMBIOS_TABLE_SET_STR(3, sku_number_str, type3.sku);
  610. SMBIOS_BUILD_TABLE_POST;
  611. }
  612. static void smbios_build_type_4_table(MachineState *ms, unsigned instance,
  613. SmbiosEntryPointType ep_type,
  614. Error **errp)
  615. {
  616. char sock_str[128];
  617. size_t tbl_len = SMBIOS_TYPE_4_LEN_V28;
  618. unsigned threads_per_socket;
  619. unsigned cores_per_socket;
  620. if (ep_type == SMBIOS_ENTRY_POINT_TYPE_64) {
  621. tbl_len = SMBIOS_TYPE_4_LEN_V30;
  622. }
  623. SMBIOS_BUILD_TABLE_PRE_SIZE(4, T4_BASE + instance,
  624. true, tbl_len); /* required */
  625. snprintf(sock_str, sizeof(sock_str), "%s%2x", type4.sock_pfx, instance);
  626. SMBIOS_TABLE_SET_STR(4, socket_designation_str, sock_str);
  627. t->processor_type = 0x03; /* CPU */
  628. t->processor_family = 0xfe; /* use Processor Family 2 field */
  629. SMBIOS_TABLE_SET_STR(4, processor_manufacturer_str, type4.manufacturer);
  630. if (type4.processor_id == 0) {
  631. t->processor_id[0] = cpu_to_le32(smbios_cpuid_version);
  632. t->processor_id[1] = cpu_to_le32(smbios_cpuid_features);
  633. } else {
  634. t->processor_id[0] = cpu_to_le32((uint32_t)type4.processor_id);
  635. t->processor_id[1] = cpu_to_le32(type4.processor_id >> 32);
  636. }
  637. SMBIOS_TABLE_SET_STR(4, processor_version_str, type4.version);
  638. t->voltage = 0;
  639. t->external_clock = cpu_to_le16(0); /* Unknown */
  640. t->max_speed = cpu_to_le16(type4.max_speed);
  641. t->current_speed = cpu_to_le16(type4.current_speed);
  642. t->status = 0x41; /* Socket populated, CPU enabled */
  643. t->processor_upgrade = 0x01; /* Other */
  644. t->l1_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
  645. t->l2_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
  646. t->l3_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
  647. SMBIOS_TABLE_SET_STR(4, serial_number_str, type4.serial);
  648. SMBIOS_TABLE_SET_STR(4, asset_tag_number_str, type4.asset);
  649. SMBIOS_TABLE_SET_STR(4, part_number_str, type4.part);
  650. threads_per_socket = machine_topo_get_threads_per_socket(ms);
  651. cores_per_socket = machine_topo_get_cores_per_socket(ms);
  652. t->core_count = (cores_per_socket > 255) ? 0xFF : cores_per_socket;
  653. t->core_enabled = t->core_count;
  654. t->thread_count = (threads_per_socket > 255) ? 0xFF : threads_per_socket;
  655. t->processor_characteristics = cpu_to_le16(0x02); /* Unknown */
  656. t->processor_family2 = cpu_to_le16(type4.processor_family);
  657. if (tbl_len == SMBIOS_TYPE_4_LEN_V30) {
  658. t->core_count2 = t->core_enabled2 = cpu_to_le16(cores_per_socket);
  659. t->thread_count2 = cpu_to_le16(threads_per_socket);
  660. } else if (t->core_count == 0xFF || t->thread_count == 0xFF) {
  661. error_setg(errp, "SMBIOS 2.0 doesn't support number of processor "
  662. "cores/threads more than 255, use "
  663. "-machine smbios-entry-point-type=64 option to enable "
  664. "SMBIOS 3.0 support");
  665. return;
  666. }
  667. SMBIOS_BUILD_TABLE_POST;
  668. smbios_type4_count++;
  669. }
  670. static void smbios_build_type_8_table(void)
  671. {
  672. unsigned instance = 0;
  673. struct type8_instance *t8;
  674. QTAILQ_FOREACH(t8, &type8, next) {
  675. SMBIOS_BUILD_TABLE_PRE(8, T0_BASE + instance, true);
  676. SMBIOS_TABLE_SET_STR(8, internal_reference_str, t8->internal_reference);
  677. SMBIOS_TABLE_SET_STR(8, external_reference_str, t8->external_reference);
  678. /* most vendors seem to set this to None */
  679. t->internal_connector_type = 0x0;
  680. t->external_connector_type = t8->connector_type;
  681. t->port_type = t8->port_type;
  682. SMBIOS_BUILD_TABLE_POST;
  683. instance++;
  684. }
  685. }
  686. static void smbios_build_type_9_table(Error **errp)
  687. {
  688. unsigned instance = 0;
  689. struct type9_instance *t9;
  690. QTAILQ_FOREACH(t9, &type9, next) {
  691. SMBIOS_BUILD_TABLE_PRE(9, T9_BASE + instance, true);
  692. SMBIOS_TABLE_SET_STR(9, slot_designation, t9->slot_designation);
  693. t->slot_type = t9->slot_type;
  694. t->slot_data_bus_width = t9->slot_data_bus_width;
  695. t->current_usage = t9->current_usage;
  696. t->slot_length = t9->slot_length;
  697. t->slot_id = t9->slot_id;
  698. t->slot_characteristics1 = t9->slot_characteristics1;
  699. t->slot_characteristics2 = t9->slot_characteristics2;
  700. if (t9->pcidev) {
  701. PCIDevice *pdev = NULL;
  702. int rc = pci_qdev_find_device(t9->pcidev, &pdev);
  703. if (rc != 0) {
  704. error_setg(errp,
  705. "No PCI device %s for SMBIOS type 9 entry %s",
  706. t9->pcidev, t9->slot_designation);
  707. return;
  708. }
  709. /*
  710. * We only handle the case were the device is attached to
  711. * the PCI root bus. The general case is more complex as
  712. * bridges are enumerated later and the table would need
  713. * to be updated at this moment.
  714. */
  715. if (!pci_bus_is_root(pci_get_bus(pdev))) {
  716. error_setg(errp,
  717. "Cannot create type 9 entry for PCI device %s: "
  718. "not attached to the root bus",
  719. t9->pcidev);
  720. return;
  721. }
  722. t->segment_group_number = cpu_to_le16(0);
  723. t->bus_number = pci_dev_bus_num(pdev);
  724. t->device_number = pdev->devfn;
  725. } else {
  726. /*
  727. * Per SMBIOS spec, For slots that are not of the PCI, AGP, PCI-X,
  728. * or PCI-Express type that do not have bus/device/function
  729. * information, 0FFh should be populated in the fields of Segment
  730. * Group Number, Bus Number, Device/Function Number.
  731. */
  732. t->segment_group_number = 0xff;
  733. t->bus_number = 0xff;
  734. t->device_number = 0xff;
  735. }
  736. SMBIOS_BUILD_TABLE_POST;
  737. instance++;
  738. }
  739. }
  740. static void smbios_build_type_11_table(void)
  741. {
  742. char count_str[128];
  743. size_t i;
  744. if (type11.nvalues == 0) {
  745. return;
  746. }
  747. SMBIOS_BUILD_TABLE_PRE(11, T11_BASE, true); /* required */
  748. snprintf(count_str, sizeof(count_str), "%zu", type11.nvalues);
  749. t->count = type11.nvalues;
  750. for (i = 0; i < type11.nvalues; i++) {
  751. SMBIOS_TABLE_SET_STR_LIST(11, type11.values[i]);
  752. g_free(type11.values[i]);
  753. type11.values[i] = NULL;
  754. }
  755. SMBIOS_BUILD_TABLE_POST;
  756. }
  757. #define MAX_T16_STD_SZ 0x80000000 /* 2T in Kilobytes */
  758. static void smbios_build_type_16_table(unsigned dimm_cnt)
  759. {
  760. uint64_t size_kb;
  761. SMBIOS_BUILD_TABLE_PRE(16, T16_BASE, true); /* required */
  762. t->location = 0x01; /* Other */
  763. t->use = 0x03; /* System memory */
  764. t->error_correction = 0x06; /* Multi-bit ECC (for Microsoft, per SeaBIOS) */
  765. size_kb = QEMU_ALIGN_UP(current_machine->ram_size, KiB) / KiB;
  766. if (size_kb < MAX_T16_STD_SZ) {
  767. t->maximum_capacity = cpu_to_le32(size_kb);
  768. t->extended_maximum_capacity = cpu_to_le64(0);
  769. } else {
  770. t->maximum_capacity = cpu_to_le32(MAX_T16_STD_SZ);
  771. t->extended_maximum_capacity = cpu_to_le64(current_machine->ram_size);
  772. }
  773. t->memory_error_information_handle = cpu_to_le16(0xFFFE); /* Not provided */
  774. t->number_of_memory_devices = cpu_to_le16(dimm_cnt);
  775. SMBIOS_BUILD_TABLE_POST;
  776. }
  777. #define MAX_T17_STD_SZ 0x7FFF /* (32G - 1M), in Megabytes */
  778. #define MAX_T17_EXT_SZ 0x80000000 /* 2P, in Megabytes */
  779. static void smbios_build_type_17_table(unsigned instance, uint64_t size)
  780. {
  781. char loc_str[128];
  782. uint64_t size_mb;
  783. SMBIOS_BUILD_TABLE_PRE(17, T17_BASE + instance, true); /* required */
  784. t->physical_memory_array_handle = cpu_to_le16(0x1000); /* Type 16 above */
  785. t->memory_error_information_handle = cpu_to_le16(0xFFFE); /* Not provided */
  786. t->total_width = cpu_to_le16(0xFFFF); /* Unknown */
  787. t->data_width = cpu_to_le16(0xFFFF); /* Unknown */
  788. size_mb = QEMU_ALIGN_UP(size, MiB) / MiB;
  789. if (size_mb < MAX_T17_STD_SZ) {
  790. t->size = cpu_to_le16(size_mb);
  791. t->extended_size = cpu_to_le32(0);
  792. } else {
  793. assert(size_mb < MAX_T17_EXT_SZ);
  794. t->size = cpu_to_le16(MAX_T17_STD_SZ);
  795. t->extended_size = cpu_to_le32(size_mb);
  796. }
  797. t->form_factor = 0x09; /* DIMM */
  798. t->device_set = 0; /* Not in a set */
  799. snprintf(loc_str, sizeof(loc_str), "%s %d", type17.loc_pfx, instance);
  800. SMBIOS_TABLE_SET_STR(17, device_locator_str, loc_str);
  801. SMBIOS_TABLE_SET_STR(17, bank_locator_str, type17.bank);
  802. t->memory_type = 0x07; /* RAM */
  803. t->type_detail = cpu_to_le16(0x02); /* Other */
  804. t->speed = cpu_to_le16(type17.speed);
  805. SMBIOS_TABLE_SET_STR(17, manufacturer_str, type17.manufacturer);
  806. SMBIOS_TABLE_SET_STR(17, serial_number_str, type17.serial);
  807. SMBIOS_TABLE_SET_STR(17, asset_tag_number_str, type17.asset);
  808. SMBIOS_TABLE_SET_STR(17, part_number_str, type17.part);
  809. t->attributes = 0; /* Unknown */
  810. t->configured_clock_speed = t->speed; /* reuse value for max speed */
  811. t->minimum_voltage = cpu_to_le16(0); /* Unknown */
  812. t->maximum_voltage = cpu_to_le16(0); /* Unknown */
  813. t->configured_voltage = cpu_to_le16(0); /* Unknown */
  814. SMBIOS_BUILD_TABLE_POST;
  815. }
  816. static void smbios_build_type_19_table(unsigned instance, unsigned offset,
  817. uint64_t start, uint64_t size)
  818. {
  819. uint64_t end, start_kb, end_kb;
  820. SMBIOS_BUILD_TABLE_PRE(19, T19_BASE + offset + instance,
  821. true); /* required */
  822. end = start + size - 1;
  823. assert(end > start);
  824. start_kb = start / KiB;
  825. end_kb = end / KiB;
  826. if (start_kb < UINT32_MAX && end_kb < UINT32_MAX) {
  827. t->starting_address = cpu_to_le32(start_kb);
  828. t->ending_address = cpu_to_le32(end_kb);
  829. t->extended_starting_address =
  830. t->extended_ending_address = cpu_to_le64(0);
  831. } else {
  832. t->starting_address = t->ending_address = cpu_to_le32(UINT32_MAX);
  833. t->extended_starting_address = cpu_to_le64(start);
  834. t->extended_ending_address = cpu_to_le64(end);
  835. }
  836. t->memory_array_handle = cpu_to_le16(0x1000); /* Type 16 above */
  837. t->partition_width = 1; /* One device per row */
  838. SMBIOS_BUILD_TABLE_POST;
  839. }
  840. static void smbios_build_type_32_table(void)
  841. {
  842. SMBIOS_BUILD_TABLE_PRE(32, T32_BASE, true); /* required */
  843. memset(t->reserved, 0, 6);
  844. t->boot_status = 0; /* No errors detected */
  845. SMBIOS_BUILD_TABLE_POST;
  846. }
  847. static void smbios_build_type_41_table(Error **errp)
  848. {
  849. unsigned instance = 0;
  850. struct type41_instance *t41;
  851. QTAILQ_FOREACH(t41, &type41, next) {
  852. SMBIOS_BUILD_TABLE_PRE(41, T41_BASE + instance, true);
  853. SMBIOS_TABLE_SET_STR(41, reference_designation_str, t41->designation);
  854. t->device_type = t41->kind;
  855. t->device_type_instance = t41->instance;
  856. t->segment_group_number = cpu_to_le16(0);
  857. t->bus_number = 0;
  858. t->device_number = 0;
  859. if (t41->pcidev) {
  860. PCIDevice *pdev = NULL;
  861. int rc = pci_qdev_find_device(t41->pcidev, &pdev);
  862. if (rc != 0) {
  863. error_setg(errp,
  864. "No PCI device %s for SMBIOS type 41 entry %s",
  865. t41->pcidev, t41->designation);
  866. return;
  867. }
  868. /*
  869. * We only handle the case were the device is attached to
  870. * the PCI root bus. The general case is more complex as
  871. * bridges are enumerated later and the table would need
  872. * to be updated at this moment.
  873. */
  874. if (!pci_bus_is_root(pci_get_bus(pdev))) {
  875. error_setg(errp,
  876. "Cannot create type 41 entry for PCI device %s: "
  877. "not attached to the root bus",
  878. t41->pcidev);
  879. return;
  880. }
  881. t->segment_group_number = cpu_to_le16(0);
  882. t->bus_number = pci_dev_bus_num(pdev);
  883. t->device_number = pdev->devfn;
  884. }
  885. SMBIOS_BUILD_TABLE_POST;
  886. instance++;
  887. }
  888. }
  889. static void smbios_build_type_127_table(void)
  890. {
  891. SMBIOS_BUILD_TABLE_PRE(127, T127_BASE, true); /* required */
  892. SMBIOS_BUILD_TABLE_POST;
  893. }
  894. void smbios_set_cpuid(uint32_t version, uint32_t features)
  895. {
  896. smbios_cpuid_version = version;
  897. smbios_cpuid_features = features;
  898. }
  899. #define SMBIOS_SET_DEFAULT(field, value) \
  900. if (!field) { \
  901. field = value; \
  902. }
  903. void smbios_set_default_processor_family(uint16_t processor_family)
  904. {
  905. if (type4.processor_family <= 0x01) {
  906. type4.processor_family = processor_family;
  907. }
  908. }
  909. void smbios_set_defaults(const char *manufacturer, const char *product,
  910. const char *version)
  911. {
  912. smbios_have_defaults = true;
  913. SMBIOS_SET_DEFAULT(smbios_type1.manufacturer, manufacturer);
  914. SMBIOS_SET_DEFAULT(smbios_type1.product, product);
  915. SMBIOS_SET_DEFAULT(smbios_type1.version, version);
  916. SMBIOS_SET_DEFAULT(type2.manufacturer, manufacturer);
  917. SMBIOS_SET_DEFAULT(type2.product, product);
  918. SMBIOS_SET_DEFAULT(type2.version, version);
  919. SMBIOS_SET_DEFAULT(type3.manufacturer, manufacturer);
  920. SMBIOS_SET_DEFAULT(type3.version, version);
  921. SMBIOS_SET_DEFAULT(type4.sock_pfx, "CPU");
  922. SMBIOS_SET_DEFAULT(type4.manufacturer, manufacturer);
  923. SMBIOS_SET_DEFAULT(type4.version, version);
  924. SMBIOS_SET_DEFAULT(type17.loc_pfx, "DIMM");
  925. SMBIOS_SET_DEFAULT(type17.manufacturer, manufacturer);
  926. }
  927. static void smbios_entry_point_setup(SmbiosEntryPointType ep_type)
  928. {
  929. switch (ep_type) {
  930. case SMBIOS_ENTRY_POINT_TYPE_32:
  931. memcpy(ep.ep21.anchor_string, "_SM_", 4);
  932. memcpy(ep.ep21.intermediate_anchor_string, "_DMI_", 5);
  933. ep.ep21.length = sizeof(struct smbios_21_entry_point);
  934. ep.ep21.entry_point_revision = 0; /* formatted_area reserved */
  935. memset(ep.ep21.formatted_area, 0, 5);
  936. /* compliant with smbios spec v2.8 */
  937. ep.ep21.smbios_major_version = 2;
  938. ep.ep21.smbios_minor_version = 8;
  939. ep.ep21.smbios_bcd_revision = 0x28;
  940. /* set during table construction, but BIOS may override: */
  941. ep.ep21.structure_table_length = cpu_to_le16(smbios_tables_len);
  942. ep.ep21.max_structure_size = cpu_to_le16(smbios_table_max);
  943. ep.ep21.number_of_structures = cpu_to_le16(smbios_table_cnt);
  944. /* BIOS must recalculate */
  945. ep.ep21.checksum = 0;
  946. ep.ep21.intermediate_checksum = 0;
  947. ep.ep21.structure_table_address = cpu_to_le32(0);
  948. break;
  949. case SMBIOS_ENTRY_POINT_TYPE_64:
  950. memcpy(ep.ep30.anchor_string, "_SM3_", 5);
  951. ep.ep30.length = sizeof(struct smbios_30_entry_point);
  952. ep.ep30.entry_point_revision = 1;
  953. ep.ep30.reserved = 0;
  954. /* compliant with smbios spec 3.0 */
  955. ep.ep30.smbios_major_version = 3;
  956. ep.ep30.smbios_minor_version = 0;
  957. ep.ep30.smbios_doc_rev = 0;
  958. /* set during table construct, but BIOS might override */
  959. ep.ep30.structure_table_max_size = cpu_to_le32(smbios_tables_len);
  960. /* BIOS must recalculate */
  961. ep.ep30.checksum = 0;
  962. ep.ep30.structure_table_address = cpu_to_le64(0);
  963. break;
  964. default:
  965. abort();
  966. break;
  967. }
  968. }
  969. static bool smbios_get_tables_ep(MachineState *ms,
  970. SmbiosEntryPointType ep_type,
  971. const struct smbios_phys_mem_area *mem_array,
  972. const unsigned int mem_array_size,
  973. uint8_t **tables, size_t *tables_len,
  974. uint8_t **anchor, size_t *anchor_len,
  975. Error **errp)
  976. {
  977. unsigned i, dimm_cnt, offset;
  978. MachineClass *mc = MACHINE_GET_CLASS(ms);
  979. ERRP_GUARD();
  980. assert(ep_type == SMBIOS_ENTRY_POINT_TYPE_32 ||
  981. ep_type == SMBIOS_ENTRY_POINT_TYPE_64);
  982. g_free(smbios_tables);
  983. smbios_type4_count = 0;
  984. smbios_tables = g_memdup2(usr_blobs, usr_blobs_len);
  985. smbios_tables_len = usr_blobs_len;
  986. smbios_table_max = usr_table_max;
  987. smbios_table_cnt = usr_table_cnt;
  988. smbios_build_type_0_table();
  989. smbios_build_type_1_table();
  990. smbios_build_type_2_table();
  991. smbios_build_type_3_table();
  992. assert(ms->smp.sockets >= 1);
  993. for (i = 0; i < ms->smp.sockets; i++) {
  994. smbios_build_type_4_table(ms, i, ep_type, errp);
  995. if (*errp) {
  996. goto err_exit;
  997. }
  998. }
  999. smbios_build_type_8_table();
  1000. smbios_build_type_9_table(errp);
  1001. smbios_build_type_11_table();
  1002. #define GET_DIMM_SZ ((i < dimm_cnt - 1) ? mc->smbios_memory_device_size \
  1003. : ((current_machine->ram_size - 1) % mc->smbios_memory_device_size) + 1)
  1004. dimm_cnt = QEMU_ALIGN_UP(current_machine->ram_size,
  1005. mc->smbios_memory_device_size) /
  1006. mc->smbios_memory_device_size;
  1007. /*
  1008. * The offset determines if we need to keep additional space between
  1009. * table 17 and table 19 header handle numbers so that they do
  1010. * not overlap. For example, for a VM with larger than 8 TB guest
  1011. * memory and DIMM like chunks of 16 GiB, the default space between
  1012. * the two tables (T19_BASE - T17_BASE = 512) is not enough.
  1013. */
  1014. offset = (dimm_cnt > (T19_BASE - T17_BASE)) ? \
  1015. dimm_cnt - (T19_BASE - T17_BASE) : 0;
  1016. smbios_build_type_16_table(dimm_cnt);
  1017. for (i = 0; i < dimm_cnt; i++) {
  1018. smbios_build_type_17_table(i, GET_DIMM_SZ);
  1019. }
  1020. for (i = 0; i < mem_array_size; i++) {
  1021. smbios_build_type_19_table(i, offset, mem_array[i].address,
  1022. mem_array[i].length);
  1023. }
  1024. /*
  1025. * make sure 16 bit handle numbers in the headers of tables 19
  1026. * and 32 do not overlap.
  1027. */
  1028. assert((mem_array_size + offset) < (T32_BASE - T19_BASE));
  1029. smbios_build_type_32_table();
  1030. smbios_build_type_38_table();
  1031. smbios_build_type_41_table(errp);
  1032. smbios_build_type_127_table();
  1033. if (!smbios_check_type4_count(ms->smp.sockets, errp)) {
  1034. goto err_exit;
  1035. }
  1036. if (!smbios_validate_table(ep_type, errp)) {
  1037. goto err_exit;
  1038. }
  1039. smbios_entry_point_setup(ep_type);
  1040. /* return tables blob and entry point (anchor), and their sizes */
  1041. *tables = smbios_tables;
  1042. *tables_len = smbios_tables_len;
  1043. *anchor = (uint8_t *)&ep;
  1044. /* calculate length based on anchor string */
  1045. if (!strncmp((char *)&ep, "_SM_", 4)) {
  1046. *anchor_len = sizeof(struct smbios_21_entry_point);
  1047. } else if (!strncmp((char *)&ep, "_SM3_", 5)) {
  1048. *anchor_len = sizeof(struct smbios_30_entry_point);
  1049. } else {
  1050. abort();
  1051. }
  1052. return true;
  1053. err_exit:
  1054. g_free(smbios_tables);
  1055. smbios_tables = NULL;
  1056. return false;
  1057. }
  1058. void smbios_get_tables(MachineState *ms,
  1059. SmbiosEntryPointType ep_type,
  1060. const struct smbios_phys_mem_area *mem_array,
  1061. const unsigned int mem_array_size,
  1062. uint8_t **tables, size_t *tables_len,
  1063. uint8_t **anchor, size_t *anchor_len,
  1064. Error **errp)
  1065. {
  1066. Error *local_err = NULL;
  1067. bool is_valid;
  1068. ERRP_GUARD();
  1069. switch (ep_type) {
  1070. case SMBIOS_ENTRY_POINT_TYPE_AUTO:
  1071. case SMBIOS_ENTRY_POINT_TYPE_32:
  1072. is_valid = smbios_get_tables_ep(ms, SMBIOS_ENTRY_POINT_TYPE_32,
  1073. mem_array, mem_array_size,
  1074. tables, tables_len,
  1075. anchor, anchor_len,
  1076. &local_err);
  1077. if (is_valid || ep_type != SMBIOS_ENTRY_POINT_TYPE_AUTO) {
  1078. break;
  1079. }
  1080. /*
  1081. * fall through in case AUTO endpoint is selected and
  1082. * SMBIOS 2.x tables can't be generated, to try if SMBIOS 3.x
  1083. * tables would work
  1084. */
  1085. case SMBIOS_ENTRY_POINT_TYPE_64:
  1086. error_free(local_err);
  1087. local_err = NULL;
  1088. is_valid = smbios_get_tables_ep(ms, SMBIOS_ENTRY_POINT_TYPE_64,
  1089. mem_array, mem_array_size,
  1090. tables, tables_len,
  1091. anchor, anchor_len,
  1092. &local_err);
  1093. break;
  1094. default:
  1095. abort();
  1096. }
  1097. if (!is_valid) {
  1098. error_propagate(errp, local_err);
  1099. }
  1100. }
  1101. static void save_opt(const char **dest, QemuOpts *opts, const char *name)
  1102. {
  1103. const char *val = qemu_opt_get(opts, name);
  1104. if (val) {
  1105. *dest = val;
  1106. }
  1107. }
  1108. struct opt_list {
  1109. size_t *ndest;
  1110. char ***dest;
  1111. };
  1112. static int save_opt_one(void *opaque,
  1113. const char *name, const char *value,
  1114. Error **errp)
  1115. {
  1116. struct opt_list *opt = opaque;
  1117. if (g_str_equal(name, "path")) {
  1118. g_autoptr(GByteArray) data = g_byte_array_new();
  1119. g_autofree char *buf = g_new(char, 4096);
  1120. ssize_t ret;
  1121. int fd = qemu_open(value, O_RDONLY, errp);
  1122. if (fd < 0) {
  1123. return -1;
  1124. }
  1125. while (1) {
  1126. ret = read(fd, buf, 4096);
  1127. if (ret == 0) {
  1128. break;
  1129. }
  1130. if (ret < 0) {
  1131. error_setg(errp, "Unable to read from %s: %s",
  1132. value, strerror(errno));
  1133. qemu_close(fd);
  1134. return -1;
  1135. }
  1136. if (memchr(buf, '\0', ret)) {
  1137. error_setg(errp, "NUL in OEM strings value in %s", value);
  1138. qemu_close(fd);
  1139. return -1;
  1140. }
  1141. g_byte_array_append(data, (guint8 *)buf, ret);
  1142. }
  1143. buf[0] = '\0';
  1144. g_byte_array_append(data, (guint8 *)buf, 1);
  1145. qemu_close(fd);
  1146. *opt->dest = g_renew(char *, *opt->dest, (*opt->ndest) + 1);
  1147. (*opt->dest)[*opt->ndest] = (char *)g_byte_array_free(data, FALSE);
  1148. (*opt->ndest)++;
  1149. data = NULL;
  1150. } else if (g_str_equal(name, "value")) {
  1151. *opt->dest = g_renew(char *, *opt->dest, (*opt->ndest) + 1);
  1152. (*opt->dest)[*opt->ndest] = g_strdup(value);
  1153. (*opt->ndest)++;
  1154. } else if (!g_str_equal(name, "type")) {
  1155. error_setg(errp, "Unexpected option %s", name);
  1156. return -1;
  1157. }
  1158. return 0;
  1159. }
  1160. static bool save_opt_list(size_t *ndest, char ***dest, QemuOpts *opts,
  1161. Error **errp)
  1162. {
  1163. struct opt_list opt = {
  1164. ndest, dest,
  1165. };
  1166. if (!qemu_opt_foreach(opts, save_opt_one, &opt, errp)) {
  1167. return false;
  1168. }
  1169. return true;
  1170. }
  1171. void smbios_entry_add(QemuOpts *opts, Error **errp)
  1172. {
  1173. const char *val;
  1174. val = qemu_opt_get(opts, "file");
  1175. if (val) {
  1176. struct smbios_structure_header *header;
  1177. size_t size;
  1178. if (!qemu_opts_validate(opts, qemu_smbios_file_opts, errp)) {
  1179. return;
  1180. }
  1181. size = get_image_size(val);
  1182. if (size == -1 || size < sizeof(struct smbios_structure_header)) {
  1183. error_setg(errp, "Cannot read SMBIOS file %s", val);
  1184. return;
  1185. }
  1186. /*
  1187. * NOTE: standard double '\0' terminator expected, per smbios spec.
  1188. * (except in legacy mode, where the second '\0' is implicit and
  1189. * will be inserted by the BIOS).
  1190. */
  1191. usr_blobs = g_realloc(usr_blobs, usr_blobs_len + size);
  1192. header = (struct smbios_structure_header *)(usr_blobs +
  1193. usr_blobs_len);
  1194. if (load_image_size(val, (uint8_t *)header, size) != size) {
  1195. error_setg(errp, "Failed to load SMBIOS file %s", val);
  1196. return;
  1197. }
  1198. if (header->type <= SMBIOS_MAX_TYPE) {
  1199. if (test_bit(header->type, smbios_have_fields_bitmap)) {
  1200. error_setg(errp,
  1201. "can't load type %d struct, fields already specified!",
  1202. header->type);
  1203. return;
  1204. }
  1205. set_bit(header->type, smbios_have_binfile_bitmap);
  1206. }
  1207. if (header->type == 4) {
  1208. smbios_type4_count++;
  1209. }
  1210. /*
  1211. * preserve blob size for legacy mode so it could build its
  1212. * blobs flavor from 'usr_blobs'
  1213. */
  1214. smbios_add_usr_blob_size(size);
  1215. usr_blobs_len += size;
  1216. if (size > usr_table_max) {
  1217. usr_table_max = size;
  1218. }
  1219. usr_table_cnt++;
  1220. return;
  1221. }
  1222. val = qemu_opt_get(opts, "type");
  1223. if (val) {
  1224. unsigned long type = strtoul(val, NULL, 0);
  1225. if (type > SMBIOS_MAX_TYPE) {
  1226. error_setg(errp, "out of range!");
  1227. return;
  1228. }
  1229. if (test_bit(type, smbios_have_binfile_bitmap)) {
  1230. error_setg(errp, "can't add fields, binary file already loaded!");
  1231. return;
  1232. }
  1233. set_bit(type, smbios_have_fields_bitmap);
  1234. switch (type) {
  1235. case 0:
  1236. if (!qemu_opts_validate(opts, qemu_smbios_type0_opts, errp)) {
  1237. return;
  1238. }
  1239. save_opt(&smbios_type0.vendor, opts, "vendor");
  1240. save_opt(&smbios_type0.version, opts, "version");
  1241. save_opt(&smbios_type0.date, opts, "date");
  1242. smbios_type0.uefi = qemu_opt_get_bool(opts, "uefi", false);
  1243. val = qemu_opt_get(opts, "release");
  1244. if (val) {
  1245. if (sscanf(val, "%hhu.%hhu", &smbios_type0.major,
  1246. &smbios_type0.minor) != 2) {
  1247. error_setg(errp, "Invalid release");
  1248. return;
  1249. }
  1250. smbios_type0.have_major_minor = true;
  1251. }
  1252. return;
  1253. case 1:
  1254. if (!qemu_opts_validate(opts, qemu_smbios_type1_opts, errp)) {
  1255. return;
  1256. }
  1257. save_opt(&smbios_type1.manufacturer, opts, "manufacturer");
  1258. save_opt(&smbios_type1.product, opts, "product");
  1259. save_opt(&smbios_type1.version, opts, "version");
  1260. save_opt(&smbios_type1.serial, opts, "serial");
  1261. save_opt(&smbios_type1.sku, opts, "sku");
  1262. save_opt(&smbios_type1.family, opts, "family");
  1263. val = qemu_opt_get(opts, "uuid");
  1264. if (val) {
  1265. if (qemu_uuid_parse(val, &qemu_uuid) != 0) {
  1266. error_setg(errp, "Invalid UUID");
  1267. return;
  1268. }
  1269. qemu_uuid_set = true;
  1270. }
  1271. return;
  1272. case 2:
  1273. if (!qemu_opts_validate(opts, qemu_smbios_type2_opts, errp)) {
  1274. return;
  1275. }
  1276. save_opt(&type2.manufacturer, opts, "manufacturer");
  1277. save_opt(&type2.product, opts, "product");
  1278. save_opt(&type2.version, opts, "version");
  1279. save_opt(&type2.serial, opts, "serial");
  1280. save_opt(&type2.asset, opts, "asset");
  1281. save_opt(&type2.location, opts, "location");
  1282. return;
  1283. case 3:
  1284. if (!qemu_opts_validate(opts, qemu_smbios_type3_opts, errp)) {
  1285. return;
  1286. }
  1287. save_opt(&type3.manufacturer, opts, "manufacturer");
  1288. save_opt(&type3.version, opts, "version");
  1289. save_opt(&type3.serial, opts, "serial");
  1290. save_opt(&type3.asset, opts, "asset");
  1291. save_opt(&type3.sku, opts, "sku");
  1292. return;
  1293. case 4:
  1294. if (!qemu_opts_validate(opts, qemu_smbios_type4_opts, errp)) {
  1295. return;
  1296. }
  1297. save_opt(&type4.sock_pfx, opts, "sock_pfx");
  1298. type4.processor_family = qemu_opt_get_number(opts,
  1299. "processor-family",
  1300. 0x01 /* Other */);
  1301. save_opt(&type4.manufacturer, opts, "manufacturer");
  1302. save_opt(&type4.version, opts, "version");
  1303. save_opt(&type4.serial, opts, "serial");
  1304. save_opt(&type4.asset, opts, "asset");
  1305. save_opt(&type4.part, opts, "part");
  1306. /* If the value is 0, it will take the value from the CPU model. */
  1307. type4.processor_id = qemu_opt_get_number(opts, "processor-id", 0);
  1308. type4.max_speed = qemu_opt_get_number(opts, "max-speed",
  1309. DEFAULT_CPU_SPEED);
  1310. type4.current_speed = qemu_opt_get_number(opts, "current-speed",
  1311. DEFAULT_CPU_SPEED);
  1312. if (type4.max_speed > UINT16_MAX ||
  1313. type4.current_speed > UINT16_MAX) {
  1314. error_setg(errp, "SMBIOS CPU speed is too large (> %d)",
  1315. UINT16_MAX);
  1316. }
  1317. return;
  1318. case 8:
  1319. if (!qemu_opts_validate(opts, qemu_smbios_type8_opts, errp)) {
  1320. return;
  1321. }
  1322. struct type8_instance *t8_i;
  1323. t8_i = g_new0(struct type8_instance, 1);
  1324. save_opt(&t8_i->internal_reference, opts, "internal_reference");
  1325. save_opt(&t8_i->external_reference, opts, "external_reference");
  1326. t8_i->connector_type = qemu_opt_get_number(opts,
  1327. "connector_type", 0);
  1328. t8_i->port_type = qemu_opt_get_number(opts, "port_type", 0);
  1329. QTAILQ_INSERT_TAIL(&type8, t8_i, next);
  1330. return;
  1331. case 9: {
  1332. if (!qemu_opts_validate(opts, qemu_smbios_type9_opts, errp)) {
  1333. return;
  1334. }
  1335. struct type9_instance *t;
  1336. t = g_new0(struct type9_instance, 1);
  1337. save_opt(&t->slot_designation, opts, "slot_designation");
  1338. t->slot_type = qemu_opt_get_number(opts, "slot_type", 0);
  1339. t->slot_data_bus_width =
  1340. qemu_opt_get_number(opts, "slot_data_bus_width", 0);
  1341. t->current_usage = qemu_opt_get_number(opts, "current_usage", 0);
  1342. t->slot_length = qemu_opt_get_number(opts, "slot_length", 0);
  1343. t->slot_id = qemu_opt_get_number(opts, "slot_id", 0);
  1344. t->slot_characteristics1 =
  1345. qemu_opt_get_number(opts, "slot_characteristics1", 0);
  1346. t->slot_characteristics2 =
  1347. qemu_opt_get_number(opts, "slot_characteristics2", 0);
  1348. save_opt(&t->pcidev, opts, "pcidev");
  1349. QTAILQ_INSERT_TAIL(&type9, t, next);
  1350. return;
  1351. }
  1352. case 11:
  1353. if (!qemu_opts_validate(opts, qemu_smbios_type11_opts, errp)) {
  1354. return;
  1355. }
  1356. if (!save_opt_list(&type11.nvalues, &type11.values, opts, errp)) {
  1357. return;
  1358. }
  1359. return;
  1360. case 17:
  1361. if (!qemu_opts_validate(opts, qemu_smbios_type17_opts, errp)) {
  1362. return;
  1363. }
  1364. save_opt(&type17.loc_pfx, opts, "loc_pfx");
  1365. save_opt(&type17.bank, opts, "bank");
  1366. save_opt(&type17.manufacturer, opts, "manufacturer");
  1367. save_opt(&type17.serial, opts, "serial");
  1368. save_opt(&type17.asset, opts, "asset");
  1369. save_opt(&type17.part, opts, "part");
  1370. type17.speed = qemu_opt_get_number(opts, "speed", 0);
  1371. return;
  1372. case 41: {
  1373. struct type41_instance *t41_i;
  1374. Error *local_err = NULL;
  1375. if (!qemu_opts_validate(opts, qemu_smbios_type41_opts, errp)) {
  1376. return;
  1377. }
  1378. t41_i = g_new0(struct type41_instance, 1);
  1379. save_opt(&t41_i->designation, opts, "designation");
  1380. t41_i->kind = qapi_enum_parse(&type41_kind_lookup,
  1381. qemu_opt_get(opts, "kind"),
  1382. 0, &local_err) + 1;
  1383. t41_i->kind |= 0x80; /* enabled */
  1384. if (local_err != NULL) {
  1385. error_propagate(errp, local_err);
  1386. g_free(t41_i);
  1387. return;
  1388. }
  1389. t41_i->instance = qemu_opt_get_number(opts, "instance", 1);
  1390. save_opt(&t41_i->pcidev, opts, "pcidev");
  1391. QTAILQ_INSERT_TAIL(&type41, t41_i, next);
  1392. return;
  1393. }
  1394. default:
  1395. error_setg(errp,
  1396. "Don't know how to build fields for SMBIOS type %ld",
  1397. type);
  1398. return;
  1399. }
  1400. }
  1401. error_setg(errp, "Must specify type= or file=");
  1402. }