aboutsummaryrefslogtreecommitdiffstats
path: root/android/avd/util.c
blob: cc51e0f18cb3f9fa9a1aa8b1a8b19b87e9ec146b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* Copyright (C) 2011 The Android Open Source Project
**
** This software is licensed under the terms of the GNU General Public
** License version 2, as published by the Free Software Foundation, and
** may be copied, distributed, and modified under those terms.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
*/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include "android/utils/debug.h"
#include "android/utils/bufprint.h"
#include "android/utils/ini.h"
#include "android/utils/panic.h"
#include "android/utils/path.h"
#include "android/utils/system.h"
#include "android/avd/util.h"

#define D(...) VERBOSE_PRINT(init,__VA_ARGS__)

/* this is the subdirectory of $HOME/.android where all
 * root configuration files (and default content directories)
 * are located.
 */
#define  ANDROID_AVD_DIR    "avd"


/* Return the path to the Android SDK root installation.
 *
 * (*pFromEnv) will be set to 1 if it comes from the $ANDROID_SDK_ROOT
 * environment variable, or 0 otherwise.
 *
 * Caller must free() returned string.
 */
char*
path_getSdkRoot( char *pFromEnv )
{
    const char*  env;
    char*        sdkPath;
    char         temp[PATH_MAX], *p=temp, *end=p+sizeof(temp);

    /* If ANDROID_SDK_ROOT is defined is must point to a directory
     * containing a valid SDK installation.
     */
#define  SDK_ROOT_ENV  "ANDROID_SDK_ROOT"

    env = getenv(SDK_ROOT_ENV);
    if (env != NULL && env[0] != 0) {
        if (path_exists(env)) {
            D("found " SDK_ROOT_ENV ": %s", env);
            *pFromEnv = 1;
            return ASTRDUP(env);
        }
        D(SDK_ROOT_ENV " points to unknown directory: %s", env);
    }

    *pFromEnv = 0;

    /* We assume the emulator binary is under tools/ so use its
     * parent as the Android SDK root.
     */
    (void) bufprint_app_dir(temp, end);
    sdkPath = path_parent(temp, 1);
    if (sdkPath == NULL) {
        derror("can't find root of SDK directory");
        return NULL;
    }
    D("found SDK root at %s", sdkPath);
    return sdkPath;
}


/* Return the path to the AVD's root configuration .ini file. it is located in
 * ~/.android/avd/<name>.ini or Windows equivalent
 *
 * This file contains the path to the AVD's content directory, which
 * includes its own config.ini.
 */
char*
path_getRootIniPath( const char*  avdName )
{
    char temp[PATH_MAX], *p=temp, *end=p+sizeof(temp);

    p = bufprint_config_path(temp, end);
    p = bufprint(p, end, "/" ANDROID_AVD_DIR "/%s.ini", avdName);
    if (p >= end) {
        return NULL;
    }
    if (!path_exists(temp)) {
        return NULL;
    }
    return ASTRDUP(temp);
}


char*
path_getSdkHome(void)
{
    char temp[PATH_MAX], *p=temp, *end=p+sizeof(temp);
    p = bufprint_config_path(temp, end);
    if (p >= end) {
        APANIC("User path too long!: %s\n", temp);
    }
    return strdup(temp);
}


static char*
_getAvdContentPath(const char* avdName)
{
    char*    sdkHome = path_getSdkHome();
    char*    avdPath = NULL;
    IniFile* ini;
    char     temp[PATH_MAX], *p=temp, *end=p+sizeof(temp);

    /* Look for the root .ini file */
    p = bufprint(temp, end, "%s/avd/%s.ini", sdkHome, avdName);
    if (p >= end) {
        APANIC("AVD Name too long: %s\n", avdName);
    }

    ini = iniFile_newFromFile(temp);
    if (ini == NULL) {
        APANIC("Could not open: %s", temp);
    }

    avdPath = iniFile_getString(ini, "path", NULL);

    iniFile_free(ini);
    AFREE(sdkHome);

    return avdPath;
}


static char*
_getAvdTargetArch(const char* avdPath)
{
    IniFile* ini;
    char*    targetArch = NULL;
    char     temp[PATH_MAX], *p=temp, *end=p+sizeof(temp);
    p = bufprint(temp, end, "%s/config.ini", avdPath);
    if (p >= end) {
        APANIC("AVD path too long: %s\n", avdPath);
    }
    ini = iniFile_newFromFile(temp);
    if (ini == NULL) {
        APANIC("Could not open AVD config file: %s", temp);
    }
    targetArch = iniFile_getString(ini, "hw.cpu.arch", "arm");
    iniFile_free(ini);

    return targetArch;
}

char*
path_getAvdTargetArch( const char* avdName )
{
    char*  avdPath = _getAvdContentPath(avdName);
    char*  avdArch = _getAvdTargetArch(avdPath);

    return avdArch;
}

