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