2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
4 * FILE: dos/dos32krnl/dos.c
5 * PURPOSE: DOS32 Kernel
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
7 * Hermes Belusca-Maito (hermes.belusca@sfr.fr)
10 /* INCLUDES *******************************************************************/
23 #include "bios/bios.h"
26 #include "hardware/ps2.h"
28 /* PRIVATE VARIABLES **********************************************************/
30 CALLBACK16 DosContext
;
32 static DWORD DiskTransferArea
;
33 /*static*/ BYTE CurrentDrive
;
34 static CHAR LastDrive
= 'E';
35 static CHAR CurrentDirectories
[NUM_DRIVES
][DOS_DIR_LENGTH
];
36 static DOS_SFT_ENTRY DosSystemFileTable
[DOS_SFT_SIZE
];
37 static WORD DosErrorLevel
= 0x0000;
39 /* PUBLIC VARIABLES ***********************************************************/
41 /* Echo state for INT 21h, AH = 01h and AH = 3Fh */
42 BOOLEAN DoEcho
= FALSE
;
43 WORD CurrentPsp
= SYSTEM_PSP
;
44 WORD DosLastError
= 0;
46 /* PRIVATE FUNCTIONS **********************************************************/
48 static WORD
DosCopyEnvironmentBlock(LPCSTR Environment OPTIONAL
,
51 PCHAR Ptr
, DestBuffer
= NULL
;
55 /* If we have an environment strings list, compute its size */
58 /* Calculate the size of the environment block */
59 Ptr
= (PCHAR
)Environment
;
60 while (*Ptr
) Ptr
+= strlen(Ptr
) + 1;
61 TotalSize
= (ULONG_PTR
)Ptr
- (ULONG_PTR
)Environment
;
65 /* Empty environment string */
68 /* Add the final environment block NULL-terminator */
71 /* Add the two bytes for the program name tag */
74 /* Add the string buffer size */
75 TotalSize
+= strlen(ProgramName
) + 1;
77 /* Allocate the memory for the environment block */
78 DestSegment
= DosAllocateMemory((WORD
)((TotalSize
+ 0x0F) >> 4), NULL
);
79 if (!DestSegment
) return 0;
81 DestBuffer
= (PCHAR
)SEG_OFF_TO_PTR(DestSegment
, 0);
83 /* If we have an environment strings list, copy it */
86 Ptr
= (PCHAR
)Environment
;
89 /* Copy the string and NULL-terminate it */
90 strcpy(DestBuffer
, Ptr
);
91 DestBuffer
+= strlen(Ptr
);
92 *(DestBuffer
++) = '\0';
94 /* Move to the next string */
95 Ptr
+= strlen(Ptr
) + 1;
100 /* Empty environment string */
101 *(DestBuffer
++) = '\0';
103 /* NULL-terminate the environment block */
104 *(DestBuffer
++) = '\0';
106 /* Store the special program name tag */
107 *(DestBuffer
++) = LOBYTE(DOS_PROGRAM_NAME_TAG
);
108 *(DestBuffer
++) = HIBYTE(DOS_PROGRAM_NAME_TAG
);
110 /* Copy the program name after the environment block */
111 strcpy(DestBuffer
, ProgramName
);
116 /* Taken from base/shell/cmd/console.c */
117 static BOOL
IsConsoleHandle(HANDLE hHandle
)
121 /* Check whether the handle may be that of a console... */
122 if ((GetFileType(hHandle
) & FILE_TYPE_CHAR
) == 0) return FALSE
;
125 * It may be. Perform another test... The idea comes from the
126 * MSDN description of the WriteConsole API:
128 * "WriteConsole fails if it is used with a standard handle
129 * that is redirected to a file. If an application processes
130 * multilingual output that can be redirected, determine whether
131 * the output handle is a console handle (one method is to call
132 * the GetConsoleMode function and check whether it succeeds).
133 * If the handle is a console handle, call WriteConsole. If the
134 * handle is not a console handle, the output is redirected and
135 * you should call WriteFile to perform the I/O."
137 return GetConsoleMode(hHandle
, &dwMode
);
140 static inline PDOS_SFT_ENTRY
DosFindFreeSftEntry(VOID
)
144 for (i
= 0; i
< DOS_SFT_SIZE
; i
++)
146 if (DosSystemFileTable
[i
].Type
== DOS_SFT_ENTRY_NONE
)
148 return &DosSystemFileTable
[i
];
155 static inline PDOS_SFT_ENTRY
DosFindWin32SftEntry(HANDLE Handle
)
159 for (i
= 0; i
< DOS_SFT_SIZE
; i
++)
161 if (DosSystemFileTable
[i
].Type
== DOS_SFT_ENTRY_WIN32
162 && DosSystemFileTable
[i
].Handle
== Handle
)
164 return &DosSystemFileTable
[i
];
171 static inline PDOS_SFT_ENTRY
DosFindDeviceSftEntry(PDOS_DEVICE_NODE Device
)
175 for (i
= 0; i
< DOS_SFT_SIZE
; i
++)
177 if (DosSystemFileTable
[i
].Type
== DOS_SFT_ENTRY_DEVICE
178 && DosSystemFileTable
[i
].DeviceNode
== Device
)
180 return &DosSystemFileTable
[i
];
187 WORD
DosOpenHandle(HANDLE Handle
)
192 PDOS_SFT_ENTRY SftEntry
;
194 /* The system PSP has no handle table */
195 if (CurrentPsp
== SYSTEM_PSP
) return INVALID_DOS_HANDLE
;
197 /* Get a pointer to the handle table */
198 PspBlock
= SEGMENT_TO_PSP(CurrentPsp
);
199 HandleTable
= (LPBYTE
)FAR_POINTER(PspBlock
->HandleTablePtr
);
201 /* Find a free entry in the JFT */
202 for (DosHandle
= 0; DosHandle
< PspBlock
->HandleTableSize
; DosHandle
++)
204 if (HandleTable
[DosHandle
] == 0xFF) break;
207 /* If there are no free entries, fail */
208 if (DosHandle
== PspBlock
->HandleTableSize
) return INVALID_DOS_HANDLE
;
210 /* Check if the handle is already in the SFT */
211 SftEntry
= DosFindWin32SftEntry(Handle
);
212 if (SftEntry
!= NULL
)
214 /* Already in the table, reference it */
215 SftEntry
->RefCount
++;
219 /* Find a free SFT entry to use */
220 SftEntry
= DosFindFreeSftEntry();
221 if (SftEntry
== NULL
)
223 /* The SFT is full */
224 return INVALID_DOS_HANDLE
;
227 /* Initialize the empty table entry */
228 SftEntry
->Type
= DOS_SFT_ENTRY_WIN32
;
229 SftEntry
->Handle
= Handle
;
230 SftEntry
->RefCount
= 1;
234 /* Set the JFT entry to that SFT index */
235 HandleTable
[DosHandle
] = ARRAY_INDEX(SftEntry
, DosSystemFileTable
);
237 /* Return the new handle */
241 WORD
DosOpenDevice(PDOS_DEVICE_NODE Device
)
246 PDOS_SFT_ENTRY SftEntry
;
248 DPRINT("DosOpenDevice(\"%Z\")\n", &Device
->Name
);
250 /* The system PSP has no handle table */
251 if (CurrentPsp
== SYSTEM_PSP
) return INVALID_DOS_HANDLE
;
253 /* Get a pointer to the handle table */
254 PspBlock
= SEGMENT_TO_PSP(CurrentPsp
);
255 HandleTable
= (LPBYTE
)FAR_POINTER(PspBlock
->HandleTablePtr
);
257 /* Find a free entry in the JFT */
258 for (DosHandle
= 0; DosHandle
< PspBlock
->HandleTableSize
; DosHandle
++)
260 if (HandleTable
[DosHandle
] == 0xFF) break;
263 /* If there are no free entries, fail */
264 if (DosHandle
== PspBlock
->HandleTableSize
) return INVALID_DOS_HANDLE
;
266 /* Check if the device is already in the SFT */
267 SftEntry
= DosFindDeviceSftEntry(Device
);
268 if (SftEntry
!= NULL
)
270 /* Already in the table, reference it */
271 SftEntry
->RefCount
++;
275 /* Find a free SFT entry to use */
276 SftEntry
= DosFindFreeSftEntry();
277 if (SftEntry
== NULL
)
279 /* The SFT is full */
280 return INVALID_DOS_HANDLE
;
283 /* Initialize the empty table entry */
284 SftEntry
->Type
= DOS_SFT_ENTRY_DEVICE
;
285 SftEntry
->DeviceNode
= Device
;
286 SftEntry
->RefCount
= 1;
290 /* Call the open routine, if it exists */
291 if (Device
->OpenRoutine
) Device
->OpenRoutine(Device
);
293 /* Set the JFT entry to that SFT index */
294 HandleTable
[DosHandle
] = ARRAY_INDEX(SftEntry
, DosSystemFileTable
);
296 /* Return the new handle */
300 static VOID
DosCopyHandleTable(LPBYTE DestinationTable
)
306 /* Clear the table first */
307 for (i
= 0; i
< 20; i
++) DestinationTable
[i
] = 0xFF;
309 /* Check if this is the initial process */
310 if (CurrentPsp
== SYSTEM_PSP
)
312 PDOS_SFT_ENTRY SftEntry
;
313 HANDLE StandardHandles
[3];
314 PDOS_DEVICE_NODE Con
= DosGetDevice("CON");
317 /* Get the native standard handles */
318 StandardHandles
[0] = GetStdHandle(STD_INPUT_HANDLE
);
319 StandardHandles
[1] = GetStdHandle(STD_OUTPUT_HANDLE
);
320 StandardHandles
[2] = GetStdHandle(STD_ERROR_HANDLE
);
322 for (i
= 0; i
< 3; i
++)
324 /* Find the corresponding SFT entry */
325 if (IsConsoleHandle(StandardHandles
[i
]))
327 SftEntry
= DosFindDeviceSftEntry(Con
);
331 SftEntry
= DosFindWin32SftEntry(StandardHandles
[i
]);
334 if (SftEntry
== NULL
)
336 /* Create a new SFT entry for it */
337 SftEntry
= DosFindFreeSftEntry();
338 if (SftEntry
== NULL
)
340 DPRINT1("Cannot create standard handle %d, the SFT is full!\n", i
);
344 SftEntry
->RefCount
= 0;
346 if (IsConsoleHandle(StandardHandles
[i
]))
348 SftEntry
->Type
= DOS_SFT_ENTRY_DEVICE
;
349 SftEntry
->DeviceNode
= Con
;
351 /* Call the open routine */
352 if (Con
->OpenRoutine
) Con
->OpenRoutine(Con
);
356 SftEntry
->Type
= DOS_SFT_ENTRY_WIN32
;
357 SftEntry
->Handle
= StandardHandles
[i
];
361 SftEntry
->RefCount
++;
362 DestinationTable
[i
] = ARRAY_INDEX(SftEntry
, DosSystemFileTable
);
367 /* Get the parent PSP block and handle table */
368 PspBlock
= SEGMENT_TO_PSP(CurrentPsp
);
369 SourceTable
= (LPBYTE
)FAR_POINTER(PspBlock
->HandleTablePtr
);
371 /* Copy the first 20 handles into the new table */
372 for (i
= 0; i
< DEFAULT_JFT_SIZE
; i
++)
374 DestinationTable
[i
] = SourceTable
[i
];
376 /* Increase the reference count */
377 DosSystemFileTable
[SourceTable
[i
]].RefCount
++;
382 static BOOLEAN
DosResizeHandleTable(WORD NewSize
)
388 /* Get the PSP block */
389 PspBlock
= SEGMENT_TO_PSP(CurrentPsp
);
391 if (NewSize
== PspBlock
->HandleTableSize
)
397 if (PspBlock
->HandleTableSize
> DEFAULT_JFT_SIZE
)
399 /* Get the segment of the current table */
400 Segment
= (LOWORD(PspBlock
->HandleTablePtr
) >> 4) + HIWORD(PspBlock
->HandleTablePtr
);
402 if (NewSize
<= DEFAULT_JFT_SIZE
)
404 /* Get the current handle table */
405 HandleTable
= FAR_POINTER(PspBlock
->HandleTablePtr
);
407 /* Copy it to the PSP */
408 RtlCopyMemory(PspBlock
->HandleTable
, HandleTable
, NewSize
);
410 /* Free the memory */
411 DosFreeMemory(Segment
);
413 /* Update the handle table pointer and size */
414 PspBlock
->HandleTableSize
= NewSize
;
415 PspBlock
->HandleTablePtr
= MAKELONG(0x18, CurrentPsp
);
419 /* Resize the memory */
420 if (!DosResizeMemory(Segment
, NewSize
, NULL
))
422 /* Unable to resize, try allocating it somewhere else */
423 Segment
= DosAllocateMemory(NewSize
, NULL
);
424 if (Segment
== 0) return FALSE
;
426 /* Get the new handle table */
427 HandleTable
= SEG_OFF_TO_PTR(Segment
, 0);
429 /* Copy the handles to the new table */
430 RtlCopyMemory(HandleTable
,
431 FAR_POINTER(PspBlock
->HandleTablePtr
),
432 PspBlock
->HandleTableSize
);
434 /* Update the handle table pointer */
435 PspBlock
->HandleTablePtr
= MAKELONG(0, Segment
);
438 /* Update the handle table size */
439 PspBlock
->HandleTableSize
= NewSize
;
442 else if (NewSize
> DEFAULT_JFT_SIZE
)
444 Segment
= DosAllocateMemory(NewSize
, NULL
);
445 if (Segment
== 0) return FALSE
;
447 /* Get the new handle table */
448 HandleTable
= SEG_OFF_TO_PTR(Segment
, 0);
450 /* Copy the handles from the PSP to the new table */
451 RtlCopyMemory(HandleTable
,
452 FAR_POINTER(PspBlock
->HandleTablePtr
),
453 PspBlock
->HandleTableSize
);
455 /* Update the handle table pointer and size */
456 PspBlock
->HandleTableSize
= NewSize
;
457 PspBlock
->HandleTablePtr
= MAKELONG(0, Segment
);
463 static BOOLEAN
DosCloseHandle(WORD DosHandle
)
467 PDOS_SFT_ENTRY SftEntry
;
469 DPRINT("DosCloseHandle: DosHandle 0x%04X\n", DosHandle
);
471 /* The system PSP has no handle table */
472 if (CurrentPsp
== SYSTEM_PSP
) return FALSE
;
474 /* Get a pointer to the handle table */
475 PspBlock
= SEGMENT_TO_PSP(CurrentPsp
);
476 HandleTable
= (LPBYTE
)FAR_POINTER(PspBlock
->HandleTablePtr
);
478 /* Make sure the handle is open */
479 if (HandleTable
[DosHandle
] == 0xFF) return FALSE
;
481 /* Make sure the SFT entry is valid */
482 SftEntry
= &DosSystemFileTable
[HandleTable
[DosHandle
]];
483 if (SftEntry
->Type
== DOS_SFT_ENTRY_NONE
) return FALSE
;
485 /* Decrement the reference count of the SFT entry */
486 SftEntry
->RefCount
--;
488 /* Check if the reference count fell to zero */
489 if (!SftEntry
->RefCount
)
491 switch (SftEntry
->Type
)
493 case DOS_SFT_ENTRY_WIN32
:
495 /* Close the win32 handle and clear it */
496 CloseHandle(SftEntry
->Handle
);
501 case DOS_SFT_ENTRY_DEVICE
:
503 PDOS_DEVICE_NODE Node
= SftEntry
->DeviceNode
;
505 /* Call the close routine, if it exists */
506 if (Node
->CloseRoutine
) SftEntry
->DeviceNode
->CloseRoutine(SftEntry
->DeviceNode
);
513 /* Shouldn't happen */
518 /* Invalidate the SFT entry */
519 SftEntry
->Type
= DOS_SFT_ENTRY_NONE
;
522 /* Clear the entry in the JFT */
523 HandleTable
[DosHandle
] = 0xFF;
528 static BOOLEAN
DosDuplicateHandle(WORD OldHandle
, WORD NewHandle
)
534 DPRINT("DosDuplicateHandle: OldHandle 0x%04X, NewHandle 0x%04X\n",
538 /* The system PSP has no handle table */
539 if (CurrentPsp
== SYSTEM_PSP
) return FALSE
;
541 /* Get a pointer to the handle table */
542 PspBlock
= SEGMENT_TO_PSP(CurrentPsp
);
543 HandleTable
= (LPBYTE
)FAR_POINTER(PspBlock
->HandleTablePtr
);
545 /* Make sure the old handle is open */
546 if (HandleTable
[OldHandle
] == 0xFF) return FALSE
;
548 /* Check if the new handle is open */
549 if (HandleTable
[NewHandle
] != 0xFF)
552 DosCloseHandle(NewHandle
);
555 /* Increment the reference count of the SFT entry */
556 SftIndex
= HandleTable
[OldHandle
];
557 DosSystemFileTable
[SftIndex
].RefCount
++;
559 /* Make the new handle point to that SFT entry */
560 HandleTable
[NewHandle
] = SftIndex
;
566 static BOOLEAN
DosChangeDrive(BYTE Drive
)
568 WCHAR DirectoryPath
[DOS_CMDLINE_LENGTH
];
570 /* Make sure the drive exists */
571 if (Drive
> (LastDrive
- 'A')) return FALSE
;
573 /* Find the path to the new current directory */
574 swprintf(DirectoryPath
, L
"%c\\%S", Drive
+ 'A', CurrentDirectories
[Drive
]);
576 /* Change the current directory of the process */
577 if (!SetCurrentDirectory(DirectoryPath
)) return FALSE
;
579 /* Set the current drive */
580 CurrentDrive
= Drive
;
586 static BOOLEAN
DosChangeDirectory(LPSTR Directory
)
592 /* Make sure the directory path is not too long */
593 if (strlen(Directory
) >= DOS_DIR_LENGTH
)
595 DosLastError
= ERROR_PATH_NOT_FOUND
;
599 /* Get the drive number */
600 DriveNumber
= Directory
[0] - 'A';
602 /* Make sure the drive exists */
603 if (DriveNumber
> (LastDrive
- 'A'))
605 DosLastError
= ERROR_PATH_NOT_FOUND
;
609 /* Get the file attributes */
610 Attributes
= GetFileAttributesA(Directory
);
612 /* Make sure the path exists and is a directory */
613 if ((Attributes
== INVALID_FILE_ATTRIBUTES
)
614 || !(Attributes
& FILE_ATTRIBUTE_DIRECTORY
))
616 DosLastError
= ERROR_PATH_NOT_FOUND
;
620 /* Check if this is the current drive */
621 if (DriveNumber
== CurrentDrive
)
623 /* Change the directory */
624 if (!SetCurrentDirectoryA(Directory
))
626 DosLastError
= LOWORD(GetLastError());
631 /* Get the directory part of the path */
632 Path
= strchr(Directory
, '\\');
635 /* Skip the backslash */
639 /* Set the directory for the drive */
642 strncpy(CurrentDirectories
[DriveNumber
], Path
, DOS_DIR_LENGTH
);
646 CurrentDirectories
[DriveNumber
][0] = '\0';
653 /* PUBLIC FUNCTIONS ***********************************************************/
655 PDOS_SFT_ENTRY
DosGetSftEntry(WORD DosHandle
)
660 /* The system PSP has no handle table */
661 if (CurrentPsp
== SYSTEM_PSP
) return NULL
;
663 /* Get a pointer to the handle table */
664 PspBlock
= SEGMENT_TO_PSP(CurrentPsp
);
665 HandleTable
= (LPBYTE
)FAR_POINTER(PspBlock
->HandleTablePtr
);
667 /* Make sure the handle is open */
668 if (HandleTable
[DosHandle
] == 0xFF) return NULL
;
670 /* Return a pointer to the SFT entry */
671 return &DosSystemFileTable
[HandleTable
[DosHandle
]];
674 VOID
DosInitializePsp(WORD PspSegment
, LPCSTR CommandLine
, WORD ProgramSize
, WORD Environment
)
676 PDOS_PSP PspBlock
= SEGMENT_TO_PSP(PspSegment
);
677 LPDWORD IntVecTable
= (LPDWORD
)((ULONG_PTR
)BaseAddress
);
679 RtlZeroMemory(PspBlock
, sizeof(*PspBlock
));
681 /* Set the exit interrupt */
682 PspBlock
->Exit
[0] = 0xCD; // int 0x20
683 PspBlock
->Exit
[1] = 0x20;
685 /* Set the number of the last paragraph */
686 PspBlock
->LastParagraph
= PspSegment
+ ProgramSize
- 1;
688 /* Save the interrupt vectors */
689 PspBlock
->TerminateAddress
= IntVecTable
[0x22];
690 PspBlock
->BreakAddress
= IntVecTable
[0x23];
691 PspBlock
->CriticalAddress
= IntVecTable
[0x24];
693 /* Set the parent PSP */
694 PspBlock
->ParentPsp
= CurrentPsp
;
696 /* Copy the parent handle table */
697 DosCopyHandleTable(PspBlock
->HandleTable
);
699 /* Set the environment block */
700 PspBlock
->EnvBlock
= Environment
;
702 /* Set the handle table pointers to the internal handle table */
703 PspBlock
->HandleTableSize
= 20;
704 PspBlock
->HandleTablePtr
= MAKELONG(0x18, PspSegment
);
706 /* Set the DOS version */
707 PspBlock
->DosVersion
= DOS_VERSION
;
709 /* Set the far call opcodes */
710 PspBlock
->FarCall
[0] = 0xCD; // int 0x21
711 PspBlock
->FarCall
[1] = 0x21;
712 PspBlock
->FarCall
[2] = 0xCB; // retf
714 /* Set the command line */
715 PspBlock
->CommandLineSize
= (BYTE
)min(strlen(CommandLine
), DOS_CMDLINE_LENGTH
- 1);
716 RtlCopyMemory(PspBlock
->CommandLine
, CommandLine
, PspBlock
->CommandLineSize
);
717 PspBlock
->CommandLine
[PspBlock
->CommandLineSize
] = '\r';
720 DWORD
DosLoadExecutable(IN DOS_EXEC_TYPE LoadType
,
721 IN LPCSTR ExecutablePath
,
722 IN LPCSTR CommandLine
,
723 IN LPCSTR Environment OPTIONAL
,
724 OUT PDWORD StackLocation OPTIONAL
,
725 OUT PDWORD EntryPoint OPTIONAL
)
727 DWORD Result
= ERROR_SUCCESS
;
728 HANDLE FileHandle
= INVALID_HANDLE_VALUE
, FileMapping
= NULL
;
729 LPBYTE Address
= NULL
;
733 DWORD i
, FileSize
, ExeSize
;
734 PIMAGE_DOS_HEADER Header
;
735 PDWORD RelocationTable
;
737 LPSTR CmdLinePtr
= (LPSTR
)CommandLine
;
739 DPRINT1("DosLoadExecutable(%d, %s, %s, %s, 0x%08X, 0x%08X)\n",
743 Environment
? Environment
: "n/a",
747 if (LoadType
== DOS_LOAD_OVERLAY
)
749 DPRINT1("Overlay loading is not supported yet.\n");
750 return ERROR_NOT_SUPPORTED
;
753 /* NULL-terminate the command line by removing the return carriage character */
754 while (*CmdLinePtr
&& *CmdLinePtr
!= '\r') CmdLinePtr
++;
757 /* Open a handle to the executable */
758 FileHandle
= CreateFileA(ExecutablePath
,
763 FILE_ATTRIBUTE_NORMAL
,
765 if (FileHandle
== INVALID_HANDLE_VALUE
)
767 Result
= GetLastError();
771 /* Get the file size */
772 FileSize
= GetFileSize(FileHandle
, NULL
);
774 /* Create a mapping object for the file */
775 FileMapping
= CreateFileMapping(FileHandle
,
781 if (FileMapping
== NULL
)
783 Result
= GetLastError();
787 /* Map the file into memory */
788 Address
= (LPBYTE
)MapViewOfFile(FileMapping
, FILE_MAP_READ
, 0, 0, 0);
791 Result
= GetLastError();
795 /* Copy the environment block to DOS memory */
796 EnvBlock
= DosCopyEnvironmentBlock(Environment
, ExecutablePath
);
799 Result
= ERROR_NOT_ENOUGH_MEMORY
;
803 /* Check if this is an EXE file or a COM file */
804 if (Address
[0] == 'M' && Address
[1] == 'Z')
808 /* Get the MZ header */
809 Header
= (PIMAGE_DOS_HEADER
)Address
;
811 /* Get the base size of the file, in paragraphs (rounded up) */
812 ExeSize
= (((Header
->e_cp
- 1) * 512) + Header
->e_cblp
+ 0x0F) >> 4;
814 /* Add the PSP size, in paragraphs */
815 ExeSize
+= sizeof(DOS_PSP
) >> 4;
817 /* Add the maximum size that should be allocated */
818 ExeSize
+= Header
->e_maxalloc
;
820 /* Make sure it does not pass 0xFFFF */
821 if (ExeSize
> 0xFFFF) ExeSize
= 0xFFFF;
823 /* Try to allocate that much memory */
824 Segment
= DosAllocateMemory((WORD
)ExeSize
, &MaxAllocSize
);
828 /* Check if there's at least enough memory for the minimum size */
829 if (MaxAllocSize
< (ExeSize
- Header
->e_maxalloc
+ Header
->e_minalloc
))
831 Result
= DosLastError
;
835 /* Allocate that minimum amount */
836 ExeSize
= MaxAllocSize
;
837 Segment
= DosAllocateMemory((WORD
)ExeSize
, NULL
);
838 ASSERT(Segment
!= 0);
841 /* Initialize the PSP */
842 DosInitializePsp(Segment
,
847 /* The process owns its own memory */
848 DosChangeMemoryOwner(Segment
, Segment
);
849 DosChangeMemoryOwner(EnvBlock
, Segment
);
851 /* Copy the program to Segment:0100 */
852 RtlCopyMemory(SEG_OFF_TO_PTR(Segment
, 0x100),
853 Address
+ (Header
->e_cparhdr
<< 4),
854 min(FileSize
- (Header
->e_cparhdr
<< 4),
855 (ExeSize
<< 4) - sizeof(DOS_PSP
)));
857 /* Get the relocation table */
858 RelocationTable
= (PDWORD
)(Address
+ Header
->e_lfarlc
);
860 /* Perform relocations */
861 for (i
= 0; i
< Header
->e_crlc
; i
++)
863 /* Get a pointer to the word that needs to be patched */
864 RelocWord
= (PWORD
)SEG_OFF_TO_PTR(Segment
+ HIWORD(RelocationTable
[i
]),
865 0x100 + LOWORD(RelocationTable
[i
]));
867 /* Add the number of the EXE segment to it */
868 *RelocWord
+= Segment
+ (sizeof(DOS_PSP
) >> 4);
871 if (LoadType
== DOS_LOAD_AND_EXECUTE
)
873 /* Set the initial segment registers */
877 /* Set the stack to the location from the header */
878 setSS(Segment
+ (sizeof(DOS_PSP
) >> 4) + Header
->e_ss
);
882 CurrentPsp
= Segment
;
883 DiskTransferArea
= MAKELONG(0x80, Segment
);
884 CpuExecute(Segment
+ Header
->e_cs
+ (sizeof(DOS_PSP
) >> 4),
892 /* Find the maximum amount of memory that can be allocated */
893 DosAllocateMemory(0xFFFF, &MaxAllocSize
);
895 /* Make sure it's enough for the whole program and the PSP */
896 if (((DWORD
)MaxAllocSize
<< 4) < (FileSize
+ sizeof(DOS_PSP
)))
898 Result
= ERROR_NOT_ENOUGH_MEMORY
;
902 /* Allocate all of it */
903 Segment
= DosAllocateMemory(MaxAllocSize
, NULL
);
906 Result
= DosLastError
;
910 /* The process owns its own memory */
911 DosChangeMemoryOwner(Segment
, Segment
);
912 DosChangeMemoryOwner(EnvBlock
, Segment
);
914 /* Copy the program to Segment:0100 */
915 RtlCopyMemory(SEG_OFF_TO_PTR(Segment
, 0x100),
919 /* Initialize the PSP */
920 DosInitializePsp(Segment
,
925 if (LoadType
== DOS_LOAD_AND_EXECUTE
)
927 /* Set the initial segment registers */
931 /* Set the stack to the last word of the segment */
936 * Set the value on the stack to 0, so that a near return
937 * jumps to PSP:0000 which has the exit code.
939 *((LPWORD
)SEG_OFF_TO_PTR(Segment
, 0xFFFE)) = 0;
942 CurrentPsp
= Segment
;
943 DiskTransferArea
= MAKELONG(0x80, Segment
);
944 CpuExecute(Segment
, 0x100);
949 if (Result
!= ERROR_SUCCESS
)
951 /* It was not successful, cleanup the DOS memory */
952 if (EnvBlock
) DosFreeMemory(EnvBlock
);
953 if (Segment
) DosFreeMemory(Segment
);
957 if (Address
!= NULL
) UnmapViewOfFile(Address
);
959 /* Close the file mapping object */
960 if (FileMapping
!= NULL
) CloseHandle(FileMapping
);
962 /* Close the file handle */
963 if (FileHandle
!= INVALID_HANDLE_VALUE
) CloseHandle(FileHandle
);
968 DWORD
DosStartProcess(IN LPCSTR ExecutablePath
,
969 IN LPCSTR CommandLine
,
970 IN LPCSTR Environment OPTIONAL
)
974 Result
= DosLoadExecutable(DOS_LOAD_AND_EXECUTE
,
981 if (Result
!= ERROR_SUCCESS
) goto Quit
;
983 /* Attach to the console */
984 VidBiosAttachToConsole(); // FIXME: And in fact, attach the full NTVDM UI to the console
986 // HACK: Simulate a ENTER key release scancode on the PS/2 port because
987 // some apps expect to read a key release scancode (> 0x80) when they
989 IOWriteB(PS2_CONTROL_PORT
, 0xD2); // Next write is for the first PS/2 port
990 IOWriteB(PS2_DATA_PORT
, 0x80 | 0x1C); // ENTER key release
992 /* Start simulation */
993 SetEvent(VdmTaskEvent
);
996 /* Detach from the console */
997 VidBiosDetachFromConsole(); // FIXME: And in fact, detach the full NTVDM UI from the console
1004 WORD
DosCreateProcess(DOS_EXEC_TYPE LoadType
,
1006 PDOS_EXEC_PARAM_BLOCK Parameters
)
1010 LPVOID Environment
= NULL
;
1011 VDM_COMMAND_INFO CommandInfo
;
1012 CHAR CmdLine
[MAX_PATH
];
1013 CHAR AppName
[MAX_PATH
];
1014 CHAR PifFile
[MAX_PATH
];
1015 CHAR Desktop
[MAX_PATH
];
1016 CHAR Title
[MAX_PATH
];
1018 STARTUPINFOA StartupInfo
;
1019 PROCESS_INFORMATION ProcessInfo
;
1021 /* Get the binary type */
1022 if (!GetBinaryTypeA(ProgramName
, &BinaryType
)) return GetLastError();
1024 /* Did the caller specify an environment segment? */
1025 if (Parameters
->Environment
)
1027 /* Yes, use it instead of the parent one */
1028 Environment
= SEG_OFF_TO_PTR(Parameters
->Environment
, 0);
1031 /* Set up the startup info structure */
1032 RtlZeroMemory(&StartupInfo
, sizeof(StartupInfo
));
1033 StartupInfo
.cb
= sizeof(StartupInfo
);
1035 /* Create the process */
1036 if (!CreateProcessA(ProgramName
,
1037 FAR_POINTER(Parameters
->CommandLine
),
1047 return GetLastError();
1050 /* Check the type of the program */
1053 /* These are handled by NTVDM */
1054 case SCS_DOS_BINARY
:
1055 case SCS_WOW_BINARY
:
1057 /* Clear the structure */
1058 RtlZeroMemory(&CommandInfo
, sizeof(CommandInfo
));
1060 /* Initialize the structure members */
1061 CommandInfo
.TaskId
= SessionId
;
1062 CommandInfo
.VDMState
= VDM_FLAG_NESTED_TASK
| VDM_FLAG_DONT_WAIT
;
1063 CommandInfo
.CmdLine
= CmdLine
;
1064 CommandInfo
.CmdLen
= sizeof(CmdLine
);
1065 CommandInfo
.AppName
= AppName
;
1066 CommandInfo
.AppLen
= sizeof(AppName
);
1067 CommandInfo
.PifFile
= PifFile
;
1068 CommandInfo
.PifLen
= sizeof(PifFile
);
1069 CommandInfo
.Desktop
= Desktop
;
1070 CommandInfo
.DesktopLen
= sizeof(Desktop
);
1071 CommandInfo
.Title
= Title
;
1072 CommandInfo
.TitleLen
= sizeof(Title
);
1073 CommandInfo
.Env
= Env
;
1074 CommandInfo
.EnvLen
= sizeof(Env
);
1076 /* Get the VDM command information */
1077 if (!GetNextVDMCommand(&CommandInfo
))
1079 /* Shouldn't happen */
1083 /* Increment the re-entry count */
1084 CommandInfo
.VDMState
= VDM_INC_REENTER_COUNT
;
1085 GetNextVDMCommand(&CommandInfo
);
1087 /* Load the executable */
1088 Result
= DosLoadExecutable(LoadType
,
1092 &Parameters
->StackLocation
,
1093 &Parameters
->EntryPoint
);
1094 if (Result
!= ERROR_SUCCESS
)
1096 DisplayMessage(L
"Could not load '%S'. Error: %u", AppName
, Result
);
1097 // FIXME: Decrement the reenter count. Or, instead, just increment
1098 // the VDM reenter count *only* if this call succeeds...
1104 /* Not handled by NTVDM */
1107 /* Wait for the process to finish executing */
1108 WaitForSingleObject(ProcessInfo
.hProcess
, INFINITE
);
1112 /* Close the handles */
1113 CloseHandle(ProcessInfo
.hProcess
);
1114 CloseHandle(ProcessInfo
.hThread
);
1116 return ERROR_SUCCESS
;
1120 VOID
DosTerminateProcess(WORD Psp
, BYTE ReturnCode
)
1123 WORD McbSegment
= FIRST_MCB_SEGMENT
;
1124 PDOS_MCB CurrentMcb
;
1125 LPDWORD IntVecTable
= (LPDWORD
)((ULONG_PTR
)BaseAddress
);
1126 PDOS_PSP PspBlock
= SEGMENT_TO_PSP(Psp
);
1128 DPRINT("DosTerminateProcess: Psp 0x%04X, ReturnCode 0x%02X\n",
1132 /* Check if this PSP is it's own parent */
1133 if (PspBlock
->ParentPsp
== Psp
) goto Done
;
1135 for (i
= 0; i
< PspBlock
->HandleTableSize
; i
++)
1137 /* Close the handle */
1141 /* Free the memory used by the process */
1144 /* Get a pointer to the MCB */
1145 CurrentMcb
= SEGMENT_TO_MCB(McbSegment
);
1147 /* Make sure the MCB is valid */
1148 if (CurrentMcb
->BlockType
!= 'M' && CurrentMcb
->BlockType
!='Z') break;
1150 /* If this block was allocated by the process, free it */
1151 if (CurrentMcb
->OwnerPsp
== Psp
) DosFreeMemory(McbSegment
+ 1);
1153 /* If this was the last block, quit */
1154 if (CurrentMcb
->BlockType
== 'Z') break;
1156 /* Update the segment and continue */
1157 McbSegment
+= CurrentMcb
->Size
+ 1;
1161 /* Restore the interrupt vectors */
1162 IntVecTable
[0x22] = PspBlock
->TerminateAddress
;
1163 IntVecTable
[0x23] = PspBlock
->BreakAddress
;
1164 IntVecTable
[0x24] = PspBlock
->CriticalAddress
;
1166 /* Update the current PSP */
1167 if (Psp
== CurrentPsp
)
1169 CurrentPsp
= PspBlock
->ParentPsp
;
1170 if (CurrentPsp
== SYSTEM_PSP
)
1172 ResetEvent(VdmTaskEvent
);
1178 // FIXME: This is probably not the best way to do it
1179 /* Check if this was a nested DOS task */
1180 if (CurrentPsp
!= SYSTEM_PSP
)
1182 VDM_COMMAND_INFO CommandInfo
;
1184 /* Decrement the re-entry count */
1185 CommandInfo
.TaskId
= SessionId
;
1186 CommandInfo
.VDMState
= VDM_DEC_REENTER_COUNT
;
1187 GetNextVDMCommand(&CommandInfo
);
1189 /* Clear the structure */
1190 RtlZeroMemory(&CommandInfo
, sizeof(CommandInfo
));
1192 /* Update the VDM state of the task */
1193 CommandInfo
.TaskId
= SessionId
;
1194 CommandInfo
.VDMState
= VDM_FLAG_DONT_WAIT
;
1195 GetNextVDMCommand(&CommandInfo
);
1199 /* Save the return code - Normal termination */
1200 DosErrorLevel
= MAKEWORD(ReturnCode
, 0x00);
1202 /* Return control to the parent process */
1203 CpuExecute(HIWORD(PspBlock
->TerminateAddress
),
1204 LOWORD(PspBlock
->TerminateAddress
));
1207 BOOLEAN
DosHandleIoctl(BYTE ControlCode
, WORD FileHandle
)
1209 PDOS_SFT_ENTRY SftEntry
= DosGetSftEntry(FileHandle
);
1210 PDOS_DEVICE_NODE Node
;
1212 /* Make sure it exists and is a device */
1213 if (!SftEntry
|| SftEntry
->Type
!= DOS_SFT_ENTRY_DEVICE
)
1215 DosLastError
= ERROR_FILE_NOT_FOUND
;
1219 Node
= SftEntry
->DeviceNode
;
1221 switch (ControlCode
)
1223 /* Get Device Information */
1227 * See Ralf Brown: http://www.ctyme.com/intr/rb-2820.htm
1228 * for a list of possible flags.
1231 /* Return the device information word */
1232 setDX(Node
->DeviceAttributes
);
1236 /* Set Device Information */
1239 Node
->DeviceAttributes
= getDX();
1243 /* Read From Device I/O Control Channel */
1246 WORD Length
= getCX();
1248 if (!(Node
->DeviceAttributes
& DOS_DEVATTR_IOCTL
))
1250 DosLastError
= ERROR_INVALID_FUNCTION
;
1254 /* Do nothing if there is no IOCTL routine */
1255 if (!Node
->IoctlReadRoutine
)
1261 Node
->IoctlReadRoutine(Node
, MAKELONG(getDX(), getDS()), &Length
);
1267 /* Write To Device I/O Control Channel */
1270 WORD Length
= getCX();
1272 if (!(Node
->DeviceAttributes
& DOS_DEVATTR_IOCTL
))
1274 DosLastError
= ERROR_INVALID_FUNCTION
;
1278 /* Do nothing if there is no IOCTL routine */
1279 if (!Node
->IoctlWriteRoutine
)
1285 Node
->IoctlWriteRoutine(Node
, MAKELONG(getDX(), getDS()), &Length
);
1291 /* Unsupported control code */
1294 DPRINT1("Unsupported IOCTL: 0x%02X\n", ControlCode
);
1296 DosLastError
= ERROR_INVALID_PARAMETER
;
1302 VOID WINAPI
DosInt20h(LPWORD Stack
)
1304 /* This is the exit interrupt */
1305 DosTerminateProcess(Stack
[STACK_CS
], 0);
1308 VOID WINAPI
DosInt21h(LPWORD Stack
)
1311 SYSTEMTIME SystemTime
;
1313 PDOS_INPUT_BUFFER InputBuffer
;
1314 PDOS_COUNTRY_CODE_BUFFER CountryCodeBuffer
;
1317 /* Check the value in the AH register */
1320 /* Terminate Program */
1323 DosTerminateProcess(Stack
[STACK_CS
], 0);
1327 /* Read Character from STDIN with Echo */
1330 DPRINT("INT 21h, AH = 01h\n");
1332 // FIXME: Under DOS 2+, input / output handle may be redirected!!!!
1334 Character
= DosReadCharacter(DOS_INPUT_HANDLE
);
1337 // FIXME: Check whether Ctrl-C / Ctrl-Break is pressed, and call INT 23h if so.
1338 // Check also Ctrl-P and set echo-to-printer flag.
1339 // Ctrl-Z is not interpreted.
1345 /* Write Character to STDOUT */
1348 // FIXME: Under DOS 2+, output handle may be redirected!!!!
1349 Character
= getDL();
1350 DosPrintCharacter(DOS_OUTPUT_HANDLE
, Character
);
1353 * We return the output character (DOS 2.1+).
1354 * Also, if we're going to output a TAB, then
1355 * don't return a TAB but a SPACE instead.
1356 * See Ralf Brown: http://www.ctyme.com/intr/rb-2554.htm
1357 * for more information.
1359 setAL(Character
== '\t' ? ' ' : Character
);
1363 /* Read Character from STDAUX */
1366 // FIXME: Really read it from STDAUX!
1367 DPRINT1("INT 16h, 03h: Read character from STDAUX is HALFPLEMENTED\n");
1368 // setAL(DosReadCharacter());
1372 /* Write Character to STDAUX */
1375 // FIXME: Really write it to STDAUX!
1376 DPRINT1("INT 16h, 04h: Write character to STDAUX is HALFPLEMENTED\n");
1377 // DosPrintCharacter(getDL());
1381 /* Write Character to Printer */
1384 // FIXME: Really write it to printer!
1385 DPRINT1("INT 16h, 05h: Write character to printer is HALFPLEMENTED -\n\n");
1386 DPRINT1("0x%p\n", getDL());
1387 DPRINT1("\n\n-----------\n\n");
1391 /* Direct Console I/O */
1394 Character
= getDL();
1396 // FIXME: Under DOS 2+, output handle may be redirected!!!!
1398 if (Character
!= 0xFF)
1401 DosPrintCharacter(DOS_OUTPUT_HANDLE
, Character
);
1404 * We return the output character (DOS 2.1+).
1405 * See Ralf Brown: http://www.ctyme.com/intr/rb-2558.htm
1406 * for more information.
1413 if (DosCheckInput())
1415 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_ZF
;
1416 setAL(DosReadCharacter(DOS_INPUT_HANDLE
));
1420 /* No character available */
1421 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_ZF
;
1429 /* Character Input without Echo */
1433 DPRINT("Char input without echo\n");
1435 // FIXME: Under DOS 2+, input handle may be redirected!!!!
1436 Character
= DosReadCharacter(DOS_INPUT_HANDLE
);
1438 // FIXME: For 0x07, do not check Ctrl-C/Break.
1439 // For 0x08, do check those control sequences and if needed,
1442 // /* Let the BOP repeat if needed */
1443 // if (getCF()) break;
1449 /* Write string to STDOUT */
1452 String
= (PCHAR
)SEG_OFF_TO_PTR(getDS(), getDX());
1454 while (*String
!= '$')
1456 DosPrintCharacter(DOS_OUTPUT_HANDLE
, *String
);
1461 * We return the terminating character (DOS 2.1+).
1462 * See Ralf Brown: http://www.ctyme.com/intr/rb-2562.htm
1463 * for more information.
1465 setAL('$'); // *String
1469 /* Read Buffered Input */
1473 InputBuffer
= (PDOS_INPUT_BUFFER
)SEG_OFF_TO_PTR(getDS(), getDX());
1475 DPRINT("Read Buffered Input\n");
1477 while (Count
< InputBuffer
->MaxLength
)
1479 // FIXME!! This function should interpret backspaces etc...
1481 /* Try to read a character (wait) */
1482 Character
= DosReadCharacter(DOS_INPUT_HANDLE
);
1484 // FIXME: Check whether Ctrl-C / Ctrl-Break is pressed, and call INT 23h if so.
1486 /* Echo the character and append it to the buffer */
1487 DosPrintCharacter(DOS_OUTPUT_HANDLE
, Character
);
1488 InputBuffer
->Buffer
[Count
] = Character
;
1490 Count
++; /* Carriage returns are also counted */
1492 if (Character
== '\r') break;
1495 /* Update the length */
1496 InputBuffer
->Length
= Count
;
1501 /* Get STDIN Status */
1504 setAL(DosCheckInput() ? 0xFF : 0x00);
1508 /* Flush Buffer and Read STDIN */
1511 BYTE InputFunction
= getAL();
1513 /* Flush STDIN buffer */
1514 DosFlushFileBuffers(DOS_INPUT_HANDLE
);
1517 * If the input function number contained in AL is valid, i.e.
1518 * AL == 0x01 or 0x06 or 0x07 or 0x08 or 0x0A, call ourselves
1519 * recursively with AL == AH.
1521 if (InputFunction
== 0x01 || InputFunction
== 0x06 ||
1522 InputFunction
== 0x07 || InputFunction
== 0x08 ||
1523 InputFunction
== 0x0A)
1525 /* Call ourselves recursively */
1526 setAH(InputFunction
);
1535 PDOS_PSP PspBlock
= SEGMENT_TO_PSP(CurrentPsp
);
1537 // TODO: Flush what's needed.
1538 DPRINT1("INT 21h, 0Dh is UNIMPLEMENTED\n");
1540 /* Clear CF in DOS 6 only */
1541 if (PspBlock
->DosVersion
== 0x0006)
1542 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
1547 /* Set Default Drive */
1550 DosChangeDrive(getDL());
1551 setAL(LastDrive
- 'A' + 1);
1555 /* NULL Function for CP/M Compatibility */
1559 * This function corresponds to the CP/M BDOS function
1560 * "get bit map of logged drives", which is meaningless
1563 * For: PTS-DOS 6.51 & S/DOS 1.0 - EXTENDED RENAME FILE USING FCB
1564 * See Ralf Brown: http://www.ctyme.com/intr/rb-2584.htm
1565 * for more information.
1571 /* Get Default Drive */
1574 setAL(CurrentDrive
);
1578 /* Set Disk Transfer Area */
1581 DiskTransferArea
= MAKELONG(getDX(), getDS());
1585 /* NULL Function for CP/M Compatibility */
1590 * Function 0x1D corresponds to the CP/M BDOS function
1591 * "get bit map of read-only drives", which is meaningless
1593 * See Ralf Brown: http://www.ctyme.com/intr/rb-2592.htm
1594 * for more information.
1596 * Function 0x1E corresponds to the CP/M BDOS function
1597 * "set file attributes", which was meaningless under MS-DOS 1.x.
1598 * See Ralf Brown: http://www.ctyme.com/intr/rb-2593.htm
1599 * for more information.
1605 /* NULL Function for CP/M Compatibility */
1609 * This function corresponds to the CP/M BDOS function
1610 * "get/set default user (sublibrary) number", which is meaningless
1613 * For: S/DOS 1.0+ & PTS-DOS 6.51+ - GET OEM REVISION
1614 * See Ralf Brown: http://www.ctyme.com/intr/rb-2596.htm
1615 * for more information.
1621 /* Set Interrupt Vector */
1624 ULONG FarPointer
= MAKELONG(getDX(), getDS());
1625 DPRINT1("Setting interrupt 0x%02X to %04X:%04X ...\n",
1626 getAL(), HIWORD(FarPointer
), LOWORD(FarPointer
));
1628 /* Write the new far pointer to the IDT */
1629 ((PULONG
)BaseAddress
)[getAL()] = FarPointer
;
1633 /* Create New PSP */
1636 DPRINT1("INT 21h, AH = 26h - Create New PSP is UNIMPLEMENTED\n");
1640 /* Get System Date */
1643 GetLocalTime(&SystemTime
);
1644 setCX(SystemTime
.wYear
);
1645 setDX(MAKEWORD(SystemTime
.wDay
, SystemTime
.wMonth
));
1646 setAL(SystemTime
.wDayOfWeek
);
1650 /* Set System Date */
1653 GetLocalTime(&SystemTime
);
1654 SystemTime
.wYear
= getCX();
1655 SystemTime
.wMonth
= getDH();
1656 SystemTime
.wDay
= getDL();
1658 /* Return success or failure */
1659 setAL(SetLocalTime(&SystemTime
) ? 0x00 : 0xFF);
1663 /* Get System Time */
1666 GetLocalTime(&SystemTime
);
1667 setCX(MAKEWORD(SystemTime
.wMinute
, SystemTime
.wHour
));
1668 setDX(MAKEWORD(SystemTime
.wMilliseconds
/ 10, SystemTime
.wSecond
));
1672 /* Set System Time */
1675 GetLocalTime(&SystemTime
);
1676 SystemTime
.wHour
= getCH();
1677 SystemTime
.wMinute
= getCL();
1678 SystemTime
.wSecond
= getDH();
1679 SystemTime
.wMilliseconds
= getDL() * 10; // In hundredths of seconds
1681 /* Return success or failure */
1682 setAL(SetLocalTime(&SystemTime
) ? 0x00 : 0xFF);
1686 /* Get Disk Transfer Area */
1689 setES(HIWORD(DiskTransferArea
));
1690 setBX(LOWORD(DiskTransferArea
));
1694 /* Get DOS Version */
1697 PDOS_PSP PspBlock
= SEGMENT_TO_PSP(CurrentPsp
);
1700 * DOS 2+ - GET DOS VERSION
1701 * See Ralf Brown: http://www.ctyme.com/intr/rb-2711.htm
1702 * for more information.
1705 if (LOBYTE(PspBlock
->DosVersion
) < 5 || getAL() == 0x00)
1708 * Return DOS OEM number:
1709 * 0x00 for IBM PC-DOS
1710 * 0x02 for packaged MS-DOS
1716 if (LOBYTE(PspBlock
->DosVersion
) >= 5 && getAL() == 0x01)
1719 * Return version flag:
1720 * 1 << 3 if DOS is in ROM,
1721 * 0 (reserved) if not.
1726 /* Return DOS 24-bit user serial number in BL:CX */
1731 * Return DOS version: Minor:Major in AH:AL
1732 * The Windows NT DOS box returns version 5.00, subject to SETVER.
1734 setAX(PspBlock
->DosVersion
);
1739 /* Extended functionalities */
1742 if (getAL() == 0x06)
1745 * DOS 5+ - GET TRUE VERSION NUMBER
1746 * This function always returns the true version number, unlike
1747 * AH=30h, whose return value may be changed with SETVER.
1748 * See Ralf Brown: http://www.ctyme.com/intr/rb-2730.htm
1749 * for more information.
1753 * Return the true DOS version: Minor:Major in BH:BL
1754 * The Windows NT DOS box returns BX=3205h (version 5.50).
1756 setBX(NTDOS_VERSION
);
1758 /* DOS revision 0 */
1766 // /* Invalid subfunction */
1773 /* Get Interrupt Vector */
1776 DWORD FarPointer
= ((PDWORD
)BaseAddress
)[getAL()];
1778 /* Read the address from the IDT into ES:BX */
1779 setES(HIWORD(FarPointer
));
1780 setBX(LOWORD(FarPointer
));
1784 /* SWITCH character - AVAILDEV */
1787 if (getAL() == 0x00)
1790 * DOS 2+ - "SWITCHAR" - GET SWITCH CHARACTER
1791 * This setting is ignored by MS-DOS 4.0+.
1792 * MS-DOS 5+ always return AL=00h/DL=2Fh.
1793 * See Ralf Brown: http://www.ctyme.com/intr/rb-2752.htm
1794 * for more information.
1799 else if (getAL() == 0x01)
1802 * DOS 2+ - "SWITCHAR" - SET SWITCH CHARACTER
1803 * This setting is ignored by MS-DOS 5+.
1804 * See Ralf Brown: http://www.ctyme.com/intr/rb-2753.htm
1805 * for more information.
1810 else if (getAL() == 0x02)
1813 * DOS 2.x and 3.3+ only - "AVAILDEV" - SPECIFY \DEV\ PREFIX USE
1814 * See Ralf Brown: http://www.ctyme.com/intr/rb-2754.htm
1815 * for more information.
1820 else if (getAL() == 0x03)
1823 * DOS 2.x and 3.3+ only - "AVAILDEV" - SPECIFY \DEV\ PREFIX USE
1824 * See Ralf Brown: http://www.ctyme.com/intr/rb-2754.htm
1825 * for more information.
1832 /* Invalid subfunction */
1839 /* Get/Set Country-dependent Information */
1842 CountryCodeBuffer
= (PDOS_COUNTRY_CODE_BUFFER
)SEG_OFF_TO_PTR(getDS(), getDX());
1844 if (getAL() == 0x00)
1847 Return
= GetLocaleInfo(LOCALE_USER_DEFAULT
, LOCALE_IDATE
,
1848 &CountryCodeBuffer
->TimeFormat
,
1849 sizeof(CountryCodeBuffer
->TimeFormat
) / sizeof(TCHAR
));
1852 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
1853 setAX(LOWORD(GetLastError()));
1857 Return
= GetLocaleInfo(LOCALE_USER_DEFAULT
, LOCALE_SCURRENCY
,
1858 &CountryCodeBuffer
->CurrencySymbol
,
1859 sizeof(CountryCodeBuffer
->CurrencySymbol
) / sizeof(TCHAR
));
1862 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
1863 setAX(LOWORD(GetLastError()));
1867 Return
= GetLocaleInfo(LOCALE_USER_DEFAULT
, LOCALE_STHOUSAND
,
1868 &CountryCodeBuffer
->ThousandSep
,
1869 sizeof(CountryCodeBuffer
->ThousandSep
) / sizeof(TCHAR
));
1872 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
1873 setAX(LOWORD(GetLastError()));
1877 Return
= GetLocaleInfo(LOCALE_USER_DEFAULT
, LOCALE_SDECIMAL
,
1878 &CountryCodeBuffer
->DecimalSep
,
1879 sizeof(CountryCodeBuffer
->DecimalSep
) / sizeof(TCHAR
));
1882 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
1883 setAX(LOWORD(GetLastError()));
1887 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
1891 // TODO: NOT IMPLEMENTED
1898 /* Create Directory */
1901 String
= (PCHAR
)SEG_OFF_TO_PTR(getDS(), getDX());
1903 if (CreateDirectoryA(String
, NULL
))
1905 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
1909 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
1910 setAX(LOWORD(GetLastError()));
1916 /* Remove Directory */
1919 String
= (PCHAR
)SEG_OFF_TO_PTR(getDS(), getDX());
1921 if (RemoveDirectoryA(String
))
1923 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
1927 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
1928 setAX(LOWORD(GetLastError()));
1934 /* Set Current Directory */
1937 String
= (PCHAR
)SEG_OFF_TO_PTR(getDS(), getDX());
1939 if (DosChangeDirectory(String
))
1941 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
1945 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
1946 setAX(DosLastError
);
1952 /* Create or Truncate File */
1956 WORD ErrorCode
= DosCreateFile(&FileHandle
,
1957 (LPCSTR
)SEG_OFF_TO_PTR(getDS(), getDX()),
1961 if (ErrorCode
== ERROR_SUCCESS
)
1963 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
1968 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
1975 /* Open File or Device */
1980 LPCSTR FileName
= (LPCSTR
)SEG_OFF_TO_PTR(getDS(), getDX());
1981 PDOS_DEVICE_NODE Device
= DosGetDevice(FileName
);
1985 FileHandle
= DosOpenDevice(Device
);
1986 ErrorCode
= (FileHandle
!= INVALID_DOS_HANDLE
)
1987 ? ERROR_SUCCESS
: ERROR_TOO_MANY_OPEN_FILES
;
1991 ErrorCode
= DosOpenFile(&FileHandle
, FileName
, getAL());
1994 if (ErrorCode
== ERROR_SUCCESS
)
1996 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2001 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2008 /* Close File or Device */
2011 if (DosCloseHandle(getBX()))
2013 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2017 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2018 setAX(ERROR_INVALID_HANDLE
);
2024 /* Read from File or Device */
2030 DPRINT("DosReadFile(0x%04X)\n", getBX());
2033 ErrorCode
= DosReadFile(getBX(),
2034 MAKELONG(getDX(), getDS()),
2039 if (ErrorCode
== ERROR_SUCCESS
)
2041 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2044 else if (ErrorCode
!= ERROR_NOT_READY
)
2046 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2053 /* Write to File or Device */
2056 WORD BytesWritten
= 0;
2057 WORD ErrorCode
= DosWriteFile(getBX(),
2058 MAKELONG(getDX(), getDS()),
2062 if (ErrorCode
== ERROR_SUCCESS
)
2064 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2065 setAX(BytesWritten
);
2069 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2079 LPSTR FileName
= (LPSTR
)SEG_OFF_TO_PTR(getDS(), getDX());
2081 if (demFileDelete(FileName
) == ERROR_SUCCESS
)
2083 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2085 * See Ralf Brown: http://www.ctyme.com/intr/rb-2797.htm
2086 * "AX destroyed (DOS 3.3) AL seems to be drive of deleted file."
2088 setAL(FileName
[0] - 'A');
2092 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2093 setAX(GetLastError());
2103 WORD ErrorCode
= DosSeekFile(getBX(),
2104 MAKELONG(getDX(), getCX()),
2108 if (ErrorCode
== ERROR_SUCCESS
)
2110 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2112 /* Return the new offset in DX:AX */
2113 setDX(HIWORD(NewLocation
));
2114 setAX(LOWORD(NewLocation
));
2118 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2125 /* Get/Set File Attributes */
2129 LPSTR FileName
= (LPSTR
)SEG_OFF_TO_PTR(getDS(), getDX());
2131 if (getAL() == 0x00)
2133 /* Get the attributes */
2134 Attributes
= GetFileAttributesA(FileName
);
2136 /* Check if it failed */
2137 if (Attributes
== INVALID_FILE_ATTRIBUTES
)
2139 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2140 setAX(GetLastError());
2144 /* Return the attributes that DOS can understand */
2145 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2146 setCX(Attributes
& 0x00FF);
2149 else if (getAL() == 0x01)
2151 /* Try to set the attributes */
2152 if (SetFileAttributesA(FileName
, getCL()))
2154 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2158 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2159 setAX(GetLastError());
2164 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2165 setAX(ERROR_INVALID_FUNCTION
);
2174 if (DosHandleIoctl(getAL(), getBX()))
2176 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2180 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2181 setAX(DosLastError
);
2187 /* Duplicate Handle */
2191 PDOS_SFT_ENTRY SftEntry
= DosGetSftEntry(getBX());
2193 if (SftEntry
== NULL
|| SftEntry
->Type
== DOS_SFT_ENTRY_NONE
)
2195 /* The handle is invalid */
2196 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2197 setAX(ERROR_INVALID_HANDLE
);
2201 /* Open a new handle to the same entry */
2202 switch (SftEntry
->Type
)
2204 case DOS_SFT_ENTRY_WIN32
:
2206 NewHandle
= DosOpenHandle(SftEntry
->Handle
);
2210 case DOS_SFT_ENTRY_DEVICE
:
2212 NewHandle
= DosOpenDevice(SftEntry
->DeviceNode
);
2218 /* Shouldn't happen */
2223 if (NewHandle
== INVALID_DOS_HANDLE
)
2225 /* Too many files open */
2226 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2227 setAX(ERROR_TOO_MANY_OPEN_FILES
);
2231 /* Return the result */
2232 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2237 /* Force Duplicate Handle */
2240 if (DosDuplicateHandle(getBX(), getCX()))
2242 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2246 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2247 setAX(ERROR_INVALID_HANDLE
);
2253 /* Get Current Directory */
2256 BYTE DriveNumber
= getDL();
2257 String
= (PCHAR
)SEG_OFF_TO_PTR(getDS(), getSI());
2259 /* Get the real drive number */
2260 if (DriveNumber
== 0)
2262 DriveNumber
= CurrentDrive
;
2266 /* Decrement DriveNumber since it was 1-based */
2270 if (DriveNumber
<= LastDrive
- 'A')
2273 * Copy the current directory into the target buffer.
2274 * It doesn't contain the drive letter and the backslash.
2276 strncpy(String
, CurrentDirectories
[DriveNumber
], DOS_DIR_LENGTH
);
2277 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2278 setAX(0x0100); // Undocumented, see Ralf Brown: http://www.ctyme.com/intr/rb-2933.htm
2282 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2283 setAX(ERROR_INVALID_DRIVE
);
2289 /* Allocate Memory */
2292 WORD MaxAvailable
= 0;
2293 WORD Segment
= DosAllocateMemory(getBX(), &MaxAvailable
);
2297 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2302 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2303 setAX(DosLastError
);
2304 setBX(MaxAvailable
);
2313 if (DosFreeMemory(getES()))
2315 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2319 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2320 setAX(ERROR_ARENA_TRASHED
);
2326 /* Resize Memory Block */
2331 if (DosResizeMemory(getES(), getBX(), &Size
))
2333 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2337 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2338 setAX(DosLastError
);
2349 DOS_EXEC_TYPE LoadType
= (DOS_EXEC_TYPE
)getAL();
2350 LPSTR ProgramName
= SEG_OFF_TO_PTR(getDS(), getDX());
2351 PDOS_EXEC_PARAM_BLOCK ParamBlock
= SEG_OFF_TO_PTR(getES(), getBX());
2352 WORD ErrorCode
= DosCreateProcess(LoadType
, ProgramName
, ParamBlock
);
2354 if (ErrorCode
== ERROR_SUCCESS
)
2356 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2360 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2368 /* Terminate With Return Code */
2371 DosTerminateProcess(CurrentPsp
, getAL());
2375 /* Get Return Code (ERRORLEVEL) */
2379 * According to Ralf Brown: http://www.ctyme.com/intr/rb-2976.htm
2380 * DosErrorLevel is cleared after being read by this function.
2382 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2383 setAX(DosErrorLevel
);
2384 DosErrorLevel
= 0x0000; // Clear it
2388 /* Find First File */
2391 WORD Result
= (WORD
)demFileFindFirst(FAR_POINTER(DiskTransferArea
),
2392 SEG_OFF_TO_PTR(getDS(), getDX()),
2397 if (Result
== ERROR_SUCCESS
)
2398 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2400 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2405 /* Find Next File */
2408 WORD Result
= (WORD
)demFileFindNext(FAR_POINTER(DiskTransferArea
));
2412 if (Result
== ERROR_SUCCESS
)
2413 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2415 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2420 /* Internal - Set Current Process ID (Set PSP Address) */
2423 // FIXME: Is it really what it's done ??
2424 CurrentPsp
= getBX();
2428 /* Internal - Get Current Process ID (Get PSP Address) */
2430 /* Get Current PSP Address */
2434 * Undocumented AH=51h is identical to the documented AH=62h.
2435 * See Ralf Brown: http://www.ctyme.com/intr/rb-2982.htm
2436 * and http://www.ctyme.com/intr/rb-3140.htm
2437 * for more information.
2443 /* Internal - Get "List of lists" (SYSVARS) */
2447 * On return, ES points at the DOS data segment (see also INT 2F/AX=1203h).
2448 * See Ralf Brown: http://www.ctyme.com/intr/rb-2983.htm
2449 * for more information.
2452 /* Return the DOS "list of lists" in ES:BX */
2456 DisplayMessage(L
"Required for AARD code, do you remember? :P");
2463 LPSTR ExistingFileName
= (LPSTR
)SEG_OFF_TO_PTR(getDS(), getDX());
2464 LPSTR NewFileName
= (LPSTR
)SEG_OFF_TO_PTR(getES(), getDI());
2467 * See Ralf Brown: http://www.ctyme.com/intr/rb-2990.htm
2468 * for more information.
2471 if (MoveFileA(ExistingFileName
, NewFileName
))
2473 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2477 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2478 setAX(GetLastError());
2484 /* Get/Set Memory Management Options */
2487 if (getAL() == 0x00)
2489 /* Get allocation strategy */
2490 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2491 setAX(DosAllocStrategy
);
2493 else if (getAL() == 0x01)
2495 /* Set allocation strategy */
2497 if ((getBL() & (DOS_ALLOC_HIGH
| DOS_ALLOC_HIGH_LOW
))
2498 == (DOS_ALLOC_HIGH
| DOS_ALLOC_HIGH_LOW
))
2500 /* Can't set both */
2501 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2502 setAX(ERROR_INVALID_PARAMETER
);
2506 if ((getBL() & 0x3F) > DOS_ALLOC_LAST_FIT
)
2508 /* Invalid allocation strategy */
2509 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2510 setAX(ERROR_INVALID_PARAMETER
);
2514 DosAllocStrategy
= getBL();
2515 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2517 else if (getAL() == 0x02)
2519 /* Get UMB link state */
2520 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2521 setAL(DosUmbLinked
? 0x01 : 0x00);
2523 else if (getAL() == 0x03)
2525 /* Set UMB link state */
2526 if (getBX()) DosLinkUmb();
2527 else DosUnlinkUmb();
2528 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2532 /* Invalid or unsupported function */
2533 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2534 setAX(ERROR_INVALID_FUNCTION
);
2540 /* Get Extended Error Information */
2543 DPRINT1("INT 21h, AH = 59h, BX = %04Xh - Get Extended Error Information is UNIMPLEMENTED\n",
2548 /* Create Temporary File */
2551 LPSTR PathName
= (LPSTR
)SEG_OFF_TO_PTR(getDS(), getDX());
2552 LPSTR FileName
= PathName
; // The buffer for the path and the full file name is the same.
2558 * See Ralf Brown: http://www.ctyme.com/intr/rb-3014.htm
2559 * for more information.
2562 // FIXME: Check for buffer validity?
2563 // It should be a ASCIZ path ending with a '\' + 13 zero bytes
2564 // to receive the generated filename.
2566 /* First create the temporary file */
2567 uRetVal
= GetTempFileNameA(PathName
, NULL
, 0, FileName
);
2570 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2571 setAX(GetLastError());
2575 /* Now try to open it in read/write access */
2576 ErrorCode
= DosOpenFile(&FileHandle
, FileName
, 2);
2577 if (ErrorCode
== ERROR_SUCCESS
)
2579 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2584 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2591 /* Create New File */
2595 WORD ErrorCode
= DosCreateFile(&FileHandle
,
2596 (LPCSTR
)SEG_OFF_TO_PTR(getDS(), getDX()),
2600 if (ErrorCode
== ERROR_SUCCESS
)
2602 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2607 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2614 /* Lock/Unlock Region of File */
2617 PDOS_SFT_ENTRY SftEntry
= DosGetSftEntry(getBX());
2619 if (SftEntry
== NULL
|| SftEntry
->Type
!= DOS_SFT_ENTRY_WIN32
)
2621 /* The handle is invalid */
2622 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2623 setAX(ERROR_INVALID_HANDLE
);
2627 if (getAL() == 0x00)
2629 /* Lock region of file */
2630 if (LockFile(SftEntry
->Handle
,
2631 MAKELONG(getCX(), getDX()), 0,
2632 MAKELONG(getSI(), getDI()), 0))
2634 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2638 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2639 setAX(GetLastError());
2642 else if (getAL() == 0x01)
2644 /* Unlock region of file */
2645 if (UnlockFile(SftEntry
->Handle
,
2646 MAKELONG(getCX(), getDX()), 0,
2647 MAKELONG(getSI(), getDI()), 0))
2649 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2653 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2654 setAX(GetLastError());
2659 /* Invalid subfunction */
2660 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2661 setAX(ERROR_INVALID_FUNCTION
);
2667 /* Canonicalize File Name or Path */
2671 * See Ralf Brown: http://www.ctyme.com/intr/rb-3137.htm
2672 * for more information.
2676 * We suppose that the DOS app gave to us a valid
2677 * 128-byte long buffer for the canonicalized name.
2679 DWORD dwRetVal
= GetFullPathNameA(SEG_OFF_TO_PTR(getDS(), getSI()),
2681 SEG_OFF_TO_PTR(getES(), getDI()),
2685 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2686 setAX(GetLastError());
2690 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2694 // FIXME: Convert the full path name into short version.
2695 // We cannot reliably use GetShortPathName, because it fails
2696 // if the path name given doesn't exist. However this DOS
2697 // function AH=60h should be able to work even for non-existing
2698 // path and file names.
2703 /* Set Handle Count */
2706 if (!DosResizeHandleTable(getBX()))
2708 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2709 setAX(DosLastError
);
2711 else Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2721 * Function 6Ah is identical to function 68h,
2722 * and sets AH to 68h if success.
2723 * See Ralf Brown: http://www.ctyme.com/intr/rb-3176.htm
2724 * for more information.
2728 if (DosFlushFileBuffers(getBX()))
2730 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2734 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2735 setAX(GetLastError());
2741 /* Extended Open/Create */
2745 WORD CreationStatus
;
2748 /* Check for AL == 00 */
2749 if (getAL() != 0x00)
2751 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2752 setAX(ERROR_INVALID_FUNCTION
);
2757 * See Ralf Brown: http://www.ctyme.com/intr/rb-3179.htm
2758 * for the full detailed description.
2760 * WARNING: BH contains some extended flags that are NOT SUPPORTED.
2763 ErrorCode
= DosCreateFileEx(&FileHandle
,
2765 (LPCSTR
)SEG_OFF_TO_PTR(getDS(), getSI()),
2770 if (ErrorCode
== ERROR_SUCCESS
)
2772 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2773 setCX(CreationStatus
);
2778 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2788 DPRINT1("DOS Function INT 0x21, AH = %xh, AL = %xh NOT IMPLEMENTED!\n",
2791 setAL(0); // Some functions expect AL to be 0 when it's not supported.
2792 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2797 VOID WINAPI
DosBreakInterrupt(LPWORD Stack
)
2799 UNREFERENCED_PARAMETER(Stack
);
2801 /* Stop the VDM task */
2802 ResetEvent(VdmTaskEvent
);
2806 VOID WINAPI
DosFastConOut(LPWORD Stack
)
2809 * This is the DOS 2+ Fast Console Output Interrupt.
2810 * The default handler under DOS 2.x and 3.x simply calls INT 10h/AH=0Eh.
2812 * See Ralf Brown: http://www.ctyme.com/intr/rb-4124.htm
2813 * for more information.
2816 /* Save AX and BX */
2817 USHORT AX
= getAX();
2818 USHORT BX
= getBX();
2821 * Set the parameters:
2822 * AL contains the character to print (already set),
2823 * BL contains the character attribute,
2824 * BH contains the video page to use.
2826 setBL(DOS_CHAR_ATTRIBUTE
);
2827 setBH(Bda
->VideoPage
);
2829 /* Call the BIOS INT 10h, AH=0Eh "Teletype Output" */
2831 Int32Call(&DosContext
, BIOS_VIDEO_INTERRUPT
);
2833 /* Restore AX and BX */
2838 VOID WINAPI
DosInt2Fh(LPWORD Stack
)
2840 DPRINT1("DOS Internal System Function INT 0x2F, AH = %xh, AL = %xh NOT IMPLEMENTED!\n",
2842 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2845 BOOLEAN
DosKRNLInitialize(VOID
)
2851 CHAR CurrentDirectory
[MAX_PATH
];
2852 CHAR DosDirectory
[DOS_DIR_LENGTH
];
2858 /* Clear the current directory buffer */
2859 RtlZeroMemory(CurrentDirectories
, sizeof(CurrentDirectories
));
2861 /* Get the current directory */
2862 if (!GetCurrentDirectoryA(MAX_PATH
, CurrentDirectory
))
2864 // TODO: Use some kind of default path?
2868 /* Convert that to a DOS path */
2869 if (!GetShortPathNameA(CurrentDirectory
, DosDirectory
, DOS_DIR_LENGTH
))
2871 // TODO: Use some kind of default path?
2876 CurrentDrive
= DosDirectory
[0] - 'A';
2878 /* Get the directory part of the path */
2879 Path
= strchr(DosDirectory
, '\\');
2882 /* Skip the backslash */
2886 /* Set the directory */
2889 strncpy(CurrentDirectories
[CurrentDrive
], Path
, DOS_DIR_LENGTH
);
2892 /* Read CONFIG.SYS */
2893 Stream
= _wfopen(DOS_CONFIG_PATH
, L
"r");
2896 while (fgetws(Buffer
, 256, Stream
))
2898 // TODO: Parse the line
2903 /* Initialize the SFT */
2904 for (i
= 0; i
< DOS_SFT_SIZE
; i
++)
2906 DosSystemFileTable
[i
].Type
= DOS_SFT_ENTRY_NONE
;
2907 DosSystemFileTable
[i
].RefCount
= 0;
2910 /* Load the EMS driver */
2913 /* Load the CON driver */
2918 /* Initialize the callback context */
2919 InitializeContext(&DosContext
, 0x0070, 0x0000);
2921 /* Register the DOS 32-bit Interrupts */
2922 RegisterDosInt32(0x20, DosInt20h
);
2923 RegisterDosInt32(0x21, DosInt21h
);
2924 // RegisterDosInt32(0x22, DosInt22h ); // Termination
2925 RegisterDosInt32(0x23, DosBreakInterrupt
); // Ctrl-C / Ctrl-Break
2926 // RegisterDosInt32(0x24, DosInt24h ); // Critical Error
2927 RegisterDosInt32(0x29, DosFastConOut
); // DOS 2+ Fast Console Output
2928 RegisterDosInt32(0x2F, DosInt2Fh
);