I am testing an in-field upgrade on EMX from version 4.1 to 4.2.
I am downloading the files from a web server and writing it using the CompleteUpdate.Write() see the code below.
During the upgrade i can successfully write the first file Firmware.hex, but i have an issue around half way in to the second file Firmware2.hex, with the following error,
Downloaded 11680
Written 1751736 of 3102128
Downloaded 11680
Written 1763416 of 3102128
Downloaded 7300
#### Exception System.ArgumentException - 0xfd000000 (1) ####
#### Message:
#### GHIElectronics.NETMF.System.SystemUpdate+CompleteUpdate::Write [IP: 0000] ####
#### BootloaderTestApp.WebApplicationUpdater::DownloadFirmware [IP: 00d6] ####
#### BootloaderTestApp.WebApplicationUpdater::Update [IP: 002f] ####
#### BootloaderTestApp.Program::Main [IP: 0132] ####
A first chance exception of type 'System.ArgumentException' occurred in GHIElectronics.NETMF.SystemUpdate.dll
The upgrade always fails at the same spot,
I have tried doing a full upgrade from 4.1 to 4.1 and it works fine, it seems the issue is only going from 4.1 to 4.2.
I have downloaded the file using a web browser and compared it with the original file and they are identical so i dont think there are issues with the actual data being transferred.
var updater = new WebApplicationUpdater();
var info = new FirmwareUpdateInfo
{
Urls = new[]
{
"http://172.16.0.1/smartnode/Firmware.hex",
"http://172.16.0.1/smartnode/Firmware2.hex",
"http://172.16.0.1/smartnode/Config.hex",
"http://172.16.0.1/smartnode/TestApp42.hex",
}
};
if (updater.Update(info)) SystemUpdate.AccessApplication();
public class WebApplicationUpdater
{
private const int RequestTimeoutInMilliseconds = 5000;
private const int ReadTimeoutInMilliseconds = 5000;
private const int DownloadBufferSizeInKb = 32;
/// <summary>
/// Downlaods and writes application image to flash.
/// IMPORTANT: Never access application when this method returns false (possible that application image is corrupt).
/// </summary>
/// <param name="updateInfo"></param>
/// <returns></returns>
public bool Update(FirmwareUpdateInfo updateInfo)
{
if (updateInfo == null)
{
throw new ArgumentNullException("updateInfo");
}
SystemUpdate.CompleteUpdate.Start();
foreach (string url in updateInfo.Urls)
{
DownloadFirmware(url);
}
SystemUpdate.CompleteUpdate.End();
return true;
}
private static void DownloadFirmware(string url)
{
var request = (HttpWebRequest) WebRequest.Create(new Uri(url));
request.Method = "GET";
request.ReadWriteTimeout = ReadTimeoutInMilliseconds;
request.Timeout = RequestTimeoutInMilliseconds;
Debug.Print("Requesting firmware image from " + url);
using (request)
{
var response = (HttpWebResponse) request.GetResponse();
using (response)
{
using (Stream stream = response.GetResponseStream())
{
stream.ReadTimeout = ReadTimeoutInMilliseconds;
if (response.ContentLength < 0)
{
throw new InvalidOperationException("Invalid content length");
}
var buffer = new byte[DownloadBufferSizeInKb*1024];
Debug.Print("Downloading firmware image (" + response.ContentLength + ") bytes...");
int totalBytes = 0;
for (var remainingBytes = (int) response.ContentLength; remainingBytes > 0;)
{
Thread.Sleep(50);
int bytesRead = stream.Read(buffer, 0, buffer.Length);
totalBytes += bytesRead;
Debug.Print("Downloaded " + bytesRead);
SystemUpdate.CompleteUpdate.Write(buffer, 0, bytesRead);
Debug.Print("Written " + totalBytes + " of " + response.ContentLength);
remainingBytes -= bytesRead;
}
stream.Close();
}
response.Close();
}
}
}
}