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 | #include "Win32Serial.h" |
---|
19 | |
---|
20 | void usage(void) |
---|
21 | { |
---|
22 | char *usageStrings[] = { |
---|
23 | "usage: SerServe [options] master-imagefile [[slave-options] slave-imagefile]", |
---|
24 | " -g cyl:sect:head Geometry in cylinders, sectors per cylinder, and heads", |
---|
25 | " (default is 65:63:16 for a 32 MB disk)", |
---|
26 | " -n [megabytes] New, Create new disk with given size or use -g geometry", |
---|
27 | " -p Named pipe mode for emulators (pipe is " PIPENAME ")", |
---|
28 | " -c PortNumber COM Port to use (default is first found)", |
---|
29 | " -b BaudRate Baud rate to use on the COM port ", |
---|
30 | " 9600, 38400, 115200, 230400, or 460800", |
---|
31 | " (default 9600, 115200 in pipe mode)", |
---|
32 | " -t Disable timeout, useful for long delays when debugging", |
---|
33 | " -r Read Only disk", |
---|
34 | " -v [level] Reporting level 1-6, increasing information", |
---|
35 | NULL }; |
---|
36 | |
---|
37 | for( int t = 0; usageStrings[t]; t++ ) |
---|
38 | fprintf( stderr, "%s\n", usageStrings[t] ); |
---|
39 | |
---|
40 | exit( 1 ); |
---|
41 | } |
---|
42 | |
---|
43 | int verbose = 1; |
---|
44 | |
---|
45 | int main(int argc, char* argv[]) |
---|
46 | { |
---|
47 | DWORD len; |
---|
48 | |
---|
49 | unsigned long check; |
---|
50 | unsigned char w; |
---|
51 | |
---|
52 | unsigned short wbuff[256]; |
---|
53 | |
---|
54 | Serial *serial; |
---|
55 | Image *img; |
---|
56 | struct baudRate *baudRate; |
---|
57 | |
---|
58 | int timeoutEnabled = 1; |
---|
59 | |
---|
60 | char *ComPort = NULL, ComPortBuff[20]; |
---|
61 | |
---|
62 | _fmode = _O_BINARY; |
---|
63 | |
---|
64 | unsigned long cyl = 0, sect = 0, head = 0; |
---|
65 | int readOnly = 0, createFile = 0, explicitGeometry = 0; |
---|
66 | |
---|
67 | int imagecount = 0; |
---|
68 | Image *images[2] = { NULL, NULL }; |
---|
69 | |
---|
70 | baudRate = baudRateMatchString( "9600" ); |
---|
71 | |
---|
72 | for( int t = 1; t < argc; t++ ) |
---|
73 | { |
---|
74 | if( argv[t][0] == '/' || argv[t][0] == '-' ) |
---|
75 | { |
---|
76 | char *c; |
---|
77 | unsigned long a; |
---|
78 | for( c = &argv[t][1]; *c && !isdigit( *c ); c++ ) |
---|
79 | ; |
---|
80 | a = atol(c); |
---|
81 | |
---|
82 | switch( argv[t][1] ) |
---|
83 | { |
---|
84 | case 'c': case 'C': |
---|
85 | a = atol( argv[++t] ); |
---|
86 | if( a < 1 ) |
---|
87 | usage(); |
---|
88 | sprintf( ComPortBuff, "COM%d", a ); |
---|
89 | ComPort = &ComPortBuff[0]; |
---|
90 | break; |
---|
91 | case 'v': case 'V': |
---|
92 | if( atol(argv[t+1]) != 0 ) |
---|
93 | verbose = atol(argv[++t]); |
---|
94 | else |
---|
95 | verbose = 2; |
---|
96 | break; |
---|
97 | case 'r': case 'R': |
---|
98 | readOnly = 1; |
---|
99 | break; |
---|
100 | case 'p': case 'P': |
---|
101 | ComPort = "PIPE"; |
---|
102 | baudRate = baudRateMatchString( "115200" ); |
---|
103 | break; |
---|
104 | case 'g': case 'G': |
---|
105 | if( !Image::parseGeometry( argv[++t], &cyl, §, &head ) ) |
---|
106 | usage(); |
---|
107 | explicitGeometry = 1; |
---|
108 | break; |
---|
109 | case 'h': case 'H': case '?': |
---|
110 | usage(); |
---|
111 | break; |
---|
112 | case 'n': case 'N': |
---|
113 | createFile = 1; |
---|
114 | if( atol(argv[t+1]) != 0 ) |
---|
115 | { |
---|
116 | unsigned long size = atol(argv[++t]); |
---|
117 | sect = 63; |
---|
118 | head = 16; |
---|
119 | cyl = (size*1024*2) / (16*63); |
---|
120 | explicitGeometry = 1; |
---|
121 | } |
---|
122 | break; |
---|
123 | case 't': case 'T': |
---|
124 | timeoutEnabled = 0; |
---|
125 | break; |
---|
126 | case 'b': case 'B': |
---|
127 | if( !(baudRate = baudRateMatchString( argv[++t] )) ) |
---|
128 | { |
---|
129 | fprintf( stderr, "Unknown Baud Rate %s\n\n", argv[t] ); |
---|
130 | usage(); |
---|
131 | } |
---|
132 | break; |
---|
133 | default: |
---|
134 | fprintf( stderr, "Unknown Option: %s\n\n", argv[t] ); |
---|
135 | usage(); |
---|
136 | } |
---|
137 | } |
---|
138 | else if( imagecount < 2 ) |
---|
139 | { |
---|
140 | images[imagecount] = new FlatImage( argv[t], readOnly, imagecount, createFile, cyl, sect, head ); |
---|
141 | imagecount++; |
---|
142 | createFile = readOnly = cyl = sect = head = 0; |
---|
143 | } |
---|
144 | else |
---|
145 | usage(); |
---|
146 | } |
---|
147 | |
---|
148 | if( imagecount == 0 ) |
---|
149 | usage(); |
---|
150 | |
---|
151 | do |
---|
152 | { |
---|
153 | serial = new Win32Serial( ComPort, baudRate ); |
---|
154 | |
---|
155 | processRequests( serial, images[0], images[1], timeoutEnabled, verbose ); |
---|
156 | |
---|
157 | delete serial; |
---|
158 | |
---|
159 | if( serial->resetConnection ) |
---|
160 | log( 1, "==== Resetting Connection ====" ); |
---|
161 | } |
---|
162 | while( serial->resetConnection ); |
---|
163 | } |
---|
164 | |
---|
165 | void log( int level, char *message, ... ) |
---|
166 | { |
---|
167 | va_list args; |
---|
168 | |
---|
169 | va_start( args, message ); |
---|
170 | |
---|
171 | if( level == 0 ) |
---|
172 | { |
---|
173 | vfprintf( stderr, message, args ); |
---|
174 | fprintf( stderr, "\n" ); |
---|
175 | exit( 1 ); |
---|
176 | } |
---|
177 | else if( verbose >= level ) |
---|
178 | { |
---|
179 | vprintf( message, args ); |
---|
180 | printf( "\n" ); |
---|
181 | } |
---|
182 | |
---|
183 | va_end( args ); |
---|
184 | } |
---|
185 | |
---|
186 | unsigned long GetTime(void) |
---|
187 | { |
---|
188 | return( GetTickCount() ); |
---|
189 | } |
---|
190 | |
---|
191 | unsigned long GetTime_Timeout(void) |
---|
192 | { |
---|
193 | return( 1000 ); |
---|
194 | } |
---|