Statistics
| Branch: | Revision:

root / vl.c @ 05175535

History | View | Annotate | Download (91.4 kB)

1 0824d6fc bellard
/*
2 80cabfad bellard
 * QEMU System Emulator
3 5fafdf24 ths
 *
4 68d0f70e bellard
 * Copyright (c) 2003-2008 Fabrice Bellard
5 5fafdf24 ths
 *
6 1df912cf bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 1df912cf bellard
 * of this software and associated documentation files (the "Software"), to deal
8 1df912cf bellard
 * in the Software without restriction, including without limitation the rights
9 1df912cf bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 1df912cf bellard
 * copies of the Software, and to permit persons to whom the Software is
11 1df912cf bellard
 * furnished to do so, subject to the following conditions:
12 1df912cf bellard
 *
13 1df912cf bellard
 * The above copyright notice and this permission notice shall be included in
14 1df912cf bellard
 * all copies or substantial portions of the Software.
15 1df912cf bellard
 *
16 1df912cf bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 1df912cf bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 1df912cf bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 1df912cf bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 1df912cf bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 1df912cf bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 1df912cf bellard
 * THE SOFTWARE.
23 0824d6fc bellard
 */
24 0824d6fc bellard
#include <unistd.h>
25 0824d6fc bellard
#include <fcntl.h>
26 0824d6fc bellard
#include <signal.h>
27 0824d6fc bellard
#include <time.h>
28 0824d6fc bellard
#include <errno.h>
29 67b915a5 bellard
#include <sys/time.h>
30 c88676f8 bellard
#include <zlib.h>
31 67b915a5 bellard
32 71e72a19 Juan Quintela
/* Needed early for CONFIG_BSD etc. */
33 d40cdb10 blueswir1
#include "config-host.h"
34 d40cdb10 blueswir1
35 67b915a5 bellard
#ifndef _WIN32
36 5cea8590 Paul Brook
#include <libgen.h>
37 67b915a5 bellard
#include <sys/times.h>
38 f1510b2c bellard
#include <sys/wait.h>
39 67b915a5 bellard
#include <termios.h>
40 67b915a5 bellard
#include <sys/mman.h>
41 f1510b2c bellard
#include <sys/ioctl.h>
42 24646c7e blueswir1
#include <sys/resource.h>
43 f1510b2c bellard
#include <sys/socket.h>
44 c94c8d64 bellard
#include <netinet/in.h>
45 24646c7e blueswir1
#include <net/if.h>
46 24646c7e blueswir1
#include <arpa/inet.h>
47 9d728e8c bellard
#include <dirent.h>
48 7c9d8e07 bellard
#include <netdb.h>
49 cb4b976b ths
#include <sys/select.h>
50 ab6540d5 Prerna Saxena
51 71e72a19 Juan Quintela
#ifdef CONFIG_BSD
52 7d3505c5 bellard
#include <sys/stat.h>
53 a167ba50 Aurelien Jarno
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
54 7d3505c5 bellard
#include <libutil.h>
55 92c0e657 Juergen Lock
#include <sys/sysctl.h>
56 24646c7e blueswir1
#else
57 24646c7e blueswir1
#include <util.h>
58 128ab2ff blueswir1
#endif
59 bbe813a2 Aurelien Jarno
#else
60 223f0d72 blueswir1
#ifdef __linux__
61 7d3505c5 bellard
#include <pty.h>
62 7d3505c5 bellard
#include <malloc.h>
63 bd494f4c ths
64 e57a8c0e bellard
#include <linux/ppdev.h>
65 5867c88a ths
#include <linux/parport.h>
66 223f0d72 blueswir1
#endif
67 223f0d72 blueswir1
#ifdef __sun__
68 d5d10bc3 ths
#include <sys/stat.h>
69 d5d10bc3 ths
#include <sys/ethernet.h>
70 d5d10bc3 ths
#include <sys/sockio.h>
71 d5d10bc3 ths
#include <netinet/arp.h>
72 d5d10bc3 ths
#include <netinet/in_systm.h>
73 d5d10bc3 ths
#include <netinet/ip.h>
74 d5d10bc3 ths
#include <netinet/ip_icmp.h> // must come after ip.h
75 d5d10bc3 ths
#include <netinet/udp.h>
76 d5d10bc3 ths
#include <netinet/tcp.h>
77 d5d10bc3 ths
#include <net/if.h>
78 d5d10bc3 ths
#include <syslog.h>
79 d5d10bc3 ths
#include <stropts.h>
80 67b915a5 bellard
#endif
81 7d3505c5 bellard
#endif
82 ec530c81 bellard
#endif
83 67b915a5 bellard
84 9892fbfb blueswir1
#if defined(__OpenBSD__)
85 9892fbfb blueswir1
#include <util.h>
86 9892fbfb blueswir1
#endif
87 9892fbfb blueswir1
88 8a16d273 ths
#if defined(CONFIG_VDE)
89 8a16d273 ths
#include <libvdeplug.h>
90 8a16d273 ths
#endif
91 8a16d273 ths
92 67b915a5 bellard
#ifdef _WIN32
93 49dc768d aliguori
#include <windows.h>
94 67b915a5 bellard
#endif
95 67b915a5 bellard
96 73332e5c bellard
#ifdef CONFIG_SDL
97 59a36a2f Stefan Weil
#if defined(__APPLE__) || defined(main)
98 6693665a Stefan Weil
#include <SDL.h>
99 880fec5d malc
int qemu_main(int argc, char **argv, char **envp);
100 880fec5d malc
int main(int argc, char **argv)
101 880fec5d malc
{
102 59a36a2f Stefan Weil
    return qemu_main(argc, argv, NULL);
103 880fec5d malc
}
104 880fec5d malc
#undef main
105 880fec5d malc
#define main qemu_main
106 96bcd4f8 bellard
#endif
107 73332e5c bellard
#endif /* CONFIG_SDL */
108 0824d6fc bellard
109 5b0753e0 bellard
#ifdef CONFIG_COCOA
110 5b0753e0 bellard
#undef main
111 5b0753e0 bellard
#define main qemu_main
112 5b0753e0 bellard
#endif /* CONFIG_COCOA */
113 5b0753e0 bellard
114 511d2b14 blueswir1
#include "hw/hw.h"
115 511d2b14 blueswir1
#include "hw/boards.h"
116 511d2b14 blueswir1
#include "hw/usb.h"
117 511d2b14 blueswir1
#include "hw/pcmcia.h"
118 511d2b14 blueswir1
#include "hw/pc.h"
119 511d2b14 blueswir1
#include "hw/isa.h"
120 511d2b14 blueswir1
#include "hw/baum.h"
121 511d2b14 blueswir1
#include "hw/bt.h"
122 9dd986cc Richard W.M. Jones
#include "hw/watchdog.h"
123 b6f6e3d3 aliguori
#include "hw/smbios.h"
124 e37630ca aliguori
#include "hw/xen.h"
125 bd3c948d Gerd Hoffmann
#include "hw/qdev.h"
126 45a50b16 Gerd Hoffmann
#include "hw/loader.h"
127 5ef4efa4 aurel32
#include "bt-host.h"
128 511d2b14 blueswir1
#include "net.h"
129 68ac40d2 Mark McLoughlin
#include "net/slirp.h"
130 511d2b14 blueswir1
#include "monitor.h"
131 511d2b14 blueswir1
#include "console.h"
132 511d2b14 blueswir1
#include "sysemu.h"
133 511d2b14 blueswir1
#include "gdbstub.h"
134 511d2b14 blueswir1
#include "qemu-timer.h"
135 511d2b14 blueswir1
#include "qemu-char.h"
136 511d2b14 blueswir1
#include "cache-utils.h"
137 511d2b14 blueswir1
#include "block.h"
138 666daa68 Markus Armbruster
#include "blockdev.h"
139 c163b5ca lirans@il.ibm.com
#include "block-migration.h"
140 a718acec blueswir1
#include "dma.h"
141 511d2b14 blueswir1
#include "audio/audio.h"
142 511d2b14 blueswir1
#include "migration.h"
143 511d2b14 blueswir1
#include "kvm.h"
144 d3f24367 Kevin Wolf
#include "qemu-option.h"
145 7282a033 Gerd Hoffmann
#include "qemu-config.h"
146 e78c48ec Luiz Capitulino
#include "qemu-objects.h"
147 59a5264b Jes Sorensen
#include "qemu-options.h"
148 758e8e38 Venkateswararao Jujjuri (JV)
#ifdef CONFIG_VIRTFS
149 74db920c Gautham R Shenoy
#include "fsdev/qemu-fsdev.h"
150 74db920c Gautham R Shenoy
#endif
151 511d2b14 blueswir1
152 0824d6fc bellard
#include "disas.h"
153 fc01f7e7 bellard
154 511d2b14 blueswir1
#include "qemu_socket.h"
155 511d2b14 blueswir1
156 d918f23e Jan Kiszka
#include "slirp/libslirp.h"
157 511d2b14 blueswir1
158 94b0b5ff Stefan Hajnoczi
#include "trace.h"
159 0b5538c3 Stefan Hajnoczi
#include "simpletrace.h"
160 72cf2d4f Blue Swirl
#include "qemu-queue.h"
161 296af7c9 Blue Swirl
#include "cpus.h"
162 ad96090a Blue Swirl
#include "arch_init.h"
163 72cf2d4f Blue Swirl
164 29b0040b Gerd Hoffmann
#include "ui/qemu-spice.h"
165 29b0040b Gerd Hoffmann
166 9dc63a1e blueswir1
//#define DEBUG_NET
167 9dc63a1e blueswir1
//#define DEBUG_SLIRP
168 330d0414 bellard
169 1bfe856e bellard
#define DEFAULT_RAM_SIZE 128
170 313aa567 bellard
171 98b19252 Amit Shah
#define MAX_VIRTIO_CONSOLES 1
172 98b19252 Amit Shah
173 5cea8590 Paul Brook
static const char *data_dir;
174 1192dad8 j_mayer
const char *bios_name = NULL;
175 cb5a7aa8 malc
enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
176 993fbfdb Anthony Liguori
DisplayType display_type = DT_DEFAULT;
177 6b62dc2d Gerd Hoffmann
int display_remote = 0;
178 3d11d0eb bellard
const char* keyboard_layout = NULL;
179 c227f099 Anthony Liguori
ram_addr_t ram_size;
180 c902760f Marcelo Tosatti
const char *mem_path = NULL;
181 c902760f Marcelo Tosatti
#ifdef MAP_POPULATE
182 c902760f Marcelo Tosatti
int mem_prealloc = 0; /* force preallocation of physical target memory */
183 c902760f Marcelo Tosatti
#endif
184 c4b1fcc0 bellard
int nb_nics;
185 7c9d8e07 bellard
NICInfo nd_table[MAX_NICS];
186 8a7ddc38 bellard
int vm_running;
187 d399f677 Paolo Bonzini
int autostart;
188 8e84865e Amit Shah
int incoming_expected; /* Started with -incoming and waiting for incoming */
189 f6503059 balrog
static int rtc_utc = 1;
190 f6503059 balrog
static int rtc_date_offset = -1; /* -1 means no change */
191 6875204c Jan Kiszka
QEMUClock *rtc_clock;
192 64465297 Gerd Hoffmann
int vga_interface_type = VGA_NONE;
193 dbed7e40 blueswir1
static int full_screen = 0;
194 634a21f6 blueswir1
#ifdef CONFIG_SDL
195 dbed7e40 blueswir1
static int no_frame = 0;
196 634a21f6 blueswir1
#endif
197 667accab ths
int no_quit = 0;
198 8d11df9e bellard
CharDriverState *serial_hds[MAX_SERIAL_PORTS];
199 6508fe59 bellard
CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
200 9ede2fde aliguori
CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
201 a09db21f bellard
int win2k_install_hack = 0;
202 73822ec8 aliguori
int rtc_td_hack = 0;
203 bb36d470 bellard
int usb_enabled = 0;
204 1b530a6d aurel32
int singlestep = 0;
205 6a00d601 bellard
int smp_cpus = 1;
206 6be68d7e Jes Sorensen
int max_cpus = 0;
207 dc6b1c09 Andre Przywara
int smp_cores = 1;
208 dc6b1c09 Andre Przywara
int smp_threads = 1;
209 821601ea Jes Sorensen
#ifdef CONFIG_VNC
210 73fc9742 ths
const char *vnc_display;
211 821601ea Jes Sorensen
#endif
212 6515b203 bellard
int acpi_enabled = 1;
213 16b29ae1 aliguori
int no_hpet = 0;
214 52ca8d6a bellard
int fd_bootchk = 1;
215 d1beab82 bellard
int no_reboot = 0;
216 b2f76161 aurel32
int no_shutdown = 0;
217 9467cd46 balrog
int cursor_hide = 1;
218 a171fe39 balrog
int graphic_rotate = 0;
219 6b35e7bf Jes Sorensen
uint8_t irq0override = 1;
220 09aaa160 Markus Armbruster
const char *watchdog;
221 2e55e842 Gleb Natapov
QEMUOptionRom option_rom[MAX_OPTION_ROMS];
222 9ae02555 ths
int nb_option_roms;
223 8e71621f pbrook
int semihosting_enabled = 0;
224 2b8f2d41 balrog
int old_param = 0;
225 c35734b2 ths
const char *qemu_name;
226 3780e197 ths
int alt_grab = 0;
227 0ca9f8a4 Dustin Kirkland
int ctrl_grab = 0;
228 66508601 blueswir1
unsigned int nb_prom_envs = 0;
229 66508601 blueswir1
const char *prom_envs[MAX_PROM_ENVS];
230 95387491 Jan Kiszka
int boot_menu;
231 0824d6fc bellard
232 1ca4d09a Gleb Natapov
typedef struct FWBootEntry FWBootEntry;
233 1ca4d09a Gleb Natapov
234 1ca4d09a Gleb Natapov
struct FWBootEntry {
235 1ca4d09a Gleb Natapov
    QTAILQ_ENTRY(FWBootEntry) link;
236 1ca4d09a Gleb Natapov
    int32_t bootindex;
237 1ca4d09a Gleb Natapov
    DeviceState *dev;
238 1ca4d09a Gleb Natapov
    char *suffix;
239 1ca4d09a Gleb Natapov
};
240 1ca4d09a Gleb Natapov
241 1ca4d09a Gleb Natapov
QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
242 1ca4d09a Gleb Natapov
243 268a362c aliguori
int nb_numa_nodes;
244 268a362c aliguori
uint64_t node_mem[MAX_NODES];
245 268a362c aliguori
uint64_t node_cpumask[MAX_NODES];
246 268a362c aliguori
247 9043b62d blueswir1
static QEMUTimer *nographic_timer;
248 ee5605e5 balrog
249 8fcb1b90 blueswir1
uint8_t qemu_uuid[16];
250 8fcb1b90 blueswir1
251 76e30d0f Jan Kiszka
static QEMUBootSetHandler *boot_set_handler;
252 76e30d0f Jan Kiszka
static void *boot_set_opaque;
253 76e30d0f Jan Kiszka
254 fd42deeb Gerd Hoffmann
static NotifierList exit_notifiers =
255 fd42deeb Gerd Hoffmann
    NOTIFIER_LIST_INITIALIZER(exit_notifiers);
256 fd42deeb Gerd Hoffmann
257 4cab946a Gleb Natapov
static NotifierList machine_init_done_notifiers =
258 4cab946a Gleb Natapov
    NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
