1 /*
2  * COPYRIGHT:       GPL - See COPYING in the top level directory
3  * PROJECT:         ReactOS Virtual DOS Machine
4  * FILE:            subsystems/mvdm/ntvdm/dos/dos32krnl/dos.h
5  * PURPOSE:         DOS32 Kernel
6  * PROGRAMMERS:     Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
7  */
8 
9 #ifndef _DOS_H_
10 #define _DOS_H_
11 
12 /* INCLUDES *******************************************************************/
13 
14 #include "device.h"
15 
16 /**/ #include "int32.h" /**/
17 
18 /* DEFINES ********************************************************************/
19 
20 //
21 // We are DOS 5.00 (reported by INT 21h, AH=30h)
22 //    and DOS 5.50 (reported by INT 21h, AX=3306h) for Windows NT Compatibility
23 //
24 #define DOS_VERSION     MAKEWORD(5, 00)
25 #define NTDOS_VERSION   MAKEWORD(5, 50)
26 
27 #define DOS_CONFIG_PATH L"%SystemRoot%\\system32\\CONFIG.NT"
28 #define DOS_COMMAND_INTERPRETER L"%SystemRoot%\\system32\\COMMAND.COM /k %SystemRoot%\\system32\\AUTOEXEC.NT"
29 
30 #define BIOS_CODE_SEGMENT   0x70
31 #define BIOS_DATA_SEGMENT   0x70
32 #define DOS_CODE_SEGMENT    0x80
33 #define DOS_DATA_SEGMENT    0xA5
34 
35 #define DOS_DATA_OFFSET(x) FIELD_OFFSET(DOS_DATA, x)
36 
37 #define SYSTEM_ENV_BLOCK    0x600   // FIXME: Should be dynamically initialized!
38 
39 #define SYSTEM_PSP          0x0008
40 
41 #define INVALID_DOS_HANDLE  0xFFFF
42 #define DOS_INPUT_HANDLE    0
43 #define DOS_OUTPUT_HANDLE   1
44 #define DOS_ERROR_HANDLE    2
45 
46 #define DOS_SFT_SIZE        255    // Value of the 'FILES=' command; maximum 255
47 #define DOS_DIR_LENGTH      64
48 #define NUM_DRIVES          ('Z' - 'A' + 1)
49 #define DOS_CHAR_ATTRIBUTE  0x07
50 
51 #pragma pack(push, 1)
52 
53 typedef struct _DOS_FCB
54 {
55     BYTE DriveNumber;
56     CHAR FileName[8];
57     CHAR FileExt[3];
58     WORD BlockNumber;
59     WORD RecordSize;
60     DWORD FileSize;
61     WORD LastWriteDate;
62     WORD LastWriteTime;
63     BYTE Reserved[8];
64     BYTE BlockRecord;
65     BYTE RecordNumber[3];
66 } DOS_FCB, *PDOS_FCB;
67 
68 // http://www.ctyme.com/intr/rb-2983.htm
69 typedef struct _DOS_SYSVARS
70 {
71     DWORD OemHandler;
72     WORD Int21hReturn;
73     WORD ShareRetryCount;
74     WORD ShareRetryDelay;
75     DWORD DiskBuffer;
76     WORD UnreadConInput;
77     WORD FirstMcb;
78 
79     /* This is where the SYSVARS really start */
80     DWORD FirstDpb;                             // 0x00
81     DWORD FirstSft;                             // 0x04
82     DWORD ActiveClock;                          // 0x08
83     DWORD ActiveCon;                            // 0x0c
84     BYTE Reserved0[6];                          // 0x10
85     DWORD CurrentDirs;                          // 0x16
86     BYTE Reserved1[6];                          // 0x1a
87     BYTE NumBlockDevices;                       // 0x20
88     BYTE NumLocalDrives;                        // 0x21 - Set by LASTDRIVE
89     DOS_DRIVER NullDevice;                      // 0x22
90     BYTE Reserved2;                             // 0x34
91     WORD ProgramVersionTable;                   // 0x35
92     DWORD SetVerTable;                          // 0x37
93     WORD Reserved3[2];                          // 0x3b
94     WORD BuffersNumber;                         // 0x3f - 'x' parameter in "BUFFERS=x,y" command
95     WORD BuffersLookaheadNumber;                // 0x41 - 'y' parameter in "BUFFERS=x,y" command
96     BYTE BootDrive;                             // 0x43
97     BYTE UseDwordMoves;                         // 0x44
98     WORD ExtMemSize;                            // 0x45
99     BYTE Reserved4[0x1C];                       // 0x47
100     BYTE UmbLinked;                             // 0x63 - 0/1: UMB chain (un)linked to MCB chain
101     WORD Reserved5;                             // 0x64
102     WORD UmbChainStart;                         // 0x66 - Segment of the first UMB MCB
103     WORD MemAllocScanStart;                     // 0x68 - Segment where allocation scan starts
104 } DOS_SYSVARS, *PDOS_SYSVARS;
105 
106 typedef struct _DOS_CLOCK_TRANSFER_RECORD
107 {
108     WORD NumberOfDays;
109     BYTE Minutes;
110     BYTE Hours;
111     BYTE Hundredths;
112     BYTE Seconds;
113 } DOS_CLOCK_TRANSFER_RECORD, *PDOS_CLOCK_TRANSFER_RECORD;
114 
115 typedef struct _DOS_INPUT_BUFFER
116 {
117     BYTE MaxLength;
118     BYTE Length;
119     CHAR Buffer[ANYSIZE_ARRAY];
120 } DOS_INPUT_BUFFER, *PDOS_INPUT_BUFFER;
121 
122 typedef struct _DOS_FIND_FILE_BLOCK
123 {
124     CHAR DriveLetter;
125     CHAR Pattern[11];
126     UCHAR AttribMask;
127     DWORD Unused;
128     HANDLE SearchHandle;
129 
130     /* The following part of the structure is documented */
131     UCHAR Attributes;
132     WORD FileTime;
133     WORD FileDate;
134     DWORD FileSize;
135     CHAR FileName[13];
136 } DOS_FIND_FILE_BLOCK, *PDOS_FIND_FILE_BLOCK;
137 
138 // http://www.ctyme.com/intr/rb-3023.htm
139 typedef struct _DOS_SDA
140 {
141     BYTE PrinterEchoFlag;
142     CHAR CurrentSwitchChar;
143     BYTE AllocStrategy;
144     BYTE Unused0[28];
145 
146     /* This is where the SDA really starts */
147     BYTE ErrorMode;
148     BYTE InDos;
149     BYTE ErrorDrive;
150     BYTE LastErrorLocus;
151     WORD LastErrorCode;
152     BYTE LastErrorAction;
153     BYTE LastErrorClass;
154     DWORD LastErrorPointer;
155     DWORD DiskTransferArea;
156     WORD CurrentPsp;
157     WORD Int23StackPointer;
158     WORD ErrorLevel;
159     BYTE CurrentDrive;
160     BYTE ExtendedBreakFlag;
161 
162     /* This part is only valid while in DOS */
163     WORD LastAX;
164     WORD NetworkPsp;
165     WORD NetworkMachineNumber;
166     WORD FirstFreeMcb;
167     WORD BestFreeMcb;
168     WORD LastFreeMcb;
169     WORD MemorySize;
170     WORD LastSearchDirEntry;
171     BYTE Int24FailFlag;
172     BYTE DirectoryFlag;
173     BYTE CtrlBreakFlag;
174     BYTE AllowFcbBlanks;
175     BYTE Unused1;
176     BYTE DayOfMonth;
177     BYTE Month;
178     WORD Year;
179     WORD NumDays;
180     BYTE DayOfWeek;
181     BYTE ConsoleSwappedFlag;
182     BYTE Int28CallOk;
183     BYTE Int24AbortFlag;
184     DOS_RW_REQUEST Request;
185     DWORD DriverEntryPoint;
186     BYTE Unused2[44];
187     BYTE PspCopyType;
188     BYTE Unused3;
189     BYTE UserNumber[3];
190     BYTE OemNumber;
191     WORD ErrorCodeTable;
192     DOS_CLOCK_TRANSFER_RECORD ClockTransferRecord;
193     BYTE ByteBuffer;
194     BYTE Unused4;
195     CHAR FileNameBuffer[256];
196     BYTE Unused5[53];
197     CHAR CurrentDirectory[81];
198     CHAR FcbFilename[12];
199     CHAR FcbRenameDest[12];
200     BYTE Unused6[8];
201     BYTE ExtendedAttribute;
202     BYTE FcbType;
203     BYTE DirSearchAttributes;
204     BYTE FileOpenMode;
205     BYTE FileFound;
206     BYTE DeviceNameFound;
207     BYTE SpliceFlag;
208     BYTE DosCallFlag;
209     BYTE Unused7[5];
210     BYTE InsertMode;
211     BYTE ParsedFcbExists;
212     BYTE VolumeIDFlag;
213     BYTE TerminationType;
214     BYTE CreateFileFlag;
215     BYTE FileDeletedChar;
216     DWORD CriticalErrorDpb;
217     DWORD UserRegistersStack;
218     WORD Int24StackPointer;
219     BYTE Unused8[14];
220     DWORD DeviceHeader;
221     DWORD CurrentSft;
222     DWORD CurrentDirPointer;
223     DWORD CallerFcb;
224     WORD SftNumber;
225     WORD TempFileHandle;
226     DWORD JftEntry;
227     WORD FirstArgument;
228     WORD SecondArgument;
229     WORD LastComponent;
230     WORD TransferOffset;
231     BYTE Unused9[38];
232     DWORD WorkingSft;
233     WORD Int21CallerBX;
234     WORD Int21CallerDS;
235     WORD Unused10;
236     DWORD PrevCallFrame;
237 } DOS_SDA, *PDOS_SDA;
238 
239 /*
240  * DOS kernel data structure
241  */
242 typedef struct _DOS_DATA
243 {
244     DOS_SYSVARS SysVars;
245     BYTE NullDriverRoutine[7];
246     WORD DosVersion; // DOS version to report to programs (can be different from the true one)
247     DOS_SDA Sda;
248     CHAR CurrentDirectories[NUM_DRIVES][DOS_DIR_LENGTH];
249     BYTE UnreadConInputBuffer[128];
250     BYTE DosStack[384];
251     BYTE Sft[ANYSIZE_ARRAY];
252 } DOS_DATA, *PDOS_DATA;
253 
254 /*
255  * DOS BIOS data structure at segment 70h
256  */
257 typedef struct _BIOS_DATA
258 {
259     BYTE StartupCode[20];                       // 0x00 - 20 bytes: large enough for now!
260 
261 /*
262  * INT 13h (BIOS Disk Services) handler chain support.
263  *
264  * RomBiosInt13: The original INT 13h vector (normally from ROM BIOS).
265  * PrevInt13   : The previous INT 13h vector in the handler chain (initially
266  *               initialized with the RomBiosInt13 value; each time some
267  *               program calls INT 2Fh, AH=13h, PrevInt13 is updated).
268  *
269  * DOS hooks INT 13h with its own code, then (in normal circumstances) calls
270  * PrevInt13, so that when a program calls INT 13h, the DOS hook is first called,
271  * followed by the previous INT 13h (be it the original or some other hooked one).
272  * DOS may call PrevInt13 directly in some internal operations too.
273  * RomBiosInt13 is intended to be the original INT 13h vector that existed
274  * before DOS was loaded. A particular version of PC-AT's IBM's ROM BIOS
275  * (on systems with model byte FCh and BIOS date "01/10/84" only, see
276  * http://www.ctyme.com/intr/rb-4453.htm for more details) had a bug on disk
277  * reads so that it was patched by DOS, and therefore PrevInt13 was the fixed
278  * INT 13 interrupt (for the other cases, a direct call to RomBiosInt13 is done).
279  *
280  * NOTE: For compatibility with some programs (including virii), PrevInt13 should
281  * be at 0070:00B4, see for more details:
282  * http://repo.hackerzvoice.net/depot_madchat/vxdevl/vdat/tuvd0001.htm
283  * http://vxheaven.org/lib/vsm01.html
284  */
285     BYTE Padding0[0xB0 - /*FIELD_OFFSET(BIOS_DATA, StartupCode)*/ 20];
286     DWORD RomBiosInt13;                         // 0xb0
287     DWORD PrevInt13;                            // 0xb4
288     BYTE Padding1[0x100 - 0xB8];                // 0xb8
289 } BIOS_DATA, *PBIOS_DATA;
290 
291 C_ASSERT(sizeof(BIOS_DATA) == 0x100);
292 
293 #pragma pack(pop)
294 
295 /* VARIABLES ******************************************************************/
296 
297 extern PBIOS_DATA BiosData;
298 extern PDOS_DATA DosData;
299 extern PDOS_SYSVARS SysVars;
300 extern PDOS_SDA Sda;
301 
302 /* FUNCTIONS ******************************************************************/
303 
304 extern CALLBACK16 DosContext;
305 #define RegisterDosInt32(IntNumber, IntHandler)             \
306 do { \
307     ASSERT((0x20 <= IntNumber) && (IntNumber <= 0x2F));     \
308     RegisterInt32(DosContext.TrampolineFarPtr +             \
309                   DosContext.TrampolineSize   +             \
310                   (IntNumber - 0x20) * Int16To32StubSize,   \
311                   (IntNumber), (IntHandler), NULL);         \
312 } while(0);
313 
314 VOID ConDrvInitialize(VOID);
315 VOID ConDrvCleanup(VOID);
316 
317 /*
318  * DOS BIOS Functions
319  * See bios.c
320  */
321 CHAR DosReadCharacter(WORD FileHandle, BOOLEAN Echo);
322 BOOLEAN DosCheckInput(VOID);
323 VOID DosPrintCharacter(WORD FileHandle, CHAR Character);
324 
325 BOOLEAN DosBIOSInitialize(VOID);
326 
327 BOOLEAN DosControlBreak(VOID);
328 VOID DosEchoCharacter(CHAR Character);
329 
330 /*
331  * DOS Kernel Functions
332  * See dos.c
333  */
334 BOOLEAN DosKRNLInitialize(VOID);
335 
336 #endif // _DOS_H_
337 
338 /* EOF */
339