-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
executable file
·178 lines (139 loc) · 4.42 KB
/
build.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python
import sys
import os
import collections
import re
import fnmatch
## check
if (len(sys.argv)<8):
print "usage: python build.py"
print "\t<path_to_gen_esp32part.py>"
print "\t<folder_name> <output_h_name> <output_to_makefile> <output_binary>"
print "\t<partition_table_filename> <partition_name> <grow_size_boundary>"
print "\t[<file_name>=[p:<partition_name>,o:<offset>|r:<raw_flash_offset>][[,]g:<growsize>][...]]"
exit()
sys.path.append(sys.argv[1])
sys.argv[0:] = sys.argv[1:]
from gen_esp32part import PartitionTable, PartitionDefinition
class Option(object):
regex = None
partition = ""
offset = 0
growsize = -1
raw_flash_offset = -1
def fits(self, file):
return self.regex.match(file) is not None
def __init__(self, input, pattern):
self.regex = re.compile(fnmatch.translate(pattern))
opts = input.split(',');
for opt in opts:
if opt[1:2] == ':':
if opt[0:1] == 'p':
self.partition = opt[2:]
if opt[0:1] == 'o':
self.offset = opt[2:]
if opt[0:1] == 'g':
try:
self.growsize = int(opt[2:],0)
except:
if (opt[2:] == "max"):
self.growsize = -2
if opt[0:1] == 'r':
self.raw_flash_offset = int(opt[2:],0)
def MainCode():
## init
files = []
partitions = []
controls = []
## parameters
folder = sys.argv[1]
output_h = sys.argv[2]
output_m = sys.argv[3]
output_b = sys.argv[4]
part_file = sys.argv[5]
partition_name = sys.argv[6]
increment = int(sys.argv[7], 0)
## load partition table
table = PartitionTable.from_csv(open(part_file,'r').read())
## find referenced main partition
main_partition = table[partition_name]
sadr = main_partition.offset
offset = 0
filldata = bytearray(main_partition.size)
for (dirpath, dirnames, filenames) in os.walk(folder):
files.extend([os.path.normpath(os.path.relpath(os.path.join(dirpath,item),folder)) for item in filenames])
files.sort()
print "FFS files found: %s" % files
f_load = ""
h_enum = "#define FFS_FILE_LIST \\\n"
h_def = "#define FFS_FILE_METADATA \\\n"
optprog = re.compile('(((\S|\\\s)+)=((\\\s|\S)+))')
args = ' '.join(sys.argv[8:])
for m in re.finditer(optprog, args):
controls.append(Option(m.group(4), m.group(2)))
for file in files:
fullname = os.path.join(folder, file)
fh = open(fullname, 'rb')
inbytes = bytearray(fh.read())
flen = len(inbytes)
fh.close()
fn = re.sub("[^A-Za-z0-9]", "_", file)
file = "/" + file.replace(os.pathsep, "/")
fstart = sadr
foffset = offset
blocks = flen // increment
blocks += 1
blocks *= increment
curr_partition = main_partition
for opts in controls:
if opts.fits(file):
if (opts.growsize >= 0):
blocks = flen // opts.growsize
blocks += 1
blocks *= opts.growsize
if ((opts.growsize == -2) and (opts.partition != "")):
blocks = flen // table[opts.partition].size
blocks += 1
blocks *= table[opts.partition].size
if (opts.partition != ""):
fstart = table[opts.partition].offset
foffset = opts.offset
curr_partition = table[opts.partition]
if (opts.raw_flash_offset >= 0):
fstart = 0
foffset = opts.raw_flash_offset
curr_partition = None
break
h_enum += "%s, \\\n" % fn
if (curr_partition is None):
h_def += "\t\t{ \"%s\", %d, %d, %s, %d, %d, 0x%x }, \\\n" % (file, flen, blocks, "NULL", 0, 0, foffset)
else:
h_def += "\t\t{ \"%s\", %d, %d, %s, %d, %d, 0x%x }, \\\n" % (file, flen, blocks, "\"" + curr_partition.name + "\"", curr_partition.type, curr_partition.subtype, foffset)
if (foffset + blocks) > curr_partition.size:
raise Exception("File %s with grow is %d bytes, too large to fit in partition %s" % (file, blocks, curr_partition.name))
if (flen>0):
if (curr_partition is main_partition):
filldata[foffset:foffset+flen-1] = inbytes[0:flen-1]
else:
f_load += " 0x%x %s " % (fstart + foffset, fullname)
if (curr_partition is main_partition):
offset += blocks
if offset > main_partition.size:
raise Exception("Filesystem data with grow space too large to fit in main partition %s" % main_partition.name)
h_enum += "\n"
h_def += "\n"
outh = open(output_h, 'w')
outm = open(output_m, 'w')
outb = open(output_b, 'wb')
outh.write(h_enum)
outh.write(h_def)
outm.write(f_load)
outm.write(" 0x%x %s " % (main_partition.offset, output_b))
outb.write(filldata)
outh.close()
outm.close()
outb.close()
try:
MainCode()
except Exception as ex:
print "Failed: %s\n" % ex