Skip to content

Commit e5c008b

Browse files
authored
Merge pull request #28 from toniebox-reverse-engineering/develop
Next Version
2 parents 2a31b7d + 393e8f6 commit e5c008b

File tree

3 files changed

+144
-17
lines changed

3 files changed

+144
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# CC3200 Tool v1.2.2
1+
# CC3200 Tool v1.2.3
22

33
A small tool to read and write files in TI's CC3200 SimpleLink (TM) filesystem.
44
Partial support for the CC32XX (CC3230/CC3235) series (via flash image)

cc3200tool/cc.py

Lines changed: 142 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import sys
2121
import os
22+
import tempfile
2223
import time
2324
import argparse
2425
import struct
@@ -232,19 +233,26 @@ def __call__(self, string):
232233
help="enables fail safe MIRROR feature")
233234
parser_write_file.add_argument(
234235
"--file-id", type=auto_int, default=-1, help="if filename not available you can read a file by its id (image_file only!)")
236+
parser_write_file.add_argument(
237+
"--no-verify", type=bool, default=False,
238+
help="do not perform a read of the written data to verify")
235239

236240
parser_read_file = subparsers.add_parser(
237241
"read_file", help="read a file from the SL filesystem")
238242
parser_read_file.add_argument(
239243
"cc_filename", help="file to read from the target")
240244
parser_read_file.add_argument(
241-
"local_file", type=argparse.FileType('wb'),
245+
"local_file", type=argparse.FileType('w+b'),
242246
help="local path to store the file contents in")
243247
parser_read_file.add_argument(
244248
"--file-id", type=auto_int, default=-1, help="if filename not available you can read a file by its id")
245249
parser_read_file.add_argument(
246250
"--inactive", action="store_true",
247251
help="read from inactive FAT copy")
252+
parser_read_file.add_argument(
253+
"--no-verify", type=bool, default=False,
254+
help="do not perform a second read of the data to verify")
255+
248256

249257
parser_write_flash = subparsers.add_parser(
250258
"write_flash", help="Write a Gang image on the flash")
@@ -254,6 +262,9 @@ def __call__(self, string):
254262
parser_write_flash.add_argument(
255263
"--no-erase", type=bool, default=False,
256264
help="do not perform an erase before write (for blank chips)")
265+
parser_write_flash.add_argument(
266+
"--no-verify", type=bool, default=False,
267+
help="do not perform a read of the written data to verify")
257268

258269
parser_read_flash = subparsers.add_parser(
259270
"read_flash", help="Read SFFS contents into the file")
@@ -266,6 +277,12 @@ def __call__(self, string):
266277
parser_read_flash.add_argument(
267278
"--size", type=auto_int, default=-1,
268279
help="dump size (default is complete SFFS)")
280+
parser_read_flash.add_argument(
281+
"--ignore-max-size", type=bool, default=False,
282+
help="ignore the maximum size of the flash")
283+
parser_read_flash.add_argument(
284+
"--no-verify", type=bool, default=False,
285+
help="do not perform a second read of the data to verify")
269286

270287

