Skip to content

Commit

Permalink
GetStorageStructure enhancement.
Browse files Browse the repository at this point in the history
  • Loading branch information
maniglia committed Jan 7, 2024
1 parent 5a039ee commit 0fd63d6
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 58 deletions.
83 changes: 51 additions & 32 deletions src/01/KSociety.SharpCubeProgrammer/CubeProgrammerApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace KSociety.SharpCubeProgrammer
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -830,7 +831,8 @@ public void AutomaticMode(string filePath, string address, uint skipErase = 1U,
public (CubeProgrammerError, DeviceStorageStructure) GetStorageStructure()
{
var deviceStorageStructure = new DeviceStorageStructure();

var deviceBankSize = Marshal.SizeOf<DeviceBank>();
var bankSectorSize = Marshal.SizeOf<BankSector>();
var storageStructurePtr = new IntPtr();

var output = CubeProgrammerError.CubeprogrammerErrorOther;
Expand All @@ -847,12 +849,39 @@ public void AutomaticMode(string filePath, string address, uint skipErase = 1U,
var storageStructure = Marshal.PtrToStructure<StorageStructure>(storageStructurePtr);

deviceStorageStructure.BanksNumber = storageStructure.BanksNumber;
var deviceBankResult = Marshal.PtrToStructure<DeviceBank>(storageStructure.Banks);
deviceStorageStructure.SectorsNumber = deviceBankResult.SectorsNumber;
var bankSectors = Marshal.PtrToStructure<BankSector>(deviceBankResult.Sectors);
deviceStorageStructure.Index = bankSectors.Index;
deviceStorageStructure.Size = bankSectors.Size;
deviceStorageStructure.Address = bankSectors.Address;
var deviceBankList = new List<DeviceDeviceBank>();
for (var i = 0; i < storageStructure.BanksNumber; i++)
{

if (storageStructure.Banks != IntPtr.Zero)
{
var deviceBank = Marshal.PtrToStructure<DeviceBank>(storageStructure.Banks + (i * deviceBankSize));
var bankSectorList = new List<BankSector>();
if (deviceBank.Sectors != IntPtr.Zero)
{
for (var ii = 0; ii < deviceBank.SectorsNumber; ii++)
{
var bankSector = Marshal.PtrToStructure<BankSector>(deviceBank.Sectors + (ii * bankSectorSize));

bankSectorList.Add(bankSector);

Marshal.DestroyStructure<BankSector>(deviceBank.Sectors + (ii * bankSectorSize));
}
}

var deviceDeviceBank = new DeviceDeviceBank
{
SectorsNumber = deviceBank.SectorsNumber, Sectors = bankSectorList
};

deviceBankList.Add(deviceDeviceBank);

Marshal.DestroyStructure<DeviceBank>(storageStructure.Banks + (i * deviceBankSize));
}
}

deviceStorageStructure.Banks = deviceBankList;
Marshal.DestroyStructure<StorageStructure>(storageStructurePtr);
}
}
}
Expand Down Expand Up @@ -882,7 +911,20 @@ public CubeProgrammerError SendOptionBytesCmd(string command)
public DevicePeripheralC? InitOptionBytesInterface()
{
var pointer = Native.ProgrammerApi.InitOptionBytesInterface();


return pointer != IntPtr.Zero ? this.DevicePeripheralCHandler(pointer) : null;
}

/// <inheritdoc />
public DevicePeripheralC? FastRomInitOptionBytesInterface(ushort deviceId)
{
var pointer = Native.ProgrammerApi.FastRomInitOptionBytesInterface(deviceId);

return pointer != IntPtr.Zero ? this.DevicePeripheralCHandler(pointer) : null;
}

private DevicePeripheralC? DevicePeripheralCHandler(IntPtr pointer)
{
var pointerSize = Marshal.SizeOf<IntPtr>();

try
Expand Down Expand Up @@ -989,7 +1031,7 @@ public CubeProgrammerError SendOptionBytesCmd(string command)
}
catch (Exception ex)
{
this._logger?.LogError(ex, "InitOptionBytesInterface: ");
this._logger?.LogError(ex, "DevicePeripheralCHandler: ");
}
finally
{
Expand All @@ -999,29 +1041,6 @@ public CubeProgrammerError SendOptionBytesCmd(string command)
return null;
}

