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() */
42 #include "reactos/internal/ex.h" /* for ExpInitLookasideLists() */
44 #include <glib/gstrfuncs.h>
47 /* Are we initialized? */
48 static gboolean active;
50 /* Module of fs module itself loaded by captive_w32_init() */
51 static PMODULE_OBJECT ModuleObject;
53 /* Driver in fs module loaded by captive_w32_init() */
54 static DRIVER_OBJECT DriverObject;
56 /* Structure holding the pointer to the toplevel IRP */
57 static TOP_LEVEL_IRP TopLevelIrp; /* TODO:thread */
60 gchar *captive_option_filesystem;
61 enum captive_option_rwmode captive_option_rwmode=CAPTIVE_OPTION_RWMODE_BLIND;
62 extern enum captive_option_media captive_option_media=CAPTIVE_OPTION_MEDIA_DISK;
63 GIOChannel *captive_image_iochannel;
64 guint64 captive_image_size;
67 static gchar *captive_popt_optarg;
70 static void arg_filesystem(void)
72 g_free(captive_option_filesystem);
73 captive_option_filesystem=g_strdup(captive_popt_optarg);
76 static void arg_ro(void)
78 captive_option_rwmode=CAPTIVE_OPTION_RWMODE_RO;
80 static void arg_blind(void)
82 captive_option_rwmode=CAPTIVE_OPTION_RWMODE_BLIND;
84 static void arg_rw(void)
86 captive_option_rwmode=CAPTIVE_OPTION_RWMODE_RW;
89 static void arg_cdrom(void)
91 captive_option_media=CAPTIVE_OPTION_MEDIA_CDROM;
93 static void arg_disk(void)
95 captive_option_media=CAPTIVE_OPTION_MEDIA_DISK;
99 static void captive_popt_callback
100 (poptContext con,enum poptCallbackReason reason,const struct poptOption *opt,const char *arg,const void *data);
102 const struct poptOption captive_popt[]={
103 { argInfo:POPT_ARG_INTL_DOMAIN, arg:(void *)PACKAGE },
104 { argInfo:POPT_ARG_CALLBACK, arg:(void *)captive_popt_callback },
105 #define POPT_OFFSET 2
106 #define CAPTIVE_POPT(longname,argInfoP,argP,descripP,argDescripP) \
108 longName: (longname), \
110 argInfo: (argInfoP), \
113 descrip: (descripP), \
114 argDescrip: (argDescripP), \
116 #define CAPTIVE_POPT_STRING(longname,descripP,argDescripP) \
117 CAPTIVE_POPT(longname,POPT_ARG_STRING,&captive_popt_optarg,descripP,argDescripP)
118 #define CAPTIVE_POPT_NONE(longname,descripP) \
119 CAPTIVE_POPT(longname,POPT_ARG_NONE ,NULL ,descripP,NULL )
121 CAPTIVE_POPT_STRING("filesystem",N_("Path to .sys or .so filesystem module file"),N_("pathname")),
122 CAPTIVE_POPT_NONE( "ro" ,N_("Read/write mode: Any write access will be forbidden")),
123 CAPTIVE_POPT_NONE( "blind" ,N_("Read/write mode: All writes are just simulated in memory (default)")),
124 CAPTIVE_POPT_NONE( "rw" ,N_("Read/write mode: Write directly to the image file/device")),
125 CAPTIVE_POPT_NONE( "cdrom" ,N_("Media type: CD-ROM")),
126 CAPTIVE_POPT_NONE( "disk" ,N_("Media type: Disk (default)")),
128 #undef CAPTIVE_POPT_NONE
129 #undef CAPTIVE_POPT_STRING
134 static const struct poptOption captive_popt_standalone[]={
135 CAPTIVE_POPT_INCLUDE,
141 static void (*const popt_func_table[])(void)={
151 /* poptCallbackType captive_popt_callback */
152 static void captive_popt_callback
153 (poptContext con,enum poptCallbackReason reason,const struct poptOption *opt,const char *arg,const void *data)
157 g_return_if_fail(reason==POPT_CALLBACK_REASON_OPTION);
159 funci=(opt-(captive_popt+POPT_OFFSET));
160 g_return_if_fail(funci>=0);
161 g_return_if_fail(funci<(gint)G_N_ELEMENTS(popt_func_table));
162 if (popt_func_table[funci])
163 (*popt_func_table[funci])();
164 free(captive_popt_optarg);
165 captive_popt_optarg=NULL; /* sanity, shouldn't be needed */
169 static gboolean captive_w32_init(void)
173 GIOStatus erriostatus;
175 g_return_val_if_fail(captive_option_filesystem!=NULL,FALSE);
177 erriostatus=g_io_channel_set_encoding(captive_image_iochannel,
178 NULL, /* encoding; force binary data */
180 g_assert(erriostatus==G_IO_STATUS_NORMAL);
182 /* captive_giochannel_size() only _after_ g_io_channel_set_encoding() ! */
183 captive_image_size=captive_giochannel_size(captive_image_iochannel);
184 g_return_val_if_fail(captive_image_size>0,FALSE);
186 /* Part of reactos/ntoskrnl/ke/main.c/KiSystemStartup() begins. */
187 /* ExpInitializeExecutive(); */
188 /* Part of reactos/ntoskrnl/ke/main.c/ExpInitializeExecutive() begins
189 * here as the rest of the function does a lot of hardware initializations.
192 /* Part of reactos/ntoskrnl/ldr/loader.c/LdrInit1() begins. */
193 InitializeListHead(&ModuleTextListHead);
194 /* Part of reactos/ntoskrnl/ldr/loader.c/LdrInit1() ends. */
195 KeLowerIrql(DISPATCH_LEVEL);
197 KeLowerIrql(PASSIVE_LEVEL);
199 /* create default nls tables */
204 /* PiInitProcessManager(); */
205 /* Part of reactos/ntoskrnl/ps/psmgr.c/PiInitProcessManager() begins. */
206 PsInitProcessManagment();
207 PsInitThreadManagment();
208 /* Part of reactos/ntoskrnl/ps/psmgr.c/PiInitProcessManager() ends. */
212 /* Part of reactos/ntoskrnl/ex/init.c/Exinit() begins. */
213 ExpInitLookasideLists();
214 /* Part of reactos/ntoskrnl/ex/init.c/Exinit() ends. */
216 /* LdrInitModuleManagement(); */
217 /* Part of reactos/ntoskrnl/ldr/loader.c/LdrInitModuleManagement() begins
218 * here as the rest "Create module object for {NTOSKRNL,HAL}"
219 * is dependent on {NTOSKRNL,HAL} PE image headers not provided by libcaptive.
221 /* Initialize the module list and spinlock */
222 InitializeListHead(&ModuleListHead);
223 KeInitializeSpinLock(&ModuleListLock);
224 /* Part of reactos/ntoskrnl/ldr/loader.c/LdrInitModuleManagement ends. */
225 /* Part of reactos/ntoskrnl/ke/main.c/ExpInitializeExecutive() ends. */
226 /* Part of reactos/ntoskrnl/ke/main.c/KiSystemStartup() ends. */
228 /* Simulate our PE headers and export the symbols of {NTOSKRNL,HAL} */
229 captive_kernel_exports();
231 errbool=captive_cdrom_init();
232 g_return_val_if_fail(errbool==TRUE,FALSE);
233 errbool=captive_disk_init();
234 g_return_val_if_fail(errbool==TRUE,FALSE);
236 err=captive_LdrpLoadAndCallImage(
237 &ModuleObject, /* ModuleObjectp */
238 captive_utf8_to_UnicodeString_alloca(captive_option_filesystem), /* ModuleName */
239 &DriverObject, /* DriverEntry_DriverObject */
240 captive_utf8_to_UnicodeString_alloca("\\captive\\filesystem")); /* DriverEntry_RegistryPath */
241 g_return_val_if_fail(NT_SUCCESS(err),FALSE);
243 /* set TopLevelIrp() - FIXME: where is it set by native reactos? */
244 PsGetCurrentThread()->TopLevelIrp=&TopLevelIrp; /* otherwise Io{Get,Set}TopLevelIrp() would SIGSEGV */
246 /* Begin possible handling of foreign W32 binary code here */
247 /* If you want to disable SIGSEGV handler if not needed:
248 * if (ModuleObject->Flags & MODULE_FLAG_PE)
250 captive_signal_init();
258 * @captive_args: String with possible options to parse by popt.
259 * %NULL value is permitted.
260 * @image_iochannel: Host OS file of the disk image to mount.
261 * %NULL value is forbidden.
263 * Initializes %libcaptive and loads the specified filesystem.
267 * Returns: %TRUE if successfuly loaded.
269 gboolean captive_init(const gchar *captive_args,GIOChannel *image_iochannel)
274 #ifdef MAINTAINER_MODE
275 g_log_set_always_fatal(~(0
282 g_return_val_if_fail(active==FALSE,FALSE);
284 /* (optionally) parse the given 'captive_args' string */
286 int captive_args_argc;
287 const char **captive_args_argv=NULL;
291 errint=poptParseArgvString(captive_args,&captive_args_argc,&captive_args_argv);
293 g_assert_not_reached(); /* argument parsing args_error */
296 context=poptGetContext(
298 captive_args_argc,captive_args_argv, /* argc,argv */
299 captive_popt_standalone, /* options */
300 POPT_CONTEXT_KEEP_FIRST);
302 g_assert_not_reached(); /* argument recognization args_error */
305 errint=poptReadDefaultConfig(context,
308 g_assert_not_reached(); /* argument recognization args_error */
309 goto args_err_context;
311 errint=poptGetNextOpt(context);
313 g_assert_not_reached(); /* some non-callbacked argument reached */
314 goto args_err_context;
316 /* FIXME: reject non-"--"-prefixed arguments; how to detected them? */
317 r=TRUE; /* success */
319 poptFreeContext(context);
321 free(captive_args_argv); /* may be NULL here */
324 puts("FIXME: HELP HERE");
325 g_return_val_if_reached(r);
329 g_return_val_if_fail(image_iochannel!=NULL,FALSE);
331 captive_image_iochannel=image_iochannel;
332 /* Do not initialize 'captive_image_size' by captive_giochannel_size() here
333 * as we yet need to do g_io_channel_set_encoding().
336 errbool=captive_w32_init();
337 g_return_val_if_fail(errbool==TRUE,FALSE);