def __init__(self, dt: Union[str, Path], pagesize: int = 4096) -> None: dt_path = Path(dt) if dt_path.is_dir(): temp_file = mktemp(suffix=".dtb") try: subprocess.run( f"dtc -I fs -O dtb -o {temp_file} {str(dt_path)}".split(), stderr=subprocess.DEVNULL, ) with open(temp_file, "rb") as f: self.fdt = fdt.parse_dtb(f.read()) finally: pass else: with dt_path.open("rb") as f: self.fdt = fdt.parse_dtb(f.read()) self.aliases: MutableMapping[str, str] = OrderedDict() self.aliases_reversed: MutableMapping[str, List[str]] = defaultdict( list ) # type: ignore self.handles: MutableMapping[str, str] = OrderedDict() self.memory_regions: MutableMapping[ str, MemoryRegionData ] = OrderedDict() self.devices: MutableMapping[str, DeviceData] = OrderedDict() self.interrupt_controllers: List[GIC] = [] self.stdout_path: str = "" self.cpus: List[CPU] = [] self.logger = getLogger()
def parse_fdt(file_path: str, file_type: str): """ Parse *.dtb ot *.dts input file and return FDT object :param file_path: The path to input file :param file_type: File type 'dtb', 'dts' or 'auto' """ if not os.path.exists(file_path): raise Exception('File doesnt exist: {}'.format(file_path)) if file_type == 'auto': if file_path.endswith(".dtb"): file_type = 'dtb' elif file_path.endswith(".dts"): file_type = 'dts' else: raise Exception('Not supported file extension: {}'.format(file_path)) if file_type == 'dtb': with open(file_path, 'rb') as f: obj = fdt.parse_dtb(f.read()) else: with open(file_path, 'r') as f: obj = fdt.parse_dts(f.read(), os.path.dirname(file_path)) return obj
def read_fdt(self): """Read devicetree""" if self.args.dts: with open(self.args.dts, 'r') as f: return parse_dts(f.read()) else: with open(self.args.dtb, 'rb') as f: return parse_dtb(f.read())
def todts(outfile, infile, tabsize): """ Convert *.dtb to *.dts """ try: with open(infile, 'rb') as f: data = f.read() dt = fdt.parse_dtb(data) with open(outfile, 'w') as f: f.write(dt.to_dts(tabsize)) except Exception as e: click.echo(" ERROR: {}".format(str(e) if str(e) else "Unknown!")) sys.exit(ERROR_CODE) click.secho(" DTS saved as: %s" % outfile)
def parse_itb(data, offset=0): """ Parse ITB data-blob :param data: :param offset: :return: """ fdt_obj = fdt.parse_dtb(data, offset) # ... fim_obj = FdtImage() fim_obj.time_stamp = get_value(fdt_obj, "timestamp") fim_obj.description = get_value(fdt_obj, "description", "") # Parse images node = fdt_obj.get_node("images") if node is None or not node.nodes: raise Exception("parse_itb: images not defined") for img in node.nodes: if img.exist_property("data"): img_data = get_data(img) img.remove_property("data") elif img.exist_property("data-size") and img.exist_property( "data-position"): data_size = get_value(img, "data-size") data_offset = get_value(img, "data-position") img_data = data[offset + data_offset:offset + data_offset + data_size] img.remove_property("data-size") img.remove_property("data-position") else: raise Exception() fim_obj.add_img(img, img_data) # Parse configs node = fdt_obj.get_node("configurations") if node is None or not node.nodes: raise Exception("parse_itb: configurations not defined") for cfg in node.nodes: fim_obj.add_cfg(cfg, True) fim_obj.def_config = get_value(node, "default") return fim_obj
def open_fdt(file_path, file_type): if file_type == 'auto': if file_path.endswith(".dtb"): file_type = 'dtb' elif file_path.endswith(".dts"): file_type = 'dts' else: raise Exception( 'Not supported file extension: {}'.format(file_path)) try: if file_type == 'dtb': with open(file_path, 'rb') as f: obj = fdt.parse_dtb(f.read()) else: with open(file_path, 'r') as f: obj = fdt.parse_dts(f.read(), os.path.dirname(file_path)) except: raise Exception('Not supported file format: {}'.format(file_path)) return obj
def todts(outfile, infile, tabsize): """ Convert device tree binary blob (*.dtb) into readable text file (*.dts) """ fdt_obj = None if outfile is None: outfile = os.path.splitext(os.path.basename(infile))[0] + ".dts" try: with open(infile, 'rb') as f: try: fdt_obj = fdt.parse_dtb(f.read()) except: raise Exception('Not supported file format: {}'.format(infile)) with open(outfile, 'w') as f: f.write(fdt_obj.to_dts(tabsize)) except Exception as e: click.echo(" Error: {}".format(str(e) if str(e) else "Unknown!")) sys.exit(ERROR_CODE) click.secho(" DTS saved as: %s" % outfile)
def load(self, db, root_path): """ load DCD segments :param db: ... :param root_path: ... """ assert isinstance(db, list) assert isinstance(root_path, str) file_path = get_full_path(root_path, self.path)[0] if file_path.endswith(".dtb"): with open(file_path, 'rb') as f: fdt_obj = fdt.parse_dtb(f.read()) else: with open(file_path, 'r') as f: fdt_obj = fdt.parse_dts(f.read()) if self._mode is 'merge': fdt_obj.merge(fdt.parse_dts(self._dts_data)) if fdt_obj.header.version is None: fdt_obj.header.version = 17 self.data = fdt_obj.to_dtb()
def unpack(self, raw): super().unpack(raw) self.fdt = parse_dtb(raw) return self