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# SPDX-FileCopyrightText: 2017-2022 Espressif Systems (Shanghai) CO LTD
8#
9# SPDX-License-Identifier: Apache-2.0
10from __future__ import division, print_function
11
12import argparse
13import hashlib
14import os
15import re
16import sys
17from datetime import datetime
18
19__version__ = '1.0'
20
21quiet = False
22max_blk_len = 256
23idf_target = 'esp32'
24
25
26def get_copyright():
27    copyright_str = '''/*
28 * SPDX-FileCopyrightText: 2017-%d Espressif Systems (Shanghai) CO LTD
29 *
30 * SPDX-License-Identifier: Apache-2.0
31 */
32'''
33    return copyright_str % datetime.today().year
34
35
36def status(msg):
37    """ Print status message to stderr """
38    if not quiet:
39        critical(msg)
40
41
42def critical(msg):
43    """ Print critical message to stderr """
44    sys.stderr.write(msg)
45    sys.stderr.write('\n')
46
47
48class FuseTable(list):
49    def __init__(self):
50        super(FuseTable, self).__init__(self)
51        self.md5_digest_table = ''
52
53    @classmethod
54    def from_csv(cls, csv_contents):
55        res = FuseTable()
56        lines = csv_contents.splitlines()
57
58        def expand_vars(f):
59            f = os.path.expandvars(f)
60            m = re.match(r'(?<!\\)\$([A-Za-z_][A-Za-z0-9_]*)', f)
61            if m:
62                raise InputError("unknown variable '%s'" % (m.group(1)))
63            return f
64
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
77        # fix up missing bit_start
78        last_efuse_block = None
79        for i in res:
80            if last_efuse_block != i.efuse_block:
81                last_end = 0
82            if i.bit_start is None:
83                i.bit_start = last_end
84            last_end = i.bit_start + i.bit_count
85            last_efuse_block = i.efuse_block
86
87        res.verify_duplicate_name()
88
89        # fix up missing field_name
90        last_field = None
91        for i in res:
92            if i.field_name == '' and last_field is None:
93                raise InputError('Error at line %d: %s missing field name' % (line_no + 1, i))
94            elif i.field_name == '' and last_field is not None:
95                i.field_name = last_field.field_name
96            last_field = i
97
98        # fill group
99        names = [p.field_name for p in res]
100        duplicates = set(n for n in names if names.count(n) > 1)
101        for dname in duplicates:
102            i_count = 0
103            for p in res:
104                if p.field_name != dname:
105                    continue
106                if len(duplicates.intersection([p.field_name])) != 0:
107                    p.group = str(i_count)
108                    i_count += 1
109                else:
110                    i_count = 0
111        res.verify_duplicate_name()
112
113        # clac md5 for table
114        res.calc_md5()
115
116        return res
117
118    def verify_duplicate_name(self):
119        # check on duplicate name
120        names = [p.field_name for p in self]
121        names += [name.replace('.', '_') for name in names if '.' in name]
122        duplicates = set(n for n in names if names.count(n) > 1)
123
124        # print sorted duplicate partitions by name
125        if len(duplicates) != 0:
126            fl_error = False
127            for p in self:
128                field_name = p.field_name + p.group
129                if field_name != '' and len(duplicates.intersection([field_name])) != 0:
130                    fl_error = True
131                    print('Field at %s, %s, %s, %s have dublicate field_name' %
132                          (p.field_name, p.efuse_block, p.bit_start, p.bit_count))
133            if fl_error is True:
134                raise InputError('Field names must be unique')
135
136    def check_struct_field_name(self):
137        # check that stuctured fields have a root field
138        for p in self:
139            if '.' in p.field_name:
140                name = ''
141                for sub in p.field_name.split('.')[:-1]:
142                    name = sub if name == '' else name + '.' + sub
143                    missed_name = True
144                    for d in self:
145                        if p is not d and p.efuse_block == d.efuse_block and name == d.field_name:
146                            missed_name = False
147                    if missed_name:
148                        raise InputError('%s is not found' % name)
149
150    def verify(self, type_table=None):
151        def check(p, n):
152            left = n.bit_start
153            right = n.bit_start + n.bit_count - 1
154            start = p.bit_start
155            end = p.bit_start + p.bit_count - 1
156            if left <= start <= right:
157                if left <= end <= right:
158                    return 'included in'  # [n  [p...p]  n]
159                return 'intersected with'  # [n  [p..n]..p]
160            if left <= end <= right:
161                return 'intersected with'  # [p..[n..p] n]
162            if start <= left and right <= end:
163                return 'wraps'  # [p  [n...n]  p]
164            return 'ok'  # [p] [n]  or  [n] [p]
165
166        def print_error(p, n, state):
167            raise InputError('Field at %s, %s, %s, %s  %s  %s, %s, %s, %s' %
168                             (p.field_name, p.efuse_block, p.bit_start, p.bit_count, state,
169                              n.field_name, n.efuse_block, n.bit_start, n.bit_count))
170        for p in self:
171            p.verify(type_table)
172
173        self.verify_duplicate_name()
174        if type_table != 'custom_table':
175            # check will be done for common and custom tables together
176            self.check_struct_field_name()
177
178        # check for overlaps
179        for p in self:
180            for n in self:
181                if p is not n and p.efuse_block == n.efuse_block:
182                    state = check(p, n)
183                    if state != 'ok':
184                        if '.' in p.field_name:
185                            name = ''
186                            for sub in p.field_name.split('.'):
187                                name = sub if name == '' else name + '.' + sub
188                                for d in self:
189                                    if p is not d and p.efuse_block == d.efuse_block and name == d.field_name:
190                                        state = check(p, d)
191                                        if state == 'included in':
192                                            break
193                                        elif state != 'intersected with':
194                                            state = 'out of range'
195                                        print_error(p, d, state)
196                            continue
197                        elif '.' in n.field_name:
198                            continue
199                        print_error(p, n, state)
200
201    def calc_md5(self):
202        txt_table = ''
203        for p in self:
204            txt_table += '%s %s %d %s %s' % (p.field_name, p.efuse_block, p.bit_start, str(p.get_bit_count()), p.comment) + '\n'
205        self.md5_digest_table = hashlib.md5(txt_table.encode('utf-8')).hexdigest()
206
207    def show_range_used_bits(self):
208        # print used and free bits
209        rows = ''
210        rows += 'Sorted efuse table:\n'
211        num = 1
212        rows += '{0} \t{1:<30} \t{2} \t{3} \t{4}'.format('#', 'field_name', 'efuse_block', 'bit_start', 'bit_count') + '\n'
213        for p in sorted(self, key=lambda x:(x.efuse_block, x.bit_start)):
214            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'
215            num += 1
216
217        rows += '\nUsed bits in efuse table:\n'
218        last = None
219        for p in sorted(self, key=lambda x:(x.efuse_block, x.bit_start)):
220            if last is None:
221                rows += '%s \n[%d ' % (p.efuse_block, p.bit_start)
222            if last is not None:
223                if last.efuse_block != p.efuse_block:
224                    rows += '%d] \n\n%s \n[%d ' % (last.bit_start + last.bit_count - 1, p.efuse_block, p.bit_start)
225                elif last.bit_start + last.bit_count != p.bit_start:
226                    rows += '%d] [%d ' % (last.bit_start + last.bit_count - 1, p.bit_start)
227            last = p
228        rows += '%d] \n' % (last.bit_start + last.bit_count - 1)
229        rows += '\nNote: Not printed ranges are free for using. (bits in EFUSE_BLK0 are reserved for Espressif)\n'
230        return rows
231
232    def get_str_position_last_free_bit_in_blk(self, blk):
233        last_used_bit = 0
234        for p in self:
235            if p.efuse_block == blk:
236                if p.define is not None:
237                    return p.get_bit_count()
238                else:
239                    if last_used_bit < p.bit_start + p.bit_count:
240                        last_used_bit = p.bit_start + p.bit_count
241        if last_used_bit == 0:
242            return None
243        return str(last_used_bit)
244
245    def to_header(self, file_name):
246        rows = [get_copyright()]
247        rows += ['#ifdef __cplusplus',
248                 'extern "C" {',
249                 '#endif',
250                 '',
251                 '#include "esp_efuse.h"',
252                 '',
253                 '// md5_digest_table ' + self.md5_digest_table,
254                 '// This file was generated from the file ' + file_name + '.csv. DO NOT CHANGE THIS FILE MANUALLY.',
255                 '// If you want to change some fields, you need to change ' + file_name + '.csv file',
256                 '// then run `efuse_common_table` or `efuse_custom_table` command it will generate this file.',
257                 "// To show efuse_table run the command 'show_efuse_table'.",
258                 '',
259                 '']
260
261        last_field_name = ''
262        for p in self:
263            if (p.field_name != last_field_name):
264                name = 'ESP_EFUSE_' + p.field_name.replace('.', '_')
265                rows += ['extern const esp_efuse_desc_t* ' + name + '[];']
266                for alt_name in p.get_alt_names():
267                    alt_name = 'ESP_EFUSE_' + alt_name.replace('.', '_')
268                    rows += ['#define ' + alt_name + ' ' + name]
269                last_field_name = p.field_name
270
271        rows += ['',
272                 '#ifdef __cplusplus',
273                 '}',
274                 '#endif',
275                 '']
276        return '\n'.join(rows)
277
278    def to_c_file(self, file_name, debug):
279        rows = [get_copyright()]
280        rows += ['#include "sdkconfig.h"',
281                 '#include "esp_efuse.h"',
282                 '#include <assert.h>',
283                 '#include "' + file_name + '.h"',
284                 '',
285                 '// md5_digest_table ' + self.md5_digest_table,
286                 '// This file was generated from the file ' + file_name + '.csv. DO NOT CHANGE THIS FILE MANUALLY.',
287                 '// If you want to change some fields, you need to change ' + file_name + '.csv file',
288                 '// then run `efuse_common_table` or `efuse_custom_table` command it will generate this file.',
289                 "// To show efuse_table run the command 'show_efuse_table'."]
290
291        rows += ['']
292
293        if idf_target == 'esp32':
294            rows += ['#define MAX_BLK_LEN CONFIG_EFUSE_MAX_BLK_LEN']
295
296            rows += ['']
297
298            last_free_bit_blk1 = self.get_str_position_last_free_bit_in_blk('EFUSE_BLK1')
299            last_free_bit_blk2 = self.get_str_position_last_free_bit_in_blk('EFUSE_BLK2')
300            last_free_bit_blk3 = self.get_str_position_last_free_bit_in_blk('EFUSE_BLK3')
301
302            rows += ['// The last free bit in the block is counted over the entire file.']
303            if last_free_bit_blk1 is not None:
304                rows += ['#define LAST_FREE_BIT_BLK1 ' + last_free_bit_blk1]
305            if last_free_bit_blk2 is not None:
306                rows += ['#define LAST_FREE_BIT_BLK2 ' + last_free_bit_blk2]
307            if last_free_bit_blk3 is not None:
308                rows += ['#define LAST_FREE_BIT_BLK3 ' + last_free_bit_blk3]
309
310            rows += ['']
311
312            if last_free_bit_blk1 is not None:
313                rows += ['_Static_assert(LAST_FREE_BIT_BLK1 <= MAX_BLK_LEN, "The eFuse table does not match the coding scheme. '
314                         'Edit the table and restart the efuse_common_table or efuse_custom_table command to regenerate the new files.");']
315            if last_free_bit_blk2 is not None:
316                rows += ['_Static_assert(LAST_FREE_BIT_BLK2 <= MAX_BLK_LEN, "The eFuse table does not match the coding scheme. '
317                         'Edit the table and restart the efuse_common_table or efuse_custom_table command to regenerate the new files.");']
318            if last_free_bit_blk3 is not None:
319                rows += ['_Static_assert(LAST_FREE_BIT_BLK3 <= MAX_BLK_LEN, "The eFuse table does not match the coding scheme. '
320                         'Edit the table and restart the efuse_common_table or efuse_custom_table command to regenerate the new files.");']
321
322            rows += ['']
323
324        last_name = ''
325        for p in self:
326            if (p.field_name != last_name):
327                if last_name != '':
328                    rows += ['};\n']
329                rows += ['static const esp_efuse_desc_t ' + p.field_name.replace('.', '_') + '[] = {']
330                last_name = p.field_name
331            rows += [p.to_struct(debug) + ',']
332        rows += ['};\n']
333        rows += ['\n\n\n']
334
335        last_name = ''
336        for p in self:
337            if (p.field_name != last_name):
338                if last_name != '':
339                    rows += ['    NULL',
340                             '};\n']
341                rows += ['const esp_efuse_desc_t* ' + 'ESP_EFUSE_' + p.field_name.replace('.', '_') + '[] = {']
342            last_name = p.field_name
343            index = str(0) if str(p.group) == '' else str(p.group)
344            rows += ['    &' + p.field_name.replace('.', '_') + '[' + index + '],    \t\t// ' + p.comment]
345        rows += ['    NULL',
346                 '};\n']
347
348        return '\n'.join(rows)
349
350
351class FuseDefinition(object):
352    def __init__(self):
353        self.field_name = ''
354        self.group = ''
355        self.efuse_block = ''
356        self.bit_start = None
357        self.bit_count = None
358        self.define = None
359        self.comment = ''
360
361    @classmethod
362    def from_csv(cls, line):
363        """ Parse a line from the CSV """
364        line_w_defaults = line + ',,,,'  # lazy way to support default fields
365        fields = [f.strip() for f in line_w_defaults.split(',')]
366
367        res = FuseDefinition()
368        res.field_name = fields[0]
369        res.efuse_block = res.parse_block(fields[1])
370        res.bit_start = res.parse_num(fields[2])
371        res.bit_count = res.parse_bit_count(fields[3])
372        if res.bit_count is None or res.bit_count == 0:
373            raise InputError("Field bit_count can't be empty")
374        res.comment = fields[4]
375        return res
376
377    def parse_num(self, strval):
378        if strval == '':
379            return None  # Field will fill in default
380        return self.parse_int(strval)
381
382    def parse_bit_count(self, strval):
383        if strval == 'MAX_BLK_LEN':
384            self.define = strval
385            return self.get_max_bits_of_block()
386        else:
387            return self.parse_num(strval)
388
389    def parse_int(self, v):
390        try:
391            return int(v, 0)
392        except ValueError:
393            raise InputError('Invalid field value %s' % v)
394
395    def parse_block(self, strval):
396        if strval == '':
397            raise InputError("Field 'efuse_block' can't be left empty.")
398        if idf_target == 'esp32':
399            if strval not in ['EFUSE_BLK0', 'EFUSE_BLK1', 'EFUSE_BLK2', 'EFUSE_BLK3']:
400                raise InputError("Field 'efuse_block' should be one of EFUSE_BLK0..EFUSE_BLK3")
401        else:
402            if strval not in ['EFUSE_BLK0', 'EFUSE_BLK1', 'EFUSE_BLK2', 'EFUSE_BLK3', 'EFUSE_BLK4',
403                              'EFUSE_BLK5', 'EFUSE_BLK6', 'EFUSE_BLK7', 'EFUSE_BLK8', 'EFUSE_BLK9',
404                              'EFUSE_BLK10']:
405                raise InputError("Field 'efuse_block' should be one of EFUSE_BLK0..EFUSE_BLK10")
406
407        return strval
408
409    def get_max_bits_of_block(self):
410        '''common_table: EFUSE_BLK0, EFUSE_BLK1, EFUSE_BLK2, EFUSE_BLK3
411           custom_table: ----------, ----------, ----------, EFUSE_BLK3(some reserved in common_table)
412        '''
413        if self.efuse_block == 'EFUSE_BLK0':
414            return 256
415        else:
416            return max_blk_len
417
418    def verify(self, type_table):
419        if self.efuse_block is None:
420            raise ValidationError(self, 'efuse_block field is not set')
421        if self.bit_count is None:
422            raise ValidationError(self, 'bit_count field is not set')
423        max_bits = self.get_max_bits_of_block()
424        if self.bit_start + self.bit_count > max_bits:
425            raise ValidationError(self, 'The field is outside the boundaries(max_bits = %d) of the %s block' % (max_bits, self.efuse_block))
426
427    def get_bit_count(self, check_define=True):
428        if check_define is True and self.define is not None:
429            return self.define
430        else:
431            return self.bit_count
432
433    def to_struct(self, debug):
434        start = '    {'
435        if debug is True:
436            start = '    {' + '"' + self.field_name + '" ,'
437        return ', '.join([start + self.efuse_block,
438                         str(self.bit_start),
439                         str(self.get_bit_count()) + '}, \t // ' + self.comment])
440
441    def get_alt_names(self):
442        result = re.search(r'^\[(.*?)\]', self.comment)
443        if result:
444            return result.group(1).split()
445        return []
446
447
448def process_input_file(file, type_table):
449    status('Parsing efuse CSV input file ' + file.name + ' ...')
450    input = file.read()
451    table = FuseTable.from_csv(input)
452    status('Verifying efuse table...')
453    table.verify(type_table)
454    return table
455
456
457def ckeck_md5_in_file(md5, filename):
458    if os.path.exists(filename):
459        with open(filename, 'r') as f:
460            for line in f:
461                if md5 in line:
462                    return True
463    return False
464
465
466def create_output_files(name, output_table, debug):
467    file_name = os.path.splitext(os.path.basename(name))[0]
468    gen_dir = os.path.dirname(name)
469
470    dir_for_file_h = gen_dir + '/include'
471    try:
472        os.stat(dir_for_file_h)
473    except Exception:
474        os.mkdir(dir_for_file_h)
475
476    file_h_path = os.path.join(dir_for_file_h, file_name + '.h')
477    file_c_path = os.path.join(gen_dir, file_name + '.c')
478
479    # src files are the same
480    if ckeck_md5_in_file(output_table.md5_digest_table, file_c_path) is False:
481        status('Creating efuse *.h file ' + file_h_path + ' ...')
482        output = output_table.to_header(file_name)
483        with open(file_h_path, 'w') as f:
484            f.write(output)
485
486        status('Creating efuse *.c file ' + file_c_path + ' ...')
487        output = output_table.to_c_file(file_name, debug)
488        with open(file_c_path, 'w') as f:
489            f.write(output)
490    else:
491        print('Source files do not require updating correspond to csv file.')
492
493
494def main():
495    global quiet
496    global max_blk_len
497    global idf_target
498
499    parser = argparse.ArgumentParser(description='ESP32 eFuse Manager')
500    parser.add_argument('--idf_target', '-t', help='Target chip type', choices=['esp32', 'esp32s2', 'esp32s3', 'esp32c3',
501                        'esp32c2', 'esp32c6', 'esp32h2'], default='esp32')
502    parser.add_argument('--quiet', '-q', help="Don't print non-critical status messages to stderr", action='store_true')
503    parser.add_argument('--debug', help='Create header file with debug info', default=False, action='store_false')
504    parser.add_argument('--info', help='Print info about range of used bits', default=False, action='store_true')
505    parser.add_argument('--max_blk_len', help='Max number of bits in BLOCKs', type=int, default=256)
506    parser.add_argument('common_input', help='Path to common CSV file to parse.', type=argparse.FileType('r'))
507    parser.add_argument('custom_input', help='Path to custom CSV file to parse.', type=argparse.FileType('r'), nargs='?', default=None)
508
509    args = parser.parse_args()
510
511    idf_target = args.idf_target
512
513    max_blk_len = args.max_blk_len
514    print('Max number of bits in BLK %d' % (max_blk_len))
515    if max_blk_len not in [256, 192, 128]:
516        raise InputError('Unsupported block length = %d' % (max_blk_len))
517
518    quiet = args.quiet
519    debug = args.debug
520    info = args.info
521
522    common_table = process_input_file(args.common_input, 'common_table')
523    two_table = common_table
524    if args.custom_input is not None:
525        custom_table = process_input_file(args.custom_input, 'custom_table')
526        two_table += custom_table
527        two_table.verify()
528
529    # save files.
530    if info is False:
531        if args.custom_input is None:
532            create_output_files(args.common_input.name, common_table, debug)
533        else:
534            create_output_files(args.custom_input.name, custom_table, debug)
535    else:
536        print(two_table.show_range_used_bits())
537    return 0
538
539
540class InputError(RuntimeError):
541    def __init__(self, e):
542        super(InputError, self).__init__(e)
543
544
545class ValidationError(InputError):
546    def __init__(self, p, message):
547        super(ValidationError, self).__init__('Entry %s invalid: %s' % (p.field_name, message))
548
549
550if __name__ == '__main__':
551    try:
552        main()
553    except InputError as e:
554        print(e, file=sys.stderr)
555        sys.exit(2)
556