+options --cdrom and --disk
[captive.git] / src / libcaptive / client / init.c
1 /* $Id$
2  * Init and cleanup code of libcaptive to be called by client application
3  * Copyright (C) 2002 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
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
8  * 
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.
13  * 
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
17  */
18
19
20 #include "config.h"
21
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 */
32 #include <fcntl.h>
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() */
41 #include <stdio.h>
42 #include "reactos/internal/ex.h"        /* for ExpInitLookasideLists() */
43 #include <popt.h>
44 #include <glib/gstrfuncs.h>
45
46
47 /* Are we initialized? */
48 static gboolean active;
49
50 /* Module of fs module itself loaded by captive_w32_init() */
51 static PMODULE_OBJECT ModuleObject;
52
53 /* Driver in fs module loaded by captive_w32_init() */
54 static DRIVER_OBJECT DriverObject;
55
56 /* Structure holding the pointer to the toplevel IRP */
57 static TOP_LEVEL_IRP TopLevelIrp;       /* TODO:thread */
58
59
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;
65
66
67 static gchar *captive_popt_optarg;
68
69
70 static void arg_filesystem(void)
71 {
72         g_free(captive_option_filesystem);
73         captive_option_filesystem=g_strdup(captive_popt_optarg);
74 }
75
76 static void arg_ro(void)
77 {
78         captive_option_rwmode=CAPTIVE_OPTION_RWMODE_RO;
79 }
80 static void arg_blind(void)
81 {
82         captive_option_rwmode=CAPTIVE_OPTION_RWMODE_BLIND;
83 }
84 static void arg_rw(void)
85 {
86         captive_option_rwmode=CAPTIVE_OPTION_RWMODE_RW;
87 }
88
89 static void arg_cdrom(void)
90 {
91         captive_option_media=CAPTIVE_OPTION_MEDIA_CDROM;
92 }
93 static void arg_disk(void)
94 {
95         captive_option_media=CAPTIVE_OPTION_MEDIA_DISK;
96 }
97
98
99 static void captive_popt_callback
100                 (poptContext con,enum poptCallbackReason reason,const struct poptOption *opt,const char *arg,const void *data);
101
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) \
107                 { \
108                         longName: (longname), \
109                         shortName: 0, \
110                         argInfo: (argInfoP), \
111                         arg: (void *)argP, \
112                         val: 0, \
113                         descrip: (descripP), \
114                         argDescrip: (argDescripP), \
115                 }
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       )
120
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)")),
127
128 #undef CAPTIVE_POPT_NONE
129 #undef CAPTIVE_POPT_STRING
130 #undef CAPTIVE_POPT
131                 POPT_TABLEEND
132                 };
133
134 static const struct poptOption captive_popt_standalone[]={
135                 CAPTIVE_POPT_INCLUDE,
136                 POPT_AUTOHELP
137                 POPT_TABLEEND
138                 };
139
140
141 static void (*const popt_func_table[])(void)={
142                 arg_filesystem,
143                 arg_ro,
144                 arg_blind,
145                 arg_rw,
146                 arg_cdrom,
147                 arg_disk,
148                 };
149
150
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)
154 {
155 gint funci;
156
157         g_return_if_fail(reason==POPT_CALLBACK_REASON_OPTION);
158
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 */
166 }
167
168
169 static gboolean captive_w32_init(void)
170 {
171 NTSTATUS err;
172 gboolean errbool;
173 GIOStatus erriostatus;
174
175         g_return_val_if_fail(captive_option_filesystem!=NULL,FALSE);
176
177         erriostatus=g_io_channel_set_encoding(captive_image_iochannel,
178                         NULL,   /* encoding; force binary data */
179                         NULL);  /* error */
180         g_assert(erriostatus==G_IO_STATUS_NORMAL);
181
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);
185
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.
190                  */
191                 /* LdrInit1(); */
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);
196                 /*...*/
197                 KeLowerIrql(PASSIVE_LEVEL);
198                 /*...*/
199                 /* create default nls tables */
200                 RtlpInitNlsTables();
201                 /*...*/
202                 ObInit();
203                 /*...*/
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. */
209                 /*...*/
210                 IoInit();
211                 /* Exinit(); */
212                         /* Part of reactos/ntoskrnl/ex/init.c/Exinit() begins. */
213                         ExpInitLookasideLists();
214                         /* Part of reactos/ntoskrnl/ex/init.c/Exinit() ends. */
215                 /*...*/
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.
220                          */
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. */
227
228         /* Simulate our PE headers and export the symbols of {NTOSKRNL,HAL} */
229         captive_kernel_exports();
230
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);
235
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);
242
243         /* set TopLevelIrp() - FIXME: where is it set by native reactos? */
244         PsGetCurrentThread()->TopLevelIrp=&TopLevelIrp; /* otherwise Io{Get,Set}TopLevelIrp() would SIGSEGV */
245
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)
249          */
250         captive_signal_init();
251
252         return TRUE;
253 }
254
255
256 /**
257  * captive_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.
262  *
263  * Initializes %libcaptive and loads the specified filesystem.
264  *
265  * You should supply
266  *
267  * Returns: %TRUE if successfuly loaded.
268  */
269 gboolean captive_init(const gchar *captive_args,GIOChannel *image_iochannel)
270 {
271 gboolean errbool;
272 int errint;
273
274 #ifdef MAINTAINER_MODE
275         g_log_set_always_fatal(~(0
276                         |G_LOG_LEVEL_MESSAGE
277                         |G_LOG_LEVEL_INFO
278                         |G_LOG_LEVEL_DEBUG
279                         ));
280 #endif
281
282         g_return_val_if_fail(active==FALSE,FALSE);
283
284         /* (optionally) parse the given 'captive_args' string */
285         if (captive_args) {
286 int captive_args_argc;
287 const char **captive_args_argv=NULL;
288 poptContext context;
289 gboolean r=FALSE;
290
291                 errint=poptParseArgvString(captive_args,&captive_args_argc,&captive_args_argv);
292                 if (errint!=0) {
293                         g_assert_not_reached(); /* argument parsing args_error */
294                         goto args_err;
295                         }
296                 context=poptGetContext(
297                                 PACKAGE,        /* name */
298                                 captive_args_argc,captive_args_argv,    /* argc,argv */
299                                 captive_popt_standalone,        /* options */
300                                 POPT_CONTEXT_KEEP_FIRST);
301                 if (context==NULL) {
302                         g_assert_not_reached(); /* argument recognization args_error */
303                         goto args_err_argv;
304                         }
305                 errint=poptReadDefaultConfig(context,
306                                 TRUE);  /* useEnv */
307                 if (errint!=0) {
308                         g_assert_not_reached(); /* argument recognization args_error */
309                         goto args_err_context;
310                         }
311                 errint=poptGetNextOpt(context);
312                 if (errint!=-1) {
313                         g_assert_not_reached(); /* some non-callbacked argument reached */
314                         goto args_err_context;
315                         }
316                 /* FIXME: reject non-"--"-prefixed arguments; how to detected them? */
317                 r=TRUE; /* success */
318 args_err_context:
319                 poptFreeContext(context);
320 args_err_argv:
321                 free(captive_args_argv);        /* may be NULL here */
322 args_err:
323                 if (!r) {
324                         puts("FIXME: HELP HERE");
325                         g_return_val_if_reached(r);
326                         }
327                 }
328
329         g_return_val_if_fail(image_iochannel!=NULL,FALSE);
330
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().
334          */
335
336         errbool=captive_w32_init();
337         g_return_val_if_fail(errbool==TRUE,FALSE);
338
339         active=TRUE;
340         return TRUE;
341 }