-
Notifications
You must be signed in to change notification settings - Fork 0
/
boot-sbsa-ref.py
executable file
·234 lines (180 loc) · 6.76 KB
/
boot-sbsa-ref.py
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
#!/usr/bin/python3
import argparse
import os
import random
import string
import sys
import yaml
from pprint import pprint
def parse_args():
parser = argparse.ArgumentParser(description="Run virtual machine.")
parser.add_argument("--cmd", help="command to run in EFI shell")
parser.add_argument("--cpu", default="neoverse-v1",
help="cpu core to use")
parser.add_argument("--gdb", action="store_true",
help="wait for GDB connection")
parser.add_argument("--gfx", action="store_true",
help="open window with graphics output")
parser.add_argument("--no-its", action="store_true",
help="disable extra PCIe devices")
parser.add_argument("--no-reset", action="store_true",
help="do not shutdown after running EFI command")
parser.add_argument("--numa", action="store_true",
help="use NUMA configation")
parser.add_argument("--os",
help="select OS to attach")
parser.add_argument("--secure", action="store_true",
help="")
parser.add_argument("--smp", help="amount of cpu cores", default=4)
parser.add_argument("--virt", action="store_true",
help='use "virt" instead of "sbsa-ref"')
return parser.parse_args()
def handle_uefi_command(command, no_reset):
try:
os.remove("disks/virtual/startup.nsh")
except FileNotFoundError:
pass
with open("disks/virtual/startup.nsh", "w") as startup:
startup.write(f"""
mode 100 31
pci
{command}
""")
# we shutdown by default
if not no_reset:
startup.write("reset -c\n")
def add_drive(drive_file, drive_type="", media="", drive_format="raw"):
drive_extra = ""
if drive_type:
drive_extra = f",if={drive_type}"
if media:
drive_extra += ",media=cdrom"
qemu_args.extend(["-drive",
f"file={drive_file},format={drive_format}{drive_extra}"])
def add_firmware(is_it_virt):
if is_it_virt:
add_drive("firmware/VIRT_FLASH0.fd", "pflash")
add_drive("firmware/VIRT_FLASH1.fd", "pflash")
else:
add_drive("firmware/SBSA_FLASH0.fd", "pflash")
add_drive("firmware/SBSA_FLASH1.fd", "pflash")
def add_machine(is_it_virt):
if is_it_virt:
qemu_args.extend(["-machine", "virt,iommu=smmuv3,gic-version=max"])
# virt does not have USB host controller
add_device("qemu-xhci,id=usb")
else:
qemu_args.extend(["-machine", "sbsa-ref"])
def add_cpu(cpu_type, is_it_numa, smp):
qemu_args.extend(["-cpu", cpu_type])
if not is_it_numa:
qemu_args.extend(["-smp", str(smp), "-m", "4G"])
else:
qemu_args.extend([
"-smp", f"{smp},sockets={smp},maxcpus={smp}",
"-m", "4G,slots=2,maxmem=5G",
"-object", "memory-backend-ram,size=1G,id=m0",
"-object", "memory-backend-ram,size=3G,id=m1",
"-numa", "node,nodeid=0,cpus=0-1,memdev=m0",
"-numa", "node,nodeid=1,cpus=2,memdev=m1",
"-numa", "node,nodeid=2,cpus=3"])
qemu_args.extend([
"-device", "pxb-pcie,id=pxb1,bus_nr=64,numa_node=1",
"-device", "pcie-root-port,id=root_port3,bus=pxb1,chassis=11,slot=0",
"-device", "sdhci-pci,bus=root_port3,id=sdhci",
"-device", "pcie-root-port,id=root_port4,bus=pxb1,chassis=12,slot=0",
"-device", "bochs-display,bus=root_port4,id=bochs",
"-device", "pxb-pcie,id=pxb2,bus_nr=128,numa_node=2,bus=pcie.0",
"-device", "pcie-pci-bridge,id=pci,bus=pxb2",
"-device", "es1370,bus=pci,addr=9,id=es1370",
# "-device", "e1000,bus=pci,addr=10,id=e1000",
"-device", "pcie-root-port,id=root_port_for_ahci,bus=pxb2,chassis=10,slot=0",
"-device", "ahci,bus=root_port_for_ahci,id=ahci"
])
def add_device(device_name):
qemu_args.extend(["-device", device_name])
def add_usb_devices():
add_device("usb-kbd")
add_device("usb-tablet")
# FreeBSD prefers mouse over tablet
add_device("usb-mouse")
def add_os_drive(os_name):
if os_name is None:
return
with open("os.yml") as yml:
os_data = yaml.safe_load(yml)
if os_name not in os_data:
print(f"Selected OS: {os_name} is not defined!")
sys.exit(-1)
if os_data[os_name]["file"].endswith(".iso"):
add_drive(os_data[os_name]["file"], "", "cdrom")
elif os_data[os_name]["file"].endswith(".qcow2"):
add_drive(os_data[os_name]["file"], drive_format="qcow2")
else:
add_drive(os_data[os_name]["file"])
if "reset" in os_data[os_name] and os_data[os_name]["reset"]:
qemu_args.append("--no-reboot")
def enable_graphics_window(is_gfx):
if is_gfx:
qemu_args.extend(["-display", "gtk,show-tabs=on"])
else:
qemu_args.append("-nographic")
def enable_gdb(is_gdb):
if is_gdb:
qemu_args.extend(["-s", "-S"])
def show_args():
prev_arg = qemu_args[0]
print("QEMU command arguments:\n")
for arg in qemu_args:
try:
if arg.startswith("-") and prev_arg.startswith("-"):
print(prev_arg)
elif arg.startswith("-"):
pass
elif prev_arg.startswith("-"):
print(f"{prev_arg} {arg}")
else:
print(arg)
prev_arg = arg
except AttributeError:
print(f"ERROR: prev_arg:{prev_arg} arg:{arg}")
print("", flush=True)
def add_pcie(card_name, add_root_port=True):
global chassis
bus = ""
if add_root_port:
rpid = "".join(random.choices(string.ascii_letters, k=5))
qemu_args.extend(["-device",
f"pcie-root-port,id={rpid},slot=0,chassis={chassis}"])
chassis += 1
bus = f",bus={rpid}"
qemu_args.extend(["-device", f"{card_name}{bus}"])
# -----------------------------------------------------------------------------
args = parse_args()
chassis = 0
if args.cmd:
handle_uefi_command(args.cmd, args.no_reset)
qemu_args = [
"../code/qemu/build/qemu-system-aarch64",
"-monitor", "telnet::45454,server,nowait",
"-serial", "stdio"
]
add_firmware(args.virt)
add_machine(args.virt)
add_cpu(args.cpu, args.numa, args.smp)
add_usb_devices()
enable_graphics_window(args.gfx)
enable_gdb(args.gdb)
add_os_drive(args.os)
# full Debian installation
add_drive("disks/full-debian.hddimg")
if not args.virt:
# virtual drive with EFI tools
add_drive("fat:rw:disks/virtual")
#add_pcie("igb")
show_args()
os.execv(qemu_args[0], qemu_args)
try:
os.remove("disks/virtual/startup.nsh")
except FileNotFoundError:
pass