summaryrefslogtreecommitdiffstats
path: root/private/mvdm/v86/scaffold/i386/softpc.c
blob: b97ee53a9f78c443ddf6d46d3af052b275fc3c41 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <ctype.h>
#include "demexp.h"
#include "softpc.h"
#include <cmdsvc.h>
#include <xmssvc.h>
#include <dbgexp.h>
#include "xwincon.h"
#include "fun.h"
#include <conapi.h>

#define VDM_VIRTUAL_INTERRUPTS 0x00000200L
extern CONTEXT IntelRegisters;

VOID DumpIntelRegs();
VOID ParseSwitches( int, char**);
VOID usage();
BOOL ConInit (VOID);
extern VOID VDMCtrlCHandler(ULONG);

INT flOptions;

HANDLE OutputHandle;
HANDLE InputHandle;
HANDLE SCSCreateEvent;
char *EmDirectory;
BOOL scaffMin = FALSE;
BOOL scaffWow = FALSE;
BOOL VDMForWOW = FALSE;
CHAR BootLetter;


void main (argc, argv)
int argc;
char *argv[];
{

    PSZ psz,pszNULL;
    HANDLE hfile;
    DWORD BytesRead;
    int i;
    PCHAR FileAddress;
    BOOL	IsFirst;
    int   temp_argc = argc;
    char  **temp_argv = argv;

//    DebugBreak();

    if(SetConsoleCtrlHandler((PHANDLER_ROUTINE)VDMCtrlCHandler,TRUE)
	    == FALSE) {
        VDprint(
            VDP_LEVEL_INFO, 
	    ("CtrlC Handler Could'nt be installed\n")
	    );
    }

    // Tell the console that we want the last event (i.e when the
    // window is just to be destroyed.
    SetLastConsoleEventActive();

    // Check if the VDM Is for WOW
    while (--temp_argc > 0) {
	psz = *++temp_argv;
	if (*psz == '-' || *psz == '/') {
	    psz++;
	    if(tolower(*psz) == 'w'){
		VDMForWOW = TRUE;
		break;
	    }
	}
    }

    // This following API is required for recovery purposes. This
    // tells the basesrv that VDM has hooked ctrlc event. After
    // this it will always get the termination notification. If
    // the window is killed before we hook ctrl-c then basesrv
    // will know that data structures for this VDM has to be
    // freed. This should be the first call to base.dll.

    VDMOperationStarted (VDMForWOW);

    EmDirectory = NULL;

    // Hide the cmd window of WOWVDM
    if(VDMForWOW)
	VDMConsoleOperation((DWORD)VDM_HIDE_WINDOW);

    ParseSwitches( argc, argv );

    IsFirst = GetNextVDMCommand(NULL);

    for (i = 0; i < argc; i++) {
        VDprint(
            VDP_LEVEL_INFO, 
            ("%s\n", argv[i])
            );
    }

    VDbreak(VDB_LEVEL_INFO);

    if (EmDirectory == NULL) {
        usage();
	TerminateVDM();
    }

    // Sudeepb 26-Dec-1991 Temporary code to make
    // the life easy for WOW's internal users such that they dont have
    // to change the config.sys as per their setup.

    FixConfigFile (EmDirectory,IsFirst);

    pszNULL = strchr(EmDirectory,'\0');
    psz = EmDirectory;
    while(*psz == ' ' || *psz == '\t')
	psz++;

    BootLetter = *psz;

    host_cpu_init();
    sas_init(1024L * 1024L + 64L * 1024L);

    // Initialize ROM support

    BiosInit(argc, argv);

    // Initialize console support

    if (!ConInit()) {
        VDprint(
            VDP_LEVEL_ERROR,
	    ("SoftPC: error initializing console\n")
            );
	TerminateVDM();
    }

    // Initialize WOW


    CMDInit (argc,argv);

    // Initialize DOSEm

    if(!DemInit (argc,argv,EmDirectory)) {
        VDprint(
            VDP_LEVEL_ERROR,
	    ("SoftPC: error initializing DOSEm\n")
            );
	TerminateVDM();
    }

    // Initialize XMS
    
    if(!XMSInit (argc,argv)) {
        VDprint(
            VDP_LEVEL_ERROR,
	    ("SoftPC: error initializing XMS\n")
            );
	TerminateVDM();
    }

    // Initialize DBG
    
    if(!DBGInit (argc,argv)) {
        VDprint(
            VDP_LEVEL_ERROR,
	    ("SoftPC: error initializing DBG\n")
            );
	TerminateVDM();
    }

    // Prepare to load ntio.sys
    strcat (EmDirectory,"\\ntio.sys");
    hfile = CreateFile(EmDirectory,
                        GENERIC_READ, 
                        FILE_SHARE_READ,
                        NULL,
                        OPEN_EXISTING,
                        FILE_ATTRIBUTE_NORMAL,
                        NULL );

    if (hfile == (HANDLE)0xffffffff) {
        VDprint(
            VDP_LEVEL_ERROR,
            ("SoftPC: could not open file %s.  Error %d\n",
	    EmDirectory,
            GetLastError())
            );
	TerminateVDM();
    }

    FileAddress = (PCHAR)0x700;
    BytesRead = 1;
    while (BytesRead) {
        if (!ReadFile(hfile, FileAddress, 16384, &BytesRead, NULL)) {
            VDprint(
                VDP_LEVEL_ERROR,
                ("SoftPC: read failure on file %s.  Error %d\n",
		EmDirectory,
                GetLastError())
                );
	    TerminateVDM();
        }

        VDprint(VDP_LEVEL_INFO, 
            ("SoftPC: read a block of file %s\n", 
	    EmDirectory)
	    );
	FileAddress = (PCHAR)FileAddress + BytesRead;
    }

    VDprint(VDP_LEVEL_WARNING, 
        ("SoftPC: using Emulation file %s\n", 
	EmDirectory)
        );
    
    VDbreak(VDB_LEVEL_INFO);

    CloseHandle (hfile);

    // restore the emulation directory
    *pszNULL = 0;


    IntelRegisters.Eip = 0x0;
    IntelRegisters.SegCs = 0x70;
    IntelRegisters.EFlags = VDM_VIRTUAL_INTERRUPTS;

    host_simulate();

    if (IntelRegisters.EFlags & VDM_VIRTUAL_INTERRUPTS) {
        VDprint(VDP_LEVEL_INFO, ("Virtual ints enabled\n"));
    } else {
        VDprint(VDP_LEVEL_INFO, ("Virtual ints disabled\n"));
    }

    DumpIntelRegs();
}