259 4cab946a Gleb Natapov
260 d745bef8 Blue Swirl
int kvm_allowed = 0;
261 d745bef8 Blue Swirl
uint32_t xen_domid;
262 d745bef8 Blue Swirl
enum xen_mode xen_mode = XEN_EMULATE;
263 d745bef8 Blue Swirl
264 998bbd74 Gerd Hoffmann
static int default_serial = 1;
265 6a5e8b0e Gerd Hoffmann
static int default_parallel = 1;
266 986c5f78 Gerd Hoffmann
static int default_virtcon = 1;
267 abdeed06 Gerd Hoffmann
static int default_monitor = 1;
268 64465297 Gerd Hoffmann
static int default_vga = 1;
269 ac33f8fa Gerd Hoffmann
static int default_floppy = 1;
270 ac33f8fa Gerd Hoffmann
static int default_cdrom = 1;
271 ac33f8fa Gerd Hoffmann
static int default_sdcard = 1;
272 998bbd74 Gerd Hoffmann
273 998bbd74 Gerd Hoffmann
static struct {
274 998bbd74 Gerd Hoffmann
    const char *driver;
275 998bbd74 Gerd Hoffmann
    int *flag;
276 998bbd74 Gerd Hoffmann
} default_list[] = {
277 6a5e8b0e Gerd Hoffmann
    { .driver = "isa-serial",           .flag = &default_serial    },
278 6a5e8b0e Gerd Hoffmann
    { .driver = "isa-parallel",         .flag = &default_parallel  },
279 d8bcbabf Gerd Hoffmann
    { .driver = "isa-fdc",              .flag = &default_floppy    },
280 d8bcbabf Gerd Hoffmann
    { .driver = "ide-drive",            .flag = &default_cdrom     },
281 392ecf54 Amit Shah
    { .driver = "virtio-serial-pci",    .flag = &default_virtcon   },
282 392ecf54 Amit Shah
    { .driver = "virtio-serial-s390",   .flag = &default_virtcon   },
283 392ecf54 Amit Shah
    { .driver = "virtio-serial",        .flag = &default_virtcon   },
284 64465297 Gerd Hoffmann
    { .driver = "VGA",                  .flag = &default_vga       },
285 69fd02ee Gerd Hoffmann
    { .driver = "cirrus-vga",           .flag = &default_vga       },
286 69fd02ee Gerd Hoffmann
    { .driver = "vmware-svga",          .flag = &default_vga       },
287 998bbd74 Gerd Hoffmann
};
288 998bbd74 Gerd Hoffmann
289 998bbd74 Gerd Hoffmann
static int default_driver_check(QemuOpts *opts, void *opaque)
290 998bbd74 Gerd Hoffmann
{
291 998bbd74 Gerd Hoffmann
    const char *driver = qemu_opt_get(opts, "driver");
292 998bbd74 Gerd Hoffmann
    int i;
293 998bbd74 Gerd Hoffmann
294 998bbd74 Gerd Hoffmann
    if (!driver)
295 998bbd74 Gerd Hoffmann
        return 0;
296 998bbd74 Gerd Hoffmann
    for (i = 0; i < ARRAY_SIZE(default_list); i++) {
297 998bbd74 Gerd Hoffmann
        if (strcmp(default_list[i].driver, driver) != 0)
298 998bbd74 Gerd Hoffmann
            continue;
299 998bbd74 Gerd Hoffmann
        *(default_list[i].flag) = 0;
300 998bbd74 Gerd Hoffmann
    }
301 998bbd74 Gerd Hoffmann
    return 0;
302 998bbd74 Gerd Hoffmann
}
303 998bbd74 Gerd Hoffmann
304 0824d6fc bellard
/***********************************************************/
305 8f0056b7 Paolo Bonzini
/* real time host monotonic timer */
306 09b26c5e bellard
307 c4b1fcc0 bellard
/***********************************************************/
308 f6503059 balrog
/* host time/date access */
309 f6503059 balrog
void qemu_get_timedate(struct tm *tm, int offset)
310 f6503059 balrog
{
311 f6503059 balrog
    time_t ti;
312 f6503059 balrog
    struct tm *ret;
313 f6503059 balrog
314 f6503059 balrog
    time(&ti);
315 f6503059 balrog
    ti += offset;
316 f6503059 balrog
    if (rtc_date_offset == -1) {
317 f6503059 balrog
        if (rtc_utc)
318 f6503059 balrog
            ret = gmtime(&ti);
319 f6503059 balrog
        else
320 f6503059 balrog
            ret = localtime(&ti);
321 f6503059 balrog
    } else {
322 f6503059 balrog
        ti -= rtc_date_offset;
323 f6503059 balrog
        ret = gmtime(&ti);
324 f6503059 balrog
    }
325 f6503059 balrog
326 f6503059 balrog
    memcpy(tm, ret, sizeof(struct tm));
327 f6503059 balrog
}
328 f6503059 balrog
329 f6503059 balrog
int qemu_timedate_diff(struct tm *tm)
330 f6503059 balrog
{
331 f6503059 balrog
    time_t seconds;
332 f6503059 balrog
333 f6503059 balrog
    if (rtc_date_offset == -1)
334 f6503059 balrog
        if (rtc_utc)
335 f6503059 balrog
            seconds = mktimegm(tm);
336 f6503059 balrog
        else
337 f6503059 balrog
            seconds = mktime(tm);
338 f6503059 balrog
    else
339 f6503059 balrog
        seconds = mktimegm(tm) + rtc_date_offset;
340 f6503059 balrog
341 f6503059 balrog
    return seconds - time(NULL);
342 f6503059 balrog
}
343 f6503059 balrog
344 80cd3478 Luiz Capitulino
void rtc_change_mon_event(struct tm *tm)
345 80cd3478 Luiz Capitulino
{
346 80cd3478 Luiz Capitulino
    QObject *data;
347 80cd3478 Luiz Capitulino
348 80cd3478 Luiz Capitulino
    data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
349 80cd3478 Luiz Capitulino
    monitor_protocol_event(QEVENT_RTC_CHANGE, data);
350 80cd3478 Luiz Capitulino
    qobject_decref(data);
351 80cd3478 Luiz Capitulino
}
352 80cd3478 Luiz Capitulino
353 1ed2fc1f Jan Kiszka
static void configure_rtc_date_offset(const char *startdate, int legacy)
354 1ed2fc1f Jan Kiszka
{
355 1ed2fc1f Jan Kiszka
    time_t rtc_start_date;
356 1ed2fc1f Jan Kiszka
    struct tm tm;
357 1ed2fc1f Jan Kiszka
358 1ed2fc1f Jan Kiszka
    if (!strcmp(startdate, "now") && legacy) {
359 1ed2fc1f Jan Kiszka
        rtc_date_offset = -1;
360 1ed2fc1f Jan Kiszka
    } else {
361 1ed2fc1f Jan Kiszka
        if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
362 1ed2fc1f Jan Kiszka
                   &tm.tm_year,
363 1ed2fc1f Jan Kiszka
                   &tm.tm_mon,
364 1ed2fc1f Jan Kiszka
                   &tm.tm_mday,
365 1ed2fc1f Jan Kiszka
                   &tm.tm_hour,
366 1ed2fc1f Jan Kiszka
                   &tm.tm_min,
367 1ed2fc1f Jan Kiszka
                   &tm.tm_sec) == 6) {
368 1ed2fc1f Jan Kiszka
            /* OK */
369 1ed2fc1f Jan Kiszka
        } else if (sscanf(startdate, "%d-%d-%d",
370 1ed2fc1f Jan Kiszka
                          &tm.tm_year,
371 1ed2fc1f Jan Kiszka
                          &tm.tm_mon,
372 1ed2fc1f Jan Kiszka
                          &tm.tm_mday) == 3) {
373 1ed2fc1f Jan Kiszka
            tm.tm_hour = 0;
374 1ed2fc1f Jan Kiszka
            tm.tm_min = 0;
375 1ed2fc1f Jan Kiszka
            tm.tm_sec = 0;
376 1ed2fc1f Jan Kiszka
        } else {
377 1ed2fc1f Jan Kiszka
            goto date_fail;
378 1ed2fc1f Jan Kiszka
        }
379 1ed2fc1f Jan Kiszka
        tm.tm_year -= 1900;
380 1ed2fc1f Jan Kiszka
        tm.tm_mon--;
381 1ed2fc1f Jan Kiszka
        rtc_start_date = mktimegm(&tm);
382 1ed2fc1f Jan Kiszka
        if (rtc_start_date == -1) {
383 1ed2fc1f Jan Kiszka
        date_fail:
384 1ed2fc1f Jan Kiszka
            fprintf(stderr, "Invalid date format. Valid formats are:\n"
385 1ed2fc1f Jan Kiszka
                            "'2006-06-17T16:01:21' or '2006-06-17'\n");
386 1ed2fc1f Jan Kiszka
            exit(1);
387 1ed2fc1f Jan Kiszka
        }
388 1ed2fc1f Jan Kiszka
        rtc_date_offset = time(NULL) - rtc_start_date;
389 1ed2fc1f Jan Kiszka
    }
390 1ed2fc1f Jan Kiszka
}
391 1ed2fc1f Jan Kiszka
392 1ed2fc1f Jan Kiszka
static void configure_rtc(QemuOpts *opts)
393 1ed2fc1f Jan Kiszka
{
394 1ed2fc1f Jan Kiszka
    const char *value;
395 1ed2fc1f Jan Kiszka
396 1ed2fc1f Jan Kiszka
    value = qemu_opt_get(opts, "base");
397 1ed2fc1f Jan Kiszka
    if (value) {
398 1ed2fc1f Jan Kiszka
        if (!strcmp(value, "utc")) {
399 1ed2fc1f Jan Kiszka
            rtc_utc = 1;
400 1ed2fc1f Jan Kiszka
        } else if (!strcmp(value, "localtime")) {
401 1ed2fc1f Jan Kiszka
            rtc_utc = 0;
402 1ed2fc1f Jan Kiszka
        } else {
403 1ed2fc1f Jan Kiszka
            configure_rtc_date_offset(value, 0);
404 1ed2fc1f Jan Kiszka
        }
405 1ed2fc1f Jan Kiszka
    }
406 6875204c Jan Kiszka
    value = qemu_opt_get(opts, "clock");
407 6875204c Jan Kiszka
    if (value) {
408 6875204c Jan Kiszka
        if (!strcmp(value, "host")) {
409 6875204c Jan Kiszka
            rtc_clock = host_clock;
410 6875204c Jan Kiszka
        } else if (!strcmp(value, "vm")) {
411 6875204c Jan Kiszka
            rtc_clock = vm_clock;
412 6875204c Jan Kiszka
        } else {
413 6875204c Jan Kiszka
            fprintf(stderr, "qemu: invalid option value '%s'\n", value);
414 6875204c Jan Kiszka
            exit(1);
415 6875204c Jan Kiszka
        }
416 6875204c Jan Kiszka
    }
417 1ed2fc1f Jan Kiszka
    value = qemu_opt_get(opts, "driftfix");
418 1ed2fc1f Jan Kiszka
    if (value) {
419 7e4c0336 Blue Swirl
        if (!strcmp(value, "slew")) {
420 1ed2fc1f Jan Kiszka
            rtc_td_hack = 1;
421 7e4c0336 Blue Swirl
        } else if (!strcmp(value, "none")) {
422 1ed2fc1f Jan Kiszka
            rtc_td_hack = 0;
423 1ed2fc1f Jan Kiszka
        } else {
424 1ed2fc1f Jan Kiszka
            fprintf(stderr, "qemu: invalid option value '%s'\n", value);
425 1ed2fc1f Jan Kiszka
            exit(1);
426 1ed2fc1f Jan Kiszka
        }
427 1ed2fc1f Jan Kiszka
    }
428 1ed2fc1f Jan Kiszka
}
429 1ed2fc1f Jan Kiszka
430 1ae26a18 balrog
/***********************************************************/
431 1ae26a18 balrog
/* Bluetooth support */
432 1ae26a18 balrog
static int nb_hcis;
433 1ae26a18 balrog
static int cur_hci;
434 1ae26a18 balrog
static struct HCIInfo *hci_table[MAX_NICS];
435 dc72ac14 balrog
436 1ae26a18 balrog
static struct bt_vlan_s {
437 1ae26a18 balrog
    struct bt_scatternet_s net;
438 1ae26a18 balrog
    int id;
439 1ae26a18 balrog
    struct bt_vlan_s *next;
440 1ae26a18 balrog
} *first_bt_vlan;
441 1ae26a18 balrog
442 1ae26a18 balrog
/* find or alloc a new bluetooth "VLAN" */
443 674bb261 blueswir1
static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
444 1ae26a18 balrog
{
445 1ae26a18 balrog
    struct bt_vlan_s **pvlan, *vlan;
446 1ae26a18 balrog
    for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
447 1ae26a18 balrog
        if (vlan->id == id)
448 1ae26a18 balrog
            return &vlan->net;
449 1ae26a18 balrog
    }
450 1ae26a18 balrog
    vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
451 1ae26a18 balrog
    vlan->id = id;
452 1ae26a18 balrog
    pvlan = &first_bt_vlan;
453 1ae26a18 balrog
    while (*pvlan != NULL)
454 1ae26a18 balrog
        pvlan = &(*pvlan)->next;
455 1ae26a18 balrog
    *pvlan = vlan;
456 1ae26a18 balrog
    return &vlan->net;
457 1ae26a18 balrog
}
458 1ae26a18 balrog
459 1ae26a18 balrog
static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
460 1ae26a18 balrog
{
461 1ae26a18 balrog
}
462 1ae26a18 balrog
463 1ae26a18 balrog
static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
464 1ae26a18 balrog
{
465 1ae26a18 balrog
    return -ENOTSUP;
466 1ae26a18 balrog
}
467 1ae26a18 balrog
468 1ae26a18 balrog
static struct HCIInfo null_hci = {
469 1ae26a18 balrog
    .cmd_send = null_hci_send,
470 1ae26a18 balrog
    .sco_send = null_hci_send,
471 1ae26a18 balrog
    .acl_send = null_hci_send,
472 1ae26a18 balrog
    .bdaddr_set = null_hci_addr_set,
473 1ae26a18 balrog
};
474 1ae26a18 balrog
475 1ae26a18 balrog
struct HCIInfo *qemu_next_hci(void)
476 1ae26a18 balrog
{
477 1ae26a18 balrog
    if (cur_hci == nb_hcis)
478 1ae26a18 balrog
        return &null_hci;
479 1ae26a18 balrog
480 1ae26a18 balrog
    return hci_table[cur_hci++];
481 1ae26a18 balrog
}
482 1ae26a18 balrog
483 dc72ac14 balrog
static struct HCIInfo *hci_init(const char *str)
484 dc72ac14 balrog
{
485 dc72ac14 balrog
    char *endp;
486 dc72ac14 balrog
    struct bt_scatternet_s *vlan = 0;
487 dc72ac14 balrog
488 dc72ac14 balrog
    if (!strcmp(str, "null"))
489 dc72ac14 balrog
        /* null */
490 dc72ac14 balrog
        return &null_hci;
491 dc72ac14 balrog
    else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
492 dc72ac14 balrog
        /* host[:hciN] */
493 dc72ac14 balrog
        return bt_host_hci(str[4] ? str + 5 : "hci0");
494 dc72ac14 balrog
    else if (!strncmp(str, "hci", 3)) {
495 dc72ac14 balrog
        /* hci[,vlan=n] */
496 dc72ac14 balrog
        if (str[3]) {
497 dc72ac14 balrog
            if (!strncmp(str + 3, ",vlan=", 6)) {
498 dc72ac14 balrog
                vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
499 dc72ac14 balrog
                if (*endp)
500 dc72ac14 balrog
                    vlan = 0;
501 dc72ac14 balrog
            }
502 dc72ac14 balrog
        } else
503 dc72ac14 balrog
            vlan = qemu_find_bt_vlan(0);
504 dc72ac14 balrog
        if (vlan)
505 dc72ac14 balrog
           return bt_new_hci(vlan);
506 dc72ac14 balrog
    }
507 dc72ac14 balrog
508 dc72ac14 balrog
    fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
509 dc72ac14 balrog
510 dc72ac14 balrog
    return 0;
511 dc72ac14 balrog
}
512 dc72ac14 balrog
513 dc72ac14 balrog
static int bt_hci_parse(const char *str)
514 dc72ac14 balrog
{
515 dc72ac14 balrog
    struct HCIInfo *hci;
516 c227f099 Anthony Liguori
    bdaddr_t bdaddr;
517 dc72ac14 balrog
518 dc72ac14 balrog
    if (nb_hcis >= MAX_NICS) {
519 dc72ac14 balrog
        fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
520 dc72ac14 balrog
        return -1;
521 dc72ac14 balrog
    }
522 dc72ac14 balrog
523 dc72ac14 balrog
    hci = hci_init(str);
524 dc72ac14 balrog
    if (!hci)
525 dc72ac14 balrog
        return -1;
526 dc72ac14 balrog
527 dc72ac14 balrog
    bdaddr.b[0] = 0x52;
528 dc72ac14 balrog
    bdaddr.b[1] = 0x54;
529 dc72ac14 balrog
    bdaddr.b[2] = 0x00;
530 dc72ac14 balrog
    bdaddr.b[3] = 0x12;
531 dc72ac14 balrog
    bdaddr.b[4] = 0x34;
532 dc72ac14 balrog
    bdaddr.b[5] = 0x56 + nb_hcis;
533 dc72ac14 balrog
    hci->bdaddr_set(hci, bdaddr.b);
534 dc72ac14 balrog
535 dc72ac14 balrog
    hci_table[nb_hcis++] = hci;
536 dc72ac14 balrog
537 dc72ac14 balrog
    return 0;
538 dc72ac14 balrog
}
539 dc72ac14 balrog
540 dc72ac14 balrog
static void bt_vhci_add(int vlan_id)
541 dc72ac14 balrog
{
542 dc72ac14 balrog
    struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
543 dc72ac14 balrog
544 dc72ac14 balrog
    if (!vlan->slave)
545 dc72ac14 balrog
        fprintf(stderr, "qemu: warning: adding a VHCI to "
546 dc72ac14 balrog
                        "an empty scatternet %i\n", vlan_id);
547 dc72ac14 balrog
548 dc72ac14 balrog
    bt_vhci_init(bt_new_hci(vlan));
549 dc72ac14 balrog
}
550 dc72ac14 balrog
551 dc72ac14 balrog
static struct bt_device_s *bt_device_add(const char *opt)
552 dc72ac14 balrog
{
553 dc72ac14 balrog
    struct bt_scatternet_s *vlan;
554 dc72ac14 balrog
    int vlan_id = 0;
555 dc72ac14 balrog
    char *endp = strstr(opt, ",vlan=");
556 dc72ac14 balrog
    int len = (endp ? endp - opt : strlen(opt)) + 1;
557 dc72ac14 balrog
    char devname[10];
558 dc72ac14 balrog
559 dc72ac14 balrog
    pstrcpy(devname, MIN(sizeof(devname), len), opt);
560 dc72ac14 balrog
561 dc72ac14 balrog
    if (endp) {
562 dc72ac14 balrog
        vlan_id = strtol(endp + 6, &endp, 0);
563 dc72ac14 balrog
        if (*endp) {
564 dc72ac14 balrog
            fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
565 dc72ac14 balrog
            return 0;
566 dc72ac14 balrog
        }
567 dc72ac14 balrog
    }
568 dc72ac14 balrog
569 dc72ac14 balrog
    vlan = qemu_find_bt_vlan(vlan_id);
570 dc72ac14 balrog
571 dc72ac14 balrog
    if (!vlan->slave)
572 dc72ac14 balrog
        fprintf(stderr, "qemu: warning: adding a slave device to "
573 dc72ac14 balrog
                        "an empty scatternet %i\n", vlan_id);
574 dc72ac14 balrog
575 dc72ac14 balrog
    if (!strcmp(devname, "keyboard"))
576 dc72ac14 balrog
        return bt_keyboard_init(vlan);
577 dc72ac14 balrog
578 dc72ac14 balrog
    fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
579 dc72ac14 balrog
    return 0;
580 dc72ac14 balrog
}
581 dc72ac14 balrog
582 dc72ac14 balrog
static int bt_parse(const char *opt)
583 dc72ac14 balrog
{
584 dc72ac14 balrog
    const char *endp, *p;
585 dc72ac14 balrog
    int vlan;
586 dc72ac14 balrog
587 dc72ac14 balrog
    if (strstart(opt, "hci", &endp)) {
588 dc72ac14 balrog
        if (!*endp || *endp == ',') {
589 dc72ac14 balrog
            if (*endp)
590 dc72ac14 balrog
                if (!strstart(endp, ",vlan=", 0))
591 dc72ac14 balrog
                    opt = endp + 1;
592 dc72ac14 balrog
593 dc72ac14 balrog
            return bt_hci_parse(opt);
594 dc72ac14 balrog
       }
595 dc72ac14 balrog
    } else if (strstart(opt, "vhci", &endp)) {
596 dc72ac14 balrog
        if (!*endp || *endp == ',') {
597 dc72ac14 balrog
            if (*endp) {
598 dc72ac14 balrog
                if (strstart(endp, ",vlan=", &p)) {
599 dc72ac14 balrog
                    vlan = strtol(p, (char **) &endp, 0);
600 dc72ac14 balrog
                    if (*endp) {
601 dc72ac14 balrog
                        fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
602 dc72ac14 balrog
                        return 1;
603 dc72ac14 balrog
                    }
604 dc72ac14 balrog
                } else {
605 dc72ac14 balrog
                    fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
606 dc72ac14 balrog
                    return 1;
607 dc72ac14 balrog
                }
608 dc72ac14 balrog
            } else
609 dc72ac14 balrog
                vlan = 0;
610 dc72ac14 balrog
611 dc72ac14 balrog
            bt_vhci_add(vlan);
612 dc72ac14 balrog
            return 0;
613 dc72ac14 balrog
        }
614 dc72ac14 balrog
    } else if (strstart(opt, "device:", &endp))
615 dc72ac14 balrog
        return !bt_device_add(endp);
616 dc72ac14 balrog
617 dc72ac14 balrog
    fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
618 dc72ac14 balrog
    return 1;
619 dc72ac14 balrog
}
620 dc72ac14 balrog
621 1ae26a18 balrog
/***********************************************************/
622 1ae26a18 balrog
/* QEMU Block devices */
623 1ae26a18 balrog
624 2292ddae Markus Armbruster
#define HD_OPTS "media=disk"
625 2292ddae Markus Armbruster
#define CDROM_OPTS "media=cdrom"
626 2292ddae Markus Armbruster
#define FD_OPTS ""
627 2292ddae Markus Armbruster
#define PFLASH_OPTS ""
628 2292ddae Markus Armbruster
#define MTD_OPTS ""
629 2292ddae Markus Armbruster
#define SD_OPTS ""
630 e4bcb14c ths
631 9dfd7c7a Gerd Hoffmann
static int drive_init_func(QemuOpts *opts, void *opaque)
632 9dfd7c7a Gerd Hoffmann
{
633 a803cb8e Markus Armbruster
    int *use_scsi = opaque;
634 9dfd7c7a Gerd Hoffmann
635 319ae529 Markus Armbruster
    return drive_init(opts, *use_scsi) == NULL;
636 9dfd7c7a Gerd Hoffmann
}
637 9dfd7c7a Gerd Hoffmann
638 9dfd7c7a Gerd Hoffmann
static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
639 9dfd7c7a Gerd Hoffmann
{
640 9dfd7c7a Gerd Hoffmann
    if (NULL == qemu_opt_get(opts, "snapshot")) {
641 9dfd7c7a Gerd Hoffmann
        qemu_opt_set(opts, "snapshot", "on");
642 9dfd7c7a Gerd Hoffmann
    }
643 9dfd7c7a Gerd Hoffmann
    return 0;
644 9dfd7c7a Gerd Hoffmann
}
645 9dfd7c7a Gerd Hoffmann
646 4e5d9b57 Markus Armbruster
static void default_drive(int enable, int snapshot, int use_scsi,
647 4e5d9b57 Markus Armbruster
                          BlockInterfaceType type, int index,
648 4e5d9b57 Markus Armbruster
                          const char *optstr)
