fw_cfg.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. /* debug firmware config */
  30. //#define DEBUG_FW_CFG
  31. #ifdef DEBUG_FW_CFG
  32. #define FW_CFG_DPRINTF(fmt, ...) \
  33. do { printf("FW_CFG: " fmt , ## __VA_ARGS__); } while (0)
  34. #else
  35. #define FW_CFG_DPRINTF(fmt, ...)
  36. #endif
  37. #define FW_CFG_SIZE 2
  38. typedef struct FWCfgEntry {
  39. uint32_t len;
  40. uint8_t *data;
  41. void *callback_opaque;
  42. FWCfgCallback callback;
  43. } FWCfgEntry;
  44. struct FWCfgState {
  45. SysBusDevice busdev;
  46. uint32_t ctl_iobase, data_iobase;
  47. FWCfgEntry entries[2][FW_CFG_MAX_ENTRY];
  48. FWCfgFiles *files;
  49. uint16_t cur_entry;
  50. uint32_t cur_offset;
  51. Notifier machine_ready;
  52. };
  53. static void fw_cfg_write(FWCfgState *s, uint8_t value)
  54. {
  55. int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
  56. FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
  57. FW_CFG_DPRINTF("write %d\n", value);
  58. if (s->cur_entry & FW_CFG_WRITE_CHANNEL && s->cur_offset < e->len) {
  59. e->data[s->cur_offset++] = value;
  60. if (s->cur_offset == e->len) {
  61. e->callback(e->callback_opaque, e->data);
  62. s->cur_offset = 0;
  63. }
  64. }
  65. }
  66. static int fw_cfg_select(FWCfgState *s, uint16_t key)
  67. {
  68. int ret;
  69. s->cur_offset = 0;
  70. if ((key & FW_CFG_ENTRY_MASK) >= FW_CFG_MAX_ENTRY) {
  71. s->cur_entry = FW_CFG_INVALID;
  72. ret = 0;
  73. } else {
  74. s->cur_entry = key;
  75. ret = 1;
  76. }
  77. FW_CFG_DPRINTF("select key %d (%sfound)\n", key, ret ? "" : "not ");
  78. return ret;
  79. }
  80. static uint8_t fw_cfg_read(FWCfgState *s)
  81. {
  82. int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
  83. FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
  84. uint8_t ret;
  85. if (s->cur_entry == FW_CFG_INVALID || !e->data || s->cur_offset >= e->len)
  86. ret = 0;
  87. else
  88. ret = e->data[s->cur_offset++];
  89. FW_CFG_DPRINTF("read %d\n", ret);
  90. return ret;
  91. }
  92. static uint32_t fw_cfg_io_readb(void *opaque, uint32_t addr)
  93. {
  94. return fw_cfg_read(opaque);
  95. }
  96. static void fw_cfg_io_writeb(void *opaque, uint32_t addr, uint32_t value)
  97. {
  98. fw_cfg_write(opaque, (uint8_t)value);
  99. }
  100. static void fw_cfg_io_writew(void *opaque, uint32_t addr, uint32_t value)
  101. {
  102. fw_cfg_select(opaque, (uint16_t)value);
  103. }
  104. static uint32_t fw_cfg_mem_readb(void *opaque, target_phys_addr_t addr)
  105. {
  106. return fw_cfg_read(opaque);
  107. }
  108. static void fw_cfg_mem_writeb(void *opaque, target_phys_addr_t addr,
  109. uint32_t value)
  110. {
  111. fw_cfg_write(opaque, (uint8_t)value);
  112. }
  113. static void fw_cfg_mem_writew(void *opaque, target_phys_addr_t addr,
  114. uint32_t value)
  115. {
  116. fw_cfg_select(opaque, (uint16_t)value);
  117. }
  118. static CPUReadMemoryFunc * const fw_cfg_ctl_mem_read[3] = {
  119. NULL,
  120. NULL,
  121. NULL,
  122. };
  123. static CPUWriteMemoryFunc * const fw_cfg_ctl_mem_write[3] = {
  124. NULL,
  125. fw_cfg_mem_writew,
  126. NULL,
  127. };
  128. static CPUReadMemoryFunc * const fw_cfg_data_mem_read[3] = {
  129. fw_cfg_mem_readb,
  130. NULL,
  131. NULL,
  132. };
  133. static CPUWriteMemoryFunc * const fw_cfg_data_mem_write[3] = {
  134. fw_cfg_mem_writeb,
  135. NULL,
  136. NULL,
  137. };
  138. static void fw_cfg_reset(DeviceState *d)
  139. {
  140. FWCfgState *s = DO_UPCAST(FWCfgState, busdev.qdev, d);
  141. fw_cfg_select(s, 0);
  142. }
  143. /* Save restore 32 bit int as uint16_t
  144. This is a Big hack, but it is how the old state did it.
  145. Or we broke compatibility in the state, or we can't use struct tm
  146. */
  147. static int get_uint32_as_uint16(QEMUFile *f, void *pv, size_t size)
  148. {
  149. uint32_t *v = pv;
  150. *v = qemu_get_be16(f);
  151. return 0;
  152. }
  153. static void put_unused(QEMUFile *f, void *pv, size_t size)
  154. {
  155. fprintf(stderr, "uint32_as_uint16 is only used for backward compatibility.\n");
  156. fprintf(stderr, "This functions shouldn't be called.\n");
  157. }
  158. static const VMStateInfo vmstate_hack_uint32_as_uint16 = {
  159. .name = "int32_as_uint16",
  160. .get = get_uint32_as_uint16,
  161. .put = put_unused,
  162. };
  163. #define VMSTATE_UINT16_HACK(_f, _s, _t) \
  164. VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
  165. static bool is_version_1(void *opaque, int version_id)
  166. {
  167. return version_id == 1;
  168. }
  169. static const VMStateDescription vmstate_fw_cfg = {
  170. .name = "fw_cfg",
  171. .version_id = 2,
  172. .minimum_version_id = 1,
  173. .minimum_version_id_old = 1,
  174. .fields = (VMStateField []) {
  175. VMSTATE_UINT16(cur_entry, FWCfgState),
  176. VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1),
  177. VMSTATE_UINT32_V(cur_offset, FWCfgState, 2),
  178. VMSTATE_END_OF_LIST()
  179. }
  180. };
  181. int fw_cfg_add_bytes(FWCfgState *s, uint16_t key, uint8_t *data, uint32_t len)
  182. {
  183. int arch = !!(key & FW_CFG_ARCH_LOCAL);
  184. key &= FW_CFG_ENTRY_MASK;
  185. if (key >= FW_CFG_MAX_ENTRY)
  186. return 0;
  187. s->entries[arch][key].data = data;
  188. s->entries[arch][key].len = len;
  189. return 1;
  190. }
  191. int fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value)
  192. {
  193. uint16_t *copy;
  194. copy = qemu_malloc(sizeof(value));
  195. *copy = cpu_to_le16(value);
  196. return fw_cfg_add_bytes(s, key, (uint8_t *)copy, sizeof(value));
  197. }
  198. int fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value)
  199. {
  200. uint32_t *copy;
  201. copy = qemu_malloc(sizeof(value));
  202. *copy = cpu_to_le32(value);
  203. return fw_cfg_add_bytes(s, key, (uint8_t *)copy, sizeof(value));
  204. }
  205. int fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value)
  206. {
  207. uint64_t *copy;
  208. copy = qemu_malloc(sizeof(value));
  209. *copy = cpu_to_le64(value);
  210. return fw_cfg_add_bytes(s, key, (uint8_t *)copy, sizeof(value));
  211. }
  212. int fw_cfg_add_callback(FWCfgState *s, uint16_t key, FWCfgCallback callback,
  213. void *callback_opaque, uint8_t *data, size_t len)
  214. {
  215. int arch = !!(key & FW_CFG_ARCH_LOCAL);
  216. if (!(key & FW_CFG_WRITE_CHANNEL))
  217. return 0;
  218. key &= FW_CFG_ENTRY_MASK;
  219. if (key >= FW_CFG_MAX_ENTRY || len > 65535)
  220. return 0;
  221. s->entries[arch][key].data = data;
  222. s->entries[arch][key].len = len;
  223. s->entries[arch][key].callback_opaque = callback_opaque;
  224. s->entries[arch][key].callback = callback;
  225. return 1;
  226. }
  227. int fw_cfg_add_file(FWCfgState *s, const char *filename, uint8_t *data,
  228. uint32_t len)
  229. {
  230. int i, index;
  231. if (!s->files) {
  232. int dsize = sizeof(uint32_t) + sizeof(FWCfgFile) * FW_CFG_FILE_SLOTS;
  233. s->files = qemu_mallocz(dsize);
  234. fw_cfg_add_bytes(s, FW_CFG_FILE_DIR, (uint8_t*)s->files, dsize);
  235. }
  236. index = be32_to_cpu(s->files->count);
  237. if (index == FW_CFG_FILE_SLOTS) {
  238. fprintf(stderr, "fw_cfg: out of file slots\n");
  239. return 0;
  240. }
  241. fw_cfg_add_bytes(s, FW_CFG_FILE_FIRST + index, data, len);
  242. pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name),
  243. filename);
  244. for (i = 0; i < index; i++) {
  245. if (strcmp(s->files->f[index].name, s->files->f[i].name) == 0) {
  246. FW_CFG_DPRINTF("%s: skip duplicate: %s\n", __FUNCTION__,
  247. s->files->f[index].name);
  248. return 1;
  249. }
  250. }
  251. s->files->f[index].size = cpu_to_be32(len);
  252. s->files->f[index].select = cpu_to_be16(FW_CFG_FILE_FIRST + index);
  253. FW_CFG_DPRINTF("%s: #%d: %s (%d bytes)\n", __FUNCTION__,
  254. index, s->files->f[index].name, len);
  255. s->files->count = cpu_to_be32(index+1);
  256. return 1;
  257. }
  258. static void fw_cfg_machine_ready(struct Notifier *n, void *data)
  259. {
  260. uint32_t len;
  261. FWCfgState *s = container_of(n, FWCfgState, machine_ready);
  262. char *bootindex = get_boot_devices_list(&len);
  263. fw_cfg_add_file(s, "bootorder", (uint8_t*)bootindex, len);
  264. }
  265. FWCfgState *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
  266. target_phys_addr_t ctl_addr, target_phys_addr_t data_addr)
  267. {
  268. DeviceState *dev;
  269. SysBusDevice *d;
  270. FWCfgState *s;
  271. dev = qdev_create(NULL, "fw_cfg");
  272. qdev_prop_set_uint32(dev, "ctl_iobase", ctl_port);
  273. qdev_prop_set_uint32(dev, "data_iobase", data_port);
  274. qdev_init_nofail(dev);
  275. d = sysbus_from_qdev(dev);
  276. s = DO_UPCAST(FWCfgState, busdev.qdev, dev);
  277. if (ctl_addr) {
  278. sysbus_mmio_map(d, 0, ctl_addr);
  279. }
  280. if (data_addr) {
  281. sysbus_mmio_map(d, 1, data_addr);
  282. }
  283. fw_cfg_add_bytes(s, FW_CFG_SIGNATURE, (uint8_t *)"QEMU", 4);
  284. fw_cfg_add_bytes(s, FW_CFG_UUID, qemu_uuid, 16);
  285. fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)(display_type == DT_NOGRAPHIC));
  286. fw_cfg_add_i16(s, FW_CFG_NB_CPUS, (uint16_t)smp_cpus);
  287. fw_cfg_add_i16(s, FW_CFG_MAX_CPUS, (uint16_t)max_cpus);
  288. fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu);
  289. s->machine_ready.notify = fw_cfg_machine_ready;
  290. qemu_add_machine_init_done_notifier(&s->machine_ready);
  291. return s;
  292. }
  293. static int fw_cfg_init1(SysBusDevice *dev)
  294. {
  295. FWCfgState *s = FROM_SYSBUS(FWCfgState, dev);
  296. int io_ctl_memory, io_data_memory;
  297. io_ctl_memory = cpu_register_io_memory(fw_cfg_ctl_mem_read,
  298. fw_cfg_ctl_mem_write, s,
  299. DEVICE_NATIVE_ENDIAN);
  300. sysbus_init_mmio(dev, FW_CFG_SIZE, io_ctl_memory);
  301. io_data_memory = cpu_register_io_memory(fw_cfg_data_mem_read,
  302. fw_cfg_data_mem_write, s,
  303. DEVICE_NATIVE_ENDIAN);
  304. sysbus_init_mmio(dev, FW_CFG_SIZE, io_data_memory);
  305. if (s->ctl_iobase) {
  306. register_ioport_write(s->ctl_iobase, 2, 2, fw_cfg_io_writew, s);
  307. }
  308. if (s->data_iobase) {
  309. register_ioport_read(s->data_iobase, 1, 1, fw_cfg_io_readb, s);
  310. register_ioport_write(s->data_iobase, 1, 1, fw_cfg_io_writeb, s);
  311. }
  312. return 0;
  313. }
  314. static SysBusDeviceInfo fw_cfg_info = {
  315. .init = fw_cfg_init1,
  316. .qdev.name = "fw_cfg",
  317. .qdev.size = sizeof(FWCfgState),
  318. .qdev.vmsd = &vmstate_fw_cfg,
  319. .qdev.reset = fw_cfg_reset,
  320. .qdev.no_user = 1,
  321. .qdev.props = (Property[]) {
  322. DEFINE_PROP_HEX32("ctl_iobase", FWCfgState, ctl_iobase, -1),
  323. DEFINE_PROP_HEX32("data_iobase", FWCfgState, data_iobase, -1),
  324. DEFINE_PROP_END_OF_LIST(),
  325. },
  326. };
  327. static void fw_cfg_register_devices(void)
  328. {
  329. sysbus_register_withprop(&fw_cfg_info);
  330. }
  331. device_init(fw_cfg_register_devices)