source: mainline/tools/ew.py@ 1783f75

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1783f75 was 1783f75, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 6 years ago

Remove autotool.py

Meson does everything that it was used for.

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