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

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

Serial Server, minor improvements to file handling.

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