Skip to content

Commit 586157b

Browse files
committed
experiments/smc_watcher.py: new experiment
Signed-off-by: Hector Martin <[email protected]>
1 parent 8c2ad9a commit 586157b

1 file changed

Lines changed: 115 additions & 0 deletions

File tree

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#!/usr/bin/env python3
2+
# SPDX-License-Identifier: MIT
3+
import sys, pathlib, fnmatch, signal
4+
import time
5+
sys.path.append(str(pathlib.Path(__file__).resolve().parents[1]))
6+
7+
import struct
8+
from m1n1.setup import *
9+
from m1n1.shell import run_shell
10+
from m1n1.fw.smc import SMCClient, SMCError
11+
12+
smc_addr = u.adt["arm-io/smc"].get_reg(0)[0]
13+
smc = SMCClient(u, smc_addr)
14+
smc.start()
15+
smc.start_ep(0x20)
16+
17+
smc.verbose = 0
18+
19+
smcep = smc.epmap[0x20]
20+
21+
count = smcep.read32b("#KEY")
22+
print(f"Key count: {count}")
23+
24+
print("Scanning keys...")
25+
26+
pats = sys.argv[1:]
27+
28+
vals = {}
29+
30+
fmts = {
31+
"D?CR": "#x",
32+
"AC-I": "#x",
33+
"D?FC": "#x",
34+
"D?VM": lambda v: (v>>8) | ((v&0xff)<<8),
35+
"D?VX": lambda v: (v>>8) | ((v&0xff)<<8),
36+
"B0RM": lambda v: (v>>8) | ((v&0xff)<<8),
37+
##"BAAC": lambda v: ((v&0xff00)>>8) | ((v&0xff)<<8),
38+
}
39+
40+
smcep.write8("NTAP", 1)
41+
42+
for i in range(count):
43+
k = smcep.get_key_by_index(i)
44+
if not any(fnmatch.fnmatchcase(k, i) for i in pats):
45+
continue
46+
if any(fnmatch.fnmatchcase('-' + k, i) for i in pats):
47+
continue
48+
length, type, flags = smcep.get_key_info(k)
49+
if type in ("ch8*", "{jst"):
50+
continue
51+
if flags & 0x80:
52+
try:
53+
val = smcep.read_type(k, length, type)
54+
fmt = None
55+
for fk, fv in fmts.items():
56+
if fnmatch.fnmatchcase(k, fk):
57+
fmt = fv
58+
if fmt is None:
59+
fmt = lambda a: ("%.02f" % a) if isinstance(a, float) else a
60+
elif isinstance(fmt, str):
61+
def ff(fmt):
62+
return lambda a: f"{a:{fmt}}"
63+
fmt = ff(fmt)
64+
vals[k] = val, length, type, fmt
65+
print(f"#{i}: {k} = ({type}, {flags:#x}) {fmt(val)}")
66+
except SMCError as e:
67+
print(f"#{i}: {k} = ({type}, {flags:#x}) <error {e}>")
68+
else:
69+
print(f"#{i}: {k} = ({type}, {flags:#x}) <not available>")
70+
71+
slots = {}
72+
73+
def poll():
74+
global cnt
75+
reprint = cnt % 10 == 0
76+
changed = set()
77+
for k, (oval, length, type, fmt) in vals.items():
78+
val = smcep.read_type(k, length, type)
79+
if val != oval:
80+
if k not in slots:
81+
reprint = True
82+
slots[k] = fmt(val)
83+
changed.add(k)
84+
vals[k] = val, length, type, fmt
85+
if reprint:
86+
print("\x1b[1;4m", end="")
87+
for k, v in slots.items():
88+
wd = len(f"{v:>8}")
89+
print(f"{k:>{wd}s}", end=" ")
90+
print("\x1b[m")
91+
for k, v in slots.items():
92+
if k in changed:
93+
print("\x1b[32m", end="")
94+
print(f"{v:>8}\x1b[m", end=" ")
95+
print()
96+
cnt += 1
97+
time.sleep(1)
98+
99+
def handle_sigint(signal=None, stack=None):
100+
global doshell
101+
doshell = True
102+
103+
signal.signal(signal.SIGINT, handle_sigint)
104+
105+
doshell = False
106+
try:
107+
cnt = 0
108+
while True:
109+
poll()
110+
if doshell:
111+
run_shell(globals(), msg="Interrupted")
112+
doshell = False
113+
finally:
114+
smc.stop()
115+

0 commit comments

Comments
 (0)