[NTVDM]
[reactos.git] / reactos / subsystems / 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
18 #include "bios/bios.h"
19
20 /* PRIVATE VARIABLES **********************************************************/
21
22 // static BYTE CurrentDrive;
23 // static CHAR CurrentDirectories[NUM_DRIVES][DOS_DIR_LENGTH];
24
25 /* PRIVATE FUNCTIONS **********************************************************/
26
27 /* PUBLIC FUNCTIONS ***********************************************************/
28
29 CHAR DosReadCharacter(WORD FileHandle)
30 {
31 CHAR Character = '\0';
32 WORD BytesRead;
33
34 DPRINT("DosReadCharacter\n");
35
36 /* Use the file reading function */
37 DosReadFile(FileHandle, &Character, 1, &BytesRead);
38
39 return Character;
40 }
41
42 BOOLEAN DosCheckInput(VOID)
43 {
44 HANDLE Handle = DosGetRealHandle(DOS_INPUT_HANDLE);
45
46 if (IsConsoleHandle(Handle))
47 {
48 /* Save AX */
49 USHORT AX = getAX();
50
51 /* Call the BIOS */
52 setAH(0x01); // or 0x11 for enhanced, but what to choose?
53 Int32Call(&DosContext, BIOS_KBD_INTERRUPT);
54
55 /* Restore AX */
56 setAX(AX);
57
58 /* Return keyboard status */
59 return (getZF() == 0);
60 }
61 else
62 {
63 DWORD FileSizeHigh;
64 DWORD FileSize = GetFileSize(Handle, &FileSizeHigh);
65 LONG LocationHigh = 0;
66 DWORD Location = SetFilePointer(Handle, 0, &LocationHigh, FILE_CURRENT);
67
68 return ((Location != FileSize) || (LocationHigh != FileSizeHigh));
69 }
70 }
71
72 VOID DosPrintCharacter(WORD FileHandle, CHAR Character)
73 {
74 WORD BytesWritten;
75
76 /* Use the file writing function */
77 DosWriteFile(FileHandle, &Character, 1, &BytesWritten);
78 }
79
80 BOOLEAN DosBIOSInitialize(VOID)
81 {
82 PDOS_MCB Mcb = SEGMENT_TO_MCB(FIRST_MCB_SEGMENT);
83
84 LPWSTR SourcePtr, Environment;
85 LPSTR AsciiString;
86 DWORD AsciiSize;
87 LPSTR DestPtr = (LPSTR)SEG_OFF_TO_PTR(SYSTEM_ENV_BLOCK, 0);
88
89 #if 0
90 UCHAR i;
91 CHAR CurrentDirectory[MAX_PATH];
92 CHAR DosDirectory[DOS_DIR_LENGTH];
93 LPSTR Path;
94
95 FILE *Stream;
96 WCHAR Buffer[256];
97 #endif
98
99 /* Initialize the MCB */
100 Mcb->BlockType = 'Z';
101 Mcb->Size = USER_MEMORY_SIZE;
102 Mcb->OwnerPsp = 0;
103
104 /* Initialize the link MCB to the UMB area */
105 Mcb = SEGMENT_TO_MCB(FIRST_MCB_SEGMENT + USER_MEMORY_SIZE + 1);
106 Mcb->BlockType = 'M';
107 Mcb->Size = UMB_START_SEGMENT - FIRST_MCB_SEGMENT - USER_MEMORY_SIZE - 2;
108 Mcb->OwnerPsp = SYSTEM_PSP;
109
110 /* Initialize the UMB area */
111 Mcb = SEGMENT_TO_MCB(UMB_START_SEGMENT);
112 Mcb->BlockType = 'Z';
113 Mcb->Size = UMB_END_SEGMENT - UMB_START_SEGMENT;
114 Mcb->OwnerPsp = 0;
115
116 /* Get the environment strings */
117 SourcePtr = Environment = GetEnvironmentStringsW();
118 if (Environment == NULL) return FALSE;
119
120 /* Fill the DOS system environment block */
121 while (*SourcePtr)
122 {
123 /* Get the size of the ASCII string */
124 AsciiSize = WideCharToMultiByte(CP_ACP,
125 0,
126 SourcePtr,
127 -1,
128 NULL,
129 0,
130 NULL,
131 NULL);
132
133 /* Allocate memory for the ASCII string */
134 AsciiString = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, AsciiSize);
135 if (AsciiString == NULL)
136 {
137 FreeEnvironmentStringsW(Environment);
138 return FALSE;
139 }
140
141 /* Convert to ASCII */
142 WideCharToMultiByte(CP_ACP,
143 0,
144 SourcePtr,
145 -1,
146 AsciiString,
147 AsciiSize,
148 NULL,
149 NULL);
150
151 /* Copy the string into DOS memory */
152 strcpy(DestPtr, AsciiString);
153
154 /* Move to the next string */
155 SourcePtr += wcslen(SourcePtr) + 1;
156 DestPtr += strlen(AsciiString);
157 *(DestPtr++) = 0;
158
159 /* Free the memory */
160 HeapFree(GetProcessHeap(), 0, AsciiString);
161 }
162 *DestPtr = 0;
163
164 /* Free the memory allocated for environment strings */
165 FreeEnvironmentStringsW(Environment);
166
167
168 #if 0
169
170 /* Clear the current directory buffer */
171 ZeroMemory(CurrentDirectories, sizeof(CurrentDirectories));
172
173 /* Get the current directory */
174 if (!GetCurrentDirectoryA(MAX_PATH, CurrentDirectory))
175 {
176 // TODO: Use some kind of default path?
177 return FALSE;
178 }
179
180 /* Convert that to a DOS path */
181 if (!GetShortPathNameA(CurrentDirectory, DosDirectory, DOS_DIR_LENGTH))
182 {
183 // TODO: Use some kind of default path?
184 return FALSE;
185 }
186
187 /* Set the drive */
188 CurrentDrive = DosDirectory[0] - 'A';
189
190 /* Get the directory part of the path */
191 Path = strchr(DosDirectory, '\\');
192 if (Path != NULL)
193 {
194 /* Skip the backslash */
195 Path++;
196 }
197
198 /* Set the directory */
199 if (Path != NULL)
200 {
201 strncpy(CurrentDirectories[CurrentDrive], Path, DOS_DIR_LENGTH);
202 }
203
204 /* Read CONFIG.SYS */
205 Stream = _wfopen(DOS_CONFIG_PATH, L"r");
206 if (Stream != NULL)
207 {
208 while (fgetws(Buffer, sizeof(Buffer)/sizeof(Buffer[0]), Stream))
209 {
210 // TODO: Parse the line
211 }
212 fclose(Stream);
213 }
214
215 #endif
216
217
218 /* Register the DOS 32-bit Interrupts */
219 // RegisterDosInt32(0x20, DosInt20h);
220
221 /* Initialize the DOS kernel */
222 return DosKRNLInitialize();
223 }
224
225 /* EOF */