From aaabbc553163626521b78d73fc346c84e7b56e3f Mon Sep 17 00:00:00 2001 From: KonstantinKondrashov Date: Thu, 21 Jul 2022 21:17:45 +0800 Subject: [PATCH] espefuse: Hide traceback when new data value is incorrect for burn_efuse cmd Closes https://github.com/espressif/esptool/issues/760 --- espefuse/efuse/base_fields.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/espefuse/efuse/base_fields.py b/espefuse/efuse/base_fields.py index e5d250ef2..71310f5f7 100644 --- a/espefuse/efuse/base_fields.py +++ b/espefuse/efuse/base_fields.py @@ -6,12 +6,11 @@ # # SPDX-License-Identifier: GPL-2.0-or-later -import argparse import binascii import re import sys -from bitstring import BitArray, BitString +from bitstring import BitArray, BitString, CreationError import esptool @@ -28,7 +27,7 @@ def check_arg_value(efuse, new_value): if efuse.efuse_type.startswith("bool"): new_value = 1 if new_value is None else int(new_value, 0) if new_value != 1: - raise argparse.ArgumentTypeError( + raise esptool.FatalError( "New value is not accepted for efuse '{}' " "(will always burn 0->1), given value={}".format( efuse.name, new_value @@ -48,32 +47,32 @@ def check_arg_value(efuse, new_value): new_value = int(new_value, 0) else: if new_value is None: - raise argparse.ArgumentTypeError( + raise esptool.FatalError( "New value required for efuse '{}' (given None)".format( efuse.name ) ) new_value = int(new_value, 0) if new_value == 0: - raise argparse.ArgumentTypeError( + raise esptool.FatalError( "New value should not be 0 for '{}' " "(given value= {})".format(efuse.name, new_value) ) elif efuse.efuse_type.startswith("bytes"): if new_value is None: - raise argparse.ArgumentTypeError( + raise esptool.FatalError( "New value required for efuse '{}' " "(given None)".format(efuse.name) ) if len(new_value) * 8 != efuse.bitarray.len: - raise argparse.ArgumentTypeError( + raise esptool.FatalError( "The length of efuse '{}' ({} bits) " "(given len of the new value= {} bits)".format( efuse.name, efuse.bitarray.len, len(new_value) * 8 ) ) else: - raise argparse.ArgumentTypeError( + raise esptool.FatalError( "The '{}' type for the '{}' efuse is not supported yet.".format( efuse.efuse_type, efuse.name ) @@ -598,7 +597,15 @@ def convert_to_bitstring(self, new_value): # *[x] - means a byte. return BitArray(bytes=new_value[::-1], length=len(new_value) * 8) else: - return BitArray(self.efuse_type + "={}".format(new_value)) + try: + return BitArray(self.efuse_type + "={}".format(new_value)) + except CreationError as err: + print( + "New value '{}' is not suitable for {} ({})".format( + new_value, self.name, self.efuse_type + ) + ) + raise esptool.FatalError(err) def check_new_value(self, bitarray_new_value): bitarray_old_value = self.get_bitstring() | self.get_bitstring(from_read=False)