private void LaunchThreadToGetBaseAddressAndSize(StorageFile file) { WindowWrapper.Current().Dispatcher.Dispatch(async() => { var r = await SRecordFile.ParseAsync(file, null); List <SRecordFile.Block> lb = r.Item2; FileSize = String.Format("0x{0}", lb[0].data.Length.ToString("x4")); FileBaseAddress = String.Format("0x{0}", lb[0].address.ToString("x4")); }); }
/// <summary> /// Attempts to deploy an SREC (.hex) file to the connected .Net Micro Framework device. The /// signatureFile is used to validate the image once it has been deployed to the device. If /// the signature does not match the image is erased. /// </summary> /// <param name="srecFile">Storage file with the SREC (.hex) file</param> /// <param name="signatureFile">Storage file with the signature file (.sig) for the corresponding SREC file in the srecFile parameter</param> /// <param name="entrypoint">Out parameter that is set to the entry point address for the given SREC file</param> /// <returns>Returns false if the deployment fails, true otherwise /// Possible exceptions: MFFileNotFoundException, MFDeviceNoResponseException, MFUserExitException /// </returns> public async Task <Tuple <uint, bool> > DeployAsync(StorageFile srecFile, StorageFile signatureFile, CancellationToken cancellationToken, IProgress <ProgressReport> progress = null) { uint entryPoint = 0; if (!srecFile.IsAvailable) { throw new FileNotFoundException(srecFile.Path); } if (DebugEngine == null) { throw new MFDeviceNoResponseException(); } // make sure we know who we are talking to if (await CheckForMicroBooterAsync(cancellationToken)) { var reply = await DeploySRECAsync(srecFile, cancellationToken); // check if request was successful if (reply.Item2) { entryPoint = reply.Item1; return(new Tuple <uint, bool>(entryPoint, true)); } else { return(new Tuple <uint, bool>(0, false)); } } await DebugEngine.ConnectAsync(1, 1000, false, ConnectionSource.Unknown); List <SRecordFile.Block> blocks = new List <SRecordFile.Block>(); entryPoint = await SRecordFile.ParseAsync(srecFile, blocks, signatureFile != null?signatureFile : null); if (blocks.Count > 0) { long total = 0; long value = 0; for (int i = 0; i < blocks.Count; i++) { total += (blocks[i] as SRecordFile.Block).data.Length; } await PrepareForDeployAsync(blocks, cancellationToken, progress); foreach (SRecordFile.Block block in blocks) { long len = block.data.Length; uint addr = block.address; // check if cancellation was requested if (cancellationToken.IsCancellationRequested) { throw new MFUserExitException(); } block.data.Seek(0, SeekOrigin.Begin); progress?.Report(new ProgressReport(0, total, string.Format("Erasing sector 0x{0:x08}", block.address))); // the clr requires erase before writing if (!await DebugEngine.EraseMemoryAsync(block.address, (uint)len)) { return(new Tuple <uint, bool>(0, false)); } while (len > 0) { // check if cancellation was requested if (cancellationToken.IsCancellationRequested) { throw new MFUserExitException(); } int buflen = len > 1024 ? 1024 : (int)len; byte[] data = new byte[buflen]; if (block.data.Read(data, 0, buflen) <= 0) { return(new Tuple <uint, bool>(0, false)); } if (!await DebugEngine.WriteMemoryAsync(addr, data)) { return(new Tuple <uint, bool>(0, false)); } value += buflen; addr += (uint)buflen; len -= buflen; progress?.Report(new ProgressReport(value, total, string.Format("Deploying {0}...", srecFile.Name))); } if (DebugEngine.ConnectionSource != ConnectionSource.TinyCLR) { byte[] emptySig = new byte[128]; progress?.Report(new ProgressReport(value, total, "Checking Signature...")); if (!await DebugEngine.CheckSignatureAsync(((block.signature == null || block.signature.Length == 0) ? emptySig : block.signature), 0)) { throw new MFSignatureFailureException(signatureFile); } } } } return(new Tuple <uint, bool>(entryPoint, true)); }