efuse_table_gen.py 20 KB

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