/// <inheritdoc />
public PeripheralC? FastRomInitOptionBytesInterface(ushort deviceId)
{
PeripheralC? peripheralC = null;

var pointer = Native.ProgrammerApi.FastRomInitOptionBytesInterface(deviceId);

try
{
peripheralC = Marshal.PtrToStructure<PeripheralC>(pointer);
}
catch (Exception ex)
{
this._logger?.LogError(ex, "FastRomInitOptionBytesInterface: ");
}
finally
{
Marshal.DestroyStructure<PeripheralC>(pointer);
}

return peripheralC;
}

/// <inheritdoc />
public CubeProgrammerError ObDisplay()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright © K-Society and contributors. All rights reserved. Licensed under the K-Society License. See LICENSE.TXT file in the project root for full license information.

namespace KSociety.SharpCubeProgrammer.DeviceDataStructure
{
using System.Collections.Generic;

public struct DeviceDeviceBank
{
public uint SectorsNumber;
public List<BankSector> Sectors;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace KSociety.SharpCubeProgrammer.DeviceDataStructure
{
using System.Collections.Generic;

public struct DeviceStorageStructure
{
public uint BanksNumber;
public uint SectorsNumber;
public uint Index;
public uint Size;
public uint Address;
public List<DeviceDeviceBank> Banks;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public interface ICubeProgrammerApi : IDisposable
/// <summary>
/// This routine allows to get option bytes values of the connected target.
/// </summary>
PeripheralC? FastRomInitOptionBytesInterface(ushort deviceId);
DevicePeripheralC? FastRomInitOptionBytesInterface(ushort deviceId);

/// <summary>
/// This routine allows to display the Option bytes.
Expand Down
31 changes: 21 additions & 10 deletions src/01/Samples/Programming/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,28 @@ private static void Main(string[] args)

if (storageStructure.Item1.Equals(CubeProgrammerError.CubeprogrammerNoError))
{

var storage = storageStructure.Item2;
Logger.LogInformation("Storage structure: \n" +
"Address: {0} \n" +
"BanksNumber: {1} \n" +
"Index: {2} \n" +
"Sectors number: {3} \n" +
"Size: {4} \n",
CubeProgrammerApi.HexConverterToString(storageStructure.Item2.Address),
storageStructure.Item2.BanksNumber,
storageStructure.Item2.Index,
storageStructure.Item2.SectorsNumber,
storageStructure.Item2.Size);
"BanksNumber: {0} \n",

storageStructure.Item2.BanksNumber);

for (var i = 0; i < storageStructure.Item2.BanksNumber; i++)
{
var bank = storageStructure.Item2.Banks[i];
Logger.LogInformation("Bank [{0}] \n" +
"Sector number: {1}", i, bank.SectorsNumber);

for (var ii = 0; ii < bank.SectorsNumber; ii++)
{
var sector = bank.Sectors[ii];
Logger.LogInformation("Sector [{0}] \n" +
"Sector address: {1} \n" +
"Sector index: {2} \n" +
"Sector size: {3} \n", ii, CubeProgrammerApi.HexConverterToString(sector.Address), sector.Index, sector.Size);
}
}
}

//var uid64 = CubeProgrammerApi.GetUID64();
Expand Down
22 changes: 11 additions & 11 deletions src/01/Samples/ProgrammingLegacy/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,17 @@ static void Main(string[] args)

if (storageStructure.Item1.Equals(CubeProgrammerError.CubeprogrammerNoError))
{
Logger.LogInformation("Storage structure: \n" +
"Address: {0} \n" +
"BanksNumber: {1} \n" +
"Index: {2} \n" +
"Sectors number: {3} \n" +
"Size: {4} \n",
CubeProgrammerApi.HexConverterToString(storageStructure.Item2.Address),
storageStructure.Item2.BanksNumber,
storageStructure.Item2.Index,
storageStructure.Item2.SectorsNumber,
storageStructure.Item2.Size);
//Logger.LogInformation("Storage structure: \n" +
// "Address: {0} \n" +
// "BanksNumber: {1} \n" +
// "Index: {2} \n" +
// "Sectors number: {3} \n" +
// "Size: {4} \n",
// CubeProgrammerApi.HexConverterToString(storageStructure.Item2.Address),
// storageStructure.Item2.BanksNumber,
// storageStructure.Item2.Index,
// storageStructure.Item2.SectorsNumber,
// storageStructure.Item2.Size);
}

var peripheral = CubeProgrammerApi.InitOptionBytesInterface();
Expand Down

0 comments on commit 0fd63d6

Please sign in to comment.