Skip to content

Commit 06ce398

Browse files
authored
Merge pull request #41 from nathanrossi/nrossi/tinyprog-meta-fallback
Allow 'addrmap' data in the root metadata to avoid programming security pages
2 parents 95f282e + c1a6386 commit 06ce398

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

programmer/tinyprog/__init__.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def __init__(self, prog):
158158

159159
def _parse_json(self, data):
160160
try:
161-
return json.loads(bytes(data).decode("utf-8"))
161+
return json.loads(bytes(data).replace(b"\x00", b"").replace(b"\xff", b"").decode("utf-8"))
162162
except BaseException:
163163
return None
164164

@@ -176,7 +176,7 @@ def _resolve_pointers(self, meta):
176176
if m:
177177
data = self.prog.read(
178178
int(m.group("addr"), 16), int(m.group("len")))
179-
return json.loads(bytes(data).decode("utf-8"))
179+
return self._parse_json(data)
180180
else:
181181
return meta
182182

@@ -187,14 +187,12 @@ def _read_metadata(self):
187187
meta_roots = (
188188
[
189189
self._parse_json(
190-
self.prog.read_security_register_page(p).replace(
191-
b"\x00", b"").replace(b"\xff", b""))
190+
self.prog.read_security_register_page(p))
192191
for p in [1, 2, 3]
193192
] + [
194193
self._parse_json(
195194
self.prog.read(
196-
int(math.pow(2, p) - (4 * 1024)), (4 * 1024)).replace(
197-
b"\x00", b"").replace(b"\xff", b""))
195+
int(math.pow(2, p) - (4 * 1024)), (4 * 1024)))
198196
for p in [17, 18, 19, 20, 21, 22, 23, 24]
199197
])
200198
meta_roots = [root for root in meta_roots if root is not None]
@@ -213,7 +211,14 @@ def userdata_addr_range(self):
213211
return self._get_addr_range(u"userdata")
214212

215213
def _get_addr_range(self, name):
216-
addr_str = self.root[u"bootmeta"][u"addrmap"][name]
214+
# get the bootmeta's addrmap or fallback to the root's addrmap.
215+
addr_map = self.root.get(u"bootmeta", {}).get(u"addrmap", self.root.get(u"addrmap", None))
216+
if addr_map is None:
217+
raise Exception("Missing address map from device metadata")
218+
addr_str = addr_map.get(name, None)
219+
if addr_str is None:
220+
raise Exception("Missing address map for '{0}'.".format(name))
221+
217222
m = re.search(
218223
r"^\s*0x(?P<start>[A-Fa-f0-9]+)\s*-\s*0x(?P<end>[A-Fa-f0-9]+)\s*$",
219224
addr_str)

0 commit comments

Comments
 (0)