source: mainline/tools/ew.py@ 241ab7e

serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 241ab7e was e9f7778, checked in by Jakub Jermář <jakub@…>, 4 years ago

tools/ew.py: Add support for Raspberry Pi

  • Property mode set to 100755
File size: 15.0 KB
Line 
1#!/usr/bin/env python3
2#
3# Copyright (c) 2013 Jakub Jermar
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#
10# - Redistributions of source code must retain the above copyright
11# notice, this list of conditions and the following disclaimer.
12# - Redistributions in binary form must reproduce the above copyright
13# notice, this list of conditions and the following disclaimer in the
14# documentation and/or other materials provided with the distribution.
15# - The name of the author may not be used to endorse or promote products
16# derived from this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28#
29
30
31"""
32Emulator wrapper for running HelenOS
33"""
34
35import inspect
36import os
37import platform
38import re
39import subprocess
40import sys
41import _thread
42import time
43
44overrides = {}
45
46CONFIG = 'Makefile.config'
47
48TOOLS_DIR = os.path.dirname(inspect.getabsfile(inspect.currentframe()))
49
50def read_config():
51 "Read HelenOS build configuration"
52
53 inf = open(CONFIG, 'r')
54 config = {}
55
56 for line in inf:
57 res = re.match(r'^(?:#!# )?([^#]\w*)\s*=\s*(.*?)\s*$', line)
58 if (res):
59 config[res.group(1)] = res.group(2)
60
61 inf.close()
62 return config
63
64def is_override(str):
65 if str in overrides.keys():
66 return overrides[str]
67 return False
68
69def cfg_get(platform, machine, processor):
70 if machine == "" or "run" in emulators[platform]:
71 return emulators[platform]
72 elif processor == "" or "run" in emulators[platform][machine]:
73 return emulators[platform][machine]
74 else:
75 return emulators[platform][machine][processor]
76
77def termemu_detect():
78 emus = ['gnome-terminal', 'xfce4-terminal', 'xterm']
79 for termemu in emus:
80 try:
81 subprocess.check_output('which ' + termemu, shell = True)
82 return termemu
83 except:
84 pass
85
86 print('Could not find any of the terminal emulators %s.'%(emus))
87 sys.exit(1)
88
89def run_in_console(cmd, title):
90 temu = termemu_detect()
91 if temu == 'gnome-terminal':
92 cmdline = temu + ' -- ' + cmd
93 else:
94 ecmd = cmd.replace('"', '\\"')
95 cmdline = temu + ' -T ' + '"' + title + '"' + ' -e "' + ecmd + '"'
96
97 print(cmdline)
98 if not is_override('dryrun'):
99 subprocess.call(cmdline, shell = True)
100
101def get_host_native_width():
102 return int(platform.architecture()[0].strip('bit'))
103
104def pc_options(guest_width):
105 opts = ''
106
107 # Do not enable KVM if running 64 bits HelenOS
108 # on 32 bits host
109 host_width = get_host_native_width()
110 if guest_width <= host_width and not is_override('nokvm'):
111 opts = opts + ' -enable-kvm'
112
113 # Remove the leading space
114 return opts[1:]
115
116def malta_options():
117 return '-cpu 4Kc -append "console=devices/\\hw\\pci0\\00:0a.0\\com1\\a"'
118
119def find_firmware(name, environ_var, default_paths, extra_info=None):
120 """Find firmware image(s)."""
121
122 if environ_var in os.environ:
123 return os.environ[environ_var]
124
125 for path in default_paths:
126 if os.path.exists(path):
127 return path
128
129 sys.stderr.write("Cannot find %s binary image(s)!\n" % name)
130 sys.stderr.write(
131 "Either set %s environment variable accordingly or place the image(s) in one of the default locations: %s.\n" %
132 (environ_var, ", ".join(default_paths)))
133 if extra_info is not None:
134 sys.stderr.write(extra_info)
135 return None
136
137def platform_to_qemu_options(platform, machine, processor):
138 if platform == 'amd64':
139 return 'system-x86_64', pc_options(64)
140 elif platform == 'arm32':
141 if machine == 'integratorcp':
142 return 'system-arm', '-M integratorcp'
143 elif machine == 'raspberrypi':
144 return 'system-arm', '-M raspi1ap'
145 elif platform == 'arm64':
146 # Search for the EDK2 firmware image
147 default_paths = (
148 '/usr/local/qemu-efi-aarch64/QEMU_EFI.fd', # Custom
149 '/usr/share/edk2/aarch64/QEMU_EFI.fd', # Fedora
150 '/usr/share/qemu-efi-aarch64/QEMU_EFI.fd', # Ubuntu
151 )
152 extra_info = ("Pre-compiled binary can be obtained from "
153 "http://snapshots.linaro.org/components/kernel/leg-virt-tianocore-edk2-upstream/latest/QEMU-AARCH64/RELEASE_GCC5/QEMU_EFI.fd.\n")
154 efi_path = find_firmware(
155 "EDK2", 'EW_QEMU_EFI_AARCH64', default_paths, extra_info)
156 if efi_path is None:
157 raise Exception
158
159 return 'system-aarch64', \
160 '-M virt -cpu cortex-a57 -m 1024 -bios %s' % efi_path
161 elif platform == 'ia32':
162 return 'system-i386', pc_options(32)
163 elif platform == 'mips32':
164 if machine == 'lmalta':
165 return 'system-mipsel', malta_options()
166 elif machine == 'bmalta':
167 return 'system-mips', malta_options()
168 elif platform == 'ppc32':
169 return 'system-ppc', '-m 256'
170 elif platform == 'sparc64':
171 if machine != 'generic':
172 raise Exception
173 if processor == 'us':
174 return 'system-sparc64', '-M sun4u --prom-env boot-args="console=devices/\\hw\\pci0\\01:01.0\\com1\\a"'
175
176 # processor = 'sun4v'
177 opensparc_bins = find_firmware(
178 "OpenSPARC", 'OPENSPARC_BINARIES',
179 ('/usr/local/opensparc/image/', ))
180 if opensparc_bins is None:
181 raise Exception
182
183 return 'system-sparc64', '-M niagara -m 256 -L %s' % (opensparc_bins)
184
185
186def hdisk_mk():
187 if not os.path.exists('hdisk.img'):
188 subprocess.call(TOOLS_DIR + '/mkfat.py 1048576 dist/data hdisk.img', shell = True)
189
190def qemu_bd_options():
191 if is_override('nohdd'):
192 return ''
193
194 hdisk_mk()
195
196 hdd_options = ''
197 if 'hdd' in overrides.keys():
198 if 'ata' in overrides['hdd'].keys():
199 hdd_options += ''
200 elif 'virtio-blk' in overrides['hdd'].keys():
201 hdd_options += ',if=virtio'
202
203 return ' -drive file=hdisk.img,index=0,media=disk,format=raw' + hdd_options
204
205def qemu_nic_ne2k_options():
206 return ' -device ne2k_isa,irq=5,netdev=n1'
207
208def qemu_nic_e1k_options():
209 return ' -device e1000,netdev=n1'
210
211def qemu_nic_rtl8139_options():
212 return ' -device rtl8139,netdev=n1'
213
214def qemu_nic_virtio_options():
215 return ' -device virtio-net,netdev=n1'
216
217def qemu_net_options():
218 if is_override('nonet'):
219 return ''
220
221 nic_options = ''
222 if 'net' in overrides.keys():
223 if 'e1k' in overrides['net'].keys():
224 nic_options += qemu_nic_e1k_options()
225 if 'rtl8139' in overrides['net'].keys():
226 nic_options += qemu_nic_rtl8139_options()
227 if 'ne2k' in overrides['net'].keys():
228 nic_options += qemu_nic_ne2k_options()
229 if 'virtio-net' in overrides['net'].keys():
230 nic_options += qemu_nic_virtio_options()
231 else:
232 # Use the default NIC
233 nic_options += qemu_nic_e1k_options()
234
235 return nic_options + ' -netdev user,id=n1,hostfwd=udp::8080-:8080,hostfwd=udp::8081-:8081,hostfwd=tcp::8080-:8080,hostfwd=tcp::8081-:8081,hostfwd=tcp::2223-:2223'
236
237def qemu_usb_options():
238 if is_override('nousb'):
239 return ''
240 return ' -usb'
241
242def qemu_xhci_options():
243 if is_override('noxhci'):
244 return ''
245 return ' -device nec-usb-xhci,id=xhci'
246
247def qemu_tablet_options():
248 if is_override('notablet') or (is_override('nousb') and is_override('noxhci')):
249 return ''
250 return ' -device usb-tablet'
251
252def qemu_audio_options():
253 if is_override('nosnd'):
254 return ''
255 return ' -device intel-hda -device hda-duplex'
256
257def qemu_run(platform, machine, processor):
258 cfg = cfg_get(platform, machine, processor)
259 suffix, options = platform_to_qemu_options(platform, machine, processor)
260 cmd = 'qemu-' + suffix
261
262 cmdline = cmd
263 if 'qemu_path' in overrides.keys():
264 cmdline = overrides['qemu_path'] + cmd
265
266 if options != '':
267 cmdline += ' ' + options
268
269 if (not 'hdd' in cfg.keys() or cfg['hdd']):
270 cmdline += qemu_bd_options()
271 if (not 'net' in cfg.keys()) or cfg['net']:
272 cmdline += qemu_net_options()
273 if (not 'usb' in cfg.keys()) or cfg['usb']:
274 cmdline += qemu_usb_options()
275 if (not 'xhci' in cfg.keys()) or cfg['xhci']:
276 cmdline += qemu_xhci_options()
277 if (not 'tablet' in cfg.keys()) or cfg['tablet']:
278 cmdline += qemu_tablet_options()
279 if (not 'audio' in cfg.keys()) or cfg['audio']:
280 cmdline += qemu_audio_options()
281
282 console = ('console' in cfg.keys() and cfg['console'])
283
284 if (is_override('nographic')):
285 cmdline += ' -nographic'
286
287 if (not console and (not is_override('nographic')) and not is_override('noserial')):
288 cmdline += ' -serial stdio'
289
290 if (is_override('bigmem')):
291 cmdline += ' -m 4G'
292
293 if cfg['image'] == 'image.iso':
294 cmdline += ' -boot d -cdrom image.iso'
295 elif cfg['image'] == 'image.iso@arm64':
296 # Define image.iso cdrom backend.
297 cmdline += ' -drive if=none,file=image.iso,id=cdrom,media=cdrom'
298 # Define scsi bus.
299 cmdline += ' -device virtio-scsi-device'
300 # Define cdrom frontend connected to this scsi bus.
301 cmdline += ' -device scsi-cd,drive=cdrom'
302 elif cfg['image'] == 'image.boot':
303 cmdline += ' -kernel image.boot'
304 elif cfg['image'] == 'kernel.img@rpi':
305 cmdline += ' -bios boot/image.boot.bin'
306 else:
307 cmdline += ' ' + cfg['image']
308
309 if console:
310 cmdline += ' -nographic'
311
312 title = 'HelenOS/' + platform
313 if machine != '':
314 title += ' on ' + machine
315 if 'expect' in cfg.keys():
316 cmdline = 'expect -c \'spawn %s; expect "%s" { send "%s" } timeout exp_continue; interact\'' % (cmdline, cfg['expect']['src'], cfg['expect']['dst'])
317 run_in_console(cmdline, title)
318 else:
319 print(cmdline)
320 if not is_override('dryrun'):
321 subprocess.call(cmdline, shell = True)
322
323def ski_run(platform, machine, processor):
324 run_in_console('ski -i ' + TOOLS_DIR + '/conf/ski.conf', 'HelenOS/ia64 on ski')
325
326def msim_run(platform, machine, processor):
327 hdisk_mk()
328 run_in_console('msim -c ' + TOOLS_DIR + '/conf/msim.conf', 'HelenOS/mips32 on msim')
329
330def spike_run(platform, machine, processor):
331 run_in_console('spike -m1073741824:1073741824 image.boot', 'HelenOS/risvc64 on Spike')
332
333emulators = {
334 'amd64' : {
335 'run' : qemu_run,
336 'image' : 'image.iso'
337 },
338 'arm32' : {
339 'integratorcp' : {
340 'run' : qemu_run,
341 'image' : 'image.boot',
342 'net' : False,
343 'audio' : False,
344 'xhci' : False,
345 'tablet' : False
346 },
347 'raspberrypi' : {
348 'run' : qemu_run,
349 'image' : 'kernel.img@rpi',
350 'audio' : False,
351 'console' : True,
352 'hdd' : False,
353 'net' : False,
354 'tablet' : False,
355 'usb' : False,
356 'xhci' : False
357 },
358 },
359 'arm64' : {
360 'virt' : {
361 'run' : qemu_run,
362 'image' : 'image.iso@arm64',
363 'audio' : False,
364 'console' : True,
365 'hdd' : False,
366 'net' : False,
367 'tablet' : False,
368 'usb' : False,
369 'xhci' : False
370 }
371 },
372 'ia32' : {
373 'run' : qemu_run,
374 'image' : 'image.iso'
375 },
376 'ia64' : {
377 'ski' : {
378 'run' : ski_run
379 }
380 },
381 'mips32' : {
382 'msim' : {
383 'run' : msim_run
384 },
385 'lmalta' : {
386 'run' : qemu_run,
387 'image' : 'image.boot',
388 'console' : True
389 },
390 'bmalta' : {
391 'run' : qemu_run,
392 'image' : 'image.boot',
393 'console' : True
394 },
395 },
396 'ppc32' : {
397 'run' : qemu_run,
398 'image' : 'image.iso',
399 'audio' : False
400 },
401 'riscv64' : {
402 'run' : spike_run,
403 'image' : 'image.boot'
404 },
405 'sparc64' : {
406 'generic' : {
407 'us' : {
408 'run' : qemu_run,
409 'image' : 'image.iso',
410 'audio' : False,
411 'console' : True,
412 'net' : False,
413 'usb' : False,
414 'xhci' : False,
415 'tablet' : False
416 },
417 'sun4v' : {
418 'run' : qemu_run,
419 'image' : '-drive if=pflash,readonly=on,file=image.iso',
420 'audio' : False,
421 'console' : True,
422 'net' : False,
423 'usb' : False,
424 'xhci' : False,
425 'tablet' : False,
426 'expect' : {
427 'src' : 'ok ',
428 'dst' : 'boot\n'
429 },
430 }
431 }
432 },
433}
434
435def usage():
436 print("%s - emulator wrapper for running HelenOS\n" % os.path.basename(sys.argv[0]))
437 print("%s [-d] [-h] [-net e1k|rtl8139|ne2k|virtio-net] [-hdd ata|virtio-blk] [-nohdd] [-nokvm] [-nonet] [-nosnd] [-nousb] [-noxhci] [-notablet]\n" %
438 os.path.basename(sys.argv[0]))
439 print("-d\tDry run: do not run the emulation, just print the command line.")
440 print("-h\tPrint the usage information and exit.")
441 print("-nohdd\tDisable hard disk, if applicable.")
442 print("-nokvm\tDisable KVM, if applicable.")
443 print("-nonet\tDisable networking support, if applicable.")
444 print("-nosnd\tDisable sound, if applicable.")
445 print("-nousb\tDisable USB support, if applicable.")
446 print("-noxhci\tDisable XHCI support, if applicable.")
447 print("-notablet\tDisable USB tablet (use only relative-position PS/2 mouse instead), if applicable.")
448 print("-nographic\tDisable graphical output. Serial port output must be enabled for this to be useful.")
449 print("-noserial\tDisable serial port output in the terminal.")
450 print("-bigmem\tSets maximum RAM size to 4GB.")
451
452def fail(platform, machine):
453 print("Cannot start emulation for the chosen configuration. (%s/%s)" % (platform, machine))
454
455
456def run():
457 expect_nic = False
458 expect_hdd = False
459 expect_qemu = False
460
461 for i in range(1, len(sys.argv)):
462
463 if expect_nic:
464 expect_nic = False
465 if not 'net' in overrides.keys():
466 overrides['net'] = {}
467 if sys.argv[i] == 'e1k':
468 overrides['net']['e1k'] = True
469 elif sys.argv[i] == 'rtl8139':
470 overrides['net']['rtl8139'] = True
471 elif sys.argv[i] == 'ne2k':
472 overrides['net']['ne2k'] = True
473 elif sys.argv[i] == 'virtio-net':
474 overrides['net']['virtio-net'] = True
475 else:
476 usage()
477 exit()
478 continue
479
480 if expect_hdd:
481 expect_hdd = False
482 if not 'hdd' in overrides.keys():
483 overrides['hdd'] = {}
484 if sys.argv[i] == 'ata':
485 overrides['hdd']['ata'] = True
486 elif sys.argv[i] == 'virtio-blk':
487 overrides['hdd']['virtio-blk'] = True
488 else:
489 usage()
490 exit()
491 continue
492
493 if expect_qemu:
494 expect_qemu = False
495 overrides['qemu_path'] = sys.argv[i]
496
497 elif sys.argv[i] == '-h':
498 usage()
499 exit()
500 elif sys.argv[i] == '-d':
501 overrides['dryrun'] = True
502 elif sys.argv[i] == '-net' and i < len(sys.argv) - 1:
503 expect_nic = True
504 elif sys.argv[i] == '-hdd' and i < len(sys.argv) - 1:
505 expect_hdd = True
506 elif sys.argv[i] == '-nohdd':
507 overrides['nohdd'] = True
508 elif sys.argv[i] == '-nokvm':
509 overrides['nokvm'] = True
510 elif sys.argv[i] == '-nonet':
511 overrides['nonet'] = True
512 elif sys.argv[i] == '-nosnd':
513 overrides['nosnd'] = True
514 elif sys.argv[i] == '-nousb':
515 overrides['nousb'] = True
516 elif sys.argv[i] == '-noxhci':
517 overrides['noxhci'] = True
518 elif sys.argv[i] == '-notablet':
519 overrides['notablet'] = True
520 elif sys.argv[i] == '-nographic':
521 overrides['nographic'] = True
522 elif sys.argv[i] == '-bigmem':
523 overrides['bigmem'] = True
524 elif sys.argv[i] == '-noserial':
525 overrides['noserial'] = True
526 elif sys.argv[i] == '-qemu_path' and i < len(sys.argv) - 1:
527 expect_qemu = True
528 else:
529 usage()
530 exit()
531
532 config = read_config()
533
534 if 'PLATFORM' in config.keys():
535 platform = config['PLATFORM']
536 else:
537 platform = ''
538
539 if 'MACHINE' in config.keys():
540 mach = config['MACHINE']
541 else:
542 mach = ''
543
544 if 'PROCESSOR' in config.keys():
545 processor = config['PROCESSOR']
546 else:
547 processor = ''
548
549 try:
550 emu_run = cfg_get(platform, mach, processor)['run']
551 emu_run(platform, mach, processor)
552 except:
553 fail(platform, mach)
554 return
555
556run()
Note: See TracBrowser for help on using the repository browser.