[python] add view interface
This commit is contained in:
parent
7c2d040195
commit
7774c948d2
@ -25,6 +25,7 @@ def detect(args):
|
|||||||
print("detected", filetype.name)
|
print("detected", filetype.name)
|
||||||
for m in filetype.matches:
|
for m in filetype.matches:
|
||||||
print(">", m)
|
print(">", m)
|
||||||
|
filetype.view(m)
|
||||||
|
|
||||||
return matches
|
return matches
|
||||||
|
|
||||||
|
@ -1,7 +1,23 @@
|
|||||||
|
import os
|
||||||
import io
|
import io
|
||||||
|
|
||||||
|
from pyfdt.pyfdt import FdtBlobParse
|
||||||
|
|
||||||
from .matcher import SignatureMatcher, Match
|
from .matcher import SignatureMatcher, Match
|
||||||
|
|
||||||
class FlattenDeviceTree(SignatureMatcher):
|
class FlattenDeviceTree(SignatureMatcher):
|
||||||
|
"""
|
||||||
|
https://devicetree-specification.readthedocs.io/en/stable/flattened-format.html
|
||||||
|
|
||||||
|
header
|
||||||
|
-> space
|
||||||
|
-> memory reservation block
|
||||||
|
-> space
|
||||||
|
-> structure block
|
||||||
|
-> space
|
||||||
|
-> strings block
|
||||||
|
-> space
|
||||||
|
"""
|
||||||
def __init__(self, file):
|
def __init__(self, file):
|
||||||
self.name = "Flatten Device Tree"
|
self.name = "Flatten Device Tree"
|
||||||
self.signature = b'\xd0\x0d\xfe\xed'
|
self.signature = b'\xd0\x0d\xfe\xed'
|
||||||
@ -9,6 +25,9 @@ class FlattenDeviceTree(SignatureMatcher):
|
|||||||
|
|
||||||
def is_valid(self):
|
def is_valid(self):
|
||||||
for match in self.search():
|
for match in self.search():
|
||||||
|
"""
|
||||||
|
All the header fields ... stored in big-endian format
|
||||||
|
"""
|
||||||
start = match
|
start = match
|
||||||
header = io.BytesIO(self.file[start:start+4*10])
|
header = io.BytesIO(self.file[start:start+4*10])
|
||||||
magic = header.read(4)
|
magic = header.read(4)
|
||||||
@ -22,7 +41,32 @@ class FlattenDeviceTree(SignatureMatcher):
|
|||||||
size_dt_strings = header.read(4)
|
size_dt_strings = header.read(4)
|
||||||
size_dt_struct = header.read(4)
|
size_dt_struct = header.read(4)
|
||||||
|
|
||||||
totalsize = int.from_bytes(totalsize, 'little')
|
as_num = lambda f: int.from_bytes(f, 'big')
|
||||||
self.matches += [Match(start, totalsize)]
|
totalsize = as_num(totalsize)
|
||||||
|
data = {
|
||||||
|
'off_dt_struct': as_num(off_dt_struct),
|
||||||
|
'off_dt_strings': as_num(off_dt_strings),
|
||||||
|
'off_mem_rsvmap': as_num(off_mem_rsvmap),
|
||||||
|
'version': as_num(version),
|
||||||
|
'last_comp_version': as_num(last_comp_version),
|
||||||
|
'boot_cpuid_phys': as_num(boot_cpuid_phys),
|
||||||
|
'size_dt_strings': as_num(size_dt_strings),
|
||||||
|
'size_dt_struct': as_num(size_dt_struct),
|
||||||
|
}
|
||||||
|
self.matches += [Match(start, totalsize, data)]
|
||||||
|
|
||||||
return len(self.matches) != 0
|
return len(self.matches) != 0
|
||||||
|
|
||||||
|
def view(self, match):
|
||||||
|
as_num = lambda n: int.from_bytes(n, 'big')
|
||||||
|
data = match.data
|
||||||
|
region = io.BytesIO(self.file[match.offset : match.offset + match.length])
|
||||||
|
|
||||||
|
dtb = FdtBlobParse(region)
|
||||||
|
s = dtb.to_fdt()
|
||||||
|
|
||||||
|
for name, node in s.rootnode.walk():
|
||||||
|
print(name)
|
||||||
|
if "kernel" in name:
|
||||||
|
if len(list(filter(lambda f: f in name, ["arch", "os", "description"]))) != 0:
|
||||||
|
print("> ", node)
|
||||||
|
@ -26,3 +26,6 @@ class SignatureMatcher:
|
|||||||
|
|
||||||
def is_valid(self):
|
def is_valid(self):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def view(self, match):
|
||||||
|
pass
|
||||||
|
Loading…
Reference in New Issue
Block a user