blob: d7247d22b779f876e3e3100268baed77814834d9 [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' Turner5d8f37a2009-09-14 14:32:27 -070038#include "hw/baum.h"
David 'Digit' Turnerf0665422013-12-17 10:47:09 +010039#include "hw/android/goldfish/nand.h"
David 'Digit' Turnercc330d42013-12-14 23:26:42 +010040#include "net/net.h"
David 'Digit' Turner1c31e3e2013-12-14 20:07:17 +010041#include "ui/console.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +010042#include "sysemu/sysemu.h"
David 'Digit' Turner852088c2013-12-14 23:04:12 +010043#include "exec/gdbstub.h"
David 'Digit' Turner7e9f6c12014-01-22 18:42:40 +010044#include "qemu/log.h"
David 'Digit' Turner7a78db72013-12-14 11:46:01 +010045#include "qemu/timer.h"
David 'Digit' Turnere7216d82013-12-15 00:51:13 +010046#include "sysemu/char.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +010047#include "sysemu/blockdev.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070048#include "audio/audio.h"
49
David 'Digit' Turner28a09b62013-12-15 00:16:00 +010050#include "migration/qemu-file.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070051#include "android/android.h"
David 'Digit' Turnere1e03df2013-12-15 00:42:21 +010052#include "android/charpipe.h"
David 'Digit' Turnerf9077a82014-02-10 23:10:47 +010053#include "android/log-rotate.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070054#include "modem_driver.h"
55#include "android/gps.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070056#include "android/hw-kmsg.h"
David 'Digit' Turnerc0ac7332011-05-02 15:05:35 +020057#include "android/hw-pipe-net.h"
58#include "android/hw-qemud.h"
Vladimir Chtchetkinec646f5e2011-09-03 15:17:13 -070059#include "android/camera/camera-service.h"
Vladimir Chtchetkine8dd31e82012-02-15 17:16:04 -080060#include "android/multitouch-port.h"
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -070061#include "android/charmap.h"
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -070062#include "android/globals.h"
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -070063#include "android/utils/bufprint.h"
David 'Digit' Turner5f824112011-03-01 14:00:26 +010064#include "android/utils/debug.h"
David 'Digit' Turner48a3c662011-03-01 14:03:20 +010065#include "android/utils/filelock.h"
66#include "android/utils/path.h"
David 'Digit' Turner5f824112011-03-01 14:00:26 +010067#include "android/utils/stralloc.h"
David 'Digit' Turner40841b22011-03-01 14:04:00 +010068#include "android/utils/tempfile.h"
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -080069#include "android/display-core.h"
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -080070#include "android/utils/timezone.h"
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +010071#include "android/snapshot.h"
David 'Digit' Turnercb88e792011-08-26 01:35:14 +020072#include "android/opengles.h"
Vladimir Chtchetkine1a820e92012-04-11 13:22:48 -070073#include "android/multitouch-screen.h"
David 'Digit' Turnerbcde1092014-01-09 23:19:19 +010074#include "exec/hwaddr.h"
David 'Digit' Turner6e2eb782013-12-15 00:54:21 +010075#include "android/tcpdump.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070076
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -070077#ifdef CONFIG_MEMCHECK
78#include "memcheck/memcheck.h"
79#endif // CONFIG_MEMCHECK
80
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070081#include <unistd.h>
82#include <fcntl.h>
83#include <signal.h>
84#include <time.h>
85#include <errno.h>
86#include <sys/time.h>
87#include <zlib.h>
88
David 'Digit' Turner2c538c82010-05-10 16:48:20 -070089/* Needed early for CONFIG_BSD etc. */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070090#include "config-host.h"
91
92#ifndef _WIN32
93#include <libgen.h>
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070094#include <sys/times.h>
95#include <sys/wait.h>
96#include <termios.h>
97#include <sys/mman.h>
98#include <sys/ioctl.h>
99#include <sys/resource.h>
100#include <sys/socket.h>
101#include <netinet/in.h>
102#include <net/if.h>
103#if defined(__NetBSD__)
104#include <net/if_tap.h>
105#endif
106#ifdef __linux__
107#include <linux/if_tun.h>
108#endif
109#include <arpa/inet.h>
110#include <dirent.h>
111#include <netdb.h>
112#include <sys/select.h>
David 'Digit' Turner2c538c82010-05-10 16:48:20 -0700113#ifdef CONFIG_BSD
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700114#include <sys/stat.h>
115#if defined(__FreeBSD__) || defined(__DragonFly__)
116#include <libutil.h>
117#else
118#include <util.h>
119#endif
120#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
121#include <freebsd/stdlib.h>
122#else
123#ifdef __linux__
124#include <pty.h>
125#include <malloc.h>
126#include <linux/rtc.h>
127
128/* For the benefit of older linux systems which don't supply it,
129 we use a local copy of hpet.h. */
130/* #include <linux/hpet.h> */
David 'Digit' Turner2dbdad52013-12-17 09:24:18 +0100131#include "hw/timer/hpet.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700132
133#include <linux/ppdev.h>
134#include <linux/parport.h>
135#endif
136#ifdef __sun__
137#include <sys/stat.h>
138#include <sys/ethernet.h>
139#include <sys/sockio.h>
140#include <netinet/arp.h>
141#include <netinet/in.h>
142#include <netinet/in_systm.h>
143#include <netinet/ip.h>
144#include <netinet/ip_icmp.h> // must come after ip.h
145#include <netinet/udp.h>
146#include <netinet/tcp.h>
147#include <net/if.h>
148#include <syslog.h>
149#include <stropts.h>
150#endif
151#endif
152#endif
153
154#if defined(__OpenBSD__)
155#include <util.h>
156#endif
157
158#if defined(CONFIG_VDE)
159#include <libvdeplug.h>
160#endif
161
162#ifdef _WIN32
163#include <windows.h>
164#include <malloc.h>
165#include <sys/timeb.h>
166#include <mmsystem.h>
167#define getopt_long_only getopt_long
168#define memalign(align, size) malloc(size)
169#endif
170
David 'Digit' Turner11822842013-12-17 09:16:47 +0100171#include "sysemu/cpus.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100172#include "sysemu/arch_init.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700173
174#ifdef CONFIG_COCOA
David 'Digit' Turnerae3098a2011-05-11 16:01:57 +0200175int qemu_main(int argc, char **argv, char **envp);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700176#undef main
177#define main qemu_main
178#endif /* CONFIG_COCOA */
179
180#include "hw/hw.h"
181#include "hw/boards.h"
182#include "hw/usb.h"
183#include "hw/pcmcia.h"
David 'Digit' Turner2ec695a2013-12-17 10:03:39 +0100184#include "hw/i386/pc.h"
185#include "hw/isa/isa.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700186#include "hw/baum.h"
187#include "hw/bt.h"
David 'Digit' Turner7821d632013-12-17 11:27:50 +0100188#include "sysemu/watchdog.h"
David 'Digit' Turner73a93a12013-12-17 11:26:01 +0100189#include "hw/i386/smbios.h"
David 'Digit' Turner50319182013-12-17 11:28:54 +0100190#include "hw/xen/xen.h"
David 'Digit' Turner6800f0a2013-12-17 09:12:54 +0100191#include "sysemu/bt.h"
David 'Digit' Turnercc330d42013-12-14 23:26:42 +0100192#include "net/net.h"
David 'Digit' Turner6af67652013-12-14 23:49:32 +0100193#include "monitor/monitor.h"
David 'Digit' Turner1c31e3e2013-12-14 20:07:17 +0100194#include "ui/console.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100195#include "sysemu/sysemu.h"
David 'Digit' Turner852088c2013-12-14 23:04:12 +0100196#include "exec/gdbstub.h"
David 'Digit' Turner7a78db72013-12-14 11:46:01 +0100197#include "qemu/timer.h"
David 'Digit' Turnere7216d82013-12-15 00:51:13 +0100198#include "sysemu/char.h"
David 'Digit' Turner37dc41a2013-12-14 14:45:51 +0100199#include "qemu/cache-utils.h"
David 'Digit' Turnere1e03df2013-12-15 00:42:21 +0100200#include "block/block.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100201#include "sysemu/dma.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700202#include "audio/audio.h"
David 'Digit' Turner28a09b62013-12-15 00:16:00 +0100203#include "migration/migration.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100204#include "sysemu/kvm.h"
David 'Digit' Turnere1e03df2013-12-15 00:42:21 +0100205#include "exec/hax.h"
David 'Digit' Turner36597752011-05-20 01:18:01 +0200206#ifdef CONFIG_KVM
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100207#include "android/kvm.h"
David 'Digit' Turner36597752011-05-20 01:18:01 +0200208#endif
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100209#include "sysemu/balloon.h"
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700210#include "android/hw-lcd.h"
211#include "android/boot-properties.h"
David 'Digit' Turnerca950592011-04-27 12:26:15 +0200212#include "android/hw-control.h"
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700213#include "android/core-init-utils.h"
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +0100214#include "android/audio-test.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700215
Vladimir Chtchetkinedb450d72012-01-12 13:37:40 -0800216#include "android/snaphost-android.h"
217
Vladimir Chtchetkine57584042011-01-20 16:15:30 -0800218#if !defined(CONFIG_STANDALONE_CORE)
219/* in android/qemulator.c */
220extern void android_emulator_set_base_port(int port);
221#endif
222
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700223#if defined(CONFIG_SKINS) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700224#undef main
225#define main qemu_main
226#endif
227
David 'Digit' Turnercc33b2d2013-12-15 00:09:42 +0100228#include "disas/disas.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700229
David 'Digit' Turnerd0edecb2013-12-17 09:32:26 +0100230#include "qemu/sockets.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700231
232#if defined(CONFIG_SLIRP)
233#include "libslirp.h"
234#endif
235
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700236#define DEFAULT_RAM_SIZE 128
237
238/* Max number of USB devices that can be specified on the commandline. */
239#define MAX_USB_CMDLINE 8
240
241/* Max number of bluetooth switches on the commandline. */
242#define MAX_BT_CMDLINE 10
243
244/* XXX: use a two level table to limit memory usage */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700245
246static const char *data_dir;
247const char *bios_name = NULL;
248static void *ioport_opaque[MAX_IOPORTS];
249static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
250static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100251#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700252/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
253 to store the VM snapshots */
254DriveInfo drives_table[MAX_DRIVES+1];
255int nb_drives;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100256#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700257enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700258DisplayType display_type = DT_DEFAULT;
259const char* keyboard_layout = NULL;
260int64_t ticks_per_sec;
261ram_addr_t ram_size;
David 'Digit' Turner280afa02011-05-11 17:37:44 +0200262const char *mem_path = NULL;
263#ifdef MAP_POPULATE
264int mem_prealloc = 0; /* force preallocation of physical target memory */
265#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700266int nb_nics;
267NICInfo nd_table[MAX_NICS];
268int vm_running;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100269int autostart;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700270static int rtc_utc = 1;
271static int rtc_date_offset = -1; /* -1 means no change */
272int cirrus_vga_enabled = 1;
273int std_vga_enabled = 0;
274int vmsvga_enabled = 0;
275int xenfb_enabled = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700276static int full_screen = 0;
277#ifdef CONFIG_SDL
278static int no_frame = 0;
279#endif
280int no_quit = 0;
281CharDriverState *serial_hds[MAX_SERIAL_PORTS];
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +0100282int serial_hds_count;
283
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700284CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
285CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
286#ifdef TARGET_I386
287int win2k_install_hack = 0;
288int rtc_td_hack = 0;
289#endif
290int usb_enabled = 0;
291int singlestep = 0;
292int smp_cpus = 1;
293const char *vnc_display;
294int acpi_enabled = 1;
295int no_hpet = 0;
Jun Nakajimaa381ef02011-12-17 19:13:25 -0800296int hax_disabled = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700297int no_virtio_balloon = 0;
298int fd_bootchk = 1;
299int no_reboot = 0;
300int no_shutdown = 0;
301int cursor_hide = 1;
302int graphic_rotate = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700303WatchdogTimerModel *watchdog = NULL;
304int watchdog_action = WDT_RESET;
305const char *option_rom[MAX_OPTION_ROMS];
306int nb_option_roms;
307int semihosting_enabled = 0;
308#ifdef TARGET_ARM
309int old_param = 0;
310#endif
311const char *qemu_name;
312int alt_grab = 0;
313#if defined(TARGET_SPARC) || defined(TARGET_PPC)
314unsigned int nb_prom_envs = 0;
315const char *prom_envs[MAX_PROM_ENVS];
316#endif
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100317#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700318int nb_drives_opt;
319struct drive_opt drives_opt[MAX_DRIVES];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100320#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700321int nb_numa_nodes;
322uint64_t node_mem[MAX_NODES];
323uint64_t node_cpumask[MAX_NODES];
324
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700325static QEMUTimer *nographic_timer;
326
327uint8_t qemu_uuid[16];
328
329
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700330int qemu_cpu_delay;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700331extern char* audio_input_source;
332
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700333extern char* android_op_ports;
334extern char* android_op_port;
335extern char* android_op_report_console;
336extern char* op_http_proxy;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700337// Path to the file containing specific key character map.
338char* op_charmap_file = NULL;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700339
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -0700340/* Path to hardware initialization file passed with -android-hw option. */
341char* android_op_hwini = NULL;
342
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -0700343/* Memory checker options. */
344char* android_op_memcheck = NULL;
345
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -0700346/* -dns-server option value. */
347char* android_op_dns_server = NULL;
348
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -0700349/* -radio option value. */
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700350char* android_op_radio = NULL;
351
352/* -gps option value. */
353char* android_op_gps = NULL;
354
355/* -audio option value. */
356char* android_op_audio = NULL;
357
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700358/* -cpu-delay option value. */
359char* android_op_cpu_delay = NULL;
360
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -0700361#ifdef CONFIG_NAND_LIMITS
362/* -nand-limits option value. */
363char* android_op_nand_limits = NULL;
364#endif // CONFIG_NAND_LIMITS
365
366/* -netspeed option value. */
367char* android_op_netspeed = NULL;
368
369/* -netdelay option value. */
370char* android_op_netdelay = NULL;
371
372/* -netfast option value. */
373int android_op_netfast = 0;
374
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -0700375/* -tcpdump option value. */
376char* android_op_tcpdump = NULL;
377
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700378/* -lcd-density option value. */
379char* android_op_lcd_density = NULL;
380
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700381/* -ui-port option value. This port will be used to report the core
382 * initialization completion.
383 */
384char* android_op_ui_port = NULL;
385
386/* -ui-settings option value. This value will be passed to the UI when new UI
387 * process is attaching to the core.
388 */
389char* android_op_ui_settings = NULL;
390
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -0800391/* -android-avdname option value. */
392char* android_op_avd_name = "unknown";
393
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -0700394extern int android_display_width;
395extern int android_display_height;
396extern int android_display_bpp;
397
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700398extern void dprint( const char* format, ... );
399
rich canningsd952f282011-03-01 15:40:09 -0800400const char* dns_log_filename = NULL;
401const char* drop_log_filename = NULL;
rich canningsd952f282011-03-01 15:40:09 -0800402
Tim Baverstock24204cc2010-11-25 11:37:43 +0000403const char* savevm_on_exit = NULL;
Tim Baverstock24204cc2010-11-25 11:37:43 +0000404
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700405#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
406
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700407/* Reports the core initialization failure to the error stdout and to the UI
408 * socket before exiting the application.
409 * Parameters that are passed to this macro are used to format the error
410 * mesage using sprintf routine.
411 */
412#ifdef CONFIG_ANDROID
413#define PANIC(...) android_core_init_failure(__VA_ARGS__)
414#else
415#define PANIC(...) do { fprintf(stderr, __VA_ARGS__); \
416 exit(1); \
417 } while (0)
418#endif // CONFIG_ANDROID
419
420/* Exits the core during initialization. */
421#ifdef CONFIG_ANDROID
422#define QEMU_EXIT(exit_code) android_core_init_exit(exit_code)
423#else
424#define QEMU_EXIT(exit_code) exit(exit_code)
425#endif // CONFIG_ANDROID
426
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700427/***********************************************************/
428/* x86 ISA bus support */
429
David 'Digit' Turnerbcde1092014-01-09 23:19:19 +0100430hwaddr isa_mem_base = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700431PicState2 *isa_pic;
432
433static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
434static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
435
436static uint32_t ioport_read(int index, uint32_t address)
437{
438 static IOPortReadFunc *default_func[3] = {
439 default_ioport_readb,
440 default_ioport_readw,
441 default_ioport_readl
442 };
443 IOPortReadFunc *func = ioport_read_table[index][address];
444 if (!func)
445 func = default_func[index];
446 return func(ioport_opaque[address], address);
447}
448
449static void ioport_write(int index, uint32_t address, uint32_t data)
450{
451 static IOPortWriteFunc *default_func[3] = {
452 default_ioport_writeb,
453 default_ioport_writew,
454 default_ioport_writel
455 };
456 IOPortWriteFunc *func = ioport_write_table[index][address];
457 if (!func)
458 func = default_func[index];
459 func(ioport_opaque[address], address, data);
460}
461
462static uint32_t default_ioport_readb(void *opaque, uint32_t address)
463{
464#ifdef DEBUG_UNUSED_IOPORT
465 fprintf(stderr, "unused inb: port=0x%04x\n", address);
466#endif
467 return 0xff;
468}
469
470static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
471{
472#ifdef DEBUG_UNUSED_IOPORT
473 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
474#endif
475}
476
477/* default is to make two byte accesses */
478static uint32_t default_ioport_readw(void *opaque, uint32_t address)
479{
480 uint32_t data;
481 data = ioport_read(0, address);
482 address = (address + 1) & (MAX_IOPORTS - 1);
483 data |= ioport_read(0, address) << 8;
484 return data;
485}
486
487static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
488{
489 ioport_write(0, address, data & 0xff);
490 address = (address + 1) & (MAX_IOPORTS - 1);
491 ioport_write(0, address, (data >> 8) & 0xff);
492}
493
494static uint32_t default_ioport_readl(void *opaque, uint32_t address)
495{
496#ifdef DEBUG_UNUSED_IOPORT
497 fprintf(stderr, "unused inl: port=0x%04x\n", address);
498#endif
499 return 0xffffffff;
500}
501
502static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
503{
504#ifdef DEBUG_UNUSED_IOPORT
505 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
506#endif
507}
508
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700509/***************/
510/* ballooning */
511
512static QEMUBalloonEvent *qemu_balloon_event;
513void *qemu_balloon_event_opaque;
514
515void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
516{
517 qemu_balloon_event = func;
518 qemu_balloon_event_opaque = opaque;
519}
520
521void qemu_balloon(ram_addr_t target)
522{
523 if (qemu_balloon_event)
524 qemu_balloon_event(qemu_balloon_event_opaque, target);
525}
526
527ram_addr_t qemu_balloon_status(void)
528{
529 if (qemu_balloon_event)
530 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
531 return 0;
532}
533
534/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700535/* host time/date access */
536void qemu_get_timedate(struct tm *tm, int offset)
537{
538 time_t ti;
539 struct tm *ret;
540
541 time(&ti);
542 ti += offset;
543 if (rtc_date_offset == -1) {
544 if (rtc_utc)
545 ret = gmtime(&ti);
546 else
547 ret = localtime(&ti);
548 } else {
549 ti -= rtc_date_offset;
550 ret = gmtime(&ti);
551 }
552
553 memcpy(tm, ret, sizeof(struct tm));
554}
555
556int qemu_timedate_diff(struct tm *tm)
557{
558 time_t seconds;
559
560 if (rtc_date_offset == -1)
561 if (rtc_utc)
562 seconds = mktimegm(tm);
563 else
564 seconds = mktime(tm);
565 else
566 seconds = mktimegm(tm) + rtc_date_offset;
567
568 return seconds - time(NULL);
569}
570
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700571/***********************************************************/
572/* Bluetooth support */
573static int nb_hcis;
574static int cur_hci;
575static struct HCIInfo *hci_table[MAX_NICS];
576
577static struct bt_vlan_s {
578 struct bt_scatternet_s net;
579 int id;
580 struct bt_vlan_s *next;
581} *first_bt_vlan;
582
583/* find or alloc a new bluetooth "VLAN" */
584static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
585{
586 struct bt_vlan_s **pvlan, *vlan;
587 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
588 if (vlan->id == id)
589 return &vlan->net;
590 }
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +0100591 vlan = g_malloc0(sizeof(struct bt_vlan_s));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700592 vlan->id = id;
593 pvlan = &first_bt_vlan;
594 while (*pvlan != NULL)
595 pvlan = &(*pvlan)->next;
596 *pvlan = vlan;
597 return &vlan->net;
598}
599
600static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
601{
602}
603
604static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
605{
606 return -ENOTSUP;
607}
608
609static struct HCIInfo null_hci = {
610 .cmd_send = null_hci_send,
611 .sco_send = null_hci_send,
612 .acl_send = null_hci_send,
613 .bdaddr_set = null_hci_addr_set,
614};
615
616struct HCIInfo *qemu_next_hci(void)
617{
618 if (cur_hci == nb_hcis)
619 return &null_hci;
620
621 return hci_table[cur_hci++];
622}
623
624static struct HCIInfo *hci_init(const char *str)
625{
626 char *endp;
627 struct bt_scatternet_s *vlan = 0;
628
629 if (!strcmp(str, "null"))
630 /* null */
631 return &null_hci;
632 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
633 /* host[:hciN] */
634 return bt_host_hci(str[4] ? str + 5 : "hci0");
635 else if (!strncmp(str, "hci", 3)) {
636 /* hci[,vlan=n] */
637 if (str[3]) {
638 if (!strncmp(str + 3, ",vlan=", 6)) {
639 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
640 if (*endp)
641 vlan = 0;
642 }
643 } else
644 vlan = qemu_find_bt_vlan(0);
645 if (vlan)
646 return bt_new_hci(vlan);
647 }
648
649 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
650
651 return 0;
652}
653
654static int bt_hci_parse(const char *str)
655{
656 struct HCIInfo *hci;
657 bdaddr_t bdaddr;
658
659 if (nb_hcis >= MAX_NICS) {
660 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
661 return -1;
662 }
663
664 hci = hci_init(str);
665 if (!hci)
666 return -1;
667
668 bdaddr.b[0] = 0x52;
669 bdaddr.b[1] = 0x54;
670 bdaddr.b[2] = 0x00;
671 bdaddr.b[3] = 0x12;
672 bdaddr.b[4] = 0x34;
673 bdaddr.b[5] = 0x56 + nb_hcis;
674 hci->bdaddr_set(hci, bdaddr.b);
675
676 hci_table[nb_hcis++] = hci;
677
678 return 0;
679}
680
681static void bt_vhci_add(int vlan_id)
682{
683 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
684
685 if (!vlan->slave)
686 fprintf(stderr, "qemu: warning: adding a VHCI to "
687 "an empty scatternet %i\n", vlan_id);
688
689 bt_vhci_init(bt_new_hci(vlan));
690}
691
692static struct bt_device_s *bt_device_add(const char *opt)
693{
694 struct bt_scatternet_s *vlan;
695 int vlan_id = 0;
696 char *endp = strstr(opt, ",vlan=");
697 int len = (endp ? endp - opt : strlen(opt)) + 1;
698 char devname[10];
699
700 pstrcpy(devname, MIN(sizeof(devname), len), opt);
701
702 if (endp) {
703 vlan_id = strtol(endp + 6, &endp, 0);
704 if (*endp) {
705 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
706 return 0;
707 }
708 }
709
710 vlan = qemu_find_bt_vlan(vlan_id);
711
712 if (!vlan->slave)
713 fprintf(stderr, "qemu: warning: adding a slave device to "
714 "an empty scatternet %i\n", vlan_id);
715
716 if (!strcmp(devname, "keyboard"))
717 return bt_keyboard_init(vlan);
718
719 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
720 return 0;
721}
722
723static int bt_parse(const char *opt)
724{
725 const char *endp, *p;
726 int vlan;
727
728 if (strstart(opt, "hci", &endp)) {
729 if (!*endp || *endp == ',') {
730 if (*endp)
731 if (!strstart(endp, ",vlan=", 0))
732 opt = endp + 1;
733
734 return bt_hci_parse(opt);
735 }
736 } else if (strstart(opt, "vhci", &endp)) {
737 if (!*endp || *endp == ',') {
738 if (*endp) {
739 if (strstart(endp, ",vlan=", &p)) {
740 vlan = strtol(p, (char **) &endp, 0);
741 if (*endp) {
742 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
743 return 1;
744 }
745 } else {
746 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
747 return 1;
748 }
749 } else
750 vlan = 0;
751
752 bt_vhci_add(vlan);
753 return 0;
754 }
755 } else if (strstart(opt, "device:", &endp))
756 return !bt_device_add(endp);
757
758 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
759 return 1;
760}
761
762/***********************************************************/
763/* QEMU Block devices */
764
765#define HD_ALIAS "index=%d,media=disk"
766#define CDROM_ALIAS "index=2,media=cdrom"
767#define FD_ALIAS "index=%d,if=floppy"
768#define PFLASH_ALIAS "if=pflash"
769#define MTD_ALIAS "if=mtd"
770#define SD_ALIAS "index=0,if=sd"
771
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100772static int drive_init_func(QemuOpts *opts, void *opaque)
773{
774 int *use_scsi = opaque;
775 int fatal_error = 0;
776
777 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
778 if (fatal_error)
779 return 1;
780 }
781 return 0;
782}
783
784static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
785{
786 if (NULL == qemu_opt_get(opts, "snapshot")) {
787 qemu_opt_set(opts, "snapshot", "on");
788 }
789 return 0;
790}
791
792#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700793static int drive_opt_get_free_idx(void)
794{
795 int index;
796
797 for (index = 0; index < MAX_DRIVES; index++)
798 if (!drives_opt[index].used) {
799 drives_opt[index].used = 1;
800 return index;
801 }
802
803 return -1;
804}
805
806static int drive_get_free_idx(void)
807{
808 int index;
809
810 for (index = 0; index < MAX_DRIVES; index++)
811 if (!drives_table[index].used) {
812 drives_table[index].used = 1;
813 return index;
814 }
815
816 return -1;
817}
818
819int drive_add(const char *file, const char *fmt, ...)
820{
821 va_list ap;
822 int index = drive_opt_get_free_idx();
823
824 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
825 fprintf(stderr, "qemu: too many drives\n");
826 return -1;
827 }
828
829 drives_opt[index].file = file;
830 va_start(ap, fmt);
831 vsnprintf(drives_opt[index].opt,
832 sizeof(drives_opt[0].opt), fmt, ap);
833 va_end(ap);
David 'Digit' Turner92568952010-04-15 15:04:16 -0700834
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700835 nb_drives_opt++;
836 return index;
837}
838
839void drive_remove(int index)
840{
841 drives_opt[index].used = 0;
842 nb_drives_opt--;
843}
844
845int drive_get_index(BlockInterfaceType type, int bus, int unit)
846{
847 int index;
848
849 /* seek interface, bus and unit */
850
851 for (index = 0; index < MAX_DRIVES; index++)
852 if (drives_table[index].type == type &&
853 drives_table[index].bus == bus &&
854 drives_table[index].unit == unit &&
855 drives_table[index].used)
856 return index;
857
858 return -1;
859}
860
861int drive_get_max_bus(BlockInterfaceType type)
862{
863 int max_bus;
864 int index;
865
866 max_bus = -1;
867 for (index = 0; index < nb_drives; index++) {
868 if(drives_table[index].type == type &&
869 drives_table[index].bus > max_bus)
870 max_bus = drives_table[index].bus;
871 }
872 return max_bus;
873}
874
875const char *drive_get_serial(BlockDriverState *bdrv)
876{
877 int index;
878
879 for (index = 0; index < nb_drives; index++)
880 if (drives_table[index].bdrv == bdrv)
881 return drives_table[index].serial;
882
883 return "\0";
884}
885
886BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
887{
888 int index;
889
890 for (index = 0; index < nb_drives; index++)
891 if (drives_table[index].bdrv == bdrv)
892 return drives_table[index].onerror;
893
894 return BLOCK_ERR_STOP_ENOSPC;
895}
896
897static void bdrv_format_print(void *opaque, const char *name)
898{
899 fprintf(stderr, " %s", name);
900}
901
902void drive_uninit(BlockDriverState *bdrv)
903{
904 int i;
905
906 for (i = 0; i < MAX_DRIVES; i++)
907 if (drives_table[i].bdrv == bdrv) {
908 drives_table[i].bdrv = NULL;
909 drives_table[i].used = 0;
910 drive_remove(drives_table[i].drive_opt_idx);
911 nb_drives--;
912 break;
913 }
914}
915
916int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
917{
918 char buf[128];
919 char file[1024];
920 char devname[128];
921 char serial[21];
922 const char *mediastr = "";
923 BlockInterfaceType type;
924 enum { MEDIA_DISK, MEDIA_CDROM } media;
925 int bus_id, unit_id;
926 int cyls, heads, secs, translation;
927 BlockDriverState *bdrv;
928 BlockDriver *drv = NULL;
929 QEMUMachine *machine = opaque;
930 int max_devs;
931 int index;
932 int cache;
933 int bdrv_flags, onerror;
934 int drives_table_idx;
935 char *str = arg->opt;
936 static const char * const params[] = { "bus", "unit", "if", "index",
937 "cyls", "heads", "secs", "trans",
938 "media", "snapshot", "file",
939 "cache", "format", "serial", "werror",
940 NULL };
941
942 if (check_params(buf, sizeof(buf), params, str) < 0) {
943 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
944 buf, str);
945 return -1;
946 }
947
948 file[0] = 0;
949 cyls = heads = secs = 0;
950 bus_id = 0;
951 unit_id = -1;
952 translation = BIOS_ATA_TRANSLATION_AUTO;
953 index = -1;
954 cache = 3;
955
956 if (machine->use_scsi) {
957 type = IF_SCSI;
958 max_devs = MAX_SCSI_DEVS;
959 pstrcpy(devname, sizeof(devname), "scsi");
960 } else {
961 type = IF_IDE;
962 max_devs = MAX_IDE_DEVS;
963 pstrcpy(devname, sizeof(devname), "ide");
964 }
965 media = MEDIA_DISK;
966
967 /* extract parameters */
968
969 if (get_param_value(buf, sizeof(buf), "bus", str)) {
970 bus_id = strtol(buf, NULL, 0);
971 if (bus_id < 0) {
972 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
973 return -1;
974 }
975 }
976
977 if (get_param_value(buf, sizeof(buf), "unit", str)) {
978 unit_id = strtol(buf, NULL, 0);
979 if (unit_id < 0) {
980 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
981 return -1;
982 }
983 }
984
985 if (get_param_value(buf, sizeof(buf), "if", str)) {
986 pstrcpy(devname, sizeof(devname), buf);
987 if (!strcmp(buf, "ide")) {
988 type = IF_IDE;
989 max_devs = MAX_IDE_DEVS;
990 } else if (!strcmp(buf, "scsi")) {
991 type = IF_SCSI;
992 max_devs = MAX_SCSI_DEVS;
993 } else if (!strcmp(buf, "floppy")) {
994 type = IF_FLOPPY;
995 max_devs = 0;
996 } else if (!strcmp(buf, "pflash")) {
997 type = IF_PFLASH;
998 max_devs = 0;
999 } else if (!strcmp(buf, "mtd")) {
1000 type = IF_MTD;
1001 max_devs = 0;
1002 } else if (!strcmp(buf, "sd")) {
1003 type = IF_SD;
1004 max_devs = 0;
1005 } else if (!strcmp(buf, "virtio")) {
1006 type = IF_VIRTIO;
1007 max_devs = 0;
1008 } else if (!strcmp(buf, "xen")) {
1009 type = IF_XEN;
1010 max_devs = 0;
1011 } else {
1012 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
1013 return -1;
1014 }
1015 }
1016
1017 if (get_param_value(buf, sizeof(buf), "index", str)) {
1018 index = strtol(buf, NULL, 0);
1019 if (index < 0) {
1020 fprintf(stderr, "qemu: '%s' invalid index\n", str);
1021 return -1;
1022 }
1023 }
1024
1025 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
1026 cyls = strtol(buf, NULL, 0);
1027 }
1028
1029 if (get_param_value(buf, sizeof(buf), "heads", str)) {
1030 heads = strtol(buf, NULL, 0);
1031 }
1032
1033 if (get_param_value(buf, sizeof(buf), "secs", str)) {
1034 secs = strtol(buf, NULL, 0);
1035 }
1036
1037 if (cyls || heads || secs) {
1038 if (cyls < 1 || cyls > 16383) {
1039 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
1040 return -1;
1041 }
1042 if (heads < 1 || heads > 16) {
1043 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
1044 return -1;
1045 }
1046 if (secs < 1 || secs > 63) {
1047 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
1048 return -1;
1049 }
1050 }
1051
1052 if (get_param_value(buf, sizeof(buf), "trans", str)) {
1053 if (!cyls) {
1054 fprintf(stderr,
1055 "qemu: '%s' trans must be used with cyls,heads and secs\n",
1056 str);
1057 return -1;
1058 }
1059 if (!strcmp(buf, "none"))
1060 translation = BIOS_ATA_TRANSLATION_NONE;
1061 else if (!strcmp(buf, "lba"))
1062 translation = BIOS_ATA_TRANSLATION_LBA;
1063 else if (!strcmp(buf, "auto"))
1064 translation = BIOS_ATA_TRANSLATION_AUTO;
1065 else {
1066 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
1067 return -1;
1068 }
1069 }
1070
1071 if (get_param_value(buf, sizeof(buf), "media", str)) {
1072 if (!strcmp(buf, "disk")) {
1073 media = MEDIA_DISK;
1074 } else if (!strcmp(buf, "cdrom")) {
1075 if (cyls || secs || heads) {
1076 fprintf(stderr,
1077 "qemu: '%s' invalid physical CHS format\n", str);
1078 return -1;
1079 }
1080 media = MEDIA_CDROM;
1081 } else {
1082 fprintf(stderr, "qemu: '%s' invalid media\n", str);
1083 return -1;
1084 }
1085 }
1086
1087 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
1088 if (!strcmp(buf, "on"))
1089 snapshot = 1;
1090 else if (!strcmp(buf, "off"))
1091 snapshot = 0;
1092 else {
1093 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
1094 return -1;
1095 }
1096 }
1097
1098 if (get_param_value(buf, sizeof(buf), "cache", str)) {
1099 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
1100 cache = 0;
1101 else if (!strcmp(buf, "writethrough"))
1102 cache = 1;
1103 else if (!strcmp(buf, "writeback"))
1104 cache = 2;
1105 else {
1106 fprintf(stderr, "qemu: invalid cache option\n");
1107 return -1;
1108 }
1109 }
1110
1111 if (get_param_value(buf, sizeof(buf), "format", str)) {
1112 if (strcmp(buf, "?") == 0) {
1113 fprintf(stderr, "qemu: Supported formats:");
1114 bdrv_iterate_format(bdrv_format_print, NULL);
1115 fprintf(stderr, "\n");
1116 return -1;
1117 }
1118 drv = bdrv_find_format(buf);
1119 if (!drv) {
1120 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
1121 return -1;
1122 }
1123 }
1124
1125 if (arg->file == NULL)
1126 get_param_value(file, sizeof(file), "file", str);
1127 else
1128 pstrcpy(file, sizeof(file), arg->file);
1129
1130 if (!get_param_value(serial, sizeof(serial), "serial", str))
1131 memset(serial, 0, sizeof(serial));
1132
1133 onerror = BLOCK_ERR_STOP_ENOSPC;
1134 if (get_param_value(buf, sizeof(serial), "werror", str)) {
1135 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
1136 fprintf(stderr, "werror is no supported by this format\n");
1137 return -1;
1138 }
1139 if (!strcmp(buf, "ignore"))
1140 onerror = BLOCK_ERR_IGNORE;
1141 else if (!strcmp(buf, "enospc"))
1142 onerror = BLOCK_ERR_STOP_ENOSPC;
1143 else if (!strcmp(buf, "stop"))
1144 onerror = BLOCK_ERR_STOP_ANY;
1145 else if (!strcmp(buf, "report"))
1146 onerror = BLOCK_ERR_REPORT;
1147 else {
1148 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
1149 return -1;
1150 }
1151 }
1152
1153 /* compute bus and unit according index */
1154
1155 if (index != -1) {
1156 if (bus_id != 0 || unit_id != -1) {
1157 fprintf(stderr,
1158 "qemu: '%s' index cannot be used with bus and unit\n", str);
1159 return -1;
1160 }
1161 if (max_devs == 0)
1162 {
1163 unit_id = index;
1164 bus_id = 0;
1165 } else {
1166 unit_id = index % max_devs;
1167 bus_id = index / max_devs;
1168 }
1169 }
1170
1171 /* if user doesn't specify a unit_id,
1172 * try to find the first free
1173 */
1174
1175 if (unit_id == -1) {
1176 unit_id = 0;
1177 while (drive_get_index(type, bus_id, unit_id) != -1) {
1178 unit_id++;
1179 if (max_devs && unit_id >= max_devs) {
1180 unit_id -= max_devs;
1181 bus_id++;
1182 }
1183 }
1184 }
1185
1186 /* check unit id */
1187
1188 if (max_devs && unit_id >= max_devs) {
1189 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
1190 str, unit_id, max_devs - 1);
1191 return -1;
1192 }
1193
1194 /*
1195 * ignore multiple definitions
1196 */
1197
1198 if (drive_get_index(type, bus_id, unit_id) != -1)
1199 return -2;
1200
1201 /* init */
1202
1203 if (type == IF_IDE || type == IF_SCSI)
1204 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1205 if (max_devs)
1206 snprintf(buf, sizeof(buf), "%s%i%s%i",
1207 devname, bus_id, mediastr, unit_id);
1208 else
1209 snprintf(buf, sizeof(buf), "%s%s%i",
1210 devname, mediastr, unit_id);
1211 bdrv = bdrv_new(buf);
1212 drives_table_idx = drive_get_free_idx();
1213 drives_table[drives_table_idx].bdrv = bdrv;
1214 drives_table[drives_table_idx].type = type;
1215 drives_table[drives_table_idx].bus = bus_id;
1216 drives_table[drives_table_idx].unit = unit_id;
1217 drives_table[drives_table_idx].onerror = onerror;
1218 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
1219 strncpy(drives_table[drives_table_idx].serial, serial, sizeof(serial));
1220 nb_drives++;
1221
1222 switch(type) {
1223 case IF_IDE:
1224 case IF_SCSI:
1225 case IF_XEN:
1226 switch(media) {
1227 case MEDIA_DISK:
1228 if (cyls != 0) {
1229 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
1230 bdrv_set_translation_hint(bdrv, translation);
1231 }
1232 break;
1233 case MEDIA_CDROM:
1234 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
1235 break;
1236 }
1237 break;
1238 case IF_SD:
1239 /* FIXME: This isn't really a floppy, but it's a reasonable
1240 approximation. */
1241 case IF_FLOPPY:
1242 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
1243 break;
1244 case IF_PFLASH:
1245 case IF_MTD:
1246 case IF_VIRTIO:
1247 break;
1248 case IF_COUNT:
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07001249 case IF_NONE:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001250 abort();
1251 }
1252 if (!file[0])
1253 return -2;
1254 bdrv_flags = 0;
1255 if (snapshot) {
1256 bdrv_flags |= BDRV_O_SNAPSHOT;
1257 cache = 2; /* always use write-back with snapshot */
1258 }
1259 if (cache == 0) /* no caching */
1260 bdrv_flags |= BDRV_O_NOCACHE;
1261 else if (cache == 2) /* write-back */
1262 bdrv_flags |= BDRV_O_CACHE_WB;
1263 else if (cache == 3) /* not specified */
1264 bdrv_flags |= BDRV_O_CACHE_DEF;
1265 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
1266 fprintf(stderr, "qemu: could not open disk image %s\n",
1267 file);
1268 return -1;
1269 }
1270 if (bdrv_key_required(bdrv))
1271 autostart = 0;
1272 return drives_table_idx;
1273}
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01001274#endif /* MAX_DRIVES */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001275
1276static void numa_add(const char *optarg)
1277{
1278 char option[128];
1279 char *endptr;
1280 unsigned long long value, endvalue;
1281 int nodenr;
1282
1283 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1284 if (!strcmp(option, "node")) {
1285 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1286 nodenr = nb_numa_nodes;
1287 } else {
1288 nodenr = strtoull(option, NULL, 10);
1289 }
1290
1291 if (get_param_value(option, 128, "mem", optarg) == 0) {
1292 node_mem[nodenr] = 0;
1293 } else {
1294 value = strtoull(option, &endptr, 0);
1295 switch (*endptr) {
1296 case 0: case 'M': case 'm':
1297 value <<= 20;
1298 break;
1299 case 'G': case 'g':
1300 value <<= 30;
1301 break;
1302 }
1303 node_mem[nodenr] = value;
1304 }
1305 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1306 node_cpumask[nodenr] = 0;
1307 } else {
1308 value = strtoull(option, &endptr, 10);
1309 if (value >= 64) {
1310 value = 63;
1311 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1312 } else {
1313 if (*endptr == '-') {
1314 endvalue = strtoull(endptr+1, &endptr, 10);
1315 if (endvalue >= 63) {
1316 endvalue = 62;
1317 fprintf(stderr,
1318 "only 63 CPUs in NUMA mode supported.\n");
1319 }
1320 value = (1 << (endvalue + 1)) - (1 << value);
1321 } else {
1322 value = 1 << value;
1323 }
1324 }
1325 node_cpumask[nodenr] = value;
1326 }
1327 nb_numa_nodes++;
1328 }
1329 return;
1330}
1331
1332/***********************************************************/
1333/* USB devices */
1334
1335static USBPort *used_usb_ports;
1336static USBPort *free_usb_ports;
1337
1338/* ??? Maybe change this to register a hub to keep track of the topology. */
1339void qemu_register_usb_port(USBPort *port, void *opaque, int index,
1340 usb_attachfn attach)
1341{
1342 port->opaque = opaque;
1343 port->index = index;
1344 port->attach = attach;
1345 port->next = free_usb_ports;
1346 free_usb_ports = port;
1347}
1348
1349int usb_device_add_dev(USBDevice *dev)
1350{
1351 USBPort *port;
1352
1353 /* Find a USB port to add the device to. */
1354 port = free_usb_ports;
Manuel Francisco Naranjo58ecd742012-07-18 11:22:09 -03001355 if (!port){
1356 USBDevice *hub = usb_hub_init(VM_USB_HUB_SIZE);
1357 port = free_usb_ports;
1358 usb_attach(port, hub);
1359 }
1360 else if (!port->next){
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001361 USBDevice *hub;
1362
1363 /* Create a new hub and chain it on. */
1364 free_usb_ports = NULL;
1365 port->next = used_usb_ports;
1366 used_usb_ports = port;
1367
1368 hub = usb_hub_init(VM_USB_HUB_SIZE);
1369 usb_attach(port, hub);
1370 port = free_usb_ports;
1371 }
1372
1373 free_usb_ports = port->next;
1374 port->next = used_usb_ports;
1375 used_usb_ports = port;
1376 usb_attach(port, dev);
1377 return 0;
1378}
1379
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001380#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001381static void usb_msd_password_cb(void *opaque, int err)
1382{
1383 USBDevice *dev = opaque;
1384
1385 if (!err)
1386 usb_device_add_dev(dev);
1387 else
1388 dev->handle_destroy(dev);
1389}
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001390#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001391
1392static int usb_device_add(const char *devname, int is_hotplug)
1393{
1394 const char *p;
1395 USBDevice *dev;
1396
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001397 if (strstart(devname, "host:", &p)) {
1398 dev = usb_host_device_open(p);
1399 } else if (!strcmp(devname, "mouse")) {
1400 dev = usb_mouse_init();
1401 } else if (!strcmp(devname, "tablet")) {
1402 dev = usb_tablet_init();
1403 } else if (!strcmp(devname, "keyboard")) {
1404 dev = usb_keyboard_init();
1405 } else if (strstart(devname, "disk:", &p)) {
1406#if 0
1407 BlockDriverState *bs;
1408#endif
1409 dev = usb_msd_init(p);
1410 if (!dev)
1411 return -1;
1412#if 0
1413 bs = usb_msd_get_bdrv(dev);
1414 if (bdrv_key_required(bs)) {
1415 autostart = 0;
1416 if (is_hotplug) {
1417 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
1418 dev);
1419 return 0;
1420 }
1421 }
1422 } else if (!strcmp(devname, "wacom-tablet")) {
1423 dev = usb_wacom_init();
1424 } else if (strstart(devname, "serial:", &p)) {
1425 dev = usb_serial_init(p);
1426#ifdef CONFIG_BRLAPI
1427 } else if (!strcmp(devname, "braille")) {
1428 dev = usb_baum_init();
1429#endif
1430 } else if (strstart(devname, "net:", &p)) {
1431 int nic = nb_nics;
1432
1433 if (net_client_init("nic", p) < 0)
1434 return -1;
1435 nd_table[nic].model = "usb";
1436 dev = usb_net_init(&nd_table[nic]);
1437 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1438 dev = usb_bt_init(devname[2] ? hci_init(p) :
1439 bt_new_hci(qemu_find_bt_vlan(0)));
1440#endif
1441 } else {
1442 return -1;
1443 }
1444 if (!dev)
1445 return -1;
1446
1447 return usb_device_add_dev(dev);
1448}
1449
1450int usb_device_del_addr(int bus_num, int addr)
1451{
1452 USBPort *port;
1453 USBPort **lastp;
1454 USBDevice *dev;
1455
1456 if (!used_usb_ports)
1457 return -1;
1458
1459 if (bus_num != 0)
1460 return -1;
1461
1462 lastp = &used_usb_ports;
1463 port = used_usb_ports;
1464 while (port && port->dev->addr != addr) {
1465 lastp = &port->next;
1466 port = port->next;
1467 }
1468
1469 if (!port)
1470 return -1;
1471
1472 dev = port->dev;
1473 *lastp = port->next;
1474 usb_attach(port, NULL);
1475 dev->handle_destroy(dev);
1476 port->next = free_usb_ports;
1477 free_usb_ports = port;
1478 return 0;
1479}
1480
1481static int usb_device_del(const char *devname)
1482{
1483 int bus_num, addr;
1484 const char *p;
1485
1486 if (strstart(devname, "host:", &p))
1487 return usb_host_device_close(p);
1488
1489 if (!used_usb_ports)
1490 return -1;
1491
1492 p = strchr(devname, '.');
1493 if (!p)
1494 return -1;
1495 bus_num = strtoul(devname, NULL, 0);
1496 addr = strtoul(p + 1, NULL, 0);
1497
1498 return usb_device_del_addr(bus_num, addr);
1499}
1500
1501void do_usb_add(Monitor *mon, const char *devname)
1502{
1503 usb_device_add(devname, 1);
1504}
1505
1506void do_usb_del(Monitor *mon, const char *devname)
1507{
1508 usb_device_del(devname);
1509}
1510
1511void usb_info(Monitor *mon)
1512{
1513 USBDevice *dev;
1514 USBPort *port;
1515 const char *speed_str;
1516
1517 if (!usb_enabled) {
1518 monitor_printf(mon, "USB support not enabled\n");
1519 return;
1520 }
1521
1522 for (port = used_usb_ports; port; port = port->next) {
1523 dev = port->dev;
1524 if (!dev)
1525 continue;
1526 switch(dev->speed) {
1527 case USB_SPEED_LOW:
1528 speed_str = "1.5";
1529 break;
1530 case USB_SPEED_FULL:
1531 speed_str = "12";
1532 break;
1533 case USB_SPEED_HIGH:
1534 speed_str = "480";
1535 break;
1536 default:
1537 speed_str = "?";
1538 break;
1539 }
1540 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
1541 0, dev->addr, speed_str, dev->devname);
1542 }
1543}
1544
1545/***********************************************************/
1546/* PCMCIA/Cardbus */
1547
1548static struct pcmcia_socket_entry_s {
1549 PCMCIASocket *socket;
1550 struct pcmcia_socket_entry_s *next;
1551} *pcmcia_sockets = 0;
1552
1553void pcmcia_socket_register(PCMCIASocket *socket)
1554{
1555 struct pcmcia_socket_entry_s *entry;
1556
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001557 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001558 entry->socket = socket;
1559 entry->next = pcmcia_sockets;
1560 pcmcia_sockets = entry;
1561}
1562
1563void pcmcia_socket_unregister(PCMCIASocket *socket)
1564{
1565 struct pcmcia_socket_entry_s *entry, **ptr;
1566
1567 ptr = &pcmcia_sockets;
1568 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1569 if (entry->socket == socket) {
1570 *ptr = entry->next;
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001571 g_free(entry);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001572 }
1573}
1574
1575void pcmcia_info(Monitor *mon)
1576{
1577 struct pcmcia_socket_entry_s *iter;
1578
1579 if (!pcmcia_sockets)
1580 monitor_printf(mon, "No PCMCIA sockets\n");
1581
1582 for (iter = pcmcia_sockets; iter; iter = iter->next)
1583 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1584 iter->socket->attached ? iter->socket->card_string :
1585 "Empty");
1586}
1587
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001588/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001589/* machine registration */
1590
1591static QEMUMachine *first_machine = NULL;
1592QEMUMachine *current_machine = NULL;
1593
1594int qemu_register_machine(QEMUMachine *m)
1595{
1596 QEMUMachine **pm;
1597 pm = &first_machine;
1598 while (*pm != NULL)
1599 pm = &(*pm)->next;
1600 m->next = NULL;
1601 *pm = m;
1602 return 0;
1603}
1604
1605static QEMUMachine *find_machine(const char *name)
1606{
1607 QEMUMachine *m;
1608
1609 for(m = first_machine; m != NULL; m = m->next) {
1610 if (!strcmp(m->name, name))
1611 return m;
1612 }
1613 return NULL;
1614}
1615
1616static QEMUMachine *find_default_machine(void)
1617{
1618 QEMUMachine *m;
1619
1620 for(m = first_machine; m != NULL; m = m->next) {
1621 if (m->is_default) {
1622 return m;
1623 }
1624 }
1625 return NULL;
1626}
1627
1628/***********************************************************/
1629/* main execution loop */
1630
1631static void gui_update(void *opaque)
1632{
1633 uint64_t interval = GUI_REFRESH_INTERVAL;
1634 DisplayState *ds = opaque;
1635 DisplayChangeListener *dcl = ds->listeners;
1636
1637 dpy_refresh(ds);
1638
1639 while (dcl != NULL) {
1640 if (dcl->gui_timer_interval &&
1641 dcl->gui_timer_interval < interval)
1642 interval = dcl->gui_timer_interval;
1643 dcl = dcl->next;
1644 }
David 'Digit' Turnerdcda9492014-02-16 15:13:55 +01001645 timer_mod(ds->gui_timer, interval + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001646}
1647
1648static void nographic_update(void *opaque)
1649{
1650 uint64_t interval = GUI_REFRESH_INTERVAL;
1651
David 'Digit' Turnerdcda9492014-02-16 15:13:55 +01001652 timer_mod(nographic_timer, interval + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001653}
1654
1655struct vm_change_state_entry {
1656 VMChangeStateHandler *cb;
1657 void *opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001658 QLIST_ENTRY (vm_change_state_entry) entries;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001659};
1660
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001661static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001662
1663VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1664 void *opaque)
1665{
1666 VMChangeStateEntry *e;
1667
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001668 e = g_malloc0(sizeof (*e));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001669
1670 e->cb = cb;
1671 e->opaque = opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001672 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001673 return e;
1674}
1675
1676void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1677{
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001678 QLIST_REMOVE (e, entries);
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001679 g_free (e);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001680}
1681
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001682void vm_state_notify(int running, int reason)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001683{
1684 VMChangeStateEntry *e;
1685
1686 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1687 e->cb(e->opaque, running, reason);
1688 }
1689}
1690
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001691void vm_start(void)
1692{
1693 if (!vm_running) {
1694 cpu_enable_ticks();
1695 vm_running = 1;
1696 vm_state_notify(1, 0);
David Turner6a9ef172010-09-09 22:54:36 +02001697 //qemu_rearm_alarm_timer(alarm_timer);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001698 resume_all_vcpus();
1699 }
1700}
1701
1702/* reset/shutdown handler */
1703
1704typedef struct QEMUResetEntry {
1705 QEMUResetHandler *func;
1706 void *opaque;
1707 int order;
1708 struct QEMUResetEntry *next;
1709} QEMUResetEntry;
1710
1711static QEMUResetEntry *first_reset_entry;
1712static int reset_requested;
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001713static int shutdown_requested, shutdown_signal = -1;
1714static pid_t shutdown_pid;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001715static int powerdown_requested;
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001716int debug_requested;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001717static int vmstop_requested;
1718
1719int qemu_shutdown_requested(void)
1720{
1721 int r = shutdown_requested;
1722 shutdown_requested = 0;
1723 return r;
1724}
1725
1726int qemu_reset_requested(void)
1727{
1728 int r = reset_requested;
1729 reset_requested = 0;
1730 return r;
1731}
1732
1733int qemu_powerdown_requested(void)
1734{
1735 int r = powerdown_requested;
1736 powerdown_requested = 0;
1737 return r;
1738}
1739
David 'Digit' Turnerf9077a82014-02-10 23:10:47 +01001740int qemu_debug_requested(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001741{
1742 int r = debug_requested;
1743 debug_requested = 0;
1744 return r;
1745}
1746
David 'Digit' Turnerf9077a82014-02-10 23:10:47 +01001747int qemu_vmstop_requested(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001748{
1749 int r = vmstop_requested;
1750 vmstop_requested = 0;
1751 return r;
1752}
1753
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001754void qemu_register_reset(QEMUResetHandler *func, int order, void *opaque)
1755{
1756 QEMUResetEntry **pre, *re;
1757
1758 pre = &first_reset_entry;
1759 while (*pre != NULL && (*pre)->order >= order) {
1760 pre = &(*pre)->next;
1761 }
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001762 re = g_malloc0(sizeof(QEMUResetEntry));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001763 re->func = func;
1764 re->opaque = opaque;
1765 re->order = order;
1766 re->next = NULL;
1767 *pre = re;
1768}
1769
1770void qemu_system_reset(void)
1771{
1772 QEMUResetEntry *re;
1773
1774 /* reset all devices */
1775 for(re = first_reset_entry; re != NULL; re = re->next) {
1776 re->func(re->opaque);
1777 }
1778}
1779
1780void qemu_system_reset_request(void)
1781{
1782 if (no_reboot) {
1783 shutdown_requested = 1;
1784 } else {
1785 reset_requested = 1;
1786 }
1787 qemu_notify_event();
1788}
1789
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001790void qemu_system_killed(int signal, pid_t pid)
1791{
1792 shutdown_signal = signal;
1793 shutdown_pid = pid;
1794 qemu_system_shutdown_request();
1795}
1796
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001797void qemu_system_shutdown_request(void)
1798{
1799 shutdown_requested = 1;
1800 qemu_notify_event();
1801}
1802
1803void qemu_system_powerdown_request(void)
1804{
1805 powerdown_requested = 1;
1806 qemu_notify_event();
1807}
1808
David 'Digit' Turnerf9077a82014-02-10 23:10:47 +01001809int vm_can_run(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001810{
1811 if (powerdown_requested)
1812 return 0;
1813 if (reset_requested)
1814 return 0;
1815 if (shutdown_requested)
1816 return 0;
1817 if (debug_requested)
1818 return 0;
1819 return 1;
1820}
1821
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07001822void version(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001823{
1824 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1825}
1826
1827void qemu_help(int exitcode)
1828{
1829 version();
1830 printf("usage: %s [options] [disk_image]\n"
1831 "\n"
1832 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
1833 "\n"
1834#define DEF(option, opt_arg, opt_enum, opt_help) \
1835 opt_help
1836#define DEFHEADING(text) stringify(text) "\n"
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001837#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001838#undef DEF
1839#undef DEFHEADING
1840#undef GEN_DOCS
1841 "\n"
1842 "During emulation, the following keys are useful:\n"
1843 "ctrl-alt-f toggle full screen\n"
1844 "ctrl-alt-n switch to virtual console 'n'\n"
1845 "ctrl-alt toggle mouse and keyboard grab\n"
1846 "\n"
1847 "When using -nographic, press 'ctrl-a h' to get some help.\n"
1848 ,
1849 "qemu",
1850 DEFAULT_RAM_SIZE,
1851#ifndef _WIN32
1852 DEFAULT_NETWORK_SCRIPT,
1853 DEFAULT_NETWORK_DOWN_SCRIPT,
1854#endif
1855 DEFAULT_GDBSTUB_PORT,
1856 "/tmp/qemu.log");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07001857 QEMU_EXIT(exitcode);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001858}
1859
1860#define HAS_ARG 0x0001
1861
1862enum {
1863#define DEF(option, opt_arg, opt_enum, opt_help) \
1864 opt_enum,
1865#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001866#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001867#undef DEF
1868#undef DEFHEADING
1869#undef GEN_DOCS
1870};
1871
1872typedef struct QEMUOption {
1873 const char *name;
1874 int flags;
1875 int index;
1876} QEMUOption;
1877
1878static const QEMUOption qemu_options[] = {
1879 { "h", 0, QEMU_OPTION_h },
1880#define DEF(option, opt_arg, opt_enum, opt_help) \
1881 { option, opt_arg, opt_enum },
1882#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001883#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001884#undef DEF
1885#undef DEFHEADING
1886#undef GEN_DOCS
1887 { NULL, 0, 0 },
1888};
1889
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001890static void select_vgahw (const char *p)
1891{
1892 const char *opts;
1893
1894 cirrus_vga_enabled = 0;
1895 std_vga_enabled = 0;
1896 vmsvga_enabled = 0;
1897 xenfb_enabled = 0;
1898 if (strstart(p, "std", &opts)) {
1899 std_vga_enabled = 1;
1900 } else if (strstart(p, "cirrus", &opts)) {
1901 cirrus_vga_enabled = 1;
1902 } else if (strstart(p, "vmware", &opts)) {
1903 vmsvga_enabled = 1;
1904 } else if (strstart(p, "xenfb", &opts)) {
1905 xenfb_enabled = 1;
1906 } else if (!strstart(p, "none", &opts)) {
1907 invalid_vga:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07001908 PANIC("Unknown vga type: %s", p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001909 }
1910 while (*opts) {
1911 const char *nextopt;
1912
1913 if (strstart(opts, ",retrace=", &nextopt)) {
1914 opts = nextopt;
1915 if (strstart(opts, "dumb", &nextopt))
1916 vga_retrace_method = VGA_RETRACE_DUMB;
1917 else if (strstart(opts, "precise", &nextopt))
1918 vga_retrace_method = VGA_RETRACE_PRECISE;
1919 else goto invalid_vga;
1920 } else goto invalid_vga;
1921 opts = nextopt;
1922 }
1923}
1924
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001925#define MAX_NET_CLIENTS 32
1926
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001927#ifdef _WIN32
1928/* Look for support files in the same directory as the executable. */
1929static char *find_datadir(const char *argv0)
1930{
1931 char *p;
1932 char buf[MAX_PATH];
1933 DWORD len;
1934
1935 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
1936 if (len == 0) {
1937 return NULL;
1938 }
1939
1940 buf[len] = 0;
1941 p = buf + len - 1;
1942 while (p != buf && *p != '\\')
1943 p--;
1944 *p = 0;
1945 if (access(buf, R_OK) == 0) {
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001946 return g_strdup(buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001947 }
1948 return NULL;
1949}
1950#else /* !_WIN32 */
1951
David 'Digit' Turner24d27522011-06-01 16:50:56 +02001952/* Similarly, return the location of the executable */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001953static char *find_datadir(const char *argv0)
1954{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001955 char *p = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001956 char buf[PATH_MAX];
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001957
1958#if defined(__linux__)
1959 {
1960 int len;
1961 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
1962 if (len > 0) {
1963 buf[len] = 0;
1964 p = buf;
1965 }
1966 }
1967#elif defined(__FreeBSD__)
1968 {
1969 int len;
1970 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
1971 if (len > 0) {
1972 buf[len] = 0;
1973 p = buf;
1974 }
1975 }
1976#endif
1977 /* If we don't have any way of figuring out the actual executable
1978 location then try argv[0]. */
1979 if (!p) {
David 'Digit' Turner24d27522011-06-01 16:50:56 +02001980 p = realpath(argv0, buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001981 if (!p) {
1982 return NULL;
1983 }
1984 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001985
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001986 return g_strdup(dirname(buf));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001987}
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001988#endif
1989
David 'Digit' Turner24d27522011-06-01 16:50:56 +02001990static char*
1991qemu_find_file_with_subdir(const char* data_dir, const char* subdir, const char* name)
1992{
1993 int len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001994 char* buf = g_malloc0(len);
David 'Digit' Turner24d27522011-06-01 16:50:56 +02001995
1996 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1997 VERBOSE_PRINT(init," trying to find: %s\n", buf);
1998 if (access(buf, R_OK)) {
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001999 g_free(buf);
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002000 return NULL;
2001 }
2002 return buf;
2003}
2004
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002005char *qemu_find_file(int type, const char *name)
2006{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002007 const char *subdir;
2008 char *buf;
2009
2010 /* If name contains path separators then try it as a straight path. */
2011 if ((strchr(name, '/') || strchr(name, '\\'))
2012 && access(name, R_OK) == 0) {
2013 return strdup(name);
2014 }
2015 switch (type) {
2016 case QEMU_FILE_TYPE_BIOS:
2017 subdir = "";
2018 break;
2019 case QEMU_FILE_TYPE_KEYMAP:
2020 subdir = "keymaps/";
2021 break;
2022 default:
2023 abort();
2024 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002025 buf = qemu_find_file_with_subdir(data_dir, subdir, name);
2026#ifdef CONFIG_ANDROID
2027 if (type == QEMU_FILE_TYPE_BIOS) {
2028 /* This case corresponds to the emulator being used as part of an
2029 * SDK installation. NOTE: data_dir is really $bindir. */
2030 if (buf == NULL)
2031 buf = qemu_find_file_with_subdir(data_dir, "lib/pc-bios/", name);
2032 /* This case corresponds to platform builds. */
2033 if (buf == NULL)
2034 buf = qemu_find_file_with_subdir(data_dir, "../usr/share/pc-bios/", name);
2035 /* Finally, try this for standalone builds under external/qemu */
2036 if (buf == NULL)
Andrew Hsieh3d894282012-05-02 12:06:12 +08002037 buf = qemu_find_file_with_subdir(data_dir, "../../../prebuilts/qemu-kernel/x86/pc-bios/", name);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002038 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002039#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002040 return buf;
2041}
2042
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07002043static int
2044add_dns_server( const char* server_name )
2045{
2046 SockAddress addr;
2047
2048 if (sock_address_init_resolve( &addr, server_name, 55, 0 ) < 0) {
2049 fprintf(stdout,
2050 "### WARNING: can't resolve DNS server name '%s'\n",
2051 server_name );
2052 return -1;
2053 }
2054
2055 fprintf(stderr,
2056 "DNS server name '%s' resolved to %s\n", server_name, sock_address_to_string(&addr) );
2057
2058 if ( slirp_add_dns_server( &addr ) < 0 ) {
2059 fprintf(stderr,
2060 "### WARNING: could not add DNS server '%s' to the network stack\n", server_name);
2061 return -1;
2062 }
2063 return 0;
2064}
2065
rich cannings7339b552011-02-16 13:43:44 -08002066/* Parses an integer
2067 * Pararm:
2068 * str String containing a number to be parsed.
2069 * result Passes the parsed integer in this argument
2070 * returns 0 if ok, -1 if failed
2071 */
2072int
2073parse_int(const char *str, int *result)
2074{
2075 char* r;
2076 *result = strtol(str, &r, 0);
2077 if (r == NULL || *r != '\0')
2078 return -1;
2079
2080 return 0;
2081}
2082
rich cannings7339b552011-02-16 13:43:44 -08002083/* parses a null-terminated string specifying a network port (e.g., "80") or
2084 * port range (e.g., "[6666-7000]"). In case of a single port, lport and hport
2085 * are the same. Returns 0 on success, -1 on error. */
2086
2087int parse_port_range(const char *str, unsigned short *lport,
2088 unsigned short *hport) {
2089
2090 unsigned int low = 0, high = 0;
2091 char *p, *arg = strdup(str);
2092
2093 if ((*arg == '[') && ((p = strrchr(arg, ']')) != NULL)) {
2094 p = arg + 1; /* skip '[' */
2095 low = atoi(strtok(p, "-"));
2096 high = atoi(strtok(NULL, "-"));
2097 if ((low > 0) && (high > 0) && (low < high) && (high < 65535)) {
2098 *lport = low;
2099 *hport = high;
2100 }
2101 }
2102 else {
2103 low = atoi(arg);
2104 if ((0 < low) && (low < 65535)) {
2105 *lport = low;
2106 *hport = low;
2107 }
2108 }
2109 free(arg);
2110 if (low != 0)
2111 return 0;
2112 return -1;
2113}
2114
2115/*
2116 * Implements the generic port forwarding option
2117 */
2118void
2119net_slirp_forward(const char *optarg)
2120{
2121 /*
2122 * we expect the following format:
2123 * dst_net:dst_mask:dst_port:redirect_ip:redirect_port OR
2124 * dst_net:dst_mask:[dp_range_start-dp_range_end]:redirect_ip:redirect_port
2125 */
2126 char *argument = strdup(optarg), *p = argument;
2127 char *dst_net, *dst_mask, *dst_port;
2128 char *redirect_ip, *redirect_port;
2129 uint32_t dnet, dmask, rip;
Vladimir Chtchetkineb557e9f2012-03-22 15:38:08 -07002130 unsigned short dlport = 0, dhport = 0, rport;
rich cannings7339b552011-02-16 13:43:44 -08002131
2132
2133 dst_net = strtok(p, ":");
2134 dst_mask = strtok(NULL, ":");
2135 dst_port = strtok(NULL, ":");
2136 redirect_ip = strtok(NULL, ":");
2137 redirect_port = strtok(NULL, ":");
2138
2139 if (dst_net == NULL || dst_mask == NULL || dst_port == NULL ||
2140 redirect_ip == NULL || redirect_port == NULL) {
2141 fprintf(stderr,
2142 "Invalid argument for -net-forward, we expect "
2143 "dst_net:dst_mask:dst_port:redirect_ip:redirect_port or "
2144 "dst_net:dst_mask:[dp_range_start-dp_range_end]"
2145 ":redirect_ip:redirect_port: %s\n",
2146 optarg);
2147 exit(1);
2148 }
2149
2150 /* inet_strtoip converts dotted address to host byte order */
2151 if (inet_strtoip(dst_net, &dnet) == -1) {
2152 fprintf(stderr, "Invalid destination IP net: %s\n", dst_net);
2153 exit(1);
2154 }
2155 if (inet_strtoip(dst_mask, &dmask) == -1) {
2156 fprintf(stderr, "Invalid destination IP mask: %s\n", dst_mask);
2157 exit(1);
2158 }
2159 if (inet_strtoip(redirect_ip, &rip) == -1) {
2160 fprintf(stderr, "Invalid redirect IP address: %s\n", redirect_ip);
2161 exit(1);
2162 }
2163
2164 if (parse_port_range(dst_port, &dlport, &dhport) == -1) {
2165 fprintf(stderr, "Invalid destination port or port range\n");
2166 exit(1);
2167 }
2168
2169 rport = atoi(redirect_port);
2170 if (!rport) {
2171 fprintf(stderr, "Invalid redirect port: %s\n", redirect_port);
2172 exit(1);
2173 }
2174
2175 dnet &= dmask;
2176
2177 slirp_add_net_forward(dnet, dmask, dlport, dhport,
2178 rip, rport);
2179
2180 free(argument);
2181}
2182
2183
2184/* Parses an -allow-tcp or -allow-udp argument and inserts a corresponding
2185 * entry in the allows list */
2186void
2187slirp_allow(const char *optarg, u_int8_t proto)
2188{
2189 /*
2190 * we expect the following format:
2191 * dst_ip:dst_port OR dst_ip:[dst_lport-dst_hport]
2192 */
2193 char *argument = strdup(optarg), *p = argument;
2194 char *dst_ip_str, *dst_port_str;
2195 uint32_t dst_ip;
Vladimir Chtchetkineb557e9f2012-03-22 15:38:08 -07002196 unsigned short dst_lport = 0, dst_hport = 0;
rich cannings7339b552011-02-16 13:43:44 -08002197
2198 dst_ip_str = strtok(p, ":");
2199 dst_port_str = strtok(NULL, ":");
2200
2201 if (dst_ip_str == NULL || dst_port_str == NULL) {
2202 fprintf(stderr,
2203 "Invalid argument %s for -allow. We expect "
2204 "dst_ip:dst_port or dst_ip:[dst_lport-dst_hport]\n",
2205 optarg);
2206 exit(1);
2207 }
2208
2209 if (inet_strtoip(dst_ip_str, &dst_ip) == -1) {
2210 fprintf(stderr, "Invalid destination IP address: %s\n", dst_ip_str);
2211 exit(1);
2212 }
2213 if (parse_port_range(dst_port_str, &dst_lport, &dst_hport) == -1) {
2214 fprintf(stderr, "Invalid destination port or port range\n");
2215 exit(1);
2216 }
2217
2218 slirp_add_allow(dst_ip, dst_lport, dst_hport, proto);
2219
2220 free(argument);
2221}
2222
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01002223/* Add a serial device at a given location in the emulated hardware table.
2224 * On failure, this function aborts the program with an error message.
2225 */
2226static void
2227serial_hds_add_at(int index, const char* devname)
2228{
2229 char label[32];
2230
2231 if (!devname || !strcmp(devname,"none"))
2232 return;
2233
2234 if (index >= MAX_SERIAL_PORTS) {
2235 PANIC("qemu: invalid serial index for %s (%d >= %d)",
2236 devname, index, MAX_SERIAL_PORTS);
2237 }
2238 if (serial_hds[index] != NULL) {
2239 PANIC("qemu: invalid serial index for %s (%d: already taken!)",
2240 devname, index);
2241 }
2242 snprintf(label, sizeof(label), "serial%d", index);
2243 serial_hds[index] = qemu_chr_open(label, devname, NULL);
2244 if (!serial_hds[index]) {
2245 PANIC("qemu: could not open serial device '%s'", devname);
2246 }
2247}
2248
2249
2250/* Find a free slot in the emulated serial device table, and register
2251 * it. Return the allocated table index.
2252 */
2253static int
2254serial_hds_add(const char* devname)
2255{
2256 int index;
2257
2258 /* Find first free slot */
2259 for (index = 0; index < MAX_SERIAL_PORTS; index++) {
2260 if (serial_hds[index] == NULL) {
2261 serial_hds_add_at(index, devname);
2262 return index;
2263 }
2264 }
2265
2266 PANIC("qemu: too many serial devices registered (%d)", index);
2267 return -1; /* shouldn't happen */
2268}
2269
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002270int main(int argc, char **argv, char **envp)
2271{
2272 const char *gdbstub_dev = NULL;
2273 uint32_t boot_devices_bitmap = 0;
2274 int i;
David 'Digit' Turnera2c14f92014-02-04 01:02:30 +01002275 int snapshot, linux_boot, __attribute__((unused)) net_boot;
David Turner6a9ef172010-09-09 22:54:36 +02002276 const char *icount_option = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002277 const char *initrd_filename;
2278 const char *kernel_filename, *kernel_cmdline;
2279 const char *boot_devices = "";
2280 DisplayState *ds;
2281 DisplayChangeListener *dcl;
2282 int cyls, heads, secs, translation;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002283 QemuOpts *hda_opts = NULL;
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01002284 QemuOpts *hdb_opts = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002285 const char *net_clients[MAX_NET_CLIENTS];
2286 int nb_net_clients;
2287 const char *bt_opts[MAX_BT_CMDLINE];
2288 int nb_bt_opts;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002289 int optind;
2290 const char *r, *optarg;
2291 CharDriverState *monitor_hd = NULL;
2292 const char *monitor_device;
2293 const char *serial_devices[MAX_SERIAL_PORTS];
2294 int serial_device_index;
2295 const char *parallel_devices[MAX_PARALLEL_PORTS];
2296 int parallel_device_index;
2297 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
2298 int virtio_console_index;
2299 const char *loadvm = NULL;
2300 QEMUMachine *machine;
2301 const char *cpu_model;
2302 const char *usb_devices[MAX_USB_CMDLINE];
2303 int usb_devices_index;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002304 int tb_size;
2305 const char *pid_file = NULL;
2306 const char *incoming = NULL;
David 'Digit' Turner7e9f6c12014-01-22 18:42:40 +01002307 const char* log_mask = NULL;
2308 const char* log_file = NULL;
David 'Digit' Turnere2678e12014-01-16 15:56:43 +01002309 CPUOldState *env;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002310 int show_vnc_port = 0;
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07002311 IniFile* hw_ini = NULL;
David 'Digit' Turner5f824112011-03-01 14:00:26 +01002312 STRALLOC_DEFINE(kernel_params);
2313 STRALLOC_DEFINE(kernel_config);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07002314 int dns_count = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002315
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002316 /* Initialize sockets before anything else, so we can properly report
2317 * initialization failures back to the UI. */
2318#ifdef _WIN32
2319 socket_init();
2320#endif
2321
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07002322 init_clocks();
2323
David 'Digit' Turner8848f632014-01-16 18:10:21 +01002324 qemu_cache_utils_init();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002325
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002326 QLIST_INIT (&vm_change_state_head);
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02002327 os_setup_early_signal_handling();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002328
2329 module_call_init(MODULE_INIT_MACHINE);
2330 machine = find_default_machine();
2331 cpu_model = NULL;
2332 initrd_filename = NULL;
2333 ram_size = 0;
2334 snapshot = 0;
2335 kernel_filename = NULL;
2336 kernel_cmdline = "";
David 'Digit' Turner5f824112011-03-01 14:00:26 +01002337
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002338 cyls = heads = secs = 0;
2339 translation = BIOS_ATA_TRANSLATION_AUTO;
2340 monitor_device = "vc:80Cx24C";
2341
2342 serial_devices[0] = "vc:80Cx24C";
2343 for(i = 1; i < MAX_SERIAL_PORTS; i++)
2344 serial_devices[i] = NULL;
2345 serial_device_index = 0;
2346
2347 parallel_devices[0] = "vc:80Cx24C";
2348 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
2349 parallel_devices[i] = NULL;
2350 parallel_device_index = 0;
2351
2352 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
2353 virtio_consoles[i] = NULL;
2354 virtio_console_index = 0;
2355
2356 for (i = 0; i < MAX_NODES; i++) {
2357 node_mem[i] = 0;
2358 node_cpumask[i] = 0;
2359 }
2360
2361 usb_devices_index = 0;
2362
2363 nb_net_clients = 0;
2364 nb_bt_opts = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002365#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002366 nb_drives = 0;
2367 nb_drives_opt = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002368#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002369 nb_numa_nodes = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002370
2371 nb_nics = 0;
2372
2373 tb_size = 0;
2374 autostart= 1;
2375
2376 register_watchdogs();
2377
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07002378 /* Initialize boot properties. */
2379 boot_property_init_service();
David 'Digit' Turnerca950592011-04-27 12:26:15 +02002380 android_hw_control_init();
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02002381 android_net_pipes_init();
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07002382
David 'Digit' Turner36597752011-05-20 01:18:01 +02002383#ifdef CONFIG_KVM
2384 /* By default, force auto-detection for kvm */
2385 kvm_allowed = -1;
2386#endif
2387
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002388 optind = 1;
2389 for(;;) {
2390 if (optind >= argc)
2391 break;
2392 r = argv[optind];
2393 if (r[0] != '-') {
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002394 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002395 } else {
2396 const QEMUOption *popt;
2397
2398 optind++;
2399 /* Treat --foo the same as -foo. */
2400 if (r[1] == '-')
2401 r++;
2402 popt = qemu_options;
2403 for(;;) {
2404 if (!popt->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002405 PANIC("%s: invalid option -- '%s'",
2406 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002407 }
2408 if (!strcmp(popt->name, r + 1))
2409 break;
2410 popt++;
2411 }
2412 if (popt->flags & HAS_ARG) {
2413 if (optind >= argc) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002414 PANIC("%s: option '%s' requires an argument",
2415 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002416 }
2417 optarg = argv[optind++];
2418 } else {
2419 optarg = NULL;
2420 }
2421
2422 switch(popt->index) {
2423 case QEMU_OPTION_M:
2424 machine = find_machine(optarg);
2425 if (!machine) {
2426 QEMUMachine *m;
2427 printf("Supported machines are:\n");
2428 for(m = first_machine; m != NULL; m = m->next) {
2429 printf("%-10s %s%s\n",
2430 m->name, m->desc,
2431 m->is_default ? " (default)" : "");
2432 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002433 if (*optarg != '?') {
2434 PANIC("Invalid machine parameter: %s",
2435 optarg);
2436 } else {
2437 QEMU_EXIT(0);
2438 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002439 }
2440 break;
2441 case QEMU_OPTION_cpu:
2442 /* hw initialization will check this */
2443 if (*optarg == '?') {
2444/* XXX: implement xxx_cpu_list for targets that still miss it */
2445#if defined(cpu_list)
2446 cpu_list(stdout, &fprintf);
2447#endif
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002448 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002449 } else {
2450 cpu_model = optarg;
2451 }
2452 break;
2453 case QEMU_OPTION_initrd:
2454 initrd_filename = optarg;
2455 break;
2456 case QEMU_OPTION_hda:
2457 if (cyls == 0)
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002458 hda_opts = drive_add(optarg, HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002459 else
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002460 hda_opts = drive_add(optarg, HD_ALIAS
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002461 ",cyls=%d,heads=%d,secs=%d%s",
2462 0, cyls, heads, secs,
2463 translation == BIOS_ATA_TRANSLATION_LBA ?
2464 ",trans=lba" :
2465 translation == BIOS_ATA_TRANSLATION_NONE ?
2466 ",trans=none" : "");
2467 break;
2468 case QEMU_OPTION_hdb:
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01002469 hdb_opts = drive_add(optarg, HD_ALIAS, 1);
2470 break;
2471
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002472 case QEMU_OPTION_hdc:
2473 case QEMU_OPTION_hdd:
2474 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
2475 break;
2476 case QEMU_OPTION_drive:
2477 drive_add(NULL, "%s", optarg);
2478 break;
2479 case QEMU_OPTION_mtdblock:
2480 drive_add(optarg, MTD_ALIAS);
2481 break;
2482 case QEMU_OPTION_sd:
2483 drive_add(optarg, SD_ALIAS);
2484 break;
2485 case QEMU_OPTION_pflash:
2486 drive_add(optarg, PFLASH_ALIAS);
2487 break;
2488 case QEMU_OPTION_snapshot:
2489 snapshot = 1;
2490 break;
2491 case QEMU_OPTION_hdachs:
2492 {
2493 const char *p;
2494 p = optarg;
2495 cyls = strtol(p, (char **)&p, 0);
2496 if (cyls < 1 || cyls > 16383)
2497 goto chs_fail;
2498 if (*p != ',')
2499 goto chs_fail;
2500 p++;
2501 heads = strtol(p, (char **)&p, 0);
2502 if (heads < 1 || heads > 16)
2503 goto chs_fail;
2504 if (*p != ',')
2505 goto chs_fail;
2506 p++;
2507 secs = strtol(p, (char **)&p, 0);
2508 if (secs < 1 || secs > 63)
2509 goto chs_fail;
2510 if (*p == ',') {
2511 p++;
2512 if (!strcmp(p, "none"))
2513 translation = BIOS_ATA_TRANSLATION_NONE;
2514 else if (!strcmp(p, "lba"))
2515 translation = BIOS_ATA_TRANSLATION_LBA;
2516 else if (!strcmp(p, "auto"))
2517 translation = BIOS_ATA_TRANSLATION_AUTO;
2518 else
2519 goto chs_fail;
2520 } else if (*p != '\0') {
2521 chs_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002522 PANIC("qemu: invalid physical CHS format");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002523 }
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002524 if (hda_opts != NULL) {
2525 char num[16];
2526 snprintf(num, sizeof(num), "%d", cyls);
2527 qemu_opt_set(hda_opts, "cyls", num);
2528 snprintf(num, sizeof(num), "%d", heads);
2529 qemu_opt_set(hda_opts, "heads", num);
2530 snprintf(num, sizeof(num), "%d", secs);
2531 qemu_opt_set(hda_opts, "secs", num);
2532 if (translation == BIOS_ATA_TRANSLATION_LBA)
2533 qemu_opt_set(hda_opts, "trans", "lba");
2534 if (translation == BIOS_ATA_TRANSLATION_NONE)
2535 qemu_opt_set(hda_opts, "trans", "none");
2536 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002537 }
2538 break;
2539 case QEMU_OPTION_numa:
2540 if (nb_numa_nodes >= MAX_NODES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002541 PANIC("qemu: too many NUMA nodes");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002542 }
2543 numa_add(optarg);
2544 break;
2545 case QEMU_OPTION_nographic:
2546 display_type = DT_NOGRAPHIC;
2547 break;
2548#ifdef CONFIG_CURSES
2549 case QEMU_OPTION_curses:
2550 display_type = DT_CURSES;
2551 break;
2552#endif
2553 case QEMU_OPTION_portrait:
2554 graphic_rotate = 1;
2555 break;
2556 case QEMU_OPTION_kernel:
2557 kernel_filename = optarg;
2558 break;
2559 case QEMU_OPTION_append:
2560 kernel_cmdline = optarg;
2561 break;
2562 case QEMU_OPTION_cdrom:
2563 drive_add(optarg, CDROM_ALIAS);
2564 break;
2565 case QEMU_OPTION_boot:
2566 boot_devices = optarg;
2567 /* We just do some generic consistency checks */
2568 {
2569 /* Could easily be extended to 64 devices if needed */
2570 const char *p;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07002571
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002572 boot_devices_bitmap = 0;
2573 for (p = boot_devices; *p != '\0'; p++) {
2574 /* Allowed boot devices are:
2575 * a b : floppy disk drives
2576 * c ... f : IDE disk drives
2577 * g ... m : machine implementation dependant drives
2578 * n ... p : network devices
2579 * It's up to each machine implementation to check
2580 * if the given boot devices match the actual hardware
2581 * implementation and firmware features.
2582 */
2583 if (*p < 'a' || *p > 'q') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002584 PANIC("Invalid boot device '%c'", *p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002585 }
2586 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002587 PANIC(
2588 "Boot device '%c' was given twice",*p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002589 }
2590 boot_devices_bitmap |= 1 << (*p - 'a');
2591 }
2592 }
2593 break;
2594 case QEMU_OPTION_fda:
2595 case QEMU_OPTION_fdb:
2596 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2597 break;
2598#ifdef TARGET_I386
2599 case QEMU_OPTION_no_fd_bootchk:
2600 fd_bootchk = 0;
2601 break;
2602#endif
2603 case QEMU_OPTION_net:
2604 if (nb_net_clients >= MAX_NET_CLIENTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002605 PANIC("qemu: too many network clients");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002606 }
2607 net_clients[nb_net_clients] = optarg;
2608 nb_net_clients++;
2609 break;
2610#ifdef CONFIG_SLIRP
2611 case QEMU_OPTION_tftp:
2612 tftp_prefix = optarg;
2613 break;
2614 case QEMU_OPTION_bootp:
2615 bootp_filename = optarg;
2616 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002617 case QEMU_OPTION_redir:
2618 net_slirp_redir(NULL, optarg, NULL);
2619 break;
2620#endif
2621 case QEMU_OPTION_bt:
2622 if (nb_bt_opts >= MAX_BT_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002623 PANIC("qemu: too many bluetooth options");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002624 }
2625 bt_opts[nb_bt_opts++] = optarg;
2626 break;
2627#ifdef HAS_AUDIO
2628 case QEMU_OPTION_audio_help:
2629 AUD_help ();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002630 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002631 break;
2632 case QEMU_OPTION_soundhw:
2633 select_soundhw (optarg);
2634 break;
2635#endif
2636 case QEMU_OPTION_h:
2637 qemu_help(0);
2638 break;
2639 case QEMU_OPTION_version:
2640 version();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002641 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002642 break;
2643 case QEMU_OPTION_m: {
2644 uint64_t value;
2645 char *ptr;
2646
2647 value = strtoul(optarg, &ptr, 10);
2648 switch (*ptr) {
2649 case 0: case 'M': case 'm':
2650 value <<= 20;
2651 break;
2652 case 'G': case 'g':
2653 value <<= 30;
2654 break;
2655 default:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002656 PANIC("qemu: invalid ram size: %s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002657 }
2658
2659 /* On 32-bit hosts, QEMU is limited by virtual address space */
David 'Digit' Turner81911b02014-02-10 17:53:45 +01002660 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002661 PANIC("qemu: at most 2047 MB RAM can be simulated");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002662 }
2663 if (value != (uint64_t)(ram_addr_t)value) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002664 PANIC("qemu: ram size too large");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002665 }
2666 ram_size = value;
2667 break;
2668 }
2669 case QEMU_OPTION_d:
David 'Digit' Turner7e9f6c12014-01-22 18:42:40 +01002670 log_mask = optarg;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002671 break;
2672 case QEMU_OPTION_s:
2673 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2674 break;
2675 case QEMU_OPTION_gdb:
2676 gdbstub_dev = optarg;
2677 break;
2678 case QEMU_OPTION_L:
2679 data_dir = optarg;
2680 break;
2681 case QEMU_OPTION_bios:
2682 bios_name = optarg;
2683 break;
2684 case QEMU_OPTION_singlestep:
2685 singlestep = 1;
2686 break;
2687 case QEMU_OPTION_S:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002688 autostart = 0;
2689 break;
2690#ifndef _WIN32
2691 case QEMU_OPTION_k:
2692 keyboard_layout = optarg;
2693 break;
2694#endif
2695 case QEMU_OPTION_localtime:
2696 rtc_utc = 0;
2697 break;
2698 case QEMU_OPTION_vga:
2699 select_vgahw (optarg);
2700 break;
2701#if defined(TARGET_PPC) || defined(TARGET_SPARC)
2702 case QEMU_OPTION_g:
2703 {
2704 const char *p;
2705 int w, h, depth;
2706 p = optarg;
2707 w = strtol(p, (char **)&p, 10);
2708 if (w <= 0) {
2709 graphic_error:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002710 PANIC("qemu: invalid resolution or depth");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002711 }
2712 if (*p != 'x')
2713 goto graphic_error;
2714 p++;
2715 h = strtol(p, (char **)&p, 10);
2716 if (h <= 0)
2717 goto graphic_error;
2718 if (*p == 'x') {
2719 p++;
2720 depth = strtol(p, (char **)&p, 10);
2721 if (depth != 8 && depth != 15 && depth != 16 &&
2722 depth != 24 && depth != 32)
2723 goto graphic_error;
2724 } else if (*p == '\0') {
2725 depth = graphic_depth;
2726 } else {
2727 goto graphic_error;
2728 }
2729
2730 graphic_width = w;
2731 graphic_height = h;
2732 graphic_depth = depth;
2733 }
2734 break;
2735#endif
2736 case QEMU_OPTION_echr:
2737 {
2738 char *r;
2739 term_escape_char = strtol(optarg, &r, 0);
2740 if (r == optarg)
2741 printf("Bad argument to echr\n");
2742 break;
2743 }
2744 case QEMU_OPTION_monitor:
2745 monitor_device = optarg;
2746 break;
2747 case QEMU_OPTION_serial:
2748 if (serial_device_index >= MAX_SERIAL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002749 PANIC("qemu: too many serial ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002750 }
2751 serial_devices[serial_device_index] = optarg;
2752 serial_device_index++;
2753 break;
2754 case QEMU_OPTION_watchdog:
2755 i = select_watchdog(optarg);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002756 if (i > 0) {
2757 if (i == 1) {
2758 PANIC("Invalid watchdog parameter: %s",
2759 optarg);
2760 } else {
2761 QEMU_EXIT(0);
2762 }
2763 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002764 break;
2765 case QEMU_OPTION_watchdog_action:
2766 if (select_watchdog_action(optarg) == -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002767 PANIC("Unknown -watchdog-action parameter");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002768 }
2769 break;
2770 case QEMU_OPTION_virtiocon:
2771 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002772 PANIC("qemu: too many virtio consoles");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002773 }
2774 virtio_consoles[virtio_console_index] = optarg;
2775 virtio_console_index++;
2776 break;
2777 case QEMU_OPTION_parallel:
2778 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002779 PANIC("qemu: too many parallel ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002780 }
2781 parallel_devices[parallel_device_index] = optarg;
2782 parallel_device_index++;
2783 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00002784 case QEMU_OPTION_loadvm:
2785 loadvm = optarg;
2786 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00002787 case QEMU_OPTION_savevm_on_exit:
2788 savevm_on_exit = optarg;
2789 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002790 case QEMU_OPTION_full_screen:
2791 full_screen = 1;
2792 break;
2793#ifdef CONFIG_SDL
2794 case QEMU_OPTION_no_frame:
2795 no_frame = 1;
2796 break;
2797 case QEMU_OPTION_alt_grab:
2798 alt_grab = 1;
2799 break;
2800 case QEMU_OPTION_no_quit:
2801 no_quit = 1;
2802 break;
2803 case QEMU_OPTION_sdl:
2804 display_type = DT_SDL;
2805 break;
2806#endif
2807 case QEMU_OPTION_pidfile:
2808 pid_file = optarg;
2809 break;
2810#ifdef TARGET_I386
2811 case QEMU_OPTION_win2k_hack:
2812 win2k_install_hack = 1;
2813 break;
2814 case QEMU_OPTION_rtc_td_hack:
2815 rtc_td_hack = 1;
2816 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08002817#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002818 case QEMU_OPTION_acpitable:
2819 if(acpi_table_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002820 PANIC("Wrong acpi table provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002821 }
2822 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08002823#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002824 case QEMU_OPTION_smbios:
David 'Digit' Turnerae3098a2011-05-11 16:01:57 +02002825 do_smbios_option(optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002826 break;
2827#endif
2828#ifdef CONFIG_KVM
2829 case QEMU_OPTION_enable_kvm:
2830 kvm_allowed = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002831 break;
David 'Digit' Turner36597752011-05-20 01:18:01 +02002832 case QEMU_OPTION_disable_kvm:
2833 kvm_allowed = 0;
2834 break;
2835#endif /* CONFIG_KVM */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002836 case QEMU_OPTION_usb:
2837 usb_enabled = 1;
2838 break;
2839 case QEMU_OPTION_usbdevice:
2840 usb_enabled = 1;
2841 if (usb_devices_index >= MAX_USB_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002842 PANIC("Too many USB devices");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002843 }
2844 usb_devices[usb_devices_index] = optarg;
2845 usb_devices_index++;
2846 break;
2847 case QEMU_OPTION_smp:
2848 smp_cpus = atoi(optarg);
2849 if (smp_cpus < 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002850 PANIC("Invalid number of CPUs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002851 }
2852 break;
2853 case QEMU_OPTION_vnc:
2854 display_type = DT_VNC;
2855 vnc_display = optarg;
2856 break;
2857#ifdef TARGET_I386
2858 case QEMU_OPTION_no_acpi:
2859 acpi_enabled = 0;
2860 break;
2861 case QEMU_OPTION_no_hpet:
2862 no_hpet = 1;
2863 break;
2864 case QEMU_OPTION_no_virtio_balloon:
2865 no_virtio_balloon = 1;
2866 break;
2867#endif
2868 case QEMU_OPTION_no_reboot:
2869 no_reboot = 1;
2870 break;
2871 case QEMU_OPTION_no_shutdown:
2872 no_shutdown = 1;
2873 break;
2874 case QEMU_OPTION_show_cursor:
2875 cursor_hide = 0;
2876 break;
2877 case QEMU_OPTION_uuid:
2878 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002879 PANIC("Fail to parse UUID string. Wrong format.");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002880 }
2881 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002882 case QEMU_OPTION_option_rom:
2883 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002884 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002885 }
2886 option_rom[nb_option_roms] = optarg;
2887 nb_option_roms++;
2888 break;
2889#if defined(TARGET_ARM) || defined(TARGET_M68K)
2890 case QEMU_OPTION_semihosting:
2891 semihosting_enabled = 1;
2892 break;
2893#endif
2894 case QEMU_OPTION_name:
2895 qemu_name = optarg;
2896 break;
2897#if defined(TARGET_SPARC) || defined(TARGET_PPC)
2898 case QEMU_OPTION_prom_env:
2899 if (nb_prom_envs >= MAX_PROM_ENVS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002900 PANIC("Too many prom variables");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002901 }
2902 prom_envs[nb_prom_envs] = optarg;
2903 nb_prom_envs++;
2904 break;
2905#endif
2906#ifdef TARGET_ARM
2907 case QEMU_OPTION_old_param:
2908 old_param = 1;
2909 break;
2910#endif
2911 case QEMU_OPTION_clock:
2912 configure_alarms(optarg);
2913 break;
2914 case QEMU_OPTION_startdate:
2915 {
2916 struct tm tm;
David 'Digit' Turnerdc468202010-10-27 02:34:46 +02002917 time_t rtc_start_date = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002918 if (!strcmp(optarg, "now")) {
2919 rtc_date_offset = -1;
2920 } else {
2921 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
2922 &tm.tm_year,
2923 &tm.tm_mon,
2924 &tm.tm_mday,
2925 &tm.tm_hour,
2926 &tm.tm_min,
2927 &tm.tm_sec) == 6) {
2928 /* OK */
2929 } else if (sscanf(optarg, "%d-%d-%d",
2930 &tm.tm_year,
2931 &tm.tm_mon,
2932 &tm.tm_mday) == 3) {
2933 tm.tm_hour = 0;
2934 tm.tm_min = 0;
2935 tm.tm_sec = 0;
2936 } else {
2937 goto date_fail;
2938 }
2939 tm.tm_year -= 1900;
2940 tm.tm_mon--;
2941 rtc_start_date = mktimegm(&tm);
2942 if (rtc_start_date == -1) {
2943 date_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002944 PANIC("Invalid date format. Valid format are:\n"
2945 "'now' or '2006-06-17T16:01:21' or '2006-06-17'");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002946 }
2947 rtc_date_offset = time(NULL) - rtc_start_date;
2948 }
2949 }
2950 break;
rich cannings7339b552011-02-16 13:43:44 -08002951
2952 /* -------------------------------------------------------*/
2953 /* User mode network stack restrictions */
2954 case QEMU_OPTION_drop_udp:
2955 slirp_drop_udp();
2956 break;
2957 case QEMU_OPTION_drop_tcp:
2958 slirp_drop_tcp();
2959 break;
2960 case QEMU_OPTION_allow_tcp:
2961 slirp_allow(optarg, IPPROTO_TCP);
2962 break;
2963 case QEMU_OPTION_allow_udp:
2964 slirp_allow(optarg, IPPROTO_UDP);
2965 break;
2966 case QEMU_OPTION_drop_log:
2967 {
2968 FILE* drop_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08002969 drop_log_filename = optarg;
2970 drop_log_fd = fopen(optarg, "w+");
rich cannings7339b552011-02-16 13:43:44 -08002971
2972 if (!drop_log_fd) {
2973 fprintf(stderr, "Cannot open drop log: %s\n", optarg);
2974 exit(1);
2975 }
2976
2977 slirp_drop_log_fd(drop_log_fd);
2978 }
2979 break;
2980
2981 case QEMU_OPTION_dns_log:
2982 {
2983 FILE* dns_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08002984 dns_log_filename = optarg;
2985 dns_log_fd = fopen(optarg, "wb+");
rich cannings7339b552011-02-16 13:43:44 -08002986
2987 if (dns_log_fd == NULL) {
2988 fprintf(stderr, "Cannot open dns log: %s\n", optarg);
2989 exit(1);
2990 }
2991
2992 slirp_dns_log_fd(dns_log_fd);
2993 }
2994 break;
2995
2996
2997 case QEMU_OPTION_max_dns_conns:
2998 {
2999 int max_dns_conns = 0;
3000 if (parse_int(optarg, &max_dns_conns)) {
3001 fprintf(stderr,
3002 "qemu: syntax: -max-dns-conns max_connections\n");
3003 exit(1);
3004 }
3005 if (max_dns_conns <= 0 || max_dns_conns == LONG_MAX) {
3006 fprintf(stderr,
3007 "Invalid arg for max dns connections: %s\n",
3008 optarg);
3009 exit(1);
3010 }
3011 slirp_set_max_dns_conns(max_dns_conns);
3012 }
3013 break;
3014
3015 case QEMU_OPTION_net_forward:
3016 net_slirp_forward(optarg);
3017 break;
3018 case QEMU_OPTION_net_forward_tcp2sink:
3019 {
3020 SockAddress saddr;
3021
3022 if (parse_host_port(&saddr, optarg)) {
3023 fprintf(stderr,
3024 "Invalid ip/port %s for "
3025 "-forward-dropped-tcp2sink. "
3026 "We expect 'sink_ip:sink_port'\n",
3027 optarg);
3028 exit(1);
3029 }
3030 slirp_forward_dropped_tcp2sink(saddr.u.inet.address,
3031 saddr.u.inet.port);
3032 }
3033 break;
3034 /* -------------------------------------------------------*/
3035
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003036 case QEMU_OPTION_tb_size:
3037 tb_size = strtol(optarg, NULL, 0);
3038 if (tb_size < 0)
3039 tb_size = 0;
3040 break;
3041 case QEMU_OPTION_icount:
David Turner6a9ef172010-09-09 22:54:36 +02003042 icount_option = optarg;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003043 break;
3044 case QEMU_OPTION_incoming:
3045 incoming = optarg;
3046 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003047#ifdef CONFIG_XEN
3048 case QEMU_OPTION_xen_domid:
3049 xen_domid = atoi(optarg);
3050 break;
3051 case QEMU_OPTION_xen_create:
3052 xen_mode = XEN_CREATE;
3053 break;
3054 case QEMU_OPTION_xen_attach:
3055 xen_mode = XEN_ATTACH;
3056 break;
3057#endif
3058
3059
3060 case QEMU_OPTION_mic:
3061 audio_input_source = (char*)optarg;
3062 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003063#ifdef CONFIG_NAND
3064 case QEMU_OPTION_nand:
3065 nand_add_dev(optarg);
3066 break;
Jun Nakajimaa381ef02011-12-17 19:13:25 -08003067
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003068#endif
Jun Nakajimaa381ef02011-12-17 19:13:25 -08003069 case QEMU_OPTION_disable_hax:
3070 hax_disabled = 1;
3071 break;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07003072 case QEMU_OPTION_android_ports:
3073 android_op_ports = (char*)optarg;
3074 break;
3075
3076 case QEMU_OPTION_android_port:
3077 android_op_port = (char*)optarg;
3078 break;
3079
3080 case QEMU_OPTION_android_report_console:
3081 android_op_report_console = (char*)optarg;
3082 break;
3083
3084 case QEMU_OPTION_http_proxy:
3085 op_http_proxy = (char*)optarg;
3086 break;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003087
3088 case QEMU_OPTION_charmap:
3089 op_charmap_file = (char*)optarg;
3090 break;
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07003091
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003092 case QEMU_OPTION_android_hw:
3093 android_op_hwini = (char*)optarg;
3094 break;
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07003095
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003096 case QEMU_OPTION_dns_server:
3097 android_op_dns_server = (char*)optarg;
3098 break;
3099
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003100 case QEMU_OPTION_radio:
3101 android_op_radio = (char*)optarg;
3102 break;
3103
3104 case QEMU_OPTION_gps:
3105 android_op_gps = (char*)optarg;
3106 break;
3107
3108 case QEMU_OPTION_audio:
3109 android_op_audio = (char*)optarg;
3110 break;
3111
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003112 case QEMU_OPTION_cpu_delay:
3113 android_op_cpu_delay = (char*)optarg;
3114 break;
3115
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07003116 case QEMU_OPTION_show_kernel:
3117 android_kmsg_init(ANDROID_KMSG_PRINT_MESSAGES);
3118 break;
3119
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003120#ifdef CONFIG_NAND_LIMITS
3121 case QEMU_OPTION_nand_limits:
3122 android_op_nand_limits = (char*)optarg;
3123 break;
3124#endif // CONFIG_NAND_LIMITS
3125
3126 case QEMU_OPTION_netspeed:
3127 android_op_netspeed = (char*)optarg;
3128 break;
3129
3130 case QEMU_OPTION_netdelay:
3131 android_op_netdelay = (char*)optarg;
3132 break;
3133
3134 case QEMU_OPTION_netfast:
3135 android_op_netfast = 1;
3136 break;
3137
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07003138 case QEMU_OPTION_tcpdump:
3139 android_op_tcpdump = (char*)optarg;
3140 break;
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003141
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003142 case QEMU_OPTION_boot_property:
3143 boot_property_parse_option((char*)optarg);
3144 break;
3145
3146 case QEMU_OPTION_lcd_density:
3147 android_op_lcd_density = (char*)optarg;
3148 break;
3149
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003150 case QEMU_OPTION_ui_port:
3151 android_op_ui_port = (char*)optarg;
3152 break;
3153
3154 case QEMU_OPTION_ui_settings:
3155 android_op_ui_settings = (char*)optarg;
3156 break;
3157
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +01003158 case QEMU_OPTION_audio_test_out:
3159 android_audio_test_start_out();
3160 break;
3161
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -08003162 case QEMU_OPTION_android_avdname:
3163 android_op_avd_name = (char*)optarg;
3164 break;
3165
3166 case QEMU_OPTION_timezone:
3167 if (timezone_set((char*)optarg)) {
3168 fprintf(stderr, "emulator: it seems the timezone '%s' is not in zoneinfo format\n",
3169 (char*)optarg);
3170 }
3171 break;
3172
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003173#ifdef CONFIG_MEMCHECK
3174 case QEMU_OPTION_android_memcheck:
3175 android_op_memcheck = (char*)optarg;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003176 /* This will set ro.kernel.memcheck system property
3177 * to memcheck's tracing flags. */
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003178 stralloc_add_format(kernel_config, " memcheck=%s", android_op_memcheck);
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003179 break;
3180#endif // CONFIG_MEMCHECK
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +01003181
3182 case QEMU_OPTION_snapshot_no_time_update:
3183 android_snapshot_update_time = 0;
3184 break;
Vladimir Chtchetkineb8dcaff2011-09-17 11:15:47 -07003185
3186 case QEMU_OPTION_list_webcam:
3187 android_list_web_cameras();
3188 exit(0);
3189
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003190 default:
3191 os_parse_cmd_args(popt->index, optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003192 }
3193 }
3194 }
3195
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003196 /* Initialize character map. */
3197 if (android_charmap_setup(op_charmap_file)) {
3198 if (op_charmap_file) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003199 PANIC(
3200 "Unable to initialize character map from file %s.",
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003201 op_charmap_file);
3202 } else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003203 PANIC(
3204 "Unable to initialize default character map.");
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003205 }
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003206 }
3207
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003208 /* If no data_dir is specified then try to find it relative to the
3209 executable path. */
3210 if (!data_dir) {
3211 data_dir = find_datadir(argv[0]);
3212 }
3213 /* If all else fails use the install patch specified when building. */
3214 if (!data_dir) {
3215 data_dir = CONFIG_QEMU_SHAREDIR;
3216 }
3217
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003218 if (!android_op_hwini) {
3219 PANIC("Missing -android-hw <file> option!");
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003220 }
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003221 hw_ini = iniFile_newFromFile(android_op_hwini);
3222 if (hw_ini == NULL) {
3223 PANIC("Could not find %s file.", android_op_hwini);
3224 }
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01003225
3226 androidHwConfig_init(android_hw, 0);
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003227 androidHwConfig_read(android_hw, hw_ini);
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01003228
Vladimir Chtchetkinedb450d72012-01-12 13:37:40 -08003229 /* If we're loading VM from a snapshot, make sure that the current HW config
3230 * matches the one with which the VM has been saved. */
3231 if (loadvm && *loadvm && !snaphost_match_configs(hw_ini, loadvm)) {
3232 exit(0);
3233 }
3234
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003235 iniFile_free(hw_ini);
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003236
3237 {
3238 int width = android_hw->hw_lcd_width;
3239 int height = android_hw->hw_lcd_height;
3240 int depth = android_hw->hw_lcd_depth;
3241
3242 /* A bit of sanity checking */
3243 if (width <= 0 || height <= 0 ||
3244 (depth != 16 && depth != 32) ||
3245 (((width|height) & 3) != 0) )
3246 {
3247 PANIC("Invalid display configuration (%d,%d,%d)",
3248 width, height, depth);
3249 }
3250 android_display_width = width;
3251 android_display_height = height;
3252 android_display_bpp = depth;
3253 }
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003254
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003255#ifdef CONFIG_NAND_LIMITS
3256 /* Init nand stuff. */
3257 if (android_op_nand_limits) {
3258 parse_nand_limits(android_op_nand_limits);
3259 }
3260#endif // CONFIG_NAND_LIMITS
3261
David 'Digit' Turner53eb66d2011-03-01 14:58:23 +01003262 /* Initialize AVD name from hardware configuration if needed */
3263 if (!android_op_avd_name) {
3264 if (android_hw->avd_name && *android_hw->avd_name) {
3265 android_op_avd_name = android_hw->avd_name;
3266 VERBOSE_PRINT(init,"AVD Name: %s", android_op_avd_name);
3267 }
3268 }
3269
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003270 /* Initialize system partition image */
3271 {
3272 char tmp[PATH_MAX+32];
3273 const char* sysImage = android_hw->disk_systemPartition_path;
3274 const char* initImage = android_hw->disk_systemPartition_initPath;
3275 uint64_t sysBytes = android_hw->disk_systemPartition_size;
3276
3277 if (sysBytes == 0) {
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003278 PANIC("Invalid system partition size: %" PRIu64, sysBytes);
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003279 }
3280
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003281 snprintf(tmp,sizeof(tmp),"system,size=0x%" PRIx64, sysBytes);
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003282
3283 if (sysImage && *sysImage) {
3284 if (filelock_create(sysImage) == NULL) {
3285 fprintf(stderr,"WARNING: System image already in use, changes will not persist!\n");
3286 /* If there is no file= parameters, nand_add_dev will create
3287 * a temporary file to back the partition image. */
3288 } else {
3289 pstrcat(tmp,sizeof(tmp),",file=");
3290 pstrcat(tmp,sizeof(tmp),sysImage);
3291 }
3292 }
3293 if (initImage && *initImage) {
3294 if (!path_exists(initImage)) {
3295 PANIC("Invalid initial system image path: %s", initImage);
3296 }
3297 pstrcat(tmp,sizeof(tmp),",initfile=");
3298 pstrcat(tmp,sizeof(tmp),initImage);
3299 } else {
3300 PANIC("Missing initial system image path!");
3301 }
Deepanshu Gupta04b20f42013-09-11 12:04:27 -07003302 if (android_hw->hw_useext4) {
3303 /* Using a nand device to approximate a block device until full
3304 * support is added */
3305 pstrcat(tmp,sizeof(tmp),",pagesize=512,extrasize=0");
3306 }
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003307 nand_add_dev(tmp);
3308 }
3309
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003310 /* Initialize data partition image */
3311 {
3312 char tmp[PATH_MAX+32];
3313 const char* dataImage = android_hw->disk_dataPartition_path;
3314 const char* initImage = android_hw->disk_dataPartition_initPath;
3315 uint64_t dataBytes = android_hw->disk_dataPartition_size;
3316
3317 if (dataBytes == 0) {
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003318 PANIC("Invalid data partition size: %" PRIu64, dataBytes);
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003319 }
3320
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003321 snprintf(tmp,sizeof(tmp),"userdata,size=0x%" PRIx64, dataBytes);
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003322
3323 if (dataImage && *dataImage) {
3324 if (filelock_create(dataImage) == NULL) {
3325 fprintf(stderr, "WARNING: Data partition already in use. Changes will not persist!\n");
3326 /* Note: if there is no file= parameters, nand_add_dev() will
3327 * create a temporary file to back the partition image. */
3328 } else {
3329 /* Create the file if needed */
3330 if (!path_exists(dataImage)) {
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003331 if (path_empty_file(dataImage) < 0) {
3332 PANIC("Could not create data image file %s: %s", dataImage, strerror(errno));
3333 }
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003334 }
3335 pstrcat(tmp, sizeof(tmp), ",file=");
3336 pstrcat(tmp, sizeof(tmp), dataImage);
3337 }
3338 }
3339 if (initImage && *initImage) {
3340 pstrcat(tmp, sizeof(tmp), ",initfile=");
3341 pstrcat(tmp, sizeof(tmp), initImage);
3342 }
Deepanshu Gupta04b20f42013-09-11 12:04:27 -07003343 if (android_hw->hw_useext4) {
3344 /* Using a nand device to approximate a block device until full
3345 * support is added */
3346 pstrcat(tmp, sizeof(tmp), ",pagesize=512,extrasize=0");
3347 }
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003348 nand_add_dev(tmp);
3349 }
3350
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01003351 /* Init SD-Card stuff. For Android, it is always hda */
3352 /* If the -hda option was used, ignore the Android-provided one */
3353 if (hda_opts == NULL) {
3354 const char* sdPath = android_hw->hw_sdCard_path;
3355 if (sdPath && *sdPath) {
3356 if (!path_exists(sdPath)) {
3357 fprintf(stderr, "WARNING: SD Card image is missing: %s\n", sdPath);
3358 } else if (filelock_create(sdPath) == NULL) {
3359 fprintf(stderr, "WARNING: SD Card image already in use: %s\n", sdPath);
3360 } else {
3361 /* Successful locking */
3362 hda_opts = drive_add(sdPath, HD_ALIAS, 0);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02003363 /* Set this property of any operation involving the SD Card
3364 * will be x100 slower, due to the corresponding file being
3365 * mounted as O_DIRECT. Note that this is only 'unsafe' in
3366 * the context of an emulator crash. The data is already
3367 * synced properly when the emulator exits (either normally or through ^C).
3368 */
3369 qemu_opt_set(hda_opts, "cache", "unsafe");
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01003370 }
3371 }
3372 }
3373
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003374 if (hdb_opts == NULL) {
3375 const char* spath = android_hw->disk_snapStorage_path;
3376 if (spath && *spath) {
3377 if (!path_exists(spath)) {
3378 PANIC("Snapshot storage file does not exist: %s", spath);
3379 }
3380 if (filelock_create(spath) == NULL) {
David 'Digit' Turner4297b822011-05-04 19:18:15 +02003381 PANIC("Snapshot storage already in use: %s", spath);
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003382 }
3383 hdb_opts = drive_add(spath, HD_ALIAS, 1);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02003384 /* See comment above to understand why this is needed. */
David 'Digit' Turner9fb360e2011-05-04 22:01:28 +02003385 qemu_opt_set(hdb_opts, "cache", "unsafe");
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003386 }
3387 }
3388
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003389 /* Set the VM's max heap size, passed as a boot property */
3390 if (android_hw->vm_heapSize > 0) {
3391 char tmp[64];
3392 snprintf(tmp, sizeof(tmp), "%dm", android_hw->vm_heapSize);
3393 boot_property_add("dalvik.vm.heapsize",tmp);
3394 }
3395
Siva Velusamy0faeb3a2013-10-29 14:23:35 -07003396 /* From API 19 and above, the platform provides an explicit property for low memory devices. */
3397 if (android_hw->hw_ramSize <= 512) {
3398 boot_property_add("ro.config.low_ram", "true");
3399 }
3400
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003401 /* Initialize net speed and delays stuff. */
3402 if (android_parse_network_speed(android_op_netspeed) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003403 PANIC("invalid -netspeed parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003404 android_op_netspeed);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003405 }
3406
3407 if ( android_parse_network_latency(android_op_netdelay) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003408 PANIC("invalid -netdelay parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003409 android_op_netdelay);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003410 }
3411
3412 if (android_op_netfast) {
3413 qemu_net_download_speed = 0;
3414 qemu_net_upload_speed = 0;
3415 qemu_net_min_latency = 0;
3416 qemu_net_max_latency = 0;
3417 }
3418
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003419 /* Initialize LCD density */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01003420 if (android_hw->hw_lcd_density) {
3421 long density = android_hw->hw_lcd_density;
3422 if (density <= 0) {
3423 PANIC("Invalid hw.lcd.density value: %ld", density);
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003424 }
3425 hwLcd_setBootProperty(density);
3426 }
3427
Xavier Ducrohetfa0c8e22011-10-04 10:41:26 -07003428 /* Initialize presence of hardware nav button */
3429 boot_property_add("qemu.hw.mainkeys", android_hw->hw_mainKeys ? "1" : "0");
3430
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07003431 /* Initialize TCP dump */
3432 if (android_op_tcpdump) {
3433 if (qemu_tcpdump_start(android_op_tcpdump) < 0) {
3434 fprintf(stdout, "could not start packet capture: %s\n", strerror(errno));
3435 }
3436 }
3437
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003438 /* Initialize modem */
3439 if (android_op_radio) {
3440 CharDriverState* cs = qemu_chr_open("radio", android_op_radio, NULL);
3441 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003442 PANIC("unsupported character device specification: %s\n"
3443 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003444 android_op_radio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003445 }
3446 android_qemud_set_channel( ANDROID_QEMUD_GSM, cs);
3447 } else if (android_hw->hw_gsmModem != 0 ) {
3448 if ( android_qemud_get_channel( ANDROID_QEMUD_GSM, &android_modem_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003449 PANIC("could not initialize qemud 'gsm' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003450 }
3451 }
3452
3453 /* Initialize GPS */
3454 if (android_op_gps) {
3455 CharDriverState* cs = qemu_chr_open("gps", android_op_gps, NULL);
3456 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003457 PANIC("unsupported character device specification: %s\n"
3458 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003459 android_op_gps);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003460 }
3461 android_qemud_set_channel( ANDROID_QEMUD_GPS, cs);
3462 } else if (android_hw->hw_gps != 0) {
3463 if ( android_qemud_get_channel( "gps", &android_gps_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003464 PANIC("could not initialize qemud 'gps' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003465 }
3466 }
3467
3468 /* Initialize audio. */
3469 if (android_op_audio) {
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003470 if ( !audio_check_backend_name( 0, android_op_audio ) ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003471 PANIC("'%s' is not a valid audio output backend. see -help-audio-out",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003472 android_op_audio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003473 }
David 'Digit' Turnerf816a752011-06-23 18:40:11 +02003474 setenv("QEMU_AUDIO_DRV", android_op_audio, 1);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003475 }
3476
David 'Digit' Turner5b481492011-04-11 17:37:32 +02003477 /* Initialize OpenGLES emulation */
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02003478 //android_hw_opengles_init();
David 'Digit' Turner5b481492011-04-11 17:37:32 +02003479
Vladimir Chtchetkineae0d8132011-09-13 10:48:02 -07003480 /* Initialize fake camera */
Vladimir Chtchetkine7485c292012-03-19 11:35:29 -07003481 if (strcmp(android_hw->hw_camera_back, "emulated") &&
3482 strcmp(android_hw->hw_camera_front, "emulated")) {
3483 /* Fake camera is not used for camera emulation. */
3484 boot_property_add("qemu.sf.fake_camera", "none");
Vladimir Chtchetkineae0d8132011-09-13 10:48:02 -07003485 } else {
Vladimir Chtchetkine7485c292012-03-19 11:35:29 -07003486 /* Fake camera is used for at least one camera emulation. */
3487 if (!strcmp(android_hw->hw_camera_back, "emulated") &&
3488 !strcmp(android_hw->hw_camera_front, "emulated")) {
3489 /* Fake camera is used for both, front and back camera emulation. */
3490 boot_property_add("qemu.sf.fake_camera", "both");
3491 } else if (!strcmp(android_hw->hw_camera_back, "emulated")) {
3492 boot_property_add("qemu.sf.fake_camera", "back");
3493 } else {
3494 boot_property_add("qemu.sf.fake_camera", "front");
3495 }
Vladimir Chtchetkineae0d8132011-09-13 10:48:02 -07003496 }
3497
Vladimir Chtchetkine035f8052012-05-18 12:19:32 -07003498 /* Set LCD density (if required by -qemu, and AVD is missing it. */
3499 if (android_op_lcd_density && !android_hw->hw_lcd_density) {
3500 int density;
3501 if (parse_int(android_op_lcd_density, &density) || density <= 0) {
3502 PANIC("-lcd-density : %d", density);
3503 }
3504 hwLcd_setBootProperty(density);
3505 }
3506
Vladimir Chtchetkineb8dcaff2011-09-17 11:15:47 -07003507 /* Initialize camera emulation. */
3508 android_camera_service_init();
3509
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003510 if (android_op_cpu_delay) {
3511 char* end;
3512 long delay = strtol(android_op_cpu_delay, &end, 0);
3513 if (end == NULL || *end || delay < 0 || delay > 1000 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003514 PANIC("option -cpu-delay must be an integer between 0 and 1000" );
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003515 }
3516 if (delay > 0)
3517 delay = (1000-delay);
3518
3519 qemu_cpu_delay = (int) delay;
3520 }
3521
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003522 if (android_op_dns_server) {
3523 char* x = strchr(android_op_dns_server, ',');
3524 dns_count = 0;
3525 if (x == NULL)
3526 {
3527 if ( add_dns_server( android_op_dns_server ) == 0 )
3528 dns_count = 1;
3529 }
3530 else
3531 {
3532 x = android_op_dns_server;
3533 while (*x) {
3534 char* y = strchr(x, ',');
3535
3536 if (y != NULL) {
3537 *y = 0;
3538 y++;
3539 } else {
3540 y = x + strlen(x);
3541 }
3542
3543 if (y > x && add_dns_server( x ) == 0) {
3544 dns_count += 1;
3545 }
3546 x = y;
3547 }
3548 }
3549 if (dns_count == 0)
3550 fprintf( stdout, "### WARNING: will use system default DNS server\n" );
3551 }
3552
3553 if (dns_count == 0)
3554 dns_count = slirp_get_system_dns_servers();
3555 if (dns_count) {
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003556 stralloc_add_format(kernel_config, " ndns=%d", dns_count);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003557 }
3558
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003559#ifdef CONFIG_MEMCHECK
3560 if (android_op_memcheck) {
3561 memcheck_init(android_op_memcheck);
3562 }
3563#endif // CONFIG_MEMCHECK
3564
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003565 /* Initialize cache partition, if any */
3566 if (android_hw->disk_cachePartition != 0) {
3567 char tmp[PATH_MAX+32];
3568 const char* partPath = android_hw->disk_cachePartition_path;
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003569 uint64_t partSize = android_hw->disk_cachePartition_size;
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003570
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003571 snprintf(tmp,sizeof(tmp),"cache,size=0x%" PRIx64, partSize);
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003572
3573 if (partPath && *partPath && strcmp(partPath, "<temp>") != 0) {
3574 if (filelock_create(partPath) == NULL) {
3575 fprintf(stderr, "WARNING: Cache partition already in use. Changes will not persist!\n");
3576 /* Note: if there is no file= parameters, nand_add_dev() will
3577 * create a temporary file to back the partition image. */
3578 } else {
3579 /* Create the file if needed */
3580 if (!path_exists(partPath)) {
3581 if (path_empty_file(partPath) < 0) {
3582 PANIC("Could not create cache image file %s: %s", partPath, strerror(errno));
3583 }
3584 }
3585 pstrcat(tmp, sizeof(tmp), ",file=");
3586 pstrcat(tmp, sizeof(tmp), partPath);
3587 }
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003588 }
Deepanshu Gupta04b20f42013-09-11 12:04:27 -07003589 if (android_hw->hw_useext4) {
3590 /* Using a nand device to approximate a block device until full
3591 * support is added */
3592 pstrcat(tmp, sizeof(tmp), ",pagesize=512,extrasize=0");
3593 }
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003594 nand_add_dev(tmp);
3595 }
3596
Jesse Hall733fffa2012-04-26 11:07:32 -07003597 /* qemu.gles will be read by the OpenGL ES emulation libraries.
3598 * If set to 0, the software GL ES renderer will be used as a fallback.
3599 * If the parameter is undefined, this means the system image runs
3600 * inside an emulator that doesn't support GPU emulation at all.
3601 *
3602 * We always start the GL ES renderer so we can gather stats on the
3603 * underlying GL implementation. If GL ES acceleration is disabled,
3604 * we just shut it down again once we have the strings. */
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003605 {
Jesse Hall733fffa2012-04-26 11:07:32 -07003606 int qemu_gles = 0;
Jesse Hall733fffa2012-04-26 11:07:32 -07003607 if (android_initOpenglesEmulation() == 0 &&
Jesse Hallba5c1f62012-05-08 15:44:35 -07003608 android_startOpenglesRenderer(android_hw->hw_lcd_width, android_hw->hw_lcd_height) == 0)
Jesse Hall733fffa2012-04-26 11:07:32 -07003609 {
3610 android_getOpenglesHardwareStrings(
3611 android_gl_vendor, sizeof(android_gl_vendor),
3612 android_gl_renderer, sizeof(android_gl_renderer),
3613 android_gl_version, sizeof(android_gl_version));
3614 if (android_hw->hw_gpu_enabled) {
3615 qemu_gles = 1;
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003616 } else {
Jesse Hall733fffa2012-04-26 11:07:32 -07003617 android_stopOpenglesRenderer();
3618 qemu_gles = 0;
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003619 }
Jesse Hall733fffa2012-04-26 11:07:32 -07003620 } else {
3621 dwarning("Could not initialize OpenglES emulation, using software renderer.");
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003622 }
Jesse Hall733fffa2012-04-26 11:07:32 -07003623 if (qemu_gles) {
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003624 stralloc_add_str(kernel_params, " qemu.gles=1");
3625 } else {
3626 stralloc_add_str(kernel_params, " qemu.gles=0");
3627 }
3628 }
3629
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003630 /* We always force qemu=1 when running inside QEMU */
3631 stralloc_add_str(kernel_params, " qemu=1");
3632
3633 /* We always initialize the first serial port for the android-kmsg
3634 * character device (used to send kernel messages) */
3635 serial_hds_add_at(0, "android-kmsg");
3636 stralloc_add_str(kernel_params, " console=ttyS0");
3637
3638 /* We always initialize the second serial port for the android-qemud
3639 * character device as well */
3640 serial_hds_add_at(1, "android-qemud");
3641 stralloc_add_str(kernel_params, " android.qemud=ttyS1");
3642
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003643 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3644 os_pidfile_error();
3645 exit(1);
3646 }
3647
David 'Digit' Turner7e9f6c12014-01-22 18:42:40 +01003648 /* Open the logfile at this point, if necessary. We can't open the logfile
3649 * when encountering either of the logging options (-d or -D) because the
3650 * other one may be encountered later on the command line, changing the
3651 * location or level of logging.
3652 */
3653 if (log_mask) {
3654 int mask;
3655 if (log_file) {
3656 qemu_set_log_filename(log_file);
3657 }
3658
3659 mask = qemu_str_to_log_mask(log_mask);
3660 if (!mask) {
3661 qemu_print_log_usage(stdout);
3662 exit(1);
3663 }
3664 qemu_set_log(mask);
3665 }
3666
David 'Digit' Turner36597752011-05-20 01:18:01 +02003667#if defined(CONFIG_KVM)
3668 if (kvm_allowed < 0) {
3669 kvm_allowed = kvm_check_allowed();
3670 }
3671#endif
3672
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003673 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3674 if (smp_cpus > machine->max_cpus) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003675 PANIC("Number of SMP cpus requested (%d), exceeds max cpus "
3676 "supported by machine `%s' (%d)", smp_cpus, machine->name,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003677 machine->max_cpus);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003678 }
3679
3680 if (display_type == DT_NOGRAPHIC) {
3681 if (serial_device_index == 0)
3682 serial_devices[0] = "stdio";
3683 if (parallel_device_index == 0)
3684 parallel_devices[0] = "null";
3685 if (strncmp(monitor_device, "vc", 2) == 0)
3686 monitor_device = "stdio";
3687 }
3688
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003689 if (qemu_init_main_loop()) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003690 PANIC("qemu_init_main_loop failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003691 }
David 'Digit' Turner0b019492011-03-01 14:02:42 +01003692
3693 if (kernel_filename == NULL) {
3694 kernel_filename = android_hw->kernel_path;
3695 }
3696 if (initrd_filename == NULL) {
3697 initrd_filename = android_hw->disk_ramdisk_path;
3698 }
3699
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003700 linux_boot = (kernel_filename != NULL);
3701 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
3702
3703 if (!linux_boot && *kernel_cmdline != '\0') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003704 PANIC("-append only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003705 }
3706
3707 if (!linux_boot && initrd_filename != NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003708 PANIC("-initrd only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003709 }
3710
3711 /* boot to floppy or the default cd if no hard disk defined yet */
3712 if (!boot_devices[0]) {
3713 boot_devices = "cad";
3714 }
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003715 os_set_line_buffering();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003716
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003717 if (init_timer_alarm() < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003718 PANIC("could not initialize alarm timer");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003719 }
David Turner6a9ef172010-09-09 22:54:36 +02003720 configure_icount(icount_option);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003721
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003722 /* init network clients */
3723 if (nb_net_clients == 0) {
3724 /* if no clients, we use a default config */
3725 net_clients[nb_net_clients++] = "nic";
3726#ifdef CONFIG_SLIRP
3727 net_clients[nb_net_clients++] = "user";
3728#endif
3729 }
3730
3731 for(i = 0;i < nb_net_clients; i++) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003732 if (net_client_parse(net_clients[i]) < 0) {
3733 PANIC("Unable to parse net clients");
3734 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003735 }
3736 net_client_check();
3737
3738#ifdef TARGET_I386
3739 /* XXX: this should be moved in the PC machine instantiation code */
3740 if (net_boot != 0) {
3741 int netroms = 0;
3742 for (i = 0; i < nb_nics && i < 4; i++) {
3743 const char *model = nd_table[i].model;
3744 char buf[1024];
3745 char *filename;
3746 if (net_boot & (1 << i)) {
3747 if (model == NULL)
3748 model = "ne2k_pci";
3749 snprintf(buf, sizeof(buf), "pxe-%s.bin", model);
3750 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, buf);
3751 if (filename && get_image_size(filename) > 0) {
3752 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003753 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003754 }
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01003755 option_rom[nb_option_roms] = g_strdup(buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003756 nb_option_roms++;
3757 netroms++;
3758 }
3759 if (filename) {
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01003760 g_free(filename);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003761 }
3762 }
3763 }
3764 if (netroms == 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003765 PANIC("No valid PXE rom found for network device");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003766 }
3767 }
3768#endif
3769
3770 /* init the bluetooth world */
3771 for (i = 0; i < nb_bt_opts; i++)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003772 if (bt_parse(bt_opts[i])) {
3773 PANIC("Unable to parse bluetooth options");
3774 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003775
3776 /* init the memory */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01003777 if (ram_size == 0) {
3778 ram_size = android_hw->hw_ramSize * 1024LL * 1024;
3779 if (ram_size == 0) {
3780 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3781 }
3782 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003783
Vladimir Chtchetkinecf289fb2012-05-14 08:04:29 -07003784 /* Quite often (especially on older XP machines) attempts to allocate large
3785 * VM RAM is going to fail, and crash the emulator. Since it's failing deep
3786 * inside QEMU, it's not really possible to provide the user with a
3787 * meaningful explanation for the crash. So, lets see if QEMU is going to be
3788 * able to allocate requested amount of RAM, and if not, lets try to come up
3789 * with a recomendation. */
3790 {
3791 ram_addr_t r_ram = ram_size;
3792 void* alloc_check = malloc(r_ram);
3793 while (alloc_check == NULL && r_ram > 1024 * 1024) {
3794 /* Make it 25% less */
3795 r_ram -= r_ram / 4;
3796 alloc_check = malloc(r_ram);
3797 }
3798 if (alloc_check != NULL) {
3799 free(alloc_check);
3800 }
3801 if (r_ram != ram_size) {
3802 /* Requested RAM is too large. Report this, as well as calculated
3803 * recomendation. */
3804 dwarning("Requested RAM size of %dMB is too large for your environment, and is reduced to %dMB.",
3805 (int)(ram_size / 1024 / 1024), (int)(r_ram / 1024 / 1024));
3806 ram_size = r_ram;
3807 }
3808 }
3809
rich canningsd952f282011-03-01 15:40:09 -08003810#ifndef _WIN32
David 'Digit' Turnerf9077a82014-02-10 23:10:47 +01003811 qemu_log_rotation_init();
rich canningsd952f282011-03-01 15:40:09 -08003812#endif
3813
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003814 /* init the dynamic translator */
3815 cpu_exec_init_all(tb_size * 1024 * 1024);
3816
3817 bdrv_init();
3818
3819 /* we always create the cdrom drive, even if no disk is there */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003820#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003821 if (nb_drives_opt < MAX_DRIVES)
3822 drive_add(NULL, CDROM_ALIAS);
3823
3824 /* we always create at least one floppy */
3825
3826 if (nb_drives_opt < MAX_DRIVES)
3827 drive_add(NULL, FD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003828 /* we always create one sd slot, even if no card is in it */
3829
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003830 if (1) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003831 drive_add(NULL, SD_ALIAS);
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003832 }
3833#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003834
3835 /* open the virtual block devices */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003836 if (snapshot)
3837 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3838 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3839 exit(1);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003840
David Turner6a9ef172010-09-09 22:54:36 +02003841 //register_savevm("timer", 0, 2, timer_save, timer_load, &timers_state);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003842 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
3843
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003844 /* must be after terminal init, SDL library changes signal handlers */
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003845 os_setup_signal_handling();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003846
3847 /* Maintain compatibility with multiple stdio monitors */
3848 if (!strcmp(monitor_device,"stdio")) {
3849 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
3850 const char *devname = serial_devices[i];
3851 if (devname && !strcmp(devname,"mon:stdio")) {
3852 monitor_device = NULL;
3853 break;
3854 } else if (devname && !strcmp(devname,"stdio")) {
3855 monitor_device = NULL;
3856 serial_devices[i] = "mon:stdio";
3857 break;
3858 }
3859 }
3860 }
3861
3862 if (nb_numa_nodes > 0) {
3863 int i;
3864
3865 if (nb_numa_nodes > smp_cpus) {
3866 nb_numa_nodes = smp_cpus;
3867 }
3868
3869 /* If no memory size if given for any node, assume the default case
3870 * and distribute the available memory equally across all nodes
3871 */
3872 for (i = 0; i < nb_numa_nodes; i++) {
3873 if (node_mem[i] != 0)
3874 break;
3875 }
3876 if (i == nb_numa_nodes) {
3877 uint64_t usedmem = 0;
3878
3879 /* On Linux, the each node's border has to be 8MB aligned,
3880 * the final node gets the rest.
3881 */
3882 for (i = 0; i < nb_numa_nodes - 1; i++) {
3883 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3884 usedmem += node_mem[i];
3885 }
3886 node_mem[i] = ram_size - usedmem;
3887 }
3888
3889 for (i = 0; i < nb_numa_nodes; i++) {
3890 if (node_cpumask[i] != 0)
3891 break;
3892 }
3893 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3894 * must cope with this anyway, because there are BIOSes out there in
3895 * real machines which also use this scheme.
3896 */
3897 if (i == nb_numa_nodes) {
3898 for (i = 0; i < smp_cpus; i++) {
3899 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3900 }
3901 }
3902 }
3903
3904 if (kvm_enabled()) {
3905 int ret;
3906
3907 ret = kvm_init(smp_cpus);
3908 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003909 PANIC("failed to initialize KVM");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003910 }
3911 }
3912
Jun Nakajimaa381ef02011-12-17 19:13:25 -08003913#ifdef CONFIG_HAX
3914 if (!hax_disabled)
3915 {
3916 int ret;
3917
Jiang, Yunhong4a5a0ef2012-02-23 06:31:12 +08003918 hax_set_ramsize(ram_size);
Jun Nakajimaa381ef02011-12-17 19:13:25 -08003919 ret = hax_init(smp_cpus);
3920 fprintf(stderr, "HAX is %s and emulator runs in %s mode\n",
3921 !ret ? "working" :"not working", !ret ? "fast virt" : "emulation");
3922 }
3923#endif
3924
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003925 if (monitor_device) {
3926 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
3927 if (!monitor_hd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003928 PANIC("qemu: could not open monitor device '%s'",
3929 monitor_device);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003930 }
3931 }
3932
3933 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003934 serial_hds_add(serial_devices[i]);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003935 }
3936
3937 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
3938 const char *devname = parallel_devices[i];
3939 if (devname && strcmp(devname, "none")) {
3940 char label[32];
3941 snprintf(label, sizeof(label), "parallel%d", i);
3942 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
3943 if (!parallel_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003944 PANIC("qemu: could not open parallel device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003945 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003946 }
3947 }
3948 }
3949
3950 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
3951 const char *devname = virtio_consoles[i];
3952 if (devname && strcmp(devname, "none")) {
3953 char label[32];
3954 snprintf(label, sizeof(label), "virtcon%d", i);
3955 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
3956 if (!virtcon_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003957 PANIC("qemu: could not open virtio console '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003958 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003959 }
3960 }
3961 }
3962
3963 module_call_init(MODULE_INIT_DEVICE);
3964
3965
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01003966 /* Check the CPU Architecture value */
3967#if defined(TARGET_ARM)
3968 if (strcmp(android_hw->hw_cpu_arch,"arm") != 0) {
3969 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'arm'\n",
3970 android_hw->hw_cpu_arch);
3971 exit(1);
3972 }
Vladimir Chtchetkine33f89d02011-09-28 09:19:09 -07003973#elif defined(TARGET_I386)
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01003974 if (strcmp(android_hw->hw_cpu_arch,"x86") != 0) {
3975 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'x86'\n",
3976 android_hw->hw_cpu_arch);
3977 exit(1);
3978 }
3979#endif
3980
3981 /* Grab CPU model if provided in hardware.ini */
3982 if ( !cpu_model
3983 && android_hw->hw_cpu_model
3984 && android_hw->hw_cpu_model[0] != '\0')
3985 {
3986 cpu_model = android_hw->hw_cpu_model;
3987 }
3988
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003989 /* Combine kernel command line passed from the UI with parameters
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003990 * collected during initialization.
3991 *
3992 * The order is the following:
3993 * - parameters from the hw configuration (kernel.parameters)
3994 * - additionnal parameters from options (e.g. -memcheck)
3995 * - the -append parameters.
3996 */
3997 {
3998 const char* kernel_parameters;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003999
David 'Digit' Turner0b019492011-03-01 14:02:42 +01004000 if (android_hw->kernel_parameters) {
David 'Digit' Turner62c7ae52011-03-08 15:46:53 +01004001 stralloc_add_c(kernel_params, ' ');
David 'Digit' Turner0b019492011-03-01 14:02:42 +01004002 stralloc_add_str(kernel_params, android_hw->kernel_parameters);
4003 }
4004
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004005 /* If not empty, kernel_config always contains a leading space */
4006 stralloc_append(kernel_params, kernel_config);
4007
4008 if (*kernel_cmdline) {
4009 stralloc_add_c(kernel_params, ' ');
4010 stralloc_add_str(kernel_params, kernel_cmdline);
4011 }
4012
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01004013 /* Remove any leading/trailing spaces */
4014 stralloc_strip(kernel_params);
4015
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004016 kernel_parameters = stralloc_cstr(kernel_params);
4017 VERBOSE_PRINT(init, "Kernel parameters: %s", kernel_parameters);
4018
4019 machine->init(ram_size,
4020 boot_devices,
4021 kernel_filename,
4022 kernel_parameters,
4023 initrd_filename,
4024 cpu_model);
4025
Vladimir Chtchetkine8dd31e82012-02-15 17:16:04 -08004026 /* Initialize multi-touch emulation. */
Vladimir Chtchetkine863d1012012-03-16 12:25:23 -07004027 if (androidHwConfig_isScreenMultiTouch(android_hw)) {
Vladimir Chtchetkine8dd31e82012-02-15 17:16:04 -08004028 mts_port_create(NULL);
4029 }
4030
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004031 stralloc_reset(kernel_params);
4032 stralloc_reset(kernel_config);
4033 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004034
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004035 for (env = first_cpu; env != NULL; env = env->next_cpu) {
4036 for (i = 0; i < nb_numa_nodes; i++) {
4037 if (node_cpumask[i] & (1 << env->cpu_index)) {
4038 env->numa_node = i;
4039 }
4040 }
4041 }
4042
4043 current_machine = machine;
4044
David 'Digit' Turnere2678e12014-01-16 15:56:43 +01004045 /* Set KVM's vcpu state to qemu's initial CPUOldState. */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004046 if (kvm_enabled()) {
4047 int ret;
4048
4049 ret = kvm_sync_vcpus();
4050 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004051 PANIC("failed to initialize vcpus");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004052 }
4053 }
4054
Jun Nakajimaa381ef02011-12-17 19:13:25 -08004055#ifdef CONFIG_HAX
4056 if (hax_enabled())
4057 hax_sync_vcpus();
4058#endif
4059
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004060 /* init USB devices */
4061 if (usb_enabled) {
4062 for(i = 0; i < usb_devices_index; i++) {
4063 if (usb_device_add(usb_devices[i], 0) < 0) {
4064 fprintf(stderr, "Warning: could not add USB device %s\n",
4065 usb_devices[i]);
4066 }
4067 }
4068 }
4069
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08004070 /* just use the first displaystate for the moment */
David 'Digit' Turner94702b02011-01-20 02:46:33 +01004071 ds = get_displaystate();
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08004072
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004073 /* Initialize display from the command line parameters. */
4074 android_display_reset(ds,
4075 android_display_width,
4076 android_display_height,
4077 android_display_bpp);
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07004078
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004079 if (display_type == DT_DEFAULT) {
4080#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4081 display_type = DT_SDL;
4082#else
4083 display_type = DT_VNC;
4084 vnc_display = "localhost:0,to=99";
4085 show_vnc_port = 1;
4086#endif
4087 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004088
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004089
4090 switch (display_type) {
4091 case DT_NOGRAPHIC:
4092 break;
4093#if defined(CONFIG_CURSES)
4094 case DT_CURSES:
4095 curses_display_init(ds, full_screen);
4096 break;
4097#endif
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07004098#if defined(CONFIG_SDL) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004099 case DT_SDL:
4100 sdl_display_init(ds, full_screen, no_frame);
4101 break;
4102#elif defined(CONFIG_COCOA)
4103 case DT_SDL:
4104 cocoa_display_init(ds, full_screen);
4105 break;
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08004106#elif defined(CONFIG_STANDALONE_CORE)
4107 case DT_SDL:
Vladimir Chtchetkinee95660a2010-12-20 08:28:03 -08004108 coredisplay_init(ds);
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08004109 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004110#endif
4111 case DT_VNC:
4112 vnc_display_init(ds);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004113 if (vnc_display_open(ds, vnc_display) < 0) {
4114 PANIC("Unable to initialize VNC display");
4115 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004116
4117 if (show_vnc_port) {
4118 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
4119 }
4120 break;
4121 default:
4122 break;
4123 }
4124 dpy_resize(ds);
4125
4126 dcl = ds->listeners;
4127 while (dcl != NULL) {
4128 if (dcl->dpy_refresh != NULL) {
David 'Digit' Turnerdcda9492014-02-16 15:13:55 +01004129 ds->gui_timer = timer_new(QEMU_CLOCK_REALTIME, SCALE_MS, gui_update, ds);
4130 timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004131 }
4132 dcl = dcl->next;
4133 }
4134
4135 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
David 'Digit' Turnerdcda9492014-02-16 15:13:55 +01004136 nographic_timer = timer_new(QEMU_CLOCK_REALTIME, SCALE_MS, nographic_update, NULL);
4137 timer_mod(nographic_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004138 }
4139
David 'Digit' Turner94702b02011-01-20 02:46:33 +01004140 text_consoles_set_display(ds);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004141 qemu_chr_initial_reset();
4142
4143 if (monitor_device && monitor_hd)
4144 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
4145
4146 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
4147 const char *devname = serial_devices[i];
4148 if (devname && strcmp(devname, "none")) {
4149 if (strstart(devname, "vc", 0))
4150 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
4151 }
4152 }
4153
4154 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
4155 const char *devname = parallel_devices[i];
4156 if (devname && strcmp(devname, "none")) {
4157 if (strstart(devname, "vc", 0))
4158 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
4159 }
4160 }
4161
4162 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
4163 const char *devname = virtio_consoles[i];
4164 if (virtcon_hds[i] && devname) {
4165 if (strstart(devname, "vc", 0))
4166 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
4167 }
4168 }
4169
4170 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004171 PANIC("qemu: could not open gdbserver on device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004172 gdbstub_dev);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004173 }
4174
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004175 /* call android-specific setup function */
4176 android_emulation_setup();
4177
Vladimir Chtchetkine57584042011-01-20 16:15:30 -08004178#if !defined(CONFIG_STANDALONE_CORE)
4179 // For the standalone emulator (UI+core in one executable) we need to
4180 // set the window title here.
4181 android_emulator_set_base_port(android_base_port);
4182#endif
4183
Ot ten Thije871da2a2010-09-20 10:29:22 +01004184 if (loadvm)
4185 do_loadvm(cur_mon, loadvm);
4186
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004187 if (incoming) {
4188 autostart = 0; /* fixme how to deal with -daemonize */
4189 qemu_start_incoming_migration(incoming);
4190 }
4191
4192 if (autostart)
4193 vm_start();
4194
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02004195 os_setup_post();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004196
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004197#ifdef CONFIG_ANDROID
4198 // This will notify the UI that the core is successfuly initialized
4199 android_core_init_completed();
4200#endif // CONFIG_ANDROID
4201
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004202 main_loop();
4203 quit_timers();
4204 net_cleanup();
4205 android_emulation_teardown();
4206 return 0;
4207}
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07004208
4209void
4210android_emulation_teardown(void)
4211{
4212 android_charmap_done();
4213}