summaryrefslogtreecommitdiff
path: root/usb/DataManager.c
blob: c40e36b37761f24588a3f09ccece7184623f05ec (plain)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include <LUFA/Drivers/Peripheral/Serial.h>
#include "SCSI.h"

typedef PROGMEM struct _FAT_BOOT_RECORD
{
		uint8_t		bootstrap[3];			//eb 3c 90
		uint8_t		OEM[8];
		uint16_t	iBytesPerSector;		//512
		uint8_t		iSectorsPerCluster;		//1
		uint16_t	iReservedSectors;		//1
		uint8_t		iFATs;					//2
		uint16_t	iRootEntries;
		uint16_t 	iTotalSectors;
		uint8_t		iMediaDescr;
		uint16_t	iSectorsPerFAT;
		uint16_t	iSectorsPerTrack;
		uint16_t	iHeads;
		uint32_t	iHiddenSectors;
		uint32_t	iTotalSectorsEx;
		uint16_t	iLogicDriveNumber;
		uint8_t		extSignature;			//29 (hex)
		uint32_t	serialNumber;
		uint8_t		volumeLabel[11];
		uint8_t		fatName[8];				//FAT16
		//uint8_t		exeCode[448];
		//uint8_t		exeEndMarker[2];		//55 aa
} FAT_BOOT_RECORD; //total 512 bytes

//Boot Record: 1 sector; FAT1: 1 sector; FAT2: 1 sector; Root Directory: 1 sector
FAT_BOOT_RECORD PROGMEM fatBootData = 
{
		.bootstrap          = {0xeb, 0x3c, 0x90},
		.OEM				= "OpenPCR ",
		.iBytesPerSector 	= 512,
		.iSectorsPerCluster = 1,
		.iReservedSectors	= 1,
		.iFATs				= 2,
		.iRootEntries		= 512,
		.iTotalSectors		= 4352,
		.iMediaDescr		= 0xf0,
		.iSectorsPerFAT		= 17,
		.iSectorsPerTrack	= 0,
		.iHeads				= 0,
		.iHiddenSectors		= 0,
		.iTotalSectorsEx	= 0,
		.iLogicDriveNumber	= 0x00, //0x80,
		.extSignature		= 0x29,
		.serialNumber		= USE_INTERNAL_SERIAL,
		.volumeLabel        = "OPENPCR    ",
		.fatName			= "FAT16   ",
	//	.exeCode			= {},
	//	.exeEndMarker		= {0x55, 0xaa}
};

 typedef PROGMEM struct
{
		uint8_t		filename[8];
		uint8_t		ext[3];
		uint8_t		attribute;
		uint8_t		reserved;
		uint8_t		create_time_ms;
		uint16_t	create_time;
		uint16_t	create_date;
		uint16_t	access_date;
		uint16_t	first_cluster_highorder;
		uint16_t	modified_time;
		uint16_t	modified_date;
		uint16_t	first_cluster_loworder;
		uint32_t	size;
} FAT_ROOT_DIRECTORY; //total 512 bytes

/*date format: bits 0-4: day of month 1-31; bits 5-8: month of year 1-12; bits 9-15: years since 1980 0-127
  time format: bits 0-4: 2 second count 0-29; bits 5-10: minutes 0-59; bits 11-15: hours 0-23  */
FAT_ROOT_DIRECTORY PROGMEM volumeLabel = 
{
		.filename          			= "OPENPCR ",
		.ext						= "   ",
		.attribute 					= 0x08,
		.reserved 					= 0,
		.create_time_ms				= 0,
		.create_time				= 0,
		.create_date				= 0,
		.access_date				= 0,
		.first_cluster_highorder	= 0,
		.modified_time				= 0,
		.modified_date				= 0,
		.first_cluster_loworder		= 0,
		.size						= 0
};

FAT_ROOT_DIRECTORY PROGMEM fileName = 
{
		.filename          			= "STATUS  ",
		.ext						= "TXT",
		.attribute 					= 0,
		.reserved 					= 0,
		.create_time_ms				= 0,
		.create_time				= 0x8800,
		.create_date				= 0x3ea1,
		.access_date				= 0x3ea1,
		.first_cluster_highorder	= 0,
		.modified_time				= 0x8800,
		.modified_date				= 0x3ea1,
		.first_cluster_loworder		= 2,
		.size						= 300
};

