Browse code

* Fixes for gcc and clang (while they can compile the code, the DLLs made aren't functional, but oh well).

* [2SF] Used more up-to-date asmjit, despite the ugly looking code.

Naram Qashat authored on 2014/09/17 19:51:45
Showing 1 changed files
... ...
@@ -152,7 +152,7 @@ uint16_t CFIRMWARE::getBootCodeCRC16()
152 152
 uint32_t CFIRMWARE::decrypt(const uint8_t *in, std::unique_ptr<uint8_t[]> &out)
153 153
 {
154 154
 	uint32_t curBlock[2] = { 0 };
155
-	
155
+
156 156
 	uint32_t xIn = 4, xOut = 0;
157 157
 
158 158
 	memcpy(curBlock, in, 8);
Browse code

Updating in_2sf to use a newish version of DeSmuME, 0.9.9 from SVN. Somewhat cleaned up as well, but not everything because it's a pain in the ass.

Naram Qashat authored on 2013/04/18 17:22:55
Showing 1 changed files
... ...
@@ -17,18 +17,17 @@
17 17
 
18 18
 #include "firmware.h"
19 19
 #include "NDSSystem.h"
20
-#include "path.h"
21 20
 
22
-//#define DWNUM(i) ((i) >> 2)
23 21
 static inline uint32_t DWNUM(uint32_t i) { return i >> 2; }
24 22
 
25 23
 bool CFIRMWARE::getKeyBuf()
26 24
 {
27 25
 	FILE *file = fopen(CommonSettings.ARM7BIOS, "rb");
28
-	if (!file) return false;
26
+	if (!file)
27
+		return false;
29 28
 
30 29
 	fseek(file, 0x30, SEEK_SET);
31
-	size_t res = fread(keyBuf, 4, 0x412, file);
30
+	size_t res = fread(this->keyBuf, 4, 0x412, file);
32 31
 	fclose(file);
33 32
 	return res == 0x412;
34 33
 }
... ...
@@ -38,19 +37,19 @@ void CFIRMWARE::crypt64BitUp(uint32_t *ptr)
38 37
 	uint32_t Y = ptr[0];
39 38
 	uint32_t X = ptr[1];
40 39
 
41
-	for(uint32_t i = 0x00; i <= 0x0F; i++)
40
+	for (uint32_t i = 0x00; i <= 0x0F; ++i)
42 41
 	{
43
-		uint32_t Z = (keyBuf[i] ^ X);
44
-		X = keyBuf[DWNUM(0x048 + (((Z >> 24) & 0xFF) << 2))];
45
-		X = (keyBuf[DWNUM(0x448 + (((Z >> 16) & 0xFF) << 2))] + X);
46
-		X = (keyBuf[DWNUM(0x848 + (((Z >> 8) & 0xFF) << 2))] ^ X);
47
-		X = (keyBuf[DWNUM(0xC48 + ((Z & 0xFF) << 2))] + X);
48
-		X = (Y ^ X);
42
+		uint32_t Z = this->keyBuf[i] ^ X;
43
+		X = this->keyBuf[DWNUM(0x048 + (((Z >> 24) & 0xFF) << 2))];
44
+		X = this->keyBuf[DWNUM(0x448 + (((Z >> 16) & 0xFF) << 2))] + X;
45
+		X = this->keyBuf[DWNUM(0x848 + (((Z >> 8) & 0xFF) << 2))] ^ X;
46
+		X = this->keyBuf[DWNUM(0xC48 + ((Z & 0xFF) << 2))] + X;
47
+		X = Y ^ X;
49 48
 		Y = Z;
50 49
 	}
51 50
 
52
-	ptr[0] = (X ^ keyBuf[DWNUM(0x40)]);
53
-	ptr[1] = (Y ^ keyBuf[DWNUM(0x44)]);
51
+	ptr[0] = X ^ this->keyBuf[DWNUM(0x40)];
52
+	ptr[1] = Y ^ this->keyBuf[DWNUM(0x44)];
54 53
 }
55 54
 
56 55
 void CFIRMWARE::crypt64BitDown(uint32_t *ptr)
... ...
@@ -58,361 +57,327 @@ void CFIRMWARE::crypt64BitDown(uint32_t *ptr)
58 57
 	uint32_t Y = ptr[0];
59 58
 	uint32_t X = ptr[1];
60 59
 
61
-	for(uint32_t i = 0x11; i >= 0x02; i--)
60
+	for (uint32_t i = 0x11; i >= 0x02; --i)
62 61
 	{
63
-		uint32_t Z = (keyBuf[i] ^ X);
64
-		X = keyBuf[DWNUM(0x048 + (((Z >> 24) & 0xFF) << 2))];
65
-		X = (keyBuf[DWNUM(0x448 + (((Z >> 16) & 0xFF) << 2))] + X);
66
-		X = (keyBuf[DWNUM(0x848 + (((Z >> 8)  & 0xFF) << 2))] ^ X);
67
-		X = (keyBuf[DWNUM(0xC48 + ((Z & 0xFF) << 2))] + X);
68
-		X = (Y ^ X);
62
+		uint32_t Z = this->keyBuf[i] ^ X;
63
+		X = this->keyBuf[DWNUM(0x048 + (((Z >> 24) & 0xFF) << 2))];
64
+		X = this->keyBuf[DWNUM(0x448 + (((Z >> 16) & 0xFF) << 2))] + X;
65
+		X = this->keyBuf[DWNUM(0x848 + (((Z >> 8) & 0xFF) << 2))] ^ X;
66
+		X = this->keyBuf[DWNUM(0xC48 + ((Z & 0xFF) << 2))] + X;
67
+		X = Y ^ X;
69 68
 		Y = Z;
70 69
 	}
71 70
 
72
-	ptr[0] = (X ^ keyBuf[DWNUM(0x04)]);
73
-	ptr[1] = (Y ^ keyBuf[DWNUM(0x00)]);
71
+	ptr[0] = X ^ this->keyBuf[DWNUM(0x04)];
72
+	ptr[1] = Y ^ this->keyBuf[DWNUM(0x00)];
74 73
 }
75 74
 
76
-//#define bswap32(val) (((val & 0x000000FF) << 24) | ((val & 0x0000FF00) << 8) | ((val & 0x00FF0000) >> 8) | ((val & 0xFF000000) >> 24))
77 75
 static inline uint32_t bswap32(uint32_t val) { return ((val & 0x000000FF) << 24) | ((val & 0x0000FF00) << 8) | ((val & 0x00FF0000) >> 8) | ((val & 0xFF000000) >> 24); }
78 76
 void CFIRMWARE::applyKeycode(uint32_t modulo)
79 77
 {
80
-	crypt64BitUp(&keyCode[1]);
81
-	crypt64BitUp(&keyCode[0]);
78
+	this->crypt64BitUp(&this->keyCode[1]);
79
+	this->crypt64BitUp(&this->keyCode[0]);
82 80
 
83
-	uint32_t scratch[2] = {0x00000000, 0x00000000};
81
+	uint32_t scratch[] = { 0x00000000, 0x00000000 };
84 82
 
85
-	for(uint32_t i = 0; i <= 0x44; i += 4)
86
-	{
87
-		keyBuf[DWNUM(i)] = (keyBuf[DWNUM(i)] ^ bswap32(keyCode[DWNUM(i % modulo)]));
88
-	}
83
+	for (uint32_t i = 0; i <= 0x44; i += 4)
84
+		this->keyBuf[DWNUM(i)] = this->keyBuf[DWNUM(i)] ^ bswap32(this->keyCode[DWNUM(i % modulo)]);
89 85
 
90
-	for(uint32_t i = 0; i <= 0x1040; i += 8)
86
+	for (uint32_t i = 0; i <= 0x1040; i += 8)
91 87
 	{
92
-		crypt64BitUp(scratch);
93
-		keyBuf[DWNUM(i)] = scratch[1];
94
-		keyBuf[DWNUM(i+4)] = scratch[0];
88
+		this->crypt64BitUp(scratch);
89
+		this->keyBuf[DWNUM(i)] = scratch[1];
90
+		this->keyBuf[DWNUM(i + 4)] = scratch[0];
95 91
 	}
96 92
 }
97
-//#undef bswap32
98 93
 
99 94
 bool CFIRMWARE::initKeycode(uint32_t idCode, int level, uint32_t modulo)
100 95
 {
101
-	if(getKeyBuf() == false)
96
+	if (!this->getKeyBuf())
102 97
 		return false;
103 98
 
104
-	keyCode[0] = idCode;
105
-	keyCode[1] = (idCode >> 1);
106
-	keyCode[2] = (idCode << 1);
99
+	this->keyCode[0] = idCode;
100
+	this->keyCode[1] = idCode >> 1;
101
+	this->keyCode[2] = idCode << 1;
107 102
 
108
-	if(level >= 1) applyKeycode(modulo);
109
-	if(level >= 2) applyKeycode(modulo);
103
+	if (level >= 1)
104
+		this->applyKeycode(modulo);
105
+	if (level >= 2)
106
+		this->applyKeycode(modulo);
110 107
 
111
-	keyCode[1] <<= 1;
112
-	keyCode[2] >>= 1;
108
+	this->keyCode[1] <<= 1;
109
+	this->keyCode[2] >>= 1;
113 110
 
114
-	if(level >= 3) applyKeycode(modulo);
111
+	if (level >= 3)
112
+		this->applyKeycode(modulo);
115 113
 
116 114
 	return true;
117 115
 }
118 116
 
119 117
 uint16_t CFIRMWARE::getBootCodeCRC16()
120 118
 {
121
-	unsigned int i, j;
122 119
 	uint32_t crc = 0xFFFF;
123
-	const uint16_t val[8] = {0xC0C1, 0xC181, 0xC301, 0xC601, 0xCC01, 0xD801, 0xF001, 0xA001};
120
+	const uint16_t val[] = { 0xC0C1, 0xC181, 0xC301, 0xC601, 0xCC01, 0xD801, 0xF001, 0xA001 };
124 121
 
125
-	for(i = 0; i < size9; i++)
122
+	unsigned i, j;
123
+	for (i = 0; i < this->size9; ++i)
126 124
 	{
127
-		crc = (crc ^ tmp_data9[i]);
125
+		crc ^= this->tmp_data9[i];
128 126
 
129
-		for(j = 0; j < 8; j++)
127
+		for (j = 0; j < 8; ++j)
130 128
 		{
131
-			if(crc & 0x0001)
132
-				crc = ((crc >> 1) ^ (val[j] << (7-j)));
129
+			if (crc & 0x0001)
130
+				crc = (crc >> 1) ^ (val[j] << (7 - j));
133 131
 			else
134
-				crc =  (crc >> 1);
132
+				crc >>= 1;
135 133
 		}
136 134
 	}
137 135
 
138
-	for(i = 0; i < size7; i++)
136
+	for (i = 0; i < this->size7; ++i)
139 137
 	{
140
-		crc = (crc ^ tmp_data7[i]);
138
+		crc ^= this->tmp_data7[i];
141 139
 
142
-		for(j = 0; j < 8; j++)
140
+		for (j = 0; j < 8; ++j)
143 141
 		{
144
-			if(crc & 0x0001)
145
-				crc = ((crc >> 1) ^ (val[j] << (7-j)));
142
+			if (crc & 0x0001)
143
+				crc = (crc >> 1) ^ (val[j] << (7 - j));
146 144
 			else
147
-				crc =  (crc >> 1);
145
+				crc >>= 1;
148 146
 		}
149 147
 	}
150 148
 
151 149
 	return crc & 0xFFFF;
152 150
 }
153 151
 
