From 582a7cab1458d2c8b913285a69de4f1df73b6492 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Wed, 2 Sep 2015 10:49:03 +1000 Subject: [PATCH] Remove floats from block calculations, fix regression in 0e67846 #53 Float argument strayed into flash_begin() size, caused erase sizes to be calculated as too long. This triggered an error when writing near the end of the flash, it would ask for erasing beyond the end of available space. Closes #60 --- esptool.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/esptool.py b/esptool.py index bd59d37de..00ee3a66b 100755 --- a/esptool.py +++ b/esptool.py @@ -415,6 +415,13 @@ def load_section(self, section): def arg_auto_int(x): return int(x, 0) +def div_roundup(a, b): + """ Return a/b rounded up to nearest integer, + equivalent result to int(math.ceil(float(int(a)) / float(int(b))), only + without possible floating point accuracy errors. + """ + return (int(a) + int(b) - 1) / int(b) + if __name__ == '__main__': parser = argparse.ArgumentParser(description = 'ESP8266 ROM Bootloader Utility', prog = 'esptool') @@ -532,7 +539,7 @@ def arg_auto_int(x): for (offset, size, data) in image.segments: print 'Downloading %d bytes at %08x...' % (size, offset), sys.stdout.flush() - esp.mem_begin(size, math.ceil(size / float(esp.ESP_RAM_BLOCK)), esp.ESP_RAM_BLOCK, offset) + esp.mem_begin(size, div_roundup(size, esp.ESP_RAM_BLOCK), esp.ESP_RAM_BLOCK, offset) seq = 0 while len(data) > 0: @@ -575,7 +582,7 @@ def arg_auto_int(x): args.addr_filename = args.addr_filename[2:] image = file(filename, 'rb').read() print 'Erasing flash...' - blocks = math.ceil(len(image)/float(esp.ESP_FLASH_BLOCK)) + blocks = div_roundup(len(image), esp.ESP_FLASH_BLOCK) esp.flash_begin(blocks*esp.ESP_FLASH_BLOCK, address) seq = 0 while len(image) > 0: @@ -658,7 +665,7 @@ def arg_auto_int(x): elif args.operation == 'read_flash': print 'Please wait...' - file(args.filename, 'wb').write(esp.flash_read(args.address, 1024, int(math.ceil(args.size / 1024.)))[:args.size]) + file(args.filename, 'wb').write(esp.flash_read(args.address, 1024, div_roundup(args.size, 1024))[:args.size]) elif args.operation == 'erase_flash': esp.flash_erase()