649 4e5d9b57 Markus Armbruster
{
650 4e5d9b57 Markus Armbruster
    QemuOpts *opts;
651 4e5d9b57 Markus Armbruster
652 4e5d9b57 Markus Armbruster
    if (type == IF_DEFAULT) {
653 4e5d9b57 Markus Armbruster
        type = use_scsi ? IF_SCSI : IF_IDE;
654 4e5d9b57 Markus Armbruster
    }
655 4e5d9b57 Markus Armbruster
656 4e5d9b57 Markus Armbruster
    if (!enable || drive_get_by_index(type, index)) {
657 4e5d9b57 Markus Armbruster
        return;
658 4e5d9b57 Markus Armbruster
    }
659 4e5d9b57 Markus Armbruster
660 4e5d9b57 Markus Armbruster
    opts = drive_add(type, index, NULL, optstr);
661 4e5d9b57 Markus Armbruster
    if (snapshot) {
662 4e5d9b57 Markus Armbruster
        drive_enable_snapshot(opts, NULL);
663 4e5d9b57 Markus Armbruster
    }
664 319ae529 Markus Armbruster
    if (!drive_init(opts, use_scsi)) {
665 4e5d9b57 Markus Armbruster
        exit(1);
666 4e5d9b57 Markus Armbruster
    }
667 4e5d9b57 Markus Armbruster
}
668 4e5d9b57 Markus Armbruster
669 76e30d0f Jan Kiszka
void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
670 76e30d0f Jan Kiszka
{
671 76e30d0f Jan Kiszka
    boot_set_handler = func;
672 76e30d0f Jan Kiszka
    boot_set_opaque = opaque;
673 76e30d0f Jan Kiszka
}
674 76e30d0f Jan Kiszka
675 76e30d0f Jan Kiszka
int qemu_boot_set(const char *boot_devices)
676 76e30d0f Jan Kiszka
{
677 76e30d0f Jan Kiszka
    if (!boot_set_handler) {
678 76e30d0f Jan Kiszka
        return -EINVAL;
679 76e30d0f Jan Kiszka
    }
680 76e30d0f Jan Kiszka
    return boot_set_handler(boot_set_opaque, boot_devices);
681 76e30d0f Jan Kiszka
}
682 76e30d0f Jan Kiszka
683 4e9e9d6e Eduardo Habkost
static void validate_bootdevices(char *devices)
684 ef3adf68 Jan Kiszka
{
685 ef3adf68 Jan Kiszka
    /* We just do some generic consistency checks */
686 ef3adf68 Jan Kiszka
    const char *p;
687 ef3adf68 Jan Kiszka
    int bitmap = 0;
688 ef3adf68 Jan Kiszka
689 ef3adf68 Jan Kiszka
    for (p = devices; *p != '\0'; p++) {
690 ef3adf68 Jan Kiszka
        /* Allowed boot devices are:
691 ef3adf68 Jan Kiszka
         * a-b: floppy disk drives
692 ef3adf68 Jan Kiszka
         * c-f: IDE disk drives
693 ef3adf68 Jan Kiszka
         * g-m: machine implementation dependant drives
694 ef3adf68 Jan Kiszka
         * n-p: network devices
695 ef3adf68 Jan Kiszka
         * It's up to each machine implementation to check if the given boot
696 ef3adf68 Jan Kiszka
         * devices match the actual hardware implementation and firmware
697 ef3adf68 Jan Kiszka
         * features.
698 ef3adf68 Jan Kiszka
         */
699 ef3adf68 Jan Kiszka
        if (*p < 'a' || *p > 'p') {
700 ef3adf68 Jan Kiszka
            fprintf(stderr, "Invalid boot device '%c'\n", *p);
701 ef3adf68 Jan Kiszka
            exit(1);
702 ef3adf68 Jan Kiszka
        }
703 ef3adf68 Jan Kiszka
        if (bitmap & (1 << (*p - 'a'))) {
704 ef3adf68 Jan Kiszka
            fprintf(stderr, "Boot device '%c' was given twice\n", *p);
705 ef3adf68 Jan Kiszka
            exit(1);
706 ef3adf68 Jan Kiszka
        }
707 ef3adf68 Jan Kiszka
        bitmap |= 1 << (*p - 'a');
708 ef3adf68 Jan Kiszka
    }
709 ef3adf68 Jan Kiszka
}
710 ef3adf68 Jan Kiszka
711 e0f084bf Jan Kiszka
static void restore_boot_devices(void *opaque)
712 e0f084bf Jan Kiszka
{
713 e0f084bf Jan Kiszka
    char *standard_boot_devices = opaque;
714 37905d6a Alex Williamson
    static int first = 1;
715 37905d6a Alex Williamson
716 37905d6a Alex Williamson
    /* Restore boot order and remove ourselves after the first boot */
717 37905d6a Alex Williamson
    if (first) {
718 37905d6a Alex Williamson
        first = 0;
719 37905d6a Alex Williamson
        return;
720 37905d6a Alex Williamson
    }
721 e0f084bf Jan Kiszka
722 e0f084bf Jan Kiszka
    qemu_boot_set(standard_boot_devices);
723 e0f084bf Jan Kiszka
724 e0f084bf Jan Kiszka
    qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
725 e0f084bf Jan Kiszka
    qemu_free(standard_boot_devices);
726 e0f084bf Jan Kiszka
}
727 e0f084bf Jan Kiszka
728 1ca4d09a Gleb Natapov
void add_boot_device_path(int32_t bootindex, DeviceState *dev,
729 1ca4d09a Gleb Natapov
                          const char *suffix)
730 1ca4d09a Gleb Natapov
{
731 1ca4d09a Gleb Natapov
    FWBootEntry *node, *i;
732 1ca4d09a Gleb Natapov
733 1ca4d09a Gleb Natapov
    if (bootindex < 0) {
734 1ca4d09a Gleb Natapov
        return;
735 1ca4d09a Gleb Natapov
    }
736 1ca4d09a Gleb Natapov
737 1ca4d09a Gleb Natapov
    assert(dev != NULL || suffix != NULL);
738 1ca4d09a Gleb Natapov
739 1ca4d09a Gleb Natapov
    node = qemu_mallocz(sizeof(FWBootEntry));
740 1ca4d09a Gleb Natapov
    node->bootindex = bootindex;
741 4fef930a Gleb Natapov
    node->suffix = suffix ? qemu_strdup(suffix) : NULL;
742 1ca4d09a Gleb Natapov
    node->dev = dev;
743 1ca4d09a Gleb Natapov
744 1ca4d09a Gleb Natapov
    QTAILQ_FOREACH(i, &fw_boot_order, link) {
745 1ca4d09a Gleb Natapov
        if (i->bootindex == bootindex) {
746 1ca4d09a Gleb Natapov
            fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
747 1ca4d09a Gleb Natapov
            exit(1);
748 1ca4d09a Gleb Natapov
        } else if (i->bootindex < bootindex) {
749 1ca4d09a Gleb Natapov
            continue;
750 1ca4d09a Gleb Natapov
        }
751 1ca4d09a Gleb Natapov
        QTAILQ_INSERT_BEFORE(i, node, link);
752 1ca4d09a Gleb Natapov
        return;
753 1ca4d09a Gleb Natapov
    }
754 1ca4d09a Gleb Natapov
    QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
755 1ca4d09a Gleb Natapov
}
756 1ca4d09a Gleb Natapov
757 962630f2 Gleb Natapov
/*
758 962630f2 Gleb Natapov
 * This function returns null terminated string that consist of new line
759 962630f2 Gleb Natapov
 * separated device pathes.
760 962630f2 Gleb Natapov
 *
761 962630f2 Gleb Natapov
 * memory pointed by "size" is assigned total length of the array in bytes
762 962630f2 Gleb Natapov
 *
763 962630f2 Gleb Natapov
 */
764 962630f2 Gleb Natapov
char *get_boot_devices_list(uint32_t *size)
765 962630f2 Gleb Natapov
{
766 962630f2 Gleb Natapov
    FWBootEntry *i;
767 962630f2 Gleb Natapov
    uint32_t total = 0;
768 962630f2 Gleb Natapov
    char *list = NULL;
769 962630f2 Gleb Natapov
770 962630f2 Gleb Natapov
    QTAILQ_FOREACH(i, &fw_boot_order, link) {
771 962630f2 Gleb Natapov
        char *devpath = NULL, *bootpath;
772 962630f2 Gleb Natapov
        int len;
773 962630f2 Gleb Natapov
774 962630f2 Gleb Natapov
        if (i->dev) {
775 962630f2 Gleb Natapov
            devpath = qdev_get_fw_dev_path(i->dev);
776 962630f2 Gleb Natapov
            assert(devpath);
777 962630f2 Gleb Natapov
        }
778 962630f2 Gleb Natapov
779 962630f2 Gleb Natapov
        if (i->suffix && devpath) {
780 4fd37a98 Blue Swirl
            size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
781 4fd37a98 Blue Swirl
782 4fd37a98 Blue Swirl
            bootpath = qemu_malloc(bootpathlen);
783 4fd37a98 Blue Swirl
            snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
784 962630f2 Gleb Natapov
            qemu_free(devpath);
785 962630f2 Gleb Natapov
        } else if (devpath) {
786 962630f2 Gleb Natapov
            bootpath = devpath;
787 962630f2 Gleb Natapov
        } else {
788 4fef930a Gleb Natapov
            bootpath = qemu_strdup(i->suffix);
789 962630f2 Gleb Natapov
            assert(bootpath);
790 962630f2 Gleb Natapov
        }
791 962630f2 Gleb Natapov
792 962630f2 Gleb Natapov
        if (total) {
793 962630f2 Gleb Natapov
            list[total-1] = '\n';
794 962630f2 Gleb Natapov
        }
795 962630f2 Gleb Natapov
        len = strlen(bootpath) + 1;
796 962630f2 Gleb Natapov
        list = qemu_realloc(list, total + len);
797 962630f2 Gleb Natapov
        memcpy(&list[total], bootpath, len);
798 962630f2 Gleb Natapov
        total += len;
799 962630f2 Gleb Natapov
        qemu_free(bootpath);
800 962630f2 Gleb Natapov
    }
801 962630f2 Gleb Natapov
802 962630f2 Gleb Natapov
    *size = total;
803 962630f2 Gleb Natapov
804 962630f2 Gleb Natapov
    return list;
805 962630f2 Gleb Natapov
}
806 962630f2 Gleb Natapov
807 268a362c aliguori
static void numa_add(const char *optarg)
808 268a362c aliguori
{
809 268a362c aliguori
    char option[128];
810 268a362c aliguori
    char *endptr;
811 268a362c aliguori
    unsigned long long value, endvalue;
812 268a362c aliguori
    int nodenr;
813 268a362c aliguori
814 268a362c aliguori
    optarg = get_opt_name(option, 128, optarg, ',') + 1;
815 268a362c aliguori
    if (!strcmp(option, "node")) {
816 268a362c aliguori
        if (get_param_value(option, 128, "nodeid", optarg) == 0) {
817 268a362c aliguori
            nodenr = nb_numa_nodes;
818 268a362c aliguori
        } else {
819 268a362c aliguori
            nodenr = strtoull(option, NULL, 10);
820 268a362c aliguori
        }
821 268a362c aliguori
822 268a362c aliguori
        if (get_param_value(option, 128, "mem", optarg) == 0) {
823 268a362c aliguori
            node_mem[nodenr] = 0;
824 268a362c aliguori
        } else {
825 70b4f4bb Jes Sorensen
            int64_t sval;
826 9f9b17a4 Jes Sorensen
            sval = strtosz(option, NULL);
827 9f9b17a4 Jes Sorensen
            if (sval < 0) {
828 9f9b17a4 Jes Sorensen
                fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
829 9f9b17a4 Jes Sorensen
                exit(1);
830 268a362c aliguori
            }
831 9f9b17a4 Jes Sorensen
            node_mem[nodenr] = sval;
832 268a362c aliguori
        }
833 268a362c aliguori
        if (get_param_value(option, 128, "cpus", optarg) == 0) {
834 268a362c aliguori
            node_cpumask[nodenr] = 0;
835 268a362c aliguori
        } else {
836 268a362c aliguori
            value = strtoull(option, &endptr, 10);
837 268a362c aliguori
            if (value >= 64) {
838 268a362c aliguori
                value = 63;
839 268a362c aliguori
                fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
840 268a362c aliguori
            } else {
841 268a362c aliguori
                if (*endptr == '-') {
842 268a362c aliguori
                    endvalue = strtoull(endptr+1, &endptr, 10);
843 268a362c aliguori
                    if (endvalue >= 63) {
844 268a362c aliguori
                        endvalue = 62;
845 268a362c aliguori
                        fprintf(stderr,
846 268a362c aliguori
                            "only 63 CPUs in NUMA mode supported.\n");
847 268a362c aliguori
                    }
848 0dfbd514 Paolo Bonzini
                    value = (2ULL << endvalue) - (1ULL << value);
849 268a362c aliguori
                } else {
850 0dfbd514 Paolo Bonzini
                    value = 1ULL << value;
851 268a362c aliguori
                }
852 268a362c aliguori
            }
853 268a362c aliguori
            node_cpumask[nodenr] = value;
854 268a362c aliguori
        }
855 268a362c aliguori
        nb_numa_nodes++;
856 268a362c aliguori
    }
857 268a362c aliguori
    return;
858 268a362c aliguori
}
859 268a362c aliguori
860 dc6b1c09 Andre Przywara
static void smp_parse(const char *optarg)
861 dc6b1c09 Andre Przywara
{
862 dc6b1c09 Andre Przywara
    int smp, sockets = 0, threads = 0, cores = 0;
863 dc6b1c09 Andre Przywara
    char *endptr;
864 dc6b1c09 Andre Przywara
    char option[128];
865 dc6b1c09 Andre Przywara
866 dc6b1c09 Andre Przywara
    smp = strtoul(optarg, &endptr, 10);
867 dc6b1c09 Andre Przywara
    if (endptr != optarg) {
868 dc6b1c09 Andre Przywara
        if (*endptr == ',') {
869 dc6b1c09 Andre Przywara
            endptr++;
870 dc6b1c09 Andre Przywara
        }
871 dc6b1c09 Andre Przywara
    }
872 dc6b1c09 Andre Przywara
    if (get_param_value(option, 128, "sockets", endptr) != 0)
873 dc6b1c09 Andre Przywara
        sockets = strtoull(option, NULL, 10);
874 dc6b1c09 Andre Przywara
    if (get_param_value(option, 128, "cores", endptr) != 0)
875 dc6b1c09 Andre Przywara
        cores = strtoull(option, NULL, 10);
876 dc6b1c09 Andre Przywara
    if (get_param_value(option, 128, "threads", endptr) != 0)
877 dc6b1c09 Andre Przywara
        threads = strtoull(option, NULL, 10);
878 dc6b1c09 Andre Przywara
    if (get_param_value(option, 128, "maxcpus", endptr) != 0)
879 dc6b1c09 Andre Przywara
        max_cpus = strtoull(option, NULL, 10);
880 dc6b1c09 Andre Przywara
881 dc6b1c09 Andre Przywara
    /* compute missing values, prefer sockets over cores over threads */
882 dc6b1c09 Andre Przywara
    if (smp == 0 || sockets == 0) {
883 dc6b1c09 Andre Przywara
        sockets = sockets > 0 ? sockets : 1;
884 dc6b1c09 Andre Przywara
        cores = cores > 0 ? cores : 1;
885 dc6b1c09 Andre Przywara
        threads = threads > 0 ? threads : 1;
886 dc6b1c09 Andre Przywara
        if (smp == 0) {
887 dc6b1c09 Andre Przywara
            smp = cores * threads * sockets;
888 dc6b1c09 Andre Przywara
        }
889 dc6b1c09 Andre Przywara
    } else {
890 dc6b1c09 Andre Przywara
        if (cores == 0) {
891 dc6b1c09 Andre Przywara
            threads = threads > 0 ? threads : 1;
892 dc6b1c09 Andre Przywara
            cores = smp / (sockets * threads);
893 dc6b1c09 Andre Przywara
        } else {
894 dca98169 Joel Schopp
            threads = smp / (cores * sockets);
895 dc6b1c09 Andre Przywara
        }
896 dc6b1c09 Andre Przywara
    }
897 dc6b1c09 Andre Przywara
    smp_cpus = smp;
898 dc6b1c09 Andre Przywara
    smp_cores = cores > 0 ? cores : 1;
899 dc6b1c09 Andre Przywara
    smp_threads = threads > 0 ? threads : 1;
900 dc6b1c09 Andre Przywara
    if (max_cpus == 0)
901 dc6b1c09 Andre Przywara
        max_cpus = smp_cpus;
902 dc6b1c09 Andre Przywara
}
903 dc6b1c09 Andre Przywara
904 330d0414 bellard
/***********************************************************/
905 a594cfbf bellard
/* USB devices */
906 a594cfbf bellard
907 fb08000c Markus Armbruster
static int usb_device_add(const char *devname)
908 a594cfbf bellard
{
909 a594cfbf bellard
    const char *p;
910 a5d2f727 Gerd Hoffmann
    USBDevice *dev = NULL;
911 a594cfbf bellard
912 a5d2f727 Gerd Hoffmann
    if (!usb_enabled)
913 a594cfbf bellard
        return -1;
914 a594cfbf bellard
915 0958b4cc Gerd Hoffmann
    /* drivers with .usbdevice_name entry in USBDeviceInfo */
916 0958b4cc Gerd Hoffmann
    dev = usbdevice_create(devname);
917 0958b4cc Gerd Hoffmann
    if (dev)
918 0958b4cc Gerd Hoffmann
        goto done;
919 0958b4cc Gerd Hoffmann
920 a5d2f727 Gerd Hoffmann
    /* the other ones */
921 a594cfbf bellard
    if (strstart(devname, "host:", &p)) {
922 a594cfbf bellard
        dev = usb_host_device_open(p);
923 dc72ac14 balrog
    } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
924 dc72ac14 balrog
        dev = usb_bt_init(devname[2] ? hci_init(p) :
925 dc72ac14 balrog
                        bt_new_hci(qemu_find_bt_vlan(0)));
926 a594cfbf bellard
    } else {
927 a594cfbf bellard
        return -1;
928 a594cfbf bellard
    }
