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