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
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 sys
37import subprocess
38import autotool
39import platform
40import thread
41import time
42
43overrides = {}
44
45def is_override(str):
46 if str in overrides.keys():
47 return overrides[str]
48 return False
49
50def cfg_get(platform, machine, processor):
51 if machine == "" or emulators[platform].has_key("run"):
52 return emulators[platform]
53 elif processor == "" or emulators[platform][machine].has_key("run"):
54 return emulators[platform][machine]
55 else:
56 return emulators[platform][machine][processor]
57
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
66def run_in_console(cmd, title):
67 ecmd = cmd.replace('"', '\\"')
68 cmdline = termemu_detect() + ' -T ' + '"' + title + '"' + ' -e "' + ecmd + '"'
69 print(cmdline)
70 if not is_override('dryrun'):
71 subprocess.call(cmdline, shell = True)
72
73def get_host_native_width():
74 return int(platform.architecture()[0].strip('bit'))
75
76def pc_options(guest_width):
77 opts = ''
78
79 # Do not enable KVM if running 64 bits HelenOS
80 # on 32 bits host
81 host_width = get_host_native_width()
82 if guest_width <= host_width and not is_override('nokvm'):
83 opts = opts + ' -enable-kvm'
84
85 # Remove the leading space
86 return opts[1:]
87
88def malta_options():
89 return '-cpu 4Kc'
90
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
109def platform_to_qemu_options(platform, machine, processor):
110 if platform == 'amd64':
111 return 'system-x86_64', pc_options(64)
112 elif platform == 'arm32':
113 return 'system-arm', '-M integratorcp'
114 elif platform == 'arm64':
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:
126 raise Exception
127
128 return 'system-aarch64', \
129 '-M virt -cpu cortex-a57 -m 1024 -bios %s' % efi_path
130 elif platform == 'ia32':
131 return 'system-i386', pc_options(32)
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':
138 return 'system-ppc', '-m 256'
139 elif platform == 'sparc64':
140 if machine != 'generic':
141 raise Exception
142 if processor == 'us':
143 return 'system-sparc64', '-M sun4u --prom-env boot-args="console=devices/\\hw\\pci0\\01:01.0\\com1\\a"'
144
145 # processor = 'sun4v'
146 opensparc_bins = find_firmware(
147 "OpenSPARC", 'OPENSPARC_BINARIES',
148 ('/usr/local/opensparc/image/', ))
149 if opensparc_bins is None:
150 raise Exception
151
152 return 'system-sparc64', '-M niagara -m 256 -L %s' % (opensparc_bins)
153
154
155def hdisk_mk():
156 if not os.path.exists('hdisk.img'):
157 subprocess.call('tools/mkfat.py 1048576 uspace/dist/data hdisk.img', shell = True)
158
159def qemu_bd_options():
160 if is_override('nohdd'):
161 return ''
162
163 hdisk_mk()
164
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
173
174def qemu_nic_ne2k_options():
175 return ' -device ne2k_isa,irq=5,netdev=n1'
176
177def qemu_nic_e1k_options():
178 return ' -device e1000,netdev=n1'
179
180def qemu_nic_rtl8139_options():
181 return ' -device rtl8139,netdev=n1'
182
183def qemu_nic_virtio_options():
184 return ' -device virtio-net,netdev=n1'
185
186def qemu_net_options():
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()
198 if 'virtio-net' in overrides['net'].keys():
199 nic_options += qemu_nic_virtio_options()
200 else:
201 # Use the default NIC
202 nic_options += qemu_nic_e1k_options()
203
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'
205
206def qemu_usb_options():
207 if is_override('nousb'):
208 return ''
209 return ' -usb'
210
211def qemu_xhci_options():
212 if is_override('noxhci'):
213 return ''
214 return ' -device nec-usb-xhci,id=xhci'
215
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
221def qemu_audio_options():
222 if is_override('nosnd'):
223 return ''
224 return ' -device intel-hda -device hda-duplex'
225
226def qemu_run(platform, machine, processor):
227 cfg = cfg_get(platform, machine, processor)
228 suffix, options = platform_to_qemu_options(platform, machine, processor)
229 cmd = 'qemu-' + suffix
230
231 cmdline = cmd
232 if 'qemu_path' in overrides.keys():
233 cmdline = overrides['qemu_path'] + cmd
234
235 if options != '':
236 cmdline += ' ' + options
237
238 if (not 'hdd' in cfg.keys() or cfg['hdd']):
239 cmdline += qemu_bd_options()
240 if (not 'net' in cfg.keys()) or cfg['net']:
241 cmdline += qemu_net_options()
242 if (not 'usb' in cfg.keys()) or cfg['usb']:
243 cmdline += qemu_usb_options()
244 if (not 'xhci' in cfg.keys()) or cfg['xhci']:
245 cmdline += qemu_xhci_options()
246 if (not 'tablet' in cfg.keys()) or cfg['tablet']:
247 cmdline += qemu_tablet_options()
248 if (not 'audio' in cfg.keys()) or cfg['audio']:
249 cmdline += qemu_audio_options()
250
251 console = ('console' in cfg.keys() and cfg['console'])
252
253 if (is_override('nographic')):
254 cmdline += ' -nographic'
255
256 if (not console and (not is_override('nographic')) and not is_override('noserial')):
257 cmdline += ' -serial stdio'
258
259 if (is_override('bigmem')):
260 cmdline += ' -m 4G'
261
262 if cfg['image'] == 'image.iso':
263 cmdline += ' -boot d -cdrom image.iso'
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'
271 elif cfg['image'] == 'image.boot':
272 cmdline += ' -kernel image.boot'
273 else:
274 cmdline += ' ' + cfg['image']
275
276 if console:
277 cmdline += ' -nographic'
278
279 title = 'HelenOS/' + platform
280 if machine != '':
281 title += ' on ' + machine
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'])
284 run_in_console(cmdline, title)
285 else:
286 print(cmdline)
287 if not is_override('dryrun'):
288 subprocess.call(cmdline, shell = True)
289
290def ski_run(platform, machine, processor):
291 run_in_console('ski -i tools/conf/ski.conf', 'HelenOS/ia64 on ski')
292
293def msim_run(platform, machine, processor):
294 hdisk_mk()
295 run_in_console('msim -c tools/conf/msim.conf', 'HelenOS/mips32 on msim')
296
297def spike_run(platform, machine, processor):
298 run_in_console('spike -m1073741824:1073741824 image.boot', 'HelenOS/risvc64 on Spike')
299
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,
310 'audio' : False,
311 'xhci' : False,
312 'tablet' : False
313 }
314 },
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 },
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',
344 'console' : True
345 },
346 'bmalta' : {
347 'run' : qemu_run,
348 'image' : 'image.boot',
349 'console' : True
350 },
351 },
352 'ppc32' : {
353 'run' : qemu_run,
354 'image' : 'image.iso',
355 'audio' : False
356 },
357 'riscv64' : {
358 'run' : spike_run,
359 'image' : 'image.boot'
360 },
361 'sparc64' : {
362 'generic' : {
363 'us' : {
364 'run' : qemu_run,
365 'image' : 'image.iso',
366 'audio' : False,
367 'console' : True,
368 'net' : False,
369 'usb' : False,
370 'xhci' : False,
371 'tablet' : False
372 },
373 'sun4v' : {
374 'run' : qemu_run,
375 'image' : '-drive if=pflash,readonly=on,file=image.iso',
376 'audio' : False,
377 'console' : True,
378 'net' : False,
379 'usb' : False,
380 'xhci' : False,
381 'tablet' : False,
382 'expect' : {
383 'src' : 'ok ',
384 'dst' : 'boot\n'
385 },
386 }
387 }
388 },
389}
390
391def usage():
392 print("%s - emulator wrapper for running HelenOS\n" % os.path.basename(sys.argv[0]))
393 print("%s [-d] [-h] [-net e1k|rtl8139|ne2k|virtio-net] [-hdd ata|virtio-blk] [-nohdd] [-nokvm] [-nonet] [-nosnd] [-nousb] [-noxhci] [-notablet]\n" %
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.")
402 print("-noxhci\tDisable XHCI support, if applicable.")
403 print("-notablet\tDisable USB tablet (use only relative-position PS/2 mouse instead), if applicable.")
404 print("-nographic\tDisable graphical output. Serial port output must be enabled for this to be useful.")
405 print("-noserial\tDisable serial port output in the terminal.")
406 print("-bigmem\tSets maximum RAM size to 4GB.")
407
408def fail(platform, machine):
409 print("Cannot start emulation for the chosen configuration. (%s/%s)" % (platform, machine))
410
411
412def run():
413 expect_nic = False
414 expect_hdd = False
415 expect_qemu = False
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
429 elif sys.argv[i] == 'virtio-net':
430 overrides['net']['virtio-net'] = True
431 else:
432 usage()
433 exit()
434 continue
435
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
449 if expect_qemu:
450 expect_qemu = False
451 overrides['qemu_path'] = sys.argv[i]
452
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
460 elif sys.argv[i] == '-hdd' and i < len(sys.argv) - 1:
461 expect_hdd = True
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
472 elif sys.argv[i] == '-noxhci':
473 overrides['noxhci'] = True
474 elif sys.argv[i] == '-notablet':
475 overrides['notablet'] = True
476 elif sys.argv[i] == '-nographic':
477 overrides['nographic'] = True
478 elif sys.argv[i] == '-bigmem':
479 overrides['bigmem'] = True
480 elif sys.argv[i] == '-noserial':
481 overrides['noserial'] = True
482 elif sys.argv[i] == '-qemu_path' and i < len(sys.argv) - 1:
483 expect_qemu = True
484 else:
485 usage()
486 exit()
487
488 config = {}
489 autotool.read_config(autotool.CONFIG, config)
490
491 if 'PLATFORM' in config.keys():
492 platform = config['PLATFORM']
493 else:
494 platform = ''
495
496 if 'MACHINE' in config.keys():
497 mach = config['MACHINE']
498 else:
499 mach = ''
500
501 if 'PROCESSOR' in config.keys():
502 processor = config['PROCESSOR']
503 else:
504 processor = ''
505
506 try:
507 emu_run = cfg_get(platform, mach, processor)['run']
508 emu_run(platform, mach, processor)
509 except:
510 fail(platform, mach)
511 return
512
513run()
Note: See TracBrowser for help on using the repository browser.