2
0

fw_cfg.c 16 KB

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