source: xtideuniversalbios/trunk/Serial_Server/win32/Win32.cpp@ 569

Last change on this file since 569 was 526, checked in by krille_n_@…, 12 years ago

Changes:

  • Update of the copyright notices to include the year 2013.
File size: 8.2 KB
Line 
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//
12// XTIDE Universal BIOS and Associated Tools
13// Copyright (C) 2009-2010 by Tomi Tilli, 2011-2013 by XTIDE Universal BIOS Team.
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.
19//
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
23// GNU General Public License for more details.
24// Visit http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
25//
26
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
35#include "../../XTIDE_Universal_BIOS/inc/version.inc"
36
37char *bannerStrings[] = {
38 "SerDrive - XTIDE Universal BIOS Serial Drive Server",
39 "Copyright (C) 2012-2013 by XTIDE Universal BIOS Team",
40 "Released under GNU GPL v2, with ABSOLUTELY NO WARRANTY",
41 ROM_VERSION_STRING,
42 "",
43 NULL };
44
45char *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",
60 " Maximum size is " USAGE_MAXSECTORS,
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:",
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",
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
98void usagePrint( char *strings[] )
99{
100 for( int t = 0; strings[t]; t++ )
101 {
102 if( !strncmp( strings[t], "COM", 3 ) )
103 {
104 char logbuff[ 1024 ];
105
106 SerialAccess::EnumerateCOMPorts( logbuff, 1024 );
107 fprintf( stderr, "%s%s\n", strings[t]+3, logbuff );
108 }
109 else
110 fprintf( stderr, "%s\n", strings[t] );
111 }
112}
113
114#define usage() { usagePrint( usageStrings ); exit(1); }
115
116int verbose = 0;
117
118int 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
127 SerialAccess serial;
128 Image *img;
129 struct baudRate *baudRate = NULL;
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;
138 int readOnly = 0, createFile = 0;
139 int useCHS = 0;
140
141 int imagecount = 0;
142 Image *images[2] = { NULL, NULL };
143
144 usagePrint( bannerStrings );
145
146 for( int t = 1; t < argc; t++ )
147 {
148 char *next = (t+1 < argc ? argv[t+1] : NULL );
149
150 if( argv[t][0] == '/' || argv[t][0] == '-' )
151 {
152 char *c;
153 unsigned long a;
154 for( c = &argv[t][1]; *c && !isdigit( *c ); c++ )
155 ;
156 a = atol(c);
157
158 switch( argv[t][1] )
159 {
160 case 'c': case 'C':
161 if( !next )
162 usage();
163 t++;
164 a = atol( next );
165 if( a < 1 )
166 usage();
167 sprintf( ComPortBuff, "COM%d", a );
168 ComPort = &ComPortBuff[0];
169 break;
170 case 'v': case 'V':
171 if( next && atol(next) != 0 )
172 {
173 t++;
174 verbose = atol(next);
175 }
176 else
177 verbose = 1;
178 break;
179 case 'r': case 'R':
180 readOnly = 1;
181 break;
182 case 'p': case 'P':
183 if( next && next[0] == '\\' && next[1] == '\\' )
184 {
185 t++;
186 ComPort = next;
187 }
188 else
189 ComPort = PIPENAME;
190 if( !baudRate )
191 baudRate = baudRateMatchString( "115200" );
192 break;
193 case 'g': case 'G':
194 if( next && atol(next) != 0 )
195 {
196 t++;
197 if( !Image::parseGeometry( next, &cyl, &head, &sect ) )
198 usage();
199 }
200 useCHS = 1;
201 break;
202 case 'h': case 'H': case '?':
203 usage();
204 break;
205 case 'n': case 'N':
206 createFile = 1;
207 if( next && atol(next) != 0 )
208 {
209 double size = atof(next);
210 struct floppyInfo *fi;
211 char *c;
212
213 t++;
214
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 }
232 }
233 break;
234 case 't': case 'T':
235 timeoutEnabled = 0;
236 break;
237 case 'b': case 'B':
238 if( !next )
239 usage();
240 t++;
241 if( !(baudRate = baudRateMatchString( next )) || !baudRate->rate )
242 log( -2, "Unknown Baud Rate \"%s\"", next );
243 break;
244 default:
245 log( -2, "Unknown Option: \"%s\"", argv[t] );
246 }
247 }
248 else if( imagecount < 2 )
249 {
250 if( createFile && cyl == 0 )
251 {
252 cyl = 65;
253 sect = 63;
254 head = 16;
255 }
256 images[imagecount] = new FlatImage( argv[t], readOnly, imagecount, createFile, cyl, head, sect, useCHS );
257 imagecount++;
258 createFile = readOnly = cyl = sect = head = useCHS = 0;
259 }
260 else
261 usage();
262 }
263
264 if( imagecount == 0 )
265 usage();
266
267 if( !baudRate )
268 baudRate = baudRateMatchString( "9600" );
269
270 do
271 {
272 serial.Connect( ComPort, baudRate );
273
274 processRequests( &serial, images[0], images[1], timeoutEnabled, verbose );
275
276 serial.Disconnect();
277
278 if( serial.resetConnection )
279 log( 0, "Serial Connection closed, reset..." );
280 }
281 while( serial.resetConnection );
282}
283
284void log( int level, char *message, ... )
285{
286 va_list args;
287
288 va_start( args, message );
289
290 if( level < 0 )
291 {
292 fprintf( stderr, "ERROR: " );
293 vfprintf( stderr, message, args );
294 fprintf( stderr, "\n" );
295 if( level < -1 )
296 {
297 fprintf( stderr, "\n" );
298 usage();
299 }
300 exit( 1 );
301 }
302 else if( verbose >= level )
303 {
304 vprintf( message, args );
305 printf( "\n" );
306 }
307
308 va_end( args );
309}
310
311unsigned long GetTime(void)
312{
313 return( GetTickCount() );
314}
315
316unsigned long GetTime_Timeout(void)
317{
318 return( 1000 );
319}
Note: See TracBrowser for help on using the repository browser.