[209] | 1 | //======================================================================
|
---|
| 2 | //
|
---|
| 3 | // Project: XTIDE Universal BIOS, Serial Port Server
|
---|
| 4 | //
|
---|
| 5 | // File: Win32.cpp - Microsoft Windows 32-bit application
|
---|
| 6 | //
|
---|
| 7 | // This file contains the entry point for the Win32 version of the server.
|
---|
| 8 | // It also handles log reporting, timers, and command line parameter parsing.
|
---|
| 9 | //
|
---|
| 10 |
|
---|
| 11 | #include <stdio.h>
|
---|
| 12 | #include <stdlib.h>
|
---|
| 13 | #include <fcntl.h>
|
---|
| 14 | #include <stdarg.h>
|
---|
| 15 |
|
---|
| 16 | #include "../library/library.h"
|
---|
| 17 | #include "../library/flatimage.h"
|
---|
| 18 |
|
---|
[369] | 19 | char *bannerStrings[] = {
|
---|
| 20 | "SerDrive - XTIDE Universal BIOS Serial Drive Server",
|
---|
| 21 | "Copyright (C) 2012 by XTIDE Universal BIOS Team",
|
---|
| 22 | "Released under GNU GPL v2, with ABSOLUTELY NO WARRANTY",
|
---|
| 23 | "Version 2.0.0 Beta1, Built " __DATE__,
|
---|
| 24 | "",
|
---|
| 25 | NULL };
|
---|
| 26 |
|
---|
| 27 | char *usageStrings[] = {
|
---|
| 28 | "This is free software, and you are welcome to redistribute it under certain",
|
---|
| 29 | "conditions. For more license details, see the LICENSE.TXT file included with",
|
---|
| 30 | "this distribution, visit the XTIDE Universal BIOS wiki (address below), or",
|
---|
| 31 | "http://www.gnu.org/licenses/gpl-2.0.html",
|
---|
| 32 | "",
|
---|
| 33 | "Visit the wiki on http://code.google.com/p/xtideuniversalbios for detailed",
|
---|
| 34 | "serial drive usage directions.",
|
---|
| 35 | "",
|
---|
| 36 | "Usage: SerDrive [options] imagefile [[slave-options] slave-imagefile]",
|
---|
| 37 | "",
|
---|
| 38 | " -g [cyl:head:sect] Geometry in cylinders, sectors per cylinder, and heads",
|
---|
| 39 | " -g also implies CHS addressing mode (default is LBA28)",
|
---|
| 40 | "",
|
---|
| 41 | " -n [megabytes] Create new disk with given size or use -g geometry",
|
---|
| 42 | " Maximum size is " USAGE_MAXSECTORS,
|
---|
| 43 | " Floppy images can also be created, such as \"360K\"",
|
---|
| 44 | " (default is a 32 MB disk, with CHS geometry 65:16:63)",
|
---|
| 45 | "",
|
---|
| 46 | " -p [pipename] Named Pipe mode for emulators",
|
---|
| 47 | " (must begin with \"\\\\\", default is \"" PIPENAME "\")",
|
---|
| 48 | "",
|
---|
| 49 | " -c COMPortNumber COM Port to use (default is first found)",
|
---|
| 50 | " Available COM ports on this system are:",
|
---|
| 51 | "COM ",
|
---|
| 52 | "",
|
---|
| 53 | " -b BaudRate Baud rate to use on the COM port, with client machine",
|
---|
| 54 | " rate multiplier in effect:",
|
---|
| 55 | " None: 2400, 4800, 9600, 28.8K, 57.6K, 115.2K",
|
---|
| 56 | " 2x: 4800, 9600, 19200, 57.6K, 115.2K, 230.4K",
|
---|
| 57 | " 4x: 9600, 19200, 38400, 115.2K, 230.4K, 460.8K",
|
---|
| 58 | " and for completeness: 76.8K, 153.6K",
|
---|
| 59 | " (default is 9600, 115.2K when in named pipe mode)",
|
---|
| 60 | "",
|
---|
| 61 | " -t Disable timeout, useful for long delays when debugging",
|
---|
| 62 | "",
|
---|
| 63 | " -r Read Only disk, do not allow writes",
|
---|
| 64 | "",
|
---|
| 65 | " -v [level] Reporting level 1-6, with increasing information",
|
---|
| 66 | "",
|
---|
| 67 | "On the client computer, a serial port can be configured for use as a hard disk",
|
---|
| 68 | "with xtidecfg.com. Or one can hold down the ALT key at the end of the normal",
|
---|
| 69 | "IDE hard disk scan and the XTIDE Universal BIOS will scan COM1-7, at each of",
|
---|
| 70 | "the six speeds given above for BaudRate. Note that hardware rate multipliers",
|
---|
| 71 | "must be taken into account on the server end, but are invisible on the client.",
|
---|
| 72 | "",
|
---|
| 73 | "Floppy images may also be used. Image size must be exactly the same size",
|
---|
| 74 | "as a 2.88MB, 1.44MB, 1.2MB, 720KB, 360KB, 320KB, 180KB, or 160KB disk.",
|
---|
| 75 | "Floppy images must be the last disks discovered by the BIOS, and only",
|
---|
| 76 | "two floppy drives are supported by the BIOS at a time.",
|
---|
| 77 | NULL };
|
---|
| 78 |
|
---|
| 79 | void usagePrint( char *strings[] )
|
---|
[209] | 80 | {
|
---|
[369] | 81 | for( int t = 0; strings[t]; t++ )
|
---|
[258] | 82 | {
|
---|
[369] | 83 | if( !strncmp( strings[t], "COM", 3 ) )
|
---|
[258] | 84 | {
|
---|
| 85 | char logbuff[ 1024 ];
|
---|
[209] | 86 |
|
---|
[258] | 87 | SerialAccess::EnumerateCOMPorts( logbuff, 1024 );
|
---|
[369] | 88 | fprintf( stderr, "%s%s\n", strings[t]+3, logbuff );
|
---|
[258] | 89 | }
|
---|
| 90 | else
|
---|
[369] | 91 | fprintf( stderr, "%s\n", strings[t] );
|
---|
[258] | 92 | }
|
---|
[209] | 93 | }
|
---|
| 94 |
|
---|
[369] | 95 | #define usage() { usagePrint( usageStrings ); exit(1); }
|
---|
| 96 |
|
---|
[211] | 97 | int verbose = 0;
|
---|
[209] | 98 |
|
---|
| 99 | int main(int argc, char* argv[])
|
---|
| 100 | {
|
---|
| 101 | DWORD len;
|
---|
| 102 |
|
---|
| 103 | unsigned long check;
|
---|
| 104 | unsigned char w;
|
---|
| 105 |
|
---|
| 106 | unsigned short wbuff[256];
|
---|
| 107 |
|
---|
[219] | 108 | SerialAccess serial;
|
---|
[209] | 109 | Image *img;
|
---|
[215] | 110 | struct baudRate *baudRate = NULL;
|
---|
[209] | 111 |
|
---|
| 112 | int timeoutEnabled = 1;
|
---|
| 113 |
|
---|
| 114 | char *ComPort = NULL, ComPortBuff[20];
|
---|
| 115 |
|
---|
| 116 | _fmode = _O_BINARY;
|
---|
| 117 |
|
---|
| 118 | unsigned long cyl = 0, sect = 0, head = 0;
|
---|
[217] | 119 | int readOnly = 0, createFile = 0;
|
---|
| 120 | int useCHS = 0;
|
---|
[209] | 121 |
|
---|
| 122 | int imagecount = 0;
|
---|
| 123 | Image *images[2] = { NULL, NULL };
|
---|
| 124 |
|
---|
[369] | 125 | usagePrint( bannerStrings );
|
---|
| 126 |
|
---|
[209] | 127 | for( int t = 1; t < argc; t++ )
|
---|
| 128 | {
|
---|
[369] | 129 | char *next = (t+1 < argc ? argv[t+1] : NULL );
|
---|
| 130 |
|
---|
[209] | 131 | if( argv[t][0] == '/' || argv[t][0] == '-' )
|
---|
| 132 | {
|
---|
| 133 | char *c;
|
---|
| 134 | unsigned long a;
|
---|
| 135 | for( c = &argv[t][1]; *c && !isdigit( *c ); c++ )
|
---|
| 136 | ;
|
---|
| 137 | a = atol(c);
|
---|
| 138 |
|
---|
| 139 | switch( argv[t][1] )
|
---|
| 140 | {
|
---|
| 141 | case 'c': case 'C':
|
---|
[369] | 142 | if( !next )
|
---|
| 143 | usage();
|
---|
| 144 | t++;
|
---|
| 145 | a = atol( next );
|
---|
[209] | 146 | if( a < 1 )
|
---|
| 147 | usage();
|
---|
| 148 | sprintf( ComPortBuff, "COM%d", a );
|
---|
| 149 | ComPort = &ComPortBuff[0];
|
---|
| 150 | break;
|
---|
| 151 | case 'v': case 'V':
|
---|
[369] | 152 | if( next && atol(next) != 0 )
|
---|
| 153 | {
|
---|
| 154 | t++;
|
---|
| 155 | verbose = atol(next);
|
---|
| 156 | }
|
---|
[209] | 157 | else
|
---|
[213] | 158 | verbose = 1;
|
---|
[209] | 159 | break;
|
---|
| 160 | case 'r': case 'R':
|
---|
| 161 | readOnly = 1;
|
---|
| 162 | break;
|
---|
| 163 | case 'p': case 'P':
|
---|
[369] | 164 | if( next && next[0] == '\\' && next[1] == '\\' )
|
---|
| 165 | {
|
---|
| 166 | t++;
|
---|
| 167 | ComPort = next;
|
---|
| 168 | }
|
---|
[258] | 169 | else
|
---|
| 170 | ComPort = PIPENAME;
|
---|
[215] | 171 | if( !baudRate )
|
---|
| 172 | baudRate = baudRateMatchString( "115200" );
|
---|
[209] | 173 | break;
|
---|
| 174 | case 'g': case 'G':
|
---|
[369] | 175 | if( next && atol(next) != 0 )
|
---|
[217] | 176 | {
|
---|
[369] | 177 | t++;
|
---|
| 178 | if( !Image::parseGeometry( next, &cyl, &head, § ) )
|
---|
[217] | 179 | usage();
|
---|
| 180 | }
|
---|
| 181 | useCHS = 1;
|
---|
[209] | 182 | break;
|
---|
| 183 | case 'h': case 'H': case '?':
|
---|
| 184 | usage();
|
---|
| 185 | break;
|
---|
| 186 | case 'n': case 'N':
|
---|
| 187 | createFile = 1;
|
---|
[369] | 188 | if( next && atol(next) != 0 )
|
---|
[209] | 189 | {
|
---|
[369] | 190 | double size = atof(next);
|
---|
[259] | 191 | struct floppyInfo *fi;
|
---|
| 192 | char *c;
|
---|
| 193 |
|
---|
[369] | 194 | t++;
|
---|
| 195 |
|
---|
[259] | 196 | size *= 2;
|
---|
| 197 | for( c = argv[t]; *c && *c != 'k' && *c != 'K'; c++ ) ;
|
---|
| 198 | if( !(*c) )
|
---|
| 199 | size *= 1000;
|
---|
| 200 |
|
---|
| 201 | if( (fi = FindFloppyInfoBySize( size )) )
|
---|
| 202 | {
|
---|
| 203 | sect = fi->sectors;
|
---|
| 204 | head = fi->heads;
|
---|
| 205 | cyl = fi->cylinders;
|
---|
| 206 | }
|
---|
| 207 | else
|
---|
| 208 | {
|
---|
| 209 | sect = 63;
|
---|
| 210 | head = 16;
|
---|
| 211 | cyl = size / (16*63);
|
---|
| 212 | }
|
---|
[209] | 213 | }
|
---|
| 214 | break;
|
---|
| 215 | case 't': case 'T':
|
---|
| 216 | timeoutEnabled = 0;
|
---|
| 217 | break;
|
---|
| 218 | case 'b': case 'B':
|
---|
[369] | 219 | if( !next )
|
---|
| 220 | usage();
|
---|
| 221 | t++;
|
---|
| 222 | if( !(baudRate = baudRateMatchString( next )) || !baudRate->rate )
|
---|
| 223 | log( -2, "Unknown Baud Rate \"%s\"", next );
|
---|
[209] | 224 | break;
|
---|
| 225 | default:
|
---|
[233] | 226 | log( -2, "Unknown Option: \"%s\"", argv[t] );
|
---|
[209] | 227 | }
|
---|
| 228 | }
|
---|
| 229 | else if( imagecount < 2 )
|
---|
| 230 | {
|
---|
[225] | 231 | if( createFile && cyl == 0 )
|
---|
| 232 | {
|
---|
| 233 | cyl = 65;
|
---|
| 234 | sect = 63;
|
---|
| 235 | head = 16;
|
---|
| 236 | }
|
---|
[217] | 237 | images[imagecount] = new FlatImage( argv[t], readOnly, imagecount, createFile, cyl, head, sect, useCHS );
|
---|
[209] | 238 | imagecount++;
|
---|
[217] | 239 | createFile = readOnly = cyl = sect = head = useCHS = 0;
|
---|
[209] | 240 | }
|
---|
| 241 | else
|
---|
| 242 | usage();
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | if( imagecount == 0 )
|
---|
| 246 | usage();
|
---|
| 247 |
|
---|
[215] | 248 | if( !baudRate )
|
---|
[233] | 249 | baudRate = baudRateMatchString( "9600" );
|
---|
[215] | 250 |
|
---|
[209] | 251 | do
|
---|
| 252 | {
|
---|
[219] | 253 | serial.Connect( ComPort, baudRate );
|
---|
[209] | 254 |
|
---|
[219] | 255 | processRequests( &serial, images[0], images[1], timeoutEnabled, verbose );
|
---|
[209] | 256 |
|
---|
[219] | 257 | serial.Disconnect();
|
---|
[209] | 258 |
|
---|
[219] | 259 | if( serial.resetConnection )
|
---|
| 260 | log( 0, "Serial Connection closed, reset..." );
|
---|
[209] | 261 | }
|
---|
[219] | 262 | while( serial.resetConnection );
|
---|
[209] | 263 | }
|
---|
| 264 |
|
---|
| 265 | void log( int level, char *message, ... )
|
---|
| 266 | {
|
---|
| 267 | va_list args;
|
---|
| 268 |
|
---|
| 269 | va_start( args, message );
|
---|
| 270 |
|
---|
[211] | 271 | if( level < 0 )
|
---|
[209] | 272 | {
|
---|
[211] | 273 | fprintf( stderr, "ERROR: " );
|
---|
| 274 | vfprintf( stderr, message, args );
|
---|
| 275 | fprintf( stderr, "\n" );
|
---|
[233] | 276 | if( level < -1 )
|
---|
| 277 | {
|
---|
| 278 | fprintf( stderr, "\n" );
|
---|
| 279 | usage();
|
---|
| 280 | }
|
---|
[211] | 281 | exit( 1 );
|
---|
[209] | 282 | }
|
---|
| 283 | else if( verbose >= level )
|
---|
| 284 | {
|
---|
[211] | 285 | vprintf( message, args );
|
---|
| 286 | printf( "\n" );
|
---|
[209] | 287 | }
|
---|
| 288 |
|
---|
| 289 | va_end( args );
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | unsigned long GetTime(void)
|
---|
| 293 | {
|
---|
| 294 | return( GetTickCount() );
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | unsigned long GetTime_Timeout(void)
|
---|
| 298 | {
|
---|
| 299 | return( 1000 );
|
---|
| 300 | }
|
---|