fw_cfg.c 16 KB

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