blob: 009d2aa68777cafc628c23bbb10e00444ad81bc4 [file] [log] [blame]
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25/* the following is needed on Linux to define ptsname() in stdlib.h */
26#if defined(__linux__)
27#define _GNU_SOURCE 1
28#endif
29
30#include "qemu-common.h"
31#include "hw/hw.h"
32#include "hw/boards.h"
33#include "hw/usb.h"
34#include "hw/pcmcia.h"
David 'Digit' Turner2ec695a2013-12-17 10:03:39 +010035#include "hw/i386/pc.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070036#include "hw/audiodev.h"
David 'Digit' Turner2ec695a2013-12-17 10:03:39 +010037#include "hw/isa/isa.h"
David 'Digit' Turner1365eb22014-02-16 22:23:26 +010038#include "hw/loader.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070039#include "hw/baum.h"
David 'Digit' Turnerf0665422013-12-17 10:47:09 +010040#include "hw/android/goldfish/nand.h"
David 'Digit' Turnercc330d42013-12-14 23:26:42 +010041#include "net/net.h"
David 'Digit' Turner1c31e3e2013-12-14 20:07:17 +010042#include "ui/console.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +010043#include "sysemu/sysemu.h"
David 'Digit' Turner852088c2013-12-14 23:04:12 +010044#include "exec/gdbstub.h"
David 'Digit' Turner7e9f6c12014-01-22 18:42:40 +010045#include "qemu/log.h"
David 'Digit' Turner7a78db72013-12-14 11:46:01 +010046#include "qemu/timer.h"
David 'Digit' Turnere7216d82013-12-15 00:51:13 +010047#include "sysemu/char.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +010048#include "sysemu/blockdev.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070049#include "audio/audio.h"
50
David 'Digit' Turner28a09b62013-12-15 00:16:00 +010051#include "migration/qemu-file.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070052#include "android/android.h"
David 'Digit' Turnere1e03df2013-12-15 00:42:21 +010053#include "android/charpipe.h"
David 'Digit' Turnerf9077a82014-02-10 23:10:47 +010054#include "android/log-rotate.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070055#include "modem_driver.h"
56#include "android/gps.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070057#include "android/hw-kmsg.h"
David 'Digit' Turnerc0ac7332011-05-02 15:05:35 +020058#include "android/hw-pipe-net.h"
59#include "android/hw-qemud.h"
Vladimir Chtchetkinec646f5e2011-09-03 15:17:13 -070060#include "android/camera/camera-service.h"
Vladimir Chtchetkine8dd31e82012-02-15 17:16:04 -080061#include "android/multitouch-port.h"
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -070062#include "android/charmap.h"
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -070063#include "android/globals.h"
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -070064#include "android/utils/bufprint.h"
David 'Digit' Turner5f824112011-03-01 14:00:26 +010065#include "android/utils/debug.h"
David 'Digit' Turner48a3c662011-03-01 14:03:20 +010066#include "android/utils/filelock.h"
67#include "android/utils/path.h"
David 'Digit' Turner5f824112011-03-01 14:00:26 +010068#include "android/utils/stralloc.h"
David 'Digit' Turner40841b22011-03-01 14:04:00 +010069#include "android/utils/tempfile.h"
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -080070#include "android/display-core.h"
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -080071#include "android/utils/timezone.h"
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +010072#include "android/snapshot.h"
David 'Digit' Turnercb88e792011-08-26 01:35:14 +020073#include "android/opengles.h"
Vladimir Chtchetkine1a820e92012-04-11 13:22:48 -070074#include "android/multitouch-screen.h"
David 'Digit' Turnerbcde1092014-01-09 23:19:19 +010075#include "exec/hwaddr.h"
David 'Digit' Turner6e2eb782013-12-15 00:54:21 +010076#include "android/tcpdump.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070077
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -070078#ifdef CONFIG_MEMCHECK
79#include "memcheck/memcheck.h"
80#endif // CONFIG_MEMCHECK
81
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070082#include <unistd.h>
83#include <fcntl.h>
84#include <signal.h>
85#include <time.h>
86#include <errno.h>
87#include <sys/time.h>
88#include <zlib.h>
89
David 'Digit' Turner2c538c82010-05-10 16:48:20 -070090/* Needed early for CONFIG_BSD etc. */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070091#include "config-host.h"
92
93#ifndef _WIN32
94#include <libgen.h>
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070095#include <sys/times.h>
96#include <sys/wait.h>
97#include <termios.h>
98#include <sys/mman.h>
99#include <sys/ioctl.h>
100#include <sys/resource.h>
101#include <sys/socket.h>
102#include <netinet/in.h>
103#include <net/if.h>
104#if defined(__NetBSD__)
105#include <net/if_tap.h>
106#endif
107#ifdef __linux__
108#include <linux/if_tun.h>
109#endif
110#include <arpa/inet.h>
111#include <dirent.h>
112#include <netdb.h>
113#include <sys/select.h>
David 'Digit' Turner2c538c82010-05-10 16:48:20 -0700114#ifdef CONFIG_BSD
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700115#include <sys/stat.h>
116#if defined(__FreeBSD__) || defined(__DragonFly__)
117#include <libutil.h>
118#else
119#include <util.h>
120#endif
121#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
122#include <freebsd/stdlib.h>
123#else
124#ifdef __linux__
125#include <pty.h>
126#include <malloc.h>
127#include <linux/rtc.h>
128
129/* For the benefit of older linux systems which don't supply it,
130 we use a local copy of hpet.h. */
131/* #include <linux/hpet.h> */
David 'Digit' Turner2dbdad52013-12-17 09:24:18 +0100132#include "hw/timer/hpet.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700133
134#include <linux/ppdev.h>
135#include <linux/parport.h>
136#endif
137#ifdef __sun__
138#include <sys/stat.h>
139#include <sys/ethernet.h>
140#include <sys/sockio.h>
141#include <netinet/arp.h>
142#include <netinet/in.h>
143#include <netinet/in_systm.h>
144#include <netinet/ip.h>
145#include <netinet/ip_icmp.h> // must come after ip.h
146#include <netinet/udp.h>
147#include <netinet/tcp.h>
148#include <net/if.h>
149#include <syslog.h>
150#include <stropts.h>
151#endif
152#endif
153#endif
154
155#if defined(__OpenBSD__)
156#include <util.h>
157#endif
158
159#if defined(CONFIG_VDE)
160#include <libvdeplug.h>
161#endif
162
163#ifdef _WIN32
164#include <windows.h>
165#include <malloc.h>
166#include <sys/timeb.h>
167#include <mmsystem.h>
168#define getopt_long_only getopt_long
169#define memalign(align, size) malloc(size)
170#endif
171
David 'Digit' Turner11822842013-12-17 09:16:47 +0100172#include "sysemu/cpus.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100173#include "sysemu/arch_init.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700174
175#ifdef CONFIG_COCOA
David 'Digit' Turnerae3098a2011-05-11 16:01:57 +0200176int qemu_main(int argc, char **argv, char **envp);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700177#undef main
178#define main qemu_main
179#endif /* CONFIG_COCOA */
180
181#include "hw/hw.h"
182#include "hw/boards.h"
183#include "hw/usb.h"
184#include "hw/pcmcia.h"
David 'Digit' Turner2ec695a2013-12-17 10:03:39 +0100185#include "hw/i386/pc.h"
186#include "hw/isa/isa.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700187#include "hw/baum.h"
188#include "hw/bt.h"
David 'Digit' Turner7821d632013-12-17 11:27:50 +0100189#include "sysemu/watchdog.h"
David 'Digit' Turner73a93a12013-12-17 11:26:01 +0100190#include "hw/i386/smbios.h"
David 'Digit' Turner50319182013-12-17 11:28:54 +0100191#include "hw/xen/xen.h"
David 'Digit' Turner6800f0a2013-12-17 09:12:54 +0100192#include "sysemu/bt.h"
David 'Digit' Turnercc330d42013-12-14 23:26:42 +0100193#include "net/net.h"
David 'Digit' Turner6af67652013-12-14 23:49:32 +0100194#include "monitor/monitor.h"
David 'Digit' Turner1c31e3e2013-12-14 20:07:17 +0100195#include "ui/console.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100196#include "sysemu/sysemu.h"
David 'Digit' Turner852088c2013-12-14 23:04:12 +0100197#include "exec/gdbstub.h"
David 'Digit' Turner7a78db72013-12-14 11:46:01 +0100198#include "qemu/timer.h"
David 'Digit' Turnere7216d82013-12-15 00:51:13 +0100199#include "sysemu/char.h"
David 'Digit' Turner37dc41a2013-12-14 14:45:51 +0100200#include "qemu/cache-utils.h"
David 'Digit' Turnere1e03df2013-12-15 00:42:21 +0100201#include "block/block.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100202#include "sysemu/dma.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700203#include "audio/audio.h"
David 'Digit' Turner28a09b62013-12-15 00:16:00 +0100204#include "migration/migration.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100205#include "sysemu/kvm.h"
David 'Digit' Turnere1e03df2013-12-15 00:42:21 +0100206#include "exec/hax.h"
David 'Digit' Turner36597752011-05-20 01:18:01 +0200207#ifdef CONFIG_KVM
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100208#include "android/kvm.h"
David 'Digit' Turner36597752011-05-20 01:18:01 +0200209#endif
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100210#include "sysemu/balloon.h"
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700211#include "android/hw-lcd.h"
212#include "android/boot-properties.h"
David 'Digit' Turnerca950592011-04-27 12:26:15 +0200213#include "android/hw-control.h"
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700214#include "android/core-init-utils.h"
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +0100215#include "android/audio-test.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700216
Vladimir Chtchetkinedb450d72012-01-12 13:37:40 -0800217#include "android/snaphost-android.h"
218
Vladimir Chtchetkine57584042011-01-20 16:15:30 -0800219#if !defined(CONFIG_STANDALONE_CORE)
220/* in android/qemulator.c */
221extern void android_emulator_set_base_port(int port);
222#endif
223
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700224#if defined(CONFIG_SKINS) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700225#undef main
226#define main qemu_main
227#endif
228
David 'Digit' Turnercc33b2d2013-12-15 00:09:42 +0100229#include "disas/disas.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700230
David 'Digit' Turnerd0edecb2013-12-17 09:32:26 +0100231#include "qemu/sockets.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700232
233#if defined(CONFIG_SLIRP)
234#include "libslirp.h"
235#endif
236
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700237#define DEFAULT_RAM_SIZE 128
238
239/* Max number of USB devices that can be specified on the commandline. */
240#define MAX_USB_CMDLINE 8
241
242/* Max number of bluetooth switches on the commandline. */
243#define MAX_BT_CMDLINE 10
244
245/* XXX: use a two level table to limit memory usage */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700246
247static const char *data_dir;
248const char *bios_name = NULL;
249static void *ioport_opaque[MAX_IOPORTS];
250static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
251static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100252#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700253/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
254 to store the VM snapshots */
255DriveInfo drives_table[MAX_DRIVES+1];
256int nb_drives;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100257#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700258enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700259DisplayType display_type = DT_DEFAULT;
260const char* keyboard_layout = NULL;
261int64_t ticks_per_sec;
262ram_addr_t ram_size;
David 'Digit' Turner280afa02011-05-11 17:37:44 +0200263const char *mem_path = NULL;
264#ifdef MAP_POPULATE
265int mem_prealloc = 0; /* force preallocation of physical target memory */
266#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700267int nb_nics;
268NICInfo nd_table[MAX_NICS];
269int vm_running;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100270int autostart;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700271static int rtc_utc = 1;
272static int rtc_date_offset = -1; /* -1 means no change */
273int cirrus_vga_enabled = 1;
274int std_vga_enabled = 0;
275int vmsvga_enabled = 0;
276int xenfb_enabled = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700277static int full_screen = 0;
278#ifdef CONFIG_SDL
279static int no_frame = 0;
280#endif
281int no_quit = 0;
282CharDriverState *serial_hds[MAX_SERIAL_PORTS];
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +0100283int serial_hds_count;
284
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700285CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
286CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
287#ifdef TARGET_I386
288int win2k_install_hack = 0;
289int rtc_td_hack = 0;
290#endif
291int usb_enabled = 0;
292int singlestep = 0;
293int smp_cpus = 1;
294const char *vnc_display;
295int acpi_enabled = 1;
296int no_hpet = 0;
Jun Nakajimaa381ef02011-12-17 19:13:25 -0800297int hax_disabled = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700298int no_virtio_balloon = 0;
299int fd_bootchk = 1;
300int no_reboot = 0;
301int no_shutdown = 0;
302int cursor_hide = 1;
303int graphic_rotate = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700304WatchdogTimerModel *watchdog = NULL;
305int watchdog_action = WDT_RESET;
306const char *option_rom[MAX_OPTION_ROMS];
307int nb_option_roms;
308int semihosting_enabled = 0;
309#ifdef TARGET_ARM
310int old_param = 0;
311#endif
312const char *qemu_name;
313int alt_grab = 0;
314#if defined(TARGET_SPARC) || defined(TARGET_PPC)
315unsigned int nb_prom_envs = 0;
316const char *prom_envs[MAX_PROM_ENVS];
317#endif
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100318#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700319int nb_drives_opt;
320struct drive_opt drives_opt[MAX_DRIVES];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100321#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700322int nb_numa_nodes;
323uint64_t node_mem[MAX_NODES];
324uint64_t node_cpumask[MAX_NODES];
325
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700326static QEMUTimer *nographic_timer;
327
328uint8_t qemu_uuid[16];
329
330
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700331int qemu_cpu_delay;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700332extern char* audio_input_source;
333
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700334extern char* android_op_ports;
335extern char* android_op_port;
336extern char* android_op_report_console;
337extern char* op_http_proxy;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700338// Path to the file containing specific key character map.
339char* op_charmap_file = NULL;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700340
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -0700341/* Path to hardware initialization file passed with -android-hw option. */
342char* android_op_hwini = NULL;
343
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -0700344/* Memory checker options. */
345char* android_op_memcheck = NULL;
346
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -0700347/* -dns-server option value. */
348char* android_op_dns_server = NULL;
349
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -0700350/* -radio option value. */
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700351char* android_op_radio = NULL;
352
353/* -gps option value. */
354char* android_op_gps = NULL;
355
356/* -audio option value. */
357char* android_op_audio = NULL;
358
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700359/* -cpu-delay option value. */
360char* android_op_cpu_delay = NULL;
361
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -0700362#ifdef CONFIG_NAND_LIMITS
363/* -nand-limits option value. */
364char* android_op_nand_limits = NULL;
365#endif // CONFIG_NAND_LIMITS
366
367/* -netspeed option value. */
368char* android_op_netspeed = NULL;
369
370/* -netdelay option value. */
371char* android_op_netdelay = NULL;
372
373/* -netfast option value. */
374int android_op_netfast = 0;
375
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -0700376/* -tcpdump option value. */
377char* android_op_tcpdump = NULL;
378
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700379/* -lcd-density option value. */
380char* android_op_lcd_density = NULL;
381
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700382/* -ui-port option value. This port will be used to report the core
383 * initialization completion.
384 */
385char* android_op_ui_port = NULL;
386
387/* -ui-settings option value. This value will be passed to the UI when new UI
388 * process is attaching to the core.
389 */
390char* android_op_ui_settings = NULL;
391
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -0800392/* -android-avdname option value. */
393char* android_op_avd_name = "unknown";
394
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -0700395extern int android_display_width;
396extern int android_display_height;
397extern int android_display_bpp;
398
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700399extern void dprint( const char* format, ... );
400
rich canningsd952f282011-03-01 15:40:09 -0800401const char* dns_log_filename = NULL;
402const char* drop_log_filename = NULL;
rich canningsd952f282011-03-01 15:40:09 -0800403
Tim Baverstock24204cc2010-11-25 11:37:43 +0000404const char* savevm_on_exit = NULL;
Tim Baverstock24204cc2010-11-25 11:37:43 +0000405
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700406#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
407
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700408/* Reports the core initialization failure to the error stdout and to the UI
409 * socket before exiting the application.
410 * Parameters that are passed to this macro are used to format the error
411 * mesage using sprintf routine.
412 */
413#ifdef CONFIG_ANDROID
414#define PANIC(...) android_core_init_failure(__VA_ARGS__)
415#else
416#define PANIC(...) do { fprintf(stderr, __VA_ARGS__); \
417 exit(1); \
418 } while (0)
419#endif // CONFIG_ANDROID
420
421/* Exits the core during initialization. */
422#ifdef CONFIG_ANDROID
423#define QEMU_EXIT(exit_code) android_core_init_exit(exit_code)
424#else
425#define QEMU_EXIT(exit_code) exit(exit_code)
426#endif // CONFIG_ANDROID
427
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700428/***********************************************************/
429/* x86 ISA bus support */
430
David 'Digit' Turnerbcde1092014-01-09 23:19:19 +0100431hwaddr isa_mem_base = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700432PicState2 *isa_pic;
433
434static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
435static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
436
437static uint32_t ioport_read(int index, uint32_t address)
438{
439 static IOPortReadFunc *default_func[3] = {
440 default_ioport_readb,
441 default_ioport_readw,
442 default_ioport_readl
443 };
444 IOPortReadFunc *func = ioport_read_table[index][address];
445 if (!func)
446 func = default_func[index];
447 return func(ioport_opaque[address], address);
448}
449
450static void ioport_write(int index, uint32_t address, uint32_t data)
451{
452 static IOPortWriteFunc *default_func[3] = {
453 default_ioport_writeb,
454 default_ioport_writew,
455 default_ioport_writel
456 };
457 IOPortWriteFunc *func = ioport_write_table[index][address];
458 if (!func)
459 func = default_func[index];
460 func(ioport_opaque[address], address, data);
461}
462
463static uint32_t default_ioport_readb(void *opaque, uint32_t address)
464{
465#ifdef DEBUG_UNUSED_IOPORT
466 fprintf(stderr, "unused inb: port=0x%04x\n", address);
467#endif
468 return 0xff;
469}
470
471static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
472{
473#ifdef DEBUG_UNUSED_IOPORT
474 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
475#endif
476}
477
478/* default is to make two byte accesses */
479static uint32_t default_ioport_readw(void *opaque, uint32_t address)
480{
481 uint32_t data;
482 data = ioport_read(0, address);
483 address = (address + 1) & (MAX_IOPORTS - 1);
484 data |= ioport_read(0, address) << 8;
485 return data;
486}
487
488static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
489{
490 ioport_write(0, address, data & 0xff);
491 address = (address + 1) & (MAX_IOPORTS - 1);
492 ioport_write(0, address, (data >> 8) & 0xff);
493}
494
495static uint32_t default_ioport_readl(void *opaque, uint32_t address)
496{
497#ifdef DEBUG_UNUSED_IOPORT
498 fprintf(stderr, "unused inl: port=0x%04x\n", address);
499#endif
500 return 0xffffffff;
501}
502
503static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
504{
505#ifdef DEBUG_UNUSED_IOPORT
506 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
507#endif
508}
509
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700510/***************/
511/* ballooning */
512
513static QEMUBalloonEvent *qemu_balloon_event;
514void *qemu_balloon_event_opaque;
515
516void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
517{
518 qemu_balloon_event = func;
519 qemu_balloon_event_opaque = opaque;
520}
521
522void qemu_balloon(ram_addr_t target)
523{
524 if (qemu_balloon_event)
525 qemu_balloon_event(qemu_balloon_event_opaque, target);
526}
527
528ram_addr_t qemu_balloon_status(void)
529{
530 if (qemu_balloon_event)
531 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
532 return 0;
533}
534
535/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700536/* host time/date access */
537void qemu_get_timedate(struct tm *tm, int offset)
538{
539 time_t ti;
540 struct tm *ret;
541
542 time(&ti);
543 ti += offset;
544 if (rtc_date_offset == -1) {
545 if (rtc_utc)
546 ret = gmtime(&ti);
547 else
548 ret = localtime(&ti);
549 } else {
550 ti -= rtc_date_offset;
551 ret = gmtime(&ti);
552 }
553
554 memcpy(tm, ret, sizeof(struct tm));
555}
556
557int qemu_timedate_diff(struct tm *tm)
558{
559 time_t seconds;
560
561 if (rtc_date_offset == -1)
562 if (rtc_utc)
563 seconds = mktimegm(tm);
564 else
565 seconds = mktime(tm);
566 else
567 seconds = mktimegm(tm) + rtc_date_offset;
568
569 return seconds - time(NULL);
570}
571
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700572/***********************************************************/
573/* Bluetooth support */
574static int nb_hcis;
575static int cur_hci;
576static struct HCIInfo *hci_table[MAX_NICS];
577
578static struct bt_vlan_s {
579 struct bt_scatternet_s net;
580 int id;
581 struct bt_vlan_s *next;
582} *first_bt_vlan;
583
584/* find or alloc a new bluetooth "VLAN" */
585static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
586{
587 struct bt_vlan_s **pvlan, *vlan;
588 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
589 if (vlan->id == id)
590 return &vlan->net;
591 }
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +0100592 vlan = g_malloc0(sizeof(struct bt_vlan_s));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700593 vlan->id = id;
594 pvlan = &first_bt_vlan;
595 while (*pvlan != NULL)
596 pvlan = &(*pvlan)->next;
597 *pvlan = vlan;
598 return &vlan->net;
599}
600
601static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
602{
603}
604
605static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
606{
607 return -ENOTSUP;
608}
609
610static struct HCIInfo null_hci = {
611 .cmd_send = null_hci_send,
612 .sco_send = null_hci_send,
613 .acl_send = null_hci_send,
614 .bdaddr_set = null_hci_addr_set,
615};
616
617struct HCIInfo *qemu_next_hci(void)
618{
619 if (cur_hci == nb_hcis)
620 return &null_hci;
621
622 return hci_table[cur_hci++];
623}
624
625static struct HCIInfo *hci_init(const char *str)
626{
627 char *endp;
628 struct bt_scatternet_s *vlan = 0;
629
630 if (!strcmp(str, "null"))
631 /* null */
632 return &null_hci;
633 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
634 /* host[:hciN] */
635 return bt_host_hci(str[4] ? str + 5 : "hci0");
636 else if (!strncmp(str, "hci", 3)) {
637 /* hci[,vlan=n] */
638 if (str[3]) {
639 if (!strncmp(str + 3, ",vlan=", 6)) {
640 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
641 if (*endp)
642 vlan = 0;
643 }
644 } else
645 vlan = qemu_find_bt_vlan(0);
646 if (vlan)
647 return bt_new_hci(vlan);
648 }
649
650 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
651
652 return 0;
653}
654
655static int bt_hci_parse(const char *str)
656{
657 struct HCIInfo *hci;
658 bdaddr_t bdaddr;
659
660 if (nb_hcis >= MAX_NICS) {
661 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
662 return -1;
663 }
664
665 hci = hci_init(str);
666 if (!hci)
667 return -1;
668
669 bdaddr.b[0] = 0x52;
670 bdaddr.b[1] = 0x54;
671 bdaddr.b[2] = 0x00;
672 bdaddr.b[3] = 0x12;
673 bdaddr.b[4] = 0x34;
674 bdaddr.b[5] = 0x56 + nb_hcis;
675 hci->bdaddr_set(hci, bdaddr.b);
676
677 hci_table[nb_hcis++] = hci;
678
679 return 0;
680}
681
682static void bt_vhci_add(int vlan_id)
683{
684 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
685
686 if (!vlan->slave)
687 fprintf(stderr, "qemu: warning: adding a VHCI to "
688 "an empty scatternet %i\n", vlan_id);
689
690 bt_vhci_init(bt_new_hci(vlan));
691}
692
693static struct bt_device_s *bt_device_add(const char *opt)
694{
695 struct bt_scatternet_s *vlan;
696 int vlan_id = 0;
697 char *endp = strstr(opt, ",vlan=");
698 int len = (endp ? endp - opt : strlen(opt)) + 1;
699 char devname[10];
700
701 pstrcpy(devname, MIN(sizeof(devname), len), opt);
702
703 if (endp) {
704 vlan_id = strtol(endp + 6, &endp, 0);
705 if (*endp) {
706 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
707 return 0;
708 }
709 }
710
711 vlan = qemu_find_bt_vlan(vlan_id);
712
713 if (!vlan->slave)
714 fprintf(stderr, "qemu: warning: adding a slave device to "
715 "an empty scatternet %i\n", vlan_id);
716
717 if (!strcmp(devname, "keyboard"))
718 return bt_keyboard_init(vlan);
719
720 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
721 return 0;
722}
723
724static int bt_parse(const char *opt)
725{
726 const char *endp, *p;
727 int vlan;
728
729 if (strstart(opt, "hci", &endp)) {
730 if (!*endp || *endp == ',') {
731 if (*endp)
732 if (!strstart(endp, ",vlan=", 0))
733 opt = endp + 1;
734
735 return bt_hci_parse(opt);
736 }
737 } else if (strstart(opt, "vhci", &endp)) {
738 if (!*endp || *endp == ',') {
739 if (*endp) {
740 if (strstart(endp, ",vlan=", &p)) {
741 vlan = strtol(p, (char **) &endp, 0);
742 if (*endp) {
743 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
744 return 1;
745 }
746 } else {
747 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
748 return 1;
749 }
750 } else
751 vlan = 0;
752
753 bt_vhci_add(vlan);
754 return 0;
755 }
756 } else if (strstart(opt, "device:", &endp))
757 return !bt_device_add(endp);
758
759 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
760 return 1;
761}
762
763/***********************************************************/
764/* QEMU Block devices */
765
766#define HD_ALIAS "index=%d,media=disk"
767#define CDROM_ALIAS "index=2,media=cdrom"
768#define FD_ALIAS "index=%d,if=floppy"
769#define PFLASH_ALIAS "if=pflash"
770#define MTD_ALIAS "if=mtd"
771#define SD_ALIAS "index=0,if=sd"
772
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100773static int drive_init_func(QemuOpts *opts, void *opaque)
774{
775 int *use_scsi = opaque;
776 int fatal_error = 0;
777
778 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
779 if (fatal_error)
780 return 1;
781 }
782 return 0;
783}
784
785static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
786{
787 if (NULL == qemu_opt_get(opts, "snapshot")) {
788 qemu_opt_set(opts, "snapshot", "on");
789 }
790 return 0;
791}
792
793#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700794static int drive_opt_get_free_idx(void)
795{
796 int index;
797
798 for (index = 0; index < MAX_DRIVES; index++)
799 if (!drives_opt[index].used) {
800 drives_opt[index].used = 1;
801 return index;
802 }
803
804 return -1;
805}
806
807static int drive_get_free_idx(void)
808{
809 int index;
810
811 for (index = 0; index < MAX_DRIVES; index++)
812 if (!drives_table[index].used) {
813 drives_table[index].used = 1;
814 return index;
815 }
816
817 return -1;
818}
819
820int drive_add(const char *file, const char *fmt, ...)
821{
822 va_list ap;
823 int index = drive_opt_get_free_idx();
824
825 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
826 fprintf(stderr, "qemu: too many drives\n");
827 return -1;
828 }
829
830 drives_opt[index].file = file;
831 va_start(ap, fmt);
832 vsnprintf(drives_opt[index].opt,
833 sizeof(drives_opt[0].opt), fmt, ap);
834 va_end(ap);
David 'Digit' Turner92568952010-04-15 15:04:16 -0700835
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700836 nb_drives_opt++;
837 return index;
838}
839
840void drive_remove(int index)
841{
842 drives_opt[index].used = 0;
843 nb_drives_opt--;
844}
845
846int drive_get_index(BlockInterfaceType type, int bus, int unit)
847{
848 int index;
849
850 /* seek interface, bus and unit */
851
852 for (index = 0; index < MAX_DRIVES; index++)
853 if (drives_table[index].type == type &&
854 drives_table[index].bus == bus &&
855 drives_table[index].unit == unit &&
856 drives_table[index].used)
857 return index;
858
859 return -1;
860}
861
862int drive_get_max_bus(BlockInterfaceType type)
863{
864 int max_bus;
865 int index;
866
867 max_bus = -1;
868 for (index = 0; index < nb_drives; index++) {
869 if(drives_table[index].type == type &&
870 drives_table[index].bus > max_bus)
871 max_bus = drives_table[index].bus;
872 }
873 return max_bus;
874}
875
876const char *drive_get_serial(BlockDriverState *bdrv)
877{
878 int index;
879
880 for (index = 0; index < nb_drives; index++)
881 if (drives_table[index].bdrv == bdrv)
882 return drives_table[index].serial;
883
884 return "\0";
885}
886
887BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
888{
889 int index;
890
891 for (index = 0; index < nb_drives; index++)
892 if (drives_table[index].bdrv == bdrv)
893 return drives_table[index].onerror;
894
895 return BLOCK_ERR_STOP_ENOSPC;
896}
897
898static void bdrv_format_print(void *opaque, const char *name)
899{
900 fprintf(stderr, " %s", name);
901}
902
903void drive_uninit(BlockDriverState *bdrv)
904{
905 int i;
906
907 for (i = 0; i < MAX_DRIVES; i++)
908 if (drives_table[i].bdrv == bdrv) {
909 drives_table[i].bdrv = NULL;
910 drives_table[i].used = 0;
911 drive_remove(drives_table[i].drive_opt_idx);
912 nb_drives--;
913 break;
914 }
915}
916
917int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
918{
919 char buf[128];
920 char file[1024];
921 char devname[128];
922 char serial[21];
923 const char *mediastr = "";
924 BlockInterfaceType type;
925 enum { MEDIA_DISK, MEDIA_CDROM } media;
926 int bus_id, unit_id;
927 int cyls, heads, secs, translation;
928 BlockDriverState *bdrv;
929 BlockDriver *drv = NULL;
930 QEMUMachine *machine = opaque;
931 int max_devs;
932 int index;
933 int cache;
934 int bdrv_flags, onerror;
935 int drives_table_idx;
936 char *str = arg->opt;
937 static const char * const params[] = { "bus", "unit", "if", "index",
938 "cyls", "heads", "secs", "trans",
939 "media", "snapshot", "file",
940 "cache", "format", "serial", "werror",
941 NULL };
942
943 if (check_params(buf, sizeof(buf), params, str) < 0) {
944 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
945 buf, str);
946 return -1;
947 }
948
949 file[0] = 0;
950 cyls = heads = secs = 0;
951 bus_id = 0;
952 unit_id = -1;
953 translation = BIOS_ATA_TRANSLATION_AUTO;
954 index = -1;
955 cache = 3;
956
957 if (machine->use_scsi) {
958 type = IF_SCSI;
959 max_devs = MAX_SCSI_DEVS;
960 pstrcpy(devname, sizeof(devname), "scsi");
961 } else {
962 type = IF_IDE;
963 max_devs = MAX_IDE_DEVS;
964 pstrcpy(devname, sizeof(devname), "ide");
965 }
966 media = MEDIA_DISK;
967
968 /* extract parameters */
969
970 if (get_param_value(buf, sizeof(buf), "bus", str)) {
971 bus_id = strtol(buf, NULL, 0);
972 if (bus_id < 0) {
973 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
974 return -1;
975 }
976 }
977
978 if (get_param_value(buf, sizeof(buf), "unit", str)) {
979 unit_id = strtol(buf, NULL, 0);
980 if (unit_id < 0) {
981 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
982 return -1;
983 }
984 }
985
986 if (get_param_value(buf, sizeof(buf), "if", str)) {
987 pstrcpy(devname, sizeof(devname), buf);
988 if (!strcmp(buf, "ide")) {
989 type = IF_IDE;
990 max_devs = MAX_IDE_DEVS;
991 } else if (!strcmp(buf, "scsi")) {
992 type = IF_SCSI;
993 max_devs = MAX_SCSI_DEVS;
994 } else if (!strcmp(buf, "floppy")) {
995 type = IF_FLOPPY;
996 max_devs = 0;
997 } else if (!strcmp(buf, "pflash")) {
998 type = IF_PFLASH;
999 max_devs = 0;
1000 } else if (!strcmp(buf, "mtd")) {
1001 type = IF_MTD;
1002 max_devs = 0;
1003 } else if (!strcmp(buf, "sd")) {
1004 type = IF_SD;
1005 max_devs = 0;
1006 } else if (!strcmp(buf, "virtio")) {
1007 type = IF_VIRTIO;
1008 max_devs = 0;
1009 } else if (!strcmp(buf, "xen")) {
1010 type = IF_XEN;
1011 max_devs = 0;
1012 } else {
1013 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
1014 return -1;
1015 }
1016 }
1017
1018 if (get_param_value(buf, sizeof(buf), "index", str)) {
1019 index = strtol(buf, NULL, 0);
1020 if (index < 0) {
1021 fprintf(stderr, "qemu: '%s' invalid index\n", str);
1022 return -1;
1023 }
1024 }
1025
1026 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
1027 cyls = strtol(buf, NULL, 0);
1028 }
1029
1030 if (get_param_value(buf, sizeof(buf), "heads", str)) {
1031 heads = strtol(buf, NULL, 0);
1032 }
1033
1034 if (get_param_value(buf, sizeof(buf), "secs", str)) {
1035 secs = strtol(buf, NULL, 0);
1036 }
1037
1038 if (cyls || heads || secs) {
1039 if (cyls < 1 || cyls > 16383) {
1040 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
1041 return -1;
1042 }
1043 if (heads < 1 || heads > 16) {
1044 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
1045 return -1;
1046 }
1047 if (secs < 1 || secs > 63) {
1048 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
1049 return -1;
1050 }
1051 }
1052
1053 if (get_param_value(buf, sizeof(buf), "trans", str)) {
1054 if (!cyls) {
1055 fprintf(stderr,
1056 "qemu: '%s' trans must be used with cyls,heads and secs\n",
1057 str);
1058 return -1;
1059 }
1060 if (!strcmp(buf, "none"))
1061 translation = BIOS_ATA_TRANSLATION_NONE;
1062 else if (!strcmp(buf, "lba"))
1063 translation = BIOS_ATA_TRANSLATION_LBA;
1064 else if (!strcmp(buf, "auto"))
1065 translation = BIOS_ATA_TRANSLATION_AUTO;
1066 else {
1067 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
1068 return -1;
1069 }
1070 }
1071
1072 if (get_param_value(buf, sizeof(buf), "media", str)) {
1073 if (!strcmp(buf, "disk")) {
1074 media = MEDIA_DISK;
1075 } else if (!strcmp(buf, "cdrom")) {
1076 if (cyls || secs || heads) {
1077 fprintf(stderr,
1078 "qemu: '%s' invalid physical CHS format\n", str);
1079 return -1;
1080 }
1081 media = MEDIA_CDROM;
1082 } else {
1083 fprintf(stderr, "qemu: '%s' invalid media\n", str);
1084 return -1;
1085 }
1086 }
1087
1088 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
1089 if (!strcmp(buf, "on"))
1090 snapshot = 1;
1091 else if (!strcmp(buf, "off"))
1092 snapshot = 0;
1093 else {
1094 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
1095 return -1;
1096 }
1097 }
1098
1099 if (get_param_value(buf, sizeof(buf), "cache", str)) {
1100 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
1101 cache = 0;
1102 else if (!strcmp(buf, "writethrough"))
1103 cache = 1;
1104 else if (!strcmp(buf, "writeback"))
1105 cache = 2;
1106 else {
1107 fprintf(stderr, "qemu: invalid cache option\n");
1108 return -1;
1109 }
1110 }
1111
1112 if (get_param_value(buf, sizeof(buf), "format", str)) {
1113 if (strcmp(buf, "?") == 0) {
1114 fprintf(stderr, "qemu: Supported formats:");
1115 bdrv_iterate_format(bdrv_format_print, NULL);
1116 fprintf(stderr, "\n");
1117 return -1;
1118 }
1119 drv = bdrv_find_format(buf);
1120 if (!drv) {
1121 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
1122 return -1;
1123 }
1124 }
1125
1126 if (arg->file == NULL)
1127 get_param_value(file, sizeof(file), "file", str);
1128 else
1129 pstrcpy(file, sizeof(file), arg->file);
1130
1131 if (!get_param_value(serial, sizeof(serial), "serial", str))
1132 memset(serial, 0, sizeof(serial));
1133
1134 onerror = BLOCK_ERR_STOP_ENOSPC;
1135 if (get_param_value(buf, sizeof(serial), "werror", str)) {
1136 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
1137 fprintf(stderr, "werror is no supported by this format\n");
1138 return -1;
1139 }
1140 if (!strcmp(buf, "ignore"))
1141 onerror = BLOCK_ERR_IGNORE;
1142 else if (!strcmp(buf, "enospc"))
1143 onerror = BLOCK_ERR_STOP_ENOSPC;
1144 else if (!strcmp(buf, "stop"))
1145 onerror = BLOCK_ERR_STOP_ANY;
1146 else if (!strcmp(buf, "report"))
1147 onerror = BLOCK_ERR_REPORT;
1148 else {
1149 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
1150 return -1;
1151 }
1152 }
1153
1154 /* compute bus and unit according index */
1155
1156 if (index != -1) {
1157 if (bus_id != 0 || unit_id != -1) {
1158 fprintf(stderr,
1159 "qemu: '%s' index cannot be used with bus and unit\n", str);
1160 return -1;
1161 }
1162 if (max_devs == 0)
1163 {
1164 unit_id = index;
1165 bus_id = 0;
1166 } else {
1167 unit_id = index % max_devs;
1168 bus_id = index / max_devs;
1169 }
1170 }
1171
1172 /* if user doesn't specify a unit_id,
1173 * try to find the first free
1174 */
1175
1176 if (unit_id == -1) {
1177 unit_id = 0;
1178 while (drive_get_index(type, bus_id, unit_id) != -1) {
1179 unit_id++;
1180 if (max_devs && unit_id >= max_devs) {
1181 unit_id -= max_devs;
1182 bus_id++;
1183 }
1184 }
1185 }
1186
1187 /* check unit id */
1188
1189 if (max_devs && unit_id >= max_devs) {
1190 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
1191 str, unit_id, max_devs - 1);
1192 return -1;
1193 }
1194
1195 /*
1196 * ignore multiple definitions
1197 */
1198
1199 if (drive_get_index(type, bus_id, unit_id) != -1)
1200 return -2;
1201
1202 /* init */
1203
1204 if (type == IF_IDE || type == IF_SCSI)
1205 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1206 if (max_devs)
1207 snprintf(buf, sizeof(buf), "%s%i%s%i",
1208 devname, bus_id, mediastr, unit_id);
1209 else
1210 snprintf(buf, sizeof(buf), "%s%s%i",
1211 devname, mediastr, unit_id);
1212 bdrv = bdrv_new(buf);
1213 drives_table_idx = drive_get_free_idx();
1214 drives_table[drives_table_idx].bdrv = bdrv;
1215 drives_table[drives_table_idx].type = type;
1216 drives_table[drives_table_idx].bus = bus_id;
1217 drives_table[drives_table_idx].unit = unit_id;
1218 drives_table[drives_table_idx].onerror = onerror;
1219 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
1220 strncpy(drives_table[drives_table_idx].serial, serial, sizeof(serial));
1221 nb_drives++;
1222
1223 switch(type) {
1224 case IF_IDE:
1225 case IF_SCSI:
1226 case IF_XEN:
1227 switch(media) {
1228 case MEDIA_DISK:
1229 if (cyls != 0) {
1230 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
1231 bdrv_set_translation_hint(bdrv, translation);
1232 }
1233 break;
1234 case MEDIA_CDROM:
1235 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
1236 break;
1237 }
1238 break;
1239 case IF_SD:
1240 /* FIXME: This isn't really a floppy, but it's a reasonable
1241 approximation. */
1242 case IF_FLOPPY:
1243 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
1244 break;
1245 case IF_PFLASH:
1246 case IF_MTD:
1247 case IF_VIRTIO:
1248 break;
1249 case IF_COUNT:
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07001250 case IF_NONE:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001251 abort();
1252 }
1253 if (!file[0])
1254 return -2;
1255 bdrv_flags = 0;
1256 if (snapshot) {
1257 bdrv_flags |= BDRV_O_SNAPSHOT;
1258 cache = 2; /* always use write-back with snapshot */
1259 }
1260 if (cache == 0) /* no caching */
1261 bdrv_flags |= BDRV_O_NOCACHE;
1262 else if (cache == 2) /* write-back */
1263 bdrv_flags |= BDRV_O_CACHE_WB;
1264 else if (cache == 3) /* not specified */
1265 bdrv_flags |= BDRV_O_CACHE_DEF;
1266 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
1267 fprintf(stderr, "qemu: could not open disk image %s\n",
1268 file);
1269 return -1;
1270 }
1271 if (bdrv_key_required(bdrv))
1272 autostart = 0;
1273 return drives_table_idx;
1274}
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01001275#endif /* MAX_DRIVES */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001276
1277static void numa_add(const char *optarg)
1278{
1279 char option[128];
1280 char *endptr;
1281 unsigned long long value, endvalue;
1282 int nodenr;
1283
1284 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1285 if (!strcmp(option, "node")) {
1286 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1287 nodenr = nb_numa_nodes;
1288 } else {
1289 nodenr = strtoull(option, NULL, 10);
1290 }
1291
1292 if (get_param_value(option, 128, "mem", optarg) == 0) {
1293 node_mem[nodenr] = 0;
1294 } else {
1295 value = strtoull(option, &endptr, 0);
1296 switch (*endptr) {
1297 case 0: case 'M': case 'm':
1298 value <<= 20;
1299 break;
1300 case 'G': case 'g':
1301 value <<= 30;
1302 break;
1303 }
1304 node_mem[nodenr] = value;
1305 }
1306 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1307 node_cpumask[nodenr] = 0;
1308 } else {
1309 value = strtoull(option, &endptr, 10);
1310 if (value >= 64) {
1311 value = 63;
1312 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1313 } else {
1314 if (*endptr == '-') {
1315 endvalue = strtoull(endptr+1, &endptr, 10);
1316 if (endvalue >= 63) {
1317 endvalue = 62;
1318 fprintf(stderr,
1319 "only 63 CPUs in NUMA mode supported.\n");
1320 }
1321 value = (1 << (endvalue + 1)) - (1 << value);
1322 } else {
1323 value = 1 << value;
1324 }
1325 }
1326 node_cpumask[nodenr] = value;
1327 }
1328 nb_numa_nodes++;
1329 }
1330 return;
1331}
1332
1333/***********************************************************/
1334/* USB devices */
1335
1336static USBPort *used_usb_ports;
1337static USBPort *free_usb_ports;
1338
1339/* ??? Maybe change this to register a hub to keep track of the topology. */
1340void qemu_register_usb_port(USBPort *port, void *opaque, int index,
1341 usb_attachfn attach)
1342{
1343 port->opaque = opaque;
1344 port->index = index;
1345 port->attach = attach;
1346 port->next = free_usb_ports;
1347 free_usb_ports = port;
1348}
1349
1350int usb_device_add_dev(USBDevice *dev)
1351{
1352 USBPort *port;
1353
1354 /* Find a USB port to add the device to. */
1355 port = free_usb_ports;
Manuel Francisco Naranjo58ecd742012-07-18 11:22:09 -03001356 if (!port){
1357 USBDevice *hub = usb_hub_init(VM_USB_HUB_SIZE);
1358 port = free_usb_ports;
1359 usb_attach(port, hub);
1360 }
1361 else if (!port->next){
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001362 USBDevice *hub;
1363
1364 /* Create a new hub and chain it on. */
1365 free_usb_ports = NULL;
1366 port->next = used_usb_ports;
1367 used_usb_ports = port;
1368
1369 hub = usb_hub_init(VM_USB_HUB_SIZE);
1370 usb_attach(port, hub);
1371 port = free_usb_ports;
1372 }
1373
1374 free_usb_ports = port->next;
1375 port->next = used_usb_ports;
1376 used_usb_ports = port;
1377 usb_attach(port, dev);
1378 return 0;
1379}
1380
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001381#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001382static void usb_msd_password_cb(void *opaque, int err)
1383{
1384 USBDevice *dev = opaque;
1385
1386 if (!err)
1387 usb_device_add_dev(dev);
1388 else
1389 dev->handle_destroy(dev);
1390}
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001391#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001392
1393static int usb_device_add(const char *devname, int is_hotplug)
1394{
1395 const char *p;
1396 USBDevice *dev;
1397
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001398 if (strstart(devname, "host:", &p)) {
1399 dev = usb_host_device_open(p);
1400 } else if (!strcmp(devname, "mouse")) {
1401 dev = usb_mouse_init();
1402 } else if (!strcmp(devname, "tablet")) {
1403 dev = usb_tablet_init();
1404 } else if (!strcmp(devname, "keyboard")) {
1405 dev = usb_keyboard_init();
1406 } else if (strstart(devname, "disk:", &p)) {
1407#if 0
1408 BlockDriverState *bs;
1409#endif
1410 dev = usb_msd_init(p);
1411 if (!dev)
1412 return -1;
1413#if 0
1414 bs = usb_msd_get_bdrv(dev);
1415 if (bdrv_key_required(bs)) {
1416 autostart = 0;
1417 if (is_hotplug) {
1418 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
1419 dev);
1420 return 0;
1421 }
1422 }
1423 } else if (!strcmp(devname, "wacom-tablet")) {
1424 dev = usb_wacom_init();
1425 } else if (strstart(devname, "serial:", &p)) {
1426 dev = usb_serial_init(p);
1427#ifdef CONFIG_BRLAPI
1428 } else if (!strcmp(devname, "braille")) {
1429 dev = usb_baum_init();
1430#endif
1431 } else if (strstart(devname, "net:", &p)) {
1432 int nic = nb_nics;
1433
1434 if (net_client_init("nic", p) < 0)
1435 return -1;
1436 nd_table[nic].model = "usb";
1437 dev = usb_net_init(&nd_table[nic]);
1438 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1439 dev = usb_bt_init(devname[2] ? hci_init(p) :
1440 bt_new_hci(qemu_find_bt_vlan(0)));
1441#endif
1442 } else {
1443 return -1;
1444 }
1445 if (!dev)
1446 return -1;
1447
1448 return usb_device_add_dev(dev);
1449}
1450
1451int usb_device_del_addr(int bus_num, int addr)
1452{
1453 USBPort *port;
1454 USBPort **lastp;
1455 USBDevice *dev;
1456
1457 if (!used_usb_ports)
1458 return -1;
1459
1460 if (bus_num != 0)
1461 return -1;
1462
1463 lastp = &used_usb_ports;
1464 port = used_usb_ports;
1465 while (port && port->dev->addr != addr) {
1466 lastp = &port->next;
1467 port = port->next;
1468 }
1469
1470 if (!port)
1471 return -1;
1472
1473 dev = port->dev;
1474 *lastp = port->next;
1475 usb_attach(port, NULL);
1476 dev->handle_destroy(dev);
1477 port->next = free_usb_ports;
1478 free_usb_ports = port;
1479 return 0;
1480}
1481
1482static int usb_device_del(const char *devname)
1483{
1484 int bus_num, addr;
1485 const char *p;
1486
1487 if (strstart(devname, "host:", &p))
1488 return usb_host_device_close(p);
1489
1490 if (!used_usb_ports)
1491 return -1;
1492
1493 p = strchr(devname, '.');
1494 if (!p)
1495 return -1;
1496 bus_num = strtoul(devname, NULL, 0);
1497 addr = strtoul(p + 1, NULL, 0);
1498
1499 return usb_device_del_addr(bus_num, addr);
1500}
1501
1502void do_usb_add(Monitor *mon, const char *devname)
1503{
1504 usb_device_add(devname, 1);
1505}
1506
1507void do_usb_del(Monitor *mon, const char *devname)
1508{
1509 usb_device_del(devname);
1510}
1511
1512void usb_info(Monitor *mon)
1513{
1514 USBDevice *dev;
1515 USBPort *port;
1516 const char *speed_str;
1517
1518 if (!usb_enabled) {
1519 monitor_printf(mon, "USB support not enabled\n");
1520 return;
1521 }
1522
1523 for (port = used_usb_ports; port; port = port->next) {
1524 dev = port->dev;
1525 if (!dev)
1526 continue;
1527 switch(dev->speed) {
1528 case USB_SPEED_LOW:
1529 speed_str = "1.5";
1530 break;
1531 case USB_SPEED_FULL:
1532 speed_str = "12";
1533 break;
1534 case USB_SPEED_HIGH:
1535 speed_str = "480";
1536 break;
1537 default:
1538 speed_str = "?";
1539 break;
1540 }
1541 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
1542 0, dev->addr, speed_str, dev->devname);
1543 }
1544}
1545
1546/***********************************************************/
1547/* PCMCIA/Cardbus */
1548
1549static struct pcmcia_socket_entry_s {
1550 PCMCIASocket *socket;
1551 struct pcmcia_socket_entry_s *next;
1552} *pcmcia_sockets = 0;
1553
1554void pcmcia_socket_register(PCMCIASocket *socket)
1555{
1556 struct pcmcia_socket_entry_s *entry;
1557
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001558 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001559 entry->socket = socket;
1560 entry->next = pcmcia_sockets;
1561 pcmcia_sockets = entry;
1562}
1563
1564void pcmcia_socket_unregister(PCMCIASocket *socket)
1565{
1566 struct pcmcia_socket_entry_s *entry, **ptr;
1567
1568 ptr = &pcmcia_sockets;
1569 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1570 if (entry->socket == socket) {
1571 *ptr = entry->next;
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001572 g_free(entry);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001573 }
1574}
1575
1576void pcmcia_info(Monitor *mon)
1577{
1578 struct pcmcia_socket_entry_s *iter;
1579
1580 if (!pcmcia_sockets)
1581 monitor_printf(mon, "No PCMCIA sockets\n");
1582
1583 for (iter = pcmcia_sockets; iter; iter = iter->next)
1584 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1585 iter->socket->attached ? iter->socket->card_string :
1586 "Empty");
1587}
1588
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001589/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001590/* machine registration */
1591
1592static QEMUMachine *first_machine = NULL;
1593QEMUMachine *current_machine = NULL;
1594
1595int qemu_register_machine(QEMUMachine *m)
1596{
1597 QEMUMachine **pm;
1598 pm = &first_machine;
1599 while (*pm != NULL)
1600 pm = &(*pm)->next;
1601 m->next = NULL;
1602 *pm = m;
1603 return 0;
1604}
1605
1606static QEMUMachine *find_machine(const char *name)
1607{
1608 QEMUMachine *m;
1609
1610 for(m = first_machine; m != NULL; m = m->next) {
1611 if (!strcmp(m->name, name))
1612 return m;
1613 }
1614 return NULL;
1615}
1616
1617static QEMUMachine *find_default_machine(void)
1618{
1619 QEMUMachine *m;
1620
1621 for(m = first_machine; m != NULL; m = m->next) {
1622 if (m->is_default) {
1623 return m;
1624 }
1625 }
1626 return NULL;
1627}
1628
1629/***********************************************************/
1630/* main execution loop */
1631
1632static void gui_update(void *opaque)
1633{
1634 uint64_t interval = GUI_REFRESH_INTERVAL;
1635 DisplayState *ds = opaque;
1636 DisplayChangeListener *dcl = ds->listeners;
1637
1638 dpy_refresh(ds);
1639
1640 while (dcl != NULL) {
1641 if (dcl->gui_timer_interval &&
1642 dcl->gui_timer_interval < interval)
1643 interval = dcl->gui_timer_interval;
1644 dcl = dcl->next;
1645 }
David 'Digit' Turnerdcda9492014-02-16 15:13:55 +01001646 timer_mod(ds->gui_timer, interval + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001647}
1648
1649static void nographic_update(void *opaque)
1650{
1651 uint64_t interval = GUI_REFRESH_INTERVAL;
1652
David 'Digit' Turnerdcda9492014-02-16 15:13:55 +01001653 timer_mod(nographic_timer, interval + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001654}
1655
1656struct vm_change_state_entry {
1657 VMChangeStateHandler *cb;
1658 void *opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001659 QLIST_ENTRY (vm_change_state_entry) entries;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001660};
1661
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001662static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001663
1664VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1665 void *opaque)
1666{
1667 VMChangeStateEntry *e;
1668
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001669 e = g_malloc0(sizeof (*e));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001670
1671 e->cb = cb;
1672 e->opaque = opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001673 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001674 return e;
1675}
1676
1677void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1678{
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001679 QLIST_REMOVE (e, entries);
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001680 g_free (e);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001681}
1682
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001683void vm_state_notify(int running, int reason)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001684{
1685 VMChangeStateEntry *e;
1686
1687 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1688 e->cb(e->opaque, running, reason);
1689 }
1690}
1691
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001692void vm_start(void)
1693{
1694 if (!vm_running) {
1695 cpu_enable_ticks();
1696 vm_running = 1;
1697 vm_state_notify(1, 0);
David Turner6a9ef172010-09-09 22:54:36 +02001698 //qemu_rearm_alarm_timer(alarm_timer);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001699 resume_all_vcpus();
1700 }
1701}
1702
1703/* reset/shutdown handler */
1704
1705typedef struct QEMUResetEntry {
1706 QEMUResetHandler *func;
1707 void *opaque;
1708 int order;
1709 struct QEMUResetEntry *next;
1710} QEMUResetEntry;
1711
1712static QEMUResetEntry *first_reset_entry;
1713static int reset_requested;
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001714static int shutdown_requested, shutdown_signal = -1;
1715static pid_t shutdown_pid;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001716static int powerdown_requested;
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001717int debug_requested;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001718static int vmstop_requested;
1719
1720int qemu_shutdown_requested(void)
1721{
1722 int r = shutdown_requested;
1723 shutdown_requested = 0;
1724 return r;
1725}
1726
1727int qemu_reset_requested(void)
1728{
1729 int r = reset_requested;
1730 reset_requested = 0;
1731 return r;
1732}
1733
1734int qemu_powerdown_requested(void)
1735{
1736 int r = powerdown_requested;
1737 powerdown_requested = 0;
1738 return r;
1739}
1740
David 'Digit' Turnerf9077a82014-02-10 23:10:47 +01001741int qemu_debug_requested(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001742{
1743 int r = debug_requested;
1744 debug_requested = 0;
1745 return r;
1746}
1747
David 'Digit' Turnerf9077a82014-02-10 23:10:47 +01001748int qemu_vmstop_requested(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001749{
1750 int r = vmstop_requested;
1751 vmstop_requested = 0;
1752 return r;
1753}
1754
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001755void qemu_register_reset(QEMUResetHandler *func, int order, void *opaque)
1756{
1757 QEMUResetEntry **pre, *re;
1758
1759 pre = &first_reset_entry;
1760 while (*pre != NULL && (*pre)->order >= order) {
1761 pre = &(*pre)->next;
1762 }
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001763 re = g_malloc0(sizeof(QEMUResetEntry));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001764 re->func = func;
1765 re->opaque = opaque;
1766 re->order = order;
1767 re->next = NULL;
1768 *pre = re;
1769}
1770
1771void qemu_system_reset(void)
1772{
1773 QEMUResetEntry *re;
1774
1775 /* reset all devices */
1776 for(re = first_reset_entry; re != NULL; re = re->next) {
1777 re->func(re->opaque);
1778 }
1779}
1780
1781void qemu_system_reset_request(void)
1782{
1783 if (no_reboot) {
1784 shutdown_requested = 1;
1785 } else {
1786 reset_requested = 1;
1787 }
1788 qemu_notify_event();
1789}
1790
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001791void qemu_system_killed(int signal, pid_t pid)
1792{
1793 shutdown_signal = signal;
1794 shutdown_pid = pid;
1795 qemu_system_shutdown_request();
1796}
1797
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001798void qemu_system_shutdown_request(void)
1799{
1800 shutdown_requested = 1;
1801 qemu_notify_event();
1802}
1803
1804void qemu_system_powerdown_request(void)
1805{
1806 powerdown_requested = 1;
1807 qemu_notify_event();
1808}
1809
David 'Digit' Turnerf9077a82014-02-10 23:10:47 +01001810int vm_can_run(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001811{
1812 if (powerdown_requested)
1813 return 0;
1814 if (reset_requested)
1815 return 0;
1816 if (shutdown_requested)
1817 return 0;
1818 if (debug_requested)
1819 return 0;
1820 return 1;
1821}
1822
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07001823void version(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001824{
1825 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1826}
1827
1828void qemu_help(int exitcode)
1829{
1830 version();
1831 printf("usage: %s [options] [disk_image]\n"
1832 "\n"
1833 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
1834 "\n"
1835#define DEF(option, opt_arg, opt_enum, opt_help) \
1836 opt_help
1837#define DEFHEADING(text) stringify(text) "\n"
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001838#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001839#undef DEF
1840#undef DEFHEADING
1841#undef GEN_DOCS
1842 "\n"
1843 "During emulation, the following keys are useful:\n"
1844 "ctrl-alt-f toggle full screen\n"
1845 "ctrl-alt-n switch to virtual console 'n'\n"
1846 "ctrl-alt toggle mouse and keyboard grab\n"
1847 "\n"
1848 "When using -nographic, press 'ctrl-a h' to get some help.\n"
1849 ,
1850 "qemu",
1851 DEFAULT_RAM_SIZE,
1852#ifndef _WIN32
1853 DEFAULT_NETWORK_SCRIPT,
1854 DEFAULT_NETWORK_DOWN_SCRIPT,
1855#endif
1856 DEFAULT_GDBSTUB_PORT,
1857 "/tmp/qemu.log");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07001858 QEMU_EXIT(exitcode);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001859}
1860
1861#define HAS_ARG 0x0001
1862
1863enum {
1864#define DEF(option, opt_arg, opt_enum, opt_help) \
1865 opt_enum,
1866#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001867#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001868#undef DEF
1869#undef DEFHEADING
1870#undef GEN_DOCS
1871};
1872
1873typedef struct QEMUOption {
1874 const char *name;
1875 int flags;
1876 int index;
1877} QEMUOption;
1878
1879static const QEMUOption qemu_options[] = {
1880 { "h", 0, QEMU_OPTION_h },
1881#define DEF(option, opt_arg, opt_enum, opt_help) \
1882 { option, opt_arg, opt_enum },
1883#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001884#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001885#undef DEF
1886#undef DEFHEADING
1887#undef GEN_DOCS
1888 { NULL, 0, 0 },
1889};
1890
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001891static void select_vgahw (const char *p)
1892{
1893 const char *opts;
1894
1895 cirrus_vga_enabled = 0;
1896 std_vga_enabled = 0;
1897 vmsvga_enabled = 0;
1898 xenfb_enabled = 0;
1899 if (strstart(p, "std", &opts)) {
1900 std_vga_enabled = 1;
1901 } else if (strstart(p, "cirrus", &opts)) {
1902 cirrus_vga_enabled = 1;
1903 } else if (strstart(p, "vmware", &opts)) {
1904 vmsvga_enabled = 1;
1905 } else if (strstart(p, "xenfb", &opts)) {
1906 xenfb_enabled = 1;
1907 } else if (!strstart(p, "none", &opts)) {
1908 invalid_vga:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07001909 PANIC("Unknown vga type: %s", p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001910 }
1911 while (*opts) {
1912 const char *nextopt;
1913
1914 if (strstart(opts, ",retrace=", &nextopt)) {
1915 opts = nextopt;
1916 if (strstart(opts, "dumb", &nextopt))
1917 vga_retrace_method = VGA_RETRACE_DUMB;
1918 else if (strstart(opts, "precise", &nextopt))
1919 vga_retrace_method = VGA_RETRACE_PRECISE;
1920 else goto invalid_vga;
1921 } else goto invalid_vga;
1922 opts = nextopt;
1923 }
1924}
1925
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001926#define MAX_NET_CLIENTS 32
1927
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001928#ifdef _WIN32
1929/* Look for support files in the same directory as the executable. */
1930static char *find_datadir(const char *argv0)
1931{
1932 char *p;
1933 char buf[MAX_PATH];
1934 DWORD len;
1935
1936 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
1937 if (len == 0) {
1938 return NULL;
1939 }
1940
1941 buf[len] = 0;
1942 p = buf + len - 1;
1943 while (p != buf && *p != '\\')
1944 p--;
1945 *p = 0;
1946 if (access(buf, R_OK) == 0) {
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001947 return g_strdup(buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001948 }
1949 return NULL;
1950}
1951#else /* !_WIN32 */
1952
David 'Digit' Turner24d27522011-06-01 16:50:56 +02001953/* Similarly, return the location of the executable */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001954static char *find_datadir(const char *argv0)
1955{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001956 char *p = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001957 char buf[PATH_MAX];
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001958
1959#if defined(__linux__)
1960 {
1961 int len;
1962 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
1963 if (len > 0) {
1964 buf[len] = 0;
1965 p = buf;
1966 }
1967 }
1968#elif defined(__FreeBSD__)
1969 {
1970 int len;
1971 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
1972 if (len > 0) {
1973 buf[len] = 0;
1974 p = buf;
1975 }
1976 }
1977#endif
1978 /* If we don't have any way of figuring out the actual executable
1979 location then try argv[0]. */
1980 if (!p) {
David 'Digit' Turner24d27522011-06-01 16:50:56 +02001981 p = realpath(argv0, buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001982 if (!p) {
1983 return NULL;
1984 }
1985 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001986
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001987 return g_strdup(dirname(buf));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001988}
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001989#endif
1990
David 'Digit' Turner24d27522011-06-01 16:50:56 +02001991static char*
1992qemu_find_file_with_subdir(const char* data_dir, const char* subdir, const char* name)
1993{
1994 int len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001995 char* buf = g_malloc0(len);
David 'Digit' Turner24d27522011-06-01 16:50:56 +02001996
1997 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1998 VERBOSE_PRINT(init," trying to find: %s\n", buf);
1999 if (access(buf, R_OK)) {
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01002000 g_free(buf);
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002001 return NULL;
2002 }
2003 return buf;
2004}
2005
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002006char *qemu_find_file(int type, const char *name)
2007{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002008 const char *subdir;
2009 char *buf;
2010
2011 /* If name contains path separators then try it as a straight path. */
2012 if ((strchr(name, '/') || strchr(name, '\\'))
2013 && access(name, R_OK) == 0) {
2014 return strdup(name);
2015 }
2016 switch (type) {
2017 case QEMU_FILE_TYPE_BIOS:
2018 subdir = "";
2019 break;
2020 case QEMU_FILE_TYPE_KEYMAP:
2021 subdir = "keymaps/";
2022 break;
2023 default:
2024 abort();
2025 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002026 buf = qemu_find_file_with_subdir(data_dir, subdir, name);
2027#ifdef CONFIG_ANDROID
2028 if (type == QEMU_FILE_TYPE_BIOS) {
2029 /* This case corresponds to the emulator being used as part of an
2030 * SDK installation. NOTE: data_dir is really $bindir. */
2031 if (buf == NULL)
2032 buf = qemu_find_file_with_subdir(data_dir, "lib/pc-bios/", name);
2033 /* This case corresponds to platform builds. */
2034 if (buf == NULL)
2035 buf = qemu_find_file_with_subdir(data_dir, "../usr/share/pc-bios/", name);
2036 /* Finally, try this for standalone builds under external/qemu */
2037 if (buf == NULL)
Andrew Hsieh3d894282012-05-02 12:06:12 +08002038 buf = qemu_find_file_with_subdir(data_dir, "../../../prebuilts/qemu-kernel/x86/pc-bios/", name);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002039 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002040#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002041 return buf;
2042}
2043
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07002044static int
2045add_dns_server( const char* server_name )
2046{
2047 SockAddress addr;
2048
2049 if (sock_address_init_resolve( &addr, server_name, 55, 0 ) < 0) {
2050 fprintf(stdout,
2051 "### WARNING: can't resolve DNS server name '%s'\n",
2052 server_name );
2053 return -1;
2054 }
2055
2056 fprintf(stderr,
2057 "DNS server name '%s' resolved to %s\n", server_name, sock_address_to_string(&addr) );
2058
2059 if ( slirp_add_dns_server( &addr ) < 0 ) {
2060 fprintf(stderr,
2061 "### WARNING: could not add DNS server '%s' to the network stack\n", server_name);
2062 return -1;
2063 }
2064 return 0;
2065}
2066
rich cannings7339b552011-02-16 13:43:44 -08002067/* Parses an integer
2068 * Pararm:
2069 * str String containing a number to be parsed.
2070 * result Passes the parsed integer in this argument
2071 * returns 0 if ok, -1 if failed
2072 */
2073int
2074parse_int(const char *str, int *result)
2075{
2076 char* r;
2077 *result = strtol(str, &r, 0);
2078 if (r == NULL || *r != '\0')
2079 return -1;
2080
2081 return 0;
2082}
2083
rich cannings7339b552011-02-16 13:43:44 -08002084/* parses a null-terminated string specifying a network port (e.g., "80") or
2085 * port range (e.g., "[6666-7000]"). In case of a single port, lport and hport
2086 * are the same. Returns 0 on success, -1 on error. */
2087
2088int parse_port_range(const char *str, unsigned short *lport,
2089 unsigned short *hport) {
2090
2091 unsigned int low = 0, high = 0;
2092 char *p, *arg = strdup(str);
2093
2094 if ((*arg == '[') && ((p = strrchr(arg, ']')) != NULL)) {
2095 p = arg + 1; /* skip '[' */
2096 low = atoi(strtok(p, "-"));
2097 high = atoi(strtok(NULL, "-"));
2098 if ((low > 0) && (high > 0) && (low < high) && (high < 65535)) {
2099 *lport = low;
2100 *hport = high;
2101 }
2102 }
2103 else {
2104 low = atoi(arg);
2105 if ((0 < low) && (low < 65535)) {
2106 *lport = low;
2107 *hport = low;
2108 }
2109 }
2110 free(arg);
2111 if (low != 0)
2112 return 0;
2113 return -1;
2114}
2115
2116/*
2117 * Implements the generic port forwarding option
2118 */
2119void
2120net_slirp_forward(const char *optarg)
2121{
2122 /*
2123 * we expect the following format:
2124 * dst_net:dst_mask:dst_port:redirect_ip:redirect_port OR
2125 * dst_net:dst_mask:[dp_range_start-dp_range_end]:redirect_ip:redirect_port
2126 */
2127 char *argument = strdup(optarg), *p = argument;
2128 char *dst_net, *dst_mask, *dst_port;
2129 char *redirect_ip, *redirect_port;
2130 uint32_t dnet, dmask, rip;
Vladimir Chtchetkineb557e9f2012-03-22 15:38:08 -07002131 unsigned short dlport = 0, dhport = 0, rport;
rich cannings7339b552011-02-16 13:43:44 -08002132
2133
2134 dst_net = strtok(p, ":");
2135 dst_mask = strtok(NULL, ":");
2136 dst_port = strtok(NULL, ":");
2137 redirect_ip = strtok(NULL, ":");
2138 redirect_port = strtok(NULL, ":");
2139
2140 if (dst_net == NULL || dst_mask == NULL || dst_port == NULL ||
2141 redirect_ip == NULL || redirect_port == NULL) {
2142 fprintf(stderr,
2143 "Invalid argument for -net-forward, we expect "
2144 "dst_net:dst_mask:dst_port:redirect_ip:redirect_port or "
2145 "dst_net:dst_mask:[dp_range_start-dp_range_end]"
2146 ":redirect_ip:redirect_port: %s\n",
2147 optarg);
2148 exit(1);
2149 }
2150
2151 /* inet_strtoip converts dotted address to host byte order */
2152 if (inet_strtoip(dst_net, &dnet) == -1) {
2153 fprintf(stderr, "Invalid destination IP net: %s\n", dst_net);
2154 exit(1);
2155 }
2156 if (inet_strtoip(dst_mask, &dmask) == -1) {
2157 fprintf(stderr, "Invalid destination IP mask: %s\n", dst_mask);
2158 exit(1);
2159 }
2160 if (inet_strtoip(redirect_ip, &rip) == -1) {
2161 fprintf(stderr, "Invalid redirect IP address: %s\n", redirect_ip);
2162 exit(1);
2163 }
2164
2165 if (parse_port_range(dst_port, &dlport, &dhport) == -1) {
2166 fprintf(stderr, "Invalid destination port or port range\n");
2167 exit(1);
2168 }
2169
2170 rport = atoi(redirect_port);
2171 if (!rport) {
2172 fprintf(stderr, "Invalid redirect port: %s\n", redirect_port);
2173 exit(1);
2174 }
2175
2176 dnet &= dmask;
2177
2178 slirp_add_net_forward(dnet, dmask, dlport, dhport,
2179 rip, rport);
2180
2181 free(argument);
2182}
2183
2184
2185/* Parses an -allow-tcp or -allow-udp argument and inserts a corresponding
2186 * entry in the allows list */
2187void
2188slirp_allow(const char *optarg, u_int8_t proto)
2189{
2190 /*
2191 * we expect the following format:
2192 * dst_ip:dst_port OR dst_ip:[dst_lport-dst_hport]
2193 */
2194 char *argument = strdup(optarg), *p = argument;
2195 char *dst_ip_str, *dst_port_str;
2196 uint32_t dst_ip;
Vladimir Chtchetkineb557e9f2012-03-22 15:38:08 -07002197 unsigned short dst_lport = 0, dst_hport = 0;
rich cannings7339b552011-02-16 13:43:44 -08002198
2199 dst_ip_str = strtok(p, ":");
2200 dst_port_str = strtok(NULL, ":");
2201
2202 if (dst_ip_str == NULL || dst_port_str == NULL) {
2203 fprintf(stderr,
2204 "Invalid argument %s for -allow. We expect "
2205 "dst_ip:dst_port or dst_ip:[dst_lport-dst_hport]\n",
2206 optarg);
2207 exit(1);
2208 }
2209
2210 if (inet_strtoip(dst_ip_str, &dst_ip) == -1) {
2211 fprintf(stderr, "Invalid destination IP address: %s\n", dst_ip_str);
2212 exit(1);
2213 }
2214 if (parse_port_range(dst_port_str, &dst_lport, &dst_hport) == -1) {
2215 fprintf(stderr, "Invalid destination port or port range\n");
2216 exit(1);
2217 }
2218
2219 slirp_add_allow(dst_ip, dst_lport, dst_hport, proto);
2220
2221 free(argument);
2222}
2223
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01002224/* Add a serial device at a given location in the emulated hardware table.
2225 * On failure, this function aborts the program with an error message.
2226 */
2227static void
2228serial_hds_add_at(int index, const char* devname)
2229{
2230 char label[32];
2231
2232 if (!devname || !strcmp(devname,"none"))
2233 return;
2234
2235 if (index >= MAX_SERIAL_PORTS) {
2236 PANIC("qemu: invalid serial index for %s (%d >= %d)",
2237 devname, index, MAX_SERIAL_PORTS);
2238 }
2239 if (serial_hds[index] != NULL) {
2240 PANIC("qemu: invalid serial index for %s (%d: already taken!)",
2241 devname, index);
2242 }
2243 snprintf(label, sizeof(label), "serial%d", index);
2244 serial_hds[index] = qemu_chr_open(label, devname, NULL);
2245 if (!serial_hds[index]) {
2246 PANIC("qemu: could not open serial device '%s'", devname);
2247 }
2248}
2249
2250
2251/* Find a free slot in the emulated serial device table, and register
2252 * it. Return the allocated table index.
2253 */
2254static int
2255serial_hds_add(const char* devname)
2256{
2257 int index;
2258
2259 /* Find first free slot */
2260 for (index = 0; index < MAX_SERIAL_PORTS; index++) {
2261 if (serial_hds[index] == NULL) {
2262 serial_hds_add_at(index, devname);
2263 return index;
2264 }
2265 }
2266
2267 PANIC("qemu: too many serial devices registered (%d)", index);
2268 return -1; /* shouldn't happen */
2269}
2270
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002271int main(int argc, char **argv, char **envp)
2272{
2273 const char *gdbstub_dev = NULL;
2274 uint32_t boot_devices_bitmap = 0;
2275 int i;
David 'Digit' Turnera2c14f92014-02-04 01:02:30 +01002276 int snapshot, linux_boot, __attribute__((unused)) net_boot;
David Turner6a9ef172010-09-09 22:54:36 +02002277 const char *icount_option = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002278 const char *initrd_filename;
2279 const char *kernel_filename, *kernel_cmdline;
2280 const char *boot_devices = "";
2281 DisplayState *ds;
2282 DisplayChangeListener *dcl;
2283 int cyls, heads, secs, translation;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002284 QemuOpts *hda_opts = NULL;
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01002285 QemuOpts *hdb_opts = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002286 const char *net_clients[MAX_NET_CLIENTS];
2287 int nb_net_clients;
2288 const char *bt_opts[MAX_BT_CMDLINE];
2289 int nb_bt_opts;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002290 int optind;
2291 const char *r, *optarg;
2292 CharDriverState *monitor_hd = NULL;
2293 const char *monitor_device;
2294 const char *serial_devices[MAX_SERIAL_PORTS];
2295 int serial_device_index;
2296 const char *parallel_devices[MAX_PARALLEL_PORTS];
2297 int parallel_device_index;
2298 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
2299 int virtio_console_index;
2300 const char *loadvm = NULL;
2301 QEMUMachine *machine;
2302 const char *cpu_model;
2303 const char *usb_devices[MAX_USB_CMDLINE];
2304 int usb_devices_index;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002305 int tb_size;
2306 const char *pid_file = NULL;
2307 const char *incoming = NULL;
David 'Digit' Turner7e9f6c12014-01-22 18:42:40 +01002308 const char* log_mask = NULL;
2309 const char* log_file = NULL;
David 'Digit' Turnere2678e12014-01-16 15:56:43 +01002310 CPUOldState *env;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002311 int show_vnc_port = 0;
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07002312 IniFile* hw_ini = NULL;
David 'Digit' Turner5f824112011-03-01 14:00:26 +01002313 STRALLOC_DEFINE(kernel_params);
2314 STRALLOC_DEFINE(kernel_config);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07002315 int dns_count = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002316
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002317 /* Initialize sockets before anything else, so we can properly report
2318 * initialization failures back to the UI. */
2319#ifdef _WIN32
2320 socket_init();
2321#endif
2322
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07002323 init_clocks();
2324
David 'Digit' Turner8848f632014-01-16 18:10:21 +01002325 qemu_cache_utils_init();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002326
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002327 QLIST_INIT (&vm_change_state_head);
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02002328 os_setup_early_signal_handling();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002329
2330 module_call_init(MODULE_INIT_MACHINE);
2331 machine = find_default_machine();
2332 cpu_model = NULL;
2333 initrd_filename = NULL;
2334 ram_size = 0;
2335 snapshot = 0;
2336 kernel_filename = NULL;
2337 kernel_cmdline = "";
David 'Digit' Turner5f824112011-03-01 14:00:26 +01002338
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002339 cyls = heads = secs = 0;
2340 translation = BIOS_ATA_TRANSLATION_AUTO;
2341 monitor_device = "vc:80Cx24C";
2342
2343 serial_devices[0] = "vc:80Cx24C";
2344 for(i = 1; i < MAX_SERIAL_PORTS; i++)
2345 serial_devices[i] = NULL;
2346 serial_device_index = 0;
2347
2348 parallel_devices[0] = "vc:80Cx24C";
2349 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
2350 parallel_devices[i] = NULL;
2351 parallel_device_index = 0;
2352
2353 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
2354 virtio_consoles[i] = NULL;
2355 virtio_console_index = 0;
2356
2357 for (i = 0; i < MAX_NODES; i++) {
2358 node_mem[i] = 0;
2359 node_cpumask[i] = 0;
2360 }
2361
2362 usb_devices_index = 0;
2363
2364 nb_net_clients = 0;
2365 nb_bt_opts = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002366#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002367 nb_drives = 0;
2368 nb_drives_opt = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002369#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002370 nb_numa_nodes = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002371
2372 nb_nics = 0;
2373
2374 tb_size = 0;
2375 autostart= 1;
2376
2377 register_watchdogs();
2378
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07002379 /* Initialize boot properties. */
2380 boot_property_init_service();
David 'Digit' Turnerca950592011-04-27 12:26:15 +02002381 android_hw_control_init();
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02002382 android_net_pipes_init();
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07002383
David 'Digit' Turner36597752011-05-20 01:18:01 +02002384#ifdef CONFIG_KVM
2385 /* By default, force auto-detection for kvm */
2386 kvm_allowed = -1;
2387#endif
2388
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002389 optind = 1;
2390 for(;;) {
2391 if (optind >= argc)
2392 break;
2393 r = argv[optind];
2394 if (r[0] != '-') {
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002395 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002396 } else {
2397 const QEMUOption *popt;
2398
2399 optind++;
2400 /* Treat --foo the same as -foo. */
2401 if (r[1] == '-')
2402 r++;
2403 popt = qemu_options;
2404 for(;;) {
2405 if (!popt->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002406 PANIC("%s: invalid option -- '%s'",
2407 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002408 }
2409 if (!strcmp(popt->name, r + 1))
2410 break;
2411 popt++;
2412 }
2413 if (popt->flags & HAS_ARG) {
2414 if (optind >= argc) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002415 PANIC("%s: option '%s' requires an argument",
2416 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002417 }
2418 optarg = argv[optind++];
2419 } else {
2420 optarg = NULL;
2421 }
2422
2423 switch(popt->index) {
2424 case QEMU_OPTION_M:
2425 machine = find_machine(optarg);
2426 if (!machine) {
2427 QEMUMachine *m;
2428 printf("Supported machines are:\n");
2429 for(m = first_machine; m != NULL; m = m->next) {
2430 printf("%-10s %s%s\n",
2431 m->name, m->desc,
2432 m->is_default ? " (default)" : "");
2433 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002434 if (*optarg != '?') {
2435 PANIC("Invalid machine parameter: %s",
2436 optarg);
2437 } else {
2438 QEMU_EXIT(0);
2439 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002440 }
2441 break;
2442 case QEMU_OPTION_cpu:
2443 /* hw initialization will check this */
2444 if (*optarg == '?') {
2445/* XXX: implement xxx_cpu_list for targets that still miss it */
2446#if defined(cpu_list)
2447 cpu_list(stdout, &fprintf);
2448#endif
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002449 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002450 } else {
2451 cpu_model = optarg;
2452 }
2453 break;
2454 case QEMU_OPTION_initrd:
2455 initrd_filename = optarg;
2456 break;
2457 case QEMU_OPTION_hda:
2458 if (cyls == 0)
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002459 hda_opts = drive_add(optarg, HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002460 else
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002461 hda_opts = drive_add(optarg, HD_ALIAS
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002462 ",cyls=%d,heads=%d,secs=%d%s",
2463 0, cyls, heads, secs,
2464 translation == BIOS_ATA_TRANSLATION_LBA ?
2465 ",trans=lba" :
2466 translation == BIOS_ATA_TRANSLATION_NONE ?
2467 ",trans=none" : "");
2468 break;
2469 case QEMU_OPTION_hdb:
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01002470 hdb_opts = drive_add(optarg, HD_ALIAS, 1);
2471 break;
2472
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002473 case QEMU_OPTION_hdc:
2474 case QEMU_OPTION_hdd:
2475 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
2476 break;
2477 case QEMU_OPTION_drive:
2478 drive_add(NULL, "%s", optarg);
2479 break;
2480 case QEMU_OPTION_mtdblock:
2481 drive_add(optarg, MTD_ALIAS);
2482 break;
2483 case QEMU_OPTION_sd:
2484 drive_add(optarg, SD_ALIAS);
2485 break;
2486 case QEMU_OPTION_pflash:
2487 drive_add(optarg, PFLASH_ALIAS);
2488 break;
2489 case QEMU_OPTION_snapshot:
2490 snapshot = 1;
2491 break;
2492 case QEMU_OPTION_hdachs:
2493 {
2494 const char *p;
2495 p = optarg;
2496 cyls = strtol(p, (char **)&p, 0);
2497 if (cyls < 1 || cyls > 16383)
2498 goto chs_fail;
2499 if (*p != ',')
2500 goto chs_fail;
2501 p++;
2502 heads = strtol(p, (char **)&p, 0);
2503 if (heads < 1 || heads > 16)
2504 goto chs_fail;
2505 if (*p != ',')
2506 goto chs_fail;
2507 p++;
2508 secs = strtol(p, (char **)&p, 0);
2509 if (secs < 1 || secs > 63)
2510 goto chs_fail;
2511 if (*p == ',') {
2512 p++;
2513 if (!strcmp(p, "none"))
2514 translation = BIOS_ATA_TRANSLATION_NONE;
2515 else if (!strcmp(p, "lba"))
2516 translation = BIOS_ATA_TRANSLATION_LBA;
2517 else if (!strcmp(p, "auto"))
2518 translation = BIOS_ATA_TRANSLATION_AUTO;
2519 else
2520 goto chs_fail;
2521 } else if (*p != '\0') {
2522 chs_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002523 PANIC("qemu: invalid physical CHS format");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002524 }
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002525 if (hda_opts != NULL) {
2526 char num[16];
2527 snprintf(num, sizeof(num), "%d", cyls);
2528 qemu_opt_set(hda_opts, "cyls", num);
2529 snprintf(num, sizeof(num), "%d", heads);
2530 qemu_opt_set(hda_opts, "heads", num);
2531 snprintf(num, sizeof(num), "%d", secs);
2532 qemu_opt_set(hda_opts, "secs", num);
2533 if (translation == BIOS_ATA_TRANSLATION_LBA)
2534 qemu_opt_set(hda_opts, "trans", "lba");
2535 if (translation == BIOS_ATA_TRANSLATION_NONE)
2536 qemu_opt_set(hda_opts, "trans", "none");
2537 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002538 }
2539 break;
2540 case QEMU_OPTION_numa:
2541 if (nb_numa_nodes >= MAX_NODES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002542 PANIC("qemu: too many NUMA nodes");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002543 }
2544 numa_add(optarg);
2545 break;
2546 case QEMU_OPTION_nographic:
2547 display_type = DT_NOGRAPHIC;
2548 break;
2549#ifdef CONFIG_CURSES
2550 case QEMU_OPTION_curses:
2551 display_type = DT_CURSES;
2552 break;
2553#endif
2554 case QEMU_OPTION_portrait:
2555 graphic_rotate = 1;
2556 break;
2557 case QEMU_OPTION_kernel:
2558 kernel_filename = optarg;
2559 break;
2560 case QEMU_OPTION_append:
2561 kernel_cmdline = optarg;
2562 break;
2563 case QEMU_OPTION_cdrom:
2564 drive_add(optarg, CDROM_ALIAS);
2565 break;
2566 case QEMU_OPTION_boot:
2567 boot_devices = optarg;
2568 /* We just do some generic consistency checks */
2569 {
2570 /* Could easily be extended to 64 devices if needed */
2571 const char *p;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07002572
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002573 boot_devices_bitmap = 0;
2574 for (p = boot_devices; *p != '\0'; p++) {
2575 /* Allowed boot devices are:
2576 * a b : floppy disk drives
2577 * c ... f : IDE disk drives
2578 * g ... m : machine implementation dependant drives
2579 * n ... p : network devices
2580 * It's up to each machine implementation to check
2581 * if the given boot devices match the actual hardware
2582 * implementation and firmware features.
2583 */
2584 if (*p < 'a' || *p > 'q') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002585 PANIC("Invalid boot device '%c'", *p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002586 }
2587 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002588 PANIC(
2589 "Boot device '%c' was given twice",*p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002590 }
2591 boot_devices_bitmap |= 1 << (*p - 'a');
2592 }
2593 }
2594 break;
2595 case QEMU_OPTION_fda:
2596 case QEMU_OPTION_fdb:
2597 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2598 break;
2599#ifdef TARGET_I386
2600 case QEMU_OPTION_no_fd_bootchk:
2601 fd_bootchk = 0;
2602 break;
2603#endif
2604 case QEMU_OPTION_net:
2605 if (nb_net_clients >= MAX_NET_CLIENTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002606 PANIC("qemu: too many network clients");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002607 }
2608 net_clients[nb_net_clients] = optarg;
2609 nb_net_clients++;
2610 break;
2611#ifdef CONFIG_SLIRP
2612 case QEMU_OPTION_tftp:
2613 tftp_prefix = optarg;
2614 break;
2615 case QEMU_OPTION_bootp:
2616 bootp_filename = optarg;
2617 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002618 case QEMU_OPTION_redir:
2619 net_slirp_redir(NULL, optarg, NULL);
2620 break;
2621#endif
2622 case QEMU_OPTION_bt:
2623 if (nb_bt_opts >= MAX_BT_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002624 PANIC("qemu: too many bluetooth options");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002625 }
2626 bt_opts[nb_bt_opts++] = optarg;
2627 break;
2628#ifdef HAS_AUDIO
2629 case QEMU_OPTION_audio_help:
2630 AUD_help ();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002631 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002632 break;
2633 case QEMU_OPTION_soundhw:
2634 select_soundhw (optarg);
2635 break;
2636#endif
2637 case QEMU_OPTION_h:
2638 qemu_help(0);
2639 break;
2640 case QEMU_OPTION_version:
2641 version();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002642 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002643 break;
2644 case QEMU_OPTION_m: {
2645 uint64_t value;
2646 char *ptr;
2647
2648 value = strtoul(optarg, &ptr, 10);
2649 switch (*ptr) {
2650 case 0: case 'M': case 'm':
2651 value <<= 20;
2652 break;
2653 case 'G': case 'g':
2654 value <<= 30;
2655 break;
2656 default:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002657 PANIC("qemu: invalid ram size: %s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002658 }
2659
2660 /* On 32-bit hosts, QEMU is limited by virtual address space */
David 'Digit' Turner81911b02014-02-10 17:53:45 +01002661 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002662 PANIC("qemu: at most 2047 MB RAM can be simulated");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002663 }
2664 if (value != (uint64_t)(ram_addr_t)value) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002665 PANIC("qemu: ram size too large");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002666 }
2667 ram_size = value;
2668 break;
2669 }
2670 case QEMU_OPTION_d:
David 'Digit' Turner7e9f6c12014-01-22 18:42:40 +01002671 log_mask = optarg;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002672 break;
2673 case QEMU_OPTION_s:
2674 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2675 break;
2676 case QEMU_OPTION_gdb:
2677 gdbstub_dev = optarg;
2678 break;
2679 case QEMU_OPTION_L:
2680 data_dir = optarg;
2681 break;
2682 case QEMU_OPTION_bios:
2683 bios_name = optarg;
2684 break;
2685 case QEMU_OPTION_singlestep:
2686 singlestep = 1;
2687 break;
2688 case QEMU_OPTION_S:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002689 autostart = 0;
2690 break;
2691#ifndef _WIN32
2692 case QEMU_OPTION_k:
2693 keyboard_layout = optarg;
2694 break;
2695#endif
2696 case QEMU_OPTION_localtime:
2697 rtc_utc = 0;
2698 break;
2699 case QEMU_OPTION_vga:
2700 select_vgahw (optarg);
2701 break;
2702#if defined(TARGET_PPC) || defined(TARGET_SPARC)
2703 case QEMU_OPTION_g:
2704 {
2705 const char *p;
2706 int w, h, depth;
2707 p = optarg;
2708 w = strtol(p, (char **)&p, 10);
2709 if (w <= 0) {
2710 graphic_error:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002711 PANIC("qemu: invalid resolution or depth");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002712 }
2713 if (*p != 'x')
2714 goto graphic_error;
2715 p++;
2716 h = strtol(p, (char **)&p, 10);
2717 if (h <= 0)
2718 goto graphic_error;
2719 if (*p == 'x') {
2720 p++;
2721 depth = strtol(p, (char **)&p, 10);
2722 if (depth != 8 && depth != 15 && depth != 16 &&
2723 depth != 24 && depth != 32)
2724 goto graphic_error;
2725 } else if (*p == '\0') {
2726 depth = graphic_depth;
2727 } else {
2728 goto graphic_error;
2729 }
2730
2731 graphic_width = w;
2732 graphic_height = h;
2733 graphic_depth = depth;
2734 }
2735 break;
2736#endif
2737 case QEMU_OPTION_echr:
2738 {
2739 char *r;
2740 term_escape_char = strtol(optarg, &r, 0);
2741 if (r == optarg)
2742 printf("Bad argument to echr\n");
2743 break;
2744 }
2745 case QEMU_OPTION_monitor:
2746 monitor_device = optarg;
2747 break;
2748 case QEMU_OPTION_serial:
2749 if (serial_device_index >= MAX_SERIAL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002750 PANIC("qemu: too many serial ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002751 }
2752 serial_devices[serial_device_index] = optarg;
2753 serial_device_index++;
2754 break;
2755 case QEMU_OPTION_watchdog:
2756 i = select_watchdog(optarg);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002757 if (i > 0) {
2758 if (i == 1) {
2759 PANIC("Invalid watchdog parameter: %s",
2760 optarg);
2761 } else {
2762 QEMU_EXIT(0);
2763 }
2764 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002765 break;
2766 case QEMU_OPTION_watchdog_action:
2767 if (select_watchdog_action(optarg) == -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002768 PANIC("Unknown -watchdog-action parameter");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002769 }
2770 break;
2771 case QEMU_OPTION_virtiocon:
2772 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002773 PANIC("qemu: too many virtio consoles");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002774 }
2775 virtio_consoles[virtio_console_index] = optarg;
2776 virtio_console_index++;
2777 break;
2778 case QEMU_OPTION_parallel:
2779 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002780 PANIC("qemu: too many parallel ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002781 }
2782 parallel_devices[parallel_device_index] = optarg;
2783 parallel_device_index++;
2784 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00002785 case QEMU_OPTION_loadvm:
2786 loadvm = optarg;
2787 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00002788 case QEMU_OPTION_savevm_on_exit:
2789 savevm_on_exit = optarg;
2790 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002791 case QEMU_OPTION_full_screen:
2792 full_screen = 1;
2793 break;
2794#ifdef CONFIG_SDL
2795 case QEMU_OPTION_no_frame:
2796 no_frame = 1;
2797 break;
2798 case QEMU_OPTION_alt_grab:
2799 alt_grab = 1;
2800 break;
2801 case QEMU_OPTION_no_quit:
2802 no_quit = 1;
2803 break;
2804 case QEMU_OPTION_sdl:
2805 display_type = DT_SDL;
2806 break;
2807#endif
2808 case QEMU_OPTION_pidfile:
2809 pid_file = optarg;
2810 break;
2811#ifdef TARGET_I386
2812 case QEMU_OPTION_win2k_hack:
2813 win2k_install_hack = 1;
2814 break;
2815 case QEMU_OPTION_rtc_td_hack:
2816 rtc_td_hack = 1;
2817 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08002818#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002819 case QEMU_OPTION_acpitable:
2820 if(acpi_table_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002821 PANIC("Wrong acpi table provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002822 }
2823 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08002824#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002825 case QEMU_OPTION_smbios:
David 'Digit' Turnerae3098a2011-05-11 16:01:57 +02002826 do_smbios_option(optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002827 break;
2828#endif
2829#ifdef CONFIG_KVM
2830 case QEMU_OPTION_enable_kvm:
2831 kvm_allowed = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002832 break;
David 'Digit' Turner36597752011-05-20 01:18:01 +02002833 case QEMU_OPTION_disable_kvm:
2834 kvm_allowed = 0;
2835 break;
2836#endif /* CONFIG_KVM */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002837 case QEMU_OPTION_usb:
2838 usb_enabled = 1;
2839 break;
2840 case QEMU_OPTION_usbdevice:
2841 usb_enabled = 1;
2842 if (usb_devices_index >= MAX_USB_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002843 PANIC("Too many USB devices");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002844 }
2845 usb_devices[usb_devices_index] = optarg;
2846 usb_devices_index++;
2847 break;
2848 case QEMU_OPTION_smp:
2849 smp_cpus = atoi(optarg);
2850 if (smp_cpus < 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002851 PANIC("Invalid number of CPUs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002852 }
2853 break;
2854 case QEMU_OPTION_vnc:
2855 display_type = DT_VNC;
2856 vnc_display = optarg;
2857 break;
2858#ifdef TARGET_I386
2859 case QEMU_OPTION_no_acpi:
2860 acpi_enabled = 0;
2861 break;
2862 case QEMU_OPTION_no_hpet:
2863 no_hpet = 1;
2864 break;
2865 case QEMU_OPTION_no_virtio_balloon:
2866 no_virtio_balloon = 1;
2867 break;
2868#endif
2869 case QEMU_OPTION_no_reboot:
2870 no_reboot = 1;
2871 break;
2872 case QEMU_OPTION_no_shutdown:
2873 no_shutdown = 1;
2874 break;
2875 case QEMU_OPTION_show_cursor:
2876 cursor_hide = 0;
2877 break;
2878 case QEMU_OPTION_uuid:
2879 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002880 PANIC("Fail to parse UUID string. Wrong format.");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002881 }
2882 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002883 case QEMU_OPTION_option_rom:
2884 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002885 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002886 }
2887 option_rom[nb_option_roms] = optarg;
2888 nb_option_roms++;
2889 break;
2890#if defined(TARGET_ARM) || defined(TARGET_M68K)
2891 case QEMU_OPTION_semihosting:
2892 semihosting_enabled = 1;
2893 break;
2894#endif
2895 case QEMU_OPTION_name:
2896 qemu_name = optarg;
2897 break;
2898#if defined(TARGET_SPARC) || defined(TARGET_PPC)
2899 case QEMU_OPTION_prom_env:
2900 if (nb_prom_envs >= MAX_PROM_ENVS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002901 PANIC("Too many prom variables");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002902 }
2903 prom_envs[nb_prom_envs] = optarg;
2904 nb_prom_envs++;
2905 break;
2906#endif
2907#ifdef TARGET_ARM
2908 case QEMU_OPTION_old_param:
2909 old_param = 1;
2910 break;
2911#endif
2912 case QEMU_OPTION_clock:
2913 configure_alarms(optarg);
2914 break;
2915 case QEMU_OPTION_startdate:
2916 {
2917 struct tm tm;
David 'Digit' Turnerdc468202010-10-27 02:34:46 +02002918 time_t rtc_start_date = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002919 if (!strcmp(optarg, "now")) {
2920 rtc_date_offset = -1;
2921 } else {
2922 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
2923 &tm.tm_year,
2924 &tm.tm_mon,
2925 &tm.tm_mday,
2926 &tm.tm_hour,
2927 &tm.tm_min,
2928 &tm.tm_sec) == 6) {
2929 /* OK */
2930 } else if (sscanf(optarg, "%d-%d-%d",
2931 &tm.tm_year,
2932 &tm.tm_mon,
2933 &tm.tm_mday) == 3) {
2934 tm.tm_hour = 0;
2935 tm.tm_min = 0;
2936 tm.tm_sec = 0;
2937 } else {
2938 goto date_fail;
2939 }
2940 tm.tm_year -= 1900;
2941 tm.tm_mon--;
2942 rtc_start_date = mktimegm(&tm);
2943 if (rtc_start_date == -1) {
2944 date_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002945 PANIC("Invalid date format. Valid format are:\n"
2946 "'now' or '2006-06-17T16:01:21' or '2006-06-17'");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002947 }
2948 rtc_date_offset = time(NULL) - rtc_start_date;
2949 }
2950 }
2951 break;
rich cannings7339b552011-02-16 13:43:44 -08002952
2953 /* -------------------------------------------------------*/
2954 /* User mode network stack restrictions */
2955 case QEMU_OPTION_drop_udp:
2956 slirp_drop_udp();
2957 break;
2958 case QEMU_OPTION_drop_tcp:
2959 slirp_drop_tcp();
2960 break;
2961 case QEMU_OPTION_allow_tcp:
2962 slirp_allow(optarg, IPPROTO_TCP);
2963 break;
2964 case QEMU_OPTION_allow_udp:
2965 slirp_allow(optarg, IPPROTO_UDP);
2966 break;
2967 case QEMU_OPTION_drop_log:
2968 {
2969 FILE* drop_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08002970 drop_log_filename = optarg;
2971 drop_log_fd = fopen(optarg, "w+");
rich cannings7339b552011-02-16 13:43:44 -08002972
2973 if (!drop_log_fd) {
2974 fprintf(stderr, "Cannot open drop log: %s\n", optarg);
2975 exit(1);
2976 }
2977
2978 slirp_drop_log_fd(drop_log_fd);
2979 }
2980 break;
2981
2982 case QEMU_OPTION_dns_log:
2983 {
2984 FILE* dns_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08002985 dns_log_filename = optarg;
2986 dns_log_fd = fopen(optarg, "wb+");
rich cannings7339b552011-02-16 13:43:44 -08002987
2988 if (dns_log_fd == NULL) {
2989 fprintf(stderr, "Cannot open dns log: %s\n", optarg);
2990 exit(1);
2991 }
2992
2993 slirp_dns_log_fd(dns_log_fd);
2994 }
2995 break;
2996
2997
2998 case QEMU_OPTION_max_dns_conns:
2999 {
3000 int max_dns_conns = 0;
3001 if (parse_int(optarg, &max_dns_conns)) {
3002 fprintf(stderr,
3003 "qemu: syntax: -max-dns-conns max_connections\n");
3004 exit(1);
3005 }
3006 if (max_dns_conns <= 0 || max_dns_conns == LONG_MAX) {
3007 fprintf(stderr,
3008 "Invalid arg for max dns connections: %s\n",
3009 optarg);
3010 exit(1);
3011 }
3012 slirp_set_max_dns_conns(max_dns_conns);
3013 }
3014 break;
3015
3016 case QEMU_OPTION_net_forward:
3017 net_slirp_forward(optarg);
3018 break;
3019 case QEMU_OPTION_net_forward_tcp2sink:
3020 {
3021 SockAddress saddr;
3022
3023 if (parse_host_port(&saddr, optarg)) {
3024 fprintf(stderr,
3025 "Invalid ip/port %s for "
3026 "-forward-dropped-tcp2sink. "
3027 "We expect 'sink_ip:sink_port'\n",
3028 optarg);
3029 exit(1);
3030 }
3031 slirp_forward_dropped_tcp2sink(saddr.u.inet.address,
3032 saddr.u.inet.port);
3033 }
3034 break;
3035 /* -------------------------------------------------------*/
3036
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003037 case QEMU_OPTION_tb_size:
3038 tb_size = strtol(optarg, NULL, 0);
3039 if (tb_size < 0)
3040 tb_size = 0;
3041 break;
3042 case QEMU_OPTION_icount:
David Turner6a9ef172010-09-09 22:54:36 +02003043 icount_option = optarg;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003044 break;
3045 case QEMU_OPTION_incoming:
3046 incoming = optarg;
3047 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003048#ifdef CONFIG_XEN
3049 case QEMU_OPTION_xen_domid:
3050 xen_domid = atoi(optarg);
3051 break;
3052 case QEMU_OPTION_xen_create:
3053 xen_mode = XEN_CREATE;
3054 break;
3055 case QEMU_OPTION_xen_attach:
3056 xen_mode = XEN_ATTACH;
3057 break;
3058#endif
3059
3060
3061 case QEMU_OPTION_mic:
3062 audio_input_source = (char*)optarg;
3063 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003064#ifdef CONFIG_NAND
3065 case QEMU_OPTION_nand:
3066 nand_add_dev(optarg);
3067 break;
Jun Nakajimaa381ef02011-12-17 19:13:25 -08003068
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003069#endif
Jun Nakajimaa381ef02011-12-17 19:13:25 -08003070 case QEMU_OPTION_disable_hax:
3071 hax_disabled = 1;
3072 break;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07003073 case QEMU_OPTION_android_ports:
3074 android_op_ports = (char*)optarg;
3075 break;
3076
3077 case QEMU_OPTION_android_port:
3078 android_op_port = (char*)optarg;
3079 break;
3080
3081 case QEMU_OPTION_android_report_console:
3082 android_op_report_console = (char*)optarg;
3083 break;
3084
3085 case QEMU_OPTION_http_proxy:
3086 op_http_proxy = (char*)optarg;
3087 break;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003088
3089 case QEMU_OPTION_charmap:
3090 op_charmap_file = (char*)optarg;
3091 break;
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07003092
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003093 case QEMU_OPTION_android_hw:
3094 android_op_hwini = (char*)optarg;
3095 break;
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07003096
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003097 case QEMU_OPTION_dns_server:
3098 android_op_dns_server = (char*)optarg;
3099 break;
3100
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003101 case QEMU_OPTION_radio:
3102 android_op_radio = (char*)optarg;
3103 break;
3104
3105 case QEMU_OPTION_gps:
3106 android_op_gps = (char*)optarg;
3107 break;
3108
3109 case QEMU_OPTION_audio:
3110 android_op_audio = (char*)optarg;
3111 break;
3112
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003113 case QEMU_OPTION_cpu_delay:
3114 android_op_cpu_delay = (char*)optarg;
3115 break;
3116
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07003117 case QEMU_OPTION_show_kernel:
3118 android_kmsg_init(ANDROID_KMSG_PRINT_MESSAGES);
3119 break;
3120
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003121#ifdef CONFIG_NAND_LIMITS
3122 case QEMU_OPTION_nand_limits:
3123 android_op_nand_limits = (char*)optarg;
3124 break;
3125#endif // CONFIG_NAND_LIMITS
3126
3127 case QEMU_OPTION_netspeed:
3128 android_op_netspeed = (char*)optarg;
3129 break;
3130
3131 case QEMU_OPTION_netdelay:
3132 android_op_netdelay = (char*)optarg;
3133 break;
3134
3135 case QEMU_OPTION_netfast:
3136 android_op_netfast = 1;
3137 break;
3138
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07003139 case QEMU_OPTION_tcpdump:
3140 android_op_tcpdump = (char*)optarg;
3141 break;
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003142
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003143 case QEMU_OPTION_boot_property:
3144 boot_property_parse_option((char*)optarg);
3145 break;
3146
3147 case QEMU_OPTION_lcd_density:
3148 android_op_lcd_density = (char*)optarg;
3149 break;
3150
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003151 case QEMU_OPTION_ui_port:
3152 android_op_ui_port = (char*)optarg;
3153 break;
3154
3155 case QEMU_OPTION_ui_settings:
3156 android_op_ui_settings = (char*)optarg;
3157 break;
3158
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +01003159 case QEMU_OPTION_audio_test_out:
3160 android_audio_test_start_out();
3161 break;
3162
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -08003163 case QEMU_OPTION_android_avdname:
3164 android_op_avd_name = (char*)optarg;
3165 break;
3166
3167 case QEMU_OPTION_timezone:
3168 if (timezone_set((char*)optarg)) {
3169 fprintf(stderr, "emulator: it seems the timezone '%s' is not in zoneinfo format\n",
3170 (char*)optarg);
3171 }
3172 break;
3173
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003174#ifdef CONFIG_MEMCHECK
3175 case QEMU_OPTION_android_memcheck:
3176 android_op_memcheck = (char*)optarg;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003177 /* This will set ro.kernel.memcheck system property
3178 * to memcheck's tracing flags. */
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003179 stralloc_add_format(kernel_config, " memcheck=%s", android_op_memcheck);
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003180 break;
3181#endif // CONFIG_MEMCHECK
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +01003182
3183 case QEMU_OPTION_snapshot_no_time_update:
3184 android_snapshot_update_time = 0;
3185 break;
Vladimir Chtchetkineb8dcaff2011-09-17 11:15:47 -07003186
3187 case QEMU_OPTION_list_webcam:
3188 android_list_web_cameras();
3189 exit(0);
3190
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003191 default:
3192 os_parse_cmd_args(popt->index, optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003193 }
3194 }
3195 }
3196
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003197 /* Initialize character map. */
3198 if (android_charmap_setup(op_charmap_file)) {
3199 if (op_charmap_file) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003200 PANIC(
3201 "Unable to initialize character map from file %s.",
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003202 op_charmap_file);
3203 } else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003204 PANIC(
3205 "Unable to initialize default character map.");
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003206 }
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003207 }
3208
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003209 /* If no data_dir is specified then try to find it relative to the
3210 executable path. */
3211 if (!data_dir) {
3212 data_dir = find_datadir(argv[0]);
3213 }
3214 /* If all else fails use the install patch specified when building. */
3215 if (!data_dir) {
3216 data_dir = CONFIG_QEMU_SHAREDIR;
3217 }
3218
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003219 if (!android_op_hwini) {
3220 PANIC("Missing -android-hw <file> option!");
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003221 }
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003222 hw_ini = iniFile_newFromFile(android_op_hwini);
3223 if (hw_ini == NULL) {
3224 PANIC("Could not find %s file.", android_op_hwini);
3225 }
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01003226
3227 androidHwConfig_init(android_hw, 0);
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003228 androidHwConfig_read(android_hw, hw_ini);
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01003229
Vladimir Chtchetkinedb450d72012-01-12 13:37:40 -08003230 /* If we're loading VM from a snapshot, make sure that the current HW config
3231 * matches the one with which the VM has been saved. */
3232 if (loadvm && *loadvm && !snaphost_match_configs(hw_ini, loadvm)) {
3233 exit(0);
3234 }
3235
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003236 iniFile_free(hw_ini);
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003237
3238 {
3239 int width = android_hw->hw_lcd_width;
3240 int height = android_hw->hw_lcd_height;
3241 int depth = android_hw->hw_lcd_depth;
3242
3243 /* A bit of sanity checking */
3244 if (width <= 0 || height <= 0 ||
3245 (depth != 16 && depth != 32) ||
3246 (((width|height) & 3) != 0) )
3247 {
3248 PANIC("Invalid display configuration (%d,%d,%d)",
3249 width, height, depth);
3250 }
3251 android_display_width = width;
3252 android_display_height = height;
3253 android_display_bpp = depth;
3254 }
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003255
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003256#ifdef CONFIG_NAND_LIMITS
3257 /* Init nand stuff. */
3258 if (android_op_nand_limits) {
3259 parse_nand_limits(android_op_nand_limits);
3260 }
3261#endif // CONFIG_NAND_LIMITS
3262
David 'Digit' Turner53eb66d2011-03-01 14:58:23 +01003263 /* Initialize AVD name from hardware configuration if needed */
3264 if (!android_op_avd_name) {
3265 if (android_hw->avd_name && *android_hw->avd_name) {
3266 android_op_avd_name = android_hw->avd_name;
3267 VERBOSE_PRINT(init,"AVD Name: %s", android_op_avd_name);
3268 }
3269 }
3270
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003271 /* Initialize system partition image */
3272 {
3273 char tmp[PATH_MAX+32];
3274 const char* sysImage = android_hw->disk_systemPartition_path;
3275 const char* initImage = android_hw->disk_systemPartition_initPath;
3276 uint64_t sysBytes = android_hw->disk_systemPartition_size;
3277
3278 if (sysBytes == 0) {
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003279 PANIC("Invalid system partition size: %" PRIu64, sysBytes);
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003280 }
3281
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003282 snprintf(tmp,sizeof(tmp),"system,size=0x%" PRIx64, sysBytes);
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003283
3284 if (sysImage && *sysImage) {
3285 if (filelock_create(sysImage) == NULL) {
3286 fprintf(stderr,"WARNING: System image already in use, changes will not persist!\n");
3287 /* If there is no file= parameters, nand_add_dev will create
3288 * a temporary file to back the partition image. */
3289 } else {
3290 pstrcat(tmp,sizeof(tmp),",file=");
3291 pstrcat(tmp,sizeof(tmp),sysImage);
3292 }
3293 }
3294 if (initImage && *initImage) {
3295 if (!path_exists(initImage)) {
3296 PANIC("Invalid initial system image path: %s", initImage);
3297 }
3298 pstrcat(tmp,sizeof(tmp),",initfile=");
3299 pstrcat(tmp,sizeof(tmp),initImage);
3300 } else {
3301 PANIC("Missing initial system image path!");
3302 }
Deepanshu Gupta04b20f42013-09-11 12:04:27 -07003303 if (android_hw->hw_useext4) {
3304 /* Using a nand device to approximate a block device until full
3305 * support is added */
3306 pstrcat(tmp,sizeof(tmp),",pagesize=512,extrasize=0");
3307 }
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003308 nand_add_dev(tmp);
3309 }
3310
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003311 /* Initialize data partition image */
3312 {
3313 char tmp[PATH_MAX+32];
3314 const char* dataImage = android_hw->disk_dataPartition_path;
3315 const char* initImage = android_hw->disk_dataPartition_initPath;
3316 uint64_t dataBytes = android_hw->disk_dataPartition_size;
3317
3318 if (dataBytes == 0) {
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003319 PANIC("Invalid data partition size: %" PRIu64, dataBytes);
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003320 }
3321
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003322 snprintf(tmp,sizeof(tmp),"userdata,size=0x%" PRIx64, dataBytes);
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003323
3324 if (dataImage && *dataImage) {
3325 if (filelock_create(dataImage) == NULL) {
3326 fprintf(stderr, "WARNING: Data partition already in use. Changes will not persist!\n");
3327 /* Note: if there is no file= parameters, nand_add_dev() will
3328 * create a temporary file to back the partition image. */
3329 } else {
3330 /* Create the file if needed */
3331 if (!path_exists(dataImage)) {
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003332 if (path_empty_file(dataImage) < 0) {
3333 PANIC("Could not create data image file %s: %s", dataImage, strerror(errno));
3334 }
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003335 }
3336 pstrcat(tmp, sizeof(tmp), ",file=");
3337 pstrcat(tmp, sizeof(tmp), dataImage);
3338 }
3339 }
3340 if (initImage && *initImage) {
3341 pstrcat(tmp, sizeof(tmp), ",initfile=");
3342 pstrcat(tmp, sizeof(tmp), initImage);
3343 }
Deepanshu Gupta04b20f42013-09-11 12:04:27 -07003344 if (android_hw->hw_useext4) {
3345 /* Using a nand device to approximate a block device until full
3346 * support is added */
3347 pstrcat(tmp, sizeof(tmp), ",pagesize=512,extrasize=0");
3348 }
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003349 nand_add_dev(tmp);
3350 }
3351
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01003352 /* Init SD-Card stuff. For Android, it is always hda */
3353 /* If the -hda option was used, ignore the Android-provided one */
3354 if (hda_opts == NULL) {
3355 const char* sdPath = android_hw->hw_sdCard_path;
3356 if (sdPath && *sdPath) {
3357 if (!path_exists(sdPath)) {
3358 fprintf(stderr, "WARNING: SD Card image is missing: %s\n", sdPath);
3359 } else if (filelock_create(sdPath) == NULL) {
3360 fprintf(stderr, "WARNING: SD Card image already in use: %s\n", sdPath);
3361 } else {
3362 /* Successful locking */
3363 hda_opts = drive_add(sdPath, HD_ALIAS, 0);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02003364 /* Set this property of any operation involving the SD Card
3365 * will be x100 slower, due to the corresponding file being
3366 * mounted as O_DIRECT. Note that this is only 'unsafe' in
3367 * the context of an emulator crash. The data is already
3368 * synced properly when the emulator exits (either normally or through ^C).
3369 */
3370 qemu_opt_set(hda_opts, "cache", "unsafe");
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01003371 }
3372 }
3373 }
3374
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003375 if (hdb_opts == NULL) {
3376 const char* spath = android_hw->disk_snapStorage_path;
3377 if (spath && *spath) {
3378 if (!path_exists(spath)) {
3379 PANIC("Snapshot storage file does not exist: %s", spath);
3380 }
3381 if (filelock_create(spath) == NULL) {
David 'Digit' Turner4297b822011-05-04 19:18:15 +02003382 PANIC("Snapshot storage already in use: %s", spath);
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003383 }
3384 hdb_opts = drive_add(spath, HD_ALIAS, 1);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02003385 /* See comment above to understand why this is needed. */
David 'Digit' Turner9fb360e2011-05-04 22:01:28 +02003386 qemu_opt_set(hdb_opts, "cache", "unsafe");
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003387 }
3388 }
3389
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003390 /* Set the VM's max heap size, passed as a boot property */
3391 if (android_hw->vm_heapSize > 0) {
3392 char tmp[64];
3393 snprintf(tmp, sizeof(tmp), "%dm", android_hw->vm_heapSize);
3394 boot_property_add("dalvik.vm.heapsize",tmp);
3395 }
3396
Siva Velusamy0faeb3a2013-10-29 14:23:35 -07003397 /* From API 19 and above, the platform provides an explicit property for low memory devices. */
3398 if (android_hw->hw_ramSize <= 512) {
3399 boot_property_add("ro.config.low_ram", "true");
3400 }
3401
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003402 /* Initialize net speed and delays stuff. */
3403 if (android_parse_network_speed(android_op_netspeed) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003404 PANIC("invalid -netspeed parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003405 android_op_netspeed);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003406 }
3407
3408 if ( android_parse_network_latency(android_op_netdelay) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003409 PANIC("invalid -netdelay parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003410 android_op_netdelay);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003411 }
3412
3413 if (android_op_netfast) {
3414 qemu_net_download_speed = 0;
3415 qemu_net_upload_speed = 0;
3416 qemu_net_min_latency = 0;
3417 qemu_net_max_latency = 0;
3418 }
3419
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003420 /* Initialize LCD density */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01003421 if (android_hw->hw_lcd_density) {
3422 long density = android_hw->hw_lcd_density;
3423 if (density <= 0) {
3424 PANIC("Invalid hw.lcd.density value: %ld", density);
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003425 }
3426 hwLcd_setBootProperty(density);
3427 }
3428
Xavier Ducrohetfa0c8e22011-10-04 10:41:26 -07003429 /* Initialize presence of hardware nav button */
3430 boot_property_add("qemu.hw.mainkeys", android_hw->hw_mainKeys ? "1" : "0");
3431
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07003432 /* Initialize TCP dump */
3433 if (android_op_tcpdump) {
3434 if (qemu_tcpdump_start(android_op_tcpdump) < 0) {
3435 fprintf(stdout, "could not start packet capture: %s\n", strerror(errno));
3436 }
3437 }
3438
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003439 /* Initialize modem */
3440 if (android_op_radio) {
3441 CharDriverState* cs = qemu_chr_open("radio", android_op_radio, NULL);
3442 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003443 PANIC("unsupported character device specification: %s\n"
3444 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003445 android_op_radio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003446 }
3447 android_qemud_set_channel( ANDROID_QEMUD_GSM, cs);
3448 } else if (android_hw->hw_gsmModem != 0 ) {
3449 if ( android_qemud_get_channel( ANDROID_QEMUD_GSM, &android_modem_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003450 PANIC("could not initialize qemud 'gsm' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003451 }
3452 }
3453
3454 /* Initialize GPS */
3455 if (android_op_gps) {
3456 CharDriverState* cs = qemu_chr_open("gps", android_op_gps, NULL);
3457 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003458 PANIC("unsupported character device specification: %s\n"
3459 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003460 android_op_gps);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003461 }
3462 android_qemud_set_channel( ANDROID_QEMUD_GPS, cs);
3463 } else if (android_hw->hw_gps != 0) {
3464 if ( android_qemud_get_channel( "gps", &android_gps_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003465 PANIC("could not initialize qemud 'gps' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003466 }
3467 }
3468
3469 /* Initialize audio. */
3470 if (android_op_audio) {
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003471 if ( !audio_check_backend_name( 0, android_op_audio ) ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003472 PANIC("'%s' is not a valid audio output backend. see -help-audio-out",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003473 android_op_audio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003474 }
David 'Digit' Turnerf816a752011-06-23 18:40:11 +02003475 setenv("QEMU_AUDIO_DRV", android_op_audio, 1);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003476 }
3477
David 'Digit' Turner5b481492011-04-11 17:37:32 +02003478 /* Initialize OpenGLES emulation */
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02003479 //android_hw_opengles_init();
David 'Digit' Turner5b481492011-04-11 17:37:32 +02003480
Vladimir Chtchetkineae0d8132011-09-13 10:48:02 -07003481 /* Initialize fake camera */
Vladimir Chtchetkine7485c292012-03-19 11:35:29 -07003482 if (strcmp(android_hw->hw_camera_back, "emulated") &&
3483 strcmp(android_hw->hw_camera_front, "emulated")) {
3484 /* Fake camera is not used for camera emulation. */
3485 boot_property_add("qemu.sf.fake_camera", "none");
Vladimir Chtchetkineae0d8132011-09-13 10:48:02 -07003486 } else {
Vladimir Chtchetkine7485c292012-03-19 11:35:29 -07003487 /* Fake camera is used for at least one camera emulation. */
3488 if (!strcmp(android_hw->hw_camera_back, "emulated") &&
3489 !strcmp(android_hw->hw_camera_front, "emulated")) {
3490 /* Fake camera is used for both, front and back camera emulation. */
3491 boot_property_add("qemu.sf.fake_camera", "both");
3492 } else if (!strcmp(android_hw->hw_camera_back, "emulated")) {
3493 boot_property_add("qemu.sf.fake_camera", "back");
3494 } else {
3495 boot_property_add("qemu.sf.fake_camera", "front");
3496 }
Vladimir Chtchetkineae0d8132011-09-13 10:48:02 -07003497 }
3498
Vladimir Chtchetkine035f8052012-05-18 12:19:32 -07003499 /* Set LCD density (if required by -qemu, and AVD is missing it. */
3500 if (android_op_lcd_density && !android_hw->hw_lcd_density) {
3501 int density;
3502 if (parse_int(android_op_lcd_density, &density) || density <= 0) {
3503 PANIC("-lcd-density : %d", density);
3504 }
3505 hwLcd_setBootProperty(density);
3506 }
3507
Vladimir Chtchetkineb8dcaff2011-09-17 11:15:47 -07003508 /* Initialize camera emulation. */
3509 android_camera_service_init();
3510
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003511 if (android_op_cpu_delay) {
3512 char* end;
3513 long delay = strtol(android_op_cpu_delay, &end, 0);
3514 if (end == NULL || *end || delay < 0 || delay > 1000 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003515 PANIC("option -cpu-delay must be an integer between 0 and 1000" );
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003516 }
3517 if (delay > 0)
3518 delay = (1000-delay);
3519
3520 qemu_cpu_delay = (int) delay;
3521 }
3522
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003523 if (android_op_dns_server) {
3524 char* x = strchr(android_op_dns_server, ',');
3525 dns_count = 0;
3526 if (x == NULL)
3527 {
3528 if ( add_dns_server( android_op_dns_server ) == 0 )
3529 dns_count = 1;
3530 }
3531 else
3532 {
3533 x = android_op_dns_server;
3534 while (*x) {
3535 char* y = strchr(x, ',');
3536
3537 if (y != NULL) {
3538 *y = 0;
3539 y++;
3540 } else {
3541 y = x + strlen(x);
3542 }
3543
3544 if (y > x && add_dns_server( x ) == 0) {
3545 dns_count += 1;
3546 }
3547 x = y;
3548 }
3549 }
3550 if (dns_count == 0)
3551 fprintf( stdout, "### WARNING: will use system default DNS server\n" );
3552 }
3553
3554 if (dns_count == 0)
3555 dns_count = slirp_get_system_dns_servers();
3556 if (dns_count) {
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003557 stralloc_add_format(kernel_config, " ndns=%d", dns_count);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003558 }
3559
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003560#ifdef CONFIG_MEMCHECK
3561 if (android_op_memcheck) {
3562 memcheck_init(android_op_memcheck);
3563 }
3564#endif // CONFIG_MEMCHECK
3565
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003566 /* Initialize cache partition, if any */
3567 if (android_hw->disk_cachePartition != 0) {
3568 char tmp[PATH_MAX+32];
3569 const char* partPath = android_hw->disk_cachePartition_path;
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003570 uint64_t partSize = android_hw->disk_cachePartition_size;
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003571
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003572 snprintf(tmp,sizeof(tmp),"cache,size=0x%" PRIx64, partSize);
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003573
3574 if (partPath && *partPath && strcmp(partPath, "<temp>") != 0) {
3575 if (filelock_create(partPath) == NULL) {
3576 fprintf(stderr, "WARNING: Cache partition already in use. Changes will not persist!\n");
3577 /* Note: if there is no file= parameters, nand_add_dev() will
3578 * create a temporary file to back the partition image. */
3579 } else {
3580 /* Create the file if needed */
3581 if (!path_exists(partPath)) {
3582 if (path_empty_file(partPath) < 0) {
3583 PANIC("Could not create cache image file %s: %s", partPath, strerror(errno));
3584 }
3585 }
3586 pstrcat(tmp, sizeof(tmp), ",file=");
3587 pstrcat(tmp, sizeof(tmp), partPath);
3588 }
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003589 }
Deepanshu Gupta04b20f42013-09-11 12:04:27 -07003590 if (android_hw->hw_useext4) {
3591 /* Using a nand device to approximate a block device until full
3592 * support is added */
3593 pstrcat(tmp, sizeof(tmp), ",pagesize=512,extrasize=0");
3594 }
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003595 nand_add_dev(tmp);
3596 }
3597
Jesse Hall733fffa2012-04-26 11:07:32 -07003598 /* qemu.gles will be read by the OpenGL ES emulation libraries.
3599 * If set to 0, the software GL ES renderer will be used as a fallback.
3600 * If the parameter is undefined, this means the system image runs
3601 * inside an emulator that doesn't support GPU emulation at all.
3602 *
3603 * We always start the GL ES renderer so we can gather stats on the
3604 * underlying GL implementation. If GL ES acceleration is disabled,
3605 * we just shut it down again once we have the strings. */
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003606 {
Jesse Hall733fffa2012-04-26 11:07:32 -07003607 int qemu_gles = 0;
Jesse Hall733fffa2012-04-26 11:07:32 -07003608 if (android_initOpenglesEmulation() == 0 &&
Jesse Hallba5c1f62012-05-08 15:44:35 -07003609 android_startOpenglesRenderer(android_hw->hw_lcd_width, android_hw->hw_lcd_height) == 0)
Jesse Hall733fffa2012-04-26 11:07:32 -07003610 {
3611 android_getOpenglesHardwareStrings(
3612 android_gl_vendor, sizeof(android_gl_vendor),
3613 android_gl_renderer, sizeof(android_gl_renderer),
3614 android_gl_version, sizeof(android_gl_version));
3615 if (android_hw->hw_gpu_enabled) {
3616 qemu_gles = 1;
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003617 } else {
Jesse Hall733fffa2012-04-26 11:07:32 -07003618 android_stopOpenglesRenderer();
3619 qemu_gles = 0;
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003620 }
Jesse Hall733fffa2012-04-26 11:07:32 -07003621 } else {
3622 dwarning("Could not initialize OpenglES emulation, using software renderer.");
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003623 }
Jesse Hall733fffa2012-04-26 11:07:32 -07003624 if (qemu_gles) {
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003625 stralloc_add_str(kernel_params, " qemu.gles=1");
3626 } else {
3627 stralloc_add_str(kernel_params, " qemu.gles=0");
3628 }
3629 }
3630
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003631 /* We always force qemu=1 when running inside QEMU */
3632 stralloc_add_str(kernel_params, " qemu=1");
3633
3634 /* We always initialize the first serial port for the android-kmsg
3635 * character device (used to send kernel messages) */
3636 serial_hds_add_at(0, "android-kmsg");
3637 stralloc_add_str(kernel_params, " console=ttyS0");
3638
3639 /* We always initialize the second serial port for the android-qemud
3640 * character device as well */
3641 serial_hds_add_at(1, "android-qemud");
3642 stralloc_add_str(kernel_params, " android.qemud=ttyS1");
3643
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003644 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3645 os_pidfile_error();
3646 exit(1);
3647 }
3648
David 'Digit' Turner7e9f6c12014-01-22 18:42:40 +01003649 /* Open the logfile at this point, if necessary. We can't open the logfile
3650 * when encountering either of the logging options (-d or -D) because the
3651 * other one may be encountered later on the command line, changing the
3652 * location or level of logging.
3653 */
3654 if (log_mask) {
3655 int mask;
3656 if (log_file) {
3657 qemu_set_log_filename(log_file);
3658 }
3659
3660 mask = qemu_str_to_log_mask(log_mask);
3661 if (!mask) {
3662 qemu_print_log_usage(stdout);
3663 exit(1);
3664 }
3665 qemu_set_log(mask);
3666 }
3667
David 'Digit' Turner36597752011-05-20 01:18:01 +02003668#if defined(CONFIG_KVM)
3669 if (kvm_allowed < 0) {
3670 kvm_allowed = kvm_check_allowed();
3671 }
3672#endif
3673
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003674 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3675 if (smp_cpus > machine->max_cpus) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003676 PANIC("Number of SMP cpus requested (%d), exceeds max cpus "
3677 "supported by machine `%s' (%d)", smp_cpus, machine->name,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003678 machine->max_cpus);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003679 }
3680
3681 if (display_type == DT_NOGRAPHIC) {
3682 if (serial_device_index == 0)
3683 serial_devices[0] = "stdio";
3684 if (parallel_device_index == 0)
3685 parallel_devices[0] = "null";
3686 if (strncmp(monitor_device, "vc", 2) == 0)
3687 monitor_device = "stdio";
3688 }
3689
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003690 if (qemu_init_main_loop()) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003691 PANIC("qemu_init_main_loop failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003692 }
David 'Digit' Turner0b019492011-03-01 14:02:42 +01003693
3694 if (kernel_filename == NULL) {
3695 kernel_filename = android_hw->kernel_path;
3696 }
3697 if (initrd_filename == NULL) {
3698 initrd_filename = android_hw->disk_ramdisk_path;
3699 }
3700
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003701 linux_boot = (kernel_filename != NULL);
3702 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
3703
3704 if (!linux_boot && *kernel_cmdline != '\0') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003705 PANIC("-append only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003706 }
3707
3708 if (!linux_boot && initrd_filename != NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003709 PANIC("-initrd only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003710 }
3711
3712 /* boot to floppy or the default cd if no hard disk defined yet */
3713 if (!boot_devices[0]) {
3714 boot_devices = "cad";
3715 }
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003716 os_set_line_buffering();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003717
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003718 if (init_timer_alarm() < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003719 PANIC("could not initialize alarm timer");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003720 }
David Turner6a9ef172010-09-09 22:54:36 +02003721 configure_icount(icount_option);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003722
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003723 /* init network clients */
3724 if (nb_net_clients == 0) {
3725 /* if no clients, we use a default config */
3726 net_clients[nb_net_clients++] = "nic";
3727#ifdef CONFIG_SLIRP
3728 net_clients[nb_net_clients++] = "user";
3729#endif
3730 }
3731
3732 for(i = 0;i < nb_net_clients; i++) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003733 if (net_client_parse(net_clients[i]) < 0) {
3734 PANIC("Unable to parse net clients");
3735 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003736 }
3737 net_client_check();
3738
3739#ifdef TARGET_I386
3740 /* XXX: this should be moved in the PC machine instantiation code */
3741 if (net_boot != 0) {
3742 int netroms = 0;
3743 for (i = 0; i < nb_nics && i < 4; i++) {
3744 const char *model = nd_table[i].model;
3745 char buf[1024];
3746 char *filename;
3747 if (net_boot & (1 << i)) {
3748 if (model == NULL)
3749 model = "ne2k_pci";
3750 snprintf(buf, sizeof(buf), "pxe-%s.bin", model);
3751 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, buf);
3752 if (filename && get_image_size(filename) > 0) {
3753 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003754 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003755 }
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01003756 option_rom[nb_option_roms] = g_strdup(buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003757 nb_option_roms++;
3758 netroms++;
3759 }
3760 if (filename) {
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01003761 g_free(filename);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003762 }
3763 }
3764 }
3765 if (netroms == 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003766 PANIC("No valid PXE rom found for network device");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003767 }
3768 }
3769#endif
3770
3771 /* init the bluetooth world */
3772 for (i = 0; i < nb_bt_opts; i++)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003773 if (bt_parse(bt_opts[i])) {
3774 PANIC("Unable to parse bluetooth options");
3775 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003776
3777 /* init the memory */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01003778 if (ram_size == 0) {
3779 ram_size = android_hw->hw_ramSize * 1024LL * 1024;
3780 if (ram_size == 0) {
3781 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3782 }
3783 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003784
Vladimir Chtchetkinecf289fb2012-05-14 08:04:29 -07003785 /* Quite often (especially on older XP machines) attempts to allocate large
3786 * VM RAM is going to fail, and crash the emulator. Since it's failing deep
3787 * inside QEMU, it's not really possible to provide the user with a
3788 * meaningful explanation for the crash. So, lets see if QEMU is going to be
3789 * able to allocate requested amount of RAM, and if not, lets try to come up
3790 * with a recomendation. */
3791 {
3792 ram_addr_t r_ram = ram_size;
3793 void* alloc_check = malloc(r_ram);
3794 while (alloc_check == NULL && r_ram > 1024 * 1024) {
3795 /* Make it 25% less */
3796 r_ram -= r_ram / 4;
3797 alloc_check = malloc(r_ram);
3798 }
3799 if (alloc_check != NULL) {
3800 free(alloc_check);
3801 }
3802 if (r_ram != ram_size) {
3803 /* Requested RAM is too large. Report this, as well as calculated
3804 * recomendation. */
3805 dwarning("Requested RAM size of %dMB is too large for your environment, and is reduced to %dMB.",
3806 (int)(ram_size / 1024 / 1024), (int)(r_ram / 1024 / 1024));
3807 ram_size = r_ram;
3808 }
3809 }
3810
rich canningsd952f282011-03-01 15:40:09 -08003811#ifndef _WIN32
David 'Digit' Turnerf9077a82014-02-10 23:10:47 +01003812 qemu_log_rotation_init();
rich canningsd952f282011-03-01 15:40:09 -08003813#endif
3814
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003815 /* init the dynamic translator */
3816 cpu_exec_init_all(tb_size * 1024 * 1024);
3817
3818 bdrv_init();
3819
3820 /* we always create the cdrom drive, even if no disk is there */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003821#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003822 if (nb_drives_opt < MAX_DRIVES)
3823 drive_add(NULL, CDROM_ALIAS);
3824
3825 /* we always create at least one floppy */
3826
3827 if (nb_drives_opt < MAX_DRIVES)
3828 drive_add(NULL, FD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003829 /* we always create one sd slot, even if no card is in it */
3830
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003831 if (1) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003832 drive_add(NULL, SD_ALIAS);
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003833 }
3834#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003835
3836 /* open the virtual block devices */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003837 if (snapshot)
3838 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3839 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3840 exit(1);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003841
David Turner6a9ef172010-09-09 22:54:36 +02003842 //register_savevm("timer", 0, 2, timer_save, timer_load, &timers_state);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003843 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
3844
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003845 /* must be after terminal init, SDL library changes signal handlers */
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003846 os_setup_signal_handling();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003847
3848 /* Maintain compatibility with multiple stdio monitors */
3849 if (!strcmp(monitor_device,"stdio")) {
3850 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
3851 const char *devname = serial_devices[i];
3852 if (devname && !strcmp(devname,"mon:stdio")) {
3853 monitor_device = NULL;
3854 break;
3855 } else if (devname && !strcmp(devname,"stdio")) {
3856 monitor_device = NULL;
3857 serial_devices[i] = "mon:stdio";
3858 break;
3859 }
3860 }
3861 }
3862
3863 if (nb_numa_nodes > 0) {
3864 int i;
3865
3866 if (nb_numa_nodes > smp_cpus) {
3867 nb_numa_nodes = smp_cpus;
3868 }
3869
3870 /* If no memory size if given for any node, assume the default case
3871 * and distribute the available memory equally across all nodes
3872 */
3873 for (i = 0; i < nb_numa_nodes; i++) {
3874 if (node_mem[i] != 0)
3875 break;
3876 }
3877 if (i == nb_numa_nodes) {
3878 uint64_t usedmem = 0;
3879
3880 /* On Linux, the each node's border has to be 8MB aligned,
3881 * the final node gets the rest.
3882 */
3883 for (i = 0; i < nb_numa_nodes - 1; i++) {
3884 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3885 usedmem += node_mem[i];
3886 }
3887 node_mem[i] = ram_size - usedmem;
3888 }
3889
3890 for (i = 0; i < nb_numa_nodes; i++) {
3891 if (node_cpumask[i] != 0)
3892 break;
3893 }
3894 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3895 * must cope with this anyway, because there are BIOSes out there in
3896 * real machines which also use this scheme.
3897 */
3898 if (i == nb_numa_nodes) {
3899 for (i = 0; i < smp_cpus; i++) {
3900 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3901 }
3902 }
3903 }
3904
3905 if (kvm_enabled()) {
3906 int ret;
3907
3908 ret = kvm_init(smp_cpus);
3909 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003910 PANIC("failed to initialize KVM");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003911 }
3912 }
3913
Jun Nakajimaa381ef02011-12-17 19:13:25 -08003914#ifdef CONFIG_HAX
3915 if (!hax_disabled)
3916 {
3917 int ret;
3918
Jiang, Yunhong4a5a0ef2012-02-23 06:31:12 +08003919 hax_set_ramsize(ram_size);
Jun Nakajimaa381ef02011-12-17 19:13:25 -08003920 ret = hax_init(smp_cpus);
3921 fprintf(stderr, "HAX is %s and emulator runs in %s mode\n",
3922 !ret ? "working" :"not working", !ret ? "fast virt" : "emulation");
3923 }
3924#endif
3925
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003926 if (monitor_device) {
3927 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
3928 if (!monitor_hd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003929 PANIC("qemu: could not open monitor device '%s'",
3930 monitor_device);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003931 }
3932 }
3933
3934 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003935 serial_hds_add(serial_devices[i]);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003936 }
3937
3938 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
3939 const char *devname = parallel_devices[i];
3940 if (devname && strcmp(devname, "none")) {
3941 char label[32];
3942 snprintf(label, sizeof(label), "parallel%d", i);
3943 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
3944 if (!parallel_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003945 PANIC("qemu: could not open parallel device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003946 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003947 }
3948 }
3949 }
3950
3951 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
3952 const char *devname = virtio_consoles[i];
3953 if (devname && strcmp(devname, "none")) {
3954 char label[32];
3955 snprintf(label, sizeof(label), "virtcon%d", i);
3956 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
3957 if (!virtcon_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003958 PANIC("qemu: could not open virtio console '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003959 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003960 }
3961 }
3962 }
3963
3964 module_call_init(MODULE_INIT_DEVICE);
3965
3966
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01003967 /* Check the CPU Architecture value */
3968#if defined(TARGET_ARM)
3969 if (strcmp(android_hw->hw_cpu_arch,"arm") != 0) {
3970 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'arm'\n",
3971 android_hw->hw_cpu_arch);
3972 exit(1);
3973 }
Vladimir Chtchetkine33f89d02011-09-28 09:19:09 -07003974#elif defined(TARGET_I386)
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01003975 if (strcmp(android_hw->hw_cpu_arch,"x86") != 0) {
3976 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'x86'\n",
3977 android_hw->hw_cpu_arch);
3978 exit(1);
3979 }
3980#endif
3981
3982 /* Grab CPU model if provided in hardware.ini */
3983 if ( !cpu_model
3984 && android_hw->hw_cpu_model
3985 && android_hw->hw_cpu_model[0] != '\0')
3986 {
3987 cpu_model = android_hw->hw_cpu_model;
3988 }
3989
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003990 /* Combine kernel command line passed from the UI with parameters
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003991 * collected during initialization.
3992 *
3993 * The order is the following:
3994 * - parameters from the hw configuration (kernel.parameters)
3995 * - additionnal parameters from options (e.g. -memcheck)
3996 * - the -append parameters.
3997 */
3998 {
3999 const char* kernel_parameters;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004000
David 'Digit' Turner0b019492011-03-01 14:02:42 +01004001 if (android_hw->kernel_parameters) {
David 'Digit' Turner62c7ae52011-03-08 15:46:53 +01004002 stralloc_add_c(kernel_params, ' ');
David 'Digit' Turner0b019492011-03-01 14:02:42 +01004003 stralloc_add_str(kernel_params, android_hw->kernel_parameters);
4004 }
4005
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004006 /* If not empty, kernel_config always contains a leading space */
4007 stralloc_append(kernel_params, kernel_config);
4008
4009 if (*kernel_cmdline) {
4010 stralloc_add_c(kernel_params, ' ');
4011 stralloc_add_str(kernel_params, kernel_cmdline);
4012 }
4013
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01004014 /* Remove any leading/trailing spaces */
4015 stralloc_strip(kernel_params);
4016
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004017 kernel_parameters = stralloc_cstr(kernel_params);
4018 VERBOSE_PRINT(init, "Kernel parameters: %s", kernel_parameters);
4019
4020 machine->init(ram_size,
4021 boot_devices,
4022 kernel_filename,
4023 kernel_parameters,
4024 initrd_filename,
4025 cpu_model);
4026
Vladimir Chtchetkine8dd31e82012-02-15 17:16:04 -08004027 /* Initialize multi-touch emulation. */
Vladimir Chtchetkine863d1012012-03-16 12:25:23 -07004028 if (androidHwConfig_isScreenMultiTouch(android_hw)) {
Vladimir Chtchetkine8dd31e82012-02-15 17:16:04 -08004029 mts_port_create(NULL);
4030 }
4031
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004032 stralloc_reset(kernel_params);
4033 stralloc_reset(kernel_config);
4034 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004035
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004036 for (env = first_cpu; env != NULL; env = env->next_cpu) {
4037 for (i = 0; i < nb_numa_nodes; i++) {
4038 if (node_cpumask[i] & (1 << env->cpu_index)) {
4039 env->numa_node = i;
4040 }
4041 }
4042 }
4043
4044 current_machine = machine;
4045
David 'Digit' Turnere2678e12014-01-16 15:56:43 +01004046 /* Set KVM's vcpu state to qemu's initial CPUOldState. */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004047 if (kvm_enabled()) {
4048 int ret;
4049
4050 ret = kvm_sync_vcpus();
4051 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004052 PANIC("failed to initialize vcpus");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004053 }
4054 }
4055
Jun Nakajimaa381ef02011-12-17 19:13:25 -08004056#ifdef CONFIG_HAX
4057 if (hax_enabled())
4058 hax_sync_vcpus();
4059#endif
4060
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004061 /* init USB devices */
4062 if (usb_enabled) {
4063 for(i = 0; i < usb_devices_index; i++) {
4064 if (usb_device_add(usb_devices[i], 0) < 0) {
4065 fprintf(stderr, "Warning: could not add USB device %s\n",
4066 usb_devices[i]);
4067 }
4068 }
4069 }
4070
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08004071 /* just use the first displaystate for the moment */
David 'Digit' Turner94702b02011-01-20 02:46:33 +01004072 ds = get_displaystate();
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08004073
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004074 /* Initialize display from the command line parameters. */
4075 android_display_reset(ds,
4076 android_display_width,
4077 android_display_height,
4078 android_display_bpp);
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07004079
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004080 if (display_type == DT_DEFAULT) {
4081#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4082 display_type = DT_SDL;
4083#else
4084 display_type = DT_VNC;
4085 vnc_display = "localhost:0,to=99";
4086 show_vnc_port = 1;
4087#endif
4088 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004089
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004090
4091 switch (display_type) {
4092 case DT_NOGRAPHIC:
4093 break;
4094#if defined(CONFIG_CURSES)
4095 case DT_CURSES:
4096 curses_display_init(ds, full_screen);
4097 break;
4098#endif
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07004099#if defined(CONFIG_SDL) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004100 case DT_SDL:
4101 sdl_display_init(ds, full_screen, no_frame);
4102 break;
4103#elif defined(CONFIG_COCOA)
4104 case DT_SDL:
4105 cocoa_display_init(ds, full_screen);
4106 break;
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08004107#elif defined(CONFIG_STANDALONE_CORE)
4108 case DT_SDL:
Vladimir Chtchetkinee95660a2010-12-20 08:28:03 -08004109 coredisplay_init(ds);
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08004110 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004111#endif
4112 case DT_VNC:
4113 vnc_display_init(ds);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004114 if (vnc_display_open(ds, vnc_display) < 0) {
4115 PANIC("Unable to initialize VNC display");
4116 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004117
4118 if (show_vnc_port) {
4119 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
4120 }
4121 break;
4122 default:
4123 break;
4124 }
4125 dpy_resize(ds);
4126
4127 dcl = ds->listeners;
4128 while (dcl != NULL) {
4129 if (dcl->dpy_refresh != NULL) {
David 'Digit' Turnerdcda9492014-02-16 15:13:55 +01004130 ds->gui_timer = timer_new(QEMU_CLOCK_REALTIME, SCALE_MS, gui_update, ds);
4131 timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004132 }
4133 dcl = dcl->next;
4134 }
4135
4136 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
David 'Digit' Turnerdcda9492014-02-16 15:13:55 +01004137 nographic_timer = timer_new(QEMU_CLOCK_REALTIME, SCALE_MS, nographic_update, NULL);
4138 timer_mod(nographic_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004139 }
4140
David 'Digit' Turner94702b02011-01-20 02:46:33 +01004141 text_consoles_set_display(ds);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004142 qemu_chr_initial_reset();
4143
4144 if (monitor_device && monitor_hd)
4145 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
4146
4147 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
4148 const char *devname = serial_devices[i];
4149 if (devname && strcmp(devname, "none")) {
4150 if (strstart(devname, "vc", 0))
4151 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
4152 }
4153 }
4154
4155 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
4156 const char *devname = parallel_devices[i];
4157 if (devname && strcmp(devname, "none")) {
4158 if (strstart(devname, "vc", 0))
4159 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
4160 }
4161 }
4162
4163 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
4164 const char *devname = virtio_consoles[i];
4165 if (virtcon_hds[i] && devname) {
4166 if (strstart(devname, "vc", 0))
4167 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
4168 }
4169 }
4170
4171 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004172 PANIC("qemu: could not open gdbserver on device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004173 gdbstub_dev);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004174 }
4175
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004176 /* call android-specific setup function */
4177 android_emulation_setup();
4178
Vladimir Chtchetkine57584042011-01-20 16:15:30 -08004179#if !defined(CONFIG_STANDALONE_CORE)
4180 // For the standalone emulator (UI+core in one executable) we need to
4181 // set the window title here.
4182 android_emulator_set_base_port(android_base_port);
4183#endif
4184
Ot ten Thije871da2a2010-09-20 10:29:22 +01004185 if (loadvm)
4186 do_loadvm(cur_mon, loadvm);
4187
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004188 if (incoming) {
4189 autostart = 0; /* fixme how to deal with -daemonize */
4190 qemu_start_incoming_migration(incoming);
4191 }
4192
4193 if (autostart)
4194 vm_start();
4195
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02004196 os_setup_post();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004197
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004198#ifdef CONFIG_ANDROID
4199 // This will notify the UI that the core is successfuly initialized
4200 android_core_init_completed();
4201#endif // CONFIG_ANDROID
4202
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004203 main_loop();
4204 quit_timers();
4205 net_cleanup();
4206 android_emulation_teardown();
4207 return 0;
4208}
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07004209
4210void
4211android_emulation_teardown(void)
4212{
4213 android_charmap_done();
4214}