Repository navigation
Expand file tree
/
Copy pathblackwall
More file actions
executable file
·169 lines (153 loc) · 7.5 KB
/
Copy pathblackwall
File metadata and controls
executable file
·169 lines (153 loc) · 7.5 KB
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
#!/usr/bin/env python3
"""Run pinned G2 emulator scenarios using a Docker asset volume."""
import argparse
import hashlib
import http.server
import json
from pathlib import Path
import shutil
import subprocess
import sys
import tempfile
from datetime import datetime
from uuid import uuid4
ROOT = Path(__file__).resolve().parent
sys.path.insert(0, str(ROOT / 'tools'))
from run_original_boot_screen import checked_asset
from volume_storage import IMAGE, extract_volume, import_volume, pinned_inputs, select_profile, volume_name
DEFAULT_VOLUME = 'blackwall-assets'
SCENARIOS = {
'normal': 'Stock 2.4 bootloader → main → virtual touch and screen',
'unknown-boot-marker': 'Expected rejection at the virtual boot metadata boundary',
}
def show_assets(profile):
print('Docker image:', profile['image_id'])
for group in ('assets', 'components'):
for name, spec in profile[group].items():
print(f"{group}/{name}: {spec['path']} sha256={spec['sha256']}")
def apply_images(workspace, profile, boot, main):
for key, override in (('boot', boot), ('main', main)):
if override:
spec = profile['assets'][key]
source = checked_asset(workspace, spec, override)
target = workspace / spec['path']
if source != target:
shutil.copyfile(source, target)
def setup(args):
source = args.source.expanduser().resolve()
profile_file = select_profile(source, args.profile)
if not profile_file:
raise ValueError('Selected profile was not found in the supplied source directory')
profile, entries = pinned_inputs(source, profile_file, args.boot_image, args.main_image)
import_volume(args.volume, entries)
print('Created Docker volume:', args.volume)
show_assets(profile)
return 0
def inspect_volume(args):
with tempfile.TemporaryDirectory(prefix='blackwall-inspect-') as folder:
root = Path(folder)
profile_file = extract_volume(args.volume, root)
profile = json.loads(profile_file.read_text())
apply_images(root, profile, args.boot_image, args.main_image)
pinned_inputs(root, profile_file)
print('Verified Docker volume:', args.volume)
show_assets(profile)
return 0
def report(args):
if args.capture and (args.scenario or args.scenario_file or args.boot_image or args.main_image):
raise ValueError('--capture cannot be combined with a new scenario or firmware input')
if args.scenario and args.scenario_file:
raise ValueError('--scenario and --scenario-file cannot be combined')
if not 1 <= args.port <= 65535:
raise ValueError('--port must be 1..65535')
output = args.output.expanduser().resolve() if args.output else Path(tempfile.gettempdir()) / ('blackwall-report-' + datetime.now().strftime('%Y%m%d-%H%M%S') + '-' + uuid4().hex[:8])
if output.exists() or output.is_relative_to(ROOT):
raise ValueError('Output must be a new directory outside the repository')
output.mkdir(mode=0o700, parents=True)
command = [sys.executable, '-B', str(ROOT / 'tools/blackwall_report.py'), '--workspace', str(output), '--output', str(output / 'run')]
try:
if args.capture:
command += ['--capture', str(args.capture.expanduser().resolve())]
else:
profile_file = extract_volume(args.volume, output)
profile = json.loads(profile_file.read_text())
apply_images(output, profile, args.boot_image, args.main_image)
pinned_inputs(output, profile_file)
command += ['--profile', str(profile_file), '--scenario', args.scenario or 'normal']
if args.scenario_file:
command += ['--scenario-file', str(args.scenario_file.expanduser().resolve())]
except (OSError, ValueError, RuntimeError, subprocess.SubprocessError):
# The output was created by this invocation and no emulator run has started.
shutil.rmtree(output)
raise
print('Output:', output, flush=True)
result = subprocess.run(command, cwd=ROOT)
page = output / 'run/report/index.html'
if page.is_file():
print('\nReport:', page, flush=True)
if args.serve:
class Handler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *a, **kw):
super().__init__(*a, directory=str(page.parent), **kw)
try:
server = http.server.ThreadingHTTPServer(('127.0.0.1', args.port), Handler)
except OSError as exc:
raise ValueError('Could not start local server: ' + str(exc)) from exc
print(f'Open http://127.0.0.1:{args.port}/ (Ctrl-C to stop)', flush=True)
try:
server.serve_forever()
except KeyboardInterrupt:
pass
finally:
server.server_close()
return result.returncode
def main():
parser = argparse.ArgumentParser(prog='blackwall', description=__doc__)
sub = parser.add_subparsers(dest='command', required=True)
sub.add_parser('scenarios', help='List built-in scenarios')
assets = sub.add_parser('assets', help='Show required input hashes')
assets.add_argument('--source', type=Path, help='Source directory containing a pinned profile')
assets.add_argument('--profile', choices=['auto', 'imu', 'detailed', 'timed', 'stock'], default='auto')
init = sub.add_parser('setup', help='Import verified inputs into a new Docker volume')
init.add_argument('--source', type=Path, required=True, help='Directory containing pinned model and snapshot inputs')
init.add_argument('--profile', choices=['auto', 'imu', 'detailed', 'timed', 'stock'], default='auto')
init.add_argument('--boot-image', type=Path)
init.add_argument('--main-image', type=Path)
init.add_argument('--volume', default=DEFAULT_VOLUME)
doctor = sub.add_parser('doctor', help='Verify the Docker volume and pinned assets')
doctor.add_argument('--volume', default=DEFAULT_VOLUME)
doctor.add_argument('--boot-image', type=Path)
doctor.add_argument('--main-image', type=Path)
run = sub.add_parser('report', help='Run or audit a scenario and generate its HTML report')
run.add_argument('--volume', default=DEFAULT_VOLUME)
run.add_argument('--scenario', choices=SCENARIOS)
run.add_argument('--scenario-file', type=Path)
run.add_argument('--capture', type=Path)
run.add_argument('--boot-image', type=Path)
run.add_argument('--main-image', type=Path)
run.add_argument('--output', type=Path, help='New output directory outside the repository')
run.add_argument('--serve', action='store_true')
run.add_argument('--port', type=int, default=8765)
args = parser.parse_args()
try:
if args.command == 'scenarios':
for name, description in SCENARIOS.items():
print(f'{name:22} {description}')
return 0
if args.command == 'assets':
profile_file = select_profile(args.source or ROOT, args.profile)
if not profile_file:
raise ValueError('Selected profile is not available')
show_assets(json.loads(profile_file.read_text()))
return 0
volume_name(args.volume)
if args.command == 'setup':
return setup(args)
if args.command == 'doctor':
return inspect_volume(args)
return report(args)
except (ValueError, OSError, RuntimeError, subprocess.SubprocessError) as exc:
print('Blackwall:', exc, file=sys.stderr)
return 2
if __name__ == '__main__':
raise SystemExit(main())