def test_02(data_dir): with open(os.path.join(data_dir, "comments.dts")) as f: data = f.read() fdt_obj = fdt.parse_dts(data) assert len(fdt_obj.root.props) == 10 assert len(fdt_obj.root.nodes) == 1
def parse_its(text, root_dir=''): """ Parse ITS file :param text: :param root_dir: :return: """ its_obj = fdt.parse_dts(text, root_dir) # ... fim_obj = FdtImage() prop = its_obj.get_property("description") if prop is not None: fim_obj.description = prop[0] # Parse images node = its_obj.get_node("images") if node is None: raise Exception("parse_its: images not defined") for img in node.nodes: img_data = get_data(img) img.remove_property("data") fim_obj.add_img(img, img_data) # Parse configs node = its_obj.get_node("configurations") if node is None or not node.nodes: raise Exception("parse_its: 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 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 test_01(): with open(DIRECTORY + "addresses.dts") as f: data = f.read() fdt_obj = fdt.parse_dts(data) with pytest.raises(Exception): blob = fdt_obj.to_dtb() blob = fdt_obj.to_dtb(17)
def dts2dtb(args): file_dts = args.dts file_dtb = args.dtb with open(file_dts, "r", encoding='utf-8') as f: tmp_dts = f.read() tmp_dtb = fdt.parse_dts(tmp_dts) with open(file_dtb, "wb") as f: f.write(tmp_dtb.to_dtb(version=17))
def bl_dts2dtb(src_addr='', dest_addr=''): if '' == src_addr or ('' == dest_addr): bflb_utils.printf('bl_dts2dtb please check arg.') return bflb_utils.printf('=========', src_addr, dest_addr, '=========') with open(src_addr, 'r') as f: tmp1_dts = f.read() tmp2_dtb = fdt.parse_dts(tmp1_dts) dest_addr = os.path.join(app_path, dest_addr) with open(dest_addr, 'wb') as f: f.write(tmp2_dtb.to_dtb(version=17))
def todtb(outfile, infile, version, lcversion, cpuid, align, padding, size): """ Convert device tree as readable text file (*.dts) into binary blob (*.dtb) """ fdt_obj = None if outfile is None: outfile = os.path.splitext(os.path.basename(infile))[0] + ".dtb" try: if version is not None and version > fdt.Header.MAX_VERSION: raise Exception("DTB Version must be lover or equal {} !".format( fdt.Header.MAX_VERSION)) with open(infile, 'r') as f: try: fdt_obj = fdt.parse_dts(f.read(), os.path.dirname(infile)) except: raise Exception('Not supported file format: {}'.format(infile)) raw_data = fdt_obj.to_dtb(version, lcversion, cpuid) if align is not None: if size is not None: raise Exception( "The \"-a/--align\" option can't be used together with \"-s/--size\"" ) if not align % 2: raise Exception( "The \"-a/--align\" option must be dividable with two !") if len(raw_data) % align: raw_data += bytes([0] * (len(raw_data) % align)) if padding is not None: if align is not None: raise Exception( "The \"-p/--padding\" option can't be used together with \"-a/--align\"" ) raw_data += bytes([0] * padding) if size is not None: if size < len(raw_data): raise Exception("The \"-s/--size\" option must be > {}".format( len(raw_data))) raw_data += bytes([0] * (size - len(raw_data))) with open(outfile, 'wb') as f: f.write(raw_data) except Exception as e: click.echo(" Error: {}".format(str(e) if str(e) else "Unknown!")) sys.exit(ERROR_CODE) click.secho(" DTB 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 todtb(outfile, infiles, version, lcversion, cpuid, align, padding, size): """ Convert *.dts to *.dtb """ try: dt = None if version is not None and version > fdt.Header.MAX_VERSION: raise Exception("DTB Version must be lover or equal {} !".format(fdt.Header.MAX_VERSION)) if not isinstance(infiles, (list, tuple)): infiles = [infiles] for file in infiles: with open(file, 'r') as f: data = fdt.parse_dts(f.read(), os.path.dirname(file)) if dt is None: dt = data else: dt.merge(data) raw_data = dt.to_dtb(version, lcversion, cpuid) if align is not None: if size is not None: raise Exception("The \"-a/--align\" option can't be used together with \"-s/--size\"") if not align % 2: raise Exception("The \"-a/--align\" option must be dividable with two !") if len(raw_data) % align: raw_data += bytes([0] * (len(raw_data) % align)) if padding is not None: if align is not None: raise Exception("The \"-p/--padding\" option can't be used together with \"-a/--align\"") raw_data += bytes([0] * padding) if size is not None: if size < len(raw_data): raise Exception("The \"-s/--size\" option must be > {}".format(len(raw_data))) raw_data += bytes([0] * (size - len(raw_data))) with open(outfile, 'wb') as f: f.write(raw_data) except Exception as e: click.echo(" ERROR: {}".format(str(e) if str(e) else "Unknown!")) sys.exit(ERROR_CODE) click.secho(" DTB saved as: %s" % outfile)
def test_01(data_dir): with open(os.path.join(data_dir, "addresses.dts")) as f: data = f.read() fdt_obj = fdt.parse_dts(data) assert fdt_obj.get_property('compatible').value == "test_addresses" assert fdt_obj.get_property('#address-cells').value == 2 assert fdt_obj.get_property('#size-cells').value == 2 assert fdt_obj.get_node('identity-bus@0') == fdt.Node('identity-bus@0') assert fdt_obj.get_node('simple-bus@1000000') == fdt.Node('simple-bus@1000000', fdt.PropWords('#address-cells', 2), fdt.PropWords('#size-cells', 1)) with pytest.raises(Exception): _ = fdt_obj.to_dtb() data = fdt_obj.to_dtb(17) assert isinstance(data, bytes) assert len(data) == 254
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 test_04(data_dir): with open(os.path.join(data_dir, "base.dts")) as f: data = f.read() with pytest.raises(AssertionError): _ = fdt.parse_dts(data)
def generate_fdt(filename): with open(filename, 'r') as f: data = f.read() dtb = fdt.parse_dts(data) return dtb
def test_04(): with open(DIRECTORY + "base.dts") as f: data = f.read() with pytest.raises(AssertionError): fdt_obj = fdt.parse_dts(data)
def test_02(): with open(DIRECTORY + "comments.dts") as f: data = f.read() fdt_obj = fdt.parse_dts(data) print(fdt_obj)