Skip to content
This repository was archived by the owner on Jun 5, 2019. It is now read-only.

Commit

Permalink
- Added code to tinybooter to dump out newkey, signature and current …
Browse files Browse the repository at this point in the history
…key on failure to verify signature on key updates to aid in diagnosing issues with Crypto library.

- fixed multiple issues of return or throw on smae line as conditional expression which make debugging difficult. (Some debuggers can't set breakpoints on line and column)
- for the crypto.lib project forced mapping MDK to the RVDS3.1 thumb2 crypto libraries.
- added more readable wire-protocal packet tracing, now prints the command id as a textual name along with a decoded form of the flags with textual names for all active flags to aid in debugging the communications.
- fixed DLLImports for the Crypto.dll to use CDecl calling convention to prevent issues with stack corruption and eliminate MDA exception while debugging.

- Fixed flash driver for MCBSTM32F400 to check for ready status after erase to ensure chip is in a valid state before continuing. Othereise the chip can get corrupted (particularly the Config sector)

- Added Addditional debug and ETW tracing for the NETMF wireprotocol in MFDeploy and VS integration.
- Added additional trace messaging support toe device side wire protocol to help in tracking any failures that might occur on a device.

- Fixed tracing to eprovide event code on WireProtocolTxHeader.

Conflicts:
	crypto/dotNetMF.proj
  • Loading branch information
smaillet-ms authored and mortezag committed Sep 14, 2015
1 parent af70717 commit 5ef03bc
Show file tree
Hide file tree
Showing 15 changed files with 297 additions and 79 deletions.
23 changes: 22 additions & 1 deletion Application/TinyBooter/Commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,17 @@ bool Loader_Engine::Monitor_CheckSignature( WP_Message* msg )
return true;
}

#ifdef DEBUG
// dumps binary block in a form useable as C code constants for isolated testing and verification
void DumpBlockDeclaration( char const* name, UINT8 const* pBlock, size_t len )
{
debug_printf( "const char %s[] = {", name );
for( int i = 0; i < len; ++i )
debug_printf( "%c%d", i == 0 ? ' ' : ',', pBlock[ i ] );
debug_printf( "};\n" );
}
#endif

bool Loader_Engine::Monitor_SignatureKeyUpdate( WP_Message* msg )
{
bool fSuccess = false;
Expand Down Expand Up @@ -1486,13 +1497,23 @@ bool Loader_Engine::Monitor_SignatureKeyUpdate( WP_Message* msg )
ASSERT(0);
fSuccess = true;
}
else
{
#ifdef DEBUG
debug_printf( "Failed cert check for new key:\n");
DumpBlockDeclaration( "newKey", cmd->m_newKey, sizeof(RSAKey) );
DumpBlockDeclaration( "newKeySig", cmd->m_newKeySignature, sizeof( cmd->m_newKeySignature ) );
DumpBlockDeclaration( "currentKey", g_PrimaryConfigManager.GetDeploymentKeys( cmd->m_keyIndex ), sizeof(RSAKey) );
#endif
fSuccess = false;
}
}
}
}

ReplyToCommand( msg, fSuccess, false );

return true;
return true;
}

bool Loader_Engine::Monitor_FlashSectorMap( WP_Message* msg )
Expand Down
12 changes: 8 additions & 4 deletions Application/TinyBooter/ConfigurationManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ void ConfigurationSectorManager::LocateConfigurationSector( UINT32 BlockUsage )

