blob: e8a3779318ce3ee8f4260351124626b583f27a2e [file] [log] [blame]
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25/* the following is needed on Linux to define ptsname() in stdlib.h */
26#if defined(__linux__)
27#define _GNU_SOURCE 1
28#endif
29
30#include "qemu-common.h"
31#include "hw/hw.h"
32#include "hw/boards.h"
33#include "hw/usb.h"
34#include "hw/pcmcia.h"
David 'Digit' Turner2ec695a2013-12-17 10:03:39 +010035#include "hw/i386/pc.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070036#include "hw/audiodev.h"
David 'Digit' Turner2ec695a2013-12-17 10:03:39 +010037#include "hw/isa/isa.h"
David 'Digit' Turner1365eb22014-02-16 22:23:26 +010038#include "hw/loader.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070039#include "hw/baum.h"
David 'Digit' Turnerf0665422013-12-17 10:47:09 +010040#include "hw/android/goldfish/nand.h"
David 'Digit' Turnercc330d42013-12-14 23:26:42 +010041#include "net/net.h"
David 'Digit' Turner1c31e3e2013-12-14 20:07:17 +010042#include "ui/console.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +010043#include "sysemu/sysemu.h"
David 'Digit' Turner852088c2013-12-14 23:04:12 +010044#include "exec/gdbstub.h"
David 'Digit' Turner7e9f6c12014-01-22 18:42:40 +010045#include "qemu/log.h"
David 'Digit' Turner7a78db72013-12-14 11:46:01 +010046#include "qemu/timer.h"
David 'Digit' Turnere7216d82013-12-15 00:51:13 +010047#include "sysemu/char.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +010048#include "sysemu/blockdev.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070049#include "audio/audio.h"
50
David 'Digit' Turner28a09b62013-12-15 00:16:00 +010051#include "migration/qemu-file.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070052#include "android/android.h"
David 'Digit' Turnere1e03df2013-12-15 00:42:21 +010053#include "android/charpipe.h"
David 'Digit' Turnerf9077a82014-02-10 23:10:47 +010054#include "android/log-rotate.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070055#include "modem_driver.h"
David 'Digit' Turner890f4642014-02-24 11:53:03 +010056#include "android/filesystems/ext4_utils.h"
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +020057#include "android/filesystems/partition_types.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070058#include "android/gps.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070059#include "android/hw-kmsg.h"
David 'Digit' Turnerc0ac7332011-05-02 15:05:35 +020060#include "android/hw-pipe-net.h"
61#include "android/hw-qemud.h"
Vladimir Chtchetkinec646f5e2011-09-03 15:17:13 -070062#include "android/camera/camera-service.h"
Vladimir Chtchetkine8dd31e82012-02-15 17:16:04 -080063#include "android/multitouch-port.h"
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -070064#include "android/charmap.h"
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -070065#include "android/globals.h"
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -070066#include "android/utils/bufprint.h"
David 'Digit' Turner5f824112011-03-01 14:00:26 +010067#include "android/utils/debug.h"
David 'Digit' Turner48a3c662011-03-01 14:03:20 +010068#include "android/utils/filelock.h"
69#include "android/utils/path.h"
David 'Digit' Turner5f824112011-03-01 14:00:26 +010070#include "android/utils/stralloc.h"
David 'Digit' Turner40841b22011-03-01 14:04:00 +010071#include "android/utils/tempfile.h"
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -080072#include "android/display-core.h"
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -080073#include "android/utils/timezone.h"
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +010074#include "android/snapshot.h"
David 'Digit' Turnercb88e792011-08-26 01:35:14 +020075#include "android/opengles.h"
Vladimir Chtchetkine1a820e92012-04-11 13:22:48 -070076#include "android/multitouch-screen.h"
David 'Digit' Turnerbcde1092014-01-09 23:19:19 +010077#include "exec/hwaddr.h"
David 'Digit' Turner6e2eb782013-12-15 00:54:21 +010078#include "android/tcpdump.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070079
David 'Digit' Turner5bb450e2014-03-14 17:19:45 +010080#ifdef CONFIG_ANDROID_MEMCHECK
David 'Digit' Turner96e493a2014-03-14 17:17:26 +010081#include "android/qemu/memcheck/memcheck.h"
David 'Digit' Turner5bb450e2014-03-14 17:19:45 +010082#endif // CONFIG_ANDROID_MEMCHECK
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -070083
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070084#include <unistd.h>
85#include <fcntl.h>
86#include <signal.h>
87#include <time.h>
88#include <errno.h>
89#include <sys/time.h>
90#include <zlib.h>
91
David 'Digit' Turner2c538c82010-05-10 16:48:20 -070092/* Needed early for CONFIG_BSD etc. */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070093#include "config-host.h"
94
95#ifndef _WIN32
96#include <libgen.h>
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070097#include <sys/times.h>
98#include <sys/wait.h>
99#include <termios.h>
100#include <sys/mman.h>
101#include <sys/ioctl.h>
102#include <sys/resource.h>
103#include <sys/socket.h>
104#include <netinet/in.h>
105#include <net/if.h>
106#if defined(__NetBSD__)
107#include <net/if_tap.h>
108#endif
109#ifdef __linux__
110#include <linux/if_tun.h>
111#endif
112#include <arpa/inet.h>
113#include <dirent.h>
114#include <netdb.h>
115#include <sys/select.h>
David 'Digit' Turner2c538c82010-05-10 16:48:20 -0700116#ifdef CONFIG_BSD
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700117#include <sys/stat.h>
118#if defined(__FreeBSD__) || defined(__DragonFly__)
119#include <libutil.h>
120#else
121#include <util.h>
122#endif
123#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
124#include <freebsd/stdlib.h>
125#else
126#ifdef __linux__
127#include <pty.h>
128#include <malloc.h>
129#include <linux/rtc.h>
130
131/* For the benefit of older linux systems which don't supply it,
132 we use a local copy of hpet.h. */
133/* #include <linux/hpet.h> */
David 'Digit' Turner2dbdad52013-12-17 09:24:18 +0100134#include "hw/timer/hpet.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700135
136#include <linux/ppdev.h>
137#include <linux/parport.h>
138#endif
139#ifdef __sun__
140#include <sys/stat.h>
141#include <sys/ethernet.h>
142#include <sys/sockio.h>
143#include <netinet/arp.h>
144#include <netinet/in.h>
145#include <netinet/in_systm.h>
146#include <netinet/ip.h>
147#include <netinet/ip_icmp.h> // must come after ip.h
148#include <netinet/udp.h>
149#include <netinet/tcp.h>
150#include <net/if.h>
151#include <syslog.h>
152#include <stropts.h>
153#endif
154#endif
155#endif
156
157#if defined(__OpenBSD__)
158#include <util.h>
159#endif
160
161#if defined(CONFIG_VDE)
162#include <libvdeplug.h>
163#endif
164
165#ifdef _WIN32
166#include <windows.h>
167#include <malloc.h>
168#include <sys/timeb.h>
169#include <mmsystem.h>
170#define getopt_long_only getopt_long
171#define memalign(align, size) malloc(size)
172#endif
173
David 'Digit' Turner11822842013-12-17 09:16:47 +0100174#include "sysemu/cpus.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100175#include "sysemu/arch_init.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700176
177#ifdef CONFIG_COCOA
David 'Digit' Turnerae3098a2011-05-11 16:01:57 +0200178int qemu_main(int argc, char **argv, char **envp);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700179#undef main
180#define main qemu_main
181#endif /* CONFIG_COCOA */
182
183#include "hw/hw.h"
184#include "hw/boards.h"
185#include "hw/usb.h"
186#include "hw/pcmcia.h"
David 'Digit' Turner2ec695a2013-12-17 10:03:39 +0100187#include "hw/i386/pc.h"
188#include "hw/isa/isa.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700189#include "hw/baum.h"
190#include "hw/bt.h"
David 'Digit' Turner7821d632013-12-17 11:27:50 +0100191#include "sysemu/watchdog.h"
David 'Digit' Turner73a93a12013-12-17 11:26:01 +0100192#include "hw/i386/smbios.h"
David 'Digit' Turner50319182013-12-17 11:28:54 +0100193#include "hw/xen/xen.h"
David 'Digit' Turner6800f0a2013-12-17 09:12:54 +0100194#include "sysemu/bt.h"
David 'Digit' Turnercc330d42013-12-14 23:26:42 +0100195#include "net/net.h"
David 'Digit' Turner6af67652013-12-14 23:49:32 +0100196#include "monitor/monitor.h"
David 'Digit' Turner1c31e3e2013-12-14 20:07:17 +0100197#include "ui/console.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100198#include "sysemu/sysemu.h"
David 'Digit' Turner852088c2013-12-14 23:04:12 +0100199#include "exec/gdbstub.h"
David 'Digit' Turner7a78db72013-12-14 11:46:01 +0100200#include "qemu/timer.h"
David 'Digit' Turnere7216d82013-12-15 00:51:13 +0100201#include "sysemu/char.h"
David 'Digit' Turner37dc41a2013-12-14 14:45:51 +0100202#include "qemu/cache-utils.h"
David 'Digit' Turnere1e03df2013-12-15 00:42:21 +0100203#include "block/block.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100204#include "sysemu/dma.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700205#include "audio/audio.h"
David 'Digit' Turner28a09b62013-12-15 00:16:00 +0100206#include "migration/migration.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100207#include "sysemu/kvm.h"
David 'Digit' Turnere1e03df2013-12-15 00:42:21 +0100208#include "exec/hax.h"
David 'Digit' Turner36597752011-05-20 01:18:01 +0200209#ifdef CONFIG_KVM
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100210#include "android/kvm.h"
David 'Digit' Turner36597752011-05-20 01:18:01 +0200211#endif
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100212#include "sysemu/balloon.h"
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700213#include "android/hw-lcd.h"
214#include "android/boot-properties.h"
David 'Digit' Turnerca950592011-04-27 12:26:15 +0200215#include "android/hw-control.h"
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700216#include "android/core-init-utils.h"
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +0100217#include "android/audio-test.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700218
Vladimir Chtchetkinedb450d72012-01-12 13:37:40 -0800219#include "android/snaphost-android.h"
220
Vladimir Chtchetkine57584042011-01-20 16:15:30 -0800221#if !defined(CONFIG_STANDALONE_CORE)
222/* in android/qemulator.c */
223extern void android_emulator_set_base_port(int port);
224#endif
225
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700226#if defined(CONFIG_SKINS) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700227#undef main
228#define main qemu_main
229#endif
230
David 'Digit' Turnercc33b2d2013-12-15 00:09:42 +0100231#include "disas/disas.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700232
David 'Digit' Turnerd0edecb2013-12-17 09:32:26 +0100233#include "qemu/sockets.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700234
235#if defined(CONFIG_SLIRP)
236#include "libslirp.h"
237#endif
238
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700239#define DEFAULT_RAM_SIZE 128
240
241/* Max number of USB devices that can be specified on the commandline. */
242#define MAX_USB_CMDLINE 8
243
244/* Max number of bluetooth switches on the commandline. */
245#define MAX_BT_CMDLINE 10
246
247/* XXX: use a two level table to limit memory usage */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700248
249static const char *data_dir;
250const char *bios_name = NULL;
251static void *ioport_opaque[MAX_IOPORTS];
252static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
253static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100254#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700255/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
256 to store the VM snapshots */
257DriveInfo drives_table[MAX_DRIVES+1];
258int nb_drives;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100259#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700260enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700261DisplayType display_type = DT_DEFAULT;
262const char* keyboard_layout = NULL;
263int64_t ticks_per_sec;
264ram_addr_t ram_size;
David 'Digit' Turner0e8e7482014-02-17 20:25:52 +0100265bool xen_allowed;
David 'Digit' Turner280afa02011-05-11 17:37:44 +0200266const char *mem_path = NULL;
267#ifdef MAP_POPULATE
268int mem_prealloc = 0; /* force preallocation of physical target memory */
269#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700270int nb_nics;
271NICInfo nd_table[MAX_NICS];
272int vm_running;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100273int autostart;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700274static int rtc_utc = 1;
275static int rtc_date_offset = -1; /* -1 means no change */
276int cirrus_vga_enabled = 1;
277int std_vga_enabled = 0;
278int vmsvga_enabled = 0;
279int xenfb_enabled = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700280static int full_screen = 0;
281#ifdef CONFIG_SDL
282static int no_frame = 0;
283#endif
284int no_quit = 0;
285CharDriverState *serial_hds[MAX_SERIAL_PORTS];
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +0100286int serial_hds_count;
287
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700288CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
289CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
290#ifdef TARGET_I386
291int win2k_install_hack = 0;
292int rtc_td_hack = 0;
293#endif
294int usb_enabled = 0;
295int singlestep = 0;
296int smp_cpus = 1;
297const char *vnc_display;
298int acpi_enabled = 1;
299int no_hpet = 0;
David 'Digit' Turner045bdbe2014-04-28 22:26:52 +0200300int hax_disabled = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700301int no_virtio_balloon = 0;
302int fd_bootchk = 1;
303int no_reboot = 0;
304int no_shutdown = 0;
305int cursor_hide = 1;
306int graphic_rotate = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700307WatchdogTimerModel *watchdog = NULL;
308int watchdog_action = WDT_RESET;
309const char *option_rom[MAX_OPTION_ROMS];
310int nb_option_roms;
311int semihosting_enabled = 0;
312#ifdef TARGET_ARM
313int old_param = 0;
314#endif
315const char *qemu_name;
316int alt_grab = 0;
317#if defined(TARGET_SPARC) || defined(TARGET_PPC)
318unsigned int nb_prom_envs = 0;
319const char *prom_envs[MAX_PROM_ENVS];
320#endif
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100321#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700322int nb_drives_opt;
323struct drive_opt drives_opt[MAX_DRIVES];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100324#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700325int nb_numa_nodes;
326uint64_t node_mem[MAX_NODES];
327uint64_t node_cpumask[MAX_NODES];
328
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700329static QEMUTimer *nographic_timer;
330
331uint8_t qemu_uuid[16];
332
333
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700334int qemu_cpu_delay;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700335extern char* audio_input_source;
336
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700337extern char* android_op_ports;
338extern char* android_op_port;
339extern char* android_op_report_console;
340extern char* op_http_proxy;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700341// Path to the file containing specific key character map.
342char* op_charmap_file = NULL;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700343
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -0700344/* Path to hardware initialization file passed with -android-hw option. */
345char* android_op_hwini = NULL;
346
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -0700347/* Memory checker options. */
348char* android_op_memcheck = NULL;
349
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -0700350/* -dns-server option value. */
351char* android_op_dns_server = NULL;
352
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -0700353/* -radio option value. */
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700354char* android_op_radio = NULL;
355
356/* -gps option value. */
357char* android_op_gps = NULL;
358
359/* -audio option value. */
360char* android_op_audio = NULL;
361
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700362/* -cpu-delay option value. */
363char* android_op_cpu_delay = NULL;
364
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -0700365#ifdef CONFIG_NAND_LIMITS
366/* -nand-limits option value. */
367char* android_op_nand_limits = NULL;
368#endif // CONFIG_NAND_LIMITS
369
370/* -netspeed option value. */
371char* android_op_netspeed = NULL;
372
373/* -netdelay option value. */
374char* android_op_netdelay = NULL;
375
376/* -netfast option value. */
377int android_op_netfast = 0;
378
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -0700379/* -tcpdump option value. */
380char* android_op_tcpdump = NULL;
381
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700382/* -lcd-density option value. */
383char* android_op_lcd_density = NULL;
384
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700385/* -ui-port option value. This port will be used to report the core
386 * initialization completion.
387 */
388char* android_op_ui_port = NULL;
389
390/* -ui-settings option value. This value will be passed to the UI when new UI
391 * process is attaching to the core.
392 */
393char* android_op_ui_settings = NULL;
394
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -0800395/* -android-avdname option value. */
396char* android_op_avd_name = "unknown";
397
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -0700398extern int android_display_width;
399extern int android_display_height;
400extern int android_display_bpp;
401
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700402extern void dprint( const char* format, ... );
403
rich canningsd952f282011-03-01 15:40:09 -0800404const char* dns_log_filename = NULL;
405const char* drop_log_filename = NULL;
rich canningsd952f282011-03-01 15:40:09 -0800406
Tim Baverstock24204cc2010-11-25 11:37:43 +0000407const char* savevm_on_exit = NULL;
Tim Baverstock24204cc2010-11-25 11:37:43 +0000408
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700409#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
410
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700411/* Reports the core initialization failure to the error stdout and to the UI
412 * socket before exiting the application.
413 * Parameters that are passed to this macro are used to format the error
414 * mesage using sprintf routine.
415 */
416#ifdef CONFIG_ANDROID
417#define PANIC(...) android_core_init_failure(__VA_ARGS__)
418#else
419#define PANIC(...) do { fprintf(stderr, __VA_ARGS__); \
420 exit(1); \
421 } while (0)
422#endif // CONFIG_ANDROID
423
424/* Exits the core during initialization. */
425#ifdef CONFIG_ANDROID
426#define QEMU_EXIT(exit_code) android_core_init_exit(exit_code)
427#else
428#define QEMU_EXIT(exit_code) exit(exit_code)
429#endif // CONFIG_ANDROID
430
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700431/***********************************************************/
432/* x86 ISA bus support */
433
David 'Digit' Turnerbcde1092014-01-09 23:19:19 +0100434hwaddr isa_mem_base = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700435PicState2 *isa_pic;
436
437static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
438static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
439
440static uint32_t ioport_read(int index, uint32_t address)
441{
442 static IOPortReadFunc *default_func[3] = {
443 default_ioport_readb,
444 default_ioport_readw,
445 default_ioport_readl
446 };
447 IOPortReadFunc *func = ioport_read_table[index][address];
448 if (!func)
449 func = default_func[index];
450 return func(ioport_opaque[address], address);
451}
452
453static void ioport_write(int index, uint32_t address, uint32_t data)
454{
455 static IOPortWriteFunc *default_func[3] = {
456 default_ioport_writeb,
457 default_ioport_writew,
458 default_ioport_writel
459 };
460 IOPortWriteFunc *func = ioport_write_table[index][address];
461 if (!func)
462 func = default_func[index];
463 func(ioport_opaque[address], address, data);
464}
465
466static uint32_t default_ioport_readb(void *opaque, uint32_t address)
467{
468#ifdef DEBUG_UNUSED_IOPORT
469 fprintf(stderr, "unused inb: port=0x%04x\n", address);
470#endif
471 return 0xff;
472}
473
474static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
475{
476#ifdef DEBUG_UNUSED_IOPORT
477 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
478#endif
479}
480
481/* default is to make two byte accesses */
482static uint32_t default_ioport_readw(void *opaque, uint32_t address)
483{
484 uint32_t data;
485 data = ioport_read(0, address);
486 address = (address + 1) & (MAX_IOPORTS - 1);
487 data |= ioport_read(0, address) << 8;
488 return data;
489}
490
491static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
492{
493 ioport_write(0, address, data & 0xff);
494 address = (address + 1) & (MAX_IOPORTS - 1);
495 ioport_write(0, address, (data >> 8) & 0xff);
496}
497
498static uint32_t default_ioport_readl(void *opaque, uint32_t address)
499{
500#ifdef DEBUG_UNUSED_IOPORT
501 fprintf(stderr, "unused inl: port=0x%04x\n", address);
502#endif
503 return 0xffffffff;
504}
505
506static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
507{
508#ifdef DEBUG_UNUSED_IOPORT
509 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
510#endif
511}
512
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700513/***************/
514/* ballooning */
515
516static QEMUBalloonEvent *qemu_balloon_event;
517void *qemu_balloon_event_opaque;
518
519void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
520{
521 qemu_balloon_event = func;
522 qemu_balloon_event_opaque = opaque;
523}
524
525void qemu_balloon(ram_addr_t target)
526{
527 if (qemu_balloon_event)
528 qemu_balloon_event(qemu_balloon_event_opaque, target);
529}
530
531ram_addr_t qemu_balloon_status(void)
532{
533 if (qemu_balloon_event)
534 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
535 return 0;
536}
537
538/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700539/* host time/date access */
540void qemu_get_timedate(struct tm *tm, int offset)
541{
542 time_t ti;
543 struct tm *ret;
544
545 time(&ti);
546 ti += offset;
547 if (rtc_date_offset == -1) {
548 if (rtc_utc)
549 ret = gmtime(&ti);
550 else
551 ret = localtime(&ti);
552 } else {
553 ti -= rtc_date_offset;
554 ret = gmtime(&ti);
555 }
556
557 memcpy(tm, ret, sizeof(struct tm));
558}
559
560int qemu_timedate_diff(struct tm *tm)
561{
562 time_t seconds;
563
564 if (rtc_date_offset == -1)
565 if (rtc_utc)
566 seconds = mktimegm(tm);
567 else
568 seconds = mktime(tm);
569 else
570 seconds = mktimegm(tm) + rtc_date_offset;
571
572 return seconds - time(NULL);
573}
574
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700575/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700576/* QEMU Block devices */
577
578#define HD_ALIAS "index=%d,media=disk"
579#define CDROM_ALIAS "index=2,media=cdrom"
580#define FD_ALIAS "index=%d,if=floppy"
581#define PFLASH_ALIAS "if=pflash"
582#define MTD_ALIAS "if=mtd"
583#define SD_ALIAS "index=0,if=sd"
584
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100585static int drive_init_func(QemuOpts *opts, void *opaque)
586{
587 int *use_scsi = opaque;
588 int fatal_error = 0;
589
590 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
591 if (fatal_error)
592 return 1;
593 }
594 return 0;
595}
596
597static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
598{
599 if (NULL == qemu_opt_get(opts, "snapshot")) {
600 qemu_opt_set(opts, "snapshot", "on");
601 }
602 return 0;
603}
604
605#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700606static int drive_opt_get_free_idx(void)
607{
608 int index;
609
610 for (index = 0; index < MAX_DRIVES; index++)
611 if (!drives_opt[index].used) {
612 drives_opt[index].used = 1;
613 return index;
614 }
615
616 return -1;
617}
618
619static int drive_get_free_idx(void)
620{
621 int index;
622
623 for (index = 0; index < MAX_DRIVES; index++)
624 if (!drives_table[index].used) {
625 drives_table[index].used = 1;
626 return index;
627 }
628
629 return -1;
630}
631
632int drive_add(const char *file, const char *fmt, ...)
633{
634 va_list ap;
635 int index = drive_opt_get_free_idx();
636
637 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
638 fprintf(stderr, "qemu: too many drives\n");
639 return -1;
640 }
641
642 drives_opt[index].file = file;
643 va_start(ap, fmt);
644 vsnprintf(drives_opt[index].opt,
645 sizeof(drives_opt[0].opt), fmt, ap);
646 va_end(ap);
David 'Digit' Turner92568952010-04-15 15:04:16 -0700647
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700648 nb_drives_opt++;
649 return index;
650}
651
652void drive_remove(int index)
653{
654 drives_opt[index].used = 0;
655 nb_drives_opt--;
656}
657
658int drive_get_index(BlockInterfaceType type, int bus, int unit)
659{
660 int index;
661
662 /* seek interface, bus and unit */
663
664 for (index = 0; index < MAX_DRIVES; index++)
665 if (drives_table[index].type == type &&
666 drives_table[index].bus == bus &&
667 drives_table[index].unit == unit &&
668 drives_table[index].used)
669 return index;
670
671 return -1;
672}
673
674int drive_get_max_bus(BlockInterfaceType type)
675{
676 int max_bus;
677 int index;
678
679 max_bus = -1;
680 for (index = 0; index < nb_drives; index++) {
681 if(drives_table[index].type == type &&
682 drives_table[index].bus > max_bus)
683 max_bus = drives_table[index].bus;
684 }
685 return max_bus;
686}
687
688const char *drive_get_serial(BlockDriverState *bdrv)
689{
690 int index;
691
692 for (index = 0; index < nb_drives; index++)
693 if (drives_table[index].bdrv == bdrv)
694 return drives_table[index].serial;
695
696 return "\0";
697}
698
699BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
700{
701 int index;
702
703 for (index = 0; index < nb_drives; index++)
704 if (drives_table[index].bdrv == bdrv)
705 return drives_table[index].onerror;
706
707 return BLOCK_ERR_STOP_ENOSPC;
708}
709
710static void bdrv_format_print(void *opaque, const char *name)
711{
712 fprintf(stderr, " %s", name);
713}
714
715void drive_uninit(BlockDriverState *bdrv)
716{
717 int i;
718
719 for (i = 0; i < MAX_DRIVES; i++)
720 if (drives_table[i].bdrv == bdrv) {
721 drives_table[i].bdrv = NULL;
722 drives_table[i].used = 0;
723 drive_remove(drives_table[i].drive_opt_idx);
724 nb_drives--;
725 break;
726 }
727}
728
729int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
730{
731 char buf[128];
732 char file[1024];
733 char devname[128];
734 char serial[21];
735 const char *mediastr = "";
736 BlockInterfaceType type;
737 enum { MEDIA_DISK, MEDIA_CDROM } media;
738 int bus_id, unit_id;
739 int cyls, heads, secs, translation;
740 BlockDriverState *bdrv;
741 BlockDriver *drv = NULL;
742 QEMUMachine *machine = opaque;
743 int max_devs;
744 int index;
745 int cache;
746 int bdrv_flags, onerror;
747 int drives_table_idx;
748 char *str = arg->opt;
749 static const char * const params[] = { "bus", "unit", "if", "index",
750 "cyls", "heads", "secs", "trans",
751 "media", "snapshot", "file",
752 "cache", "format", "serial", "werror",
753 NULL };
754
755 if (check_params(buf, sizeof(buf), params, str) < 0) {
756 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
757 buf, str);
758 return -1;
759 }
760
761 file[0] = 0;
762 cyls = heads = secs = 0;
763 bus_id = 0;
764 unit_id = -1;
765 translation = BIOS_ATA_TRANSLATION_AUTO;
766 index = -1;
767 cache = 3;
768
769 if (machine->use_scsi) {
770 type = IF_SCSI;
771 max_devs = MAX_SCSI_DEVS;
772 pstrcpy(devname, sizeof(devname), "scsi");
773 } else {
774 type = IF_IDE;
775 max_devs = MAX_IDE_DEVS;
776 pstrcpy(devname, sizeof(devname), "ide");
777 }
778 media = MEDIA_DISK;
779
780 /* extract parameters */
781
782 if (get_param_value(buf, sizeof(buf), "bus", str)) {
783 bus_id = strtol(buf, NULL, 0);
784 if (bus_id < 0) {
785 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
786 return -1;
787 }
788 }
789
790 if (get_param_value(buf, sizeof(buf), "unit", str)) {
791 unit_id = strtol(buf, NULL, 0);
792 if (unit_id < 0) {
793 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
794 return -1;
795 }
796 }
797
798 if (get_param_value(buf, sizeof(buf), "if", str)) {
799 pstrcpy(devname, sizeof(devname), buf);
800 if (!strcmp(buf, "ide")) {
801 type = IF_IDE;
802 max_devs = MAX_IDE_DEVS;
803 } else if (!strcmp(buf, "scsi")) {
804 type = IF_SCSI;
805 max_devs = MAX_SCSI_DEVS;
806 } else if (!strcmp(buf, "floppy")) {
807 type = IF_FLOPPY;
808 max_devs = 0;
809 } else if (!strcmp(buf, "pflash")) {
810 type = IF_PFLASH;
811 max_devs = 0;
812 } else if (!strcmp(buf, "mtd")) {
813 type = IF_MTD;
814 max_devs = 0;
815 } else if (!strcmp(buf, "sd")) {
816 type = IF_SD;
817 max_devs = 0;
818 } else if (!strcmp(buf, "virtio")) {
819 type = IF_VIRTIO;
820 max_devs = 0;
821 } else if (!strcmp(buf, "xen")) {
822 type = IF_XEN;
823 max_devs = 0;
824 } else {
825 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
826 return -1;
827 }
828 }
829
830 if (get_param_value(buf, sizeof(buf), "index", str)) {
831 index = strtol(buf, NULL, 0);
832 if (index < 0) {
833 fprintf(stderr, "qemu: '%s' invalid index\n", str);
834 return -1;
835 }
836 }
837
838 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
839 cyls = strtol(buf, NULL, 0);
840 }
841
842 if (get_param_value(buf, sizeof(buf), "heads", str)) {
843 heads = strtol(buf, NULL, 0);
844 }
845
846 if (get_param_value(buf, sizeof(buf), "secs", str)) {
847 secs = strtol(buf, NULL, 0);
848 }
849
850 if (cyls || heads || secs) {
851 if (cyls < 1 || cyls > 16383) {
852 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
853 return -1;
854 }
855 if (heads < 1 || heads > 16) {
856 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
857 return -1;
858 }
859 if (secs < 1 || secs > 63) {
860 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
861 return -1;
862 }
863 }
864
865 if (get_param_value(buf, sizeof(buf), "trans", str)) {
866 if (!cyls) {
867 fprintf(stderr,
868 "qemu: '%s' trans must be used with cyls,heads and secs\n",
869 str);
870 return -1;
871 }
872 if (!strcmp(buf, "none"))
873 translation = BIOS_ATA_TRANSLATION_NONE;
874 else if (!strcmp(buf, "lba"))
875 translation = BIOS_ATA_TRANSLATION_LBA;
876 else if (!strcmp(buf, "auto"))
877 translation = BIOS_ATA_TRANSLATION_AUTO;
878 else {
879 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
880 return -1;
881 }
882 }
883
884 if (get_param_value(buf, sizeof(buf), "media", str)) {
885 if (!strcmp(buf, "disk")) {
886 media = MEDIA_DISK;
887 } else if (!strcmp(buf, "cdrom")) {
888 if (cyls || secs || heads) {
889 fprintf(stderr,
890 "qemu: '%s' invalid physical CHS format\n", str);
891 return -1;
892 }
893 media = MEDIA_CDROM;
894 } else {
895 fprintf(stderr, "qemu: '%s' invalid media\n", str);
896 return -1;
897 }
898 }
899
900 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
901 if (!strcmp(buf, "on"))
902 snapshot = 1;
903 else if (!strcmp(buf, "off"))
904 snapshot = 0;
905 else {
906 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
907 return -1;
908 }
909 }
910
911 if (get_param_value(buf, sizeof(buf), "cache", str)) {
912 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
913 cache = 0;
914 else if (!strcmp(buf, "writethrough"))
915 cache = 1;
916 else if (!strcmp(buf, "writeback"))
917 cache = 2;
918 else {
919 fprintf(stderr, "qemu: invalid cache option\n");
920 return -1;
921 }
922 }
923
924 if (get_param_value(buf, sizeof(buf), "format", str)) {
925 if (strcmp(buf, "?") == 0) {
926 fprintf(stderr, "qemu: Supported formats:");
927 bdrv_iterate_format(bdrv_format_print, NULL);
928 fprintf(stderr, "\n");
929 return -1;
930 }
931 drv = bdrv_find_format(buf);
932 if (!drv) {
933 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
934 return -1;
935 }
936 }
937
938 if (arg->file == NULL)
939 get_param_value(file, sizeof(file), "file", str);
940 else
941 pstrcpy(file, sizeof(file), arg->file);
942
943 if (!get_param_value(serial, sizeof(serial), "serial", str))
944 memset(serial, 0, sizeof(serial));
945
946 onerror = BLOCK_ERR_STOP_ENOSPC;
947 if (get_param_value(buf, sizeof(serial), "werror", str)) {
948 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
949 fprintf(stderr, "werror is no supported by this format\n");
950 return -1;
951 }
952 if (!strcmp(buf, "ignore"))
953 onerror = BLOCK_ERR_IGNORE;
954 else if (!strcmp(buf, "enospc"))
955 onerror = BLOCK_ERR_STOP_ENOSPC;
956 else if (!strcmp(buf, "stop"))
957 onerror = BLOCK_ERR_STOP_ANY;
958 else if (!strcmp(buf, "report"))
959 onerror = BLOCK_ERR_REPORT;
960 else {
961 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
962 return -1;
963 }
964 }
965
966 /* compute bus and unit according index */
967
968 if (index != -1) {
969 if (bus_id != 0 || unit_id != -1) {
970 fprintf(stderr,
971 "qemu: '%s' index cannot be used with bus and unit\n", str);
972 return -1;
973 }
974 if (max_devs == 0)
975 {
976 unit_id = index;
977 bus_id = 0;
978 } else {
979 unit_id = index % max_devs;
980 bus_id = index / max_devs;
981 }
982 }
983
984 /* if user doesn't specify a unit_id,
985 * try to find the first free
986 */
987
988 if (unit_id == -1) {
989 unit_id = 0;
990 while (drive_get_index(type, bus_id, unit_id) != -1) {
991 unit_id++;
992 if (max_devs && unit_id >= max_devs) {
993 unit_id -= max_devs;
994 bus_id++;
995 }
996 }
997 }
998
999 /* check unit id */
1000
1001 if (max_devs && unit_id >= max_devs) {
1002 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
1003 str, unit_id, max_devs - 1);
1004 return -1;
1005 }
1006
1007 /*
1008 * ignore multiple definitions
1009 */
1010
1011 if (drive_get_index(type, bus_id, unit_id) != -1)
1012 return -2;
1013
1014 /* init */
1015
1016 if (type == IF_IDE || type == IF_SCSI)
1017 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1018 if (max_devs)
1019 snprintf(buf, sizeof(buf), "%s%i%s%i",
1020 devname, bus_id, mediastr, unit_id);
1021 else
1022 snprintf(buf, sizeof(buf), "%s%s%i",
1023 devname, mediastr, unit_id);
1024 bdrv = bdrv_new(buf);
1025 drives_table_idx = drive_get_free_idx();
1026 drives_table[drives_table_idx].bdrv = bdrv;
1027 drives_table[drives_table_idx].type = type;
1028 drives_table[drives_table_idx].bus = bus_id;
1029 drives_table[drives_table_idx].unit = unit_id;
1030 drives_table[drives_table_idx].onerror = onerror;
1031 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
1032 strncpy(drives_table[drives_table_idx].serial, serial, sizeof(serial));
1033 nb_drives++;
1034
1035 switch(type) {
1036 case IF_IDE:
1037 case IF_SCSI:
1038 case IF_XEN:
1039 switch(media) {
1040 case MEDIA_DISK:
1041 if (cyls != 0) {
1042 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
1043 bdrv_set_translation_hint(bdrv, translation);
1044 }
1045 break;
1046 case MEDIA_CDROM:
1047 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
1048 break;
1049 }
1050 break;
1051 case IF_SD:
1052 /* FIXME: This isn't really a floppy, but it's a reasonable
1053 approximation. */
1054 case IF_FLOPPY:
1055 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
1056 break;
1057 case IF_PFLASH:
1058 case IF_MTD:
1059 case IF_VIRTIO:
1060 break;
1061 case IF_COUNT:
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07001062 case IF_NONE:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001063 abort();
1064 }
1065 if (!file[0])
1066 return -2;
1067 bdrv_flags = 0;
1068 if (snapshot) {
1069 bdrv_flags |= BDRV_O_SNAPSHOT;
1070 cache = 2; /* always use write-back with snapshot */
1071 }
1072 if (cache == 0) /* no caching */
1073 bdrv_flags |= BDRV_O_NOCACHE;
1074 else if (cache == 2) /* write-back */
1075 bdrv_flags |= BDRV_O_CACHE_WB;
1076 else if (cache == 3) /* not specified */
1077 bdrv_flags |= BDRV_O_CACHE_DEF;
1078 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
1079 fprintf(stderr, "qemu: could not open disk image %s\n",
1080 file);
1081 return -1;
1082 }
1083 if (bdrv_key_required(bdrv))
1084 autostart = 0;
1085 return drives_table_idx;
1086}
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01001087#endif /* MAX_DRIVES */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001088
1089static void numa_add(const char *optarg)
1090{
1091 char option[128];
1092 char *endptr;
1093 unsigned long long value, endvalue;
1094 int nodenr;
1095
1096 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1097 if (!strcmp(option, "node")) {
1098 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1099 nodenr = nb_numa_nodes;
1100 } else {
1101 nodenr = strtoull(option, NULL, 10);
1102 }
1103
1104 if (get_param_value(option, 128, "mem", optarg) == 0) {
1105 node_mem[nodenr] = 0;
1106 } else {
1107 value = strtoull(option, &endptr, 0);
1108 switch (*endptr) {
1109 case 0: case 'M': case 'm':
1110 value <<= 20;
1111 break;
1112 case 'G': case 'g':
1113 value <<= 30;
1114 break;
1115 }
1116 node_mem[nodenr] = value;
1117 }
1118 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1119 node_cpumask[nodenr] = 0;
1120 } else {
1121 value = strtoull(option, &endptr, 10);
1122 if (value >= 64) {
1123 value = 63;
1124 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1125 } else {
1126 if (*endptr == '-') {
1127 endvalue = strtoull(endptr+1, &endptr, 10);
1128 if (endvalue >= 63) {
1129 endvalue = 62;
1130 fprintf(stderr,
1131 "only 63 CPUs in NUMA mode supported.\n");
1132 }
1133 value = (1 << (endvalue + 1)) - (1 << value);
1134 } else {
1135 value = 1 << value;
1136 }
1137 }
1138 node_cpumask[nodenr] = value;
1139 }
1140 nb_numa_nodes++;
1141 }
1142 return;
1143}
1144
1145/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001146/* PCMCIA/Cardbus */
1147
1148static struct pcmcia_socket_entry_s {
1149 PCMCIASocket *socket;
1150 struct pcmcia_socket_entry_s *next;
1151} *pcmcia_sockets = 0;
1152
1153void pcmcia_socket_register(PCMCIASocket *socket)
1154{
1155 struct pcmcia_socket_entry_s *entry;
1156
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001157 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001158 entry->socket = socket;
1159 entry->next = pcmcia_sockets;
1160 pcmcia_sockets = entry;
1161}
1162
1163void pcmcia_socket_unregister(PCMCIASocket *socket)
1164{
1165 struct pcmcia_socket_entry_s *entry, **ptr;
1166
1167 ptr = &pcmcia_sockets;
1168 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1169 if (entry->socket == socket) {
1170 *ptr = entry->next;
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001171 g_free(entry);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001172 }
1173}
1174
1175void pcmcia_info(Monitor *mon)
1176{
1177 struct pcmcia_socket_entry_s *iter;
1178
1179 if (!pcmcia_sockets)
1180 monitor_printf(mon, "No PCMCIA sockets\n");
1181
1182 for (iter = pcmcia_sockets; iter; iter = iter->next)
1183 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1184 iter->socket->attached ? iter->socket->card_string :
1185 "Empty");
1186}
1187
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001188/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001189/* machine registration */
1190
1191static QEMUMachine *first_machine = NULL;
1192QEMUMachine *current_machine = NULL;
1193
1194int qemu_register_machine(QEMUMachine *m)
1195{
1196 QEMUMachine **pm;
1197 pm = &first_machine;
1198 while (*pm != NULL)
1199 pm = &(*pm)->next;
1200 m->next = NULL;
1201 *pm = m;
1202 return 0;
1203}
1204
1205static QEMUMachine *find_machine(const char *name)
1206{
1207 QEMUMachine *m;
1208
1209 for(m = first_machine; m != NULL; m = m->next) {
1210 if (!strcmp(m->name, name))
1211 return m;
1212 }
1213 return NULL;
1214}
1215
1216static QEMUMachine *find_default_machine(void)
1217{
1218 QEMUMachine *m;
1219
1220 for(m = first_machine; m != NULL; m = m->next) {
1221 if (m->is_default) {
1222 return m;
1223 }
1224 }
1225 return NULL;
1226}
1227
1228/***********************************************************/
1229/* main execution loop */
1230
1231static void gui_update(void *opaque)
1232{
1233 uint64_t interval = GUI_REFRESH_INTERVAL;
1234 DisplayState *ds = opaque;
1235 DisplayChangeListener *dcl = ds->listeners;
1236
1237 dpy_refresh(ds);
1238
1239 while (dcl != NULL) {
1240 if (dcl->gui_timer_interval &&
1241 dcl->gui_timer_interval < interval)
1242 interval = dcl->gui_timer_interval;
1243 dcl = dcl->next;
1244 }
David 'Digit' Turnerdcda9492014-02-16 15:13:55 +01001245 timer_mod(ds->gui_timer, interval + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001246}
1247
1248static void nographic_update(void *opaque)
1249{
1250 uint64_t interval = GUI_REFRESH_INTERVAL;
1251
David 'Digit' Turnerdcda9492014-02-16 15:13:55 +01001252 timer_mod(nographic_timer, interval + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001253}
1254
1255struct vm_change_state_entry {
1256 VMChangeStateHandler *cb;
1257 void *opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001258 QLIST_ENTRY (vm_change_state_entry) entries;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001259};
1260
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001261static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001262
1263VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1264 void *opaque)
1265{
1266 VMChangeStateEntry *e;
1267
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001268 e = g_malloc0(sizeof (*e));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001269
1270 e->cb = cb;
1271 e->opaque = opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001272 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001273 return e;
1274}
1275
1276void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1277{
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001278 QLIST_REMOVE (e, entries);
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001279 g_free (e);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001280}
1281
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001282void vm_state_notify(int running, int reason)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001283{
1284 VMChangeStateEntry *e;
1285
1286 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1287 e->cb(e->opaque, running, reason);
1288 }
1289}
1290
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001291void vm_start(void)
1292{
1293 if (!vm_running) {
1294 cpu_enable_ticks();
1295 vm_running = 1;
1296 vm_state_notify(1, 0);
David Turner6a9ef172010-09-09 22:54:36 +02001297 //qemu_rearm_alarm_timer(alarm_timer);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001298 resume_all_vcpus();
1299 }
1300}
1301
1302/* reset/shutdown handler */
1303
1304typedef struct QEMUResetEntry {
1305 QEMUResetHandler *func;
1306 void *opaque;
1307 int order;
1308 struct QEMUResetEntry *next;
1309} QEMUResetEntry;
1310
1311static QEMUResetEntry *first_reset_entry;
1312static int reset_requested;
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001313static int shutdown_requested, shutdown_signal = -1;
1314static pid_t shutdown_pid;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001315static int powerdown_requested;
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001316int debug_requested;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001317static int vmstop_requested;
1318
1319int qemu_shutdown_requested(void)
1320{
1321 int r = shutdown_requested;
1322 shutdown_requested = 0;
1323 return r;
1324}
1325
1326int qemu_reset_requested(void)
1327{
1328 int r = reset_requested;
1329 reset_requested = 0;
1330 return r;
1331}
1332
1333int qemu_powerdown_requested(void)
1334{
1335 int r = powerdown_requested;
1336 powerdown_requested = 0;
1337 return r;
1338}
1339
David 'Digit' Turnerf9077a82014-02-10 23:10:47 +01001340int qemu_debug_requested(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001341{
1342 int r = debug_requested;
1343 debug_requested = 0;
1344 return r;
1345}
1346
David 'Digit' Turnerf9077a82014-02-10 23:10:47 +01001347int qemu_vmstop_requested(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001348{
1349 int r = vmstop_requested;
1350 vmstop_requested = 0;
1351 return r;
1352}
1353
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001354void qemu_register_reset(QEMUResetHandler *func, int order, void *opaque)
1355{
1356 QEMUResetEntry **pre, *re;
1357
1358 pre = &first_reset_entry;
1359 while (*pre != NULL && (*pre)->order >= order) {
1360 pre = &(*pre)->next;
1361 }
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001362 re = g_malloc0(sizeof(QEMUResetEntry));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001363 re->func = func;
1364 re->opaque = opaque;
1365 re->order = order;
1366 re->next = NULL;
1367 *pre = re;
1368}
1369
1370void qemu_system_reset(void)
1371{
1372 QEMUResetEntry *re;
1373
1374 /* reset all devices */
1375 for(re = first_reset_entry; re != NULL; re = re->next) {
1376 re->func(re->opaque);
1377 }
1378}
1379
1380void qemu_system_reset_request(void)
1381{
1382 if (no_reboot) {
1383 shutdown_requested = 1;
1384 } else {
1385 reset_requested = 1;
1386 }
1387 qemu_notify_event();
1388}
1389
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001390void qemu_system_killed(int signal, pid_t pid)
1391{
1392 shutdown_signal = signal;
1393 shutdown_pid = pid;
1394 qemu_system_shutdown_request();
1395}
1396
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001397void qemu_system_shutdown_request(void)
1398{
1399 shutdown_requested = 1;
1400 qemu_notify_event();
1401}
1402
1403void qemu_system_powerdown_request(void)
1404{
1405 powerdown_requested = 1;
1406 qemu_notify_event();
1407}
1408
David 'Digit' Turnerf9077a82014-02-10 23:10:47 +01001409int vm_can_run(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001410{
1411 if (powerdown_requested)
1412 return 0;
1413 if (reset_requested)
1414 return 0;
1415 if (shutdown_requested)
1416 return 0;
1417 if (debug_requested)
1418 return 0;
1419 return 1;
1420}
1421
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07001422void version(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001423{
1424 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1425}
1426
1427void qemu_help(int exitcode)
1428{
1429 version();
1430 printf("usage: %s [options] [disk_image]\n"
1431 "\n"
1432 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
1433 "\n"
1434#define DEF(option, opt_arg, opt_enum, opt_help) \
1435 opt_help
1436#define DEFHEADING(text) stringify(text) "\n"
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001437#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001438#undef DEF
1439#undef DEFHEADING
1440#undef GEN_DOCS
1441 "\n"
1442 "During emulation, the following keys are useful:\n"
1443 "ctrl-alt-f toggle full screen\n"
1444 "ctrl-alt-n switch to virtual console 'n'\n"
1445 "ctrl-alt toggle mouse and keyboard grab\n"
1446 "\n"
1447 "When using -nographic, press 'ctrl-a h' to get some help.\n"
1448 ,
1449 "qemu",
1450 DEFAULT_RAM_SIZE,
1451#ifndef _WIN32
1452 DEFAULT_NETWORK_SCRIPT,
1453 DEFAULT_NETWORK_DOWN_SCRIPT,
1454#endif
1455 DEFAULT_GDBSTUB_PORT,
1456 "/tmp/qemu.log");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07001457 QEMU_EXIT(exitcode);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001458}
1459
1460#define HAS_ARG 0x0001
1461
1462enum {
1463#define DEF(option, opt_arg, opt_enum, opt_help) \
1464 opt_enum,
1465#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001466#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001467#undef DEF
1468#undef DEFHEADING
1469#undef GEN_DOCS
1470};
1471
1472typedef struct QEMUOption {
1473 const char *name;
1474 int flags;
1475 int index;
1476} QEMUOption;
1477
1478static const QEMUOption qemu_options[] = {
1479 { "h", 0, QEMU_OPTION_h },
1480#define DEF(option, opt_arg, opt_enum, opt_help) \
1481 { option, opt_arg, opt_enum },
1482#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001483#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001484#undef DEF
1485#undef DEFHEADING
1486#undef GEN_DOCS
1487 { NULL, 0, 0 },
1488};
1489
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001490static void select_vgahw (const char *p)
1491{
1492 const char *opts;
1493
1494 cirrus_vga_enabled = 0;
1495 std_vga_enabled = 0;
1496 vmsvga_enabled = 0;
1497 xenfb_enabled = 0;
1498 if (strstart(p, "std", &opts)) {
1499 std_vga_enabled = 1;
1500 } else if (strstart(p, "cirrus", &opts)) {
1501 cirrus_vga_enabled = 1;
1502 } else if (strstart(p, "vmware", &opts)) {
1503 vmsvga_enabled = 1;
1504 } else if (strstart(p, "xenfb", &opts)) {
1505 xenfb_enabled = 1;
1506 } else if (!strstart(p, "none", &opts)) {
1507 invalid_vga:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07001508 PANIC("Unknown vga type: %s", p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001509 }
1510 while (*opts) {
1511 const char *nextopt;
1512
1513 if (strstart(opts, ",retrace=", &nextopt)) {
1514 opts = nextopt;
1515 if (strstart(opts, "dumb", &nextopt))
1516 vga_retrace_method = VGA_RETRACE_DUMB;
1517 else if (strstart(opts, "precise", &nextopt))
1518 vga_retrace_method = VGA_RETRACE_PRECISE;
1519 else goto invalid_vga;
1520 } else goto invalid_vga;
1521 opts = nextopt;
1522 }
1523}
1524
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001525#define MAX_NET_CLIENTS 32
1526
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001527#ifdef _WIN32
1528/* Look for support files in the same directory as the executable. */
1529static char *find_datadir(const char *argv0)
1530{
1531 char *p;
1532 char buf[MAX_PATH];
1533 DWORD len;
1534
1535 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
1536 if (len == 0) {
1537 return NULL;
1538 }
1539
1540 buf[len] = 0;
1541 p = buf + len - 1;
1542 while (p != buf && *p != '\\')
1543 p--;
1544 *p = 0;
1545 if (access(buf, R_OK) == 0) {
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001546 return g_strdup(buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001547 }
1548 return NULL;
1549}
1550#else /* !_WIN32 */
1551
David 'Digit' Turner24d27522011-06-01 16:50:56 +02001552/* Similarly, return the location of the executable */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001553static char *find_datadir(const char *argv0)
1554{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001555 char *p = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001556 char buf[PATH_MAX];
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001557
1558#if defined(__linux__)
1559 {
1560 int len;
1561 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
1562 if (len > 0) {
1563 buf[len] = 0;
1564 p = buf;
1565 }
1566 }
1567#elif defined(__FreeBSD__)
1568 {
1569 int len;
1570 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
1571 if (len > 0) {
1572 buf[len] = 0;
1573 p = buf;
1574 }
1575 }
1576#endif
1577 /* If we don't have any way of figuring out the actual executable
1578 location then try argv[0]. */
1579 if (!p) {
David 'Digit' Turner24d27522011-06-01 16:50:56 +02001580 p = realpath(argv0, buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001581 if (!p) {
1582 return NULL;
1583 }
1584 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001585
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001586 return g_strdup(dirname(buf));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001587}
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001588#endif
1589
David 'Digit' Turner24d27522011-06-01 16:50:56 +02001590static char*
1591qemu_find_file_with_subdir(const char* data_dir, const char* subdir, const char* name)
1592{
1593 int len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001594 char* buf = g_malloc0(len);
David 'Digit' Turner24d27522011-06-01 16:50:56 +02001595
1596 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1597 VERBOSE_PRINT(init," trying to find: %s\n", buf);
1598 if (access(buf, R_OK)) {
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001599 g_free(buf);
David 'Digit' Turner24d27522011-06-01 16:50:56 +02001600 return NULL;
1601 }
1602 return buf;
1603}
1604
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001605char *qemu_find_file(int type, const char *name)
1606{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001607 const char *subdir;
1608 char *buf;
1609
1610 /* If name contains path separators then try it as a straight path. */
1611 if ((strchr(name, '/') || strchr(name, '\\'))
1612 && access(name, R_OK) == 0) {
1613 return strdup(name);
1614 }
1615 switch (type) {
1616 case QEMU_FILE_TYPE_BIOS:
1617 subdir = "";
1618 break;
1619 case QEMU_FILE_TYPE_KEYMAP:
1620 subdir = "keymaps/";
1621 break;
1622 default:
1623 abort();
1624 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02001625 buf = qemu_find_file_with_subdir(data_dir, subdir, name);
1626#ifdef CONFIG_ANDROID
1627 if (type == QEMU_FILE_TYPE_BIOS) {
1628 /* This case corresponds to the emulator being used as part of an
1629 * SDK installation. NOTE: data_dir is really $bindir. */
1630 if (buf == NULL)
1631 buf = qemu_find_file_with_subdir(data_dir, "lib/pc-bios/", name);
1632 /* This case corresponds to platform builds. */
1633 if (buf == NULL)
1634 buf = qemu_find_file_with_subdir(data_dir, "../usr/share/pc-bios/", name);
1635 /* Finally, try this for standalone builds under external/qemu */
1636 if (buf == NULL)
Andrew Hsieh3d894282012-05-02 12:06:12 +08001637 buf = qemu_find_file_with_subdir(data_dir, "../../../prebuilts/qemu-kernel/x86/pc-bios/", name);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001638 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02001639#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001640 return buf;
1641}
1642
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07001643static int
1644add_dns_server( const char* server_name )
1645{
1646 SockAddress addr;
1647
1648 if (sock_address_init_resolve( &addr, server_name, 55, 0 ) < 0) {
1649 fprintf(stdout,
1650 "### WARNING: can't resolve DNS server name '%s'\n",
1651 server_name );
1652 return -1;
1653 }
1654
1655 fprintf(stderr,
1656 "DNS server name '%s' resolved to %s\n", server_name, sock_address_to_string(&addr) );
1657
1658 if ( slirp_add_dns_server( &addr ) < 0 ) {
1659 fprintf(stderr,
1660 "### WARNING: could not add DNS server '%s' to the network stack\n", server_name);
1661 return -1;
1662 }
1663 return 0;
1664}
1665
rich cannings7339b552011-02-16 13:43:44 -08001666/* Parses an integer
1667 * Pararm:
1668 * str String containing a number to be parsed.
1669 * result Passes the parsed integer in this argument
1670 * returns 0 if ok, -1 if failed
1671 */
1672int
1673parse_int(const char *str, int *result)
1674{
1675 char* r;
1676 *result = strtol(str, &r, 0);
1677 if (r == NULL || *r != '\0')
1678 return -1;
1679
1680 return 0;
1681}
1682
rich cannings7339b552011-02-16 13:43:44 -08001683/* parses a null-terminated string specifying a network port (e.g., "80") or
1684 * port range (e.g., "[6666-7000]"). In case of a single port, lport and hport
1685 * are the same. Returns 0 on success, -1 on error. */
1686
1687int parse_port_range(const char *str, unsigned short *lport,
1688 unsigned short *hport) {
1689
1690 unsigned int low = 0, high = 0;
1691 char *p, *arg = strdup(str);
1692
1693 if ((*arg == '[') && ((p = strrchr(arg, ']')) != NULL)) {
1694 p = arg + 1; /* skip '[' */
1695 low = atoi(strtok(p, "-"));
1696 high = atoi(strtok(NULL, "-"));
1697 if ((low > 0) && (high > 0) && (low < high) && (high < 65535)) {
1698 *lport = low;
1699 *hport = high;
1700 }
1701 }
1702 else {
1703 low = atoi(arg);
1704 if ((0 < low) && (low < 65535)) {
1705 *lport = low;
1706 *hport = low;
1707 }
1708 }
1709 free(arg);
1710 if (low != 0)
1711 return 0;
1712 return -1;
1713}
1714
1715/*
1716 * Implements the generic port forwarding option
1717 */
1718void
1719net_slirp_forward(const char *optarg)
1720{
1721 /*
1722 * we expect the following format:
1723 * dst_net:dst_mask:dst_port:redirect_ip:redirect_port OR
1724 * dst_net:dst_mask:[dp_range_start-dp_range_end]:redirect_ip:redirect_port
1725 */
1726 char *argument = strdup(optarg), *p = argument;
1727 char *dst_net, *dst_mask, *dst_port;
1728 char *redirect_ip, *redirect_port;
1729 uint32_t dnet, dmask, rip;
Vladimir Chtchetkineb557e9f2012-03-22 15:38:08 -07001730 unsigned short dlport = 0, dhport = 0, rport;
rich cannings7339b552011-02-16 13:43:44 -08001731
1732
1733 dst_net = strtok(p, ":");
1734 dst_mask = strtok(NULL, ":");
1735 dst_port = strtok(NULL, ":");
1736 redirect_ip = strtok(NULL, ":");
1737 redirect_port = strtok(NULL, ":");
1738
1739 if (dst_net == NULL || dst_mask == NULL || dst_port == NULL ||
1740 redirect_ip == NULL || redirect_port == NULL) {
1741 fprintf(stderr,
1742 "Invalid argument for -net-forward, we expect "
1743 "dst_net:dst_mask:dst_port:redirect_ip:redirect_port or "
1744 "dst_net:dst_mask:[dp_range_start-dp_range_end]"
1745 ":redirect_ip:redirect_port: %s\n",
1746 optarg);
1747 exit(1);
1748 }
1749
1750 /* inet_strtoip converts dotted address to host byte order */
1751 if (inet_strtoip(dst_net, &dnet) == -1) {
1752 fprintf(stderr, "Invalid destination IP net: %s\n", dst_net);
1753 exit(1);
1754 }
1755 if (inet_strtoip(dst_mask, &dmask) == -1) {
1756 fprintf(stderr, "Invalid destination IP mask: %s\n", dst_mask);
1757 exit(1);
1758 }
1759 if (inet_strtoip(redirect_ip, &rip) == -1) {
1760 fprintf(stderr, "Invalid redirect IP address: %s\n", redirect_ip);
1761 exit(1);
1762 }
1763
1764 if (parse_port_range(dst_port, &dlport, &dhport) == -1) {
1765 fprintf(stderr, "Invalid destination port or port range\n");
1766 exit(1);
1767 }
1768
1769 rport = atoi(redirect_port);
1770 if (!rport) {
1771 fprintf(stderr, "Invalid redirect port: %s\n", redirect_port);
1772 exit(1);
1773 }
1774
1775 dnet &= dmask;
1776
1777 slirp_add_net_forward(dnet, dmask, dlport, dhport,
1778 rip, rport);
1779
1780 free(argument);
1781}
1782
1783
1784/* Parses an -allow-tcp or -allow-udp argument and inserts a corresponding
1785 * entry in the allows list */
1786void
1787slirp_allow(const char *optarg, u_int8_t proto)
1788{
1789 /*
1790 * we expect the following format:
1791 * dst_ip:dst_port OR dst_ip:[dst_lport-dst_hport]
1792 */
1793 char *argument = strdup(optarg), *p = argument;
1794 char *dst_ip_str, *dst_port_str;
1795 uint32_t dst_ip;
Vladimir Chtchetkineb557e9f2012-03-22 15:38:08 -07001796 unsigned short dst_lport = 0, dst_hport = 0;
rich cannings7339b552011-02-16 13:43:44 -08001797
1798 dst_ip_str = strtok(p, ":");
1799 dst_port_str = strtok(NULL, ":");
1800
1801 if (dst_ip_str == NULL || dst_port_str == NULL) {
1802 fprintf(stderr,
1803 "Invalid argument %s for -allow. We expect "
1804 "dst_ip:dst_port or dst_ip:[dst_lport-dst_hport]\n",
1805 optarg);
1806 exit(1);
1807 }
1808
1809 if (inet_strtoip(dst_ip_str, &dst_ip) == -1) {
1810 fprintf(stderr, "Invalid destination IP address: %s\n", dst_ip_str);
1811 exit(1);
1812 }
1813 if (parse_port_range(dst_port_str, &dst_lport, &dst_hport) == -1) {
1814 fprintf(stderr, "Invalid destination port or port range\n");
1815 exit(1);
1816 }
1817
1818 slirp_add_allow(dst_ip, dst_lport, dst_hport, proto);
1819
1820 free(argument);
1821}
1822
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01001823/* Add a serial device at a given location in the emulated hardware table.
1824 * On failure, this function aborts the program with an error message.
1825 */
1826static void
1827serial_hds_add_at(int index, const char* devname)
1828{
1829 char label[32];
1830
1831 if (!devname || !strcmp(devname,"none"))
1832 return;
1833
1834 if (index >= MAX_SERIAL_PORTS) {
1835 PANIC("qemu: invalid serial index for %s (%d >= %d)",
1836 devname, index, MAX_SERIAL_PORTS);
1837 }
1838 if (serial_hds[index] != NULL) {
1839 PANIC("qemu: invalid serial index for %s (%d: already taken!)",
1840 devname, index);
1841 }
1842 snprintf(label, sizeof(label), "serial%d", index);
1843 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1844 if (!serial_hds[index]) {
1845 PANIC("qemu: could not open serial device '%s'", devname);
1846 }
1847}
1848
1849
1850/* Find a free slot in the emulated serial device table, and register
1851 * it. Return the allocated table index.
1852 */
1853static int
1854serial_hds_add(const char* devname)
1855{
1856 int index;
1857
1858 /* Find first free slot */
1859 for (index = 0; index < MAX_SERIAL_PORTS; index++) {
1860 if (serial_hds[index] == NULL) {
1861 serial_hds_add_at(index, devname);
1862 return index;
1863 }
1864 }
1865
1866 PANIC("qemu: too many serial devices registered (%d)", index);
1867 return -1; /* shouldn't happen */
1868}
1869
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +02001870
1871// Add a NAND partition image to the hardware configuration.
1872// |part_name| is a string indicating the type of partition, i.e. "system",
1873// "userdata" or "cache".
1874// |part_type| is an enum describing the type of partition. If it is
1875// DISK_PARTITION_TYPE_PROBE, then try to auto-detect the type directly
1876// from the content of |part_file| or |part_init_file|.
1877// |part_size| is the partition size in bytes.
1878// |part_file| is the partition file path, can be NULL if |path_init_file|
1879// is not NULL.
1880// |part_init_file| is an optional path to the initialization partition file.
1881// |is_ext4| is true if the partition is formatted as EXT4, false for YAFFS2.
1882//
1883// The NAND partition will be backed by |path_file|, except in the following
1884// cases:
1885// - |part_file| is NULL, or its value is "<temp>", indicating that a
1886// new temporary image file must be used instead.
1887//
1888// - |part_file| is not NULL, but the function fails to lock the file,
1889// indicating it's already used by another instance. A warning should
1890// be printed to warn the user, and a new temporary image should be
1891// used.
1892//
1893// If |part_file| is not NULL and can be locked, if the partition image does
1894// not exit, then the file must be created as an empty partition.
1895//
1896// When a new partition image is created, what happens depends on the
1897// value of |is_ext4|:
1898//
1899// - If |is_ext4| is false, a simple empty file is created, since that's
1900// enough to create an empty YAFFS2 partition.
1901//
1902// - If |is_ext4| is true, an "empty ext4" partition image is created
1903// instead, which will _not_ be backed by an empty file.
1904//
1905// If |part_init_file| is not NULL, its content will be used to erase
1906// the content of the main partition image. This is automatically handled
1907// by the NAND code though.
1908//
1909void android_nand_add_image(const char* part_name,
1910 AndroidPartitionType part_type,
1911 uint64_t part_size,
1912 const char* part_file,
1913 const char* part_init_file)
1914{
1915 char tmp[PATH_MAX * 2 + 32];
1916
1917 // Sanitize parameters, an empty string must be the same as NULL.
1918 if (part_file && !*part_file) {
1919 part_file = NULL;
1920 }
1921 if (part_init_file && !*part_init_file) {
1922 part_init_file = NULL;
1923 }
1924
1925 // Sanity checks.
1926 if (part_size == 0) {
1927 PANIC("Invalid %s partition size 0x%" PRIx64, part_size);
1928 }
1929
1930 if (part_init_file && !path_exists(part_init_file)) {
1931 PANIC("Missing initial %s image: %s", part_name, part_init_file);
1932 }
1933
1934 // As a special case, a |part_file| of '<temp>' means a temporary
1935 // partition is needed.
1936 if (part_file && !strcmp(part_file, "<temp>")) {
1937 part_file = NULL;
1938 }
1939
1940 // Verify partition type, or probe it if needed.
1941 {
1942 const char* image_file = NULL;
1943 if (part_init_file && path_exists(part_init_file)) {
1944 image_file = part_init_file;
1945 } else if (part_file && path_exists(part_file)) {
1946 image_file = part_file;
1947 }
1948
1949 if (part_type == ANDROID_PARTITION_TYPE_UNKNOWN) {
1950 if (!image_file) {
1951 PANIC("Cannot determine type of %s partition: no image files!",
1952 part_name);
1953 }
1954 VERBOSE_PRINT(init, "Probing %s image file for partition type: %s",
1955 part_name, image_file);
1956
1957 part_type = androidPartitionType_probeFile(image_file);
1958
1959 VERBOSE_PRINT(init, "Found %s image format: %s",
1960 part_name, androidPartitionType_toString(part_type));
1961 } else {
1962 if (!image_file) {
1963 PANIC("Cannot find any %s file!", part_name);
1964 }
1965
1966 AndroidPartitionType image_type =
1967 androidPartitionType_probeFile(image_file);
1968 if (image_type == ANDROID_PARTITION_TYPE_UNKNOWN) {
1969 PANIC("Cannot determine %s partition type of: %s", part_name,
1970 image_file);
1971 }
1972
1973 if (image_type != part_type) {
1974 PANIC("Invalid %s partition image type: %s (expected %s)",
1975 part_name,
1976 androidPartitionType_toString(image_type),
1977 androidPartitionType_toString(part_type));
1978 }
1979 }
1980 }
1981
1982 snprintf(tmp, sizeof tmp, "%s,size=0x%" PRIx64, part_name, part_size);
1983
1984 bool need_temp_partition = true;
1985 bool need_make_empty = false;
1986
1987 if (part_file && *part_file) {
1988 if (filelock_create(part_file) == NULL) {
1989 fprintf(stderr,
1990 "WARNING: %s image already in use, changes will not persist!\n",
1991 part_name);
1992 } else {
1993 need_temp_partition = false;
1994
1995 if (!path_exists(part_file) && !part_init_file) {
1996 need_make_empty = true;
1997 }
1998 }
1999 }
2000
2001 // Do we need a temporary partition image ?
2002 if (need_temp_partition) {
2003 TempFile* temp_file = tempfile_create();
2004 if (temp_file == NULL) {
2005 PANIC("Could not create temp file for %s partition image: %s\n",
2006 part_name);
2007 }
2008 part_file = tempfile_path(temp_file);
2009 VERBOSE_PRINT(init,
2010 "Mapping '%s' partition image to %s",
2011 part_name,
2012 part_file);
2013
2014 need_make_empty = true;
2015 }
2016
2017 pstrcat(tmp, sizeof tmp, ",file=");
2018 pstrcat(tmp, sizeof tmp, part_file);
2019
2020 // Do we need to make the partition image empty?
2021 if (need_make_empty && !part_init_file) {
2022 int ret = androidPartitionType_makeEmptyFile(part_type,
2023 part_size,
2024 part_file);
2025 if (ret < 0) {
2026 PANIC("Could not create %s image file at %s: %s",
2027 part_name,
2028 part_file,
2029 strerror(-ret));
2030 }
2031 }
2032
2033 if (part_init_file) {
2034 if (!path_exists(part_init_file)) {
2035 PANIC("Invalid initial %s image path: %s",
2036 part_name,
2037 part_init_file);
2038 }
2039 pstrcat(tmp, sizeof tmp, ",initfile=");
2040 pstrcat(tmp, sizeof tmp, part_init_file);
2041 }
2042
2043 if (part_type == ANDROID_PARTITION_TYPE_EXT4) {
2044 // Using a nand device to approximate a block device until full
2045 // support is added.
2046 pstrcat(tmp, sizeof tmp,",pagesize=512,extrasize=0");
2047 }
2048
2049 VERBOSE_PRINT(init, "%s partition format: %s", part_name,
2050 androidPartitionType_toString(part_type));
2051
2052 nand_add_dev(tmp);
2053}
2054
2055
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002056int main(int argc, char **argv, char **envp)
2057{
2058 const char *gdbstub_dev = NULL;
2059 uint32_t boot_devices_bitmap = 0;
2060 int i;
David 'Digit' Turnera2c14f92014-02-04 01:02:30 +01002061 int snapshot, linux_boot, __attribute__((unused)) net_boot;
David Turner6a9ef172010-09-09 22:54:36 +02002062 const char *icount_option = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002063 const char *initrd_filename;
2064 const char *kernel_filename, *kernel_cmdline;
2065 const char *boot_devices = "";
2066 DisplayState *ds;
2067 DisplayChangeListener *dcl;
2068 int cyls, heads, secs, translation;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002069 QemuOpts *hda_opts = NULL;
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01002070 QemuOpts *hdb_opts = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002071 const char *net_clients[MAX_NET_CLIENTS];
2072 int nb_net_clients;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002073 int optind;
2074 const char *r, *optarg;
2075 CharDriverState *monitor_hd = NULL;
2076 const char *monitor_device;
2077 const char *serial_devices[MAX_SERIAL_PORTS];
2078 int serial_device_index;
2079 const char *parallel_devices[MAX_PARALLEL_PORTS];
2080 int parallel_device_index;
2081 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
2082 int virtio_console_index;
2083 const char *loadvm = NULL;
2084 QEMUMachine *machine;
2085 const char *cpu_model;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002086 int tb_size;
2087 const char *pid_file = NULL;
2088 const char *incoming = NULL;
David 'Digit' Turner7e9f6c12014-01-22 18:42:40 +01002089 const char* log_mask = NULL;
2090 const char* log_file = NULL;
David 'Digit' Turner66576782014-03-24 16:57:57 +01002091 CPUState *cpu;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002092 int show_vnc_port = 0;
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07002093 IniFile* hw_ini = NULL;
David 'Digit' Turner5f824112011-03-01 14:00:26 +01002094 STRALLOC_DEFINE(kernel_params);
2095 STRALLOC_DEFINE(kernel_config);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07002096 int dns_count = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002097
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002098 /* Initialize sockets before anything else, so we can properly report
2099 * initialization failures back to the UI. */
2100#ifdef _WIN32
2101 socket_init();
2102#endif
2103
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07002104 init_clocks();
2105
David 'Digit' Turner8848f632014-01-16 18:10:21 +01002106 qemu_cache_utils_init();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002107
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002108 QLIST_INIT (&vm_change_state_head);
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02002109 os_setup_early_signal_handling();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002110
2111 module_call_init(MODULE_INIT_MACHINE);
2112 machine = find_default_machine();
2113 cpu_model = NULL;
2114 initrd_filename = NULL;
2115 ram_size = 0;
2116 snapshot = 0;
2117 kernel_filename = NULL;
2118 kernel_cmdline = "";
David 'Digit' Turner5f824112011-03-01 14:00:26 +01002119
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002120 cyls = heads = secs = 0;
2121 translation = BIOS_ATA_TRANSLATION_AUTO;
2122 monitor_device = "vc:80Cx24C";
2123
2124 serial_devices[0] = "vc:80Cx24C";
2125 for(i = 1; i < MAX_SERIAL_PORTS; i++)
2126 serial_devices[i] = NULL;
2127 serial_device_index = 0;
2128
2129 parallel_devices[0] = "vc:80Cx24C";
2130 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
2131 parallel_devices[i] = NULL;
2132 parallel_device_index = 0;
2133
2134 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
2135 virtio_consoles[i] = NULL;
2136 virtio_console_index = 0;
2137
2138 for (i = 0; i < MAX_NODES; i++) {
2139 node_mem[i] = 0;
2140 node_cpumask[i] = 0;
2141 }
2142
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002143 nb_net_clients = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002144#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002145 nb_drives = 0;
2146 nb_drives_opt = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002147#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002148 nb_numa_nodes = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002149
2150 nb_nics = 0;
2151
2152 tb_size = 0;
2153 autostart= 1;
2154
2155 register_watchdogs();
2156
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07002157 /* Initialize boot properties. */
2158 boot_property_init_service();
David 'Digit' Turnerca950592011-04-27 12:26:15 +02002159 android_hw_control_init();
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02002160 android_net_pipes_init();
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07002161
David 'Digit' Turner36597752011-05-20 01:18:01 +02002162#ifdef CONFIG_KVM
2163 /* By default, force auto-detection for kvm */
2164 kvm_allowed = -1;
2165#endif
2166
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002167 optind = 1;
2168 for(;;) {
2169 if (optind >= argc)
2170 break;
2171 r = argv[optind];
2172 if (r[0] != '-') {
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002173 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002174 } else {
2175 const QEMUOption *popt;
2176
2177 optind++;
2178 /* Treat --foo the same as -foo. */
2179 if (r[1] == '-')
2180 r++;
2181 popt = qemu_options;
2182 for(;;) {
2183 if (!popt->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002184 PANIC("%s: invalid option -- '%s'",
2185 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002186 }
2187 if (!strcmp(popt->name, r + 1))
2188 break;
2189 popt++;
2190 }
2191 if (popt->flags & HAS_ARG) {
2192 if (optind >= argc) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002193 PANIC("%s: option '%s' requires an argument",
2194 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002195 }
2196 optarg = argv[optind++];
2197 } else {
2198 optarg = NULL;
2199 }
2200
2201 switch(popt->index) {
2202 case QEMU_OPTION_M:
2203 machine = find_machine(optarg);
2204 if (!machine) {
2205 QEMUMachine *m;
2206 printf("Supported machines are:\n");
2207 for(m = first_machine; m != NULL; m = m->next) {
2208 printf("%-10s %s%s\n",
2209 m->name, m->desc,
2210 m->is_default ? " (default)" : "");
2211 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002212 if (*optarg != '?') {
2213 PANIC("Invalid machine parameter: %s",
2214 optarg);
2215 } else {
2216 QEMU_EXIT(0);
2217 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002218 }
2219 break;
2220 case QEMU_OPTION_cpu:
2221 /* hw initialization will check this */
2222 if (*optarg == '?') {
2223/* XXX: implement xxx_cpu_list for targets that still miss it */
2224#if defined(cpu_list)
2225 cpu_list(stdout, &fprintf);
2226#endif
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002227 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002228 } else {
2229 cpu_model = optarg;
2230 }
2231 break;
2232 case QEMU_OPTION_initrd:
2233 initrd_filename = optarg;
2234 break;
2235 case QEMU_OPTION_hda:
2236 if (cyls == 0)
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002237 hda_opts = drive_add(optarg, HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002238 else
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002239 hda_opts = drive_add(optarg, HD_ALIAS
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002240 ",cyls=%d,heads=%d,secs=%d%s",
2241 0, cyls, heads, secs,
2242 translation == BIOS_ATA_TRANSLATION_LBA ?
2243 ",trans=lba" :
2244 translation == BIOS_ATA_TRANSLATION_NONE ?
2245 ",trans=none" : "");
2246 break;
2247 case QEMU_OPTION_hdb:
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01002248 hdb_opts = drive_add(optarg, HD_ALIAS, 1);
2249 break;
2250
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002251 case QEMU_OPTION_hdc:
2252 case QEMU_OPTION_hdd:
2253 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
2254 break;
2255 case QEMU_OPTION_drive:
2256 drive_add(NULL, "%s", optarg);
2257 break;
2258 case QEMU_OPTION_mtdblock:
2259 drive_add(optarg, MTD_ALIAS);
2260 break;
2261 case QEMU_OPTION_sd:
2262 drive_add(optarg, SD_ALIAS);
2263 break;
2264 case QEMU_OPTION_pflash:
2265 drive_add(optarg, PFLASH_ALIAS);
2266 break;
2267 case QEMU_OPTION_snapshot:
2268 snapshot = 1;
2269 break;
2270 case QEMU_OPTION_hdachs:
2271 {
2272 const char *p;
2273 p = optarg;
2274 cyls = strtol(p, (char **)&p, 0);
2275 if (cyls < 1 || cyls > 16383)
2276 goto chs_fail;
2277 if (*p != ',')
2278 goto chs_fail;
2279 p++;
2280 heads = strtol(p, (char **)&p, 0);
2281 if (heads < 1 || heads > 16)
2282 goto chs_fail;
2283 if (*p != ',')
2284 goto chs_fail;
2285 p++;
2286 secs = strtol(p, (char **)&p, 0);
2287 if (secs < 1 || secs > 63)
2288 goto chs_fail;
2289 if (*p == ',') {
2290 p++;
2291 if (!strcmp(p, "none"))
2292 translation = BIOS_ATA_TRANSLATION_NONE;
2293 else if (!strcmp(p, "lba"))
2294 translation = BIOS_ATA_TRANSLATION_LBA;
2295 else if (!strcmp(p, "auto"))
2296 translation = BIOS_ATA_TRANSLATION_AUTO;
2297 else
2298 goto chs_fail;
2299 } else if (*p != '\0') {
2300 chs_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002301 PANIC("qemu: invalid physical CHS format");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002302 }
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002303 if (hda_opts != NULL) {
2304 char num[16];
2305 snprintf(num, sizeof(num), "%d", cyls);
2306 qemu_opt_set(hda_opts, "cyls", num);
2307 snprintf(num, sizeof(num), "%d", heads);
2308 qemu_opt_set(hda_opts, "heads", num);
2309 snprintf(num, sizeof(num), "%d", secs);
2310 qemu_opt_set(hda_opts, "secs", num);
2311 if (translation == BIOS_ATA_TRANSLATION_LBA)
2312 qemu_opt_set(hda_opts, "trans", "lba");
2313 if (translation == BIOS_ATA_TRANSLATION_NONE)
2314 qemu_opt_set(hda_opts, "trans", "none");
2315 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002316 }
2317 break;
2318 case QEMU_OPTION_numa:
2319 if (nb_numa_nodes >= MAX_NODES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002320 PANIC("qemu: too many NUMA nodes");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002321 }
2322 numa_add(optarg);
2323 break;
2324 case QEMU_OPTION_nographic:
2325 display_type = DT_NOGRAPHIC;
2326 break;
2327#ifdef CONFIG_CURSES
2328 case QEMU_OPTION_curses:
2329 display_type = DT_CURSES;
2330 break;
2331#endif
2332 case QEMU_OPTION_portrait:
2333 graphic_rotate = 1;
2334 break;
2335 case QEMU_OPTION_kernel:
2336 kernel_filename = optarg;
2337 break;
2338 case QEMU_OPTION_append:
2339 kernel_cmdline = optarg;
2340 break;
2341 case QEMU_OPTION_cdrom:
2342 drive_add(optarg, CDROM_ALIAS);
2343 break;
2344 case QEMU_OPTION_boot:
2345 boot_devices = optarg;
2346 /* We just do some generic consistency checks */
2347 {
2348 /* Could easily be extended to 64 devices if needed */
2349 const char *p;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07002350
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002351 boot_devices_bitmap = 0;
2352 for (p = boot_devices; *p != '\0'; p++) {
2353 /* Allowed boot devices are:
2354 * a b : floppy disk drives
2355 * c ... f : IDE disk drives
2356 * g ... m : machine implementation dependant drives
2357 * n ... p : network devices
2358 * It's up to each machine implementation to check
2359 * if the given boot devices match the actual hardware
2360 * implementation and firmware features.
2361 */
2362 if (*p < 'a' || *p > 'q') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002363 PANIC("Invalid boot device '%c'", *p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002364 }
2365 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002366 PANIC(
2367 "Boot device '%c' was given twice",*p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002368 }
2369 boot_devices_bitmap |= 1 << (*p - 'a');
2370 }
2371 }
2372 break;
2373 case QEMU_OPTION_fda:
2374 case QEMU_OPTION_fdb:
2375 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2376 break;
2377#ifdef TARGET_I386
2378 case QEMU_OPTION_no_fd_bootchk:
2379 fd_bootchk = 0;
2380 break;
2381#endif
2382 case QEMU_OPTION_net:
2383 if (nb_net_clients >= MAX_NET_CLIENTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002384 PANIC("qemu: too many network clients");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002385 }
2386 net_clients[nb_net_clients] = optarg;
2387 nb_net_clients++;
2388 break;
2389#ifdef CONFIG_SLIRP
2390 case QEMU_OPTION_tftp:
2391 tftp_prefix = optarg;
2392 break;
2393 case QEMU_OPTION_bootp:
2394 bootp_filename = optarg;
2395 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002396 case QEMU_OPTION_redir:
2397 net_slirp_redir(NULL, optarg, NULL);
2398 break;
2399#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002400#ifdef HAS_AUDIO
2401 case QEMU_OPTION_audio_help:
2402 AUD_help ();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002403 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002404 break;
2405 case QEMU_OPTION_soundhw:
2406 select_soundhw (optarg);
2407 break;
2408#endif
2409 case QEMU_OPTION_h:
2410 qemu_help(0);
2411 break;
2412 case QEMU_OPTION_version:
2413 version();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002414 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002415 break;
2416 case QEMU_OPTION_m: {
2417 uint64_t value;
2418 char *ptr;
2419
2420 value = strtoul(optarg, &ptr, 10);
2421 switch (*ptr) {
2422 case 0: case 'M': case 'm':
2423 value <<= 20;
2424 break;
2425 case 'G': case 'g':
2426 value <<= 30;
2427 break;
2428 default:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002429 PANIC("qemu: invalid ram size: %s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002430 }
2431
2432 /* On 32-bit hosts, QEMU is limited by virtual address space */
David 'Digit' Turner81911b02014-02-10 17:53:45 +01002433 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002434 PANIC("qemu: at most 2047 MB RAM can be simulated");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002435 }
2436 if (value != (uint64_t)(ram_addr_t)value) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002437 PANIC("qemu: ram size too large");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002438 }
2439 ram_size = value;
2440 break;
2441 }
2442 case QEMU_OPTION_d:
David 'Digit' Turner7e9f6c12014-01-22 18:42:40 +01002443 log_mask = optarg;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002444 break;
2445 case QEMU_OPTION_s:
2446 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2447 break;
2448 case QEMU_OPTION_gdb:
2449 gdbstub_dev = optarg;
2450 break;
2451 case QEMU_OPTION_L:
2452 data_dir = optarg;
2453 break;
2454 case QEMU_OPTION_bios:
2455 bios_name = optarg;
2456 break;
2457 case QEMU_OPTION_singlestep:
2458 singlestep = 1;
2459 break;
2460 case QEMU_OPTION_S:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002461 autostart = 0;
2462 break;
2463#ifndef _WIN32
2464 case QEMU_OPTION_k:
2465 keyboard_layout = optarg;
2466 break;
2467#endif
2468 case QEMU_OPTION_localtime:
2469 rtc_utc = 0;
2470 break;
2471 case QEMU_OPTION_vga:
2472 select_vgahw (optarg);
2473 break;
2474#if defined(TARGET_PPC) || defined(TARGET_SPARC)
2475 case QEMU_OPTION_g:
2476 {
2477 const char *p;
2478 int w, h, depth;
2479 p = optarg;
2480 w = strtol(p, (char **)&p, 10);
2481 if (w <= 0) {
2482 graphic_error:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002483 PANIC("qemu: invalid resolution or depth");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002484 }
2485 if (*p != 'x')
2486 goto graphic_error;
2487 p++;
2488 h = strtol(p, (char **)&p, 10);
2489 if (h <= 0)
2490 goto graphic_error;
2491 if (*p == 'x') {
2492 p++;
2493 depth = strtol(p, (char **)&p, 10);
2494 if (depth != 8 && depth != 15 && depth != 16 &&
2495 depth != 24 && depth != 32)
2496 goto graphic_error;
2497 } else if (*p == '\0') {
2498 depth = graphic_depth;
2499 } else {
2500 goto graphic_error;
2501 }
2502
2503 graphic_width = w;
2504 graphic_height = h;
2505 graphic_depth = depth;
2506 }
2507 break;
2508#endif
2509 case QEMU_OPTION_echr:
2510 {
2511 char *r;
2512 term_escape_char = strtol(optarg, &r, 0);
2513 if (r == optarg)
2514 printf("Bad argument to echr\n");
2515 break;
2516 }
2517 case QEMU_OPTION_monitor:
2518 monitor_device = optarg;
2519 break;
2520 case QEMU_OPTION_serial:
2521 if (serial_device_index >= MAX_SERIAL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002522 PANIC("qemu: too many serial ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002523 }
2524 serial_devices[serial_device_index] = optarg;
2525 serial_device_index++;
2526 break;
2527 case QEMU_OPTION_watchdog:
2528 i = select_watchdog(optarg);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002529 if (i > 0) {
2530 if (i == 1) {
2531 PANIC("Invalid watchdog parameter: %s",
2532 optarg);
2533 } else {
2534 QEMU_EXIT(0);
2535 }
2536 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002537 break;
2538 case QEMU_OPTION_watchdog_action:
2539 if (select_watchdog_action(optarg) == -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002540 PANIC("Unknown -watchdog-action parameter");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002541 }
2542 break;
2543 case QEMU_OPTION_virtiocon:
2544 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002545 PANIC("qemu: too many virtio consoles");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002546 }
2547 virtio_consoles[virtio_console_index] = optarg;
2548 virtio_console_index++;
2549 break;
2550 case QEMU_OPTION_parallel:
2551 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002552 PANIC("qemu: too many parallel ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002553 }
2554 parallel_devices[parallel_device_index] = optarg;
2555 parallel_device_index++;
2556 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00002557 case QEMU_OPTION_loadvm:
2558 loadvm = optarg;
2559 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00002560 case QEMU_OPTION_savevm_on_exit:
2561 savevm_on_exit = optarg;
2562 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002563 case QEMU_OPTION_full_screen:
2564 full_screen = 1;
2565 break;
2566#ifdef CONFIG_SDL
2567 case QEMU_OPTION_no_frame:
2568 no_frame = 1;
2569 break;
2570 case QEMU_OPTION_alt_grab:
2571 alt_grab = 1;
2572 break;
2573 case QEMU_OPTION_no_quit:
2574 no_quit = 1;
2575 break;
2576 case QEMU_OPTION_sdl:
2577 display_type = DT_SDL;
2578 break;
2579#endif
2580 case QEMU_OPTION_pidfile:
2581 pid_file = optarg;
2582 break;
2583#ifdef TARGET_I386
2584 case QEMU_OPTION_win2k_hack:
2585 win2k_install_hack = 1;
2586 break;
2587 case QEMU_OPTION_rtc_td_hack:
2588 rtc_td_hack = 1;
2589 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08002590#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002591 case QEMU_OPTION_acpitable:
2592 if(acpi_table_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002593 PANIC("Wrong acpi table provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002594 }
2595 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08002596#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002597 case QEMU_OPTION_smbios:
David 'Digit' Turnerae3098a2011-05-11 16:01:57 +02002598 do_smbios_option(optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002599 break;
2600#endif
2601#ifdef CONFIG_KVM
2602 case QEMU_OPTION_enable_kvm:
2603 kvm_allowed = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002604 break;
David 'Digit' Turner36597752011-05-20 01:18:01 +02002605 case QEMU_OPTION_disable_kvm:
2606 kvm_allowed = 0;
2607 break;
2608#endif /* CONFIG_KVM */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002609 case QEMU_OPTION_smp:
2610 smp_cpus = atoi(optarg);
2611 if (smp_cpus < 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002612 PANIC("Invalid number of CPUs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002613 }
2614 break;
2615 case QEMU_OPTION_vnc:
2616 display_type = DT_VNC;
2617 vnc_display = optarg;
2618 break;
2619#ifdef TARGET_I386
2620 case QEMU_OPTION_no_acpi:
2621 acpi_enabled = 0;
2622 break;
2623 case QEMU_OPTION_no_hpet:
2624 no_hpet = 1;
2625 break;
2626 case QEMU_OPTION_no_virtio_balloon:
2627 no_virtio_balloon = 1;
2628 break;
2629#endif
2630 case QEMU_OPTION_no_reboot:
2631 no_reboot = 1;
2632 break;
2633 case QEMU_OPTION_no_shutdown:
2634 no_shutdown = 1;
2635 break;
2636 case QEMU_OPTION_show_cursor:
2637 cursor_hide = 0;
2638 break;
2639 case QEMU_OPTION_uuid:
2640 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002641 PANIC("Fail to parse UUID string. Wrong format.");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002642 }
2643 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002644 case QEMU_OPTION_option_rom:
2645 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002646 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002647 }
2648 option_rom[nb_option_roms] = optarg;
2649 nb_option_roms++;
2650 break;
2651#if defined(TARGET_ARM) || defined(TARGET_M68K)
2652 case QEMU_OPTION_semihosting:
2653 semihosting_enabled = 1;
2654 break;
2655#endif
2656 case QEMU_OPTION_name:
2657 qemu_name = optarg;
2658 break;
2659#if defined(TARGET_SPARC) || defined(TARGET_PPC)
2660 case QEMU_OPTION_prom_env:
2661 if (nb_prom_envs >= MAX_PROM_ENVS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002662 PANIC("Too many prom variables");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002663 }
2664 prom_envs[nb_prom_envs] = optarg;
2665 nb_prom_envs++;
2666 break;
2667#endif
2668#ifdef TARGET_ARM
2669 case QEMU_OPTION_old_param:
2670 old_param = 1;
2671 break;
2672#endif
2673 case QEMU_OPTION_clock:
2674 configure_alarms(optarg);
2675 break;
2676 case QEMU_OPTION_startdate:
2677 {
2678 struct tm tm;
David 'Digit' Turnerdc468202010-10-27 02:34:46 +02002679 time_t rtc_start_date = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002680 if (!strcmp(optarg, "now")) {
2681 rtc_date_offset = -1;
2682 } else {
2683 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
2684 &tm.tm_year,
2685 &tm.tm_mon,
2686 &tm.tm_mday,
2687 &tm.tm_hour,
2688 &tm.tm_min,
2689 &tm.tm_sec) == 6) {
2690 /* OK */
2691 } else if (sscanf(optarg, "%d-%d-%d",
2692 &tm.tm_year,
2693 &tm.tm_mon,
2694 &tm.tm_mday) == 3) {
2695 tm.tm_hour = 0;
2696 tm.tm_min = 0;
2697 tm.tm_sec = 0;
2698 } else {
2699 goto date_fail;
2700 }
2701 tm.tm_year -= 1900;
2702 tm.tm_mon--;
2703 rtc_start_date = mktimegm(&tm);
2704 if (rtc_start_date == -1) {
2705 date_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002706 PANIC("Invalid date format. Valid format are:\n"
2707 "'now' or '2006-06-17T16:01:21' or '2006-06-17'");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002708 }
2709 rtc_date_offset = time(NULL) - rtc_start_date;
2710 }
2711 }
2712 break;
rich cannings7339b552011-02-16 13:43:44 -08002713
2714 /* -------------------------------------------------------*/
2715 /* User mode network stack restrictions */
2716 case QEMU_OPTION_drop_udp:
2717 slirp_drop_udp();
2718 break;
2719 case QEMU_OPTION_drop_tcp:
2720 slirp_drop_tcp();
2721 break;
2722 case QEMU_OPTION_allow_tcp:
2723 slirp_allow(optarg, IPPROTO_TCP);
2724 break;
2725 case QEMU_OPTION_allow_udp:
2726 slirp_allow(optarg, IPPROTO_UDP);
2727 break;
2728 case QEMU_OPTION_drop_log:
2729 {
2730 FILE* drop_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08002731 drop_log_filename = optarg;
2732 drop_log_fd = fopen(optarg, "w+");
rich cannings7339b552011-02-16 13:43:44 -08002733
2734 if (!drop_log_fd) {
2735 fprintf(stderr, "Cannot open drop log: %s\n", optarg);
2736 exit(1);
2737 }
2738
2739 slirp_drop_log_fd(drop_log_fd);
2740 }
2741 break;
2742
2743 case QEMU_OPTION_dns_log:
2744 {
2745 FILE* dns_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08002746 dns_log_filename = optarg;
2747 dns_log_fd = fopen(optarg, "wb+");
rich cannings7339b552011-02-16 13:43:44 -08002748
2749 if (dns_log_fd == NULL) {
2750 fprintf(stderr, "Cannot open dns log: %s\n", optarg);
2751 exit(1);
2752 }
2753
2754 slirp_dns_log_fd(dns_log_fd);
2755 }
2756 break;
2757
2758
2759 case QEMU_OPTION_max_dns_conns:
2760 {
2761 int max_dns_conns = 0;
2762 if (parse_int(optarg, &max_dns_conns)) {
2763 fprintf(stderr,
2764 "qemu: syntax: -max-dns-conns max_connections\n");
2765 exit(1);
2766 }
2767 if (max_dns_conns <= 0 || max_dns_conns == LONG_MAX) {
2768 fprintf(stderr,
2769 "Invalid arg for max dns connections: %s\n",
2770 optarg);
2771 exit(1);
2772 }
2773 slirp_set_max_dns_conns(max_dns_conns);
2774 }
2775 break;
2776
2777 case QEMU_OPTION_net_forward:
2778 net_slirp_forward(optarg);
2779 break;
2780 case QEMU_OPTION_net_forward_tcp2sink:
2781 {
2782 SockAddress saddr;
2783
2784 if (parse_host_port(&saddr, optarg)) {
2785 fprintf(stderr,
2786 "Invalid ip/port %s for "
2787 "-forward-dropped-tcp2sink. "
2788 "We expect 'sink_ip:sink_port'\n",
2789 optarg);
2790 exit(1);
2791 }
2792 slirp_forward_dropped_tcp2sink(saddr.u.inet.address,
2793 saddr.u.inet.port);
2794 }
2795 break;
2796 /* -------------------------------------------------------*/
2797
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002798 case QEMU_OPTION_tb_size:
2799 tb_size = strtol(optarg, NULL, 0);
2800 if (tb_size < 0)
2801 tb_size = 0;
2802 break;
2803 case QEMU_OPTION_icount:
David Turner6a9ef172010-09-09 22:54:36 +02002804 icount_option = optarg;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002805 break;
2806 case QEMU_OPTION_incoming:
2807 incoming = optarg;
2808 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002809#ifdef CONFIG_XEN
2810 case QEMU_OPTION_xen_domid:
2811 xen_domid = atoi(optarg);
2812 break;
2813 case QEMU_OPTION_xen_create:
2814 xen_mode = XEN_CREATE;
2815 break;
2816 case QEMU_OPTION_xen_attach:
2817 xen_mode = XEN_ATTACH;
2818 break;
2819#endif
2820
2821
2822 case QEMU_OPTION_mic:
2823 audio_input_source = (char*)optarg;
2824 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002825#ifdef CONFIG_NAND
2826 case QEMU_OPTION_nand:
2827 nand_add_dev(optarg);
2828 break;
Jun Nakajimaa381ef02011-12-17 19:13:25 -08002829
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002830#endif
David 'Digit' Turner045bdbe2014-04-28 22:26:52 +02002831#ifdef CONFIG_HAX
2832 case QEMU_OPTION_enable_hax:
2833 hax_disabled = 0;
2834 break;
Jun Nakajimaa381ef02011-12-17 19:13:25 -08002835 case QEMU_OPTION_disable_hax:
2836 hax_disabled = 1;
2837 break;
David 'Digit' Turner045bdbe2014-04-28 22:26:52 +02002838#endif
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07002839 case QEMU_OPTION_android_ports:
2840 android_op_ports = (char*)optarg;
2841 break;
2842
2843 case QEMU_OPTION_android_port:
2844 android_op_port = (char*)optarg;
2845 break;
2846
2847 case QEMU_OPTION_android_report_console:
2848 android_op_report_console = (char*)optarg;
2849 break;
2850
2851 case QEMU_OPTION_http_proxy:
2852 op_http_proxy = (char*)optarg;
2853 break;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07002854
2855 case QEMU_OPTION_charmap:
2856 op_charmap_file = (char*)optarg;
2857 break;
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07002858
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07002859 case QEMU_OPTION_android_hw:
2860 android_op_hwini = (char*)optarg;
2861 break;
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07002862
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07002863 case QEMU_OPTION_dns_server:
2864 android_op_dns_server = (char*)optarg;
2865 break;
2866
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07002867 case QEMU_OPTION_radio:
2868 android_op_radio = (char*)optarg;
2869 break;
2870
2871 case QEMU_OPTION_gps:
2872 android_op_gps = (char*)optarg;
2873 break;
2874
2875 case QEMU_OPTION_audio:
2876 android_op_audio = (char*)optarg;
2877 break;
2878
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07002879 case QEMU_OPTION_cpu_delay:
2880 android_op_cpu_delay = (char*)optarg;
2881 break;
2882
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07002883 case QEMU_OPTION_show_kernel:
2884 android_kmsg_init(ANDROID_KMSG_PRINT_MESSAGES);
2885 break;
2886
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07002887#ifdef CONFIG_NAND_LIMITS
2888 case QEMU_OPTION_nand_limits:
2889 android_op_nand_limits = (char*)optarg;
2890 break;
2891#endif // CONFIG_NAND_LIMITS
2892
2893 case QEMU_OPTION_netspeed:
2894 android_op_netspeed = (char*)optarg;
2895 break;
2896
2897 case QEMU_OPTION_netdelay:
2898 android_op_netdelay = (char*)optarg;
2899 break;
2900
2901 case QEMU_OPTION_netfast:
2902 android_op_netfast = 1;
2903 break;
2904
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07002905 case QEMU_OPTION_tcpdump:
2906 android_op_tcpdump = (char*)optarg;
2907 break;
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07002908
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07002909 case QEMU_OPTION_boot_property:
2910 boot_property_parse_option((char*)optarg);
2911 break;
2912
2913 case QEMU_OPTION_lcd_density:
2914 android_op_lcd_density = (char*)optarg;
2915 break;
2916
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002917 case QEMU_OPTION_ui_port:
2918 android_op_ui_port = (char*)optarg;
2919 break;
2920
2921 case QEMU_OPTION_ui_settings:
2922 android_op_ui_settings = (char*)optarg;
2923 break;
2924
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +01002925 case QEMU_OPTION_audio_test_out:
2926 android_audio_test_start_out();
2927 break;
2928
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -08002929 case QEMU_OPTION_android_avdname:
2930 android_op_avd_name = (char*)optarg;
2931 break;
2932
2933 case QEMU_OPTION_timezone:
2934 if (timezone_set((char*)optarg)) {
2935 fprintf(stderr, "emulator: it seems the timezone '%s' is not in zoneinfo format\n",
2936 (char*)optarg);
2937 }
2938 break;
2939
David 'Digit' Turner5bb450e2014-03-14 17:19:45 +01002940#ifdef CONFIG_ANDROID_MEMCHECK
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07002941 case QEMU_OPTION_android_memcheck:
2942 android_op_memcheck = (char*)optarg;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07002943 /* This will set ro.kernel.memcheck system property
2944 * to memcheck's tracing flags. */
David 'Digit' Turner5f824112011-03-01 14:00:26 +01002945 stralloc_add_format(kernel_config, " memcheck=%s", android_op_memcheck);
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07002946 break;
David 'Digit' Turner5bb450e2014-03-14 17:19:45 +01002947#endif // CONFIG_ANDROID_MEMCHECK
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +01002948
2949 case QEMU_OPTION_snapshot_no_time_update:
2950 android_snapshot_update_time = 0;
2951 break;
Vladimir Chtchetkineb8dcaff2011-09-17 11:15:47 -07002952
2953 case QEMU_OPTION_list_webcam:
2954 android_list_web_cameras();
2955 exit(0);
2956
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02002957 default:
2958 os_parse_cmd_args(popt->index, optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002959 }
2960 }
2961 }
2962
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07002963 /* Initialize character map. */
2964 if (android_charmap_setup(op_charmap_file)) {
2965 if (op_charmap_file) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002966 PANIC(
2967 "Unable to initialize character map from file %s.",
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07002968 op_charmap_file);
2969 } else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002970 PANIC(
2971 "Unable to initialize default character map.");
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07002972 }
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07002973 }
2974
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002975 /* If no data_dir is specified then try to find it relative to the
2976 executable path. */
2977 if (!data_dir) {
2978 data_dir = find_datadir(argv[0]);
2979 }
2980 /* If all else fails use the install patch specified when building. */
2981 if (!data_dir) {
2982 data_dir = CONFIG_QEMU_SHAREDIR;
2983 }
2984
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01002985 if (!android_op_hwini) {
2986 PANIC("Missing -android-hw <file> option!");
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07002987 }
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01002988 hw_ini = iniFile_newFromFile(android_op_hwini);
2989 if (hw_ini == NULL) {
2990 PANIC("Could not find %s file.", android_op_hwini);
2991 }
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01002992
2993 androidHwConfig_init(android_hw, 0);
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07002994 androidHwConfig_read(android_hw, hw_ini);
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01002995
Vladimir Chtchetkinedb450d72012-01-12 13:37:40 -08002996 /* If we're loading VM from a snapshot, make sure that the current HW config
2997 * matches the one with which the VM has been saved. */
2998 if (loadvm && *loadvm && !snaphost_match_configs(hw_ini, loadvm)) {
2999 exit(0);
3000 }
3001
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003002 iniFile_free(hw_ini);
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003003
David 'Digit' Turnerc6e0cae2014-03-07 23:08:30 +01003004 const char* kernelSerialDevicePrefix =
3005 androidHwConfig_getKernelSerialPrefix(android_hw);
3006 VERBOSE_PRINT(init, "Using kernel serial device prefix: %s",
3007 kernelSerialDevicePrefix);
3008
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003009 {
3010 int width = android_hw->hw_lcd_width;
3011 int height = android_hw->hw_lcd_height;
3012 int depth = android_hw->hw_lcd_depth;
3013
3014 /* A bit of sanity checking */
3015 if (width <= 0 || height <= 0 ||
3016 (depth != 16 && depth != 32) ||
3017 (((width|height) & 3) != 0) )
3018 {
3019 PANIC("Invalid display configuration (%d,%d,%d)",
3020 width, height, depth);
3021 }
3022 android_display_width = width;
3023 android_display_height = height;
3024 android_display_bpp = depth;
3025 }
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003026
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003027#ifdef CONFIG_NAND_LIMITS
3028 /* Init nand stuff. */
3029 if (android_op_nand_limits) {
3030 parse_nand_limits(android_op_nand_limits);
3031 }
3032#endif // CONFIG_NAND_LIMITS
3033
David 'Digit' Turner53eb66d2011-03-01 14:58:23 +01003034 /* Initialize AVD name from hardware configuration if needed */
3035 if (!android_op_avd_name) {
3036 if (android_hw->avd_name && *android_hw->avd_name) {
3037 android_op_avd_name = android_hw->avd_name;
3038 VERBOSE_PRINT(init,"AVD Name: %s", android_op_avd_name);
3039 }
3040 }
3041
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003042 /* Initialize system partition image */
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +02003043 android_nand_add_image("system",
3044 ANDROID_PARTITION_TYPE_UNKNOWN,
3045 android_hw->disk_systemPartition_size,
3046 android_hw->disk_systemPartition_path,
3047 android_hw->disk_systemPartition_initPath);
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003048
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003049 /* Initialize data partition image */
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +02003050 android_nand_add_image("userdata",
3051 ANDROID_PARTITION_TYPE_UNKNOWN,
3052 android_hw->disk_dataPartition_size,
3053 android_hw->disk_dataPartition_path,
3054 android_hw->disk_dataPartition_initPath);
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003055
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +02003056 /* Initialize cache partition image, if any. Its type depends on the
3057 * kernel version. For anything >= 3.10, it must be EXT4, or
3058 * YAFFS2 otherwise.
3059 */
3060 if (android_hw->disk_cachePartition != 0) {
3061 AndroidPartitionType cache_part_type =
3062 (androidHwConfig_getKernelYaffs2Support(android_hw) >= 1) ?
3063 ANDROID_PARTITION_TYPE_YAFFS2 :
3064 ANDROID_PARTITION_TYPE_EXT4;
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003065
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +02003066 android_nand_add_image("cache",
3067 cache_part_type,
3068 android_hw->disk_cachePartition_size,
3069 android_hw->disk_cachePartition_path,
3070 NULL);
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003071 }
3072
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01003073 /* Init SD-Card stuff. For Android, it is always hda */
3074 /* If the -hda option was used, ignore the Android-provided one */
3075 if (hda_opts == NULL) {
3076 const char* sdPath = android_hw->hw_sdCard_path;
3077 if (sdPath && *sdPath) {
3078 if (!path_exists(sdPath)) {
3079 fprintf(stderr, "WARNING: SD Card image is missing: %s\n", sdPath);
3080 } else if (filelock_create(sdPath) == NULL) {
3081 fprintf(stderr, "WARNING: SD Card image already in use: %s\n", sdPath);
3082 } else {
3083 /* Successful locking */
3084 hda_opts = drive_add(sdPath, HD_ALIAS, 0);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02003085 /* Set this property of any operation involving the SD Card
3086 * will be x100 slower, due to the corresponding file being
3087 * mounted as O_DIRECT. Note that this is only 'unsafe' in
3088 * the context of an emulator crash. The data is already
3089 * synced properly when the emulator exits (either normally or through ^C).
3090 */
3091 qemu_opt_set(hda_opts, "cache", "unsafe");
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01003092 }
3093 }
3094 }
3095
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003096 if (hdb_opts == NULL) {
3097 const char* spath = android_hw->disk_snapStorage_path;
3098 if (spath && *spath) {
3099 if (!path_exists(spath)) {
3100 PANIC("Snapshot storage file does not exist: %s", spath);
3101 }
3102 if (filelock_create(spath) == NULL) {
David 'Digit' Turner4297b822011-05-04 19:18:15 +02003103 PANIC("Snapshot storage already in use: %s", spath);
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003104 }
3105 hdb_opts = drive_add(spath, HD_ALIAS, 1);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02003106 /* See comment above to understand why this is needed. */
David 'Digit' Turner9fb360e2011-05-04 22:01:28 +02003107 qemu_opt_set(hdb_opts, "cache", "unsafe");
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003108 }
3109 }
3110
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003111 /* Set the VM's max heap size, passed as a boot property */
3112 if (android_hw->vm_heapSize > 0) {
3113 char tmp[64];
3114 snprintf(tmp, sizeof(tmp), "%dm", android_hw->vm_heapSize);
3115 boot_property_add("dalvik.vm.heapsize",tmp);
3116 }
3117
Siva Velusamy0faeb3a2013-10-29 14:23:35 -07003118 /* From API 19 and above, the platform provides an explicit property for low memory devices. */
3119 if (android_hw->hw_ramSize <= 512) {
3120 boot_property_add("ro.config.low_ram", "true");
3121 }
3122
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003123 /* Initialize net speed and delays stuff. */
3124 if (android_parse_network_speed(android_op_netspeed) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003125 PANIC("invalid -netspeed parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003126 android_op_netspeed);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003127 }
3128
3129 if ( android_parse_network_latency(android_op_netdelay) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003130 PANIC("invalid -netdelay parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003131 android_op_netdelay);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003132 }
3133
3134 if (android_op_netfast) {
3135 qemu_net_download_speed = 0;
3136 qemu_net_upload_speed = 0;
3137 qemu_net_min_latency = 0;
3138 qemu_net_max_latency = 0;
3139 }
3140
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003141 /* Initialize LCD density */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01003142 if (android_hw->hw_lcd_density) {
3143 long density = android_hw->hw_lcd_density;
3144 if (density <= 0) {
3145 PANIC("Invalid hw.lcd.density value: %ld", density);
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003146 }
3147 hwLcd_setBootProperty(density);
3148 }
3149
Xavier Ducrohetfa0c8e22011-10-04 10:41:26 -07003150 /* Initialize presence of hardware nav button */
3151 boot_property_add("qemu.hw.mainkeys", android_hw->hw_mainKeys ? "1" : "0");
3152
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07003153 /* Initialize TCP dump */
3154 if (android_op_tcpdump) {
3155 if (qemu_tcpdump_start(android_op_tcpdump) < 0) {
3156 fprintf(stdout, "could not start packet capture: %s\n", strerror(errno));
3157 }
3158 }
3159
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003160 /* Initialize modem */
3161 if (android_op_radio) {
3162 CharDriverState* cs = qemu_chr_open("radio", android_op_radio, NULL);
3163 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003164 PANIC("unsupported character device specification: %s\n"
3165 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003166 android_op_radio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003167 }
3168 android_qemud_set_channel( ANDROID_QEMUD_GSM, cs);
3169 } else if (android_hw->hw_gsmModem != 0 ) {
3170 if ( android_qemud_get_channel( ANDROID_QEMUD_GSM, &android_modem_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003171 PANIC("could not initialize qemud 'gsm' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003172 }
3173 }
3174
3175 /* Initialize GPS */
3176 if (android_op_gps) {
3177 CharDriverState* cs = qemu_chr_open("gps", android_op_gps, NULL);
3178 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003179 PANIC("unsupported character device specification: %s\n"
3180 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003181 android_op_gps);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003182 }
3183 android_qemud_set_channel( ANDROID_QEMUD_GPS, cs);
3184 } else if (android_hw->hw_gps != 0) {
3185 if ( android_qemud_get_channel( "gps", &android_gps_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003186 PANIC("could not initialize qemud 'gps' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003187 }
3188 }
3189
3190 /* Initialize audio. */
3191 if (android_op_audio) {
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003192 if ( !audio_check_backend_name( 0, android_op_audio ) ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003193 PANIC("'%s' is not a valid audio output backend. see -help-audio-out",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003194 android_op_audio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003195 }
David 'Digit' Turnerf816a752011-06-23 18:40:11 +02003196 setenv("QEMU_AUDIO_DRV", android_op_audio, 1);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003197 }
3198
David 'Digit' Turner5b481492011-04-11 17:37:32 +02003199 /* Initialize OpenGLES emulation */
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02003200 //android_hw_opengles_init();
David 'Digit' Turner5b481492011-04-11 17:37:32 +02003201
Vladimir Chtchetkineae0d8132011-09-13 10:48:02 -07003202 /* Initialize fake camera */
Vladimir Chtchetkine7485c292012-03-19 11:35:29 -07003203 if (strcmp(android_hw->hw_camera_back, "emulated") &&
3204 strcmp(android_hw->hw_camera_front, "emulated")) {
3205 /* Fake camera is not used for camera emulation. */
3206 boot_property_add("qemu.sf.fake_camera", "none");
Vladimir Chtchetkineae0d8132011-09-13 10:48:02 -07003207 } else {
Vladimir Chtchetkine7485c292012-03-19 11:35:29 -07003208 /* Fake camera is used for at least one camera emulation. */
3209 if (!strcmp(android_hw->hw_camera_back, "emulated") &&
3210 !strcmp(android_hw->hw_camera_front, "emulated")) {
3211 /* Fake camera is used for both, front and back camera emulation. */
3212 boot_property_add("qemu.sf.fake_camera", "both");
3213 } else if (!strcmp(android_hw->hw_camera_back, "emulated")) {
3214 boot_property_add("qemu.sf.fake_camera", "back");
3215 } else {
3216 boot_property_add("qemu.sf.fake_camera", "front");
3217 }
Vladimir Chtchetkineae0d8132011-09-13 10:48:02 -07003218 }
3219
Vladimir Chtchetkine035f8052012-05-18 12:19:32 -07003220 /* Set LCD density (if required by -qemu, and AVD is missing it. */
3221 if (android_op_lcd_density && !android_hw->hw_lcd_density) {
3222 int density;
3223 if (parse_int(android_op_lcd_density, &density) || density <= 0) {
3224 PANIC("-lcd-density : %d", density);
3225 }
3226 hwLcd_setBootProperty(density);
3227 }
3228
Vladimir Chtchetkineb8dcaff2011-09-17 11:15:47 -07003229 /* Initialize camera emulation. */
3230 android_camera_service_init();
3231
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003232 if (android_op_cpu_delay) {
3233 char* end;
3234 long delay = strtol(android_op_cpu_delay, &end, 0);
3235 if (end == NULL || *end || delay < 0 || delay > 1000 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003236 PANIC("option -cpu-delay must be an integer between 0 and 1000" );
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003237 }
3238 if (delay > 0)
3239 delay = (1000-delay);
3240
3241 qemu_cpu_delay = (int) delay;
3242 }
3243
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003244 if (android_op_dns_server) {
3245 char* x = strchr(android_op_dns_server, ',');
3246 dns_count = 0;
3247 if (x == NULL)
3248 {
3249 if ( add_dns_server( android_op_dns_server ) == 0 )
3250 dns_count = 1;
3251 }
3252 else
3253 {
3254 x = android_op_dns_server;
3255 while (*x) {
3256 char* y = strchr(x, ',');
3257
3258 if (y != NULL) {
3259 *y = 0;
3260 y++;
3261 } else {
3262 y = x + strlen(x);
3263 }
3264
3265 if (y > x && add_dns_server( x ) == 0) {
3266 dns_count += 1;
3267 }
3268 x = y;
3269 }
3270 }
3271 if (dns_count == 0)
3272 fprintf( stdout, "### WARNING: will use system default DNS server\n" );
3273 }
3274
3275 if (dns_count == 0)
3276 dns_count = slirp_get_system_dns_servers();
3277 if (dns_count) {
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003278 stralloc_add_format(kernel_config, " ndns=%d", dns_count);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003279 }
3280
David 'Digit' Turner5bb450e2014-03-14 17:19:45 +01003281#ifdef CONFIG_ANDROID_MEMCHECK
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003282 if (android_op_memcheck) {
3283 memcheck_init(android_op_memcheck);
3284 }
David 'Digit' Turner5bb450e2014-03-14 17:19:45 +01003285#endif // CONFIG_ANDROID_MEMCHECK
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003286
Jesse Hall733fffa2012-04-26 11:07:32 -07003287 /* qemu.gles will be read by the OpenGL ES emulation libraries.
3288 * If set to 0, the software GL ES renderer will be used as a fallback.
3289 * If the parameter is undefined, this means the system image runs
3290 * inside an emulator that doesn't support GPU emulation at all.
3291 *
3292 * We always start the GL ES renderer so we can gather stats on the
3293 * underlying GL implementation. If GL ES acceleration is disabled,
3294 * we just shut it down again once we have the strings. */
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003295 {
Jesse Hall733fffa2012-04-26 11:07:32 -07003296 int qemu_gles = 0;
Jesse Hall733fffa2012-04-26 11:07:32 -07003297 if (android_initOpenglesEmulation() == 0 &&
Jesse Hallba5c1f62012-05-08 15:44:35 -07003298 android_startOpenglesRenderer(android_hw->hw_lcd_width, android_hw->hw_lcd_height) == 0)
Jesse Hall733fffa2012-04-26 11:07:32 -07003299 {
3300 android_getOpenglesHardwareStrings(
3301 android_gl_vendor, sizeof(android_gl_vendor),
3302 android_gl_renderer, sizeof(android_gl_renderer),
3303 android_gl_version, sizeof(android_gl_version));
3304 if (android_hw->hw_gpu_enabled) {
3305 qemu_gles = 1;
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003306 } else {
Jesse Hall733fffa2012-04-26 11:07:32 -07003307 android_stopOpenglesRenderer();
3308 qemu_gles = 0;
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003309 }
Jesse Hall733fffa2012-04-26 11:07:32 -07003310 } else {
3311 dwarning("Could not initialize OpenglES emulation, using software renderer.");
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003312 }
Jesse Hall733fffa2012-04-26 11:07:32 -07003313 if (qemu_gles) {
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003314 stralloc_add_str(kernel_params, " qemu.gles=1");
3315 } else {
3316 stralloc_add_str(kernel_params, " qemu.gles=0");
3317 }
3318 }
3319
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003320 /* We always force qemu=1 when running inside QEMU */
3321 stralloc_add_str(kernel_params, " qemu=1");
3322
3323 /* We always initialize the first serial port for the android-kmsg
3324 * character device (used to send kernel messages) */
3325 serial_hds_add_at(0, "android-kmsg");
David 'Digit' Turnerc6e0cae2014-03-07 23:08:30 +01003326 stralloc_add_format(kernel_params,
3327 " console=%s0",
3328 kernelSerialDevicePrefix);
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003329
3330 /* We always initialize the second serial port for the android-qemud
3331 * character device as well */
3332 serial_hds_add_at(1, "android-qemud");
David 'Digit' Turnerc6e0cae2014-03-07 23:08:30 +01003333 stralloc_add_format(kernel_params,
3334 " android.qemud=%s1",
3335 kernelSerialDevicePrefix);
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003336
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003337 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3338 os_pidfile_error();
3339 exit(1);
3340 }
3341
David 'Digit' Turner7e9f6c12014-01-22 18:42:40 +01003342 /* Open the logfile at this point, if necessary. We can't open the logfile
3343 * when encountering either of the logging options (-d or -D) because the
3344 * other one may be encountered later on the command line, changing the
3345 * location or level of logging.
3346 */
3347 if (log_mask) {
3348 int mask;
3349 if (log_file) {
3350 qemu_set_log_filename(log_file);
3351 }
3352
3353 mask = qemu_str_to_log_mask(log_mask);
3354 if (!mask) {
3355 qemu_print_log_usage(stdout);
3356 exit(1);
3357 }
3358 qemu_set_log(mask);
3359 }
David 'Digit' Turnerc0052462014-02-25 18:39:29 +01003360
David 'Digit' Turner36597752011-05-20 01:18:01 +02003361#if defined(CONFIG_KVM)
3362 if (kvm_allowed < 0) {
3363 kvm_allowed = kvm_check_allowed();
3364 }
3365#endif
3366
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003367 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3368 if (smp_cpus > machine->max_cpus) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003369 PANIC("Number of SMP cpus requested (%d), exceeds max cpus "
3370 "supported by machine `%s' (%d)", smp_cpus, machine->name,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003371 machine->max_cpus);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003372 }
3373
3374 if (display_type == DT_NOGRAPHIC) {
3375 if (serial_device_index == 0)
3376 serial_devices[0] = "stdio";
3377 if (parallel_device_index == 0)
3378 parallel_devices[0] = "null";
3379 if (strncmp(monitor_device, "vc", 2) == 0)
3380 monitor_device = "stdio";
3381 }
3382
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003383 if (qemu_init_main_loop()) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003384 PANIC("qemu_init_main_loop failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003385 }
David 'Digit' Turner0b019492011-03-01 14:02:42 +01003386
3387 if (kernel_filename == NULL) {
3388 kernel_filename = android_hw->kernel_path;
3389 }
3390 if (initrd_filename == NULL) {
3391 initrd_filename = android_hw->disk_ramdisk_path;
3392 }
3393
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003394 linux_boot = (kernel_filename != NULL);
3395 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
3396
3397 if (!linux_boot && *kernel_cmdline != '\0') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003398 PANIC("-append only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003399 }
3400
3401 if (!linux_boot && initrd_filename != NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003402 PANIC("-initrd only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003403 }
3404
3405 /* boot to floppy or the default cd if no hard disk defined yet */
3406 if (!boot_devices[0]) {
3407 boot_devices = "cad";
3408 }
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003409 os_set_line_buffering();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003410
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003411 if (init_timer_alarm() < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003412 PANIC("could not initialize alarm timer");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003413 }
David Turner6a9ef172010-09-09 22:54:36 +02003414 configure_icount(icount_option);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003415
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003416 /* init network clients */
3417 if (nb_net_clients == 0) {
3418 /* if no clients, we use a default config */
3419 net_clients[nb_net_clients++] = "nic";
3420#ifdef CONFIG_SLIRP
3421 net_clients[nb_net_clients++] = "user";
3422#endif
3423 }
3424
3425 for(i = 0;i < nb_net_clients; i++) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003426 if (net_client_parse(net_clients[i]) < 0) {
3427 PANIC("Unable to parse net clients");
3428 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003429 }
3430 net_client_check();
3431
3432#ifdef TARGET_I386
3433 /* XXX: this should be moved in the PC machine instantiation code */
3434 if (net_boot != 0) {
3435 int netroms = 0;
3436 for (i = 0; i < nb_nics && i < 4; i++) {
3437 const char *model = nd_table[i].model;
3438 char buf[1024];
3439 char *filename;
3440 if (net_boot & (1 << i)) {
3441 if (model == NULL)
3442 model = "ne2k_pci";
3443 snprintf(buf, sizeof(buf), "pxe-%s.bin", model);
3444 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, buf);
3445 if (filename && get_image_size(filename) > 0) {
3446 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003447 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003448 }
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01003449 option_rom[nb_option_roms] = g_strdup(buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003450 nb_option_roms++;
3451 netroms++;
3452 }
3453 if (filename) {
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01003454 g_free(filename);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003455 }
3456 }
3457 }
3458 if (netroms == 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003459 PANIC("No valid PXE rom found for network device");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003460 }
3461 }
3462#endif
3463
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003464 /* init the memory */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01003465 if (ram_size == 0) {
3466 ram_size = android_hw->hw_ramSize * 1024LL * 1024;
3467 if (ram_size == 0) {
3468 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3469 }
3470 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003471
Vladimir Chtchetkinecf289fb2012-05-14 08:04:29 -07003472 /* Quite often (especially on older XP machines) attempts to allocate large
3473 * VM RAM is going to fail, and crash the emulator. Since it's failing deep
3474 * inside QEMU, it's not really possible to provide the user with a
3475 * meaningful explanation for the crash. So, lets see if QEMU is going to be
3476 * able to allocate requested amount of RAM, and if not, lets try to come up
3477 * with a recomendation. */
3478 {
3479 ram_addr_t r_ram = ram_size;
3480 void* alloc_check = malloc(r_ram);
3481 while (alloc_check == NULL && r_ram > 1024 * 1024) {
3482 /* Make it 25% less */
3483 r_ram -= r_ram / 4;
3484 alloc_check = malloc(r_ram);
3485 }
3486 if (alloc_check != NULL) {
3487 free(alloc_check);
3488 }
3489 if (r_ram != ram_size) {
3490 /* Requested RAM is too large. Report this, as well as calculated
3491 * recomendation. */
3492 dwarning("Requested RAM size of %dMB is too large for your environment, and is reduced to %dMB.",
3493 (int)(ram_size / 1024 / 1024), (int)(r_ram / 1024 / 1024));
3494 ram_size = r_ram;
3495 }
3496 }
3497
rich canningsd952f282011-03-01 15:40:09 -08003498#ifndef _WIN32
David 'Digit' Turnerf9077a82014-02-10 23:10:47 +01003499 qemu_log_rotation_init();
rich canningsd952f282011-03-01 15:40:09 -08003500#endif
3501
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003502 /* init the dynamic translator */
3503 cpu_exec_init_all(tb_size * 1024 * 1024);
3504
3505 bdrv_init();
3506
3507 /* we always create the cdrom drive, even if no disk is there */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003508#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003509 if (nb_drives_opt < MAX_DRIVES)
3510 drive_add(NULL, CDROM_ALIAS);
3511
3512 /* we always create at least one floppy */
3513
3514 if (nb_drives_opt < MAX_DRIVES)
3515 drive_add(NULL, FD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003516 /* we always create one sd slot, even if no card is in it */
3517
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003518 if (1) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003519 drive_add(NULL, SD_ALIAS);
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003520 }
3521#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003522
3523 /* open the virtual block devices */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003524 if (snapshot)
3525 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3526 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3527 exit(1);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003528
David 'Digit' Turner5cb5c0b2014-02-17 16:04:03 +01003529 //register_savevm(NULL, "timer", 0, 2, timer_save, timer_load, &timers_state);
3530
3531 SaveVMHandlers* ops = g_malloc0(sizeof(*ops));
3532 ops->save_live_state = ram_save_live;
3533 ops->load_state = ram_load;
3534
3535 register_savevm_live(NULL,
3536 "ram",
3537 0,
3538 3,
3539 ops,
3540 NULL);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003541
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003542 /* must be after terminal init, SDL library changes signal handlers */
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003543 os_setup_signal_handling();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003544
3545 /* Maintain compatibility with multiple stdio monitors */
3546 if (!strcmp(monitor_device,"stdio")) {
3547 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
3548 const char *devname = serial_devices[i];
3549 if (devname && !strcmp(devname,"mon:stdio")) {
3550 monitor_device = NULL;
3551 break;
3552 } else if (devname && !strcmp(devname,"stdio")) {
3553 monitor_device = NULL;
3554 serial_devices[i] = "mon:stdio";
3555 break;
3556 }
3557 }
3558 }
3559
3560 if (nb_numa_nodes > 0) {
3561 int i;
3562
3563 if (nb_numa_nodes > smp_cpus) {
3564 nb_numa_nodes = smp_cpus;
3565 }
3566
3567 /* If no memory size if given for any node, assume the default case
3568 * and distribute the available memory equally across all nodes
3569 */
3570 for (i = 0; i < nb_numa_nodes; i++) {
3571 if (node_mem[i] != 0)
3572 break;
3573 }
3574 if (i == nb_numa_nodes) {
3575 uint64_t usedmem = 0;
3576
3577 /* On Linux, the each node's border has to be 8MB aligned,
3578 * the final node gets the rest.
3579 */
3580 for (i = 0; i < nb_numa_nodes - 1; i++) {
3581 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3582 usedmem += node_mem[i];
3583 }
3584 node_mem[i] = ram_size - usedmem;
3585 }
3586
3587 for (i = 0; i < nb_numa_nodes; i++) {
3588 if (node_cpumask[i] != 0)
3589 break;
3590 }
3591 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3592 * must cope with this anyway, because there are BIOSes out there in
3593 * real machines which also use this scheme.
3594 */
3595 if (i == nb_numa_nodes) {
3596 for (i = 0; i < smp_cpus; i++) {
3597 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3598 }
3599 }
3600 }
3601
3602 if (kvm_enabled()) {
3603 int ret;
3604
3605 ret = kvm_init(smp_cpus);
3606 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003607 PANIC("failed to initialize KVM");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003608 }
3609 }
3610
Jun Nakajimaa381ef02011-12-17 19:13:25 -08003611#ifdef CONFIG_HAX
3612 if (!hax_disabled)
3613 {
3614 int ret;
3615
Jiang, Yunhong4a5a0ef2012-02-23 06:31:12 +08003616 hax_set_ramsize(ram_size);
Jun Nakajimaa381ef02011-12-17 19:13:25 -08003617 ret = hax_init(smp_cpus);
3618 fprintf(stderr, "HAX is %s and emulator runs in %s mode\n",
3619 !ret ? "working" :"not working", !ret ? "fast virt" : "emulation");
3620 }
3621#endif
3622
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003623 if (monitor_device) {
3624 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
3625 if (!monitor_hd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003626 PANIC("qemu: could not open monitor device '%s'",
3627 monitor_device);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003628 }
3629 }
3630
3631 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003632 serial_hds_add(serial_devices[i]);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003633 }
3634
3635 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
3636 const char *devname = parallel_devices[i];
3637 if (devname && strcmp(devname, "none")) {
3638 char label[32];
3639 snprintf(label, sizeof(label), "parallel%d", i);
3640 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
3641 if (!parallel_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003642 PANIC("qemu: could not open parallel device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003643 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003644 }
3645 }
3646 }
3647
3648 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
3649 const char *devname = virtio_consoles[i];
3650 if (devname && strcmp(devname, "none")) {
3651 char label[32];
3652 snprintf(label, sizeof(label), "virtcon%d", i);
3653 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
3654 if (!virtcon_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003655 PANIC("qemu: could not open virtio console '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003656 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003657 }
3658 }
3659 }
3660
3661 module_call_init(MODULE_INIT_DEVICE);
3662
3663
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01003664 /* Check the CPU Architecture value */
David 'Digit' Turner7c07e4e2014-06-14 00:07:04 +02003665 {
3666 static const char* kSupportedArchs[] = {
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01003667#if defined(TARGET_ARM)
David 'Digit' Turner7c07e4e2014-06-14 00:07:04 +02003668 "arm",
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01003669#endif
David 'Digit' Turner7c07e4e2014-06-14 00:07:04 +02003670#if defined(TARGET_I386)
3671 "x86",
3672#endif
3673#if defined(TARGET_X86_64)
3674 "x86_64",
3675#endif
3676#if defined(TARGET_MIPS)
3677 "mips",
3678#endif
3679 };
3680 const size_t kNumSupportedArchs =
3681 sizeof(kSupportedArchs) / sizeof(kSupportedArchs[0]);
3682 bool supported_arch = false;
3683 size_t n;
3684 for (n = 0; n < kNumSupportedArchs; ++n) {
3685 if (!strcmp(android_hw->hw_cpu_arch, kSupportedArchs[n])) {
3686 supported_arch = true;
3687 break;
3688 }
3689 }
3690 if (!supported_arch) {
3691 fprintf(stderr, "-- Invalid CPU architecture: %s, valid values:",
3692 android_hw->hw_cpu_arch);
3693 for (n = 0; n < kNumSupportedArchs; ++n) {
3694 fprintf(stderr, " %s", kSupportedArchs[n]);
3695 }
3696 fprintf(stderr, "\n");
3697 exit(1);
3698 }
3699 }
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01003700
3701 /* Grab CPU model if provided in hardware.ini */
3702 if ( !cpu_model
3703 && android_hw->hw_cpu_model
3704 && android_hw->hw_cpu_model[0] != '\0')
3705 {
3706 cpu_model = android_hw->hw_cpu_model;
3707 }
3708
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003709 /* Combine kernel command line passed from the UI with parameters
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003710 * collected during initialization.
3711 *
3712 * The order is the following:
3713 * - parameters from the hw configuration (kernel.parameters)
3714 * - additionnal parameters from options (e.g. -memcheck)
3715 * - the -append parameters.
3716 */
3717 {
3718 const char* kernel_parameters;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003719
David 'Digit' Turner0b019492011-03-01 14:02:42 +01003720 if (android_hw->kernel_parameters) {
David 'Digit' Turner62c7ae52011-03-08 15:46:53 +01003721 stralloc_add_c(kernel_params, ' ');
David 'Digit' Turner0b019492011-03-01 14:02:42 +01003722 stralloc_add_str(kernel_params, android_hw->kernel_parameters);
3723 }
3724
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003725 /* If not empty, kernel_config always contains a leading space */
3726 stralloc_append(kernel_params, kernel_config);
3727
3728 if (*kernel_cmdline) {
3729 stralloc_add_c(kernel_params, ' ');
3730 stralloc_add_str(kernel_params, kernel_cmdline);
3731 }
3732
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003733 /* Remove any leading/trailing spaces */
3734 stralloc_strip(kernel_params);
3735
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003736 kernel_parameters = stralloc_cstr(kernel_params);
3737 VERBOSE_PRINT(init, "Kernel parameters: %s", kernel_parameters);
3738
3739 machine->init(ram_size,
3740 boot_devices,
3741 kernel_filename,
3742 kernel_parameters,
3743 initrd_filename,
3744 cpu_model);
3745
Vladimir Chtchetkine8dd31e82012-02-15 17:16:04 -08003746 /* Initialize multi-touch emulation. */
Vladimir Chtchetkine863d1012012-03-16 12:25:23 -07003747 if (androidHwConfig_isScreenMultiTouch(android_hw)) {
Vladimir Chtchetkine8dd31e82012-02-15 17:16:04 -08003748 mts_port_create(NULL);
3749 }
3750
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003751 stralloc_reset(kernel_params);
3752 stralloc_reset(kernel_config);
3753 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003754
David 'Digit' Turner66576782014-03-24 16:57:57 +01003755 CPU_FOREACH(cpu) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003756 for (i = 0; i < nb_numa_nodes; i++) {
David 'Digit' Turner66576782014-03-24 16:57:57 +01003757 if (node_cpumask[i] & (1 << cpu->cpu_index)) {
3758 cpu->numa_node = i;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003759 }
3760 }
3761 }
3762
3763 current_machine = machine;
3764
David 'Digit' Turnere2678e12014-01-16 15:56:43 +01003765 /* Set KVM's vcpu state to qemu's initial CPUOldState. */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003766 if (kvm_enabled()) {
3767 int ret;
3768
3769 ret = kvm_sync_vcpus();
3770 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003771 PANIC("failed to initialize vcpus");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003772 }
3773 }
3774
Jun Nakajimaa381ef02011-12-17 19:13:25 -08003775#ifdef CONFIG_HAX
3776 if (hax_enabled())
3777 hax_sync_vcpus();
3778#endif
3779
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08003780 /* just use the first displaystate for the moment */
David 'Digit' Turner94702b02011-01-20 02:46:33 +01003781 ds = get_displaystate();
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08003782
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003783 /* Initialize display from the command line parameters. */
3784 android_display_reset(ds,
3785 android_display_width,
3786 android_display_height,
3787 android_display_bpp);
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07003788
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003789 if (display_type == DT_DEFAULT) {
3790#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3791 display_type = DT_SDL;
3792#else
3793 display_type = DT_VNC;
3794 vnc_display = "localhost:0,to=99";
3795 show_vnc_port = 1;
3796#endif
3797 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07003798
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003799
3800 switch (display_type) {
3801 case DT_NOGRAPHIC:
3802 break;
3803#if defined(CONFIG_CURSES)
3804 case DT_CURSES:
3805 curses_display_init(ds, full_screen);
3806 break;
3807#endif
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07003808#if defined(CONFIG_SDL) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003809 case DT_SDL:
3810 sdl_display_init(ds, full_screen, no_frame);
3811 break;
3812#elif defined(CONFIG_COCOA)
3813 case DT_SDL:
3814 cocoa_display_init(ds, full_screen);
3815 break;
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08003816#elif defined(CONFIG_STANDALONE_CORE)
3817 case DT_SDL:
Vladimir Chtchetkinee95660a2010-12-20 08:28:03 -08003818 coredisplay_init(ds);
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08003819 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003820#endif
3821 case DT_VNC:
3822 vnc_display_init(ds);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003823 if (vnc_display_open(ds, vnc_display) < 0) {
3824 PANIC("Unable to initialize VNC display");
3825 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003826
3827 if (show_vnc_port) {
3828 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3829 }
3830 break;
3831 default:
3832 break;
3833 }
3834 dpy_resize(ds);
3835
3836 dcl = ds->listeners;
3837 while (dcl != NULL) {
3838 if (dcl->dpy_refresh != NULL) {
David 'Digit' Turnerdcda9492014-02-16 15:13:55 +01003839 ds->gui_timer = timer_new(QEMU_CLOCK_REALTIME, SCALE_MS, gui_update, ds);
3840 timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003841 }
3842 dcl = dcl->next;
3843 }
3844
3845 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
David 'Digit' Turnerdcda9492014-02-16 15:13:55 +01003846 nographic_timer = timer_new(QEMU_CLOCK_REALTIME, SCALE_MS, nographic_update, NULL);
3847 timer_mod(nographic_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003848 }
3849
David 'Digit' Turner94702b02011-01-20 02:46:33 +01003850 text_consoles_set_display(ds);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003851 qemu_chr_initial_reset();
3852
3853 if (monitor_device && monitor_hd)
3854 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
3855
3856 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
3857 const char *devname = serial_devices[i];
3858 if (devname && strcmp(devname, "none")) {
3859 if (strstart(devname, "vc", 0))
3860 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
3861 }
3862 }
3863
3864 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
3865 const char *devname = parallel_devices[i];
3866 if (devname && strcmp(devname, "none")) {
3867 if (strstart(devname, "vc", 0))
3868 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
3869 }
3870 }
3871
3872 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
3873 const char *devname = virtio_consoles[i];
3874 if (virtcon_hds[i] && devname) {
3875 if (strstart(devname, "vc", 0))
3876 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
3877 }
3878 }
3879
3880 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003881 PANIC("qemu: could not open gdbserver on device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003882 gdbstub_dev);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003883 }
3884
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003885 /* call android-specific setup function */
3886 android_emulation_setup();
3887
Vladimir Chtchetkine57584042011-01-20 16:15:30 -08003888#if !defined(CONFIG_STANDALONE_CORE)
3889 // For the standalone emulator (UI+core in one executable) we need to
3890 // set the window title here.
3891 android_emulator_set_base_port(android_base_port);
3892#endif
3893
Ot ten Thije871da2a2010-09-20 10:29:22 +01003894 if (loadvm)
3895 do_loadvm(cur_mon, loadvm);
3896
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003897 if (incoming) {
3898 autostart = 0; /* fixme how to deal with -daemonize */
3899 qemu_start_incoming_migration(incoming);
3900 }
3901
3902 if (autostart)
3903 vm_start();
3904
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003905 os_setup_post();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003906
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003907#ifdef CONFIG_ANDROID
3908 // This will notify the UI that the core is successfuly initialized
3909 android_core_init_completed();
3910#endif // CONFIG_ANDROID
3911
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003912 main_loop();
3913 quit_timers();
3914 net_cleanup();
3915 android_emulation_teardown();
3916 return 0;
3917}
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07003918
3919void
3920android_emulation_teardown(void)
3921{
3922 android_charmap_done();
3923}