ReactOS 0.4.16-dev-2104-gb84fa49
bootvid.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Boot Video Driver for VGA-compatible cards
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: Main file
5 * COPYRIGHT: Copyright 2007 Alex Ionescu <alex.ionescu@reactos.org>
6 * Copyright 2019 Hermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
7 */
8
9#include "precomp.h"
10
11/* PRIVATE FUNCTIONS *********************************************************/
12
13static BOOLEAN
15 _In_ const USHORT* CmdStream)
16{
17 USHORT Cmd;
18 UCHAR Major, Minor;
23 USHORT ShortValue;
24
25 /* First make sure that we have a Command Stream */
26 if (!CmdStream)
27 return TRUE;
28
29 /* Loop as long as we have commands */
30 while (*CmdStream != EOD)
31 {
32 /* Get the next command and its Major and Minor functions */
33 Cmd = *CmdStream++;
34 Major = Cmd & 0xF0;
35 Minor = Cmd & 0x0F;
36
37 /* Check which major function this is */
38 if (Major == INOUT)
39 {
40 /* Check the minor function */
41 if (Minor & IO /* CMD_STREAM_READ */)
42 {
43 /* Check the sub-type */
44 if (Minor & BW /* CMD_STREAM_USHORT */)
45 {
46 /* Get the port and read an USHORT from it */
47 Port = *CmdStream++;
48 ShortValue = __inpw(Port);
49 }
50 else // if (Minor & CMD_STREAM_WRITE)
51 {
52 /* Get the port and read an UCHAR from it */
53 Port = *CmdStream++;
54 Value = __inpb(Port);
55 }
56 }
57 else if (Minor & MULTI /* CMD_STREAM_WRITE_ARRAY */)
58 {
59 /* Check the sub-type */
60 if (Minor & BW /* CMD_STREAM_USHORT */)
61 {
62 /* Get the port and the count of elements */
63 Port = *CmdStream++;
64 Count = *CmdStream++;
65
66 /* Write the USHORT to the port; the buffer is what's in the command stream */
68
69 /* Move past the buffer in the command stream */
70 CmdStream += Count;
71 }
72 else // if (Minor & CMD_STREAM_WRITE)
73 {
74 /* Get the port and the count of elements */
75 Port = *CmdStream++;
76 Count = *CmdStream++;
77
78 /* Loop the command array */
79 for (; Count; --Count, ++CmdStream)
80 {
81 /* Get the UCHAR and write it to the port */
82 Value = (UCHAR)*CmdStream;
84 }
85 }
86 }
87 else if (Minor & BW /* CMD_STREAM_USHORT */)
88 {
89 /* Get the port */
90 Port = *CmdStream++;
91
92 /* Get the USHORT and write it to the port */
93 ShortValue = *CmdStream++;
94 __outpw(Port, ShortValue);
95 }
96 else // if (Minor & CMD_STREAM_WRITE)
97 {
98 /* Get the port */
99 Port = *CmdStream++;
100
101 /* Get the UCHAR and write it to the port */
102 Value = (UCHAR)*CmdStream++;
104 }
105 }
106 else if (Major == METAOUT)
107 {
108 /* Check the minor function. Note these are not flags. */
109 switch (Minor)
110 {
111 case INDXOUT:
112 {
113 /* Get the port, the count of elements and the start index */
114 Port = *CmdStream++;
115 Count = *CmdStream++;
116 Index = (UCHAR)*CmdStream++;
117
118 /* Loop the command array */
119 for (; Count; --Count, ++Index, ++CmdStream)
120 {
121 /* Get the USHORT and write it to the port */
122 ShortValue = (USHORT)Index + ((*CmdStream) << 8);
123 __outpw(Port, ShortValue);
124 }
125 break;
126 }
127
128 case ATCOUT:
129 {
130 /* Get the port, the count of elements and the start index */
131 Port = *CmdStream++;
132 Count = *CmdStream++;
133 Index = (UCHAR)*CmdStream++;
134
135 /* Loop the command array */
136 for (; Count; --Count, ++Index, ++CmdStream)
137 {
138 /* Write the index */
140
141 /* Get the UCHAR and write it to the port */
142 Value = (UCHAR)*CmdStream;
144 }
145 break;
146 }
147
148 case MASKOUT:
149 {
150 /* Get the port */
151 Port = *CmdStream++;
152
153 /* Read the current value and add the stream data */
154 Value = __inpb(Port);
155 Value &= *CmdStream++;
156 Value ^= *CmdStream++;
157
158 /* Write the value */
160 break;
161 }
162
163 default:
164 /* Unknown command, fail */
165 return FALSE;
166 }
167 }
168 else if (Major != NCMD)
169 {
170 /* Unknown major function, fail */
171 return FALSE;
172 }
173 }
174
175 /* If we got here, return success */
176 return TRUE;
177}
178
179static BOOLEAN
181{
182 UCHAR OrgGCAddr, OrgReadMap, OrgBitMask;
183 UCHAR OrgSCAddr, OrgMemMode;
184 UCHAR i;
185
186 /* Remember the original state of the Graphics Controller Address register */
188
189 /*
190 * Write the Read Map register with a known state so we can verify
191 * that it isn't changed after we fool with the Bit Mask. This ensures
192 * that we're dealing with indexed registers, since both the Read Map and
193 * the Bit Mask are addressed at GRAPH_DATA_PORT.
194 */
196
197 /*
198 * If we can't read back the Graphics Address register setting we just
199 * performed, it's not readable and this isn't a VGA.
200 */
202 return FALSE;
203
204 /*
205 * Set the Read Map register to a known state.
206 */
209
210 /* Read it back... it should be the same */
212 {
213 /*
214 * The Read Map setting we just performed can't be read back; not a
215 * VGA. Restore the default Read Map state and fail.
216 */
218 return FALSE;
219 }
220
221 /* Remember the original setting of the Bit Mask register */
223
224 /* Read it back... it should be the same */
226 {
227 /*
228 * The Graphics Address register setting we just made can't be read
229 * back; not a VGA. Restore the default Read Map state and fail.
230 */
233 return FALSE;
234 }
235
236 /* Read the VGA Data Register */
238
239 /*
240 * Set up the initial test mask we'll write to and read from the Bit Mask,
241 * and loop on the bitmasks.
242 */
243 for (i = 0xBB; i; i >>= 1)
244 {
245 /* Write the test mask to the Bit Mask */
247
248 /* Read it back... it should be the same */
250 {
251 /*
252 * The Bit Mask is not properly writable and readable; not a VGA.
253 * Restore the Bit Mask and Read Map to their default states and fail.
254 */
258 return FALSE;
259 }
260 }
261
262 /*
263 * There's something readable at GRAPH_DATA_PORT; now switch back and
264 * make sure that the Read Map register hasn't changed, to verify that
265 * we're dealing with indexed registers.
266 */
268
269 /* Read it back */
271 {
272 /*
273 * The Read Map is not properly writable and readable; not a VGA.
274 * Restore the Bit Mask and Read Map to their default states, in case
275 * this is an EGA, so subsequent writes to the screen aren't garbled.
276 * Then fail.
277 */
281 return FALSE;
282 }
283
284 /*
285 * We've pretty surely verified the existence of the Bit Mask register.
286 * Put the Graphics Controller back to the original state.
287 */
292
293 /*
294 * Now, check for the existence of the Chain4 bit.
295 */
296
297 /*
298 * Remember the original states of the Sequencer Address and Memory Mode
299 * registers.
300 */
303
304 /* Read it back... it should be the same */
306 {
307 /*
308 * Couldn't read back the Sequencer Address register setting
309 * we just performed, fail.
310 */
311 return FALSE;
312 }
313
314 /* Read sequencer Data */
315 OrgMemMode = __inpb(VGA_BASE_IO_PORT + SEQ_DATA_PORT);
316
317 /*
318 * Toggle the Chain4 bit and read back the result. This must be done during
319 * sync reset, since we're changing the chaining state.
320 */
321
322 /* Begin sync reset */
324
325 /* Toggle the Chain4 bit */
328
329 /* Read it back... it should be the same */
330 if (__inpb(VGA_BASE_IO_PORT + SEQ_DATA_PORT) != (OrgMemMode ^ CHAIN4_MASK))
331 {
332 /*
333 * Chain4 bit is not there, not a VGA.
334 * Set text mode default for Memory Mode register.
335 */
337
338 /* End sync reset */
340
341 /* Fail */
342 return FALSE;
343 }
344
345 /*
346 * It's a VGA.
347 */
348
349 /* Restore the original Memory Mode setting */
351
352 /* End sync reset */
354
355 /* Restore the original Sequencer Address setting */
357
358 /* VGA is present! */
359 return TRUE;
360}
361
362/* PUBLIC FUNCTIONS **********************************************************/
363
365NTAPI
367 _In_ BOOLEAN SetMode)
368{
369 ULONG_PTR Context = 0;
371 PHYSICAL_ADDRESS NullAddress = {{0, 0}}, VgaAddress;
375
376 /* Make sure that we have a bus translation function */
378
379 /* Loop trying to find possible VGA base addresses */
380 while (TRUE)
381 {
382 /* Get the VGA Register address */
383 AddressSpace = 1;
387 &Context,
388 TRUE);
389 if (!Result) return FALSE;
390
391 /* See if this is I/O Space, which we need to map */
392 if (!AddressSpace)
393 {
394 /* Map it */
396 }
397 else
398 {
399 /* The base is the translated address, no need to map I/O space */
401 }
402
403 /* Try to see if this is VGA */
405 if (VgaIsPresent())
406 {
407 /* Translate the VGA Memory Address */
408 VgaAddress.LowPart = MEM_VGA;
409 VgaAddress.HighPart = 0;
410 AddressSpace = 0;
414 &Context,
415 FALSE);
416 if (Result) break;
417 }
418 else
419 {
420 /* It's not, so unmap the I/O space if we mapped it */
422 }
423
424 /* Continue trying to see if there's any other address */
425 }
426
427 /* Success! See if this is I/O Space, which we need to map */
428 if (!AddressSpace)
429 {
430 /* Map it */
434 }
435 else
436 {
437 /* The base is the translated address, no need to map I/O space */
439 }
440
441 /* Set the VGA Memory Base */
442 VgaBase = Base;
443
444 /* Check whether we have to set the video mode */
445 if (SetMode)
446 {
447 /* Clear the current position */
448 VidpCurrentX = 0;
449 VidpCurrentY = 0;
450
451 /* Reset the display and initialize it */
452 if (HalResetDisplay())
453 {
454 /* The HAL handled the display, re-initialize only the AC registers */
456 }
457 else
458 {
459 /* The HAL didn't handle the display, fully re-initialize the VGA */
461 }
462 }
463
464 return TRUE;
465}
466
467VOID
469 _In_ BOOLEAN SetMode)
470{
471 /* Reset the video mode with HAL if requested */
472 if (SetMode && !HalResetDisplay())
473 {
474 /* The HAL didn't handle the display, fully re-initialize the VGA */
476 }
477
478 /* Always re-initialize the AC registers */
480
481 /* Re-initialize the palette and fill the screen black */
484}
unsigned char BOOLEAN
VOID NTAPI VidSolidColorFill(_In_ ULONG Left, _In_ ULONG Top, _In_ ULONG Right, _In_ ULONG Bottom, _In_ UCHAR Color)
Definition: bootvid.c:298
VOID ResetDisplay(_In_ BOOLEAN SetMode)
Definition: bootvid.c:263
BOOLEAN NTAPI VidInitialize(_In_ BOOLEAN SetMode)
Definition: bootvid.c:231
#define WRITE_PORT_BUFFER_USHORT(port, buffer, count)
Definition: hardware.h:34
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
ULONG VidpCurrentY
Definition: console.c:16
ULONG VidpCurrentX
Definition: console.c:15
const USHORT VGA_640x480[]
Definition: bootdata.c:47
const USHORT AT_Initialization[]
Definition: bootdata.c:16
#define ATCOUT
Definition: cmdcnst.h:76
#define NCMD
Definition: cmdcnst.h:63
#define INOUT
Definition: cmdcnst.h:61
#define INDXOUT
Definition: cmdcnst.h:75
#define BW
Definition: cmdcnst.h:70
#define MASKOUT
Definition: cmdcnst.h:77
#define EOD
Definition: cmdcnst.h:60
#define METAOUT
Definition: cmdcnst.h:62
#define MULTI
Definition: cmdcnst.h:69
#define IO
Definition: cmdcnst.h:71
#define START_SYNC_RESET_VALUE
Definition: vga.h:123
#define SEQ_ADDRESS_PORT
Definition: vga.h:69
#define SEQ_ADDR_MASK
Definition: vga.h:145
#define GRAPH_ADDRESS_PORT
Definition: vga.h:81
#define GRAPH_DATA_PORT
Definition: vga.h:82
#define MEM_VGA_SIZE
Definition: vga.h:22
#define BIT_MASK_DEFAULT
Definition: vga.h:167
#define GRAPH_ADDR_MASK
Definition: vga.h:144
#define CHAIN4_MASK
Definition: vga.h:151
#define IND_BIT_MASK
Definition: vga.h:114
#define READ_MAP_DEFAULT
Definition: vga.h:168
#define IND_READ_MAP
Definition: vga.h:111
#define IND_SYNC_RESET
Definition: vga.h:115
#define READ_MAP_TEST_SETTING
Definition: vga.h:159
#define IND_MEMORY_MODE
Definition: vga.h:117
#define VGA_BASE_IO_PORT
Definition: vga.h:38
#define SEQ_DATA_PORT
Definition: vga.h:70
#define END_SYNC_RESET_VALUE
Definition: vga.h:125
#define MEM_VGA
Definition: vga.h:21
#define MEMORY_MODE_TEXT_DEFAULT
Definition: vga.h:166
#define InitializePalette()
Definition: precomp.h:84
#define ULONG_PTR
Definition: config.h:101
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
CPPORT Port[4]
Definition: headless.c:35
static BOOLEAN VgaIsPresent(VOID)
Definition: bootvid.c:180
static BOOLEAN VgaInterpretCmdStream(_In_ const USHORT *CmdStream)
Definition: bootvid.c:14
VOID NTAPI MmUnmapIoSpace(IN PVOID BaseAddress, IN SIZE_T NumberOfBytes)
Definition: iosup.c:193
PVOID NTAPI MmMapIoSpace(IN PHYSICAL_ADDRESS PhysicalAddress, IN SIZE_T NumberOfBytes, IN MEMORY_CACHING_TYPE CacheType)
Definition: iosup.c:47
#define HalFindBusAddressTranslation
Definition: halfuncs.h:44
#define HalResetDisplay
Definition: halfuncs.h:45
_In_opt_ ULONG Base
Definition: rtlfuncs.h:2486
#define _In_
Definition: no_sal2.h:158
int Count
Definition: noreturn.cpp:7
#define SCREEN_WIDTH
Definition: pc98video.c:24
#define SCREEN_HEIGHT
Definition: pc98video.c:25
ULONG_PTR VgaRegisterBase
Definition: vga.c:68
#define __inpb(Port)
Definition: pc.h:18
#define __outpw(Port, Value)
Definition: pc.h:27
ULONG_PTR VgaBase
Definition: vga.c:69
#define __outpb(Port, Value)
Definition: pc.h:24
#define __inpw(Port)
Definition: pc.h:21
unsigned short USHORT
Definition: pedump.c:61
@ Cmd
Definition: sacdrv.h:278
#define BV_COLOR_BLACK
Definition: display.h:15
#define NTAPI
Definition: typedefs.h:36
uint16_t * PUSHORT
Definition: typedefs.h:56
uint32_t ULONG_PTR
Definition: typedefs.h:65
uint32_t ULONG
Definition: typedefs.h:59
ULONG LowPart
Definition: typedefs.h:106
_In_ WDFCOLLECTION _In_ ULONG Index
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _Out_opt_ PUSHORT _Inout_opt_ PUNICODE_STRING Value
Definition: wdfregistry.h:413
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:409
_Out_opt_ PULONG Minor
Definition: cmfuncs.h:44
_In_ ULONG _In_ PHYSICAL_ADDRESS _Inout_ PULONG _Out_ PPHYSICAL_ADDRESS TranslatedAddress
Definition: iofuncs.h:2275
_In_ ULONG _In_ PHYSICAL_ADDRESS _Inout_ PULONG AddressSpace
Definition: iofuncs.h:2274
@ MmNonCached
Definition: mmtypes.h:129
unsigned char UCHAR
Definition: xmlstorage.h:181