271288
parser_list_filesystem = subparsers.add_parser(
@@ -285,7 +302,8 @@ def __call__(self, string):
285302
"read_all_files",
286303
help="Reads all files into a subfolder structure")
287304
parser_read_all_files.add_argument(
288-
"local_dir", type=PathType(exists=True, type='dir'),
305+
#"local_dir", type=PathType(exists=False, type='dir'),
306+
"local_dir",
289307
help="local path to store the files in")
290308
parser_read_all_files.add_argument(
291309
"--by-file-id", action="store_true",
@@ -296,6 +314,9 @@ def __call__(self, string):
296314
parser_read_all_files.add_argument(
297315
"--inactive", action="store_true",
298316
help="read from inactive FAT copy")
317+
parser_read_all_files.add_argument(
318+
"--no-verify", type=bool, default=False,
319+
help="do not perform a second read of the data to verify")
299320

300321
parser_write_all_files = subparsers.add_parser(
301322
"write_all_files",
@@ -306,6 +327,9 @@ def __call__(self, string):
306327
parser_write_all_files.add_argument(
307328
"--simulate", action="store_false",
308329
help="List all files to be written and skip writing them")
330+
parser_write_all_files.add_argument(
331+
"--no-verify", type=bool, default=False,
332+
help="do not perform a read of the written data to verify")
309333

310334
def dll_data(fname):
311335
return get_data('cc3200tool', os.path.join('dll', fname))
@@ -860,7 +884,7 @@ def _read_chunk(self, offset, size, storage_id=STORAGE_ID_SRAM):
860884
data = self._image_file.read(size)
861885
return data
862886

863-
def _raw_read(self, offset, size, storage_id=STORAGE_ID_SRAM, sinfo=None):
887+
def _raw_read(self, offset, size, storage_id=STORAGE_ID_SRAM, sinfo=None, ignore_max_size=False):
864888
slist = self._get_storage_list()
865889
if storage_id == STORAGE_ID_SFLASH and not slist.sflash:
866890
raise CC3200Error("no serial flash?!")
@@ -871,9 +895,13 @@ def _raw_read(self, offset, size, storage_id=STORAGE_ID_SRAM, sinfo=None):
871895
sinfo = self._get_storage_info(storage_id)
872896
storage_size = sinfo.block_count * sinfo.block_size
873897

898+
if ignore_max_size:
899+
storage_size = offset + size
900+
log.warning("Ignoring storage size limits")
901+
874902
if offset > storage_size:
875903
raise CC3200Error("offset %d is bigger than available mem %d" %
876-
(offset, storage_size))
904+
(offset, storage_size))
877905

878906
if size < 1:
879907
size = storage_size - offset
@@ -1211,8 +1239,8 @@ def write_flash(self, image, erase=True):
12111239
self._raw_write(8, data[8:], storage_id=STORAGE_ID_SFLASH)
12121240
self._raw_write(0, data[:8], storage_id=STORAGE_ID_SFLASH)
12131241

1214-
def read_flash(self, image_file, offset, size):
1215-
data = self._raw_read(offset, size, storage_id=STORAGE_ID_SFLASH)
1242+
def read_flash(self, image_file, offset, size, ignore_max_size=False):
1243+
data = self._raw_read(offset, size, storage_id=STORAGE_ID_SFLASH, ignore_max_size=ignore_max_size)
12161244
image_file.write(data)
12171245

12181246
def get_fat_info(self, inactive=False, extended=False):
@@ -1290,13 +1318,26 @@ def list_filesystem(self, json_output=False, inactive=False, extended=False):
12901318
if json_output:
12911319
fat_info.print_sffs_info_json()
12921320

1293-
def read_all_files(self, local_dir, by_file_id=False, all_by_file_id=False, inactive=False):
1321+
def read_all_files(self, local_dir, by_file_id=False, all_by_file_id=False, inactive=False, no_verify=False):
12941322
fat_info = self.get_fat_info(inactive=inactive)
12951323
fat_info.print_sffs_info()
1324+
has_error = False
1325+
1326+
if not os.path.exists(local_dir):
1327+
os.makedirs(local_dir)
1328+
if not os.path.exists(local_dir):
1329+
raise CC3200Error("could not create local directory %s" % local_dir)
1330+
log.info("Created local directory %s" % local_dir)
1331+
12961332
for f in fat_info.files:
12971333
ccname = f.fname
1298-
if by_file_id and f.fname == '':
1299-
ccname = str(f.index)
1334+
if f.fname == '':
1335+
if by_file_id:
1336+
ccname = str(f.index)
1337+
else:
1338+
log.error("Found file without filename, skipping index=%i", f.index)
1339+
has_error = True
1340+
continue
13001341

13011342
if ccname.startswith('/'):
13021343
ccname = ccname[1:]
@@ -1309,25 +1350,58 @@ def read_all_files(self, local_dir, by_file_id=False, all_by_file_id=False, inac
13091350
os.makedirs(name=os.path.dirname(target_file))
13101351

13111352
try:
1353+
tmpFile = tempfile.NamedTemporaryFile(mode='w+b')
13121354
if all_by_file_id or ( by_file_id and f.fname == '' ):
1313-
self.read_file(ccname, open(target_file, 'wb', -1), f.index, inactive=inactive)
1355+
self.read_file(ccname, open(target_file, 'w+b', -1), f.index, inactive=inactive)
1356+
if not no_verify:
1357+
self.read_file(ccname, tmpFile, f.index, inactive=inactive)
13141358
else:
1315-
self.read_file(f.fname, open(target_file, 'wb', -1), inactive=inactive)
1359+
self.read_file(f.fname, open(target_file, 'w+b', -1), inactive=inactive)
1360+
if not no_verify:
1361+
self.read_file(f.fname, tmpFile, inactive=inactive)
1362+
1363+
if not no_verify:
1364+
tmpFile.seek(0)
1365+
if tmpFile.read() == open(target_file, 'rb').read():
1366+
log.info("File %s verified" % target_file)
1367+
else:
1368+
log.error("File %s could not be verified" % target_file)
1369+
has_error = True
13161370
except Exception as ex:
13171371
log.error("File %s could not be read, %s" % (f.fname, str (ex)))
1372+
has_error = True
1373+
1374+
if has_error:
1375+
log.error("One or more files could not be verified or read at all")
1376+
sys.exit(-4)
13181377

1319-
def write_all_files(self, local_dir, write=True, use_api=True):
1378+
def write_all_files(self, local_dir, write=True, use_api=True, no_verify=False):
1379+
has_error = False
13201380
for root, dirs, files in os.walk(local_dir):
13211381
for file in files:
13221382
filepath = os.path.join(root, file)
13231383
ccpath = filepath[len(local_dir):]
1384+
ccpath = ccpath.replace("\\", "/")
13241385
if not ccpath.startswith("/"):
13251386
ccpath = "/" + ccpath
13261387

13271388
if write:
13281389
self.write_file(local_file=open(filepath, 'rb', -1), cc_filename=ccpath, use_api=use_api)
1390+
if not no_verify:
1391+
log.info("Verify written file with second read...")
1392+
tmpFile = tempfile.NamedTemporaryFile(mode='w+b')
1393+
self.read_file(ccpath, tmpFile)
1394+
tmpFile.seek(0)
1395+
if tmpFile.read() == open(filepath, 'rb').read():
1396+
log.info("File %s verified" % ccpath)
1397+
else:
1398+
log.error("File %s could not be verified" % ccpath)
1399+
has_error = True
13291400
else:
13301401
log.info("Simulation: Would copy local file %s to cc3200 %s" % (filepath, ccpath))
1402+
if has_error:
1403+
log.error("One or more files could not be verified")
1404+
sys.exit(-4)
13311405

13321406

13331407
def split_argv(cmdline_args):
@@ -1417,33 +1491,86 @@ def main():
14171491
cc.write_file(command.local_file, command.cc_filename, command.file_id,
14181492
command.signature, command.file_size,
14191493
command.commit_flag, use_api)
1494+
1495+
if not command.no_verify:
1496+
log.info("Read file after writing for verification...")
1497+
tmpFile = tempfile.NamedTemporaryFile(mode='w+b')
1498+
cc.read_file(command.cc_filename, tmpFile)
1499+
tmpFile.seek(0)
1500+
command.local_file.seek(0)
1501+
if tmpFile.read() == command.local_file.read():
1502+
log.info("File %s verified" % command.cc_filename)
1503+
else:
1504+
log.error("File %s could not be verified" % command.cc_filename)
1505+
sys.exit(-4)
1506+
14201507
check_fat = True
14211508

14221509
if command.cmd == "read_file":
14231510
cc.read_file(command.cc_filename, command.local_file, command.file_id, command.inactive)
1511+
if not command.no_verify:
1512+
log.info("Read file a second time for verification...")
1513+
tmpFile = tempfile.NamedTemporaryFile(mode='w+b')
1514+
cc.read_file(command.cc_filename, tmpFile, command.file_id, command.inactive)
1515+
tmpFile.seek(0)
1516+
command.local_file.seek(0)
1517+
if tmpFile.read() == command.local_file.read():
1518+
log.info("File %s verified" % command.cc_filename)
1519+
else:
1520+
log.error("File %s could not be verified" % command.cc_filename)
1521+
local_file_name = command.local_file.name
1522+
command.local_file.close()
1523+
os.remove(local_file_name)
1524+
sys.exit(-4)
14241525

14251526
if command.cmd == "erase_file":
14261527
log.info("Erasing file %s", command.filename)
14271528
cc.erase_file(command.filename)
14281529

14291530
if command.cmd == "write_flash":
14301531
cc.write_flash(command.gang_image_file, not command.no_erase)
1532+
if not command.no_verify:
1533+
log.info("Verify flash write by reading...")
1534+
tmpFile = tempfile.NamedTemporaryFile(mode='w+b')
1535+
cc.read_flash(tmpFile, 0, -1)
1536+
tmpFile.seek(0)
1537+
command.gang_image_file.seek(0)
1538+
if tmpFile.read() == command.gang_image_file.read():
1539+
log.info("Flash verified")
1540+
else:
1541+
log.error("Flash could not be verified, please flash again!")
1542+
sys.exit(-4)
1543+
14311544

14321545
if command.cmd == "read_flash":
1433-
cc.read_flash(command.dump_file, command.offset, command.size)
1546+
cc.read_flash(command.dump_file, command.offset, command.size, command.ignore_max_size)
1547+
if not command.no_verify:
1548+
log.info("Verify flash dump with second reading...")
1549+
tmpFile = tempfile.NamedTemporaryFile(mode='w+b')
1550+
cc.read_flash(tmpFile, command.offset, command.size, command.ignore_max_size)
1551+
tmpFile.seek(0)
1552+
command.dump_file.seek(0)
1553+
if tmpFile.read() == command.dump_file.read():
1554+
log.info("Flash verified, reading equal!")
1555+
else:
1556+
log.error("Flash could not be verified, first and second dump are different!")
1557+
dump_file_name = command.dump_file.name
1558+
command.dump_file.close()
1559+
os.remove(dump_file_name)
1560+
sys.exit(-4)
14341561

14351562
if command.cmd == "list_filesystem":
14361563
cc.list_filesystem(command.json_output, command.inactive, command.extended)
14371564

14381565
if command.cmd == "read_all_files":
1439-
cc.read_all_files(command.local_dir, command.by_file_id, command.all_by_file_id, command.inactive)
1566+
cc.read_all_files(command.local_dir, command.by_file_id, command.all_by_file_id, command.inactive, command.no_verify)
14401567

14411568
if command.cmd == "write_all_files":
14421569
use_api = True
14431570
if not command.image_file is None and not command.output_file is None:
14441571
use_api = False
14451572
cc.copy_input_file_to_output_file()
1446-
cc.write_all_files(command.local_dir, command.simulate, use_api)
1573+
cc.write_all_files(command.local_dir, command.simulate, use_api, command.no_verify)
14471574
check_fat = True
14481575

14491576

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name="cc3200tool",
5-
version="1.2.2",
5+
version="1.2.3",
66
description="A tool to down-/upload files form/to TI CC3200",
77
author="Kiril Zyapkov, 0xbadbee",
88
author_email="k.zyapkov@allterco.com",

0 commit comments

Comments
 (0)