929 0d92ed30 pbrook
    if (!dev)
930 0d92ed30 pbrook
        return -1;
931 0d92ed30 pbrook
932 a5d2f727 Gerd Hoffmann
done:
933 a594cfbf bellard
    return 0;
934 a594cfbf bellard
}
935 a594cfbf bellard
936 1f3870ab aliguori
static int usb_device_del(const char *devname)
937 1f3870ab aliguori
{
938 1f3870ab aliguori
    int bus_num, addr;
939 1f3870ab aliguori
    const char *p;
940 1f3870ab aliguori
941 5d0c5750 aliguori
    if (strstart(devname, "host:", &p))
942 5d0c5750 aliguori
        return usb_host_device_close(p);
943 5d0c5750 aliguori
944 a5d2f727 Gerd Hoffmann
    if (!usb_enabled)
945 1f3870ab aliguori
        return -1;
946 1f3870ab aliguori
947 1f3870ab aliguori
    p = strchr(devname, '.');
948 1f3870ab aliguori
    if (!p)
949 1f3870ab aliguori
        return -1;
950 1f3870ab aliguori
    bus_num = strtoul(devname, NULL, 0);
951 1f3870ab aliguori
    addr = strtoul(p + 1, NULL, 0);
952 1f3870ab aliguori
953 a5d2f727 Gerd Hoffmann
    return usb_device_delete_addr(bus_num, addr);
954 1f3870ab aliguori
}
955 1f3870ab aliguori
956 bd3c948d Gerd Hoffmann
static int usb_parse(const char *cmdline)
957 bd3c948d Gerd Hoffmann
{
958 59d1c1c2 Scott Tsai
    int r;
959 fb08000c Markus Armbruster
    r = usb_device_add(cmdline);
960 59d1c1c2 Scott Tsai
    if (r < 0) {
961 59d1c1c2 Scott Tsai
        fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
962 59d1c1c2 Scott Tsai
    }
963 59d1c1c2 Scott Tsai
    return r;
964 bd3c948d Gerd Hoffmann
}
965 bd3c948d Gerd Hoffmann
966 d54908a5 Luiz Capitulino
void do_usb_add(Monitor *mon, const QDict *qdict)
967 a594cfbf bellard
{
968 59d1c1c2 Scott Tsai
    const char *devname = qdict_get_str(qdict, "devname");
969 fb08000c Markus Armbruster
    if (usb_device_add(devname) < 0) {
970 1ecda02b Markus Armbruster
        error_report("could not add USB device '%s'", devname);
971 59d1c1c2 Scott Tsai
    }
972 a594cfbf bellard
}
973 a594cfbf bellard
974 d54908a5 Luiz Capitulino
void do_usb_del(Monitor *mon, const QDict *qdict)
975 a594cfbf bellard
{
976 59d1c1c2 Scott Tsai
    const char *devname = qdict_get_str(qdict, "devname");
977 59d1c1c2 Scott Tsai
    if (usb_device_del(devname) < 0) {
978 1ecda02b Markus Armbruster
        error_report("could not delete USB device '%s'", devname);
979 59d1c1c2 Scott Tsai
    }
980 a594cfbf bellard
}
981 a594cfbf bellard
982 f7cce898 bellard
/***********************************************************/
983 201a51fc balrog
/* PCMCIA/Cardbus */
984 201a51fc balrog
985 201a51fc balrog
static struct pcmcia_socket_entry_s {
986 bc24a225 Paul Brook
    PCMCIASocket *socket;
987 201a51fc balrog
    struct pcmcia_socket_entry_s *next;
988 201a51fc balrog
} *pcmcia_sockets = 0;
989 201a51fc balrog
990 bc24a225 Paul Brook
void pcmcia_socket_register(PCMCIASocket *socket)
991 201a51fc balrog
{
992 201a51fc balrog
    struct pcmcia_socket_entry_s *entry;
993 201a51fc balrog
994 201a51fc balrog
    entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
995 201a51fc balrog
    entry->socket = socket;
996 201a51fc balrog
    entry->next = pcmcia_sockets;
997 201a51fc balrog
    pcmcia_sockets = entry;
998 201a51fc balrog
}
999 201a51fc balrog
1000 bc24a225 Paul Brook
void pcmcia_socket_unregister(PCMCIASocket *socket)
1001 201a51fc balrog
{
1002 201a51fc balrog
    struct pcmcia_socket_entry_s *entry, **ptr;
1003 201a51fc balrog
1004 201a51fc balrog
    ptr = &pcmcia_sockets;
1005 201a51fc balrog
    for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1006 201a51fc balrog
        if (entry->socket == socket) {
1007 201a51fc balrog
            *ptr = entry->next;
1008 201a51fc balrog
            qemu_free(entry);
1009 201a51fc balrog
        }
1010 201a51fc balrog
}
1011 201a51fc balrog
1012 376253ec aliguori
void pcmcia_info(Monitor *mon)
1013 201a51fc balrog
{
1014 201a51fc balrog
    struct pcmcia_socket_entry_s *iter;
1015 376253ec aliguori
1016 201a51fc balrog
    if (!pcmcia_sockets)
1017 376253ec aliguori
        monitor_printf(mon, "No PCMCIA sockets\n");
1018 201a51fc balrog
1019 201a51fc balrog
    for (iter = pcmcia_sockets; iter; iter = iter->next)
1020 376253ec aliguori
        monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1021 376253ec aliguori
                       iter->socket->attached ? iter->socket->card_string :
1022 376253ec aliguori
                       "Empty");
1023 201a51fc balrog
}
1024 201a51fc balrog
1025 201a51fc balrog
/***********************************************************/
1026 8a7ddc38 bellard
/* I/O handling */
1027 0824d6fc bellard
1028 c4b1fcc0 bellard
typedef struct IOHandlerRecord {
1029 c4b1fcc0 bellard
    int fd;
1030 7b27a769 Juan Quintela
    IOCanReadHandler *fd_read_poll;
1031 7c9d8e07 bellard
    IOHandler *fd_read;
1032 7c9d8e07 bellard
    IOHandler *fd_write;
1033 cafffd40 ths
    int deleted;
1034 c4b1fcc0 bellard
    void *opaque;
1035 c4b1fcc0 bellard
    /* temporary data */
1036 c4b1fcc0 bellard
    struct pollfd *ufd;
1037 31d4ee6c Juan Quintela
    QLIST_ENTRY(IOHandlerRecord) next;
1038 c4b1fcc0 bellard
} IOHandlerRecord;
1039 c4b1fcc0 bellard
1040 31d4ee6c Juan Quintela
static QLIST_HEAD(, IOHandlerRecord) io_handlers =
1041 31d4ee6c Juan Quintela
    QLIST_HEAD_INITIALIZER(io_handlers);
1042 31d4ee6c Juan Quintela
1043 c4b1fcc0 bellard
1044 7c9d8e07 bellard
/* XXX: fd_read_poll should be suppressed, but an API change is
1045 7c9d8e07 bellard
   necessary in the character devices to suppress fd_can_read(). */
1046 5fafdf24 ths
int qemu_set_fd_handler2(int fd,
1047 7b27a769 Juan Quintela
                         IOCanReadHandler *fd_read_poll,
1048 5fafdf24 ths
                         IOHandler *fd_read,
1049 5fafdf24 ths
                         IOHandler *fd_write,
1050 7c9d8e07 bellard
                         void *opaque)
1051 c4b1fcc0 bellard
{
1052 31d4ee6c Juan Quintela
    IOHandlerRecord *ioh;
1053 c4b1fcc0 bellard
1054 7c9d8e07 bellard
    if (!fd_read && !fd_write) {
1055 31d4ee6c Juan Quintela
        QLIST_FOREACH(ioh, &io_handlers, next) {
1056 7c9d8e07 bellard
            if (ioh->fd == fd) {
1057 cafffd40 ths
                ioh->deleted = 1;
1058 7c9d8e07 bellard
                break;
1059 7c9d8e07 bellard
            }
1060 7c9d8e07 bellard
        }
1061 7c9d8e07 bellard
    } else {
1062 31d4ee6c Juan Quintela
        QLIST_FOREACH(ioh, &io_handlers, next) {
1063 7c9d8e07 bellard
            if (ioh->fd == fd)
1064 7c9d8e07 bellard
                goto found;
1065 7c9d8e07 bellard
        }
1066 7c9d8e07 bellard
        ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1067 31d4ee6c Juan Quintela
        QLIST_INSERT_HEAD(&io_handlers, ioh, next);
1068 7c9d8e07 bellard
    found:
1069 7c9d8e07 bellard
        ioh->fd = fd;
1070 7c9d8e07 bellard
        ioh->fd_read_poll = fd_read_poll;
1071 7c9d8e07 bellard
        ioh->fd_read = fd_read;
1072 7c9d8e07 bellard
        ioh->fd_write = fd_write;
1073 7c9d8e07 bellard
        ioh->opaque = opaque;
1074 cafffd40 ths
        ioh->deleted = 0;
1075 7c9d8e07 bellard
    }
1076 c4b1fcc0 bellard
    return 0;
1077 c4b1fcc0 bellard
}
1078 c4b1fcc0 bellard
1079 5fafdf24 ths
int qemu_set_fd_handler(int fd,
1080 5fafdf24 ths
                        IOHandler *fd_read,
1081 5fafdf24 ths
                        IOHandler *fd_write,
1082 7c9d8e07 bellard
                        void *opaque)
1083 8a7ddc38 bellard
{
1084 7c9d8e07 bellard
    return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1085 8a7ddc38 bellard
}
1086 8a7ddc38 bellard
1087 8a7ddc38 bellard
/***********************************************************/
1088 cc1daa40 bellard
/* machine registration */
1089 cc1daa40 bellard
1090 bdaf78e0 blueswir1
static QEMUMachine *first_machine = NULL;
1091 6f338c34 aliguori
QEMUMachine *current_machine = NULL;
1092 cc1daa40 bellard
1093 cc1daa40 bellard
int qemu_register_machine(QEMUMachine *m)
1094 cc1daa40 bellard
{
1095 cc1daa40 bellard
    QEMUMachine **pm;
1096 cc1daa40 bellard
    pm = &first_machine;
1097 cc1daa40 bellard
    while (*pm != NULL)
1098 cc1daa40 bellard
        pm = &(*pm)->next;
1099 cc1daa40 bellard
    m->next = NULL;
1100 cc1daa40 bellard
    *pm = m;
1101 cc1daa40 bellard
    return 0;
1102 cc1daa40 bellard
}
1103 cc1daa40 bellard
1104 9596ebb7 pbrook
static QEMUMachine *find_machine(const char *name)
1105 cc1daa40 bellard
{
1106 cc1daa40 bellard
    QEMUMachine *m;
1107 cc1daa40 bellard
1108 cc1daa40 bellard
    for(m = first_machine; m != NULL; m = m->next) {
1109 cc1daa40 bellard
        if (!strcmp(m->name, name))
1110 cc1daa40 bellard
            return m;
1111 3f6599e6 Mark McLoughlin
        if (m->alias && !strcmp(m->alias, name))
1112 3f6599e6 Mark McLoughlin
            return m;
1113 cc1daa40 bellard
    }
1114 cc1daa40 bellard
    return NULL;
1115 cc1daa40 bellard
}
1116 cc1daa40 bellard
1117 0c257437 Anthony Liguori
static QEMUMachine *find_default_machine(void)
1118 0c257437 Anthony Liguori
{
1119 0c257437 Anthony Liguori
    QEMUMachine *m;
1120 0c257437 Anthony Liguori
1121 0c257437 Anthony Liguori
    for(m = first_machine; m != NULL; m = m->next) {
1122 0c257437 Anthony Liguori
        if (m->is_default) {
1123 0c257437 Anthony Liguori
            return m;
1124 0c257437 Anthony Liguori
        }
1125 0c257437 Anthony Liguori
    }
1126 0c257437 Anthony Liguori
    return NULL;
1127 0c257437 Anthony Liguori
}
1128 0c257437 Anthony Liguori
1129 cc1daa40 bellard
/***********************************************************/
1130 8a7ddc38 bellard
/* main execution loop */
1131 8a7ddc38 bellard
1132 9596ebb7 pbrook
static void gui_update(void *opaque)
1133 8a7ddc38 bellard
{
1134 7d957bd8 aliguori
    uint64_t interval = GUI_REFRESH_INTERVAL;
1135 740733bb ths
    DisplayState *ds = opaque;
1136 7d957bd8 aliguori
    DisplayChangeListener *dcl = ds->listeners;
1137 7d957bd8 aliguori
1138 62a2744c Sheng Yang
    qemu_flush_coalesced_mmio_buffer();
1139 7d957bd8 aliguori
    dpy_refresh(ds);
1140 7d957bd8 aliguori
1141 7d957bd8 aliguori
    while (dcl != NULL) {
1142 7d957bd8 aliguori
        if (dcl->gui_timer_interval &&
1143 7d957bd8 aliguori
            dcl->gui_timer_interval < interval)
1144 7d957bd8 aliguori
            interval = dcl->gui_timer_interval;
1145 7d957bd8 aliguori
        dcl = dcl->next;
1146 7d957bd8 aliguori
    }
1147 7bd427d8 Paolo Bonzini
    qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1148 8a7ddc38 bellard
}
1149 8a7ddc38 bellard
1150 9043b62d blueswir1
static void nographic_update(void *opaque)
1151 9043b62d blueswir1
{
1152 9043b62d blueswir1
    uint64_t interval = GUI_REFRESH_INTERVAL;
1153 9043b62d blueswir1
1154 62a2744c Sheng Yang
    qemu_flush_coalesced_mmio_buffer();
1155 7bd427d8 Paolo Bonzini
    qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
1156 9043b62d blueswir1
}
1157 9043b62d blueswir1
1158 0bd48850 bellard
struct vm_change_state_entry {
1159 0bd48850 bellard
    VMChangeStateHandler *cb;
1160 0bd48850 bellard
    void *opaque;
1161 72cf2d4f Blue Swirl
    QLIST_ENTRY (vm_change_state_entry) entries;
1162 0bd48850 bellard
};
1163 0bd48850 bellard
1164 72cf2d4f Blue Swirl
static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1165 0bd48850 bellard
1166 0bd48850 bellard
VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1167 0bd48850 bellard
                                                     void *opaque)
1168 0bd48850 bellard
{
1169 0bd48850 bellard
    VMChangeStateEntry *e;
1170 0bd48850 bellard
1171 0bd48850 bellard
    e = qemu_mallocz(sizeof (*e));
1172 0bd48850 bellard
1173 0bd48850 bellard
    e->cb = cb;
1174 0bd48850 bellard
    e->opaque = opaque;
1175 72cf2d4f Blue Swirl
    QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1176 0bd48850 bellard
    return e;
1177 0bd48850 bellard
}
1178 0bd48850 bellard
1179 0bd48850 bellard
void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1180 0bd48850 bellard
{
1181 72cf2d4f Blue Swirl
    QLIST_REMOVE (e, entries);
1182 0bd48850 bellard
    qemu_free (e);
1183 0bd48850 bellard
}
1184 0bd48850 bellard
1185 296af7c9 Blue Swirl
void vm_state_notify(int running, int reason)
1186 0bd48850 bellard
{
1187 0bd48850 bellard
    VMChangeStateEntry *e;
1188 0bd48850 bellard
1189 94b0b5ff Stefan Hajnoczi
    trace_vm_state_notify(running, reason);
1190 94b0b5ff Stefan Hajnoczi
1191 0bd48850 bellard
    for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1192 9781e040 aliguori
        e->cb(e->opaque, running, reason);
1193 0bd48850 bellard
    }
