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