blob: 3c2191c0db150243f474dc8ad4474b13939dd290 [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"
David 'Digit' Turner890f4642014-02-24 11:53:03 +010056#include "android/filesystems/ext4_utils.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070057#include "android/gps.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070058#include "android/hw-kmsg.h"
David 'Digit' Turnerc0ac7332011-05-02 15:05:35 +020059#include "android/hw-pipe-net.h"
60#include "android/hw-qemud.h"
Vladimir Chtchetkinec646f5e2011-09-03 15:17:13 -070061#include "android/camera/camera-service.h"
Vladimir Chtchetkine8dd31e82012-02-15 17:16:04 -080062#include "android/multitouch-port.h"
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -070063#include "android/charmap.h"
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -070064#include "android/globals.h"
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -070065#include "android/utils/bufprint.h"
David 'Digit' Turner5f824112011-03-01 14:00:26 +010066#include "android/utils/debug.h"
David 'Digit' Turner48a3c662011-03-01 14:03:20 +010067#include "android/utils/filelock.h"
68#include "android/utils/path.h"
David 'Digit' Turner5f824112011-03-01 14:00:26 +010069#include "android/utils/stralloc.h"
David 'Digit' Turner40841b22011-03-01 14:04:00 +010070#include "android/utils/tempfile.h"
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -080071#include "android/display-core.h"
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -080072#include "android/utils/timezone.h"
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +010073#include "android/snapshot.h"
David 'Digit' Turnercb88e792011-08-26 01:35:14 +020074#include "android/opengles.h"
Vladimir Chtchetkine1a820e92012-04-11 13:22:48 -070075#include "android/multitouch-screen.h"
David 'Digit' Turnerbcde1092014-01-09 23:19:19 +010076#include "exec/hwaddr.h"
David 'Digit' Turner6e2eb782013-12-15 00:54:21 +010077#include "android/tcpdump.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070078
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -070079#ifdef CONFIG_MEMCHECK
80#include "memcheck/memcheck.h"
81#endif // CONFIG_MEMCHECK
82
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070083#include <unistd.h>
84#include <fcntl.h>
85#include <signal.h>
86#include <time.h>
87#include <errno.h>
88#include <sys/time.h>
89#include <zlib.h>
90
David 'Digit' Turner2c538c82010-05-10 16:48:20 -070091/* Needed early for CONFIG_BSD etc. */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070092#include "config-host.h"
93
94#ifndef _WIN32
95#include <libgen.h>
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070096#include <sys/times.h>
97#include <sys/wait.h>
98#include <termios.h>
99#include <sys/mman.h>
100#include <sys/ioctl.h>
101#include <sys/resource.h>
102#include <sys/socket.h>
103#include <netinet/in.h>
104#include <net/if.h>
105#if defined(__NetBSD__)
106#include <net/if_tap.h>
107#endif
108#ifdef __linux__
109#include <linux/if_tun.h>
110#endif
111#include <arpa/inet.h>
112#include <dirent.h>
113#include <netdb.h>
114#include <sys/select.h>
David 'Digit' Turner2c538c82010-05-10 16:48:20 -0700115#ifdef CONFIG_BSD
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700116#include <sys/stat.h>
117#if defined(__FreeBSD__) || defined(__DragonFly__)
118#include <libutil.h>
119#else
120#include <util.h>
121#endif
122#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
123#include <freebsd/stdlib.h>
124#else
125#ifdef __linux__
126#include <pty.h>
127#include <malloc.h>
128#include <linux/rtc.h>
129
130/* For the benefit of older linux systems which don't supply it,
131 we use a local copy of hpet.h. */
132/* #include <linux/hpet.h> */
David 'Digit' Turner2dbdad52013-12-17 09:24:18 +0100133#include "hw/timer/hpet.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700134
135#include <linux/ppdev.h>
136#include <linux/parport.h>
137#endif
138#ifdef __sun__
139#include <sys/stat.h>
140#include <sys/ethernet.h>
141#include <sys/sockio.h>
142#include <netinet/arp.h>
143#include <netinet/in.h>
144#include <netinet/in_systm.h>
145#include <netinet/ip.h>
146#include <netinet/ip_icmp.h> // must come after ip.h
147#include <netinet/udp.h>
148#include <netinet/tcp.h>
149#include <net/if.h>
150#include <syslog.h>
151#include <stropts.h>
152#endif
153#endif
154#endif
155
156#if defined(__OpenBSD__)
157#include <util.h>
158#endif
159
160#if defined(CONFIG_VDE)
161#include <libvdeplug.h>
162#endif
163
164#ifdef _WIN32
165#include <windows.h>
166#include <malloc.h>
167#include <sys/timeb.h>
168#include <mmsystem.h>
169#define getopt_long_only getopt_long
170#define memalign(align, size) malloc(size)
171#endif
172
David 'Digit' Turner11822842013-12-17 09:16:47 +0100173#include "sysemu/cpus.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100174#include "sysemu/arch_init.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700175
176#ifdef CONFIG_COCOA
David 'Digit' Turnerae3098a2011-05-11 16:01:57 +0200177int qemu_main(int argc, char **argv, char **envp);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700178#undef main
179#define main qemu_main
180#endif /* CONFIG_COCOA */
181
182#include "hw/hw.h"
183#include "hw/boards.h"
184#include "hw/usb.h"
185#include "hw/pcmcia.h"
David 'Digit' Turner2ec695a2013-12-17 10:03:39 +0100186#include "hw/i386/pc.h"
187#include "hw/isa/isa.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700188#include "hw/baum.h"
189#include "hw/bt.h"
David 'Digit' Turner7821d632013-12-17 11:27:50 +0100190#include "sysemu/watchdog.h"
David 'Digit' Turner73a93a12013-12-17 11:26:01 +0100191#include "hw/i386/smbios.h"
David 'Digit' Turner50319182013-12-17 11:28:54 +0100192#include "hw/xen/xen.h"
David 'Digit' Turner6800f0a2013-12-17 09:12:54 +0100193#include "sysemu/bt.h"
David 'Digit' Turnercc330d42013-12-14 23:26:42 +0100194#include "net/net.h"
David 'Digit' Turner6af67652013-12-14 23:49:32 +0100195#include "monitor/monitor.h"
David 'Digit' Turner1c31e3e2013-12-14 20:07:17 +0100196#include "ui/console.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100197#include "sysemu/sysemu.h"
David 'Digit' Turner852088c2013-12-14 23:04:12 +0100198#include "exec/gdbstub.h"
David 'Digit' Turner7a78db72013-12-14 11:46:01 +0100199#include "qemu/timer.h"
David 'Digit' Turnere7216d82013-12-15 00:51:13 +0100200#include "sysemu/char.h"
David 'Digit' Turner37dc41a2013-12-14 14:45:51 +0100201#include "qemu/cache-utils.h"
David 'Digit' Turnere1e03df2013-12-15 00:42:21 +0100202#include "block/block.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100203#include "sysemu/dma.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700204#include "audio/audio.h"
David 'Digit' Turner28a09b62013-12-15 00:16:00 +0100205#include "migration/migration.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100206#include "sysemu/kvm.h"
David 'Digit' Turnere1e03df2013-12-15 00:42:21 +0100207#include "exec/hax.h"
David 'Digit' Turner36597752011-05-20 01:18:01 +0200208#ifdef CONFIG_KVM
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100209#include "android/kvm.h"
David 'Digit' Turner36597752011-05-20 01:18:01 +0200210#endif
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100211#include "sysemu/balloon.h"
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700212#include "android/hw-lcd.h"
213#include "android/boot-properties.h"
David 'Digit' Turnerca950592011-04-27 12:26:15 +0200214#include "android/hw-control.h"
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700215#include "android/core-init-utils.h"
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +0100216#include "android/audio-test.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700217
Vladimir Chtchetkinedb450d72012-01-12 13:37:40 -0800218#include "android/snaphost-android.h"
219
Vladimir Chtchetkine57584042011-01-20 16:15:30 -0800220#if !defined(CONFIG_STANDALONE_CORE)
221/* in android/qemulator.c */
222extern void android_emulator_set_base_port(int port);
223#endif
224
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700225#if defined(CONFIG_SKINS) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700226#undef main
227#define main qemu_main
228#endif
229
David 'Digit' Turnercc33b2d2013-12-15 00:09:42 +0100230#include "disas/disas.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700231
David 'Digit' Turnerd0edecb2013-12-17 09:32:26 +0100232#include "qemu/sockets.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700233
234#if defined(CONFIG_SLIRP)
235#include "libslirp.h"
236#endif
237
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700238#define DEFAULT_RAM_SIZE 128
239
240/* Max number of USB devices that can be specified on the commandline. */
241#define MAX_USB_CMDLINE 8
242
243/* Max number of bluetooth switches on the commandline. */
244#define MAX_BT_CMDLINE 10
245
246/* XXX: use a two level table to limit memory usage */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700247
248static const char *data_dir;
249const char *bios_name = NULL;
250static void *ioport_opaque[MAX_IOPORTS];
251static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
252static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100253#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700254/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
255 to store the VM snapshots */
256DriveInfo drives_table[MAX_DRIVES+1];
257int nb_drives;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100258#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700259enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700260DisplayType display_type = DT_DEFAULT;
261const char* keyboard_layout = NULL;
262int64_t ticks_per_sec;
263ram_addr_t ram_size;
David 'Digit' Turner0e8e7482014-02-17 20:25:52 +0100264bool xen_allowed;
David 'Digit' Turner280afa02011-05-11 17:37:44 +0200265const char *mem_path = NULL;
266#ifdef MAP_POPULATE
267int mem_prealloc = 0; /* force preallocation of physical target memory */
268#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700269int nb_nics;
270NICInfo nd_table[MAX_NICS];
271int vm_running;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100272int autostart;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700273static int rtc_utc = 1;
274static int rtc_date_offset = -1; /* -1 means no change */
275int cirrus_vga_enabled = 1;
276int std_vga_enabled = 0;
277int vmsvga_enabled = 0;
278int xenfb_enabled = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700279static int full_screen = 0;
280#ifdef CONFIG_SDL
281static int no_frame = 0;
282#endif
283int no_quit = 0;
284CharDriverState *serial_hds[MAX_SERIAL_PORTS];
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +0100285int serial_hds_count;
286
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700287CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
288CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
289#ifdef TARGET_I386
290int win2k_install_hack = 0;
291int rtc_td_hack = 0;
292#endif
293int usb_enabled = 0;
294int singlestep = 0;
295int smp_cpus = 1;
296const char *vnc_display;
297int acpi_enabled = 1;
298int no_hpet = 0;
Jun Nakajimaa381ef02011-12-17 19:13:25 -0800299int hax_disabled = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700300int no_virtio_balloon = 0;
301int fd_bootchk = 1;
302int no_reboot = 0;
303int no_shutdown = 0;
304int cursor_hide = 1;
305int graphic_rotate = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700306WatchdogTimerModel *watchdog = NULL;
307int watchdog_action = WDT_RESET;
308const char *option_rom[MAX_OPTION_ROMS];
309int nb_option_roms;
310int semihosting_enabled = 0;
311#ifdef TARGET_ARM
312int old_param = 0;
313#endif
314const char *qemu_name;
315int alt_grab = 0;
316#if defined(TARGET_SPARC) || defined(TARGET_PPC)
317unsigned int nb_prom_envs = 0;
318const char *prom_envs[MAX_PROM_ENVS];
319#endif
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100320#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700321int nb_drives_opt;
322struct drive_opt drives_opt[MAX_DRIVES];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100323#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700324int nb_numa_nodes;
325uint64_t node_mem[MAX_NODES];
326uint64_t node_cpumask[MAX_NODES];
327
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700328static QEMUTimer *nographic_timer;
329
330uint8_t qemu_uuid[16];
331
332
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700333int qemu_cpu_delay;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700334extern char* audio_input_source;
335
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700336extern char* android_op_ports;
337extern char* android_op_port;
338extern char* android_op_report_console;
339extern char* op_http_proxy;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700340// Path to the file containing specific key character map.
341char* op_charmap_file = NULL;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700342
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -0700343/* Path to hardware initialization file passed with -android-hw option. */
344char* android_op_hwini = NULL;
345
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -0700346/* Memory checker options. */
347char* android_op_memcheck = NULL;
348
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -0700349/* -dns-server option value. */
350char* android_op_dns_server = NULL;
351
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -0700352/* -radio option value. */
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700353char* android_op_radio = NULL;
354
355/* -gps option value. */
356char* android_op_gps = NULL;
357
358/* -audio option value. */
359char* android_op_audio = NULL;
360
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700361/* -cpu-delay option value. */
362char* android_op_cpu_delay = NULL;
363
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -0700364#ifdef CONFIG_NAND_LIMITS
365/* -nand-limits option value. */
366char* android_op_nand_limits = NULL;
367#endif // CONFIG_NAND_LIMITS
368
369/* -netspeed option value. */
370char* android_op_netspeed = NULL;
371
372/* -netdelay option value. */
373char* android_op_netdelay = NULL;
374
375/* -netfast option value. */
376int android_op_netfast = 0;
377
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -0700378/* -tcpdump option value. */
379char* android_op_tcpdump = NULL;
380
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700381/* -lcd-density option value. */
382char* android_op_lcd_density = NULL;
383
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700384/* -ui-port option value. This port will be used to report the core
385 * initialization completion.
386 */
387char* android_op_ui_port = NULL;
388
389/* -ui-settings option value. This value will be passed to the UI when new UI
390 * process is attaching to the core.
391 */
392char* android_op_ui_settings = NULL;
393
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -0800394/* -android-avdname option value. */
395char* android_op_avd_name = "unknown";
396
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -0700397extern int android_display_width;
398extern int android_display_height;
399extern int android_display_bpp;
400
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700401extern void dprint( const char* format, ... );
402
rich canningsd952f282011-03-01 15:40:09 -0800403const char* dns_log_filename = NULL;
404const char* drop_log_filename = NULL;
rich canningsd952f282011-03-01 15:40:09 -0800405
Tim Baverstock24204cc2010-11-25 11:37:43 +0000406const char* savevm_on_exit = NULL;
Tim Baverstock24204cc2010-11-25 11:37:43 +0000407
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700408#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
409
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700410/* Reports the core initialization failure to the error stdout and to the UI
411 * socket before exiting the application.
412 * Parameters that are passed to this macro are used to format the error
413 * mesage using sprintf routine.
414 */
415#ifdef CONFIG_ANDROID
416#define PANIC(...) android_core_init_failure(__VA_ARGS__)
417#else
418#define PANIC(...) do { fprintf(stderr, __VA_ARGS__); \
419 exit(1); \
420 } while (0)
421#endif // CONFIG_ANDROID
422
423/* Exits the core during initialization. */
424#ifdef CONFIG_ANDROID
425#define QEMU_EXIT(exit_code) android_core_init_exit(exit_code)
426#else
427#define QEMU_EXIT(exit_code) exit(exit_code)
428#endif // CONFIG_ANDROID
429
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700430/***********************************************************/
431/* x86 ISA bus support */
432
David 'Digit' Turnerbcde1092014-01-09 23:19:19 +0100433hwaddr isa_mem_base = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700434PicState2 *isa_pic;
435
436static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
437static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
438
439static uint32_t ioport_read(int index, uint32_t address)
440{
441 static IOPortReadFunc *default_func[3] = {
442 default_ioport_readb,
443 default_ioport_readw,
444 default_ioport_readl
445 };
446 IOPortReadFunc *func = ioport_read_table[index][address];
447 if (!func)
448 func = default_func[index];
449 return func(ioport_opaque[address], address);
450}
451
452static void ioport_write(int index, uint32_t address, uint32_t data)
453{
454 static IOPortWriteFunc *default_func[3] = {
455 default_ioport_writeb,
456 default_ioport_writew,
457 default_ioport_writel
458 };
459 IOPortWriteFunc *func = ioport_write_table[index][address];
460 if (!func)
461 func = default_func[index];
462 func(ioport_opaque[address], address, data);
463}
464
465static uint32_t default_ioport_readb(void *opaque, uint32_t address)
466{
467#ifdef DEBUG_UNUSED_IOPORT
468 fprintf(stderr, "unused inb: port=0x%04x\n", address);
469#endif
470 return 0xff;
471}
472
473static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
474{
475#ifdef DEBUG_UNUSED_IOPORT
476 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
477#endif
478}
479
480/* default is to make two byte accesses */
481static uint32_t default_ioport_readw(void *opaque, uint32_t address)
482{
483 uint32_t data;
484 data = ioport_read(0, address);
485 address = (address + 1) & (MAX_IOPORTS - 1);
486 data |= ioport_read(0, address) << 8;
487 return data;
488}
489
490static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
491{
492 ioport_write(0, address, data & 0xff);
493 address = (address + 1) & (MAX_IOPORTS - 1);
494 ioport_write(0, address, (data >> 8) & 0xff);
495}
496
497static uint32_t default_ioport_readl(void *opaque, uint32_t address)
498{
499#ifdef DEBUG_UNUSED_IOPORT
500 fprintf(stderr, "unused inl: port=0x%04x\n", address);
501#endif
502 return 0xffffffff;
503}
504
505static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
506{
507#ifdef DEBUG_UNUSED_IOPORT
508 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
509#endif
510}
511
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700512/***************/
513/* ballooning */
514
515static QEMUBalloonEvent *qemu_balloon_event;
516void *qemu_balloon_event_opaque;
517
518void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
519{
520 qemu_balloon_event = func;
521 qemu_balloon_event_opaque = opaque;
522}
523
524void qemu_balloon(ram_addr_t target)
525{
526 if (qemu_balloon_event)
527 qemu_balloon_event(qemu_balloon_event_opaque, target);
528}
529
530ram_addr_t qemu_balloon_status(void)
531{
532 if (qemu_balloon_event)
533 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
534 return 0;
535}
536
537/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700538/* host time/date access */
539void qemu_get_timedate(struct tm *tm, int offset)
540{
541 time_t ti;
542 struct tm *ret;
543
544 time(&ti);
545 ti += offset;
546 if (rtc_date_offset == -1) {
547 if (rtc_utc)
548 ret = gmtime(&ti);
549 else
550 ret = localtime(&ti);
551 } else {
552 ti -= rtc_date_offset;
553 ret = gmtime(&ti);
554 }
555
556 memcpy(tm, ret, sizeof(struct tm));
557}
558
559int qemu_timedate_diff(struct tm *tm)
560{
561 time_t seconds;
562
563 if (rtc_date_offset == -1)
564 if (rtc_utc)
565 seconds = mktimegm(tm);
566 else
567 seconds = mktime(tm);
568 else
569 seconds = mktimegm(tm) + rtc_date_offset;
570
571 return seconds - time(NULL);
572}
573
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700574/***********************************************************/
575/* Bluetooth support */
576static int nb_hcis;
577static int cur_hci;
578static struct HCIInfo *hci_table[MAX_NICS];
579
580static struct bt_vlan_s {
581 struct bt_scatternet_s net;
582 int id;
583 struct bt_vlan_s *next;
584} *first_bt_vlan;
585
586/* find or alloc a new bluetooth "VLAN" */
587static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
588{
589 struct bt_vlan_s **pvlan, *vlan;
590 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
591 if (vlan->id == id)
592 return &vlan->net;
593 }
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +0100594 vlan = g_malloc0(sizeof(struct bt_vlan_s));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700595 vlan->id = id;
596 pvlan = &first_bt_vlan;
597 while (*pvlan != NULL)
598 pvlan = &(*pvlan)->next;
599 *pvlan = vlan;
600 return &vlan->net;
601}
602
603static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
604{
605}
606
607static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
608{
609 return -ENOTSUP;
610}
611
612static struct HCIInfo null_hci = {
613 .cmd_send = null_hci_send,
614 .sco_send = null_hci_send,
615 .acl_send = null_hci_send,
616 .bdaddr_set = null_hci_addr_set,
617};
618
619struct HCIInfo *qemu_next_hci(void)
620{
621 if (cur_hci == nb_hcis)
622 return &null_hci;
623
624 return hci_table[cur_hci++];
625}
626
627static struct HCIInfo *hci_init(const char *str)
628{
629 char *endp;
630 struct bt_scatternet_s *vlan = 0;
631
632 if (!strcmp(str, "null"))
633 /* null */
634 return &null_hci;
635 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
636 /* host[:hciN] */
637 return bt_host_hci(str[4] ? str + 5 : "hci0");
638 else if (!strncmp(str, "hci", 3)) {
639 /* hci[,vlan=n] */
640 if (str[3]) {
641 if (!strncmp(str + 3, ",vlan=", 6)) {
642 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
643 if (*endp)
644 vlan = 0;
645 }
646 } else
647 vlan = qemu_find_bt_vlan(0);
648 if (vlan)
649 return bt_new_hci(vlan);
650 }
651
652 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
653
654 return 0;
655}
656
657static int bt_hci_parse(const char *str)
658{
659 struct HCIInfo *hci;
660 bdaddr_t bdaddr;
661
662 if (nb_hcis >= MAX_NICS) {
663 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
664 return -1;
665 }
666
667 hci = hci_init(str);
668 if (!hci)
669 return -1;
670
671 bdaddr.b[0] = 0x52;
672 bdaddr.b[1] = 0x54;
673 bdaddr.b[2] = 0x00;
674 bdaddr.b[3] = 0x12;
675 bdaddr.b[4] = 0x34;
676 bdaddr.b[5] = 0x56 + nb_hcis;
677 hci->bdaddr_set(hci, bdaddr.b);
678
679 hci_table[nb_hcis++] = hci;
680
681 return 0;
682}
683
684static void bt_vhci_add(int vlan_id)
685{
686 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
687
688 if (!vlan->slave)
689 fprintf(stderr, "qemu: warning: adding a VHCI to "
690 "an empty scatternet %i\n", vlan_id);
691
692 bt_vhci_init(bt_new_hci(vlan));
693}
694
695static struct bt_device_s *bt_device_add(const char *opt)
696{
697 struct bt_scatternet_s *vlan;
698 int vlan_id = 0;
699 char *endp = strstr(opt, ",vlan=");
700 int len = (endp ? endp - opt : strlen(opt)) + 1;
701 char devname[10];
702
703 pstrcpy(devname, MIN(sizeof(devname), len), opt);
704
705 if (endp) {
706 vlan_id = strtol(endp + 6, &endp, 0);
707 if (*endp) {
708 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
709 return 0;
710 }
711 }
712
713 vlan = qemu_find_bt_vlan(vlan_id);
714
715 if (!vlan->slave)
716 fprintf(stderr, "qemu: warning: adding a slave device to "
717 "an empty scatternet %i\n", vlan_id);
718
719 if (!strcmp(devname, "keyboard"))
720 return bt_keyboard_init(vlan);
721
722 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
723 return 0;
724}
725
726static int bt_parse(const char *opt)
727{
728 const char *endp, *p;
729 int vlan;
730
731 if (strstart(opt, "hci", &endp)) {
732 if (!*endp || *endp == ',') {
733 if (*endp)
734 if (!strstart(endp, ",vlan=", 0))
735 opt = endp + 1;
736
737 return bt_hci_parse(opt);
738 }
739 } else if (strstart(opt, "vhci", &endp)) {
740 if (!*endp || *endp == ',') {
741 if (*endp) {
742 if (strstart(endp, ",vlan=", &p)) {
743 vlan = strtol(p, (char **) &endp, 0);
744 if (*endp) {
745 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
746 return 1;
747 }
748 } else {
749 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
750 return 1;
751 }
752 } else
753 vlan = 0;
754
755 bt_vhci_add(vlan);
756 return 0;
757 }
758 } else if (strstart(opt, "device:", &endp))
759 return !bt_device_add(endp);
760
761 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
762 return 1;
763}
764
765/***********************************************************/
766/* QEMU Block devices */
767
768#define HD_ALIAS "index=%d,media=disk"
769#define CDROM_ALIAS "index=2,media=cdrom"
770#define FD_ALIAS "index=%d,if=floppy"
771#define PFLASH_ALIAS "if=pflash"
772#define MTD_ALIAS "if=mtd"
773#define SD_ALIAS "index=0,if=sd"
774
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100775static int drive_init_func(QemuOpts *opts, void *opaque)
776{
777 int *use_scsi = opaque;
778 int fatal_error = 0;
779
780 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
781 if (fatal_error)
782 return 1;
783 }
784 return 0;
785}
786
787static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
788{
789 if (NULL == qemu_opt_get(opts, "snapshot")) {
790 qemu_opt_set(opts, "snapshot", "on");
791 }
792 return 0;
793}
794
795#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700796static int drive_opt_get_free_idx(void)
797{
798 int index;
799
800 for (index = 0; index < MAX_DRIVES; index++)
801 if (!drives_opt[index].used) {
802 drives_opt[index].used = 1;
803 return index;
804 }
805
806 return -1;
807}
808
809static int drive_get_free_idx(void)
810{
811 int index;
812
813 for (index = 0; index < MAX_DRIVES; index++)
814 if (!drives_table[index].used) {
815 drives_table[index].used = 1;
816 return index;
817 }
818
819 return -1;
820}
821
822int drive_add(const char *file, const char *fmt, ...)
823{
824 va_list ap;
825 int index = drive_opt_get_free_idx();
826
827 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
828 fprintf(stderr, "qemu: too many drives\n");
829 return -1;
830 }
831
832 drives_opt[index].file = file;
833 va_start(ap, fmt);
834 vsnprintf(drives_opt[index].opt,
835 sizeof(drives_opt[0].opt), fmt, ap);
836 va_end(ap);
David 'Digit' Turner92568952010-04-15 15:04:16 -0700837
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700838 nb_drives_opt++;
839 return index;
840}
841
842void drive_remove(int index)
843{
844 drives_opt[index].used = 0;
845 nb_drives_opt--;
846}
847
848int drive_get_index(BlockInterfaceType type, int bus, int unit)
849{
850 int index;
851
852 /* seek interface, bus and unit */
853
854 for (index = 0; index < MAX_DRIVES; index++)
855 if (drives_table[index].type == type &&
856 drives_table[index].bus == bus &&
857 drives_table[index].unit == unit &&
858 drives_table[index].used)
859 return index;
860
861 return -1;
862}
863
864int drive_get_max_bus(BlockInterfaceType type)
865{
866 int max_bus;
867 int index;
868
869 max_bus = -1;
870 for (index = 0; index < nb_drives; index++) {
871 if(drives_table[index].type == type &&
872 drives_table[index].bus > max_bus)
873 max_bus = drives_table[index].bus;
874 }
875 return max_bus;
876}
877
878const char *drive_get_serial(BlockDriverState *bdrv)
879{
880 int index;
881
882 for (index = 0; index < nb_drives; index++)
883 if (drives_table[index].bdrv == bdrv)
884 return drives_table[index].serial;
885
886 return "\0";
887}
888
889BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
890{
891 int index;
892
893 for (index = 0; index < nb_drives; index++)
894 if (drives_table[index].bdrv == bdrv)
895 return drives_table[index].onerror;
896
897 return BLOCK_ERR_STOP_ENOSPC;
898}
899
900static void bdrv_format_print(void *opaque, const char *name)
901{
902 fprintf(stderr, " %s", name);
903}
904
905void drive_uninit(BlockDriverState *bdrv)
906{
907 int i;
908
909 for (i = 0; i < MAX_DRIVES; i++)
910 if (drives_table[i].bdrv == bdrv) {
911 drives_table[i].bdrv = NULL;
912 drives_table[i].used = 0;
913 drive_remove(drives_table[i].drive_opt_idx);
914 nb_drives--;
915 break;
916 }
917}
918
919int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
920{
921 char buf[128];
922 char file[1024];
923 char devname[128];
924 char serial[21];
925 const char *mediastr = "";
926 BlockInterfaceType type;
927 enum { MEDIA_DISK, MEDIA_CDROM } media;
928 int bus_id, unit_id;
929 int cyls, heads, secs, translation;
930 BlockDriverState *bdrv;
931 BlockDriver *drv = NULL;
932 QEMUMachine *machine = opaque;
933 int max_devs;
934 int index;
935 int cache;
936 int bdrv_flags, onerror;
937 int drives_table_idx;
938 char *str = arg->opt;
939 static const char * const params[] = { "bus", "unit", "if", "index",
940 "cyls", "heads", "secs", "trans",
941 "media", "snapshot", "file",
942 "cache", "format", "serial", "werror",
943 NULL };
944
945 if (check_params(buf, sizeof(buf), params, str) < 0) {
946 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
947 buf, str);
948 return -1;
949 }
950
951 file[0] = 0;
952 cyls = heads = secs = 0;
953 bus_id = 0;
954 unit_id = -1;
955 translation = BIOS_ATA_TRANSLATION_AUTO;
956 index = -1;
957 cache = 3;
958
959 if (machine->use_scsi) {
960 type = IF_SCSI;
961 max_devs = MAX_SCSI_DEVS;
962 pstrcpy(devname, sizeof(devname), "scsi");
963 } else {
964 type = IF_IDE;
965 max_devs = MAX_IDE_DEVS;
966 pstrcpy(devname, sizeof(devname), "ide");
967 }
968 media = MEDIA_DISK;
969
970 /* extract parameters */
971
972 if (get_param_value(buf, sizeof(buf), "bus", str)) {
973 bus_id = strtol(buf, NULL, 0);
974 if (bus_id < 0) {
975 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
976 return -1;
977 }
978 }
979
980 if (get_param_value(buf, sizeof(buf), "unit", str)) {
981 unit_id = strtol(buf, NULL, 0);
982 if (unit_id < 0) {
983 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
984 return -1;
985 }
986 }
987
988 if (get_param_value(buf, sizeof(buf), "if", str)) {
989 pstrcpy(devname, sizeof(devname), buf);
990 if (!strcmp(buf, "ide")) {
991 type = IF_IDE;
992 max_devs = MAX_IDE_DEVS;
993 } else if (!strcmp(buf, "scsi")) {
994 type = IF_SCSI;
995 max_devs = MAX_SCSI_DEVS;
996 } else if (!strcmp(buf, "floppy")) {
997 type = IF_FLOPPY;
998 max_devs = 0;
999 } else if (!strcmp(buf, "pflash")) {
1000 type = IF_PFLASH;
1001 max_devs = 0;
1002 } else if (!strcmp(buf, "mtd")) {
1003 type = IF_MTD;
1004 max_devs = 0;
1005 } else if (!strcmp(buf, "sd")) {
1006 type = IF_SD;
1007 max_devs = 0;
1008 } else if (!strcmp(buf, "virtio")) {
1009 type = IF_VIRTIO;
1010 max_devs = 0;
1011 } else if (!strcmp(buf, "xen")) {
1012 type = IF_XEN;
1013 max_devs = 0;
1014 } else {
1015 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
1016 return -1;
1017 }
1018 }
1019
1020 if (get_param_value(buf, sizeof(buf), "index", str)) {
1021 index = strtol(buf, NULL, 0);
1022 if (index < 0) {
1023 fprintf(stderr, "qemu: '%s' invalid index\n", str);
1024 return -1;
1025 }
1026 }
1027
1028 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
1029 cyls = strtol(buf, NULL, 0);
1030 }
1031
1032 if (get_param_value(buf, sizeof(buf), "heads", str)) {
1033 heads = strtol(buf, NULL, 0);
1034 }
1035
1036 if (get_param_value(buf, sizeof(buf), "secs", str)) {
1037 secs = strtol(buf, NULL, 0);
1038 }
1039
1040 if (cyls || heads || secs) {
1041 if (cyls < 1 || cyls > 16383) {
1042 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
1043 return -1;
1044 }
1045 if (heads < 1 || heads > 16) {
1046 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
1047 return -1;
1048 }
1049 if (secs < 1 || secs > 63) {
1050 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
1051 return -1;
1052 }
1053 }
1054
1055 if (get_param_value(buf, sizeof(buf), "trans", str)) {
1056 if (!cyls) {
1057 fprintf(stderr,
1058 "qemu: '%s' trans must be used with cyls,heads and secs\n",
1059 str);
1060 return -1;
1061 }
1062 if (!strcmp(buf, "none"))
1063 translation = BIOS_ATA_TRANSLATION_NONE;
1064 else if (!strcmp(buf, "lba"))
1065 translation = BIOS_ATA_TRANSLATION_LBA;
1066 else if (!strcmp(buf, "auto"))
1067 translation = BIOS_ATA_TRANSLATION_AUTO;
1068 else {
1069 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
1070 return -1;
1071 }
1072 }
1073
1074 if (get_param_value(buf, sizeof(buf), "media", str)) {
1075 if (!strcmp(buf, "disk")) {
1076 media = MEDIA_DISK;
1077 } else if (!strcmp(buf, "cdrom")) {
1078 if (cyls || secs || heads) {
1079 fprintf(stderr,
1080 "qemu: '%s' invalid physical CHS format\n", str);
1081 return -1;
1082 }
1083 media = MEDIA_CDROM;
1084 } else {
1085 fprintf(stderr, "qemu: '%s' invalid media\n", str);
1086 return -1;
1087 }
1088 }
1089
1090 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
1091 if (!strcmp(buf, "on"))
1092 snapshot = 1;
1093 else if (!strcmp(buf, "off"))
1094 snapshot = 0;
1095 else {
1096 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
1097 return -1;
1098 }
1099 }
1100
1101 if (get_param_value(buf, sizeof(buf), "cache", str)) {
1102 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
1103 cache = 0;
1104 else if (!strcmp(buf, "writethrough"))
1105 cache = 1;
1106 else if (!strcmp(buf, "writeback"))
1107 cache = 2;
1108 else {
1109 fprintf(stderr, "qemu: invalid cache option\n");
1110 return -1;
1111 }
1112 }
1113
1114 if (get_param_value(buf, sizeof(buf), "format", str)) {
1115 if (strcmp(buf, "?") == 0) {
1116 fprintf(stderr, "qemu: Supported formats:");
1117 bdrv_iterate_format(bdrv_format_print, NULL);
1118 fprintf(stderr, "\n");
1119 return -1;
1120 }
1121 drv = bdrv_find_format(buf);
1122 if (!drv) {
1123 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
1124 return -1;
1125 }
1126 }
1127
1128 if (arg->file == NULL)
1129 get_param_value(file, sizeof(file), "file", str);
1130 else
1131 pstrcpy(file, sizeof(file), arg->file);
1132
1133 if (!get_param_value(serial, sizeof(serial), "serial", str))
1134 memset(serial, 0, sizeof(serial));
1135
1136 onerror = BLOCK_ERR_STOP_ENOSPC;
1137 if (get_param_value(buf, sizeof(serial), "werror", str)) {
1138 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
1139 fprintf(stderr, "werror is no supported by this format\n");
1140 return -1;
1141 }
1142 if (!strcmp(buf, "ignore"))
1143 onerror = BLOCK_ERR_IGNORE;
1144 else if (!strcmp(buf, "enospc"))
1145 onerror = BLOCK_ERR_STOP_ENOSPC;
1146 else if (!strcmp(buf, "stop"))
1147 onerror = BLOCK_ERR_STOP_ANY;
1148 else if (!strcmp(buf, "report"))
1149 onerror = BLOCK_ERR_REPORT;
1150 else {
1151 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
1152 return -1;
1153 }
1154 }
1155
1156 /* compute bus and unit according index */
1157
1158 if (index != -1) {
1159 if (bus_id != 0 || unit_id != -1) {
1160 fprintf(stderr,
1161 "qemu: '%s' index cannot be used with bus and unit\n", str);
1162 return -1;
1163 }
1164 if (max_devs == 0)
1165 {
1166 unit_id = index;
1167 bus_id = 0;
1168 } else {
1169 unit_id = index % max_devs;
1170 bus_id = index / max_devs;
1171 }
1172 }
1173
1174 /* if user doesn't specify a unit_id,
1175 * try to find the first free
1176 */
1177
1178 if (unit_id == -1) {
1179 unit_id = 0;
1180 while (drive_get_index(type, bus_id, unit_id) != -1) {
1181 unit_id++;
1182 if (max_devs && unit_id >= max_devs) {
1183 unit_id -= max_devs;
1184 bus_id++;
1185 }
1186 }
1187 }
1188
1189 /* check unit id */
1190
1191 if (max_devs && unit_id >= max_devs) {
1192 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
1193 str, unit_id, max_devs - 1);
1194 return -1;
1195 }
1196
1197 /*
1198 * ignore multiple definitions
1199 */
1200
1201 if (drive_get_index(type, bus_id, unit_id) != -1)
1202 return -2;
1203
1204 /* init */
1205
1206 if (type == IF_IDE || type == IF_SCSI)
1207 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1208 if (max_devs)
1209 snprintf(buf, sizeof(buf), "%s%i%s%i",
1210 devname, bus_id, mediastr, unit_id);
1211 else
1212 snprintf(buf, sizeof(buf), "%s%s%i",
1213 devname, mediastr, unit_id);
1214 bdrv = bdrv_new(buf);
1215 drives_table_idx = drive_get_free_idx();
1216 drives_table[drives_table_idx].bdrv = bdrv;
1217 drives_table[drives_table_idx].type = type;
1218 drives_table[drives_table_idx].bus = bus_id;
1219 drives_table[drives_table_idx].unit = unit_id;
1220 drives_table[drives_table_idx].onerror = onerror;
1221 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
1222 strncpy(drives_table[drives_table_idx].serial, serial, sizeof(serial));
1223 nb_drives++;
1224
1225 switch(type) {
1226 case IF_IDE:
1227 case IF_SCSI:
1228 case IF_XEN:
1229 switch(media) {
1230 case MEDIA_DISK:
1231 if (cyls != 0) {
1232 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
1233 bdrv_set_translation_hint(bdrv, translation);
1234 }
1235 break;
1236 case MEDIA_CDROM:
1237 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
1238 break;
1239 }
1240 break;
1241 case IF_SD:
1242 /* FIXME: This isn't really a floppy, but it's a reasonable
1243 approximation. */
1244 case IF_FLOPPY:
1245 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
1246 break;
1247 case IF_PFLASH:
1248 case IF_MTD:
1249 case IF_VIRTIO:
1250 break;
1251 case IF_COUNT:
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07001252 case IF_NONE:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001253 abort();
1254 }
1255 if (!file[0])
1256 return -2;
1257 bdrv_flags = 0;
1258 if (snapshot) {
1259 bdrv_flags |= BDRV_O_SNAPSHOT;
1260 cache = 2; /* always use write-back with snapshot */
1261 }
1262 if (cache == 0) /* no caching */
1263 bdrv_flags |= BDRV_O_NOCACHE;
1264 else if (cache == 2) /* write-back */
1265 bdrv_flags |= BDRV_O_CACHE_WB;
1266 else if (cache == 3) /* not specified */
1267 bdrv_flags |= BDRV_O_CACHE_DEF;
1268 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
1269 fprintf(stderr, "qemu: could not open disk image %s\n",
1270 file);
1271 return -1;
1272 }
1273 if (bdrv_key_required(bdrv))
1274 autostart = 0;
1275 return drives_table_idx;
1276}
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01001277#endif /* MAX_DRIVES */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001278
1279static void numa_add(const char *optarg)
1280{
1281 char option[128];
1282 char *endptr;
1283 unsigned long long value, endvalue;
1284 int nodenr;
1285
1286 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1287 if (!strcmp(option, "node")) {
1288 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1289 nodenr = nb_numa_nodes;
1290 } else {
1291 nodenr = strtoull(option, NULL, 10);
1292 }
1293
1294 if (get_param_value(option, 128, "mem", optarg) == 0) {
1295 node_mem[nodenr] = 0;
1296 } else {
1297 value = strtoull(option, &endptr, 0);
1298 switch (*endptr) {
1299 case 0: case 'M': case 'm':
1300 value <<= 20;
1301 break;
1302 case 'G': case 'g':
1303 value <<= 30;
1304 break;
1305 }
1306 node_mem[nodenr] = value;
1307 }
1308 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1309 node_cpumask[nodenr] = 0;
1310 } else {
1311 value = strtoull(option, &endptr, 10);
1312 if (value >= 64) {
1313 value = 63;
1314 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1315 } else {
1316 if (*endptr == '-') {
1317 endvalue = strtoull(endptr+1, &endptr, 10);
1318 if (endvalue >= 63) {
1319 endvalue = 62;
1320 fprintf(stderr,
1321 "only 63 CPUs in NUMA mode supported.\n");
1322 }
1323 value = (1 << (endvalue + 1)) - (1 << value);
1324 } else {
1325 value = 1 << value;
1326 }
1327 }
1328 node_cpumask[nodenr] = value;
1329 }
1330 nb_numa_nodes++;
1331 }
1332 return;
1333}
1334
1335/***********************************************************/
1336/* USB devices */
1337
1338static USBPort *used_usb_ports;
1339static USBPort *free_usb_ports;
1340
1341/* ??? Maybe change this to register a hub to keep track of the topology. */
1342void qemu_register_usb_port(USBPort *port, void *opaque, int index,
1343 usb_attachfn attach)
1344{
1345 port->opaque = opaque;
1346 port->index = index;
1347 port->attach = attach;
1348 port->next = free_usb_ports;
1349 free_usb_ports = port;
1350}
1351
1352int usb_device_add_dev(USBDevice *dev)
1353{
1354 USBPort *port;
1355
1356 /* Find a USB port to add the device to. */
1357 port = free_usb_ports;
Manuel Francisco Naranjo58ecd742012-07-18 11:22:09 -03001358 if (!port){
1359 USBDevice *hub = usb_hub_init(VM_USB_HUB_SIZE);
1360 port = free_usb_ports;
1361 usb_attach(port, hub);
1362 }
1363 else if (!port->next){
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001364 USBDevice *hub;
1365
1366 /* Create a new hub and chain it on. */
1367 free_usb_ports = NULL;
1368 port->next = used_usb_ports;
1369 used_usb_ports = port;
1370
1371 hub = usb_hub_init(VM_USB_HUB_SIZE);
1372 usb_attach(port, hub);
1373 port = free_usb_ports;
1374 }
1375
1376 free_usb_ports = port->next;
1377 port->next = used_usb_ports;
1378 used_usb_ports = port;
1379 usb_attach(port, dev);
1380 return 0;
1381}
1382
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001383#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001384static void usb_msd_password_cb(void *opaque, int err)
1385{
1386 USBDevice *dev = opaque;
1387
1388 if (!err)
1389 usb_device_add_dev(dev);
1390 else
1391 dev->handle_destroy(dev);
1392}
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001393#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001394
1395static int usb_device_add(const char *devname, int is_hotplug)
1396{
1397 const char *p;
1398 USBDevice *dev;
1399
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001400 if (strstart(devname, "host:", &p)) {
1401 dev = usb_host_device_open(p);
1402 } else if (!strcmp(devname, "mouse")) {
1403 dev = usb_mouse_init();
1404 } else if (!strcmp(devname, "tablet")) {
1405 dev = usb_tablet_init();
1406 } else if (!strcmp(devname, "keyboard")) {
1407 dev = usb_keyboard_init();
1408 } else if (strstart(devname, "disk:", &p)) {
1409#if 0
1410 BlockDriverState *bs;
1411#endif
1412 dev = usb_msd_init(p);
1413 if (!dev)
1414 return -1;
1415#if 0
1416 bs = usb_msd_get_bdrv(dev);
1417 if (bdrv_key_required(bs)) {
1418 autostart = 0;
1419 if (is_hotplug) {
1420 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
1421 dev);
1422 return 0;
1423 }
1424 }
1425 } else if (!strcmp(devname, "wacom-tablet")) {
1426 dev = usb_wacom_init();
1427 } else if (strstart(devname, "serial:", &p)) {
1428 dev = usb_serial_init(p);
1429#ifdef CONFIG_BRLAPI
1430 } else if (!strcmp(devname, "braille")) {
1431 dev = usb_baum_init();
1432#endif
1433 } else if (strstart(devname, "net:", &p)) {
1434 int nic = nb_nics;
1435
1436 if (net_client_init("nic", p) < 0)
1437 return -1;
1438 nd_table[nic].model = "usb";
1439 dev = usb_net_init(&nd_table[nic]);
1440 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1441 dev = usb_bt_init(devname[2] ? hci_init(p) :
1442 bt_new_hci(qemu_find_bt_vlan(0)));
1443#endif
1444 } else {
1445 return -1;
1446 }
1447 if (!dev)
1448 return -1;
1449
1450 return usb_device_add_dev(dev);
1451}
1452
1453int usb_device_del_addr(int bus_num, int addr)
1454{
1455 USBPort *port;
1456 USBPort **lastp;
1457 USBDevice *dev;
1458
1459 if (!used_usb_ports)
1460 return -1;
1461
1462 if (bus_num != 0)
1463 return -1;
1464
1465 lastp = &used_usb_ports;
1466 port = used_usb_ports;
1467 while (port && port->dev->addr != addr) {
1468 lastp = &port->next;
1469 port = port->next;
1470 }
1471
1472 if (!port)
1473 return -1;
1474
1475 dev = port->dev;
1476 *lastp = port->next;
1477 usb_attach(port, NULL);
1478 dev->handle_destroy(dev);
1479 port->next = free_usb_ports;
1480 free_usb_ports = port;
1481 return 0;
1482}
1483
1484static int usb_device_del(const char *devname)
1485{
1486 int bus_num, addr;
1487 const char *p;
1488
1489 if (strstart(devname, "host:", &p))
1490 return usb_host_device_close(p);
1491
1492 if (!used_usb_ports)
1493 return -1;
1494
1495 p = strchr(devname, '.');
1496 if (!p)
1497 return -1;
1498 bus_num = strtoul(devname, NULL, 0);
1499 addr = strtoul(p + 1, NULL, 0);
1500
1501 return usb_device_del_addr(bus_num, addr);
1502}
1503
1504void do_usb_add(Monitor *mon, const char *devname)
1505{
1506 usb_device_add(devname, 1);
1507}
1508
1509void do_usb_del(Monitor *mon, const char *devname)
1510{
1511 usb_device_del(devname);
1512}
1513
1514void usb_info(Monitor *mon)
1515{
1516 USBDevice *dev;
1517 USBPort *port;
1518 const char *speed_str;
1519
1520 if (!usb_enabled) {
1521 monitor_printf(mon, "USB support not enabled\n");
1522 return;
1523 }
1524
1525 for (port = used_usb_ports; port; port = port->next) {
1526 dev = port->dev;
1527 if (!dev)
1528 continue;
1529 switch(dev->speed) {
1530 case USB_SPEED_LOW:
1531 speed_str = "1.5";
1532 break;
1533 case USB_SPEED_FULL:
1534 speed_str = "12";
1535 break;
1536 case USB_SPEED_HIGH:
1537 speed_str = "480";
1538 break;
1539 default:
1540 speed_str = "?";
1541 break;
1542 }
1543 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
1544 0, dev->addr, speed_str, dev->devname);
1545 }
1546}
1547
1548/***********************************************************/
1549/* PCMCIA/Cardbus */
1550
1551static struct pcmcia_socket_entry_s {
1552 PCMCIASocket *socket;
1553 struct pcmcia_socket_entry_s *next;
1554} *pcmcia_sockets = 0;
1555
1556void pcmcia_socket_register(PCMCIASocket *socket)
1557{
1558 struct pcmcia_socket_entry_s *entry;
1559
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001560 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001561 entry->socket = socket;
1562 entry->next = pcmcia_sockets;
1563 pcmcia_sockets = entry;
1564}
1565
1566void pcmcia_socket_unregister(PCMCIASocket *socket)
1567{
1568 struct pcmcia_socket_entry_s *entry, **ptr;
1569
1570 ptr = &pcmcia_sockets;
1571 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1572 if (entry->socket == socket) {
1573 *ptr = entry->next;
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001574 g_free(entry);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001575 }
1576}
1577
1578void pcmcia_info(Monitor *mon)
1579{
1580 struct pcmcia_socket_entry_s *iter;
1581
1582 if (!pcmcia_sockets)
1583 monitor_printf(mon, "No PCMCIA sockets\n");
1584
1585 for (iter = pcmcia_sockets; iter; iter = iter->next)
1586 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1587 iter->socket->attached ? iter->socket->card_string :
1588 "Empty");
1589}
1590
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001591/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001592/* machine registration */
1593
1594static QEMUMachine *first_machine = NULL;
1595QEMUMachine *current_machine = NULL;
1596
1597int qemu_register_machine(QEMUMachine *m)
1598{
1599 QEMUMachine **pm;
1600 pm = &first_machine;
1601 while (*pm != NULL)
1602 pm = &(*pm)->next;
1603 m->next = NULL;
1604 *pm = m;
1605 return 0;
1606}
1607
1608static QEMUMachine *find_machine(const char *name)
1609{
1610 QEMUMachine *m;
1611
1612 for(m = first_machine; m != NULL; m = m->next) {
1613 if (!strcmp(m->name, name))
1614 return m;
1615 }
1616 return NULL;
1617}
1618
1619static QEMUMachine *find_default_machine(void)
1620{
1621 QEMUMachine *m;
1622
1623 for(m = first_machine; m != NULL; m = m->next) {
1624 if (m->is_default) {
1625 return m;
1626 }
1627 }
1628 return NULL;
1629}
1630
1631/***********************************************************/
1632/* main execution loop */
1633
1634static void gui_update(void *opaque)
1635{
1636 uint64_t interval = GUI_REFRESH_INTERVAL;
1637 DisplayState *ds = opaque;
1638 DisplayChangeListener *dcl = ds->listeners;
1639
1640 dpy_refresh(ds);
1641
1642 while (dcl != NULL) {
1643 if (dcl->gui_timer_interval &&
1644 dcl->gui_timer_interval < interval)
1645 interval = dcl->gui_timer_interval;
1646 dcl = dcl->next;
1647 }
David 'Digit' Turnerdcda9492014-02-16 15:13:55 +01001648 timer_mod(ds->gui_timer, interval + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001649}
1650
1651static void nographic_update(void *opaque)
1652{
1653 uint64_t interval = GUI_REFRESH_INTERVAL;
1654
David 'Digit' Turnerdcda9492014-02-16 15:13:55 +01001655 timer_mod(nographic_timer, interval + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001656}
1657
1658struct vm_change_state_entry {
1659 VMChangeStateHandler *cb;
1660 void *opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001661 QLIST_ENTRY (vm_change_state_entry) entries;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001662};
1663
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001664static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001665
1666VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1667 void *opaque)
1668{
1669 VMChangeStateEntry *e;
1670
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001671 e = g_malloc0(sizeof (*e));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001672
1673 e->cb = cb;
1674 e->opaque = opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001675 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001676 return e;
1677}
1678
1679void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1680{
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001681 QLIST_REMOVE (e, entries);
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001682 g_free (e);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001683}
1684
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001685void vm_state_notify(int running, int reason)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001686{
1687 VMChangeStateEntry *e;
1688
1689 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1690 e->cb(e->opaque, running, reason);
1691 }
1692}
1693
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001694void vm_start(void)
1695{
1696 if (!vm_running) {
1697 cpu_enable_ticks();
1698 vm_running = 1;
1699 vm_state_notify(1, 0);
David Turner6a9ef172010-09-09 22:54:36 +02001700 //qemu_rearm_alarm_timer(alarm_timer);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001701 resume_all_vcpus();
1702 }
1703}
1704
1705/* reset/shutdown handler */
1706
1707typedef struct QEMUResetEntry {
1708 QEMUResetHandler *func;
1709 void *opaque;
1710 int order;
1711 struct QEMUResetEntry *next;
1712} QEMUResetEntry;
1713
1714static QEMUResetEntry *first_reset_entry;
1715static int reset_requested;
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001716static int shutdown_requested, shutdown_signal = -1;
1717static pid_t shutdown_pid;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001718static int powerdown_requested;
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001719int debug_requested;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001720static int vmstop_requested;
1721
1722int qemu_shutdown_requested(void)
1723{
1724 int r = shutdown_requested;
1725 shutdown_requested = 0;
1726 return r;
1727}
1728
1729int qemu_reset_requested(void)
1730{
1731 int r = reset_requested;
1732 reset_requested = 0;
1733 return r;
1734}
1735
1736int qemu_powerdown_requested(void)
1737{
1738 int r = powerdown_requested;
1739 powerdown_requested = 0;
1740 return r;
1741}
1742
David 'Digit' Turnerf9077a82014-02-10 23:10:47 +01001743int qemu_debug_requested(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001744{
1745 int r = debug_requested;
1746 debug_requested = 0;
1747 return r;
1748}
1749
David 'Digit' Turnerf9077a82014-02-10 23:10:47 +01001750int qemu_vmstop_requested(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001751{
1752 int r = vmstop_requested;
1753 vmstop_requested = 0;
1754 return r;
1755}
1756
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001757void qemu_register_reset(QEMUResetHandler *func, int order, void *opaque)
1758{
1759 QEMUResetEntry **pre, *re;
1760
1761 pre = &first_reset_entry;
1762 while (*pre != NULL && (*pre)->order >= order) {
1763 pre = &(*pre)->next;
1764 }
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001765 re = g_malloc0(sizeof(QEMUResetEntry));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001766 re->func = func;
1767 re->opaque = opaque;
1768 re->order = order;
1769 re->next = NULL;
1770 *pre = re;
1771}
1772
1773void qemu_system_reset(void)
1774{
1775 QEMUResetEntry *re;
1776
1777 /* reset all devices */
1778 for(re = first_reset_entry; re != NULL; re = re->next) {
1779 re->func(re->opaque);
1780 }
1781}
1782
1783void qemu_system_reset_request(void)
1784{
1785 if (no_reboot) {
1786 shutdown_requested = 1;
1787 } else {
1788 reset_requested = 1;
1789 }
1790 qemu_notify_event();
1791}
1792
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001793void qemu_system_killed(int signal, pid_t pid)
1794{
1795 shutdown_signal = signal;
1796 shutdown_pid = pid;
1797 qemu_system_shutdown_request();
1798}
1799
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001800void qemu_system_shutdown_request(void)
1801{
1802 shutdown_requested = 1;
1803 qemu_notify_event();
1804}
1805
1806void qemu_system_powerdown_request(void)
1807{
1808 powerdown_requested = 1;
1809 qemu_notify_event();
1810}
1811
David 'Digit' Turnerf9077a82014-02-10 23:10:47 +01001812int vm_can_run(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001813{
1814 if (powerdown_requested)
1815 return 0;
1816 if (reset_requested)
1817 return 0;
1818 if (shutdown_requested)
1819 return 0;
1820 if (debug_requested)
1821 return 0;
1822 return 1;
1823}
1824
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07001825void version(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001826{
1827 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1828}
1829
1830void qemu_help(int exitcode)
1831{
1832 version();
1833 printf("usage: %s [options] [disk_image]\n"
1834 "\n"
1835 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
1836 "\n"
1837#define DEF(option, opt_arg, opt_enum, opt_help) \
1838 opt_help
1839#define DEFHEADING(text) stringify(text) "\n"
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001840#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001841#undef DEF
1842#undef DEFHEADING
1843#undef GEN_DOCS
1844 "\n"
1845 "During emulation, the following keys are useful:\n"
1846 "ctrl-alt-f toggle full screen\n"
1847 "ctrl-alt-n switch to virtual console 'n'\n"
1848 "ctrl-alt toggle mouse and keyboard grab\n"
1849 "\n"
1850 "When using -nographic, press 'ctrl-a h' to get some help.\n"
1851 ,
1852 "qemu",
1853 DEFAULT_RAM_SIZE,
1854#ifndef _WIN32
1855 DEFAULT_NETWORK_SCRIPT,
1856 DEFAULT_NETWORK_DOWN_SCRIPT,
1857#endif
1858 DEFAULT_GDBSTUB_PORT,
1859 "/tmp/qemu.log");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07001860 QEMU_EXIT(exitcode);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001861}
1862
1863#define HAS_ARG 0x0001
1864
1865enum {
1866#define DEF(option, opt_arg, opt_enum, opt_help) \
1867 opt_enum,
1868#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001869#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001870#undef DEF
1871#undef DEFHEADING
1872#undef GEN_DOCS
1873};
1874
1875typedef struct QEMUOption {
1876 const char *name;
1877 int flags;
1878 int index;
1879} QEMUOption;
1880
1881static const QEMUOption qemu_options[] = {
1882 { "h", 0, QEMU_OPTION_h },
1883#define DEF(option, opt_arg, opt_enum, opt_help) \
1884 { option, opt_arg, opt_enum },
1885#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001886#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001887#undef DEF
1888#undef DEFHEADING
1889#undef GEN_DOCS
1890 { NULL, 0, 0 },
1891};
1892
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001893static void select_vgahw (const char *p)
1894{
1895 const char *opts;
1896
1897 cirrus_vga_enabled = 0;
1898 std_vga_enabled = 0;
1899 vmsvga_enabled = 0;
1900 xenfb_enabled = 0;
1901 if (strstart(p, "std", &opts)) {
1902 std_vga_enabled = 1;
1903 } else if (strstart(p, "cirrus", &opts)) {
1904 cirrus_vga_enabled = 1;
1905 } else if (strstart(p, "vmware", &opts)) {
1906 vmsvga_enabled = 1;
1907 } else if (strstart(p, "xenfb", &opts)) {
1908 xenfb_enabled = 1;
1909 } else if (!strstart(p, "none", &opts)) {
1910 invalid_vga:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07001911 PANIC("Unknown vga type: %s", p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001912 }
1913 while (*opts) {
1914 const char *nextopt;
1915
1916 if (strstart(opts, ",retrace=", &nextopt)) {
1917 opts = nextopt;
1918 if (strstart(opts, "dumb", &nextopt))
1919 vga_retrace_method = VGA_RETRACE_DUMB;
1920 else if (strstart(opts, "precise", &nextopt))
1921 vga_retrace_method = VGA_RETRACE_PRECISE;
1922 else goto invalid_vga;
1923 } else goto invalid_vga;
1924 opts = nextopt;
1925 }
1926}
1927
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001928#define MAX_NET_CLIENTS 32
1929
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001930#ifdef _WIN32
1931/* Look for support files in the same directory as the executable. */
1932static char *find_datadir(const char *argv0)
1933{
1934 char *p;
1935 char buf[MAX_PATH];
1936 DWORD len;
1937
1938 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
1939 if (len == 0) {
1940 return NULL;
1941 }
1942
1943 buf[len] = 0;
1944 p = buf + len - 1;
1945 while (p != buf && *p != '\\')
1946 p--;
1947 *p = 0;
1948 if (access(buf, R_OK) == 0) {
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001949 return g_strdup(buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001950 }
1951 return NULL;
1952}
1953#else /* !_WIN32 */
1954
David 'Digit' Turner24d27522011-06-01 16:50:56 +02001955/* Similarly, return the location of the executable */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001956static char *find_datadir(const char *argv0)
1957{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001958 char *p = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001959 char buf[PATH_MAX];
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001960
1961#if defined(__linux__)
1962 {
1963 int len;
1964 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
1965 if (len > 0) {
1966 buf[len] = 0;
1967 p = buf;
1968 }
1969 }
1970#elif defined(__FreeBSD__)
1971 {
1972 int len;
1973 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
1974 if (len > 0) {
1975 buf[len] = 0;
1976 p = buf;
1977 }
1978 }
1979#endif
1980 /* If we don't have any way of figuring out the actual executable
1981 location then try argv[0]. */
1982 if (!p) {
David 'Digit' Turner24d27522011-06-01 16:50:56 +02001983 p = realpath(argv0, buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001984 if (!p) {
1985 return NULL;
1986 }
1987 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001988
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001989 return g_strdup(dirname(buf));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001990}
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001991#endif
1992
David 'Digit' Turner24d27522011-06-01 16:50:56 +02001993static char*
1994qemu_find_file_with_subdir(const char* data_dir, const char* subdir, const char* name)
1995{
1996 int len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001997 char* buf = g_malloc0(len);
David 'Digit' Turner24d27522011-06-01 16:50:56 +02001998
1999 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
2000 VERBOSE_PRINT(init," trying to find: %s\n", buf);
2001 if (access(buf, R_OK)) {
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01002002 g_free(buf);
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002003 return NULL;
2004 }
2005 return buf;
2006}
2007
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002008char *qemu_find_file(int type, const char *name)
2009{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002010 const char *subdir;
2011 char *buf;
2012
2013 /* If name contains path separators then try it as a straight path. */
2014 if ((strchr(name, '/') || strchr(name, '\\'))
2015 && access(name, R_OK) == 0) {
2016 return strdup(name);
2017 }
2018 switch (type) {
2019 case QEMU_FILE_TYPE_BIOS:
2020 subdir = "";
2021 break;
2022 case QEMU_FILE_TYPE_KEYMAP:
2023 subdir = "keymaps/";
2024 break;
2025 default:
2026 abort();
2027 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002028 buf = qemu_find_file_with_subdir(data_dir, subdir, name);
2029#ifdef CONFIG_ANDROID
2030 if (type == QEMU_FILE_TYPE_BIOS) {
2031 /* This case corresponds to the emulator being used as part of an
2032 * SDK installation. NOTE: data_dir is really $bindir. */
2033 if (buf == NULL)
2034 buf = qemu_find_file_with_subdir(data_dir, "lib/pc-bios/", name);
2035 /* This case corresponds to platform builds. */
2036 if (buf == NULL)
2037 buf = qemu_find_file_with_subdir(data_dir, "../usr/share/pc-bios/", name);
2038 /* Finally, try this for standalone builds under external/qemu */
2039 if (buf == NULL)
Andrew Hsieh3d894282012-05-02 12:06:12 +08002040 buf = qemu_find_file_with_subdir(data_dir, "../../../prebuilts/qemu-kernel/x86/pc-bios/", name);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002041 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002042#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002043 return buf;
2044}
2045
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07002046static int
2047add_dns_server( const char* server_name )
2048{
2049 SockAddress addr;
2050
2051 if (sock_address_init_resolve( &addr, server_name, 55, 0 ) < 0) {
2052 fprintf(stdout,
2053 "### WARNING: can't resolve DNS server name '%s'\n",
2054 server_name );
2055 return -1;
2056 }
2057
2058 fprintf(stderr,
2059 "DNS server name '%s' resolved to %s\n", server_name, sock_address_to_string(&addr) );
2060
2061 if ( slirp_add_dns_server( &addr ) < 0 ) {
2062 fprintf(stderr,
2063 "### WARNING: could not add DNS server '%s' to the network stack\n", server_name);
2064 return -1;
2065 }
2066 return 0;
2067}
2068
rich cannings7339b552011-02-16 13:43:44 -08002069/* Parses an integer
2070 * Pararm:
2071 * str String containing a number to be parsed.
2072 * result Passes the parsed integer in this argument
2073 * returns 0 if ok, -1 if failed
2074 */
2075int
2076parse_int(const char *str, int *result)
2077{
2078 char* r;
2079 *result = strtol(str, &r, 0);
2080 if (r == NULL || *r != '\0')
2081 return -1;
2082
2083 return 0;
2084}
2085
rich cannings7339b552011-02-16 13:43:44 -08002086/* parses a null-terminated string specifying a network port (e.g., "80") or
2087 * port range (e.g., "[6666-7000]"). In case of a single port, lport and hport
2088 * are the same. Returns 0 on success, -1 on error. */
2089
2090int parse_port_range(const char *str, unsigned short *lport,
2091 unsigned short *hport) {
2092
2093 unsigned int low = 0, high = 0;
2094 char *p, *arg = strdup(str);
2095
2096 if ((*arg == '[') && ((p = strrchr(arg, ']')) != NULL)) {
2097 p = arg + 1; /* skip '[' */
2098 low = atoi(strtok(p, "-"));
2099 high = atoi(strtok(NULL, "-"));
2100 if ((low > 0) && (high > 0) && (low < high) && (high < 65535)) {
2101 *lport = low;
2102 *hport = high;
2103 }
2104 }
2105 else {
2106 low = atoi(arg);
2107 if ((0 < low) && (low < 65535)) {
2108 *lport = low;
2109 *hport = low;
2110 }
2111 }
2112 free(arg);
2113 if (low != 0)
2114 return 0;
2115 return -1;
2116}
2117
2118/*
2119 * Implements the generic port forwarding option
2120 */
2121void
2122net_slirp_forward(const char *optarg)
2123{
2124 /*
2125 * we expect the following format:
2126 * dst_net:dst_mask:dst_port:redirect_ip:redirect_port OR
2127 * dst_net:dst_mask:[dp_range_start-dp_range_end]:redirect_ip:redirect_port
2128 */
2129 char *argument = strdup(optarg), *p = argument;
2130 char *dst_net, *dst_mask, *dst_port;
2131 char *redirect_ip, *redirect_port;
2132 uint32_t dnet, dmask, rip;
Vladimir Chtchetkineb557e9f2012-03-22 15:38:08 -07002133 unsigned short dlport = 0, dhport = 0, rport;
rich cannings7339b552011-02-16 13:43:44 -08002134
2135
2136 dst_net = strtok(p, ":");
2137 dst_mask = strtok(NULL, ":");
2138 dst_port = strtok(NULL, ":");
2139 redirect_ip = strtok(NULL, ":");
2140 redirect_port = strtok(NULL, ":");
2141
2142 if (dst_net == NULL || dst_mask == NULL || dst_port == NULL ||
2143 redirect_ip == NULL || redirect_port == NULL) {
2144 fprintf(stderr,
2145 "Invalid argument for -net-forward, we expect "
2146 "dst_net:dst_mask:dst_port:redirect_ip:redirect_port or "
2147 "dst_net:dst_mask:[dp_range_start-dp_range_end]"
2148 ":redirect_ip:redirect_port: %s\n",
2149 optarg);
2150 exit(1);
2151 }
2152
2153 /* inet_strtoip converts dotted address to host byte order */
2154 if (inet_strtoip(dst_net, &dnet) == -1) {
2155 fprintf(stderr, "Invalid destination IP net: %s\n", dst_net);
2156 exit(1);
2157 }
2158 if (inet_strtoip(dst_mask, &dmask) == -1) {
2159 fprintf(stderr, "Invalid destination IP mask: %s\n", dst_mask);
2160 exit(1);
2161 }
2162 if (inet_strtoip(redirect_ip, &rip) == -1) {
2163 fprintf(stderr, "Invalid redirect IP address: %s\n", redirect_ip);
2164 exit(1);
2165 }
2166
2167 if (parse_port_range(dst_port, &dlport, &dhport) == -1) {
2168 fprintf(stderr, "Invalid destination port or port range\n");
2169 exit(1);
2170 }
2171
2172 rport = atoi(redirect_port);
2173 if (!rport) {
2174 fprintf(stderr, "Invalid redirect port: %s\n", redirect_port);
2175 exit(1);
2176 }
2177
2178 dnet &= dmask;
2179
2180 slirp_add_net_forward(dnet, dmask, dlport, dhport,
2181 rip, rport);
2182
2183 free(argument);
2184}
2185
2186
2187/* Parses an -allow-tcp or -allow-udp argument and inserts a corresponding
2188 * entry in the allows list */
2189void
2190slirp_allow(const char *optarg, u_int8_t proto)
2191{
2192 /*
2193 * we expect the following format:
2194 * dst_ip:dst_port OR dst_ip:[dst_lport-dst_hport]
2195 */
2196 char *argument = strdup(optarg), *p = argument;
2197 char *dst_ip_str, *dst_port_str;
2198 uint32_t dst_ip;
Vladimir Chtchetkineb557e9f2012-03-22 15:38:08 -07002199 unsigned short dst_lport = 0, dst_hport = 0;
rich cannings7339b552011-02-16 13:43:44 -08002200
2201 dst_ip_str = strtok(p, ":");
2202 dst_port_str = strtok(NULL, ":");
2203
2204 if (dst_ip_str == NULL || dst_port_str == NULL) {
2205 fprintf(stderr,
2206 "Invalid argument %s for -allow. We expect "
2207 "dst_ip:dst_port or dst_ip:[dst_lport-dst_hport]\n",
2208 optarg);
2209 exit(1);
2210 }
2211
2212 if (inet_strtoip(dst_ip_str, &dst_ip) == -1) {
2213 fprintf(stderr, "Invalid destination IP address: %s\n", dst_ip_str);
2214 exit(1);
2215 }
2216 if (parse_port_range(dst_port_str, &dst_lport, &dst_hport) == -1) {
2217 fprintf(stderr, "Invalid destination port or port range\n");
2218 exit(1);
2219 }
2220
2221 slirp_add_allow(dst_ip, dst_lport, dst_hport, proto);
2222
2223 free(argument);
2224}
2225
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01002226/* Add a serial device at a given location in the emulated hardware table.
2227 * On failure, this function aborts the program with an error message.
2228 */
2229static void
2230serial_hds_add_at(int index, const char* devname)
2231{
2232 char label[32];
2233
2234 if (!devname || !strcmp(devname,"none"))
2235 return;
2236
2237 if (index >= MAX_SERIAL_PORTS) {
2238 PANIC("qemu: invalid serial index for %s (%d >= %d)",
2239 devname, index, MAX_SERIAL_PORTS);
2240 }
2241 if (serial_hds[index] != NULL) {
2242 PANIC("qemu: invalid serial index for %s (%d: already taken!)",
2243 devname, index);
2244 }
2245 snprintf(label, sizeof(label), "serial%d", index);
2246 serial_hds[index] = qemu_chr_open(label, devname, NULL);
2247 if (!serial_hds[index]) {
2248 PANIC("qemu: could not open serial device '%s'", devname);
2249 }
2250}
2251
2252
2253/* Find a free slot in the emulated serial device table, and register
2254 * it. Return the allocated table index.
2255 */
2256static int
2257serial_hds_add(const char* devname)
2258{
2259 int index;
2260
2261 /* Find first free slot */
2262 for (index = 0; index < MAX_SERIAL_PORTS; index++) {
2263 if (serial_hds[index] == NULL) {
2264 serial_hds_add_at(index, devname);
2265 return index;
2266 }
2267 }
2268
2269 PANIC("qemu: too many serial devices registered (%d)", index);
2270 return -1; /* shouldn't happen */
2271}
2272
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002273int main(int argc, char **argv, char **envp)
2274{
2275 const char *gdbstub_dev = NULL;
2276 uint32_t boot_devices_bitmap = 0;
2277 int i;
David 'Digit' Turnera2c14f92014-02-04 01:02:30 +01002278 int snapshot, linux_boot, __attribute__((unused)) net_boot;
David Turner6a9ef172010-09-09 22:54:36 +02002279 const char *icount_option = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002280 const char *initrd_filename;
2281 const char *kernel_filename, *kernel_cmdline;
2282 const char *boot_devices = "";
2283 DisplayState *ds;
2284 DisplayChangeListener *dcl;
2285 int cyls, heads, secs, translation;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002286 QemuOpts *hda_opts = NULL;
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01002287 QemuOpts *hdb_opts = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002288 const char *net_clients[MAX_NET_CLIENTS];
2289 int nb_net_clients;
2290 const char *bt_opts[MAX_BT_CMDLINE];
2291 int nb_bt_opts;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002292 int optind;
2293 const char *r, *optarg;
2294 CharDriverState *monitor_hd = NULL;
2295 const char *monitor_device;
2296 const char *serial_devices[MAX_SERIAL_PORTS];
2297 int serial_device_index;
2298 const char *parallel_devices[MAX_PARALLEL_PORTS];
2299 int parallel_device_index;
2300 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
2301 int virtio_console_index;
2302 const char *loadvm = NULL;
2303 QEMUMachine *machine;
2304 const char *cpu_model;
2305 const char *usb_devices[MAX_USB_CMDLINE];
2306 int usb_devices_index;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002307 int tb_size;
2308 const char *pid_file = NULL;
2309 const char *incoming = NULL;
David 'Digit' Turner7e9f6c12014-01-22 18:42:40 +01002310 const char* log_mask = NULL;
2311 const char* log_file = NULL;
David 'Digit' Turnere2678e12014-01-16 15:56:43 +01002312 CPUOldState *env;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002313 int show_vnc_port = 0;
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07002314 IniFile* hw_ini = NULL;
David 'Digit' Turner5f824112011-03-01 14:00:26 +01002315 STRALLOC_DEFINE(kernel_params);
2316 STRALLOC_DEFINE(kernel_config);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07002317 int dns_count = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002318
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002319 /* Initialize sockets before anything else, so we can properly report
2320 * initialization failures back to the UI. */
2321#ifdef _WIN32
2322 socket_init();
2323#endif
2324
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07002325 init_clocks();
2326
David 'Digit' Turner8848f632014-01-16 18:10:21 +01002327 qemu_cache_utils_init();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002328
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002329 QLIST_INIT (&vm_change_state_head);
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02002330 os_setup_early_signal_handling();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002331
2332 module_call_init(MODULE_INIT_MACHINE);
2333 machine = find_default_machine();
2334 cpu_model = NULL;
2335 initrd_filename = NULL;
2336 ram_size = 0;
2337 snapshot = 0;
2338 kernel_filename = NULL;
2339 kernel_cmdline = "";
David 'Digit' Turner5f824112011-03-01 14:00:26 +01002340
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002341 cyls = heads = secs = 0;
2342 translation = BIOS_ATA_TRANSLATION_AUTO;
2343 monitor_device = "vc:80Cx24C";
2344
2345 serial_devices[0] = "vc:80Cx24C";
2346 for(i = 1; i < MAX_SERIAL_PORTS; i++)
2347 serial_devices[i] = NULL;
2348 serial_device_index = 0;
2349
2350 parallel_devices[0] = "vc:80Cx24C";
2351 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
2352 parallel_devices[i] = NULL;
2353 parallel_device_index = 0;
2354
2355 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
2356 virtio_consoles[i] = NULL;
2357 virtio_console_index = 0;
2358
2359 for (i = 0; i < MAX_NODES; i++) {
2360 node_mem[i] = 0;
2361 node_cpumask[i] = 0;
2362 }
2363
2364 usb_devices_index = 0;
2365
2366 nb_net_clients = 0;
2367 nb_bt_opts = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002368#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002369 nb_drives = 0;
2370 nb_drives_opt = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002371#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002372 nb_numa_nodes = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002373
2374 nb_nics = 0;
2375
2376 tb_size = 0;
2377 autostart= 1;
2378
2379 register_watchdogs();
2380
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07002381 /* Initialize boot properties. */
2382 boot_property_init_service();
David 'Digit' Turnerca950592011-04-27 12:26:15 +02002383 android_hw_control_init();
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02002384 android_net_pipes_init();
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07002385
David 'Digit' Turner36597752011-05-20 01:18:01 +02002386#ifdef CONFIG_KVM
2387 /* By default, force auto-detection for kvm */
2388 kvm_allowed = -1;
2389#endif
2390
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002391 optind = 1;
2392 for(;;) {
2393 if (optind >= argc)
2394 break;
2395 r = argv[optind];
2396 if (r[0] != '-') {
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002397 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002398 } else {
2399 const QEMUOption *popt;
2400
2401 optind++;
2402 /* Treat --foo the same as -foo. */
2403 if (r[1] == '-')
2404 r++;
2405 popt = qemu_options;
2406 for(;;) {
2407 if (!popt->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002408 PANIC("%s: invalid option -- '%s'",
2409 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002410 }
2411 if (!strcmp(popt->name, r + 1))
2412 break;
2413 popt++;
2414 }
2415 if (popt->flags & HAS_ARG) {
2416 if (optind >= argc) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002417 PANIC("%s: option '%s' requires an argument",
2418 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002419 }
2420 optarg = argv[optind++];
2421 } else {
2422 optarg = NULL;
2423 }
2424
2425 switch(popt->index) {
2426 case QEMU_OPTION_M:
2427 machine = find_machine(optarg);
2428 if (!machine) {
2429 QEMUMachine *m;
2430 printf("Supported machines are:\n");
2431 for(m = first_machine; m != NULL; m = m->next) {
2432 printf("%-10s %s%s\n",
2433 m->name, m->desc,
2434 m->is_default ? " (default)" : "");
2435 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002436 if (*optarg != '?') {
2437 PANIC("Invalid machine parameter: %s",
2438 optarg);
2439 } else {
2440 QEMU_EXIT(0);
2441 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002442 }
2443 break;
2444 case QEMU_OPTION_cpu:
2445 /* hw initialization will check this */
2446 if (*optarg == '?') {
2447/* XXX: implement xxx_cpu_list for targets that still miss it */
2448#if defined(cpu_list)
2449 cpu_list(stdout, &fprintf);
2450#endif
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002451 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002452 } else {
2453 cpu_model = optarg;
2454 }
2455 break;
2456 case QEMU_OPTION_initrd:
2457 initrd_filename = optarg;
2458 break;
2459 case QEMU_OPTION_hda:
2460 if (cyls == 0)
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002461 hda_opts = drive_add(optarg, HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002462 else
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002463 hda_opts = drive_add(optarg, HD_ALIAS
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002464 ",cyls=%d,heads=%d,secs=%d%s",
2465 0, cyls, heads, secs,
2466 translation == BIOS_ATA_TRANSLATION_LBA ?
2467 ",trans=lba" :
2468 translation == BIOS_ATA_TRANSLATION_NONE ?
2469 ",trans=none" : "");
2470 break;
2471 case QEMU_OPTION_hdb:
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01002472 hdb_opts = drive_add(optarg, HD_ALIAS, 1);
2473 break;
2474
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002475 case QEMU_OPTION_hdc:
2476 case QEMU_OPTION_hdd:
2477 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
2478 break;
2479 case QEMU_OPTION_drive:
2480 drive_add(NULL, "%s", optarg);
2481 break;
2482 case QEMU_OPTION_mtdblock:
2483 drive_add(optarg, MTD_ALIAS);
2484 break;
2485 case QEMU_OPTION_sd:
2486 drive_add(optarg, SD_ALIAS);
2487 break;
2488 case QEMU_OPTION_pflash:
2489 drive_add(optarg, PFLASH_ALIAS);
2490 break;
2491 case QEMU_OPTION_snapshot:
2492 snapshot = 1;
2493 break;
2494 case QEMU_OPTION_hdachs:
2495 {
2496 const char *p;
2497 p = optarg;
2498 cyls = strtol(p, (char **)&p, 0);
2499 if (cyls < 1 || cyls > 16383)
2500 goto chs_fail;
2501 if (*p != ',')
2502 goto chs_fail;
2503 p++;
2504 heads = strtol(p, (char **)&p, 0);
2505 if (heads < 1 || heads > 16)
2506 goto chs_fail;
2507 if (*p != ',')
2508 goto chs_fail;
2509 p++;
2510 secs = strtol(p, (char **)&p, 0);
2511 if (secs < 1 || secs > 63)
2512 goto chs_fail;
2513 if (*p == ',') {
2514 p++;
2515 if (!strcmp(p, "none"))
2516 translation = BIOS_ATA_TRANSLATION_NONE;
2517 else if (!strcmp(p, "lba"))
2518 translation = BIOS_ATA_TRANSLATION_LBA;
2519 else if (!strcmp(p, "auto"))
2520 translation = BIOS_ATA_TRANSLATION_AUTO;
2521 else
2522 goto chs_fail;
2523 } else if (*p != '\0') {
2524 chs_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002525 PANIC("qemu: invalid physical CHS format");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002526 }
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002527 if (hda_opts != NULL) {
2528 char num[16];
2529 snprintf(num, sizeof(num), "%d", cyls);
2530 qemu_opt_set(hda_opts, "cyls", num);
2531 snprintf(num, sizeof(num), "%d", heads);
2532 qemu_opt_set(hda_opts, "heads", num);
2533 snprintf(num, sizeof(num), "%d", secs);
2534 qemu_opt_set(hda_opts, "secs", num);
2535 if (translation == BIOS_ATA_TRANSLATION_LBA)
2536 qemu_opt_set(hda_opts, "trans", "lba");
2537 if (translation == BIOS_ATA_TRANSLATION_NONE)
2538 qemu_opt_set(hda_opts, "trans", "none");
2539 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002540 }
2541 break;
2542 case QEMU_OPTION_numa:
2543 if (nb_numa_nodes >= MAX_NODES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002544 PANIC("qemu: too many NUMA nodes");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002545 }
2546 numa_add(optarg);
2547 break;
2548 case QEMU_OPTION_nographic:
2549 display_type = DT_NOGRAPHIC;
2550 break;
2551#ifdef CONFIG_CURSES
2552 case QEMU_OPTION_curses:
2553 display_type = DT_CURSES;
2554 break;
2555#endif
2556 case QEMU_OPTION_portrait:
2557 graphic_rotate = 1;
2558 break;
2559 case QEMU_OPTION_kernel:
2560 kernel_filename = optarg;
2561 break;
2562 case QEMU_OPTION_append:
2563 kernel_cmdline = optarg;
2564 break;
2565 case QEMU_OPTION_cdrom:
2566 drive_add(optarg, CDROM_ALIAS);
2567 break;
2568 case QEMU_OPTION_boot:
2569 boot_devices = optarg;
2570 /* We just do some generic consistency checks */
2571 {
2572 /* Could easily be extended to 64 devices if needed */
2573 const char *p;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07002574
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002575 boot_devices_bitmap = 0;
2576 for (p = boot_devices; *p != '\0'; p++) {
2577 /* Allowed boot devices are:
2578 * a b : floppy disk drives
2579 * c ... f : IDE disk drives
2580 * g ... m : machine implementation dependant drives
2581 * n ... p : network devices
2582 * It's up to each machine implementation to check
2583 * if the given boot devices match the actual hardware
2584 * implementation and firmware features.
2585 */
2586 if (*p < 'a' || *p > 'q') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002587 PANIC("Invalid boot device '%c'", *p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002588 }
2589 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002590 PANIC(
2591 "Boot device '%c' was given twice",*p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002592 }
2593 boot_devices_bitmap |= 1 << (*p - 'a');
2594 }
2595 }
2596 break;
2597 case QEMU_OPTION_fda:
2598 case QEMU_OPTION_fdb:
2599 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2600 break;
2601#ifdef TARGET_I386
2602 case QEMU_OPTION_no_fd_bootchk:
2603 fd_bootchk = 0;
2604 break;
2605#endif
2606 case QEMU_OPTION_net:
2607 if (nb_net_clients >= MAX_NET_CLIENTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002608 PANIC("qemu: too many network clients");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002609 }
2610 net_clients[nb_net_clients] = optarg;
2611 nb_net_clients++;
2612 break;
2613#ifdef CONFIG_SLIRP
2614 case QEMU_OPTION_tftp:
2615 tftp_prefix = optarg;
2616 break;
2617 case QEMU_OPTION_bootp:
2618 bootp_filename = optarg;
2619 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002620 case QEMU_OPTION_redir:
2621 net_slirp_redir(NULL, optarg, NULL);
2622 break;
2623#endif
2624 case QEMU_OPTION_bt:
2625 if (nb_bt_opts >= MAX_BT_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002626 PANIC("qemu: too many bluetooth options");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002627 }
2628 bt_opts[nb_bt_opts++] = optarg;
2629 break;
2630#ifdef HAS_AUDIO
2631 case QEMU_OPTION_audio_help:
2632 AUD_help ();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002633 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002634 break;
2635 case QEMU_OPTION_soundhw:
2636 select_soundhw (optarg);
2637 break;
2638#endif
2639 case QEMU_OPTION_h:
2640 qemu_help(0);
2641 break;
2642 case QEMU_OPTION_version:
2643 version();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002644 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002645 break;
2646 case QEMU_OPTION_m: {
2647 uint64_t value;
2648 char *ptr;
2649
2650 value = strtoul(optarg, &ptr, 10);
2651 switch (*ptr) {
2652 case 0: case 'M': case 'm':
2653 value <<= 20;
2654 break;
2655 case 'G': case 'g':
2656 value <<= 30;
2657 break;
2658 default:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002659 PANIC("qemu: invalid ram size: %s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002660 }
2661
2662 /* On 32-bit hosts, QEMU is limited by virtual address space */
David 'Digit' Turner81911b02014-02-10 17:53:45 +01002663 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002664 PANIC("qemu: at most 2047 MB RAM can be simulated");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002665 }
2666 if (value != (uint64_t)(ram_addr_t)value) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002667 PANIC("qemu: ram size too large");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002668 }
2669 ram_size = value;
2670 break;
2671 }
2672 case QEMU_OPTION_d:
David 'Digit' Turner7e9f6c12014-01-22 18:42:40 +01002673 log_mask = optarg;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002674 break;
2675 case QEMU_OPTION_s:
2676 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2677 break;
2678 case QEMU_OPTION_gdb:
2679 gdbstub_dev = optarg;
2680 break;
2681 case QEMU_OPTION_L:
2682 data_dir = optarg;
2683 break;
2684 case QEMU_OPTION_bios:
2685 bios_name = optarg;
2686 break;
2687 case QEMU_OPTION_singlestep:
2688 singlestep = 1;
2689 break;
2690 case QEMU_OPTION_S:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002691 autostart = 0;
2692 break;
2693#ifndef _WIN32
2694 case QEMU_OPTION_k:
2695 keyboard_layout = optarg;
2696 break;
2697#endif
2698 case QEMU_OPTION_localtime:
2699 rtc_utc = 0;
2700 break;
2701 case QEMU_OPTION_vga:
2702 select_vgahw (optarg);
2703 break;
2704#if defined(TARGET_PPC) || defined(TARGET_SPARC)
2705 case QEMU_OPTION_g:
2706 {
2707 const char *p;
2708 int w, h, depth;
2709 p = optarg;
2710 w = strtol(p, (char **)&p, 10);
2711 if (w <= 0) {
2712 graphic_error:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002713 PANIC("qemu: invalid resolution or depth");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002714 }
2715 if (*p != 'x')
2716 goto graphic_error;
2717 p++;
2718 h = strtol(p, (char **)&p, 10);
2719 if (h <= 0)
2720 goto graphic_error;
2721 if (*p == 'x') {
2722 p++;
2723 depth = strtol(p, (char **)&p, 10);
2724 if (depth != 8 && depth != 15 && depth != 16 &&
2725 depth != 24 && depth != 32)
2726 goto graphic_error;
2727 } else if (*p == '\0') {
2728 depth = graphic_depth;
2729 } else {
2730 goto graphic_error;
2731 }
2732
2733 graphic_width = w;
2734 graphic_height = h;
2735 graphic_depth = depth;
2736 }
2737 break;
2738#endif
2739 case QEMU_OPTION_echr:
2740 {
2741 char *r;
2742 term_escape_char = strtol(optarg, &r, 0);
2743 if (r == optarg)
2744 printf("Bad argument to echr\n");
2745 break;
2746 }
2747 case QEMU_OPTION_monitor:
2748 monitor_device = optarg;
2749 break;
2750 case QEMU_OPTION_serial:
2751 if (serial_device_index >= MAX_SERIAL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002752 PANIC("qemu: too many serial ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002753 }
2754 serial_devices[serial_device_index] = optarg;
2755 serial_device_index++;
2756 break;
2757 case QEMU_OPTION_watchdog:
2758 i = select_watchdog(optarg);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002759 if (i > 0) {
2760 if (i == 1) {
2761 PANIC("Invalid watchdog parameter: %s",
2762 optarg);
2763 } else {
2764 QEMU_EXIT(0);
2765 }
2766 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002767 break;
2768 case QEMU_OPTION_watchdog_action:
2769 if (select_watchdog_action(optarg) == -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002770 PANIC("Unknown -watchdog-action parameter");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002771 }
2772 break;
2773 case QEMU_OPTION_virtiocon:
2774 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002775 PANIC("qemu: too many virtio consoles");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002776 }
2777 virtio_consoles[virtio_console_index] = optarg;
2778 virtio_console_index++;
2779 break;
2780 case QEMU_OPTION_parallel:
2781 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002782 PANIC("qemu: too many parallel ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002783 }
2784 parallel_devices[parallel_device_index] = optarg;
2785 parallel_device_index++;
2786 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00002787 case QEMU_OPTION_loadvm:
2788 loadvm = optarg;
2789 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00002790 case QEMU_OPTION_savevm_on_exit:
2791 savevm_on_exit = optarg;
2792 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002793 case QEMU_OPTION_full_screen:
2794 full_screen = 1;
2795 break;
2796#ifdef CONFIG_SDL
2797 case QEMU_OPTION_no_frame:
2798 no_frame = 1;
2799 break;
2800 case QEMU_OPTION_alt_grab:
2801 alt_grab = 1;
2802 break;
2803 case QEMU_OPTION_no_quit:
2804 no_quit = 1;
2805 break;
2806 case QEMU_OPTION_sdl:
2807 display_type = DT_SDL;
2808 break;
2809#endif
2810 case QEMU_OPTION_pidfile:
2811 pid_file = optarg;
2812 break;
2813#ifdef TARGET_I386
2814 case QEMU_OPTION_win2k_hack:
2815 win2k_install_hack = 1;
2816 break;
2817 case QEMU_OPTION_rtc_td_hack:
2818 rtc_td_hack = 1;
2819 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08002820#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002821 case QEMU_OPTION_acpitable:
2822 if(acpi_table_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002823 PANIC("Wrong acpi table provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002824 }
2825 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08002826#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002827 case QEMU_OPTION_smbios:
David 'Digit' Turnerae3098a2011-05-11 16:01:57 +02002828 do_smbios_option(optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002829 break;
2830#endif
2831#ifdef CONFIG_KVM
2832 case QEMU_OPTION_enable_kvm:
2833 kvm_allowed = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002834 break;
David 'Digit' Turner36597752011-05-20 01:18:01 +02002835 case QEMU_OPTION_disable_kvm:
2836 kvm_allowed = 0;
2837 break;
2838#endif /* CONFIG_KVM */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002839 case QEMU_OPTION_usb:
2840 usb_enabled = 1;
2841 break;
2842 case QEMU_OPTION_usbdevice:
2843 usb_enabled = 1;
2844 if (usb_devices_index >= MAX_USB_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002845 PANIC("Too many USB devices");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002846 }
2847 usb_devices[usb_devices_index] = optarg;
2848 usb_devices_index++;
2849 break;
2850 case QEMU_OPTION_smp:
2851 smp_cpus = atoi(optarg);
2852 if (smp_cpus < 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002853 PANIC("Invalid number of CPUs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002854 }
2855 break;
2856 case QEMU_OPTION_vnc:
2857 display_type = DT_VNC;
2858 vnc_display = optarg;
2859 break;
2860#ifdef TARGET_I386
2861 case QEMU_OPTION_no_acpi:
2862 acpi_enabled = 0;
2863 break;
2864 case QEMU_OPTION_no_hpet:
2865 no_hpet = 1;
2866 break;
2867 case QEMU_OPTION_no_virtio_balloon:
2868 no_virtio_balloon = 1;
2869 break;
2870#endif
2871 case QEMU_OPTION_no_reboot:
2872 no_reboot = 1;
2873 break;
2874 case QEMU_OPTION_no_shutdown:
2875 no_shutdown = 1;
2876 break;
2877 case QEMU_OPTION_show_cursor:
2878 cursor_hide = 0;
2879 break;
2880 case QEMU_OPTION_uuid:
2881 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002882 PANIC("Fail to parse UUID string. Wrong format.");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002883 }
2884 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002885 case QEMU_OPTION_option_rom:
2886 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002887 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002888 }
2889 option_rom[nb_option_roms] = optarg;
2890 nb_option_roms++;
2891 break;
2892#if defined(TARGET_ARM) || defined(TARGET_M68K)
2893 case QEMU_OPTION_semihosting:
2894 semihosting_enabled = 1;
2895 break;
2896#endif
2897 case QEMU_OPTION_name:
2898 qemu_name = optarg;
2899 break;
2900#if defined(TARGET_SPARC) || defined(TARGET_PPC)
2901 case QEMU_OPTION_prom_env:
2902 if (nb_prom_envs >= MAX_PROM_ENVS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002903 PANIC("Too many prom variables");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002904 }
2905 prom_envs[nb_prom_envs] = optarg;
2906 nb_prom_envs++;
2907 break;
2908#endif
2909#ifdef TARGET_ARM
2910 case QEMU_OPTION_old_param:
2911 old_param = 1;
2912 break;
2913#endif
2914 case QEMU_OPTION_clock:
2915 configure_alarms(optarg);
2916 break;
2917 case QEMU_OPTION_startdate:
2918 {
2919 struct tm tm;
David 'Digit' Turnerdc468202010-10-27 02:34:46 +02002920 time_t rtc_start_date = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002921 if (!strcmp(optarg, "now")) {
2922 rtc_date_offset = -1;
2923 } else {
2924 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
2925 &tm.tm_year,
2926 &tm.tm_mon,
2927 &tm.tm_mday,
2928 &tm.tm_hour,
2929 &tm.tm_min,
2930 &tm.tm_sec) == 6) {
2931 /* OK */
2932 } else if (sscanf(optarg, "%d-%d-%d",
2933 &tm.tm_year,
2934 &tm.tm_mon,
2935 &tm.tm_mday) == 3) {
2936 tm.tm_hour = 0;
2937 tm.tm_min = 0;
2938 tm.tm_sec = 0;
2939 } else {
2940 goto date_fail;
2941 }
2942 tm.tm_year -= 1900;
2943 tm.tm_mon--;
2944 rtc_start_date = mktimegm(&tm);
2945 if (rtc_start_date == -1) {
2946 date_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002947 PANIC("Invalid date format. Valid format are:\n"
2948 "'now' or '2006-06-17T16:01:21' or '2006-06-17'");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002949 }
2950 rtc_date_offset = time(NULL) - rtc_start_date;
2951 }
2952 }
2953 break;
rich cannings7339b552011-02-16 13:43:44 -08002954
2955 /* -------------------------------------------------------*/
2956 /* User mode network stack restrictions */
2957 case QEMU_OPTION_drop_udp:
2958 slirp_drop_udp();
2959 break;
2960 case QEMU_OPTION_drop_tcp:
2961 slirp_drop_tcp();
2962 break;
2963 case QEMU_OPTION_allow_tcp:
2964 slirp_allow(optarg, IPPROTO_TCP);
2965 break;
2966 case QEMU_OPTION_allow_udp:
2967 slirp_allow(optarg, IPPROTO_UDP);
2968 break;
2969 case QEMU_OPTION_drop_log:
2970 {
2971 FILE* drop_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08002972 drop_log_filename = optarg;
2973 drop_log_fd = fopen(optarg, "w+");
rich cannings7339b552011-02-16 13:43:44 -08002974
2975 if (!drop_log_fd) {
2976 fprintf(stderr, "Cannot open drop log: %s\n", optarg);
2977 exit(1);
2978 }
2979
2980 slirp_drop_log_fd(drop_log_fd);
2981 }
2982 break;
2983
2984 case QEMU_OPTION_dns_log:
2985 {
2986 FILE* dns_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08002987 dns_log_filename = optarg;
2988 dns_log_fd = fopen(optarg, "wb+");
rich cannings7339b552011-02-16 13:43:44 -08002989
2990 if (dns_log_fd == NULL) {
2991 fprintf(stderr, "Cannot open dns log: %s\n", optarg);
2992 exit(1);
2993 }
2994
2995 slirp_dns_log_fd(dns_log_fd);
2996 }
2997 break;
2998
2999
3000 case QEMU_OPTION_max_dns_conns:
3001 {
3002 int max_dns_conns = 0;
3003 if (parse_int(optarg, &max_dns_conns)) {
3004 fprintf(stderr,
3005 "qemu: syntax: -max-dns-conns max_connections\n");
3006 exit(1);
3007 }
3008 if (max_dns_conns <= 0 || max_dns_conns == LONG_MAX) {
3009 fprintf(stderr,
3010 "Invalid arg for max dns connections: %s\n",
3011 optarg);
3012 exit(1);
3013 }
3014 slirp_set_max_dns_conns(max_dns_conns);
3015 }
3016 break;
3017
3018 case QEMU_OPTION_net_forward:
3019 net_slirp_forward(optarg);
3020 break;
3021 case QEMU_OPTION_net_forward_tcp2sink:
3022 {
3023 SockAddress saddr;
3024
3025 if (parse_host_port(&saddr, optarg)) {
3026 fprintf(stderr,
3027 "Invalid ip/port %s for "
3028 "-forward-dropped-tcp2sink. "
3029 "We expect 'sink_ip:sink_port'\n",
3030 optarg);
3031 exit(1);
3032 }
3033 slirp_forward_dropped_tcp2sink(saddr.u.inet.address,
3034 saddr.u.inet.port);
3035 }
3036 break;
3037 /* -------------------------------------------------------*/
3038
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003039 case QEMU_OPTION_tb_size:
3040 tb_size = strtol(optarg, NULL, 0);
3041 if (tb_size < 0)
3042 tb_size = 0;
3043 break;
3044 case QEMU_OPTION_icount:
David Turner6a9ef172010-09-09 22:54:36 +02003045 icount_option = optarg;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003046 break;
3047 case QEMU_OPTION_incoming:
3048 incoming = optarg;
3049 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003050#ifdef CONFIG_XEN
3051 case QEMU_OPTION_xen_domid:
3052 xen_domid = atoi(optarg);
3053 break;
3054 case QEMU_OPTION_xen_create:
3055 xen_mode = XEN_CREATE;
3056 break;
3057 case QEMU_OPTION_xen_attach:
3058 xen_mode = XEN_ATTACH;
3059 break;
3060#endif
3061
3062
3063 case QEMU_OPTION_mic:
3064 audio_input_source = (char*)optarg;
3065 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003066#ifdef CONFIG_NAND
3067 case QEMU_OPTION_nand:
3068 nand_add_dev(optarg);
3069 break;
Jun Nakajimaa381ef02011-12-17 19:13:25 -08003070
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003071#endif
Jun Nakajimaa381ef02011-12-17 19:13:25 -08003072 case QEMU_OPTION_disable_hax:
3073 hax_disabled = 1;
3074 break;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07003075 case QEMU_OPTION_android_ports:
3076 android_op_ports = (char*)optarg;
3077 break;
3078
3079 case QEMU_OPTION_android_port:
3080 android_op_port = (char*)optarg;
3081 break;
3082
3083 case QEMU_OPTION_android_report_console:
3084 android_op_report_console = (char*)optarg;
3085 break;
3086
3087 case QEMU_OPTION_http_proxy:
3088 op_http_proxy = (char*)optarg;
3089 break;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003090
3091 case QEMU_OPTION_charmap:
3092 op_charmap_file = (char*)optarg;
3093 break;
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07003094
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003095 case QEMU_OPTION_android_hw:
3096 android_op_hwini = (char*)optarg;
3097 break;
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07003098
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003099 case QEMU_OPTION_dns_server:
3100 android_op_dns_server = (char*)optarg;
3101 break;
3102
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003103 case QEMU_OPTION_radio:
3104 android_op_radio = (char*)optarg;
3105 break;
3106
3107 case QEMU_OPTION_gps:
3108 android_op_gps = (char*)optarg;
3109 break;
3110
3111 case QEMU_OPTION_audio:
3112 android_op_audio = (char*)optarg;
3113 break;
3114
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003115 case QEMU_OPTION_cpu_delay:
3116 android_op_cpu_delay = (char*)optarg;
3117 break;
3118
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07003119 case QEMU_OPTION_show_kernel:
3120 android_kmsg_init(ANDROID_KMSG_PRINT_MESSAGES);
3121 break;
3122
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003123#ifdef CONFIG_NAND_LIMITS
3124 case QEMU_OPTION_nand_limits:
3125 android_op_nand_limits = (char*)optarg;
3126 break;
3127#endif // CONFIG_NAND_LIMITS
3128
3129 case QEMU_OPTION_netspeed:
3130 android_op_netspeed = (char*)optarg;
3131 break;
3132
3133 case QEMU_OPTION_netdelay:
3134 android_op_netdelay = (char*)optarg;
3135 break;
3136
3137 case QEMU_OPTION_netfast:
3138 android_op_netfast = 1;
3139 break;
3140
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07003141 case QEMU_OPTION_tcpdump:
3142 android_op_tcpdump = (char*)optarg;
3143 break;
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003144
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003145 case QEMU_OPTION_boot_property:
3146 boot_property_parse_option((char*)optarg);
3147 break;
3148
3149 case QEMU_OPTION_lcd_density:
3150 android_op_lcd_density = (char*)optarg;
3151 break;
3152
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003153 case QEMU_OPTION_ui_port:
3154 android_op_ui_port = (char*)optarg;
3155 break;
3156
3157 case QEMU_OPTION_ui_settings:
3158 android_op_ui_settings = (char*)optarg;
3159 break;
3160
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +01003161 case QEMU_OPTION_audio_test_out:
3162 android_audio_test_start_out();
3163 break;
3164
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -08003165 case QEMU_OPTION_android_avdname:
3166 android_op_avd_name = (char*)optarg;
3167 break;
3168
3169 case QEMU_OPTION_timezone:
3170 if (timezone_set((char*)optarg)) {
3171 fprintf(stderr, "emulator: it seems the timezone '%s' is not in zoneinfo format\n",
3172 (char*)optarg);
3173 }
3174 break;
3175
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003176#ifdef CONFIG_MEMCHECK
3177 case QEMU_OPTION_android_memcheck:
3178 android_op_memcheck = (char*)optarg;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003179 /* This will set ro.kernel.memcheck system property
3180 * to memcheck's tracing flags. */
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003181 stralloc_add_format(kernel_config, " memcheck=%s", android_op_memcheck);
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003182 break;
3183#endif // CONFIG_MEMCHECK
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +01003184
3185 case QEMU_OPTION_snapshot_no_time_update:
3186 android_snapshot_update_time = 0;
3187 break;
Vladimir Chtchetkineb8dcaff2011-09-17 11:15:47 -07003188
3189 case QEMU_OPTION_list_webcam:
3190 android_list_web_cameras();
3191 exit(0);
3192
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003193 default:
3194 os_parse_cmd_args(popt->index, optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003195 }
3196 }
3197 }
3198
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003199 /* Initialize character map. */
3200 if (android_charmap_setup(op_charmap_file)) {
3201 if (op_charmap_file) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003202 PANIC(
3203 "Unable to initialize character map from file %s.",
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003204 op_charmap_file);
3205 } else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003206 PANIC(
3207 "Unable to initialize default character map.");
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003208 }
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003209 }
3210
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003211 /* If no data_dir is specified then try to find it relative to the
3212 executable path. */
3213 if (!data_dir) {
3214 data_dir = find_datadir(argv[0]);
3215 }
3216 /* If all else fails use the install patch specified when building. */
3217 if (!data_dir) {
3218 data_dir = CONFIG_QEMU_SHAREDIR;
3219 }
3220
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003221 if (!android_op_hwini) {
3222 PANIC("Missing -android-hw <file> option!");
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003223 }
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003224 hw_ini = iniFile_newFromFile(android_op_hwini);
3225 if (hw_ini == NULL) {
3226 PANIC("Could not find %s file.", android_op_hwini);
3227 }
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01003228
3229 androidHwConfig_init(android_hw, 0);
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003230 androidHwConfig_read(android_hw, hw_ini);
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01003231
Vladimir Chtchetkinedb450d72012-01-12 13:37:40 -08003232 /* If we're loading VM from a snapshot, make sure that the current HW config
3233 * matches the one with which the VM has been saved. */
3234 if (loadvm && *loadvm && !snaphost_match_configs(hw_ini, loadvm)) {
3235 exit(0);
3236 }
3237
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003238 iniFile_free(hw_ini);
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003239
David 'Digit' Turnerc6e0cae2014-03-07 23:08:30 +01003240 const char* kernelSerialDevicePrefix =
3241 androidHwConfig_getKernelSerialPrefix(android_hw);
3242 VERBOSE_PRINT(init, "Using kernel serial device prefix: %s",
3243 kernelSerialDevicePrefix);
3244
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003245 {
3246 int width = android_hw->hw_lcd_width;
3247 int height = android_hw->hw_lcd_height;
3248 int depth = android_hw->hw_lcd_depth;
3249
3250 /* A bit of sanity checking */
3251 if (width <= 0 || height <= 0 ||
3252 (depth != 16 && depth != 32) ||
3253 (((width|height) & 3) != 0) )
3254 {
3255 PANIC("Invalid display configuration (%d,%d,%d)",
3256 width, height, depth);
3257 }
3258 android_display_width = width;
3259 android_display_height = height;
3260 android_display_bpp = depth;
3261 }
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003262
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003263#ifdef CONFIG_NAND_LIMITS
3264 /* Init nand stuff. */
3265 if (android_op_nand_limits) {
3266 parse_nand_limits(android_op_nand_limits);
3267 }
3268#endif // CONFIG_NAND_LIMITS
3269
David 'Digit' Turner53eb66d2011-03-01 14:58:23 +01003270 /* Initialize AVD name from hardware configuration if needed */
3271 if (!android_op_avd_name) {
3272 if (android_hw->avd_name && *android_hw->avd_name) {
3273 android_op_avd_name = android_hw->avd_name;
3274 VERBOSE_PRINT(init,"AVD Name: %s", android_op_avd_name);
3275 }
3276 }
3277
David 'Digit' Turner890f4642014-02-24 11:53:03 +01003278 bool systemImageIsExt4 = false;
3279 bool dataImageIsExt4 = false;
3280 bool cacheImageIsExt4 = false;
3281
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003282 /* Initialize system partition image */
3283 {
3284 char tmp[PATH_MAX+32];
3285 const char* sysImage = android_hw->disk_systemPartition_path;
3286 const char* initImage = android_hw->disk_systemPartition_initPath;
3287 uint64_t sysBytes = android_hw->disk_systemPartition_size;
3288
3289 if (sysBytes == 0) {
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003290 PANIC("Invalid system partition size: %" PRIu64, sysBytes);
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003291 }
3292
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003293 snprintf(tmp,sizeof(tmp),"system,size=0x%" PRIx64, sysBytes);
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003294
David 'Digit' Turner890f4642014-02-24 11:53:03 +01003295 const char* imageFile = NULL;
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003296 if (sysImage && *sysImage) {
David 'Digit' Turner890f4642014-02-24 11:53:03 +01003297 imageFile = sysImage;
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003298 if (filelock_create(sysImage) == NULL) {
3299 fprintf(stderr,"WARNING: System image already in use, changes will not persist!\n");
3300 /* If there is no file= parameters, nand_add_dev will create
3301 * a temporary file to back the partition image. */
3302 } else {
3303 pstrcat(tmp,sizeof(tmp),",file=");
3304 pstrcat(tmp,sizeof(tmp),sysImage);
3305 }
3306 }
3307 if (initImage && *initImage) {
David 'Digit' Turner890f4642014-02-24 11:53:03 +01003308 imageFile = initImage;
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003309 if (!path_exists(initImage)) {
3310 PANIC("Invalid initial system image path: %s", initImage);
3311 }
3312 pstrcat(tmp,sizeof(tmp),",initfile=");
3313 pstrcat(tmp,sizeof(tmp),initImage);
3314 } else {
3315 PANIC("Missing initial system image path!");
3316 }
David 'Digit' Turner890f4642014-02-24 11:53:03 +01003317 systemImageIsExt4 = imageFile && android_pathIsExt4PartitionImage(imageFile);
3318 if (systemImageIsExt4) {
Deepanshu Gupta04b20f42013-09-11 12:04:27 -07003319 /* Using a nand device to approximate a block device until full
3320 * support is added */
3321 pstrcat(tmp,sizeof(tmp),",pagesize=512,extrasize=0");
3322 }
David 'Digit' Turner28f52f22014-02-26 12:17:25 +01003323 VERBOSE_PRINT(init, "System partition format: %s",
David 'Digit' Turner890f4642014-02-24 11:53:03 +01003324 systemImageIsExt4 ? "ext4" : "yaffs2");
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003325 nand_add_dev(tmp);
3326 }
3327
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003328 /* Initialize data partition image */
3329 {
3330 char tmp[PATH_MAX+32];
3331 const char* dataImage = android_hw->disk_dataPartition_path;
3332 const char* initImage = android_hw->disk_dataPartition_initPath;
3333 uint64_t dataBytes = android_hw->disk_dataPartition_size;
3334
3335 if (dataBytes == 0) {
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003336 PANIC("Invalid data partition size: %" PRIu64, dataBytes);
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003337 }
3338
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003339 snprintf(tmp,sizeof(tmp),"userdata,size=0x%" PRIx64, dataBytes);
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003340
David 'Digit' Turner890f4642014-02-24 11:53:03 +01003341 const char* imageFile = dataImage;
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003342 if (dataImage && *dataImage) {
3343 if (filelock_create(dataImage) == NULL) {
3344 fprintf(stderr, "WARNING: Data partition already in use. Changes will not persist!\n");
3345 /* Note: if there is no file= parameters, nand_add_dev() will
3346 * create a temporary file to back the partition image. */
3347 } else {
3348 /* Create the file if needed */
3349 if (!path_exists(dataImage)) {
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003350 if (path_empty_file(dataImage) < 0) {
3351 PANIC("Could not create data image file %s: %s", dataImage, strerror(errno));
3352 }
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003353 }
3354 pstrcat(tmp, sizeof(tmp), ",file=");
3355 pstrcat(tmp, sizeof(tmp), dataImage);
3356 }
3357 }
3358 if (initImage && *initImage) {
David 'Digit' Turner890f4642014-02-24 11:53:03 +01003359 imageFile = initImage;
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003360 pstrcat(tmp, sizeof(tmp), ",initfile=");
3361 pstrcat(tmp, sizeof(tmp), initImage);
3362 }
David 'Digit' Turner890f4642014-02-24 11:53:03 +01003363 dataImageIsExt4 = imageFile && android_pathIsExt4PartitionImage(imageFile);
3364 if (dataImageIsExt4) {
Deepanshu Gupta04b20f42013-09-11 12:04:27 -07003365 /* Using a nand device to approximate a block device until full
3366 * support is added */
3367 pstrcat(tmp, sizeof(tmp), ",pagesize=512,extrasize=0");
3368 }
David 'Digit' Turner28f52f22014-02-26 12:17:25 +01003369 VERBOSE_PRINT(init, "Data partition format: %s",
David 'Digit' Turner890f4642014-02-24 11:53:03 +01003370 dataImageIsExt4 ? "ext4" : "yaffs2");
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003371 nand_add_dev(tmp);
3372 }
3373
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01003374 /* Init SD-Card stuff. For Android, it is always hda */
3375 /* If the -hda option was used, ignore the Android-provided one */
3376 if (hda_opts == NULL) {
3377 const char* sdPath = android_hw->hw_sdCard_path;
3378 if (sdPath && *sdPath) {
3379 if (!path_exists(sdPath)) {
3380 fprintf(stderr, "WARNING: SD Card image is missing: %s\n", sdPath);
3381 } else if (filelock_create(sdPath) == NULL) {
3382 fprintf(stderr, "WARNING: SD Card image already in use: %s\n", sdPath);
3383 } else {
3384 /* Successful locking */
3385 hda_opts = drive_add(sdPath, HD_ALIAS, 0);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02003386 /* Set this property of any operation involving the SD Card
3387 * will be x100 slower, due to the corresponding file being
3388 * mounted as O_DIRECT. Note that this is only 'unsafe' in
3389 * the context of an emulator crash. The data is already
3390 * synced properly when the emulator exits (either normally or through ^C).
3391 */
3392 qemu_opt_set(hda_opts, "cache", "unsafe");
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01003393 }
3394 }
3395 }
3396
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003397 if (hdb_opts == NULL) {
3398 const char* spath = android_hw->disk_snapStorage_path;
3399 if (spath && *spath) {
3400 if (!path_exists(spath)) {
3401 PANIC("Snapshot storage file does not exist: %s", spath);
3402 }
3403 if (filelock_create(spath) == NULL) {
David 'Digit' Turner4297b822011-05-04 19:18:15 +02003404 PANIC("Snapshot storage already in use: %s", spath);
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003405 }
3406 hdb_opts = drive_add(spath, HD_ALIAS, 1);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02003407 /* See comment above to understand why this is needed. */
David 'Digit' Turner9fb360e2011-05-04 22:01:28 +02003408 qemu_opt_set(hdb_opts, "cache", "unsafe");
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003409 }
3410 }
3411
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003412 /* Set the VM's max heap size, passed as a boot property */
3413 if (android_hw->vm_heapSize > 0) {
3414 char tmp[64];
3415 snprintf(tmp, sizeof(tmp), "%dm", android_hw->vm_heapSize);
3416 boot_property_add("dalvik.vm.heapsize",tmp);
3417 }
3418
Siva Velusamy0faeb3a2013-10-29 14:23:35 -07003419 /* From API 19 and above, the platform provides an explicit property for low memory devices. */
3420 if (android_hw->hw_ramSize <= 512) {
3421 boot_property_add("ro.config.low_ram", "true");
3422 }
3423
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003424 /* Initialize net speed and delays stuff. */
3425 if (android_parse_network_speed(android_op_netspeed) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003426 PANIC("invalid -netspeed parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003427 android_op_netspeed);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003428 }
3429
3430 if ( android_parse_network_latency(android_op_netdelay) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003431 PANIC("invalid -netdelay parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003432 android_op_netdelay);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003433 }
3434
3435 if (android_op_netfast) {
3436 qemu_net_download_speed = 0;
3437 qemu_net_upload_speed = 0;
3438 qemu_net_min_latency = 0;
3439 qemu_net_max_latency = 0;
3440 }
3441
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003442 /* Initialize LCD density */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01003443 if (android_hw->hw_lcd_density) {
3444 long density = android_hw->hw_lcd_density;
3445 if (density <= 0) {
3446 PANIC("Invalid hw.lcd.density value: %ld", density);
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003447 }
3448 hwLcd_setBootProperty(density);
3449 }
3450
Xavier Ducrohetfa0c8e22011-10-04 10:41:26 -07003451 /* Initialize presence of hardware nav button */
3452 boot_property_add("qemu.hw.mainkeys", android_hw->hw_mainKeys ? "1" : "0");
3453
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07003454 /* Initialize TCP dump */
3455 if (android_op_tcpdump) {
3456 if (qemu_tcpdump_start(android_op_tcpdump) < 0) {
3457 fprintf(stdout, "could not start packet capture: %s\n", strerror(errno));
3458 }
3459 }
3460
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003461 /* Initialize modem */
3462 if (android_op_radio) {
3463 CharDriverState* cs = qemu_chr_open("radio", android_op_radio, NULL);
3464 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003465 PANIC("unsupported character device specification: %s\n"
3466 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003467 android_op_radio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003468 }
3469 android_qemud_set_channel( ANDROID_QEMUD_GSM, cs);
3470 } else if (android_hw->hw_gsmModem != 0 ) {
3471 if ( android_qemud_get_channel( ANDROID_QEMUD_GSM, &android_modem_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003472 PANIC("could not initialize qemud 'gsm' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003473 }
3474 }
3475
3476 /* Initialize GPS */
3477 if (android_op_gps) {
3478 CharDriverState* cs = qemu_chr_open("gps", android_op_gps, NULL);
3479 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003480 PANIC("unsupported character device specification: %s\n"
3481 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003482 android_op_gps);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003483 }
3484 android_qemud_set_channel( ANDROID_QEMUD_GPS, cs);
3485 } else if (android_hw->hw_gps != 0) {
3486 if ( android_qemud_get_channel( "gps", &android_gps_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003487 PANIC("could not initialize qemud 'gps' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003488 }
3489 }
3490
3491 /* Initialize audio. */
3492 if (android_op_audio) {
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003493 if ( !audio_check_backend_name( 0, android_op_audio ) ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003494 PANIC("'%s' is not a valid audio output backend. see -help-audio-out",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003495 android_op_audio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003496 }
David 'Digit' Turnerf816a752011-06-23 18:40:11 +02003497 setenv("QEMU_AUDIO_DRV", android_op_audio, 1);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003498 }
3499
David 'Digit' Turner5b481492011-04-11 17:37:32 +02003500 /* Initialize OpenGLES emulation */
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02003501 //android_hw_opengles_init();
David 'Digit' Turner5b481492011-04-11 17:37:32 +02003502
Vladimir Chtchetkineae0d8132011-09-13 10:48:02 -07003503 /* Initialize fake camera */
Vladimir Chtchetkine7485c292012-03-19 11:35:29 -07003504 if (strcmp(android_hw->hw_camera_back, "emulated") &&
3505 strcmp(android_hw->hw_camera_front, "emulated")) {
3506 /* Fake camera is not used for camera emulation. */
3507 boot_property_add("qemu.sf.fake_camera", "none");
Vladimir Chtchetkineae0d8132011-09-13 10:48:02 -07003508 } else {
Vladimir Chtchetkine7485c292012-03-19 11:35:29 -07003509 /* Fake camera is used for at least one camera emulation. */
3510 if (!strcmp(android_hw->hw_camera_back, "emulated") &&
3511 !strcmp(android_hw->hw_camera_front, "emulated")) {
3512 /* Fake camera is used for both, front and back camera emulation. */
3513 boot_property_add("qemu.sf.fake_camera", "both");
3514 } else if (!strcmp(android_hw->hw_camera_back, "emulated")) {
3515 boot_property_add("qemu.sf.fake_camera", "back");
3516 } else {
3517 boot_property_add("qemu.sf.fake_camera", "front");
3518 }
Vladimir Chtchetkineae0d8132011-09-13 10:48:02 -07003519 }
3520
Vladimir Chtchetkine035f8052012-05-18 12:19:32 -07003521 /* Set LCD density (if required by -qemu, and AVD is missing it. */
3522 if (android_op_lcd_density && !android_hw->hw_lcd_density) {
3523 int density;
3524 if (parse_int(android_op_lcd_density, &density) || density <= 0) {
3525 PANIC("-lcd-density : %d", density);
3526 }
3527 hwLcd_setBootProperty(density);
3528 }
3529
Vladimir Chtchetkineb8dcaff2011-09-17 11:15:47 -07003530 /* Initialize camera emulation. */
3531 android_camera_service_init();
3532
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003533 if (android_op_cpu_delay) {
3534 char* end;
3535 long delay = strtol(android_op_cpu_delay, &end, 0);
3536 if (end == NULL || *end || delay < 0 || delay > 1000 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003537 PANIC("option -cpu-delay must be an integer between 0 and 1000" );
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003538 }
3539 if (delay > 0)
3540 delay = (1000-delay);
3541
3542 qemu_cpu_delay = (int) delay;
3543 }
3544
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003545 if (android_op_dns_server) {
3546 char* x = strchr(android_op_dns_server, ',');
3547 dns_count = 0;
3548 if (x == NULL)
3549 {
3550 if ( add_dns_server( android_op_dns_server ) == 0 )
3551 dns_count = 1;
3552 }
3553 else
3554 {
3555 x = android_op_dns_server;
3556 while (*x) {
3557 char* y = strchr(x, ',');
3558
3559 if (y != NULL) {
3560 *y = 0;
3561 y++;
3562 } else {
3563 y = x + strlen(x);
3564 }
3565
3566 if (y > x && add_dns_server( x ) == 0) {
3567 dns_count += 1;
3568 }
3569 x = y;
3570 }
3571 }
3572 if (dns_count == 0)
3573 fprintf( stdout, "### WARNING: will use system default DNS server\n" );
3574 }
3575
3576 if (dns_count == 0)
3577 dns_count = slirp_get_system_dns_servers();
3578 if (dns_count) {
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003579 stralloc_add_format(kernel_config, " ndns=%d", dns_count);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003580 }
3581
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003582#ifdef CONFIG_MEMCHECK
3583 if (android_op_memcheck) {
3584 memcheck_init(android_op_memcheck);
3585 }
3586#endif // CONFIG_MEMCHECK
3587
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003588 /* Initialize cache partition, if any */
3589 if (android_hw->disk_cachePartition != 0) {
3590 char tmp[PATH_MAX+32];
3591 const char* partPath = android_hw->disk_cachePartition_path;
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003592 uint64_t partSize = android_hw->disk_cachePartition_size;
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003593
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003594 snprintf(tmp,sizeof(tmp),"cache,size=0x%" PRIx64, partSize);
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003595
David 'Digit' Turner890f4642014-02-24 11:53:03 +01003596 // NOTE: Assume the /cache and /data partitions have the same format
3597 cacheImageIsExt4 = dataImageIsExt4;
3598
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003599 if (partPath && *partPath && strcmp(partPath, "<temp>") != 0) {
3600 if (filelock_create(partPath) == NULL) {
3601 fprintf(stderr, "WARNING: Cache partition already in use. Changes will not persist!\n");
3602 /* Note: if there is no file= parameters, nand_add_dev() will
3603 * create a temporary file to back the partition image. */
3604 } else {
3605 /* Create the file if needed */
3606 if (!path_exists(partPath)) {
David 'Digit' Turner890f4642014-02-24 11:53:03 +01003607 // TODO(digit): For EXT4, create a real empty ext4 partition image.
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003608 if (path_empty_file(partPath) < 0) {
3609 PANIC("Could not create cache image file %s: %s", partPath, strerror(errno));
3610 }
3611 }
3612 pstrcat(tmp, sizeof(tmp), ",file=");
3613 pstrcat(tmp, sizeof(tmp), partPath);
3614 }
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003615 }
David 'Digit' Turner890f4642014-02-24 11:53:03 +01003616 if (cacheImageIsExt4) {
Deepanshu Gupta04b20f42013-09-11 12:04:27 -07003617 /* Using a nand device to approximate a block device until full
3618 * support is added */
3619 pstrcat(tmp, sizeof(tmp), ",pagesize=512,extrasize=0");
3620 }
David 'Digit' Turner28f52f22014-02-26 12:17:25 +01003621 VERBOSE_PRINT(init, "Cache partition format: %s",
David 'Digit' Turner890f4642014-02-24 11:53:03 +01003622 cacheImageIsExt4 ? "ext4" : "yaffs2");
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003623 nand_add_dev(tmp);
3624 }
3625
Jesse Hall733fffa2012-04-26 11:07:32 -07003626 /* qemu.gles will be read by the OpenGL ES emulation libraries.
3627 * If set to 0, the software GL ES renderer will be used as a fallback.
3628 * If the parameter is undefined, this means the system image runs
3629 * inside an emulator that doesn't support GPU emulation at all.
3630 *
3631 * We always start the GL ES renderer so we can gather stats on the
3632 * underlying GL implementation. If GL ES acceleration is disabled,
3633 * we just shut it down again once we have the strings. */
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003634 {
Jesse Hall733fffa2012-04-26 11:07:32 -07003635 int qemu_gles = 0;
Jesse Hall733fffa2012-04-26 11:07:32 -07003636 if (android_initOpenglesEmulation() == 0 &&
Jesse Hallba5c1f62012-05-08 15:44:35 -07003637 android_startOpenglesRenderer(android_hw->hw_lcd_width, android_hw->hw_lcd_height) == 0)
Jesse Hall733fffa2012-04-26 11:07:32 -07003638 {
3639 android_getOpenglesHardwareStrings(
3640 android_gl_vendor, sizeof(android_gl_vendor),
3641 android_gl_renderer, sizeof(android_gl_renderer),
3642 android_gl_version, sizeof(android_gl_version));
3643 if (android_hw->hw_gpu_enabled) {
3644 qemu_gles = 1;
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003645 } else {
Jesse Hall733fffa2012-04-26 11:07:32 -07003646 android_stopOpenglesRenderer();
3647 qemu_gles = 0;
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003648 }
Jesse Hall733fffa2012-04-26 11:07:32 -07003649 } else {
3650 dwarning("Could not initialize OpenglES emulation, using software renderer.");
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003651 }
Jesse Hall733fffa2012-04-26 11:07:32 -07003652 if (qemu_gles) {
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003653 stralloc_add_str(kernel_params, " qemu.gles=1");
3654 } else {
3655 stralloc_add_str(kernel_params, " qemu.gles=0");
3656 }
3657 }
3658
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003659 /* We always force qemu=1 when running inside QEMU */
3660 stralloc_add_str(kernel_params, " qemu=1");
3661
3662 /* We always initialize the first serial port for the android-kmsg
3663 * character device (used to send kernel messages) */
3664 serial_hds_add_at(0, "android-kmsg");
David 'Digit' Turnerc6e0cae2014-03-07 23:08:30 +01003665 stralloc_add_format(kernel_params,
3666 " console=%s0",
3667 kernelSerialDevicePrefix);
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003668
3669 /* We always initialize the second serial port for the android-qemud
3670 * character device as well */
3671 serial_hds_add_at(1, "android-qemud");
David 'Digit' Turnerc6e0cae2014-03-07 23:08:30 +01003672 stralloc_add_format(kernel_params,
3673 " android.qemud=%s1",
3674 kernelSerialDevicePrefix);
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003675
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003676 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3677 os_pidfile_error();
3678 exit(1);
3679 }
3680
David 'Digit' Turner7e9f6c12014-01-22 18:42:40 +01003681 /* Open the logfile at this point, if necessary. We can't open the logfile
3682 * when encountering either of the logging options (-d or -D) because the
3683 * other one may be encountered later on the command line, changing the
3684 * location or level of logging.
3685 */
3686 if (log_mask) {
3687 int mask;
3688 if (log_file) {
3689 qemu_set_log_filename(log_file);
3690 }
3691
3692 mask = qemu_str_to_log_mask(log_mask);
3693 if (!mask) {
3694 qemu_print_log_usage(stdout);
3695 exit(1);
3696 }
3697 qemu_set_log(mask);
3698 }
David 'Digit' Turnerc0052462014-02-25 18:39:29 +01003699
David 'Digit' Turner36597752011-05-20 01:18:01 +02003700#if defined(CONFIG_KVM)
3701 if (kvm_allowed < 0) {
3702 kvm_allowed = kvm_check_allowed();
3703 }
3704#endif
3705
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003706 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3707 if (smp_cpus > machine->max_cpus) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003708 PANIC("Number of SMP cpus requested (%d), exceeds max cpus "
3709 "supported by machine `%s' (%d)", smp_cpus, machine->name,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003710 machine->max_cpus);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003711 }
3712
3713 if (display_type == DT_NOGRAPHIC) {
3714 if (serial_device_index == 0)
3715 serial_devices[0] = "stdio";
3716 if (parallel_device_index == 0)
3717 parallel_devices[0] = "null";
3718 if (strncmp(monitor_device, "vc", 2) == 0)
3719 monitor_device = "stdio";
3720 }
3721
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003722 if (qemu_init_main_loop()) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003723 PANIC("qemu_init_main_loop failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003724 }
David 'Digit' Turner0b019492011-03-01 14:02:42 +01003725
3726 if (kernel_filename == NULL) {
3727 kernel_filename = android_hw->kernel_path;
3728 }
3729 if (initrd_filename == NULL) {
3730 initrd_filename = android_hw->disk_ramdisk_path;
3731 }
3732
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003733 linux_boot = (kernel_filename != NULL);
3734 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
3735
3736 if (!linux_boot && *kernel_cmdline != '\0') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003737 PANIC("-append only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003738 }
3739
3740 if (!linux_boot && initrd_filename != NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003741 PANIC("-initrd only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003742 }
3743
3744 /* boot to floppy or the default cd if no hard disk defined yet */
3745 if (!boot_devices[0]) {
3746 boot_devices = "cad";
3747 }
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003748 os_set_line_buffering();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003749
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003750 if (init_timer_alarm() < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003751 PANIC("could not initialize alarm timer");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003752 }
David Turner6a9ef172010-09-09 22:54:36 +02003753 configure_icount(icount_option);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003754
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003755 /* init network clients */
3756 if (nb_net_clients == 0) {
3757 /* if no clients, we use a default config */
3758 net_clients[nb_net_clients++] = "nic";
3759#ifdef CONFIG_SLIRP
3760 net_clients[nb_net_clients++] = "user";
3761#endif
3762 }
3763
3764 for(i = 0;i < nb_net_clients; i++) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003765 if (net_client_parse(net_clients[i]) < 0) {
3766 PANIC("Unable to parse net clients");
3767 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003768 }
3769 net_client_check();
3770
3771#ifdef TARGET_I386
3772 /* XXX: this should be moved in the PC machine instantiation code */
3773 if (net_boot != 0) {
3774 int netroms = 0;
3775 for (i = 0; i < nb_nics && i < 4; i++) {
3776 const char *model = nd_table[i].model;
3777 char buf[1024];
3778 char *filename;
3779 if (net_boot & (1 << i)) {
3780 if (model == NULL)
3781 model = "ne2k_pci";
3782 snprintf(buf, sizeof(buf), "pxe-%s.bin", model);
3783 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, buf);
3784 if (filename && get_image_size(filename) > 0) {
3785 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003786 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003787 }
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01003788 option_rom[nb_option_roms] = g_strdup(buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003789 nb_option_roms++;
3790 netroms++;
3791 }
3792 if (filename) {
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01003793 g_free(filename);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003794 }
3795 }
3796 }
3797 if (netroms == 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003798 PANIC("No valid PXE rom found for network device");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003799 }
3800 }
3801#endif
3802
3803 /* init the bluetooth world */
3804 for (i = 0; i < nb_bt_opts; i++)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003805 if (bt_parse(bt_opts[i])) {
3806 PANIC("Unable to parse bluetooth options");
3807 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003808
3809 /* init the memory */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01003810 if (ram_size == 0) {
3811 ram_size = android_hw->hw_ramSize * 1024LL * 1024;
3812 if (ram_size == 0) {
3813 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3814 }
3815 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003816
Vladimir Chtchetkinecf289fb2012-05-14 08:04:29 -07003817 /* Quite often (especially on older XP machines) attempts to allocate large
3818 * VM RAM is going to fail, and crash the emulator. Since it's failing deep
3819 * inside QEMU, it's not really possible to provide the user with a
3820 * meaningful explanation for the crash. So, lets see if QEMU is going to be
3821 * able to allocate requested amount of RAM, and if not, lets try to come up
3822 * with a recomendation. */
3823 {
3824 ram_addr_t r_ram = ram_size;
3825 void* alloc_check = malloc(r_ram);
3826 while (alloc_check == NULL && r_ram > 1024 * 1024) {
3827 /* Make it 25% less */
3828 r_ram -= r_ram / 4;
3829 alloc_check = malloc(r_ram);
3830 }
3831 if (alloc_check != NULL) {
3832 free(alloc_check);
3833 }
3834 if (r_ram != ram_size) {
3835 /* Requested RAM is too large. Report this, as well as calculated
3836 * recomendation. */
3837 dwarning("Requested RAM size of %dMB is too large for your environment, and is reduced to %dMB.",
3838 (int)(ram_size / 1024 / 1024), (int)(r_ram / 1024 / 1024));
3839 ram_size = r_ram;
3840 }
3841 }
3842
rich canningsd952f282011-03-01 15:40:09 -08003843#ifndef _WIN32
David 'Digit' Turnerf9077a82014-02-10 23:10:47 +01003844 qemu_log_rotation_init();
rich canningsd952f282011-03-01 15:40:09 -08003845#endif
3846
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003847 /* init the dynamic translator */
3848 cpu_exec_init_all(tb_size * 1024 * 1024);
3849
3850 bdrv_init();
3851
3852 /* we always create the cdrom drive, even if no disk is there */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003853#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003854 if (nb_drives_opt < MAX_DRIVES)
3855 drive_add(NULL, CDROM_ALIAS);
3856
3857 /* we always create at least one floppy */
3858
3859 if (nb_drives_opt < MAX_DRIVES)
3860 drive_add(NULL, FD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003861 /* we always create one sd slot, even if no card is in it */
3862
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003863 if (1) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003864 drive_add(NULL, SD_ALIAS);
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003865 }
3866#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003867
3868 /* open the virtual block devices */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003869 if (snapshot)
3870 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3871 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3872 exit(1);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003873
David 'Digit' Turner5cb5c0b2014-02-17 16:04:03 +01003874 //register_savevm(NULL, "timer", 0, 2, timer_save, timer_load, &timers_state);
3875
3876 SaveVMHandlers* ops = g_malloc0(sizeof(*ops));
3877 ops->save_live_state = ram_save_live;
3878 ops->load_state = ram_load;
3879
3880 register_savevm_live(NULL,
3881 "ram",
3882 0,
3883 3,
3884 ops,
3885 NULL);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003886
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003887 /* must be after terminal init, SDL library changes signal handlers */
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003888 os_setup_signal_handling();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003889
3890 /* Maintain compatibility with multiple stdio monitors */
3891 if (!strcmp(monitor_device,"stdio")) {
3892 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
3893 const char *devname = serial_devices[i];
3894 if (devname && !strcmp(devname,"mon:stdio")) {
3895 monitor_device = NULL;
3896 break;
3897 } else if (devname && !strcmp(devname,"stdio")) {
3898 monitor_device = NULL;
3899 serial_devices[i] = "mon:stdio";
3900 break;
3901 }
3902 }
3903 }
3904
3905 if (nb_numa_nodes > 0) {
3906 int i;
3907
3908 if (nb_numa_nodes > smp_cpus) {
3909 nb_numa_nodes = smp_cpus;
3910 }
3911
3912 /* If no memory size if given for any node, assume the default case
3913 * and distribute the available memory equally across all nodes
3914 */
3915 for (i = 0; i < nb_numa_nodes; i++) {
3916 if (node_mem[i] != 0)
3917 break;
3918 }
3919 if (i == nb_numa_nodes) {
3920 uint64_t usedmem = 0;
3921
3922 /* On Linux, the each node's border has to be 8MB aligned,
3923 * the final node gets the rest.
3924 */
3925 for (i = 0; i < nb_numa_nodes - 1; i++) {
3926 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3927 usedmem += node_mem[i];
3928 }
3929 node_mem[i] = ram_size - usedmem;
3930 }
3931
3932 for (i = 0; i < nb_numa_nodes; i++) {
3933 if (node_cpumask[i] != 0)
3934 break;
3935 }
3936 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3937 * must cope with this anyway, because there are BIOSes out there in
3938 * real machines which also use this scheme.
3939 */
3940 if (i == nb_numa_nodes) {
3941 for (i = 0; i < smp_cpus; i++) {
3942 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3943 }
3944 }
3945 }
3946
3947 if (kvm_enabled()) {
3948 int ret;
3949
3950 ret = kvm_init(smp_cpus);
3951 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003952 PANIC("failed to initialize KVM");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003953 }
3954 }
3955
Jun Nakajimaa381ef02011-12-17 19:13:25 -08003956#ifdef CONFIG_HAX
3957 if (!hax_disabled)
3958 {
3959 int ret;
3960
Jiang, Yunhong4a5a0ef2012-02-23 06:31:12 +08003961 hax_set_ramsize(ram_size);
Jun Nakajimaa381ef02011-12-17 19:13:25 -08003962 ret = hax_init(smp_cpus);
3963 fprintf(stderr, "HAX is %s and emulator runs in %s mode\n",
3964 !ret ? "working" :"not working", !ret ? "fast virt" : "emulation");
3965 }
3966#endif
3967
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003968 if (monitor_device) {
3969 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
3970 if (!monitor_hd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003971 PANIC("qemu: could not open monitor device '%s'",
3972 monitor_device);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003973 }
3974 }
3975
3976 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003977 serial_hds_add(serial_devices[i]);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003978 }
3979
3980 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
3981 const char *devname = parallel_devices[i];
3982 if (devname && strcmp(devname, "none")) {
3983 char label[32];
3984 snprintf(label, sizeof(label), "parallel%d", i);
3985 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
3986 if (!parallel_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003987 PANIC("qemu: could not open parallel device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003988 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003989 }
3990 }
3991 }
3992
3993 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
3994 const char *devname = virtio_consoles[i];
3995 if (devname && strcmp(devname, "none")) {
3996 char label[32];
3997 snprintf(label, sizeof(label), "virtcon%d", i);
3998 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
3999 if (!virtcon_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004000 PANIC("qemu: could not open virtio console '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004001 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004002 }
4003 }
4004 }
4005
4006 module_call_init(MODULE_INIT_DEVICE);
4007
4008
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01004009 /* Check the CPU Architecture value */
4010#if defined(TARGET_ARM)
4011 if (strcmp(android_hw->hw_cpu_arch,"arm") != 0) {
4012 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'arm'\n",
4013 android_hw->hw_cpu_arch);
4014 exit(1);
4015 }
Vladimir Chtchetkine33f89d02011-09-28 09:19:09 -07004016#elif defined(TARGET_I386)
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01004017 if (strcmp(android_hw->hw_cpu_arch,"x86") != 0) {
4018 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'x86'\n",
4019 android_hw->hw_cpu_arch);
4020 exit(1);
4021 }
4022#endif
4023
4024 /* Grab CPU model if provided in hardware.ini */
4025 if ( !cpu_model
4026 && android_hw->hw_cpu_model
4027 && android_hw->hw_cpu_model[0] != '\0')
4028 {
4029 cpu_model = android_hw->hw_cpu_model;
4030 }
4031
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004032 /* Combine kernel command line passed from the UI with parameters
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004033 * collected during initialization.
4034 *
4035 * The order is the following:
4036 * - parameters from the hw configuration (kernel.parameters)
4037 * - additionnal parameters from options (e.g. -memcheck)
4038 * - the -append parameters.
4039 */
4040 {
4041 const char* kernel_parameters;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004042
David 'Digit' Turner0b019492011-03-01 14:02:42 +01004043 if (android_hw->kernel_parameters) {
David 'Digit' Turner62c7ae52011-03-08 15:46:53 +01004044 stralloc_add_c(kernel_params, ' ');
David 'Digit' Turner0b019492011-03-01 14:02:42 +01004045 stralloc_add_str(kernel_params, android_hw->kernel_parameters);
4046 }
4047
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004048 /* If not empty, kernel_config always contains a leading space */
4049 stralloc_append(kernel_params, kernel_config);
4050
4051 if (*kernel_cmdline) {
4052 stralloc_add_c(kernel_params, ' ');
4053 stralloc_add_str(kernel_params, kernel_cmdline);
4054 }
4055
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01004056 /* Remove any leading/trailing spaces */
4057 stralloc_strip(kernel_params);
4058
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004059 kernel_parameters = stralloc_cstr(kernel_params);
4060 VERBOSE_PRINT(init, "Kernel parameters: %s", kernel_parameters);
4061
4062 machine->init(ram_size,
4063 boot_devices,
4064 kernel_filename,
4065 kernel_parameters,
4066 initrd_filename,
4067 cpu_model);
4068
Vladimir Chtchetkine8dd31e82012-02-15 17:16:04 -08004069 /* Initialize multi-touch emulation. */
Vladimir Chtchetkine863d1012012-03-16 12:25:23 -07004070 if (androidHwConfig_isScreenMultiTouch(android_hw)) {
Vladimir Chtchetkine8dd31e82012-02-15 17:16:04 -08004071 mts_port_create(NULL);
4072 }
4073
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004074 stralloc_reset(kernel_params);
4075 stralloc_reset(kernel_config);
4076 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004077
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004078 for (env = first_cpu; env != NULL; env = env->next_cpu) {
4079 for (i = 0; i < nb_numa_nodes; i++) {
4080 if (node_cpumask[i] & (1 << env->cpu_index)) {
4081 env->numa_node = i;
4082 }
4083 }
4084 }
4085
4086 current_machine = machine;
4087
David 'Digit' Turnere2678e12014-01-16 15:56:43 +01004088 /* Set KVM's vcpu state to qemu's initial CPUOldState. */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004089 if (kvm_enabled()) {
4090 int ret;
4091
4092 ret = kvm_sync_vcpus();
4093 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004094 PANIC("failed to initialize vcpus");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004095 }
4096 }
4097
Jun Nakajimaa381ef02011-12-17 19:13:25 -08004098#ifdef CONFIG_HAX
4099 if (hax_enabled())
4100 hax_sync_vcpus();
4101#endif
4102
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004103 /* init USB devices */
4104 if (usb_enabled) {
4105 for(i = 0; i < usb_devices_index; i++) {
4106 if (usb_device_add(usb_devices[i], 0) < 0) {
4107 fprintf(stderr, "Warning: could not add USB device %s\n",
4108 usb_devices[i]);
4109 }
4110 }
4111 }
4112
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08004113 /* just use the first displaystate for the moment */
David 'Digit' Turner94702b02011-01-20 02:46:33 +01004114 ds = get_displaystate();
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08004115
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004116 /* Initialize display from the command line parameters. */
4117 android_display_reset(ds,
4118 android_display_width,
4119 android_display_height,
4120 android_display_bpp);
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07004121
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004122 if (display_type == DT_DEFAULT) {
4123#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4124 display_type = DT_SDL;
4125#else
4126 display_type = DT_VNC;
4127 vnc_display = "localhost:0,to=99";
4128 show_vnc_port = 1;
4129#endif
4130 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004131
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004132
4133 switch (display_type) {
4134 case DT_NOGRAPHIC:
4135 break;
4136#if defined(CONFIG_CURSES)
4137 case DT_CURSES:
4138 curses_display_init(ds, full_screen);
4139 break;
4140#endif
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07004141#if defined(CONFIG_SDL) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004142 case DT_SDL:
4143 sdl_display_init(ds, full_screen, no_frame);
4144 break;
4145#elif defined(CONFIG_COCOA)
4146 case DT_SDL:
4147 cocoa_display_init(ds, full_screen);
4148 break;
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08004149#elif defined(CONFIG_STANDALONE_CORE)
4150 case DT_SDL:
Vladimir Chtchetkinee95660a2010-12-20 08:28:03 -08004151 coredisplay_init(ds);
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08004152 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004153#endif
4154 case DT_VNC:
4155 vnc_display_init(ds);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004156 if (vnc_display_open(ds, vnc_display) < 0) {
4157 PANIC("Unable to initialize VNC display");
4158 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004159
4160 if (show_vnc_port) {
4161 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
4162 }
4163 break;
4164 default:
4165 break;
4166 }
4167 dpy_resize(ds);
4168
4169 dcl = ds->listeners;
4170 while (dcl != NULL) {
4171 if (dcl->dpy_refresh != NULL) {
David 'Digit' Turnerdcda9492014-02-16 15:13:55 +01004172 ds->gui_timer = timer_new(QEMU_CLOCK_REALTIME, SCALE_MS, gui_update, ds);
4173 timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004174 }
4175 dcl = dcl->next;
4176 }
4177
4178 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
David 'Digit' Turnerdcda9492014-02-16 15:13:55 +01004179 nographic_timer = timer_new(QEMU_CLOCK_REALTIME, SCALE_MS, nographic_update, NULL);
4180 timer_mod(nographic_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004181 }
4182
David 'Digit' Turner94702b02011-01-20 02:46:33 +01004183 text_consoles_set_display(ds);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004184 qemu_chr_initial_reset();
4185
4186 if (monitor_device && monitor_hd)
4187 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
4188
4189 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
4190 const char *devname = serial_devices[i];
4191 if (devname && strcmp(devname, "none")) {
4192 if (strstart(devname, "vc", 0))
4193 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
4194 }
4195 }
4196
4197 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
4198 const char *devname = parallel_devices[i];
4199 if (devname && strcmp(devname, "none")) {
4200 if (strstart(devname, "vc", 0))
4201 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
4202 }
4203 }
4204
4205 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
4206 const char *devname = virtio_consoles[i];
4207 if (virtcon_hds[i] && devname) {
4208 if (strstart(devname, "vc", 0))
4209 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
4210 }
4211 }
4212
4213 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004214 PANIC("qemu: could not open gdbserver on device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004215 gdbstub_dev);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004216 }
4217
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004218 /* call android-specific setup function */
4219 android_emulation_setup();
4220
Vladimir Chtchetkine57584042011-01-20 16:15:30 -08004221#if !defined(CONFIG_STANDALONE_CORE)
4222 // For the standalone emulator (UI+core in one executable) we need to
4223 // set the window title here.
4224 android_emulator_set_base_port(android_base_port);
4225#endif
4226
Ot ten Thije871da2a2010-09-20 10:29:22 +01004227 if (loadvm)
4228 do_loadvm(cur_mon, loadvm);
4229
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004230 if (incoming) {
4231 autostart = 0; /* fixme how to deal with -daemonize */
4232 qemu_start_incoming_migration(incoming);
4233 }
4234
4235 if (autostart)
4236 vm_start();
4237
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02004238 os_setup_post();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004239
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004240#ifdef CONFIG_ANDROID
4241 // This will notify the UI that the core is successfuly initialized
4242 android_core_init_completed();
4243#endif // CONFIG_ANDROID
4244
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004245 main_loop();
4246 quit_timers();
4247 net_cleanup();
4248 android_emulation_teardown();
4249 return 0;
4250}
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07004251
4252void
4253android_emulation_teardown(void)
4254{
4255 android_charmap_done();
4256}