a40cf69afcd23e6ccf96f76c1a7199d75f23ffc3
[reactos.git] / reactos / subsys / csrss / video.c
1 /* $Id$
2 *
3 * ReactOS Project
4 */
5
6 #include <windows.h>
7 #include <ddk/ntddk.h>
8 #include <ddk/ntapi.h>
9 #include <rosrtl/string.h>
10
11 ULONG
12 InitializeVideoAddressSpace(VOID)
13 {
14 OBJECT_ATTRIBUTES ObjectAttributes;
15 UNICODE_STRING PhysMemName;
16 NTSTATUS Status;
17 HANDLE PhysMemHandle;
18 PVOID BaseAddress;
19 PVOID NullAddress;
20 LARGE_INTEGER Offset;
21 ULONG ViewSize;
22 CHAR IVT[1024];
23 CHAR BDA[256];
24
25 /*
26 * Open the physical memory section
27 */
28 RtlRosInitUnicodeStringFromLiteral(&PhysMemName, L"\\Device\\PhysicalMemory");
29 InitializeObjectAttributes(&ObjectAttributes,
30 &PhysMemName,
31 0,
32 NULL,
33 NULL);
34 Status = ZwOpenSection(&PhysMemHandle, SECTION_ALL_ACCESS,
35 &ObjectAttributes);
36 if (!NT_SUCCESS(Status))
37 {
38 DbgPrint("Couldn't open \\Device\\PhysicalMemory\n");
39 return(0);
40 }
41
42 /*
43 * Map the BIOS and device registers into the address space
44 */
45 Offset.QuadPart = 0xa0000;
46 ViewSize = 0x100000 - 0xa0000;
47 BaseAddress = (PVOID)0xa0000;
48 Status = NtMapViewOfSection(PhysMemHandle,
49 NtCurrentProcess(),
50 &BaseAddress,
51 0,
52 8192,
53 &Offset,
54 &ViewSize,
55 ViewUnmap,
56 0,
57 PAGE_EXECUTE_READWRITE);
58 if (!NT_SUCCESS(Status))
59 {
60 DbgPrint("Couldn't map physical memory (%x)\n", Status);
61 NtClose(PhysMemHandle);
62 return(0);
63 }
64 NtClose(PhysMemHandle);
65 if (BaseAddress != (PVOID)0xa0000)
66 {
67 DbgPrint("Couldn't map physical memory at the right address "
68 "(was %x)\n", BaseAddress);
69 return(0);
70 }
71
72 /*
73 * Map some memory to use for the non-BIOS parts of the v86 mode address
74 * space
75 */
76 BaseAddress = (PVOID)0x1;
77 ViewSize = 0xa0000 - 0x1000;
78 Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
79 &BaseAddress,
80 0,
81 &ViewSize,
82 MEM_COMMIT,
83 PAGE_EXECUTE_READWRITE);
84 if (!NT_SUCCESS(Status))
85 {
86 DbgPrint("Failed to allocate virtual memory (Status %x)\n", Status);
87 return(0);
88 }
89 if (BaseAddress != (PVOID)0x0)
90 {
91 DbgPrint("Failed to allocate virtual memory at right address "
92 "(was %x)\n", BaseAddress);
93 return(0);
94 }
95
96 /*
97 * Get the real mode IVT from the kernel
98 */
99 Status = NtVdmControl(0, IVT);
100 if (!NT_SUCCESS(Status))
101 {
102 DbgPrint("NtVdmControl failed (status %x)\n", Status);
103 return(0);
104 }
105
106 /*
107 * Copy the real mode IVT into the right place
108 */
109 NullAddress = (PVOID)0x0; /* Workaround for GCC 3.4 */
110 memcpy(NullAddress, IVT, 1024);
111
112 /*
113 * Get the BDA from the kernel
114 */
115 Status = NtVdmControl(1, BDA);
116 if (!NT_SUCCESS(Status))
117 {
118 DbgPrint("NtVdmControl failed (status %x)\n", Status);
119 return(0);
120 }
121
122 /*
123 * Copy the BDA into the right place
124 */
125 memcpy((PVOID)0x400, BDA, 256);
126
127 return(1);
128 }
129
130
131 /* EOF */