#define MAX_CONFIG_SIZE  1024

VOID FixConfigFile (pszBin86Dir,IsFirstVDM)
PSZ  pszBin86Dir;
BOOL IsFirstVDM;
{

    // Temporary code. To be thrown out once we have full configuration
    // and installation.

CHAR   ConfigFile[]="?:\\config.vdm";
CHAR   Buffer [MAX_CONFIG_SIZE];
DWORD  len,i;
DWORD  BytesRead,BytesWritten;
HANDLE hfile;

    if (IsFirstVDM == FALSE)
	return;

    ConfigFile[0] = *pszBin86Dir;
    hfile = CreateFile( ConfigFile,
			GENERIC_WRITE | GENERIC_READ,
                        FILE_SHARE_READ,
                        NULL,
                        OPEN_EXISTING,
                        FILE_ATTRIBUTE_NORMAL,
			NULL );
    if (hfile == (HANDLE)0xffffffff) {
	DbgPrint ("config.vdm is'nt found on the root drive of yout NT trre\n");
	return;
    }


    if (!ReadFile(hfile, Buffer, MAX_CONFIG_SIZE, &BytesRead, NULL)){
	DbgPrint ("config.vdm could'nt be read\n, %x\n",GetLastError ());
	CloseHandle (hfile);
	return;
    }

    if (BytesRead == MAX_CONFIG_SIZE) {
	DbgPrint ("config.vdm is too big, could'nt perform macro substitution\n");
	CloseHandle (hfile);
	return;
    }

    SetFilePointer (hfile,
		    0,
		    NULL,
		    FILE_BEGIN);

    len = strlen (pszBin86Dir);

    for (i=0; i < BytesRead; i++) {
	if (Buffer [i] != '@'){
	    WriteFile (hfile,
		       &Buffer[i],
		       1,
		       &BytesWritten,
		       NULL);
	}
	else {
	    WriteFile (hfile,
		       pszBin86Dir,
		       len,
		       &BytesWritten,
		       NULL);
	}
    }

    CloseHandle (hfile);
    return;
}