FAT_ROOT_DIRECTORY PROGMEM autorun = 
{
		.filename          			= "AUTORUN ",
		.ext						= "INF",
		.attribute 					= 0,
		.reserved 					= 0,
		.create_time_ms				= 0,
		.create_time				= 0x8800,
		.create_date				= 0x3ea1,
		.access_date				= 0x3ea1,
		.first_cluster_highorder	= 0,
		.modified_time				= 0x8800,
		.modified_date				= 0x3ea1,
		.first_cluster_loworder		= 3,
		.size						= 32
};

char PROGMEM autorun_content[] = "[autorun]\r\n";
uint8_t autorun_content_length = 11;

#define START_CODE				0xFF
#define PACKET_HEADER_LENGTH	4

typedef enum{
	SEND_CMD		= 0x10,
	STATUS_REQ		= 0x40,
	STATUS_RESP		= 0x80	
}PACKET_TYPE;

#define FILE_SIGNATURE		"s=ACGTC"
#define FILE_SIGNATURE_LEN	7
#define FILE_MAX_LENGTH		252

bool DoReadFlowControl() {
	/* Check if the endpoint is currently full */
	if (!(Endpoint_IsReadWriteAllowed()))
	{
		/* Clear the endpoint bank to send its contents to the host */
		Endpoint_ClearIN();

		/* Wait until the endpoint is ready for more data */
		if (Endpoint_WaitUntilReady())
		  return false;
	}

	return true;
}

bool DoWriteFlowControl() {
	/* Check if the endpoint is currently full */
	if (!(Endpoint_IsReadWriteAllowed()))
	{
		/* Clear the endpoint bank to send its contents to the host */
		Endpoint_ClearOUT();

		/* Wait until the endpoint is ready for more data */
		if (Endpoint_WaitUntilReady())
		  return false;
	}

	return true;
}

bool SerialWaitUntilReady(){
	//timeout in 50000*60/16M (clock frequency) = 0.1875 secs
	uint16_t timeout = 60;
	
	TCNT1 = 0;
	while(timeout){
		if (Serial_IsCharReceived())
			return true;
		if (TCNT1 >= 50000){ 
			TCNT1 = 0;
			timeout--;
		}
	}

	return false;
}

/*
void Endpoint_Write_Zeros(uint16_t size){
	uint16_t block_index = 0;

	while(block_index < size){
		DoReadFlowControl();
		for(uint16_t blockdiv16_index = 0; blockdiv16_index < 1; blockdiv16_index++) {
			Endpoint_Write_Byte(0x00);
			block_index++;
			if (block_index >= size)
				return;
		}
	}
}
*/

void FlushBlock(uint16_t size)
{
	uint16_t block_index;
	for (block_index=0; block_index<size; block_index++){
		DoWriteFlowControl();
		Endpoint_Read_Byte();
	}
}

