summaryrefslogtreecommitdiff
path: root/scripts/poky-qemu-internal
blob: f1aa43945af5cac6679cd3411cd383038a25bae5 (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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/bin/bash

# Handle running Poky images under qemu
#
# Copyright (C) 2006-2008 OpenedHand Ltd.
#
# 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.


# Call setting:
#   QEMU_MEMORY (optional) - set the amount of memory in the emualted system.
#   SERIAL_LOGFILE (optional) - log the serial port output to a file
#   CROSSPATH - the path to any cross toolchain to use with distcc
#
# Image options:
#   MACHINE - the machine to run
#   TYPE - the image type to run
#   ZIMAGE - the kernel image file to use
#   HDIMAGE - the disk image file to use
#

if [ -z "$QEMU_MEMORY" ]; then
	case "$MACHINE" in
		"qemux86")
			QEMU_MEMORY="128M"
			;;
		"qemux86-64")
			QEMU_MEMORY="128M"
			;;
		"qemumips")
			QEMU_MEMORY="128M"
			;;
		"qemuppc")
			QEMU_MEMORY="128M"
			;;
		*)
			QEMU_MEMORY="64M"
			;;
	esac

fi

QEMUIFUP=`which poky-qemu-ifup`
QEMUIFDOWN=`which poky-qemu-ifdown`

LOCKDIR="/tmp/qemu-tap-locks"
[ ! -d "$LOCKDIR" ] && mkdir $LOCKDIR

POSSIBLE=`ifconfig -a | grep '^tap' | awk '{print $1}'`
TAP=""
LOCKFILE=""
for tap in $POSSIBLE; do
    LOCKFILE="$LOCKDIR/$tap"
    if lockfile $LOCKFILE; then
	TAP=$tap
	break;
    fi
done

if [ "$TAP" = "" ]; then
    GROUPID=`id -g`
    echo 'Setting up tap interface under sudo'
    TAP=`sudo $QEMUIFUP $GROUPID $POKY_NATIVE_SYSROOT`
    if [ $? -ne 0 ]; then
        # Re-run standalone to see verbose errors
        sudo $QEMUIFUP $GROUPID $POKY_NATIVE_SYSROOT
        return
    fi
    LOCKFILE=""
else
    echo "Using preconfigured tap device '$TAP'"
fi

release_lock() {
    if [ "$LOCKFILE" = "" ]; then
        $QEMUIFDOWN $TAP $POKY_NATIVE_SYSROOT
    else
        echo "Releasing lockfile of preconfigured tap device '$TAP'"
        rm -f $LOCKFILE
    fi
}

n1=$[ (`echo $TAP | sed 's/tap//'` * 2) + 1 ]
n2=$[ (`echo $TAP | sed 's/tap//'` * 2) + 2 ]

KERNEL_NETWORK_CMD="ip=192.168.7.$n2::192.168.7.$n1:255.255.255.0"
QEMU_TAP_CMD="-net tap,vlan=0,ifname=$TAP,script=no,downscript=no"
QEMU_NETWORK_CMD="-net nic,vlan=0 $QEMU_TAP_CMD"
KERNCMDLINE="mem=$QEMU_MEMORY"
QEMU_UI_OPTIONS="-show-cursor -usb -usbdevice wacom-tablet"

SERIALOPTS=""
if [ "x$SERIAL_LOGFILE" != "x" ]; then
    SERIALOPTS="-serial file:$SERIAL_LOGFILE"
fi

case "$MACHINE" in
	"qemuarm") ;;
	"qemumips") ;;
	"qemuppc") ;;
	"qemuarmv6") ;;
	"qemuarmv7") ;;
	"qemux86") ;;
        "qemux86-64") ;;
	"akita") ;;
	"spitz") ;;
	"nokia800") ;;
	"nokia800-maemo") ;;
	*)
		echo "Error: Unsupported machine type $MACHINE"
		return
		;;
