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

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

Moved the bulk of the serial code to the assembly library, for inclusion in other utilities. Fixed a bug in int13h.asm when floppy support was not enabled that was preventing foreign drives from working properly.

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