summaryrefslogtreecommitdiffstats
path: root/configs/platform-mipsel/run
blob: 9ecf65b2e429a7e7397be8751d88a6f74671ff06 (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
#!/bin/bash

platformconfig=$(dirname $0)/platformconfig

#
# we need information about the platform
#
if [ ! -e "$platformconfig" ]; then
	echo "error: ${platformconfig} does not exist"
	exit 1
fi

source $platformconfig

if [ -n "${PTXCONF_PLATFORM}" ]; then
	PTXDIST_PLATFORMDIR="./platform-${PTXCONF_PLATFORM}"
else
	PTXDIST_PLATFORMDIR="."
fi

if [ ! -e "${PTXDIST_PLATFORMDIR}/images/vmlinuz-malta" ]; then
	echo "error: run 'ptxdist go' first"
	exit 1
fi

# the emulator to run
QEMU_EXEC="${PTXDIST_PLATFORMDIR}/sysroot-host/bin/qemu-system-mipsel"

if [ ! -e "${QEMU_EXEC}" ]; then
	echo "error: enable and install 'host-qemu' first"
	exit 1
fi

# the port to which an sshd would connect (in the emulated system)
SSH_INTERNAL_PORT=22
# the port which QEMU opens at the host side to give access to the ${SSH_INTERNAL_PORT}
SSH_EXTERNAL_PORT=${QEMU_SSH_PORT:-$((PPID%64000+1025))}

# check if vde is available for networking
if [ -z "${VDE_SOCKET}" ]; then
	for dir in $(ls -d /var/run/vde2/*.ctl 2>/dev/null); do
		if [ -r "${dir}" ]; then
			VDE_SOCKET="${dir}"
		fi
	done
fi
if [ -n "${VDE_SOCKET}" ]; then
	# make sure qemu supports vde networking
	if ${QEMU_EXEC} --help | grep -q -- '-net.*vde'; then
		QEMU_NET=( -netdev vde,id=net1,sock=${VDE_SOCKET} )
	fi
fi

# fall back to user network if necessary
if [ -z "${QEMU_NET}" ]; then
	QEMU_NET=(-netdev user,id=net1,hostfwd=tcp:127.0.0.1:${SSH_EXTERNAL_PORT}-:${SSH_INTERNAL_PORT},hostfwd=tcp:127.0.0.1:12345-:12345)
	echo "Forwarding SSH port 127.0.0.1:${SSH_EXTERNAL_PORT} -> qemu:${SSH_INTERNAL_PORT}"
fi

BASE_CMDLINE="console=ttyS0,115200"

# Machine to emulate
QEMU_ARGS=( -M malta -m 256 -serial stdio -monitor null -device qemu-xhci,id=xhci)
# disable graphics output
QEMU_ARGS[${#QEMU_ARGS[@]}]="-nographic"
# Exit qemu on reboot
QEMU_ARGS[${#QEMU_ARGS[@]}]="-no-reboot"
# Configure networking
QEMU_ARGS=( "${QEMU_ARGS[@]}" -net nic,netdev=net1 "${QEMU_NET[@]}" )
# Set base time to test NTP and time handling
QEMU_ARGS=( "${QEMU_ARGS[@]}" -rtc base=2021-01-01 )

QEMU_LINUX_ARGS=( -kernel ${PTXDIST_PLATFORMDIR}/images/vmlinuz-malta -dtb ${PTXDIST_PLATFORMDIR}/images/qemu-malta.dtb-bb )
# the barebox device tree has a state node for bootchooser
QEMU_BAREBOX_ARGS=( -bios  ${PTXDIST_PLATFORMDIR}/images/barebox-qemu-malta.img )

check_hd() {
	if [ ! -e "${PTXDIST_PLATFORMDIR}/images/malta.hdimg" ]; then
		echo "error: malta.hdimg is missing. Run 'ptxdist images' first"
		exit 1
	fi
}

run_qemu_nfs() {
	exec ${QEMU_EXEC} \
		"${QEMU_ARGS[@]}" \
		"${QEMU_EXTRA_ARGS[@]}" \
		"${QEMU_LINUX_ARGS[@]}" \
		-append "root=/dev/nfs nfsroot=/root,v3,tcp,port=2049,mountport=2049 ip=dhcp ${BASE_CMDLINE}"
}

run_qemu_hda() {
	check_hd
	exec ${QEMU_EXEC} \
		"${QEMU_ARGS[@]}" \
		-drive file=${PTXDIST_PLATFORMDIR}/images/malta.hdimg,index=0,media=disk,format=raw \
		"${QEMU_EXTRA_ARGS[@]}" \
		"${QEMU_LINUX_ARGS[@]}" \
		-append "root=/dev/sda1 rootfstype=ext4 rootwait ${BASE_CMDLINE}"
}

run_qemu_barebox() {
	check_hd
	exec ${QEMU_EXEC} \
		"${QEMU_ARGS[@]}" \
		"${QEMU_EXTRA_ARGS[@]}" \
		"${QEMU_BAREBOX_ARGS[@]}"
}

target="${1:-hda}"

#set -x
run_qemu_${target}