esac

if [ "$TYPE" != "nfs" -a ! -f "$HDIMAGE" ]; then
    echo "Error: Image file $HDIMAGE doesn't exist"
    release_lock
    return
fi

if [ "$TYPE" = "nfs" ]; then
    NFS_SERVER="192.168.7.1"
    NFS_DIR=`echo $HDIMAGE | sed 's/^[^:]*:\(.*\)/\1/'`
    UNFS_OPTS="nfsvers=2,mountprog=21111,nfsprog=11111,udp"
fi

if [ "$NFS_SERVER" = "" ]; then
    NFS_SERVER="192.168.7.1"
    NFS_DIR=$HDIMAGE
fi

if [ ! -f "$ZIMAGE" ]; then
    echo "Error: Kernel image file $ZIMAGE doesn't exist"
    release_lock
    return
fi

if [ "$MACHINE" = "qemuarm" -o "$MACHINE" = "qemuarmv6" -o "$MACHINE" = "qemuarmv7" ]; then
    QEMU=qemu-system-arm
    MACHINE_SUBTYPE=versatilepb
    QEMU_UI_OPTIONS="$QEMU_UI_OPTIONS"
    # QEMU_UI_OPTIONS="$QEMU_UI_OPTIONS -force-pointer"
    if [ "$TYPE" = "ext3" ]; then
        KERNCMDLINE="root=/dev/sda console=ttyAMA0,115200 console=tty $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY highres=off"
        QEMUOPTIONS="$QEMU_NETWORK_CMD -M versatilepb -hda $HDIMAGE -no-reboot $QEMU_UI_OPTIONS"
    fi
    if [ "$TYPE" = "nfs" ]; then
        if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then
            echo "Error: NFS mount point $HDIMAGE doesn't exist"
            release_lock
            return
        fi
        KERNCMDLINE="root=/dev/nfs nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY"
        QEMUOPTIONS="$QEMU_NETWORK_CMD -M versatilepb --no-reboot $QEMU_UI_OPTIONS"
    fi
	if [ "$MACHINE" = "qemuarmv6" ]; then
		QEMUOPTIONS="$QEMUOPTIONS -cpu arm1136"
	fi
	if [ "$MACHINE" = "qemuarmv7" ]; then
		QEMUOPTIONS="$QEMUOPTIONS -cpu cortex-a8"
	fi
fi

if [ "$MACHINE" = "qemux86" ]; then
    QEMU=qemu
    QEMU_UI_OPTIONS="$QEMU_UI_OPTIONS -vga vmware -enable-gl"
    if [ "$TYPE" = "ext3" ]; then
        KERNCMDLINE="vga=0 root=/dev/hda mem=$QEMU_MEMORY $KERNEL_NETWORK_CMD"
        QEMUOPTIONS="$QEMU_NETWORK_CMD -hda $HDIMAGE $QEMU_UI_OPTIONS"
    fi
    if [ "$TYPE" = "nfs" ]; then
        if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then
            echo "Error: NFS mount point $HDIMAGE doesn't exist."
            release_lock
            return
        fi
        KERNCMDLINE="root=/dev/nfs nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY"
        QEMUOPTIONS="$QEMU_NETWORK_CMD $QEMU_UI_OPTIONS"
    fi
fi

if [ "$MACHINE" = "qemux86-64" ]; then
    QEMU=qemu-system-x86_64
    QEMU_UI_OPTIONS="$QEMU_UI_OPTIONS -vga vmware -enable-gl"
    if [ "$TYPE" = "ext3" ]; then
        KERNCMDLINE="vga=0 root=/dev/hda mem=$QEMU_MEMORY $KERNEL_NETWORK_CMD"
        QEMUOPTIONS="$QEMU_NETWORK_CMD -hda $HDIMAGE $QEMU_UI_OPTIONS"
    fi
    if [ "$TYPE" = "nfs" ]; then
        if [ "x$HDIMAGE" = "x" ]; then
            HDIMAGE=/srv/nfs/qemux86-64
        fi
        if [ ! -d "$HDIMAGE" ]; then
            echo "Error: NFS mount point $HDIMAGE doesn't exist."
            release_lock
            return
        fi
        KERNCMDLINE="root=/dev/nfs nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY"
        QEMUOPTIONS="$QEMU_NETWORK_CMD $QEMU_UI_OPTIONS"
    fi