1194 0bd48850 bellard
}
1195 0bd48850 bellard
1196 8a7ddc38 bellard
void vm_start(void)
1197 8a7ddc38 bellard
{
1198 8a7ddc38 bellard
    if (!vm_running) {
1199 8a7ddc38 bellard
        cpu_enable_ticks();
1200 8a7ddc38 bellard
        vm_running = 1;
1201 9781e040 aliguori
        vm_state_notify(1, 0);
1202 d6dc3d42 aliguori
        resume_all_vcpus();
1203 6ed2c484 Luiz Capitulino
        monitor_protocol_event(QEVENT_RESUME, NULL);
1204 8a7ddc38 bellard
    }
1205 8a7ddc38 bellard
}
1206 8a7ddc38 bellard
1207 bb0c6722 bellard
/* reset/shutdown handler */
1208 bb0c6722 bellard
1209 bb0c6722 bellard
typedef struct QEMUResetEntry {
1210 72cf2d4f Blue Swirl
    QTAILQ_ENTRY(QEMUResetEntry) entry;
1211 bb0c6722 bellard
    QEMUResetHandler *func;
1212 bb0c6722 bellard
    void *opaque;
1213 bb0c6722 bellard
} QEMUResetEntry;
1214 bb0c6722 bellard
1215 72cf2d4f Blue Swirl
static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1216 72cf2d4f Blue Swirl
    QTAILQ_HEAD_INITIALIZER(reset_handlers);
1217 bb0c6722 bellard
static int reset_requested;
1218 bb0c6722 bellard
static int shutdown_requested;
1219 3475187d bellard
static int powerdown_requested;
1220 8cf71710 Jan Kiszka
static int debug_requested;
1221 8cf71710 Jan Kiszka
static int vmstop_requested;
1222 bb0c6722 bellard
1223 cf7a2fe2 aurel32
int qemu_shutdown_requested(void)
1224 cf7a2fe2 aurel32
{
1225 cf7a2fe2 aurel32
    int r = shutdown_requested;
1226 cf7a2fe2 aurel32
    shutdown_requested = 0;
1227 cf7a2fe2 aurel32
    return r;
1228 cf7a2fe2 aurel32
}
1229 cf7a2fe2 aurel32
1230 cf7a2fe2 aurel32
int qemu_reset_requested(void)
1231 cf7a2fe2 aurel32
{
1232 cf7a2fe2 aurel32
    int r = reset_requested;
1233 cf7a2fe2 aurel32
    reset_requested = 0;
1234 cf7a2fe2 aurel32
    return r;
1235 cf7a2fe2 aurel32
}
1236 cf7a2fe2 aurel32
1237 cf7a2fe2 aurel32
int qemu_powerdown_requested(void)
1238 cf7a2fe2 aurel32
{
1239 cf7a2fe2 aurel32
    int r = powerdown_requested;
1240 cf7a2fe2 aurel32
    powerdown_requested = 0;
1241 cf7a2fe2 aurel32
    return r;
1242 cf7a2fe2 aurel32
}
1243 cf7a2fe2 aurel32
1244 e568902a aliguori
static int qemu_debug_requested(void)
1245 e568902a aliguori
{
1246 e568902a aliguori
    int r = debug_requested;
1247 e568902a aliguori
    debug_requested = 0;
1248 e568902a aliguori
    return r;
1249 e568902a aliguori
}
1250 e568902a aliguori
1251 6e29f5da aliguori
static int qemu_vmstop_requested(void)
1252 6e29f5da aliguori
{
1253 6e29f5da aliguori
    int r = vmstop_requested;
1254 6e29f5da aliguori
    vmstop_requested = 0;
1255 6e29f5da aliguori
    return r;
1256 6e29f5da aliguori
}
1257 6e29f5da aliguori
1258 a08d4367 Jan Kiszka
void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1259 bb0c6722 bellard
{
1260 55ddfe8e Jan Kiszka
    QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1261 bb0c6722 bellard
1262 bb0c6722 bellard
    re->func = func;
1263 bb0c6722 bellard
    re->opaque = opaque;
1264 72cf2d4f Blue Swirl
    QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1265 bb0c6722 bellard
}
1266 bb0c6722 bellard
1267 dda9b29f Jan Kiszka
void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1268 bb0c6722 bellard
{
1269 bb0c6722 bellard
    QEMUResetEntry *re;
1270 bb0c6722 bellard
1271 72cf2d4f Blue Swirl
    QTAILQ_FOREACH(re, &reset_handlers, entry) {
1272 dda9b29f Jan Kiszka
        if (re->func == func && re->opaque == opaque) {
1273 72cf2d4f Blue Swirl
            QTAILQ_REMOVE(&reset_handlers, re, entry);
1274 dda9b29f Jan Kiszka
            qemu_free(re);
1275 dda9b29f Jan Kiszka
            return;
1276 dda9b29f Jan Kiszka
        }
1277 dda9b29f Jan Kiszka
    }
1278 dda9b29f Jan Kiszka
}
1279 dda9b29f Jan Kiszka
1280 dda9b29f Jan Kiszka
void qemu_system_reset(void)
1281 dda9b29f Jan Kiszka
{
1282 dda9b29f Jan Kiszka
    QEMUResetEntry *re, *nre;
1283 dda9b29f Jan Kiszka
1284 dda9b29f Jan Kiszka
    /* reset all devices */
1285 72cf2d4f Blue Swirl
    QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1286 bb0c6722 bellard
        re->func(re->opaque);
1287 bb0c6722 bellard
    }
1288 81d9b784 Luiz Capitulino
    monitor_protocol_event(QEVENT_RESET, NULL);
1289 ea375f9a Jan Kiszka
    cpu_synchronize_all_post_reset();
1290 bb0c6722 bellard
}
1291 bb0c6722 bellard
1292 bb0c6722 bellard
void qemu_system_reset_request(void)
1293 bb0c6722 bellard
{
1294 d1beab82 bellard
    if (no_reboot) {
1295 d1beab82 bellard
        shutdown_requested = 1;
1296 d1beab82 bellard
    } else {
1297 d1beab82 bellard
        reset_requested = 1;
1298 d1beab82 bellard
    }
1299 b4a3d965 Jan Kiszka
    cpu_stop_current();
1300 d9f75a4e aliguori
    qemu_notify_event();
1301 bb0c6722 bellard
}
1302 bb0c6722 bellard
1303 bb0c6722 bellard
void qemu_system_shutdown_request(void)
1304 bb0c6722 bellard
{
1305 bb0c6722 bellard
    shutdown_requested = 1;
1306 d9f75a4e aliguori
    qemu_notify_event();
1307 bb0c6722 bellard
}
1308 bb0c6722 bellard
1309 3475187d bellard
void qemu_system_powerdown_request(void)
1310 3475187d bellard
{
1311 3475187d bellard
    powerdown_requested = 1;
1312 d9f75a4e aliguori
    qemu_notify_event();
1313 d9f75a4e aliguori
}
1314 d9f75a4e aliguori
1315 8cf71710 Jan Kiszka
void qemu_system_debug_request(void)
1316 8cf71710 Jan Kiszka
{
1317 8cf71710 Jan Kiszka
    debug_requested = 1;
1318 83f338f7 Jan Kiszka
    qemu_notify_event();
1319 8cf71710 Jan Kiszka
}
1320 8cf71710 Jan Kiszka
1321 8cf71710 Jan Kiszka
void qemu_system_vmstop_request(int reason)
1322 8cf71710 Jan Kiszka
{
1323 8cf71710 Jan Kiszka
    vmstop_requested = reason;
1324 8cf71710 Jan Kiszka
    qemu_notify_event();
1325 8cf71710 Jan Kiszka
}
1326 8cf71710 Jan Kiszka
1327 d6f4ade2 Paolo Bonzini
void main_loop_wait(int nonblocking)
1328 56f3a5d0 aliguori
{
1329 56f3a5d0 aliguori
    IOHandlerRecord *ioh;
1330 56f3a5d0 aliguori
    fd_set rfds, wfds, xfds;
1331 56f3a5d0 aliguori
    int ret, nfds;
1332 56f3a5d0 aliguori
    struct timeval tv;
1333 d6f4ade2 Paolo Bonzini
    int timeout;
1334 56f3a5d0 aliguori
1335 d6f4ade2 Paolo Bonzini
    if (nonblocking)
1336 d6f4ade2 Paolo Bonzini
        timeout = 0;
1337 d6f4ade2 Paolo Bonzini
    else {
1338 d6f4ade2 Paolo Bonzini
        timeout = qemu_calculate_timeout();
1339 d6f4ade2 Paolo Bonzini
        qemu_bh_update_timeout(&timeout);
1340 d6f4ade2 Paolo Bonzini
    }
1341 56f3a5d0 aliguori
1342 0d93ca7c Jes Sorensen
    os_host_main_loop_wait(&timeout);
1343 56f3a5d0 aliguori
1344 fd1dff4b bellard
    /* poll any events */
1345 fd1dff4b bellard
    /* XXX: separate device handlers from system ones */
1346 6abfbd79 aliguori
    nfds = -1;
1347 fd1dff4b bellard
    FD_ZERO(&rfds);
1348 fd1dff4b bellard
    FD_ZERO(&wfds);
1349 e035649e bellard
    FD_ZERO(&xfds);
1350 31d4ee6c Juan Quintela
    QLIST_FOREACH(ioh, &io_handlers, next) {
1351 cafffd40 ths
        if (ioh->deleted)
1352 cafffd40 ths
            continue;
1353 fd1dff4b bellard
        if (ioh->fd_read &&
1354 fd1dff4b bellard
            (!ioh->fd_read_poll ||
1355 fd1dff4b bellard
             ioh->fd_read_poll(ioh->opaque) != 0)) {
1356 fd1dff4b bellard
            FD_SET(ioh->fd, &rfds);
1357 fd1dff4b bellard
            if (ioh->fd > nfds)
1358 fd1dff4b bellard
                nfds = ioh->fd;
1359 fd1dff4b bellard
        }
1360 fd1dff4b bellard
        if (ioh->fd_write) {
1361 fd1dff4b bellard
            FD_SET(ioh->fd, &wfds);
1362 fd1dff4b bellard
            if (ioh->fd > nfds)
1363 fd1dff4b bellard
                nfds = ioh->fd;
1364 fd1dff4b bellard
        }
1365 fd1dff4b bellard
    }
1366 3b46e624 ths
1367 56f3a5d0 aliguori
    tv.tv_sec = timeout / 1000;
1368 56f3a5d0 aliguori
    tv.tv_usec = (timeout % 1000) * 1000;
1369 56f3a5d0 aliguori
1370 d918f23e Jan Kiszka
    slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1371 d918f23e Jan Kiszka
1372 4870852c aliguori
    qemu_mutex_unlock_iothread();
1373 e035649e bellard
    ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1374 4870852c aliguori
    qemu_mutex_lock_iothread();
1375 fd1dff4b bellard
    if (ret > 0) {
1376 31d4ee6c Juan Quintela
        IOHandlerRecord *pioh;
1377 cafffd40 ths
1378 31d4ee6c Juan Quintela
        QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1379 0290b57b Stefan Hajnoczi
            if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1380 4bed9837 Juan Quintela
                ioh->fd_read(ioh->opaque);
1381 4bed9837 Juan Quintela
            }
1382 0290b57b Stefan Hajnoczi
            if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1383 4bed9837 Juan Quintela
                ioh->fd_write(ioh->opaque);
1384 31d4ee6c Juan Quintela
            }
1385 0290b57b Stefan Hajnoczi
1386 0290b57b Stefan Hajnoczi
            /* Do this last in case read/write handlers marked it for deletion */
1387 0290b57b Stefan Hajnoczi
            if (ioh->deleted) {
1388 0290b57b Stefan Hajnoczi
                QLIST_REMOVE(ioh, next);
1389 0290b57b Stefan Hajnoczi
                qemu_free(ioh);
1390 0290b57b Stefan Hajnoczi
            }
1391 cafffd40 ths
        }
1392 fd1dff4b bellard
    }
1393 d918f23e Jan Kiszka
1394 d918f23e Jan Kiszka
    slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1395 c20709aa bellard
1396 b6964827 Paolo Bonzini
    qemu_run_all_timers();
1397 21d5d12b Jan Kiszka
1398 423f0742 pbrook
    /* Check bottom-halves last in case any of the earlier events triggered
1399 423f0742 pbrook
       them.  */
1400 423f0742 pbrook
    qemu_bh_poll();
