-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathfdtmerge.py
executable file
·57 lines (49 loc) · 2.2 KB
/
fdtmerge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
DTB merge : merge dtb2 into db1, replace properties of dtb1 with properties of dtb2
@author: Neil 'superna' Armstrong <[email protected]>
"""
import argparse
from pyfdt.pyfdt import FdtBlobParse, FdtJsonParse, FdtFsParse
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Device Tree Blob merge')
parser.add_argument('--format1', dest = 'format1' ,help="input format1 (dtb, fs or json), default to dtb", default = "dtb")
parser.add_argument('--format2', dest = 'format2' ,help="input format2 (dtb, fs or json), default to dtb", default = "dtb")
parser.add_argument('--outformat', dest = 'outformat' ,help="output format (dtb, dts or json), default to dtb", default = "dtb")
parser.add_argument('in_dtb1', help="input filename 1")
parser.add_argument('in_dtb2', help="input filename 2")
parser.add_argument('out_filename', help="output filename")
args = parser.parse_args()
if args.format1 not in ('fs', 'dtb', 'json'):
raise Exception('Invalid Format1')
if args.format2 not in ('fs', 'dtb', 'json'):
raise Exception('Invalid Format2')
if args.format1 == 'dtb':
with open(args.in_dtb1) as infile:
dtb1 = FdtBlobParse(infile)
fdt1 = dtb1.to_fdt()
elif args.format1 == 'json':
with open(args.in_dtb1) as infile:
fdt1 = FdtJsonParse(infile.read())
else:
fdt1 = FdtFsParse(args.in_dtb1)
if args.format2 == 'dtb':
with open(args.in_dtb2) as infile:
dtb2 = FdtBlobParse(infile)
fdt2 = dtb2.to_fdt()
elif args.format2 == 'json':
with open(args.in_dtb2) as infile:
fdt2 = FdtJsonParse(infile.read())
else:
fdt2 = FdtFsParse(args.in_dtb2)
fdt1.get_rootnode().merge(fdt2.get_rootnode())
if args.outformat == "dts":
with open(args.out_filename, 'wb') as outfile:
outfile.write(fdt1.to_dts())
elif args.outformat == "dtb":
with open(args.out_filename, 'wb') as outfile:
outfile.write(fdt1.to_dtb())
elif args.outformat == "json":
with open(args.out_filename, 'wb') as outfile:
outfile.write(fdt1.to_json())