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