Fixed calling of root IoCreateFile() by FILE_SYNCHRONOUS_IO_ALERT
[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/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
42
43 /* Are we initialized? */
44 static gboolean active;
45
46 /* Module of fs module itself loaded by captive_init(fs_path) */
47 static PMODULE_OBJECT ModuleObject;
48
49 /* Driver in fs module loaded by captive_init(fs_path) */
50 static DRIVER_OBJECT DriverObject;
51
52 /* Handle for the root directory of the mounted volume */
53 static HANDLE root_Handle;
54
55 /* Structure holding the pointer to the toplevel IRP */
56 static TOP_LEVEL_IRP TopLevelIrp;       /* TODO:thread */
57
58
59 /**
60  * captive_init:
61  * @fs_path: Host OS file #utf8 pathname of the filesystem module to load.
62  * %NULL value is forbidden.
63  * @image_pathname: Host OS file #utf8 pathname of the disk image to mount.
64  * %NULL value is forbidden.
65  *
66  * Initializes %libcaptive and loads the specified filesystem.
67  *
68  * Returns: %TRUE if successfuly loaded.
69  */
70 gboolean captive_init(const gchar *fs_path,const gchar *image_pathname)
71 {
72 NTSTATUS err;
73 gboolean errbool;
74 OBJECT_ATTRIBUTES root_ObjectAttributes;
75 IO_STATUS_BLOCK root_IoStatusBlock;
76
77 #ifdef MAINTAINER_MODE
78         g_log_set_always_fatal(~(0
79                         |G_LOG_LEVEL_MESSAGE
80                         |G_LOG_LEVEL_INFO
81                         |G_LOG_LEVEL_DEBUG
82                         ));
83 #endif
84
85         g_return_val_if_fail(fs_path!=NULL,FALSE);
86         g_return_val_if_fail(image_pathname!=NULL,FALSE);
87         g_return_val_if_fail(active==FALSE,FALSE);
88
89         /* Part of reactos/ntoskrnl/ke/main.c/KiSystemStartup() begins. */
90         /* ExpInitializeExecutive(); */
91                 /* Part of reactos/ntoskrnl/ke/main.c/ExpInitializeExecutive() begins
92                  * here as the rest of the function does a lot of hardware initializations.
93                  */
94                 /* LdrInit1(); */
95                         /* Part of reactos/ntoskrnl/ldr/loader.c/LdrInit1() begins. */
96                         InitializeListHead(&ModuleTextListHead);
97                         /* Part of reactos/ntoskrnl/ldr/loader.c/LdrInit1() ends. */
98                 /*...*/
99                 /* create default nls tables */
100                 RtlpInitNlsTables();
101                 /*...*/
102                 ObInit();
103                 /*...*/
104                 /* PiInitProcessManager(); */
105                         /* Part of reactos/ntoskrnl/ps/psmgr.c/PiInitProcessManager() begins. */
106                         PsInitProcessManagment();
107                         PsInitThreadManagment();
108                         /* Part of reactos/ntoskrnl/ps/psmgr.c/PiInitProcessManager() ends. */
109                 /*...*/
110                 IoInit();
111                 /*...*/
112                 /* LdrInitModuleManagement(); */
113                         /* Part of reactos/ntoskrnl/ldr/loader.c/LdrInitModuleManagement() begins
114                          * here as the rest "Create module object for {NTOSKRNL,HAL}"
115                          * is dependent on {NTOSKRNL,HAL} PE image headers not provided by libcaptive.
116                          */
117                         /* Initialize the module list and spinlock */
118                         InitializeListHead(&ModuleListHead);
119                         KeInitializeSpinLock(&ModuleListLock);
120                         /* Part of reactos/ntoskrnl/ldr/loader.c/LdrInitModuleManagement ends. */
121                 /* Part of reactos/ntoskrnl/ke/main.c/ExpInitializeExecutive() ends. */
122         /* Part of reactos/ntoskrnl/ke/main.c/KiSystemStartup() ends. */
123
124         /* Simulate our PE headers and export the symbols of {NTOSKRNL,HAL} */
125         captive_kernel_exports();
126
127         errbool=captive_cdrom_init(image_pathname);
128         g_return_val_if_fail(errbool==TRUE,FALSE);
129
130         err=captive_LdrpLoadAndCallImage(
131                         &ModuleObject,  /* ModuleObjectp */
132                         captive_utf8_to_UnicodeString_alloca(fs_path),  /* ModuleName */
133                         &DriverObject,  /* DriverEntry_DriverObject */
134                         captive_utf8_to_UnicodeString_alloca("\\captive\\filesystem")); /* DriverEntry_RegistryPath */
135         g_return_val_if_fail(NT_SUCCESS(err),FALSE);
136
137         /* set TopLevelIrp() - FIXME: where is it set by native reactos? */
138         PsGetCurrentThread()->TopLevelIrp=&TopLevelIrp; /* otherwise Io{Get,Set}TopLevelIrp() would SIGSEGV */
139
140         /* Begin possible handling of foreign W32 binary code here */
141         captive_signal_init();
142
143         /* Do not open "\Cdfs"(anything) as it is just the filesystem implementation.
144          * ntoskrnl/io/fs.c/IoMountVolume() will map
145          *      FILE_DEVICE_CD_ROM -> FILE_DEVICE_CD_ROM_FILE_SYSTEM
146          * for us automatically when opening the device itself.
147          * Also you must put some trailing content there as otherwise
148          *      IoCreateFile()->ObCreateObject()->ObFindObject()
149          * would leave 'ObCreateObject::RemainingPath' as NULL
150          * and later IopCreateFile() would consider it FO_DIRECT_DEVICE_OPEN (e.g. w/o any mount!)
151          */
152         InitializeObjectAttributes(
153                         &root_ObjectAttributes, /* InitializedAttributes */
154                         captive_utf8_to_UnicodeString_alloca("\\Device\\CdRom0\\."),    /* ObjectName */
155                         0,      /* Attributes; I hope no OBJ_KERNEL_HANDLE as we are 'system process' */
156                         NULL,   /* RootDirectory */
157                         NULL);  /* SecurityDescriptor; ignored */
158
159         /* wanted: * IoCreateFile()->ObCreateObject(,,,IoFileObjectType)->
160          * ->(IoFileObjectType->Create==IopCreateFile)()->IoMountVolume()
161          */
162         err=IoCreateFile(
163                         &root_Handle,   /* FileHandle */
164                         FILE_LIST_DIRECTORY,    /* DesiredAccess */
165                         &root_ObjectAttributes, /* ObjectAttributes */
166                         &root_IoStatusBlock,    /* IoStatusBlock */
167                         NULL,   /* AllocationSize; ignored for open */
168                         FILE_ATTRIBUTE_NORMAL,  /* FileAttributes; ignored for open */
169                         0,      /* ShareAccess; 0 means exclusive */
170                         FILE_OPEN,      /* CreateDisposition */
171                         /* FILE_SYNCHRONOUS_IO_{,NON}ALERT: We need to allow W32 filesystem
172                          * any waits to not to let it return STATUS_CANT_WAIT us.
173                          * Alertability should have only effect on asynchronous events
174                          * from KeWaitForSingleObject() by setting/clearing its parameter 'Alertable'.
175                          */
176                         FILE_DIRECTORY_FILE|FILE_SYNCHRONOUS_IO_ALERT,  /* CreateOptions */
177                         NULL,   /* EaBuffer */
178                         0,      /* EaLength */
179                         CreateFileTypeNone,     /* CreateFileType */
180                         NULL,   /* ExtraCreateParameters */
181                         0);     /* Options */
182         g_return_val_if_fail(NT_SUCCESS(err),FALSE);
183         g_return_val_if_fail(NT_SUCCESS(root_IoStatusBlock.Status),FALSE);
184         g_return_val_if_fail(root_IoStatusBlock.Information==FILE_OPENED,FALSE);
185
186
187         active=TRUE;
188         return TRUE;
189 }
190
191
192 /**
193  * captive_cleanup:
194  *
195  * Closes #libcaptive. Frees any used system resources. You are forbidden
196  * to touch any #libcaptive data or funtions before a new captive_init()
197  * is done. Forbidden to call it before successful captive_init() is done.
198  *
199  * Currently this function IS NOT IMPLEMENTED.
200  *
201  * Returns: %TRUE if the successful resource cleanup was done during the call.
202  */
203 gboolean captive_cleanup(void)
204 {
205 NTSTATUS err;
206
207         g_return_val_if_fail(active==TRUE,FALSE);
208
209         err=LdrUnloadModule(ModuleObject);
210         g_assert(NT_SUCCESS(err));
211
212         /* captive_cleanup() NOT IMPLEMENTED */
213         g_return_val_if_reached(FALSE);
214
215         active=FALSE;
216         return TRUE;
217 }