2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
5 * PURPOSE: VDM DOS Kernel
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
9 /* INCLUDES *******************************************************************/
18 #include "bios/bios.h"
20 /* PRIVATE VARIABLES **********************************************************/
22 // static BYTE CurrentDrive;
23 // static CHAR CurrentDirectories[NUM_DRIVES][DOS_DIR_LENGTH];
25 /* PRIVATE FUNCTIONS **********************************************************/
28 static WORD
DosWriteFile(WORD FileHandle
, LPVOID Buffer
, WORD Count
, LPWORD BytesWritten
)
30 WORD Result
= ERROR_SUCCESS
;
31 DWORD BytesWritten32
= 0;
32 HANDLE Handle
= DosGetRealHandle(FileHandle
);
35 DPRINT("DosWriteFile: FileHandle 0x%04X, Count 0x%04X\n",
39 /* Make sure the handle is valid */
40 if (Handle
== INVALID_HANDLE_VALUE
) return ERROR_INVALID_HANDLE
;
42 if (IsConsoleHandle(Handle
))
44 for (i
= 0; i
< Count
; i
++)
46 /* Call the BIOS to print the character */
47 VidBiosPrintCharacter(((LPBYTE
)Buffer
)[i
], DOS_CHAR_ATTRIBUTE
, Bda
->VideoPage
);
54 if (!WriteFile(Handle
, Buffer
, Count
, &BytesWritten32
, NULL
))
56 /* Store the error code */
57 Result
= (WORD
)GetLastError();
61 /* The number of bytes written is always 16-bit */
62 *BytesWritten
= LOWORD(BytesWritten32
);
64 /* Return the error code */
69 /* PUBLIC FUNCTIONS ***********************************************************/
71 CHAR
DosReadCharacter(VOID
)
73 CHAR Character
= '\0';
76 if (IsConsoleHandle(DosGetRealHandle(DOS_INPUT_HANDLE
)))
79 Character
= LOBYTE(BiosGetCharacter());
83 /* Use the file reading function */
84 DosReadFile(DOS_INPUT_HANDLE
, &Character
, sizeof(CHAR
), &BytesRead
);
90 BOOLEAN
DosCheckInput(VOID
)
92 HANDLE Handle
= DosGetRealHandle(DOS_INPUT_HANDLE
);
94 if (IsConsoleHandle(Handle
))
100 setAH(0x01); // or 0x11 for enhanced, but what to choose?
101 Int32Call(&DosContext
, BIOS_KBD_INTERRUPT
);
106 /* Return keyboard status */
107 return (getZF() == 0);
112 DWORD FileSize
= GetFileSize(Handle
, &FileSizeHigh
);
113 LONG LocationHigh
= 0;
114 DWORD Location
= SetFilePointer(Handle
, 0, &LocationHigh
, FILE_CURRENT
);
116 return ((Location
!= FileSize
) || (LocationHigh
!= FileSizeHigh
));
120 VOID
DosPrintCharacter(CHAR Character
)
124 /* Use the file writing function */
125 DosWriteFile(DOS_OUTPUT_HANDLE
, &Character
, sizeof(CHAR
), &BytesWritten
);
128 BOOLEAN
DosBIOSInitialize(VOID
)
130 PDOS_MCB Mcb
= SEGMENT_TO_MCB(FIRST_MCB_SEGMENT
);
132 LPWSTR SourcePtr
, Environment
;
135 LPSTR DestPtr
= (LPSTR
)SEG_OFF_TO_PTR(SYSTEM_ENV_BLOCK
, 0);
139 CHAR CurrentDirectory
[MAX_PATH
];
140 CHAR DosDirectory
[DOS_DIR_LENGTH
];
147 /* Initialize the MCB */
148 Mcb
->BlockType
= 'Z';
149 Mcb
->Size
= USER_MEMORY_SIZE
;
152 /* Initialize the link MCB to the UMB area */
153 Mcb
= SEGMENT_TO_MCB(FIRST_MCB_SEGMENT
+ USER_MEMORY_SIZE
+ 1);
154 Mcb
->BlockType
= 'M';
155 Mcb
->Size
= UMB_START_SEGMENT
- FIRST_MCB_SEGMENT
- USER_MEMORY_SIZE
- 2;
156 Mcb
->OwnerPsp
= SYSTEM_PSP
;
158 /* Initialize the UMB area */
159 Mcb
= SEGMENT_TO_MCB(UMB_START_SEGMENT
);
160 Mcb
->BlockType
= 'Z';
161 Mcb
->Size
= UMB_END_SEGMENT
- UMB_START_SEGMENT
;
164 /* Get the environment strings */
165 SourcePtr
= Environment
= GetEnvironmentStringsW();
166 if (Environment
== NULL
) return FALSE
;
168 /* Fill the DOS system environment block */
171 /* Get the size of the ASCII string */
172 AsciiSize
= WideCharToMultiByte(CP_ACP
,
181 /* Allocate memory for the ASCII string */
182 AsciiString
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, AsciiSize
);
183 if (AsciiString
== NULL
)
185 FreeEnvironmentStringsW(Environment
);
189 /* Convert to ASCII */
190 WideCharToMultiByte(CP_ACP
,
199 /* Copy the string into DOS memory */
200 strcpy(DestPtr
, AsciiString
);
202 /* Move to the next string */
203 SourcePtr
+= wcslen(SourcePtr
) + 1;
204 DestPtr
+= strlen(AsciiString
);
207 /* Free the memory */
208 HeapFree(GetProcessHeap(), 0, AsciiString
);
212 /* Free the memory allocated for environment strings */
213 FreeEnvironmentStringsW(Environment
);
218 /* Clear the current directory buffer */
219 ZeroMemory(CurrentDirectories
, sizeof(CurrentDirectories
));
221 /* Get the current directory */
222 if (!GetCurrentDirectoryA(MAX_PATH
, CurrentDirectory
))
224 // TODO: Use some kind of default path?
228 /* Convert that to a DOS path */
229 if (!GetShortPathNameA(CurrentDirectory
, DosDirectory
, DOS_DIR_LENGTH
))
231 // TODO: Use some kind of default path?
236 CurrentDrive
= DosDirectory
[0] - 'A';
238 /* Get the directory part of the path */
239 Path
= strchr(DosDirectory
, '\\');
242 /* Skip the backslash */
246 /* Set the directory */
249 strncpy(CurrentDirectories
[CurrentDrive
], Path
, DOS_DIR_LENGTH
);
252 /* Read CONFIG.SYS */
253 Stream
= _wfopen(DOS_CONFIG_PATH
, L
"r");
256 while (fgetws(Buffer
, sizeof(Buffer
)/sizeof(Buffer
[0]), Stream
))
258 // TODO: Parse the line
266 /* Register the DOS 32-bit Interrupts */
267 // RegisterDosInt32(0x20, DosInt20h);
269 /* Initialize the DOS kernel */
270 return DosKRNLInitialize();