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/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 /* Are we initialized? */
44 static gboolean active;
46 /* Module of fs module itself loaded by captive_init(fs_path) */
47 static PMODULE_OBJECT ModuleObject;
49 /* Driver in fs module loaded by captive_init(fs_path) */
50 static DRIVER_OBJECT DriverObject;
52 /* Handle for the root directory of the mounted volume */
53 static HANDLE root_Handle;
55 /* Structure holding the pointer to the toplevel IRP */
56 static TOP_LEVEL_IRP TopLevelIrp; /* TODO:thread */
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.
66 * Initializes %libcaptive and loads the specified filesystem.
68 * Returns: %TRUE if successfuly loaded.
70 gboolean captive_init(const gchar *fs_path,const gchar *image_pathname)
74 OBJECT_ATTRIBUTES root_ObjectAttributes;
75 IO_STATUS_BLOCK root_IoStatusBlock;
77 #ifdef MAINTAINER_MODE
78 g_log_set_always_fatal(~(0
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);
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.
95 /* Part of reactos/ntoskrnl/ldr/loader.c/LdrInit1() begins. */
96 InitializeListHead(&ModuleTextListHead);
97 /* Part of reactos/ntoskrnl/ldr/loader.c/LdrInit1() ends. */
99 /* create default nls tables */
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. */
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.
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. */
124 /* Simulate our PE headers and export the symbols of {NTOSKRNL,HAL} */
125 captive_kernel_exports();
127 errbool=captive_cdrom_init(image_pathname);
128 g_return_val_if_fail(errbool==TRUE,FALSE);
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);
137 /* set TopLevelIrp() - FIXME: where is it set by native reactos? */
138 PsGetCurrentThread()->TopLevelIrp=&TopLevelIrp; /* otherwise Io{Get,Set}TopLevelIrp() would SIGSEGV */
140 /* Begin possible handling of foreign W32 binary code here */
141 captive_signal_init();
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!)
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 */
159 /* wanted: * IoCreateFile()->ObCreateObject(,,,IoFileObjectType)->
160 * ->(IoFileObjectType->Create==IopCreateFile)()->IoMountVolume()
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_DIRECTORY_FILE, /* CreateOptions */
174 CreateFileTypeNone, /* CreateFileType */
175 NULL, /* ExtraCreateParameters */
177 g_return_val_if_fail(NT_SUCCESS(err),FALSE);
178 g_return_val_if_fail(NT_SUCCESS(root_IoStatusBlock.Status),FALSE);
179 g_return_val_if_fail(root_IoStatusBlock.Information==FILE_OPENED,FALSE);
190 * Closes #libcaptive. Frees any used system resources. You are forbidden
191 * to touch any #libcaptive data or funtions before a new captive_init()
192 * is done. Forbidden to call it before successful captive_init() is done.
194 * Currently this function IS NOT IMPLEMENTED.
196 * Returns: %TRUE if the successful resource cleanup was done during the call.
198 gboolean captive_cleanup(void)
202 g_return_val_if_fail(active==TRUE,FALSE);
204 err=LdrUnloadModule(ModuleObject);
205 g_assert(NT_SUCCESS(err));
207 /* captive_cleanup() NOT IMPLEMENTED */
208 g_return_val_if_reached(FALSE);