1401 3b46e624 ths
1402 5905b2e5 bellard
}
1403 5905b2e5 bellard
1404 46481d39 Jan Kiszka
#ifndef CONFIG_IOTHREAD
1405 46481d39 Jan Kiszka
static int vm_request_pending(void)
1406 43b96858 aliguori
{
1407 46481d39 Jan Kiszka
    return powerdown_requested ||
1408 46481d39 Jan Kiszka
           reset_requested ||
1409 46481d39 Jan Kiszka
           shutdown_requested ||
1410 46481d39 Jan Kiszka
           debug_requested ||
1411 46481d39 Jan Kiszka
           vmstop_requested;
1412 43b96858 aliguori
}
1413 46481d39 Jan Kiszka
#endif
1414 43b96858 aliguori
1415 d9c32310 Blue Swirl
qemu_irq qemu_system_powerdown;
1416 d9c32310 Blue Swirl
1417 43b96858 aliguori
static void main_loop(void)
1418 43b96858 aliguori
{
1419 8e1b90ec Jan Kiszka
    bool nonblocking = false;
1420 8e1b90ec Jan Kiszka
#ifdef CONFIG_PROFILER
1421 8e1b90ec Jan Kiszka
    int64_t ti;
1422 8e1b90ec Jan Kiszka
#endif
1423 6e29f5da aliguori
    int r;
1424 e6e35b1e aliguori
1425 7277e027 Blue Swirl
    qemu_main_loop_start();
1426 d6dc3d42 aliguori
1427 6e29f5da aliguori
    for (;;) {
1428 d6dc3d42 aliguori
#ifndef CONFIG_IOTHREAD
1429 46481d39 Jan Kiszka
        nonblocking = cpu_exec_all();
1430 46481d39 Jan Kiszka
        if (vm_request_pending()) {
1431 46481d39 Jan Kiszka
            nonblocking = true;
1432 46481d39 Jan Kiszka
        }
1433 d6dc3d42 aliguori
#endif
1434 89bfc105 bellard
#ifdef CONFIG_PROFILER
1435 46481d39 Jan Kiszka
        ti = profile_getclock();
1436 89bfc105 bellard
#endif
1437 46481d39 Jan Kiszka
        main_loop_wait(nonblocking);
1438 89bfc105 bellard
#ifdef CONFIG_PROFILER
1439 46481d39 Jan Kiszka
        dev_time += profile_getclock() - ti;
1440 89bfc105 bellard
#endif
1441 43b96858 aliguori
1442 8cf71710 Jan Kiszka
        if (qemu_debug_requested()) {
1443 8cf71710 Jan Kiszka
            vm_stop(VMSTOP_DEBUG);
1444 b1a15e7e Luiz Capitulino
        }
1445 43b96858 aliguori
        if (qemu_shutdown_requested()) {
1446 242cd003 Blue Swirl
            monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1447 43b96858 aliguori
            if (no_shutdown) {
1448 e07bbac5 Jan Kiszka
                vm_stop(VMSTOP_SHUTDOWN);
1449 43b96858 aliguori
                no_shutdown = 0;
1450 43b96858 aliguori
            } else
1451 43b96858 aliguori
                break;
1452 43b96858 aliguori
        }
1453 d6dc3d42 aliguori
        if (qemu_reset_requested()) {
1454 d6dc3d42 aliguori
            pause_all_vcpus();
1455 a7ada151 Jan Kiszka
            cpu_synchronize_all_states();
1456 43b96858 aliguori
            qemu_system_reset();
1457 d6dc3d42 aliguori
            resume_all_vcpus();
1458 d6dc3d42 aliguori
        }
1459 d9c32310 Blue Swirl
        if (qemu_powerdown_requested()) {
1460 242cd003 Blue Swirl
            monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1461 d9c32310 Blue Swirl
            qemu_irq_raise(qemu_system_powerdown);
1462 d9c32310 Blue Swirl
        }
1463 b1a15e7e Luiz Capitulino
        if ((r = qemu_vmstop_requested())) {
1464 6e29f5da aliguori
            vm_stop(r);
1465 b1a15e7e Luiz Capitulino
        }
1466 b4608c04 bellard
    }
1467 2bc93fed MORITA Kazutaka
    bdrv_close_all();
1468 d6dc3d42 aliguori
    pause_all_vcpus();
1469 b4608c04 bellard
}
1470 b4608c04 bellard
1471 9bd7e6d9 pbrook
static void version(void)
1472 9bd7e6d9 pbrook
{
1473 f75ca1ae Thomas Monjalon
    printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1474 9bd7e6d9 pbrook
}
1475 9bd7e6d9 pbrook
1476 15f82208 ths
static void help(int exitcode)
1477 0824d6fc bellard
{
1478 e8105ebb Paolo Bonzini
    const char *options_help =
1479 ad96090a Blue Swirl
#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
1480 ad96090a Blue Swirl
        opt_help
1481 5824d651 blueswir1
#define DEFHEADING(text) stringify(text) "\n"
1482 9f16732a Jes Sorensen
#include "qemu-options.def"
1483 5824d651 blueswir1
#undef DEF
1484 5824d651 blueswir1
#undef DEFHEADING
1485 5824d651 blueswir1
#undef GEN_DOCS
1486 e8105ebb Paolo Bonzini
        ;
1487 e8105ebb Paolo Bonzini
    version();
1488 e8105ebb Paolo Bonzini
    printf("usage: %s [options] [disk_image]\n"
1489 e8105ebb Paolo Bonzini
           "\n"
1490 f75ca1ae Thomas Monjalon
           "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1491 3f020d70 malc
           "\n"
1492 e8105ebb Paolo Bonzini
           "%s\n"
1493 3f020d70 malc
           "During emulation, the following keys are useful:\n"
1494 3f020d70 malc
           "ctrl-alt-f      toggle full screen\n"
1495 3f020d70 malc
           "ctrl-alt-n      switch to virtual console 'n'\n"
1496 3f020d70 malc
           "ctrl-alt        toggle mouse and keyboard grab\n"
1497 3f020d70 malc
           "\n"
1498 e8105ebb Paolo Bonzini
           "When using -nographic, press 'ctrl-a h' to get some help.\n",
1499 e8105ebb Paolo Bonzini
           "qemu",
1500 e8105ebb Paolo Bonzini
           options_help);
1501 15f82208 ths
    exit(exitcode);
1502 0824d6fc bellard
}
1503 0824d6fc bellard
1504 cd6f1169 bellard
#define HAS_ARG 0x0001
1505 cd6f1169 bellard
1506 cd6f1169 bellard
typedef struct QEMUOption {
1507 cd6f1169 bellard
    const char *name;
1508 cd6f1169 bellard
    int flags;
1509 cd6f1169 bellard
    int index;
1510 ad96090a Blue Swirl
    uint32_t arch_mask;
1511 cd6f1169 bellard
} QEMUOption;
1512 cd6f1169 bellard
1513 dbed7e40 blueswir1
static const QEMUOption qemu_options[] = {
1514 ad96090a Blue Swirl
    { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1515 ad96090a Blue Swirl
#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
1516 ad96090a Blue Swirl
    { option, opt_arg, opt_enum, arch_mask },
1517 5824d651 blueswir1
#define DEFHEADING(text)
1518 9f16732a Jes Sorensen
#include "qemu-options.def"
1519 5824d651 blueswir1
#undef DEF
1520 5824d651 blueswir1
#undef DEFHEADING
1521 5824d651 blueswir1
#undef GEN_DOCS
1522 cd6f1169 bellard
    { NULL },
1523 fc01f7e7 bellard
};
1524 3893c124 malc
static void select_vgahw (const char *p)
1525 3893c124 malc
{
1526 3893c124 malc
    const char *opts;
1527 3893c124 malc
1528 64465297 Gerd Hoffmann
    default_vga = 0;
1529 86176759 Zachary Amsden
    vga_interface_type = VGA_NONE;
1530 3893c124 malc
    if (strstart(p, "std", &opts)) {
1531 86176759 Zachary Amsden
        vga_interface_type = VGA_STD;
1532 3893c124 malc
    } else if (strstart(p, "cirrus", &opts)) {
1533 86176759 Zachary Amsden
        vga_interface_type = VGA_CIRRUS;
1534 3893c124 malc
    } else if (strstart(p, "vmware", &opts)) {
1535 86176759 Zachary Amsden
        vga_interface_type = VGA_VMWARE;
1536 94909d9f aliguori
    } else if (strstart(p, "xenfb", &opts)) {
1537 86176759 Zachary Amsden
        vga_interface_type = VGA_XENFB;
1538 a19cbfb3 Gerd Hoffmann
    } else if (strstart(p, "qxl", &opts)) {
1539 a19cbfb3 Gerd Hoffmann
        vga_interface_type = VGA_QXL;
1540 28b85ed8 aliguori
    } else if (!strstart(p, "none", &opts)) {
1541 3893c124 malc
    invalid_vga:
1542 3893c124 malc
        fprintf(stderr, "Unknown vga type: %s\n", p);
1543 3893c124 malc
        exit(1);
1544 3893c124 malc
    }
1545 cb5a7aa8 malc
    while (*opts) {
1546 cb5a7aa8 malc
        const char *nextopt;
1547 cb5a7aa8 malc
1548 cb5a7aa8 malc
        if (strstart(opts, ",retrace=", &nextopt)) {
1549 cb5a7aa8 malc
            opts = nextopt;
1550 cb5a7aa8 malc
            if (strstart(opts, "dumb", &nextopt))
1551 cb5a7aa8 malc
                vga_retrace_method = VGA_RETRACE_DUMB;
1552 cb5a7aa8 malc
            else if (strstart(opts, "precise", &nextopt))
1553 cb5a7aa8 malc
                vga_retrace_method = VGA_RETRACE_PRECISE;
1554 cb5a7aa8 malc
            else goto invalid_vga;
1555 cb5a7aa8 malc
        } else goto invalid_vga;
1556 cb5a7aa8 malc
        opts = nextopt;
1557 cb5a7aa8 malc
    }
1558 3893c124 malc
}
1559 3893c124 malc
1560 1472a95b Jes Sorensen
static DisplayType select_display(const char *p)
1561 1472a95b Jes Sorensen
{
1562 1472a95b Jes Sorensen
    const char *opts;
1563 1472a95b Jes Sorensen
    DisplayType display = DT_DEFAULT;
1564 1472a95b Jes Sorensen
1565 1472a95b Jes Sorensen
    if (strstart(p, "sdl", &opts)) {
1566 1472a95b Jes Sorensen
#ifdef CONFIG_SDL
1567 1472a95b Jes Sorensen
        display = DT_SDL;
1568 1472a95b Jes Sorensen
        while (*opts) {
1569 1472a95b Jes Sorensen
            const char *nextopt;
1570 1472a95b Jes Sorensen
1571 1472a95b Jes Sorensen
            if (strstart(opts, ",frame=", &nextopt)) {
1572 1472a95b Jes Sorensen
                opts = nextopt;
1573 1472a95b Jes Sorensen
                if (strstart(opts, "on", &nextopt)) {
1574 1472a95b Jes Sorensen
                    no_frame = 0;
1575 1472a95b Jes Sorensen
                } else if (strstart(opts, "off", &nextopt)) {
1576 1472a95b Jes Sorensen
                    no_frame = 1;
1577 1472a95b Jes Sorensen
                } else {
1578 05175535 Peter Maydell
                    goto invalid_sdl_args;
1579 1472a95b Jes Sorensen
                }
1580 1472a95b Jes Sorensen
            } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1581 1472a95b Jes Sorensen
                opts = nextopt;
1582 1472a95b Jes Sorensen
                if (strstart(opts, "on", &nextopt)) {
1583 1472a95b Jes Sorensen
                    alt_grab = 1;
1584 1472a95b Jes Sorensen
                } else if (strstart(opts, "off", &nextopt)) {
1585 1472a95b Jes Sorensen
                    alt_grab = 0;
1586 1472a95b Jes Sorensen
                } else {
1587 05175535 Peter Maydell
                    goto invalid_sdl_args;
1588 1472a95b Jes Sorensen
                }
1589 1472a95b Jes Sorensen
            } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1590 1472a95b Jes Sorensen
                opts = nextopt;
1591 1472a95b Jes Sorensen
                if (strstart(opts, "on", &nextopt)) {
1592 1472a95b Jes Sorensen
                    ctrl_grab = 1;
1593 1472a95b Jes Sorensen
                } else if (strstart(opts, "off", &nextopt)) {
1594 1472a95b Jes Sorensen
                    ctrl_grab = 0;
1595 1472a95b Jes Sorensen
                } else {
1596 05175535 Peter Maydell
                    goto invalid_sdl_args;
1597 1472a95b Jes Sorensen
                }
1598 1472a95b Jes Sorensen
            } else if (strstart(opts, ",window_close=", &nextopt)) {
1599 1472a95b Jes Sorensen
                opts = nextopt;
1600 1472a95b Jes Sorensen
                if (strstart(opts, "on", &nextopt)) {
1601 1472a95b Jes Sorensen
                    no_quit = 0;
1602 1472a95b Jes Sorensen
                } else if (strstart(opts, "off", &nextopt)) {
1603 1472a95b Jes Sorensen
                    no_quit = 1;
1604 1472a95b Jes Sorensen
                } else {
1605 05175535 Peter Maydell
                    goto invalid_sdl_args;
1606 1472a95b Jes Sorensen
                }
1607 1472a95b Jes Sorensen
            } else {
1608 05175535 Peter Maydell
            invalid_sdl_args:
1609 05175535 Peter Maydell
                fprintf(stderr, "Invalid SDL option string: %s\n", p);
1610 05175535 Peter Maydell
                exit(1);
1611 1472a95b Jes Sorensen
            }
1612 1472a95b Jes Sorensen
            opts = nextopt;
1613 1472a95b Jes Sorensen
        }
1614 1472a95b Jes Sorensen
#else
1615 1472a95b Jes Sorensen
        fprintf(stderr, "SDL support is disabled\n");
1616 1472a95b Jes Sorensen
        exit(1);
1617 1472a95b Jes Sorensen
#endif
1618 3264ff12 Jes Sorensen
    } else if (strstart(p, "vnc", &opts)) {
1619 821601ea Jes Sorensen
#ifdef CONFIG_VNC
1620 3264ff12 Jes Sorensen
        display_remote++;
1621 3264ff12 Jes Sorensen
1622 3264ff12 Jes Sorensen
        if (*opts) {
1623 3264ff12 Jes Sorensen
            const char *nextopt;
1624 3264ff12 Jes Sorensen
1625 3264ff12 Jes Sorensen
            if (strstart(opts, "=", &nextopt)) {
1626 3264ff12 Jes Sorensen
                vnc_display = nextopt;
1627 3264ff12 Jes Sorensen
            }
1628 3264ff12 Jes Sorensen
        }
1629 3264ff12 Jes Sorensen
        if (!vnc_display) {
1630 3264ff12 Jes Sorensen
            fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1631 3264ff12 Jes Sorensen
            exit(1);
1632 3264ff12 Jes Sorensen
        }
1633 821601ea Jes Sorensen
#else
1634 821601ea Jes Sorensen
        fprintf(stderr, "VNC support is disabled\n");
1635 821601ea Jes Sorensen
        exit(1);
1636 821601ea Jes Sorensen
#endif
1637 1472a95b Jes Sorensen
    } else if (strstart(p, "curses", &opts)) {
1638 1472a95b Jes Sorensen
#ifdef CONFIG_CURSES
1639 1472a95b Jes Sorensen
        display = DT_CURSES;
1640 1472a95b Jes Sorensen
#else
1641 1472a95b Jes Sorensen
        fprintf(stderr, "Curses support is disabled\n");
1642 1472a95b Jes Sorensen
        exit(1);
1643 1472a95b Jes Sorensen
#endif
1644 4171d32e Jes Sorensen
    } else if (strstart(p, "none", &opts)) {
1645 4171d32e Jes Sorensen
        display = DT_NONE;
1646 1472a95b Jes Sorensen
    } else {
1647 1472a95b Jes Sorensen
        fprintf(stderr, "Unknown display type: %s\n", p);
1648 1472a95b Jes Sorensen
        exit(1);
1649 1472a95b Jes Sorensen
    }
1650 1472a95b Jes Sorensen
1651 1472a95b Jes Sorensen
    return display;
1652 1472a95b Jes Sorensen
}
1653 1472a95b Jes Sorensen
1654 7d4c3d53 Markus Armbruster
static int balloon_parse(const char *arg)
1655 7d4c3d53 Markus Armbruster
{
1656 382f0743 Gerd Hoffmann
    QemuOpts *opts;
1657 7d4c3d53 Markus Armbruster
1658 382f0743 Gerd Hoffmann
    if (strcmp(arg, "none") == 0) {
1659 382f0743 Gerd Hoffmann
        return 0;
1660 382f0743 Gerd Hoffmann
    }
1661 382f0743 Gerd Hoffmann
1662 382f0743 Gerd Hoffmann
    if (!strncmp(arg, "virtio", 6)) {
1663 382f0743 Gerd Hoffmann
        if (arg[6] == ',') {
1664 382f0743 Gerd Hoffmann
            /* have params -> parse them */
1665 3329f07b Gerd Hoffmann
            opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1666 382f0743 Gerd Hoffmann
            if (!opts)
1667 382f0743 Gerd Hoffmann
                return  -1;
1668 382f0743 Gerd Hoffmann
        } else {
1669 382f0743 Gerd Hoffmann
            /* create empty opts */
1670 3329f07b Gerd Hoffmann
            opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1671 7d4c3d53 Markus Armbruster
        }
1672 382f0743 Gerd Hoffmann
        qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1673 382f0743 Gerd Hoffmann
        return 0;
1674 7d4c3d53 Markus Armbruster
    }
1675 382f0743 Gerd Hoffmann
1676 382f0743 Gerd Hoffmann
    return -1;
1677 7d4c3d53 Markus Armbruster
}
1678 7d4c3d53 Markus Armbruster
1679 5cea8590 Paul Brook
char *qemu_find_file(int type, const char *name)
1680 5cea8590 Paul Brook
{
1681 5cea8590 Paul Brook
    int len;
1682 5cea8590 Paul Brook
    const char *subdir;
1683 5cea8590 Paul Brook
    char *buf;
1684 5cea8590 Paul Brook
1685 5cea8590 Paul Brook
    /* If name contains path separators then try it as a straight path.  */
1686 5cea8590 Paul Brook
    if ((strchr(name, '/') || strchr(name, '\\'))
1687 5cea8590 Paul Brook
        && access(name, R_OK) == 0) {
1688 73ffc805 Jean-Christophe DUBOIS
        return qemu_strdup(name);
1689 5cea8590 Paul Brook
    }
1690 5cea8590 Paul Brook
    switch (type) {
1691 5cea8590 Paul Brook
    case QEMU_FILE_TYPE_BIOS:
1692 5cea8590 Paul Brook
        subdir = "";
1693 5cea8590 Paul Brook
        break;
1694 5cea8590 Paul Brook
    case QEMU_FILE_TYPE_KEYMAP:
1695 5cea8590 Paul Brook
        subdir = "keymaps/";
1696 5cea8590 Paul Brook
        break;
1697 5cea8590 Paul Brook
    default:
1698 5cea8590 Paul Brook
        abort();
1699 5cea8590 Paul Brook
    }
1700 5cea8590 Paul Brook
    len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1701 5cea8590 Paul Brook
    buf = qemu_mallocz(len);
1702 3a41759d Blue Swirl
    snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1703 5cea8590 Paul Brook
    if (access(buf, R_OK)) {
1704 5cea8590 Paul Brook
        qemu_free(buf);
1705 5cea8590 Paul Brook
        return NULL;
1706 5cea8590 Paul Brook
    }
1707 5cea8590 Paul Brook
    return buf;
1708 5cea8590 Paul Brook
}
1709 5cea8590 Paul Brook
1710 ff952ba2 Markus Armbruster
static int device_help_func(QemuOpts *opts, void *opaque)
1711 ff952ba2 Markus Armbruster
{
1712 ff952ba2 Markus Armbruster
    return qdev_device_help(opts);
1713 ff952ba2 Markus Armbruster
}
1714 ff952ba2 Markus Armbruster
1715 f31d07d1 Gerd Hoffmann
static int device_init_func(QemuOpts *opts, void *opaque)
1716 f31d07d1 Gerd Hoffmann
{
1717 f31d07d1 Gerd Hoffmann
    DeviceState *dev;
1718 f31d07d1 Gerd Hoffmann
1719 f31d07d1 Gerd Hoffmann
    dev = qdev_device_add(opts);
1720 f31d07d1 Gerd Hoffmann
    if (!dev)
1721 f31d07d1 Gerd Hoffmann
        return -1;
1722 f31d07d1 Gerd Hoffmann
    return 0;
1723 f31d07d1 Gerd Hoffmann
}
1724 f31d07d1 Gerd Hoffmann
1725 1a688d3b Gerd Hoffmann
static int chardev_init_func(QemuOpts *opts, void *opaque)
1726 1a688d3b Gerd Hoffmann
{
1727 1a688d3b Gerd Hoffmann
    CharDriverState *chr;
1728 1a688d3b Gerd Hoffmann
1729 1a688d3b Gerd Hoffmann
    chr = qemu_chr_open_opts(opts, NULL);
1730 1a688d3b Gerd Hoffmann
    if (!chr)
1731 1a688d3b Gerd Hoffmann
        return -1;
1732 1a688d3b Gerd Hoffmann
    return 0;
1733 1a688d3b Gerd Hoffmann
}
1734 1a688d3b Gerd Hoffmann
1735 758e8e38 Venkateswararao Jujjuri (JV)
#ifdef CONFIG_VIRTFS
1736 74db920c Gautham R Shenoy
static int fsdev_init_func(QemuOpts *opts, void *opaque)
1737 74db920c Gautham R Shenoy
{
1738 74db920c Gautham R Shenoy
    int ret;
1739 74db920c Gautham R Shenoy
    ret = qemu_fsdev_add(opts);
1740 74db920c Gautham R Shenoy
1741 74db920c Gautham R Shenoy
    return ret;
1742 74db920c Gautham R Shenoy
}
1743 74db920c Gautham R Shenoy
#endif
1744 74db920c Gautham R Shenoy
1745 88589343 Gerd Hoffmann
static int mon_init_func(QemuOpts *opts, void *opaque)
1746 88589343 Gerd Hoffmann
{
1747 88589343 Gerd Hoffmann
    CharDriverState *chr;
1748 88589343 Gerd Hoffmann
    const char *chardev;
1749 88589343 Gerd Hoffmann
    const char *mode;
1750 88589343 Gerd Hoffmann
    int flags;
1751 88589343 Gerd Hoffmann
1752 88589343 Gerd Hoffmann
    mode = qemu_opt_get(opts, "mode");
1753 88589343 Gerd Hoffmann
    if (mode == NULL) {
1754 88589343 Gerd Hoffmann
        mode = "readline";
1755 88589343 Gerd Hoffmann
    }
1756 88589343 Gerd Hoffmann
    if (strcmp(mode, "readline") == 0) {
1757 88589343 Gerd Hoffmann
        flags = MONITOR_USE_READLINE;
1758 88589343 Gerd Hoffmann
    } else if (strcmp(mode, "control") == 0) {
1759 88589343 Gerd Hoffmann
        flags = MONITOR_USE_CONTROL;
1760 88589343 Gerd Hoffmann
    } else {
1761 88589343 Gerd Hoffmann
        fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1762 88589343 Gerd Hoffmann
        exit(1);
1763 88589343 Gerd Hoffmann
    }
1764 88589343 Gerd Hoffmann
1765 39eaab9a Daniel P. Berrange
    if (qemu_opt_get_bool(opts, "pretty", 0))
1766 39eaab9a Daniel P. Berrange
        flags |= MONITOR_USE_PRETTY;
1767 39eaab9a Daniel P. Berrange
1768 88589343 Gerd Hoffmann
    if (qemu_opt_get_bool(opts, "default", 0))
1769 88589343 Gerd Hoffmann
        flags |= MONITOR_IS_DEFAULT;
1770 88589343 Gerd Hoffmann
1771 88589343 Gerd Hoffmann
    chardev = qemu_opt_get(opts, "chardev");
1772 88589343 Gerd Hoffmann
    chr = qemu_chr_find(chardev);
1773 88589343 Gerd Hoffmann
    if (chr == NULL) {
1774 88589343 Gerd Hoffmann
        fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1775 88589343 Gerd Hoffmann
        exit(1);
1776 88589343 Gerd Hoffmann
    }
1777 88589343 Gerd Hoffmann
1778 88589343 Gerd Hoffmann
    monitor_init(chr, flags);
1779 88589343 Gerd Hoffmann
    return 0;
1780 88589343 Gerd Hoffmann
}
1781 88589343 Gerd Hoffmann
1782 6ca5582d Gerd Hoffmann
static void monitor_parse(const char *optarg, const char *mode)
1783 88589343 Gerd Hoffmann
{
1784 88589343 Gerd Hoffmann
    static int monitor_device_index = 0;
1785 88589343 Gerd Hoffmann
    QemuOpts *opts;
1786 88589343 Gerd Hoffmann
    const char *p;
1787 88589343 Gerd Hoffmann
    char label[32];
1788 88589343 Gerd Hoffmann
    int def = 0;
1789 88589343 Gerd Hoffmann
1790 88589343 Gerd Hoffmann
    if (strstart(optarg, "chardev:", &p)) {
1791 88589343 Gerd Hoffmann
        snprintf(label, sizeof(label), "%s", p);
1792 88589343 Gerd Hoffmann
    } else {
1793 140e065d Jan Kiszka
        snprintf(label, sizeof(label), "compat_monitor%d",
1794 140e065d Jan Kiszka
                 monitor_device_index);
1795 140e065d Jan Kiszka
        if (monitor_device_index == 0) {
1796 88589343 Gerd Hoffmann
            def = 1;
1797 88589343 Gerd Hoffmann
        }
1798 88589343 Gerd Hoffmann
        opts = qemu_chr_parse_compat(label, optarg);
1799 88589343 Gerd Hoffmann
        if (!opts) {
1800 88589343 Gerd Hoffmann
            fprintf(stderr, "parse error: %s\n", optarg);
1801 88589343 Gerd Hoffmann
            exit(1);
1802 88589343 Gerd Hoffmann
        }
1803 88589343 Gerd Hoffmann
    }
1804 88589343 Gerd Hoffmann
1805 3329f07b Gerd Hoffmann
    opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1806 88589343 Gerd Hoffmann
    if (!opts) {
1807 88589343 Gerd Hoffmann
        fprintf(stderr, "duplicate chardev: %s\n", label);
1808 88589343 Gerd Hoffmann
        exit(1);
1809 88589343 Gerd Hoffmann
    }
1810 6ca5582d Gerd Hoffmann
    qemu_opt_set(opts, "mode", mode);
1811 88589343 Gerd Hoffmann
    qemu_opt_set(opts, "chardev", label);
1812 88589343 Gerd Hoffmann
    if (def)
1813 88589343 Gerd Hoffmann
        qemu_opt_set(opts, "default", "on");
1814 88589343 Gerd Hoffmann
    monitor_device_index++;
1815 88589343 Gerd Hoffmann
}
1816 88589343 Gerd Hoffmann
1817 bd3c948d Gerd Hoffmann
struct device_config {
1818 bd3c948d Gerd Hoffmann
    enum {
1819 aee1b935 Gerd Hoffmann
        DEV_USB,       /* -usbdevice     */
1820 aee1b935 Gerd Hoffmann
        DEV_BT,        /* -bt            */
1821 aee1b935 Gerd Hoffmann
        DEV_SERIAL,    /* -serial        */
1822 aee1b935 Gerd Hoffmann
        DEV_PARALLEL,  /* -parallel      */
1823 aee1b935 Gerd Hoffmann
        DEV_VIRTCON,   /* -virtioconsole */
1824 c9f398e5 H. Peter Anvin
        DEV_DEBUGCON,  /* -debugcon */
1825 bd3c948d Gerd Hoffmann
    } type;
1826 bd3c948d Gerd Hoffmann
    const char *cmdline;
1827 72cf2d4f Blue Swirl
    QTAILQ_ENTRY(device_config) next;
1828 bd3c948d Gerd Hoffmann
};
1829 72cf2d4f Blue Swirl
QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1830 bd3c948d Gerd Hoffmann
1831 bd3c948d Gerd Hoffmann
static void add_device_config(int type, const char *cmdline)
1832 bd3c948d Gerd Hoffmann
{
1833 bd3c948d Gerd Hoffmann
    struct device_config *conf;
1834 bd3c948d Gerd Hoffmann
1835 bd3c948d Gerd Hoffmann
    conf = qemu_mallocz(sizeof(*conf));
1836 bd3c948d Gerd Hoffmann
    conf->type = type;
1837 bd3c948d Gerd Hoffmann
    conf->cmdline = cmdline;
1838 72cf2d4f Blue Swirl
    QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1839 bd3c948d Gerd Hoffmann
}
1840 bd3c948d Gerd Hoffmann
1841 bd3c948d Gerd Hoffmann
static int foreach_device_config(int type, int (*func)(const char *cmdline))
1842 bd3c948d Gerd Hoffmann
{
1843 bd3c948d Gerd Hoffmann
    struct device_config *conf;
1844 bd3c948d Gerd Hoffmann
    int rc;
1845 bd3c948d Gerd Hoffmann
1846 72cf2d4f Blue Swirl
    QTAILQ_FOREACH(conf, &device_configs, next) {
1847 bd3c948d Gerd Hoffmann
        if (conf->type != type)
1848 bd3c948d Gerd Hoffmann
            continue;
1849 bd3c948d Gerd Hoffmann
        rc = func(conf->cmdline);
1850 bd3c948d Gerd Hoffmann
        if (0 != rc)
1851 bd3c948d Gerd Hoffmann
            return rc;
1852 bd3c948d Gerd Hoffmann
    }
1853 bd3c948d Gerd Hoffmann
    return 0;
1854 bd3c948d Gerd Hoffmann
}
1855 bd3c948d Gerd Hoffmann
1856 998bbd74 Gerd Hoffmann
static int serial_parse(const char *devname)
1857 998bbd74 Gerd Hoffmann
{
1858 998bbd74 Gerd Hoffmann
    static int index = 0;
1859 998bbd74 Gerd Hoffmann
    char label[32];
1860 998bbd74 Gerd Hoffmann
1861 998bbd74 Gerd Hoffmann
    if (strcmp(devname, "none") == 0)
1862 998bbd74 Gerd Hoffmann
        return 0;
1863 998bbd74 Gerd Hoffmann
    if (index == MAX_SERIAL_PORTS) {
1864 998bbd74 Gerd Hoffmann
        fprintf(stderr, "qemu: too many serial ports\n");
1865 998bbd74 Gerd Hoffmann
        exit(1);
1866 998bbd74 Gerd Hoffmann
    }
1867 998bbd74 Gerd Hoffmann
    snprintf(label, sizeof(label), "serial%d", index);
1868 998bbd74 Gerd Hoffmann
    serial_hds[index] = qemu_chr_open(label, devname, NULL);
1869 998bbd74 Gerd Hoffmann
    if (!serial_hds[index]) {
1870 998bbd74 Gerd Hoffmann
        fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1871 998bbd74 Gerd Hoffmann
                devname, strerror(errno));
1872 998bbd74 Gerd Hoffmann
        return -1;
1873 998bbd74 Gerd Hoffmann
    }