154
-uint32_t CFIRMWARE::decrypt(const uint8_t *in, uint8_t* &out)
152
+uint32_t CFIRMWARE::decrypt(const uint8_t *in, std::unique_ptr<uint8_t[]> &out)
155 153
 {
156 154
 	uint32_t curBlock[2] = { 0 };
157
-	uint32_t blockSize = 0;
158
-	uint32_t xLen = 0;
159
-
160
-	uint32_t i = 0, j = 0;
155
+	
161 156
 	uint32_t xIn = 4, xOut = 0;
162
-	uint32_t len = 0;
163
-	uint32_t offset = 0;
164
-	uint32_t windowOffset = 0;
165
-	uint8_t d = 0;
166
-
167
-	uint16_t data = 0;
168 157
 
169 158
 	memcpy(curBlock, in, 8);
170
-	crypt64BitDown(curBlock);
171
-	blockSize = (curBlock[0] >> 8);
159
+	this->crypt64BitDown(curBlock);
160
+	uint32_t blockSize = curBlock[0] >> 8;
172 161
 
173
-	if (blockSize == 0) return 0;
162
+	if (!blockSize)
163
+		return 0;
174 164
 
175
-	out = new uint8_t [blockSize];
176
-	if (!out ) return 0;
177
-	memset(out, 0xFF, blockSize);
165
+	out.reset(new uint8_t[blockSize]);
166
+	if (!out)
167
+		return 0;
168
+	memset(&out[0], 0xFF, blockSize);
178 169
 
179
-	xLen = blockSize;
180
-	while(xLen > 0)
170
+	uint32_t xLen = blockSize;
171
+	while (xLen > 0)
181 172
 	{
182
-		d = T1ReadByte((uint8_t*)curBlock, (xIn % 8));
183
-		xIn++;
184
-		if((xIn % 8) == 0)
173
+		uint8_t d = T1ReadByte(reinterpret_cast<uint8_t *>(curBlock), xIn % 8);
174
+		++xIn;
175
+		if (!(xIn % 8))
185 176
 		{
186 177
 			memcpy(curBlock, in + xIn, 8);
187
-			crypt64BitDown(curBlock);
178
+			this->crypt64BitDown(curBlock);
188 179
 		}
189 180
 
190
-		for(i = 0; i < 8; i++)
181
+		for (uint32_t i = 0; i < 8; ++i)
191 182
 		{
192
-			if(d & 0x80)
183
+			if (d & 0x80)
193 184
 			{
194
-				data = (T1ReadByte((uint8_t*)curBlock, (xIn % 8)) << 8);
195
-				xIn++;
196
-				if((xIn % 8) == 0)
185
+				uint16_t data = T1ReadByte(reinterpret_cast<uint8_t *>(curBlock), xIn % 8) << 8;
186
+				++xIn;
187
+				if (!(xIn % 8))
197 188
 				{
198 189
 					memcpy(curBlock, in + xIn, 8);
199
-					crypt64BitDown(curBlock);
190
+					this->crypt64BitDown(curBlock);
200 191
 				}
201
-				data |= T1ReadByte((uint8_t*)curBlock, (xIn % 8));
202
-				xIn++;
203
-				if((xIn % 8) == 0)
192
+				data |= T1ReadByte(reinterpret_cast<uint8_t *>(curBlock), xIn % 8);
193
+				++xIn;
194
+				if (!(xIn % 8))
204 195
 				{
205 196
 					memcpy(curBlock, in + xIn, 8);
206
-					crypt64BitDown(curBlock);
197
+					this->crypt64BitDown(curBlock);
207 198
 				}
208 199
 
209
-				len = (data >> 12) + 3;
210
-				offset = (data & 0xFFF);
211
-				windowOffset = (xOut - offset - 1);
200
+				uint32_t len = (data >> 12) + 3;
201
+				uint32_t offset = data & 0xFFF;
202
+				uint32_t windowOffset = xOut - offset - 1;
212 203
 
213
-				for(j = 0; j < len; j++)
204
+				for (uint32_t j = 0; j < len; ++j)
214 205
 				{
215
-					T1WriteByte(out, xOut, T1ReadByte(out, windowOffset));
216
-					xOut++;
217
-					windowOffset++;
206
+					T1WriteByte(&out[0], xOut, T1ReadByte(&out[0], windowOffset));
207
+					++xOut;
208
+					++windowOffset;
218 209
 
219
-					xLen--;
220
-					if(xLen == 0) return blockSize;
210
+					--xLen;
211
+					if (!xLen)
212
+						return blockSize;
221 213
 				}
222 214
 			}
223 215
 			else
224 216
 			{
225
-				T1WriteByte(out, xOut, T1ReadByte((uint8_t*)curBlock, (xIn % 8)));
226
-				xOut++;
227
-				xIn++;
228
-				if((xIn % 8) == 0)
217
+				T1WriteByte(&out[0], xOut, T1ReadByte(reinterpret_cast<uint8_t *>(curBlock), xIn % 8));
218
+				++xOut;
219
+				++xIn;
220
+				if (!(xIn % 8))
229 221
 				{
230 222
 					memcpy(curBlock, in + xIn, 8);
231
-					crypt64BitDown(curBlock);
223
+					this->crypt64BitDown(curBlock);
232 224
 				}
233 225
 
234
-				xLen--;
235
-				if(xLen == 0) return blockSize;
226
+				--xLen;
227
+				if (!xLen)
228
+					return blockSize;
236 229
 			}
237 230
 
238
-			d = ((d << 1) & 0xFF);
231
+			d = (d << 1) & 0xFF;
239 232
 		}
240 233
 	}
241 234
 
242 235
 	return blockSize;
243 236
 }
244 237
 
245
-uint32_t CFIRMWARE::decompress(const uint8_t *in, uint8_t* &out)
238
+uint32_t CFIRMWARE::decompress(const uint8_t *in, std::unique_ptr<uint8_t[]> &out)
246 239
 {
247 240
 	uint32_t curBlock[2] = { 0 };
248
-	uint32_t blockSize = 0;
249
-	uint32_t xLen = 0;
250 241
 
251
-	uint32_t i = 0, j = 0;
252 242
 	uint32_t xIn = 4, xOut = 0;
253
-	uint32_t len = 0;
254
-	uint32_t offset = 0;
255
-	uint32_t windowOffset = 0;
256
-	uint8_t d = 0;
257
-	uint16_t data = 0;
258 243
 
259 244
 	memcpy(curBlock, in, 8);
260
-	blockSize = (curBlock[0] >> 8);
245
+	uint32_t blockSize = curBlock[0] >> 8;
261 246
 
262
-	if (blockSize == 0) return 0;
247
+	if (!blockSize)
248
+		return 0;
263 249
 
264
-	out = new uint8_t [blockSize];
265
-	if (!out ) return 0;
266
-	memset(out, 0xFF, blockSize);
250
+	out.reset(new uint8_t[blockSize]);
251
+	if (!out)
252
+		return 0;
253
+	memset(&out[0], 0xFF, blockSize);
267 254
 
268
-	xLen = blockSize;
269
-	while(xLen > 0)
255
+	uint32_t xLen = blockSize;
256
+	while (xLen > 0)
270 257
 	{
271
-		d = T1ReadByte((uint8_t*)curBlock, (xIn % 8));
272
-		xIn++;
273
-		if((xIn % 8) == 0)
274
-		{
258
+		uint8_t d = T1ReadByte(reinterpret_cast<uint8_t *>(curBlock), xIn % 8);
259
+		++xIn;
260
+		if (!(xIn % 8))
275 261
 			memcpy(curBlock, in + xIn, 8);
276
-		}
277 262
 
278
-		for(i = 0; i < 8; i++)
263
+		for (uint32_t i = 0; i < 8; ++i)
279 264
 		{
280
-			if(d & 0x80)
265
+			if (d & 0x80)
281 266
 			{
282
-				data = (T1ReadByte((uint8_t*)curBlock, (xIn % 8)) << 8);
283
-				xIn++;
284
-				if((xIn % 8) == 0)
285
-				{
267
+				uint16_t data = T1ReadByte(reinterpret_cast<uint8_t *>(curBlock), xIn % 8) << 8;
268
+				++xIn;
269
+				if (!(xIn % 8))
286 270
 					memcpy(curBlock, in + xIn, 8);
287
-				}
288
-				data |= T1ReadByte((uint8_t*)curBlock, (xIn % 8));
289
-				xIn++;
290
-				if((xIn % 8) == 0)
291
-				{
271
+				data |= T1ReadByte(reinterpret_cast<uint8_t *>(curBlock), xIn % 8);
272
+				++xIn;
273
+				if (!(xIn % 8))
292 274
 					memcpy(curBlock, in + xIn, 8);
293
-				}
294 275
 
295
-				len = (data >> 12) + 3;
296
-				offset = (data & 0xFFF);
297
-				windowOffset = (xOut - offset - 1);
276
+				uint32_t len = (data >> 12) + 3;
277
+				uint32_t offset = data & 0xFFF;
278
+				uint32_t windowOffset = xOut - offset - 1;
298 279
 
299
-				for(j = 0; j < len; j++)
280
+				for (uint32_t j = 0; j < len; ++j)
300 281
 				{
301
-					T1WriteByte(out, xOut, T1ReadByte(out, windowOffset));
302
-					xOut++;
303
-					windowOffset++;
282
+					T1WriteByte(&out[0], xOut, T1ReadByte(&out[0], windowOffset));
283
+					++xOut;
284
+					++windowOffset;
304 285
 
305
-					xLen--;
306
-					if(xLen == 0) return blockSize;
286
+					--xLen;
287
+					if (!xLen)
288
+						return blockSize;
307 289
 				}
308 290
 			}
309 291
 			else
310 292
 			{
311
-				T1WriteByte(out, xOut, T1ReadByte((uint8_t*)curBlock, (xIn % 8)));
312
-				xOut++;
313
-				xIn++;
314
-				if((xIn % 8) == 0)
315
-				{
293
+				T1WriteByte(&out[0], xOut, T1ReadByte(reinterpret_cast<uint8_t *>(curBlock), xIn % 8));
294
+				++xOut;
295
+				++xIn;
296
+				if (!(xIn % 8))
316 297
 					memcpy(curBlock, in + xIn, 8);
317
-				}
318 298
 
319
-				xLen--;
320
-				if(xLen == 0) return blockSize;
299
+				--xLen;
300
+				if (!xLen)
301
+					return blockSize;
321 302
 			}
322 303
 
323
-			d = ((d << 1) & 0xFF);
304
+			d = (d << 1) & 0xFF;
324 305
 		}
325 306
 	}
326 307
 
327 308
 	return blockSize;
328 309
 }
329
-//================================================================================
310
+
311
+// ================================================================================
330 312
 bool CFIRMWARE::load()
