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