Skip to content

Commit

Permalink
Improve update operation for ESP32 (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
josesimoes authored Dec 22, 2023
1 parent 1f10419 commit 8258d78
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
54 changes: 52 additions & 2 deletions nanoFirmwareFlasher.Library/Esp32Operations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;

namespace nanoFramework.Tools.FirmwareFlasher
{
Expand Down Expand Up @@ -103,6 +104,7 @@ public static ExitCodes BackupFlash(
/// <param name="deploymentAddress">Flash address to use when deploying an aplication.</param>
/// <param name="clrFile">Path to CLR file to use for firmware update.</param>
/// <param name="fitCheck"><see langword="true"/> to perform validation of update package against connected target.</param>
/// <param name="massErase">If <see langword="true"/> perform mass erase on device before updating.</param>
/// <param name="verbosity">Set verbosity level of progress and error messages.</param>
/// <param name="partitionTableSize">Size of partition table.</param>
/// <returns>The <see cref="ExitCodes"/> with the operation result.</returns>
Expand All @@ -117,6 +119,7 @@ public static async System.Threading.Tasks.Task<ExitCodes> UpdateFirmwareAsync(
string deploymentAddress,
string clrFile,
bool fitCheck,
bool massErase,
VerbosityLevel verbosity,
PartitionTableSize? partitionTableSize)
{
Expand Down Expand Up @@ -403,16 +406,17 @@ public static async System.Threading.Tasks.Task<ExitCodes> UpdateFirmwareAsync(
}
}

if (updateFw)
if (updateFw
&& massErase)
{
// erase flash, if masse erase was requested
// updating fw calls for a flash erase
if (verbosity >= VerbosityLevel.Normal)
{
Console.ForegroundColor = ConsoleColor.White;
Console.Write($"Erasing flash...");
}

// erase flash
operationResult = espTool.EraseFlash();

if (operationResult == ExitCodes.OK)
Expand All @@ -438,6 +442,39 @@ public static async System.Threading.Tasks.Task<ExitCodes> UpdateFirmwareAsync(
Console.Write($"Flashing firmware...");
}

int configPartitionAddress = 0;
int configPartitionSize = 0;
string configPartitionBackup = Path.GetTempFileName();

// if mass erase wasn't requested, backup config partitition
if (!massErase)
{
// compose path to partition file
string partitionCsvFile = Path.Combine(firmware.LocationPath, $"partitions_nanoclr_{Esp32DeviceInfo.GetFlashSizeAsString(esp32Device.FlashSize).ToLowerInvariant()}.csv");

var partitionDetails = File.ReadAllText(partitionCsvFile);

// grab details for the config partition
string pattern = @"config,.*?(0x[0-9A-Fa-f]+),.*?(0x[0-9A-Fa-f]+),";
Regex regex = new Regex(pattern);
Match match = regex.Match(partitionDetails);

if (match.Success)
{
// just try to parse, ignore failures
int.TryParse(match.Groups[1].Value.Substring(2), System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out configPartitionAddress);
int.TryParse(match.Groups[2].Value.Substring(2), System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out configPartitionSize);
}

// backup config partition
operationResult = espTool.BackupConfigPartition(
configPartitionBackup,
configPartitionAddress,
configPartitionSize);

firmware.FlashPartitions.Add(configPartitionAddress, configPartitionBackup);
}

// write to flash
operationResult = espTool.WriteFlash(firmware.FlashPartitions);

Expand Down Expand Up @@ -469,6 +506,19 @@ public static async System.Threading.Tasks.Task<ExitCodes> UpdateFirmwareAsync(
}
}

// delete config partition backup
try
{
if (File.Exists(configPartitionBackup))
{
File.Delete(configPartitionBackup);
}
}
catch
{
// don't care
}

Console.ForegroundColor = ConsoleColor.White;
}

Expand Down
38 changes: 38 additions & 0 deletions nanoFirmwareFlasher.Library/EspTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,44 @@ internal void BackupFlash(string backupFilename,
}
}

/// <summary>
/// Backup the entire flash into a bin file
/// </summary>
/// <param name="backupFilename">Backup file including full path</param>
/// <param name="address">Start address of the config partition.</param>
/// <param name="size">Size of the config partition.</param>
/// <returns>true if successful</returns>
internal ExitCodes BackupConfigPartition(
string backupFilename,
int address,
int size)
{
// execute dump_mem command and parse the result; progress message can be found be searching for backspaces (ASCII code 8)
if (!RunEspTool(
$"read_flash 0x{address:X} 0x{size:X} \"{backupFilename}\"",
false,
true,
false,
null,
out string messages))
{
throw new ReadEsp32FlashException(messages);
}

var match = Regex.Match(messages, "(?<message>Read .*)(.*?\n)*");
if (!match.Success)
{
throw new ReadEsp32FlashException(messages);
}

if (Verbosity >= VerbosityLevel.Detailed)
{
Console.WriteLine(match.Groups["message"].ToString().Trim());
}

return ExitCodes.OK;
}

/// <summary>
/// Erase the entire flash of the ESP32 chip.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions nanoFirmwareFlasher.Library/FirmwarePackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ internal async Task<ExitCodes> DownloadAndExtractAsync()
filesToDelete.AddRange(Directory.EnumerateFiles(LocationPath, "*.hex").ToList());
filesToDelete.AddRange(Directory.EnumerateFiles(LocationPath, "*.s19").ToList());
filesToDelete.AddRange(Directory.EnumerateFiles(LocationPath, "*.dfu").ToList());
filesToDelete.AddRange(Directory.EnumerateFiles(LocationPath, "*.csv").ToList());

foreach (var file in filesToDelete)
{
Expand Down
2 changes: 2 additions & 0 deletions nanoFirmwareFlasher.Tool/Esp32Manager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public async Task<ExitCodes> ProcessAsync()
null,
_options.ClrFile,
!_options.FitCheck,
_options.MassErase,
_verbosityLevel,
_options.Esp32PartitionTableSize);

Expand Down Expand Up @@ -182,6 +183,7 @@ public async Task<ExitCodes> ProcessAsync()
appFlashAddress,
null,
!_options.FitCheck,
_options.MassErase,
_verbosityLevel,
_options.Esp32PartitionTableSize);

Expand Down

0 comments on commit 8258d78

Please sign in to comment.