/// <summary> /// Read the full contents of the PCM. /// Assumes the PCM is unlocked an were ready to go /// </summary> public async Task <bool> ReadContents(PcmInfo info) { // switch to 4x, if possible. But continue either way. // if the vehicle bus switches but the device does not, the bus will need to time out to revert back to 1x, and the next steps will fail. await VehicleSetVPW4x(true); // execute read kernel Response <byte[]> response = await LoadKernelFromFile("kernel.bin"); if (response.Status != ResponseStatus.Success) { return(false); } if (!await PCMExecute(response.Value, 0xFF9150)) { logger.AddUserMessage("Failed to upload kernel uploaded to PCM"); return(false); } logger.AddUserMessage("kernel uploaded to PCM succesfully"); // read bin block by block // return stream return(true); }
private async void modifyVinButton_Click(object sender, EventArgs e) { try { Response <uint> osidResponse = await this.vehicle.QueryOperatingSystemId(); if (osidResponse.Status != ResponseStatus.Success) { this.AddUserMessage("Operating system query failed: " + osidResponse.Status); return; } PcmInfo info = new PcmInfo(osidResponse.Value); var vinResponse = await this.vehicle.QueryVin(); if (vinResponse.Status != ResponseStatus.Success) { this.AddUserMessage("VIN query failed: " + vinResponse.Status.ToString()); return; } DialogBoxes.VinForm vinForm = new DialogBoxes.VinForm(); vinForm.Vin = vinResponse.Value; DialogResult dialogResult = vinForm.ShowDialog(); if (dialogResult == DialogResult.OK) { Response <bool> unlocked = await this.vehicle.UnlockEcu(info.KeyAlgorithm); if (unlocked.Value) { Response <bool> vinmodified = await this.vehicle.UpdateVin(vinForm.Vin.Trim()); if (vinmodified.Value) { this.AddUserMessage("VIN successfully updated to " + vinForm.Vin); MessageBox.Show("VIN updated to " + vinForm.Vin + " successfully.", "Good news.", MessageBoxButtons.OK); } else { MessageBox.Show("Unable to change the VIN to " + vinForm.Vin + ". Error: " + vinmodified.Status, "Bad news.", MessageBoxButtons.OK); } } else { } } } catch (Exception exception) { this.AddUserMessage("VIN change failed: " + exception.ToString()); } }
private async void writeFullContentsButton_Click(object sender, EventArgs e) { if (this.vehicle == null) { // This shouldn't be possible - it would mean the buttons // were enabled when they shouldn't be. return; } Response <uint> osidResponse = await this.vehicle.QueryOperatingSystemId(); if (osidResponse.Status != ResponseStatus.Success) { this.AddUserMessage("Operating system query failed: " + osidResponse.Status); return; } PcmInfo info = new PcmInfo(osidResponse.Value); Response <bool> unlockResponse = await this.vehicle.UnlockEcu(info.KeyAlgorithm); if (unlockResponse.Status != ResponseStatus.Success) { this.AddUserMessage("Unlock was not successful."); return; } this.AddUserMessage("Unlock succeeded."); string path = this.ShowOpenDialog(); if (path == null) { return; } this.AddUserMessage("Pretending to update PCM with content from " + path); try { using (Stream stream = File.OpenRead(path)) { await this.vehicle.WriteContents(stream); } } catch (IOException exception) { this.AddUserMessage(exception.ToString()); } }
/// <summary> /// Read the full contents of the PCM. /// Assumes the PCM is unlocked an were ready to go /// </summary> public async Task <Response <Stream> > ReadContents(PcmInfo info, CancellationToken cancellationToken) { try { this.device.ClearMessageQueue(); // This must precede the switch to 4X. ToolPresentNotifier toolPresentNotifier = new ToolPresentNotifier(this.logger, this.messageFactory, this.device); await toolPresentNotifier.Notify(); // switch to 4x, if possible. But continue either way. // if the vehicle bus switches but the device does not, the bus will need to time out to revert back to 1x, and the next steps will fail. if (!await this.VehicleSetVPW4x(VpwSpeed.FourX)) { this.logger.AddUserMessage("Stopping here because we were unable to switch to 4X."); return(Response.Create(ResponseStatus.Error, (Stream)null)); } await toolPresentNotifier.Notify(); // execute read kernel Response <byte[]> response = await LoadKernelFromFile("kernel.bin"); if (response.Status != ResponseStatus.Success) { logger.AddUserMessage("Failed to load kernel from file."); return(new Response <Stream>(response.Status, null)); } if (cancellationToken.IsCancellationRequested) { return(Response.Create(ResponseStatus.Cancelled, (Stream)null)); } await toolPresentNotifier.Notify(); // TODO: instead of this hard-coded 0xFF9150, get the base address from the PcmInfo object. if (!await PCMExecute(response.Value, 0xFF9150, cancellationToken)) { logger.AddUserMessage("Failed to upload kernel to PCM"); return(new Response <Stream>( cancellationToken.IsCancellationRequested ? ResponseStatus.Cancelled : ResponseStatus.Error, null)); } logger.AddUserMessage("kernel uploaded to PCM succesfully. Requesting data..."); await this.device.SetTimeout(TimeoutScenario.ReadMemoryBlock); int startAddress = info.ImageBaseAddress; int endAddress = info.ImageBaseAddress + info.ImageSize; int bytesRemaining = info.ImageSize; int blockSize = this.device.MaxReceiveSize - 10 - 2; // allow space for the header and block checksum byte[] image = new byte[info.ImageSize]; while (startAddress < endAddress) { if (cancellationToken.IsCancellationRequested) { return(Response.Create(ResponseStatus.Cancelled, (Stream)null)); } await toolPresentNotifier.Notify(); if (startAddress + blockSize > endAddress) { blockSize = endAddress - startAddress; } if (blockSize < 1) { this.logger.AddUserMessage("Image download complete"); break; } if (!await TryReadBlock(image, blockSize, startAddress)) { this.logger.AddUserMessage( string.Format( "Unable to read block from {0} to {1}", startAddress, (startAddress + blockSize) - 1)); return(new Response <Stream>(ResponseStatus.Error, null)); } startAddress += blockSize; } await this.Cleanup(); // Not sure why this does not get called in the finally block on successfull read? MemoryStream stream = new MemoryStream(image); return(new Response <Stream>(ResponseStatus.Success, stream)); } catch (Exception exception) { this.logger.AddUserMessage("Something went wrong. " + exception.Message); this.logger.AddDebugMessage(exception.ToString()); return(new Response <Stream>(ResponseStatus.Error, null)); } finally { // Sending the exit command at both speeds and revert to 1x. await this.Cleanup(); } }
private async void readFullContentsButton_Click(object sender, EventArgs e) { this.DisableUserInput(); try { if (this.vehicle == null) { // This shouldn't be possible - it would mean the buttons // were enabled when they shouldn't be. return; } Response <uint> osidResponse = await this.vehicle.QueryOperatingSystemId(); if (osidResponse.Status != ResponseStatus.Success) { this.AddUserMessage("Operating system query failed: " + osidResponse.Status); return; } PcmInfo info = new PcmInfo(osidResponse.Value); Response <bool> unlockResponse = await this.vehicle.UnlockEcu(info.KeyAlgorithm); if (unlockResponse.Status != ResponseStatus.Success) { this.AddUserMessage("Unlock was not successful."); return; } this.AddUserMessage("Unlock succeeded."); await this.vehicle.ReadContents(info); /*Response<Stream> readResponse = await this.vehicle.ReadContents(); * if (readResponse.Status != ResponseStatus.Success) * { * this.AddUserMessage("Read failed, " + readResponse.Status.ToString()); * return; * } * * string path = this.ShowSaveAsDialog(); * if (path == null) * { * this.AddUserMessage("Save canceled."); * return; * } * * this.AddUserMessage("Saving to " + path); * * try * { * using (Stream output = File.OpenWrite(path)) * { * await readResponse.Value.CopyToAsync(output); * } * } * catch (IOException exception) * { * this.AddUserMessage(exception.Message); * }*/ } catch (Exception exception) { this.AddUserMessage("Read failed: " + exception.ToString()); } finally { this.EnableUserInput(); } }
/// <summary> /// Read the entire contents of the flash. /// </summary> private async void readFullContents_Routine() { try { this.Invoke((MethodInvoker) delegate() { this.DisableUserInput(); this.cancelButton.Enabled = true; }); this.cancellationTokenSource = new CancellationTokenSource(); if (this.vehicle == null) { // This shouldn't be possible - it would mean the buttons // were enabled when they shouldn't be. return; } this.AddUserMessage("Querying operating system of current PCM."); Response <uint> osidResponse = await this.vehicle.QueryOperatingSystemId(); if (osidResponse.Status != ResponseStatus.Success) { this.AddUserMessage("Operating system query failed, will retry: " + osidResponse.Status); await this.vehicle.ExitKernel(); osidResponse = await this.vehicle.QueryOperatingSystemId(); if (osidResponse.Status != ResponseStatus.Success) { this.AddUserMessage("Operating system query failed, giving up: " + osidResponse.Status); return; } } // Look up the information about this PCM, based on the OSID; this.AddUserMessage("OSID: " + osidResponse.Value); PcmInfo info = new PcmInfo(osidResponse.Value); await this.vehicle.SuppressChatter(); bool unlocked = await this.vehicle.UnlockEcu(info.KeyAlgorithm); if (!unlocked) { this.AddUserMessage("Unlock was not successful."); return; } this.AddUserMessage("Unlock succeeded."); if (this.cancellationTokenSource.Token.IsCancellationRequested) { return; } // Do the actual reading. Response <Stream> readResponse = await this.vehicle.ReadContents(info, this.cancellationTokenSource.Token); if (readResponse.Status != ResponseStatus.Success) { this.AddUserMessage("Read failed, " + readResponse.Status.ToString()); return; } // Get the path to save the image to. // // TODO: remember this value and offer to re-use it, in case // the read fails and the user has to try again. // string path = ""; this.Invoke((MethodInvoker) delegate() { path = this.ShowSaveAsDialog(); }); if (path == null) { this.AddUserMessage("Save canceled."); return; } this.AddUserMessage("Will save to " + path); // Save the contents to the path that the user provided. try { this.AddUserMessage("Saving contents to " + path); readResponse.Value.Position = 0; using (Stream output = File.OpenWrite(path)) { await readResponse.Value.CopyToAsync(output); } } catch (IOException exception) { this.AddUserMessage("Unable to save file: " + exception.Message); this.AddDebugMessage(exception.ToString()); } } catch (Exception exception) { this.AddUserMessage("Read failed: " + exception.ToString()); } finally { this.Invoke((MethodInvoker) delegate() { this.EnableUserInput(); this.cancelButton.Enabled = false; }); // This should not get used again. If it does, that would // indicate a bug, so let's make sure that any attempt to // use it won't go un-noticed. this.cancellationTokenSource = null; } }