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