fi

if [ "$MACHINE" = "spitz" ]; then
    QEMU=qemu-system-arm
    if [ "$TYPE" = "ext3" ]; then
        echo $HDIMAGE
        HDIMAGE=`readlink -f $HDIMAGE`
        echo $HDIMAGE
        if [ ! -e "$HDIMAGE.qemudisk" ]; then
            echo "Adding a partition table to the ext3 image for use by QEMU, please wait..."
            poky-addptable2image $HDIMAGE $HDIMAGE.qemudisk
        fi
        QEMUOPTIONS="$QEMU_NETWORK_CMD -M spitz -hda $HDIMAGE.qemudisk -portrait"
    fi
fi

if [ "$MACHINE" = "qemumips" ]; then
    QEMU=qemu-system-mips
    MACHINE_SUBTYPE=malta
    QEMU_UI_OPTIONS="$QEMU_UI_OPTIONS -vga cirrus"
    if [ "$TYPE" = "ext3" ]; then
        #KERNCMDLINE="root=/dev/hda console=ttyS0 console=tty0 $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY"
        KERNCMDLINE="root=/dev/hda console=ttyS0 console=tty $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY"
        QEMUOPTIONS="$QEMU_NETWORK_CMD -M $MACHINE_SUBTYPE -hda $HDIMAGE -no-reboot $QEMU_UI_OPTIONS"
    fi
fi

if [ "$MACHINE" = "qemuppc" ]; then
    QEMU=qemu-system-ppc
    MACHINE_SUBTYPE=prep
    CPU_SUBTYPE=603e
    BIOS=powerpc_rom.bin
    QEMU_UI_OPTIONS="$QEMU_UI_OPTIONS -nographic"
    if [ "$TYPE" = "ext3" ]; then
        KERNCMDLINE="root=/dev/hda console=ttyS0 console=tty0 $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY"
        QEMUOPTIONS="$QEMU_NETWORK_CMD -cpu $CPU_SUBTYPE -M $MACHINE_SUBTYPE -bios $BIOS -hda $HDIMAGE -no-reboot $QEMU_UI_OPTIONS"
    fi
fi

if [ "$MACHINE" = "akita" ]; then
    QEMU=qemu-system-arm
    if [ "$TYPE" = "jffs2" ]; then
        HDIMAGE=`readlink -f $HDIMAGE`
        if [ ! -e "$HDIMAGE.qemuflash" ]; then
            echo "Converting raw image into flash image format for use by QEMU, please wait..."
	    raw2flash.akita < $HDIMAGE > $HDIMAGE.qemuflash
        fi
        QEMUOPTIONS="$QEMU_NETWORK_CMD -M akita -mtdblock $HDIMAGE.qemuflash -portrait"
    fi
fi

if [ "$MACHINE" = "nokia800" ]; then
    QEMU=qemu-system-arm
    if [ "$TYPE" = "jffs2" ]; then
        HDIMAGE=`readlink -f $HDIMAGE`
        if [ ! -e "$HDIMAGE.qemuflash" ]; then
            echo "'Flashing' rootfs, please wait..."
            poky-nokia800-flashutil $HDIMAGE $HDIMAGE.qemuflash 
        fi
	KERNCMDLINE="root=/dev/mtdblock4 rootfstype=jffs2"
        QEMU_NETWORK_CMD="-net nic,model=usb,vlan=0 $QEMU_TAP_CMD"
        QEMUOPTIONS="$QEMU_NETWORK_CMD -M n800 -mtdblock $HDIMAGE.qemuflash -serial vc -m 130 -serial vc -serial vc -serial vc -usb -usbdevice net:0"
    fi
