Skip to content

Commit

Permalink
Improve commands that use full WP buffer capacity (#235)
Browse files Browse the repository at this point in the history
***PUBLISH_RELEASE***
***UPDATE_DEPENDENTS***
  • Loading branch information
josesimoes authored Mar 12, 2020
1 parent dfdd0e4 commit 38ee1ff
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,9 @@ public async Task<Tuple<uint, bool>> DeployAsync(StorageFile srecFile, Cancellat
return new Tuple<uint, bool>(0, false);
}

uint buflen = len > DebugEngine.WireProtocolPacketSize ? DebugEngine.WireProtocolPacketSize : (uint)len;
// get packet length, either the maximum allowed size or whatever is still available to TX
uint buflen = Math.Min((uint)DebugEngine.GetPacketMaxLength(new Commands.Monitor_WriteMemory()), (uint)len);

byte[] data = new byte[buflen];

if (block.data.Read(data, 0, (int)buflen) <= 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public void PrepareForDeserialize(int size, byte[] data, Converter converter)
}
}

public class Monitor_WriteMemory
public class Monitor_WriteMemory : OverheadBase
{
public uint m_address = 0;
public uint m_length = 0;
Expand All @@ -194,10 +194,18 @@ public class Reply
public void PrepareForSend(uint address, byte[] data, int offset, int length)
{
m_address = address;

PrepareForSend(data, offset, length);
}

public override bool PrepareForSend(byte[] data, int offset, int length)
{
m_length = (uint)length;
m_data = new byte[length];

Array.Copy(data, offset, m_data, 0, length);

return true;
}
}

Expand Down Expand Up @@ -253,21 +261,23 @@ public void PrepareForDeserialize(int size, byte[] data, Converter converter)
}
}

public class Monitor_Signature
public class Monitor_Signature : OverheadBase
{
public uint m_keyIndex;
public uint m_length = 0;
public byte[] m_signature = null;

public void PrepareForSend(byte[] signature, uint keyIndex)
public override bool PrepareForSend(byte[] signature, int keyIndex, int offset = 0)
{
int length = signature.Length;

m_keyIndex = keyIndex;
m_keyIndex = (uint)keyIndex;
m_length = (uint)length;
m_signature = new byte[length];

Array.Copy(signature, 0, m_signature, 0, length);

return true;
}
}

Expand Down Expand Up @@ -386,7 +396,7 @@ public void PrepareForDeserialize(int size, byte[] data, Converter converter)
}
}

public class Monitor_UpdateConfiguration
public class Monitor_UpdateConfiguration : OverheadBase
{
public uint Configuration;
public uint BlockIndex;
Expand All @@ -400,14 +410,16 @@ public class Reply
public uint ErrorCode;
};

public void PrepareForSend(byte[] data, int length, int offset = 0)
public override bool PrepareForSend(byte[] data, int length, int offset = 0)
{
Length = (uint)length;
Data = new byte[length];

Offset = (uint)offset;

Array.Copy(data, offset, Data, 0, length);

return true;
}
}

Expand Down Expand Up @@ -630,14 +642,14 @@ public class Reply
};


public class Debugging_MFUpdate_AuthCommand
public class Debugging_MFUpdate_AuthCommand : OverheadBase
{
public int m_updateHandle;
public uint m_authCommand;
public uint m_authArgsSize;
public byte[] m_authArgs;

public bool PrepareForSend(byte[] authArgs)
public override bool PrepareForSend(byte[] authArgs, int length = 0, int offset = 0)
{
m_authArgsSize = (uint)authArgs.Length;
m_authArgs = new byte[m_authArgsSize];
Expand All @@ -660,13 +672,13 @@ public void PrepareForDeserialize(int size, byte[] data, Converter converter)
};
};

public class Debugging_MFUpdate_Authenticate
public class Debugging_MFUpdate_Authenticate : OverheadBase
{
public int m_updateHandle;
public uint m_authenticationSize;
public byte[] m_authenticationData;

public bool PrepareForSend(byte[] authenticationData)
public override bool PrepareForSend(byte[] authenticationData, int length = 0, int offset = 0)
{
m_authenticationSize = authenticationData == null ? 0 : (uint)authenticationData.Length;
m_authenticationData = new byte[m_authenticationSize];
Expand Down Expand Up @@ -702,20 +714,22 @@ public void PrepareForDeserialize(int size, byte[] data, Converter converter)
};
};

