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

Commit 49be5e9

Browse files
committed
Merge pull request #298 from smaillet-ms/DebugCrypto
Adding support to provide more useful and actionalble diagnostics for Crypto libs and wire protocol debugging
2 parents 9026f15 + 3c61cd8 commit 49be5e9

File tree

15 files changed

+307
-89
lines changed

15 files changed

+307
-89
lines changed

Application/TinyBooter/Commands.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,17 @@ bool Loader_Engine::Monitor_CheckSignature( WP_Message* msg )
14521452
return true;
14531453
}
14541454

1455+
#ifdef DEBUG
1456+
// dumps binary block in a form useable as C code constants for isolated testing and verification
1457+
void DumpBlockDeclaration( char const* name, UINT8 const* pBlock, size_t len )
1458+
{
1459+
debug_printf( "const char %s[] = {", name );
1460+
for( int i = 0; i < len; ++i )
1461+
debug_printf( "%c%d", i == 0 ? ' ' : ',', pBlock[ i ] );
1462+
debug_printf( "};\n" );
1463+
}
1464+
#endif
1465+
14551466
bool Loader_Engine::Monitor_SignatureKeyUpdate( WP_Message* msg )
14561467
{
14571468
bool fSuccess = false;
@@ -1486,13 +1497,23 @@ bool Loader_Engine::Monitor_SignatureKeyUpdate( WP_Message* msg )
14861497
ASSERT(0);
14871498
fSuccess = true;
14881499
}
1500+
else
1501+
{
1502+
#ifdef DEBUG
1503+
debug_printf( "Failed cert check for new key:\n");
1504+
DumpBlockDeclaration( "newKey", cmd->m_newKey, sizeof(RSAKey) );
1505+
DumpBlockDeclaration( "newKeySig", cmd->m_newKeySignature, sizeof( cmd->m_newKeySignature ) );
1506+
DumpBlockDeclaration( "currentKey", g_PrimaryConfigManager.GetDeploymentKeys( cmd->m_keyIndex ), sizeof(RSAKey) );
1507+
#endif
1508+
fSuccess = false;
1509+
}
14891510
}
14901511
}
14911512
}
14921513

14931514
ReplyToCommand( msg, fSuccess, false );
14941515

1495-
return true;
1516+
return true;
14961517
}
14971518

14981519
bool Loader_Engine::Monitor_FlashSectorMap( WP_Message* msg )

Application/TinyBooter/ConfigurationManager.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ void ConfigurationSectorManager::LocateConfigurationSector( UINT32 BlockUsage )
6868

6969
void ConfigurationSectorManager::LoadConfiguration()
7070
{
71-
if (m_device ==NULL) return;
71+
if (m_device ==NULL)
72+
return;
73+
7274
if (m_fSupportsXIP)
7375
{
7476
// Get the real address
@@ -91,7 +93,8 @@ void ConfigurationSectorManager::WriteConfiguration( UINT32 writeOffset, BYTE *d
9193
BOOL eraseWrite = FALSE;
9294
UINT32 writeLengthInBytes ;
9395

94-
if (m_device ==NULL) return ;
96+
if (m_device ==NULL)
97+
return ;
9598

9699
LoadConfiguration();
97100

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

180183
BOOL ConfigurationSectorManager::IsBootLoaderRequired( INT32 &bootModeTimeout )
181184
{
182-
const UINT32 c_Empty = 0xFFFFFFFF;
185+
const UINT32 c_Empty = 0xFFFFFFFF;
183186

184-
if(m_device == NULL) return FALSE;
187+
if(m_device == NULL)
188+
return FALSE;
185189

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

Application/TinyBooter/CryptoInterface.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,9 @@
44

55
#include "CryptoInterface.h"
66
#include "ConfigurationManager.h"
7-
//--//
87

98
extern UINT8* g_ConfigBuffer;
10-
extern int g_ConfigBufferLength;
11-
12-
13-
//--//
14-
9+
extern int g_ConfigBufferLength;
1510

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

4844

4945
switch(m_sectorType)
5046
{
5147
case BlockRange::BLOCKTYPE_DEPLOYMENT:
5248
// backwards compatibility
53-
if(g_PrimaryConfigManager.GetTinyBooterVersion() != ConfigurationSector::c_CurrentVersionTinyBooter) return true;
49+
if(g_PrimaryConfigManager.GetTinyBooterVersion() != ConfigurationSector::c_CurrentVersionTinyBooter)
50+
return true;
5451

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

76-
if(g_ConfigBuffer == NULL || g_ConfigBufferLength <= 0) return false;
73+
if(g_ConfigBuffer == NULL || g_ConfigBufferLength <= 0)
74+
return false;
7775

7876
// the g_ConfigBuffer contains the new configuration data
79-
const ConfigurationSector* pNewCfg = (const ConfigurationSector*)g_ConfigBuffer;
77+
const ConfigurationSector* pNewCfg = (const ConfigurationSector*)g_ConfigBuffer;
8078

8179
bool fCanWrite = false;
8280
bool fRet = false;
@@ -125,7 +123,8 @@ bool CryptoState::VerifySignature( UINT32 keyIndex )
125123
// backwards compatibility
126124

127125

128-
if(g_PrimaryConfigManager.GetTinyBooterVersion() != ConfigurationSector::c_CurrentVersionTinyBooter) return true;
126+
if(g_PrimaryConfigManager.GetTinyBooterVersion() != ConfigurationSector::c_CurrentVersionTinyBooter)
127+
return true;
129128

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

138137
break;
139-
140138
};
141139

142140
if(key == NULL)
@@ -151,7 +149,7 @@ bool CryptoState::VerifySignature( UINT32 keyIndex )
151149
{
152150
m_res = ::Crypto_StepRSAOperation( &m_handle );
153151
}
154-
152+
155153
return m_res == CRYPTO_SUCCESS;
156154
}
157155

Framework/Debugger/DebuggerEventSource.cs

Lines changed: 180 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,164 @@ namespace Microsoft.SPOT.Debugger
44
{
55
using System;
66
using System.Diagnostics.Tracing;
7-
using Microsoft.SPOT.Debugger.WireProtocol;
7+
using WireProtocol;
8+
using System.Collections.Generic;
89

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

15-
[Event(1, Opcode=EventOpcode.Send )]
16-
public void WireProtocolTxHeader( uint cmd, uint flags, ushort seq, ushort seqReply )
16+
#if TRACE
17+
[Flags]
18+
enum PacketFlags
1719
{
18-
Trace.TraceInformation( "TX: {0:X08} {1:X08} {2:X04} {3:X04}", cmd, flags, seq, seqReply );
19-
WriteCustomEvent( 1, cmd, flags, seq, seqReply );
20+
None = 0,
21+
NonCritical = 0x0001, // This doesn't need an acknowledge.
22+
Reply = 0x0002, // This is the result of a command.
23+
BadHeader = 0x0004,
24+
BadPayload = 0x0008,
25+
Spare0010 = 0x0010,
26+
Spare0020 = 0x0020,
27+
Spare0040 = 0x0040,
28+
Spare0080 = 0x0080,
29+
Spare0100 = 0x0100,
30+
Spare0200 = 0x0200,
31+
Spare0400 = 0x0400,
32+
Spare0800 = 0x0800,
33+
Spare1000 = 0x1000,
34+
NoCaching = 0x2000,
35+
NACK = 0x4000,
36+
ACK = 0x8000,
37+
}
38+
39+
private static Dictionary<uint, string> CommandNameMap = new Dictionary<uint, string>
40+
{
41+
[ Commands.c_Monitor_Ping ] = "Ping",
42+
[ Commands.c_Monitor_Message ] = "Message",
43+
[ Commands.c_Monitor_ReadMemory ] = "ReadMemory",
44+
[ Commands.c_Monitor_WriteMemory ] = "WriteMemory",
45+
[ Commands.c_Monitor_CheckMemory ] = "CheckMemory",
46+
[ Commands.c_Monitor_EraseMemory ] = "EraseMemory",
47+
[ Commands.c_Monitor_Execute ] = "Execute",
48+
[ Commands.c_Monitor_Reboot ] = "Reboot",
49+
[ Commands.c_Monitor_MemoryMap ] = "MemoryMap",
50+
[ Commands.c_Monitor_ProgramExit ] = "ProgramExit",
51+
[ Commands.c_Monitor_CheckSignature ] = "CheckSignature",
52+
[ Commands.c_Monitor_DeploymentMap ] = "DeploymentMap",
53+
[ Commands.c_Monitor_FlashSectorMap ] = "FlashSectorMap",
54+
[ Commands.c_Monitor_SignatureKeyUpdate ] = "SignatureKeyUpdate",
55+
[ Commands.c_Monitor_OemInfo ] = "OemInfo",
56+
[ Commands.c_Debugging_Execution_BasePtr ] = "Execution_BasePtr",
57+
[ Commands.c_Debugging_Execution_ChangeConditions ] = "Execution_ChangeConditions",
58+
[ Commands.c_Debugging_Execution_SecurityKey ] = "Execution_SecurityKey",
59+
[ Commands.c_Debugging_Execution_Unlock ] = "Execution_Unlock",
60+
[ Commands.c_Debugging_Execution_Allocate ] = "Execution_Allocate",
61+
[ Commands.c_Debugging_Execution_Breakpoints ] = "Execution_Breakpoints",
62+
[ Commands.c_Debugging_Execution_BreakpointHit ] = "Execution_BreakpointHit",
63+
[ Commands.c_Debugging_Execution_BreakpointStatus ] = "Execution_BreakpointStatus",
64+
[ Commands.c_Debugging_Execution_QueryCLRCapabilities ] = "Execution_QueryCLRCapabilities",
65+
[ Commands.c_Debugging_Execution_SetCurrentAppDomain ] = "Execution_SetCurrentAppDomain",
66+
[ Commands.c_Debugging_Thread_Create ] = "Thread_Create",
67+
[ Commands.c_Debugging_Thread_List ] = "Thread_List",
68+
[ Commands.c_Debugging_Thread_Stack ] = "Thread_Stack",
69+
[ Commands.c_Debugging_Thread_Kill ] = "Thread_Kill",
70+
[ Commands.c_Debugging_Thread_Suspend ] = "Thread_Suspend",
71+
[ Commands.c_Debugging_Thread_Resume ] = "Thread_Resume",
72+
[ Commands.c_Debugging_Thread_GetException ] = "Thread_GetException",
73+
[ Commands.c_Debugging_Thread_Unwind ] = "Thread_Unwind",
74+
[ Commands.c_Debugging_Thread_CreateEx ] = "Thread_CreateEx",
75+
[ Commands.c_Debugging_Thread_Get ] = "Thread_Get",
76+
[ Commands.c_Debugging_Stack_Info ] = "Stack_Info",
77+
[ Commands.c_Debugging_Stack_SetIP ] = "Stack_SetIP",
78+
[ Commands.c_Debugging_Value_ResizeScratchPad ] = "Value_ResizeScratchPad",
79+
[ Commands.c_Debugging_Value_GetStack ] = "Value_GetStack",
80+
[ Commands.c_Debugging_Value_GetField ] = "Value_GetField",
81+
[ Commands.c_Debugging_Value_GetArray ] = "Value_GetArray",
82+
[ Commands.c_Debugging_Value_GetBlock ] = "Value_GetBlock",
83+
[ Commands.c_Debugging_Value_GetScratchPad ] = "Value_GetScratchPad",
84+
[ Commands.c_Debugging_Value_SetBlock ] = "Value_SetBlock",
85+
[ Commands.c_Debugging_Value_SetArray ] = "Value_SetArray",
86+
[ Commands.c_Debugging_Value_AllocateObject ] = "Value_AllocateObject",
87+
[ Commands.c_Debugging_Value_AllocateString ] = "Value_AllocateString",
88+
[ Commands.c_Debugging_Value_AllocateArray ] = "Value_AllocateArray",
89+
[ Commands.c_Debugging_Value_Assign ] = "Value_Assign",
90+
[ Commands.c_Debugging_TypeSys_Assemblies ] = "TypeSys_Assemblies",
91+
[ Commands.c_Debugging_TypeSys_AppDomains ] = "TypeSys_AppDomains",
92+
[ Commands.c_Debugging_Resolve_Assembly ] = "Resolve_Assembly",
93+
[ Commands.c_Debugging_Resolve_Type ] = "Resolve_Type",
94+
[ Commands.c_Debugging_Resolve_Field ] = "Resolve_Field",
95+
[ Commands.c_Debugging_Resolve_Method ] = "Resolve_Method",
96+
[ Commands.c_Debugging_Resolve_VirtualMethod ] = "Resolve_VirtualMethod",
97+
[ Commands.c_Debugging_Resolve_AppDomain ] = "Resolve_AppDomain",
98+
[ Commands.c_Debugging_MFUpdate_Start ] = "MFUpdate_Start",
99+
[ Commands.c_Debugging_MFUpdate_AddPacket ] = "MFUpdate_AddPacket",
100+
[ Commands.c_Debugging_MFUpdate_Install ] = "MFUpdate_Install",
101+
[ Commands.c_Debugging_MFUpdate_AuthCmd ] = "MFUpdate_AuthCmd",
102+
[ Commands.c_Debugging_MFUpdate_Authenticate ] = "MFUpdate_Authenticate",
103+
[ Commands.c_Debugging_MFUpdate_GetMissingPkts ] = "MFUpdate_GetMissingPkts",
104+
[ Commands.c_Debugging_UpgradeToSsl ] = "UpgradeToSsl",
105+
[ Commands.c_Debugging_Lcd_NewFrame ] = "Lcd_NewFrame",
106+
[ Commands.c_Debugging_Lcd_NewFrameData ] = "Lcd_NewFrameData",
107+
[ Commands.c_Debugging_Lcd_GetFrame ] = "Lcd_GetFrame",
108+
[ Commands.c_Debugging_Button_Report ] = "Button_Report",
109+
[ Commands.c_Debugging_Button_Inject ] = "Button_Inject",
110+
[ Commands.c_Debugging_Messaging_Query ] = "Messaging_Query",
111+
[ Commands.c_Debugging_Messaging_Send ] = "Messaging_Send",
112+
[ Commands.c_Debugging_Messaging_Reply ] = "Messaging_Reply",
113+
[ Commands.c_Debugging_Logging_GetNumberOfRecords ] = "Logging_GetNumberOfRecords",
114+
[ Commands.c_Debugging_Logging_GetRecord ] = "Logging_GetRecord",
115+
[ Commands.c_Debugging_Logging_Erase ] = "Logging_Erase",
116+
[ Commands.c_Debugging_Logging_GetRecords ] = "Logging_GetRecords",
117+
[ Commands.c_Debugging_Deployment_Status ] = "Deployment_Status",
118+
[ Commands.c_Debugging_Info_SetJMC ] = "Info_SetJMC",
119+
[ Commands.c_Profiling_Command ] = "Profiling_Command",
120+
[ Commands.c_Profiling_Stream ] = "Profiling_Stream"
121+
};
122+
123+
string GetCommandName( uint cmd )
124+
{
125+
string retVal;
126+
if( !CommandNameMap.TryGetValue( cmd, out retVal ) )
127+
retVal = $"0x{cmd:X08}";
128+
129+
return retVal;
130+
}
131+
#endif
132+
133+
[Event( 1, Opcode = EventOpcode.Send )]
134+
public void WireProtocolTxHeader( uint crcHeader, uint crcData, uint cmd, uint flags, ushort seq, ushort seqReply, uint length )
135+
{
136+
#if TRACE
137+
Trace.TraceInformation( "TX: {0} flags=[{1}] hCRC: 0x{2:X08} pCRC: 0x{3:X08} seq: 0x{4:X04} replySeq: 0x{5:X04} len={6}"
138+
, GetCommandName( cmd )
139+
, ( PacketFlags )flags
140+
, crcHeader
141+
, crcData
142+
, seq
143+
, seqReply
144+
, length
145+
);
146+
#endif
147+
WriteCustomEvent( 1, crcHeader, crcData, cmd, flags, seq, seqReply, length );
20148
}
21149

22150
[Event( 2, Opcode = EventOpcode.Receive )]
23-
public void WireProtocolRxHeader( uint cmd, uint flags, ushort seq, ushort seqReply )
151+
public void WireProtocolRxHeader( uint crcHeader, uint crcData, uint cmd, uint flags, ushort seq, ushort seqReply, uint length )
24152
{
25-
Trace.TraceInformation( "RX: {0:X08} {1:X08} {2:X04} {3:X04}", cmd, flags, seq, seqReply );
26-
WriteCustomEvent( 2, cmd, flags, seq, seqReply );
153+
#if TRACE
154+
Trace.TraceInformation( "RX: {0} flags=[{1}] hCRC: 0x{2:X08} pCRC: 0x{3:X08} seq: 0x{4:X04} replySeq: 0x{5:X04} len={6}"
155+
, GetCommandName( cmd )
156+
, ( PacketFlags )flags
157+
, crcHeader
158+
, crcData
159+
, seq
160+
, seqReply
161+
, length
162+
);
163+
#endif
164+
WriteCustomEvent( 2, crcHeader, crcData, cmd, flags, seq, seqReply, length );
27165
}
28166

29167
[Event( 3 )]
@@ -35,27 +173,47 @@ public void WireProtocolReceiveState( MessageReassembler.ReceiveState state )
35173
[Event(4)]
36174
public void EngineEraseMemory( uint address, uint length )
37175
{
38-
Trace.TraceInformation( "EreaseMemory: @{0:X08}; LEN={1:X08}", address, length );
176+
Trace.TraceInformation( "EraseMemory: @0x{0:X08}; LEN=0x{1:X08}", address, length );
39177
WriteEvent( 4, ( int )address, ( int )length );
40178
}
41179

180+
[Event(5)]
181+
public void EngineWriteMemory( uint address, int length )
182+
{
183+
Trace.TraceInformation( "WriteMemory: @0x{0:X08}; LEN=0x{1:X08}", address, length );
184+
WriteEvent( 5, ( int )address, length );
185+
}
186+
42187
private DebuggerEventSource()
43188
{
44189
}
45190

46191
[NonEvent]
47-
unsafe void WriteCustomEvent(int eventId, uint cmd, uint flags, ushort seq, ushort seqReply )
48-
{
49-
EventData* pDataDesc = stackalloc EventData[ 4 ];
50-
pDataDesc[ 0 ].DataPointer = (IntPtr)( &cmd );
51-
pDataDesc[ 0 ].Size = sizeof( int );
52-
pDataDesc[ 1 ].DataPointer = ( IntPtr )( &flags );
53-
pDataDesc[ 1 ].Size = sizeof( int );
54-
pDataDesc[ 2 ].DataPointer = ( IntPtr )( &seq );
55-
pDataDesc[ 2 ].Size = sizeof( ushort );
56-
pDataDesc[ 3 ].DataPointer = ( IntPtr )( &seqReply );
57-
pDataDesc[ 3 ].Size = sizeof( ushort );
58-
WriteEventCore( eventId, 4, pDataDesc );
192+
unsafe void WriteCustomEvent( int eventId, uint crcHeader, uint crcData, uint cmd, uint flags, ushort seq, ushort seqReply, uint length )
193+
{
194+
EventData* pDataDesc = stackalloc EventData[ 7 ];
195+
pDataDesc[ 0 ].DataPointer = ( IntPtr )( &crcHeader );
196+
pDataDesc[ 0 ].Size = sizeof( uint );
197+
198+
pDataDesc[ 1 ].DataPointer = ( IntPtr )( &crcData );
199+
pDataDesc[ 1 ].Size = sizeof( uint );
200+
201+
pDataDesc[ 2 ].DataPointer = (IntPtr)( &cmd );
202+
pDataDesc[ 2 ].Size = sizeof( uint );
203+
204+
pDataDesc[ 3 ].DataPointer = ( IntPtr )( &flags );
205+
pDataDesc[ 3 ].Size = sizeof( uint );
206+
207+
pDataDesc[ 4 ].DataPointer = ( IntPtr )( &seq );
208+
pDataDesc[ 4 ].Size = sizeof( ushort );
209+
210+
pDataDesc[ 5 ].DataPointer = ( IntPtr )( &seqReply );
211+
pDataDesc[ 5 ].Size = sizeof( ushort );
212+
213+
pDataDesc[ 6 ].DataPointer = ( IntPtr )( &length );
214+
pDataDesc[ 6 ].Size = sizeof( uint );
215+
216+
WriteEventCore( eventId, 7, pDataDesc );
59217
}
60218
}
61219
}

Framework/Debugger/WireProtocol/Engine.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,6 +1659,7 @@ public bool WriteMemory( uint address, byte[ ] buf, int offset, int length )
16591659

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

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

16641665
if( !IncomingMessage.IsPositiveAcknowledge( reply ) )

0 commit comments

Comments
 (0)