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

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

Updated version number of serial server.

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