efuse_table_gen.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. #!/usr/bin/env python
  2. #
  3. # ESP32 efuse table generation tool
  4. #
  5. # Converts efuse table to header file efuse_table.h.
  6. #
  7. # Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD
  8. #
  9. # Licensed under the Apache License, Version 2.0 (the "License");
  10. # you may not use this file except in compliance with the License.
  11. # You may obtain a copy of the License at
  12. #
  13. # http:#www.apache.org/licenses/LICENSE-2.0
  14. #
  15. # Unless required by applicable law or agreed to in writing, software
  16. # distributed under the License is distributed on an "AS IS" BASIS,
  17. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. # See the License for the specific language governing permissions and
  19. # limitations under the License.
  20. from __future__ import print_function, division
  21. import argparse
  22. import os
  23. import re
  24. import sys
  25. import hashlib
  26. __version__ = '1.0'
  27. quiet = False
  28. max_blk_len = 256
  29. copyright = '''// Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD
  30. //
  31. // Licensed under the Apache License, Version 2.0 (the "License");
  32. // you may not use this file except in compliance with the License.
  33. // You may obtain a copy of the License at",
  34. //
  35. // http://www.apache.org/licenses/LICENSE-2.0
  36. //
  37. // Unless required by applicable law or agreed to in writing, software
  38. // distributed under the License is distributed on an "AS IS" BASIS,
  39. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  40. // See the License for the specific language governing permissions and
  41. // limitations under the License
  42. '''
  43. def status(msg):
  44. """ Print status message to stderr """
  45. if not quiet:
  46. critical(msg)
  47. def critical(msg):
  48. """ Print critical message to stderr """
  49. sys.stderr.write(msg)
  50. sys.stderr.write('\n')
  51. class FuseTable(list):
  52. def __init__(self):
  53. super(FuseTable, self).__init__(self)
  54. self.md5_digest_table = ""
  55. @classmethod
  56. def from_csv(cls, csv_contents):
  57. res = FuseTable()
  58. lines = csv_contents.splitlines()
  59. def expand_vars(f):
  60. f = os.path.expandvars(f)
  61. m = re.match(r'(?<!\\)\$([A-Za-z_][A-Za-z0-9_]*)', f)
  62. if m:
  63. raise InputError("unknown variable '%s'" % (m.group(1)))
  64. return f
  65. for line_no in range(len(lines)):
  66. line = expand_vars(lines[line_no]).strip()
  67. if line.startswith("#") or len(line) == 0:
  68. continue
  69. try:
  70. res.append(FuseDefinition.from_csv(line))
  71. except InputError as e:
  72. raise InputError("Error at line %d: %s" % (line_no + 1, e))
  73. except Exception:
  74. critical("Unexpected error parsing line %d: %s" % (line_no + 1, line))
  75. raise
  76. # fix up missing bit_start
  77. last_efuse_block = None
  78. for e in res:
  79. if last_efuse_block != e.efuse_block:
  80. last_end = 0
  81. if e.bit_start is None:
  82. e.bit_start = last_end
  83. last_end = e.bit_start + e.bit_count
  84. last_efuse_block = e.efuse_block
  85. res.verify_duplicate_name()
  86. # fix up missing field_name
  87. last_field = None
  88. for e in res:
  89. if e.field_name == "" and last_field is None:
  90. raise InputError("Error at line %d: %s missing field name" % (line_no + 1, e))
  91. elif e.field_name == "" and last_field is not None:
  92. e.field_name = last_field.field_name
  93. last_field = e
  94. # fill group
  95. names = [p.field_name for p in res]
  96. duplicates = set(n for n in names if names.count(n) > 1)
  97. if len(duplicates) != 0:
  98. i_count = 0
  99. for p in res:
  100. if len(duplicates.intersection([p.field_name])) != 0:
  101. p.group = str(i_count)
  102. i_count += 1
  103. else:
  104. i_count = 0
  105. res.verify_duplicate_name()
  106. # clac md5 for table
  107. res.calc_md5()
  108. return res
  109. def verify_duplicate_name(self):
  110. # check on duplicate name
  111. names = [p.field_name for p in self]
  112. duplicates = set(n for n in names if names.count(n) > 1)
  113. # print sorted duplicate partitions by name
  114. if len(duplicates) != 0:
  115. fl_error = False
  116. for p in self:
  117. field_name = p.field_name + p.group
  118. if field_name != "" and len(duplicates.intersection([field_name])) != 0:
  119. fl_error = True
  120. print("Field at %s, %s, %s, %s have dublicate field_name" %
  121. (p.field_name, p.efuse_block, p.bit_start, p.bit_count))
  122. if fl_error is True:
  123. raise InputError("Field names must be unique")
  124. def verify(self, type_table=None):
  125. for p in self:
  126. p.verify(type_table)
  127. self.verify_duplicate_name()
  128. # check for overlaps
  129. last = None
  130. for p in sorted(self, key=lambda x:(x.efuse_block, x.bit_start)):
  131. if last is not None and last.efuse_block == p.efuse_block and p.bit_start < last.bit_start + last.bit_count:
  132. raise InputError("Field at %s, %s, %s, %s overlaps %s, %s, %s, %s" %
  133. (p.field_name, p.efuse_block, p.bit_start, p.bit_count,
  134. last.field_name, last.efuse_block, last.bit_start, last.bit_count))
  135. last = p
  136. def calc_md5(self):
  137. txt_table = ''
  138. for p in self:
  139. txt_table += "%s %s %d %s %s" % (p.field_name, p.efuse_block, p.bit_start, str(p.get_bit_count()), p.comment) + "\n"
  140. self.md5_digest_table = hashlib.md5(txt_table.encode('utf-8')).hexdigest()
  141. def show_range_used_bits(self):
  142. # print used and free bits
  143. rows = ''
  144. rows += 'Sorted efuse table:\n'
  145. num = 1
  146. rows += "{0} \t{1:<30} \t{2} \t{3} \t{4}".format("#", "field_name", "efuse_block", "bit_start", "bit_count") + "\n"
  147. for p in sorted(self, key=lambda x:(x.efuse_block, x.bit_start)):
  148. 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"
  149. num += 1
  150. rows += '\nUsed bits in efuse table:\n'
  151. last = None
  152. for p in sorted(self, key=lambda x:(x.efuse_block, x.bit_start)):
  153. if last is None:
  154. rows += '%s \n[%d ' % (p.efuse_block, p.bit_start)
  155. if last is not None:
  156. if last.efuse_block != p.efuse_block:
  157. rows += '%d] \n\n%s \n[%d ' % (last.bit_start + last.bit_count - 1, p.efuse_block, p.bit_start)
  158. elif last.bit_start + last.bit_count != p.bit_start:
  159. rows += '%d] [%d ' % (last.bit_start + last.bit_count - 1, p.bit_start)
  160. last = p
  161. rows += '%d] \n' % (last.bit_start + last.bit_count - 1)
  162. rows += '\nNote: Not printed ranges are free for using. (bits in EFUSE_BLK0 are reserved for Espressif)\n'
  163. return rows
  164. def get_str_position_last_free_bit_in_blk(self, blk):
  165. last_used_bit = 0
  166. for p in self:
  167. if p.efuse_block == blk:
  168. if p.define is not None:
  169. return p.get_bit_count()
  170. else:
  171. if last_used_bit < p.bit_start + p.bit_count:
  172. last_used_bit = p.bit_start + p.bit_count
  173. if last_used_bit == 0:
  174. return None
  175. return str(last_used_bit)
  176. def to_header(self, file_name):
  177. rows = [copyright]
  178. rows += ["#ifdef __cplusplus",
  179. 'extern "C" {',
  180. "#endif",
  181. "",
  182. "",
  183. "// md5_digest_table " + self.md5_digest_table,
  184. "// This file was generated from the file " + file_name + ".csv. DO NOT CHANGE THIS FILE MANUALLY.",
  185. "// If you want to change some fields, you need to change " + file_name + ".csv file",
  186. "// then run `efuse_common_table` or `efuse_custom_table` command it will generate this file.",
  187. "// To show efuse_table run the command 'show_efuse_table'.",
  188. "",
  189. ""]
  190. last_field_name = ''
  191. for p in self:
  192. if (p.field_name != last_field_name):
  193. rows += ["extern const esp_efuse_desc_t* " + "ESP_EFUSE_" + p.field_name + "[];"]
  194. last_field_name = p.field_name
  195. rows += ["",
  196. "#ifdef __cplusplus",
  197. "}",
  198. "#endif",
  199. ""]
  200. return '\n'.join(rows) + "\n"
  201. def to_c_file(self, file_name, debug):
  202. rows = [copyright]
  203. rows += ['#include "sdkconfig.h"',
  204. '#include "esp_efuse.h"',
  205. '#include <assert.h>',
  206. '#include "' + file_name + '.h"',
  207. "",
  208. "// md5_digest_table " + self.md5_digest_table,
  209. "// This file was generated from the file " + file_name + ".csv. DO NOT CHANGE THIS FILE MANUALLY.",
  210. "// If you want to change some fields, you need to change " + file_name + ".csv file",
  211. "// then run `efuse_common_table` or `efuse_custom_table` command it will generate this file.",
  212. "// To show efuse_table run the command 'show_efuse_table'."]
  213. rows += [""]
  214. rows += ["#define MAX_BLK_LEN CONFIG_EFUSE_MAX_BLK_LEN"]
  215. rows += [""]
  216. last_free_bit_blk1 = self.get_str_position_last_free_bit_in_blk("EFUSE_BLK1")
  217. last_free_bit_blk2 = self.get_str_position_last_free_bit_in_blk("EFUSE_BLK2")
  218. last_free_bit_blk3 = self.get_str_position_last_free_bit_in_blk("EFUSE_BLK3")
  219. rows += ["// The last free bit in the block is counted over the entire file."]
  220. if last_free_bit_blk1 is not None:
  221. rows += ["#define LAST_FREE_BIT_BLK1 " + last_free_bit_blk1]
  222. if last_free_bit_blk2 is not None:
  223. rows += ["#define LAST_FREE_BIT_BLK2 " + last_free_bit_blk2]
  224. if last_free_bit_blk3 is not None:
  225. rows += ["#define LAST_FREE_BIT_BLK3 " + last_free_bit_blk3]
  226. rows += [""]
  227. if last_free_bit_blk1 is not None:
  228. rows += ['_Static_assert(LAST_FREE_BIT_BLK1 <= MAX_BLK_LEN, "The eFuse table does not match the coding scheme. '
  229. 'Edit the table and restart the efuse_common_table or efuse_custom_table command to regenerate the new files.");']
  230. if last_free_bit_blk2 is not None:
  231. rows += ['_Static_assert(LAST_FREE_BIT_BLK2 <= MAX_BLK_LEN, "The eFuse table does not match the coding scheme. '
  232. 'Edit the table and restart the efuse_common_table or efuse_custom_table command to regenerate the new files.");']
  233. if last_free_bit_blk3 is not None:
  234. rows += ['_Static_assert(LAST_FREE_BIT_BLK3 <= MAX_BLK_LEN, "The eFuse table does not match the coding scheme. '
  235. 'Edit the table and restart the efuse_common_table or efuse_custom_table command to regenerate the new files.");']
  236. rows += [""]
  237. last_name = ''
  238. for p in self:
  239. if (p.field_name != last_name):
  240. if last_name != '':
  241. rows += ["};\n"]
  242. rows += ["static const esp_efuse_desc_t " + p.field_name + "[] = {"]
  243. last_name = p.field_name
  244. rows += [p.to_struct(debug) + ","]
  245. rows += ["};\n"]
  246. rows += ["\n\n\n"]
  247. last_name = ''
  248. for p in self:
  249. if (p.field_name != last_name):
  250. if last_name != '':
  251. rows += [" NULL",
  252. "};\n"]
  253. rows += ["const esp_efuse_desc_t* " + "ESP_EFUSE_" + p.field_name + "[] = {"]
  254. last_name = p.field_name
  255. index = str(0) if str(p.group) == "" else str(p.group)
  256. rows += [" &" + p.field_name + "[" + index + "], \t\t// " + p.comment]
  257. rows += [" NULL",
  258. "};\n"]
  259. return '\n'.join(rows) + "\n"
  260. class FuseDefinition(object):
  261. def __init__(self):
  262. self.field_name = ""
  263. self.group = ""
  264. self.efuse_block = ""
  265. self.bit_start = None
  266. self.bit_count = None
  267. self.define = None
  268. self.comment = ""
  269. @classmethod
  270. def from_csv(cls, line):
  271. """ Parse a line from the CSV """
  272. line_w_defaults = line + ",,,," # lazy way to support default fields
  273. fields = [f.strip() for f in line_w_defaults.split(",")]
  274. res = FuseDefinition()
  275. res.field_name = fields[0]
  276. res.efuse_block = res.parse_block(fields[1])
  277. res.bit_start = res.parse_num(fields[2])
  278. res.bit_count = res.parse_bit_count(fields[3])
  279. if res.bit_count is None or res.bit_count == 0:
  280. raise InputError("Field bit_count can't be empty")
  281. res.comment = fields[4]
  282. return res
  283. def parse_num(self, strval):
  284. if strval == "":
  285. return None # Field will fill in default
  286. return self.parse_int(strval)
  287. def parse_bit_count(self, strval):
  288. if strval == "MAX_BLK_LEN":
  289. self.define = strval
  290. return self.get_max_bits_of_block()
  291. else:
  292. return self.parse_num(strval)
  293. def parse_int(self, v):
  294. try:
  295. return int(v, 0)
  296. except ValueError:
  297. raise InputError("Invalid field value %s" % v)
  298. def parse_block(self, strval):
  299. if strval == "":
  300. raise InputError("Field 'efuse_block' can't be left empty.")
  301. if strval not in ["EFUSE_BLK0", "EFUSE_BLK1", "EFUSE_BLK2", "EFUSE_BLK3"]:
  302. raise InputError("Field 'efuse_block' should consist from EFUSE_BLK0..EFUSE_BLK3")
  303. return strval
  304. def get_max_bits_of_block(self):
  305. '''common_table: EFUSE_BLK0, EFUSE_BLK1, EFUSE_BLK2, EFUSE_BLK3
  306. custom_table: ----------, ----------, ----------, EFUSE_BLK3(some reserved in common_table)
  307. '''
  308. if self.efuse_block == "EFUSE_BLK0":
  309. return 256
  310. else:
  311. return max_blk_len
  312. def verify(self, type_table):
  313. if self.efuse_block is None:
  314. raise ValidationError(self, "efuse_block field is not set")
  315. if self.bit_count is None:
  316. raise ValidationError(self, "bit_count field is not set")
  317. if type_table is not None:
  318. if type_table == "custom_table":
  319. if self.efuse_block != "EFUSE_BLK3":
  320. raise ValidationError(self, "custom_table should use only EFUSE_BLK3")
  321. max_bits = self.get_max_bits_of_block()
  322. if self.bit_start + self.bit_count > max_bits:
  323. raise ValidationError(self, "The field is outside the boundaries(max_bits = %d) of the %s block" % (max_bits, self.efuse_block))
  324. def get_full_name(self):
  325. def get_postfix(group):
  326. postfix = ""
  327. if group != "":
  328. postfix = "_PART_" + group
  329. return postfix
  330. return self.field_name + get_postfix(self.group)
  331. def get_bit_count(self, check_define=True):
  332. if check_define is True and self.define is not None:
  333. return self.define
  334. else:
  335. return self.bit_count
  336. def to_struct(self, debug):
  337. start = " {"
  338. if debug is True:
  339. start = " {" + '"' + self.field_name + '" ,'
  340. return ", ".join([start + self.efuse_block,
  341. str(self.bit_start),
  342. str(self.get_bit_count()) + "}, \t // " + self.comment])
  343. def process_input_file(file, type_table):
  344. status("Parsing efuse CSV input file " + file.name + " ...")
  345. input = file.read()
  346. table = FuseTable.from_csv(input)
  347. status("Verifying efuse table...")
  348. table.verify(type_table)
  349. return table
  350. def ckeck_md5_in_file(md5, filename):
  351. if os.path.exists(filename):
  352. with open(filename, 'r') as f:
  353. for line in f:
  354. if md5 in line:
  355. return True
  356. return False
  357. def create_output_files(name, output_table, debug):
  358. file_name = os.path.splitext(os.path.basename(name))[0]
  359. gen_dir = os.path.dirname(name)
  360. dir_for_file_h = gen_dir + "/include"
  361. try:
  362. os.stat(dir_for_file_h)
  363. except Exception:
  364. os.mkdir(dir_for_file_h)
  365. file_h_path = os.path.join(dir_for_file_h, file_name + ".h")
  366. file_c_path = os.path.join(gen_dir, file_name + ".c")
  367. # src files are the same
  368. if ckeck_md5_in_file(output_table.md5_digest_table, file_c_path) is False:
  369. status("Creating efuse *.h file " + file_h_path + " ...")
  370. output = output_table.to_header(file_name)
  371. with open(file_h_path, 'w') as f:
  372. f.write(output)
  373. status("Creating efuse *.c file " + file_c_path + " ...")
  374. output = output_table.to_c_file(file_name, debug)
  375. with open(file_c_path, 'w') as f:
  376. f.write(output)
  377. else:
  378. print("Source files do not require updating correspond to csv file.")
  379. def main():
  380. if sys.version_info[0] < 3:
  381. print("WARNING: Support for Python 2 is deprecated and will be removed in future versions.", file=sys.stderr)
  382. elif sys.version_info[0] == 3 and sys.version_info[1] < 6:
  383. print("WARNING: Python 3 versions older than 3.6 are not supported.", file=sys.stderr)
  384. global quiet
  385. global max_blk_len
  386. parser = argparse.ArgumentParser(description='ESP32 eFuse Manager')
  387. parser.add_argument('--quiet', '-q', help="Don't print non-critical status messages to stderr", action='store_true')
  388. parser.add_argument('--debug', help='Create header file with debug info', default=False, action="store_false")
  389. parser.add_argument('--info', help='Print info about range of used bits', default=False, action="store_true")
  390. parser.add_argument('--max_blk_len', help='Max number of bits in BLK1, BLK2 and BLK3', type=int, default=256)
  391. parser.add_argument('common_input', help='Path to common CSV file to parse.', type=argparse.FileType('r'))
  392. parser.add_argument('custom_input', help='Path to custom CSV file to parse.', type=argparse.FileType('r'), nargs='?', default=None)
  393. args = parser.parse_args()
  394. max_blk_len = args.max_blk_len
  395. print("Max number of bits in BLK %d" % (max_blk_len))
  396. if max_blk_len not in [256, 192, 128]:
  397. raise InputError("Unsupported block length = %d" % (max_blk_len))
  398. quiet = args.quiet
  399. debug = args.debug
  400. info = args.info
  401. common_table = process_input_file(args.common_input, "common_table")
  402. two_table = common_table
  403. if args.custom_input is not None:
  404. custom_table = process_input_file(args.custom_input, "custom_table")
  405. two_table += custom_table
  406. two_table.verify()
  407. # save files.
  408. if info is False:
  409. if args.custom_input is None:
  410. create_output_files(args.common_input.name, common_table, debug)
  411. else:
  412. create_output_files(args.custom_input.name, custom_table, debug)
  413. else:
  414. print(two_table.show_range_used_bits())
  415. return 0
  416. class InputError(RuntimeError):
  417. def __init__(self, e):
  418. super(InputError, self).__init__(e)
  419. class ValidationError(InputError):
  420. def __init__(self, p, message):
  421. super(ValidationError, self).__init__("Entry %s invalid: %s" % (p.field_name, message))
  422. if __name__ == '__main__':
  423. try:
  424. main()
  425. except InputError as e:
  426. print(e, file=sys.stderr)
  427. sys.exit(2)