summaryrefslogtreecommitdiff
path: root/scripts/poky-qemu
blob: 66e7a298f62438238092d1961eed895df32d04ed (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
#!/bin/bash
#
# Handle running Poky images standalone with QEMU
#
# Copyright (C) 2006-2010 Intel Corp.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

usage() {
    MYNAME=`basename $0`
    echo ""
    echo "Usage: you can run this script with any valid combination"
    echo "of the following options (in any order):"
    echo "  QEMUARCH - the qemu machine architecture to use"
    echo "  KERNEL - the kernel image file to use"
    echo "  ROOTFS - the rootfs image file or nfsroot directory to use"
    echo "  MACHINE=xyz - the machine name (optional, autodetected from KERNEL filename if unspecified)"
    echo "  Additional QEMU command-line options can be passed with:"
    echo "    serial - enables a serial console on /dev/ttyS0"
    echo ""
    echo "Examples:"
    echo "  $0 qemuarm"
    echo "  $0 qemux86-64 poky-image-sato ext3"
    echo "  $0 path/to/bzImage-qemux86.bin path/to/nfsrootdir/ serial"
    exit 1
}

if [ "x$1" = "x" ]; then
    usage
fi

MACHINE=${MACHINE:=""}
KERNEL=""
FSTYPE=""
ROOTFS=""
LAZY_ROOTFS=""
SCRIPT_QEMU_OPT=""
SCRIPT_KERNEL_OPT=""

TMPDIR=""

# Parse command line args without requiring specific ordering. It's a
# bit more complex, but offers a great user experience.
i=1
while [ $i -le $# ]; do
    arg=${!i}
    case $arg in
        "qemux86" | "qemux86-64" | "qemuarm" | "qemumips" | "qemuppc")
            if [ -z "$MACHINE" ]; then
                MACHINE=$arg
            else
                echo "Error: conflicting MACHINE types [$MACHINE] and [$arg]"
                usage
            fi
            ;;
        "ext2" | "ext3" | "jffs2" | "nfs")
            if [ -z "$FSTYPE" ]; then
                FSTYPE=$arg
            else
                echo "Error: conflicting FSTYPE types [$FSTYPE] and [$arg]"
                usage
            fi
            ;;
        *-image-*)
            if [ -z "$ROOTFS" ]; then
                if [ -e "$arg" ]; then
                    ROOTFS=$arg
                else
                    ROOTFS=$arg
                    LAZY_ROOTFS="true"
                fi
            else
                echo "Error: conflicting ROOTFS args [$ROOTFS] and [$arg]"
                usage
            fi
            ;;
        "serial")
            # Will need to append to these variables when we
            # accept more values
            SCRIPT_QEMU_OPT="-serial stdio"
            SCRIPT_KERNEL_OPT="console=ttyS0"
            ;;
        *)
            # A directory name is an nfs rootfs
            if [ -d "$arg" ]; then
                echo "Assuming $arg is an nfs rootfs"
                if [[ -z "$FSTYPE" || "$FSTYPE" == "nfs" ]]; then
                    FSTYPE=nfs
                else
                    echo "Error: conflicting FSTYPE types [$arg] and nfs"
                    usage
                fi

                if [ -z "$ROOTFS" ]; then
                    ROOTFS=$arg
                else
                    echo "Error: conflicting ROOTFS args [$ROOTFS] and [$arg]"
                    usage
                fi
            elif [ -f "$arg" ]; then
                # Extract the filename extension
                EXT=`echo $arg | awk -F . '{ print \$NF }'`
                # A file ending in .bin is a kernel
                if [ "x$EXT" = "xbin" ]; then
                    if [ -z "$KERNEL" ]; then
                        KERNEL=$arg
                    else
                        echo "Error: conflicting KERNEL args [$KERNEL] and [$arg]"
                        usage
                    fi
                elif [[ "x$EXT" == "xext2" || "x$EXT" == "xext3" ||
                        "x$EXT" == "xjffs2" ]]; then
                     # A file ending in a supportted fs type is a rootfs image
                     if [[ -z "$FSTYPE" || "$FSTYPE" == "$EXT" ]]; then
                         FSTYPE=$EXT
                         ROOTFS=$arg
                     else
                         echo "Error: conflicting FSTYPE types [$FSTYPE] and [$arg]"
                         usage
                     fi
                else
                    echo "Error: unknown file arg [$arg]"
                    usage
                fi
            else
                echo "Error: unable to classify arg [$arg]"
                usage
            fi
            ;;
    esac
    i=$((i + 1))
done

# Report errors for missing combinations of options
if [[ -z "$MACHINE" && -z "$KERNEL" ]]; then
    echo "Error: you must specify at least a MACHINE or KERNEL argument"
    usage
fi
if [[ "$FSTYPE" == "nfs" && -z "$ROOTFS" ]]; then
    echo "Error: NFS booting without an explicit ROOTFS path is not yet supported"
    usage
fi

if [ -z "$MACHINE" ]; then
    MACHINE=`basename $KERNEL | sed -r -e 's#.*-([a-z]+[0-9\-]*)-?[0-9]*..*#\1#'`
    if [ -z "$MACHINE" ]; then
        echo "Error: Unable to set MACHINE from kernel filename [$KERNEL]"
        usage
    fi
    echo "Set MACHINE to [$MACHINE] based on kernel [$KERNEL]"
fi
machine2=`echo $MACHINE | tr 'a-z' 'A-Z' | sed 's/-/_/'`
# MACHINE is now set for all cases

if [[ -e "$ROOTFS" && -z "$FSTYPE" ]]; then
    # Extract the filename extension
    EXT=`echo $ROOTFS | awk -F . '{ print \$NF }'`
    if [[ "x$EXT" == "xext2" || "x$EXT" == "xext3" || 
          "x$EXT" == "xjffs2" ]]; then
        FSTYPE=$EXT
    else
        echo "Note: Unable to determine filesystem extension for $ROOTFS"
        echo "We will use the default FSTYPE for $MACHINE"
        # ...which is done further below...
    fi
fi

# Defaults used when these vars need to be inferred
QEMUX86_DEFAULT_KERNEL=bzImage-qemux86.bin
QEMUX86_DEFAULT_FSTYPE=ext3
QEMUX86_DEFAULT_ROOTFS="poky-image-sdk poky-image-sato poky-image-lsb poky-image-basic poky-image-minimal"

QEMUX86_64_DEFAULT_KERNEL=bzImage-qemux86-64.bin
QEMUX86_64_DEFAULT_FSTYPE=ext3
QEMUX86_64_DEFAULT_ROOTFS="poky-image-sdk poky-image-sato poky-image-lsb poky-image-basic poky-image-minimal"

QEMUARM_DEFAULT_KERNEL=zImage-qemuarm.bin
QEMUARM_DEFAULT_FSTYPE=ext3
QEMUARM_DEFAULT_ROOTFS="poky-image-sdk poky-image-sato poky-image-lsb poky-image-basic poky-image-minimal"

QEMUMIPS_DEFAULT_KERNEL=vmlinux-qemumips.bin
QEMUMIPS_DEFAULT_FSTYPE=ext3
QEMUMIPS_DEFAULT_ROOTFS="poky-image-sdk poky-image-sato poky-image-lsb poky-image-basic poky-image-minimal"

QEMUPPC_DEFAULT_KERNEL=zImage-qemuppc.bin
QEMUPPC_DEFAULT_FSTYPE=ext3
QEMUPPC_DEFAULT_ROOTFS="poky-image-sdk poky-image-sato poky-image-lsb poky-image-basic poky-image-minimal"

AKITA_DEFAULT_KERNEL=zImage-akita.bin
AKITA_DEFAULT_FSTYPE=jffs2
AKITA_DEFAULT_ROOTFS="poky-image-sato"

SPITZ_DEFAULT_KERNEL=zImage-spitz.bin
SPITZ_DEFAULT_FSTYPE=ext3
SPITZ_DEFAULT_ROOTFS="poky-image-sato"

