Fixed other bugs within MMIO implementation. Now, it's possible to
[wine] / loader / main.c
1 /*
2  * Main initialization code
3  */
4
5 #include <assert.h>
6 #include <stdlib.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <errno.h>
13 #include "windef.h"
14 #include "wingdi.h"
15 #include "wine/winbase16.h"
16 #include "wine/winuser16.h"
17 #include "bitmap.h"
18 #include "comm.h"
19 #include "neexe.h"
20 #include "main.h"
21 #include "menu.h"
22 #include "message.h"
23 #include "dialog.h"
24 #include "drive.h"
25 #include "queue.h"
26 #include "sysmetrics.h"
27 #include "file.h"
28 #include "heap.h"
29 #include "keyboard.h"
30 #include "mouse.h"
31 #include "input.h"
32 #include "display.h"
33 #include "miscemu.h"
34 #include "options.h"
35 #include "process.h"
36 #include "spy.h"
37 #include "tweak.h"
38 #include "user.h"
39 #include "cursoricon.h"
40 #include "global.h"
41 #include "dce.h"
42 #include "shell.h"
43 #include "win.h"
44 #include "winproc.h"
45 #include "syslevel.h"
46 #include "services.h"
47 #include "winsock.h"
48 #include "selectors.h"
49 #include "thread.h"
50 #include "task.h"
51 #include "debugtools.h"
52 #include "psdrv.h"
53 #include "win16drv.h"
54 #include "callback.h"
55 #include "server.h"
56 #include "loadorder.h"
57
58 DEFAULT_DEBUG_CHANNEL(server)
59
60 /***********************************************************************
61  *           Main initialisation routine
62  */
63 BOOL MAIN_MainInit( int argc, char *argv[], BOOL win32 )
64 {
65     /* store the program name */
66     argv0 = argv[0];
67
68     /* Create the initial process */
69     if (!PROCESS_Init( win32 )) return 0;
70
71     /* Initialize syslevel handling */
72     SYSLEVEL_Init();
73
74     /* Parse command line arguments */
75     MAIN_WineInit( argc, argv );
76
77     /* Load the configuration file */
78     if (!PROFILE_LoadWineIni()) return FALSE;
79
80     /* Initialise DOS drives */
81     if (!DRIVE_Init()) return FALSE;
82
83     /* Initialise DOS directories */
84     if (!DIR_Init()) return FALSE;
85
86     /* Registry initialisation */
87     SHELL_LoadRegistry();
88     
89     /* Global boot finished, the rest is process-local */
90     CLIENT_BootDone( TRACE_ON(server) );
91
92     /* Initialize module loadorder */
93     if (!MODULE_InitLoadOrder()) return FALSE;
94
95     /* Initialize DOS memory */
96     if (!DOSMEM_Init(0)) return FALSE;
97
98     /* Initialize communications */
99     COMM_Init();
100
101     /* Initialize IO-port permissions */
102     IO_port_init();
103
104     /* Read DOS config.sys */
105     if (!DOSCONF_ReadConfig()) return FALSE;
106
107     /* Initialize KERNEL */
108     if (!LoadLibrary16( "KRNL386.EXE" )) return FALSE;
109     if (!LoadLibraryA( "KERNEL32" )) return FALSE;
110
111     if (!LoadLibraryA( "x11drv" )) return FALSE;
112
113     SHELL_InitRegistrySaving();
114
115     return TRUE;
116 }
117
118 /***********************************************************************
119  *           KERNEL initialisation routine
120  */
121 BOOL WINAPI MAIN_KernelInit(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
122 {
123     static BOOL initDone = FALSE;
124
125     HMODULE16 hModule;
126
127     if ( initDone ) return TRUE;
128     initDone = TRUE;
129
130     /* Initialize special KERNEL entry points */
131     hModule = GetModuleHandle16( "KERNEL" );
132     if ( hModule )
133     {
134         /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
135         NE_SetEntryPoint( hModule, 178, GetWinFlags16() );
136
137         /* Initialize KERNEL.454/455 (__FLATCS/__FLATDS) */
138         NE_SetEntryPoint( hModule, 454, __get_cs() );
139         NE_SetEntryPoint( hModule, 455, __get_ds() );
140
141         /* Initialize KERNEL.THHOOK */
142         TASK_InstallTHHook((THHOOK *)PTR_SEG_TO_LIN(
143                                   (SEGPTR)NE_GetEntryPoint( hModule, 332 )));
144
145         /* Initialize the real-mode selector entry points */
146 #define SET_ENTRY_POINT( num, addr ) \
147         NE_SetEntryPoint( hModule, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
148                           DOSMEM_MapDosToLinear(addr), 0x10000, hModule, \
149                           FALSE, FALSE, FALSE, NULL ))
150
151         SET_ENTRY_POINT( 183, 0x00000 );  /* KERNEL.183: __0000H */
152         SET_ENTRY_POINT( 174, 0xa0000 );  /* KERNEL.174: __A000H */
153         SET_ENTRY_POINT( 181, 0xb0000 );  /* KERNEL.181: __B000H */
154         SET_ENTRY_POINT( 182, 0xb8000 );  /* KERNEL.182: __B800H */
155         SET_ENTRY_POINT( 195, 0xc0000 );  /* KERNEL.195: __C000H */
156         SET_ENTRY_POINT( 179, 0xd0000 );  /* KERNEL.179: __D000H */
157         SET_ENTRY_POINT( 190, 0xe0000 );  /* KERNEL.190: __E000H */
158         NE_SetEntryPoint( hModule, 173, DOSMEM_BiosSysSeg );  /* KERNEL.173: __ROMBIOS */
159         NE_SetEntryPoint( hModule, 193, DOSMEM_BiosDataSeg ); /* KERNEL.193: __0040H */
160         NE_SetEntryPoint( hModule, 194, DOSMEM_BiosSysSeg );  /* KERNEL.194: __F000H */
161 #undef SET_ENTRY_POINT
162     }
163
164     /* Initialize relay code */
165     if (!RELAY_Init()) return FALSE;
166
167     return TRUE;
168 }
169
170
171 /***********************************************************************
172  *           Winelib initialisation routine
173  */
174 HINSTANCE MAIN_WinelibInit( int *argc, char *argv[] )
175 {
176     NE_MODULE *pModule;
177     HMODULE16 hModule;
178     PDB       *curr;
179
180     /* Main initialization */
181     if (!MAIN_MainInit( *argc, argv, TRUE )) return 0;
182     *argc = Options.argc;
183
184     /* Load WineLib EXE module */
185     if ( (hModule = BUILTIN32_LoadExeModule()) < 32 ) return 0;
186     pModule = (NE_MODULE *)GlobalLock16( hModule );
187
188     /* Create initial task */
189     if (!TASK_Create( pModule, FALSE )) return 0;
190
191     /* Create 32-bit MODREF */
192     if ( !PE_CreateModule( pModule->module32, NE_MODULE_NAME(pModule), 0, FALSE ) )
193         return 0;
194
195     /* Increment EXE refcount */
196     curr = PROCESS_Current();
197     assert( curr->exe_modref );
198     curr->exe_modref->refCount++;
199
200     /* Load system DLLs into the initial process (and initialize them) */
201     if (   !LoadLibrary16("GDI.EXE" ) || !LoadLibraryA("GDI32.DLL" )
202         || !LoadLibrary16("USER.EXE") || !LoadLibraryA("USER32.DLL"))
203         ExitProcess( 1 );
204
205     /* attach the imported DLLs */
206     if ( !MODULE_DllProcessAttach( curr->exe_modref, NULL ) )
207        ExitProcess( 1 );
208
209     /* Get pointers to USER routines called by KERNEL */
210     THUNK_InitCallout();
211
212     return pModule->module32;
213 }
214
215 /***********************************************************************
216  *           ExitKernel16 (KERNEL.2)
217  *
218  * Clean-up everything and exit the Wine process.
219  *
220  */
221 void WINAPI ExitKernel16( void )
222 {
223     /* Do the clean-up stuff */
224
225     WriteOutProfiles16();
226     SHELL_SaveRegistry();
227
228     TerminateProcess( GetCurrentProcess(), 0 );
229 }
230