diff --git a/LICENSE.TXT b/LICENSE.TXT index 70ca2c9..82fd3a0 100644 --- a/LICENSE.TXT +++ b/LICENSE.TXT @@ -1,5 +1,5 @@ Copyright (c) K-Society and contributors. -This software is released under the Microsoft Reciprocal License (MS-RL) (the "License"); you may not use the software except in compliance with the License. +This software is released under the Microsoft Reciprocal License (MS-RL) AND (SLA0048) (the "License"); you may not use the software except in compliance with the License. The text of the Microsoft Reciprocal License (MS-RL) can be found online at: http://opensource.org/licenses/ms-rl diff --git a/README.md b/README.md index 4508232..fcc62e2 100644 --- a/README.md +++ b/README.md @@ -25,42 +25,87 @@ The ST-Link drivers is required, and can be downloaded from st.com and installed This has been tested on Windows 10, you don't need to install cubeprogrammer. ### KSociety.SharpCubeProgrammer -STM32CubeProgrammer_API C# wrapper. +STM32CubeProgrammer_API C# wrapper, the first wrapper for C#. Any suggestions are welcome. ## Get Packages You can get KSociety.SharpCubeProgrammer by [grabbing the latest NuGet package](https://www.nuget.org/packages/KSociety.SharpCubeProgrammer/). ## Currently supported features + +## STLINK functions - GetStLinkList - GetStLinkEnumerationList - ConnectStLink - Reset + +## Bootloader functions - GetDfuDeviceList - ConnectDfuBootloader + +## General purposes functions - CheckDeviceConnection - GetDeviceGeneralInf - ReadMemory - WriteMemory +- EditSector - DownloadFile - Execute - MassErase - SectorErase +- ReadUnprotect +- TzenRegression (does not exist) +- GetTargetInterfaceType +- GetCancelPointer - FileOpen - Verify - VerifyMemory +- SaveFileToFile - SaveMemoryToFile - Disconnect - DeleteInterfaceList +- AutomaticMode - GetStorageStructure + +## Option Bytes functions - SendOptionBytesCmd - InitOptionBytesInterface - FastRomInitOptionBytesInterface - ObDisplay +## Loaders functions +- SetLoadersPath +- SetExternalLoaderPath +- GetExternalLoaders +- RemoveExternalLoader +- DeleteLoaders + +## STM32WB specific functions +- GetUID64 +- FirmwareDelete +- FirmwareUpgrade +- StartWirelessStack +- UpdateAuthKey +- AuthKeyLock +- WriteUserKey +- AntiRollBack +- StartFus +- UnlockChip + +## STM32MP specific functions +- ProgramSsp + +## STM32 HSM specific functions +- GetHsmFirmwareID +- GetHsmCounter +- GetHsmState +- GetHsmVersion +- GetHsmType +- GetHsmLicense + ## Prerequisites -- Visual Studio 2022 (17.7.6 or higher) with the following installed: +- Visual Studio 2022 (17.8.3 or higher) with the following installed: | Workloads | | :-------- | diff --git a/docs/KSociety.SharpCubeProgrammer/README.md b/docs/KSociety.SharpCubeProgrammer/README.md index 03093f8..fb7778d 100644 --- a/docs/KSociety.SharpCubeProgrammer/README.md +++ b/docs/KSociety.SharpCubeProgrammer/README.md @@ -24,42 +24,87 @@ The ST-Link drivers is required, and can be downloaded from st.com and installed This has been tested on Windows 10, you don't need to install cubeprogrammer. ### KSociety.SharpCubeProgrammer -STM32CubeProgrammer_API C# wrapper. +STM32CubeProgrammer_API C# wrapper, the first wrapper for C#. Any suggestions are welcome. ## Get Packages You can get KSociety.SharpCubeProgrammer by [grabbing the latest NuGet package](https://www.nuget.org/packages/KSociety.SharpCubeProgrammer/). ## Currently supported features + +## STLINK functions - GetStLinkList - GetStLinkEnumerationList - ConnectStLink - Reset + +## Bootloader functions - GetDfuDeviceList - ConnectDfuBootloader + +## General purposes functions - CheckDeviceConnection - GetDeviceGeneralInf - ReadMemory - WriteMemory +- EditSector - DownloadFile - Execute - MassErase - SectorErase +- ReadUnprotect +- TzenRegression (does not exist) +- GetTargetInterfaceType +- GetCancelPointer - FileOpen - Verify - VerifyMemory +- SaveFileToFile - SaveMemoryToFile - Disconnect - DeleteInterfaceList +- AutomaticMode - GetStorageStructure + +## Option Bytes functions - SendOptionBytesCmd - InitOptionBytesInterface - FastRomInitOptionBytesInterface - ObDisplay +## Loaders functions +- SetLoadersPath +- SetExternalLoaderPath +- GetExternalLoaders +- RemoveExternalLoader +- DeleteLoaders + +## STM32WB specific functions +- GetUID64 +- FirmwareDelete +- FirmwareUpgrade +- StartWirelessStack +- UpdateAuthKey +- AuthKeyLock +- WriteUserKey +- AntiRollBack +- StartFus +- UnlockChip + +## STM32MP specific functions +- ProgramSsp + +## STM32 HSM specific functions +- GetHsmFirmwareID +- GetHsmCounter +- GetHsmState +- GetHsmVersion +- GetHsmType +- GetHsmLicense + ## Prerequisites -- Visual Studio 2022 (17.7.6 or higher) with the following installed: +- Visual Studio 2022 (17.8.3 or higher) with the following installed: | Workloads | | :-------- | diff --git a/docs/license.txt b/docs/license.txt new file mode 100644 index 0000000..7b58c7f --- /dev/null +++ b/docs/license.txt @@ -0,0 +1,82 @@ +SLA0048 Software license agreement +================================== + +SOFTWARE PACKAGE LICENSE AGREEMENT +================================== + +Please indicate your acceptance or NON-acceptance by selecting “I ACCEPT” or “I DO NOT ACCEPT” as indicated below in +the media. + +BY INSTALLING COPYING, DOWNLOADING, ACCESSING OR OTHERWISE USING THIS SOFTWARE PACKAGE OR ANY +PART THEREOF (AND THE RELATED DOCUMENTATION) FROM STMICROELECTRONICS INTERNATIONAL N.V, SWISS +BRANCH AND/OR ITS AFFILIATED COMPANIES (STMICROELECTRONICS), THE RECIPIENT, ON BEHALF OF HIMSELF +OR HERSELF, OR ON BEHALF OF ANY ENTITY BY WHICH SUCH RECIPIENT IS EMPLOYED AND/OR ENGAGED +AGREES TO BE BOUND BY THIS SOFTWARE PACKAGE LICENSE AGREEMENT. + +Under STMicroelectronics’ intellectual property rights and subject to applicable licensing terms for any third-party software +incorporated in this software package and applicable Open Source Terms (as defined here below), the redistribution, +reproduction and use in source and binary forms of the software package or any part thereof, with or without modification, are +permitted provided that the following conditions are met: + +1. Redistribution of source code (modified or not) must retain any copyright notice, this list of conditions and the following +disclaimer. + +2. Redistributions in binary form, except as embedded into microcontroller or microprocessor device manufactured by or for +STMicroelectronics or a software update for such device, must reproduce the above copyright notice, this list of conditions +and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of STMicroelectronics nor the names of other contributors to this software package may be used to +endorse or promote products derived from this software package or part thereof without specific written permission. + +4. This software package or any part thereof, including modifications and/or derivative works of this software package, must +be used and execute solely and exclusively on or in combination with a microcontroller or a microprocessor devices +manufactured by or for STMicroelectronics. + +5. No use, reproduction or redistribution of this software package partially or totally may be done in any manner that would +subject this software package to any Open Source Terms (as defined below). + +6. Some portion of the software package may contain software subject to Open Source Terms (as defined below) applicable +for each such portion (“Open Source Software”), as further specified in the software package. Such Open Source Software +is supplied under the applicable Open Source Terms and is not subject to the terms and conditions of license hereunder. +“Open Source Terms” shall mean any open source license which requires as part of distribution of software that the source +code of such software is distributed therewith or otherwise made available, or open source license that substantially +complies with the Open Source definition specified at www.opensource.org and any other comparable open source license +such as for example GNU General Public License (GPL), Eclipse Public License (EPL), Apache Software License, BSD +license and MIT license. + +7. This software package may also include third party software as expressly specified in the software package subject to +specific license terms from such third parties. Such third party software is supplied under such specific license terms and is +not subject to the terms and conditions of license hereunder. By installing copying, downloading, accessing or otherwise +using this software package, the recipient agrees to be bound by such license terms with regard to such third party +software. + +8. STMicroelectronics has no obligation to provide any maintenance, support or updates for the software package. + +9. The software package is and will remain the exclusive property of STMicroelectronics and its licensors. The recipient will +not take any action that jeopardizes STMicroelectronics and its licensors' proprietary rights or acquire any rights in the +software package, except the limited rights specified hereunder. + +10. The recipient shall comply with all applicable laws and regulations affecting the use of the software package or any part +thereof including any applicable export control law or regulation. + +11. Redistribution and use of this software package partially or any part thereof other than as permitted under this license is +void and will automatically terminate your rights under this license + +THIS SOFTWARE PACKAGE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" AND ANY +EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY +INTELLECTUAL PROPERTY RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT +SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE PACKAGE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +EXCEPT AS EXPRESSLY PERMITTED HEREUNDER AND SUBJECT TO THE APPLICABLE LICENSING TERMS FOR ANY +THIRD-PARTY SOFTWARE INCORPORATED IN THE SOFTWARE PACKAGE AND OPEN SOURCE TERMS AS +APPLICABLE, NO LICENSE OR OTHER RIGHTS, WHETHER EXPRESS OR IMPLIED, ARE GRANTED UNDER ANY +PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF STMICROELECTRONICS OR ANY THIRD PARTY. + +SLA0048 +SLA0048 - Rev 4 page 2/3 \ No newline at end of file diff --git a/src/01/KSociety.SharpCubeProgrammer/CubeProgrammerApi.cs b/src/01/KSociety.SharpCubeProgrammer/CubeProgrammerApi.cs index 8cf54b0..fe1a85e 100644 --- a/src/01/KSociety.SharpCubeProgrammer/CubeProgrammerApi.cs +++ b/src/01/KSociety.SharpCubeProgrammer/CubeProgrammerApi.cs @@ -8,6 +8,7 @@ namespace KSociety.SharpCubeProgrammer using System.IO; using System.Linq; using System.Runtime.InteropServices; + using System.Runtime.InteropServices.ComTypes; using System.Threading; using System.Threading.Tasks; using Base.InfraSub.Shared.Class; @@ -121,7 +122,6 @@ private void RegisterStm32BootLoader() private void WmiManagerOnStLinkPortChangeStatus(object sender, Wmi.StLink.StLinkPortChangeStatusEventArgs e) { - //this._logger?.LogTrace("CubeProgrammerApi WmiManagerOnStLinkPortChangeStatus: {0} - {1}", e.PortName, e.Status); if (e.Status) { this.StLinkReady = true; @@ -136,8 +136,6 @@ private void WmiManagerOnStLinkPortChangeStatus(object sender, Wmi.StLink.StLink private void WmiManagerOnStLinkPortScanned(object sender, Wmi.StLink.StLinkPortScannedEventArgs e) { - //this._logger?.LogTrace("CubeProgrammerApi WmiManagerOnStLinkPortScanned: {0}", e.PortsList.Count); - if (e.PortsList.Any()) { this.StLinkReady = true; @@ -149,7 +147,6 @@ private void WmiManagerOnStLinkPortScanned(object sender, Wmi.StLink.StLinkPortS private void WmiManagerOnStm32BootLoaderPortChangeStatus(object sender, Wmi.STM32.STM32BootLoaderPortChangeStatusEventArgs e) { - //this._logger?.LogTrace("CubeProgrammerApi WmiManagerOnStm32BootLoaderPortChangeStatus: {0} - {1}", e.PortName, e.Status); if (e.Status) { this.Stm32BootLoaderReady = true; @@ -164,8 +161,6 @@ private void WmiManagerOnStm32BootLoaderPortChangeStatus(object sender, Wmi.STM3 private void WmiManagerOnStm32BootLoaderPortScanned(object sender, Wmi.STM32.STM32BootLoaderPortScannedEventArgs e) { - //this._logger?.LogTrace("CubeProgrammerApi WmiManagerOnStm32BootLoaderPortScanned: {0}", e.PortsList.Count); - if (e.PortsList.Any()) { this.Stm32BootLoaderReady = true; @@ -189,8 +184,6 @@ public CubeProgrammerError TryConnectStLink(int stLinkProbeIndex = 0, int shared var connectStLinkResult = Native.ProgrammerApi.TryConnectStLink(stLinkProbeIndex, shared, debugConnectMode); output = this.CheckResult(connectStLinkResult); - - //this._logger?.LogTrace("TryConnectStLink: {0} result: {1}", debugConnectParameters.SerialNumber, output); } catch (Exception ex) { @@ -203,22 +196,18 @@ public CubeProgrammerError TryConnectStLink(int stLinkProbeIndex = 0, int shared /// public IEnumerable GetStLinkList(bool shared = false) { - //this._logger?.LogTrace("GetStLinkList shared: {0}", shared); var listPtr = new IntPtr(); var parametersList = new List(); try { var size = Marshal.SizeOf(); - //this._logger?.LogTrace("GetStLinkList size: {0}", size); var numberOfItems = Native.ProgrammerApi.GetStLinkList(ref listPtr, shared ? 1 : 0); - //this._logger?.LogTrace("GetStLinkList number of items: {0}", numberOfItems); if (listPtr != IntPtr.Zero) { for (var i = 0; i < numberOfItems; i++) { var currentItem = Marshal.PtrToStructure(listPtr + (i * size)); - //this._logger?.LogTrace("GetStLinkList DebugConnectParameters: {0} - {1}", i, currentItem.SerialNumber); parametersList.Add(currentItem); } } @@ -238,22 +227,18 @@ public IEnumerable GetStLinkList(bool shared = false) /// public IEnumerable GetStLinkEnumerationList(bool shared = false) { - //this._logger?.LogTrace("GetStLinkList shared: {0}", shared); var listPtr = new IntPtr(); var parametersList = new List(); try { var size = Marshal.SizeOf(); - //this._logger?.LogTrace("GetStLinkList size: {0}", size); var numberOfItems = Native.ProgrammerApi.GetStLinkEnumerationList(ref listPtr, shared ? 1 : 0); - //this._logger?.LogTrace("GetStLinkList number of items: {0}", numberOfItems); if (listPtr != IntPtr.Zero) { for (var i = 0; i < numberOfItems; i++) { var currentItem = Marshal.PtrToStructure(listPtr + (i * size)); - //this._logger?.LogTrace("GetStLinkList DebugConnectParameters: {0} - {1}", i, currentItem.SerialNumber); parametersList.Add(currentItem); } } @@ -280,8 +265,6 @@ public CubeProgrammerError ConnectStLink(DebugConnectParameters debugConnectPara var connectStLinkResult = Native.ProgrammerApi.ConnectStLink(debugConnectParameters); output = this.CheckResult(connectStLinkResult); - - this._logger?.LogTrace("ConnectStLink: {0} result: {1}", debugConnectParameters.SerialNumber, output); } catch (Exception ex) { @@ -296,7 +279,6 @@ public CubeProgrammerError Reset(DebugResetMode rstMode) { var resetResult = Native.ProgrammerApi.Reset(rstMode); var output = this.CheckResult(resetResult); - this._logger?.LogTrace("Reset: {0} result: {1}", rstMode, output); return output; } @@ -339,19 +321,13 @@ public int GetDfuDeviceList(ref List dfuDeviceList) try { var size = Marshal.SizeOf(); - - //this._logger?.LogTrace("GetDfuDeviceList iPID: {0} iVID: {1}", 0xdf11, 0x0483); numberOfItems = Native.ProgrammerApi.GetDfuDeviceList(ref listPtr, 0xdf11, 0x0483); - //this._logger?.LogTrace("GetDfuDeviceList DFU devices found : {0}", numberOfItems); - //var listDereference = Marshal.PtrToStructure(listPtr); if (listPtr != IntPtr.Zero) { for (var i = 0; i < numberOfItems; i++) { var currentItem = Marshal.PtrToStructure(listPtr + (i * size)); - - //this._logger?.LogTrace("GetDfuDeviceList DfuDeviceInfo: {0} - {1}", i, currentItem.SerialNumber); dfuDeviceList.Add(currentItem); } } @@ -371,7 +347,6 @@ public int GetDfuDeviceList(ref List dfuDeviceList) /// public CubeProgrammerError ConnectDfuBootloader(string usbIndex) { - //this._logger?.LogTrace("ConnectDfuBootloader: {0}", usbIndex); var output = CubeProgrammerError.CubeprogrammerErrorOther; try { @@ -424,10 +399,7 @@ public void ConnectI2cBootloader() /// public bool CheckDeviceConnection() { - //Register(); - var checkDeviceConnectionResult = Native.ProgrammerApi.CheckDeviceConnection(); - //this._logger?.LogTrace("CheckDeviceConnection. {0} ROW: {1}", checkDeviceConnectionResult ? "OK" : "KO", checkDeviceConnectionResult); return checkDeviceConnectionResult; } @@ -440,7 +412,6 @@ public bool CheckDeviceConnection() try { generalInf = Marshal.PtrToStructure(pointer); - //this._logger?.LogTrace("GetDeviceGeneralInf: Name: {0} Type: {1} CPU: {2}", generalInf.Value.Name, generalInf.Value.Type, generalInf.Value.Cpu); } catch (Exception ex) { @@ -456,25 +427,16 @@ public bool CheckDeviceConnection() var uintAddress = this.HexConverterToUint(address); var result = CubeProgrammerError.CubeprogrammerErrorOther; var buffer = new byte[byteSize]; - //var bufferSize = Marshal.SizeOf(buffer[0]) * buffer.Length; try { - var bufferPtr = new IntPtr(); //Marshal.AllocHGlobal(bufferSize); + var bufferPtr = new IntPtr(); var readMemoryResult = Native.ProgrammerApi.ReadMemory(uintAddress, ref bufferPtr, Convert.ToUInt32(byteSize)); result = this.CheckResult(readMemoryResult); if (bufferPtr != IntPtr.Zero) { - //var byteArray = Marshal.PtrToStringAnsi(bufferPtr);//Marshal.PtrToStructure(bufferPtr); Marshal.Copy(bufferPtr, buffer, 0, byteSize); - - //for(int i = 0; i < size; i += 4) - //{ - // _logger.LogTrace("ReadMemory: {0} {1} {2} {3}", buffer[i].ToString("X"), buffer[i+1].ToString("X"), buffer[i+2].ToString("X"), buffer[i+3].ToString("X")); - //} - - //_logger.LogTrace("ReadMemory: {0}", BitConverter.ToString(buffer)); } } catch (Exception ex) @@ -495,8 +457,7 @@ public CubeProgrammerError WriteMemory(string address, byte[] data) var gch = GCHandle.Alloc(data, GCHandleType.Pinned); - var writeMemoryResult = - Native.ProgrammerApi.WriteMemory(uintAddress, gch.AddrOfPinnedObject(), (uint)data.Length); + var writeMemoryResult = Native.ProgrammerApi.WriteMemory(uintAddress, gch.AddrOfPinnedObject(), (uint)data.Length); gch.Free(); result = this.CheckResult(writeMemoryResult); @@ -508,13 +469,34 @@ public CubeProgrammerError WriteMemory(string address, byte[] data) } /// - public CubeProgrammerError DownloadFile(string inputFilePath, string address, uint skipErase, uint verify) + public CubeProgrammerError EditSector(string address, byte[] data) + { + var result = CubeProgrammerError.CubeprogrammerErrorOther; + + if (!String.IsNullOrEmpty(address) && data.Length > 0) + { + var uintAddress = this.HexConverterToUint(address); + + var gch = GCHandle.Alloc(data, GCHandleType.Pinned); + + var writeMemoryResult = Native.ProgrammerApi.EditSector(uintAddress, gch.AddrOfPinnedObject(), (uint)data.Length); + gch.Free(); + result = this.CheckResult(writeMemoryResult); + + + return result; + } + + return result; + } + + /// + public CubeProgrammerError DownloadFile(string inputFilePath, string address, uint skipErase = 0U, uint verify = 1U) { var output = CubeProgrammerError.CubeprogrammerErrorOther; var extension = Path.GetExtension(inputFilePath); var binPath = @""; - string filePath; switch (extension) { @@ -535,8 +517,6 @@ public CubeProgrammerError DownloadFile(string inputFilePath, string address, ui var filePathAdapted = String.IsNullOrEmpty(filePath) ? "" : filePath.Replace(@"\", "/"); var binPathAdapted = String.IsNullOrEmpty(binPath) ? "" : binPath.Replace(@"\", "/"); - //this._logger?.LogTrace("DownloadFile: {0} - {1}", filePathAdapted, binPathAdapted); - try { var downloadFileResult = Native.ProgrammerApi.DownloadFile( @@ -546,9 +526,7 @@ public CubeProgrammerError DownloadFile(string inputFilePath, string address, ui verify, binPathAdapted ); - //_logger?.LogTrace("DownloadFile result: {0}", downloadFileResult); output = this.CheckResult(downloadFileResult); - //this._logger?.LogTrace("DownloadFile filePathAdapted: {0} binPathAdapted: {1} downloadFileResult: {2}", filePathAdapted, binPathAdapted, output); } catch (Exception ex) { @@ -569,7 +547,6 @@ public CubeProgrammerError Execute(string address) var executeResult = Native.ProgrammerApi.Execute(uintAddress); output = this.CheckResult(executeResult); - //this._logger?.LogTrace("Execute address: {0} result: {1}", address, output); } catch (Exception ex) { @@ -585,8 +562,6 @@ public CubeProgrammerError MassErase(string sFlashMemName = "") var massEraseResult = Native.ProgrammerApi.MassErase(sFlashMemName); var output = this.CheckResult(massEraseResult); - //this._logger?.LogTrace("MassErase flash mem name: {0} result: {1}", sFlashMemName, output); - return output; } @@ -596,8 +571,6 @@ public CubeProgrammerError SectorErase(uint[] sectors, uint sectorNbr, string sF var sectorEraseResult = Native.ProgrammerApi.SectorErase(sectors, sectorNbr, sFlashMemName); var output = this.CheckResult(sectorEraseResult); - //this._logger?.LogTrace("SectorErase sectors: {0}, sectors number: {1}, flash mem name: {2}, result: {3}", sectors, sectorNbr, sFlashMemName, output); - return output; } @@ -610,6 +583,15 @@ public CubeProgrammerError ReadUnprotect() return output; } + /// + public CubeProgrammerError TzenRegression() + { + var result = Native.ProgrammerApi.TzenRegression(); + var output = this.CheckResult(result); + + return output; + } + /// public TargetInterfaceType? GetTargetInterfaceType() { @@ -636,7 +618,6 @@ public void GetCancelPointer() if (!String.IsNullOrEmpty(filePath)) { var filePathAdapted = filePath.Replace(@"\", "/"); - //this._logger?.LogTrace("File Open: {0}", filePathAdapted); var filePointer = Native.ProgrammerApi.FileOpen(filePathAdapted); @@ -678,16 +659,15 @@ public FileDataC GetFileFromByteArray(byte[] data) } /// - public void FreeFileData() + public void FreeFileData(FileDataC data) { - throw new NotImplementedException(); + Native.ProgrammerApi.FreeFileData(data); } /// public CubeProgrammerError Verify(byte[] data, string address) { var uintAddress = this.HexConverterToUint(address); - //this._logger?.LogTrace("Verify address: {0}", uintAddress); var gch = GCHandle.Alloc(data, GCHandleType.Pinned); @@ -740,9 +720,27 @@ public CubeProgrammerError VerifyMemory(string address, byte[] data) } /// - public void SaveFileToFile() + public CubeProgrammerError SaveFileToFile(FileDataC fileData, string sFileName) { - throw new NotImplementedException(); + var sFileNameAdapted = String.IsNullOrEmpty(sFileName) ? "" : sFileName.Replace(@"\", "/"); + var output = CubeProgrammerError.CubeprogrammerErrorOther; + + if (String.IsNullOrEmpty(sFileNameAdapted)) + { + return output; + } + + try + { + var saveFileToFileResult = Native.ProgrammerApi.SaveFileToFile(fileData, sFileNameAdapted); + output = this.CheckResult(saveFileToFileResult); + } + catch (Exception ex) + { + this._logger?.LogError(ex, "SaveFileToFile: "); + } + + return output; } /// @@ -764,8 +762,6 @@ public CubeProgrammerError SaveMemoryToFile(string address, string size, string Native.ProgrammerApi.SaveMemoryToFile(intAddress, intSize, fileNameAdapted); output = this.CheckResult(saveMemoryToFileResult); - - //this._logger?.LogTrace("SaveMemoryToFile address: {0} size: {1} file name: {2} result: {3}", intAddress, intSize, fileName, output); } catch (Exception ex) { @@ -778,7 +774,6 @@ public CubeProgrammerError SaveMemoryToFile(string address, string size, string /// public CubeProgrammerError Disconnect() { - //this._logger?.LogTrace("Disconnect. "); var result = Native.ProgrammerApi.Disconnect(); var output = this.CheckResult(result); @@ -789,14 +784,19 @@ public CubeProgrammerError Disconnect() /// public void DeleteInterfaceList() { - //this._logger?.LogTrace(" DeleteInterfaceList. "); Native.ProgrammerApi.DeleteInterfaceList(); } /// - public void AutomaticMode() + public void AutomaticMode(string filePath, string address, uint skipErase = 1U, uint verify = 1U, int isMassErase = 0, string obCommand = "", int run = 1) { - throw new NotImplementedException(); + if (!String.IsNullOrEmpty(filePath) || !String.IsNullOrEmpty(address)) + { + var filePathAdapted = filePath.Replace(@"\", "/"); + var uintAddress = this.HexConverterToUint(address); + + Native.ProgrammerApi.AutomaticMode(filePathAdapted, uintAddress, skipErase, verify, isMassErase, obCommand, run); + } } /// @@ -826,8 +826,6 @@ public void AutomaticMode() deviceStorageStructure.Index = bankSectors.Index; deviceStorageStructure.Size = bankSectors.Size; deviceStorageStructure.Address = bankSectors.Address; - - //_logger?.LogTrace("GetStorageStructure: BanksNumber: {0}, SectorsNumber: {1}, Index: {2}, Size: {3}, Address: {4}", deviceStorageStructure.BanksNumber, deviceStorageStructure.SectorsNumber, deviceStorageStructure.Index, deviceStorageStructure.Size, deviceStorageStructure.Address); } } } @@ -850,23 +848,19 @@ public CubeProgrammerError SendOptionBytesCmd(string command) { var result = Native.ProgrammerApi.SendOptionBytesCmd(command); var output = this.CheckResult(result); - this._logger?.LogTrace("SendOptionBytesCmd: {0} result: {1}", command, output); return output; } /// public PeripheralC? InitOptionBytesInterface() { - //this._logger?.LogTrace("InitOptionBytesInterface."); PeripheralC? peripheralC = null; var pointer = Native.ProgrammerApi.InitOptionBytesInterface(); try { - peripheralC = Marshal.PtrToStructure(pointer); - } catch (Exception ex) { @@ -883,16 +877,13 @@ public CubeProgrammerError SendOptionBytesCmd(string command) /// public PeripheralC? FastRomInitOptionBytesInterface(ushort deviceId) { - //this._logger?.LogTrace("FastRomInitOptionBytesInterface."); PeripheralC? peripheralC = null; var pointer = Native.ProgrammerApi.FastRomInitOptionBytesInterface(deviceId); try { - peripheralC = Marshal.PtrToStructure(pointer); - } catch (Exception ex) { @@ -1013,63 +1004,178 @@ public void DeleteLoaders() /// Connection under Reset is mandatory. /// - public void GetUID64() + public (CubeProgrammerError, byte[]) GetUID64() { - throw new NotImplementedException(); + var buffer = new byte[8]; + var bufferPtr = new IntPtr(); + + var getUID64Result = Native.ProgrammerApi.GetUID64(ref bufferPtr ); + + var result = this.CheckResult(getUID64Result); + if (result.Equals(CubeProgrammerError.CubeprogrammerNoError) && bufferPtr != IntPtr.Zero) + { + Marshal.Copy(bufferPtr, buffer, 0, 8); + } + + return (result, buffer); } /// - public void FirmwareDelete() + public CubeProgrammerError FirmwareDelete() { - throw new NotImplementedException(); + var firmwareDeleteResult = Native.ProgrammerApi.FirmwareDelete(); + + var result = this.CheckResult(firmwareDeleteResult); + + return result; } /// - public void FirmwareUpgrade() + public CubeProgrammerError FirmwareUpgrade(string filePath, string address, uint firstInstall, uint startStack, uint verify) { - throw new NotImplementedException(); + var uintAddress = this.HexConverterToUint(address); + var filePathAdapted = String.IsNullOrEmpty(filePath) ? "" : filePath.Replace(@"\", "/"); + + var firmwareUpgradeResult = + Native.ProgrammerApi.FirmwareUpgrade(filePathAdapted, uintAddress, firstInstall, startStack, verify); + + var result = this.CheckResult(firmwareUpgradeResult); + + return result; } /// - public void StartWirelessStack() + public CubeProgrammerError StartWirelessStack() { - throw new NotImplementedException(); + var startWirelessStackResult = Native.ProgrammerApi.StartWirelessStack(); + + var result = this.CheckResult(startWirelessStackResult); + + return result; } /// - public void UpdateAuthKey() + public CubeProgrammerError UpdateAuthKey(string filePath) { - throw new NotImplementedException(); + var updateAuthKeyResult = Native.ProgrammerApi.UpdateAuthKey(filePath); + + var result = this.CheckResult(updateAuthKeyResult); + + return result; } /// - public void AuthKeyLock() + public CubeProgrammerError AuthKeyLock() { - throw new NotImplementedException(); + var authKeyLockResult = Native.ProgrammerApi.AuthKeyLock(); + + var result = this.CheckResult(authKeyLockResult); + + return result; } /// - public void WriteUserKey() + public CubeProgrammerError WriteUserKey(string filePath, byte keyType) { - throw new NotImplementedException(); + var filePathAdapted = String.IsNullOrEmpty(filePath) ? "" : filePath.Replace(@"\", "/"); + + var writeUserKeyResult = Native.ProgrammerApi.WriteUserKey(filePathAdapted, keyType); + + var result = this.CheckResult(writeUserKeyResult); + + return result; } /// - public void AntiRollBack() + public CubeProgrammerError AntiRollBack() { - throw new NotImplementedException(); + var antiRollBackResult = Native.ProgrammerApi.AntiRollBack(); + + var result = this.CheckResult(antiRollBackResult); + + return result; } /// - public void StartFus() + public CubeProgrammerError StartFus() { - throw new NotImplementedException(); + var startFusResult = Native.ProgrammerApi.StartFus(); + + var result = this.CheckResult(startFusResult); + + return result; } /// - public void UnlockChip() + public CubeProgrammerError UnlockChip() { - throw new NotImplementedException(); + var unlockChipResult = Native.ProgrammerApi.UnlockChip(); + + var result = this.CheckResult(unlockChipResult); + + return result; + } + + #endregion + + #region [STM32MP specific functions] + + /// + public CubeProgrammerError ProgramSsp(string sspFile, string licenseFile, string tfaFile, int hsmSlotId) + { + var sspFileAdapted = String.IsNullOrEmpty(sspFile) ? "" : sspFile.Replace(@"\", "/"); + var licenseFileAdapted = String.IsNullOrEmpty(licenseFile) ? "" : licenseFile.Replace(@"\", "/"); + var tfaFileAdapted = String.IsNullOrEmpty(tfaFile) ? "" : tfaFile.Replace(@"\", "/"); + var programSspResult = Native.ProgrammerApi.ProgramSsp(sspFileAdapted, licenseFileAdapted, tfaFileAdapted, hsmSlotId); + + var result = this.CheckResult(programSspResult); + + return result; + } + + #endregion + + #region [STM32 HSM specific functions] + + /// + public string GetHsmFirmwareID(int hsmSlotId) + { + return Native.ProgrammerApi.GetHsmFirmwareID(hsmSlotId); + } + + /// + public ulong GetHsmCounter(int hsmSlotId) + { + return Native.ProgrammerApi.GetHsmCounter(hsmSlotId); + } + + /// + public string GetHsmState(int hsmSlotId) + { + return Native.ProgrammerApi.GetHsmState(hsmSlotId); + } + + /// + public string GetHsmVersion(int hsmSlotId) + { + return Native.ProgrammerApi.GetHsmVersion(hsmSlotId); + } + + /// + public string GetHsmType(int hsmSlotId) + { + return Native.ProgrammerApi.GetHsmType(hsmSlotId); + } + + /// + public CubeProgrammerError GetHsmLicense(int hsmSlotId, string outLicensePath) + { + var outLicensePathAdapted = String.IsNullOrEmpty(outLicensePath) ? "" : outLicensePath.Replace(@"\", "/"); + var getHsmLicenseResult = Native.ProgrammerApi.GetHsmLicense(hsmSlotId, outLicensePathAdapted); + + var result = this.CheckResult(getHsmLicenseResult); + + return result; } #endregion @@ -1148,7 +1254,6 @@ private CubeProgrammerError CheckResult(int result) protected void OnStLinksFoundStatus() { - //this._logger?.LogTrace("OnStLinkFoundStatus"); try { this.StLinksFoundStatus?.Invoke(this, new StLinkFoundEventArgs()); @@ -1161,7 +1266,6 @@ protected void OnStLinksFoundStatus() protected void OnStLinkAdded() { - //this._logger?.LogTrace("OnStLinkAdded"); try { this.StLinkAdded?.Invoke(this, new StLinkAddedEventArgs()); @@ -1174,7 +1278,6 @@ protected void OnStLinkAdded() protected void OnStLinkRemoved() { - //this._logger?.LogTrace("OnStLinkRemoved"); try { this.StLinkRemoved?.Invoke(this, new StLinkRemovedEventArgs()); @@ -1187,7 +1290,6 @@ protected void OnStLinkRemoved() protected void OnStm32BootLoadersFoundStatus() { - //this._logger?.LogTrace("OnStm32BootLoadersFoundStatus"); try { this.Stm32BootLoaderFoundStatus?.Invoke(this, new Stm32BootLoaderFoundEventArgs()); @@ -1200,7 +1302,6 @@ protected void OnStm32BootLoadersFoundStatus() protected void OnStm32BootLoaderAdded() { - //this._logger?.LogTrace("OnStm32BootLoaderAdded"); try { this.Stm32BootLoaderAdded?.Invoke(this, new Stm32BootLoaderAddedEventArgs()); @@ -1213,7 +1314,6 @@ protected void OnStm32BootLoaderAdded() protected void OnStm32BootLoaderRemoved() { - //this._logger?.LogTrace("OnStm32BootLoaderRemoved"); try { this.Stm32BootLoaderRemoved?.Invoke(this, new Stm32BootLoaderRemovedEventArgs()); diff --git a/src/01/KSociety.SharpCubeProgrammer/Interface/ICubeProgrammerApi.cs b/src/01/KSociety.SharpCubeProgrammer/Interface/ICubeProgrammerApi.cs index 2bef4a6..6ec8fc9 100644 --- a/src/01/KSociety.SharpCubeProgrammer/Interface/ICubeProgrammerApi.cs +++ b/src/01/KSociety.SharpCubeProgrammer/Interface/ICubeProgrammerApi.cs @@ -135,11 +135,21 @@ public interface ICubeProgrammerApi : IDisposable /// CubeProgrammerError WriteMemory(string address, byte[] data); + /// + /// This routine allows to write sector data on the user interface with the configuration already initialized. + /// + /// The address to start writing from. + /// Data buffer. + /// CubeprogrammerNoError if the writing operation correctly finished, otherwise an error occurred. + /// Unlike ST-LINK interface, the Bootloader interface can access only to some specific memory regions. + /// Data size should not exceed sector size. + CubeProgrammerError EditSector(string address, byte[] data); + /// /// This routine allows to download data from a file to the memory. /// File formats that are supported : hex, bin, srec, tsv, elf, axf, out, stm32, ext /// - CubeProgrammerError DownloadFile(string inputFilePath, string address, uint skipErase, uint verify); + CubeProgrammerError DownloadFile(string inputFilePath, string address, uint skipErase = 0U, uint verify = 1U); /// /// This routine allows to run the application. @@ -163,6 +173,13 @@ public interface ICubeProgrammerApi : IDisposable /// CubeProgrammerError ReadUnprotect(); + /// + /// This routine allows the TZEN Option Byte regression. + /// + /// CubeprogrammerNoError if the disabling correctly accomplished, otherwise an error occurred. + /// Depending on the device used, this routine take a specific time. + CubeProgrammerError TzenRegression(); + /// /// This routine allows to know the interface what is in use. /// @@ -181,7 +198,7 @@ public interface ICubeProgrammerApi : IDisposable /// /// This routine allows to clean up the handled file data. /// - void FreeFileData(); + void FreeFileData(FileDataC data); /// /// This routine allows to verify if the indicated file data is identical to Flash memory content. @@ -196,7 +213,7 @@ public interface ICubeProgrammerApi : IDisposable /// /// This routine allows to save the data file content to another file. /// - void SaveFileToFile(); + CubeProgrammerError SaveFileToFile(FileDataC fileData, string sFileName); /// /// This routine allows to save Flash memory content to file. @@ -216,7 +233,7 @@ public interface ICubeProgrammerApi : IDisposable /// /// This routine allows to enter and make an automatic process for memory management through JTAG/SWD, UART, DFU, SPI, CAN and I²C interfaces. /// - void AutomaticMode(); + void AutomaticMode(string filePath, string address, uint skipErase = 1U, uint verify = 1U, int isMassErase = 0, string obCommand = "", int run = 1); /// /// This routine allows to get Flash storage information. @@ -287,60 +304,127 @@ public interface ICubeProgrammerApi : IDisposable #region [STM32WB specific] - /// Specific APIs used exclusively for STM32WB series to manage BLE Stack and they are available only through USB DFU and UART bootloader interfaces, + /// Specific APIs used exclusively for STM32WB series to manage BLE Stack, and they are available only through USB DFU and UART bootloader interfaces, /// except for the “firmwareDelete" and the “firmwareUpgrade", available through USB DFU, UART and SWD interfaces. /// Connection under Reset is mandatory. /// /// This routine allows to read the device unique identifier. /// - void GetUID64(); + (CubeProgrammerError, byte[]) GetUID64(); /// /// This routine allows to erase the BLE stack firmware. /// - void FirmwareDelete(); + CubeProgrammerError FirmwareDelete(); /// /// This routine allows to make upgrade of BLE stack firmware or FUS firmware. /// - void FirmwareUpgrade(); + CubeProgrammerError FirmwareUpgrade(string filePath, string address, uint firstInstall, uint startStack, uint verify); /// /// This routine allows to start the programmed Stack. /// - void StartWirelessStack(); + CubeProgrammerError StartWirelessStack(); /// /// This routine allows to start the programmed Stack. /// - void UpdateAuthKey(); + CubeProgrammerError UpdateAuthKey(string filePath); /// /// This routine allows to lock the authentication key and once locked, it is no longer possible to change it. /// - void AuthKeyLock(); + CubeProgrammerError AuthKeyLock(); /// /// This routine allows to write a customized user key. /// - void WriteUserKey(); + CubeProgrammerError WriteUserKey(string filePath, byte keyType); /// /// This routine allows to activate the AntiRollBack. /// - void AntiRollBack(); + CubeProgrammerError AntiRollBack(); /// /// This routine allows to start and establish a communication with the FUS operator. /// - void StartFus(); + CubeProgrammerError StartFus(); /// /// This routine allows to set default option Bytes. /// /// - void UnlockChip(); + CubeProgrammerError UnlockChip(); + + #endregion + + #region [STM32MP specific functions] + + //Specific APIs used exclusively for STM32MP devices. The connection is available only through USB DFU and UART interfaces + + /// + /// This routine aims to launch the Secure Secret Provisioning. + /// If you are trying to start the SSP with HSM, the licenseFile parameter should be empty. + /// + /// Indicates the full path of the ssp file [Use STM32TrustedPackageCreator to generate a ssp image]. + /// Indicates the full path of the license file. If you are trying to start the SSP without HSM, the hsmSlotId should be 0. + /// Indicates the full path of the tfa-ssp file. + /// Indicates the HSM slot ID. + /// 0 if the SSP was finished successfully, otherwise an error occurred. + CubeProgrammerError ProgramSsp(string sspFile, string licenseFile, string tfaFile, int hsmSlotId); + + #endregion + + #region [STM32 HSM specific functions] + + //Specific APIs used exclusively for STM32 devices to manage the Hardware Secure Module. + + /// + /// This routine aims to get the HSM Firmware Identifier. + /// + /// The slot index of the plugged-in HSM + /// string that contains the HSM Firmware Identifier. + string GetHsmFirmwareID(int hsmSlotId); + + /// + /// This routine aims to get the current HSM counter. + /// + /// The slot index of the plugged-in HSM + /// Counter value + ulong GetHsmCounter(int hsmSlotId); + + /// + /// This routine aims to get the HSM State. + /// + /// The slot index of the plugged-in HSM + /// string with possible values: ST_STATE , OEM_STATE, OPERATIONAL_STATE , UNKNOWN_STATE + string GetHsmState(int hsmSlotId); + + /// + /// This routine aims to get the HSM version. + /// + /// The slot index of the plugged-in HSM + /// string with possible values: 1 , 2 + string GetHsmVersion(int hsmSlotId); + + /// + /// This routine aims to get the HSM type. + /// + /// The slot index of the plugged-in HSM + /// string with possible values: SFI. SMU. SSP... + string GetHsmType(int hsmSlotId); + + /// + /// This routine aims to get and save the HSM license into a binary file. + /// Connection to target must be established before performing this routine. + /// + /// The slot index of the plugged-in HSM + /// Path of the output binary file. + /// 0 if the operation was finished successfully, otherwise an error occurred. + CubeProgrammerError GetHsmLicense(int hsmSlotId, string outLicensePath); #endregion diff --git a/src/01/KSociety.SharpCubeProgrammer/Native/ProgrammerApi.cs b/src/01/KSociety.SharpCubeProgrammer/Native/ProgrammerApi.cs index 53d64a7..2b261d4 100644 --- a/src/01/KSociety.SharpCubeProgrammer/Native/ProgrammerApi.cs +++ b/src/01/KSociety.SharpCubeProgrammer/Native/ProgrammerApi.cs @@ -148,10 +148,10 @@ internal static int ConnectStLink(DebugConnectParameters debugParameters) #region [Reset] - [DllImport(ProgrammerDll32, EntryPoint = "Reset", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "Reset", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int Reset32([MarshalAs(UnmanagedType.U4)] DebugResetMode rstMode); - [DllImport(ProgrammerDll64, EntryPoint = "Reset", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "Reset", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int Reset64([MarshalAs(UnmanagedType.U4)] DebugResetMode rstMode); private static int ResetNative(DebugResetMode rstMode) @@ -185,10 +185,10 @@ internal static int Reset(DebugResetMode rstMode) #region [GetUsartList] - [DllImport(ProgrammerDll32, EntryPoint = "GetUsartList", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "GetUsartList", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int GetUsartList32(ref IntPtr usartList); - [DllImport(ProgrammerDll64, EntryPoint = "GetUsartList", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "GetUsartList", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int GetUsartList64(ref IntPtr usartList); private static int GetUsartListNative(ref IntPtr usartList) @@ -218,10 +218,10 @@ internal static int GetUsartList(ref IntPtr usartList) #region [ConnectUsartBootloader] - [DllImport(ProgrammerDll32, EntryPoint = "ConnectUsartBootloader", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "ConnectUsartBootloader", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] private static extern int ConnectUsartBootloader32(UsartConnectParameters usartParameters); - [DllImport(ProgrammerDll64, EntryPoint = "ConnectUsartBootloader", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "ConnectUsartBootloader", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] private static extern int ConnectUsartBootloader64(UsartConnectParameters usartParameters); private static int ConnectUsartBootloaderNative(UsartConnectParameters usartParameters) @@ -251,10 +251,10 @@ internal static int ConnectUsartBootloader(UsartConnectParameters usartParameter #region [SendByteUart] - [DllImport(ProgrammerDll32, EntryPoint = "SendByteUart", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "SendByteUart", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int SendByteUart32(int bytes); - [DllImport(ProgrammerDll64, EntryPoint = "SendByteUart", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "SendByteUart", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int SendByteUart64(int bytes); private static int SendByteUartNative(int bytes) @@ -284,10 +284,10 @@ internal static int SendByteUart(int bytes) #region [GetDfuDeviceList] - [DllImport(ProgrammerDll32, EntryPoint = "GetDfuDeviceList", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "GetDfuDeviceList", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int GetDfuDeviceList32(ref IntPtr dfuList, int iPID, int iVID); - [DllImport(ProgrammerDll64, EntryPoint = "GetDfuDeviceList", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "GetDfuDeviceList", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int GetDfuDeviceList64(ref IntPtr dfuList, int iPID, int iVID); private static int GetDfuDeviceListNative(ref IntPtr dfuList, int iPID, int iVID) @@ -317,10 +317,10 @@ internal static int GetDfuDeviceList(ref IntPtr dfuList, int iPID, int iVID) #region [ConnectDfuBootloader] - [DllImport(ProgrammerDll32, EntryPoint = "ConnectDfuBootloader", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "ConnectDfuBootloader", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] private static extern int ConnectDfuBootloader32(string usbIndex); - [DllImport(ProgrammerDll64, EntryPoint = "ConnectDfuBootloader", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "ConnectDfuBootloader", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] private static extern int ConnectDfuBootloader64(string usbIndex); private static int ConnectDfuBootloaderNative(string usbIndex) @@ -350,10 +350,10 @@ internal static int ConnectDfuBootloader(string usbIndex) #region [ConnectDfuBootloader2] - [DllImport(ProgrammerDll32, EntryPoint = "ConnectDfuBootloader2", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "ConnectDfuBootloader2", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int ConnectDfuBootloader232(DfuConnectParameters dfuParameters); - [DllImport(ProgrammerDll64, EntryPoint = "ConnectDfuBootloader2", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "ConnectDfuBootloader2", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int ConnectDfuBootloader264(DfuConnectParameters dfuParameters); private static int ConnectDfuBootloader2Native(DfuConnectParameters dfuParameters) @@ -383,10 +383,10 @@ internal static int ConnectDfuBootloader2(DfuConnectParameters dfuParameters) #region [ConnectSpiBootloader] - [DllImport(ProgrammerDll32, EntryPoint = "ConnectSpiBootloader", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "ConnectSpiBootloader", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int ConnectSpiBootloader32(SpiConnectParameters spiParameters); - [DllImport(ProgrammerDll64, EntryPoint = "ConnectSpiBootloader", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "ConnectSpiBootloader", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int ConnectSpiBootloader64(SpiConnectParameters spiParameters); private static int ConnectSpiBootloaderNative(SpiConnectParameters spiParameters) @@ -416,10 +416,10 @@ internal static int ConnectSpiBootloader(SpiConnectParameters spiParameters) #region [ConnectCanBootloader] - [DllImport(ProgrammerDll32, EntryPoint = "ConnectCanBootloader", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "ConnectCanBootloader", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int ConnectCanBootloader32(CanConnectParameters canParameters); - [DllImport(ProgrammerDll64, EntryPoint = "ConnectCanBootloader", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "ConnectCanBootloader", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int ConnectCanBootloader64(CanConnectParameters canParameters); private static int ConnectCanBootloaderNative(CanConnectParameters canParameters) @@ -449,10 +449,10 @@ internal static int ConnectCanBootloader(CanConnectParameters canParameters) #region [ConnectI2cBootloader] - [DllImport(ProgrammerDll32, EntryPoint = "ConnectI2cBootloader", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "ConnectI2cBootloader", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int ConnectI2cBootloader32(I2CConnectParameters i2cParameters); - [DllImport(ProgrammerDll64, EntryPoint = "ConnectI2cBootloader", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "ConnectI2cBootloader", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int ConnectI2cBootloader64(I2CConnectParameters i2cParameters); private static int ConnectI2cBootloaderNative(I2CConnectParameters i2cParameters) @@ -492,10 +492,10 @@ internal static int ConnectI2cBootloader(I2CConnectParameters i2cParameters) #region [CheckDeviceConnection] - [DllImport(ProgrammerDll32, EntryPoint = "CheckDeviceConnection", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "CheckDeviceConnection", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern bool CheckDeviceConnection32(); - [DllImport(ProgrammerDll64, EntryPoint = "CheckDeviceConnection", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "CheckDeviceConnection", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern bool CheckDeviceConnection64(); private static bool CheckDeviceConnectionNative() @@ -525,10 +525,10 @@ internal static bool CheckDeviceConnection() #region [GetDeviceGeneralInf] - [DllImport(ProgrammerDll32, EntryPoint = "GetDeviceGeneralInf", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "GetDeviceGeneralInf", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern IntPtr GetDeviceGeneralInf32(); - [DllImport(ProgrammerDll64, EntryPoint = "GetDeviceGeneralInf", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "GetDeviceGeneralInf", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern IntPtr GetDeviceGeneralInf64(); private static IntPtr GetDeviceGeneralInfNative() @@ -558,10 +558,10 @@ internal static IntPtr GetDeviceGeneralInf() #region [ReadMemory] - [DllImport(ProgrammerDll32, EntryPoint = "ReadMemory", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "ReadMemory", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int ReadMemory32(uint address, ref IntPtr data, uint size); - [DllImport(ProgrammerDll64, EntryPoint = "ReadMemory", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "ReadMemory", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int ReadMemory64(uint address, ref IntPtr data, uint size); private static int ReadMemoryNative(uint address, ref IntPtr data, uint size) @@ -591,10 +591,10 @@ internal static int ReadMemory(uint address, ref IntPtr data, uint size) #region [WriteMemory] - [DllImport(ProgrammerDll32, EntryPoint = "WriteMemory", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "WriteMemory", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int WriteMemory32(uint address, IntPtr data, uint size); - [DllImport(ProgrammerDll64, EntryPoint = "WriteMemory", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "WriteMemory", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int WriteMemory64(uint address, IntPtr data, uint size); private static int WriteMemoryNative(uint address, IntPtr data, uint size) @@ -622,12 +622,45 @@ internal static int WriteMemory(uint address, IntPtr data, uint size) #endregion + #region [EditSector] + + [DllImport(ProgrammerDll32, EntryPoint = "EditSector", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + private static extern int EditSector32(uint address, IntPtr data, uint size); + + [DllImport(ProgrammerDll64, EntryPoint = "EditSector", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + private static extern int EditSector64(uint address, IntPtr data, uint size); + + private static int EditSectorNative(uint address, IntPtr data, uint size) + { + return !Environment.Is64BitProcess + ? EditSector32(address, data, size) + : EditSector64(address, data, size); + } + + internal static int EditSector(uint address, IntPtr data, uint size) + { + try + { + return EditSectorNative(address, data, size); + } + catch (DllNotFoundException ex) + { + throw new Exception("K-Society CubeProgrammer implementation not found.", ex); + } + catch (EntryPointNotFoundException ex) + { + throw new Exception("K-Society CubeProgrammer operation not found.", ex); + } + } + + #endregion + #region [DownloadFile] - [DllImport(ProgrammerDll32, EntryPoint = "DownloadFile", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "DownloadFile", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] private static extern int DownloadFile32([MarshalAs(UnmanagedType.LPWStr)] string filePath, uint address, uint skipErase, uint verify, [MarshalAs(UnmanagedType.LPWStr)] string binPath); - [DllImport(ProgrammerDll64, EntryPoint = "DownloadFile", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "DownloadFile", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] private static extern int DownloadFile64([MarshalAs(UnmanagedType.LPWStr)] string filePath, uint address, uint skipErase, uint verify, [MarshalAs(UnmanagedType.LPWStr)] string binPath); private static int DownloadFileNative(string filePath, uint address, uint skipErase, uint verify, string binPath) @@ -657,10 +690,10 @@ internal static int DownloadFile(string filePath, uint address, uint skipErase, #region [Execute] - [DllImport(ProgrammerDll32, EntryPoint = "Execute", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "Execute", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] private static extern int Execute32(uint address); - [DllImport(ProgrammerDll64, EntryPoint = "Execute", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "Execute", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] private static extern int Execute64(uint address); private static int ExecuteNative(uint address) @@ -690,10 +723,10 @@ internal static int Execute(uint address) #region [MassErase] - [DllImport(ProgrammerDll32, EntryPoint = "MassErase", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "MassErase", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] private static extern int MassErase32(string sFlashMemName); - [DllImport(ProgrammerDll64, EntryPoint = "MassErase", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "MassErase", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] private static extern int MassErase64(string sFlashMemName); private static int MassEraseNative(string sFlashMemName) @@ -723,10 +756,10 @@ internal static int MassErase(string sFlashMemName) #region [SectorErase] - [DllImport(ProgrammerDll32, EntryPoint = "SectorErase", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "SectorErase", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] private static extern int SectorErase32(uint[] sectors, uint sectorNbr, string sFlashMemName); - [DllImport(ProgrammerDll64, EntryPoint = "SectorErase", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "SectorErase", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] private static extern int SectorErase64(uint[] sectors, uint sectorNbr, string sFlashMemName); private static int SectorEraseNative(uint[] sectors, uint sectorNbr, string sFlashMemName) @@ -756,10 +789,10 @@ internal static int SectorErase(uint[] sectors, uint sectorNbr, string sFlashMem #region [ReadUnprotect] - [DllImport(ProgrammerDll32, EntryPoint = "ReadUnprotect", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "ReadUnprotect", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int ReadUnprotect32(); - [DllImport(ProgrammerDll64, EntryPoint = "ReadUnprotect", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "ReadUnprotect", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int ReadUnprotect64(); private static int ReadUnprotectNative() @@ -787,12 +820,45 @@ internal static int ReadUnprotect() #endregion + #region [TzenRegression] + + [DllImport(ProgrammerDll32, EntryPoint = "TzenRegression", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + private static extern int TzenRegression32(); + + [DllImport(ProgrammerDll64, EntryPoint = "TzenRegression", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + private static extern int TzenRegression64(); + + private static int TzenRegressionNative() + { + return !Environment.Is64BitProcess + ? TzenRegression32() + : TzenRegression64(); + } + + internal static int TzenRegression() + { + try + { + return TzenRegressionNative(); + } + catch (DllNotFoundException ex) + { + throw new Exception("K-Society CubeProgrammer implementation not found.", ex); + } + catch (EntryPointNotFoundException ex) + { + throw new Exception("K-Society CubeProgrammer operation not found.", ex); + } + } + + #endregion + #region [GetTargetInterfaceType] - [DllImport(ProgrammerDll32, EntryPoint = "GetTargetInterfaceType", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "GetTargetInterfaceType", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int GetTargetInterfaceType32(); - [DllImport(ProgrammerDll64, EntryPoint = "GetTargetInterfaceType", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "GetTargetInterfaceType", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int GetTargetInterfaceType64(); private static int GetTargetInterfaceTypeNative() @@ -822,10 +888,10 @@ internal static int GetTargetInterfaceType() #region [GetCancelPointer] - [DllImport(ProgrammerDll32, EntryPoint = "GetCancelPointer", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "GetCancelPointer", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern IntPtr GetCancelPointer32(); - [DllImport(ProgrammerDll64, EntryPoint = "GetCancelPointer", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "GetCancelPointer", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern IntPtr GetCancelPointer64(); private static IntPtr GetCancelPointerNative() @@ -855,10 +921,10 @@ internal static IntPtr GetCancelPointer() #region [FileOpen] - [DllImport(ProgrammerDll32, EntryPoint = "FileOpen", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "FileOpen", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] private static extern IntPtr FileOpen32([MarshalAs(UnmanagedType.LPWStr)] string filePath); - [DllImport(ProgrammerDll64, EntryPoint = "FileOpen", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "FileOpen", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] private static extern IntPtr FileOpen64([MarshalAs(UnmanagedType.LPWStr)] string filePath); private static IntPtr FileOpenNative(string filePath) @@ -888,10 +954,10 @@ internal static IntPtr FileOpen(string filePath) #region [FreeFileData] - [DllImport(ProgrammerDll32, EntryPoint = "FreeFileData", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "FreeFileData", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern void FreeFileData32(FileDataC data); - [DllImport(ProgrammerDll64, EntryPoint = "FreeFileData", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "FreeFileData", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern void FreeFileData64(FileDataC data); private static void FreeFileDataNative(FileDataC data) @@ -926,10 +992,10 @@ internal static void FreeFileData(FileDataC data) #region [Verify] - [DllImport(ProgrammerDll32, EntryPoint = "Verify", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "Verify", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int Verify32(FileDataC fileData, uint address); - [DllImport(ProgrammerDll64, EntryPoint = "Verify", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "Verify", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int Verify64(FileDataC fileData, uint address); private static int VerifyNative(FileDataC fileData, uint address) @@ -959,10 +1025,10 @@ internal static int Verify(FileDataC fileData, uint address) #region [VerifyMemory] - [DllImport(ProgrammerDll32, EntryPoint = "VerifyMemory", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "VerifyMemory", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int VerifyMemory32(uint address, IntPtr data, uint size); - [DllImport(ProgrammerDll64, EntryPoint = "VerifyMemory", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "VerifyMemory", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int VerifyMemory64(uint address, IntPtr data, uint size); private static int VerifyMemoryNative(uint address, IntPtr data, uint size) @@ -992,10 +1058,10 @@ internal static int VerifyMemory(uint address, IntPtr data, uint size) #region [SaveFileToFile] - [DllImport(ProgrammerDll32, EntryPoint = "SaveFileToFile", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "SaveFileToFile", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] private static extern int SaveFileToFile32(FileDataC fileData, [MarshalAs(UnmanagedType.LPWStr)] string sFileName); - [DllImport(ProgrammerDll64, EntryPoint = "SaveFileToFile", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "SaveFileToFile", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] private static extern int SaveFileToFile64(FileDataC fileData, [MarshalAs(UnmanagedType.LPWStr)] string sFileName); private static int SaveFileToFileNative(FileDataC fileData, string sFileName) @@ -1025,10 +1091,10 @@ internal static int SaveFileToFile(FileDataC fileData, string sFileName) #region [SaveMemoryToFile] - [DllImport(ProgrammerDll32, EntryPoint = "SaveMemoryToFile", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "SaveMemoryToFile", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] private static extern int SaveMemoryToFile32(int address, int size, [MarshalAs(UnmanagedType.LPWStr)] string sFileName); - [DllImport(ProgrammerDll64, EntryPoint = "SaveMemoryToFile", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "SaveMemoryToFile", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] private static extern int SaveMemoryToFile64(int address, int size, [MarshalAs(UnmanagedType.LPWStr)] string sFileName); private static int SaveMemoryToFileNative(int address, int size, string sFileName) @@ -1058,10 +1124,10 @@ internal static int SaveMemoryToFile(int address, int size, string sFileName) #region [Disconnect] - [DllImport(ProgrammerDll32, EntryPoint = "Disconnect", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "Disconnect", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int Disconnect32(); - [DllImport(ProgrammerDll64, EntryPoint = "Disconnect", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "Disconnect", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int Disconnect64(); private static int DisconnectNative() @@ -1091,10 +1157,10 @@ internal static int Disconnect() #region [DeleteInterfaceList] - [DllImport(ProgrammerDll32, EntryPoint = "DeleteInterfaceList", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "DeleteInterfaceList", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern void DeleteInterfaceList32(); - [DllImport(ProgrammerDll64, EntryPoint = "DeleteInterfaceList", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "DeleteInterfaceList", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern void DeleteInterfaceList64(); private static void DeleteInterfaceListNative() @@ -1129,10 +1195,10 @@ internal static void DeleteInterfaceList() #region [AutomaticMode] - [DllImport(ProgrammerDll32, EntryPoint = "AutomaticMode", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "AutomaticMode", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] private static extern void AutomaticMode32([MarshalAs(UnmanagedType.LPWStr)] string filePath, uint address, uint skipErase, uint verify, int isMassErase, string obCommand, int run); - [DllImport(ProgrammerDll64, EntryPoint = "AutomaticMode", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "AutomaticMode", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] private static extern void AutomaticMode64([MarshalAs(UnmanagedType.LPWStr)] string filePath, uint address, uint skipErase, uint verify, int isMassErase, string obCommand, int run); private static void AutomaticModeNative(string filePath, uint address, uint skipErase, uint verify, int isMassErase, string obCommand, int run) @@ -1167,10 +1233,10 @@ internal static void AutomaticMode(string filePath, uint address, uint skipErase #region [GetStorageStructure] - [DllImport(ProgrammerDll32, EntryPoint = "GetStorageStructure", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "GetStorageStructure", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int GetStorageStructure32(ref IntPtr deviceStorageStruct); - [DllImport(ProgrammerDll64, EntryPoint = "GetStorageStructure", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "GetStorageStructure", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int GetStorageStructure64(ref IntPtr deviceStorageStruct); private static int GetStorageStructureNative(ref IntPtr deviceStorageStruct) @@ -1204,10 +1270,10 @@ internal static int GetStorageStructure(ref IntPtr deviceStorageStruct) #region [SendOptionBytesCmd] - [DllImport(ProgrammerDll32, EntryPoint = "SendOptionBytesCmd", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "SendOptionBytesCmd", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] private static extern int SendOptionBytesCmd32(string command); - [DllImport(ProgrammerDll64, EntryPoint = "SendOptionBytesCmd", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "SendOptionBytesCmd", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] private static extern int SendOptionBytesCmd64(string command); private static int SendOptionBytesCmdNative(string command) @@ -1237,10 +1303,10 @@ internal static int SendOptionBytesCmd(string command) #region [InitOptionBytesInterface] - [DllImport(ProgrammerDll32, EntryPoint = "InitOptionBytesInterface", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "InitOptionBytesInterface", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern IntPtr InitOptionBytesInterface32(); - [DllImport(ProgrammerDll64, EntryPoint = "InitOptionBytesInterface", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "InitOptionBytesInterface", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern IntPtr InitOptionBytesInterface64(); private static IntPtr InitOptionBytesInterfaceNative() @@ -1270,10 +1336,10 @@ internal static IntPtr InitOptionBytesInterface() #region [FastRomInitOptionBytesInterface] - [DllImport(ProgrammerDll32, EntryPoint = "FastRomInitOptionBytesInterface", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "FastRomInitOptionBytesInterface", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern IntPtr FastRomInitOptionBytesInterface32(ushort deviceId); - [DllImport(ProgrammerDll64, EntryPoint = "FastRomInitOptionBytesInterface", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "FastRomInitOptionBytesInterface", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern IntPtr FastRomInitOptionBytesInterface64(ushort deviceId); private static IntPtr FastRomInitOptionBytesInterfaceNative(ushort deviceId) @@ -1303,10 +1369,10 @@ internal static IntPtr FastRomInitOptionBytesInterface(ushort deviceId) #region [ObDisplay] - [DllImport(ProgrammerDll32, EntryPoint = "ObDisplay", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "ObDisplay", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int ObDisplay32(); - [DllImport(ProgrammerDll64, EntryPoint = "ObDisplay", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "ObDisplay", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int ObDisplay64(); private static int ObDisplayNative() @@ -1340,10 +1406,10 @@ internal static int ObDisplay() #region [SetLoadersPath] - [DllImport(ProgrammerDll32, EntryPoint = "SetLoadersPath", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "SetLoadersPath", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] private static extern void SetLoadersPath32(string path); - [DllImport(ProgrammerDll64, EntryPoint = "SetLoadersPath", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "SetLoadersPath", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] private static extern void SetLoadersPath64(string path); private static void SetLoadersPathNative(string path) @@ -1378,10 +1444,10 @@ internal static void SetLoadersPath(string path) #region [SetExternalLoaderPath] - [DllImport(ProgrammerDll32, EntryPoint = "SetExternalLoaderPath", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "SetExternalLoaderPath", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] private static extern void SetExternalLoaderPath32(string path, ref IntPtr externalLoaderInfo); - [DllImport(ProgrammerDll64, EntryPoint = "SetExternalLoaderPath", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "SetExternalLoaderPath", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] private static extern void SetExternalLoaderPath64(string path, ref IntPtr externalLoaderInfo); private static void SetExternalLoaderPathNative(string path, ref IntPtr externalLoaderInfo) @@ -1447,10 +1513,10 @@ internal static int GetExternalLoaders(string path, ref IntPtr externalStorageNf #region [RemoveExternalLoader] - [DllImport(ProgrammerDll32, EntryPoint = "RemoveExternalLoader", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "RemoveExternalLoader", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] private static extern void RemoveExternalLoader32(string path); - [DllImport(ProgrammerDll64, EntryPoint = "RemoveExternalLoader", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "RemoveExternalLoader", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] private static extern void RemoveExternalLoader64(string path); private static void RemoveExternalLoaderNative(string path) @@ -1485,10 +1551,10 @@ internal static void RemoveExternalLoader(string path) #region [DeleteLoaders] - [DllImport(ProgrammerDll32, EntryPoint = "DeleteLoaders", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "DeleteLoaders", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern void DeleteLoaders32(); - [DllImport(ProgrammerDll64, EntryPoint = "DeleteLoaders", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "DeleteLoaders", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern void DeleteLoaders64(); private static void DeleteLoadersNative() @@ -1527,24 +1593,24 @@ internal static void DeleteLoaders() #region [GetUID64] - [DllImport(ProgrammerDll32, EntryPoint = "GetUID64", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] - private static extern int GetUID6432(out IntPtr data); + [DllImport(ProgrammerDll32, EntryPoint = "GetUID64", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + private static extern int GetUID6432(ref IntPtr data); - [DllImport(ProgrammerDll64, EntryPoint = "GetUID64", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] - private static extern int GetUID6464(out IntPtr data); + [DllImport(ProgrammerDll64, EntryPoint = "GetUID64", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + private static extern int GetUID6464(ref IntPtr data); - private static int GetUID64Native(out IntPtr data) + private static int GetUID64Native(ref IntPtr data) { return !Environment.Is64BitProcess - ? GetUID6432(out data) - : GetUID6464(out data); + ? GetUID6432(ref data) + : GetUID6464(ref data); } - internal static int GetUID64(out IntPtr data) + internal static int GetUID64(ref IntPtr data) { try { - return GetUID64Native(out data); + return GetUID64Native(ref data); } catch (DllNotFoundException ex) { @@ -1560,10 +1626,10 @@ internal static int GetUID64(out IntPtr data) #region [FirmwareDelete] - [DllImport(ProgrammerDll32, EntryPoint = "FirmwareDelete", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "FirmwareDelete", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int FirmwareDelete32(); - [DllImport(ProgrammerDll64, EntryPoint = "FirmwareDelete", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "FirmwareDelete", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int FirmwareDelete64(); private static int FirmwareDeleteNative() @@ -1593,11 +1659,11 @@ internal static int FirmwareDelete() #region [FirmwareUpgrade] - [DllImport(ProgrammerDll32, EntryPoint = "FirmwareUpgrade", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] - private static extern int FirmwareUpgrade32(string filePath, uint address, uint firstInstall, uint startStack, uint verify); + [DllImport(ProgrammerDll32, EntryPoint = "FirmwareUpgrade", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + private static extern int FirmwareUpgrade32([MarshalAs(UnmanagedType.LPWStr)] string filePath, uint address, uint firstInstall, uint startStack, uint verify); - [DllImport(ProgrammerDll64, EntryPoint = "FirmwareUpgrade", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] - private static extern int FirmwareUpgrade64(string filePath, uint address, uint firstInstall, uint startStack, uint verify); + [DllImport(ProgrammerDll64, EntryPoint = "FirmwareUpgrade", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + private static extern int FirmwareUpgrade64([MarshalAs(UnmanagedType.LPWStr)] string filePath, uint address, uint firstInstall, uint startStack, uint verify); private static int FirmwareUpgradeNative(string filePath, uint address, uint firstInstall, uint startStack, uint verify) { @@ -1626,10 +1692,10 @@ internal static int FirmwareUpgrade(string filePath, uint address, uint firstIns #region [StartWirelessStack] - [DllImport(ProgrammerDll32, EntryPoint = "StartWirelessStack", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "StartWirelessStack", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int StartWirelessStack32(); - [DllImport(ProgrammerDll64, EntryPoint = "StartWirelessStack", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "StartWirelessStack", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int StartWirelessStack64(); private static int StartWirelessStackNative() @@ -1659,11 +1725,11 @@ internal static int StartWirelessStack() #region [UpdateAuthKey] - [DllImport(ProgrammerDll32, EntryPoint = "UpdateAuthKey", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] - private static extern int UpdateAuthKey32(string filePath); + [DllImport(ProgrammerDll32, EntryPoint = "UpdateAuthKey", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + private static extern int UpdateAuthKey32([MarshalAs(UnmanagedType.LPWStr)] string filePath); - [DllImport(ProgrammerDll64, EntryPoint = "UpdateAuthKey", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] - private static extern int UpdateAuthKey64(string filePath); + [DllImport(ProgrammerDll64, EntryPoint = "UpdateAuthKey", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + private static extern int UpdateAuthKey64([MarshalAs(UnmanagedType.LPWStr)] string filePath); private static int UpdateAuthKeyNative(string filePath) { @@ -1692,10 +1758,10 @@ internal static int UpdateAuthKey(string filePath) #region [AuthKeyLock] - [DllImport(ProgrammerDll32, EntryPoint = "AuthKeyLock", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "AuthKeyLock", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int AuthKeyLock32(); - [DllImport(ProgrammerDll64, EntryPoint = "AuthKeyLock", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "AuthKeyLock", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int AuthKeyLock64(); private static int AuthKeyLockNative() @@ -1725,11 +1791,11 @@ internal static int AuthKeyLock() #region [WriteUserKey] - [DllImport(ProgrammerDll32, EntryPoint = "WriteUserKey", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] - private static extern int WriteUserKey32(string filePath, byte keyType); + [DllImport(ProgrammerDll32, EntryPoint = "WriteUserKey", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + private static extern int WriteUserKey32([MarshalAs(UnmanagedType.LPWStr)] string filePath, byte keyType); - [DllImport(ProgrammerDll64, EntryPoint = "WriteUserKey", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] - private static extern int WriteUserKey64(string filePath, byte keyType); + [DllImport(ProgrammerDll64, EntryPoint = "WriteUserKey", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + private static extern int WriteUserKey64([MarshalAs(UnmanagedType.LPWStr)] string filePath, byte keyType); private static int WriteUserKeyNative(string filePath, byte keyType) { @@ -1758,10 +1824,10 @@ internal static int WriteUserKey(string filePath, byte keyType) #region [AntiRollBack] - [DllImport(ProgrammerDll32, EntryPoint = "AntiRollBack", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "AntiRollBack", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int AntiRollBack32(); - [DllImport(ProgrammerDll64, EntryPoint = "AntiRollBack", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "AntiRollBack", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int AntiRollBack64(); private static int AntiRollBackNative() @@ -1791,10 +1857,10 @@ internal static int AntiRollBack() #region [StartFus] - [DllImport(ProgrammerDll32, EntryPoint = "StartFus", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll32, EntryPoint = "StartFus", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int StartFus32(); - [DllImport(ProgrammerDll64, EntryPoint = "StartFus", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + [DllImport(ProgrammerDll64, EntryPoint = "StartFus", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] private static extern int StartFus64(); private static int StartFusNative() @@ -1822,6 +1888,278 @@ internal static int StartFus() #endregion + #region [UnlockChip] + + [DllImport(ProgrammerDll32, EntryPoint = "UnlockChip", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + private static extern int UnlockChip32(); + + [DllImport(ProgrammerDll64, EntryPoint = "UnlockChip", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + private static extern int UnlockChip64(); + + private static int UnlockChipNative() + { + return !Environment.Is64BitProcess + ? UnlockChip32() + : UnlockChip64(); + } + + internal static int UnlockChip() + { + try + { + return UnlockChipNative(); + } + catch (DllNotFoundException ex) + { + throw new Exception("K-Society CubeProgrammer implementation not found.", ex); + } + catch (EntryPointNotFoundException ex) + { + throw new Exception("K-Society CubeProgrammer operation not found.", ex); + } + } + + #endregion + + #endregion + + #region [STM32MP specific functions] + + #region [ProgramSsp] + + [DllImport(ProgrammerDll32, EntryPoint = "ProgramSsp", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + private static extern int ProgramSsp32([MarshalAs(UnmanagedType.LPWStr)] string sspFile, [MarshalAs(UnmanagedType.LPWStr)] string licenseFile, [MarshalAs(UnmanagedType.LPWStr)] string tfaFile, int hsmSlotId); + + [DllImport(ProgrammerDll64, EntryPoint = "ProgramSsp", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + private static extern int ProgramSsp64([MarshalAs(UnmanagedType.LPWStr)] string sspFile, [MarshalAs(UnmanagedType.LPWStr)] string licenseFile, [MarshalAs(UnmanagedType.LPWStr)] string tfaFile, int hsmSlotId); + + private static int ProgramSspNative(string sspFile, string licenseFile, string tfaFile, int hsmSlotId) + { + return !Environment.Is64BitProcess + ? ProgramSsp32(sspFile, licenseFile, tfaFile, hsmSlotId) + : ProgramSsp64(sspFile, licenseFile, tfaFile, hsmSlotId); + } + + internal static int ProgramSsp(string sspFile, string licenseFile, string tfaFile, int hsmSlotId) + { + try + { + return ProgramSspNative(sspFile, licenseFile, tfaFile, hsmSlotId); + } + catch (DllNotFoundException ex) + { + throw new Exception("K-Society CubeProgrammer implementation not found.", ex); + } + catch (EntryPointNotFoundException ex) + { + throw new Exception("K-Society CubeProgrammer operation not found.", ex); + } + } + + #endregion + + #endregion + + #region [STM32 HSM specific functions] + + #region [GetHsmFirmwareID] + + [DllImport(ProgrammerDll32, EntryPoint = "GetHsmFirmwareID", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + private static extern string GetHsmFirmwareID32(int hsmSlotId); + + [DllImport(ProgrammerDll64, EntryPoint = "GetHsmFirmwareID", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + private static extern string GetHsmFirmwareID64(int hsmSlotId); + + private static string GetHsmFirmwareIDNative(int hsmSlotId) + { + return !Environment.Is64BitProcess + ? GetHsmFirmwareID32(hsmSlotId) + : GetHsmFirmwareID64(hsmSlotId); + } + + internal static string GetHsmFirmwareID(int hsmSlotId) + { + try + { + return GetHsmFirmwareIDNative(hsmSlotId); + } + catch (DllNotFoundException ex) + { + throw new Exception("K-Society CubeProgrammer implementation not found.", ex); + } + catch (EntryPointNotFoundException ex) + { + throw new Exception("K-Society CubeProgrammer operation not found.", ex); + } + } + + #endregion + + #region [GetHsmCounter] + + [DllImport(ProgrammerDll32, EntryPoint = "GetHsmCounter", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + private static extern ulong GetHsmCounter32(int hsmSlotId); + + [DllImport(ProgrammerDll64, EntryPoint = "GetHsmCounter", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] + private static extern ulong GetHsmCounter64(int hsmSlotId); + + private static ulong GetHsmCounterNative(int hsmSlotId) + { + return !Environment.Is64BitProcess + ? GetHsmCounter32(hsmSlotId) + : GetHsmCounter64(hsmSlotId); + } + + internal static ulong GetHsmCounter(int hsmSlotId) + { + try + { + return GetHsmCounterNative(hsmSlotId); + } + catch (DllNotFoundException ex) + { + throw new Exception("K-Society CubeProgrammer implementation not found.", ex); + } + catch (EntryPointNotFoundException ex) + { + throw new Exception("K-Society CubeProgrammer operation not found.", ex); + } + } + + #endregion + + #region [GetHsmState] + + [DllImport(ProgrammerDll32, EntryPoint = "GetHsmState", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + private static extern string GetHsmState32(int hsmSlotId); + + [DllImport(ProgrammerDll64, EntryPoint = "GetHsmState", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + private static extern string GetHsmState64(int hsmSlotId); + + private static string GetHsmStateNative(int hsmSlotId) + { + return !Environment.Is64BitProcess + ? GetHsmState32(hsmSlotId) + : GetHsmState64(hsmSlotId); + } + + internal static string GetHsmState(int hsmSlotId) + { + try + { + return GetHsmStateNative(hsmSlotId); + } + catch (DllNotFoundException ex) + { + throw new Exception("K-Society CubeProgrammer implementation not found.", ex); + } + catch (EntryPointNotFoundException ex) + { + throw new Exception("K-Society CubeProgrammer operation not found.", ex); + } + } + + #endregion + + #region [GetHsmVersion] + + [DllImport(ProgrammerDll32, EntryPoint = "GetHsmVersion", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + private static extern string GetHsmVersion32(int hsmSlotId); + + [DllImport(ProgrammerDll64, EntryPoint = "GetHsmVersion", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + private static extern string GetHsmVersion64(int hsmSlotId); + + private static string GetHsmVersionNative(int hsmSlotId) + { + return !Environment.Is64BitProcess + ? GetHsmVersion32(hsmSlotId) + : GetHsmVersion64(hsmSlotId); + } + + internal static string GetHsmVersion(int hsmSlotId) + { + try + { + return GetHsmVersionNative(hsmSlotId); + } + catch (DllNotFoundException ex) + { + throw new Exception("K-Society CubeProgrammer implementation not found.", ex); + } + catch (EntryPointNotFoundException ex) + { + throw new Exception("K-Society CubeProgrammer operation not found.", ex); + } + } + + #endregion + + #region [GetHsmType] + + [DllImport(ProgrammerDll32, EntryPoint = "GetHsmType", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + private static extern string GetHsmType32(int hsmSlotId); + + [DllImport(ProgrammerDll64, EntryPoint = "GetHsmType", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + private static extern string GetHsmType64(int hsmSlotId); + + private static string GetHsmTypeNative(int hsmSlotId) + { + return !Environment.Is64BitProcess + ? GetHsmType32(hsmSlotId) + : GetHsmType64(hsmSlotId); + } + + internal static string GetHsmType(int hsmSlotId) + { + try + { + return GetHsmTypeNative(hsmSlotId); + } + catch (DllNotFoundException ex) + { + throw new Exception("K-Society CubeProgrammer implementation not found.", ex); + } + catch (EntryPointNotFoundException ex) + { + throw new Exception("K-Society CubeProgrammer operation not found.", ex); + } + } + + #endregion + + #region [GetHsmLicense] + + [DllImport(ProgrammerDll32, EntryPoint = "GetHsmLicense", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + private static extern int GetHsmLicense32(int hsmSlotId, [MarshalAs(UnmanagedType.LPWStr)] string outLicensePath); + + [DllImport(ProgrammerDll64, EntryPoint = "GetHsmLicense", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)] + private static extern int GetHsmLicense64(int hsmSlotId, [MarshalAs(UnmanagedType.LPWStr)] string outLicensePath); + + private static int GetHsmLicenseNative(int hsmSlotId, string outLicensePath) + { + return !Environment.Is64BitProcess + ? GetHsmLicense32(hsmSlotId, outLicensePath) + : GetHsmLicense64(hsmSlotId, outLicensePath); + } + + internal static int GetHsmLicense(int hsmSlotId, string outLicensePath) + { + try + { + return GetHsmLicenseNative(hsmSlotId, outLicensePath); + } + catch (DllNotFoundException ex) + { + throw new Exception("K-Society CubeProgrammer implementation not found.", ex); + } + catch (EntryPointNotFoundException ex) + { + throw new Exception("K-Society CubeProgrammer operation not found.", ex); + } + } + + #endregion + #endregion } diff --git a/src/01/KSociety.Test/KSociety.Test.csproj b/src/01/KSociety.Test/KSociety.Test.csproj index e416403..aad51ef 100644 --- a/src/01/KSociety.Test/KSociety.Test.csproj +++ b/src/01/KSociety.Test/KSociety.Test.csproj @@ -13,8 +13,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/src/01/Programmer/Programmer.cpp b/src/01/Programmer/Programmer.cpp index 5541a88..6d79ba6 100644 --- a/src/01/Programmer/Programmer.cpp +++ b/src/01/Programmer/Programmer.cpp @@ -255,6 +255,31 @@ int WriteMemory(unsigned int address, char* data, unsigned int size) } } +int EditSector(unsigned int address, char* data, unsigned int size) +{ + try + { + int result = -99; + + if (size == 0) + { + return -99; + } + + result = editSector(address, data, size); + return result; + } + catch (std::exception& ex) + { + ex; + return -99; + } + catch (...) + { + return -99; + } +} + int DownloadFile(const wchar_t* filePath, unsigned int address, unsigned int skipErase, unsigned int verify, const wchar_t* binPath) { try @@ -307,7 +332,7 @@ int ReadUnprotect() int TzenRegression() { //return tzenRegression(); - return -99; + return -99; } int GetTargetInterfaceType() @@ -530,5 +555,48 @@ int StartFus() int UnlockChip() { - return -1; // unlockchip(); + return unlockchip(); +} + +/* -------------------------------------------------------------------------------------------- */ +/* STM32MP specific functions */ +/* -------------------------------------------------------------------------------------------- */ + +int ProgramSsp(const wchar_t* sspFile, const wchar_t* licenseFile, const wchar_t* tfaFile, int hsmSlotId) +{ + return programSsp(sspFile, licenseFile, tfaFile, hsmSlotId); +} + +/* -------------------------------------------------------------------------------------------- */ +/* STM32 HSM specific functions */ +/* -------------------------------------------------------------------------------------------- */ + +const char* GetHsmFirmwareID(int hsmSlotId) +{ + return getHsmFirmwareID(hsmSlotId); +} + +unsigned long GetHsmCounter(int hsmSlotId) +{ + return getHsmCounter(hsmSlotId); +} + +const char* GetHsmState(int hsmSlotId) +{ + return getHsmState(hsmSlotId); +} + +const char* GetHsmVersion(int hsmSlotId) +{ + return getHsmVersion(hsmSlotId); +} + +const char* GetHsmType(int hsmSlotId) +{ + return getHsmType(hsmSlotId); +} + +int GetHsmLicense(int hsmSlotId, const wchar_t* outLicensePath) +{ + return getHsmLicense(hsmSlotId, outLicensePath); } diff --git a/src/01/Programmer/Programmer.h b/src/01/Programmer/Programmer.h index 450ae65..db8d658 100644 --- a/src/01/Programmer/Programmer.h +++ b/src/01/Programmer/Programmer.h @@ -60,6 +60,8 @@ extern "C" PROGRAMMER_API int ReadMemory(unsigned int address, unsigned char** d extern "C" PROGRAMMER_API int WriteMemory(unsigned int address, char* data, unsigned int size); +extern "C" PROGRAMMER_API int EditSector(unsigned int address, char* data, unsigned int size); + extern "C" PROGRAMMER_API int DownloadFile(const wchar_t* filePath, unsigned int address, unsigned int skipErase, unsigned int verify, const wchar_t* binPath); extern "C" PROGRAMMER_API int Execute(unsigned int address); @@ -145,3 +147,25 @@ extern "C" PROGRAMMER_API int AntiRollBack(); extern "C" PROGRAMMER_API int StartFus(); extern "C" PROGRAMMER_API int UnlockChip(); + +/* -------------------------------------------------------------------------------------------- */ +/* STM32MP specific functions */ +/* -------------------------------------------------------------------------------------------- */ + +extern "C" PROGRAMMER_API int ProgramSsp(const wchar_t* sspFile, const wchar_t* licenseFile, const wchar_t* tfaFile, int hsmSlotId); + +/* -------------------------------------------------------------------------------------------- */ +/* STM32 HSM specific functions */ +/* -------------------------------------------------------------------------------------------- */ + +extern "C" PROGRAMMER_API const char* GetHsmFirmwareID(int hsmSlotId); + +extern "C" PROGRAMMER_API unsigned long GetHsmCounter(int hsmSlotId); + +extern "C" PROGRAMMER_API const char* GetHsmState(int hsmSlotId); + +extern "C" PROGRAMMER_API const char* GetHsmVersion(int hsmSlotId); + +extern "C" PROGRAMMER_API const char* GetHsmType(int hsmSlotId); + +extern "C" PROGRAMMER_API int GetHsmLicense(int hsmSlotId, const wchar_t* outLicensePath); diff --git a/src/01/Samples/Programming/Program.cs b/src/01/Samples/Programming/Program.cs index 0bffb83..0941097 100644 --- a/src/01/Samples/Programming/Program.cs +++ b/src/01/Samples/Programming/Program.cs @@ -142,6 +142,9 @@ private static void Main(string[] args) storageStructure.Item2.Size); } + //var uid64 = CubeProgrammerApi.GetUID64(); + //var startFusREsult = CubeProgrammerApi.StartFus(); + var peripheral = CubeProgrammerApi.InitOptionBytesInterface(); if (peripheral.HasValue) diff --git a/src/Directory.csproj.props b/src/Directory.csproj.props index 017ede4..4ef9a43 100644 --- a/src/Directory.csproj.props +++ b/src/Directory.csproj.props @@ -7,7 +7,6 @@ $([System.IO.Path]::GetFullPath($(MSBuildThisFileDirectory)ksociety.snk)) true - @@ -28,5 +27,6 @@ + \ No newline at end of file diff --git a/src/License.Build.props b/src/License.Build.props new file mode 100644 index 0000000..6ff6636 --- /dev/null +++ b/src/License.Build.props @@ -0,0 +1,18 @@ + + + + + + $(MSBuildThisFileDirectory)..\docs\ + docs\license.txt + + + + + true + docs + false + + + + \ No newline at end of file