[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>
|
---|
| 11 |
|
---|
| 12 | union _buff {
|
---|
| 13 | struct {
|
---|
| 14 | unsigned char command;
|
---|
| 15 | unsigned char driveAndHead;
|
---|
| 16 | unsigned char count;
|
---|
| 17 | unsigned char sector;
|
---|
| 18 | unsigned short cylinder;
|
---|
| 19 | } chs;
|
---|
| 20 | struct {
|
---|
| 21 | unsigned char command;
|
---|
| 22 | unsigned char bits24;
|
---|
| 23 | unsigned char count;
|
---|
| 24 | unsigned char bits00;
|
---|
| 25 | unsigned char bits08;
|
---|
| 26 | unsigned char bits16;
|
---|
| 27 | } lba;
|
---|
| 28 | struct {
|
---|
| 29 | unsigned char command;
|
---|
| 30 | unsigned char driveAndHead;
|
---|
| 31 | unsigned char count;
|
---|
| 32 | unsigned char undefined1;
|
---|
| 33 | unsigned char portAndBaud;
|
---|
| 34 | unsigned char undefined2;
|
---|
| 35 | } inquire;
|
---|
| 36 | unsigned char b[514];
|
---|
| 37 | unsigned short w[257];
|
---|
| 38 | } buff;
|
---|
| 39 |
|
---|
| 40 | #define SERIAL_COMMAND_HEADER 0xa0
|
---|
| 41 |
|
---|
| 42 | #define SERIAL_COMMAND_WRITE 1
|
---|
| 43 | #define SERIAL_COMMAND_READWRITE 2
|
---|
| 44 | #define SERIAL_COMMAND_RWMASK 3
|
---|
| 45 | #define SERIAL_COMMAND_INQUIRE 0
|
---|
| 46 |
|
---|
| 47 | #define SERIAL_COMMAND_MASK 0xe3
|
---|
| 48 | #define SERIAL_COMMAND_HEADERMASK 0xe0
|
---|
| 49 |
|
---|
| 50 | #define SERIAL_INQUIRE_PORTANDBAUD_BAUD 3
|
---|
| 51 | #define SERIAL_INQUIRE_PORTANDBAUD_PORT 0xfc
|
---|
| 52 |
|
---|
[210] | 53 | void logBuff( char *message, unsigned long buffoffset, unsigned long readto, int verboseLevel )
|
---|
| 54 | {
|
---|
| 55 | char logBuff[ 514*9 + 10 ];
|
---|
| 56 | int logCount;
|
---|
| 57 |
|
---|
| 58 | if( verboseLevel == 6 || (verboseLevel >= 4 && buffoffset == readto) )
|
---|
| 59 | {
|
---|
| 60 | if( verboseLevel == 4 && buffoffset > 11 )
|
---|
| 61 | logCount = 11;
|
---|
| 62 | else
|
---|
| 63 | logCount = buffoffset;
|
---|
| 64 |
|
---|
| 65 | for( int t = 0; t < logCount; t++ )
|
---|
| 66 | sprintf( &logBuff[t*9], "[%3d:%02x] ", t, buff.b[t] );
|
---|
| 67 | if( logCount != buffoffset )
|
---|
| 68 | sprintf( &logBuff[logCount*9], "... " );
|
---|
| 69 |
|
---|
[211] | 70 | log( 3, "%s%s", message, logBuff );
|
---|
[210] | 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[209] | 74 | void processRequests( Serial *serial, Image *image0, Image *image1, int timeoutEnabled, int verboseLevel )
|
---|
| 75 | {
|
---|
| 76 | unsigned char workCommand;
|
---|
| 77 | int workOffset, workCount;
|
---|
| 78 |
|
---|
[210] | 79 | int vtype = 0;
|
---|
[209] | 80 |
|
---|
| 81 | unsigned long mylba;
|
---|
| 82 | unsigned long readto;
|
---|
| 83 | unsigned long buffoffset;
|
---|
| 84 | int timeout;
|
---|
| 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;
|
---|
[209] | 92 |
|
---|
| 93 | GetTime_Timeout_Local = GetTime_Timeout();
|
---|
| 94 |
|
---|
| 95 | buffoffset = 0;
|
---|
| 96 | readto = 0;
|
---|
| 97 | timeout = 0;
|
---|
| 98 | workCount = workOffset = workCommand = 0;
|
---|
| 99 | lasttick = GetTime();
|
---|
| 100 |
|
---|
| 101 | while( timeout || (len = serial->readCharacters( &buff.b[buffoffset], (readto ? readto-buffoffset : 1) )) )
|
---|
| 102 | {
|
---|
| 103 | buffoffset += len;
|
---|
| 104 |
|
---|
[210] | 105 | //
|
---|
| 106 | // For debugging, look at the incoming packet
|
---|
| 107 | //
|
---|
[209] | 108 | if( verboseLevel >= 4 )
|
---|
[210] | 109 | logBuff( " Received: ", buffoffset, readto, verboseLevel );
|
---|
[209] | 110 |
|
---|
| 111 | timeout = 0;
|
---|
| 112 |
|
---|
| 113 | if( buffoffset != 1 && (timeoutEnabled && GetTime() > lasttick + GetTime_Timeout_Local) )
|
---|
| 114 | {
|
---|
| 115 | timeout = 1;
|
---|
| 116 | buff.b[0] = buff.b[buffoffset];
|
---|
| 117 | buffoffset = 0;
|
---|
| 118 | len = 1;
|
---|
| 119 | workCount = 0;
|
---|
[211] | 120 | log( 1, "Timeout waiting on command" );
|
---|
[209] | 121 | continue;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | lasttick = GetTime();
|
---|
| 125 |
|
---|
[210] | 126 | //
|
---|
| 127 | // No work currently to do, look at each character as they come in...
|
---|
| 128 | //
|
---|
[209] | 129 | if( buffoffset == 1 && !readto )
|
---|
| 130 | {
|
---|
| 131 | if( workCount )
|
---|
| 132 | {
|
---|
| 133 | readto = 1;
|
---|
| 134 | }
|
---|
| 135 | else if( (buff.b[0] & SERIAL_COMMAND_HEADERMASK) == SERIAL_COMMAND_HEADER )
|
---|
| 136 | {
|
---|
[210] | 137 | //
|
---|
| 138 | // Found our command header byte to start a commnad sequence, read the next 7 and evaluate
|
---|
| 139 | //
|
---|
[209] | 140 | readto = 8;
|
---|
[210] | 141 | continue;
|
---|
[209] | 142 | }
|
---|
| 143 | else
|
---|
| 144 | {
|
---|
[210] | 145 | //
|
---|
| 146 | // Spurious characters, discard
|
---|
| 147 | //
|
---|
[209] | 148 | if( verboseLevel >= 2 )
|
---|
| 149 | {
|
---|
| 150 | if( buff.b[0] >= 0x20 && buff.b[0] <= 0x7e )
|
---|
[211] | 151 | log( 2, "Spurious: [%d:%c]", buff.b[0], buff.b[0] );
|
---|
[209] | 152 | else
|
---|
[211] | 153 | log( 2, "Spurious: [%d]", buff.b[0] );
|
---|
[209] | 154 | }
|
---|
| 155 | buffoffset = 0;
|
---|
| 156 | continue;
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | //
|
---|
[210] | 161 | // Partial packet received, keep reading...
|
---|
| 162 | //
|
---|
| 163 | if( readto && buffoffset < readto )
|
---|
| 164 | continue;
|
---|
| 165 |
|
---|
| 166 | //
|
---|
| 167 | // Read 512 bytes from serial port, only one command reads that many characters: Write Sector
|
---|
| 168 | //
|
---|
[209] | 169 | if( buffoffset == readto && readto == 514 )
|
---|
| 170 | {
|
---|
| 171 | buffoffset = readto = 0;
|
---|
| 172 | if( (crc = checksum( &buff.w[0], 256 )) != buff.w[256] )
|
---|
| 173 | {
|
---|
[211] | 174 | log( 0, "Bad Write Sector Checksum" );
|
---|
[209] | 175 | continue;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | if( img->readOnly )
|
---|
| 179 | {
|
---|
[211] | 180 | log( 1, "Attempt to write to read-only image" );
|
---|
[209] | 181 | continue;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | img->seekSector( mylba + workOffset );
|
---|
| 185 | img->writeSector( &buff.w[0] );
|
---|
| 186 |
|
---|
[210] | 187 | //
|
---|
| 188 | // Echo back the CRC
|
---|
| 189 | //
|
---|
[209] | 190 | if( serial->writeCharacters( &buff.w[256], 2 ) != 2 )
|
---|
[211] | 191 | log( 0, "Serial Port Write Error" );
|
---|
[209] | 192 |
|
---|
| 193 | workOffset++;
|
---|
| 194 | workCount--;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[210] | 197 | //
|
---|
[209] | 198 | // 8 byte command received, or a continuation of the previous command
|
---|
| 199 | //
|
---|
| 200 | else if( (buffoffset == readto && readto == 8) ||
|
---|
| 201 | (buffoffset == readto && readto == 1 && workCount) )
|
---|
| 202 | {
|
---|
| 203 | buffoffset = readto = 0;
|
---|
| 204 | if( workCount )
|
---|
| 205 | {
|
---|
[210] | 206 | //
|
---|
| 207 | // Continuation...
|
---|
| 208 | //
|
---|
[209] | 209 | if( buff.b[0] != (workCount-0) )
|
---|
| 210 | {
|
---|
[211] | 211 | log( 0, "Continue Fault: Received=%d, Expected=%d", buff.b[0], workCount );
|
---|
[209] | 212 | workCount = 0;
|
---|
| 213 | continue;
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
| 216 | else
|
---|
| 217 | {
|
---|
[210] | 218 | //
|
---|
| 219 | // New Command...
|
---|
| 220 | //
|
---|
[209] | 221 | if( (crc = checksum( &buff.w[0], 3 )) != buff.w[3] )
|
---|
| 222 | {
|
---|
[211] | 223 | log( 0, "Bad Command Checksum: %02x %02x %02x %02x %02x %02x %02x %02x, Checksum=%02x",
|
---|
[209] | 224 | 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);
|
---|
| 225 | continue;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | if( (buff.inquire.driveAndHead & ATA_DriveAndHead_Drive) )
|
---|
| 229 | {
|
---|
| 230 | if( !image1 )
|
---|
| 231 | {
|
---|
[211] | 232 | log( 1, "Slave drive selected when not supplied" );
|
---|
[210] | 233 | img = NULL;
|
---|
[209] | 234 | continue;
|
---|
| 235 | }
|
---|
[210] | 236 | else
|
---|
| 237 | img = image1;
|
---|
[209] | 238 | }
|
---|
| 239 | else
|
---|
| 240 | img = image0;
|
---|
| 241 |
|
---|
| 242 | workCommand = buff.chs.command & SERIAL_COMMAND_RWMASK;
|
---|
| 243 |
|
---|
| 244 | if( (workCommand != SERIAL_COMMAND_INQUIRE) && (buff.chs.command & ATA_COMMAND_LBA) )
|
---|
| 245 | {
|
---|
| 246 | mylba = ((((unsigned long) buff.lba.bits24) & ATA_COMMAND_HEADMASK) << 24)
|
---|
| 247 | | (((unsigned long) buff.lba.bits16) << 16)
|
---|
| 248 | | (((unsigned long) buff.lba.bits08) << 8)
|
---|
| 249 | | ((unsigned long) buff.lba.bits00);
|
---|
| 250 | vtype = 1;
|
---|
| 251 | }
|
---|
| 252 | else
|
---|
| 253 | {
|
---|
| 254 | cyl = buff.chs.cylinder;
|
---|
| 255 | sect = buff.chs.sector;
|
---|
| 256 | head = (buff.chs.driveAndHead & ATA_COMMAND_HEADMASK);
|
---|
| 257 | mylba = (((cyl*img->head + head)*img->sect) + sect-1);
|
---|
| 258 | vtype = 2;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | if( (workCommand & SERIAL_COMMAND_WRITE) && img->readOnly )
|
---|
| 262 | {
|
---|
[211] | 263 | log( 1, "Write attempt to Read Only disk" );
|
---|
[209] | 264 | continue;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | workOffset = 0;
|
---|
| 268 | workCount = buff.chs.count;
|
---|
[210] | 269 | if( verboseLevel > 1 && workCount > 100 )
|
---|
| 270 | perfTimer = GetTime();
|
---|
[209] | 271 | }
|
---|
| 272 |
|
---|
| 273 | if( workCount && (workCommand == (SERIAL_COMMAND_WRITE | SERIAL_COMMAND_READWRITE)) )
|
---|
| 274 | {
|
---|
[210] | 275 | //
|
---|
| 276 | // Write command... Setup to receive a sector
|
---|
| 277 | //
|
---|
[209] | 278 | readto = 514;
|
---|
| 279 | }
|
---|
| 280 | else
|
---|
| 281 | {
|
---|
[210] | 282 | //
|
---|
| 283 | // Inquire command...
|
---|
| 284 | //
|
---|
[209] | 285 | if( workCommand == SERIAL_COMMAND_INQUIRE )
|
---|
| 286 | {
|
---|
| 287 | if( serial->speedEmulation &&
|
---|
| 288 | (buff.inquire.portAndBaud & SERIAL_INQUIRE_PORTANDBAUD_BAUD) != serial->baudRate->divisor )
|
---|
| 289 | {
|
---|
| 290 | struct baudRate *br;
|
---|
| 291 |
|
---|
| 292 | br = baudRateMatchDivisor( buff.inquire.portAndBaud & SERIAL_INQUIRE_PORTANDBAUD_BAUD );
|
---|
| 293 |
|
---|
| 294 | if( br )
|
---|
[211] | 295 | log( 1, " Ignoring Inquire with Baud Rate=%d", br->rate );
|
---|
[209] | 296 | else
|
---|
[211] | 297 | log( 1, " Ignoring Inquire with Unknown Baud Rate (portAndBaud=%d)", buff.inquire.portAndBaud );
|
---|
[209] | 298 | workCount = 0;
|
---|
| 299 | continue;
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | img->respondInquire( &buff.w[0], serial->baudRate, buff.inquire.portAndBaud );
|
---|
| 303 | }
|
---|
[210] | 304 | //
|
---|
| 305 | // Read command...
|
---|
| 306 | //
|
---|
[209] | 307 | else
|
---|
| 308 | {
|
---|
| 309 | img->seekSector( mylba + workOffset );
|
---|
| 310 | img->readSector( &buff.w[0] );
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | buff.w[256] = checksum( &buff.w[0], 256 );
|
---|
| 314 |
|
---|
| 315 | if( serial->writeCharacters( &buff.w[0], 514 ) != 514 )
|
---|
[211] | 316 | log( 0, "Serial Port Write Error" );
|
---|
[209] | 317 |
|
---|
| 318 | workCount--;
|
---|
| 319 | workOffset++;
|
---|
| 320 | }
|
---|
[210] | 321 | }
|
---|
[209] | 322 |
|
---|
[210] | 323 | if( verboseLevel > 1 )
|
---|
| 324 | {
|
---|
| 325 | char *comStr = (workCommand & SERIAL_COMMAND_WRITE ? "Write" :
|
---|
| 326 | (workCommand & SERIAL_COMMAND_READWRITE ? "Read" : "Inquire"));
|
---|
[209] | 327 |
|
---|
[210] | 328 | if( vtype == 1 )
|
---|
[211] | 329 | log( 1, "%s %d: LBA=%u, Count=%u", comStr, img == image0 ? 0 : 1,
|
---|
[210] | 330 | mylba, workCount );
|
---|
| 331 | else if( vtype == 2 )
|
---|
[211] | 332 | log( 1, "%s %d: Cylinder=%u, Sector=%u, Head=%u, Count=%u, LBA=%u", comStr, img == image0 ? 0 : 1,
|
---|
[210] | 333 | cyl, sect, head, workCount+1, mylba );
|
---|
[209] | 334 |
|
---|
[210] | 335 | vtype = 0;
|
---|
| 336 |
|
---|
| 337 | if( workOffset > 1 )
|
---|
[211] | 338 | log( 2, " Continuation: Offset=%u, Checksum=%04x", workOffset-1, buff.w[256] );
|
---|
[210] | 339 |
|
---|
| 340 | if( !(workCommand & SERIAL_COMMAND_WRITE) && verboseLevel >= 4 )
|
---|
| 341 | logBuff( " Sending: ", 514, 514, verboseLevel );
|
---|
| 342 |
|
---|
| 343 | if( workCount == 0 && workOffset > 100 )
|
---|
[211] | 344 | log( 1, " Block Complete: %.2lf bytes per second", (512.0 * workOffset) / (GetTime() - perfTimer) * 1000.0 );
|
---|
[209] | 345 | }
|
---|
| 346 | }
|
---|
| 347 | }
|
---|
[210] | 348 |
|
---|
| 349 |
|
---|