fi

if [ "$MACHINE" = "nokia800-maemo" ]; then
    QEMU=qemu-system-arm
    if [ "$TYPE" = "jffs2" ]; then
        HDIMAGE=`readlink -f $HDIMAGE`
        if [ ! -e "$HDIMAGE.qemuflash" ]; then
            if [ ! -e "$HDIMAGE.initfs" ]; then
                echo "Error, $HDIMAGE.initfs must exist!"
                release_lock
                return
            fi
            if [ ! -e "$HDIMAGE.config" ]; then
                echo "Error, $HDIMAGE.config must exist!"
                echo "To generate it, take an n800 and cat /dev/mtdblock1 > $HDIMAGE.config"
                release_lock
                return
            fi
            echo "'Flashing' config partition, please wait..."
            poky-nokia800-flashutil $HDIMAGE.config $HDIMAGE.qemuflash config
            echo "'Flashing' initfs, please wait..."
            poky-nokia800-flashutil $HDIMAGE.initfs $HDIMAGE.qemuflash initfs
            echo "'Flashing' rootfs, please wait..."
            poky-nokia800-flashutil $HDIMAGE $HDIMAGE.qemuflash 
        fi
	KERNCMDLINE=""
        QEMU_NETWORK_CMD="-net nic,model=usb,vlan=0 $QEMU_TAP_CMD"
        QEMUOPTIONS="$QEMU_NETWORK_CMD -M n800 -mtdblock $HDIMAGE.qemuflash -serial vc -m 130 -serial vc -serial vc -serial vc -usb -usbdevice net:0 -show-cursor"
    fi
fi

if [ "x$QEMUOPTIONS" = "x" ]; then
    echo "Error: Unable to support this combination of options"
    release_lock
    return
fi

SDKDIR="/opt/poky/sysroots"
if [ "$MACHINE" = "qemuarm" -o "$MACHINE" = "spitz" -o "$MACHINE" = "borzoi" -o "$MACHINE" = "akita" -o "$MACHINE" = "nokia800" ]; then
    SDKPATH="$SDKDIR/arm-poky-linux-gnueabi/bin"
fi

if [ "$MACHINE" = "qemux86" ]; then
    SDKPATH="$SDKDIR/i586-poky-linux/bin"
fi

if [ "$MACHINE" = "qemux86-64" ]; then
    SDKPATH="$SDKDIR/x86_64-poky-linux/bin"
fi

PATH=$CROSSPATH:$SDKPATH:$PATH

QEMUBIN=`which $QEMU`

if [ ! -x "$QEMUBIN" ]; then
    echo "Error: No QEMU binary '$QEMU' could be found."
    release_lock
    return
fi

function _quit() {
    if [ -n "$PIDFILE" ]; then
        #echo kill `cat $PIDFILE`
        kill `cat $PIDFILE`
    fi
    release_lock
    return
}

DISTCCD=`which distccd`
PIDFILE=""

trap _quit INT TERM QUIT

if [ -x "$DISTCCD" ]; then
    echo "Starting distccd..."
    PIDFILE=`mktemp`
    $DISTCCD --allow 192.168.7.2 --daemon --pid-file $PIDFILE &
else
    echo "Warning: distccd not present, no distcc support loaded."
fi

echo "Running $QEMU..."
echo $QEMUBIN -kernel $ZIMAGE $QEMUOPTIONS $SERIALOPTS $* --append '"'$KERNCMDLINE'"'
$QEMUBIN -kernel $ZIMAGE $QEMUOPTIONS $SERIALOPTS $* --append "$KERNCMDLINE" || /bin/true

release_lock

trap - INT TERM QUIT
return