Start source tree (final, I hope!) restructuration. Part 1/X
[reactos.git] / reactos / subsystems / mvdm / ntvdm / dos / dos32krnl / bios.c
1 /*
2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
4 * FILE: 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 #define NDEBUG
12
13 #include "emulator.h"
14 #include "int32.h"
15
16 #include "dos.h"
17 #include "bios/bios.h"
18
19 // This is needed because on UNICODE this symbol is redirected to
20 // GetEnvironmentStringsW whereas on ANSI it corresponds to the real
21 // "ANSI" function (and GetEnvironmentStringsA is aliased to it).
22 #undef GetEnvironmentStrings
23
24 // Symmetrize the dumbness of the previous symbol: on UNICODE
25 // FreeEnvironmentStrings aliases to FreeEnvironmentStringsW but
26 // on "ANSI" FreeEnvironmentStrings aliases to FreeEnvironmentStringsA
27 #undef FreeEnvironmentStrings
28 #define FreeEnvironmentStrings FreeEnvironmentStringsA
29
30 /* PRIVATE VARIABLES **********************************************************/
31
32 // static BYTE CurrentDrive;
33 // static CHAR CurrentDirectories[NUM_DRIVES][DOS_DIR_LENGTH];
34
35 /* PRIVATE FUNCTIONS **********************************************************/
36
37 /* PUBLIC FUNCTIONS ***********************************************************/
38
39 CHAR DosReadCharacter(WORD FileHandle)
40 {
41 CHAR Character = '\0';
42 WORD BytesRead;
43
44 DPRINT("DosReadCharacter\n");
45
46 /* Use the file reading function */
47 DosReadFile(FileHandle, &Character, 1, &BytesRead);
48
49 return Character;
50 }
51
52 BOOLEAN DosCheckInput(VOID)
53 {
54 HANDLE Handle = DosGetRealHandle(DOS_INPUT_HANDLE);
55
56 if (IsConsoleHandle(Handle))
57 {
58 /* Save AX */
59 USHORT AX = getAX();
60
61 /* Call the BIOS */
62 setAH(0x01); // or 0x11 for enhanced, but what to choose?
63 Int32Call(&DosContext, BIOS_KBD_INTERRUPT);
64
65 /* Restore AX */
66 setAX(AX);
67
68 /* Return keyboard status */
69 return (getZF() == 0);
70 }
71 else
72 {
73 DWORD FileSizeHigh;
74 DWORD FileSize = GetFileSize(Handle, &FileSizeHigh);
75 LONG LocationHigh = 0;
76 DWORD Location = SetFilePointer(Handle, 0, &LocationHigh, FILE_CURRENT);
77
78 return ((Location != FileSize) || (LocationHigh != FileSizeHigh));
79 }
80 }
81
82 VOID DosPrintCharacter(WORD FileHandle, CHAR Character)
83 {
84 WORD BytesWritten;
85
86 /* Use the file writing function */
87 DosWriteFile(FileHandle, &Character, 1, &BytesWritten);
88 }
89
90 BOOLEAN DosBIOSInitialize(VOID)
91 {
92 PDOS_MCB Mcb = SEGMENT_TO_MCB(FIRST_MCB_SEGMENT);
93
94 LPSTR SourcePtr, Environment;
95 LPSTR DestPtr = (LPSTR)SEG_OFF_TO_PTR(SYSTEM_ENV_BLOCK, 0);
96
97 #if 0
98 UCHAR i;
99 CHAR CurrentDirectory[MAX_PATH];
100 CHAR DosDirectory[DOS_DIR_LENGTH];
101 LPSTR Path;
102
103 FILE *Stream;
104 WCHAR Buffer[256];
105 #endif
106
107 /* Initialize the MCB */
108 Mcb->BlockType = 'Z';
109 Mcb->Size = USER_MEMORY_SIZE;
110 Mcb->OwnerPsp = 0;
111
112 /* Initialize the link MCB to the UMB area */
113 Mcb = SEGMENT_TO_MCB(FIRST_MCB_SEGMENT + USER_MEMORY_SIZE + 1);
114 Mcb->BlockType = 'M';
115 Mcb->Size = UMB_START_SEGMENT - FIRST_MCB_SEGMENT - USER_MEMORY_SIZE - 2;
116 Mcb->OwnerPsp = SYSTEM_PSP;
117
118 /* Initialize the UMB area */
119 Mcb = SEGMENT_TO_MCB(UMB_START_SEGMENT);
120 Mcb->BlockType = 'Z';
121 Mcb->Size = UMB_END_SEGMENT - UMB_START_SEGMENT;
122 Mcb->OwnerPsp = 0;
123
124 /* Get the environment strings */
125 SourcePtr = Environment = GetEnvironmentStrings();
126 if (Environment == NULL) return FALSE;
127
128 /* Fill the DOS system environment block */
129 while (*SourcePtr)
130 {
131 /*
132 * - Ignore environment strings starting with a '=',
133 * they describe current directories.
134 * - Ignore also the WINDIR environment variable since
135 * DOS apps should ignore that we started from ReactOS.
136 * - Upper-case the environment names, not their values.
137 */
138 if (*SourcePtr != '=' && _strnicmp(SourcePtr, "WINDIR", 6) != 0)
139 {
140 PCHAR Delim = NULL;
141
142 /* Copy the environment string */
143 strcpy(DestPtr, SourcePtr);
144
145 /* Upper-case the environment name */
146 Delim = strchr(DestPtr, '='); // Find the '=' delimiter
147 if (Delim) *Delim = '\0'; // Temporarily replace it by NULL
148 _strupr(DestPtr); // Upper-case
149 if (Delim) *Delim = '='; // Restore the delimiter
150
151 DestPtr += strlen(SourcePtr);
152
153 /* NULL-terminate the environment string */
154 *(DestPtr++) = '\0';
155 }
156
157 /* Move to the next string */
158 SourcePtr += strlen(SourcePtr) + 1;
159 }
160 /* NULL-terminate the environment block */
161 *DestPtr = '\0';
162
163 /* Free the memory allocated for environment strings */
164 FreeEnvironmentStrings(Environment);
165
166
167 #if 0
168
169 /* Clear the current directory buffer */
170 RtlZeroMemory(CurrentDirectories, sizeof(CurrentDirectories));
171
172 /* Get the current directory */
173 if (!GetCurrentDirectoryA(MAX_PATH, CurrentDirectory))
174 {
175 // TODO: Use some kind of default path?
176 return FALSE;
177 }
178
179 /* Convert that to a DOS path */
180 if (!GetShortPathNameA(CurrentDirectory, DosDirectory, DOS_DIR_LENGTH))
181 {
182 // TODO: Use some kind of default path?
183 return FALSE;
184 }
185
186 /* Set the drive */
187 CurrentDrive = DosDirectory[0] - 'A';
188
189 /* Get the directory part of the path */
190 Path = strchr(DosDirectory, '\\');
191 if (Path != NULL)
192 {
193 /* Skip the backslash */
194 Path++;
195 }
196
197 /* Set the directory */
198 if (Path != NULL)
199 {
200 strncpy(CurrentDirectories[CurrentDrive], Path, DOS_DIR_LENGTH);
201 }
202
203 /* Read CONFIG.SYS */
204 Stream = _wfopen(DOS_CONFIG_PATH, L"r");
205 if (Stream != NULL)
206 {
207 while (fgetws(Buffer, sizeof(Buffer)/sizeof(Buffer[0]), Stream))
208 {
209 // TODO: Parse the line
210 }
211 fclose(Stream);
212 }
213
214 #endif
215
216
217 /* Register the DOS 32-bit Interrupts */
218 // RegisterDosInt32(0x20, DosInt20h);
219
220 /* Initialize the DOS kernel */
221 return DosKRNLInitialize();
222 }
223
224 /* EOF */