1874 998bbd74 Gerd Hoffmann
    index++;
1875 998bbd74 Gerd Hoffmann
    return 0;
1876 998bbd74 Gerd Hoffmann
}
1877 998bbd74 Gerd Hoffmann
1878 6a5e8b0e Gerd Hoffmann
static int parallel_parse(const char *devname)
1879 6a5e8b0e Gerd Hoffmann
{
1880 6a5e8b0e Gerd Hoffmann
    static int index = 0;
1881 6a5e8b0e Gerd Hoffmann
    char label[32];
1882 6a5e8b0e Gerd Hoffmann
1883 6a5e8b0e Gerd Hoffmann
    if (strcmp(devname, "none") == 0)
1884 6a5e8b0e Gerd Hoffmann
        return 0;
1885 6a5e8b0e Gerd Hoffmann
    if (index == MAX_PARALLEL_PORTS) {
1886 6a5e8b0e Gerd Hoffmann
        fprintf(stderr, "qemu: too many parallel ports\n");
1887 6a5e8b0e Gerd Hoffmann
        exit(1);
1888 6a5e8b0e Gerd Hoffmann
    }
1889 6a5e8b0e Gerd Hoffmann
    snprintf(label, sizeof(label), "parallel%d", index);
1890 6a5e8b0e Gerd Hoffmann
    parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1891 6a5e8b0e Gerd Hoffmann
    if (!parallel_hds[index]) {
1892 6a5e8b0e Gerd Hoffmann
        fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1893 6a5e8b0e Gerd Hoffmann
                devname, strerror(errno));
1894 6a5e8b0e Gerd Hoffmann
        return -1;
1895 6a5e8b0e Gerd Hoffmann
    }
1896 6a5e8b0e Gerd Hoffmann
    index++;
1897 6a5e8b0e Gerd Hoffmann
    return 0;
1898 6a5e8b0e Gerd Hoffmann
}
1899 6a5e8b0e Gerd Hoffmann
1900 aee1b935 Gerd Hoffmann
static int virtcon_parse(const char *devname)
1901 aee1b935 Gerd Hoffmann
{
1902 3329f07b Gerd Hoffmann
    QemuOptsList *device = qemu_find_opts("device");
1903 aee1b935 Gerd Hoffmann
    static int index = 0;
1904 aee1b935 Gerd Hoffmann
    char label[32];
1905 392ecf54 Amit Shah
    QemuOpts *bus_opts, *dev_opts;
1906 aee1b935 Gerd Hoffmann
1907 aee1b935 Gerd Hoffmann
    if (strcmp(devname, "none") == 0)
1908 aee1b935 Gerd Hoffmann
        return 0;
1909 aee1b935 Gerd Hoffmann
    if (index == MAX_VIRTIO_CONSOLES) {
1910 aee1b935 Gerd Hoffmann
        fprintf(stderr, "qemu: too many virtio consoles\n");
1911 aee1b935 Gerd Hoffmann
        exit(1);
1912 aee1b935 Gerd Hoffmann
    }
1913 392ecf54 Amit Shah
1914 3329f07b Gerd Hoffmann
    bus_opts = qemu_opts_create(device, NULL, 0);
1915 392ecf54 Amit Shah
    qemu_opt_set(bus_opts, "driver", "virtio-serial");
1916 392ecf54 Amit Shah
1917 3329f07b Gerd Hoffmann
    dev_opts = qemu_opts_create(device, NULL, 0);
1918 392ecf54 Amit Shah
    qemu_opt_set(dev_opts, "driver", "virtconsole");
1919 392ecf54 Amit Shah
1920 aee1b935 Gerd Hoffmann
    snprintf(label, sizeof(label), "virtcon%d", index);
1921 aee1b935 Gerd Hoffmann
    virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1922 aee1b935 Gerd Hoffmann
    if (!virtcon_hds[index]) {
1923 aee1b935 Gerd Hoffmann
        fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1924 aee1b935 Gerd Hoffmann
                devname, strerror(errno));
1925 aee1b935 Gerd Hoffmann
        return -1;
1926 aee1b935 Gerd Hoffmann
    }
1927 392ecf54 Amit Shah
    qemu_opt_set(dev_opts, "chardev", label);
1928 392ecf54 Amit Shah
1929 aee1b935 Gerd Hoffmann
    index++;
1930 aee1b935 Gerd Hoffmann
    return 0;
1931 aee1b935 Gerd Hoffmann
}
1932 aee1b935 Gerd Hoffmann
1933 c9f398e5 H. Peter Anvin
static int debugcon_parse(const char *devname)
1934 c9f398e5 H. Peter Anvin
{   
1935 c9f398e5 H. Peter Anvin
    QemuOpts *opts;
1936 c9f398e5 H. Peter Anvin
1937 c9f398e5 H. Peter Anvin
    if (!qemu_chr_open("debugcon", devname, NULL)) {
1938 c9f398e5 H. Peter Anvin
        exit(1);
1939 c9f398e5 H. Peter Anvin
    }
1940 3329f07b Gerd Hoffmann
    opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1941 c9f398e5 H. Peter Anvin
    if (!opts) {
1942 c9f398e5 H. Peter Anvin
        fprintf(stderr, "qemu: already have a debugcon device\n");
1943 c9f398e5 H. Peter Anvin
        exit(1);
1944 c9f398e5 H. Peter Anvin
    }
1945 c9f398e5 H. Peter Anvin
    qemu_opt_set(opts, "driver", "isa-debugcon");
1946 c9f398e5 H. Peter Anvin
    qemu_opt_set(opts, "chardev", "debugcon");
1947 c9f398e5 H. Peter Anvin
    return 0;
1948 c9f398e5 H. Peter Anvin
}
1949 c9f398e5 H. Peter Anvin
1950 fd42deeb Gerd Hoffmann
void qemu_add_exit_notifier(Notifier *notify)
1951 fd42deeb Gerd Hoffmann
{
1952 fd42deeb Gerd Hoffmann
    notifier_list_add(&exit_notifiers, notify);
1953 fd42deeb Gerd Hoffmann
}
1954 fd42deeb Gerd Hoffmann
1955 fd42deeb Gerd Hoffmann
void qemu_remove_exit_notifier(Notifier *notify)
1956 fd42deeb Gerd Hoffmann
{
1957 fd42deeb Gerd Hoffmann
    notifier_list_remove(&exit_notifiers, notify);
1958 fd42deeb Gerd Hoffmann
}
1959 fd42deeb Gerd Hoffmann
1960 fd42deeb Gerd Hoffmann
static void qemu_run_exit_notifiers(void)
1961 fd42deeb Gerd Hoffmann
{
1962 fd42deeb Gerd Hoffmann
    notifier_list_notify(&exit_notifiers);
1963 fd42deeb Gerd Hoffmann
}
1964 fd42deeb Gerd Hoffmann
1965 4cab946a Gleb Natapov
void qemu_add_machine_init_done_notifier(Notifier *notify)
1966 4cab946a Gleb Natapov
{
1967 4cab946a Gleb Natapov
    notifier_list_add(&machine_init_done_notifiers, notify);
1968 4cab946a Gleb Natapov
}
1969 4cab946a Gleb Natapov
1970 4cab946a Gleb Natapov
static void qemu_run_machine_init_done_notifiers(void)
1971 4cab946a Gleb Natapov
{
1972 4cab946a Gleb Natapov
    notifier_list_notify(&machine_init_done_notifiers);
1973 4cab946a Gleb Natapov
}
1974 4cab946a Gleb Natapov
1975 6530a97b Anthony Liguori
static const QEMUOption *lookup_opt(int argc, char **argv,
1976 6530a97b Anthony Liguori
                                    const char **poptarg, int *poptind)
