fix(espefuse): Fix compatibility with Bitstring>=4

bitstring==4 dropped class alias for BitStream. BitString was just an
alias in all supported bitstring versions:
https://github.com/scott-griffiths/bitstring/blob/bitstring-3.1.6/bitstring.py#L4247

Closes https://github.com/espressif/esptool/issues/797
This commit is contained in:
Roland Dobai
2022-11-20 17:01:43 +01:00
parent c7ca87b04a
commit ee27a64375
6 changed files with 22 additions and 21 deletions

View File

@@ -10,7 +10,7 @@ import binascii
import re
import sys
from bitstring import BitArray, BitString, CreationError
from bitstring import BitArray, BitStream, CreationError
import esptool
@@ -155,14 +155,14 @@ class EfuseBlockBase(EfuseProtectBase):
self.len = param.len
self.key_purpose_name = param.key_purpose
bit_block_len = self.get_block_len() * 8
self.bitarray = BitString(bit_block_len)
self.bitarray = BitStream(bit_block_len)
self.bitarray.set(0)
self.wr_bitarray = BitString(bit_block_len)
self.wr_bitarray = BitStream(bit_block_len)
self.wr_bitarray.set(0)
self.fail = False
self.num_errors = 0
if self.id == 0:
self.err_bitarray = BitString(bit_block_len)
self.err_bitarray = BitStream(bit_block_len)
self.err_bitarray.set(0)
else:
self.err_bitarray = None
@@ -336,7 +336,7 @@ class EfuseBlockBase(EfuseProtectBase):
# in bitstring = [N] ... [2][1][0] (to get a correct bitstring
# need to reverse new_data)
# *[x] - means a byte.
data = BitString(bytes=new_data[::-1], length=len(new_data) * 8)
data = BitStream(bytes=new_data[::-1], length=len(new_data) * 8)
if self.parent.debug:
print(
"\twritten : {} ->\n\tto write: {}".format(self.get_bitstring(), data)
@@ -563,7 +563,7 @@ class EfuseFieldBase(EfuseProtectBase):
field_len = int(re.search(r"\d+", self.efuse_type).group())
if self.efuse_type.startswith("bytes"):
field_len *= 8
self.bitarray = BitString(field_len)
self.bitarray = BitStream(field_len)
self.bit_len = field_len
self.bitarray.set(0)
self.update(self.parent.blocks[self.block].bitarray)

View File

@@ -10,7 +10,7 @@ import argparse
import json
import sys
from bitstring import BitString
from bitstring import BitStream
import esptool
@@ -627,7 +627,7 @@ def burn_bit(esp, efuses, args):
efuses.force_write_always = args.force_write_always
num_block = efuses.get_index_block_by_name(args.block)
block = efuses.blocks[num_block]
data_block = BitString(block.get_block_len() * 8)
data_block = BitStream(block.get_block_len() * 8)
data_block.set(0)
try:
data_block.set(True, args.bit_number)

View File

@@ -8,7 +8,7 @@
import re
from bitstring import BitString
from bitstring import BitStream
class EmulateEfuseControllerBase(object):
@@ -26,18 +26,19 @@ class EmulateEfuseControllerBase(object):
self.efuse_file = efuse_file
if self.efuse_file:
try:
self.mem = BitString(
open(self.efuse_file, "a+b"), length=self.REGS.EFUSE_MEM_SIZE * 8
self.mem = BitStream(
bytes=open(self.efuse_file, "rb").read(),
length=self.REGS.EFUSE_MEM_SIZE * 8,
)
except ValueError:
except (ValueError, FileNotFoundError):
# the file is empty or does not fit the length.
self.mem = BitString(length=self.REGS.EFUSE_MEM_SIZE * 8)
self.mem = BitStream(length=self.REGS.EFUSE_MEM_SIZE * 8)
self.mem.set(0)
self.mem.tofile(open(self.efuse_file, "a+b"))
else:
# efuse_file is not provided
# it means we do not want to keep the result of efuse operations
self.mem = BitString(self.REGS.EFUSE_MEM_SIZE * 8)
self.mem = BitStream(self.REGS.EFUSE_MEM_SIZE * 8)
self.mem.set(0)
""" esptool method start >> """
@@ -159,7 +160,7 @@ class EmulateEfuseControllerBase(object):
# checks fields which have the write protection bit.
# if the write protection bit is set, we need to protect that area from changes.
write_disable_bit = self.read_field("WR_DIS", bitstring=False)
mask_wr_data = BitString(len(wr_data))
mask_wr_data = BitStream(len(wr_data))
mask_wr_data.set(0)
blk = self.Blocks.get(self.Blocks.BLOCKS[num_blk])
if blk.write_disable_bit is not None and write_disable_bit & (
@@ -206,7 +207,7 @@ class EmulateEfuseControllerBase(object):
block.pos = block.length - (
field.word * 32 + field.pos + raw_data.length
)
block.overwrite(BitString(raw_data.length))
block.overwrite(BitStream(raw_data.length))
self.overwrite_mem_from_block(blk, block)
def clean_mem(self):

View File

@@ -6,7 +6,7 @@
#
# SPDX-License-Identifier: GPL-2.0-or-later
from bitstring import BitString
from bitstring import BitStream
import reedsolo
@@ -138,5 +138,5 @@ class EmulateEfuseController(EmulateEfuseControllerBase):
block.pos = block.length - (
field.word * 32 + field.pos + raw_data.length
)
block.overwrite(BitString(raw_data.length))
block.overwrite(BitStream(raw_data.length))
self.overwrite_mem_from_block(blk, block)

View File

@@ -117,7 +117,7 @@ setup(
],
},
install_requires=[
"bitstring>=3.1.6,<4",
"bitstring>=3.1.6",
"cryptography>=2.1.4",
"ecdsa>=0.16.0",
"pyserial>=3.0",

View File

@@ -30,7 +30,7 @@ import sys
import tempfile
import time
from bitstring import BitString
from bitstring import BitStream
# Make command line options --port, --reset-port and --chip available
from conftest import arg_chip, arg_port, arg_reset_port
@@ -115,7 +115,7 @@ class EfuseTestCase:
self, log, file_path, repeat=1, reverse_order=False, offset=0
):
with open(file_path, "rb") as f:
data = BitString("0x00") * offset + BitString(f)
data = BitStream("0x00") * offset + BitStream(f)
blk = data.readlist(f"{data.len // 8}*uint:8")
blk = blk[::-1] if reverse_order else blk
hex_blk = " ".join(f"{num:02x}" for num in blk)