blob: 9cba6803a37c337afe390fe853578bedbb51f702 [file] [log] [blame]
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001/* Copyright (C) 2006-2008 The Android Open Source Project
2**
3** This software is licensed under the terms of the GNU General Public
4** License version 2, as published by the Free Software Foundation, and
5** may be copied, distributed, and modified under those terms.
6**
7** This program is distributed in the hope that it will be useful,
8** but WITHOUT ANY WARRANTY; without even the implied warranty of
9** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10** GNU General Public License for more details.
11*/
12
13#include <signal.h>
14#include <unistd.h>
15#include <string.h>
16#include <sys/time.h>
17#ifdef _WIN32
18#include <process.h>
19#endif
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -070020
David 'Digit' Turner9b3a4b02014-01-23 00:52:54 +010021#include "config.h"
David 'Digit' Turnercc330d42013-12-14 23:26:42 +010022#include "android/sockets.h"
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080023
24#include "android/android.h"
25#include "qemu-common.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +010026#include "sysemu/sysemu.h"
David 'Digit' Turner1c31e3e2013-12-14 20:07:17 +010027#include "ui/console.h"
David 'Digit' Turnerd4e803c2013-12-14 23:45:50 +010028#include "android/user-events.h"
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080029
30#include <SDL.h>
31#include <SDL_syswm.h>
32
33#include "math.h"
34
35#include "android/charmap.h"
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080036#include "android/utils/debug.h"
David 'Digit' Turner73dd5fc2014-02-04 12:50:55 +010037#include "android/config-file.h"
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080038#include "android/config/config.h"
David 'Digit' Turner045bdbe2014-04-28 22:26:52 +020039#include "android/cpu_accelerator.h"
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080040
David 'Digit' Turnerc6e0cae2014-03-07 23:08:30 +010041#include "android/kernel/kernel_utils.h"
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080042#include "android/user-config.h"
43#include "android/utils/bufprint.h"
David 'Digit' Turner26d41532011-03-01 15:03:07 +010044#include "android/utils/filelock.h"
David 'Digit' Turner0a879bf2011-05-12 18:45:18 +020045#include "android/utils/lineinput.h"
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080046#include "android/utils/path.h"
David 'Digit' Turner5ea91482014-02-13 18:02:56 +010047#include "android/utils/property_file.h"
David 'Digit' Turner622f1532011-02-01 17:48:37 +010048#include "android/utils/tempfile.h"
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080049
David 'Digit' Turnerf8456272011-02-02 12:34:14 +010050#include "android/main-common.h"
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080051#include "android/help.h"
David 'Digit' Turnerf0665422013-12-17 10:47:09 +010052#include "hw/android/goldfish/nand.h"
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080053
54#include "android/globals.h"
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080055
Xavier Ducrohetfc8ed802011-02-09 18:04:23 -080056#include "android/qemulator.h"
David 'Digit' Turner055ae422010-07-27 11:34:16 -070057#include "android/display.h"
Vladimir Chtchetkine01193622010-05-11 13:07:22 -070058
Ot ten Thijeae835ac2010-10-18 13:37:37 +010059#include "android/snapshot.h"
60
David 'Digit' Turnere3fdd072011-02-02 14:43:23 +010061#include "android/framebuffer.h"
David 'Digit' Turnerd413fa52013-12-14 23:35:20 +010062#include "android/iolooper.h"
David 'Digit' Turner055ae422010-07-27 11:34:16 -070063
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080064AndroidRotation android_framebuffer_rotation;
65
66#define STRINGIFY(x) _STRINGIFY(x)
67#define _STRINGIFY(x) #x
68
David 'Digit' Turnera383d022009-12-03 13:50:00 -080069#ifdef ANDROID_SDK_TOOLS_REVISION
70# define VERSION_STRING STRINGIFY(ANDROID_SDK_TOOLS_REVISION)".0"
71#else
72# define VERSION_STRING "standalone"
73#endif
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080074
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080075#define D(...) do { if (VERBOSE_CHECK(init)) dprint(__VA_ARGS__); } while (0)
76
77extern int control_console_start( int port ); /* in control.c */
78
79extern int qemu_milli_needed;
80
David 'Digit' Turnerb72ae032014-06-18 19:23:20 +020081extern bool android_op_wipe_data;
82
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080083/* the default device DPI if none is specified by the skin
84 */
85#define DEFAULT_DEVICE_DPI 165
86
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080087int qemu_main(int argc, char **argv);
88
89/* this function dumps the QEMU help */
90extern void help( void );
91extern void emulator_help( void );
92
93#define VERBOSE_OPT(str,var) { str, &var }
94
95#define _VERBOSE_TAG(x,y) { #x, VERBOSE_##x, y },
96static const struct { const char* name; int flag; const char* text; }
97verbose_options[] = {
98 VERBOSE_TAG_LIST
99 { 0, 0, 0 }
100};
101
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800102void emulator_help( void )
103{
104 STRALLOC_DEFINE(out);
105 android_help_main(out);
106 printf( "%.*s", out->n, out->s );
107 stralloc_reset(out);
108 exit(1);
109}
110
David 'Digit' Turner40841b22011-03-01 14:04:00 +0100111/* TODO: Put in shared source file */
112static char*
113_getFullFilePath( const char* rootPath, const char* fileName )
114{
115 if (path_is_absolute(fileName)) {
116 return ASTRDUP(fileName);
117 } else {
118 char temp[PATH_MAX], *p=temp, *end=p+sizeof(temp);
119
120 p = bufprint(temp, end, "%s/%s", rootPath, fileName);
121 if (p >= end) {
122 return NULL;
123 }
124 return ASTRDUP(temp);
125 }
126}
127
128static uint64_t
129_adjustPartitionSize( const char* description,
130 uint64_t imageBytes,
131 uint64_t defaultBytes,
132 int inAndroidBuild )
133{
134 char temp[64];
135 unsigned imageMB;
136 unsigned defaultMB;
137
138 if (imageBytes <= defaultBytes)
139 return defaultBytes;
140
141 imageMB = convertBytesToMB(imageBytes);
142 defaultMB = convertBytesToMB(defaultBytes);
143
144 if (imageMB > defaultMB) {
145 snprintf(temp, sizeof temp, "(%d MB > %d MB)", imageMB, defaultMB);
146 } else {
Andrew Hsiehc7389bd2012-03-13 02:13:40 -0700147 snprintf(temp, sizeof temp, "(%" PRIu64 " bytes > %" PRIu64 " bytes)", imageBytes, defaultBytes);
David 'Digit' Turner40841b22011-03-01 14:04:00 +0100148 }
149
150 if (inAndroidBuild) {
151 dwarning("%s partition size adjusted to match image file %s\n", description, temp);
152 }
153
154 return convertMBToBytes(imageMB);
155}
156
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800157int main(int argc, char **argv)
158{
159 char tmp[MAX_PATH];
160 char* tmpend = tmp + sizeof(tmp);
161 char* args[128];
162 int n;
163 char* opt;
David 'Digit' Turner5e736932011-03-18 00:02:14 +0100164 /* The emulator always uses the first serial port for kernel messages
165 * and the second one for qemud. So start at the third if we need one
166 * for logcat or 'shell'
167 */
168 int serial = 2;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800169 int shell_serial = 0;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800170
David 'Digit' Turner2ed457e2011-06-15 17:29:50 +0200171 int forceArmv7 = 0;
172
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800173 AndroidHwConfig* hw;
David 'Digit' Turnercd059b12009-08-28 19:36:27 +0200174 AvdInfo* avd;
David 'Digit' Turner74d7ace2011-02-02 13:21:03 +0100175 AConfig* skinConfig;
176 char* skinPath;
Vladimir Chtchetkine83ffd662011-02-11 12:40:59 -0800177 int inAndroidBuild;
Vladimir Chtchetkine88078b82012-04-19 12:24:38 -0700178 uint64_t defaultPartitionSize = convertMBToBytes(200);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800179
180 AndroidOptions opts[1];
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700181 /* net.shared_net_ip boot property value. */
182 char boot_prop_ip[64];
183 boot_prop_ip[0] = '\0';
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800184
185 args[0] = argv[0];
186
187 if ( android_parse_options( &argc, &argv, opts ) < 0 ) {
188 exit(1);
189 }
190
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +0100191#ifdef _WIN32
192 socket_init();
193#endif
194
195 handle_ui_options(opts);
196
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800197 while (argc-- > 1) {
198 opt = (++argv)[0];
199
200 if(!strcmp(opt, "-qemu")) {
201 argc--;
202 argv++;
203 break;
204 }
205
206 if (!strcmp(opt, "-help")) {
207 emulator_help();
208 }
209
210 if (!strncmp(opt, "-help-",6)) {
211 STRALLOC_DEFINE(out);
212 opt += 6;
213
214 if (!strcmp(opt, "all")) {
215 android_help_all(out);
216 }
217 else if (android_help_for_option(opt, out) == 0) {
218 /* ok */
219 }
220 else if (android_help_for_topic(opt, out) == 0) {
221 /* ok */
222 }
223 if (out->n > 0) {
224 printf("\n%.*s", out->n, out->s);
225 exit(0);
226 }
227
228 fprintf(stderr, "unknown option: -help-%s\n", opt);
229 fprintf(stderr, "please use -help for a list of valid topics\n");
230 exit(1);
231 }
232
233 if (opt[0] == '-') {
234 fprintf(stderr, "unknown option: %s\n", opt);
235 fprintf(stderr, "please use -help for a list of valid options\n");
236 exit(1);
237 }
238
239 fprintf(stderr, "invalid command-line parameter: %s.\n", opt);
240 fprintf(stderr, "Hint: use '@foo' to launch a virtual device named 'foo'.\n");
241 fprintf(stderr, "please use -help for more information\n");
242 exit(1);
243 }
244
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800245 if (opts->version) {
246 printf("Android emulator version %s\n"
David 'Digit' Turner5f64b872011-02-28 23:23:05 +0100247 "Copyright (C) 2006-2011 The Android Open Source Project and many others.\n"
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800248 "This program is a derivative of the QEMU CPU emulator (www.qemu.org).\n\n",
249#if defined ANDROID_BUILD_ID
250 VERSION_STRING " (build_id " STRINGIFY(ANDROID_BUILD_ID) ")" );
251#else
252 VERSION_STRING);
253#endif
254 printf(" This software is licensed under the terms of the GNU General Public\n"
255 " License version 2, as published by the Free Software Foundation, and\n"
256 " may be copied, distributed, and modified under those terms.\n\n"
257 " This program is distributed in the hope that it will be useful,\n"
258 " but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
259 " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
260 " GNU General Public License for more details.\n\n");
261
262 exit(0);
263 }
264
David 'Digit' Turner5f64b872011-02-28 23:23:05 +0100265 if (opts->snapshot_list) {
David 'Digit' Turnerd80a7862011-05-05 10:24:43 +0200266 if (opts->snapstorage == NULL) {
267 /* Need to find the default snapstorage */
268 avd = createAVD(opts, &inAndroidBuild);
269 opts->snapstorage = avdInfo_getSnapStoragePath(avd);
270 if (opts->snapstorage != NULL) {
271 D("autoconfig: -snapstorage %s", opts->snapstorage);
272 } else {
273 if (inAndroidBuild) {
274 derror("You must use the -snapstorage <file> option to specify a snapshot storage file!\n");
275 } else {
276 derror("This AVD doesn't have snapshotting enabled!\n");
277 }
278 exit(1);
279 }
280 }
David 'Digit' Turner5f64b872011-02-28 23:23:05 +0100281 snapshot_print_and_exit(opts->snapstorage);
282 }
283
David 'Digit' Turner25eb6552011-02-25 15:07:11 +0100284 sanitizeOptions(opts);
285
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +0100286 /* Initialization of UI started with -attach-core should work differently
287 * than initialization of UI that starts the core. In particular....
288 */
289
290 /* -charmap is incompatible with -attach-core, because particular
291 * charmap gets set up in the running core. */
David 'Digit' Turner74d7ace2011-02-02 13:21:03 +0100292 if (android_charmap_setup(opts->charmap)) {
293 exit(1);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800294 }
295
Vladimir Chtchetkine83ffd662011-02-11 12:40:59 -0800296 /* Parses options and builds an appropriate AVD. */
David 'Digit' Turner462564f2011-02-23 13:32:37 +0100297 avd = android_avdInfo = createAVD(opts, &inAndroidBuild);
David 'Digit' Turnercd059b12009-08-28 19:36:27 +0200298
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800299 /* get the skin from the virtual device configuration */
David 'Digit' Turner25eb6552011-02-25 15:07:11 +0100300 if (opts->skindir != NULL) {
301 if (opts->skin == NULL) {
302 /* NOTE: Normally handled by sanitizeOptions(), just be safe */
303 derror("The -skindir <path> option requires a -skin <name> option");
304 exit(2);
305 }
306 } else {
307 char* skinName;
308 char* skinDir;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800309
David 'Digit' Turner25eb6552011-02-25 15:07:11 +0100310 avdInfo_getSkinInfo(avd, &skinName, &skinDir);
311
312 if (opts->skin == NULL) {
313 opts->skin = skinName;
314 D("autoconfig: -skin %s", opts->skin);
315 } else {
316 AFREE(skinName);
317 }
318
319 opts->skindir = skinDir;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800320 D("autoconfig: -skindir %s", opts->skindir);
Xavier Ducrohet689d1142012-04-19 12:54:08 -0700321
322 /* update the avd hw config from this new skin */
323 avdInfo_getSkinHardwareIni(avd, opts->skin, opts->skindir);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800324 }
325
Siva Velusamyd8999182012-10-08 18:02:30 -0700326 if (opts->dynamic_skin == 0) {
327 opts->dynamic_skin = avdInfo_shouldUseDynamicSkin(avd);
328 }
329
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800330 /* Read hardware configuration */
331 hw = android_hw;
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +0100332 if (avdInfo_initHwConfig(avd, hw) < 0) {
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800333 derror("could not read hardware configuration ?");
334 exit(1);
335 }
336
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800337 if (opts->keyset) {
338 parse_keyset(opts->keyset, opts);
339 if (!android_keyset) {
340 fprintf(stderr,
341 "emulator: WARNING: could not find keyset file named '%s',"
342 " using defaults instead\n",
343 opts->keyset);
344 }
345 }
346 if (!android_keyset) {
347 parse_keyset("default", opts);
348 if (!android_keyset) {
349 android_keyset = skin_keyset_new_from_text( skin_keyset_get_default() );
350 if (!android_keyset) {
351 fprintf(stderr, "PANIC: default keyset file is corrupted !!\n" );
352 fprintf(stderr, "PANIC: please update the code in android/skin/keyset.c\n" );
353 exit(1);
354 }
355 if (!opts->keyset)
356 write_default_keyset();
357 }
358 }
359
Dries Harnie40beab42010-05-15 17:04:47 +0200360 if (opts->shared_net_id) {
361 char* end;
362 long shared_net_id = strtol(opts->shared_net_id, &end, 0);
363 if (end == NULL || *end || shared_net_id < 1 || shared_net_id > 255) {
364 fprintf(stderr, "option -shared-net-id must be an integer between 1 and 255\n");
365 exit(1);
366 }
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700367 snprintf(boot_prop_ip, sizeof(boot_prop_ip),
368 "net.shared_net_ip=10.1.2.%ld", shared_net_id);
Dries Harnie40beab42010-05-15 17:04:47 +0200369 }
370
371
David 'Digit' Turner755811e2011-02-07 13:38:25 +0100372 user_config_init();
David 'Digit' Turner2507cab2011-02-10 16:29:17 +0100373 parse_skin_files(opts->skindir, opts->skin, opts, hw,
David 'Digit' Turner74d7ace2011-02-02 13:21:03 +0100374 &skinConfig, &skinPath);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800375
David 'Digit' Turner092361e2011-03-01 13:14:18 +0100376 if (!opts->netspeed && skin_network_speed) {
377 D("skin network speed: '%s'", skin_network_speed);
378 if (strcmp(skin_network_speed, NETWORK_SPEED_DEFAULT) != 0) {
379 opts->netspeed = (char*)skin_network_speed;
380 }
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800381 }
David 'Digit' Turner092361e2011-03-01 13:14:18 +0100382 if (!opts->netdelay && skin_network_delay) {
383 D("skin network delay: '%s'", skin_network_delay);
384 if (strcmp(skin_network_delay, NETWORK_DELAY_DEFAULT) != 0) {
385 opts->netdelay = (char*)skin_network_delay;
386 }
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800387 }
388
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800389 if (opts->trace) {
David 'Digit' Turnercd059b12009-08-28 19:36:27 +0200390 char* tracePath = avdInfo_getTracePath(avd, opts->trace);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800391 int ret;
392
393 if (tracePath == NULL) {
394 derror( "bad -trace parameter" );
395 exit(1);
396 }
397 ret = path_mkdir_if_needed( tracePath, 0755 );
398 if (ret < 0) {
399 fprintf(stderr, "could not create directory '%s'\n", tmp);
400 exit(2);
401 }
402 opts->trace = tracePath;
403 }
404
Vladimir Chtchetkine33f89d02011-09-28 09:19:09 -0700405 /* Update CPU architecture for HW configs created from build dir. */
406 if (inAndroidBuild) {
407#if defined(TARGET_ARM)
408 free(android_hw->hw_cpu_arch);
409 android_hw->hw_cpu_arch = ASTRDUP("arm");
410#elif defined(TARGET_I386)
411 free(android_hw->hw_cpu_arch);
412 android_hw->hw_cpu_arch = ASTRDUP("x86");
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -0700413#elif defined(TARGET_MIPS)
414 free(android_hw->hw_cpu_arch);
415 android_hw->hw_cpu_arch = ASTRDUP("mips");
Vladimir Chtchetkine33f89d02011-09-28 09:19:09 -0700416#endif
417 }
418
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800419 n = 1;
420 /* generate arguments for the underlying qemu main() */
David 'Digit' Turner238b4b02009-09-20 13:10:19 -0700421 {
David 'Digit' Turner0b019492011-03-01 14:02:42 +0100422 char* kernelFile = opts->kernel;
423 int kernelFileLen;
David 'Digit' Turner238b4b02009-09-20 13:10:19 -0700424
David 'Digit' Turner0b019492011-03-01 14:02:42 +0100425 if (kernelFile == NULL) {
426 kernelFile = avdInfo_getKernelPath(avd);
427 if (kernelFile == NULL) {
428 derror( "This AVD's configuration is missing a kernel file!!" );
429 exit(2);
430 }
431 D("autoconfig: -kernel %s", kernelFile);
432 }
433 if (!path_exists(kernelFile)) {
434 derror( "Invalid or missing kernel image file: %s", kernelFile );
435 exit(2);
436 }
437
438 hw->kernel_path = kernelFile;
David 'Digit' Turner238b4b02009-09-20 13:10:19 -0700439
440 /* If the kernel image name ends in "-armv7", then change the cpu
441 * type automatically. This is a poor man's approach to configuration
442 * management, but should allow us to get past building ARMv7
443 * system images with dex preopt pass without introducing too many
444 * changes to the emulator sources.
445 *
446 * XXX:
447 * A 'proper' change would require adding some sort of hardware-property
448 * to each AVD config file, then automatically determine its value for
449 * full Android builds (depending on some environment variable), plus
450 * some build system changes. I prefer not to do that for now for reasons
451 * of simplicity.
452 */
David 'Digit' Turner0b019492011-03-01 14:02:42 +0100453 kernelFileLen = strlen(kernelFile);
David 'Digit' Turner238b4b02009-09-20 13:10:19 -0700454 if (kernelFileLen > 6 && !memcmp(kernelFile + kernelFileLen - 6, "-armv7", 6)) {
David 'Digit' Turner2ed457e2011-06-15 17:29:50 +0200455 forceArmv7 = 1;
David 'Digit' Turner238b4b02009-09-20 13:10:19 -0700456 }
457 }
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800458
David 'Digit' Turnercc5804c2014-06-12 19:26:27 +0200459 KernelType kernelType = KERNEL_TYPE_LEGACY;
460 if (!android_pathProbeKernelType(hw->kernel_path, &kernelType)) {
461 D("WARNING: Could not determine kernel device naming scheme. Assuming legacy\n"
462 "If this AVD doesn't boot, and uses a recent kernel (3.10 or above) try setting\n"
463 "'kernel.newDeviceNaming' to 'yes' in its configuration.\n");
464 }
465
David 'Digit' Turnerc6e0cae2014-03-07 23:08:30 +0100466 // Auto-detect kernel device naming scheme if needed.
467 if (androidHwConfig_getKernelDeviceNaming(hw) < 0) {
David 'Digit' Turnerc6e0cae2014-03-07 23:08:30 +0100468 const char* newDeviceNaming = "no";
David 'Digit' Turnercc5804c2014-06-12 19:26:27 +0200469 if (kernelType == KERNEL_TYPE_3_10_OR_ABOVE) {
David 'Digit' Turnerc6e0cae2014-03-07 23:08:30 +0100470 D("Auto-detect: Kernel image requires new device naming scheme.");
471 newDeviceNaming = "yes";
472 } else {
473 D("Auto-detect: Kernel image requires legacy device naming scheme.");
474 }
475 AFREE(hw->kernel_newDeviceNaming);
476 hw->kernel_newDeviceNaming = ASTRDUP(newDeviceNaming);
477 }
478
David 'Digit' Turnercc5804c2014-06-12 19:26:27 +0200479 // Auto-detect YAFFS2 partition support if needed.
480 if (androidHwConfig_getKernelYaffs2Support(hw) < 0) {
David 'Digit' Turner9cab7532014-06-19 02:41:55 +0200481 // Essentially, anything before API level 20 supports Yaffs2
David 'Digit' Turnercc5804c2014-06-12 19:26:27 +0200482 const char* newYaffs2Support = "no";
David 'Digit' Turner9cab7532014-06-19 02:41:55 +0200483 if (avdInfo_getApiLevel(avd) < 20) {
David 'Digit' Turnercc5804c2014-06-12 19:26:27 +0200484 newYaffs2Support = "yes";
David 'Digit' Turner9cab7532014-06-19 02:41:55 +0200485 D("Auto-detect: Kernel does support YAFFS2 partitions.");
486 } else {
487 D("Auto-detect: Kernel does not support YAFFS2 partitions.");
David 'Digit' Turnercc5804c2014-06-12 19:26:27 +0200488 }
489 AFREE(hw->kernel_supportsYaffs2);
490 hw->kernel_supportsYaffs2 = ASTRDUP(newYaffs2Support);
491 }
492
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700493 if (boot_prop_ip[0]) {
494 args[n++] = "-boot-property";
495 args[n++] = boot_prop_ip;
496 }
497
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -0700498 if (opts->tcpdump) {
499 args[n++] = "-tcpdump";
500 args[n++] = opts->tcpdump;
501 }
502
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -0700503#ifdef CONFIG_NAND_LIMITS
504 if (opts->nand_limits) {
505 args[n++] = "-nand-limits";
506 args[n++] = opts->nand_limits;
507 }
508#endif
509
David 'Digit' Turner74d7ace2011-02-02 13:21:03 +0100510 if (opts->timezone) {
511 args[n++] = "-timezone";
512 args[n++] = opts->timezone;
513 }
514
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -0700515 if (opts->netspeed) {
516 args[n++] = "-netspeed";
517 args[n++] = opts->netspeed;
518 }
519 if (opts->netdelay) {
520 args[n++] = "-netdelay";
521 args[n++] = opts->netdelay;
522 }
523 if (opts->netfast) {
524 args[n++] = "-netfast";
525 }
526
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700527 if (opts->audio) {
528 args[n++] = "-audio";
529 args[n++] = opts->audio;
530 }
531
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700532 if (opts->cpu_delay) {
533 args[n++] = "-cpu-delay";
534 args[n++] = opts->cpu_delay;
535 }
536
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -0700537 if (opts->dns_server) {
538 args[n++] = "-dns-server";
539 args[n++] = opts->dns_server;
540 }
541
David 'Digit' Turner9ff69722011-09-13 12:32:52 +0200542 /* opts->ramdisk is never NULL (see createAVD) here */
543 if (opts->ramdisk) {
544 AFREE(hw->disk_ramdisk_path);
545 hw->disk_ramdisk_path = ASTRDUP(opts->ramdisk);
546 }
547 else if (!hw->disk_ramdisk_path[0]) {
548 hw->disk_ramdisk_path = avdInfo_getRamdiskPath(avd);
549 D("autoconfig: -ramdisk %s", hw->disk_ramdisk_path);
550 }
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800551
David 'Digit' Turner40841b22011-03-01 14:04:00 +0100552 /* -partition-size is used to specify the max size of both the system
553 * and data partition sizes.
554 */
555 if (opts->partition_size) {
556 char* end;
557 long sizeMB = strtol(opts->partition_size, &end, 0);
558 long minSizeMB = 10;
559 long maxSizeMB = LONG_MAX / ONE_MB;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800560
David 'Digit' Turner40841b22011-03-01 14:04:00 +0100561 if (sizeMB < 0 || *end != 0) {
562 derror( "-partition-size must be followed by a positive integer" );
563 exit(1);
564 }
565 if (sizeMB < minSizeMB || sizeMB > maxSizeMB) {
566 derror( "partition-size (%d) must be between %dMB and %dMB",
567 sizeMB, minSizeMB, maxSizeMB );
568 exit(1);
569 }
570 defaultPartitionSize = (uint64_t) sizeMB * ONE_MB;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800571 }
572
David 'Digit' Turner40841b22011-03-01 14:04:00 +0100573
574 /** SYSTEM PARTITION **/
575
576 if (opts->sysdir == NULL) {
577 if (avdInfo_inAndroidBuild(avd)) {
578 opts->sysdir = ASTRDUP(avdInfo_getContentPath(avd));
579 D("autoconfig: -sysdir %s", opts->sysdir);
580 }
581 }
582
583 if (opts->sysdir != NULL) {
584 if (!path_exists(opts->sysdir)) {
585 derror("Directory does not exist: %s", opts->sysdir);
586 exit(1);
587 }
588 }
589
590 {
591 char* rwImage = NULL;
592 char* initImage = NULL;
593
594 do {
595 if (opts->system == NULL) {
596 /* If -system is not used, try to find a runtime system image
597 * (i.e. system-qemu.img) in the content directory.
598 */
599 rwImage = avdInfo_getSystemImagePath(avd);
600 if (rwImage != NULL) {
601 break;
602 }
603 /* Otherwise, try to find the initial system image */
604 initImage = avdInfo_getSystemInitImagePath(avd);
605 if (initImage == NULL) {
606 derror("No initial system image for this configuration!");
607 exit(1);
608 }
609 break;
610 }
611
612 /* If -system <name> is used, use it to find the initial image */
David 'Digit' Turnerea283772011-07-07 04:40:27 +0200613 if (opts->sysdir != NULL && !path_exists(opts->system)) {
David 'Digit' Turner40841b22011-03-01 14:04:00 +0100614 initImage = _getFullFilePath(opts->sysdir, opts->system);
615 } else {
616 initImage = ASTRDUP(opts->system);
617 }
618 if (!path_exists(initImage)) {
619 derror("System image file doesn't exist: %s", initImage);
620 exit(1);
621 }
622
623 } while (0);
624
625 if (rwImage != NULL) {
626 /* Use the read/write image file directly */
627 hw->disk_systemPartition_path = rwImage;
628 hw->disk_systemPartition_initPath = NULL;
629 D("Using direct system image: %s", rwImage);
630 } else if (initImage != NULL) {
631 hw->disk_systemPartition_path = NULL;
632 hw->disk_systemPartition_initPath = initImage;
633 D("Using initial system image: %s", initImage);
634 }
635
636 /* Check the size of the system partition image.
637 * If we have an AVD, it must be smaller than
638 * the disk.systemPartition.size hardware property.
639 *
640 * Otherwise, we need to adjust the systemPartitionSize
641 * automatically, and print a warning.
642 *
643 */
644 const char* systemImage = hw->disk_systemPartition_path;
645 uint64_t systemBytes;
646
647 if (systemImage == NULL)
648 systemImage = hw->disk_systemPartition_initPath;
649
650 if (path_get_size(systemImage, &systemBytes) < 0) {
651 derror("Missing system image: %s", systemImage);
652 exit(1);
653 }
654
655 hw->disk_systemPartition_size =
656 _adjustPartitionSize("system", systemBytes, defaultPartitionSize,
657 avdInfo_inAndroidBuild(avd));
658 }
659
660 /** DATA PARTITION **/
661
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +0100662 if (opts->datadir) {
663 if (!path_exists(opts->datadir)) {
664 derror("Invalid -datadir directory: %s", opts->datadir);
665 }
666 }
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800667
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +0100668 {
669 char* dataImage = NULL;
670 char* initImage = NULL;
671
672 do {
673 if (!opts->data) {
674 dataImage = avdInfo_getDataImagePath(avd);
675 if (dataImage != NULL) {
676 D("autoconfig: -data %s", dataImage);
677 break;
678 }
679 dataImage = avdInfo_getDefaultDataImagePath(avd);
680 if (dataImage == NULL) {
681 derror("No data image path for this configuration!");
682 exit (1);
683 }
684 opts->wipe_data = 1;
685 break;
686 }
687
688 if (opts->datadir) {
689 dataImage = _getFullFilePath(opts->datadir, opts->data);
690 } else {
691 dataImage = ASTRDUP(opts->data);
692 }
693 } while (0);
694
695 if (opts->initdata != NULL) {
696 initImage = ASTRDUP(opts->initdata);
697 if (!path_exists(initImage)) {
698 derror("Invalid initial data image path: %s", initImage);
699 exit(1);
700 }
701 } else {
702 initImage = avdInfo_getDataInitImagePath(avd);
703 D("autoconfig: -initdata %s", initImage);
704 }
705
706 hw->disk_dataPartition_path = dataImage;
707 if (opts->wipe_data) {
708 hw->disk_dataPartition_initPath = initImage;
709 } else {
710 hw->disk_dataPartition_initPath = NULL;
711 }
David 'Digit' Turnerb72ae032014-06-18 19:23:20 +0200712 android_op_wipe_data = opts->wipe_data;
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +0100713
Maciek Molerusf7584112011-06-15 22:26:35 +0200714 uint64_t defaultBytes =
715 hw->disk_dataPartition_size == 0 ?
716 defaultPartitionSize :
Vladimir Chtchetkined4f5a3a2012-03-08 14:20:20 -0800717 hw->disk_dataPartition_size;
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +0100718 uint64_t dataBytes;
719 const char* dataPath = hw->disk_dataPartition_initPath;
720
721 if (dataPath == NULL)
722 dataPath = hw->disk_dataPartition_path;
723
724 path_get_size(dataPath, &dataBytes);
725
726 hw->disk_dataPartition_size =
727 _adjustPartitionSize("data", dataBytes, defaultBytes,
728 avdInfo_inAndroidBuild(avd));
729 }
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800730
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +0100731 /** CACHE PARTITION **/
732
733 if (opts->no_cache) {
734 /* No cache partition at all */
735 hw->disk_cachePartition = 0;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800736 }
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +0100737 else if (!hw->disk_cachePartition) {
738 if (opts->cache) {
739 dwarning( "Emulated hardware doesn't support a cache partition. -cache option ignored!" );
740 opts->cache = NULL;
741 }
742 }
743 else
744 {
745 if (!opts->cache) {
746 /* Find the current cache partition file */
747 opts->cache = avdInfo_getCachePath(avd);
748 if (opts->cache == NULL) {
749 /* The file does not exists, we will force its creation
750 * if we are not in the Android build system. Otherwise,
751 * a temporary file will be used.
752 */
753 if (!avdInfo_inAndroidBuild(avd)) {
754 opts->cache = avdInfo_getDefaultCachePath(avd);
755 }
756 }
David 'Digit' Turner48a3c662011-03-01 14:03:20 +0100757 if (opts->cache) {
758 D("autoconfig: -cache %s", opts->cache);
759 }
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +0100760 }
761
762 if (opts->cache) {
763 hw->disk_cachePartition_path = ASTRDUP(opts->cache);
764 }
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800765 }
766
Vladimir Chtchetkine6f50aa32012-04-26 08:20:18 -0700767 if (hw->disk_cachePartition_path && opts->cache_size) {
768 /* Set cache partition size per user options. */
769 char* end;
770 long sizeMB = strtol(opts->cache_size, &end, 0);
771
772 if (sizeMB < 0 || *end != 0) {
773 derror( "-cache-size must be followed by a positive integer" );
774 exit(1);
775 }
776 hw->disk_cachePartition_size = (uint64_t) sizeMB * ONE_MB;
777 }
778
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +0100779 /** SD CARD PARTITION */
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800780
David 'Digit' Turner48a3c662011-03-01 14:03:20 +0100781 if (!hw->hw_sdCard) {
782 /* No SD Card emulation, so -sdcard will be ignored */
783 if (opts->sdcard) {
784 dwarning( "Emulated hardware doesn't support SD Cards. -sdcard option ignored." );
785 opts->sdcard = NULL;
786 }
787 } else {
788 /* Auto-configure -sdcard if it is not available */
789 if (!opts->sdcard) {
790 do {
791 /* If -datadir <path> is used, look for a sdcard.img file here */
792 if (opts->datadir) {
793 bufprint(tmp, tmpend, "%s/%s", opts->datadir, "system.img");
794 if (path_exists(tmp)) {
795 opts->sdcard = strdup(tmp);
796 break;
797 }
798 }
799
800 /* Otherwise, look at the AVD's content */
801 opts->sdcard = avdInfo_getSdCardPath(avd);
802 if (opts->sdcard != NULL) {
803 break;
804 }
805
806 /* Nothing */
807 } while (0);
808
809 if (opts->sdcard) {
810 D("autoconfig: -sdcard %s", opts->sdcard);
811 }
812 }
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800813 }
814
815 if(opts->sdcard) {
816 uint64_t size;
817 if (path_get_size(opts->sdcard, &size) == 0) {
818 /* see if we have an sdcard image. get its size if it exists */
David 'Digit' Turner8b657e52009-12-01 13:38:21 -0800819 /* due to what looks like limitations of the MMC protocol, one has
820 * to use an SD Card image that is equal or larger than 9 MB
821 */
822 if (size < 9*1024*1024ULL) {
823 fprintf(stderr, "### WARNING: SD Card files must be at least 9MB, ignoring '%s'\n", opts->sdcard);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800824 } else {
David 'Digit' Turner48a3c662011-03-01 14:03:20 +0100825 hw->hw_sdCard_path = ASTRDUP(opts->sdcard);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800826 }
827 } else {
David 'Digit' Turner48a3c662011-03-01 14:03:20 +0100828 dwarning("no SD Card image at '%s'", opts->sdcard);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800829 }
830 }
831
David 'Digit' Turner48a3c662011-03-01 14:03:20 +0100832
833 /** SNAPSHOT STORAGE HANDLING */
834
David 'Digit' Turner5f64b872011-02-28 23:23:05 +0100835 /* Determine snapstorage path. -no-snapstorage disables all snapshotting
836 * support. This means you can't resume a snapshot at load, save it at
837 * exit, or even load/save them dynamically at runtime with the console.
838 */
839 if (opts->no_snapstorage) {
840
841 if (opts->snapshot) {
842 dwarning("ignoring -snapshot option due to the use of -no-snapstorage");
843 opts->snapshot = NULL;
844 }
845
846 if (opts->snapstorage) {
847 dwarning("ignoring -snapstorage option due to the use of -no-snapstorage");
848 opts->snapstorage = NULL;
849 }
850 }
851 else
852 {
Vladimir Chtchetkine873c3cb2012-03-14 12:46:51 -0700853 if (!opts->snapstorage && avdInfo_getSnapshotPresent(avd)) {
David 'Digit' Turner5f64b872011-02-28 23:23:05 +0100854 opts->snapstorage = avdInfo_getSnapStoragePath(avd);
855 if (opts->snapstorage != NULL) {
856 D("autoconfig: -snapstorage %s", opts->snapstorage);
Ot ten Thije353b3b12010-10-05 17:53:30 +0100857 }
858 }
859
David 'Digit' Turner5f64b872011-02-28 23:23:05 +0100860 if (opts->snapstorage && !path_exists(opts->snapstorage)) {
861 D("no image at '%s', state snapshots disabled", opts->snapstorage);
862 opts->snapstorage = NULL;
Ot ten Thije353b3b12010-10-05 17:53:30 +0100863 }
David 'Digit' Turner5f64b872011-02-28 23:23:05 +0100864 }
865
866 /* If we have a valid snapshot storage path */
867
868 if (opts->snapstorage) {
869
870 hw->disk_snapStorage_path = ASTRDUP(opts->snapstorage);
871
872 /* -no-snapshot is equivalent to using both -no-snapshot-load
873 * and -no-snapshot-save. You can still load/save snapshots dynamically
874 * from the console though.
875 */
876 if (opts->no_snapshot) {
877
878 opts->no_snapshot_load = 1;
879 opts->no_snapshot_save = 1;
880
881 if (opts->snapshot) {
882 dwarning("ignoring -snapshot option due to the use of -no-snapshot.");
883 }
884 }
885
886 if (!opts->no_snapshot_load || !opts->no_snapshot_save) {
887 if (opts->snapshot == NULL) {
888 opts->snapshot = "default-boot";
889 D("autoconfig: -snapshot %s", opts->snapshot);
890 }
891 }
892
893 /* We still use QEMU command-line options for the following since
894 * they can change from one invokation to the next and don't really
895 * correspond to the hardware configuration itself.
896 */
897 if (!opts->no_snapshot_load) {
898 args[n++] = "-loadvm";
899 args[n++] = ASTRDUP(opts->snapshot);
900 }
901
902 if (!opts->no_snapshot_save) {
903 args[n++] = "-savevm-on-exit";
904 args[n++] = ASTRDUP(opts->snapshot);
905 }
906
Tim Baverstock622b8f42010-12-07 11:36:59 +0000907 if (opts->no_snapshot_update_time) {
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +0100908 args[n++] = "-snapshot-no-time-update";
Tim Baverstock622b8f42010-12-07 11:36:59 +0000909 }
Ot ten Thijeae835ac2010-10-18 13:37:37 +0100910 }
Ot ten Thije353b3b12010-10-05 17:53:30 +0100911
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800912 if (!opts->logcat || opts->logcat[0] == 0) {
913 opts->logcat = getenv("ANDROID_LOG_TAGS");
914 if (opts->logcat && opts->logcat[0] == 0)
915 opts->logcat = NULL;
916 }
917
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800918 /* we always send the kernel messages from ttyS0 to android_kmsg */
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +0100919 if (opts->show_kernel) {
920 args[n++] = "-show-kernel";
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800921 }
922
923 /* XXXX: TODO: implement -shell and -logcat through qemud instead */
924 if (!opts->shell_serial) {
925#ifdef _WIN32
926 opts->shell_serial = "con:";
927#else
928 opts->shell_serial = "stdio";
929#endif
930 }
931 else
932 opts->shell = 1;
933
934 if (opts->shell || opts->logcat) {
935 args[n++] = "-serial";
936 args[n++] = opts->shell_serial;
937 shell_serial = serial++;
938 }
939
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +0100940 if (opts->radio) {
941 args[n++] = "-radio";
942 args[n++] = opts->radio;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800943 }
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800944
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +0100945 if (opts->gps) {
946 args[n++] = "-gps";
947 args[n++] = opts->gps;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800948 }
949
Nick Kralevich185231a2013-10-07 13:54:31 -0700950 if (opts->selinux) {
951 if ((strcmp(opts->selinux, "permissive") != 0)
952 && (strcmp(opts->selinux, "disabled") != 0)) {
953 derror("-selinux must be \"disabled\" or \"permissive\"");
954 exit(1);
955 }
956 }
957
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800958 if (opts->memory) {
959 char* end;
960 long ramSize = strtol(opts->memory, &end, 0);
961 if (ramSize < 0 || *end != 0) {
962 derror( "-memory must be followed by a positive integer" );
963 exit(1);
964 }
965 if (ramSize < 32 || ramSize > 4096) {
966 derror( "physical memory size must be between 32 and 4096 MB" );
967 exit(1);
968 }
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +0100969 hw->hw_ramSize = ramSize;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800970 }
971 if (!opts->memory) {
David 'Digit' Turner3bbc9192011-01-19 22:18:02 +0100972 int ramSize = hw->hw_ramSize;
973 if (ramSize <= 0) {
974 /* Compute the default RAM size based on the size of screen.
975 * This is only used when the skin doesn't provide the ram
976 * size through its hardware.ini (i.e. legacy ones) or when
977 * in the full Android build system.
978 */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +0100979 int64_t pixels = hw->hw_lcd_width * hw->hw_lcd_height;
David 'Digit' Turner3bbc9192011-01-19 22:18:02 +0100980 /* The following thresholds are a bit liberal, but we
981 * essentially want to ensure the following mappings:
982 *
983 * 320x480 -> 96
984 * 800x600 -> 128
985 * 1024x768 -> 256
986 *
987 * These are just simple heuristics, they could change in
988 * the future.
989 */
990 if (pixels <= 250000)
991 ramSize = 96;
992 else if (pixels <= 500000)
993 ramSize = 128;
994 else
995 ramSize = 256;
996 }
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +0100997 hw->hw_ramSize = ramSize;
998 }
999
1000 D("Physical RAM size: %dMB\n", hw->hw_ramSize);
1001
1002 if (hw->vm_heapSize == 0) {
1003 /* Compute the default heap size based on the RAM size.
1004 * Essentially, we want to ensure the following liberal mappings:
1005 *
1006 * 96MB RAM -> 16MB heap
1007 * 128MB RAM -> 24MB heap
1008 * 256MB RAM -> 48MB heap
1009 */
1010 int ramSize = hw->hw_ramSize;
1011 int heapSize;
1012
1013 if (ramSize < 100)
1014 heapSize = 16;
1015 else if (ramSize < 192)
1016 heapSize = 24;
1017 else
1018 heapSize = 48;
1019
1020 hw->vm_heapSize = heapSize;
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001021 }
1022
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001023 if (opts->trace) {
1024 args[n++] = "-trace";
1025 args[n++] = opts->trace;
1026 args[n++] = "-tracing";
1027 args[n++] = "off";
1028 }
1029
David 'Digit' Turnerc0052462014-02-25 18:39:29 +01001030 /* Pass boot properties to the core. First, those from boot.prop,
David 'Digit' Turner5ea91482014-02-13 18:02:56 +01001031 * then those from the command-line */
1032 const FileData* bootProperties = avdInfo_getBootProperties(avd);
1033 if (!fileData_isEmpty(bootProperties)) {
1034 PropertyFileIterator iter[1];
1035 propertyFileIterator_init(iter,
1036 bootProperties->data,
1037 bootProperties->size);
1038 while (propertyFileIterator_next(iter)) {
1039 char temp[MAX_PROPERTY_NAME_LEN + MAX_PROPERTY_VALUE_LEN + 2];
1040 snprintf(temp, sizeof temp, "%s=%s", iter->name, iter->value);
1041 args[n++] = "-boot-property";
1042 args[n++] = ASTRDUP(temp);
1043 }
1044 }
David 'Digit' Turnerc0052462014-02-25 18:39:29 +01001045
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07001046 if (opts->prop != NULL) {
1047 ParamList* pl = opts->prop;
1048 for ( ; pl != NULL; pl = pl->next ) {
1049 args[n++] = "-boot-property";
1050 args[n++] = pl->param;
1051 }
1052 }
1053
David 'Digit' Turner318e4f22009-05-25 18:01:03 +02001054 /* Setup the kernel init options
1055 */
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001056 {
1057 static char params[1024];
1058 char *p = params, *end = p + sizeof(params);
1059
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01001060 /* Don't worry about having a leading space here, this is handled
1061 * by the core later. */
1062
Jun Nakajima334ab472011-02-02 23:49:59 -08001063#ifdef TARGET_I386
1064 p = bufprint(p, end, " androidboot.hardware=goldfish");
Jun Nakajimabac9add2011-02-08 22:10:52 -08001065 p = bufprint(p, end, " clocksource=pit");
Jun Nakajima334ab472011-02-02 23:49:59 -08001066#endif
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001067
1068 if (opts->shell || opts->logcat) {
David 'Digit' Turnerc6e0cae2014-03-07 23:08:30 +01001069 p = bufprint(p, end, " androidboot.console=%s%d",
1070 androidHwConfig_getKernelSerialPrefix(android_hw),
1071 shell_serial );
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001072 }
1073
1074 if (opts->trace) {
1075 p = bufprint(p, end, " android.tracing=1");
1076 }
1077
1078 if (!opts->no_jni) {
1079 p = bufprint(p, end, " android.checkjni=1");
1080 }
1081
1082 if (opts->no_boot_anim) {
1083 p = bufprint( p, end, " android.bootanim=0" );
1084 }
1085
1086 if (opts->logcat) {
1087 char* q = bufprint(p, end, " androidboot.logcat=%s", opts->logcat);
1088
1089 if (q < end) {
1090 /* replace any space by a comma ! */
1091 {
1092 int nn;
1093 for (nn = 1; p[nn] != 0; nn++)
1094 if (p[nn] == ' ' || p[nn] == '\t')
1095 p[nn] = ',';
1096 p += nn;
1097 }
1098 }
1099 p = q;
1100 }
1101
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001102 if (opts->bootchart) {
1103 p = bufprint(p, end, " androidboot.bootchart=%s", opts->bootchart);
1104 }
1105
Nick Kralevich185231a2013-10-07 13:54:31 -07001106 if (opts->selinux) {
1107 p = bufprint(p, end, " androidboot.selinux=%s", opts->selinux);
1108 }
1109
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001110 if (p >= end) {
1111 fprintf(stderr, "### ERROR: kernel parameters too long\n");
1112 exit(1);
1113 }
1114
David 'Digit' Turner0b019492011-03-01 14:02:42 +01001115 hw->kernel_parameters = strdup(params);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001116 }
1117
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07001118 if (opts->ports) {
1119 args[n++] = "-android-ports";
1120 args[n++] = opts->ports;
1121 }
1122
1123 if (opts->port) {
1124 args[n++] = "-android-port";
1125 args[n++] = opts->port;
1126 }
1127
1128 if (opts->report_console) {
1129 args[n++] = "-android-report-console";
1130 args[n++] = opts->report_console;
1131 }
1132
1133 if (opts->http_proxy) {
1134 args[n++] = "-http-proxy";
1135 args[n++] = opts->http_proxy;
1136 }
1137
Xavier Ducrohet72d56112011-08-11 18:47:27 -07001138 if (!opts->charmap) {
1139 /* Try to find a valid charmap name */
1140 char* charmap = avdInfo_getCharmapFile(avd, hw->hw_keyboard_charmap);
1141 if (charmap != NULL) {
1142 D("autoconfig: -charmap %s", charmap);
1143 opts->charmap = charmap;
1144 }
1145 }
1146
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07001147 if (opts->charmap) {
Xavier Ducrohet72d56112011-08-11 18:47:27 -07001148 char charmap_name[AKEYCHARMAP_NAME_SIZE];
1149
1150 if (!path_exists(opts->charmap)) {
1151 derror("Charmap file does not exist: %s", opts->charmap);
1152 exit(1);
1153 }
1154 /* We need to store the charmap name in the hardware configuration.
1155 * However, the charmap file itself is only used by the UI component
1156 * and doesn't need to be set to the emulation engine.
1157 */
1158 kcm_extract_charmap_name(opts->charmap, charmap_name,
1159 sizeof(charmap_name));
1160 AFREE(hw->hw_keyboard_charmap);
1161 hw->hw_keyboard_charmap = ASTRDUP(charmap_name);
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07001162 }
1163
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07001164 if (opts->memcheck) {
1165 args[n++] = "-android-memcheck";
1166 args[n++] = opts->memcheck;
1167 }
1168
David Turner9da935d2011-09-12 21:27:56 +02001169 if (opts->gpu) {
1170 const char* gpu = opts->gpu;
Vladimir Chtchetkineae0d8132011-09-13 10:48:02 -07001171 if (!strcmp(gpu,"on") || !strcmp(gpu,"enable")) {
David Turner9da935d2011-09-12 21:27:56 +02001172 hw->hw_gpu_enabled = 1;
Vladimir Chtchetkineae0d8132011-09-13 10:48:02 -07001173 } else if (!strcmp(gpu,"off") || !strcmp(gpu,"disable")) {
David Turner9da935d2011-09-12 21:27:56 +02001174 hw->hw_gpu_enabled = 0;
1175 } else if (!strcmp(gpu,"auto")) {
1176 /* Nothing to do */
1177 } else {
1178 derror("Invalid value for -gpu <mode> parameter: %s\n", gpu);
1179 derror("Valid values are: on, off or auto\n");
1180 exit(1);
1181 }
1182 }
1183
Vladimir Chtchetkine9242b332012-02-10 08:29:22 -08001184 /* Quit emulator on condition that both, gpu and snapstorage are on. This is
1185 * a temporary solution preventing the emulator from crashing until GPU state
1186 * can be properly saved / resored in snapshot file. */
1187 if (hw->hw_gpu_enabled && opts->snapstorage && (!opts->no_snapshot_load ||
1188 !opts->no_snapshot_save)) {
1189 derror("Snapshots and gpu are mutually exclusive at this point. Please turn one of them off, and restart the emulator.");
1190 exit(1);
1191 }
1192
Vladimir Chtchetkine7485c292012-03-19 11:35:29 -07001193 /* Deal with camera emulation */
1194 if (opts->webcam_list) {
1195 /* List connected webcameras */
1196 args[n++] = "-list-webcam";
1197 }
1198
1199 if (opts->camera_back) {
1200 /* Validate parameter. */
1201 if (memcmp(opts->camera_back, "webcam", 6) &&
1202 strcmp(opts->camera_back, "emulated") &&
1203 strcmp(opts->camera_back, "none")) {
1204 derror("Invalid value for -camera-back <mode> parameter: %s\n"
1205 "Valid values are: 'emulated', 'webcam<N>', or 'none'\n",
1206 opts->camera_back);
Vladimir Chtchetkineae0d8132011-09-13 10:48:02 -07001207 exit(1);
1208 }
Vladimir Chtchetkine7485c292012-03-19 11:35:29 -07001209 hw->hw_camera_back = ASTRDUP(opts->camera_back);
Vladimir Chtchetkineae0d8132011-09-13 10:48:02 -07001210 }
1211
Vladimir Chtchetkine7485c292012-03-19 11:35:29 -07001212 if (opts->camera_front) {
1213 /* Validate parameter. */
1214 if (memcmp(opts->camera_front, "webcam", 6) &&
1215 strcmp(opts->camera_front, "emulated") &&
1216 strcmp(opts->camera_front, "none")) {
1217 derror("Invalid value for -camera-front <mode> parameter: %s\n"
1218 "Valid values are: 'emulated', 'webcam<N>', or 'none'\n",
1219 opts->camera_front);
1220 exit(1);
Vladimir Chtchetkineb8dcaff2011-09-17 11:15:47 -07001221 }
Vladimir Chtchetkine7485c292012-03-19 11:35:29 -07001222 hw->hw_camera_front = ASTRDUP(opts->camera_front);
Vladimir Chtchetkinef8675c22012-01-06 10:31:41 -08001223 }
1224
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01001225 /* physical memory is now in hw->hw_ramSize */
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001226
David 'Digit' Turner53eb66d2011-03-01 14:58:23 +01001227 hw->avd_name = ASTRDUP(avdInfo_getName(avd));
David 'Digit' Turner6b8555c2011-02-17 04:28:19 +01001228
Dries Harnie40beab42010-05-15 17:04:47 +02001229 /* Set up the interfaces for inter-emulator networking */
1230 if (opts->shared_net_id) {
1231 unsigned int shared_net_id = atoi(opts->shared_net_id);
1232 char nic[37];
Dries Harnie40beab42010-05-15 17:04:47 +02001233
1234 args[n++] = "-net";
1235 args[n++] = "nic,vlan=0";
1236 args[n++] = "-net";
1237 args[n++] = "user,vlan=0";
Dries Harnie111d6f82010-06-09 21:42:18 +02001238
1239 args[n++] = "-net";
1240 snprintf(nic, sizeof nic, "nic,vlan=1,macaddr=52:54:00:12:34:%02x", shared_net_id);
1241 args[n++] = strdup(nic);
1242 args[n++] = "-net";
1243 args[n++] = "socket,vlan=1,mcast=230.0.0.10:1234";
Dries Harnie40beab42010-05-15 17:04:47 +02001244 }
1245
David 'Digit' Turner045bdbe2014-04-28 22:26:52 +02001246 /* Handle CPU acceleration options. */
1247 if (opts->no_accel) {
1248 if (opts->accel) {
1249 if (strcmp(opts->accel, "off") != 0) {
1250 derror("You cannot use -no-accel and '-accel %s' at the same time",
1251 opts->accel);
1252 exit(1);
1253 }
1254 } else {
1255 AFREE(opts->accel);
1256 opts->accel = ASTRDUP("off");
1257 }
1258 }
1259
1260 enum {
1261 ACCEL_OFF = 0,
1262 ACCEL_ON = 1,
1263 ACCEL_AUTO = 2,
1264 } accel_mode = ACCEL_AUTO;
1265
1266 if (opts->accel) {
1267 if (!strcmp(opts->accel, "off")) {
1268 accel_mode = ACCEL_OFF;
1269 } else if (!strcmp(opts->accel, "on")) {
1270 accel_mode = ACCEL_ON;
1271 } else if (!strcmp(opts->accel, "auto")) {
1272 accel_mode = ACCEL_AUTO;
1273 } else {
1274 derror("Invalid '-accel %s' parameter, valid values are: on off auto\n",
1275 opts->accel);
1276 exit(1);
1277 }
1278 }
1279
1280#if defined(TARGET_I386) || defined(TARGET_X86_64)
1281 char* accel_status = NULL;
1282 bool accel_ok = android_hasCpuAcceleration(&accel_status);
1283
1284#ifdef __linux__
1285 static const char kEnableAccelerator[] = "-enable-kvm";
1286 static const char kDisableAccelerator[] = "-disable-kvm";
1287#else
1288 static const char kEnableAccelerator[] = "-enable-hax";
1289 static const char kDisableAccelerator[] = "-disable-hax";
1290#endif
1291
1292 // Dump CPU acceleration status.
1293 if (VERBOSE_CHECK(init)) {
1294 const char* accel_str = "DISABLED";
1295 if (accel_ok) {
1296 if (accel_mode == ACCEL_OFF) {
1297 accel_str = "working, but disabled by user";
1298 } else {
1299 accel_str = "working";
1300 }
1301 }
1302 dprint("CPU Acceleration: %s", accel_str);
1303 dprint("CPU Acceleration status: %s", accel_status);
1304 }
1305
1306 // CPU acceleration only works for x86 and x86_64 system images.
1307 if (accel_mode == ACCEL_OFF && accel_ok) {
1308 args[n++] = ASTRDUP(kDisableAccelerator);
1309 } else if (accel_mode == ACCEL_ON) {
1310 if (!accel_ok) {
1311 derror("CPU acceleration not supported on this machine!");
1312 derror("Reason: %s", accel_status);
1313 exit(1);
1314 }
1315 args[n++] = ASTRDUP(kEnableAccelerator);
1316 } else {
1317 args[n++] = accel_ok ? ASTRDUP(kEnableAccelerator)
1318 : ASTRDUP(kDisableAccelerator);
1319 }
1320
1321 AFREE(accel_status);
1322#else
1323 (void)accel_mode;
1324
1325 if (VERBOSE_CHECK(init)) {
1326 dwarning("CPU acceleration only works with x86/x86_64 "
1327 "system images.");
1328 }
1329#endif
1330
Vladimir Chtchetkine8dd31e82012-02-15 17:16:04 -08001331 /* Setup screen emulation */
1332 if (opts->screen) {
Vladimir Chtchetkine863d1012012-03-16 12:25:23 -07001333 if (strcmp(opts->screen, "touch") &&
1334 strcmp(opts->screen, "multi-touch") &&
1335 strcmp(opts->screen, "no-touch")) {
1336
1337 derror("Invalid value for -screen <mode> parameter: %s\n"
1338 "Valid values are: touch, multi-touch, or no-touch\n",
1339 opts->screen);
Vladimir Chtchetkine8dd31e82012-02-15 17:16:04 -08001340 exit(1);
1341 }
Vladimir Chtchetkine863d1012012-03-16 12:25:23 -07001342 hw->hw_screen = ASTRDUP(opts->screen);
Vladimir Chtchetkine8dd31e82012-02-15 17:16:04 -08001343 }
1344
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001345 while(argc-- > 0) {
1346 args[n++] = *argv++;
1347 }
1348 args[n] = 0;
1349
David 'Digit' Turner2ed457e2011-06-15 17:29:50 +02001350 /* If the target ABI is armeabi-v7a, we can auto-detect the cpu model
1351 * as a cortex-a8, instead of the default (arm926) which only emulates
1352 * an ARMv5TE CPU.
1353 */
1354 if (!forceArmv7 && hw->hw_cpu_model[0] == '\0')
1355 {
1356 char* abi = avdInfo_getTargetAbi(avd);
1357 if (abi != NULL) {
1358 if (!strcmp(abi, "armeabi-v7a")) {
1359 forceArmv7 = 1;
1360 }
1361 AFREE(abi);
1362 }
1363 }
1364
1365 if (forceArmv7 != 0) {
1366 AFREE(hw->hw_cpu_model);
1367 hw->hw_cpu_model = ASTRDUP("cortex-a8");
1368 D("Auto-config: -qemu -cpu %s", hw->hw_cpu_model);
1369 }
1370
David 'Digit' Turner7666d5f2014-04-01 15:29:14 +02001371 /* If the target architecture is 'x86', ensure that the 'qemu32'
1372 * CPU model is used. Otherwise, the default (which is now 'qemu64')
1373 * will result in a failure to boot with some kernels under
1374 * un-accelerated emulation.
1375 */
1376 if (hw->hw_cpu_model[0] == '\0') {
1377 char* arch = avdInfo_getTargetCpuArch(avd);
1378 D("Target arch = '%s'", arch ? arch : "NULL");
1379 if (arch != NULL && !strcmp(arch, "x86")) {
1380 AFREE(hw->hw_cpu_model);
1381 hw->hw_cpu_model = ASTRDUP("qemu32");
1382 D("Auto-config: -qemu -cpu %s", hw->hw_cpu_model);
1383 }
1384 AFREE(arch);
1385 }
1386
David 'Digit' Turner26d41532011-03-01 15:03:07 +01001387 /* Generate a hardware-qemu.ini for this AVD. The real hardware
David 'Digit' Turner622f1532011-02-01 17:48:37 +01001388 * configuration is ususally stored in several files, e.g. the AVD's
1389 * config.ini plus the skin-specific hardware.ini.
1390 *
David 'Digit' Turner26d41532011-03-01 15:03:07 +01001391 * The new file will group all definitions and will be used to
David 'Digit' Turner622f1532011-02-01 17:48:37 +01001392 * launch the core with the -android-hw <file> option.
1393 */
1394 {
David 'Digit' Turner42074e52011-02-10 16:03:28 +01001395 const char* coreHwIniPath = avdInfo_getCoreHwIniPath(avd);
1396 IniFile* hwIni = iniFile_newFromMemory("", NULL);
David 'Digit' Turner622f1532011-02-01 17:48:37 +01001397 androidHwConfig_write(hw, hwIni);
David 'Digit' Turner26d41532011-03-01 15:03:07 +01001398
1399 if (filelock_create(coreHwIniPath) == NULL) {
1400 /* The AVD is already in use, we still support this as an
1401 * experimental feature. Use a temporary hardware-qemu.ini
1402 * file though to avoid overwriting the existing one. */
1403 TempFile* tempIni = tempfile_create();
1404 coreHwIniPath = tempfile_path(tempIni);
1405 }
1406
Vladimir Chtchetkinedb450d72012-01-12 13:37:40 -08001407 /* While saving HW config, ignore valueless entries. This will not break
1408 * anything, but will significantly simplify comparing the current HW
1409 * config with the one that has been associated with a snapshot (in case
1410 * VM starts from a snapshot for this instance of emulator). */
1411 if (iniFile_saveToFileClean(hwIni, coreHwIniPath) < 0) {
David 'Digit' Turner42074e52011-02-10 16:03:28 +01001412 derror("Could not write hardware.ini to %s: %s", coreHwIniPath, strerror(errno));
David 'Digit' Turner622f1532011-02-01 17:48:37 +01001413 exit(2);
1414 }
1415 args[n++] = "-android-hw";
David 'Digit' Turner42074e52011-02-10 16:03:28 +01001416 args[n++] = strdup(coreHwIniPath);
David 'Digit' Turner0a879bf2011-05-12 18:45:18 +02001417
1418 /* In verbose mode, dump the file's content */
1419 if (VERBOSE_CHECK(init)) {
1420 FILE* file = fopen(coreHwIniPath, "rt");
1421 if (file == NULL) {
1422 derror("Could not open hardware configuration file: %s\n",
1423 coreHwIniPath);
1424 } else {
1425 LineInput* input = lineInput_newFromStdFile(file);
1426 const char* line;
1427 printf("Content of hardware configuration file:\n");
1428 while ((line = lineInput_getLine(input)) != NULL) {
1429 printf(" %s\n", line);
1430 }
1431 printf(".\n");
1432 lineInput_free(input);
1433 fclose(file);
1434 }
1435 }
David 'Digit' Turner622f1532011-02-01 17:48:37 +01001436 }
1437
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001438 if(VERBOSE_CHECK(init)) {
1439 int i;
David 'Digit' Turner33361762011-01-19 22:11:03 +01001440 printf("QEMU options list:\n");
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001441 for(i = 0; i < n; i++) {
David 'Digit' Turner33361762011-01-19 22:11:03 +01001442 printf("emulator: argv[%02d] = \"%s\"\n", i, args[i]);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001443 }
David 'Digit' Turner33361762011-01-19 22:11:03 +01001444 /* Dump final command-line option to make debugging the core easier */
1445 printf("Concatenated QEMU options:\n");
1446 for (i = 0; i < n; i++) {
David 'Digit' Turner26722dd2011-02-24 16:40:20 +01001447 /* To make it easier to copy-paste the output to a command-line,
1448 * quote anything that contains spaces.
1449 */
1450 if (strchr(args[i], ' ') != NULL) {
1451 printf(" '%s'", args[i]);
1452 } else {
1453 printf(" %s", args[i]);
1454 }
David 'Digit' Turner33361762011-01-19 22:11:03 +01001455 }
1456 printf("\n");
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001457 }
David 'Digit' Turner74d7ace2011-02-02 13:21:03 +01001458
1459 /* Setup SDL UI just before calling the code */
1460 init_sdl_ui(skinConfig, skinPath, opts);
1461
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +01001462 if (attach_ui_to_core(opts) < 0) {
1463 derror("Can't attach to core!");
1464 exit(1);
1465 }
1466
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001467 return qemu_main(n, args);
1468}