public class Debugging_MFUpdate_AddPacket
public class Debugging_MFUpdate_AddPacket : OverheadBase
{
public int m_updateHandle;
public uint m_packetIndex;
public uint m_packetValidation;
public uint m_packetLength = 0;
public byte[] m_packetData;

public void PrepareForSend(byte[] packetData)
public override bool PrepareForSend(byte[] packetData, int length = 0, int offset = 0)
{
m_packetLength = (uint)packetData.Length;
m_packetData = new byte[m_packetLength];

Array.Copy(packetData, 0, m_packetData, 0, (int)m_packetLength);

return true;
}

public class Reply
Expand All @@ -724,18 +738,20 @@ public class Reply
};
};

public class Debugging_MFUpdate_Install
public class Debugging_MFUpdate_Install : OverheadBase
{
public int m_updateHandle;
public uint m_updateValidationSize;
public byte[] m_updateValidation;

public void PrepareForSend(byte[] packetValidation)
public override bool PrepareForSend(byte[] packetValidation, int length = 0, int offset = 0)
{
m_updateValidationSize = (uint)packetValidation.Length;
m_updateValidation = new byte[m_updateValidationSize];

Array.Copy(packetValidation, 0, m_updateValidation, 0, (int)m_updateValidationSize);

return true;
}

public class Reply
Expand Down Expand Up @@ -1868,5 +1884,26 @@ public static object ResolveCommandToPayload(uint cmd, bool fReply, CLRCapabilit

return null;
}

public abstract class OverheadBase
{
[IgnoreDataMember]
public int Overhead { get; private set; }

protected OverheadBase()
{
Overhead = GetOverhead();
}

public abstract bool PrepareForSend(byte[] data, int length, int offset = 0);

private int GetOverhead()
{
Converter c = new Converter();
PrepareForSend(new byte[0], 0);

return c.Serialize(this).Length;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,7 @@ public ReleaseInfo GetMonitorOemInfo()
Commands.Monitor_WriteMemory cmd = new Commands.Monitor_WriteMemory();

// get packet length, either the maximum allowed size or whatever is still available to TX
int packetLength = Math.Min((int)WireProtocolPacketSize, count);
int packetLength = Math.Min(GetPacketMaxLength(cmd), count);

cmd.PrepareForSend(address, buf, position, packetLength);

Expand Down Expand Up @@ -3587,11 +3587,11 @@ public bool UpdateDeviceConfiguration(DeviceConfiguration configuration)
};

// get packet length, either the maximum allowed size or whatever is still available to TX
int packetLength = Math.Min((int)WireProtocolPacketSize, count);
int packetLength = Math.Min(GetPacketMaxLength(cmd), count);

// check if this is the last chunk
if(count <= packetLength &&
packetLength <= WireProtocolPacketSize)
packetLength <= GetPacketMaxLength(cmd))
{
// yes, signal that by setting the Done field
cmd.Done = 1;
Expand Down Expand Up @@ -3648,6 +3648,11 @@ public bool UpdateDeviceConfiguration(DeviceConfiguration configuration)
return false;
}

public int GetPacketMaxLength(Commands.OverheadBase cmd)
{
return (int)WireProtocolPacketSize - cmd.Overhead;
}

/// <summary>
/// Writes a specific configuration block to the device.
/// The configuration block is updated only with the changes for this configuration part.
Expand Down Expand Up @@ -3744,7 +3749,7 @@ public bool UpdateDeviceConfiguration<T>(T configuration, uint blockIndex)
};

// get packet length, either the maximum allowed size or whatever is still available to TX
int packetLength = Math.Min((int)WireProtocolPacketSize, count);
int packetLength = Math.Min(GetPacketMaxLength(cmd), count);

cmd.PrepareForSend(configurationSerialized, packetLength, position);

Expand Down

0 comments on commit 38ee1ff

Please sign in to comment.