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() */
42 /* Are we initialized? */
43 static gboolean active;
45 /* Module of fs module itself loaded by captive_init(fs_path) */
46 static PMODULE_OBJECT ModuleObject;
48 /* Driver in fs module loaded by captive_init(fs_path) */
49 static DRIVER_OBJECT DriverObject;
51 /* Handle for the root directory of the mounted volume */
52 static HANDLE root_Handle;
56 * @fs_path: Host OS file #utf8 pathname of the filesystem module to load.
57 * %NULL value is forbidden.
58 * @image_pathname: Host OS file #utf8 pathname of the disk image to mount.
59 * %NULL value is forbidden.
61 * Initializes %libcaptive and loads the specified filesystem.
63 * Returns: %TRUE if successfuly loaded.
65 gboolean captive_init(const gchar *fs_path,const gchar *image_pathname)
69 OBJECT_ATTRIBUTES root_ObjectAttributes;
70 IO_STATUS_BLOCK root_IoStatusBlock;
72 #ifdef MAINTAINER_MODE
73 g_log_set_always_fatal(~(0
80 g_return_val_if_fail(fs_path!=NULL,FALSE);
81 g_return_val_if_fail(image_pathname!=NULL,FALSE);
82 g_return_val_if_fail(active==FALSE,FALSE);
84 /* Part of reactos/ntoskrnl/ke/main.c/KiSystemStartup() begins. */
85 /* ExpInitializeExecutive(); */
86 /* Part of reactos/ntoskrnl/ke/main.c/ExpInitializeExecutive() begins
87 * here as the rest of the function does a lot of hardware initializations.
90 /* Part of reactos/ntoskrnl/ldr/loader.c/LdrInit1() begins. */
91 InitializeListHead(&ModuleTextListHead);
92 /* Part of reactos/ntoskrnl/ldr/loader.c/LdrInit1() ends. */
94 /* create default nls tables */
99 /* PiInitProcessManager(); */
100 /* Part of reactos/ntoskrnl/ps/psmgr.c/PiInitProcessManager() begins. */
101 PsInitProcessManagment();
102 PsInitThreadManagment();
103 /* Part of reactos/ntoskrnl/ps/psmgr.c/PiInitProcessManager() ends. */
107 /* LdrInitModuleManagement(); */
108 /* Part of reactos/ntoskrnl/ldr/loader.c/LdrInitModuleManagement() begins
109 * here as the rest "Create module object for {NTOSKRNL,HAL}"
110 * is dependent on {NTOSKRNL,HAL} PE image headers not provided by libcaptive.
112 /* Initialize the module list and spinlock */
113 InitializeListHead(&ModuleListHead);
114 KeInitializeSpinLock(&ModuleListLock);
115 /* Part of reactos/ntoskrnl/ldr/loader.c/LdrInitModuleManagement ends. */
116 /* Part of reactos/ntoskrnl/ke/main.c/ExpInitializeExecutive() ends. */
117 /* Part of reactos/ntoskrnl/ke/main.c/KiSystemStartup() ends. */
119 /* Simulate our PE headers and export the symbols of {NTOSKRNL,HAL} */
120 captive_kernel_exports();
122 errbool=captive_cdrom_init(image_pathname);
123 g_return_val_if_fail(errbool==TRUE,FALSE);
125 err=captive_LdrpLoadAndCallImage(
126 &ModuleObject, /* ModuleObjectp */
127 captive_utf8_to_UnicodeString_alloca(fs_path), /* ModuleName */
128 &DriverObject, /* DriverEntry_DriverObject */
129 captive_utf8_to_UnicodeString_alloca("\\captive\\filesystem")); /* DriverEntry_RegistryPath */
130 g_return_val_if_fail(NT_SUCCESS(err),FALSE);
132 /* Begin possible handling of foreign W32 binary code here */
133 captive_signal_init();
135 /* Do not open "\Cdfs"(anything) as it is just the filesystem implementation.
136 * ntoskrnl/io/fs.c/IoMountVolume() will map
137 * FILE_DEVICE_CD_ROM -> FILE_DEVICE_CD_ROM_FILE_SYSTEM
138 * for us automatically when opening the device itself.
139 * Also you must put some trailing content there as otherwise
140 * IoCreateFile()->ObCreateObject()->ObFindObject()
141 * would leave 'ObCreateObject::RemainingPath' as NULL
142 * and later IopCreateFile() would consider it FO_DIRECT_DEVICE_OPEN (e.g. w/o any mount!)
144 InitializeObjectAttributes(
145 &root_ObjectAttributes, /* InitializedAttributes */
146 captive_utf8_to_UnicodeString_alloca("\\Device\\CdRom0\\."), /* ObjectName */
147 0, /* Attributes; I hope no OBJ_KERNEL_HANDLE as we are 'system process' */
148 NULL, /* RootDirectory */
149 NULL); /* SecurityDescriptor; ignored */
151 /* wanted: * IoCreateFile()->ObCreateObject(,,,IoFileObjectType)->
152 * ->(IoFileObjectType->Create==IopCreateFile)()->IoMountVolume()
155 &root_Handle, /* FileHandle */
156 FILE_LIST_DIRECTORY, /* DesiredAccess */
157 &root_ObjectAttributes, /* ObjectAttributes */
158 &root_IoStatusBlock, /* IoStatusBlock */
159 NULL, /* AllocationSize; ignored for open */
160 FILE_ATTRIBUTE_NORMAL, /* FileAttributes; ignored for open */
161 0, /* ShareAccess; 0 means exclusive */
162 FILE_OPEN, /* CreateDisposition */
163 FILE_DIRECTORY_FILE, /* CreateOptions */
166 CreateFileTypeNone, /* CreateFileType */
167 NULL, /* ExtraCreateParameters */
169 g_return_val_if_fail(NT_SUCCESS(err),FALSE);
170 g_return_val_if_fail(NT_SUCCESS(root_IoStatusBlock.Status),FALSE);
171 g_return_val_if_fail(root_IoStatusBlock.Information==FILE_OPENED,FALSE);
182 * Closes #libcaptive. Frees any used system resources. You are forbidden
183 * to touch any #libcaptive data or funtions before a new captive_init()
184 * is done. Forbidden to call it before successful captive_init() is done.
186 * Currently this function IS NOT IMPLEMENTED.
188 * Returns: %TRUE if the successful resource cleanup was done during the call.
190 gboolean captive_cleanup(void)
194 g_return_val_if_fail(active==TRUE,FALSE);
196 err=LdrUnloadModule(ModuleObject);
197 g_assert(NT_SUCCESS(err));
199 /* captive_cleanup() NOT IMPLEMENTED */
200 g_return_val_if_reached(FALSE);