source: xtideuniversalbios/trunk/Serial_Server/library/Process.cpp@ 266

Last change on this file since 266 was 258, checked in by gregli@…, 13 years ago

Added floppy drive emulation over the serial connection (MODULE_SERIAL_FLOPPY). Along the way, various optimizations were made to stay within the 8K ROM size target. Also, serial code now returns the number of sectors transferred.

File size: 9.5 KB
RevLine 
[209]1//======================================================================
2//
3// Project: XTIDE Universal BIOS, Serial Port Server
4//
5// File: process.cpp - Processes commands received over the serial port
6//
7
8#include "library.h"
9#include <memory.h>
10#include <string.h>
[219]11#include <stdio.h>
[209]12
13union _buff {
14 struct {
15 unsigned char command;
16 unsigned char driveAndHead;
17 unsigned char count;
18 unsigned char sector;
19 unsigned short cylinder;
20 } chs;
21 struct {
22 unsigned char command;
23 unsigned char bits24;
24 unsigned char count;
25 unsigned char bits00;
26 unsigned char bits08;
27 unsigned char bits16;
28 } lba;
29 struct {
30 unsigned char command;
31 unsigned char driveAndHead;
32 unsigned char count;
[233]33 unsigned char scan;
34 unsigned char port;
35 unsigned char baud;
[209]36 } inquire;
37 unsigned char b[514];
38 unsigned short w[257];
39} buff;
40
41#define SERIAL_COMMAND_HEADER 0xa0
42
43#define SERIAL_COMMAND_WRITE 1
44#define SERIAL_COMMAND_READWRITE 2
45#define SERIAL_COMMAND_RWMASK 3
46#define SERIAL_COMMAND_INQUIRE 0
47
48#define SERIAL_COMMAND_MASK 0xe3
49#define SERIAL_COMMAND_HEADERMASK 0xe0
50
[219]51#define ATA_COMMAND_LBA 0x40
52#define ATA_COMMAND_HEADMASK 0xf
53
54#define ATA_DriveAndHead_Drive 0x10
55
[210]56void logBuff( char *message, unsigned long buffoffset, unsigned long readto, int verboseLevel )
57{
58 char logBuff[ 514*9 + 10 ];
59 int logCount;
60
[213]61 if( verboseLevel == 5 || (verboseLevel >= 3 && buffoffset == readto) )
[210]62 {
[213]63 if( verboseLevel == 3 && buffoffset > 11 )
[210]64 logCount = 11;
65 else
66 logCount = buffoffset;
67
68 for( int t = 0; t < logCount; t++ )
69 sprintf( &logBuff[t*9], "[%3d:%02x] ", t, buff.b[t] );
70 if( logCount != buffoffset )
71 sprintf( &logBuff[logCount*9], "... " );
72
[211]73 log( 3, "%s%s", message, logBuff );
[210]74 }
75}
76
[219]77void processRequests( SerialAccess *serial, Image *image0, Image *image1, int timeoutEnabled, int verboseLevel )
[209]78{
79 unsigned char workCommand;
80 int workOffset, workCount;
81
82 unsigned long mylba;
83 unsigned long readto;
84 unsigned long buffoffset;
85 unsigned long lasttick;
86 unsigned short crc;
87 unsigned long GetTime_Timeout_Local;
88 unsigned long len;
89 Image *img;
90 unsigned long cyl, sect, head;
[210]91 unsigned long perfTimer;
[233]92 unsigned char lastScan;
[209]93
94 GetTime_Timeout_Local = GetTime_Timeout();
95
96 buffoffset = 0;
97 readto = 0;
98 workCount = workOffset = workCommand = 0;
[233]99 lastScan = 0;
[215]100
[258]101 //
102 // Floppy disks must come after any hard disks
103 //
104 if( (image0 && image0->floppy) && (image1 && !image1->floppy) )
105 {
106 img = image0;
107 image0 = image1;
108 image1 = img;
109 }
110
[209]111 lasttick = GetTime();
112
[215]113 while( (len = serial->readCharacters( &buff.b[buffoffset], (readto ? readto-buffoffset : 1) )) )
[209]114 {
115 buffoffset += len;
116
[210]117 //
118 // For debugging, look at the incoming packet
119 //
[213]120 if( verboseLevel >= 3 )
[210]121 logBuff( " Received: ", buffoffset, readto, verboseLevel );
[209]122
[215]123 if( timeoutEnabled && readto && GetTime() > lasttick + GetTime_Timeout_Local )
124 {
125 log( 1, "Timeout waiting on data from client, aborting previous command" );
[209]126
[215]127 workCount = workOffset = workCommand = 0;
128 readto = 0;
129
130 if( len <= 8 && (buff.b[buffoffset-len] & SERIAL_COMMAND_HEADERMASK) == SERIAL_COMMAND_HEADER )
131 {
132 // assume that we are at the front of a new command
133 //
134 memcpy( &buff.b[0], &buff.b[buffoffset-len], len );
135 buffoffset = len;
136 readto = 8;
137 // fall through to normal processing
138 }
139 else if( len == 1 )
140 {
141 // one new character, treat it like any other new character received, discarding the buffer
142 //
143 buff.b[0] = buff.b[buffoffset-1];
144 buffoffset = 1;
145 // fall through to normal processing
146 }
147 else
148 {
149 // discard even the newly received data and start listening anew
150 //
151 buffoffset = 0;
152 continue;
153 }
[209]154 }
155
156 lasttick = GetTime();
157
[210]158 //
159 // No work currently to do, look at each character as they come in...
160 //
[215]161 if( !readto )
[209]162 {
[215]163 if( (buff.b[0] & SERIAL_COMMAND_HEADERMASK) == SERIAL_COMMAND_HEADER )
[209]164 {
[210]165 //
166 // Found our command header byte to start a commnad sequence, read the next 7 and evaluate
167 //
[209]168 readto = 8;
[210]169 continue;
[209]170 }
171 else
172 {
[210]173 //
174 // Spurious characters, discard
175 //
[209]176 if( verboseLevel >= 2 )
177 {
178 if( buff.b[0] >= 0x20 && buff.b[0] <= 0x7e )
[211]179 log( 2, "Spurious: [%d:%c]", buff.b[0], buff.b[0] );
[209]180 else
[211]181 log( 2, "Spurious: [%d]", buff.b[0] );
[209]182 }
183 buffoffset = 0;
184 continue;
185 }
186 }
187
188 //
[210]189 // Partial packet received, keep reading...
190 //
191 if( readto && buffoffset < readto )
192 continue;
193
194 //
195 // Read 512 bytes from serial port, only one command reads that many characters: Write Sector
196 //
[209]197 if( buffoffset == readto && readto == 514 )
198 {
199 buffoffset = readto = 0;
200 if( (crc = checksum( &buff.w[0], 256 )) != buff.w[256] )
201 {
[211]202 log( 0, "Bad Write Sector Checksum" );
[209]203 continue;
204 }
205
206 if( img->readOnly )
207 {
[211]208 log( 1, "Attempt to write to read-only image" );
[209]209 continue;
210 }
211
212 img->seekSector( mylba + workOffset );
213 img->writeSector( &buff.w[0] );
214
[210]215 //
216 // Echo back the CRC
217 //
[219]218 if( !serial->writeCharacters( &buff.w[256], 2 ) )
219 break;
[209]220
221 workOffset++;
222 workCount--;
[215]223
224 if( workCount )
225 readto = 1; // looking for continuation ACK
[209]226 }
227
[210]228 //
[209]229 // 8 byte command received, or a continuation of the previous command
230 //
231 else if( (buffoffset == readto && readto == 8) ||
232 (buffoffset == readto && readto == 1 && workCount) )
233 {
234 buffoffset = readto = 0;
235 if( workCount )
236 {
[215]237 if( verboseLevel > 1 )
238 log( 2, " Continuation: Offset=%u, Checksum=%04x", workOffset-1, buff.w[256] );
239
[210]240 //
241 // Continuation...
242 //
[209]243 if( buff.b[0] != (workCount-0) )
244 {
[211]245 log( 0, "Continue Fault: Received=%d, Expected=%d", buff.b[0], workCount );
[209]246 workCount = 0;
247 continue;
248 }
249 }
250 else
251 {
[210]252 //
253 // New Command...
254 //
[209]255 if( (crc = checksum( &buff.w[0], 3 )) != buff.w[3] )
256 {
[215]257 log( 0, "Bad Command Checksum: %02x %02x %02x %02x %02x %02x %02x %02x, Checksum=%04x",
[209]258 buff.b[0], buff.b[1], buff.b[2], buff.b[3], buff.b[4], buff.b[5], buff.b[6], buff.b[7], crc);
259 continue;
260 }
261
[215]262 img = (buff.inquire.driveAndHead & ATA_DriveAndHead_Drive) ? image1 : image0;
[209]263
264 workCommand = buff.chs.command & SERIAL_COMMAND_RWMASK;
265
[217]266 if( (workCommand != SERIAL_COMMAND_INQUIRE) && (buff.chs.driveAndHead & ATA_COMMAND_LBA) )
[209]267 {
268 mylba = ((((unsigned long) buff.lba.bits24) & ATA_COMMAND_HEADMASK) << 24)
269 | (((unsigned long) buff.lba.bits16) << 16)
270 | (((unsigned long) buff.lba.bits08) << 8)
271 | ((unsigned long) buff.lba.bits00);
272 }
273 else
274 {
275 cyl = buff.chs.cylinder;
276 sect = buff.chs.sector;
277 head = (buff.chs.driveAndHead & ATA_COMMAND_HEADMASK);
[215]278 mylba = img ? (((cyl*img->head + head)*img->sect) + sect-1) : 0;
[209]279 }
280
[215]281 workOffset = 0;
282 workCount = buff.chs.count;
283
284 if( verboseLevel > 0 )
285 {
286 char *comStr = (workCommand & SERIAL_COMMAND_WRITE ? "Write" : "Read");
287
288 if( workCommand == SERIAL_COMMAND_INQUIRE )
289 log( 1, "Inquire %d: Client Port=0x%x, Client Baud=%s", img == image0 ? 0 : 1,
[233]290 ((unsigned short) buff.inquire.port) << 2,
291 baudRateMatchDivisor( buff.inquire.baud )->display );
[217]292 else if( buff.chs.driveAndHead & ATA_COMMAND_LBA )
[215]293 log( 1, "%s %d: LBA=%u, Count=%u", comStr, img == image0 ? 0 : 1,
294 mylba, workCount );
295 else
296 log( 1, "%s %d: Cylinder=%u, Sector=%u, Head=%u, Count=%u, LBA=%u", comStr, img == image0 ? 0 : 1,
297 cyl, sect, head, workCount, mylba );
298 }
299
300 if( !img )
301 {
302 log( 1, " No slave drive provided" );
303 workCount = 0;
304 continue;
305 }
306
[209]307 if( (workCommand & SERIAL_COMMAND_WRITE) && img->readOnly )
308 {
[215]309 log( 1, " Write attempt to Read Only disk" );
310 workCount = 0;
[209]311 continue;
312 }
313
[213]314 if( verboseLevel > 0 && workCount > 100 )
[210]315 perfTimer = GetTime();
[209]316 }
317
318 if( workCount && (workCommand == (SERIAL_COMMAND_WRITE | SERIAL_COMMAND_READWRITE)) )
319 {
[210]320 //
321 // Write command... Setup to receive a sector
322 //
[209]323 readto = 514;
324 }
325 else
326 {
[210]327 //
328 // Inquire command...
329 //
[209]330 if( workCommand == SERIAL_COMMAND_INQUIRE )
331 {
[233]332 unsigned char localScan;
333
[209]334 if( serial->speedEmulation &&
[233]335 buff.inquire.baud != serial->baudRate->divisor )
[209]336 {
[215]337 log( 1, " Ignoring Inquire with wrong baud rate" );
[209]338 workCount = 0;
339 continue;
340 }
341
[233]342 localScan = buff.inquire.scan; // need to do this before the call to
343 // img->respondInquire, as it will clear the buff
344 img->respondInquire( &buff.w[0], serial->baudRate,
345 ((unsigned short) buff.inquire.port) << 2,
346 (img == image1 && lastScan) || buff.inquire.scan );
347 lastScan = localScan;
[209]348 }
[210]349 //
350 // Read command...
351 //
[209]352 else
353 {
354 img->seekSector( mylba + workOffset );
355 img->readSector( &buff.w[0] );
[233]356 lastScan = 0;
[209]357 }
358
359 buff.w[256] = checksum( &buff.w[0], 256 );
360
[219]361 if( !serial->writeCharacters( &buff.w[0], 514 ) )
362 break;
[209]363
[215]364 if( verboseLevel >= 3 )
365 logBuff( " Sending: ", 514, 514, verboseLevel );
366
[209]367 workCount--;
368 workOffset++;
[215]369
370 if( workCount )
371 readto = 1; // looking for continuation ACK
[209]372 }
[210]373 }
[209]374
[215]375 if( workCount == 0 && workOffset > 100 )
376 log( 1, " Performance: %.2lf bytes per second", (512.0 * workOffset) / (GetTime() - perfTimer) * 1000.0 );
[209]377 }
378}
[210]379
380
Note: See TracBrowser for help on using the repository browser.