ReactOS  0.4.15-dev-440-g5f37b68
font.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Console Text-Mode Device Driver
3  * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE: Loading specific fonts into VGA.
5  * COPYRIGHT: Copyright 2008-2019 Aleksey Bragin (aleksey@reactos.org)
6  * Copyright 2008-2019 Colin Finck (mail@colinfinck.de)
7  * Copyright 2008-2019 Christoph von Wittich (christoph_vw@reactos.org)
8  */
9 
10 /* INCLUDES ***************************************************************/
11 
12 #include "blue.h"
13 #include <ndk/rtlfuncs.h>
14 
15 #define NDEBUG
16 #include <debug.h>
17 
18 NTSTATUS ExtractFont(_In_ ULONG CodePage, _In_ PUCHAR FontBitField);
21 VOID LoadFont(_In_ PUCHAR Bitplane, _In_ PUCHAR FontBitfield);
22 
23 /* FUNCTIONS ****************************************************************/
24 
25 VOID
27  _In_ ULONG CodePage)
28 {
30  PUCHAR Bitplane;
31  PUCHAR FontBitfield = NULL;
33 
34  FontBitfield = (PUCHAR)ExAllocatePoolWithTag(NonPagedPool, 2048, TAG_BLUE);
35  if (FontBitfield == NULL)
36  {
37  DPRINT1("ExAllocatePoolWithTag failed\n");
38  return;
39  }
40 
41  /* open bit plane for font table access */
42  OpenBitPlane();
43 
44  /* get pointer to video memory */
45  BaseAddress.QuadPart = BITPLANE_BASE;
46  Bitplane = (PUCHAR)MmMapIoSpace(BaseAddress, 0xFFFF, MmNonCached);
47 
48  Status = ExtractFont(CodePage, FontBitfield);
49  if (NT_SUCCESS(Status))
50  {
51  LoadFont(Bitplane, FontBitfield);
52  }
53  else
54  {
55  DPRINT1("ExtractFont failed with Status 0x%lx\n", Status);
56  }
57 
58  MmUnmapIoSpace(Bitplane, 0xFFFF);
59  ExFreePoolWithTag(FontBitfield, TAG_BLUE);
60 
61  /* close bit plane */
62  CloseBitPlane();
63 }
64 
65 VOID
67  _In_ PUCHAR FontBitfield)
68 {
70  PUCHAR Bitplane;
71 
72  /* open bit plane for font table access */
73  OpenBitPlane();
74 
75  /* get pointer to video memory */
76  BaseAddress.QuadPart = BITPLANE_BASE;
77  Bitplane = (PUCHAR)MmMapIoSpace(BaseAddress, 0xFFFF, MmNonCached);
78 
79  LoadFont(Bitplane, FontBitfield);
80 
81  MmUnmapIoSpace(Bitplane, 0xFFFF);
82 
83  /* close bit plane */
84  CloseBitPlane();
85 }
86 
87 /* PRIVATE FUNCTIONS *********************************************************/
88 
91  _In_ ULONG CodePage,
92  _In_ PUCHAR FontBitField)
93 {
94  BOOLEAN bFoundFile = FALSE;
95  HANDLE Handle;
97  CHAR FileName[20];
100  UNICODE_STRING LinkName;
102  CFHEADER CabFileHeader;
103  CFFILE CabFile;
104  ULONG CabFileOffset = 0;
106  WCHAR SourceBuffer[MAX_PATH] = { L'\0' };
107  ULONG ReadCP;
108 
111 
112  RtlInitUnicodeString(&LinkName,
113  L"\\SystemRoot");
114 
116  &LinkName,
118  NULL,
119  NULL);
120 
124 
125  if (!NT_SUCCESS(Status))
126  {
127  DPRINT1("ZwOpenSymbolicLinkObject failed with Status 0x%lx\n", Status);
128  return Status;
129  }
130 
131  SourceName.Length = 0;
132  SourceName.MaximumLength = MAX_PATH * sizeof(WCHAR);
133  SourceName.Buffer = SourceBuffer;
134 
135  Status = ZwQuerySymbolicLinkObject(Handle,
136  &SourceName,
137  NULL);
138  ZwClose(Handle);
139 
140  if (!NT_SUCCESS(Status))
141  {
142  DPRINT1("ZwQuerySymbolicLinkObject failed with Status 0x%lx\n", Status);
143  return Status;
144  }
145 
146  Status = RtlAppendUnicodeToString(&SourceName, L"\\vgafonts.cab");
147  if (!NT_SUCCESS(Status))
148  {
149  DPRINT1("RtlAppendUnicodeToString failed with Status 0x%lx\n", Status);
150  return Status;
151  }
152 
154  &SourceName,
156  NULL,
157  NULL);
158 
159  Status = ZwCreateFile(&Handle,
160  GENERIC_READ,
162  &IoStatusBlock,
163  NULL,
165  0,
166  FILE_OPEN,
168  NULL,
169  0);
170  if (!NT_SUCCESS(Status))
171  {
172  DPRINT1("Error: Cannot open vgafonts.cab (0x%lx)\n", Status);
173  return Status;
174  }
175 
176  ByteOffset.QuadPart = 0;
177  Status = ZwReadFile(Handle,
178  NULL,
179  NULL,
180  NULL,
181  &IoStatusBlock,
182  &CabFileHeader,
183  sizeof(CabFileHeader),
184  &ByteOffset,
185  NULL);
186 
187  if (!NT_SUCCESS(Status))
188  {
189  DPRINT1("Error: Cannot read from file (0x%lx)\n", Status);
190  goto Exit;
191  }
192 
193  if (CabFileHeader.Signature != CAB_SIGNATURE)
194  {
195  DPRINT1("Invalid CAB signature: 0x%lx!\n", CabFileHeader.Signature);
197  goto Exit;
198  }
199 
200  // We have a valid CAB file!
201  // Read the file table now and decrement the file count on every file. When it's zero, we read the complete table.
202  ByteOffset.QuadPart = CabFileHeader.FileTableOffset;
203 
204  while (CabFileHeader.FileCount)
205  {
206  Status = ZwReadFile(Handle,
207  NULL,
208  NULL,
209  NULL,
210  &IoStatusBlock,
211  &CabFile,
212  sizeof(CabFile),
213  &ByteOffset,
214  NULL);
215 
216  if (NT_SUCCESS(Status))
217  {
218  ByteOffset.QuadPart += sizeof(CabFile);
219 
220  // We assume here that the file name is max. 19 characters (+ 1 NULL character) long.
221  // This should be enough for our purpose.
222  Status = ZwReadFile(Handle,
223  NULL,
224  NULL,
225  NULL,
226  &IoStatusBlock,
227  FileName,
228  sizeof(FileName),
229  &ByteOffset,
230  NULL);
231 
232  if (NT_SUCCESS(Status))
233  {
234  if (!bFoundFile)
235  {
236  Status = RtlCharToInteger(FileName, 0, &ReadCP);
237  if (NT_SUCCESS(Status) && ReadCP == CodePage)
238  {
239  // We got the correct file.
240  // Save the offset and loop through the rest of the file table to find the position, where the actual data starts.
241  CabFileOffset = CabFile.FileOffset;
242  bFoundFile = TRUE;
243  }
244  }
245 
246  ByteOffset.QuadPart += strlen(FileName) + 1;
247  }
248  }
249 
250  CabFileHeader.FileCount--;
251  }
252 
253  // 8 = Size of a CFFOLDER structure (see cabman). As we don't need the values of that structure, just increase the offset here.
254  ByteOffset.QuadPart += 8;
255  ByteOffset.QuadPart += CabFileOffset;
256 
257  // ByteOffset now contains the offset of the actual data, so we can read the RAW font
258  Status = ZwReadFile(Handle,
259  NULL,
260  NULL,
261  NULL,
262  &IoStatusBlock,
263  FontBitField,
264  2048,
265  &ByteOffset,
266  NULL);
267  if (!NT_SUCCESS(Status))
268  {
269  DPRINT1("ZwReadFile failed with Status 0x%lx\n", Status);
270  }
271 
272 Exit:
273 
274  ZwClose(Handle);
275  return Status;
276 }
277 
278 /* Font-load specific funcs */
279 VOID
281 {
282  /* disable interrupts */
283  _disable();
284 
285  /* sequence reg */
290 
291  /* graphic reg */
295 
296  /* enable interrupts */
297  _enable();
298 }
299 
300 VOID
302 {
303  /* disable interrupts */
304  _disable();
305 
306  /* sequence reg */
311 
312  /* graphic reg */
316 
317  /* enable interrupts */
318  _enable();
319 }
320 
321 VOID
323  _In_ PUCHAR Bitplane,
324  _In_ PUCHAR FontBitfield)
325 {
326  UINT32 i, j;
327 
328  for (i = 0; i < 256; i++)
329  {
330  for (j = 0; j < 8; j++)
331  {
332  *Bitplane = FontBitfield[i * 8 + j];
333  Bitplane++;
334  }
335 
336  // padding
337  for (j = 8; j < 32; j++)
338  {
339  *Bitplane = 0;
340  Bitplane++;
341  }
342  }
343 }
#define KeGetCurrentIrql()
Definition: env_spec_w32.h:706
VOID CloseBitPlane(VOID)
Definition: font.c:301
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
NTSYSAPI NTSTATUS NTAPI RtlCharToInteger(PCSZ String, ULONG Base, PULONG Value)
Definition: unicode.c:261
#define TRUE
Definition: types.h:120
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
unsigned char * PUCHAR
Definition: retypes.h:3
char CHAR
Definition: xmlstorage.h:175
void __cdecl _enable(void)
Definition: intrin_arm.h:373
#define SEQ_COMMAND
Definition: blue.h:114
LONG NTSTATUS
Definition: precomp.h:26
#define SEQ_DATA
Definition: blue.h:115
#define SEQ_MEM_MODE
Definition: blue.h:123
PVOID NTAPI MmMapIoSpace(IN PHYSICAL_ADDRESS PhysicalAddress, IN SIZE_T NumberOfBytes, IN MEMORY_CACHING_TYPE CacheType)
Definition: iosup.c:47
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
#define SYMBOLIC_LINK_ALL_ACCESS
Definition: nt_native.h:1267
VOID ScrLoadFontTable(_In_ ULONG CodePage)
Definition: font.c:26
#define STATUS_INVALID_DEVICE_STATE
Definition: udferr_usr.h:178
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
unsigned int UINT32
#define SEQ_ENABLE_WRT_PLANE
Definition: blue.h:122
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint j
Definition: glfuncs.h:250
#define TAG_BLUE
Definition: blue.h:14
NTSTATUS RtlAppendUnicodeToString(IN PUNICODE_STRING Str1, IN PWSTR Str2)
Definition: string_lib.cpp:62
_In_ HANDLE Handle
Definition: extypes.h:390
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
#define SEQ_RESET
Definition: blue.h:121
#define GCT_DATA
Definition: blue.h:118
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
static void Exit(void)
Definition: sock.c:1331
#define MAX_PATH
Definition: compat.h:26
Definition: fci.c:89
#define CAB_SIGNATURE
Definition: cabinet.c:47
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
VOID LoadFont(_In_ PUCHAR Bitplane, _In_ PUCHAR FontBitfield)
Definition: font.c:322
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:126
static const WCHAR L[]
Definition: oid.c:1250
#define GENERIC_READ
Definition: compat.h:124
#define PASSIVE_LEVEL
Definition: env_spec_w32.h:693
#define GCT_RW_MODES
Definition: blue.h:127
#define BITPLANE_BASE
Definition: blue.h:100
Status
Definition: gdiplustypes.h:24
#define FILE_OPEN
Definition: from_kernel.h:54
#define _In_
Definition: no_sal2.h:204
#define GCT_GRAPH_MODE
Definition: blue.h:128
VOID ScrSetFont(_In_ PUCHAR FontBitfield)
Definition: font.c:66
VOID NTAPI MmUnmapIoSpace(IN PVOID BaseAddress, IN SIZE_T NumberOfBytes)
Definition: iosup.c:193
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
WCHAR SourceName[256]
Definition: arping.c:28
Definition: fci.c:65
#define DPRINT1
Definition: precomp.h:8
#define FILE_SYNCHRONOUS_IO_NONALERT
Definition: from_kernel.h:31
void __cdecl _disable(void)
Definition: intrin_arm.h:365
unsigned int ULONG
Definition: retypes.h:1
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
void WRITE_PORT_UCHAR(PUCHAR Address, UCHAR Value)
Definition: mach.c:532
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define GCT_COMMAND
Definition: blue.h:117
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
return STATUS_SUCCESS
Definition: btrfs.c:3014
#define GCT_READ_PLANE
Definition: blue.h:126
NTSTATUS ExtractFont(_In_ ULONG CodePage, _In_ PUCHAR FontBitField)
Definition: font.c:90
VOID OpenBitPlane(VOID)
Definition: font.c:280
NTSYSAPI NTSTATUS NTAPI ZwOpenSymbolicLinkObject(_Out_ PHANDLE SymbolicLinkHandle, _In_ ACCESS_MASK DesiredAccess, _In_ POBJECT_ATTRIBUTES ObjectAttributes)
IN PDCB IN PCCB IN VBO IN OUT PULONG OUT PDIRENT OUT PBCB OUT PVBO ByteOffset
Definition: fatprocs.h:725