void ConfigurationSectorManager::LoadConfiguration()
{
if (m_device ==NULL) return;
if (m_device ==NULL)
return;

if (m_fSupportsXIP)
{
// Get the real address
Expand All @@ -91,7 +93,8 @@ void ConfigurationSectorManager::WriteConfiguration( UINT32 writeOffset, BYTE *d
BOOL eraseWrite = FALSE;
UINT32 writeLengthInBytes ;

if (m_device ==NULL) return ;
if (m_device ==NULL)
return ;

LoadConfiguration();

Expand Down Expand Up @@ -179,9 +182,10 @@ void ConfigurationSectorManager::EraseWriteConfigBlock( BYTE * data, UINT32 size

BOOL ConfigurationSectorManager::IsBootLoaderRequired( INT32 &bootModeTimeout )
{
const UINT32 c_Empty = 0xFFFFFFFF;
const UINT32 c_Empty = 0xFFFFFFFF;

if(m_device == NULL) return FALSE;
if(m_device == NULL)
return FALSE;

volatile UINT32* data = (volatile UINT32*)&m_configurationSector->BooterFlagArray[ 0 ];

Expand Down
24 changes: 11 additions & 13 deletions Application/TinyBooter/CryptoInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@

#include "CryptoInterface.h"
#include "ConfigurationManager.h"
//--//

extern UINT8* g_ConfigBuffer;
extern int g_ConfigBufferLength;


//--//

extern int g_ConfigBufferLength;

CryptoState::CryptoState( UINT32 dataAddress, UINT32 dataLength, BYTE* sig, UINT32 sigLength, UINT32 sectorType ) :
#if defined(ARM_V1_2)
Expand Down Expand Up @@ -43,14 +38,16 @@ bool CryptoState::VerifySignature( UINT32 keyIndex )
// IF THERE IS NO CONFIG SECTOR IN THE FLASH SECTOR TABLE, THEN WE DON'T HAVE KEYS,
// THEREFORE WE WILL NOT PERFORM SIGNATURE CHECKING.
//
if(g_PrimaryConfigManager.m_device == NULL) return true;
if(g_PrimaryConfigManager.m_device == NULL)
return true;


switch(m_sectorType)
{
case BlockRange::BLOCKTYPE_DEPLOYMENT:
// backwards compatibility
if(g_PrimaryConfigManager.GetTinyBooterVersion() != ConfigurationSector::c_CurrentVersionTinyBooter) return true;
if(g_PrimaryConfigManager.GetTinyBooterVersion() != ConfigurationSector::c_CurrentVersionTinyBooter)
return true;

// if there is no key then we do not need to check the signature for the deployment sectors ONLY
if(g_PrimaryConfigManager.CheckSignatureKeyEmpty( ConfigurationSector::c_DeployKeyDeployment ))
Expand All @@ -73,10 +70,11 @@ bool CryptoState::VerifySignature( UINT32 keyIndex )
ASSERT(g_ConfigBufferLength > 0);
ASSERT(g_ConfigBuffer != NULL);

if(g_ConfigBuffer == NULL || g_ConfigBufferLength <= 0) return false;
if(g_ConfigBuffer == NULL || g_ConfigBufferLength <= 0)
return false;

// the g_ConfigBuffer contains the new configuration data
const ConfigurationSector* pNewCfg = (const ConfigurationSector*)g_ConfigBuffer;
const ConfigurationSector* pNewCfg = (const ConfigurationSector*)g_ConfigBuffer;

bool fCanWrite = false;
bool fRet = false;
Expand Down Expand Up @@ -125,7 +123,8 @@ bool CryptoState::VerifySignature( UINT32 keyIndex )
// backwards compatibility


if(g_PrimaryConfigManager.GetTinyBooterVersion() != ConfigurationSector::c_CurrentVersionTinyBooter) return true;
if(g_PrimaryConfigManager.GetTinyBooterVersion() != ConfigurationSector::c_CurrentVersionTinyBooter)
return true;

// if there is no key then we do not need to check the signature for the deployment sectors ONLY
if (g_PrimaryConfigManager.CheckSignatureKeyEmpty( keyIndex ))
Expand All @@ -136,7 +135,6 @@ bool CryptoState::VerifySignature( UINT32 keyIndex )
key = (RSAKey*)g_PrimaryConfigManager.GetDeploymentKeys( keyIndex );

break;

};

if(key == NULL)
Expand All @@ -151,7 +149,7 @@ bool CryptoState::VerifySignature( UINT32 keyIndex )
{
m_res = ::Crypto_StepRSAOperation( &m_handle );
}

return m_res == CRYPTO_SUCCESS;
}

202 changes: 180 additions & 22 deletions Framework/Debugger/DebuggerEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,164 @@ namespace Microsoft.SPOT.Debugger
{
using System;
using System.Diagnostics.Tracing;
using Microsoft.SPOT.Debugger.WireProtocol;
using WireProtocol;
using System.Collections.Generic;

[EventSource( Name="MsOpenTech-NETMF-Debugger")]
[EventSource( Name="Microsoft-NETMF-Debugger")]
internal class DebuggerEventSource : EventSource
{
public static DebuggerEventSource Log { get { return Log_.Value; } }
private static readonly Lazy<DebuggerEventSource> Log_ = new Lazy<DebuggerEventSource>( ()=>new DebuggerEventSource() );

[Event(1, Opcode=EventOpcode.Send )]
public void WireProtocolTxHeader( uint cmd, uint flags, ushort seq, ushort seqReply )
#if TRACE
[Flags]
enum PacketFlags
{
Trace.TraceInformation( "TX: {0:X08} {1:X08} {2:X04} {3:X04}", cmd, flags, seq, seqReply );
WriteCustomEvent( 1, cmd, flags, seq, seqReply );
None = 0,
NonCritical = 0x0001, // This doesn't need an acknowledge.
Reply = 0x0002, // This is the result of a command.
BadHeader = 0x0004,
BadPayload = 0x0008,
Spare0010 = 0x0010,
Spare0020 = 0x0020,
Spare0040 = 0x0040,
Spare0080 = 0x0080,
Spare0100 = 0x0100,
Spare0200 = 0x0200,
Spare0400 = 0x0400,
Spare0800 = 0x0800,
Spare1000 = 0x1000,
NoCaching = 0x2000,
NACK = 0x4000,
ACK = 0x8000,
}

private static Dictionary<uint, string> CommandNameMap = new Dictionary<uint, string>
{
[ Commands.c_Monitor_Ping ] = "Ping",
[ Commands.c_Monitor_Message ] = "Message",
[ Commands.c_Monitor_ReadMemory ] = "ReadMemory",
[ Commands.c_Monitor_WriteMemory ] = "WriteMemory",
[ Commands.c_Monitor_CheckMemory ] = "CheckMemory",
[ Commands.c_Monitor_EraseMemory ] = "EraseMemory",
[ Commands.c_Monitor_Execute ] = "Execute",
[ Commands.c_Monitor_Reboot ] = "Reboot",
[ Commands.c_Monitor_MemoryMap ] = "MemoryMap",
[ Commands.c_Monitor_ProgramExit ] = "ProgramExit",
[ Commands.c_Monitor_CheckSignature ] = "CheckSignature",
[ Commands.c_Monitor_DeploymentMap ] = "DeploymentMap",
[ Commands.c_Monitor_FlashSectorMap ] = "FlashSectorMap",
[ Commands.c_Monitor_SignatureKeyUpdate ] = "SignatureKeyUpdate",
[ Commands.c_Monitor_OemInfo ] = "OemInfo",
[ Commands.c_Debugging_Execution_BasePtr ] = "Execution_BasePtr",
[ Commands.c_Debugging_Execution_ChangeConditions ] = "Execution_ChangeConditions",
[ Commands.c_Debugging_Execution_SecurityKey ] = "Execution_SecurityKey",
[ Commands.c_Debugging_Execution_Unlock ] = "Execution_Unlock",
[ Commands.c_Debugging_Execution_Allocate ] = "Execution_Allocate",
[ Commands.c_Debugging_Execution_Breakpoints ] = "Execution_Breakpoints",
[ Commands.c_Debugging_Execution_BreakpointHit ] = "Execution_BreakpointHit",
[ Commands.c_Debugging_Execution_BreakpointStatus ] = "Execution_BreakpointStatus",
[ Commands.c_Debugging_Execution_QueryCLRCapabilities ] = "Execution_QueryCLRCapabilities",
[ Commands.c_Debugging_Execution_SetCurrentAppDomain ] = "Execution_SetCurrentAppDomain",
[ Commands.c_Debugging_Thread_Create ] = "Thread_Create",
[ Commands.c_Debugging_Thread_List ] = "Thread_List",
[ Commands.c_Debugging_Thread_Stack ] = "Thread_Stack",
[ Commands.c_Debugging_Thread_Kill ] = "Thread_Kill",
[ Commands.c_Debugging_Thread_Suspend ] = "Thread_Suspend",
[ Commands.c_Debugging_Thread_Resume ] = "Thread_Resume",
[ Commands.c_Debugging_Thread_GetException ] = "Thread_GetException",
[ Commands.c_Debugging_Thread_Unwind ] = "Thread_Unwind",
[ Commands.c_Debugging_Thread_CreateEx ] = "Thread_CreateEx",
[ Commands.c_Debugging_Thread_Get ] = "Thread_Get",
[ Commands.c_Debugging_Stack_Info ] = "Stack_Info",
[ Commands.c_Debugging_Stack_SetIP ] = "Stack_SetIP",
[ Commands.c_Debugging_Value_ResizeScratchPad ] = "Value_ResizeScratchPad",
[ Commands.c_Debugging_Value_GetStack ] = "Value_GetStack",
[ Commands.c_Debugging_Value_GetField ] = "Value_GetField",
[ Commands.c_Debugging_Value_GetArray ] = "Value_GetArray",
[ Commands.c_Debugging_Value_GetBlock ] = "Value_GetBlock",
[ Commands.c_Debugging_Value_GetScratchPad ] = "Value_GetScratchPad",
[ Commands.c_Debugging_Value_SetBlock ] = "Value_SetBlock",
[ Commands.c_Debugging_Value_SetArray ] = "Value_SetArray",
[ Commands.c_Debugging_Value_AllocateObject ] = "Value_AllocateObject",
[ Commands.c_Debugging_Value_AllocateString ] = "Value_AllocateString",
[ Commands.c_Debugging_Value_AllocateArray ] = "Value_AllocateArray",
[ Commands.c_Debugging_Value_Assign ] = "Value_Assign",
[ Commands.c_Debugging_TypeSys_Assemblies ] = "TypeSys_Assemblies",
[ Commands.c_Debugging_TypeSys_AppDomains ] = "TypeSys_AppDomains",
[ Commands.c_Debugging_Resolve_Assembly ] = "Resolve_Assembly",
[ Commands.c_Debugging_Resolve_Type ] = "Resolve_Type",
[ Commands.c_Debugging_Resolve_Field ] = "Resolve_Field",
[ Commands.c_Debugging_Resolve_Method ] = "Resolve_Method",
[ Commands.c_Debugging_Resolve_VirtualMethod ] = "Resolve_VirtualMethod",
[ Commands.c_Debugging_Resolve_AppDomain ] = "Resolve_AppDomain",
[ Commands.c_Debugging_MFUpdate_Start ] = "MFUpdate_Start",
[ Commands.c_Debugging_MFUpdate_AddPacket ] = "MFUpdate_AddPacket",
[ Commands.c_Debugging_MFUpdate_Install ] = "MFUpdate_Install",
[ Commands.c_Debugging_MFUpdate_AuthCmd ] = "MFUpdate_AuthCmd",
[ Commands.c_Debugging_MFUpdate_Authenticate ] = "MFUpdate_Authenticate",
[ Commands.c_Debugging_MFUpdate_GetMissingPkts ] = "MFUpdate_GetMissingPkts",
[ Commands.c_Debugging_UpgradeToSsl ] = "UpgradeToSsl",
[ Commands.c_Debugging_Lcd_NewFrame ] = "Lcd_NewFrame",
[ Commands.c_Debugging_Lcd_NewFrameData ] = "Lcd_NewFrameData",
[ Commands.c_Debugging_Lcd_GetFrame ] = "Lcd_GetFrame",
[ Commands.c_Debugging_Button_Report ] = "Button_Report",
[ Commands.c_Debugging_Button_Inject ] = "Button_Inject",
[ Commands.c_Debugging_Messaging_Query ] = "Messaging_Query",
[ Commands.c_Debugging_Messaging_Send ] = "Messaging_Send",
[ Commands.c_Debugging_Messaging_Reply ] = "Messaging_Reply",
[ Commands.c_Debugging_Logging_GetNumberOfRecords ] = "Logging_GetNumberOfRecords",
[ Commands.c_Debugging_Logging_GetRecord ] = "Logging_GetRecord",
[ Commands.c_Debugging_Logging_Erase ] = "Logging_Erase",
[ Commands.c_Debugging_Logging_GetRecords ] = "Logging_GetRecords",
[ Commands.c_Debugging_Deployment_Status ] = "Deployment_Status",
[ Commands.c_Debugging_Info_SetJMC ] = "Info_SetJMC",
[ Commands.c_Profiling_Command ] = "Profiling_Command",
[ Commands.c_Profiling_Stream ] = "Profiling_Stream"
};

string GetCommandName( uint cmd )
{
string retVal;
if( !CommandNameMap.TryGetValue( cmd, out retVal ) )
retVal = $"0x{cmd:X08}";

return retVal;
}
#endif

[Event( 1, Opcode = EventOpcode.Send )]
public void WireProtocolTxHeader( uint crcHeader, uint crcData, uint cmd, uint flags, ushort seq, ushort seqReply, uint length )
{
#if TRACE
Trace.TraceInformation( "TX: {0} flags=[{1}] hCRC: 0x{2:X08} pCRC: 0x{3:X08} seq: 0x{4:X04} replySeq: 0x{5:X04} len={6}"
, GetCommandName( cmd )
, ( PacketFlags )flags
, crcHeader
, crcData
, seq
, seqReply
, length
);
#endif
WriteCustomEvent( 1, crcHeader, crcData, cmd, flags, seq, seqReply, length );
}

[Event( 2, Opcode = EventOpcode.Receive )]
public void WireProtocolRxHeader( uint cmd, uint flags, ushort seq, ushort seqReply )
public void WireProtocolRxHeader( uint crcHeader, uint crcData, uint cmd, uint flags, ushort seq, ushort seqReply, uint length )
{
Trace.TraceInformation( "RX: {0:X08} {1:X08} {2:X04} {3:X04}", cmd, flags, seq, seqReply );
WriteCustomEvent( 2, cmd, flags, seq, seqReply );
#if TRACE
Trace.TraceInformation( "RX: {0} flags=[{1}] hCRC: 0x{2:X08} pCRC: 0x{3:X08} seq: 0x{4:X04} replySeq: 0x{5:X04} len={6}"
, GetCommandName( cmd )
, ( PacketFlags )flags
, crcHeader
, crcData
, seq
, seqReply
, length
);
#endif
WriteCustomEvent( 2, crcHeader, crcData, cmd, flags, seq, seqReply, length );
}

[Event( 3 )]
Expand All @@ -35,27 +173,47 @@ public void WireProtocolReceiveState( MessageReassembler.ReceiveState state )
[Event(4)]
public void EngineEraseMemory( uint address, uint length )
{
Trace.TraceInformation( "EreaseMemory: @{0:X08}; LEN={1:X08}", address, length );
Trace.TraceInformation( "EraseMemory: @0x{0:X08}; LEN=0x{1:X08}", address, length );
WriteEvent( 4, ( int )address, ( int )length );
}

[Event(5)]
public void EngineWriteMemory( uint address, int length )
{
Trace.TraceInformation( "WriteMemory: @0x{0:X08}; LEN=0x{1:X08}", address, length );
WriteEvent( 5, ( int )address, length );
}

private DebuggerEventSource()
{
}

[NonEvent]
unsafe void WriteCustomEvent(int eventId, uint cmd, uint flags, ushort seq, ushort seqReply )
{
EventData* pDataDesc = stackalloc EventData[ 4 ];
pDataDesc[ 0 ].DataPointer = (IntPtr)( &cmd );
pDataDesc[ 0 ].Size = sizeof( int );
pDataDesc[ 1 ].DataPointer = ( IntPtr )( &flags );
pDataDesc[ 1 ].Size = sizeof( int );
pDataDesc[ 2 ].DataPointer = ( IntPtr )( &seq );
pDataDesc[ 2 ].Size = sizeof( ushort );
pDataDesc[ 3 ].DataPointer = ( IntPtr )( &seqReply );
pDataDesc[ 3 ].Size = sizeof( ushort );
WriteEventCore( eventId, 4, pDataDesc );
unsafe void WriteCustomEvent( int eventId, uint crcHeader, uint crcData, uint cmd, uint flags, ushort seq, ushort seqReply, uint length )
{
EventData* pDataDesc = stackalloc EventData[ 7 ];
pDataDesc[ 0 ].DataPointer = ( IntPtr )( &crcHeader );
pDataDesc[ 0 ].Size = sizeof( uint );

pDataDesc[ 1 ].DataPointer = ( IntPtr )( &crcData );
pDataDesc[ 1 ].Size = sizeof( uint );

pDataDesc[ 2 ].DataPointer = (IntPtr)( &cmd );
pDataDesc[ 2 ].Size = sizeof( uint );

pDataDesc[ 3 ].DataPointer = ( IntPtr )( &flags );
pDataDesc[ 3 ].Size = sizeof( uint );

pDataDesc[ 4 ].DataPointer = ( IntPtr )( &seq );
pDataDesc[ 4 ].Size = sizeof( ushort );

pDataDesc[ 5 ].DataPointer = ( IntPtr )( &seqReply );
pDataDesc[ 5 ].Size = sizeof( ushort );

pDataDesc[ 6 ].DataPointer = ( IntPtr )( &length );
pDataDesc[ 6 ].Size = sizeof( uint );

WriteEventCore( eventId, 7, pDataDesc );
}
}
}
1 change: 1 addition & 0 deletions Framework/Debugger/WireProtocol/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,7 @@ public bool WriteMemory( uint address, byte[ ] buf, int offset, int length )

cmd.PrepareForSend( address, buf, pos, len );

DebuggerEventSource.Log.EngineWriteMemory( address, len );
IncomingMessage reply = SyncMessage( Commands.c_Monitor_WriteMemory, 0, cmd );

if( !IncomingMessage.IsPositiveAcknowledge( reply ) )
Expand Down
Loading

0 comments on commit 5ef03bc

Please # to comment.