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

Last change on this file since 217 was 217, checked in by gregli@…, 12 years ago

Serial Server: various improvements, turned on LBA28 support by default.

File size: 5.4 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#include "Win32Serial.h"
19
20void usage(void)
21{
22    char *usageStrings[] = {
23        "SerDrive - XTIDE Universal BIOS Serial Drive Server",
24        "Version 1.2.0_wip, Built " __DATE__,
25        "",
26        "usage: SerDrive [options] imagefile [[slave-options] slave-imagefile]",
27        "",
28        "  -g [cyl:head:sect]  Geometry in cylinders, sectors per cylinder, and heads",
29        "                      -g without parameters uses CHS mode (default is LBA28)",
30        "",
31        "  -n [megabytes]      Create new disk with given size or use -g geometry",
32        "                      Maximum size is 137.4 GB (LBA28 limit)",
33        "                      (default is a 32 MB disk, with CHS geometry 65:63:16)",
34        "",
35        "  -p                  Named Pipe mode for emulators (pipe is '" PIPENAME "')",
36        "",
37        "  -c COMPortNumber    COM Port to use (default is first found)",
38        "",
39        "  -b BaudRate         Baud rate to use on the COM port ",
40        "                      Without a rate multiplier: 2400, 9600, 38400, 115200",
41        "                      With a 2x rate multiplier: 4800, 19200, 76800, 230400",
42        "                      With a 4x rate multiplier: 9600, 38400, 153600, 460800",
43        "                      Abbreviations also accepted (ie, '460K', '38.4K', etc)",
44        "                      (default is 38400, 115200 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
65int verbose = 0;
66
67int 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    Serial *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, &sect ) )
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] )) )
152                {
153                    fprintf( stderr, "Unknown Baud Rate %s\n\n", argv[t] );
154                    usage();
155                }
156                break;
157            default:
158                fprintf( stderr, "Unknown Option: %s\n\n", argv[t] );
159                usage();
160            }
161        }
162        else if( imagecount < 2 )
163        {
164            images[imagecount] = new FlatImage( argv[t], readOnly, imagecount, createFile, cyl, head, sect, useCHS );
165            imagecount++;
166            createFile = readOnly = cyl = sect = head = useCHS = 0;
167        }
168        else
169            usage();
170    }
171
172    if( imagecount == 0 )
173        usage();
174
175    if( !baudRate )
176        baudRate = baudRateMatchString( "38400" );
177
178    do
179    {
180        serial = new Win32Serial( ComPort, baudRate );
181
182        processRequests( serial, images[0], images[1], timeoutEnabled, verbose );
183
184        delete serial;
185
186        if( serial->resetConnection )
187            log( 0, "Connection closed, reset..." );
188    }
189    while( serial->resetConnection );
190}
191
192void log( int level, char *message, ... )
193{
194    va_list args;
195
196    va_start( args, message );
197
198    if( level < 0 )
199    {
200        fprintf( stderr, "ERROR: " );
201        vfprintf( stderr, message, args );
202        fprintf( stderr, "\n" );
203        exit( 1 );
204    }
205    else if( verbose >= level )
206    {
207        vprintf( message, args );
208        printf( "\n" );
209    }
210
211    va_end( args );
212}
213
214unsigned long GetTime(void)
215{
216    return( GetTickCount() );
217}
218
219unsigned long GetTime_Timeout(void)
220{
221    return( 1000 );
222}
Note: See TracBrowser for help on using the repository browser.