/* Retrieves the value of a given system property defined in a .prop
 * file. This is a text file that contains definitions of the format:
 * <name>=<value>
 *
 * Returns NULL if property <name> is undefined or empty.
 * Returned string must be freed by the caller.
 */
static char*
_getSystemProperty( const char* propFile, const char* propName )
{
    FILE*  file;
    char   temp[PATH_MAX], *p=temp, *end=p+sizeof(temp);
    int    propNameLen = strlen(propName);
    char*  result = NULL;

    file = fopen(propFile, "rb");
    if (file == NULL) {
        D("Could not open file: %s: %s", propFile, strerror(errno));
        return NULL;
    }

    while (fgets(temp, sizeof temp, file) != NULL) {
        /* Trim trailing newlines, if any */
        p = memchr(temp, '\0', sizeof temp);
        if (p == NULL)
            p = end;
        if (p > temp && p[-1] == '\n') {
            *--p = '\0';
        }
        if (p > temp && p[-1] == '\r') {
            *--p = '\0';
        }
        /* force zero-termination in case of full-buffer */
        if (p == end)
            *--p = '\0';

        /* check that the line starts with the property name */
        if (memcmp(temp, propName, propNameLen) != 0) {
            continue;
        }
        p = temp + propNameLen;

        /* followed by an equal sign */
        if (p >= end || *p != '=')
            continue;
        p++;

        /* followed by something */
        if (p >= end || !*p)
            break;

        result = ASTRDUP(p);
        break;
    }
    fclose(file);
    return result;
}

static char*
_getBuildProperty( const char* androidOut, const char* propName )
{
    char temp[PATH_MAX], *p=temp, *end=p+sizeof(temp);

    p = bufprint(temp, end, "%s/system/build.prop", androidOut);
    if (p >= end) {
        D("%s: ANDROID_PRODUCT_OUT too long: %s", __FUNCTION__, androidOut);
        return NULL;
    }
    return _getSystemProperty(temp, propName);
}

char*
path_getBuildTargetArch( const char* androidOut )
{
    const char* defaultArch = "arm";
    char*       result = NULL;
    char*       cpuAbi = _getBuildProperty(androidOut, "ro.product.cpu.abi");

    if (cpuAbi == NULL) {
        D("Coult not find CPU ABI in build properties!");
        D("Default target architecture=%s", defaultArch);
        result = ASTRDUP(defaultArch);
    } else {
        /* Translate ABI to cpu arch if necessary */
        if (!strcmp("armeabi",cpuAbi))
            result = "arm";
        else if (!strcmp("armeabi-v7a", cpuAbi))
            result = "arm";
        else if (!strncmp("mips", cpuAbi, 4))
            result = "mips";
        else
            result = cpuAbi;

        D("Found target ABI=%s, architecture=%s", cpuAbi, result);
        result = ASTRDUP(result);
        AFREE(cpuAbi);
    }
    return result;
}

char*
path_getBuildTargetAbi( const char* androidOut )
{
    const char* defaultAbi = "armeabi";
    char*       result = NULL;
    char*       cpuAbi = _getBuildProperty(androidOut, "ro.product.cpu.abi");

    if (cpuAbi == NULL) {
        D("Coult not find CPU ABI in build properties!");
        D("Default target ABI: %s", defaultAbi);
        result = ASTRDUP(defaultAbi);
    } else {
        D("Found target ABI=%s", cpuAbi);
        result = cpuAbi;
    }
    return result;
}


int
path_getBuildTargetApiLevel( const char* androidOut )
{
    const int  defaultLevel = 1000;
    int        level        = defaultLevel;
    char*      sdkVersion = _getBuildProperty(androidOut, "ro.build.version.sdk");

    if (sdkVersion != NULL) {
        long  value;
        char* end;
        value = strtol(sdkVersion, &end, 10);
        if (end == NULL || *end != '\0' || value != (int)value) {
            D("Invalid SDK version build property: '%s'", sdkVersion);
            D("Defaulting to target API level %d", level);
        } else {
            level = (int)value;
            /* Sanity check, the Android SDK doesn't support anything
             * before Android 1.5, a.k.a API level 3 */
            if (level < 3)
                level = 3;
            D("Found target API level: %d", level);
        }
        AFREE(sdkVersion);
    } else {
        D("Could not find target API level / SDK version in build properties!");
        D("Default target API level: %d", level);
    }
    return level;
}

int
path_getAdbdCommunicationMode( const char* androidOut )
{
    char* prop = _getBuildProperty(androidOut, "ro.adb.qemud");
    if (prop != NULL) {
        long val = 0;
        char* end;
        val = strtol(prop, &end, 10);
        if (end == NULL || *end != '\0' || val != (int)val) {
            D("Invalid ro.adb.qemud build property: '%s'", prop);
            val = 0;
        } else {
            D("Found ro.adb.qemud build property: %d", val);
        }
        AFREE(prop);
        return (int)val;
    } else {
        /* Missing ro.adb.qemud means "legacy" ADBD. */
        return 0;
    }
}