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

Last change on this file since 469 was 376, checked in by gregli@…, 13 years ago

WIDE checkin... Added copyright and license information to sorce files, as per the GPL instructions for usage.

File size: 8.1 KB
RevLine 
[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
[376]11//
12// XTIDE Universal BIOS and Associated Tools
13// Copyright (C) 2009-2010 by Tomi Tilli, 2011-2012 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
[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]37char *bannerStrings[] = {
38 "SerDrive - XTIDE Universal BIOS Serial Drive Server",
39 "Copyright (C) 2012 by XTIDE Universal BIOS Team",
40 "Released under GNU GPL v2, with ABSOLUTELY NO WARRANTY",
[371]41 ROM_VERSION_STRING,
[369]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 " and for completeness: 76.8K, 153.6K",
77 " (default is 9600, 115.2K when in named pipe mode)",
78 "",
79 " -t Disable timeout, useful for long delays when debugging",
80 "",
81 " -r Read Only disk, do not allow writes",
82 "",
83 " -v [level] Reporting level 1-6, with increasing information",
84 "",
85 "On the client computer, a serial port can be configured for use as a hard disk",
86 "with xtidecfg.com. Or one can hold down the ALT key at the end of the normal",
87 "IDE hard disk scan and the XTIDE Universal BIOS will scan COM1-7, at each of",
88 "the six speeds given above for BaudRate. Note that hardware rate multipliers",
89 "must be taken into account on the server end, but are invisible on the client.",
90 "",
91 "Floppy images may also be used. Image size must be exactly the same size",
92 "as a 2.88MB, 1.44MB, 1.2MB, 720KB, 360KB, 320KB, 180KB, or 160KB disk.",
93 "Floppy images must be the last disks discovered by the BIOS, and only",
94 "two floppy drives are supported by the BIOS at a time.",
95 NULL };
96
97void usagePrint( char *strings[] )
[209]98{
[369]99 for( int t = 0; strings[t]; t++ )
[258]100 {
[369]101 if( !strncmp( strings[t], "COM", 3 ) )
[258]102 {
103 char logbuff[ 1024 ];
[209]104
[258]105 SerialAccess::EnumerateCOMPorts( logbuff, 1024 );
[369]106 fprintf( stderr, "%s%s\n", strings[t]+3, logbuff );
[258]107 }
108 else
[369]109 fprintf( stderr, "%s\n", strings[t] );
[258]110 }
[209]111}
112
[369]113#define usage() { usagePrint( usageStrings ); exit(1); }
114
[211]115int verbose = 0;
[209]116
117int main(int argc, char* argv[])
118{
119 DWORD len;
120
121 unsigned long check;
122 unsigned char w;
123
124 unsigned short wbuff[256];
125
[219]126 SerialAccess serial;
[209]127 Image *img;
[215]128 struct baudRate *baudRate = NULL;
[209]129
130 int timeoutEnabled = 1;
131
132 char *ComPort = NULL, ComPortBuff[20];
133
134 _fmode = _O_BINARY;
135
136 unsigned long cyl = 0, sect = 0, head = 0;
[217]137 int readOnly = 0, createFile = 0;
138 int useCHS = 0;
[209]139
140 int imagecount = 0;
141 Image *images[2] = { NULL, NULL };
142
[369]143 usagePrint( bannerStrings );
144
[209]145 for( int t = 1; t < argc; t++ )
146 {
[369]147 char *next = (t+1 < argc ? argv[t+1] : NULL );
148
[209]149 if( argv[t][0] == '/' || argv[t][0] == '-' )
150 {
151 char *c;
152 unsigned long a;
153 for( c = &argv[t][1]; *c && !isdigit( *c ); c++ )
154 ;
155 a = atol(c);
156
157 switch( argv[t][1] )
158 {
159 case 'c': case 'C':
[369]160 if( !next )
161 usage();
162 t++;
163 a = atol( next );
[209]164 if( a < 1 )
165 usage();
166 sprintf( ComPortBuff, "COM%d", a );
167 ComPort = &ComPortBuff[0];
168 break;
169 case 'v': case 'V':
[369]170 if( next && atol(next) != 0 )
171 {
172 t++;
173 verbose = atol(next);
174 }
[209]175 else
[213]176 verbose = 1;
[209]177 break;
178 case 'r': case 'R':
179 readOnly = 1;
180 break;
181 case 'p': case 'P':
[369]182 if( next && next[0] == '\\' && next[1] == '\\' )
183 {
184 t++;
185 ComPort = next;
186 }
[258]187 else
188 ComPort = PIPENAME;
[215]189 if( !baudRate )
190 baudRate = baudRateMatchString( "115200" );
[209]191 break;
192 case 'g': case 'G':
[369]193 if( next && atol(next) != 0 )
[217]194 {
[369]195 t++;
196 if( !Image::parseGeometry( next, &cyl, &head, &sect ) )
[217]197 usage();
198 }
199 useCHS = 1;
[209]200 break;
201 case 'h': case 'H': case '?':
202 usage();
203 break;
204 case 'n': case 'N':
205 createFile = 1;
[369]206 if( next && atol(next) != 0 )
[209]207 {
[369]208 double size = atof(next);
[259]209 struct floppyInfo *fi;
210 char *c;
211
[369]212 t++;
213
[259]214 size *= 2;
215 for( c = argv[t]; *c && *c != 'k' && *c != 'K'; c++ ) ;
216 if( !(*c) )
217 size *= 1000;
218
219 if( (fi = FindFloppyInfoBySize( size )) )
220 {
221 sect = fi->sectors;
222 head = fi->heads;
223 cyl = fi->cylinders;
224 }
225 else
226 {
227 sect = 63;
228 head = 16;
229 cyl = size / (16*63);
230 }
[209]231 }
232 break;
233 case 't': case 'T':
234 timeoutEnabled = 0;
235 break;
236 case 'b': case 'B':
[369]237 if( !next )
238 usage();
239 t++;
240 if( !(baudRate = baudRateMatchString( next )) || !baudRate->rate )
241 log( -2, "Unknown Baud Rate \"%s\"", next );
[209]242 break;
243 default:
[233]244 log( -2, "Unknown Option: \"%s\"", argv[t] );
[209]245 }
246 }
247 else if( imagecount < 2 )
248 {
[225]249 if( createFile && cyl == 0 )
250 {
251 cyl = 65;
252 sect = 63;
253 head = 16;
254 }
[217]255 images[imagecount] = new FlatImage( argv[t], readOnly, imagecount, createFile, cyl, head, sect, useCHS );
[209]256 imagecount++;
[217]257 createFile = readOnly = cyl = sect = head = useCHS = 0;
[209]258 }
259 else
260 usage();
261 }
262
263 if( imagecount == 0 )
264 usage();
265
[215]266 if( !baudRate )
[233]267 baudRate = baudRateMatchString( "9600" );
[215]268
[209]269 do
270 {
[219]271 serial.Connect( ComPort, baudRate );
[209]272
[219]273 processRequests( &serial, images[0], images[1], timeoutEnabled, verbose );
[209]274
[219]275 serial.Disconnect();
[209]276
[219]277 if( serial.resetConnection )
278 log( 0, "Serial Connection closed, reset..." );
[209]279 }
[219]280 while( serial.resetConnection );
[209]281}
282
283void log( int level, char *message, ... )
284{
285 va_list args;
286
287 va_start( args, message );
288
[211]289 if( level < 0 )
[209]290 {
[211]291 fprintf( stderr, "ERROR: " );
292 vfprintf( stderr, message, args );
293 fprintf( stderr, "\n" );
[233]294 if( level < -1 )
295 {
296 fprintf( stderr, "\n" );
297 usage();
298 }
[211]299 exit( 1 );
[209]300 }
301 else if( verbose >= level )
302 {
[211]303 vprintf( message, args );
304 printf( "\n" );
[209]305 }
306
307 va_end( args );
308}
309
310unsigned long GetTime(void)
311{
312 return( GetTickCount() );
313}
314
315unsigned long GetTime_Timeout(void)
316{
317 return( 1000 );
318}
Note: See TracBrowser for help on using the repository browser.