fw_cfg.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*
  2. * QEMU Firmware configuration device emulation
  3. *
  4. * Copyright (c) 2008 Gleb Natapov
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "hw.h"
  25. #include "sysemu/sysemu.h"
  26. #include "isa.h"
  27. #include "fw_cfg.h"
  28. #include "sysbus.h"
  29. #include "trace.h"
  30. #include "qemu/error-report.h"
  31. #include "qemu/config-file.h"
  32. #define FW_CFG_SIZE 2
  33. #define FW_CFG_DATA_SIZE 1
  34. typedef struct FWCfgEntry {
  35. uint32_t len;
  36. uint8_t *data;
  37. void *callback_opaque;
  38. FWCfgCallback callback;
  39. } FWCfgEntry;
  40. struct FWCfgState {
  41. SysBusDevice busdev;
  42. MemoryRegion ctl_iomem, data_iomem, comb_iomem;
  43. uint32_t ctl_iobase, data_iobase;
  44. FWCfgEntry entries[2][FW_CFG_MAX_ENTRY];
  45. FWCfgFiles *files;
  46. uint16_t cur_entry;
  47. uint32_t cur_offset;
  48. Notifier machine_ready;
  49. };
  50. #define JPG_FILE 0
  51. #define BMP_FILE 1
  52. static char *read_splashfile(char *filename, size_t *file_sizep,
  53. int *file_typep)
  54. {
  55. GError *err = NULL;
  56. gboolean res;
  57. gchar *content;
  58. int file_type;
  59. unsigned int filehead;
  60. int bmp_bpp;
  61. res = g_file_get_contents(filename, &content, file_sizep, &err);
  62. if (res == FALSE) {
  63. error_report("failed to read splash file '%s'", filename);
  64. g_error_free(err);
  65. return NULL;
  66. }
  67. /* check file size */
  68. if (*file_sizep < 30) {
  69. goto error;
  70. }
  71. /* check magic ID */
  72. filehead = ((content[0] & 0xff) + (content[1] << 8)) & 0xffff;
  73. if (filehead == 0xd8ff) {
  74. file_type = JPG_FILE;
  75. } else if (filehead == 0x4d42) {
  76. file_type = BMP_FILE;
  77. } else {
  78. goto error;
  79. }
  80. /* check BMP bpp */
  81. if (file_type == BMP_FILE) {
  82. bmp_bpp = (content[28] + (content[29] << 8)) & 0xffff;
  83. if (bmp_bpp != 24) {
  84. goto error;
  85. }
  86. }
  87. /* return values */
  88. *file_typep = file_type;
  89. return content;
  90. error:
  91. error_report("splash file '%s' format not recognized; must be JPEG "
  92. "or 24 bit BMP", filename);
  93. g_free(content);
  94. return NULL;
  95. }
  96. static void fw_cfg_bootsplash(FWCfgState *s)
  97. {
  98. int boot_splash_time = -1;
  99. const char *boot_splash_filename = NULL;
  100. char *p;
  101. char *filename, *file_data;
  102. size_t file_size;
  103. int file_type;
  104. const char *temp;
  105. /* get user configuration */
  106. QemuOptsList *plist = qemu_find_opts("boot-opts");
  107. QemuOpts *opts = QTAILQ_FIRST(&plist->head);
  108. if (opts != NULL) {
  109. temp = qemu_opt_get(opts, "splash");
  110. if (temp != NULL) {
  111. boot_splash_filename = temp;
  112. }
  113. temp = qemu_opt_get(opts, "splash-time");
  114. if (temp != NULL) {
  115. p = (char *)temp;
  116. boot_splash_time = strtol(p, (char **)&p, 10);
  117. }
  118. }
  119. /* insert splash time if user configurated */
  120. if (boot_splash_time >= 0) {
  121. /* validate the input */
  122. if (boot_splash_time > 0xffff) {
  123. error_report("splash time is big than 65535, force it to 65535.");
  124. boot_splash_time = 0xffff;
  125. }
  126. /* use little endian format */
  127. qemu_extra_params_fw[0] = (uint8_t)(boot_splash_time & 0xff);
  128. qemu_extra_params_fw[1] = (uint8_t)((boot_splash_time >> 8) & 0xff);
  129. fw_cfg_add_file(s, "etc/boot-menu-wait", qemu_extra_params_fw, 2);
  130. }
  131. /* insert splash file if user configurated */
  132. if (boot_splash_filename != NULL) {
  133. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, boot_splash_filename);
  134. if (filename == NULL) {
  135. error_report("failed to find file '%s'.", boot_splash_filename);
  136. return;
  137. }
  138. /* loading file data */
  139. file_data = read_splashfile(filename, &file_size, &file_type);
  140. if (file_data == NULL) {
  141. g_free(filename);
  142. return;
  143. }
  144. if (boot_splash_filedata != NULL) {
  145. g_free(boot_splash_filedata);
  146. }
  147. boot_splash_filedata = (uint8_t *)file_data;
  148. boot_splash_filedata_size = file_size;
  149. /* insert data */
  150. if (file_type == JPG_FILE) {
  151. fw_cfg_add_file(s, "bootsplash.jpg",
  152. boot_splash_filedata, boot_splash_filedata_size);
  153. } else {
  154. fw_cfg_add_file(s, "bootsplash.bmp",
  155. boot_splash_filedata, boot_splash_filedata_size);
  156. }
  157. g_free(filename);
  158. }
  159. }
  160. static void fw_cfg_reboot(FWCfgState *s)
  161. {
  162. int reboot_timeout = -1;
  163. char *p;
  164. const char *temp;
  165. /* get user configuration */
  166. QemuOptsList *plist = qemu_find_opts("boot-opts");
  167. QemuOpts *opts = QTAILQ_FIRST(&plist->head);
  168. if (opts != NULL) {
  169. temp = qemu_opt_get(opts, "reboot-timeout");
  170. if (temp != NULL) {
  171. p = (char *)temp;
  172. reboot_timeout = strtol(p, (char **)&p, 10);
  173. }
  174. }
  175. /* validate the input */
  176. if (reboot_timeout > 0xffff) {
  177. error_report("reboot timeout is larger than 65535, force it to 65535.");
  178. reboot_timeout = 0xffff;
  179. }
  180. fw_cfg_add_file(s, "etc/boot-fail-wait", g_memdup(&reboot_timeout, 4), 4);
  181. }
  182. static void fw_cfg_write(FWCfgState *s, uint8_t value)
  183. {
  184. int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
  185. FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
  186. trace_fw_cfg_write(s, value);
  187. if (s->cur_entry & FW_CFG_WRITE_CHANNEL && e->callback &&
  188. s->cur_offset < e->len) {
  189. e->data[s->cur_offset++] = value;
  190. if (s->cur_offset == e->len) {
  191. e->callback(e->callback_opaque, e->data);
  192. s->cur_offset = 0;
  193. }
  194. }
  195. }
  196. static int fw_cfg_select(FWCfgState *s, uint16_t key)
  197. {
  198. int ret;
  199. s->cur_offset = 0;
  200. if ((key & FW_CFG_ENTRY_MASK) >= FW_CFG_MAX_ENTRY) {
  201. s->cur_entry = FW_CFG_INVALID;
  202. ret = 0;
  203. } else {
  204. s->cur_entry = key;
  205. ret = 1;
  206. }
  207. trace_fw_cfg_select(s, key, ret);
  208. return ret;
  209. }
  210. static uint8_t fw_cfg_read(FWCfgState *s)
  211. {
  212. int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
  213. FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
  214. uint8_t ret;
  215. if (s->cur_entry == FW_CFG_INVALID || !e->data || s->cur_offset >= e->len)
  216. ret = 0;
  217. else
  218. ret = e->data[s->cur_offset++];
  219. trace_fw_cfg_read(s, ret);
  220. return ret;
  221. }
  222. static uint64_t fw_cfg_data_mem_read(void *opaque, hwaddr addr,
  223. unsigned size)
  224. {
  225. return fw_cfg_read(opaque);
  226. }
  227. static void fw_cfg_data_mem_write(void *opaque, hwaddr addr,
  228. uint64_t value, unsigned size)
  229. {
  230. fw_cfg_write(opaque, (uint8_t)value);
  231. }
  232. static void fw_cfg_ctl_mem_write(void *opaque, hwaddr addr,
  233. uint64_t value, unsigned size)
  234. {
  235. fw_cfg_select(opaque, (uint16_t)value);
  236. }
  237. static bool fw_cfg_ctl_mem_valid(void *opaque, hwaddr addr,
  238. unsigned size, bool is_write)
  239. {
  240. return is_write && size == 2;
  241. }
  242. static uint64_t fw_cfg_comb_read(void *opaque, hwaddr addr,
  243. unsigned size)
  244. {
  245. return fw_cfg_read(opaque);
  246. }
  247. static void fw_cfg_comb_write(void *opaque, hwaddr addr,
  248. uint64_t value, unsigned size)
  249. {
  250. switch (size) {
  251. case 1:
  252. fw_cfg_write(opaque, (uint8_t)value);
  253. break;
  254. case 2:
  255. fw_cfg_select(opaque, (uint16_t)value);
  256. break;
  257. }
  258. }
  259. static bool fw_cfg_comb_valid(void *opaque, hwaddr addr,
  260. unsigned size, bool is_write)
  261. {
  262. return (size == 1) || (is_write && size == 2);
  263. }
  264. static const MemoryRegionOps fw_cfg_ctl_mem_ops = {
  265. .write = fw_cfg_ctl_mem_write,
  266. .endianness = DEVICE_NATIVE_ENDIAN,
  267. .valid.accepts = fw_cfg_ctl_mem_valid,
  268. };
  269. static const MemoryRegionOps fw_cfg_data_mem_ops = {
  270. .read = fw_cfg_data_mem_read,
  271. .write = fw_cfg_data_mem_write,
  272. .endianness = DEVICE_NATIVE_ENDIAN,
  273. .valid = {
  274. .min_access_size = 1,
  275. .max_access_size = 1,
  276. },
  277. };
  278. static const MemoryRegionOps fw_cfg_comb_mem_ops = {
  279. .read = fw_cfg_comb_read,
  280. .write = fw_cfg_comb_write,
  281. .endianness = DEVICE_NATIVE_ENDIAN,
  282. .valid.accepts = fw_cfg_comb_valid,
  283. };
  284. static void fw_cfg_reset(DeviceState *d)
  285. {
  286. FWCfgState *s = DO_UPCAST(FWCfgState, busdev.qdev, d);
  287. fw_cfg_select(s, 0);
  288. }
  289. /* Save restore 32 bit int as uint16_t
  290. This is a Big hack, but it is how the old state did it.
  291. Or we broke compatibility in the state, or we can't use struct tm
  292. */
  293. static int get_uint32_as_uint16(QEMUFile *f, void *pv, size_t size)
  294. {
  295. uint32_t *v = pv;
  296. *v = qemu_get_be16(f);
  297. return 0;
  298. }
  299. static void put_unused(QEMUFile *f, void *pv, size_t size)
  300. {
  301. fprintf(stderr, "uint32_as_uint16 is only used for backward compatibility.\n");
  302. fprintf(stderr, "This functions shouldn't be called.\n");
  303. }
  304. static const VMStateInfo vmstate_hack_uint32_as_uint16 = {
  305. .name = "int32_as_uint16",
  306. .get = get_uint32_as_uint16,
  307. .put = put_unused,
  308. };
  309. #define VMSTATE_UINT16_HACK(_f, _s, _t) \
  310. VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
  311. static bool is_version_1(void *opaque, int version_id)
  312. {
  313. return version_id == 1;
  314. }
  315. static const VMStateDescription vmstate_fw_cfg = {
  316. .name = "fw_cfg",
  317. .version_id = 2,
  318. .minimum_version_id = 1,
  319. .minimum_version_id_old = 1,
  320. .fields = (VMStateField []) {
  321. VMSTATE_UINT16(cur_entry, FWCfgState),
  322. VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1),
  323. VMSTATE_UINT32_V(cur_offset, FWCfgState, 2),
  324. VMSTATE_END_OF_LIST()
  325. }
  326. };
  327. void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len)
  328. {
  329. int arch = !!(key & FW_CFG_ARCH_LOCAL);
  330. key &= FW_CFG_ENTRY_MASK;
  331. assert(key < FW_CFG_MAX_ENTRY && len < UINT32_MAX);
  332. s->entries[arch][key].data = data;
  333. s->entries[arch][key].len = (uint32_t)len;
  334. }
  335. void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value)
  336. {
  337. size_t sz = strlen(value) + 1;
  338. return fw_cfg_add_bytes(s, key, g_memdup(value, sz), sz);
  339. }
  340. void fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value)
  341. {
  342. uint16_t *copy;
  343. copy = g_malloc(sizeof(value));
  344. *copy = cpu_to_le16(value);
  345. fw_cfg_add_bytes(s, key, copy, sizeof(value));
  346. }
  347. void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value)
  348. {
  349. uint32_t *copy;
  350. copy = g_malloc(sizeof(value));
  351. *copy = cpu_to_le32(value);
  352. fw_cfg_add_bytes(s, key, copy, sizeof(value));
  353. }
  354. void fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value)
  355. {
  356. uint64_t *copy;
  357. copy = g_malloc(sizeof(value));
  358. *copy = cpu_to_le64(value);
  359. fw_cfg_add_bytes(s, key, copy, sizeof(value));
  360. }
  361. void fw_cfg_add_callback(FWCfgState *s, uint16_t key, FWCfgCallback callback,
  362. void *callback_opaque, void *data, size_t len)
  363. {
  364. int arch = !!(key & FW_CFG_ARCH_LOCAL);
  365. assert(key & FW_CFG_WRITE_CHANNEL);
  366. key &= FW_CFG_ENTRY_MASK;
  367. assert(key < FW_CFG_MAX_ENTRY && len <= UINT32_MAX);
  368. s->entries[arch][key].data = data;
  369. s->entries[arch][key].len = (uint32_t)len;
  370. s->entries[arch][key].callback_opaque = callback_opaque;
  371. s->entries[arch][key].callback = callback;
  372. }
  373. void fw_cfg_add_file(FWCfgState *s, const char *filename,
  374. void *data, size_t len)
  375. {
  376. int i, index;
  377. size_t dsize;
  378. if (!s->files) {
  379. dsize = sizeof(uint32_t) + sizeof(FWCfgFile) * FW_CFG_FILE_SLOTS;
  380. s->files = g_malloc0(dsize);
  381. fw_cfg_add_bytes(s, FW_CFG_FILE_DIR, s->files, dsize);
  382. }
  383. index = be32_to_cpu(s->files->count);
  384. assert(index < FW_CFG_FILE_SLOTS);
  385. fw_cfg_add_bytes(s, FW_CFG_FILE_FIRST + index, data, len);
  386. pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name),
  387. filename);
  388. for (i = 0; i < index; i++) {
  389. if (strcmp(s->files->f[index].name, s->files->f[i].name) == 0) {
  390. trace_fw_cfg_add_file_dupe(s, s->files->f[index].name);
  391. return;
  392. }
  393. }
  394. s->files->f[index].size = cpu_to_be32(len);
  395. s->files->f[index].select = cpu_to_be16(FW_CFG_FILE_FIRST + index);
  396. trace_fw_cfg_add_file(s, index, s->files->f[index].name, len);
  397. s->files->count = cpu_to_be32(index+1);
  398. }
  399. static void fw_cfg_machine_ready(struct Notifier *n, void *data)
  400. {
  401. size_t len;
  402. FWCfgState *s = container_of(n, FWCfgState, machine_ready);
  403. char *bootindex = get_boot_devices_list(&len);
  404. fw_cfg_add_file(s, "bootorder", (uint8_t*)bootindex, len);
  405. }
  406. FWCfgState *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
  407. hwaddr ctl_addr, hwaddr data_addr)
  408. {
  409. DeviceState *dev;
  410. SysBusDevice *d;
  411. FWCfgState *s;
  412. dev = qdev_create(NULL, "fw_cfg");
  413. qdev_prop_set_uint32(dev, "ctl_iobase", ctl_port);
  414. qdev_prop_set_uint32(dev, "data_iobase", data_port);
  415. qdev_init_nofail(dev);
  416. d = SYS_BUS_DEVICE(dev);
  417. s = DO_UPCAST(FWCfgState, busdev.qdev, dev);
  418. if (ctl_addr) {
  419. sysbus_mmio_map(d, 0, ctl_addr);
  420. }
  421. if (data_addr) {
  422. sysbus_mmio_map(d, 1, data_addr);
  423. }
  424. fw_cfg_add_bytes(s, FW_CFG_SIGNATURE, (char *)"QEMU", 4);
  425. fw_cfg_add_bytes(s, FW_CFG_UUID, qemu_uuid, 16);
  426. fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)(display_type == DT_NOGRAPHIC));
  427. fw_cfg_add_i16(s, FW_CFG_NB_CPUS, (uint16_t)smp_cpus);
  428. fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu);
  429. fw_cfg_bootsplash(s);
  430. fw_cfg_reboot(s);
  431. s->machine_ready.notify = fw_cfg_machine_ready;
  432. qemu_add_machine_init_done_notifier(&s->machine_ready);
  433. return s;
  434. }
  435. static int fw_cfg_init1(SysBusDevice *dev)
  436. {
  437. FWCfgState *s = FROM_SYSBUS(FWCfgState, dev);
  438. memory_region_init_io(&s->ctl_iomem, &fw_cfg_ctl_mem_ops, s,
  439. "fwcfg.ctl", FW_CFG_SIZE);
  440. sysbus_init_mmio(dev, &s->ctl_iomem);
  441. memory_region_init_io(&s->data_iomem, &fw_cfg_data_mem_ops, s,
  442. "fwcfg.data", FW_CFG_DATA_SIZE);
  443. sysbus_init_mmio(dev, &s->data_iomem);
  444. /* In case ctl and data overlap: */
  445. memory_region_init_io(&s->comb_iomem, &fw_cfg_comb_mem_ops, s,
  446. "fwcfg", FW_CFG_SIZE);
  447. if (s->ctl_iobase + 1 == s->data_iobase) {
  448. sysbus_add_io(dev, s->ctl_iobase, &s->comb_iomem);
  449. } else {
  450. if (s->ctl_iobase) {
  451. sysbus_add_io(dev, s->ctl_iobase, &s->ctl_iomem);
  452. }
  453. if (s->data_iobase) {
  454. sysbus_add_io(dev, s->data_iobase, &s->data_iomem);
  455. }
  456. }
  457. return 0;
  458. }
  459. static Property fw_cfg_properties[] = {
  460. DEFINE_PROP_HEX32("ctl_iobase", FWCfgState, ctl_iobase, -1),
  461. DEFINE_PROP_HEX32("data_iobase", FWCfgState, data_iobase, -1),
  462. DEFINE_PROP_END_OF_LIST(),
  463. };
  464. static void fw_cfg_class_init(ObjectClass *klass, void *data)
  465. {
  466. DeviceClass *dc = DEVICE_CLASS(klass);
  467. SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
  468. k->init = fw_cfg_init1;
  469. dc->no_user = 1;
  470. dc->reset = fw_cfg_reset;
  471. dc->vmsd = &vmstate_fw_cfg;
  472. dc->props = fw_cfg_properties;
  473. }
  474. static const TypeInfo fw_cfg_info = {
  475. .name = "fw_cfg",
  476. .parent = TYPE_SYS_BUS_DEVICE,
  477. .instance_size = sizeof(FWCfgState),
  478. .class_init = fw_cfg_class_init,
  479. };
  480. static void fw_cfg_register_types(void)
  481. {
  482. type_register_static(&fw_cfg_info);
  483. }
  484. type_init(fw_cfg_register_types)