captive_init(): (fs_path) -> (fs_path,image_pathname)
[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
40
41 /* Are we initialized? */
42 static gboolean active;
43
44 /* Module of fs module itself loaded by captive_init(fs_path) */
45 static PMODULE_OBJECT ModuleObject;
46
47 /* Driver in fs module loaded by captive_init(fs_path) */
48 static DRIVER_OBJECT DriverObject;
49
50 /* Handle for the root directory of the mounted volume */
51 static HANDLE root_Handle;
52
53 /**
54  * captive_init:
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.
59  *
60  * Initializes %libcaptive and loads the specified filesystem.
61  *
62  * Returns: %TRUE if successfuly loaded.
63  */
64 gboolean captive_init(const gchar *fs_path,const gchar *image_pathname)
65 {
66 NTSTATUS err;
67 gboolean errbool;
68 OBJECT_ATTRIBUTES root_ObjectAttributes;
69 IO_STATUS_BLOCK root_IoStatusBlock;
70
71 #ifdef MAINTAINER_MODE
72         g_log_set_always_fatal(~(0
73                         |G_LOG_LEVEL_MESSAGE
74                         |G_LOG_LEVEL_INFO
75                         |G_LOG_LEVEL_DEBUG
76                         ));
77 #endif
78
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);
82
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.
87                  */
88                 /* LdrInit1(); */
89                         /* Part of reactos/ntoskrnl/ldr/loader.c/LdrInit1() begins. */
90                         InitializeListHead(&ModuleTextListHead);
91                         /* Part of reactos/ntoskrnl/ldr/loader.c/LdrInit1() ends. */
92                 /*...*/
93                 /* create default nls tables */
94                 RtlpInitNlsTables();
95                 /*...*/
96                 ObInit();
97                 /*...*/
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. */
103                 /*...*/
104                 IoInit();
105                 /*...*/
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.
110                          */
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. */
117
118         /* Simulate our PE headers and export the symbols of {NTOSKRNL,HAL} */
119         captive_kernel_exports();
120
121         errbool=captive_cdrom_init(image_pathname);
122         g_return_val_if_fail(errbool==TRUE,FALSE);
123
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);
130         
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!)
139          */
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 */
146
147         /* wanted: * IoCreateFile()->ObCreateObject(,,,IoFileObjectType)->
148          * ->(IoFileObjectType->Create==IopCreateFile)()->IoMountVolume()
149          */
150         err=IoCreateFile(
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 */
160                         NULL,   /* EaBuffer */
161                         0,      /* EaLength */
162                         CreateFileTypeNone,     /* CreateFileType */
163                         NULL,   /* ExtraCreateParameters */
164                         0);     /* Options */
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);
168
169
170         active=TRUE;
171         return TRUE;
172 }
173
174
175 /**
176  * captive_cleanup:
177  *
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.
181  *
182  * Currently this function IS NOT IMPLEMENTED.
183  *
184  * Returns: %TRUE if the successful resource cleanup was done during the call.
185  */
186 gboolean captive_cleanup(void)
187 {
188 NTSTATUS err;
189
190         g_return_val_if_fail(active==TRUE,FALSE);
191
192         err=LdrUnloadModule(ModuleObject);
193         g_assert(NT_SUCCESS(err));
194
195         /* captive_cleanup() NOT IMPLEMENTED */
196         g_return_val_if_reached(FALSE);
197
198         active=FALSE;
199         return TRUE;
200 }