Small fix.
[wine/multimedia.git] / if1632 / builtin.c
blobfc2548235fe89b7e2b681fd79ac56b7ab10f8ce2
1 /*
2 * Built-in modules
4 * Copyright 1996 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <ctype.h>
9 #include <string.h>
10 #include "windows.h"
11 #include "builtin32.h"
12 #include "gdi.h"
13 #include "global.h"
14 #include "heap.h"
15 #include "module.h"
16 #include "miscemu.h"
17 #include "neexe.h"
18 #include "stackframe.h"
19 #include "user.h"
20 #include "process.h"
21 #include "snoop.h"
22 #include "task.h"
23 #include "debug.h"
25 /* Built-in modules descriptors */
26 /* Don't change these structures! (see tools/build.c) */
28 typedef struct
30 const char *name; /* DLL name */
31 void *module_start; /* 32-bit address of the module data */
32 int module_size; /* Size of the module data */
33 const BYTE *code_start; /* 32-bit address of DLL code */
34 const BYTE *data_start; /* 32-bit address of DLL data */
35 } WIN16_DESCRIPTOR;
37 typedef struct
39 const WIN16_DESCRIPTOR *descr; /* DLL descriptor */
40 int flags; /* flags (see below) */
41 } BUILTIN16_DLL;
43 /* DLL flags */
44 #define DLL_FLAG_NOT_USED 0x01 /* Use original Windows DLL if possible */
45 #define DLL_FLAG_ALWAYS_USED 0x02 /* Always use built-in DLL */
47 /* 16-bit DLLs */
49 extern const WIN16_DESCRIPTOR AVIFILE_Descriptor;
50 extern const WIN16_DESCRIPTOR COMMDLG_Descriptor;
51 extern const WIN16_DESCRIPTOR COMPOBJ_Descriptor;
52 extern const WIN16_DESCRIPTOR DDEML_Descriptor;
53 extern const WIN16_DESCRIPTOR DISPLAY_Descriptor;
54 extern const WIN16_DESCRIPTOR GDI_Descriptor;
55 extern const WIN16_DESCRIPTOR KERNEL_Descriptor;
56 extern const WIN16_DESCRIPTOR KEYBOARD_Descriptor;
57 extern const WIN16_DESCRIPTOR LZEXPAND_Descriptor;
58 extern const WIN16_DESCRIPTOR MMSYSTEM_Descriptor;
59 extern const WIN16_DESCRIPTOR MOUSE_Descriptor;
60 extern const WIN16_DESCRIPTOR MSACM_Descriptor;
61 extern const WIN16_DESCRIPTOR MSVIDEO_Descriptor;
62 extern const WIN16_DESCRIPTOR OLE2CONV_Descriptor;
63 extern const WIN16_DESCRIPTOR OLE2DISP_Descriptor;
64 extern const WIN16_DESCRIPTOR OLE2NLS_Descriptor;
65 extern const WIN16_DESCRIPTOR OLE2PROX_Descriptor;
66 extern const WIN16_DESCRIPTOR OLE2THK_Descriptor;
67 extern const WIN16_DESCRIPTOR OLE2_Descriptor;
68 extern const WIN16_DESCRIPTOR OLECLI_Descriptor;
69 extern const WIN16_DESCRIPTOR OLESVR_Descriptor;
70 extern const WIN16_DESCRIPTOR RASAPI16_Descriptor;
71 extern const WIN16_DESCRIPTOR SHELL_Descriptor;
72 extern const WIN16_DESCRIPTOR SOUND_Descriptor;
73 extern const WIN16_DESCRIPTOR STORAGE_Descriptor;
74 extern const WIN16_DESCRIPTOR STRESS_Descriptor;
75 extern const WIN16_DESCRIPTOR SYSTEM_Descriptor;
76 extern const WIN16_DESCRIPTOR TOOLHELP_Descriptor;
77 extern const WIN16_DESCRIPTOR TYPELIB_Descriptor;
78 extern const WIN16_DESCRIPTOR USER_Descriptor;
79 extern const WIN16_DESCRIPTOR VER_Descriptor;
80 extern const WIN16_DESCRIPTOR W32SYS_Descriptor;
81 extern const WIN16_DESCRIPTOR WIN32S16_Descriptor;
82 extern const WIN16_DESCRIPTOR WIN87EM_Descriptor;
83 extern const WIN16_DESCRIPTOR WINASPI_Descriptor;
84 extern const WIN16_DESCRIPTOR WINDEBUG_Descriptor;
85 extern const WIN16_DESCRIPTOR WINEPS_Descriptor;
86 extern const WIN16_DESCRIPTOR WING_Descriptor;
87 extern const WIN16_DESCRIPTOR WINSOCK_Descriptor;
88 extern const WIN16_DESCRIPTOR WPROCS_Descriptor;
90 /* Table of all built-in DLLs */
92 static BUILTIN16_DLL BuiltinDLLs[] =
94 { &KERNEL_Descriptor, DLL_FLAG_ALWAYS_USED },
95 { &USER_Descriptor, DLL_FLAG_ALWAYS_USED },
96 { &GDI_Descriptor, DLL_FLAG_ALWAYS_USED },
97 { &SYSTEM_Descriptor, DLL_FLAG_ALWAYS_USED },
98 { &DISPLAY_Descriptor, DLL_FLAG_ALWAYS_USED },
99 { &WPROCS_Descriptor, DLL_FLAG_ALWAYS_USED },
100 { &WINDEBUG_Descriptor, DLL_FLAG_NOT_USED },
101 { &AVIFILE_Descriptor, DLL_FLAG_NOT_USED },
102 { &COMMDLG_Descriptor, DLL_FLAG_NOT_USED },
103 { &COMPOBJ_Descriptor, DLL_FLAG_NOT_USED },
104 { &DDEML_Descriptor, DLL_FLAG_NOT_USED },
105 { &KEYBOARD_Descriptor, 0 },
106 { &LZEXPAND_Descriptor, 0 },
107 { &MMSYSTEM_Descriptor, 0 },
108 { &MOUSE_Descriptor, 0 },
109 { &MSACM_Descriptor, 0 },
110 { &MSVIDEO_Descriptor, 0 },
111 { &OLE2CONV_Descriptor, DLL_FLAG_NOT_USED },
112 { &OLE2DISP_Descriptor, DLL_FLAG_NOT_USED },
113 { &OLE2NLS_Descriptor, DLL_FLAG_NOT_USED },
114 { &OLE2PROX_Descriptor, DLL_FLAG_NOT_USED },
115 { &OLE2THK_Descriptor, DLL_FLAG_NOT_USED },
116 { &OLE2_Descriptor, DLL_FLAG_NOT_USED },
117 { &OLECLI_Descriptor, DLL_FLAG_NOT_USED },
118 { &OLESVR_Descriptor, DLL_FLAG_NOT_USED },
119 { &RASAPI16_Descriptor, 0 },
120 { &SHELL_Descriptor, 0 },
121 { &SOUND_Descriptor, 0 },
122 { &STORAGE_Descriptor, DLL_FLAG_NOT_USED },
123 { &STRESS_Descriptor, 0 },
124 { &TOOLHELP_Descriptor, 0 },
125 { &TYPELIB_Descriptor, DLL_FLAG_NOT_USED },
126 { &VER_Descriptor, 0 },
127 { &W32SYS_Descriptor, DLL_FLAG_NOT_USED },
128 { &WIN32S16_Descriptor, DLL_FLAG_NOT_USED },
129 { &WIN87EM_Descriptor, DLL_FLAG_NOT_USED },
130 { &WINASPI_Descriptor, 0 },
131 { &WINEPS_Descriptor, DLL_FLAG_ALWAYS_USED },
132 { &WING_Descriptor, 0 },
133 { &WINSOCK_Descriptor, 0 },
134 /* Last entry */
135 { NULL, 0 }
138 /* Ordinal number for interrupt 0 handler in WPROCS.DLL */
139 #define FIRST_INTERRUPT_ORDINAL 100
142 /***********************************************************************
143 * BUILTIN_DoLoadModule16
145 * Load a built-in Win16 module. Helper function for BUILTIN_LoadModule
146 * and BUILTIN_Init.
148 static HMODULE16 BUILTIN_DoLoadModule16( const WIN16_DESCRIPTOR *descr )
150 NE_MODULE *pModule;
151 int minsize;
152 SEGTABLEENTRY *pSegTable;
154 HMODULE16 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, descr->module_start,
155 descr->module_size, 0,
156 FALSE, FALSE, FALSE, NULL );
157 if (!hModule) return 0;
158 FarSetOwner( hModule, hModule );
160 TRACE(module, "Built-in %s: hmodule=%04x\n",
161 descr->name, hModule );
162 pModule = (NE_MODULE *)GlobalLock16( hModule );
163 pModule->self = hModule;
165 /* Allocate the code segment */
167 pSegTable = NE_SEG_TABLE( pModule );
168 pSegTable->hSeg = GLOBAL_CreateBlock( GMEM_FIXED, descr->code_start,
169 pSegTable->minsize, hModule,
170 TRUE, TRUE, FALSE, NULL );
171 if (!pSegTable->hSeg) return 0;
172 pSegTable++;
174 /* Allocate the data segment */
176 minsize = pSegTable->minsize ? pSegTable->minsize : 0x10000;
177 minsize += pModule->heap_size;
178 if (minsize > 0x10000) minsize = 0x10000;
179 pSegTable->hSeg = GLOBAL_Alloc( GMEM_FIXED, minsize,
180 hModule, FALSE, FALSE, FALSE );
181 if (!pSegTable->hSeg) return 0;
182 if (pSegTable->minsize) memcpy( GlobalLock16( pSegTable->hSeg ),
183 descr->data_start, pSegTable->minsize);
184 if (pModule->heap_size)
185 LocalInit( GlobalHandleToSel(pSegTable->hSeg),
186 pSegTable->minsize, minsize );
188 NE_RegisterModule( pModule );
189 return hModule;
193 /***********************************************************************
194 * BUILTIN_Init
196 * Load all built-in modules marked as 'always used'.
198 BOOL32 BUILTIN_Init(void)
200 BUILTIN16_DLL *dll;
201 NE_MODULE *pModule;
202 WORD vector;
203 HMODULE16 hModule;
204 WORD cs, ds;
206 fnBUILTIN_LoadModule = BUILTIN_LoadModule;
208 for (dll = BuiltinDLLs; dll->descr; dll++)
210 if (dll->flags & DLL_FLAG_ALWAYS_USED)
211 if (!BUILTIN_DoLoadModule16( dll->descr )) return FALSE;
214 /* Set the USER and GDI heap selectors */
216 pModule = NE_GetPtr( GetModuleHandle16( "USER" ));
217 USER_HeapSel = pModule ? GlobalHandleToSel((NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->hSeg) : 0;
218 pModule = NE_GetPtr( GetModuleHandle16( "GDI" ));
219 GDI_HeapSel = pModule ? GlobalHandleToSel((NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->hSeg) : 0;
221 /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
223 hModule = GetModuleHandle16( "KERNEL" );
224 NE_SetEntryPoint( hModule, 178, GetWinFlags() );
226 /* Initialize KERNEL.454/455 (__FLATCS/__FLATDS) */
228 GET_CS(cs); GET_DS(ds);
229 NE_SetEntryPoint( hModule, 454, cs );
230 NE_SetEntryPoint( hModule, 455, ds );
232 /* Initialize KERNEL.THHOOK */
234 TASK_InstallTHHook((THHOOK *)PTR_SEG_TO_LIN(
235 (SEGPTR)NE_GetEntryPoint( hModule, 332 )));
237 /* Initialize the real-mode selector entry points */
239 #define SET_ENTRY_POINT( num, addr ) \
240 NE_SetEntryPoint( hModule, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
241 DOSMEM_MapDosToLinear(addr), 0x10000, hModule, \
242 FALSE, FALSE, FALSE, NULL ))
244 SET_ENTRY_POINT( 183, 0x00000 ); /* KERNEL.183: __0000H */
245 SET_ENTRY_POINT( 174, 0xa0000 ); /* KERNEL.174: __A000H */
246 SET_ENTRY_POINT( 181, 0xb0000 ); /* KERNEL.181: __B000H */
247 SET_ENTRY_POINT( 182, 0xb8000 ); /* KERNEL.182: __B800H */
248 SET_ENTRY_POINT( 195, 0xc0000 ); /* KERNEL.195: __C000H */
249 SET_ENTRY_POINT( 179, 0xd0000 ); /* KERNEL.179: __D000H */
250 SET_ENTRY_POINT( 190, 0xe0000 ); /* KERNEL.190: __E000H */
251 SET_ENTRY_POINT( 173, 0xf0000 ); /* KERNEL.173: __ROMBIOS */
252 SET_ENTRY_POINT( 194, 0xf0000 ); /* KERNEL.194: __F000H */
253 NE_SetEntryPoint( hModule, 193, DOSMEM_BiosSeg ); /* KERNEL.193: __0040H */
254 #undef SET_ENTRY_POINT
256 /* Set interrupt vectors from entry points in WPROCS.DLL */
258 hModule = GetModuleHandle16( "WPROCS" );
259 for (vector = 0; vector < 256; vector++)
261 FARPROC16 proc = NE_GetEntryPoint( hModule,
262 FIRST_INTERRUPT_ORDINAL + vector );
263 assert(proc);
264 INT_SetPMHandler( vector, proc );
267 SNOOP16_Init();
269 return TRUE;
273 /***********************************************************************
274 * BUILTIN_LoadModule
276 * Load a built-in module. If the 'force' parameter is FALSE, we only
277 * load the module if it has not been disabled via the -dll option.
279 HMODULE16 BUILTIN_LoadModule( LPCSTR name, BOOL32 force )
281 BUILTIN16_DLL *table;
282 char dllname[16], *p;
284 /* Fix the name in case we have a full path and extension */
286 if ((p = strrchr( name, '\\' ))) name = p + 1;
287 lstrcpyn32A( dllname, name, sizeof(dllname) );
288 if ((p = strrchr( dllname, '.' ))) *p = '\0';
290 for (table = BuiltinDLLs; table->descr; table++)
291 if (!lstrcmpi32A( table->descr->name, dllname )) break;
292 if (!table->descr) return 0;
293 if ((table->flags & DLL_FLAG_NOT_USED) && !force) return 0;
295 return BUILTIN_DoLoadModule16( table->descr );
299 /***********************************************************************
300 * BUILTIN_GetEntryPoint16
302 * Return the ordinal and name corresponding to a CS:IP address.
303 * This is used only by relay debugging.
305 LPCSTR BUILTIN_GetEntryPoint16( WORD cs, WORD ip, WORD *pOrd )
307 static char buffer[80];
308 WORD ordinal, i, max_offset;
309 register BYTE *p;
310 NE_MODULE *pModule;
312 if (!(pModule = NE_GetPtr( FarGetOwner( GlobalHandle16(cs) ))))
313 return NULL;
315 /* Search for the ordinal */
317 p = (BYTE *)pModule + pModule->entry_table;
318 max_offset = 0;
319 ordinal = 1;
320 *pOrd = 0;
321 while (*p)
323 switch(p[1])
325 case 0: /* unused */
326 ordinal += *p;
327 p += 2;
328 break;
329 case 1: /* code segment */
330 i = *p;
331 p += 2;
332 while (i-- > 0)
334 p++;
335 if ((*(WORD *)p <= ip) && (*(WORD *)p >= max_offset))
337 max_offset = *(WORD *)p;
338 *pOrd = ordinal;
340 p += 2;
341 ordinal++;
343 break;
344 case 0xff: /* moveable (should not happen in built-in modules) */
345 TRACE( relay, "Built-in module has moveable entry\n" );
346 ordinal += *p;
347 p += 2 + *p * 6;
348 break;
349 default: /* other segment */
350 ordinal += *p;
351 p += 2 + *p * 3;
352 break;
356 /* Search for the name in the resident names table */
357 /* (built-in modules have no non-resident table) */
359 p = (BYTE *)pModule + pModule->name_table;
360 while (*p)
362 p += *p + 1 + sizeof(WORD);
363 if (*(WORD *)(p + *p + 1) == *pOrd) break;
366 sprintf( buffer, "%.*s.%d: %.*s",
367 *((BYTE *)pModule + pModule->name_table),
368 (char *)pModule + pModule->name_table + 1,
369 *pOrd, *p, (char *)(p + 1) );
370 return buffer;
374 /**********************************************************************
375 * BUILTIN_DefaultIntHandler
377 * Default interrupt handler.
379 void BUILTIN_DefaultIntHandler( CONTEXT *context )
381 WORD ordinal;
382 STACK16FRAME *frame = CURRENT_STACK16;
383 BUILTIN_GetEntryPoint16( frame->entry_cs, frame->entry_ip, &ordinal );
384 INT_BARF( context, ordinal - FIRST_INTERRUPT_ORDINAL );
388 /***********************************************************************
389 * BUILTIN_ParseDLLOptions
391 * Set runtime DLL usage flags
393 BOOL32 BUILTIN_ParseDLLOptions( const char *str )
395 BUILTIN16_DLL *dll;
396 const char *p;
398 while (*str)
400 while (*str && isspace(*str)) str++;
401 if (!*str) return TRUE;
402 if ((*str != '+') && (*str != '-')) return FALSE;
403 str++;
404 if (!(p = strchr( str, ',' ))) p = str + strlen(str);
405 while ((p > str) && isspace(p[-1])) p--;
406 if (p == str) return FALSE;
407 for (dll = BuiltinDLLs; dll->descr; dll++)
409 if (!lstrncmpi32A( str, dll->descr->name, (int)(p - str) ))
411 if (dll->descr->name[(int)(p-str)]) /* only partial match */
412 continue;
413 if (str[-1] == '-')
415 if (dll->flags & DLL_FLAG_ALWAYS_USED) return FALSE;
416 dll->flags |= DLL_FLAG_NOT_USED;
418 else dll->flags &= ~DLL_FLAG_NOT_USED;
419 break;
422 if (!dll->descr)
423 if (!BUILTIN32_EnableDLL( str, (int)(p - str), (str[-1] == '+') ))
424 return FALSE;
425 str = p;
426 while (*str && (isspace(*str) || (*str == ','))) str++;
428 return TRUE;
432 /***********************************************************************
433 * BUILTIN_PrintDLLs
435 * Print the list of built-in DLLs that can be disabled.
437 void BUILTIN_PrintDLLs(void)
439 int i;
440 BUILTIN16_DLL *dll;
442 MSG("Example: -dll -ole2 Do not use emulated OLE2.DLL\n");
443 MSG("Available Win16 DLLs:\n");
444 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
446 if (!(dll->flags & DLL_FLAG_ALWAYS_USED))
447 MSG("%-9s%c", dll->descr->name,
448 ((++i) % 8) ? ' ' : '\n' );
450 MSG("\n");
451 BUILTIN32_PrintDLLs();