2 * Init and cleanup code of libcaptive to be called by client application
3 * Copyright (C) 2002 Jan Kratochvil <project-captive@jankratochvil.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; exactly version 2 of June 1991 is required
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "captive/client.h" /* self */
23 #include "captive/ldr.h"
24 #include "captive/ldr_exports.h"
25 #include "captive/unicode.h"
26 #include "captive/rtl-file.h"
27 #include <glib/gtypes.h>
28 #include <glib/gmessages.h>
29 #include "reactos/internal/ldr.h"
30 #include "reactos/napi/types.h"
31 #include "reactos/internal/kd.h" /* for KDB_LOADDRIVER_HOOK */
33 #include <sys/mman.h> /* for PROT_READ, MAP_SHARED */
34 #include "reactos/ddk/kefuncs.h" /* for KeInitializeSpinLock() */
35 #include "reactos/internal/ntoskrnl.h" /* for RtlpInitNlsTables() and IoInit() */
36 #include "reactos/internal/ps.h" /* for PsInitProcessManagment() and PsInitThreadManagment() */
37 #include "reactos/ddk/iofuncs.h" /* for IoCreateFile() */
38 #include "captive/storage.h"
39 #include "captive/signal.h" /* for captive_signal_init() */
40 #include "reactos/ddk/psfuncs.h" /* for PsGetCurrentThread() */
43 #include <glib/gstrfuncs.h>
44 #include <glib/glist.h>
45 #include <glib/gutils.h> /* for g_atexit() */
46 #include "giochannel-blind.h"
49 /* Are we initialized? */
50 static gboolean active;
52 /* Module of fs module itself loaded by captive_w32_init() */
53 static PMODULE_OBJECT ModuleObject;
55 /* Driver in fs module loaded by captive_w32_init() */
56 static DRIVER_OBJECT DriverObject;
58 /* Structure holding the pointer to the toplevel IRP */
59 static TOP_LEVEL_IRP TopLevelIrp; /* TODO:thread */
62 gchar *captive_option_filesystem;
63 enum captive_option_rwmode captive_option_rwmode=CAPTIVE_OPTION_RWMODE_BLIND;
64 enum captive_option_media captive_option_media=CAPTIVE_OPTION_MEDIA_DISK;
65 gboolean captive_debug_messages=FALSE;
66 GIOChannel *captive_image_iochannel;
67 /* Non-blinded original GIOChannel for flushing during shutdown. */
68 static GIOChannel *captive_image_iochannel_orig;
69 guint64 captive_image_size;
70 static GList *captive_load_module;
73 static gchar *captive_popt_optarg;
76 static void arg_filesystem(void)
78 g_free(captive_option_filesystem);
79 captive_option_filesystem=g_strdup(captive_popt_optarg);
82 static void arg_load_module(void)
84 captive_load_module=g_list_append(captive_load_module,g_strdup(captive_popt_optarg));
87 static void arg_ro(void)
89 captive_option_rwmode=CAPTIVE_OPTION_RWMODE_RO;
91 static void arg_blind(void)
93 captive_option_rwmode=CAPTIVE_OPTION_RWMODE_BLIND;
95 static void arg_rw(void)
97 captive_option_rwmode=CAPTIVE_OPTION_RWMODE_RW;
100 static void arg_cdrom(void)
102 captive_option_media=CAPTIVE_OPTION_MEDIA_CDROM;
104 static void arg_disk(void)
106 captive_option_media=CAPTIVE_OPTION_MEDIA_DISK;
109 static void arg_debug_messages(void)
111 captive_debug_messages=TRUE;
115 static void captive_popt_callback
116 (poptContext con,enum poptCallbackReason reason,const struct poptOption *opt,const char *arg,const void *data);
118 const struct poptOption captive_popt[]={
119 { argInfo:POPT_ARG_INTL_DOMAIN, arg:(void *)PACKAGE },
120 { argInfo:POPT_ARG_CALLBACK, arg:(void *)captive_popt_callback },
121 #define POPT_OFFSET 2
122 #define CAPTIVE_POPT(longname,argInfoP,argP,descripP,argDescripP) \
124 longName: (longname), \
126 argInfo: (argInfoP), \
129 descrip: (descripP), \
130 argDescrip: (argDescripP), \
132 #define CAPTIVE_POPT_STRING(longname,descripP,argDescripP) \
133 CAPTIVE_POPT(longname,POPT_ARG_STRING,&captive_popt_optarg,descripP,argDescripP)
134 #define CAPTIVE_POPT_NONE(longname,descripP) \
135 CAPTIVE_POPT(longname,POPT_ARG_NONE ,NULL ,descripP,NULL )
137 CAPTIVE_POPT_STRING("filesystem" ,N_("Path to .sys or .so filesystem module file"),N_("pathname")),
138 CAPTIVE_POPT_STRING("load-module" ,N_("Path to any W32 module to load w/o initialization"),N_("pathname")),
139 CAPTIVE_POPT_NONE( "ro" ,N_("Read/write mode: Any write access will be forbidden")),
140 CAPTIVE_POPT_NONE( "blind" ,N_("Read/write mode: All writes are just simulated in memory (default)")),
141 CAPTIVE_POPT_NONE( "rw" ,N_("Read/write mode: Write directly to the image file/device")),
142 CAPTIVE_POPT_NONE( "cdrom" ,N_("Media type: CD-ROM")),
143 CAPTIVE_POPT_NONE( "disk" ,N_("Media type: Disk (default)")),
144 CAPTIVE_POPT_NONE( "debug-messages",N_("Turn on debugging messages")),
146 #undef CAPTIVE_POPT_NONE
147 #undef CAPTIVE_POPT_STRING
152 static const struct poptOption captive_popt_standalone[]={
153 CAPTIVE_POPT_INCLUDE,
159 static void (*const popt_func_table[])(void)={
171 /* poptCallbackType captive_popt_callback */
172 static void captive_popt_callback
173 (poptContext con,enum poptCallbackReason reason,const struct poptOption *opt,const char *arg,const void *data)
177 g_return_if_fail(reason==POPT_CALLBACK_REASON_OPTION);
179 funci=(opt-(captive_popt+POPT_OFFSET));
180 g_return_if_fail(funci>=0);
181 g_return_if_fail(funci<(gint)G_N_ELEMENTS(popt_func_table));
182 if (popt_func_table[funci])
183 (*popt_func_table[funci])();
184 free(captive_popt_optarg);
185 captive_popt_optarg=NULL; /* sanity, shouldn't be needed */
189 static gboolean captive_w32_init(void)
193 GIOStatus erriostatus;
195 g_return_val_if_fail(captive_option_filesystem!=NULL,FALSE);
197 erriostatus=g_io_channel_set_encoding(captive_image_iochannel,
198 NULL, /* encoding; force binary data */
200 g_assert(erriostatus==G_IO_STATUS_NORMAL);
202 /* captive_giochannel_size() only _after_ g_io_channel_set_encoding() ! */
203 captive_image_size=captive_giochannel_size(captive_image_iochannel);
204 g_return_val_if_fail(captive_image_size>0,FALSE);
206 /* Initialize 'FsRtlLegalAnsiCharacterArray'. */
207 captive_FsRtlLegalAnsiCharacterArray_init();
209 /* Part of reactos/ntoskrnl/ke/main.c/KiSystemStartup() begins. */
210 /* ExpInitializeExecutive(); */
211 /* Part of reactos/ntoskrnl/ke/main.c/ExpInitializeExecutive() begins
212 * here as the rest of the function does a lot of hardware initializations.
215 /* Part of reactos/ntoskrnl/ldr/loader.c/LdrInit1() begins. */
216 InitializeListHead(&ModuleTextListHead);
217 /* Part of reactos/ntoskrnl/ldr/loader.c/LdrInit1() ends. */
218 KeLowerIrql(DISPATCH_LEVEL);
220 KeLowerIrql(PASSIVE_LEVEL);
222 /* create default nls tables */
227 /* PiInitProcessManager(); */
228 /* Part of reactos/ntoskrnl/ps/psmgr.c/PiInitProcessManager() begins. */
229 PsInitProcessManagment();
230 PsInitThreadManagment();
231 /* Part of reactos/ntoskrnl/ps/psmgr.c/PiInitProcessManager() ends. */
235 /* LdrInitModuleManagement(); */
236 /* Part of reactos/ntoskrnl/ldr/loader.c/LdrInitModuleManagement() begins
237 * here as the rest "Create module object for {NTOSKRNL,HAL}"
238 * is dependent on {NTOSKRNL,HAL} PE image headers not provided by libcaptive.
240 /* Initialize the module list and spinlock */
241 InitializeListHead(&ModuleListHead);
242 KeInitializeSpinLock(&ModuleListLock);
243 /* Part of reactos/ntoskrnl/ldr/loader.c/LdrInitModuleManagement ends. */
244 /* Part of reactos/ntoskrnl/ke/main.c/ExpInitializeExecutive() ends. */
245 /* Part of reactos/ntoskrnl/ke/main.c/KiSystemStartup() ends. */
247 /* Simulate our PE headers and export the symbols of our complete libraries */
248 captive_kernel_exports();
250 errbool=captive_cdrom_init();
251 g_return_val_if_fail(errbool==TRUE,FALSE);
252 errbool=captive_disk_init();
253 g_return_val_if_fail(errbool==TRUE,FALSE);
255 while (captive_load_module) {
256 gchar *modulename=captive_load_module->data;
257 PMODULE_OBJECT ModuleObject_tmp;
260 /* load the module */
262 captive_utf8_to_UnicodeString_alloca(modulename), /* ModuleName */
263 &ModuleObject_tmp); /* ModuleObjectp */
264 g_return_val_if_fail(NT_SUCCESS(err),FALSE);
266 captive_load_module=g_list_remove(captive_load_module,modulename);
269 /* Patch 'ntoskrnl.exe' loaded by 'captive_load_module' above. */
270 captive_kernel_patches();
272 /* set TopLevelIrp() - FIXME: where is it set by native reactos? */
273 PsGetCurrentThread()->TopLevelIrp=&TopLevelIrp; /* otherwise Io{Get,Set}TopLevelIrp() would SIGSEGV */
275 /* Begin possible handling of foreign W32 binary code here */
276 /* If you want to disable SIGSEGV handler if not needed:
277 * if (ModuleObject->Flags & MODULE_FLAG_PE)
279 captive_signal_init();
281 /* You must have already captive_signal_init() passed here as the module may
282 * call some functions from W32 ntoskrnl.exe.
284 err=captive_LdrpLoadAndCallImage(
285 &ModuleObject, /* ModuleObjectp */
286 captive_utf8_to_UnicodeString_alloca(captive_option_filesystem), /* ModuleName */
287 &DriverObject, /* DriverEntry_DriverObject */
288 captive_utf8_to_UnicodeString_alloca("\\captive\\filesystem")); /* DriverEntry_RegistryPath */
289 g_return_val_if_fail(NT_SUCCESS(err),FALSE);
295 static void captive_shutdown_atexit(void);
298 static void log_discard_func(const gchar *log_domain,GLogLevelFlags log_level,const gchar *message,gpointer user_data)
306 * @captive_args: String with possible options to parse by popt.
307 * %NULL value is permitted.
308 * @image_iochannel: Host OS file of the disk image to mount.
309 * %NULL value is permitted (initialization would not apply in such case).
311 * Initializes %libcaptive and loads the specified filesystem.
312 * You can supply %NULL value for @image_iochannel - in such case no library
313 * initialization is done; only passed @captive_args are parsed. Function
314 * will return you %FALSE value as it is not yet initializied.
316 * You should supply only @captive_args with %NULL @image_iochannel if you
317 * need to parse+examine the arguments to properly initialize @image_iochannel.
319 * Returns: %TRUE if successfuly initialized.
321 gboolean captive_init(const gchar *captive_args,GIOChannel *image_iochannel)
326 #ifdef MAINTAINER_MODE
327 g_log_set_always_fatal(~(0
334 g_return_val_if_fail(active==FALSE,FALSE);
336 /* (optionally) parse the given 'captive_args' string */
338 int captive_args_argc;
339 const char **captive_args_argv=NULL;
343 errint=poptParseArgvString(captive_args,&captive_args_argc,&captive_args_argv);
345 g_assert_not_reached(); /* argument parsing args_error */
348 context=poptGetContext(
350 captive_args_argc,captive_args_argv, /* argc,argv */
351 captive_popt_standalone, /* options */
352 POPT_CONTEXT_KEEP_FIRST);
354 g_assert_not_reached(); /* argument recognization args_error */
357 errint=poptReadDefaultConfig(context,
360 g_assert_not_reached(); /* argument recognization args_error */
361 goto args_err_context;
363 errint=poptGetNextOpt(context);
365 g_assert_not_reached(); /* some non-callbacked argument reached */
366 goto args_err_context;
368 /* FIXME: reject non-"--"-prefixed arguments; how to detected them? */
369 r=TRUE; /* success */
371 poptFreeContext(context);
373 free(captive_args_argv); /* may be NULL here */
376 puts("FIXME: HELP HERE");
377 g_return_val_if_reached(r);
381 /* If we were just requested to parse the arguments. */
382 if (!image_iochannel)
385 if (!captive_debug_messages) {
387 G_LOG_DOMAIN, /* log_domain; "Captive" */
389 | G_LOG_FLAG_RECURSION
391 | G_LOG_LEVEL_MESSAGE
394 log_discard_func, /* log_func */
395 NULL); /* user_data */
398 if (captive_option_rwmode==CAPTIVE_OPTION_RWMODE_BLIND)
399 captive_image_iochannel=(GIOChannel *)captive_giochannel_blind_new(image_iochannel);
401 captive_image_iochannel=image_iochannel;
402 captive_image_iochannel_orig=image_iochannel;
404 /* Do not initialize 'captive_image_size' by captive_giochannel_size() here
405 * as we yet need to do g_io_channel_set_encoding().
408 errbool=captive_w32_init();
409 g_return_val_if_fail(errbool==TRUE,FALSE);
411 g_atexit(captive_shutdown_atexit);
421 * Closes down %libcaptive. It should flush all pending buffers and successfuly
422 * close the filesystem. Variable #captive_image_iochannel will be set to %NULL,
423 * you should close such channel yourself.
425 * Returns: %TRUE if successfuly shutdown.
427 gboolean captive_shutdown(void)
429 GIOStatus erriostatus;
431 g_return_val_if_fail(active==TRUE,FALSE);
432 g_return_val_if_fail(captive_image_iochannel!=NULL,FALSE);
434 /* FIXME: ntoskrnl/ex/power.c/NtShutdownSystem() does
435 * IoShutdownRegistered{Devices,FileSystems} order; is it correct?
437 IoShutdownRegisteredFileSystems();
438 IoShutdownRegisteredDevices();
440 /* libcaptive is not authorized to shutdown 'captive_image_channel'. */
441 erriostatus=g_io_channel_flush(
442 captive_image_iochannel, /* channel */
444 g_assert(erriostatus==G_IO_STATUS_NORMAL);
446 /* 'captive_image_iochannel' may be blinded wrapper. */
447 if (captive_image_iochannel!=captive_image_iochannel_orig) {
448 erriostatus=g_io_channel_flush(
449 captive_image_iochannel_orig, /* channel */
451 g_assert(erriostatus==G_IO_STATUS_NORMAL);
454 captive_image_iochannel=NULL;
461 static void captive_shutdown_atexit(void)