source: mainline/tools/ew.py@ 739bc43

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 739bc43 was 739bc43, checked in by Petr Pavlu <setup@…>, 6 years ago

Unify firmware search logic for arm64 and sun4v

Update tools/ew.py to first search for firmware binaries by checking an
environment variable, EW_QEMU_EFI_AARCH64 for arm64 and
OPENSPARC_BINARIES for sun4v. If this fails then try finding the
binaries in their expected default locations. If this also fails then
report an error on stderr and exit.

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