blob: 15272683b803439e826d41443bd41d998b1fefa1 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#!/bin/bash -x
2
3# Handle running OE images under qemu
4#
5# Copyright (C) 2006-2011 Linux Foundation
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License version 2 as
9# published by the Free Software Foundation.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License along
17# with this program; if not, write to the Free Software Foundation, Inc.,
18# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20# Call setting:
21# QEMU_MEMORY (optional) - set the amount of memory in the emualted system.
22# SERIAL_LOGFILE (optional) - log the serial port output to a file
23#
24# Image options:
25# MACHINE - the machine to run
26# FSTYPE - the image type to run
27# KERNEL - the kernel image file to use
28# ROOTFS - the disk image file to use
29#
30
31
32mem_size=-1
33
34#Get rid of <> and get the contents of extra qemu running params
35SCRIPT_QEMU_EXTRA_OPT=`echo $SCRIPT_QEMU_EXTRA_OPT | sed -e 's/<//' -e 's/>//'`
36#if user set qemu memory, eg: -m 256 in qemu extra params, we need to do some
37# validation check
38mem_set=`expr "$SCRIPT_QEMU_EXTRA_OPT" : '.*\(-m[[:space:]] *[0-9]*\)'`
39if [ ! -z "$mem_set" ] ; then
40#Get memory setting size from user input
41 mem_size=`echo $mem_set | sed 's/-m[[:space:]] *//'`
42else
43 case "$MACHINE" in
44 "qemux86")
45 mem_size=256
46 ;;
47 "qemux86-64")
48 mem_size=256
49 ;;
50 "qemuarm")
51 mem_size=128
52 ;;
53 "qemuarm64")
54 mem_size=512
55 ;;
56 "qemumicroblaze")
57 mem_size=64
58 ;;
59 "qemumips"|"qemumips64")
60 mem_size=256
61 ;;
62 "qemuppc")
63 mem_size=256
64 ;;
65 "qemush4")
66 mem_size=1024
67 ;;
68 "qemuzynq")
69 mem_size=1024
70 ;;
71 *)
72 mem_size=64
73 ;;
74 esac
75
76fi
77
78# QEMU_MEMORY has 'M' appended to mem_size
79QEMU_MEMORY="$mem_size"M
80
81# Bug 433: qemuarm cannot use > 256 MB RAM
82if [ "$MACHINE" = "qemuarm" ]; then
83 if [ -z "$mem_size" -o $mem_size -gt 256 ]; then
84 echo "WARNING: qemuarm does not support > 256M of RAM."
85 echo "Changing QEMU_MEMORY to default of 256M."
86 QEMU_MEMORY="256M"
87 mem_size="256"
88 SCRIPT_QEMU_EXTRA_OPT=`echo $SCRIPT_QEMU_EXTRA_OPT | sed -e "s/$mem_set/-m 256/" `
89 fi
90fi
91
92# We need to specify -m <mem_size> to overcome a bug in qemu 0.14.0
93# https://bugs.launchpad.net/ubuntu/+source/qemu-kvm/+bug/584480
94
95if [ -z "$mem_set" ] ; then
96 SCRIPT_QEMU_EXTRA_OPT="$SCRIPT_QEMU_EXTRA_OPT -m $mem_size"
97fi
98# This file is created when runqemu-gen-tapdevs creates a bank of tap
99# devices, indicating that the user should not bring up new ones using
100# sudo.
101NOSUDO_FLAG="/etc/runqemu-nosudo"
102
103QEMUIFUP=`which runqemu-ifup 2> /dev/null`
104QEMUIFDOWN=`which runqemu-ifdown 2> /dev/null`
105if [ -z "$QEMUIFUP" -o ! -x "$QEMUIFUP" ]; then
106 echo "runqemu-ifup cannot be found or executed"
107 exit 1
108fi
109if [ -z "$QEMUIFDOWN" -o ! -x "$QEMUIFDOWN" ]; then
110 echo "runqemu-ifdown cannot be found or executed"
111 exit 1
112fi
113
114NFSRUNNING="false"
115
116#capture original stty values
117ORIG_STTY=$(stty -g)
118
119if [ "$SLIRP_ENABLED" = "yes" ]; then
120 KERNEL_NETWORK_CMD="ip=dhcp"
121 QEMU_TAP_CMD=""
122 QEMU_UI_OPTIONS="-show-cursor -usb -usbdevice wacom-tablet"
123 if [ "$KVM_ACTIVE" = "yes" ]; then
124 QEMU_NETWORK_CMD=""
125 DROOT="/dev/vda"
126 ROOTFS_OPTIONS="-drive file=$ROOTFS,if=virtio,format=raw"
127 else
128 QEMU_NETWORK_CMD=""
129 DROOT="/dev/hda"
130 ROOTFS_OPTIONS="-drive file=$ROOTFS,if=ide,format=raw"
131 fi
132
133else
134 acquire_lock() {
135 lockfile=$1
136 if [ -z "$lockfile" ]; then
137 echo "Error: missing lockfile arg passed to acquire_lock()"
138 return 1
139 fi
140
141 touch $lockfile.lock 2>/dev/null
142 if [ $? -ne 0 ]; then
143 echo "Acquiring lockfile for $lockfile.lock failed"
144 return 1
145 fi
146 exec 8>$lockfile.lock
147 flock -n -x 8
148 if [ $? -ne 0 ]; then
149 exec 8>&-
150 return 1
151 fi
152
153 return 0
154 }
155
156 release_lock() {
157 lockfile=$1
158 if [ -z "$lockfile" ]; then
159 echo "Error: missing lockfile arg passed to release_lock()"
160 return 1
161 fi
162
163 rm -f $lockfile.lock
164 exec 8>&-
165 }
166
167 LOCKDIR="/tmp/qemu-tap-locks"
168 if [ ! -d "$LOCKDIR" ]; then
169 mkdir $LOCKDIR
170 chmod 777 $LOCKDIR
171 fi
172
173 IFCONFIG=`which ip 2> /dev/null`
174 if [ -z "$IFCONFIG" ]; then
175 IFCONFIG=/sbin/ip
176 fi
177 if [ ! -x "$IFCONFIG" ]; then
178 echo "$IFCONFIG cannot be executed"
179 exit 1
180 fi
181
182 POSSIBLE=`$IFCONFIG link | grep 'tap' | awk '{print $2}' | sed -e 's/://' -e 's/@.*//'`
183 TAP=""
184 LOCKFILE=""
185 USE_PRECONF_TAP="no"
186 for tap in $POSSIBLE; do
187 LOCKFILE="$LOCKDIR/$tap"
188 if [ -e "$LOCKFILE.skip" ]; then
189 echo "Found $LOCKFILE.skip, skipping $tap"
190 continue
191 fi
192 echo "Acquiring lockfile for $tap..."
193 acquire_lock $LOCKFILE
194 if [ $? -eq 0 ]; then
195 TAP=$tap
196 USE_PRECONF_TAP="yes"
197 break
198 fi
199 done
200
201 if [ "$TAP" = "" ]; then
202 if [ -e "$NOSUDO_FLAG" ]; then
203 echo "Error: There are no available tap devices to use for networking,"
204 echo "and I see $NOSUDO_FLAG exists, so I am not going to try creating"
205 echo "a new one with sudo."
206 exit 1
207 fi
208
209 GROUPID=`id -g`
210 USERID=`id -u`
211 echo "Setting up tap interface under sudo"
212 # Redirect stderr since we could see a LD_PRELOAD warning here if pseudo is loaded
213 # but inactive. This looks scary but is harmless
214 tap=`sudo $QEMUIFUP $USERID $GROUPID $OECORE_NATIVE_SYSROOT 2> /dev/null`
215 if [ $? -ne 0 ]; then
216 # Re-run standalone to see verbose errors
217 sudo $QEMUIFUP $USERID $GROUPID $OECORE_NATIVE_SYSROOT
218 return 1
219 fi
220 LOCKFILE="$LOCKDIR/$tap"
221 echo "Acquiring lockfile for $tap..."
222 acquire_lock $LOCKFILE
223 if [ $? -eq 0 ]; then
224 TAP=$tap
225 fi
226 else
227 echo "Using preconfigured tap device '$TAP'"
228 echo "If this is not intended, touch $LOCKFILE.skip to make runqemu skip $TAP."
229 fi
230
231 cleanup() {
232 if [ ! -e "$NOSUDO_FLAG" -a "$USE_PRECONF_TAP" = "no" ]; then
233 # Redirect stderr since we could see a LD_PRELOAD warning here if pseudo is loaded
234 # but inactive. This looks scary but is harmless
235 sudo $QEMUIFDOWN $TAP $OECORE_NATIVE_SYSROOT 2> /dev/null
236 fi
237 echo "Releasing lockfile of preconfigured tap device '$TAP'"
238 release_lock $LOCKFILE
239
240 if [ "$NFSRUNNING" = "true" ]; then
241 echo "Shutting down the userspace NFS server..."
242 echo "runqemu-export-rootfs stop $ROOTFS"
243 runqemu-export-rootfs stop $ROOTFS
244 fi
245 # If QEMU crashes or somehow tty properties are not restored
246 # after qemu exits, we need to run stty sane
247 #stty sane
248
249 #instead of using stty sane we set the original stty values
250 stty ${ORIG_STTY}
251
252 }
253
254
255 n0=$(echo $TAP | sed 's/tap//')
256
257 case $n0 in
258 ''|*[!0-9]*)
259 echo "Error Couldn't turn $TAP into an interface number?"
260 exit 1
261 ;;
262 esac
263
264 n1=$(($n0 * 2 + 1))
265 n2=$(($n1 + 1))
266
267 KERNEL_NETWORK_CMD="ip=192.168.7.$n2::192.168.7.$n1:255.255.255.0"
268 QEMU_TAP_CMD="-net tap,vlan=0,ifname=$TAP,script=no,downscript=no"
269 if [ "$KVM_ACTIVE" = "yes" ]; then
270 QEMU_NETWORK_CMD="-net nic,model=virtio $QEMU_TAP_CMD,vhost=on"
271 DROOT="/dev/vda"
272 ROOTFS_OPTIONS="-drive file=$ROOTFS,if=virtio,format=raw"
273 else
274 QEMU_NETWORK_CMD="-net nic,vlan=0 $QEMU_TAP_CMD"
275 DROOT="/dev/hda"
276 ROOTFS_OPTIONS="-drive file=$ROOTFS,if=ide,format=raw"
277 fi
278 if [ "$MACHINE" = "qemuarm64" ]; then
279 QEMU_NETWORK_CMD="-netdev tap,id=net0,ifname=$TAP,script=no,downscript=no -device virtio-net-device,netdev=net0 "
280 DROOT="/dev/vda"
281 ROOTFS_OPTIONS="-drive id=disk0,file=$ROOTFS,if=none,format=raw -device virtio-blk-device,drive=disk0"
282 fi
283
284 KERNCMDLINE="mem=$QEMU_MEMORY"
285 QEMU_UI_OPTIONS="-show-cursor -usb -usbdevice wacom-tablet"
286
287 NFS_INSTANCE=`echo $TAP | sed 's/tap//'`
288 export NFS_INSTANCE
289
290 SERIALOPTS=""
291 if [ "x$SERIAL_LOGFILE" != "x" ]; then
292 SERIALOPTS="-serial file:$SERIAL_LOGFILE"
293 fi
294fi
295
296case "$MACHINE" in
297 "qemuarm") ;;
298 "qemuarm64") ;;
299 "qemumicroblaze") ;;
300 "qemumips") ;;
301 "qemumipsel") ;;
302 "qemumips64") ;;
303 "qemush4") ;;
304 "qemuppc") ;;
305 "qemuarmv6") ;;
306 "qemuarmv7") ;;
307 "qemux86") ;;
308 "qemux86-64") ;;
309 "qemuzynq") ;;
310 "akita") ;;
311 "spitz") ;;
312 *)
313 echo "Error: Unsupported machine type $MACHINE"
314 return 1
315 ;;
316esac
317
318if [ ! -f "$KERNEL" -a "x$FSTYPE" != "xvmdk" -a "x$FSTYPE" != "xhddimg" -a "x$FSTYPE" != "xhdddirect" ]; then
319 echo "Error: Kernel image file $KERNEL doesn't exist"
320 cleanup
321 return 1
322fi
323
324if [ "$FSTYPE" != "nfs" -a "$FSTYPE" != "vmdk" -a "$FSTYPE" != "hddimg" -a "$FSTYPE" != "hdddirect" -a ! -f "$ROOTFS" ]; then
325 echo "Error: Image file $ROOTFS doesn't exist"
326 cleanup
327 return 1
328fi
329
330if [ "$NFS_SERVER" = "" ]; then
331 NFS_SERVER="192.168.7.1"
332 if [ "$SLIRP_ENABLED" = "yes" ]; then
333 NFS_SERVER="10.0.2.2"
334 fi
335fi
336
337if [ "$FSTYPE" = "nfs" ]; then
338 NFS_DIR=`echo $ROOTFS | sed 's/^[^:]*:\(.*\)/\1/'`
339 if [ "$NFS_INSTANCE" = "" ] ; then
340 NFS_INSTANCE=0
341 fi
342 MOUNTD_RPCPORT=$[ 21111 + $NFS_INSTANCE ]
343 NFSD_RPCPORT=$[ 11111 + $NFS_INSTANCE ]
344 NFSD_PORT=$[ 3049 + 2 * $NFS_INSTANCE ]
345 MOUNTD_PORT=$[ 3048 + 2 * $NFS_INSTANCE ]
346 UNFS_OPTS="nfsvers=3,port=$NFSD_PORT,mountprog=$MOUNTD_RPCPORT,nfsprog=$NFSD_RPCPORT,udp,mountport=$MOUNTD_PORT"
347
348 PSEUDO_LOCALSTATEDIR=~/.runqemu-sdk/pseudo
349 export PSEUDO_LOCALSTATEDIR
350
351 # Start the userspace NFS server
352 echo "runqemu-export-rootfs restart $ROOTFS"
353 runqemu-export-rootfs restart $ROOTFS
354 if [ $? != 0 ]; then
355 return 1
356 fi
357 NFSRUNNING="true"
358fi
359
360if [ "$MACHINE" = "qemuarm" -o "$MACHINE" = "qemuarmv6" -o "$MACHINE" = "qemuarmv7" ]; then
361 QEMU=qemu-system-arm
362 MACHINE_SUBTYPE=versatilepb
363 export QEMU_AUDIO_DRV="none"
364 QEMU_UI_OPTIONS="$QEMU_UI_OPTIONS"
365 # QEMU_UI_OPTIONS="$QEMU_UI_OPTIONS -force-pointer"
366 if [ "${FSTYPE:0:3}" = "ext" -o "$FSTYPE" = "btrfs" ]; then
367 KERNCMDLINE="root=/dev/sda rw console=ttyAMA0,115200 console=tty $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY highres=off"
368 QEMUOPTIONS="$QEMU_NETWORK_CMD -M ${MACHINE_SUBTYPE} -drive file=$ROOTFS,format=raw -no-reboot $QEMU_UI_OPTIONS"
369 fi
370 if [ "$FSTYPE" = "nfs" ]; then
371 if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then
372 echo "Error: NFS mount point $ROOTFS doesn't exist"
373 cleanup
374 return 1
375 fi
376 KERNCMDLINE="root=/dev/nfs nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw console=ttyAMA0,115200 $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY"
377 QEMUOPTIONS="$QEMU_NETWORK_CMD -M ${MACHINE_SUBTYPE} --no-reboot $QEMU_UI_OPTIONS"
378 fi
379 if [ "$MACHINE" = "qemuarmv6" ]; then
380 QEMUOPTIONS="$QEMUOPTIONS -cpu arm1136"
381 fi
382 if [ "$MACHINE" = "qemuarmv7" ]; then
383 QEMUOPTIONS="$QEMUOPTIONS -cpu cortex-a8"
384 fi
385fi
386
387if [ "$MACHINE" = "qemuarm64" ]; then
388 QEMU=qemu-system-aarch64
389
390 export QEMU_AUDIO_DRV="none"
391 if [ "x$SERIALSTDIO" = "x" ] ; then
392 QEMU_UI_OPTIONS="-nographic"
393 else
394 QEMU_UI_OPTIONS=""
395 fi
396 if [ "${FSTYPE:0:3}" = "ext" -o "$FSTYPE" = "btrfs" ]; then
397 KERNCMDLINE="root=/dev/vda rw console=ttyAMA0,38400 mem=$QEMU_MEMORY highres=off $KERNEL_NETWORK_CMD"
398 # qemu-system-aarch64 only support '-machine virt -cpu cortex-a57' for now
399 QEMUOPTIONS="$QEMU_NETWORK_CMD -machine virt -cpu cortex-a57 $ROOTFS_OPTIONS $QEMU_UI_OPTIONS"
400 fi
401 if [ "$FSTYPE" = "nfs" ]; then
402 if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then
403 echo "Error: NFS mount point $ROOTFS doesn't exist"
404 cleanup
405 return 1
406 fi
407 KERNCMDLINE="root=/dev/nfs nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw console=ttyAMA0,38400 mem=$QEMU_MEMORY highres=off $KERNEL_NETWORK_CMD"
408 QEMUOPTIONS="$QEMU_NETWORK_CMD -machine virt -cpu cortex-a57 $QEMU_UI_OPTIONS"
409 fi
410fi
411
412
413if [ "$MACHINE" = "qemux86" ]; then
414 QEMU=qemu-system-i386
415 if [ "$KVM_ACTIVE" = "yes" ]; then
416 CPU_SUBTYPE=kvm32
417 else
418 CPU_SUBTYPE=qemu32
419 fi
420 if [ ! -z "$vga_option" ]; then
421 QEMU_UI_OPTIONS="$QEMU_UI_OPTIONS"
422 else
423 QEMU_UI_OPTIONS="$QEMU_UI_OPTIONS -vga vmware"
424 fi
425 if [ "${FSTYPE:0:3}" = "ext" -o "$FSTYPE" = "btrfs" ]; then
426 KERNCMDLINE="vga=0 uvesafb.mode_option=640x480-32 root=$DROOT rw mem=$QEMU_MEMORY $KERNEL_NETWORK_CMD"
427 QEMUOPTIONS="$QEMU_NETWORK_CMD -cpu $CPU_SUBTYPE $ROOTFS_OPTIONS $QEMU_UI_OPTIONS"
428 fi
429 if [ "${FSTYPE:0:4}" = "cpio" ]; then
430 KERNCMDLINE="vga=0 uvesafb.mode_option=640x480-32 root=/dev/ram0 rw mem=$QEMU_MEMORY $KERNEL_NETWORK_CMD"
431 QEMUOPTIONS="$QEMU_NETWORK_CMD -cpu $CPU_SUBTYPE -initrd $ROOTFS $QEMU_UI_OPTIONS"
432 fi
433
434 if [ "$FSTYPE" = "nfs" ]; then
435 if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then
436 echo "Error: NFS mount point $ROOTFS doesn't exist."
437 cleanup
438 return 1
439 fi
440 KERNCMDLINE="root=/dev/nfs nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY"
441 QEMUOPTIONS="$QEMU_NETWORK_CMD $QEMU_UI_OPTIONS"
442 fi
443 if [ "$FSTYPE" = "vmdk" -o "$FSTYPE" = "hddimg" -o "$FSTYPE" = "hdddirect" ]; then
444 QEMUOPTIONS="$QEMU_NETWORK_CMD $QEMU_UI_OPTIONS"
445 fi
446 # Currently oprofile's event based interrupt mode doesn't work(Bug #828) in
447 # qemux86 and qemux86-64. We can use timer interrupt mode for now.
448 KERNCMDLINE="$KERNCMDLINE oprofile.timer=1"
449fi
450
451if [ "$MACHINE" = "qemux86-64" ]; then
452 QEMU=qemu-system-x86_64
453 if [ "$KVM_ACTIVE" = "yes" ]; then
454 CPU_SUBTYPE=kvm64
455 else
456 CPU_SUBTYPE=core2duo
457 fi
458 if [ ! -z "$vga_option" ]; then
459 QEMU_UI_OPTIONS="$QEMU_UI_OPTIONS"
460 else
461 QEMU_UI_OPTIONS="$QEMU_UI_OPTIONS -vga vmware"
462 fi
463 if [ "${FSTYPE:0:3}" = "ext" -o "$FSTYPE" = "btrfs" ]; then
464 KERNCMDLINE="vga=0 uvesafb.mode_option=640x480-32 root=$DROOT rw mem=$QEMU_MEMORY $KERNEL_NETWORK_CMD"
465 QEMUOPTIONS="$QEMU_NETWORK_CMD -cpu $CPU_SUBTYPE $ROOTFS_OPTIONS $QEMU_UI_OPTIONS"
466 fi
467 if [ "$FSTYPE" = "nfs" ]; then
468 if [ "x$ROOTFS" = "x" ]; then
469 ROOTFS=/srv/nfs/qemux86-64
470 fi
471 if [ ! -d "$ROOTFS" ]; then
472 echo "Error: NFS mount point $ROOTFS doesn't exist."
473 cleanup
474 return 1
475 fi
476 KERNCMDLINE="root=/dev/nfs nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY"
477 QEMUOPTIONS="$QEMU_NETWORK_CMD -cpu $CPU_SUBTYPE $QEMU_UI_OPTIONS"
478 fi
479 if [ "$FSTYPE" = "vmdk" -o "$FSTYPE" = "hddimg" -o "$FSTYPE" = "hdddirect" ]; then
480 QEMUOPTIONS="$QEMU_NETWORK_CMD -cpu $CPU_SUBTYPE $QEMU_UI_OPTIONS"
481 fi
482 # Currently oprofile's event based interrupt mode doesn't work(Bug #828) in
483 # qemux86 and qemux86-64. We can use timer interrupt mode for now.
484 KERNCMDLINE="$KERNCMDLINE oprofile.timer=1"
485fi
486
487if [ "$MACHINE" = "spitz" ]; then
488 QEMU=qemu-system-arm
489 if [ "${FSTYPE:0:3}" = "ext" -o "$FSTYPE" = "btrfs" ]; then
490 echo $ROOTFS
491 ROOTFS=`readlink -f $ROOTFS`
492 echo $ROOTFS
493 if [ ! -e "$ROOTFS.qemudisk" ]; then
494 echo "Adding a partition table to the ext3 image for use by QEMU, please wait..."
495 runqemu-addptable2image $ROOTFS $ROOTFS.qemudisk
496 fi
497 QEMUOPTIONS="$QEMU_NETWORK_CMD -M spitz -hda $ROOTFS.qemudisk -portrait"
498 fi
499fi
500
501if [ "$MACHINE" = "qemumips" -o "$MACHINE" = "qemumipsel" -o "$MACHINE" = "qemumips64" ]; then
502 case "$MACHINE" in
503 qemumips) QEMU=qemu-system-mips ;;
504 qemumipsel) QEMU=qemu-system-mipsel ;;
505 qemumips64) QEMU=qemu-system-mips64 ;;
506 esac
507 MACHINE_SUBTYPE=malta
508 QEMU_UI_OPTIONS="-vga cirrus $QEMU_UI_OPTIONS"
509 if [ "${FSTYPE:0:3}" = "ext" -o "$FSTYPE" = "btrfs" ]; then
510 #KERNCMDLINE="root=/dev/hda console=ttyS0 console=tty0 $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY"
511 KERNCMDLINE="root=/dev/hda rw console=ttyS0 console=tty $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY"
512 QEMUOPTIONS="$QEMU_NETWORK_CMD -M $MACHINE_SUBTYPE -drive file=$ROOTFS,format=raw -no-reboot $QEMU_UI_OPTIONS"
513 fi
514 if [ "$FSTYPE" = "nfs" ]; then
515 if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then
516 echo "Error: NFS mount point $ROOTFS doesn't exist"
517 cleanup
518 return 1
519 fi
520 KERNCMDLINE="root=/dev/nfs console=ttyS0 console=tty nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY"
521 QEMUOPTIONS="$QEMU_NETWORK_CMD -M $MACHINE_SUBTYPE -no-reboot $QEMU_UI_OPTIONS"
522 fi
523fi
524
525if [ "$MACHINE" = "qemuppc" ]; then
526 QEMU=qemu-system-ppc
527 MACHINE_SUBTYPE=mac99
528 CPU_SUBTYPE=G4
529 QEMU_UI_OPTIONS="$QEMU_UI_OPTIONS"
530 if [ "$SLIRP_ENABLED" = "yes" ]; then
531 QEMU_NETWORK_CMD=""
532 else
533 QEMU_NETWORK_CMD="-net nic,model=pcnet $QEMU_TAP_CMD"
534 fi
535 if [ "${FSTYPE:0:3}" = "ext" -o "$FSTYPE" = "btrfs" ]; then
536 KERNCMDLINE="root=/dev/hda rw console=ttyS0 console=tty $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY"
537 QEMUOPTIONS="$QEMU_NETWORK_CMD -cpu $CPU_SUBTYPE -M $MACHINE_SUBTYPE -drive file=$ROOTFS,format=raw -no-reboot $QEMU_UI_OPTIONS"
538 fi
539 if [ "$FSTYPE" = "nfs" ]; then
540 if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then
541 echo "Error: NFS mount point $ROOTFS doesn't exist"
542 cleanup
543 return 1
544 fi
545 KERNCMDLINE="root=/dev/nfs console=ttyS0 console=tty nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY"
546 QEMUOPTIONS="$QEMU_NETWORK_CMD -cpu $CPU_SUBTYPE -M $MACHINE_SUBTYPE -no-reboot $QEMU_UI_OPTIONS"
547 fi
548fi
549
550if [ "$MACHINE" = "qemush4" ]; then
551 QEMU=qemu-system-sh4
552 MACHINE_SUBTYPE=r2d
553 QEMU_UI_OPTIONS="$QEMU_UI_OPTIONS"
554 if [ "${FSTYPE:0:3}" = "ext" -o "$FSTYPE" = "btrfs" ]; then
555 #KERNCMDLINE="root=/dev/hda console=ttyS0 console=tty0 $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY"
556 KERNCMDLINE="root=/dev/hda rw console=ttySC1 noiotrap earlyprintk=sh-sci.1 console=tty $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY"
557 QEMUOPTIONS="$QEMU_NETWORK_CMD -M $MACHINE_SUBTYPE -hda $ROOTFS -no-reboot $QEMU_UI_OPTIONS -monitor null -serial vc -serial stdio"
558 SERIALSTDIO="1"
559 fi
560 if [ "$FSTYPE" = "nfs" ]; then
561 if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then
562 echo "Error: NFS mount point $ROOTFS doesn't exist"
563 cleanup
564 return 1
565 fi
566 KERNCMDLINE="root=/dev/nfs console=ttySC1 noiotrap earlyprintk=sh-sci.1 console=tty nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY"
567 QEMUOPTIONS="$QEMU_NETWORK_CMD -M $MACHINE_SUBTYPE -no-reboot $QEMU_UI_OPTIONS -monitor null -serial vc -serial stdio"
568 SERIALSTDIO="1"
569 fi
570fi
571
572if [ "${FSTYPE:0:3}" = "ext" ]; then
573 KERNCMDLINE="$KERNCMDLINE rootfstype=$FSTYPE"
574fi
575
576if [ "$MACHINE" = "akita" ]; then
577 QEMU=qemu-system-arm
578 if [ "$FSTYPE" = "jffs2" ]; then
579 ROOTFS=`readlink -f $ROOTFS`
580 if [ ! -e "$ROOTFS.qemuflash" ]; then
581 echo "Converting raw image into flash image format for use by QEMU, please wait..."
582 raw2flash.akita < $ROOTFS > $ROOTFS.qemuflash
583 fi
584 QEMUOPTIONS="$QEMU_NETWORK_CMD -M akita -mtdblock $ROOTFS.qemuflash -portrait"
585 fi
586fi
587
588if [ "$MACHINE" = "qemumicroblaze" ]; then
589 QEMU=qemu-system-microblazeel
590 QEMU_SYSTEM_OPTIONS="-M petalogix-ml605 -serial mon:stdio -dtb $KERNEL-$MACHINE.dtb"
591 if [ "${FSTYPE:0:3}" = "ext" -o "${FSTYPE:0:4}" = "cpio" ]; then
592 KERNCMDLINE="earlyprintk root=/dev/ram rw"
593 QEMUOPTIONS="$QEMU_SYSTEM_OPTIONS -initrd $ROOTFS"
594 fi
595fi
596
597if [ "$MACHINE" = "qemuzynq" ]; then
598 QEMU=qemu-system-arm
599 QEMU_SYSTEM_OPTIONS="-M xilinx-zynq-a9 -serial null -serial mon:stdio -dtb $KERNEL-$MACHINE.dtb"
600 # zynq serial ports are named 'ttyPS0' and 'ttyPS1', fixup the default values
601 SCRIPT_KERNEL_OPT=$(echo "$SCRIPT_KERNEL_OPT" | sed 's/console=ttyS/console=ttyPS/g')
602 if [ "${FSTYPE:0:3}" = "ext" -o "${FSTYPE:0:4}" = "cpio" ]; then
603 KERNCMDLINE="earlyprintk root=/dev/ram rw"
604 QEMUOPTIONS="$QEMU_SYSTEM_OPTIONS -initrd $ROOTFS"
605 fi
606fi
607
608if [ "x$RAMFS" = "xtrue" ]; then
609 QEMUOPTIONS="-initrd $ROOTFS -nographic"
610 KERNCMDLINE="root=/dev/ram0 console=ttyS0 debugshell"
611fi
612
613if [ "x$ISOFS" = "xtrue" ]; then
614 QEMUOPTIONS="$QEMU_NETWORK_CMD -cdrom $ROOTFS $QEMU_UI_OPTIONS"
615fi
616
617if [ "x$QEMUOPTIONS" = "x" ]; then
618 echo "Error: Unable to support this combination of options"
619 cleanup
620 return 1
621fi
622
623if [ "$TCPSERIAL_PORTNUM" != "" ]; then
624 if [ "$MACHINE" = "qemuarm64" ]; then
625 SCRIPT_QEMU_EXTRA_OPT="$SCRIPT_QEMU_EXTRA_OPT -device virtio-serial-device -chardev socket,id=virtcon,port=$TCPSERIAL_PORTNUM,host=127.0.0.1 -device virtconsole,chardev=virtcon"
626 else
627 SCRIPT_QEMU_EXTRA_OPT="$SCRIPT_QEMU_EXTRA_OPT -serial tcp:127.0.0.1:$TCPSERIAL_PORTNUM"
628 fi
629fi
630
631PATH=$OECORE_NATIVE_SYSROOT/usr/bin:$PATH
632
633QEMUBIN=`which $QEMU 2> /dev/null`
634if [ ! -x "$QEMUBIN" ]; then
635 echo "Error: No QEMU binary '$QEMU' could be found."
636 cleanup
637 return 1
638fi
639
640NEED_GL=`ldd $QEMUBIN/$QEMU 2>&1 | grep libGLU`
641# We can't run without a libGL.so
642if [ "$NEED_GL" != "" ]; then
643 libgl='no'
644
645 [ -e /usr/lib/libGL.so -a -e /usr/lib/libGLU.so ] && libgl='yes'
646 [ -e /usr/lib64/libGL.so -a -e /usr/lib64/libGLU.so ] && libgl='yes'
647 [ -e /usr/lib/*-linux-gnu/libGL.so -a -e /usr/lib/*-linux-gnu/libGLU.so ] && libgl='yes'
648
649 if [ "$libgl" != 'yes' ]; then
650 echo "You need libGL.so and libGLU.so to exist in your library path to run the QEMU emulator.
651 Ubuntu package names are: libgl1-mesa-dev and libglu1-mesa-dev.
652 Fedora package names are: mesa-libGL-devel mesa-libGLU-devel."
653 return 1;
654 fi
655fi
656
657do_quit() {
658 cleanup
659 return 1
660}
661
662trap do_quit INT TERM QUIT
663
664# qemu got segfault if linked with nVidia's libgl
665GL_LD_PRELOAD=$LD_PRELOAD
666
667if ldd $QEMUBIN | grep -i nvidia &> /dev/null
668then
669cat << EOM
670WARNING: nVidia proprietary OpenGL libraries detected.
671nVidia's OpenGL libraries are known to have compatibility issues with qemu,
672resulting in a segfault. Please uninstall these drivers or ensure the mesa libGL
673libraries precede nvidia's via LD_PRELOAD(Already do it on Ubuntu 10).
674EOM
675
676# Automatically use Ubuntu system's mesa libGL, other distro can add its own path
677if grep -i ubuntu /etc/lsb-release &> /dev/null
678then
679 # precede nvidia's driver on Ubuntu 10
680 UBUNTU_MAIN_VERSION=`cat /etc/lsb-release |grep DISTRIB_RELEASE |cut -d= -f 2| cut -d. -f 1`
681 if [ "$UBUNTU_MAIN_VERSION" = "10" ];
682 then
683 GL_PATH=""
684 if test -e /usr/lib/libGL.so
685 then
686 GL_PATH="/usr/lib/libGL.so"
687 elif test -e /usr/lib/x86_64-linux-gnu/libGL.so
688 then
689 GL_PATH="/usr/lib/x86_64-linux-gnu/libGL.so"
690 fi
691
692 echo "Skip nVidia's libGL on Ubuntu 10!"
693 GL_LD_PRELOAD="$GL_PATH $LD_PRELOAD"
694 fi
695fi
696fi
697
698if [ "x$SERIALSTDIO" = "x1" ]; then
699 echo "Interrupt character is '^]'"
700 stty intr ^]
701fi
702
703echo "Running $QEMU..."
704# -no-reboot is a mandatory option - see bug #100
705if [ "$FSTYPE" = "vmdk" -o "$FSTYPE" = "hddimg" -o "$FSTYPE" = "hdddirect" ]; then
706 echo $QEMUBIN $VM $QEMUOPTIONS $SERIALOPTS -no-reboot $SCRIPT_QEMU_OPT $SCRIPT_QEMU_EXTRA_OPT
707 LD_PRELOAD="$GL_LD_PRELOAD" $QEMUBIN $VM $QEMUOPTIONS $SERIALOPTS -no-reboot $SCRIPT_QEMU_OPT $SCRIPT_QEMU_EXTRA_OPT
708elif [ "$FSTYPE" = "iso" ]; then
709 echo $QEMUBIN $QEMUOPTIONS $SERIALOPTS -no-reboot $SCRIPT_QEMU_OPT $SCRIPT_QEMU_EXTRA_OPT
710 LD_PRELOAD="$GL_LD_PRELOAD" $QEMUBIN $QEMUOPTIONS $SERIALOPTS -no-reboot $SCRIPT_QEMU_OPT $SCRIPT_QEMU_EXTRA_OPT
711else
712 echo $QEMUBIN -kernel $KERNEL $QEMUOPTIONS $SLIRP_CMD $SERIALOPTS -no-reboot $SCRIPT_QEMU_OPT $SCRIPT_QEMU_EXTRA_OPT --append '"'$KERNCMDLINE $SCRIPT_KERNEL_OPT'"'
713 LD_PRELOAD="$GL_LD_PRELOAD" $QEMUBIN -kernel $KERNEL $QEMUOPTIONS $SERIALOPTS -no-reboot $SCRIPT_QEMU_OPT $SCRIPT_QEMU_EXTRA_OPT --append "$KERNCMDLINE $SCRIPT_KERNEL_OPT"
714fi
715ret=$?
716if [ "$SLIRP_ENABLED" != "yes" ]; then
717 cleanup
718fi
719
720#set the original stty values before exit
721stty ${ORIG_STTY}
722trap - INT TERM QUIT
723
724return $ret