ReactOS  0.4.14-dev-828-g8dc90a4
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 /* PRIVATE FUNCTIONS *********************************************************/
66 
69  _In_ ULONG CodePage,
70  _In_ PUCHAR FontBitField)
71 {
72  BOOLEAN bFoundFile = FALSE;
73  HANDLE Handle;
75  CHAR FileName[20];
78  UNICODE_STRING LinkName;
80  CFHEADER CabFileHeader;
81  CFFILE CabFile;
82  ULONG CabFileOffset = 0;
84  WCHAR SourceBuffer[MAX_PATH] = { L'\0' };
85  ULONG ReadCP;
86 
89 
90  RtlInitUnicodeString(&LinkName,
91  L"\\SystemRoot");
92 
94  &LinkName,
96  NULL,
97  NULL);
98 
102 
103  if (!NT_SUCCESS(Status))
104  {
105  DPRINT1("ZwOpenSymbolicLinkObject failed with Status 0x%lx\n", Status);
106  return Status;
107  }
108 
109  SourceName.Length = 0;
110  SourceName.MaximumLength = MAX_PATH * sizeof(WCHAR);
111  SourceName.Buffer = SourceBuffer;
112 
113  Status = ZwQuerySymbolicLinkObject(Handle,
114  &SourceName,
115  NULL);
116  ZwClose(Handle);
117 
118  if (!NT_SUCCESS(Status))
119  {
120  DPRINT1("ZwQuerySymbolicLinkObject failed with Status 0x%lx\n", Status);
121  return Status;
122  }
123 
124  Status = RtlAppendUnicodeToString(&SourceName, L"\\vgafonts.cab");
125  if (!NT_SUCCESS(Status))
126  {
127  DPRINT1("RtlAppendUnicodeToString failed with Status 0x%lx\n", Status);
128  return Status;
129  }
130 
132  &SourceName,
134  NULL,
135  NULL);
136 
137  Status = ZwCreateFile(&Handle,
138  GENERIC_READ,
140  &IoStatusBlock,
141  NULL,
143  0,
144  FILE_OPEN,
146  NULL,
147  0);
148  if (!NT_SUCCESS(Status))
149  {
150  DPRINT1("Error: Cannot open vgafonts.cab (0x%lx)\n", Status);
151  return Status;
152  }
153 
154  ByteOffset.QuadPart = 0;
155  Status = ZwReadFile(Handle,
156  NULL,
157  NULL,
158  NULL,
159  &IoStatusBlock,
160  &CabFileHeader,
161  sizeof(CabFileHeader),
162  &ByteOffset,
163  NULL);
164 
165  if (!NT_SUCCESS(Status))
166  {
167  DPRINT1("Error: Cannot read from file (0x%lx)\n", Status);
168  goto Exit;
169  }
170 
171  if (CabFileHeader.Signature != CAB_SIGNATURE)
172  {
173  DPRINT1("Invalid CAB signature: 0x%lx!\n", CabFileHeader.Signature);
175  goto Exit;
176  }
177 
178  // We have a valid CAB file!
179  // Read the file table now and decrement the file count on every file. When it's zero, we read the complete table.
180  ByteOffset.QuadPart = CabFileHeader.FileTableOffset;
181 
182  while (CabFileHeader.FileCount)
183  {
184  Status = ZwReadFile(Handle,
185  NULL,
186  NULL,
187  NULL,
188  &IoStatusBlock,
189  &CabFile,
190  sizeof(CabFile),
191  &ByteOffset,
192  NULL);
193 
194  if (NT_SUCCESS(Status))
195  {
196  ByteOffset.QuadPart += sizeof(CabFile);
197 
198  // We assume here that the file name is max. 19 characters (+ 1 NULL character) long.
199  // This should be enough for our purpose.
200  Status = ZwReadFile(Handle,
201  NULL,
202  NULL,
203  NULL,
204  &IoStatusBlock,
205  FileName,
206  sizeof(FileName),
207  &ByteOffset,
208  NULL);
209 
210  if (NT_SUCCESS(Status))
211  {
212  if (!bFoundFile)
213  {
214  Status = RtlCharToInteger(FileName, 0, &ReadCP);
215  if (NT_SUCCESS(Status) && ReadCP == CodePage)
216  {
217  // We got the correct file.
218  // Save the offset and loop through the rest of the file table to find the position, where the actual data starts.
219  CabFileOffset = CabFile.FileOffset;
220  bFoundFile = TRUE;
221  }
222  }
223 
224  ByteOffset.QuadPart += strlen(FileName) + 1;
225  }
226  }
227 
228  CabFileHeader.FileCount--;
229  }
230 
231  // 8 = Size of a CFFOLDER structure (see cabman). As we don't need the values of that structure, just increase the offset here.
232  ByteOffset.QuadPart += 8;
233  ByteOffset.QuadPart += CabFileOffset;
234 
235  // ByteOffset now contains the offset of the actual data, so we can read the RAW font
236  Status = ZwReadFile(Handle,
237  NULL,
238  NULL,
239  NULL,
240  &IoStatusBlock,
241  FontBitField,
242  2048,
243  &ByteOffset,
244  NULL);
245  if (!NT_SUCCESS(Status))
246  {
247  DPRINT1("ZwReadFile failed with Status 0x%lx\n", Status);
248  }
249 
250 Exit:
251 
252  ZwClose(Handle);
253  return Status;
254 }
255 
256 /* Font-load specific funcs */
257 VOID
259 {
260  /* disable interrupts */
261  _disable();
262 
263  /* sequence reg */
268 
269  /* graphic reg */
273 
274  /* enable interrupts */
275  _enable();
276 }
277 
278 VOID
280 {
281  /* disable interrupts */
282  _disable();
283 
284  /* sequence reg */
289 
290  /* graphic reg */
294 
295  /* enable interrupts */
296  _enable();
297 }
298 
299 VOID
301  _In_ PUCHAR Bitplane,
302  _In_ PUCHAR FontBitfield)
303 {
304  UINT32 i, j;
305 
306  for (i = 0; i < 256; i++)
307  {
308  for (j = 0; j < 8; j++)
309  {
310  *Bitplane = FontBitfield[i * 8 + j];
311  Bitplane++;
312  }
313 
314  // padding
315  for (j = 8; j < 32; j++)
316  {
317  *Bitplane = 0;
318  Bitplane++;
319  }
320  }
321 }
#define KeGetCurrentIrql()
Definition: env_spec_w32.h:706
VOID CloseBitPlane(VOID)
Definition: font.c:279
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
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 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 OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
#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:300
#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 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:2938
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
#define GCT_READ_PLANE
Definition: blue.h:126
NTSTATUS ExtractFont(_In_ ULONG CodePage, _In_ PUCHAR FontBitField)
Definition: font.c:68
VOID OpenBitPlane(VOID)
Definition: font.c:258
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:716