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

Last change on this file since 258 was 258, checked in by gregli@…, 12 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
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    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
51#define ATA_COMMAND_LBA 0x40
52#define ATA_COMMAND_HEADMASK 0xf
53
54#define ATA_DriveAndHead_Drive 0x10
55
56void logBuff( char *message, unsigned long buffoffset, unsigned long readto, int verboseLevel )
57{
58    char logBuff[ 514*9 + 10 ];
59    int logCount;
60
61    if( verboseLevel == 5 || (verboseLevel >= 3 && buffoffset == readto) )
62    {
63        if( verboseLevel == 3 && buffoffset > 11 )
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
73        log( 3, "%s%s", message, logBuff );
74    }
75}
76
77void processRequests( SerialAccess *serial, Image *image0, Image *image1, int timeoutEnabled, int verboseLevel )
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;
91    unsigned long perfTimer;
92    unsigned char lastScan;
93
94    GetTime_Timeout_Local = GetTime_Timeout();
95
96    buffoffset = 0;
97    readto = 0;
98    workCount = workOffset = workCommand = 0;
99    lastScan = 0;
100
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
111    lasttick = GetTime();
112
113    while( (len = serial->readCharacters( &buff.b[buffoffset], (readto ? readto-buffoffset : 1) )) )
114    {
115        buffoffset += len;
116
117        //
118        // For debugging, look at the incoming packet
119        //
120        if( verboseLevel >= 3 )
121            logBuff( "    Received: ", buffoffset, readto, verboseLevel );
122
123        if( timeoutEnabled && readto && GetTime() > lasttick + GetTime_Timeout_Local )
124        {
125            log( 1, "Timeout waiting on data from client, aborting previous command" );
126
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            }
154        }
155
156        lasttick = GetTime();
157
158        //
159        // No work currently to do, look at each character as they come in...
160        //
161        if( !readto )
162        {
163            if( (buff.b[0] & SERIAL_COMMAND_HEADERMASK) == SERIAL_COMMAND_HEADER )
164            {
165                //
166                // Found our command header byte to start a commnad sequence, read the next 7 and evaluate
167                //
168                readto = 8;
169                continue;
170            }
171            else
172            {
173                //
174                // Spurious characters, discard
175                //
176                if( verboseLevel >= 2 )
177                {
178                    if( buff.b[0] >= 0x20 && buff.b[0] <= 0x7e )
179                        log( 2, "Spurious: [%d:%c]", buff.b[0], buff.b[0] );
180                    else
181                        log( 2, "Spurious: [%d]", buff.b[0] );
182                }
183                buffoffset = 0;
184                continue;
185            }
186        }
187
188        //
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        //
197        if( buffoffset == readto && readto == 514 )
198        {
199            buffoffset = readto = 0;
200            if( (crc = checksum( &buff.w[0], 256 )) != buff.w[256] )
201            {
202                log( 0, "Bad Write Sector Checksum" );
203                continue;
204            }
205
206            if( img->readOnly )
207            {
208                log( 1, "Attempt to write to read-only image" );
209                continue;
210            }
211
212            img->seekSector( mylba + workOffset );
213            img->writeSector( &buff.w[0] );
214
215            //
216            // Echo back the CRC
217            //
218            if( !serial->writeCharacters( &buff.w[256], 2 ) )
219                break;
220
221            workOffset++;
222            workCount--;
223
224            if( workCount )
225                readto = 1;           // looking for continuation ACK
226        }
227
228        //
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            {
237                if( verboseLevel > 1 )
238                    log( 2, "    Continuation: Offset=%u, Checksum=%04x", workOffset-1, buff.w[256] );
239
240                //
241                // Continuation...
242                //
243                if( buff.b[0] != (workCount-0) )
244                {
245                    log( 0, "Continue Fault: Received=%d, Expected=%d", buff.b[0], workCount );
246                    workCount = 0;
247                    continue;
248                }
249            }
250            else
251            {
252                //
253                // New Command...
254                //
255                if( (crc = checksum( &buff.w[0], 3 )) != buff.w[3] )
256                {
257                    log( 0, "Bad Command Checksum: %02x %02x %02x %02x %02x %02x %02x %02x, Checksum=%04x",
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
262                img = (buff.inquire.driveAndHead & ATA_DriveAndHead_Drive) ? image1 : image0;
263
264                workCommand = buff.chs.command & SERIAL_COMMAND_RWMASK;
265
266                if( (workCommand != SERIAL_COMMAND_INQUIRE) && (buff.chs.driveAndHead & ATA_COMMAND_LBA) )
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);
278                    mylba = img ? (((cyl*img->head + head)*img->sect) + sect-1) : 0;
279                }
280
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,
290                             ((unsigned short) buff.inquire.port) << 2,
291                             baudRateMatchDivisor( buff.inquire.baud )->display );
292                    else if( buff.chs.driveAndHead & ATA_COMMAND_LBA )
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
307                if( (workCommand & SERIAL_COMMAND_WRITE) && img->readOnly )
308                {
309                    log( 1, "    Write attempt to Read Only disk" );
310                    workCount = 0;
311                    continue;
312                }
313
314                if( verboseLevel > 0 && workCount > 100 )
315                    perfTimer = GetTime();
316            }
317
318            if( workCount && (workCommand == (SERIAL_COMMAND_WRITE | SERIAL_COMMAND_READWRITE)) )
319            {
320                //
321                // Write command...   Setup to receive a sector
322                //
323                readto = 514;
324            }
325            else 
326            {
327                //
328                // Inquire command...
329                //
330                if( workCommand == SERIAL_COMMAND_INQUIRE )
331                {
332                    unsigned char localScan;
333
334                    if( serial->speedEmulation && 
335                        buff.inquire.baud != serial->baudRate->divisor )
336                    {
337                        log( 1, "    Ignoring Inquire with wrong baud rate" );
338                        workCount = 0;
339                        continue;
340                    }
341
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;
348                }
349                //
350                // Read command...
351                //
352                else
353                {
354                    img->seekSector( mylba + workOffset );
355                    img->readSector( &buff.w[0] );
356                    lastScan = 0;
357                }
358
359                buff.w[256] = checksum( &buff.w[0], 256 );
360
361                if( !serial->writeCharacters( &buff.w[0], 514 ) )
362                    break;
363
364                if( verboseLevel >= 3 )
365                    logBuff( "    Sending: ", 514, 514, verboseLevel );
366
367                workCount--;
368                workOffset++;
369
370                if( workCount )
371                    readto = 1;           // looking for continuation ACK
372            }
373        }
374
375        if( workCount == 0 && workOffset > 100 )
376            log( 1, "    Performance: %.2lf bytes per second", (512.0 * workOffset) / (GetTime() - perfTimer) * 1000.0 );
377    }
378}
379
380
Note: See TracBrowser for help on using the repository browser.