66using System ;
77using System . Collections . Generic ;
88using System . IO ;
9+ using System . Text . RegularExpressions ;
910
1011namespace nanoFramework . Tools . FirmwareFlasher
1112{
@@ -103,6 +104,7 @@ public static ExitCodes BackupFlash(
103104 /// <param name="deploymentAddress">Flash address to use when deploying an aplication.</param>
104105 /// <param name="clrFile">Path to CLR file to use for firmware update.</param>
105106 /// <param name="fitCheck"><see langword="true"/> to perform validation of update package against connected target.</param>
107+ /// <param name="massErase">If <see langword="true"/> perform mass erase on device before updating.</param>
106108 /// <param name="verbosity">Set verbosity level of progress and error messages.</param>
107109 /// <param name="partitionTableSize">Size of partition table.</param>
108110 /// <returns>The <see cref="ExitCodes"/> with the operation result.</returns>
@@ -117,6 +119,7 @@ public static async System.Threading.Tasks.Task<ExitCodes> UpdateFirmwareAsync(
117119 string deploymentAddress ,
118120 string clrFile ,
119121 bool fitCheck ,
122+ bool massErase ,
120123 VerbosityLevel verbosity ,
121124 PartitionTableSize ? partitionTableSize )
122125 {
@@ -403,16 +406,17 @@ public static async System.Threading.Tasks.Task<ExitCodes> UpdateFirmwareAsync(
403406 }
404407 }
405408
406- if ( updateFw )
409+ if ( updateFw
410+ && massErase )
407411 {
412+ // erase flash, if masse erase was requested
408413 // updating fw calls for a flash erase
409414 if ( verbosity >= VerbosityLevel . Normal )
410415 {
411416 Console . ForegroundColor = ConsoleColor . White ;
412417 Console . Write ( $ "Erasing flash...") ;
413418 }
414419
415- // erase flash
416420 operationResult = espTool . EraseFlash ( ) ;
417421
418422 if ( operationResult == ExitCodes . OK )
@@ -438,6 +442,39 @@ public static async System.Threading.Tasks.Task<ExitCodes> UpdateFirmwareAsync(
438442 Console . Write ( $ "Flashing firmware...") ;
439443 }
440444
445+ int configPartitionAddress = 0 ;
446+ int configPartitionSize = 0 ;
447+ string configPartitionBackup = Path . GetTempFileName ( ) ;
448+
449+ // if mass erase wasn't requested, backup config partitition
450+ if ( ! massErase )
451+ {
452+ // compose path to partition file
453+ string partitionCsvFile = Path . Combine ( firmware . LocationPath , $ "partitions_nanoclr_{ Esp32DeviceInfo . GetFlashSizeAsString ( esp32Device . FlashSize ) . ToLowerInvariant ( ) } .csv") ;
454+
455+ var partitionDetails = File . ReadAllText ( partitionCsvFile ) ;
456+
457+ // grab details for the config partition
458+ string pattern = @"config,.*?(0x[0-9A-Fa-f]+),.*?(0x[0-9A-Fa-f]+)," ;
459+ Regex regex = new Regex ( pattern ) ;
460+ Match match = regex . Match ( partitionDetails ) ;
461+
462+ if ( match . Success )
463+ {
464+ // just try to parse, ignore failures
465+ int . TryParse ( match . Groups [ 1 ] . Value . Substring ( 2 ) , System . Globalization . NumberStyles . HexNumber , System . Globalization . CultureInfo . InvariantCulture , out configPartitionAddress ) ;
466+ int . TryParse ( match . Groups [ 2 ] . Value . Substring ( 2 ) , System . Globalization . NumberStyles . HexNumber , System . Globalization . CultureInfo . InvariantCulture , out configPartitionSize ) ;
467+ }
468+
469+ // backup config partition
470+ operationResult = espTool . BackupConfigPartition (
471+ configPartitionBackup ,
472+ configPartitionAddress ,
473+ configPartitionSize ) ;
474+
475+ firmware . FlashPartitions . Add ( configPartitionAddress , configPartitionBackup ) ;
476+ }
477+
441478 // write to flash
442479 operationResult = espTool . WriteFlash ( firmware . FlashPartitions ) ;
443480
@@ -469,6 +506,19 @@ public static async System.Threading.Tasks.Task<ExitCodes> UpdateFirmwareAsync(
469506 }
470507 }
471508
509+ // delete config partition backup
510+ try
511+ {
512+ if ( File . Exists ( configPartitionBackup ) )
513+ {
514+ File . Delete ( configPartitionBackup ) ;
515+ }
516+ }
517+ catch
518+ {
519+ // don't care
520+ }
521+
472522 Console . ForegroundColor = ConsoleColor . White ;
473523 }
474524
0 commit comments