qemu: drive removal support (Marcelo Tosatti)
[qemu] / vl.c
diff --git a/vl.c b/vl.c
index ae2d127..2198151 100644 (file)
--- a/vl.c
+++ b/vl.c
 #  define LOG_IOPORT(...) do { } while (0)
 #endif
 
-#ifdef TARGET_PPC
-#define DEFAULT_RAM_SIZE 144
-#else
 #define DEFAULT_RAM_SIZE 128
-#endif
 
 /* Max number of USB devices that can be specified on the commandline.  */
 #define MAX_USB_CMDLINE 8
@@ -250,6 +246,7 @@ static int nb_drives_opt;
 static struct drive_opt {
     const char *file;
     char opt[1024];
+    int used;
 } drives_opt[MAX_DRIVES];
 
 static CPUState *cur_cpu;
@@ -263,6 +260,7 @@ static int icount_time_shift;
 static int64_t qemu_icount_bias;
 static QEMUTimer *icount_rt_timer;
 static QEMUTimer *icount_vm_timer;
+static QEMUTimer *nographic_timer;
 
 uint8_t qemu_uuid[16];
 
@@ -549,8 +547,6 @@ QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
     QEMUPutMouseEntry *s, *cursor;
 
     s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
-    if (!s)
-        return NULL;
 
     s->qemu_put_mouse_event = func;
     s->qemu_put_mouse_event_opaque = opaque;
@@ -1101,8 +1097,6 @@ static QEMUClock *qemu_new_clock(int type)
 {
     QEMUClock *clock;
     clock = qemu_mallocz(sizeof(QEMUClock));
-    if (!clock)
-        return NULL;
     clock->type = type;
     return clock;
 }
@@ -2142,22 +2136,56 @@ static int bt_parse(const char *opt)
 #define MTD_ALIAS "if=mtd"
 #define SD_ALIAS "index=0,if=sd"
 
+static int drive_opt_get_free_idx(void)
+{
+    int index;
+
+    for (index = 0; index < MAX_DRIVES; index++)
+        if (!drives_opt[index].used) {
+            drives_opt[index].used = 1;
+            return index;
+        }
+
+    return -1;
+}
+
+static int drive_get_free_idx(void)
+{
+    int index;
+
+    for (index = 0; index < MAX_DRIVES; index++)
+        if (!drives_table[index].used) {
+            drives_table[index].used = 1;
+            return index;
+        }
+
+    return -1;
+}
+
 static int drive_add(const char *file, const char *fmt, ...)
 {
     va_list ap;
+    int index = drive_opt_get_free_idx();
 
-    if (nb_drives_opt >= MAX_DRIVES) {
+    if (nb_drives_opt >= MAX_DRIVES || index == -1) {
         fprintf(stderr, "qemu: too many drives\n");
         exit(1);
     }
 
-    drives_opt[nb_drives_opt].file = file;
+    drives_opt[index].file = file;
     va_start(ap, fmt);
-    vsnprintf(drives_opt[nb_drives_opt].opt,
+    vsnprintf(drives_opt[index].opt,
               sizeof(drives_opt[0].opt), fmt, ap);
     va_end(ap);
 
-    return nb_drives_opt++;
+    nb_drives_opt++;
+    return index;
+}
+
+void drive_remove(int index)
+{
+    drives_opt[index].used = 0;
+    nb_drives_opt--;
 }
 
 int drive_get_index(BlockInterfaceType type, int bus, int unit)
@@ -2166,10 +2194,11 @@ int drive_get_index(BlockInterfaceType type, int bus, int unit)
 
     /* seek interface, bus and unit */
 
-    for (index = 0; index < nb_drives; index++)
+    for (index = 0; index < MAX_DRIVES; index++)
         if (drives_table[index].type == type &&
            drives_table[index].bus == bus &&
-           drives_table[index].unit == unit)
+           drives_table[index].unit == unit &&
+           drives_table[index].used)
         return index;
 
     return -1;
@@ -2200,11 +2229,36 @@ const char *drive_get_serial(BlockDriverState *bdrv)
     return "\0";
 }
 
+BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
+{
+    int index;
+
+    for (index = 0; index < nb_drives; index++)
+        if (drives_table[index].bdrv == bdrv)
+            return drives_table[index].onerror;
+
+    return BLOCK_ERR_REPORT;
+}
+
 static void bdrv_format_print(void *opaque, const char *name)
 {
     fprintf(stderr, " %s", name);
 }
 
