2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * PURPOSE: Functions for mapping files and sections
5 * FILE: subsys/win32k/eng/device.c
6 * PROGRAMER: Timo Kreuzer (timo.kreuzer@reactos.org)
15 #define MmMapViewInSessionSpace MmMapViewInSystemSpace
16 #define MmUnmapViewInSessionSpace MmUnmapViewInSystemSpace
18 typedef struct _ENGSECTION
20 PVOID pvSectionObject
;
24 } ENGSECTION
, *PENGSECTION
;
26 typedef struct _FILEVIEW
28 LARGE_INTEGER LastWriteTime
;
33 } FILEVIEW
, *PFILEVIEW
;
35 typedef struct _FONTFILEVIEW
45 } FONTFILEVIEW
, *PFONTFILEVIEW
;
54 HANDLE ghSystem32Directory
;
55 HANDLE ghRootDirectory
;
67 PVOID pvSectionObject
;
70 /* Allocate a section object */
71 pSection
= EngAllocMem(0, sizeof(ENGSECTION
), 'stsU');
72 if (!pSection
) return NULL
;
74 liSize
.QuadPart
= cjSize
;
75 Status
= MmCreateSection(&pvSectionObject
,
83 if (!NT_SUCCESS(Status
))
85 DPRINT1("Failed to create a section Status=0x%x\n", Status
);
90 /* Set the fields of the section */
91 pSection
->ulTag
= ulTag
;
92 pSection
->pvSectionObject
= pvSectionObject
;
93 pSection
->pvMappedBase
= NULL
;
94 pSection
->cjViewSize
= cjSize
;
106 OUT PVOID
* pvBaseAddress
)
109 PENGSECTION pSection
= pvSection
;
110 PEPROCESS pepProcess
;
112 /* Get a pointer to the process */
113 Status
= ObReferenceObjectByHandle(hProcess
,
114 PROCESS_VM_OPERATION
,
119 if (!NT_SUCCESS(Status
))
121 DPRINT1("Could not access process %p, Status=0x%lx\n", hProcess
, Status
);
127 /* Make sure the section isn't already mapped */
128 ASSERT(pSection
->pvMappedBase
== NULL
);
130 /* Map the section into the process address space */
131 Status
= MmMapViewOfSection(pSection
->pvSectionObject
,
133 &pSection
->pvMappedBase
,
135 pSection
->cjViewSize
,
137 &pSection
->cjViewSize
,
141 if (!NT_SUCCESS(Status
))
143 DPRINT1("Failed to map a section Status=0x%x\n", Status
);
148 /* Make sure the section is mapped */
149 ASSERT(pSection
->pvMappedBase
);
151 /* Unmap the section from the process address space */
152 Status
= MmUnmapViewOfSection(pepProcess
, pSection
->pvMappedBase
);
153 if (NT_SUCCESS(Status
))
155 pSection
->pvMappedBase
= NULL
;
159 DPRINT1("Failed to unmap a section @ &p Status=0x%x\n",
160 pSection
->pvMappedBase
, Status
);
164 /* Dereference the process */
165 ObDereferenceObject(pepProcess
);
167 /* Set the new mapping base and return bool status */
168 *pvBaseAddress
= pSection
->pvMappedBase
;
169 return NT_SUCCESS(Status
);
175 IN PVOID pvSection OPTIONAL
,
176 IN PVOID pvMappedBase OPTIONAL
)
179 PENGSECTION pSection
= pvSection
;
182 /* Did the caller give us a mapping base? */
185 Status
= MmUnmapViewInSessionSpace(pvMappedBase
);
186 if (!NT_SUCCESS(Status
))
188 DPRINT1("MmUnmapViewInSessionSpace failed: 0x%lx\n", Status
);
193 /* Check if we should free the section as well */
196 /* Dereference the kernel section */
197 ObDereferenceObject(pSection
->pvSectionObject
);
199 /* Finally free the section memory itself */
200 EngFreeMem(pSection
);
209 OUT PVOID
*ppvSection
,
215 PENGSECTION pSection
;
217 /* Check parameter */
218 if (cjSize
== 0) return NULL
;
220 /* Allocate a section object */
221 pSection
= EngCreateSection(fl
, cjSize
, ulTag
);
228 /* Map the section in session space */
229 Status
= MmMapViewInSessionSpace(pSection
->pvSectionObject
,
230 &pSection
->pvMappedBase
,
231 &pSection
->cjViewSize
);
232 if (!NT_SUCCESS(Status
))
234 DPRINT1("Failed to map a section Status=0x%x\n", Status
);
236 EngFreeSectionMem(pSection
, NULL
);
240 if (fl
& FL_ZERO_MEMORY
)
242 RtlZeroMemory(pSection
->pvMappedBase
, cjSize
);
245 /* Set section pointer and return base address */
246 *ppvSection
= pSection
;
247 return pSection
->pvMappedBase
;
255 ULONG cjSizeOfModule
,
258 PFILEVIEW pFileView
= NULL
;
259 OBJECT_ATTRIBUTES ObjectAttributes
;
261 UNICODE_STRING ustrFileName
;
262 IO_STATUS_BLOCK IoStatusBlock
;
263 FILE_BASIC_INFORMATION FileInformation
;
266 LARGE_INTEGER liSize
;
268 if (fl
& FVF_FONTFILE
)
270 pFileView
= EngAllocMem(0, sizeof(FONTFILEVIEW
), 'vffG');
274 pFileView
= EngAllocMem(0, sizeof(FILEVIEW
), 'liFg');
277 /* Check for success */
278 if (!pFileView
) return NULL
;
280 /* Check if the file is relative to system32 */
281 if (fl
& FVF_SYSTEMROOT
)
283 hRootDir
= ghSystem32Directory
;
287 hRootDir
= ghRootDirectory
;
290 /* Initialize unicode string and object attributes */
291 RtlInitUnicodeString(&ustrFileName
, pwsz
);
292 InitializeObjectAttributes(&ObjectAttributes
,
294 OBJ_CASE_INSENSITIVE
|OBJ_KERNEL_HANDLE
,
298 /* Now open the file */
299 Status
= ZwCreateFile(&hFile
,
304 FILE_ATTRIBUTE_NORMAL
,
307 FILE_NON_DIRECTORY_FILE
,
311 Status
= ZwQueryInformationFile(hFile
,
314 sizeof(FILE_BASIC_INFORMATION
),
315 FileBasicInformation
);
316 if (NT_SUCCESS(Status
))
318 pFileView
->LastWriteTime
= FileInformation
.LastWriteTime
;
321 /* Create a section from the file */
322 liSize
.QuadPart
= cjSizeOfModule
;
323 Status
= MmCreateSection(&pFileView
->pSection
,
326 cjSizeOfModule
? &liSize
: NULL
,
327 fl
& FVF_READONLY
? PAGE_EXECUTE_READ
: PAGE_EXECUTE_READWRITE
,
332 /* Close the file handle */
335 if (!NT_SUCCESS(Status
))
337 DPRINT1("Failed to create a section Status=0x%x\n", Status
);
338 EngFreeMem(pFileView
);
343 pFileView
->pvKView
= NULL
;
344 pFileView
->pvViewFD
= NULL
;
345 pFileView
->cjView
= 0;
352 EngLoadModule(LPWSTR pwsz
)
354 /* Forward to EngLoadModuleEx */
355 return (HANDLE
)EngLoadModuleEx(pwsz
, 0, FVF_READONLY
| FVF_SYSTEMROOT
);
360 EngLoadModuleForWrite(
362 IN ULONG cjSizeOfModule
)
364 /* Forward to EngLoadModuleEx */
365 return (HANDLE
)EngLoadModuleEx(pwsz
, cjSizeOfModule
, FVF_SYSTEMROOT
);
374 PFILEVIEW pFileView
= (PFILEVIEW
)h
;
377 pFileView
->cjView
= 0;
379 /* Map the section in session space */
380 Status
= MmMapViewInSessionSpace(pFileView
->pSection
,
383 if (!NT_SUCCESS(Status
))
385 DPRINT1("Failed to map a section Status=0x%x\n", Status
);
390 *pulSize
= pFileView
->cjView
;
391 return pFileView
->pvKView
;
396 EngFreeModule(IN HANDLE h
)
398 PFILEVIEW pFileView
= (PFILEVIEW
)h
;
401 /* Unmap the section */
402 Status
= MmUnmapViewInSessionSpace(pFileView
->pvKView
);
403 if (!NT_SUCCESS(Status
))
405 DPRINT1("MmUnmapViewInSessionSpace failed: 0x%lx\n", Status
);
409 /* Dereference the section */
410 ObDereferenceObject(pFileView
->pSection
);
412 /* Free the file view memory */
413 EngFreeMem(pFileView
);
421 OUT ULONG_PTR
*piFile
)
427 hModule
= EngLoadModuleEx(pwsz
, 0, 0);
435 pvBase
= EngMapModule(hModule
, &cjSize
);
438 EngFreeModule(hModule
);
442 /* Set iFile and return mapped base */
443 *piFile
= (ULONG_PTR
)hModule
;
452 HANDLE hModule
= (HANDLE
)iFile
;
454 EngFreeModule(hModule
);
467 // www.osr.com/ddk/graphics/gdifncs_0co7.htm
477 // http://www.osr.com/ddk/graphics/gdifncs_6wbr.htm
488 // www.osr.com/ddk/graphics/gdifncs_3up3.htm
489 return EngMapFontFileFD(iFile
, ppjBuf
, pcjBuf
);
497 // www.osr.com/ddk/graphics/gdifncs_09wn.htm
498 EngUnmapFontFileFD(iFile
);