VOID DumpIntelRegs()
{

    VDprint(VDP_LEVEL_ERROR,("EAX = %lx\n",IntelRegisters.Eax));
    VDprint(VDP_LEVEL_ERROR,("Ebx = %lx\n",IntelRegisters.Ebx));
    VDprint(VDP_LEVEL_ERROR,("Ecx = %lx\n",IntelRegisters.Ecx));
    VDprint(VDP_LEVEL_ERROR,("Edx = %lx\n",IntelRegisters.Edx));
    VDprint(VDP_LEVEL_ERROR,("Esi = %lx\n",IntelRegisters.Esi));
    VDprint(VDP_LEVEL_ERROR,("Edi = %lx\n",IntelRegisters.Edi));
    VDprint(VDP_LEVEL_ERROR,("Ebp = %lx\n",IntelRegisters.Ebp));
    VDprint(VDP_LEVEL_ERROR,("SegDs = %lx\n",IntelRegisters.SegDs));
    VDprint(VDP_LEVEL_ERROR,("SegEs = %lx\n",IntelRegisters.SegEs));
    VDprint(VDP_LEVEL_ERROR,("SegFs = %lx\n",IntelRegisters.SegFs));
    VDprint(VDP_LEVEL_ERROR,("SegGs = %lx\n",IntelRegisters.SegGs));
    VDprint(VDP_LEVEL_ERROR,("EFlags = %lx\n",IntelRegisters.EFlags));
    VDprint(VDP_LEVEL_ERROR,("SS:Esp = %lx:",IntelRegisters.SegSs));
    VDprint(VDP_LEVEL_ERROR,("%lx\n",IntelRegisters.Esp));
    VDprint(VDP_LEVEL_ERROR,("CS:Eip = %lx:",IntelRegisters.SegCs));
    VDprint(VDP_LEVEL_ERROR,("%lx\n",IntelRegisters.Eip));
}

VOID ParseSwitches(
    int argc,
    char **argv
    )
{
    int i;

    for (i = 1; i < argc; i++){
        if ((argv[i][0] == '-') || (argv[i][0] == '/')) {

            switch (argv[i][1]) {
                
                case 's' :
                case 'S' :
                    sscanf(&argv[i][2], "%x", &VdmDebugLevel);
                    VDprint(
                        VDP_LEVEL_WARNING, 
                        ("VdmDebugLevel = %x\n", 
                        VdmDebugLevel)
                        );
                    break;
                
                case 'f' :
		case 'F' :
		    // Note this memory is freed by DEM.
		    if((EmDirectory = (PCHAR)malloc (strlen (&argv[i][2]) +
					  1 +
					  sizeof("\\ntdos.sys") +
					  1
					 )) == NULL){
			DbgPrint("SoftPC: Not Enough Memory \n");
			TerminateVDM();
		    }
		    strcpy(EmDirectory,&argv[i][2]);
                    break;

                case 't' :
                case 'T' :
                    flOptions |= OPT_TERMINAL;
		    break;
		case 'm' :
		case 'M' :
		    scaffMin = TRUE;
		    break;
		case 'w' :
		case 'W' :
		    scaffWow = TRUE;
	    }
        } else {
            break;
        }
    }
}

VOID usage()
{
    DbgPrint("SoftPC Usage:  softpc -F<emulation file> [-D#] [<drive>:=<virtual disk>] [dos command line]\n");
}


VOID TerminateVDM(void)
{

    if(VDMForWOW)
    // Kill everything for WOW VDM
	ExitVDM(VDMForWOW,(ULONG)-1);
    else
	ExitVDM(FALSE,0);
    ExitProcess (0);
}


DWORD SCSConsoleThread(LPVOID lp)
{


    SetEvent(SCSCreateEvent);
    BiosKbdReadLoop();
    return TRUE;
}


BOOL ConInit (VOID)
{
    DWORD SCSThreadId;
    HANDLE InputThread;
    OutputHandle = GetStdHandle(STD_OUTPUT_HANDLE);
    InputHandle = GetStdHandle(STD_INPUT_HANDLE);

    SCSCreateEvent = CreateEvent( NULL, TRUE, FALSE,NULL );

    InputThread = CreateThread(
        (LPSECURITY_ATTRIBUTES)0,
        8192,
	(LPTHREAD_START_ROUTINE)SCSConsoleThread,
        (LPVOID)0,
        STANDARD_RIGHTS_REQUIRED,
	&SCSThreadId
    );

    WaitForSingleObject(SCSCreateEvent, -1);

    CloseHandle(SCSCreateEvent);

    return TRUE;
}