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

Last change on this file since 363 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
Line 
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>
11#include <stdio.h>
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;
33 unsigned char scan;
34 unsigned char port;
35 unsigned char baud;
36 } inquire;
37 struct {
38 unsigned char command;
39 unsigned char driveAndHead;
40 unsigned char count;
41 unsigned char scan;
42 unsigned short PackedPortAndBaud;
43 } inquirePacked;
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
58#define ATA_COMMAND_LBA 0x40
59#define ATA_COMMAND_HEADMASK 0xf
60
61#define ATA_DriveAndHead_Drive 0x10
62
63void logBuff( char *message, unsigned long buffoffset, unsigned long readto, int verboseLevel )
64{
65 char logBuff[ 514*9 + 10 ];
66 int logCount;
67
68 if( verboseLevel == 5 || (verboseLevel >= 3 && buffoffset == readto) )
69 {
70 if( verboseLevel == 3 && buffoffset > 11 )
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
80 log( 3, "%s%s", message, logBuff );
81 }
82}
83
84void processRequests( SerialAccess *serial, Image *image0, Image *image1, int timeoutEnabled, int verboseLevel )
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;
98 unsigned long perfTimer;
99 unsigned char lastScan;
100
101 GetTime_Timeout_Local = GetTime_Timeout();
102
103 buffoffset = 0;
104 readto = 0;
105 workCount = workOffset = workCommand = 0;
106 lastScan = 0;
107
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
118 lasttick = GetTime();
119
120 while( (len = serial->readCharacters( &buff.b[buffoffset], (readto ? readto-buffoffset : 1) )) )
121 {
122 buffoffset += len;
123
124 //
125 // For debugging, look at the incoming packet
126 //
127 if( verboseLevel >= 3 )
128 logBuff( " Received: ", buffoffset, readto, verboseLevel );
129
130 if( timeoutEnabled && readto && GetTime() > lasttick + GetTime_Timeout_Local )
131 {
132 log( 1, "Timeout waiting on data from client, aborting previous command" );
133
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 }
161 }
162
163 lasttick = GetTime();
164
165 //
166 // No work currently to do, look at each character as they come in...
167 //
168 if( !readto )
169 {
170 if( (buff.b[0] & SERIAL_COMMAND_HEADERMASK) == SERIAL_COMMAND_HEADER )
171 {
172 //
173 // Found our command header byte to start a commnad sequence, read the next 7 and evaluate
174 //
175 readto = 8;
176 continue;
177 }
178 else
179 {
180 //
181 // Spurious characters, discard
182 //
183 if( verboseLevel >= 2 )
184 {
185 if( buff.b[0] >= 0x20 && buff.b[0] <= 0x7e )
186 log( 2, "Spurious: [%d:%c]", buff.b[0], buff.b[0] );
187 else
188 log( 2, "Spurious: [%d]", buff.b[0] );
189 }
190 buffoffset = 0;
191 continue;
192 }
193 }
194
195 //
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 //
204 if( buffoffset == readto && readto == 514 )
205 {
206 buffoffset = readto = 0;
207 if( (crc = checksum( &buff.w[0], 256 )) != buff.w[256] )
208 {
209 log( 0, "Bad Write Sector Checksum" );
210 continue;
211 }
212
213 if( img->readOnly )
214 {
215 log( 1, "Attempt to write to read-only image" );
216 continue;
217 }
218
219 img->seekSector( mylba + workOffset );
220 img->writeSector( &buff.w[0] );
221
222 //
223 // Echo back the CRC
224 //
225 if( !serial->writeCharacters( &buff.w[256], 2 ) )
226 break;
227
228 workOffset++;
229 workCount--;
230
231 if( workCount )
232 readto = 1; // looking for continuation ACK
233 }
234
235 //
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 {
244 if( verboseLevel > 1 )
245 log( 2, " Continuation: Offset=%u, Checksum=%04x", workOffset-1, buff.w[256] );
246
247 //
248 // Continuation...
249 //
250 if( buff.b[0] != (workCount-0) )
251 {
252 log( 0, "Continue Fault: Received=%d, Expected=%d", buff.b[0], workCount );
253 workCount = 0;
254 continue;
255 }
256 }
257 else
258 {
259 //
260 // New Command...
261 //
262 if( (crc = checksum( &buff.w[0], 3 )) != buff.w[3] )
263 {
264 log( 0, "Bad Command Checksum: %02x %02x %02x %02x %02x %02x %02x %02x, Checksum=%04x",
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
269 img = (buff.inquire.driveAndHead & ATA_DriveAndHead_Drive) ? image1 : image0;
270
271 workCommand = buff.chs.command & SERIAL_COMMAND_RWMASK;
272
273 if( (workCommand != SERIAL_COMMAND_INQUIRE) && (buff.chs.driveAndHead & ATA_COMMAND_LBA) )
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);
285 mylba = img ? (((cyl*img->head + head)*img->sect) + sect-1) : 0;
286 }
287
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,
297 ((unsigned short) buff.inquire.port) << 2,
298 baudRateMatchDivisor( buff.inquire.baud )->display );
299 else if( buff.chs.driveAndHead & ATA_COMMAND_LBA )
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
314 if( (workCommand & SERIAL_COMMAND_WRITE) && img->readOnly )
315 {
316 log( 1, " Write attempt to Read Only disk" );
317 workCount = 0;
318 continue;
319 }
320
321 if( verboseLevel > 0 && workCount > 100 )
322 perfTimer = GetTime();
323 }
324
325 if( workCount && (workCommand == (SERIAL_COMMAND_WRITE | SERIAL_COMMAND_READWRITE)) )
326 {
327 //
328 // Write command... Setup to receive a sector
329 //
330 readto = 514;
331 }
332 else
333 {
334 //
335 // Inquire command...
336 //
337 if( workCommand == SERIAL_COMMAND_INQUIRE )
338 {
339 unsigned char localScan;
340
341 if( serial->speedEmulation &&
342 buff.inquire.baud != serial->baudRate->divisor )
343 {
344 log( 1, " Ignoring Inquire with wrong baud rate" );
345 workCount = 0;
346 continue;
347 }
348
349 localScan = buff.inquire.scan; // need to do this before the call to
350 // img->respondInquire, as it will clear the buff
351 img->respondInquire( &buff.w[0], buff.inquirePacked.PackedPortAndBaud,
352 serial->baudRate,
353 ((unsigned short) buff.inquire.port) << 2,
354 (img == image1 && lastScan) || buff.inquire.scan );
355 lastScan = localScan;
356 }
357 //
358 // Read command...
359 //
360 else
361 {
362 img->seekSector( mylba + workOffset );
363 img->readSector( &buff.w[0] );
364 lastScan = 0;
365 }
366
367 buff.w[256] = checksum( &buff.w[0], 256 );
368
369 if( !serial->writeCharacters( &buff.w[0], 514 ) )
370 break;
371
372 if( verboseLevel >= 3 )
373 logBuff( " Sending: ", 514, 514, verboseLevel );
374
375 workCount--;
376 workOffset++;
377
378 if( workCount )
379 readto = 1; // looking for continuation ACK
380 }
381 }
382
383 if( workCount == 0 && workOffset > 100 )
384 log( 1, " Performance: %.2lf bytes per second", (512.0 * workOffset) / (GetTime() - perfTimer) * 1000.0 );
385 }
386}
387
388
Note: See TracBrowser for help on using the repository browser.