Correct filename for kernel module is KRNL386.EXE.
[wine.git] / loader / main.c
blob4a4407e6418d006712845234c3298038283a0437
1 /*
2 * Main initialization code
3 */
5 #include <stdlib.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <string.h>
11 #include <errno.h>
12 #include "wine/winbase16.h"
13 #include "wine/winuser16.h"
14 #include "bitmap.h"
15 #include "comm.h"
16 #include "neexe.h"
17 #include "main.h"
18 #include "menu.h"
19 #include "message.h"
20 #include "dialog.h"
21 #include "drive.h"
22 #include "queue.h"
23 #include "sysmetrics.h"
24 #include "file.h"
25 #include "heap.h"
26 #include "keyboard.h"
27 #include "mouse.h"
28 #include "input.h"
29 #include "display.h"
30 #include "miscemu.h"
31 #include "options.h"
32 #include "process.h"
33 #include "spy.h"
34 #include "tweak.h"
35 #include "user.h"
36 #include "cursoricon.h"
37 #include "global.h"
38 #include "dce.h"
39 #include "shell.h"
40 #include "win.h"
41 #include "winproc.h"
42 #include "syslevel.h"
43 #include "services.h"
44 #include "winsock.h"
45 #include "thread.h"
46 #include "task.h"
47 #include "debugtools.h"
48 #include "psdrv.h"
49 #include "win16drv.h"
50 #include "callback.h"
51 #include "server.h"
52 #include "cursoricon.h"
53 #include "loadorder.h"
55 DEFAULT_DEBUG_CHANNEL(server)
57 /***********************************************************************
58 * Main initialisation routine
60 BOOL MAIN_MainInit(void)
62 /* Set server debug level */
63 CLIENT_SetDebug( TRACE_ON(server) );
65 /* Initialize syslevel handling */
66 SYSLEVEL_Init();
68 /* Load the configuration file */
69 if (!PROFILE_LoadWineIni()) return FALSE;
71 /* Initialize module loadorder */
72 if (!MODULE_InitLoadOrder()) return FALSE;
74 /* Initialize DOS memory */
75 if (!DOSMEM_Init(0)) return FALSE;
77 /* Initialise DOS drives */
78 if (!DRIVE_Init()) return FALSE;
80 /* Initialise DOS directories */
81 if (!DIR_Init()) return FALSE;
83 /* Initialize event handling */
84 if (!EVENT_Init()) return FALSE;
86 /* Initialise WINSOCK handling */
87 if (!WINSOCK_Init()) return FALSE;
89 /* Initialize communications */
90 COMM_Init();
92 /* Initialize IO-port permissions */
93 IO_port_init();
95 /* registry initialisation */
96 SHELL_LoadRegistry();
98 /* Read DOS config.sys */
99 if (!DOSCONF_ReadConfig()) return FALSE;
101 return TRUE;
104 /***********************************************************************
105 * KERNEL initialisation routine
107 BOOL WINAPI MAIN_KernelInit(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
109 static BOOL initDone = FALSE;
111 HMODULE16 hModule;
113 if ( initDone ) return TRUE;
114 initDone = TRUE;
116 /* Initialize special KERNEL entry points */
117 hModule = GetModuleHandle16( "KERNEL" );
118 if ( hModule )
120 WORD cs, ds;
122 /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
123 NE_SetEntryPoint( hModule, 178, GetWinFlags16() );
125 /* Initialize KERNEL.454/455 (__FLATCS/__FLATDS) */
126 GET_CS(cs); GET_DS(ds);
127 NE_SetEntryPoint( hModule, 454, cs );
128 NE_SetEntryPoint( hModule, 455, ds );
130 /* Initialize KERNEL.THHOOK */
131 TASK_InstallTHHook((THHOOK *)PTR_SEG_TO_LIN(
132 (SEGPTR)NE_GetEntryPoint( hModule, 332 )));
134 /* Initialize the real-mode selector entry points */
135 #define SET_ENTRY_POINT( num, addr ) \
136 NE_SetEntryPoint( hModule, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
137 DOSMEM_MapDosToLinear(addr), 0x10000, hModule, \
138 FALSE, FALSE, FALSE, NULL ))
140 SET_ENTRY_POINT( 183, 0x00000 ); /* KERNEL.183: __0000H */
141 SET_ENTRY_POINT( 174, 0xa0000 ); /* KERNEL.174: __A000H */
142 SET_ENTRY_POINT( 181, 0xb0000 ); /* KERNEL.181: __B000H */
143 SET_ENTRY_POINT( 182, 0xb8000 ); /* KERNEL.182: __B800H */
144 SET_ENTRY_POINT( 195, 0xc0000 ); /* KERNEL.195: __C000H */
145 SET_ENTRY_POINT( 179, 0xd0000 ); /* KERNEL.179: __D000H */
146 SET_ENTRY_POINT( 190, 0xe0000 ); /* KERNEL.190: __E000H */
147 NE_SetEntryPoint( hModule, 173, DOSMEM_BiosSysSeg ); /* KERNEL.173: __ROMBIOS */
148 NE_SetEntryPoint( hModule, 193, DOSMEM_BiosDataSeg ); /* KERNEL.193: __0040H */
149 NE_SetEntryPoint( hModule, 194, DOSMEM_BiosSysSeg ); /* KERNEL.194: __F000H */
150 #undef SET_ENTRY_POINT
153 /* Initialize relay code */
154 if (!RELAY_Init()) return FALSE;
156 return TRUE;
159 /***********************************************************************
160 * GDI initialisation routine
162 BOOL WINAPI MAIN_GdiInit(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
164 NE_MODULE *pModule;
166 if ( GDI_HeapSel ) return TRUE;
168 /* Create GDI heap */
169 pModule = NE_GetPtr( GetModuleHandle16( "GDI" ) );
170 if ( pModule )
172 GDI_HeapSel = GlobalHandleToSel16( (NE_SEG_TABLE( pModule ) +
173 pModule->dgroup - 1)->hSeg );
175 else
177 GDI_HeapSel = GlobalAlloc16( GMEM_FIXED, GDI_HEAP_SIZE );
178 LocalInit16( GDI_HeapSel, 0, GDI_HEAP_SIZE-1 );
181 if (!TWEAK_Init()) return FALSE;
183 /* GDI initialisation */
184 if(!GDI_Init()) return FALSE;
186 /* Create the Win16 printer driver */
187 if (!WIN16DRV_Init()) return FALSE;
189 /* PSDRV initialization */
190 if(!PSDRV_Init()) return FALSE;
192 return TRUE;
195 /***********************************************************************
196 * USER initialisation routine
198 BOOL WINAPI MAIN_UserInit(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
200 NE_MODULE *pModule;
201 int queueSize;
203 if ( USER_HeapSel ) return TRUE;
205 /* Create USER heap */
206 pModule = NE_GetPtr( GetModuleHandle16( "USER" ) );
207 if ( pModule )
209 USER_HeapSel = GlobalHandleToSel16( (NE_SEG_TABLE( pModule ) +
210 pModule->dgroup - 1)->hSeg );
212 else
214 USER_HeapSel = GlobalAlloc16( GMEM_FIXED, 0x10000 );
215 LocalInit16( USER_HeapSel, 0, 0xffff );
218 /* Global atom table initialisation */
219 if (!ATOM_Init( USER_HeapSel )) return FALSE;
221 /* Initialize window handling (critical section) */
222 WIN_Init();
224 /* Initialize system colors and metrics*/
225 SYSMETRICS_Init();
226 SYSCOLOR_Init();
228 /* Create the DCEs */
229 DCE_Init();
231 /* Initialize timers */
232 if (!TIMER_Init()) return FALSE;
234 /* Initialize window procedures */
235 if (!WINPROC_Init()) return FALSE;
237 /* Initialize cursor/icons */
238 CURSORICON_Init();
240 /* Initialize built-in window classes */
241 if (!WIDGETS_Init()) return FALSE;
243 /* Initialize dialog manager */
244 if (!DIALOG_Init()) return FALSE;
246 /* Initialize menus */
247 if (!MENU_Init()) return FALSE;
249 /* Initialize message spying */
250 if (!SPY_Init()) return FALSE;
252 /* Check wine.conf for old/bad entries */
253 if (!TWEAK_CheckConfiguration()) return FALSE;
255 /* Create system message queue */
256 queueSize = GetProfileIntA( "windows", "TypeAhead", 120 );
257 if (!QUEUE_CreateSysMsgQueue( queueSize )) return FALSE;
259 /* Set double click time */
260 SetDoubleClickTime( GetProfileIntA("windows","DoubleClickSpeed",452) );
262 /* Create message queue of initial thread */
263 InitThreadInput16( 0, 0 );
265 /* Create desktop window */
266 if (!WIN_CreateDesktopWindow()) return FALSE;
268 /* Initialize keyboard driver */
269 KEYBOARD_Enable( keybd_event, InputKeyStateTable );
271 /* Initialize mouse driver */
272 MOUSE_Enable( mouse_event );
274 /* Start processing X events */
275 UserRepaintDisable16( FALSE );
277 return TRUE;
281 /***********************************************************************
282 * Winelib initialisation routine
284 HINSTANCE MAIN_WinelibInit( int *argc, char *argv[] )
286 WINE_MODREF *wm;
287 NE_MODULE *pModule;
288 OFSTRUCT ofs;
289 HMODULE16 hModule;
291 /* Create the initial process */
292 if (!PROCESS_Init()) return 0;
294 /* Parse command line arguments */
295 MAIN_WineInit( argc, argv );
297 /* Main initialization */
298 if (!MAIN_MainInit()) return 0;
300 /* Initialize KERNEL */
301 if (!LoadLibrary16( "KRNL386.EXE" )) return 0;
302 if (!LoadLibraryA( "KERNEL32" )) return 0;
304 /* Create and switch to initial task */
305 if (!(wm = ELF_CreateDummyModule( argv[0], argv[0] )))
306 return 0;
307 PROCESS_Current()->exe_modref = wm;
309 strcpy( ofs.szPathName, wm->modname );
310 if ((hModule = MODULE_CreateDummyModule( &ofs, NULL )) < 32) return 0;
311 pModule = (NE_MODULE *)GlobalLock16( hModule );
312 pModule->flags = NE_FFLAGS_WIN32;
313 pModule->module32 = wm->module;
315 if (!TASK_Create( pModule, FALSE )) return 0;
317 /* Load system DLLs into the initial process (and initialize them) */
318 if ( !LoadLibrary16("GDI.EXE" ) || !LoadLibraryA("GDI32.DLL" )
319 || !LoadLibrary16("USER.EXE") || !LoadLibraryA("USER32.DLL"))
320 ExitProcess( 1 );
322 /* Get pointers to USER routines called by KERNEL */
323 THUNK_InitCallout();
325 return wm->module;
328 /***********************************************************************
329 * ExitKernel16 (KERNEL.2)
331 * Clean-up everything and exit the Wine process.
334 void WINAPI ExitKernel16( void )
336 /* Do the clean-up stuff */
338 WriteOutProfiles16();
339 SHELL_SaveRegistry();
341 TerminateProcess( GetCurrentProcess(), 0 );