Skip to content

Commit

Permalink
configurable constraints; extend MaxFlashAddr to 0xec00 to support eg…
Browse files Browse the repository at this point in the history
…zumer/uv-k5-firmware-custom
  • Loading branch information
qrp73 committed Jun 12, 2024
1 parent ad4c3e0 commit 7a5bb20
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
17 changes: 8 additions & 9 deletions CommandHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,7 @@ public static bool Handshake(Device device)
return true;
}

private const int MinEepromAddr = 0x0000;
private const int MaxEepromAddr = 0x1fff;
private const int MaxEepromBlock = 0xff;
private const int MinFlashAddr = 0x0000;
private const int MaxFlashAddr = 0xe600;
private const int MaxFlashBlock = 0x100;

private static bool ProcessAddressSpace(
Expand Down Expand Up @@ -162,7 +158,7 @@ public static bool ReadEeprom(Device device, int offset, int length, string file

public static bool ReadEeprom(Device device, int offset, int length, Action<int, byte[]> callback)
{
return ProcessAddressSpace(offset, length, 0x80, MinEepromAddr, MaxEepromAddr, MaxEepromBlock,
return ProcessAddressSpace(offset, length, 0x80, FirmwareConstraints.MinEepromAddr, FirmwareConstraints.MaxEepromAddr, MaxEepromBlock,
(absOffset, blockOffset, blockLength) => {
Console.Write(" Read {0:x4}...{1:x4}: ", absOffset, absOffset + blockLength);
device.Send(new PacketReadEepromReq((ushort)absOffset, (byte)blockLength));
Expand Down Expand Up @@ -200,7 +196,7 @@ public static bool WriteEeprom(Device device, int offset, string fileName)

public static bool WriteEeprom(Device device, int offset, byte[] data)
{
return ProcessAddressSpace(offset, data.Length, 0x80, MinEepromAddr, MaxEepromAddr, MaxEepromBlock,
return ProcessAddressSpace(offset, data.Length, 0x80, FirmwareConstraints.MinEepromAddr, FirmwareConstraints.MaxEepromAddr, MaxEepromBlock,
(absOffset, blockOffset, blockLength) => {
Console.Write(" Write {0:x4}...{1:x4}: ", absOffset, absOffset + blockLength);
var subData = new byte[blockLength];
Expand Down Expand Up @@ -272,10 +268,13 @@ public static bool WriteFlash(Device device, string version, byte[] data)
ushort offsetFinal = (ushort)data.Length;
if ((offsetFinal & 0xff) != 0)
offsetFinal = (ushort)((offsetFinal + 0x100) & 0xff00);
if (offsetFinal > 0xe600)
throw new InvalidOperationException("WARNING: DANGEROUS FLASH IMAGE SIZE");
if (offsetFinal > FirmwareConstraints.MaxFlashAddr+1)
throw new InvalidOperationException(
string.Format(
"DANGEROUS FLASH ADDRESS WRITE! offsetFinal=0x{0:x4}",
offsetFinal));

return ProcessAddressSpace(0x0000, data.Length, 0x100, MinFlashAddr, MaxFlashAddr, MaxFlashBlock,
return ProcessAddressSpace(0x0000, data.Length, 0x100, FirmwareConstraints.MinFlashAddr, FirmwareConstraints.MaxFlashAddr, MaxFlashBlock,
(absOffset, blockOffset, blockLength) => {
Console.Write(" Write {0:x4}...{1:x4}: ", absOffset, absOffset + blockLength);
var subData = new byte[blockLength];
Expand Down
1 change: 1 addition & 0 deletions K5TOOL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<Compile Include="Packets\PacketReadRssiReq.cs" />
<Compile Include="Packets\PacketReadRssiAck.cs" />
<Compile Include="Packets\PacketHelloTestReq.cs" />
<Compile Include="Packets\FirmwareConstraints.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Packets\" />
Expand Down
10 changes: 10 additions & 0 deletions Packets/FirmwareConstraints.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace K5TOOL.Packets
{
public static class FirmwareConstraints
{
public static int MinEepromAddr = 0x0000;
public static int MaxEepromAddr = 0x2000-1;
public static int MinFlashAddr = 0x0000;
public static int MaxFlashAddr = 0xec00-1; // tested with egzumer/uv-k5-firmware-custom (uses 0xec00)
}
}
8 changes: 6 additions & 2 deletions Packets/PacketFlashWriteReq.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ public PacketFlashWriteReq(ushort offset, ushort offsetFinal, byte[] data, uint
{
if (offsetFinal < offset+0x100)
throw new ArgumentOutOfRangeException("offsetFinal");
if (offset + 0x100 > 0xe600 || offsetFinal > 0xe600)
throw new InvalidOperationException("DANGEROUS FLASH ADDRESS WRITE!");
if (offset + 0x100 > FirmwareConstraints.MaxFlashAddr+1 || offsetFinal > FirmwareConstraints.MaxFlashAddr+1)
throw new InvalidOperationException(
string.Format(
"DANGEROUS FLASH ADDRESS WRITE! offset=0x{0:x4}, offsetFinal=0x{1:x4}",
offset + 0x100,
offsetFinal));
}

// 0x19, 0x5, 0xc, 0x1, 0x8a, 0x8d, 0x9f, 0x1d, address_msb, address_lsb, address_final_msb, address_final_lsb, length_msb, length_lsb, 0x0, 0x0, ...data
Expand Down

0 comments on commit 7a5bb20

Please sign in to comment.