331 313
 {
332
-	uint32_t size = 0;
333
-	uint8_t	*data = NULL;
334
-	uint16_t shift1 = 0, shift2 = 0, shift3 = 0, shift4 = 0;
335
-	uint32_t part1addr = 0, part2addr = 0/*, part3addr = 0, part4addr = 0, part5addr = 0*/;
336
-	uint32_t part1ram = 0, part2ram = 0;
337
-
338
-	uint32_t	src = 0;
339
-
340
-	if (CommonSettings.UseExtFirmware == false)
314
+	if (!CommonSettings.UseExtFirmware)
341 315
 		return false;
342
-	if (strlen(CommonSettings.Firmware) == 0)
316
+	if (!strlen(CommonSettings.Firmware))
343 317
 		return false;
344 318
 
345
-	FILE	*fp = fopen(CommonSettings.Firmware, "rb");
319
+	FILE *fp = fopen(CommonSettings.Firmware, "rb");
346 320
 	if (!fp)
347 321
 		return false;
348 322
 	fseek(fp, 0, SEEK_END);
349
-	size = ftell(fp);
323
+	uint32_t size = ftell(fp);
350 324
 	fseek(fp, 0, SEEK_SET);
351
-	if( (size != 256*1024) && (size != 512*1024) )
325
+	if (size != 262144 && size != 524288)
352 326
 	{
353 327
 		fclose(fp);
354 328
 		return false;
355 329
 	}
356 330
 
357 331
 #if 1
358
-	if (size == 512*1024)
332
+	if (size == 524288)
359 333
 	{
360
-		//INFO("ERROR: 32Mbit (512Kb) firmware not supported\n");
361 334
 		fclose(fp);
362 335
 		return false;
363 336
 	}
364 337
 #endif
365 338
 
366
-	data = new uint8_t [size];
339
+	auto data = std::unique_ptr<uint8_t[]>(new uint8_t[size]);
367 340
 	if (!data)
368 341
 	{
369 342
 		fclose(fp);
370 343
 		return false;
371 344
 	}
372 345
 
373
-	if (fread(data, 1, size, fp) != size)
346
+	if (fread(&data[0], 1, size, fp) != size)
374 347
 	{
375
-		delete [] data;
376 348
 		fclose(fp);
377 349
 		return false;
378 350
 	}
379 351
 
380
-	memcpy(&header, data, sizeof(header));
381
-	if ((header.fw_identifier[0] != 'M') ||
382
-			(header.fw_identifier[1] != 'A') ||
383
-				(header.fw_identifier[2] != 'C'))
384
-				{
385
-					delete [] data;
386
-					fclose(fp);
387
-					return false;
388
-				}
352
+	memcpy(&header, &data[0], sizeof(header));
353
+	if (header.fw_identifier[0] != 'M' || header.fw_identifier[1] != 'A' || header.fw_identifier[2] != 'C')
354
+	{
355
+		fclose(fp);
356
+		return false;
357
+	}
389 358
 
390
-	shift1 = ((header.shift_amounts >> 0) & 0x07);
391
-	shift2 = ((header.shift_amounts >> 3) & 0x07);
392
-	shift3 = ((header.shift_amounts >> 6) & 0x07);
393
-	shift4 = ((header.shift_amounts >> 9) & 0x07);
359
+	uint16_t shift1 = header.shift_amounts & 0x07;
360
+	uint16_t shift2 = (header.shift_amounts >> 3) & 0x07;
361
+	uint16_t shift3 = (header.shift_amounts >> 6) & 0x07;
362
+	uint16_t shift4 = (header.shift_amounts >> 9) & 0x07;
394 363
 
395 364
 	// todo - add support for 512Kb
396
-	part1addr = (header.part1_rom_boot9_addr << (2 + shift1));
397
-	part1ram = (0x02800000 - (header.part1_ram_boot9_addr << (2+shift2)));
398
-	part2addr = (header.part2_rom_boot7_addr << (2+shift3));
399
-	part2ram = (0x03810000 - (header.part2_ram_boot7_addr << (2+shift4)));
400
-	//part3addr = (header.part3_rom_gui9_addr << 3);
401
-	//part4addr = (header.part4_rom_wifi7_addr << 3);
402
-	//part5addr = (header.part5_data_gfx_addr << 3);
403
-
404
-	ARM9bootAddr = part1ram;
405
-	ARM7bootAddr = part2ram;
406
-
407
-	if(initKeycode(T1ReadLong(data, 0x08), 1, 0xC) == false)
365
+	uint32_t part1addr = header.part1_rom_boot9_addr << (2 + shift1);
366
+	uint32_t part1ram = 0x02800000 - (header.part1_ram_boot9_addr << (2 + shift2));
367
+	uint32_t part2addr = header.part2_rom_boot7_addr << (2 + shift3);
368
+	uint32_t part2ram = 0x03810000 - (header.part2_ram_boot7_addr << (2 + shift4));
369
+
370
+	this->ARM9bootAddr = part1ram;
371
+	this->ARM7bootAddr = part2ram;
372
+
373
+	if (!this->initKeycode(T1ReadLong(&data[0], 0x08), 1, 0xC))
408 374
 	{
409
-		delete [] data;
410 375
 		fclose(fp);
411 376
 		return false;
412 377
 	}
413 378
 
414 379
 #if 0
415
-	crypt64BitDown((uint32_t*)&data[0x18]);
380
+	this->crypt64BitDown(reinterpret_cast<uint32_t *>(&data[0x18]));
416 381
 #else
417 382
 	// fix touch coords
418 383
 	data[0x18] = 0x00;
... ...
@@ -426,267 +391,181 @@ bool CFIRMWARE::load()
426 391
 	data[0x1F] = 0x00;
427 392
 #endif
428 393
 
429
-	if(initKeycode(T1ReadLong(data, 0x08), 2, 0xC) == false)
394
+	if (!this->initKeycode(T1ReadLong(&data[0], 0x08), 2, 0xC))
430 395
 	{
431
-		delete [] data;
432 396
 		fclose(fp);
433 397
 		return false;
434 398
 	}
435 399
 
436
-	size9 = decrypt(data + part1addr, tmp_data9);
437
-	if (!tmp_data9)
400
+	this->size9 = this->decrypt(&data[part1addr], this->tmp_data9);
401
+	if (!this->tmp_data9)
438 402
 	{
439
-		delete [] data;
440 403
 		fclose(fp);
441 404
 		return false;
442 405
 	}
443 406
 
444
-	size7 = decrypt(data + part2addr, tmp_data7);
445
-	if (!tmp_data7)
407
+	this->size7 = this->decrypt(&data[part2addr], this->tmp_data7);
408
+	if (!this->tmp_data7)
446 409
 	{
447
-		delete [] tmp_data9;
448
-		delete [] data;
410
+		this->tmp_data9.reset();
449 411
 		fclose(fp);
450 412
 		return false;
451 413
 	}
452 414
 
453
-	uint16_t crc16_mine = getBootCodeCRC16();
415
+	uint16_t crc16_mine = this->getBootCodeCRC16();
454 416
 
455 417
 	if (crc16_mine != header.part12_boot_crc16)
456 418
 	{
457
-		//INFO("Firmware: ERROR: the boot code CRC16 (0x%04X) doesn't match the value in the firmware header (0x%04X)", crc16_mine, header.part12_boot_crc16);
458
-		delete [] tmp_data9;
459
-		delete [] tmp_data7;
460
-		delete [] data;
419
+		this->tmp_data9.reset();
420
+		this->tmp_data7.reset();
461 421
 		fclose(fp);
462 422
 		return false;
463 423
 	}
464 424
 
465 425
 	// Copy firmware boot codes to their respective locations
466
-	src = 0;
467
-	for(uint32_t i = 0; i < (size9 >> 2); i++)
426
+	uint32_t src = 0;
427
+	for (uint32_t i = 0; i < (this->size9 >> 2); ++i)
468 428
 	{
469
-		_MMU_write32<ARMCPU_ARM9>(part1ram, T1ReadLong(tmp_data9, src));
470
-		src += 4; part1ram += 4;
429
+		_MMU_write32<ARMCPU_ARM9>(part1ram, T1ReadLong(&this->tmp_data9[0], src));
430
+		src += 4;
431
+		part1ram += 4;
471 432
 	}
472 433
 
473 434
 	src = 0;
474
-	for(uint32_t i = 0; i < (size7 >> 2); i++)
435
+	for (uint32_t i = 0; i < (this->size7 >> 2); ++i)
475 436
 	{
476
-		_MMU_write32<ARMCPU_ARM7>(part2ram, T1ReadLong(tmp_data7, src));
477
-		src += 4; part2ram += 4;
437
+		_MMU_write32<ARMCPU_ARM7>(part2ram, T1ReadLong(&this->tmp_data7[0], src));
438
+		src += 4;
439
+		part2ram += 4;
478 440
 	}
479
-	delete [] tmp_data7;
480
-	delete [] tmp_data9;
441
+	this->tmp_data7.reset();
442
+	this->tmp_data9.reset();
481 443
 
482
-	patched = false;
444
+	this->patched = false;
483 445
 	if (data[0x17C] != 0xFF)
484
-		patched = true;
485
-
486
-	//INFO("Firmware:\n");
487
-	//INFO("- path: %s\n", CommonSettings.Firmware);
488
-	//INFO("- size: %i bytes (%i Mbit)\n", size, size/1024/8);
489
-	//INFO("- CRC : 0x%04X\n", header.part12_boot_crc16);
490
-	//INFO("- header: \n");
491
-	//INFO("   * size firmware %i\n", ((header.shift_amounts >> 12) & 0xF) * 128 * 1024);
492
-	//INFO("   * ARM9 boot code address:     0x%08X\n", part1addr);
493
-	//INFO("   * ARM9 boot code RAM address: 0x%08X\n", ARM9bootAddr);
494
-	//INFO("   * ARM9 unpacked size:         0x%08X (%i) bytes\n", size9, size9);
495
-	//INFO("   * ARM9 GUI code address:      0x%08X\n", part3addr);
496
-	//INFO("\n");
497
-	//INFO("   * ARM7 boot code address:     0x%08X\n", part2addr);
498
-	//INFO("   * ARM7 boot code RAM address: 0x%08X\n", ARM7bootAddr);
499
-	//INFO("   * ARM7 WiFi code address:     0x%08X\n", part4addr);
500
-	//INFO("   * ARM7 unpacked size:         0x%08X (%i) bytes\n", size7, size7);
501
-	//INFO("\n");
502
-	//INFO("   * Data/GFX address:           0x%08X\n", part5addr);
503
-
504
-	if (patched)
446
+		this->patched = true;
447
+
448
+	if (this->patched)
505 449
 	{
506 450
 		uint32_t patch_offset = 0x3FC80;
507 451
 		if (data[0x17C] > 1)
508 452
 			patch_offset = 0x3F680;
509 453
 
510
-		memcpy(&header, data + patch_offset, sizeof(header));
454
+		memcpy(&header, &data[patch_offset], sizeof(header));
511 455
 
512
-		shift1 = ((header.shift_amounts >> 0) & 0x07);
513
-		shift2 = ((header.shift_amounts >> 3) & 0x07);
514
-		shift3 = ((header.shift_amounts >> 6) & 0x07);
515
-		shift4 = ((header.shift_amounts >> 9) & 0x07);
456
+		shift1 = header.shift_amounts & 0x07;
457
+		shift2 = (header.shift_amounts >> 3) & 0x07;
458
+		shift3 = (header.shift_amounts >> 6) & 0x07;
459
+		shift4 = (header.shift_amounts >> 9) & 0x07;
516 460
 
517 461
 		// todo - add support for 512Kb
518
-		part1addr = (header.part1_rom_boot9_addr << (2 + shift1));
519
-		part1ram = (0x02800000 - (header.part1_ram_boot9_addr << (2+shift2)));
520
-		part2addr = (header.part2_rom_boot7_addr << (2+shift3));
521
-		part2ram = (0x03810000 - (header.part2_ram_boot7_addr << (2+shift4)));
462
+		part1addr = header.part1_rom_boot9_addr << (2 + shift1);
463
+		part1ram = 0x02800000 - (header.part1_ram_boot9_addr << (2 + shift2));
464
+		part2addr = header.part2_rom_boot7_addr << (2 + shift3);
465
+		part2ram = 0x03810000 - (header.part2_ram_boot7_addr << (2 + shift4));
522 466
 
523
-		ARM9bootAddr = part1ram;
524
-		ARM7bootAddr = part2ram;
467
+		this->ARM9bootAddr = part1ram;
468
+		this->ARM7bootAddr = part2ram;
525 469
 
526
-		size9 = decompress(data + part1addr, tmp_data9);
527
-		if (!tmp_data9)
470
+		this->size9 = this->decompress(&data[part1addr], this->tmp_data9);
471
+		if (!this->tmp_data9)
528 472
 		{
529
-			delete [] data;
530 473
 			fclose(fp);
531 474
 			return false;
532 475
 		}
533 476
 
534
-		size7 = decompress(data + part2addr, tmp_data7);
535
-		if (!tmp_data7)
477
+		this->size7 = this->decompress(&data[part2addr], this->tmp_data7);
478
+		if (!this->tmp_data7)
536 479
 		{
537
-			delete [] tmp_data9;
538
-			delete [] data;
480
+			this->tmp_data9.reset();
539 481
 			fclose(fp);
540 482
 			return false;
541 483
 		};
542 484
 		// Copy firmware boot codes to their respective locations
543 485
 		src = 0;
544
-		for(uint32_t i = 0; i < (size9 >> 2); i++)
486
+		for (uint32_t i = 0; i < (this->size9 >> 2); ++i)
545 487
 		{
546
-			_MMU_write32<ARMCPU_ARM9>(part1ram, T1ReadLong(tmp_data9, src));
547
-			src += 4; part1ram += 4;
488
+			_MMU_write32<ARMCPU_ARM9>(part1ram, T1ReadLong(&this->tmp_data9[0], src));
489
+			src += 4;
490
+			part1ram += 4;
548 491
 		}
549 492
 
550 493
 		src = 0;
551
-		for(uint32_t i = 0; i < (size7 >> 2); i++)
552
-		{
553
-			_MMU_write32<ARMCPU_ARM7>(part2ram, T1ReadLong(tmp_data7, src));
554
-			src += 4; part2ram += 4;
555
-		}
556
-		delete [] tmp_data7;
557
-		delete [] tmp_data9;
558
-
559
-		//INFO("\nFlashme:\n");
560
-		//INFO("- header: \n");
561
-		//INFO("   * ARM9 boot code address:     0x%08X\n", part1addr);
562
-		//INFO("   * ARM9 boot code RAM address: 0x%08X\n", ARM9bootAddr);
563
-		//INFO("   * ARM9 unpacked size:         0x%08X (%i) bytes\n", size9, size9);
564
-		//INFO("\n");
565
-		//INFO("   * ARM7 boot code address:     0x%08X\n", part2addr);
566
-		//INFO("   * ARM7 boot code RAM address: 0x%08X\n", ARM7bootAddr);
567
-		//INFO("   * ARM7 unpacked size:         0x%08X (%i) bytes\n", size7, size7);
568
-	}
569
-
570
-	// Generate the path for the external firmware config file.
571
-	std::string extFilePath = CFIRMWARE::GetExternalFilePath();
572
-	strncpy(MMU.fw.userfile, extFilePath.c_str(), MAX_PATH);
573
-
574
-	fclose(fp);
575
-	fp = fopen(MMU.fw.userfile, "rb");
576
-	if (fp)
577
-	{
578
-		char buf[0x300];
579
-		memset(buf, 0, 0x300);
580
-		if (fread(buf, 1, 0x100, fp) == 0x100)
494
+		for (uint32_t i = 0; i < (this->size7 >> 2); ++i)
581 495
 		{
582
-			printf("- loaded from %s:\n", MMU.fw.userfile);
583
-			memcpy(&data[0x3FE00], &buf[0], 0x100);
584
-			memcpy(&data[0x3FF00], &buf[0], 0x100);
585
-			printf("   * User settings\n");
586
-			memset(buf, 0, 0x100);
587
-			if (fread(buf, 1, 0x1D6, fp) == 0x1D6)
588
-			{
589
-				memcpy(&data[0x002A], &buf[0], 0x1D6);
590
-				printf("   * WiFi settings\n");
591
-
592
-				memset(buf, 0, 0x1D6);
593
-				if (fread(buf, 1, 0x300, fp) == 0x300)
594
-				{
595
-					memcpy(&data[0x3FA00], &buf[0], 0x300);
596
-					printf("   * WiFi AP settings\n");
597
-				}
598
-			}
599
-
496
+			_MMU_write32<ARMCPU_ARM7>(part2ram, T1ReadLong(&this->tmp_data7[0], src));
497
+			src += 4;
498
+			part2ram += 4;
600 499
 		}
601
-		fclose(fp);
500
+		this->tmp_data7.reset();
501
+		this->tmp_data9.reset();
602 502
 	}
603
-	printf("\n");
604 503
 
605 504
 	// TODO: add 512Kb support
606
-	memcpy(&MMU.fw.data[0], data, 256*1024);
607
-	MMU.fw.fp = NULL;
505
+	memcpy(&MMU.fw.data[0], &data[0], 262144);
506
+	MMU.fw.fp = nullptr;
608 507
 
609
-	delete [] data; data = NULL;
610 508
 	return true;
611 509
 }