+void drive_uninit(BlockDriverState *bdrv)
+{
+    int i;
+
+    for (i = 0; i < MAX_DRIVES; i++)
+        if (drives_table[i].bdrv == bdrv) {
+            drives_table[i].bdrv = NULL;
+            drives_table[i].used = 0;
+            drive_remove(drives_table[i].drive_opt_idx);
+            nb_drives--;
+            break;
+        }
+}
+
 static int drive_init(struct drive_opt *arg, int snapshot,
                       QEMUMachine *machine)
 {
@@ -2222,12 +2276,14 @@ static int drive_init(struct drive_opt *arg, int snapshot,
     int max_devs;
     int index;
     int cache;
-    int bdrv_flags;
+    int bdrv_flags, onerror;
+    int drives_table_idx;
     char *str = arg->opt;
     static const char * const params[] = { "bus", "unit", "if", "index",
                                            "cyls", "heads", "secs", "trans",
                                            "media", "snapshot", "file",
-                                           "cache", "format", "serial", NULL };
+                                           "cache", "format", "serial", "werror",
+                                           NULL };
 
     if (check_params(buf, sizeof(buf), params, str) < 0) {
          fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
@@ -2417,6 +2473,26 @@ static int drive_init(struct drive_opt *arg, int snapshot,
     if (!get_param_value(serial, sizeof(serial), "serial", str))
            memset(serial, 0,  sizeof(serial));
 
+    onerror = BLOCK_ERR_REPORT;
+    if (get_param_value(buf, sizeof(serial), "werror", str)) {
+        if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
+            fprintf(stderr, "werror is no supported by this format\n");
+            return -1;
+        }
+        if (!strcmp(buf, "ignore"))
+            onerror = BLOCK_ERR_IGNORE;
+        else if (!strcmp(buf, "enospc"))
+            onerror = BLOCK_ERR_STOP_ENOSPC;
+        else if (!strcmp(buf, "stop"))
+            onerror = BLOCK_ERR_STOP_ANY;
+        else if (!strcmp(buf, "report"))
+            onerror = BLOCK_ERR_REPORT;
+        else {
+            fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
+            return -1;
+        }
+    }
+
     /* compute bus and unit according index */
 
     if (index != -1) {
@@ -2476,10 +2552,13 @@ static int drive_init(struct drive_opt *arg, int snapshot,
         snprintf(buf, sizeof(buf), "%s%s%i",
                  devname, mediastr, unit_id);
     bdrv = bdrv_new(buf);
-    drives_table[nb_drives].bdrv = bdrv;
-    drives_table[nb_drives].type = type;
-    drives_table[nb_drives].bus = bus_id;
-    drives_table[nb_drives].unit = unit_id;
+    drives_table_idx = drive_get_free_idx();
+    drives_table[drives_table_idx].bdrv = bdrv;
+    drives_table[drives_table_idx].type = type;
+    drives_table[drives_table_idx].bus = bus_id;
+    drives_table[drives_table_idx].unit = unit_id;
+    drives_table[drives_table_idx].onerror = onerror;
+    drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
     strncpy(drives_table[nb_drives].serial, serial, sizeof(serial));
     nb_drives++;
 
@@ -2775,25 +2854,11 @@ DisplayState *get_displaystate(void)
 
 /* dumb display */
 
-static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
-{
-}
-
-static void dumb_resize(DisplayState *ds)
+static void dumb_display_init(void)
 {
-}
-
-static void dumb_display_init(DisplayState *ds)
-{
-    DisplayChangeListener *dcl = qemu_mallocz(sizeof(DisplayChangeListener));
-    if (!dcl)
-        exit(1);
-    dcl->dpy_update = dumb_update;
-    dcl->dpy_resize = dumb_resize;
-    dcl->dpy_refresh = NULL;
-    dcl->idle = 1;
-    dcl->gui_timer_interval = 500;
-    register_displaychangelistener(ds, dcl);
+    DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
+    ds->surface = qemu_create_displaysurface(640, 480, 32, 640 * 4);
+    register_displaystate(ds);
 }
 
 /***********************************************************/
@@ -2843,8 +2908,6 @@ int qemu_set_fd_handler2(int fd,
                 goto found;
         }
         ioh = qemu_mallocz(sizeof(IOHandlerRecord));
-        if (!ioh)
-            return -1;
         ioh->next = first_io_handler;
         first_io_handler = ioh;
     found:
@@ -2882,8 +2945,6 @@ int qemu_add_polling_cb(PollingFunc *func, void *opaque)
 {
     PollingEntry **ppe, *pe;
     pe = qemu_mallocz(sizeof(PollingEntry));
-    if (!pe)
-        return -1;
     pe->func = func;
     pe->opaque = opaque;
     for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
@@ -3253,8 +3314,6 @@ QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
 {
     QEMUBH *bh;
     bh = qemu_mallocz(sizeof(QEMUBH));
-    if (!bh)
-        return NULL;
     bh->cb = cb;
     bh->opaque = opaque;
     bh->next = first_bh;
@@ -3391,6 +3450,13 @@ static void gui_update(void *opaque)
     qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
 }
 
+static void nographic_update(void *opaque)
+{
+    uint64_t interval = GUI_REFRESH_INTERVAL;
+
+    qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
+}
+
 struct vm_change_state_entry {
     VMChangeStateHandler *cb;
     void *opaque;
@@ -3405,8 +3471,6 @@ VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
     VMChangeStateEntry *e;
 
     e = qemu_mallocz(sizeof (*e));
-    if (!e)
-        return NULL;
 
     e->cb = cb;
     e->opaque = opaque;
@@ -3420,37 +3484,21 @@ void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
     qemu_free (e);
 }
 
-static void vm_state_notify(int running)
+static void vm_state_notify(int running, int reason)
 {
     VMChangeStateEntry *e;
 
     for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
-        e->cb(e->opaque, running);
+        e->cb(e->opaque, running, reason);
     }
 }
 
-/* XXX: support several handlers */
-static VMStopHandler *vm_stop_cb;
-static void *vm_stop_opaque;
-
-int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
-{
-    vm_stop_cb = cb;
-    vm_stop_opaque = opaque;
-    return 0;
-}
-
-void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
-{
-    vm_stop_cb = NULL;
-}
-
 void vm_start(void)
 {
     if (!vm_running) {
         cpu_enable_ticks();
         vm_running = 1;
-        vm_state_notify(1);
+        vm_state_notify(1, 0);
         qemu_rearm_alarm_timer(alarm_timer);
     }
 }
@@ -3460,12 +3508,7 @@ void vm_stop(int reason)
     if (vm_running) {
         cpu_disable_ticks();
         vm_running = 0;
-        if (reason != 0) {
-            if (vm_stop_cb) {
-                vm_stop_cb(vm_stop_opaque, reason);
-            }
-        }
-        vm_state_notify(0);
+        vm_state_notify(0, reason);
     }
 }
 
@@ -3845,14 +3888,18 @@ static int main_loop(void)
 
 static void help(int exitcode)
 {
+    /* Please keep in synch with QEMU_OPTION_ enums, qemu_options[]
+       and qemu-doc.texi */
     printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
            "usage: %s [options] [disk_image]\n"
            "\n"
            "'disk_image' is a raw hard image image for IDE hard disk 0\n"
            "\n"
            "Standard options:\n"
+           "-h or -help     display this help and exit\n"
            "-M machine      select emulated machine (-M ? for list)\n"
            "-cpu cpu        select CPU (-cpu ? for list)\n"
+           "-smp n          set the number of CPUs to 'n' [default=1]\n"
            "-fda/-fdb file  use 'file' as floppy disk 0/1 image\n"
            "-hda/-hdb file  use 'file' as IDE hard disk 0/1 image\n"
            "-hdc/-hdd file  use 'file' as IDE hard disk 2/3 image\n"
@@ -3866,19 +3913,7 @@ static void help(int exitcode)
            "-pflash file    use 'file' as a parallel flash image\n"
            "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
            "-snapshot       write to temporary files instead of disk image files\n"
-#ifdef CONFIG_SDL
-           "-no-frame       open SDL window without a frame and window decorations\n"
-           "-alt-grab       use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
-           "-no-quit        disable SDL window close capability\n"
-           "-sdl            enable SDL\n"
-#endif
-#ifdef TARGET_I386
-           "-no-fd-bootchk  disable boot signature checking for floppy disks\n"
-#endif
            "-m megs         set virtual RAM size to megs MB [default=%d]\n"
-           "-smp n          set the number of CPUs to 'n' [default=1]\n"
-           "-nographic      disable graphical output and redirect serial I/Os to console\n"
-           "-portrait       rotate graphical output 90 deg left (only PXA LCD)\n"
 #ifndef _WIN32
            "-k language     use keyboard layout (for example \"fr\" for French)\n"
 #endif
@@ -3889,21 +3924,31 @@ static void help(int exitcode)
            "                use -soundhw ? to get the list of supported cards\n"
            "                use -soundhw all to enable all of them\n"
 #endif
+           "-usb            enable the USB driver (will be the default soon)\n"
+           "-usbdevice name add the host or guest USB device 'name'\n"
+           "-name string    set the name of the guest\n"
+           "-uuid %%08x-%%04x-%%04x-%%04x-%%012x\n"
+           "                specify machine UUID\n"
+           "\n"
+           "Display options:\n"
+           "-nographic      disable graphical output and redirect serial I/Os to console\n"
+#ifdef CONFIG_CURSES
+           "-curses         use a curses/ncurses interface instead of SDL\n"
+#endif
+#ifdef CONFIG_SDL
+           "-no-frame       open SDL window without a frame and window decorations\n"
+           "-alt-grab       use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
+           "-no-quit        disable SDL window close capability\n"
+           "-sdl            enable SDL\n"
+#endif
+           "-portrait       rotate graphical output 90 deg left (only PXA LCD)\n"
            "-vga [std|cirrus|vmware|none]\n"
            "                select video card type\n"
-           "-localtime      set the real time clock to local time [default=utc]\n"
            "-full-screen    start in full screen\n"
-#ifdef TARGET_I386
-           "-win2k-hack     use it when installing Windows 2000 to avoid a disk full bug\n"
-           "-rtc-td-hack    use it to fix time drift in Windows ACPI HAL\n"
-#endif
-           "-usb            enable the USB driver (will be the default soon)\n"
-           "-usbdevice name add the host or guest USB device 'name'\n"
 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
            "-g WxH[xDEPTH]  Set the initial graphical resolution and depth\n"
 #endif
-           "-name string    set the name of the guest\n"
-           "-uuid %%08x-%%04x-%%04x-%%04x-%%012x specify machine UUID\n"
+           "-vnc display    start a VNC server on display\n"
            "\n"
            "Network options:\n"
            "-net nic[,vlan=n][,macaddr=addr][,model=type][,name=str]\n"
@@ -3937,17 +3982,6 @@ static void help(int exitcode)
 #endif
            "-net none       use it alone to have zero network devices; if no -net option\n"
            "                is provided, the default is '-net nic -net user'\n"
-           "\n"
-           "-bt hci,null    Dumb bluetooth HCI - doesn't respond to commands\n"
-           "-bt hci,host[:id]\n"
-           "                Use host's HCI with the given name\n"
-           "-bt hci[,vlan=n]\n"
-           "                Emulate a standard HCI in virtual scatternet 'n'\n"
-           "-bt vhci[,vlan=n]\n"
-           "                Add host computer to virtual scatternet 'n' using VHCI\n"
-           "-bt device:dev[,vlan=n]\n"
-           "                Emulate a bluetooth device 'dev' in scatternet 'n'\n"
-           "\n"
 #ifdef CONFIG_SLIRP
            "-tftp dir       allow tftp access to files in dir [-net user]\n"
            "-bootp file     advertise file in BOOTP replies\n"
@@ -3958,23 +3992,44 @@ static void help(int exitcode)
            "                redirect TCP or UDP connections from host to guest [-net user]\n"
 #endif
            "\n"
+           "-bt hci,null    dumb bluetooth HCI - doesn't respond to commands\n"
+           "-bt hci,host[:id]\n"
+           "                use host's HCI with the given name\n"
+           "-bt hci[,vlan=n]\n"
+           "                emulate a standard HCI in virtual scatternet 'n'\n"
+           "-bt vhci[,vlan=n]\n"
+           "                add host computer to virtual scatternet 'n' using VHCI\n"
+           "-bt device:dev[,vlan=n]\n"
+           "                emulate a bluetooth device 'dev' in scatternet 'n'\n"
+           "\n"
+#ifdef TARGET_I386
+           "\n"
+           "i386 target only:\n"
+           "-win2k-hack     use it when installing Windows 2000 to avoid a disk full bug\n"
+           "-rtc-td-hack    use it to fix time drift in Windows ACPI HAL\n"
+           "-no-fd-bootchk  disable boot signature checking for floppy disks\n"
+           "-no-acpi        disable ACPI\n"
+           "-no-hpet        disable HPET\n"
+#endif
            "Linux boot specific:\n"
            "-kernel bzImage use 'bzImage' as kernel image\n"
            "-append cmdline use 'cmdline' as kernel command line\n"
            "-initrd file    use 'file' as initial ram disk\n"
            "\n"
            "Debug/Expert options:\n"
-           "-monitor dev    redirect the monitor to char device 'dev'\n"
            "-serial dev     redirect the serial port to char device 'dev'\n"
            "-parallel dev   redirect the parallel port to char device 'dev'\n"
-           "-pidfile file   Write PID to 'file'\n"
+           "-monitor dev    redirect the monitor to char device 'dev'\n"
+           "-pidfile file   write PID to 'file'\n"
            "-S              freeze CPU at startup (use 'c' to start execution)\n"
            "-s              wait gdb connection to port\n"
            "-p port         set gdb connection port [default=%s]\n"
            "-d item1,...    output log to %s (use -d ? for a list of log items)\n"
-           "-hdachs c,h,s[,t]  force hard disk 0 physical geometry and the optional BIOS\n"
+           "-hdachs c,h,s[,t]\n"
+           "                force hard disk 0 physical geometry and the optional BIOS\n"
            "                translation (t=none or lba) (usually qemu can guess them)\n"
            "-L path         set the directory for the BIOS, VGA BIOS and keymaps\n"
+           "-bios file      set the filename for the BIOS\n"
 #ifdef USE_KQEMU
            "-kernel-kqemu   enable KQEMU full virtualization (default is user mode only)\n"
            "-no-kqemu       disable KQEMU kernel module usage\n"
@@ -3982,29 +4037,36 @@ static void help(int exitcode)
 #ifdef CONFIG_KVM
            "-enable-kvm     enable KVM full virtualization support\n"
 #endif
-#ifdef TARGET_I386
-           "-no-acpi        disable ACPI\n"
-           "-no-hpet        disable HPET\n"
-#endif
-#ifdef CONFIG_CURSES
-           "-curses         use a curses/ncurses interface instead of SDL\n"
-#endif
            "-no-reboot      exit instead of rebooting\n"
            "-no-shutdown    stop before shutdown\n"
-           "-loadvm [tag|id]  start right away with a saved state (loadvm in monitor)\n"
-          "-vnc display    start a VNC server on display\n"
+           "-loadvm [tag|id]\n"
+           "                start right away with a saved state (loadvm in monitor)\n"
 #ifndef _WIN32
           "-daemonize      daemonize QEMU after initializing\n"
 #endif
           "-option-rom rom load a file, rom, into the option ROM space\n"
-#ifdef TARGET_SPARC
-           "-prom-env variable=value  set OpenBIOS nvram variables\n"
+#if defined(TARGET_SPARC) || defined(TARGET_PPC)
+           "-prom-env variable=value\n"
+           "                set OpenBIOS nvram variables\n"
 #endif
            "-clock          force the use of the given methods for timer alarm.\n"
            "                To see what timers are available use -clock ?\n"
+           "-localtime      set the real time clock to local time [default=utc]\n"
            "-startdate      select initial date of the clock\n"
            "-icount [N|auto]\n"
-           "                Enable virtual instruction counter with 2^N clock ticks per instruction\n"
+           "                enable virtual instruction counter with 2^N clock ticks per instruction\n"
+           "-echr chr       set terminal escape character instead of ctrl-a\n"
+           "-virtioconsole c\n"
+           "                set virtio console\n"
+           "-show-cursor    show cursor\n"
+#if defined(TARGET_ARM) || defined(TARGET_M68K)
+           "-semihosting    semihosting mode\n"
+#endif
+#if defined(TARGET_ARM)
+           "-old-param      old param mode\n"
+#endif
+           "-tb-size n      set TB size\n"
+           "-incoming p     prepare for incoming migration, listen on port p\n"
            "\n"
            "During emulation, the following keys are useful:\n"
            "ctrl-alt-f      toggle full screen\n"
@@ -4027,34 +4089,49 @@ static void help(int exitcode)
 #define HAS_ARG 0x0001
 
 enum {
+    /* Please keep in synch with help, qemu_options[] and
+       qemu-doc.texi */
+    /* Standard options: */
     QEMU_OPTION_h,
-
     QEMU_OPTION_M,
     QEMU_OPTION_cpu,
+    QEMU_OPTION_smp,
     QEMU_OPTION_fda,
     QEMU_OPTION_fdb,
     QEMU_OPTION_hda,
     QEMU_OPTION_hdb,
     QEMU_OPTION_hdc,
     QEMU_OPTION_hdd,
-    QEMU_OPTION_drive,
     QEMU_OPTION_cdrom,
+    QEMU_OPTION_drive,
     QEMU_OPTION_mtdblock,
     QEMU_OPTION_sd,
     QEMU_OPTION_pflash,
     QEMU_OPTION_boot,
     QEMU_OPTION_snapshot,
-#ifdef TARGET_I386
-    QEMU_OPTION_no_fd_bootchk,
-#endif
     QEMU_OPTION_m,
-    QEMU_OPTION_nographic,
-    QEMU_OPTION_portrait,
-#ifdef HAS_AUDIO
+    QEMU_OPTION_k,
     QEMU_OPTION_audio_help,
     QEMU_OPTION_soundhw,
-#endif
+    QEMU_OPTION_usb,
+    QEMU_OPTION_usbdevice,
+    QEMU_OPTION_name,
+    QEMU_OPTION_uuid,
 
+    /* Display options: */
+    QEMU_OPTION_nographic,
+    QEMU_OPTION_curses,
+    QEMU_OPTION_no_frame,
+    QEMU_OPTION_alt_grab,
+    QEMU_OPTION_no_quit,
+    QEMU_OPTION_sdl,
+    QEMU_OPTION_portrait,
+    QEMU_OPTION_vga,
+    QEMU_OPTION_full_screen,
+    QEMU_OPTION_g,
+    QEMU_OPTION_vnc,
+
+    /* Network options: */
     QEMU_OPTION_net,
     QEMU_OPTION_tftp,
     QEMU_OPTION_bootp,
@@ -4062,10 +4139,23 @@ enum {
     QEMU_OPTION_redir,
     QEMU_OPTION_bt,
 
+    /* i386 target only: */
+    QEMU_OPTION_win2k_hack,
+    QEMU_OPTION_rtc_td_hack,
+    QEMU_OPTION_no_fd_bootchk,
+    QEMU_OPTION_no_acpi,
+    QEMU_OPTION_no_hpet,
+
+    /* Linux boot specific: */
     QEMU_OPTION_kernel,
     QEMU_OPTION_append,
     QEMU_OPTION_initrd,
 
+    /* Debug/Expert options: */
+    QEMU_OPTION_serial,
+    QEMU_OPTION_parallel,
+    QEMU_OPTION_monitor,
+    QEMU_OPTION_pidfile,
     QEMU_OPTION_S,
     QEMU_OPTION_s,
     QEMU_OPTION_p,
@@ -4073,48 +4163,25 @@ enum {
     QEMU_OPTION_hdachs,
     QEMU_OPTION_L,
     QEMU_OPTION_bios,
-    QEMU_OPTION_k,
-    QEMU_OPTION_localtime,
-    QEMU_OPTION_g,
-    QEMU_OPTION_vga,
-    QEMU_OPTION_echr,
-    QEMU_OPTION_monitor,
-    QEMU_OPTION_serial,
-    QEMU_OPTION_virtiocon,
-    QEMU_OPTION_parallel,
-    QEMU_OPTION_loadvm,
-    QEMU_OPTION_full_screen,
-    QEMU_OPTION_no_frame,
-    QEMU_OPTION_alt_grab,
-    QEMU_OPTION_no_quit,
-    QEMU_OPTION_sdl,
-    QEMU_OPTION_pidfile,
-    QEMU_OPTION_no_kqemu,
     QEMU_OPTION_kernel_kqemu,
+    QEMU_OPTION_no_kqemu,
     QEMU_OPTION_enable_kvm,
-    QEMU_OPTION_win2k_hack,
-    QEMU_OPTION_rtc_td_hack,
-    QEMU_OPTION_usb,
-    QEMU_OPTION_usbdevice,
-    QEMU_OPTION_smp,
-    QEMU_OPTION_vnc,
-    QEMU_OPTION_no_acpi,
-    QEMU_OPTION_no_hpet,
-    QEMU_OPTION_curses,
     QEMU_OPTION_no_reboot,
     QEMU_OPTION_no_shutdown,
-    QEMU_OPTION_show_cursor,
+    QEMU_OPTION_loadvm,
     QEMU_OPTION_daemonize,
     QEMU_OPTION_option_rom,
-    QEMU_OPTION_semihosting,
-    QEMU_OPTION_name,
     QEMU_OPTION_prom_env,
-    QEMU_OPTION_old_param,
     QEMU_OPTION_clock,
+    QEMU_OPTION_localtime,
     QEMU_OPTION_startdate,
-    QEMU_OPTION_tb_size,
     QEMU_OPTION_icount,
-    QEMU_OPTION_uuid,
+    QEMU_OPTION_echr,
+    QEMU_OPTION_virtiocon,
+    QEMU_OPTION_show_cursor,
+    QEMU_OPTION_semihosting,
+    QEMU_OPTION_old_param,
+    QEMU_OPTION_tb_size,
     QEMU_OPTION_incoming,
 };
 
@@ -4125,36 +4192,60 @@ typedef struct QEMUOption {
 } QEMUOption;
 
 static const QEMUOption qemu_options[] = {
+    /* Please keep in synch with help, QEMU_OPTION_ enums, and
+       qemu-doc.texi */
+    /* Standard options: */
     { "h", 0, QEMU_OPTION_h },
     { "help", 0, QEMU_OPTION_h },
-
     { "M", HAS_ARG, QEMU_OPTION_M },
     { "cpu", HAS_ARG, QEMU_OPTION_cpu },
+    { "smp", HAS_ARG, QEMU_OPTION_smp },
     { "fda", HAS_ARG, QEMU_OPTION_fda },
     { "fdb", HAS_ARG, QEMU_OPTION_fdb },
     { "hda", HAS_ARG, QEMU_OPTION_hda },
     { "hdb", HAS_ARG, QEMU_OPTION_hdb },
     { "hdc", HAS_ARG, QEMU_OPTION_hdc },
     { "hdd", HAS_ARG, QEMU_OPTION_hdd },
-    { "drive", HAS_ARG, QEMU_OPTION_drive },
     { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
+    { "drive", HAS_ARG, QEMU_OPTION_drive },
     { "mtdblock", HAS_ARG, QEMU_OPTION_mtdblock },
     { "sd", HAS_ARG, QEMU_OPTION_sd },
     { "pflash", HAS_ARG, QEMU_OPTION_pflash },
     { "boot", HAS_ARG, QEMU_OPTION_boot },
     { "snapshot", 0, QEMU_OPTION_snapshot },
-#ifdef TARGET_I386
-    { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk },
-#endif
     { "m", HAS_ARG, QEMU_OPTION_m },
-    { "nographic", 0, QEMU_OPTION_nographic },
-    { "portrait", 0, QEMU_OPTION_portrait },
+#ifndef _WIN32
     { "k", HAS_ARG, QEMU_OPTION_k },
+#endif
 #ifdef HAS_AUDIO
     { "audio-help", 0, QEMU_OPTION_audio_help },
     { "soundhw", HAS_ARG, QEMU_OPTION_soundhw },
 #endif
+    { "usb", 0, QEMU_OPTION_usb },
+    { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice },
+    { "name", HAS_ARG, QEMU_OPTION_name },
+    { "uuid", HAS_ARG, QEMU_OPTION_uuid },
+
+    /* Display options: */
+    { "nographic", 0, QEMU_OPTION_nographic },
+#ifdef CONFIG_CURSES
+    { "curses", 0, QEMU_OPTION_curses },
+#endif
+#ifdef CONFIG_SDL
+    { "no-frame", 0, QEMU_OPTION_no_frame },
+    { "alt-grab", 0, QEMU_OPTION_alt_grab },
+    { "no-quit", 0, QEMU_OPTION_no_quit },
+    { "sdl", 0, QEMU_OPTION_sdl },
+#endif
+    { "portrait", 0, QEMU_OPTION_portrait },
+    { "vga", HAS_ARG, QEMU_OPTION_vga },
+    { "full-screen", 0, QEMU_OPTION_full_screen },
+#if defined(TARGET_PPC) || defined(TARGET_SPARC)
+    { "g", 1, QEMU_OPTION_g },
+#endif
+    { "vnc", HAS_ARG, QEMU_OPTION_vnc },
 
+    /* Network options: */
     { "net", HAS_ARG, QEMU_OPTION_net},
 #ifdef CONFIG_SLIRP
     { "tftp", HAS_ARG, QEMU_OPTION_tftp },
@@ -4165,11 +4256,25 @@ static const QEMUOption qemu_options[] = {
     { "redir", HAS_ARG, QEMU_OPTION_redir },
 #endif
     { "bt", HAS_ARG, QEMU_OPTION_bt },
+#ifdef TARGET_I386
+    /* i386 target only: */
+    { "win2k-hack", 0, QEMU_OPTION_win2k_hack },
+    { "rtc-td-hack", 0, QEMU_OPTION_rtc_td_hack },
+    { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk },
+    { "no-acpi", 0, QEMU_OPTION_no_acpi },
+    { "no-hpet", 0, QEMU_OPTION_no_hpet },
+#endif
 
+    /* Linux boot specific: */
     { "kernel", HAS_ARG, QEMU_OPTION_kernel },
     { "append", HAS_ARG, QEMU_OPTION_append },
     { "initrd", HAS_ARG, QEMU_OPTION_initrd },
 
+    /* Debug/Expert options: */
+    { "serial", HAS_ARG, QEMU_OPTION_serial },
+    { "parallel", HAS_ARG, QEMU_OPTION_parallel },
+    { "monitor", HAS_ARG, QEMU_OPTION_monitor },
+    { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
     { "S", 0, QEMU_OPTION_S },
     { "s", 0, QEMU_OPTION_s },
     { "p", HAS_ARG, QEMU_OPTION_p },
@@ -4178,64 +4283,34 @@ static const QEMUOption qemu_options[] = {
     { "L", HAS_ARG, QEMU_OPTION_L },
     { "bios", HAS_ARG, QEMU_OPTION_bios },
 #ifdef USE_KQEMU
-    { "no-kqemu", 0, QEMU_OPTION_no_kqemu },
     { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu },
+    { "no-kqemu", 0, QEMU_OPTION_no_kqemu },
 #endif
 #ifdef CONFIG_KVM
     { "enable-kvm", 0, QEMU_OPTION_enable_kvm },
 #endif
-#if defined(TARGET_PPC) || defined(TARGET_SPARC)
-    { "g", 1, QEMU_OPTION_g },
+    { "no-reboot", 0, QEMU_OPTION_no_reboot },
+    { "no-shutdown", 0, QEMU_OPTION_no_shutdown },
+    { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
+    { "daemonize", 0, QEMU_OPTION_daemonize },
+    { "option-rom", HAS_ARG, QEMU_OPTION_option_rom },
+#if defined(TARGET_SPARC) || defined(TARGET_PPC)
+    { "prom-env", HAS_ARG, QEMU_OPTION_prom_env },
 #endif
+    { "clock", HAS_ARG, QEMU_OPTION_clock },
     { "localtime", 0, QEMU_OPTION_localtime },
-    { "vga", HAS_ARG, QEMU_OPTION_vga },
+    { "startdate", HAS_ARG, QEMU_OPTION_startdate },
+    { "icount", HAS_ARG, QEMU_OPTION_icount },
     { "echr", HAS_ARG, QEMU_OPTION_echr },
-    { "monitor", HAS_ARG, QEMU_OPTION_monitor },
-    { "serial", HAS_ARG, QEMU_OPTION_serial },
     { "virtioconsole", HAS_ARG, QEMU_OPTION_virtiocon },
-    { "parallel", HAS_ARG, QEMU_OPTION_parallel },
-    { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
-    { "full-screen", 0, QEMU_OPTION_full_screen },
-#ifdef CONFIG_SDL
-    { "no-frame", 0, QEMU_OPTION_no_frame },
-    { "alt-grab", 0, QEMU_OPTION_alt_grab },
-    { "no-quit", 0, QEMU_OPTION_no_quit },
-    { "sdl", 0, QEMU_OPTION_sdl },
-#endif
-    { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
-    { "win2k-hack", 0, QEMU_OPTION_win2k_hack },
-    { "rtc-td-hack", 0, QEMU_OPTION_rtc_td_hack },
-    { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice },
-    { "smp", HAS_ARG, QEMU_OPTION_smp },
-    { "vnc", HAS_ARG, QEMU_OPTION_vnc },
-#ifdef CONFIG_CURSES
-    { "curses", 0, QEMU_OPTION_curses },
-#endif
-    { "uuid", HAS_ARG, QEMU_OPTION_uuid },
-
-    /* temporary options */
-    { "usb", 0, QEMU_OPTION_usb },
-    { "no-acpi", 0, QEMU_OPTION_no_acpi },
-    { "no-hpet", 0, QEMU_OPTION_no_hpet },
-    { "no-reboot", 0, QEMU_OPTION_no_reboot },
-    { "no-shutdown", 0, QEMU_OPTION_no_shutdown },
     { "show-cursor", 0, QEMU_OPTION_show_cursor },
-    { "daemonize", 0, QEMU_OPTION_daemonize },
-    { "option-rom", HAS_ARG, QEMU_OPTION_option_rom },
 #if defined(TARGET_ARM) || defined(TARGET_M68K)
     { "semihosting", 0, QEMU_OPTION_semihosting },
 #endif
-    { "name", HAS_ARG, QEMU_OPTION_name },
-#if defined(TARGET_SPARC) || defined(TARGET_PPC)
-    { "prom-env", HAS_ARG, QEMU_OPTION_prom_env },
-#endif
 #if defined(TARGET_ARM)
     { "old-param", 0, QEMU_OPTION_old_param },
 #endif
-    { "clock", HAS_ARG, QEMU_OPTION_clock },
-    { "startdate", HAS_ARG, QEMU_OPTION_startdate },
     { "tb-size", HAS_ARG, QEMU_OPTION_tb_size },
-    { "icount", HAS_ARG, QEMU_OPTION_icount },
     { "incoming", HAS_ARG, QEMU_OPTION_incoming },
     { NULL },
 };
@@ -4529,7 +4604,7 @@ int main(int argc, char **argv, char **envp)
     int hda_index;
     int optind;
     const char *r, *optarg;
-    CharDriverState *monitor_hd;
+    CharDriverState *monitor_hd = NULL;
     const char *monitor_device;
     const char *serial_devices[MAX_SERIAL_PORTS];
     int serial_device_index;
@@ -5461,6 +5536,56 @@ int main(int argc, char **argv, char **envp)
         }
     }
 
+    if (monitor_device) {
+        monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
+        if (!monitor_hd) {
+            fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
+            exit(1);
+        }
+    }
+
+    for(i = 0; i < MAX_SERIAL_PORTS; i++) {
+        const char *devname = serial_devices[i];
+        if (devname && strcmp(devname, "none")) {
+            char label[32];
+            snprintf(label, sizeof(label), "serial%d", i);
+            serial_hds[i] = qemu_chr_open(label, devname, NULL);
+            if (!serial_hds[i]) {
+                fprintf(stderr, "qemu: could not open serial device '%s'\n",
+                        devname);
+                exit(1);
+            }
+        }
+    }
+
+    for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
+        const char *devname = parallel_devices[i];
+        if (devname && strcmp(devname, "none")) {
+            char label[32];
+            snprintf(label, sizeof(label), "parallel%d", i);
+            parallel_hds[i] = qemu_chr_open(label, devname, NULL);
+            if (!parallel_hds[i]) {
+                fprintf(stderr, "qemu: could not open parallel device '%s'\n",
+                        devname);
+                exit(1);
+            }
+        }
+    }
+
+    for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
+        const char *devname = virtio_consoles[i];
+        if (devname && strcmp(devname, "none")) {
+            char label[32];
+            snprintf(label, sizeof(label), "virtcon%d", i);
+            virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
+            if (!virtcon_hds[i]) {
+                fprintf(stderr, "qemu: could not open virtio console '%s'\n",
+                        devname);
+                exit(1);
+            }
+        }
+    }
+
     machine->init(ram_size, vga_ram_size, boot_devices,
                   kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
 
@@ -5485,6 +5610,8 @@ int main(int argc, char **argv, char **envp)
         }
     }
 
+    if (!display_state)
+        dumb_display_init();
     /* just use the first displaystate for the moment */
     ds = display_state;
     /* terminal init */
@@ -5493,8 +5620,6 @@ int main(int argc, char **argv, char **envp)
             fprintf(stderr, "fatal: -nographic can't be used with -curses\n");
             exit(1);
         }
-        /* nearly nothing to do */
-        dumb_display_init(ds);
     } else { 
 #if defined(CONFIG_CURSES)
             if (curses) {
@@ -5508,13 +5633,12 @@ int main(int argc, char **argv, char **envp)
                     if (vnc_display_open(ds, vnc_display) < 0)
                         exit(1);
                 }
-                if (sdl || !vnc_display)
 #if defined(CONFIG_SDL)
+                if (sdl || !vnc_display)
                     sdl_display_init(ds, full_screen, no_frame);
 #elif defined(CONFIG_COCOA)
+                if (sdl || !vnc_display)
                     cocoa_display_init(ds, full_screen);
-#else
-                    dumb_display_init(ds);
 #endif
             }
     }
@@ -5529,26 +5653,21 @@ int main(int argc, char **argv, char **envp)
         dcl = dcl->next;
     }
 
-    if (monitor_device) {
-        monitor_hd = qemu_chr_open("monitor", monitor_device);
-        if (!monitor_hd) {
-            fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
-            exit(1);
-        }
-        monitor_init(monitor_hd, !nographic);
+    if (nographic || (vnc_display && !sdl)) {
+        nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
+        qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
     }
 
+    text_consoles_set_display(display_state);
+
+    if (monitor_device && monitor_hd)
+        monitor_init(monitor_hd, !nographic);
+
     for(i = 0; i < MAX_SERIAL_PORTS; i++) {
         const char *devname = serial_devices[i];
         if (devname && strcmp(devname, "none")) {
             char label[32];
             snprintf(label, sizeof(label), "serial%d", i);
-            serial_hds[i] = qemu_chr_open(label, devname);
-            if (!serial_hds[i]) {
-                fprintf(stderr, "qemu: could not open serial device '%s'\n",
-                        devname);
-                exit(1);
-            }
             if (strstart(devname, "vc", 0))
                 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
         }
@@ -5559,12 +5678,6 @@ int main(int argc, char **argv, char **envp)
         if (devname && strcmp(devname, "none")) {
             char label[32];
             snprintf(label, sizeof(label), "parallel%d", i);
-            parallel_hds[i] = qemu_chr_open(label, devname);
-            if (!parallel_hds[i]) {
-                fprintf(stderr, "qemu: could not open parallel device '%s'\n",
-                        devname);
-                exit(1);
-            }
             if (strstart(devname, "vc", 0))
                 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
         }
@@ -5572,15 +5685,9 @@ int main(int argc, char **argv, char **envp)
 
     for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
         const char *devname = virtio_consoles[i];
-        if (devname && strcmp(devname, "none")) {
+        if (virtcon_hds[i] && devname) {
             char label[32];
             snprintf(label, sizeof(label), "virtcon%d", i);
-            virtcon_hds[i] = qemu_chr_open(label, devname);
-            if (!virtcon_hds[i]) {
-                fprintf(stderr, "qemu: could not open virtio console '%s'\n",
-                        devname);
-                exit(1);
-            }
             if (strstart(devname, "vc", 0))
                 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
         }