efuse: Fix make/cmake build systems and docs

This commit is contained in:
Konstantin Kondrashov
2018-12-06 17:49:02 +08:00
committed by bot
parent 9822055851
commit b8141f3ad8
12 changed files with 325 additions and 303 deletions

View File

@@ -31,14 +31,11 @@ __version__ = '1.0'
quiet = False
coding_scheme = 0
custom_table_use_BLK1 = False
custom_table_use_BLK2 = False
common_table_fix_size = False
CODE_SCHEME = {
"NONE" : 0,
"3/4" : 1,
"REPEAT" : 2,
"NONE" : 0,
"3/4" : 1,
"REPEAT" : 2,
}
copyright = '''// Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD
@@ -61,25 +58,27 @@ def status(msg):
if not quiet:
critical(msg)
def critical(msg):
""" Print critical message to stderr """
sys.stderr.write(msg)
sys.stderr.write('\n')
class FuseTable(list):
def __init__(self):
super(FuseTable, self).__init__(self)
self.md5_digest_table = ""
@classmethod
def from_csv(cls, csv_contents, type_table):
def from_csv(cls, csv_contents):
res = FuseTable()
lines = csv_contents.splitlines()
def expand_vars(f):
f = os.path.expandvars(f)
m = re.match(r'(?<!\\)\$([A-Za-z_][A-Za-z0-9_]*)', f)
if m:
raise InputError("unknown variable '%s'" % m.group(1))
raise InputError("unknown variable '%s'" % (m.group(1)))
return f
for line_no in range(len(lines)):
@@ -89,9 +88,9 @@ class FuseTable(list):
try:
res.append(FuseDefinition.from_csv(line))
except InputError as e:
raise InputError("Error at line %d: %s" % (line_no+1, e))
raise InputError("Error at line %d: %s" % (line_no + 1, e))
except Exception:
critical("Unexpected error parsing line %d: %s" % (line_no+1, line))
critical("Unexpected error parsing line %d: %s" % (line_no + 1, line))
raise
# fix up missing bit_start
@@ -111,14 +110,14 @@ class FuseTable(list):
last_field = None
for e in res:
if e.field_name == "" and last_field is None:
raise InputError("Error at line %d: %s missing field name" % (line_no+1, e))
raise InputError("Error at line %d: %s missing field name" % (line_no + 1, e))
elif e.field_name == "" and last_field is not None:
e.field_name = last_field.field_name
last_field = e
# fill group
names = [ p.field_name for p in res ]
duplicates = set( n for n in names if names.count(n) > 1 )
names = [p.field_name for p in res]
duplicates = set(n for n in names if names.count(n) > 1)
if len(duplicates) != 0:
i_count = 0
for p in res:
@@ -129,13 +128,6 @@ class FuseTable(list):
i_count = 0
res.verify_duplicate_name()
# fix size due to coding scheme
if type_table == "common_table":
if common_table_fix_size == True and (custom_table_use_BLK1 == False or custom_table_use_BLK2 == False):
res.fix_size_fields_from_blk1_blk2();
if custom_table_use_BLK1 == True or custom_table_use_BLK2 == True:
res.keys_from_blk1_blk2_make_empty();
# clac md5 for table
res.calc_md5()
@@ -143,8 +135,8 @@ class FuseTable(list):
def verify_duplicate_name(self):
# check on duplicate name
names = [ p.field_name for p in self ]
duplicates = set( n for n in names if names.count(n) > 1 )
names = [p.field_name for p in self]
duplicates = set(n for n in names if names.count(n) > 1)
# print sorted duplicate partitions by name
if len(duplicates) != 0:
@@ -153,8 +145,8 @@ class FuseTable(list):
field_name = p.field_name + p.group
if field_name != "" and len(duplicates.intersection([field_name])) != 0:
fl_error = True
print ("Field at %s, %s, %s, %s have dublicate field_name"
% ( p.field_name, p.efuse_block, p.bit_start, p.bit_count))
print ("Field at %s, %s, %s, %s have dublicate field_name" %
(p.field_name, p.efuse_block, p.bit_start, p.bit_count))
if fl_error == True:
raise InputError("Field names must be unique")
@@ -168,9 +160,9 @@ class FuseTable(list):
last = None
for p in sorted(self, key=lambda x:(x.efuse_block, x.bit_start)):
if last is not None and last.efuse_block == p.efuse_block and p.bit_start < last.bit_start + last.bit_count:
raise InputError("Field at %s, %s, %s, %s overlaps %s, %s, %s, %s"
% ( p.field_name, p.efuse_block, p.bit_start, p.bit_count,
last.field_name, last.efuse_block, last.bit_start, last.bit_count))
raise InputError("Field at %s, %s, %s, %s overlaps %s, %s, %s, %s" %
(p.field_name, p.efuse_block, p.bit_start, p.bit_count,
last.field_name, last.efuse_block, last.bit_start, last.bit_count))
last = p
def fix_size_fields_from_blk1_blk2(self):
@@ -178,14 +170,14 @@ class FuseTable(list):
if (p.efuse_block == "EFUSE_BLK1" and custom_table_use_BLK1 == False) or (p.efuse_block == "EFUSE_BLK2" and custom_table_use_BLK2 == False):
max_bits = p.get_max_bits_of_block()
if p.bit_start == 0 and p.bit_count > max_bits:
print("Fix size `%s` field from %d to %d" %(p.field_name, p.bit_count, max_bits))
print("Fix size `%s` field from %d to %d" % (p.field_name, p.bit_count, max_bits))
p.bit_count = max_bits
def keys_from_blk1_blk2_make_empty(self):
for p in self:
if (p.efuse_block == "EFUSE_BLK1" and custom_table_use_BLK1 == True) or (p.efuse_block == "EFUSE_BLK2" and custom_table_use_BLK2 == True):
p.bit_count = 0
print("efuse: `%s` field was changed from %d to 0" %(p.field_name, p.bit_count))
print("efuse: `%s` field was changed from %d to 0" % (p.field_name, p.bit_count))
def calc_md5(self):
txt_table = ''
@@ -198,8 +190,9 @@ class FuseTable(list):
rows = ''
rows += 'Sorted efuse table:\n'
num = 1
rows = "{0} \t{1:<30} \t{2} \t{3} \t{4}".format("#", "field_name", "efuse_block", "bit_start", "bit_count") + "\n"
for p in sorted(self, key=lambda x:(x.efuse_block, x.bit_start)):
rows += "#%d \t%s \t\t%s \t\t%d \t\t%d" % (num, p.field_name, p.efuse_block, p.bit_start, p.bit_count) + "\n"
rows += "{0} \t{1:<30} \t{2} \t{3:^8} \t{4:^8}".format(num, p.field_name, p.efuse_block, p.bit_start, p.bit_count) + "\n"
num += 1
rows += '\nUsed bits in efuse table:\n'
@@ -214,12 +207,12 @@ class FuseTable(list):
rows += '%d] [%d ' % (last.bit_start + last.bit_count - 1, p.bit_start)
last = p
rows += '%d] \n' % (last.bit_start + last.bit_count - 1)
rows += '\nNote: Not printed ranges are free for using.\n'
rows += '\nNote: Not printed ranges are free for using. (bits in EFUSE_BLK0 are reserved for Espressif)\n'
return rows
def to_header(self, file_name):
rows = [ copyright ]
rows += [ "#ifdef __cplusplus",
rows = [copyright]
rows += ["#ifdef __cplusplus",
'extern "C" {',
"#endif",
"",
@@ -234,19 +227,19 @@ class FuseTable(list):
last_field_name = ''
for p in self:
if (p.field_name != last_field_name):
rows += [ "extern const esp_efuse_desc_t* " + "ESP_EFUSE_" + p.field_name + "[];" ]
rows += ["extern const esp_efuse_desc_t* " + "ESP_EFUSE_" + p.field_name + "[];"]
last_field_name = p.field_name
rows += [ "",
"#ifdef __cplusplus",
"}",
"#endif",
""]
rows += ["",
"#ifdef __cplusplus",
"}",
"#endif",
""]
return '\n'.join(rows) + "\n"
def to_c_file(self, file_name, debug):
rows = [ copyright ]
rows += [ '#include "esp_efuse.h"',
rows = [copyright]
rows += ['#include "esp_efuse.h"',
'#include "' + file_name + '.h"',
"",
"// md5_digest_table " + self.md5_digest_table,
@@ -260,11 +253,11 @@ class FuseTable(list):
for p in self:
if (p.field_name != last_name):
if last_name != '':
rows += [ "};\n"]
rows += [ "static const esp_efuse_desc_t " + p.field_name + "[] = {" ]
rows += ["};\n"]
rows += ["static const esp_efuse_desc_t " + p.field_name + "[] = {"]
last_name = p.field_name
rows += [ p.to_struct(debug) + "," ]
rows += [ "};\n" ]
rows += [p.to_struct(debug) + ","]
rows += ["};\n"]
rows += ["\n\n\n"]
@@ -273,18 +266,18 @@ class FuseTable(list):
if (p.field_name != last_name):
if last_name != '':
rows += [" NULL",
"};\n" ]
rows += [ "const esp_efuse_desc_t* " + "ESP_EFUSE_" + p.field_name + "[] = {" ]
"};\n"]
rows += ["const esp_efuse_desc_t* " + "ESP_EFUSE_" + p.field_name + "[] = {"]
last_name = p.field_name
index = str(0) if str(p.group) == "" else str(p.group)
rows += [ " &" + p.field_name + "[" + index + "], \t\t// " + p.comment ]
rows += [" &" + p.field_name + "[" + index + "], \t\t// " + p.comment]
rows += [" NULL",
"};\n" ]
return '\n'.join(rows) + "\n"
class FuseDefinition(object):
class FuseDefinition(object):
def __init__(self):
self.field_name = ""
self.group = ""
@@ -297,7 +290,7 @@ class FuseDefinition(object):
def from_csv(cls, line):
""" Parse a line from the CSV """
line_w_defaults = line + ",,,," # lazy way to support default fields
fields = [ f.strip() for f in line_w_defaults.split(",") ]
fields = [f.strip() for f in line_w_defaults.split(",")]
res = FuseDefinition()
res.field_name = fields[0]
@@ -339,7 +332,7 @@ class FuseDefinition(object):
elif coding_scheme == CODE_SCHEME["REPEAT"]:
max_bits = 128
else:
ValidationError(self, "Unknown coding scheme")
raise ValidationError(self, "Unknown coding scheme")
return max_bits
def verify(self, type_table):
@@ -351,13 +344,12 @@ class FuseDefinition(object):
if type_table is not None:
if type_table == "custom_table":
if self.efuse_block != "EFUSE_BLK3":
ValidationError(self, "custom_table should use only EFUSE_BLK3")
raise ValidationError(self, "custom_table should use only EFUSE_BLK3")
max_bits = self.get_max_bits_of_block()
if self.bit_start + self.bit_count > max_bits:
print("(%d + %d) > %d" % (self.bit_start, self.bit_count, max_bits))
raise ValidationError(self, "The field is outside the boundaries of the %s block" % (self.efuse_block))
raise ValidationError(self, "The field is outside the boundaries(max_bits = %d) of the %s block" % (max_bits, self.efuse_block))
def get_full_name(self):
def get_postfix(group):
@@ -376,26 +368,25 @@ class FuseDefinition(object):
str(self.bit_start),
str(self.bit_count) + "}, \t // " + self.comment])
def process_input_file(file, type_table):
status("Parsing efuse CSV input file " + file.name + " ...")
input = file.read()
table = FuseTable.from_csv(input, type_table)
table = FuseTable.from_csv(input)
status("Verifying efuse table...")
table.verify(type_table)
return table
def ckeck_md5_in_file(md5, filename):
if os.path.exists(filename):
with open(filename,'r') as f:
with open(filename, 'r') as f:
for line in f:
if md5 in line:
return True
return False
def touch(fname, times=None):
with open(fname, 'a'):
os.utime(fname, times)
def create_output_files(name, output_table, debug):
file_name = os.path.splitext(os.path.basename(name))[0]
gen_dir = os.path.dirname(name)
@@ -423,25 +414,19 @@ def create_output_files(name, output_table, debug):
f.write(output)
f.close()
else:
print("touch: %s.c" % (file_name))
touch(file_c_path)
print("Source files do not require updating correspond to csv file.")
def main():
global quiet
global coding_scheme
global custom_table_use_BLK1
global custom_table_use_BLK2
global common_table_fix_size
parser = argparse.ArgumentParser(description='ESP32 eFuse Manager')
parser.add_argument('--quiet', '-q', help="Don't print non-critical status messages to stderr", action='store_true')
parser.add_argument('--debug', help='Create header file with debug info', default=False, action="store_false")
parser.add_argument('--info', help='Print info about range of used bits', default=False, action="store_true")
parser.add_argument('--custom_table_use_BLK1', help='BLK1 is used for custom purpose', default=False, action="store_true")
parser.add_argument('--custom_table_use_BLK2', help='BLK2 is used for custom purpose', default=False, action="store_true")
parser.add_argument('--common_table_fix_size_secure_key_and_encrypt_key', help='Size of secure_key and encrypt_key will limit to coding scheme', default=False, action="store_true")
parser.add_argument('--coding_scheme', help='Coding scheme', type=int, default=None)
parser.add_argument('--coding_scheme', help='Coding scheme', type=int, default=0)
parser.add_argument('common_input', help='Path to common CSV file to parse.', type=argparse.FileType('rb'))
parser.add_argument('custom_input', help='Path to custom CSV file to parse.', type=argparse.FileType('rb'), nargs='?', default=None)
@@ -454,10 +439,9 @@ def main():
print("eFuse coding scheme: 3/4")
elif CODE_SCHEME["REPEAT"] == coding_scheme:
print("eFuse coding scheme: REPEAT")
custom_table_use_BLK1 = args.custom_table_use_BLK1
custom_table_use_BLK2 = args.custom_table_use_BLK2
common_table_fix_size = args.common_table_fix_size_secure_key_and_encrypt_key
else:
raise InputError("unknown CODE_SCHEME = %s" % (coding_scheme))
quiet = args.quiet
debug = args.debug
info = args.info
@@ -486,8 +470,7 @@ class InputError(RuntimeError):
class ValidationError(InputError):
def __init__(self, p, message):
super(ValidationError, self).__init__(
"Entry %s invalid: %s" % (p.field_name, message))
super(ValidationError, self).__init__("Entry %s invalid: %s" % (p.field_name, message))
if __name__ == '__main__':
try: