forked from madrang/MFDebugger-Mono
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSRecord.cs
222 lines (176 loc) · 8.41 KB
/
SRecord.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) Microsoft Corporation. All rights reserved.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;
using System.Threading;
using System.Runtime.Serialization;
namespace Microsoft.SPOT.Debugger
{
public class SRecordFile
{
public class Block
{
public uint address;
public MemoryStream data;
public byte[] signature;
public bool executable;
}
static public uint Parse( string file, ArrayList blocks, string signatureFile )
{
uint entrypoint = 0;
if(System.IO.File.Exists( file ) == false)
{
throw new System.IO.FileNotFoundException( String.Format( "Cannot find {0}", file ) );
}
using(System.IO.StreamReader reader = new StreamReader( file ))
{
string line;
int lineNum = 0;
while((line = reader.ReadLine()) != null)
{
char[] lineBytes = line.ToCharArray();
int len = lineBytes.Length;
int i;
lineNum++; if(len == 0) continue;
// we only accept S0, S3 and S7 records (header, memory loadable data, execution address)
if(
(Char.ToLower( lineBytes[0] ) != 's') ||
(lineBytes[1] != '0' && lineBytes[1] != '3' && lineBytes[1] != '7')
)
{
throw new System.ArgumentException( String.Format( "Unknown format at line {0} of {1}:\n {2}", lineNum, file, line ) );
}
// we discard S0 records
if( (Char.ToLower( lineBytes[0] ) == 's') && (lineBytes[1] == '0') )
{
continue;
}
int num = Byte.Parse( new string( lineBytes, 2, 2 ), System.Globalization.NumberStyles.HexNumber );
if(num != ((len / 2) - 2))
{
throw new System.ArgumentException( String.Format( "Incorrect length at line {0} of {1}: {2}", lineNum, file, num ) );
}
byte crc = (byte)num;
for(i = 4; i<len - 2; i += 2)
{
crc += Byte.Parse( new string( lineBytes, i, 2 ), System.Globalization.NumberStyles.HexNumber );
}
byte checksum = Byte.Parse( new string( lineBytes, len - 2, 2 ), System.Globalization.NumberStyles.HexNumber );
if((checksum ^ crc) != 0xFF)
{
throw new System.ArgumentException( String.Format( "Incorrect crc at line {0} of {1}: got {2:X2}, expected {3:X2}", lineNum, file, crc, checksum ) );
}
num -= 5;
uint address = UInt32.Parse( new string( lineBytes, 4, 8 ), System.Globalization.NumberStyles.HexNumber );
if(lineBytes[1] == '7')
{
entrypoint = address;
for(i=0; i<blocks.Count; i++)
{
Block bl = (Block)blocks[i];
if(bl.address == entrypoint)
{
bl.executable = true;
}
}
break;
}
else
{
Block bl = new Block();
bl.address = address;
bl.data = new MemoryStream();
bl.executable = false;
for(i=0; i<num; i++)
{
bl.data.WriteByte( Byte.Parse( new string( lineBytes, 12 + i * 2, 2 ), System.Globalization.NumberStyles.HexNumber ) );
}
for(i=0; i<blocks.Count; i++)
{
Block bl2 = (Block)blocks[i];
int num2 = (int)bl2.data.Length;
if(bl2.address + num2 == bl.address)
{
byte[] data = bl.data.ToArray();
bl2.data.Write( data, 0, data.Length );
bl = null;
break;
}
if(bl.address + num == bl2.address)
{
byte[] data = bl2.data.ToArray();
bl.data.Write( data, 0, data.Length );
bl2.address = bl.address;
bl2.data = bl.data;
bl = null;
break;
}
if(bl.address < bl2.address)
{
blocks.Insert( i, bl );
bl = null;
break;
}
}
if(bl != null)
{
if(signatureFile != null)
{
using(System.IO.FileStream fs = new FileStream( signatureFile, System.IO.FileMode.Open, System.IO.FileAccess.Read ))
{
if(fs.Length != 128)
{
throw new ArgumentOutOfRangeException( String.Format( "Signature is not 128 bytes long; it is {0} bytes long", fs.Length ) );
}
byte[] signature = new byte[128];
if(128 != fs.Read( signature, 0, 128 ))
{
throw new ArgumentOutOfRangeException( "Could not read 128 bytes from signature file" );
}
bl.signature = signature;
}
}
else
{
bl.signature = new byte[0];
}
blocks.Add( bl );
}
}
}
}
return entrypoint;
}
static public void Encode( Stream stream, byte[] buf, uint address )
{
StreamWriter writer = new StreamWriter( stream, Encoding.ASCII );
uint len = (uint)buf.Length;
int offset = 0;
while(len > 0)
{
uint size = len > 16 ? 16 : len;
byte crc = (byte)(size + 5);
writer.Write( "S3{0:X2}{1:X8}", size + 5, address );
crc += (byte)(address >> 0);
crc += (byte)(address >> 8);
crc += (byte)(address >> 16);
crc += (byte)(address >> 24);
for(uint i=0; i<size; i++)
{
byte v = buf[offset++];
writer.Write( "{0:X2}", v );
crc += v;
}
address += size;
len -= size;
writer.WriteLine( "{0:X2}", (byte)~crc );
}
writer.Flush();
}
}
}