ReactOS 0.4.15-dev-7834-g00c4b3d
bios.c
Go to the documentation of this file.
1/*
2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
4 * FILE: subsystems/mvdm/ntvdm/dos/dos32krnl/bios.c
5 * PURPOSE: DOS32 Bios
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
7 */
8
9/* INCLUDES *******************************************************************/
10
11#include "ntvdm.h"
12
13#define NDEBUG
14#include <debug.h>
15
16#include "emulator.h"
17#include "int32.h"
18
19#include "../dem.h"
20#include "dos.h"
21#include "dosfiles.h"
22#include "handle.h"
23#include "memory.h"
24#include "bios/bios.h"
25
26// This is needed because on UNICODE this symbol is redirected to
27// GetEnvironmentStringsW whereas on ANSI it corresponds to the real
28// "ANSI" function (and GetEnvironmentStringsA is aliased to it).
29#undef GetEnvironmentStrings
30
31// Symmetrize the dumbness of the previous symbol: on UNICODE
32// FreeEnvironmentStrings aliases to FreeEnvironmentStringsW but
33// on "ANSI" FreeEnvironmentStrings aliases to FreeEnvironmentStringsA
34#undef FreeEnvironmentStrings
35#define FreeEnvironmentStrings FreeEnvironmentStringsA
36
37/* PRIVATE VARIABLES **********************************************************/
38
39/* PUBLIC VARIABLES ***********************************************************/
40
41/* Global DOS BIOS data area */
43
44/* PRIVATE FUNCTIONS **********************************************************/
45
46/* PUBLIC FUNCTIONS ***********************************************************/
47
49{
50 switch (Character)
51 {
52 case '\0':
53 {
54 /* Nothing */
55 break;
56 }
57
58 case '\b':
59 {
60 /* Erase the character */
64 break;
65 }
66
67 default:
68 {
69 /*
70 * Check if this is a special character
71 * NOTE: \r and \n are handled by the underlying driver!
72 */
73 if (Character < 0x20 && Character != '\r' && Character != '\n')
74 {
76 Character += 'A' - 1;
77 }
78
79 /* Echo the character */
81 }
82 }
83}
84
86{
89 WORD OldDeviceInfo;
90
91 /* Find the standard input descriptor and switch it to binary mode */
93 if (Descriptor)
94 {
95 OldDeviceInfo = Descriptor->DeviceInfo;
96 Descriptor->DeviceInfo |= FILE_INFO_BINARY;
97 }
98
99 Sda->ByteBuffer = '\0';
100 DPRINT("DosReadCharacter\n");
101
102 /* Use the file reading function */
105 1,
106 &BytesRead);
107
108 /* Check if we should echo and the file is actually the CON device */
109 if (Echo && Descriptor && Descriptor->DeviceInfo & FILE_INFO_DEVICE)
110 {
111 /* Echo the character */
113 }
114
115 /* Restore the old mode and return the character */
116 if (Descriptor) Descriptor->DeviceInfo = OldDeviceInfo;
117 return Sda->ByteBuffer;
118}
119
121{
123
124 if (Descriptor == NULL)
125 {
126 /* Invalid handle */
127 Sda->LastErrorCode = ERROR_INVALID_HANDLE; // ERROR_FILE_NOT_FOUND
128 return FALSE;
129 }
130
131 if (Descriptor->DeviceInfo & FILE_INFO_DEVICE)
132 {
133 WORD Result;
135
136 if (!Node->InputStatusRoutine) return FALSE;
137
138 Result = Node->InputStatusRoutine(Node);
139 return !(Result & DOS_DEVSTAT_BUSY);
140 }
141 else
142 {
143 DWORD FileSizeHigh;
144 DWORD FileSize = GetFileSize(Descriptor->Win32Handle, &FileSizeHigh);
145 LONG LocationHigh = 0;
146 DWORD Location = SetFilePointer(Descriptor->Win32Handle, 0, &LocationHigh, FILE_CURRENT);
147
148 return ((Location != FileSize) || (LocationHigh != FileSizeHigh));
149 }
150}
151
153{
155
156 Sda->ByteBuffer = Character;
157
158 /* Use the file writing function */
161 1,
162 &BytesWritten);
163}
164
166{
167 LPSTR SourcePtr, Environment;
169
170 /*
171 * Get the environment strings
172 *
173 * NOTE: On non-STANDALONE builds, this corresponds to the VDM environment
174 * as created by BaseVDM for NTVDM. On STANDALONE builds this is the Win32
175 * environment. In this last case we need to convert it to a proper VDM env.
176 */
177 SourcePtr = Environment = GetEnvironmentStrings();
178 if (Environment == NULL) return FALSE;
179
180 /* Fill the DOS system environment block */
181 while (*SourcePtr)
182 {
183 /*
184 * - Ignore environment strings starting with a '=',
185 * they describe current directories.
186 * - Ignore also the WINDIR environment variable since
187 * DOS apps should ignore that we started from ReactOS.
188 * - Upper-case the environment names, not their values.
189 */
190 if (*SourcePtr != '=' && _strnicmp(SourcePtr, "WINDIR", 6) != 0)
191 {
192 PCHAR Delim = NULL;
193
194 /* Copy the environment string */
195 strcpy(DestPtr, SourcePtr);
196
197 /* Upper-case the environment name */
198 Delim = strchr(DestPtr, '='); // Find the '=' delimiter
199 if (Delim) *Delim = '\0'; // Temporarily replace it by NULL
200 _strupr(DestPtr); // Upper-case
201 if (Delim) *Delim = '='; // Restore the delimiter
202
203 DestPtr += strlen(SourcePtr);
204
205 /* NULL-terminate the environment string */
206 *(DestPtr++) = '\0';
207 }
208
209 /* Move to the next string */
210 SourcePtr += strlen(SourcePtr) + 1;
211 }
212 /* NULL-terminate the environment block */
213 *DestPtr = '\0';
214
215 /* Free the memory allocated for environment strings */
217
218 return TRUE;
219}
220
222{
223 FILE *Stream;
224 WCHAR Buffer[256];
225
226 /* Set the data segment */
228
229 /* Initialize the global DOS BIOS data area */
231
232 /* Initialize the DOS BIOS stack */
233 // FIXME: Add a block of fixed size for the stack in BIOS/DOS_DATA instead!
234 setSS(0x0F00);
235 setSP(0x0FF0);
237
238 /*
239 * Initialize the INT 13h (BIOS Disk Services) handler chain support.
240 *
241 * The INT 13h handler chain is some functionality that allows DOS
242 * to insert disk filter drivers in between the (hooked) INT 13h handler
243 * and its original handler.
244 * Typically, those are:
245 * - filter for detecting disk changes (for floppy disks),
246 * - filter for tracking formatting calls and correcting DMA boundary errors,
247 * - a possible filter to work around a bug in a particular version of PC-AT's
248 * IBM's ROM BIOS (on systems with model byte FCh and BIOS date "01/10/84" only)
249 * (see http://www.ctyme.com/intr/rb-4453.htm for more details).
250 *
251 * This functionality is known to be used by some legitimate programs,
252 * by Windows 3.x, as well as some illegitimate ones (aka. virii).
253 *
254 * See extra information about this support in dos.h
255 */
256 // FIXME: Should be done by the DOS BIOS
259// RegisterDosInt32(0x13, DosInt13h); // Unused at the moment!
260
261 //
262 // HERE: Do all hardware initialization needed for DOS
263 //
264
265 /*
266 * SysInit part...
267 */
268
269 /* Initialize the DOS kernel (DosInit) */
270 if (!DosKRNLInitialize())
271 {
272 BiosDisplayMessage("Failed to load the DOS kernel! Exiting...\n");
273 return FALSE;
274 }
275
276 /* DOS kernel loading succeeded, we can finish the initialization */
277
278 /* Build the system master (pre-) environment block (inherited by the shell) */
279 if (!DosBuildSysEnvBlock())
280 {
281 DosDisplayMessage("An error occurred when setting up the system environment block.\n");
282 }
283
284 /* TODO: Read CONFIG.NT/SYS */
286 if (Stream != NULL)
287 {
289 {
290 // TODO: Parse the line
291 }
292 fclose(Stream);
293 }
294
295 return TRUE;
296}
297
298/* EOF */
unsigned char BOOLEAN
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
char * strchr(const char *String, int ch)
Definition: utclib.c:501
Definition: bufpool.h:45
#define BiosDisplayMessage(Format,...)
Definition: dem.h:34
#define DosDisplayMessage(Format,...)
Definition: dem.h:38
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
#define SetFilePointer
Definition: compat.h:743
#define _strnicmp(_String1, _String2, _MaxCount)
Definition: compat.h:23
#define ERROR_INVALID_HANDLE
Definition: compat.h:98
DWORD WINAPI GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh)
Definition: fileinfo.c:331
BOOLEAN DosKRNLInitialize(VOID)
Definition: dos.c:2264
PDOS_SDA Sda
Definition: dos.c:48
WORD DosReadFile(WORD FileHandle, DWORD Buffer, WORD Count, LPWORD BytesRead)
Definition: dosfiles.c:768
PDOS_FILE_DESCRIPTOR DosGetHandleFileDescriptor(WORD DosHandle)
Definition: dosfiles.c:173
WORD DosWriteFile(WORD FileHandle, DWORD Buffer, WORD Count, LPWORD BytesWritten)
Definition: dosfiles.c:915
#define FILE_INFO_DEVICE
Definition: dosfiles.h:16
#define FILE_INFO_BINARY
Definition: dosfiles.h:15
#define SEG_OFF_TO_PTR(seg, off)
Definition: emulator.h:32
unsigned long DWORD
Definition: ntddk_ex.h:95
unsigned short WORD
Definition: ntddk_ex.h:93
_Must_inspect_result_ _In_opt_ PFLT_INSTANCE _Out_ PHANDLE FileHandle
Definition: fltkernel.h:1231
_Must_inspect_result_ _Out_ PLARGE_INTEGER FileSize
Definition: fsrtlfuncs.h:108
_Check_return_ _CRTIMP FILE *__cdecl _wfopen(_In_z_ const wchar_t *_Filename, _In_z_ const wchar_t *_Mode)
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
PVOID PVOID PWCHAR PVOID Environment
Definition: env.c:47
static IStream Stream
Definition: htmldoc.c:1115
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
#define L(x)
Definition: ntvdm.h:50
long LONG
Definition: pedump.c:60
_CRTIMP char *__cdecl _strupr(_Inout_z_ char *_String)
#define DPRINT
Definition: sndvol32.h:71
DWORD PrevInt13
Definition: dos.h:296
DWORD RomBiosInt13
Definition: dos.h:295
WORD LastErrorCode
Definition: dos.h:160
BYTE ByteBuffer
Definition: dos.h:202
#define FreeEnvironmentStrings
Definition: bios.c:35
PBIOS_DATA BiosData
Definition: bios.c:42
CHAR DosReadCharacter(WORD FileHandle, BOOLEAN Echo)
Definition: bios.c:85
VOID DosEchoCharacter(CHAR Character)
Definition: bios.c:48
VOID DosPrintCharacter(WORD FileHandle, CHAR Character)
Definition: bios.c:152
BOOLEAN DosCheckInput(VOID)
Definition: bios.c:120
BOOLEAN DosBuildSysEnvBlock(VOID)
Definition: bios.c:165
BOOLEAN DosBIOSInitialize(VOID)
Definition: bios.c:221
PDOS_DEVICE_NODE DosGetDriverNode(DWORD Driver)
Definition: device.c:305
#define DOS_DEVSTAT_BUSY
Definition: device.h:50
#define SYSTEM_ENV_BLOCK
Definition: dos.h:37
#define DOS_CONFIG_PATH
Definition: dos.h:27
#define DOS_DATA_OFFSET(x)
Definition: dos.h:35
#define DOS_INPUT_HANDLE
Definition: dos.h:42
struct _BIOS_DATA * PBIOS_DATA
#define DOS_OUTPUT_HANDLE
Definition: dos.h:43
#define BIOS_DATA_SEGMENT
Definition: dos.h:31
#define DOS_DATA_SEGMENT
Definition: dos.h:33
@ Echo
Definition: telnetd.h:64
uint32_t * PULONG
Definition: typedefs.h:59
#define MAKELONG(a, b)
Definition: typedefs.h:249
char * PCHAR
Definition: typedefs.h:51
Definition: dlist.c:348
VOID WINAPI setSP(USHORT)
Definition: registers.c:351
VOID WINAPI setDS(USHORT)
Definition: registers.c:515
VOID WINAPI setSS(USHORT)
Definition: registers.c:501
_Must_inspect_result_ _In_ WDFIOTARGET _In_opt_ WDFREQUEST _In_opt_ PWDF_MEMORY_DESCRIPTOR _In_opt_ PLONGLONG _In_opt_ PWDF_REQUEST_SEND_OPTIONS _Out_opt_ PULONG_PTR BytesWritten
Definition: wdfiotarget.h:960
_Must_inspect_result_ _In_ WDFIOTARGET _In_opt_ WDFREQUEST _In_opt_ PWDF_MEMORY_DESCRIPTOR _In_opt_ PLONGLONG _In_opt_ PWDF_REQUEST_SEND_OPTIONS _Out_opt_ PULONG_PTR BytesRead
Definition: wdfiotarget.h:870
_Must_inspect_result_ _In_ WDFIORESLIST _In_ PIO_RESOURCE_DESCRIPTOR Descriptor
Definition: wdfresource.h:342
LPSTR WINAPI GetEnvironmentStrings(void)
#define FILE_CURRENT
Definition: winbase.h:113
wchar_t * fgetws(wchar_t *buf, int bufsize, FILE *file)
Definition: wmain.c:22
_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
char * LPSTR
Definition: xmlstorage.h:182
__wchar_t WCHAR
Definition: xmlstorage.h:180
char CHAR
Definition: xmlstorage.h:175