bool DataManager_ReadBlocks(uint32_t BlockAddress, uint16_t TotalBlocks)
{
	uint16_t block_index = 0;

	while (TotalBlocks){
		if (BlockAddress == 0){ //BOOT Record
			for (block_index=0;block_index<62; block_index++){
				DoReadFlowControl();
				Endpoint_Write_Byte(pgm_read_byte(((char*)&fatBootData)+block_index));
			}
			for (block_index=0; block_index<448; block_index++){
				DoReadFlowControl();
				Endpoint_Write_Byte(0x00);
			}
			DoReadFlowControl();
			Endpoint_Write_Byte(0x55);
			Endpoint_Write_Byte(0xaa);				
		}
		else if (BlockAddress == 1 || BlockAddress == 18){ //FAT TABLE 1 and 2 (15 blocks)
			//first two sectors are not used
			DoReadFlowControl();
			Endpoint_Write_Byte(0xf0);
			Endpoint_Write_Byte(0xff);
			Endpoint_Write_Byte(0xff);
			Endpoint_Write_Byte(0xff);
			//sector 2 where data start
			Endpoint_Write_Byte(0xff);
			Endpoint_Write_Byte(0xff);
			//sector 3
			Endpoint_Write_Byte(0xff);
			Endpoint_Write_Byte(0xff);
			
			for (block_index=8; block_index<VIRTUAL_MEMORY_BLOCK_SIZE; block_index++){
				DoReadFlowControl();
				Endpoint_Write_Byte(0x00);
			}
		}
		else if (BlockAddress == 35){	//Root Directory
			for (block_index=0;block_index<32; block_index++){
				DoReadFlowControl();
				Endpoint_Write_Byte(pgm_read_byte(((char*)&volumeLabel)+block_index));
			}
			for (block_index=0;block_index<32; block_index++){
				DoReadFlowControl();
				Endpoint_Write_Byte(pgm_read_byte(((char*)&fileName)+block_index));
			}
			for (block_index=0;block_index<32; block_index++){
				DoReadFlowControl();
				Endpoint_Write_Byte(pgm_read_byte(((char*)&autorun)+block_index));
			}
			for (block_index=96; block_index<VIRTUAL_MEMORY_BLOCK_SIZE; block_index++){
				DoReadFlowControl();
				Endpoint_Write_Byte(0x00);
			}
		}
		else if (BlockAddress == 67){	//STATUS.TXT
			Serial_TxByte(START_CODE);
			Serial_TxByte(PACKET_HEADER_LENGTH);
			Serial_TxByte(0);
			Serial_TxByte(STATUS_REQ);

			uint16_t length = 0;
			uint8_t  packet_type;
			bool success = true;
			while(true){
				while ((success = SerialWaitUntilReady()) && Serial_RxByte() != START_CODE);
				if (!success) break;
				if (!(success = SerialWaitUntilReady())) break;
				length = Serial_RxByte();
				if (!(success = SerialWaitUntilReady())) break;
				length |= (Serial_RxByte() << 8);
				if (length > PACKET_HEADER_LENGTH){
					length -= PACKET_HEADER_LENGTH;
					if (!(success = SerialWaitUntilReady())) break;
					packet_type = Serial_RxByte();
					if (packet_type == STATUS_RESP)
						break;
				}
			}

			block_index = 0;
			if (success){
				for (; block_index<length; block_index++){
					if (!SerialWaitUntilReady()) break;
					DoReadFlowControl();
					Endpoint_Write_Byte(Serial_RxByte());
				}
			}

			for (; block_index<VIRTUAL_MEMORY_BLOCK_SIZE; block_index++){
				DoReadFlowControl();
				Endpoint_Write_Byte(0x00);
			}
		}
		else if (BlockAddress == 68){	//AUTORUN.INF
			for (block_index=0; block_index<autorun_content_length; block_index++){
				DoReadFlowControl();
				Endpoint_Write_Byte(pgm_read_byte(autorun_content+block_index));
			}
			for (; block_index<VIRTUAL_MEMORY_BLOCK_SIZE; block_index++){
				DoReadFlowControl();
				Endpoint_Write_Byte(0x00);
			}
		}
		else{
			for (block_index=0; block_index<VIRTUAL_MEMORY_BLOCK_SIZE; block_index++){
				DoReadFlowControl();
				Endpoint_Write_Byte(0x00);
			}
		}
	
		BlockAddress++;
		TotalBlocks--;
	}

	if (!(Endpoint_IsReadWriteAllowed()))
		Endpoint_ClearIN();

	return true;
}

bool DataManager_WriteBlocks(uint32_t BlockAddress, uint16_t TotalBlocks)
{
	uint16_t block_index;
	bool bSignatureFound = false;
	while(TotalBlocks){
		if (BlockAddress > 67 && bSignatureFound == false){ //in data sector
			bSignatureFound = true;
			for (block_index=0; block_index<FILE_SIGNATURE_LEN; block_index++){
				DoWriteFlowControl();
				if (Endpoint_Read_Byte() != FILE_SIGNATURE[block_index]){
					FlushBlock(VIRTUAL_MEMORY_BLOCK_SIZE-block_index-1);
					bSignatureFound = false;
					break;
				}
			}
			if (bSignatureFound){
				uint16_t length = PACKET_HEADER_LENGTH+FILE_MAX_LENGTH;
				Serial_TxByte(START_CODE);
				Serial_TxByte(length & 0xff);
				Serial_TxByte((length & 0xff00) >> 8);
				Serial_TxByte(SEND_CMD);
				for (block_index=0; block_index<FILE_MAX_LENGTH; block_index++){
					DoWriteFlowControl();
					Serial_TxByte(Endpoint_Read_Byte());
				}
				FlushBlock(VIRTUAL_MEMORY_BLOCK_SIZE-FILE_MAX_LENGTH-FILE_SIGNATURE_LEN);
			}
		}
		else{
			FlushBlock(VIRTUAL_MEMORY_BLOCK_SIZE);
		}
		BlockAddress++;
		TotalBlocks--;
	}

	if (!(Endpoint_IsReadWriteAllowed()))
		Endpoint_ClearOUT();

	return true;
}