612 510
 
613
-std::string CFIRMWARE::GetExternalFilePath()
511
+// =====================================================================================================
512
+static uint32_t calc_CRC16(uint32_t start, const uint8_t *data, int count)
614 513
 {
615
-	std::string fwPath = CommonSettings.Firmware;
616
-	std::string fwFileName = Path::GetFileNameFromPathWithoutExt(fwPath);
617
-	std::string configPath = path.pathToBattery;
618
-	std::string finalPath = configPath + DIRECTORY_DELIMITER_CHAR + fwFileName + FILE_EXT_DELIMITER_CHAR + FW_CONFIG_FILE_EXT;
619
-
620
-	return finalPath;
621
-}
622
-
623
-//=====================================================================================================
624
-static uint32_t
625
-calc_CRC16( uint32_t start, const uint8_t *data, int count) {
626
-	int i,j;
627 514
 	uint32_t crc = start & 0xffff;
628
-	const uint16_t val[8] = { 0xC0C1,0xC181,0xC301,0xC601,0xCC01,0xD801,0xF001,0xA001 };
629
-	for(i = 0; i < count; i++)
515
+	const uint16_t val[] = { 0xC0C1, 0xC181, 0xC301, 0xC601, 0xCC01, 0xD801, 0xF001, 0xA001 };
516
+	for (int i = 0; i < count; ++i)
630 517
 	{
631
-		crc = crc ^ data[i];
518
+		crc ^= data[i];
632 519
 
633
-		for(j = 0; j < 8; j++) {
634
-			int do_bit = 0;
520
+		for (int j = 0; j < 8; ++j)
521
+		{
522
+			bool do_bit = false;
635 523
 
636
-			if ( crc & 0x1)
637
-				do_bit = 1;
524
+			if (crc & 0x1)
525
+				do_bit = true;
638 526
 
639
-			crc = crc >> 1;
527
+			crc >>= 1;
640 528
 
641
-			if ( do_bit) {
642
-				crc = crc ^ (val[j] << (7-j));
643
-			}
529
+			if (do_bit)
530
+				crc ^= val[j] << (7 - j);
644 531
 		}
645 532
 	}
646 533
 	return crc;
647 534
 }
648 535
 
649
-int copy_firmware_user_data( uint8_t *dest_buffer, const uint8_t *fw_data)
536
+int copy_firmware_user_data(uint8_t *dest_buffer, const uint8_t *fw_data)
650 537
 {
651 538
 	/*
652
-	* Determine which of the two user settings in the firmware is the current
653
-	* and valid one and then copy this into the destination buffer.
654
-	*
655
-	* The current setting will have a greater count.
656
-	* Settings are only valid if its CRC16 is correct.
657
-	*/
658
-	int user1_valid = 0;
659
-	int user2_valid = 0;
660
-	uint32_t user_settings_offset;
661
-	uint32_t fw_crc;
662
-	uint32_t crc;
539
+	 * Determine which of the two user settings in the firmware is the current
540
+	 * and valid one and then copy this into the destination buffer.
541
+	 *
542
+	 * The current setting will have a greater count.
543
+	 * Settings are only valid if its CRC16 is correct.
544
+	 */
663 545
 	int copy_good = 0;
664 546
 
665
-	user_settings_offset = fw_data[0x20];
547
+	uint32_t user_settings_offset = fw_data[0x20];
666 548
 	user_settings_offset |= fw_data[0x21] << 8;
667 549
 	user_settings_offset <<= 3;
668 550
 
669
-	if ( user_settings_offset <= 0x3FE00) {
551
+	if (user_settings_offset <= 0x3FE00)
552
+	{
670 553
 		int32_t copy_settings_offset = -1;
671 554
 
672
-		crc = calc_CRC16( 0xffff, &fw_data[user_settings_offset],
673
-			NDS_FW_USER_SETTINGS_MEM_BYTE_COUNT);
674
-		fw_crc = fw_data[user_settings_offset + 0x72];
555
+		uint32_t crc = calc_CRC16(0xffff, &fw_data[user_settings_offset], NDS_FW_USER_SETTINGS_MEM_BYTE_COUNT);
556
+		uint32_t fw_crc = fw_data[user_settings_offset + 0x72];
675 557
 		fw_crc |= fw_data[user_settings_offset + 0x73] << 8;
676
-		if ( crc == fw_crc) {
677
-			user1_valid = 1;
678
-		}
558
+		bool user1_valid = crc == fw_crc;
679 559
 
680
-		crc = calc_CRC16( 0xffff, &fw_data[user_settings_offset + 0x100],
681
-			NDS_FW_USER_SETTINGS_MEM_BYTE_COUNT);
560
+		crc = calc_CRC16(0xffff, &fw_data[user_settings_offset + 0x100], NDS_FW_USER_SETTINGS_MEM_BYTE_COUNT);
682 561
 		fw_crc = fw_data[user_settings_offset + 0x100 + 0x72];
683 562
 		fw_crc |= fw_data[user_settings_offset + 0x100 + 0x73] << 8;
684
-		if ( crc == fw_crc) {
685
-			user2_valid = 1;
686
-		}
563
+		bool user2_valid = crc == fw_crc;
687 564
 
688
-		if ( user1_valid) {
689
-			if ( user2_valid) {
565
+		if (user1_valid)
566
+		{
567
+			if (user2_valid)
568
+			{
690 569
 				uint16_t count1, count2;
691 570
 
692 571
 				count1 = fw_data[user_settings_offset + 0x70];
... ...
@@ -695,25 +574,21 @@ int copy_firmware_user_data( uint8_t *dest_buffer, const uint8_t *fw_data)
695 574
 				count2 = fw_data[user_settings_offset + 0x100 + 0x70];
696 575
 				count2 |= fw_data[user_settings_offset + 0x100 + 0x71] << 8;
697 576
 
698
-				if ( count2 > count1) {
577
+				if (count2 > count1)
699 578
 					copy_settings_offset = user_settings_offset + 0x100;
700
-				}
701
-				else {
579
+				else
702 580
 					copy_settings_offset = user_settings_offset;
703
-				}
704 581
 			}
705
-			else {
582
+			else
706 583
 				copy_settings_offset = user_settings_offset;
707
-			}
708 584
 		}
709
-		else if ( user2_valid) {
585
+		else if (user2_valid)
710 586
 			/* copy the second user settings */
711 587
 			copy_settings_offset = user_settings_offset + 0x100;
712
-		}
713 588
 
714
-		if ( copy_settings_offset > 0) {
715
-			memcpy( dest_buffer, &fw_data[copy_settings_offset],
716
-				NDS_FW_USER_SETTINGS_MEM_BYTE_COUNT);
589
+		if (copy_settings_offset > 0)
590
+		{
591
+			memcpy(dest_buffer, &fw_data[copy_settings_offset], NDS_FW_USER_SETTINGS_MEM_BYTE_COUNT);
717 592
 			copy_good = 1;
718 593
 		}
719 594
 	}
... ...
@@ -721,167 +596,12 @@ int copy_firmware_user_data( uint8_t *dest_buffer, const uint8_t *fw_data)
721 596
 	return copy_good;
722 597
 }
723 598
 
724
-/*static void fill_user_data_area( struct NDS_fw_config_data *user_settings,uint8_t *data, int count)
599
+void NDS_FillDefaultFirmwareConfigData(NDS_fw_config_data *fw_config)
725 600
 {
726
-	uint32_t crc;
727
-	int i;
728
-	//uint8_t *ts_cal_data_area;
729
-
730
-	memset( data, 0, 0x100);
731
-
732
-	// version
733
-	data[0x00] = 5;
734
-	data[0x01] = 0;
735
-
736
-	// colour
737
-	data[0x02] = user_settings->fav_colour;
738
-
739
-	// birthday month and day
740
-	data[0x03] = user_settings->birth_month;
741
-	data[0x04] = user_settings->birth_day;
742
-
743
-	//nickname and length
744
-	for ( i = 0; i < MAX_FW_NICKNAME_LENGTH; i++) {
745
-		data[0x06 + (i * 2)] = user_settings->nickname[i] & 0xff;
746
-		data[0x06 + (i * 2) + 1] = (user_settings->nickname[i] >> 8) & 0xff;
747
-	}
748
-
749
-	data[0x1a] = user_settings->nickname_len;
750
-
751
-	//Message
752
-	for ( i = 0; i < MAX_FW_MESSAGE_LENGTH; i++) {
753
-		data[0x1c + (i * 2)] = user_settings->message[i] & 0xff;
754
-		data[0x1c + (i * 2) + 1] = (user_settings->message[i] >> 8) & 0xff;
755
-	}
756
-
757
-	data[0x50] = user_settings->message_len;*/
758
-
759
-	//touch screen calibration
760
-	/*ts_cal_data_area = &data[0x58];
761
-	for ( i = 0; i < 2; i++) {
762
-		// ADC x y
763
-		*ts_cal_data_area++ = user_settings->touch_cal[i].adc_x & 0xff;
764
-		*ts_cal_data_area++ = (user_settings->touch_cal[i].adc_x >> 8) & 0xff;
765
-		*ts_cal_data_area++ = user_settings->touch_cal[i].adc_y & 0xff;
766
-		*ts_cal_data_area++ = (user_settings->touch_cal[i].adc_y >> 8) & 0xff;
767
-
768
-		//screen x y
769
-		*ts_cal_data_area++ = user_settings->touch_cal[i].screen_x;
770
-		*ts_cal_data_area++ = user_settings->touch_cal[i].screen_y;
771
-	}*/
772
-
773
-	//language and flags
774
-	/*data[0x64] = user_settings->language;
775
-	data[0x65] = 0xfc;
776
-
777
-	//update count and crc
778
-	data[0x70] = count & 0xff;
779
-	data[0x71] = (count >> 8) & 0xff;
780
-
781
-	crc = calc_CRC16( 0xffff, data, 0x70);
782
-	data[0x72] = crc & 0xff;
783
-	data[0x73] = (crc >> 8) & 0xff;
784
-
785
-	memset( &data[0x74], 0xff, 0x100 - 0x74);
786
-}*/
787
-
788
-// creates an firmware flash image, which contains all needed info to initiate a wifi connection
789
-/*int NDS_CreateDummyFirmware( struct NDS_fw_config_data *user_settings)
790
-{
791
-	//Create the firmware header
792
-
793
-	memset(&MMU.fw.data[0], 0, 0x40000);
794
-
795
-	//firmware identifier
796
-	MMU.fw.data[0x8] = 'M';
797
-	MMU.fw.data[0x8 + 1] = 'A';
798
-	MMU.fw.data[0x8 + 2] = 'C';
799
-	MMU.fw.data[0x8 + 3] = 'P';
800
-
801
-	// DS type
802
-	if ( user_settings->ds_type == NDS_FW_DS_TYPE_LITE)
803
-		MMU.fw.data[0x1d] = 0x20;
804
-	else
805
-		MMU.fw.data[0x1d] = 0xff;
806
-
807
-	//User Settings offset 0x3fe00 / 8
808
-	MMU.fw.data[0x20] = 0xc0;
809
-	MMU.fw.data[0x21] = 0x7f;
810
-
811
-
812
-	//User settings (at 0x3FE00 and 0x3FF00)
813
-
814
-	fill_user_data_area( user_settings, &MMU.fw.data[ 0x3FE00], 0);
815
-	fill_user_data_area( user_settings, &MMU.fw.data[ 0x3FF00], 1);
816
-
817
-	// Wifi config length
818
-	//MMU.fw.data[0x2C] = 0x38;
819
-	//MMU.fw.data[0x2D] = 0x01;
820
-
821
-	//MMU.fw.data[0x2E] = 0x00;
822
-
823
-	//Wifi version
824
-	//MMU.fw.data[0x2F] = 0x00;
825
-
826
-	//MAC address
827
-	//memcpy((MMU.fw.data + 0x36), FW_Mac, sizeof(FW_Mac));
828
-
829
-	//Enabled channels
830
-	MMU.fw.data[0x3C] = 0xFE;
831
-	MMU.fw.data[0x3D] = 0x3F;
832
-
833
-	MMU.fw.data[0x3E] = 0xFF;
834
-	MMU.fw.data[0x3F] = 0xFF;
835
-
836
-	//RF related
837
-	MMU.fw.data[0x40] = 0x02;
838
-	MMU.fw.data[0x41] = 0x18;
839
-	MMU.fw.data[0x42] = 0x0C;
840
-
841
-	MMU.fw.data[0x43] = 0x01;
842
-
843
-	//Wifi I/O init values
844
-	//memcpy((MMU.fw.data + 0x44), FW_WIFIInit, sizeof(FW_WIFIInit));
845
-
846
-	//Wifi BB init values
847
-	//memcpy((MMU.fw.data + 0x64), FW_BBInit, sizeof(FW_BBInit));
848
-
849
-	//Wifi RF init values
850
-	//memcpy((MMU.fw.data + 0xCE), FW_RFInit, sizeof(FW_RFInit));
851
-
852
-	//Wifi channel-related init values
853
-	//memcpy((MMU.fw.data + 0xF2), FW_RFChannel, sizeof(FW_RFChannel));
854
-	//memcpy((MMU.fw.data + 0x146), FW_BBChannel, sizeof(FW_BBChannel));
855
-	memset(&MMU.fw.data[0x154], 0x10, 0xE);*/
856
-
857
-	//WFC profiles
858
-	/*memcpy((MMU.fw.data + 0x3FA40), &FW_WFCProfile1, sizeof(FW_WFCProfile));
859
-	memcpy((MMU.fw.data + 0x3FB40), &FW_WFCProfile2, sizeof(FW_WFCProfile));
860
-	memcpy((MMU.fw.data + 0x3FC40), &FW_WFCProfile3, sizeof(FW_WFCProfile));
861
-	(*(uint16_t*)(MMU.fw.data + 0x3FAFE)) = (uint16_t)calc_CRC16(0, (MMU.fw.data + 0x3FA00), 0xFE);
862
-	(*(uint16_t*)(MMU.fw.data + 0x3FBFE)) = (uint16_t)calc_CRC16(0, (MMU.fw.data + 0x3FB00), 0xFE);
863
-	(*(uint16_t*)(MMU.fw.data + 0x3FCFE)) = (uint16_t)calc_CRC16(0, (MMU.fw.data + 0x3FC00), 0xFE);*/
864
-
865
-
866
-	/*MMU.fw.data[0x162] = 0x19;
867
-	memset(&MMU.fw.data[0x163], 0xFF, 0x9D);
868
-
869
-	//Wifi settings CRC16
870
-	(*(uint16_t*)(&MMU.fw.data[0x2A])) = calc_CRC16(0, &MMU.fw.data[0x2C], 0x138);
871
-
872
-	if (&CommonSettings.InternalFirmConf != user_settings)
873
-		memcpy(&CommonSettings.InternalFirmConf, user_settings, sizeof(struct NDS_fw_config_data));
874
-
875
-	return true ;
876
-}*/
877
-
878
-void NDS_FillDefaultFirmwareConfigData( struct NDS_fw_config_data *fw_config) {
879 601
 	const char *default_nickname = "DeSmuME";
880 602
 	const char *default_message = "DeSmuME makes you happy!";
881
-	int i;
882
-	int str_length;
883 603
 
884
-	memset( fw_config, 0, sizeof( struct NDS_fw_config_data));
604
+	memset(fw_config, 0, sizeof(struct NDS_fw_config_data));
885 605
 	fw_config->ds_type = NDS_CONSOLE_TYPE_FAT;
886 606
 
887 607
 	fw_config->fav_colour = 7;
... ...
@@ -889,35 +609,17 @@ void NDS_FillDefaultFirmwareConfigData( struct NDS_fw_config_data *fw_config) {
889 609
 	fw_config->birth_day = 23;
890 610
 	fw_config->birth_month = 6;
891 611
 
892
-	str_length = strlen( default_nickname);
893
-	for ( i = 0; i < str_length; i++) {
612
+	int str_length = strlen(default_nickname);
613
+	int i;
614
+	for (i = 0; i < str_length; ++i)
894 615
 		fw_config->nickname[i] = default_nickname[i];
895
-	}
896 616
 	fw_config->nickname_len = str_length;
897 617
 
898
-	str_length = strlen( default_message);
899
-	for ( i = 0; i < str_length; i++) {
618
+	str_length = strlen(default_message);
619
+	for (i = 0; i < str_length; ++i)
900 620
 		fw_config->message[i] = default_message[i];
901
-	}
902 621
 	fw_config->message_len = str_length;
903 622
 
904 623
 	/* default to English */
905 624
 	fw_config->language = 1;
906
-
907
-	/* default touchscreen calibration */
908
-	//fw_config->touch_cal[0].adc_x = 0x200;
909
-	//fw_config->touch_cal[0].adc_y = 0x200;
910
-	//fw_config->touch_cal[0].screen_x = 0x20 + 1; // calibration screen coords are 1-based,
911
-	//fw_config->touch_cal[0].screen_y = 0x20 + 1; // either that or NDS_getADCTouchPosX/Y are wrong.
912
-
913
-	//fw_config->touch_cal[1].adc_x = 0xe00;
914
-	//fw_config->touch_cal[1].adc_y = 0x800;
915
-	//fw_config->touch_cal[1].screen_x = 0xe0 + 1;
916
-	//fw_config->touch_cal[1].screen_y = 0x80 + 1;
917 625
 }
918
-
919
-/*void NDS_PatchFirmwareMAC()
920
-{
921
-	//memcpy((MMU.fw.data + 0x36), FW_Mac, sizeof(FW_Mac));
922
-	//(*(uint16_t*)(MMU.fw.data + 0x2A)) = calc_CRC16(0, (MMU.fw.data + 0x2C), 0x138);
923
-}*/
Browse code

Import actual code.

Naram Qashat authored on 2013/03/26 02:41:19
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,923 @@
1
+/*
2
+	Copyright (C) 2009-2011 DeSmuME Team
3
+
4
+	This file is free software: you can redistribute it and/or modify
5
+	it under the terms of the GNU General Public License as published by
6
+	the Free Software Foundation, either version 2 of the License, or
7
+	(at your option) any later version.
8
+
9
+	This file is distributed in the hope that it will be useful,
10
+	but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
+	GNU General Public License for more details.
13
+
14
+	You should have received a copy of the GNU General Public License
15
+	along with the this software.  If not, see <http://www.gnu.org/licenses/>.
16
+*/
17
+
18
+#include "firmware.h"
19
+#include "NDSSystem.h"
20
+#include "path.h"
21
+
22
+//#define DWNUM(i) ((i) >> 2)
23
+static inline uint32_t DWNUM(uint32_t i) { return i >> 2; }
24
+
25
+bool CFIRMWARE::getKeyBuf()
26
+{
27
+	FILE *file = fopen(CommonSettings.ARM7BIOS, "rb");
28
+	if (!file) return false;
29
+
30
+	fseek(file, 0x30, SEEK_SET);
31
+	size_t res = fread(keyBuf, 4, 0x412, file);
32
+	fclose(file);
33
+	return res == 0x412;
34
+}
35
+
36
+void CFIRMWARE::crypt64BitUp(uint32_t *ptr)
37
+{
38
+	uint32_t Y = ptr[0];
39
+	uint32_t X = ptr[1];
40
+
41
+	for(uint32_t i = 0x00; i <= 0x0F; i++)
42
+	{
43
+		uint32_t Z = (keyBuf[i] ^ X);
44
+		X = keyBuf[DWNUM(0x048 + (((Z >> 24) & 0xFF) << 2))];
45
+		X = (keyBuf[DWNUM(0x448 + (((Z >> 16) & 0xFF) << 2))] + X);
46
+		X = (keyBuf[DWNUM(0x848 + (((Z >> 8) & 0xFF) << 2))] ^ X);
47
+		X = (keyBuf[DWNUM(0xC48 + ((Z & 0xFF) << 2))] + X);
48
+		X = (Y ^ X);
49
+		Y = Z;
50
+	}
51
+
52
+	ptr[0] = (X ^ keyBuf[DWNUM(0x40)]);
53
+	ptr[1] = (Y ^ keyBuf[DWNUM(0x44)]);
54
+}
55
+
56
+void CFIRMWARE::crypt64BitDown(uint32_t *ptr)
57
+{
58
+	uint32_t Y = ptr[0];
59
+	uint32_t X = ptr[1];
60
+
61
+	for(uint32_t i = 0x11; i >= 0x02; i--)
62
+	{
63
+		uint32_t Z = (keyBuf[i] ^ X);
64
+		X = keyBuf[DWNUM(0x048 + (((Z >> 24) & 0xFF) << 2))];
65
+		X = (keyBuf[DWNUM(0x448 + (((Z >> 16) & 0xFF) << 2))] + X);
66
+		X = (keyBuf[DWNUM(0x848 + (((Z >> 8)  & 0xFF) << 2))] ^ X);
67
+		X = (keyBuf[DWNUM(0xC48 + ((Z & 0xFF) << 2))] + X);
68
+		X = (Y ^ X);
69
+		Y = Z;
70
+	}
71
+
72
+	ptr[0] = (X ^ keyBuf[DWNUM(0x04)]);
73
+	ptr[1] = (Y ^ keyBuf[DWNUM(0x00)]);
74
+}
75
+
76
+//#define bswap32(val) (((val & 0x000000FF) << 24) | ((val & 0x0000FF00) << 8) | ((val & 0x00FF0000) >> 8) | ((val & 0xFF000000) >> 24))
77
+static inline uint32_t bswap32(uint32_t val) { return ((val & 0x000000FF) << 24) | ((val & 0x0000FF00) << 8) | ((val & 0x00FF0000) >> 8) | ((val & 0xFF000000) >> 24); }
78
+void CFIRMWARE::applyKeycode(uint32_t modulo)
79
+{
80
+	crypt64BitUp(&keyCode[1]);
81
+	crypt64BitUp(&keyCode[0]);
82
+
83
+	uint32_t scratch[2] = {0x00000000, 0x00000000};
84
+
85
+	for(uint32_t i = 0; i <= 0x44; i += 4)
86
+	{
87
+		keyBuf[DWNUM(i)] = (keyBuf[DWNUM(i)] ^ bswap32(keyCode[DWNUM(i % modulo)]));
88
+	}
89
+
90
+	for(uint32_t i = 0; i <= 0x1040; i += 8)
91
+	{
92
+		crypt64BitUp(scratch);
93
+		keyBuf[DWNUM(i)] = scratch[1];
94
+		keyBuf[DWNUM(i+4)] = scratch[0];
95
+	}
96
+}
97
+//#undef bswap32
98
+
99
+bool CFIRMWARE::initKeycode(uint32_t idCode, int level, uint32_t modulo)
100
+{
101
+	if(getKeyBuf() == false)
102
+		return false;
103
+
104
+	keyCode[0] = idCode;
105
+	keyCode[1] = (idCode >> 1);
106
+	keyCode[2] = (idCode << 1);
107
+
108
+	if(level >= 1) applyKeycode(modulo);
109
+	if(level >= 2) applyKeycode(modulo);
110
+
111
+	keyCode[1] <<= 1;
112
+	keyCode[2] >>= 1;
113
+
114
+	if(level >= 3) applyKeycode(modulo);
115
+
116
+	return true;
117
+}
118
+
119
+uint16_t CFIRMWARE::getBootCodeCRC16()
120
+{
121
+	unsigned int i, j;
122
+	uint32_t crc = 0xFFFF;
123
+	const uint16_t val[8] = {0xC0C1, 0xC181, 0xC301, 0xC601, 0xCC01, 0xD801, 0xF001, 0xA001};
124
+
125
+	for(i = 0; i < size9; i++)
126
+	{
127
+		crc = (crc ^ tmp_data9[i]);
128
+
129
+		for(j = 0; j < 8; j++)
130
+		{
131
+			if(crc & 0x0001)
132
+				crc = ((crc >> 1) ^ (val[j] << (7-j)));
133
+			else
134
+				crc =  (crc >> 1);
135
+		}
136
+	}
137
+
138
+	for(i = 0; i < size7; i++)
139
+	{
140
+		crc = (crc ^ tmp_data7[i]);
141
+
142
+		for(j = 0; j < 8; j++)
143
+		{
144
+			if(crc & 0x0001)
145
+				crc = ((crc >> 1) ^ (val[j] << (7-j)));
146
+			else
147
+				crc =  (crc >> 1);
148
+		}
149
+	}
150
+
151
+	return crc & 0xFFFF;
152
+}
153
+
154
+uint32_t CFIRMWARE::decrypt(const uint8_t *in, uint8_t* &out)
155
+{
156
+	uint32_t curBlock[2] = { 0 };
157
+	uint32_t blockSize = 0;
158
+	uint32_t xLen = 0;
159
+
160
+	uint32_t i = 0, j = 0;
161
+	uint32_t xIn = 4, xOut = 0;
162
+	uint32_t len = 0;
163
+	uint32_t offset = 0;
164
+	uint32_t windowOffset = 0;
165
+	uint8_t d = 0;
166
+
167
+	uint16_t data = 0;
168
+
169
+	memcpy(curBlock, in, 8);
170
+	crypt64BitDown(curBlock);
171
+	blockSize = (curBlock[0] >> 8);
172
+
173
+	if (blockSize == 0) return 0;
174
+
175
+	out = new uint8_t [blockSize];
176
+	if (!out ) return 0;
177
+	memset(out, 0xFF, blockSize);
178
+
179
+	xLen = blockSize;
180
+	while(xLen > 0)
181
+	{
182
+		d = T1ReadByte((uint8_t*)curBlock, (xIn % 8));
183
+		xIn++;
184
+		if((xIn % 8) == 0)
185
+		{
186
+			memcpy(curBlock, in + xIn, 8);
187
+			crypt64BitDown(curBlock);
188
+		}
189
+
190
+		for(i = 0; i < 8; i++)
191
+		{
192
+			if(d & 0x80)
193
+			{
194
+				data = (T1ReadByte((uint8_t*)curBlock, (xIn % 8)) << 8);
195
+				xIn++;
196
+				if((xIn % 8) == 0)
197
+				{
198
+					memcpy(curBlock, in + xIn, 8);
199
+					crypt64BitDown(curBlock);
200
+				}
201
+				data |= T1ReadByte((uint8_t*)curBlock, (xIn % 8));
202
+				xIn++;
203
+				if((xIn % 8) == 0)
204
+				{
205
+					memcpy(curBlock, in + xIn, 8);
206
+					crypt64BitDown(curBlock);
207
+				}
208
+
209
+				len = (data >> 12) + 3;
210
+				offset = (data & 0xFFF);
211
+				windowOffset = (xOut - offset - 1);
212
+
213
+				for(j = 0; j < len; j++)
214
+				{
215
+					T1WriteByte(out, xOut, T1ReadByte(out, windowOffset));
216
+					xOut++;
217
+					windowOffset++;
218
+
219
+					xLen--;
220
+					if(xLen == 0) return blockSize;
221
+				}
222
+			}
223
+			else
224
+			{
225
+				T1WriteByte(out, xOut, T1ReadByte((uint8_t*)curBlock, (xIn % 8)));
226
+				xOut++;
227
+				xIn++;
228
+				if((xIn % 8) == 0)
229
+				{
230
+					memcpy(curBlock, in + xIn, 8);
231
+					crypt64BitDown(curBlock);
232
+				}
233
+
234
+				xLen--;
235
+				if(xLen == 0) return blockSize;
236
+			}
237
+
238
+			d = ((d << 1) & 0xFF);
239
+		}
240
+	}
241
+
242
+	return blockSize;
243
+}
244
+
245
+uint32_t CFIRMWARE::decompress(const uint8_t *in, uint8_t* &out)
246
+{
247
+	uint32_t curBlock[2] = { 0 };
248
+	uint32_t blockSize = 0;
249
+	uint32_t xLen = 0;
250
+
251
+	uint32_t i = 0, j = 0;
252
+	uint32_t xIn = 4, xOut = 0;
253
+	uint32_t len = 0;
254
+	uint32_t offset = 0;
255
+	uint32_t windowOffset = 0;
256
+	uint8_t d = 0;
257
+	uint16_t data = 0;
258
+
259
+	memcpy(curBlock, in, 8);
260
+	blockSize = (curBlock[0] >> 8);
261
+
262
+	if (blockSize == 0) return 0;
263
+
264
+	out = new uint8_t [blockSize];
265
+	if (!out ) return 0;
266
+	memset(out, 0xFF, blockSize);
267
+
268
+	xLen = blockSize;
269
+	while(xLen > 0)
270
+	{
271
+		d = T1ReadByte((uint8_t*)curBlock, (xIn % 8));
272
+		xIn++;
273
+		if((xIn % 8) == 0)
274
+		{
275
+			memcpy(curBlock, in + xIn, 8);
276
+		}
277
+
278
+		for(i = 0; i < 8; i++)
279
+		{
280
+			if(d & 0x80)
281
+			{
282
+				data = (T1ReadByte((uint8_t*)curBlock, (xIn % 8)) << 8);
283
+				xIn++;
284
+				if((xIn % 8) == 0)
285
+				{
286
+					memcpy(curBlock, in + xIn, 8);
287
+				}
288
+				data |= T1ReadByte((uint8_t*)curBlock, (xIn % 8));
289
+				xIn++;
290
+				if((xIn % 8) == 0)
291
+				{
292
+					memcpy(curBlock, in + xIn, 8);
293
+				}
294
+
295
+				len = (data >> 12) + 3;
296
+				offset = (data & 0xFFF);
297
+				windowOffset = (xOut - offset - 1);
298
+
299
+				for(j = 0; j < len; j++)
300
+				{
301
+					T1WriteByte(out, xOut, T1ReadByte(out, windowOffset));
302
+					xOut++;
303
+					windowOffset++;
304
+
305
+					xLen--;
306
+					if(xLen == 0) return blockSize;
307
+				}
308
+			}
309
+			else
310
+			{
311
+				T1WriteByte(out, xOut, T1ReadByte((uint8_t*)curBlock, (xIn % 8)));
312
+				xOut++;
313
+				xIn++;
314
+				if((xIn % 8) == 0)
315
+				{
316
+					memcpy(curBlock, in + xIn, 8);
317
+				}
318
+
319
+				xLen--;
320
+				if(xLen == 0) return blockSize;
321
+			}
322
+
323
+			d = ((d << 1) & 0xFF);
324
+		}
325
+	}
326
+
327
+	return blockSize;
328
+}
329
+//================================================================================
330
+bool CFIRMWARE::load()
331
+{
332
+	uint32_t size = 0;
333
+	uint8_t	*data = NULL;
334
+	uint16_t shift1 = 0, shift2 = 0, shift3 = 0, shift4 = 0;
335
+	uint32_t part1addr = 0, part2addr = 0/*, part3addr = 0, part4addr = 0, part5addr = 0*/;
336
+	uint32_t part1ram = 0, part2ram = 0;
337
+
338
+	uint32_t	src = 0;
339
+
340
+	if (CommonSettings.UseExtFirmware == false)
341
+		return false;
342
+	if (strlen(CommonSettings.Firmware) == 0)
343
+		return false;
344
+
345
+	FILE	*fp = fopen(CommonSettings.Firmware, "rb");
346
+	if (!fp)
347
+		return false;
348
+	fseek(fp, 0, SEEK_END);
349
+	size = ftell(fp);
350
+	fseek(fp, 0, SEEK_SET);
351
+	if( (size != 256*1024) && (size != 512*1024) )
352
+	{
353
+		fclose(fp);
354
+		return false;
355
+	}
356
+
357
+#if 1
358
+	if (size == 512*1024)
359
+	{
360
+		//INFO("ERROR: 32Mbit (512Kb) firmware not supported\n");
361
+		fclose(fp);
362
+		return false;
363
+	}
364
+#endif
365
+
366
+	data = new uint8_t [size];
367
+	if (!data)
368
+	{
369
+		fclose(fp);
370
+		return false;
371
+	}
372
+
373
+	if (fread(data, 1, size, fp) != size)
374
+	{
375
+		delete [] data;
376
+		fclose(fp);
377
+		return false;
378
+	}
379
+
380
+	memcpy(&header, data, sizeof(header));
381
+	if ((header.fw_identifier[0] != 'M') ||
382
+			(header.fw_identifier[1] != 'A') ||
383
+				(header.fw_identifier[2] != 'C'))
384
+				{
385
+					delete [] data;
386
+					fclose(fp);
387
+					return false;
388
+				}
389
+
390
+	shift1 = ((header.shift_amounts >> 0) & 0x07);
391
+	shift2 = ((header.shift_amounts >> 3) & 0x07);
392
+	shift3 = ((header.shift_amounts >> 6) & 0x07);
393
+	shift4 = ((header.shift_amounts >> 9) & 0x07);
394
+
395
+	// todo - add support for 512Kb
396
+	part1addr = (header.part1_rom_boot9_addr << (2 + shift1));
397
+	part1ram = (0x02800000 - (header.part1_ram_boot9_addr << (2+shift2)));
398
+	part2addr = (header.part2_rom_boot7_addr << (2+shift3));
399
+	part2ram = (0x03810000 - (header.part2_ram_boot7_addr << (2+shift4)));
400
+	//part3addr = (header.part3_rom_gui9_addr << 3);
401
+	//part4addr = (header.part4_rom_wifi7_addr << 3);
402
+	//part5addr = (header.part5_data_gfx_addr << 3);
403
+
404
+	ARM9bootAddr = part1ram;
405
+	ARM7bootAddr = part2ram;
406
+
407
+	if(initKeycode(T1ReadLong(data, 0x08), 1, 0xC) == false)
408
+	{
409
+		delete [] data;
410
+		fclose(fp);
411
+		return false;
412
+	}
413
+
414
+#if 0
415
+	crypt64BitDown((uint32_t*)&data[0x18]);
416
+#else
417
+	// fix touch coords
418
+	data[0x18] = 0x00;
419
+	data[0x19] = 0x00;
420
+	data[0x1A] = 0x00;
421
+	data[0x1B] = 0x00;
422
+
423
+	data[0x1C] = 0x00;
424
+	data[0x1D] = 0xFF;
425
+	data[0x1E] = 0x00;
426
+	data[0x1F] = 0x00;
427
+#endif
428
+
429
+	if(initKeycode(T1ReadLong(data, 0x08), 2, 0xC) == false)
430
+	{
431
+		delete [] data;
432
+		fclose(fp);
433
+		return false;
434
+	}
435
+
436
+	size9 = decrypt(data + part1addr, tmp_data9);
437
+	if (!tmp_data9)
438
+	{
439
+		delete [] data;
440
+		fclose(fp);
441
+		return false;
442
+	}
443
+
444
+	size7 = decrypt(data + part2addr, tmp_data7);
445
+	if (!tmp_data7)
446
+	{
447
+		delete [] tmp_data9;
448
+		delete [] data;
449
+		fclose(fp);
450
+		return false;
451
+	}
452
+
453
+	uint16_t crc16_mine = getBootCodeCRC16();
454
+
455
+	if (crc16_mine != header.part12_boot_crc16)
456
+	{
457
+		//INFO("Firmware: ERROR: the boot code CRC16 (0x%04X) doesn't match the value in the firmware header (0x%04X)", crc16_mine, header.part12_boot_crc16);
458
+		delete [] tmp_data9;
459
+		delete [] tmp_data7;
460
+		delete [] data;
461
+		fclose(fp);
462
+		return false;
463
+	}
464
+
465
+	// Copy firmware boot codes to their respective locations
466
+	src = 0;
467
+	for(uint32_t i = 0; i < (size9 >> 2); i++)
468
+	{
469
+		_MMU_write32<ARMCPU_ARM9>(part1ram, T1ReadLong(tmp_data9, src));
470
+		src += 4; part1ram += 4;
471
+	}
472
+
473
+	src = 0;
474
+	for(uint32_t i = 0; i < (size7 >> 2); i++)
475
+	{
476
+		_MMU_write32<ARMCPU_ARM7>(part2ram, T1ReadLong(tmp_data7, src));
477
+		src += 4; part2ram += 4;
478
+	}
479
+	delete [] tmp_data7;
480
+	delete [] tmp_data9;
481
+
482
+	patched = false;
483
+	if (data[0x17C] != 0xFF)
484
+		patched = true;
485
+
486
+	//INFO("Firmware:\n");
487
+	//INFO("- path: %s\n", CommonSettings.Firmware);
488
+	//INFO("- size: %i bytes (%i Mbit)\n", size, size/1024/8);
489
+	//INFO("- CRC : 0x%04X\n", header.part12_boot_crc16);
490
+	//INFO("- header: \n");
491
+	//INFO("   * size firmware %i\n", ((header.shift_amounts >> 12) & 0xF) * 128 * 1024);
492
+	//INFO("   * ARM9 boot code address:     0x%08X\n", part1addr);
493
+	//INFO("   * ARM9 boot code RAM address: 0x%08X\n", ARM9bootAddr);
494
+	//INFO("   * ARM9 unpacked size:         0x%08X (%i) bytes\n", size9, size9);
495
+	//INFO("   * ARM9 GUI code address:      0x%08X\n", part3addr);
496
+	//INFO("\n");
497
+	//INFO("   * ARM7 boot code address:     0x%08X\n", part2addr);
498
+	//INFO("   * ARM7 boot code RAM address: 0x%08X\n", ARM7bootAddr);
499
+	//INFO("   * ARM7 WiFi code address:     0x%08X\n", part4addr);
500
+	//INFO("   * ARM7 unpacked size:         0x%08X (%i) bytes\n", size7, size7);
501
+	//INFO("\n");
502
+	//INFO("   * Data/GFX address:           0x%08X\n", part5addr);
503
+
504
+	if (patched)
505
+	{
506
+		uint32_t patch_offset = 0x3FC80;
507
+		if (data[0x17C] > 1)
508
+			patch_offset = 0x3F680;
509
+
510
+		memcpy(&header, data + patch_offset, sizeof(header));
511
+
512
+		shift1 = ((header.shift_amounts >> 0) & 0x07);
513
+		shift2 = ((header.shift_amounts >> 3) & 0x07);
514
+		shift3 = ((header.shift_amounts >> 6) & 0x07);
515
+		shift4 = ((header.shift_amounts >> 9) & 0x07);
516
+
517
+		// todo - add support for 512Kb
518
+		part1addr = (header.part1_rom_boot9_addr << (2 + shift1));
519
+		part1ram = (0x02800000 - (header.part1_ram_boot9_addr << (2+shift2)));
520
+		part2addr = (header.part2_rom_boot7_addr << (2+shift3));
521
+		part2ram = (0x03810000 - (header.part2_ram_boot7_addr << (2+shift4)));
522
+
523
+		ARM9bootAddr = part1ram;
524
+		ARM7bootAddr = part2ram;
525
+
526
+		size9 = decompress(data + part1addr, tmp_data9);
527
+		if (!tmp_data9)
528
+		{
529
+			delete [] data;
530
+			fclose(fp);
531
+			return false;
532
+		}
533
+
534
+		size7 = decompress(data + part2addr, tmp_data7);
535
+		if (!tmp_data7)
536
+		{
537
+			delete [] tmp_data9;
538
+			delete [] data;
539
+			fclose(fp);
540
+			return false;
541
+		};
542
+		// Copy firmware boot codes to their respective locations
543
+		src = 0;
544
+		for(uint32_t i = 0; i < (size9 >> 2); i++)
545
+		{
546
+			_MMU_write32<ARMCPU_ARM9>(part1ram, T1ReadLong(tmp_data9, src));
547
+			src += 4; part1ram += 4;
548
+		}
549
+
550
+		src = 0;
551
+		for(uint32_t i = 0; i < (size7 >> 2); i++)
552
+		{
553
+			_MMU_write32<ARMCPU_ARM7>(part2ram, T1ReadLong(tmp_data7, src));
554
+			src += 4; part2ram += 4;
555
+		}
556
+		delete [] tmp_data7;
557
+		delete [] tmp_data9;
558
+
559
+		//INFO("\nFlashme:\n");
560
+		//INFO("- header: \n");
561
+		//INFO("   * ARM9 boot code address:     0x%08X\n", part1addr);
562
+		//INFO("   * ARM9 boot code RAM address: 0x%08X\n", ARM9bootAddr);
563
+		//INFO("   * ARM9 unpacked size:         0x%08X (%i) bytes\n", size9, size9);
564
+		//INFO("\n");
565
+		//INFO("   * ARM7 boot code address:     0x%08X\n", part2addr);
566
+		//INFO("   * ARM7 boot code RAM address: 0x%08X\n", ARM7bootAddr);
567
+		//INFO("   * ARM7 unpacked size:         0x%08X (%i) bytes\n", size7, size7);
568
+	}
569
+
570
+	// Generate the path for the external firmware config file.
571
+	std::string extFilePath = CFIRMWARE::GetExternalFilePath();
572
+	strncpy(MMU.fw.userfile, extFilePath.c_str(), MAX_PATH);
573
+
574
+	fclose(fp);
575
+	fp = fopen(MMU.fw.userfile, "rb");
576
+	if (fp)
577
+	{
578
+		char buf[0x300];
579
+		memset(buf, 0, 0x300);
580
+		if (fread(buf, 1, 0x100, fp) == 0x100)
581
+		{
582
+			printf("- loaded from %s:\n", MMU.fw.userfile);
583
+			memcpy(&data[0x3FE00], &buf[0], 0x100);
584
+			memcpy(&data[0x3FF00], &buf[0], 0x100);
585
+			printf("   * User settings\n");
586
+			memset(buf, 0, 0x100);
587
+			if (fread(buf, 1, 0x1D6, fp) == 0x1D6)
588
+			{
589
+				memcpy(&data[0x002A], &buf[0], 0x1D6);
590
+				printf("   * WiFi settings\n");
591
+
592
+				memset(buf, 0, 0x1D6);
593
+				if (fread(buf, 1, 0x300, fp) == 0x300)
594
+				{
595
+					memcpy(&data[0x3FA00], &buf[0], 0x300);
596
+					printf("   * WiFi AP settings\n");
597
+				}
598
+			}
599
+
600
+		}
601
+		fclose(fp);
602
+	}
603
+	printf("\n");
604
+
605
+	// TODO: add 512Kb support
606
+	memcpy(&MMU.fw.data[0], data, 256*1024);
607
+	MMU.fw.fp = NULL;
608
+
609
+	delete [] data; data = NULL;
610
+	return true;
611
+}
612
+
613
+std::string CFIRMWARE::GetExternalFilePath()
614
+{
615
+	std::string fwPath = CommonSettings.Firmware;
616
+	std::string fwFileName = Path::GetFileNameFromPathWithoutExt(fwPath);
617
+	std::string configPath = path.pathToBattery;
618
+	std::string finalPath = configPath + DIRECTORY_DELIMITER_CHAR + fwFileName + FILE_EXT_DELIMITER_CHAR + FW_CONFIG_FILE_EXT;
619
+
620
+	return finalPath;
621
+}
622
+
623
+//=====================================================================================================
624
+static uint32_t
625
+calc_CRC16( uint32_t start, const uint8_t *data, int count) {
626
+	int i,j;
627
+	uint32_t crc = start & 0xffff;
628
+	const uint16_t val[8] = { 0xC0C1,0xC181,0xC301,0xC601,0xCC01,0xD801,0xF001,0xA001 };
629
+	for(i = 0; i < count; i++)
630
+	{
631
+		crc = crc ^ data[i];
632
+
633
+		for(j = 0; j < 8; j++) {
634
+			int do_bit = 0;
635
+
636
+			if ( crc & 0x1)
637
+				do_bit = 1;
638
+
639
+			crc = crc >> 1;
640
+
641
+			if ( do_bit) {
642
+				crc = crc ^ (val[j] << (7-j));
643
+			}
644
+		}
645
+	}
646
+	return crc;
647
+}
648
+
649
+int copy_firmware_user_data( uint8_t *dest_buffer, const uint8_t *fw_data)
650
+{
651
+	/*
652
+	* Determine which of the two user settings in the firmware is the current
653
+	* and valid one and then copy this into the destination buffer.
654
+	*
655
+	* The current setting will have a greater count.
656
+	* Settings are only valid if its CRC16 is correct.
657
+	*/
658
+	int user1_valid = 0;
659
+	int user2_valid = 0;
660
+	uint32_t user_settings_offset;
661
+	uint32_t fw_crc;
662
+	uint32_t crc;
663
+	int copy_good = 0;
664
+
665
+	user_settings_offset = fw_data[0x20];
666
+	user_settings_offset |= fw_data[0x21] << 8;
667
+	user_settings_offset <<= 3;
668
+
669
+	if ( user_settings_offset <= 0x3FE00) {
670
+		int32_t copy_settings_offset = -1;
671
+
672
+		crc = calc_CRC16( 0xffff, &fw_data[user_settings_offset],
673
+			NDS_FW_USER_SETTINGS_MEM_BYTE_COUNT);
674
+		fw_crc = fw_data[user_settings_offset + 0x72];
675
+		fw_crc |= fw_data[user_settings_offset + 0x73] << 8;
676
+		if ( crc == fw_crc) {
677
+			user1_valid = 1;
678
+		}
679
+
680
+		crc = calc_CRC16( 0xffff, &fw_data[user_settings_offset + 0x100],
681
+			NDS_FW_USER_SETTINGS_MEM_BYTE_COUNT);
682
+		fw_crc = fw_data[user_settings_offset + 0x100 + 0x72];
683
+		fw_crc |= fw_data[user_settings_offset + 0x100 + 0x73] << 8;
684
+		if ( crc == fw_crc) {
685
+			user2_valid = 1;
686
+		}
687
+
688
+		if ( user1_valid) {
689
+			if ( user2_valid) {
690
+				uint16_t count1, count2;
691
+
692
+				count1 = fw_data[user_settings_offset + 0x70];
693
+				count1 |= fw_data[user_settings_offset + 0x71] << 8;
694
+
695
+				count2 = fw_data[user_settings_offset + 0x100 + 0x70];
696
+				count2 |= fw_data[user_settings_offset + 0x100 + 0x71] << 8;
697
+
698
+				if ( count2 > count1) {
699
+					copy_settings_offset = user_settings_offset + 0x100;
700
+				}
701
+				else {
702
+					copy_settings_offset = user_settings_offset;
703
+				}
704
+			}
705
+			else {
706
+				copy_settings_offset = user_settings_offset;
707
+			}
708
+		}
709
+		else if ( user2_valid) {
710
+			/* copy the second user settings */
711
+			copy_settings_offset = user_settings_offset + 0x100;
712
+		}
713
+
714
+		if ( copy_settings_offset > 0) {
715
+			memcpy( dest_buffer, &fw_data[copy_settings_offset],
716
+				NDS_FW_USER_SETTINGS_MEM_BYTE_COUNT);
717
+			copy_good = 1;
718
+		}
719
+	}
720
+
721
+	return copy_good;
722
+}
723
+
724
+/*static void fill_user_data_area( struct NDS_fw_config_data *user_settings,uint8_t *data, int count)
725
+{
726
+	uint32_t crc;
727
+	int i;
728
+	//uint8_t *ts_cal_data_area;
729
+
730
+	memset( data, 0, 0x100);
731
+
732
+	// version
733
+	data[0x00] = 5;
734
+	data[0x01] = 0;
735
+
736
+	// colour
737
+	data[0x02] = user_settings->fav_colour;
738
+
739
+	// birthday month and day
740
+	data[0x03] = user_settings->birth_month;
741
+	data[0x04] = user_settings->birth_day;
742
+
743
+	//nickname and length
744
+	for ( i = 0; i < MAX_FW_NICKNAME_LENGTH; i++) {
745
+		data[0x06 + (i * 2)] = user_settings->nickname[i] & 0xff;
746
+		data[0x06 + (i * 2) + 1] = (user_settings->nickname[i] >> 8) & 0xff;
747
+	}
748
+
749
+	data[0x1a] = user_settings->nickname_len;
750
+
751
+	//Message
752
+	for ( i = 0; i < MAX_FW_MESSAGE_LENGTH; i++) {
753
+		data[0x1c + (i * 2)] = user_settings->message[i] & 0xff;
754
+		data[0x1c + (i * 2) + 1] = (user_settings->message[i] >> 8) & 0xff;
755
+	}
756
+
757
+	data[0x50] = user_settings->message_len;*/
758
+
759
+	//touch screen calibration
760
+	/*ts_cal_data_area = &data[0x58];
761
+	for ( i = 0; i < 2; i++) {
762
+		// ADC x y
763
+		*ts_cal_data_area++ = user_settings->touch_cal[i].adc_x & 0xff;
764
+		*ts_cal_data_area++ = (user_settings->touch_cal[i].adc_x >> 8) & 0xff;
765
+		*ts_cal_data_area++ = user_settings->touch_cal[i].adc_y & 0xff;
766
+		*ts_cal_data_area++ = (user_settings->touch_cal[i].adc_y >> 8) & 0xff;
767
+
768
+		//screen x y
769
+		*ts_cal_data_area++ = user_settings->touch_cal[i].screen_x;
770
+		*ts_cal_data_area++ = user_settings->touch_cal[i].screen_y;
771
+	}*/
772
+
773
+	//language and flags
774
+	/*data[0x64] = user_settings->language;
775
+	data[0x65] = 0xfc;
776
+
777
+	//update count and crc
778
+	data[0x70] = count & 0xff;
779
+	data[0x71] = (count >> 8) & 0xff;
780
+
781
+	crc = calc_CRC16( 0xffff, data, 0x70);
782
+	data[0x72] = crc & 0xff;
783
+	data[0x73] = (crc >> 8) & 0xff;
784
+
785
+	memset( &data[0x74], 0xff, 0x100 - 0x74);
786
+}*/
787
+
788
+// creates an firmware flash image, which contains all needed info to initiate a wifi connection
789
+/*int NDS_CreateDummyFirmware( struct NDS_fw_config_data *user_settings)
790
+{
791
+	//Create the firmware header
792
+
793
+	memset(&MMU.fw.data[0], 0, 0x40000);
794
+
795
+	//firmware identifier
796
+	MMU.fw.data[0x8] = 'M';
797
+	MMU.fw.data[0x8 + 1] = 'A';
798
+	MMU.fw.data[0x8 + 2] = 'C';
799
+	MMU.fw.data[0x8 + 3] = 'P';
800
+
801
+	// DS type
802
+	if ( user_settings->ds_type == NDS_FW_DS_TYPE_LITE)
803
+		MMU.fw.data[0x1d] = 0x20;
804
+	else
805
+		MMU.fw.data[0x1d] = 0xff;
806
+
807
+	//User Settings offset 0x3fe00 / 8
808
+	MMU.fw.data[0x20] = 0xc0;
809
+	MMU.fw.data[0x21] = 0x7f;
810
+
811
+
812
+	//User settings (at 0x3FE00 and 0x3FF00)
813
+
814
+	fill_user_data_area( user_settings, &MMU.fw.data[ 0x3FE00], 0);
815
+	fill_user_data_area( user_settings, &MMU.fw.data[ 0x3FF00], 1);
816
+
817
+	// Wifi config length
818
+	//MMU.fw.data[0x2C] = 0x38;
819
+	//MMU.fw.data[0x2D] = 0x01;
820
+
821
+	//MMU.fw.data[0x2E] = 0x00;
822
+
823
+	//Wifi version
824
+	//MMU.fw.data[0x2F] = 0x00;
825
+
826
+	//MAC address
827
+	//memcpy((MMU.fw.data + 0x36), FW_Mac, sizeof(FW_Mac));
828
+
829
+	//Enabled channels
830
+	MMU.fw.data[0x3C] = 0xFE;
831
+	MMU.fw.data[0x3D] = 0x3F;
832
+
833
+	MMU.fw.data[0x3E] = 0xFF;
834
+	MMU.fw.data[0x3F] = 0xFF;
835
+
836
+	//RF related
837
+	MMU.fw.data[0x40] = 0x02;
838
+	MMU.fw.data[0x41] = 0x18;
839
+	MMU.fw.data[0x42] = 0x0C;
840
+
841
+	MMU.fw.data[0x43] = 0x01;
842
+
843
+	//Wifi I/O init values
844
+	//memcpy((MMU.fw.data + 0x44), FW_WIFIInit, sizeof(FW_WIFIInit));
845
+
846
+	//Wifi BB init values
847
+	//memcpy((MMU.fw.data + 0x64), FW_BBInit, sizeof(FW_BBInit));
848
+
849
+	//Wifi RF init values
850
+	//memcpy((MMU.fw.data + 0xCE), FW_RFInit, sizeof(FW_RFInit));
851
+
852
+	//Wifi channel-related init values
853
+	//memcpy((MMU.fw.data + 0xF2), FW_RFChannel, sizeof(FW_RFChannel));
854
+	//memcpy((MMU.fw.data + 0x146), FW_BBChannel, sizeof(FW_BBChannel));
855
+	memset(&MMU.fw.data[0x154], 0x10, 0xE);*/
856
+
857
+	//WFC profiles
858
+	/*memcpy((MMU.fw.data + 0x3FA40), &FW_WFCProfile1, sizeof(FW_WFCProfile));
859
+	memcpy((MMU.fw.data + 0x3FB40), &FW_WFCProfile2, sizeof(FW_WFCProfile));
860
+	memcpy((MMU.fw.data + 0x3FC40), &FW_WFCProfile3, sizeof(FW_WFCProfile));
861
+	(*(uint16_t*)(MMU.fw.data + 0x3FAFE)) = (uint16_t)calc_CRC16(0, (MMU.fw.data + 0x3FA00), 0xFE);
862
+	(*(uint16_t*)(MMU.fw.data + 0x3FBFE)) = (uint16_t)calc_CRC16(0, (MMU.fw.data + 0x3FB00), 0xFE);
863
+	(*(uint16_t*)(MMU.fw.data + 0x3FCFE)) = (uint16_t)calc_CRC16(0, (MMU.fw.data + 0x3FC00), 0xFE);*/
864
+
865
+
866
+	/*MMU.fw.data[0x162] = 0x19;
867
+	memset(&MMU.fw.data[0x163], 0xFF, 0x9D);
868
+
869
+	//Wifi settings CRC16
870
+	(*(uint16_t*)(&MMU.fw.data[0x2A])) = calc_CRC16(0, &MMU.fw.data[0x2C], 0x138);
871
+
872
+	if (&CommonSettings.InternalFirmConf != user_settings)
873
+		memcpy(&CommonSettings.InternalFirmConf, user_settings, sizeof(struct NDS_fw_config_data));
874
+
875
+	return true ;
876
+}*/
877
+
878
+void NDS_FillDefaultFirmwareConfigData( struct NDS_fw_config_data *fw_config) {
879
+	const char *default_nickname = "DeSmuME";
880
+	const char *default_message = "DeSmuME makes you happy!";
881
+	int i;
882
+	int str_length;
883
+
884
+	memset( fw_config, 0, sizeof( struct NDS_fw_config_data));
885
+	fw_config->ds_type = NDS_CONSOLE_TYPE_FAT;
886
+
887
+	fw_config->fav_colour = 7;
888
+
889
+	fw_config->birth_day = 23;
890
+	fw_config->birth_month = 6;
891
+
892
+	str_length = strlen( default_nickname);
893
+	for ( i = 0; i < str_length; i++) {
894
+		fw_config->nickname[i] = default_nickname[i];
895
+	}
896
+	fw_config->nickname_len = str_length;
897
+
898
+	str_length = strlen( default_message);
899
+	for ( i = 0; i < str_length; i++) {
900
+		fw_config->message[i] = default_message[i];
901
+	}
902
+	fw_config->message_len = str_length;
903
+
904
+	/* default to English */
905
+	fw_config->language = 1;
906
+
907
+	/* default touchscreen calibration */
908
+	//fw_config->touch_cal[0].adc_x = 0x200;
909
+	//fw_config->touch_cal[0].adc_y = 0x200;
910
+	//fw_config->touch_cal[0].screen_x = 0x20 + 1; // calibration screen coords are 1-based,
911
+	//fw_config->touch_cal[0].screen_y = 0x20 + 1; // either that or NDS_getADCTouchPosX/Y are wrong.
912
+
913
+	//fw_config->touch_cal[1].adc_x = 0xe00;
914
+	//fw_config->touch_cal[1].adc_y = 0x800;
915
+	//fw_config->touch_cal[1].screen_x = 0xe0 + 1;
916
+	//fw_config->touch_cal[1].screen_y = 0x80 + 1;
917
+}
918
+
919
+/*void NDS_PatchFirmwareMAC()
920
+{
921
+	//memcpy((MMU.fw.data + 0x36), FW_Mac, sizeof(FW_Mac));
922
+	//(*(uint16_t*)(MMU.fw.data + 0x2A)) = calc_CRC16(0, (MMU.fw.data + 0x2C), 0x138);
923
+}*/