19
19
20
20
import sys
21
21
import os
22
+ import tempfile
22
23
import time
23
24
import argparse
24
25
import struct
@@ -232,19 +233,26 @@ def __call__(self, string):
232
233
help = "enables fail safe MIRROR feature" )
233
234
parser_write_file .add_argument (
234
235
"--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" )
235
239
236
240
parser_read_file = subparsers .add_parser (
237
241
"read_file" , help = "read a file from the SL filesystem" )
238
242
parser_read_file .add_argument (
239
243
"cc_filename" , help = "file to read from the target" )
240
244
parser_read_file .add_argument (
241
- "local_file" , type = argparse .FileType ('wb ' ),
245
+ "local_file" , type = argparse .FileType ('w+b ' ),
242
246
help = "local path to store the file contents in" )
243
247
parser_read_file .add_argument (
244
248
"--file-id" , type = auto_int , default = - 1 , help = "if filename not available you can read a file by its id" )
245
249
parser_read_file .add_argument (
246
250
"--inactive" , action = "store_true" ,
247
251
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
+
248
256
249
257
parser_write_flash = subparsers .add_parser (
250
258
"write_flash" , help = "Write a Gang image on the flash" )
@@ -254,6 +262,9 @@ def __call__(self, string):
254
262
parser_write_flash .add_argument (
255
263
"--no-erase" , type = bool , default = False ,
256
264
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" )
257
268
258
269
parser_read_flash = subparsers .add_parser (
259
270
"read_flash" , help = "Read SFFS contents into the file" )
@@ -266,6 +277,12 @@ def __call__(self, string):
266
277
parser_read_flash .add_argument (
267
278
"--size" , type = auto_int , default = - 1 ,
268
279
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" )
269
286
270
287
271
288
parser_list_filesystem = subparsers .add_parser (
@@ -285,7 +302,8 @@ def __call__(self, string):
285
302
"read_all_files" ,
286
303
help = "Reads all files into a subfolder structure" )
287
304
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" ,
289
307
help = "local path to store the files in" )
290
308
parser_read_all_files .add_argument (
291
309
"--by-file-id" , action = "store_true" ,
@@ -296,6 +314,9 @@ def __call__(self, string):
296
314
parser_read_all_files .add_argument (
297
315
"--inactive" , action = "store_true" ,
298
316
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" )
299
320
300
321
parser_write_all_files = subparsers .add_parser (
301
322
"write_all_files" ,
@@ -306,6 +327,9 @@ def __call__(self, string):
306
327
parser_write_all_files .add_argument (
307
328
"--simulate" , action = "store_false" ,
308
329
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" )
309
333
310
334
def dll_data (fname ):
311
335
return get_data ('cc3200tool' , os .path .join ('dll' , fname ))
@@ -860,7 +884,7 @@ def _read_chunk(self, offset, size, storage_id=STORAGE_ID_SRAM):
860
884
data = self ._image_file .read (size )
861
885
return data
862
886
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 ):
864
888
slist = self ._get_storage_list ()
865
889
if storage_id == STORAGE_ID_SFLASH and not slist .sflash :
866
890
raise CC3200Error ("no serial flash?!" )
@@ -871,9 +895,13 @@ def _raw_read(self, offset, size, storage_id=STORAGE_ID_SRAM, sinfo=None):
871
895
sinfo = self ._get_storage_info (storage_id )
872
896
storage_size = sinfo .block_count * sinfo .block_size
873
897
898
+ if ignore_max_size :
899
+ storage_size = offset + size
900
+ log .warning ("Ignoring storage size limits" )
901
+
874
902
if offset > storage_size :
875
903
raise CC3200Error ("offset %d is bigger than available mem %d" %
876
- (offset , storage_size ))
904
+ (offset , storage_size ))
877
905
878
906
if size < 1 :
879
907
size = storage_size - offset
@@ -1211,8 +1239,8 @@ def write_flash(self, image, erase=True):
1211
1239
self ._raw_write (8 , data [8 :], storage_id = STORAGE_ID_SFLASH )
1212
1240
self ._raw_write (0 , data [:8 ], storage_id = STORAGE_ID_SFLASH )
1213
1241
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 )
1216
1244
image_file .write (data )
1217
1245
1218
1246
def get_fat_info (self , inactive = False , extended = False ):
@@ -1290,13 +1318,26 @@ def list_filesystem(self, json_output=False, inactive=False, extended=False):
1290
1318
if json_output :
1291
1319
fat_info .print_sffs_info_json ()
1292
1320
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 ):
1294
1322
fat_info = self .get_fat_info (inactive = inactive )
1295
1323
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
+
1296
1332
for f in fat_info .files :
1297
1333
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
1300
1341
1301
1342
if ccname .startswith ('/' ):
1302
1343
ccname = ccname [1 :]
@@ -1309,25 +1350,58 @@ def read_all_files(self, local_dir, by_file_id=False, all_by_file_id=False, inac
1309
1350
os .makedirs (name = os .path .dirname (target_file ))
1310
1351
1311
1352
try :
1353
+ tmpFile = tempfile .NamedTemporaryFile (mode = 'w+b' )
1312
1354
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 )
1314
1358
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
1316
1370
except Exception as ex :
1317
1371
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 )
1318
1377
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
1320
1380
for root , dirs , files in os .walk (local_dir ):
1321
1381
for file in files :
1322
1382
filepath = os .path .join (root , file )
1323
1383
ccpath = filepath [len (local_dir ):]
1384
+ ccpath = ccpath .replace ("\\ " , "/" )
1324
1385
if not ccpath .startswith ("/" ):
1325
1386
ccpath = "/" + ccpath
1326
1387
1327
1388
if write :
1328
1389
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
1329
1400
else :
1330
1401
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 )
1331
1405
1332
1406
1333
1407
def split_argv (cmdline_args ):
@@ -1417,33 +1491,86 @@ def main():
1417
1491
cc .write_file (command .local_file , command .cc_filename , command .file_id ,
1418
1492
command .signature , command .file_size ,
1419
1493
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
+
1420
1507
check_fat = True
1421
1508
1422
1509
if command .cmd == "read_file" :
1423
1510
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 )
1424
1525
1425
1526
if command .cmd == "erase_file" :
1426
1527
log .info ("Erasing file %s" , command .filename )
1427
1528
cc .erase_file (command .filename )
1428
1529
1429
1530
if command .cmd == "write_flash" :
1430
1531
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
+
1431
1544
1432
1545
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 )
1434
1561
1435
1562
if command .cmd == "list_filesystem" :
1436
1563
cc .list_filesystem (command .json_output , command .inactive , command .extended )
1437
1564
1438
1565
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 )
1440
1567
1441
1568
if command .cmd == "write_all_files" :
1442
1569
use_api = True
1443
1570
if not command .image_file is None and not command .output_file is None :
1444
1571
use_api = False
1445
1572
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 )
1447
1574
check_fat = True
1448
1575
1449
1576
0 commit comments