setup_tmpdir() {
    if [ -z "$TMPDIR" ]; then
        if [ "x$BUILDDIR" = "x" ]; then
            # BUILDDIR unset, try and get TMPDIR from bitbake
            type -P bitbake &>/dev/null || {
                echo "In order for this script to dynamically infer paths";
                echo "to kernels or filesystem images, you either need";
                echo "bitbake in your PATH or to source poky-init-build-env";
                echo "before running this script" >&2;
                exit 1; }

            # We have bitbake in PATH, get TMPDIR and BUILD_SYS
            # from the environment
            TMPDIR=`bitbake -e | grep TMPDIR=\" | cut -d '=' -f2 | cut -d '"' -f2`
            BUILD_SYS=`bitbake -e | grep BUILD_SYS=\" | cut -d '=' -f2 | cut -d '"' -f2`
        else
            BUILD_ARCH=`uname -m`
            BUILD_OS=`uname | tr '[A-Z]' '[a-z]'`
            BUILD_SYS="$BUILD_ARCH-$BUILD_OS"
            TMPDIR=$BUILDDIR/tmp
        fi
        if [ -z "$POKY_NATIVE_SYSROOT" ]; then
            POKY_NATIVE_SYSROOT=$TMPDIR/sysroots/$BUILD_SYS
        fi
        CROSSPATH=$POKY_NATIVE_SYSROOT/usr/bin
    fi
}

# Locate a rootfs image based on defaults defined above
findimage() {
    where=$1
    machine=$2
    extension=$3
    names=$4

    for name in $names; do
        fullname=$where/$name-$machine.$extension
        if [ -e "$fullname" ]; then
            ROOTFS=$fullname
            return
        fi
    done

    echo "Couldn't find image in $where. Attempted image names were:"
    for name in $names; do
        echo $name-$machine.$extension
    done

    exit 1
}

if [ -z "$KERNEL" ]; then
    setup_tmpdir
    eval kernel_file=\$${machine2}_DEFAULT_KERNEL
    KERNEL=$TMPDIR/deploy/images/$kernel_file

    if [ -z "$KERNEL" ]; then
        echo "Error: Unable to determine default kernel for MACHINE [$MACHINE]"
        usage
    fi
fi
# KERNEL is now set for all cases

if [ -z "$FSTYPE" ]; then
    setup_tmpdir
    eval FSTYPE=\$${machine2}_DEFAULT_FSTYPE

    if [ -z "$FSTYPE" ]; then
        echo "Error: Unable to determine default fstype for MACHINE [$MACHINE]"
        usage
    fi
fi
# FSTYPE is now set for all cases

# Handle cases where a ROOTFS type is given instead of a filename, e.g.
# poky-image-sato
if [ "$LAZY_ROOTFS" = "true" ]; then
    setup_tmpdir
    echo "Assuming $ROOTFS really means $TMPDIR/deploy/images/$ROOTFS-$MACHINE.$FSTYPE"
    ROOTFS=$TMPDIR/deploy/images/$ROOTFS-$MACHINE.$FSTYPE
fi

if [ -z "$ROOTFS" ]; then
    setup_tmpdir
    T=$TMPDIR/deploy/images
    eval rootfs_list=\$${machine2}_DEFAULT_ROOTFS
    findimage $T $MACHINE $FSTYPE "$rootfs_list"

    if [ -z "$ROOTFS" ]; then
        echo "Error: Unable to determine default rootfs for MACHINE [$MACHINE]"
        usage
    fi
fi
# ROOTFS is now set for all cases

echo ""
echo "Continuing with the following parameters:"
echo "KERNEL: [$KERNEL]"
echo "ROOTFS: [$ROOTFS]"
echo "FSTYPE: [$FSTYPE]"

# We can't run without a libGL.so
libgl='no'

test -e /usr/lib/libGL.so -a -e /usr/lib/libGLU.so && libgl='yes'
test -e /usr/lib64/libGL.so -a -e /usr/lib64/libGLU.so && libgl='yes'

if [ "$libgl" != 'yes' ]; then
    echo "You need libGL.so and libGLU.so to exist in your library path to run the QEMU emulator.
    Ubuntu package names are: libgl1-mesa-dev and libglu1-mesa-dev."
    exit 1;
fi

INTERNAL_SCRIPT=`which poky-qemu-internal`

. $INTERNAL_SCRIPT