1977 6530a97b Anthony Liguori
{
1978 6530a97b Anthony Liguori
    const QEMUOption *popt;
1979 6530a97b Anthony Liguori
    int optind = *poptind;
1980 6530a97b Anthony Liguori
    char *r = argv[optind];
1981 6530a97b Anthony Liguori
    const char *optarg;
1982 6530a97b Anthony Liguori
1983 0f0bc3f1 Markus Armbruster
    loc_set_cmdline(argv, optind, 1);
1984 6530a97b Anthony Liguori
    optind++;
1985 6530a97b Anthony Liguori
    /* Treat --foo the same as -foo.  */
1986 6530a97b Anthony Liguori
    if (r[1] == '-')
1987 6530a97b Anthony Liguori
        r++;
1988 6530a97b Anthony Liguori
    popt = qemu_options;
1989 6530a97b Anthony Liguori
    for(;;) {
1990 6530a97b Anthony Liguori
        if (!popt->name) {
1991 0f0bc3f1 Markus Armbruster
            error_report("invalid option");
1992 6530a97b Anthony Liguori
            exit(1);
1993 6530a97b Anthony Liguori
        }
1994 6530a97b Anthony Liguori
        if (!strcmp(popt->name, r + 1))
1995 6530a97b Anthony Liguori
            break;
1996 6530a97b Anthony Liguori
        popt++;
1997 6530a97b Anthony Liguori
    }
1998 6530a97b Anthony Liguori
    if (popt->flags & HAS_ARG) {
1999 6530a97b Anthony Liguori
        if (optind >= argc) {
2000 0f0bc3f1 Markus Armbruster
            error_report("requires an argument");
2001 6530a97b Anthony Liguori
            exit(1);
2002 6530a97b Anthony Liguori
        }
2003 6530a97b Anthony Liguori
        optarg = argv[optind++];
2004 0f0bc3f1 Markus Armbruster
        loc_set_cmdline(argv, optind - 2, 2);
2005 6530a97b Anthony Liguori
    } else {
2006 6530a97b Anthony Liguori
        optarg = NULL;
2007 6530a97b Anthony Liguori
    }
2008 6530a97b Anthony Liguori
2009 6530a97b Anthony Liguori
    *poptarg = optarg;
2010 6530a97b Anthony Liguori
    *poptind = optind;
2011 6530a97b Anthony Liguori
2012 6530a97b Anthony Liguori
    return popt;
2013 6530a97b Anthony Liguori
}
2014 6530a97b Anthony Liguori
2015 902b3d5c malc
int main(int argc, char **argv, char **envp)
2016 0824d6fc bellard
{
2017 59030a8c aliguori
    const char *gdbstub_dev = NULL;
2018 e4bcb14c ths
    int i;
2019 da1fcfda Eduardo Habkost
    int snapshot, linux_boot;
2020 4e3de9e9 Paolo Bonzini
    const char *icount_option = NULL;
2021 7f7f9873 bellard
    const char *initrd_filename;
2022 a20dd508 bellard
    const char *kernel_filename, *kernel_cmdline;
2023 195325a4 Anthony Liguori
    char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2024 3023f332 aliguori
    DisplayState *ds;
2025 7d957bd8 aliguori
    DisplayChangeListener *dcl;
2026 46d4767d bellard
    int cyls, heads, secs, translation;
2027 f31d07d1 Gerd Hoffmann
    QemuOpts *hda_opts = NULL, *opts;
2028 03b0ba70 Gerd Hoffmann
    QemuOptsList *olist;
2029 cd6f1169 bellard
    int optind;
2030 6530a97b Anthony Liguori
    const char *optarg;
2031 d63d307f bellard
    const char *loadvm = NULL;
2032 cc1daa40 bellard
    QEMUMachine *machine;
2033 94fc95cd j_mayer
    const char *cpu_model;
2034 26a5f13b bellard
    int tb_size;
2035 93815bc2 ths
    const char *pid_file = NULL;
2036 5bb7910a aliguori
    const char *incoming = NULL;
2037 821601ea Jes Sorensen
#ifdef CONFIG_VNC
2038 993fbfdb Anthony Liguori
    int show_vnc_port = 0;
2039 821601ea Jes Sorensen
#endif
2040 292444cb Anthony Liguori
    int defconfig = 1;
2041 ab6540d5 Prerna Saxena
    const char *trace_file = NULL;
2042 0b5538c3 Stefan Hajnoczi
2043 fd42deeb Gerd Hoffmann
    atexit(qemu_run_exit_notifiers);
2044 65abca0a Markus Armbruster
    error_set_progname(argv[0]);
2045 65abca0a Markus Armbruster
2046 6875204c Jan Kiszka
    init_clocks();
2047 6875204c Jan Kiszka
2048 902b3d5c malc
    qemu_cache_utils_init(envp);
2049 902b3d5c malc
2050 72cf2d4f Blue Swirl
    QLIST_INIT (&vm_change_state_head);
2051 fe98ac14 Jes Sorensen
    os_setup_early_signal_handling();
2052 be995c27 bellard
2053 f80f9ec9 Anthony Liguori
    module_call_init(MODULE_INIT_MACHINE);
2054 0c257437 Anthony Liguori
    machine = find_default_machine();
2055 94fc95cd j_mayer
    cpu_model = NULL;
2056 fc01f7e7 bellard
    initrd_filename = NULL;
2057 4fc5d071 aurel32
    ram_size = 0;
2058 33e3963e bellard
    snapshot = 0;
2059 a20dd508 bellard
    kernel_filename = NULL;
2060 a20dd508 bellard
    kernel_cmdline = "";
2061 c4b1fcc0 bellard
    cyls = heads = secs = 0;
2062 46d4767d bellard
    translation = BIOS_ATA_TRANSLATION_AUTO;
2063 c4b1fcc0 bellard
2064 268a362c aliguori
    for (i = 0; i < MAX_NODES; i++) {
2065 268a362c aliguori
        node_mem[i] = 0;
2066 268a362c aliguori
        node_cpumask[i] = 0;
2067 268a362c aliguori
    }
2068 268a362c aliguori
2069 268a362c aliguori
    nb_numa_nodes = 0;
2070 7c9d8e07 bellard
    nb_nics = 0;
2071 3b46e624 ths
2072 26a5f13b bellard
    tb_size = 0;
2073 41bd639b blueswir1
    autostart= 1;
2074 41bd639b blueswir1
2075 292444cb Anthony Liguori
    /* first pass of option parsing */
2076 292444cb Anthony Liguori
    optind = 1;
2077 292444cb Anthony Liguori
    while (optind < argc) {
2078 292444cb Anthony Liguori
        if (argv[optind][0] != '-') {
2079 292444cb Anthony Liguori
            /* disk image */
2080 28e68d68 Anthony Liguori
            optind++;
2081 292444cb Anthony Liguori
            continue;
2082 292444cb Anthony Liguori
        } else {
2083 292444cb Anthony Liguori
            const QEMUOption *popt;
2084 292444cb Anthony Liguori
2085 292444cb Anthony Liguori
            popt = lookup_opt(argc, argv, &optarg, &optind);
2086 292444cb Anthony Liguori
            switch (popt->index) {
2087 292444cb Anthony Liguori
            case QEMU_OPTION_nodefconfig:
2088 292444cb Anthony Liguori
                defconfig=0;
2089 292444cb Anthony Liguori
                break;
2090 292444cb Anthony Liguori
            }
2091 292444cb Anthony Liguori
        }
2092 292444cb Anthony Liguori
    }
2093 292444cb Anthony Liguori
2094 292444cb Anthony Liguori
    if (defconfig) {
2095 dcfb0939 Kevin Wolf
        int ret;
2096 cf5a65aa Markus Armbruster
2097 dcfb0939 Kevin Wolf
        ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2098 019e78ba Kevin Wolf
        if (ret < 0 && ret != -ENOENT) {
2099 dcfb0939 Kevin Wolf
            exit(1);
2100 292444cb Anthony Liguori
        }
2101 292444cb Anthony Liguori
2102 dcfb0939 Kevin Wolf
        ret = qemu_read_config_file(arch_config_name);
2103 019e78ba Kevin Wolf
        if (ret < 0 && ret != -ENOENT) {
2104 dcfb0939 Kevin Wolf
            exit(1);
2105 292444cb Anthony Liguori
        }
2106 292444cb Anthony Liguori
    }
2107 de06f8d1 Blue Swirl
    cpudef_init();
2108 292444cb Anthony Liguori
2109 292444cb Anthony Liguori
    /* second pass of option parsing */
2110 cd6f1169 bellard
    optind = 1;
2111 0824d6fc bellard
    for(;;) {
2112 cd6f1169 bellard
        if (optind >= argc)
2113 0824d6fc bellard
            break;
2114 6530a97b Anthony Liguori
        if (argv[optind][0] != '-') {
2115 2292ddae Markus Armbruster
            hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2116 cd6f1169 bellard
        } else {
2117 cd6f1169 bellard
            const QEMUOption *popt;
2118 cd6f1169 bellard
2119 6530a97b Anthony Liguori
            popt = lookup_opt(argc, argv, &optarg, &optind);
2120 ad96090a Blue Swirl
            if (!(popt->arch_mask & arch_type)) {
2121 ad96090a Blue Swirl
                printf("Option %s not supported for this target\n", popt->name);
2122 ad96090a Blue Swirl
                exit(1);
2123 ad96090a Blue Swirl
            }
2124 cd6f1169 bellard
            switch(popt->index) {
2125 cc1daa40 bellard
            case QEMU_OPTION_M:
2126 cc1daa40 bellard
                machine = find_machine(optarg);
2127 cc1daa40 bellard
                if (!machine) {
2128 cc1daa40 bellard
                    QEMUMachine *m;
2129 cc1daa40 bellard
                    printf("Supported machines are:\n");
2130 cc1daa40 bellard
                    for(m = first_machine; m != NULL; m = m->next) {
2131 3f6599e6 Mark McLoughlin
                        if (m->alias)
2132 3f6599e6 Mark McLoughlin
                            printf("%-10s %s (alias of %s)\n",
2133 3f6599e6 Mark McLoughlin
                                   m->alias, m->desc, m->name);
2134 cc1daa40 bellard
                        printf("%-10s %s%s\n",
2135 5fafdf24 ths
                               m->name, m->desc,
2136 0c257437 Anthony Liguori
                               m->is_default ? " (default)" : "");
2137 cc1daa40 bellard
                    }
2138 15f82208 ths
                    exit(*optarg != '?');
2139 cc1daa40 bellard
                }
2140 cc1daa40 bellard
                break;
2141 94fc95cd j_mayer
            case QEMU_OPTION_cpu:
2142 94fc95cd j_mayer
                /* hw initialization will check this */
2143 15f82208 ths
                if (*optarg == '?') {
2144 262353cb Blue Swirl
                    list_cpus(stdout, &fprintf, optarg);
2145 15f82208 ths
                    exit(0);
2146 94fc95cd j_mayer
                } else {
2147 94fc95cd j_mayer
                    cpu_model = optarg;
2148 94fc95cd j_mayer
                }
2149 94fc95cd j_mayer
                break;
2150 cd6f1169 bellard
            case QEMU_OPTION_initrd:
2151 fc01f7e7 bellard
                initrd_filename = optarg;
2152 fc01f7e7 bellard
                break;
2153 cd6f1169 bellard
            case QEMU_OPTION_hda:
2154 5645b0f4 Markus Armbruster
                {
2155 5645b0f4 Markus Armbruster
                    char buf[256];
2156 5645b0f4 Markus Armbruster
                    if (cyls == 0)
2157 5645b0f4 Markus Armbruster
                        snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2158 5645b0f4 Markus Armbruster
                    else
2159 5645b0f4 Markus Armbruster
                        snprintf(buf, sizeof(buf),
2160 5645b0f4 Markus Armbruster
                                 "%s,cyls=%d,heads=%d,secs=%d%s",
2161 5645b0f4 Markus Armbruster
                                 HD_OPTS , cyls, heads, secs,
2162 5645b0f4 Markus Armbruster
                                 translation == BIOS_ATA_TRANSLATION_LBA ?
2163 e4bcb14c ths
                                 ",trans=lba" :
2164 5645b0f4 Markus Armbruster
                                 translation == BIOS_ATA_TRANSLATION_NONE ?
2165 e4bcb14c ths
                                 ",trans=none" : "");
2166 5645b0f4 Markus Armbruster
                    drive_add(IF_DEFAULT, 0, optarg, buf);
2167 5645b0f4 Markus Armbruster
                    break;
2168 5645b0f4 Markus Armbruster
                }
2169 cd6f1169 bellard
            case QEMU_OPTION_hdb:
2170 cc1daa40 bellard
            case QEMU_OPTION_hdc:
2171 cc1daa40 bellard
            case QEMU_OPTION_hdd:
2172 2292ddae Markus Armbruster
                drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2173 2292ddae Markus Armbruster
                          HD_OPTS);
2174 fc01f7e7 bellard
                break;
2175 e4bcb14c ths
            case QEMU_OPTION_drive:
2176 2292ddae Markus Armbruster
                drive_def(optarg);
2177 e4bcb14c ths
                break;
2178 d058fe03 Gerd Hoffmann
            case QEMU_OPTION_set:
2179 d058fe03 Gerd Hoffmann
                if (qemu_set_option(optarg) != 0)
2180 d058fe03 Gerd Hoffmann
                    exit(1);
2181 d058fe03 Gerd Hoffmann
                break;
2182 d0fef6fb Gerd Hoffmann
            case QEMU_OPTION_global:
2183 d0fef6fb Gerd Hoffmann
                if (qemu_global_option(optarg) != 0)
2184 d0fef6fb Gerd Hoffmann
                    exit(1);
2185 d0fef6fb Gerd Hoffmann
                break;
2186 3e3d5815 balrog
            case QEMU_OPTION_mtdblock:
2187 2292ddae Markus Armbruster
                drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2188 3e3d5815 balrog
                break;
2189 a1bb27b1 pbrook
            case QEMU_OPTION_sd:
2190 2292ddae Markus Armbruster
                drive_add(IF_SD, 0, optarg, SD_OPTS);
2191 a1bb27b1 pbrook
                break;
2192 86f55663 j_mayer
            case QEMU_OPTION_pflash:
2193 2292ddae Markus Armbruster
                drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2194 86f55663 j_mayer
                break;
2195 cd6f1169 bellard
            case QEMU_OPTION_snapshot:
2196 33e3963e bellard
                snapshot = 1;
2197 33e3963e bellard
                break;
2198 cd6f1169 bellard
            case QEMU_OPTION_hdachs:
2199 330d0414 bellard
                {
2200 330d0414 bellard
                    const char *p;
2201 330d0414 bellard
                    p = optarg;
2202 330d0414 bellard
                    cyls = strtol(p, (char **)&p, 0);
2203 46d4767d bellard
                    if (cyls < 1 || cyls > 16383)
2204 46d4767d bellard
                        goto chs_fail;
2205 330d0414 bellard
                    if (*p != ',')
2206 330d0414 bellard
                        goto chs_fail;
2207 330d0414 bellard
                    p++;
2208 330d0414 bellard
                    heads = strtol(p, (char **)&p, 0);
2209 46d4767d bellard
                    if (heads < 1 || heads > 16)
2210 46d4767d bellard
                        goto chs_fail;
2211 330d0414 bellard
                    if (*p != ',')
2212 330d0414 bellard
                        goto chs_fail;
2213 330d0414 bellard
                    p++;
2214 330d0414 bellard
                    secs = strtol(p, (char **)&p, 0);
2215 46d4767d bellard
                    if (secs < 1 || secs > 63)
2216 46d4767d bellard
                        goto chs_fail;
2217 46d4767d bellard
                    if (*p == ',') {
2218 46d4767d bellard
                        p++;
2219 46d4767d bellard
                        if (!strcmp(p, "none"))
2220 46d4767d bellard
                            translation = BIOS_ATA_TRANSLATION_NONE;
2221 46d4767d bellard
                        else if (!strcmp(p, "lba"))
2222 46d4767d bellard
                            translation = BIOS_ATA_TRANSLATION_LBA;
2223 46d4767d bellard
                        else if (!strcmp(p, "auto"))
2224 46d4767d bellard
                            translation = BIOS_ATA_TRANSLATION_AUTO;
2225 46d4767d bellard
                        else
2226 46d4767d bellard
                            goto chs_fail;
2227 46d4767d bellard
                    } else if (*p != '\0') {
2228 c4b1fcc0 bellard
                    chs_fail:
2229 46d4767d bellard
                        fprintf(stderr, "qemu: invalid physical CHS format\n");
2230 46d4767d bellard
                        exit(1);
2231 c4b1fcc0 bellard
                    }
2232 9dfd7c7a Gerd Hoffmann
                    if (hda_opts != NULL) {
2233 9dfd7c7a Gerd Hoffmann
                        char num[16];
2234 9dfd7c7a Gerd Hoffmann
                        snprintf(num, sizeof(num), "%d", cyls);
2235 9dfd7c7a Gerd Hoffmann
                        qemu_opt_set(hda_opts, "cyls", num);
2236 9dfd7c7a Gerd Hoffmann
                        snprintf(num, sizeof(num), "%d", heads);
2237 9dfd7c7a Gerd Hoffmann
                        qemu_opt_set(hda_opts, "heads", num);
2238 9dfd7c7a Gerd Hoffmann
                        snprintf(num, sizeof(num), "%d", secs);
2239 9dfd7c7a Gerd Hoffmann
                        qemu_opt_set(hda_opts, "secs", num);
2240 9dfd7c7a Gerd Hoffmann
                        if (translation == BIOS_ATA_TRANSLATION_LBA)
2241 9dfd7c7a Gerd Hoffmann
                            qemu_opt_set(hda_opts, "trans", "lba");
2242 9dfd7c7a Gerd Hoffmann
                        if (translation == BIOS_ATA_TRANSLATION_NONE)
2243 9dfd7c7a Gerd Hoffmann
                            qemu_opt_set(hda_opts, "trans", "none");
2244 9dfd7c7a Gerd Hoffmann
                    }
2245 330d0414 bellard
                }
2246 330d0414 bellard
                break;
2247 268a362c aliguori
            case QEMU_OPTION_numa:
2248 268a362c aliguori
                if (nb_numa_nodes >= MAX_NODES) {
2249 268a362c aliguori
                    fprintf(stderr, "qemu: too many NUMA nodes\n");
2250 268a362c aliguori
                    exit(1);
2251 268a362c aliguori
                }
2252 268a362c aliguori
                numa_add(optarg);
2253 268a362c aliguori
                break;
2254 1472a95b Jes Sorensen
            case QEMU_OPTION_display:
2255 1472a95b Jes Sorensen
                display_type = select_display(optarg);
2256 1472a95b Jes Sorensen
                break;
2257 cd6f1169 bellard
            case QEMU_OPTION_nographic:
2258 993fbfdb Anthony Liguori
                display_type = DT_NOGRAPHIC;
2259 a20dd508 bellard
                break;
2260 4d3b6f6e balrog
            case QEMU_OPTION_curses:
2261 47b05369 Jes Sorensen
#ifdef CONFIG_CURSES
2262 993fbfdb Anthony Liguori
                display_type = DT_CURSES;
2263 47b05369 Jes Sorensen
#else
2264 47b05369 Jes Sorensen
                fprintf(stderr, "Curses support is disabled\n");
2265 47b05369 Jes Sorensen
                exit(1);
2266 4d3b6f6e balrog
#endif
2267 47b05369 Jes Sorensen
                break;
2268 a171fe39 balrog
            case QEMU_OPTION_portrait:
2269 a171fe39 balrog
                graphic_rotate = 1;
2270 a171fe39 balrog
                break;
2271 cd6f1169 bellard
            case QEMU_OPTION_kernel:
2272 a20dd508 bellard
                kernel_filename = optarg;
2273 a20dd508 bellard
                break;
2274 cd6f1169 bellard
            case QEMU_OPTION_append:
2275 a20dd508 bellard
                kernel_cmdline = optarg;
2276 313aa567 bellard
                break;
2277 cd6f1169 bellard
            case QEMU_OPTION_cdrom:
2278 2292ddae Markus Armbruster
                drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2279 36b486bb bellard
                break;
2280 cd6f1169 bellard
            case QEMU_OPTION_boot:
2281 28c5af54 j_mayer
                {
2282 ef3adf68 Jan Kiszka
                    static const char * const params[] = {
2283 95387491 Jan Kiszka
                        "order", "once", "menu", NULL
2284 ef3adf68 Jan Kiszka
                    };
2285 ef3adf68 Jan Kiszka
                    char buf[sizeof(boot_devices)];
2286 e0f084bf Jan Kiszka
                    char *standard_boot_devices;
2287 ef3adf68 Jan Kiszka
                    int legacy = 0;
2288 ef3adf68 Jan Kiszka
2289 ef3adf68 Jan Kiszka
                    if (!strchr(optarg, '=')) {
2290 ef3adf68 Jan Kiszka
                        legacy = 1;
2291 ef3adf68 Jan Kiszka
                        pstrcpy(buf, sizeof(buf), optarg);
2292 ef3adf68 Jan Kiszka
                    } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2293 ef3adf68 Jan Kiszka
                        fprintf(stderr,
2294 ef3adf68 Jan Kiszka
                                "qemu: unknown boot parameter '%s' in '%s'\n",
2295 ef3adf68 Jan Kiszka
                                buf, optarg);
2296 ef3adf68 Jan Kiszka
                        exit(1);
2297 ef3adf68 Jan Kiszka
                    }
2298 ef3adf68 Jan Kiszka
2299 ef3adf68 Jan Kiszka
                    if (legacy ||
2300 ef3adf68 Jan Kiszka
                        get_param_value(buf, sizeof(buf), "order", optarg)) {
2301 4e9e9d6e Eduardo Habkost
                        validate_bootdevices(buf);
2302 ef3adf68 Jan Kiszka
                        pstrcpy(boot_devices, sizeof(boot_devices), buf);
2303 28c5af54 j_mayer
                    }
2304 e0f084bf Jan Kiszka
                    if (!legacy) {
2305 e0f084bf Jan Kiszka
                        if (get_param_value(buf, sizeof(buf),
2306 e0f084bf Jan Kiszka
                                            "once", optarg)) {
2307 4e9e9d6e Eduardo Habkost
                            validate_bootdevices(buf);
2308 e0f084bf Jan Kiszka
                            standard_boot