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 *******************************************************************/
20 #include "bios/bios.h"
21 #include "registers.h"
23 /* PRIVATE VARIABLES **********************************************************/
25 CALLBACK16 DosContext
;
27 static WORD CurrentPsp
= SYSTEM_PSP
;
28 static WORD DosLastError
= 0;
29 static DWORD DiskTransferArea
;
30 /*static*/ BYTE CurrentDrive
;
31 static CHAR LastDrive
= 'E';
32 static CHAR CurrentDirectories
[NUM_DRIVES
][DOS_DIR_LENGTH
];
38 } DosSystemFileTable
[DOS_SFT_SIZE
];
40 static BYTE DosAllocStrategy
= DOS_ALLOC_BEST_FIT
;
41 static BOOLEAN DosUmbLinked
= FALSE
;
42 static WORD DosErrorLevel
= 0x0000;
44 /* Echo state for INT 21h, AH = 01h and AH = 3Fh */
45 BOOLEAN DoEcho
= FALSE
;
47 /* PRIVATE FUNCTIONS **********************************************************/
50 * Memory management functions
52 static VOID
DosCombineFreeBlocks(WORD StartBlock
)
54 PDOS_MCB CurrentMcb
= SEGMENT_TO_MCB(StartBlock
), NextMcb
;
56 /* If this is the last block or it's not free, quit */
57 if (CurrentMcb
->BlockType
== 'Z' || CurrentMcb
->OwnerPsp
!= 0) return;
61 /* Get a pointer to the next MCB */
62 NextMcb
= SEGMENT_TO_MCB(StartBlock
+ CurrentMcb
->Size
+ 1);
64 /* Check if the next MCB is free */
65 if (NextMcb
->OwnerPsp
== 0)
68 CurrentMcb
->Size
+= NextMcb
->Size
+ 1;
69 CurrentMcb
->BlockType
= NextMcb
->BlockType
;
70 NextMcb
->BlockType
= 'I';
74 /* No more adjoining free blocks */
80 static WORD
DosAllocateMemory(WORD Size
, WORD
*MaxAvailable
)
82 WORD Result
= 0, Segment
= FIRST_MCB_SEGMENT
, MaxSize
= 0;
83 PDOS_MCB CurrentMcb
, NextMcb
;
84 BOOLEAN SearchUmb
= FALSE
;
86 DPRINT("DosAllocateMemory: Size 0x%04X\n", Size
);
88 if (DosUmbLinked
&& (DosAllocStrategy
& (DOS_ALLOC_HIGH
| DOS_ALLOC_HIGH_LOW
)))
90 /* Search UMB first */
91 Segment
= UMB_START_SEGMENT
;
97 /* Get a pointer to the MCB */
98 CurrentMcb
= SEGMENT_TO_MCB(Segment
);
100 /* Make sure it's valid */
101 if (CurrentMcb
->BlockType
!= 'M' && CurrentMcb
->BlockType
!= 'Z')
103 DPRINT("The DOS memory arena is corrupted!\n");
104 DosLastError
= ERROR_ARENA_TRASHED
;
108 /* Only check free blocks */
109 if (CurrentMcb
->OwnerPsp
!= 0) goto Next
;
111 /* Combine this free block with adjoining free blocks */
112 DosCombineFreeBlocks(Segment
);
114 /* Update the maximum block size */
115 if (CurrentMcb
->Size
> MaxSize
) MaxSize
= CurrentMcb
->Size
;
117 /* Check if this block is big enough */
118 if (CurrentMcb
->Size
< Size
) goto Next
;
120 switch (DosAllocStrategy
& 0x3F)
122 case DOS_ALLOC_FIRST_FIT
:
124 /* For first fit, stop immediately */
129 case DOS_ALLOC_BEST_FIT
:
131 /* For best fit, update the smallest block found so far */
132 if ((Result
== 0) || (CurrentMcb
->Size
< SEGMENT_TO_MCB(Result
)->Size
))
140 case DOS_ALLOC_LAST_FIT
:
142 /* For last fit, make the current block the result, but keep searching */
149 /* If this was the last MCB in the chain, quit */
150 if (CurrentMcb
->BlockType
== 'Z')
152 /* Check if nothing was found while searching through UMBs */
153 if ((Result
== 0) && SearchUmb
&& (DosAllocStrategy
& DOS_ALLOC_HIGH_LOW
))
155 /* Search low memory */
156 Segment
= FIRST_MCB_SEGMENT
;
163 /* Otherwise, update the segment and continue */
164 Segment
+= CurrentMcb
->Size
+ 1;
169 /* If we didn't find a free block, return 0 */
172 DosLastError
= ERROR_NOT_ENOUGH_MEMORY
;
173 if (MaxAvailable
) *MaxAvailable
= MaxSize
;
177 /* Get a pointer to the MCB */
178 CurrentMcb
= SEGMENT_TO_MCB(Result
);
180 /* Check if the block is larger than requested */
181 if (CurrentMcb
->Size
> Size
)
183 /* It is, split it into two blocks */
184 NextMcb
= SEGMENT_TO_MCB(Result
+ Size
+ 1);
186 /* Initialize the new MCB structure */
187 NextMcb
->BlockType
= CurrentMcb
->BlockType
;
188 NextMcb
->Size
= CurrentMcb
->Size
- Size
- 1;
189 NextMcb
->OwnerPsp
= 0;
191 /* Update the current block */
192 CurrentMcb
->BlockType
= 'M';
193 CurrentMcb
->Size
= Size
;
196 /* Take ownership of the block */
197 CurrentMcb
->OwnerPsp
= CurrentPsp
;
199 /* Return the segment of the data portion of the block */
203 static BOOLEAN
DosResizeMemory(WORD BlockData
, WORD NewSize
, WORD
*MaxAvailable
)
205 BOOLEAN Success
= TRUE
;
206 WORD Segment
= BlockData
- 1, ReturnSize
= 0, NextSegment
;
207 PDOS_MCB Mcb
= SEGMENT_TO_MCB(Segment
), NextMcb
;
209 DPRINT("DosResizeMemory: BlockData 0x%04X, NewSize 0x%04X\n",
213 /* Make sure this is a valid, allocated block */
214 if ((Mcb
->BlockType
!= 'M' && Mcb
->BlockType
!= 'Z') || Mcb
->OwnerPsp
== 0)
217 DosLastError
= ERROR_INVALID_HANDLE
;
221 ReturnSize
= Mcb
->Size
;
223 /* Check if we need to expand or contract the block */
224 if (NewSize
> Mcb
->Size
)
226 /* We can't expand the last block */
227 if (Mcb
->BlockType
!= 'M')
233 /* Get the pointer and segment of the next MCB */
234 NextSegment
= Segment
+ Mcb
->Size
+ 1;
235 NextMcb
= SEGMENT_TO_MCB(NextSegment
);
237 /* Make sure the next segment is free */
238 if (NextMcb
->OwnerPsp
!= 0)
240 DPRINT("Cannot expand memory block: next segment is not free!\n");
241 DosLastError
= ERROR_NOT_ENOUGH_MEMORY
;
246 /* Combine this free block with adjoining free blocks */
247 DosCombineFreeBlocks(NextSegment
);
249 /* Set the maximum possible size of the block */
250 ReturnSize
+= NextMcb
->Size
+ 1;
252 if (ReturnSize
< NewSize
)
254 DPRINT("Cannot expand memory block: insufficient free segments available!\n");
255 DosLastError
= ERROR_NOT_ENOUGH_MEMORY
;
260 /* Maximize the current block */
261 Mcb
->Size
= ReturnSize
;
262 Mcb
->BlockType
= NextMcb
->BlockType
;
264 /* Invalidate the next block */
265 NextMcb
->BlockType
= 'I';
267 /* Check if the block is larger than requested */
268 if (Mcb
->Size
> NewSize
)
270 DPRINT("Block too large, reducing size from 0x%04X to 0x%04X\n",
274 /* It is, split it into two blocks */
275 NextMcb
= SEGMENT_TO_MCB(Segment
+ NewSize
+ 1);
277 /* Initialize the new MCB structure */
278 NextMcb
->BlockType
= Mcb
->BlockType
;
279 NextMcb
->Size
= Mcb
->Size
- NewSize
- 1;
280 NextMcb
->OwnerPsp
= 0;
282 /* Update the current block */
283 Mcb
->BlockType
= 'M';
287 else if (NewSize
< Mcb
->Size
)
289 DPRINT("Shrinking block from 0x%04X to 0x%04X\n",
293 /* Just split the block */
294 NextMcb
= SEGMENT_TO_MCB(Segment
+ NewSize
+ 1);
295 NextMcb
->BlockType
= Mcb
->BlockType
;
296 NextMcb
->Size
= Mcb
->Size
- NewSize
- 1;
297 NextMcb
->OwnerPsp
= 0;
300 Mcb
->BlockType
= 'M';
305 /* Check if the operation failed */
308 DPRINT("DosResizeMemory FAILED. Maximum available: 0x%04X\n",
311 /* Return the maximum possible size */
312 if (MaxAvailable
) *MaxAvailable
= ReturnSize
;
318 static BOOLEAN
DosFreeMemory(WORD BlockData
)
320 PDOS_MCB Mcb
= SEGMENT_TO_MCB(BlockData
- 1);
322 DPRINT("DosFreeMemory: BlockData 0x%04X\n", BlockData
);
324 /* Make sure the MCB is valid */
325 if (Mcb
->BlockType
!= 'M' && Mcb
->BlockType
!= 'Z')
327 DPRINT("MCB block type '%c' not valid!\n", Mcb
->BlockType
);
331 /* Mark the block as free */
337 static BOOLEAN
DosLinkUmb(VOID
)
339 DWORD Segment
= FIRST_MCB_SEGMENT
;
340 PDOS_MCB Mcb
= SEGMENT_TO_MCB(Segment
);
342 DPRINT("Linking UMB\n");
344 /* Check if UMBs are already linked */
345 if (DosUmbLinked
) return FALSE
;
347 /* Find the last block */
348 while ((Mcb
->BlockType
== 'M') && (Segment
<= 0xFFFF))
350 Segment
+= Mcb
->Size
+ 1;
351 Mcb
= SEGMENT_TO_MCB(Segment
);
354 /* Make sure it's valid */
355 if (Mcb
->BlockType
!= 'Z') return FALSE
;
357 /* Connect the MCB with the UMB chain */
358 Mcb
->BlockType
= 'M';
364 static BOOLEAN
DosUnlinkUmb(VOID
)
366 DWORD Segment
= FIRST_MCB_SEGMENT
;
367 PDOS_MCB Mcb
= SEGMENT_TO_MCB(Segment
);
369 DPRINT("Unlinking UMB\n");
371 /* Check if UMBs are already unlinked */
372 if (!DosUmbLinked
) return FALSE
;
374 /* Find the block preceding the MCB that links it with the UMB chain */
375 while (Segment
<= 0xFFFF)
377 if ((Segment
+ Mcb
->Size
) == (FIRST_MCB_SEGMENT
+ USER_MEMORY_SIZE
))
379 /* This is the last non-UMB segment */
383 /* Advance to the next MCB */
384 Segment
+= Mcb
->Size
+ 1;
385 Mcb
= SEGMENT_TO_MCB(Segment
);
388 /* Mark the MCB as the last MCB */
389 Mcb
->BlockType
= 'Z';
391 DosUmbLinked
= FALSE
;
395 static VOID
DosChangeMemoryOwner(WORD Segment
, WORD NewOwner
)
397 PDOS_MCB Mcb
= SEGMENT_TO_MCB(Segment
- 1);
399 /* Just set the owner */
400 Mcb
->OwnerPsp
= NewOwner
;
403 static WORD
DosCopyEnvironmentBlock(LPCVOID Environment
, LPCSTR ProgramName
)
405 PCHAR Ptr
, DestBuffer
= NULL
;
409 Ptr
= (PCHAR
)Environment
;
411 /* Calculate the size of the environment block */
414 TotalSize
+= strlen(Ptr
) + 1;
415 Ptr
+= strlen(Ptr
) + 1;
419 /* Add the string buffer size */
420 TotalSize
+= strlen(ProgramName
) + 1;
422 /* Allocate the memory for the environment block */
423 DestSegment
= DosAllocateMemory((WORD
)((TotalSize
+ 0x0F) >> 4), NULL
);
424 if (!DestSegment
) return 0;
426 Ptr
= (PCHAR
)Environment
;
428 DestBuffer
= (PCHAR
)SEG_OFF_TO_PTR(DestSegment
, 0);
431 /* Copy the string */
432 strcpy(DestBuffer
, Ptr
);
434 /* Advance to the next string */
435 DestBuffer
+= strlen(Ptr
);
436 Ptr
+= strlen(Ptr
) + 1;
438 /* Put a zero after the string */
442 /* Set the final zero */
445 /* Store the special program name tag */
446 *(DestBuffer
++) = LOBYTE(DOS_PROGRAM_NAME_TAG
);
447 *(DestBuffer
++) = HIBYTE(DOS_PROGRAM_NAME_TAG
);
449 /* Copy the program name after the environment block */
450 strcpy(DestBuffer
, ProgramName
);
460 /* Taken from base/shell/cmd/console.c */
461 BOOL
IsConsoleHandle(HANDLE hHandle
)
465 /* Check whether the handle may be that of a console... */
466 if ((GetFileType(hHandle
) & FILE_TYPE_CHAR
) == 0) return FALSE
;
469 * It may be. Perform another test... The idea comes from the
470 * MSDN description of the WriteConsole API:
472 * "WriteConsole fails if it is used with a standard handle
473 * that is redirected to a file. If an application processes
474 * multilingual output that can be redirected, determine whether
475 * the output handle is a console handle (one method is to call
476 * the GetConsoleMode function and check whether it succeeds).
477 * If the handle is a console handle, call WriteConsole. If the
478 * handle is not a console handle, the output is redirected and
479 * you should call WriteFile to perform the I/O."
481 return GetConsoleMode(hHandle
, &dwMode
);
484 WORD
DosOpenHandle(HANDLE Handle
)
491 /* The system PSP has no handle table */
492 if (CurrentPsp
== SYSTEM_PSP
) return INVALID_DOS_HANDLE
;
494 /* Get a pointer to the handle table */
495 PspBlock
= SEGMENT_TO_PSP(CurrentPsp
);
496 HandleTable
= (LPBYTE
)FAR_POINTER(PspBlock
->HandleTablePtr
);
498 /* Find a free entry in the JFT */
499 for (DosHandle
= 0; DosHandle
< PspBlock
->HandleTableSize
; DosHandle
++)
501 if (HandleTable
[DosHandle
] == 0xFF) break;
504 /* If there are no free entries, fail */
505 if (DosHandle
== PspBlock
->HandleTableSize
) return INVALID_DOS_HANDLE
;
507 /* Check if the handle is already in the SFT */
508 for (i
= 0; i
< DOS_SFT_SIZE
; i
++)
510 /* Check if this is the same handle */
511 if (DosSystemFileTable
[i
].Handle
!= Handle
) continue;
513 /* Already in the table, reference it */
514 DosSystemFileTable
[i
].RefCount
++;
516 /* Set the JFT entry to that SFT index */
517 HandleTable
[DosHandle
] = i
;
519 /* Return the new handle */
523 /* Add the handle to the SFT */
524 for (i
= 0; i
< DOS_SFT_SIZE
; i
++)
526 /* Make sure this is an empty table entry */
527 if (DosSystemFileTable
[i
].Handle
!= INVALID_HANDLE_VALUE
) continue;
529 /* Initialize the empty table entry */
530 DosSystemFileTable
[i
].Handle
= Handle
;
531 DosSystemFileTable
[i
].RefCount
= 1;
533 /* Set the JFT entry to that SFT index */
534 HandleTable
[DosHandle
] = i
;
536 /* Return the new handle */
540 /* The SFT is full */
541 return INVALID_DOS_HANDLE
;
544 HANDLE
DosGetRealHandle(WORD DosHandle
)
549 /* The system PSP has no handle table */
550 if (CurrentPsp
== SYSTEM_PSP
) return INVALID_HANDLE_VALUE
;
552 /* Get a pointer to the handle table */
553 PspBlock
= SEGMENT_TO_PSP(CurrentPsp
);
554 HandleTable
= (LPBYTE
)FAR_POINTER(PspBlock
->HandleTablePtr
);
556 /* Make sure the handle is open */
557 if (HandleTable
[DosHandle
] == 0xFF) return INVALID_HANDLE_VALUE
;
559 /* Return the Win32 handle */
560 return DosSystemFileTable
[HandleTable
[DosHandle
]].Handle
;
563 static VOID
DosCopyHandleTable(LPBYTE DestinationTable
)
569 /* Clear the table first */
570 for (i
= 0; i
< 20; i
++) DestinationTable
[i
] = 0xFF;
572 /* Check if this is the initial process */
573 if (CurrentPsp
== SYSTEM_PSP
)
575 /* Set up the standard I/O devices */
576 for (i
= 0; i
<= 2; i
++)
578 /* Set the index in the SFT */
579 DestinationTable
[i
] = (BYTE
)i
;
581 /* Increase the reference count */
582 DosSystemFileTable
[i
].RefCount
++;
589 /* Get the parent PSP block and handle table */
590 PspBlock
= SEGMENT_TO_PSP(CurrentPsp
);
591 SourceTable
= (LPBYTE
)FAR_POINTER(PspBlock
->HandleTablePtr
);
593 /* Copy the first 20 handles into the new table */
594 for (i
= 0; i
< 20; i
++)
596 DestinationTable
[i
] = SourceTable
[i
];
598 /* Increase the reference count */
599 DosSystemFileTable
[SourceTable
[i
]].RefCount
++;
603 static BOOLEAN
DosResizeHandleTable(WORD NewSize
)
609 /* Get the PSP block */
610 PspBlock
= SEGMENT_TO_PSP(CurrentPsp
);
612 if (NewSize
== PspBlock
->HandleTableSize
)
618 if (PspBlock
->HandleTableSize
> 20)
620 /* Get the segment of the current table */
621 Segment
= (LOWORD(PspBlock
->HandleTablePtr
) >> 4) + HIWORD(PspBlock
->HandleTablePtr
);
625 /* Get the current handle table */
626 HandleTable
= FAR_POINTER(PspBlock
->HandleTablePtr
);
628 /* Copy it to the PSP */
629 RtlCopyMemory(PspBlock
->HandleTable
, HandleTable
, NewSize
);
631 /* Free the memory */
632 DosFreeMemory(Segment
);
634 /* Update the handle table pointer and size */
635 PspBlock
->HandleTableSize
= NewSize
;
636 PspBlock
->HandleTablePtr
= MAKELONG(0x18, CurrentPsp
);
640 /* Resize the memory */
641 if (!DosResizeMemory(Segment
, NewSize
, NULL
))
643 /* Unable to resize, try allocating it somewhere else */
644 Segment
= DosAllocateMemory(NewSize
, NULL
);
645 if (Segment
== 0) return FALSE
;
647 /* Get the new handle table */
648 HandleTable
= SEG_OFF_TO_PTR(Segment
, 0);
650 /* Copy the handles to the new table */
651 RtlCopyMemory(HandleTable
,
652 FAR_POINTER(PspBlock
->HandleTablePtr
),
653 PspBlock
->HandleTableSize
);
655 /* Update the handle table pointer */
656 PspBlock
->HandleTablePtr
= MAKELONG(0, Segment
);
659 /* Update the handle table size */
660 PspBlock
->HandleTableSize
= NewSize
;
663 else if (NewSize
> 20)
665 Segment
= DosAllocateMemory(NewSize
, NULL
);
666 if (Segment
== 0) return FALSE
;
668 /* Get the new handle table */
669 HandleTable
= SEG_OFF_TO_PTR(Segment
, 0);
671 /* Copy the handles from the PSP to the new table */
672 RtlCopyMemory(HandleTable
,
673 FAR_POINTER(PspBlock
->HandleTablePtr
),
674 PspBlock
->HandleTableSize
);
676 /* Update the handle table pointer and size */
677 PspBlock
->HandleTableSize
= NewSize
;
678 PspBlock
->HandleTablePtr
= MAKELONG(0, Segment
);
684 static BOOLEAN
DosCloseHandle(WORD DosHandle
)
690 DPRINT("DosCloseHandle: DosHandle 0x%04X\n", DosHandle
);
692 /* The system PSP has no handle table */
693 if (CurrentPsp
== SYSTEM_PSP
) return FALSE
;
695 /* Get a pointer to the handle table */
696 PspBlock
= SEGMENT_TO_PSP(CurrentPsp
);
697 HandleTable
= (LPBYTE
)FAR_POINTER(PspBlock
->HandleTablePtr
);
699 /* Make sure the handle is open */
700 if (HandleTable
[DosHandle
] == 0xFF) return FALSE
;
702 /* Decrement the reference count of the SFT entry */
703 SftIndex
= HandleTable
[DosHandle
];
704 DosSystemFileTable
[SftIndex
].RefCount
--;
706 /* Check if the reference count fell to zero */
707 if (!DosSystemFileTable
[SftIndex
].RefCount
)
709 /* Close the file, it's no longer needed */
710 CloseHandle(DosSystemFileTable
[SftIndex
].Handle
);
712 /* Clear the handle */
713 DosSystemFileTable
[SftIndex
].Handle
= INVALID_HANDLE_VALUE
;
716 /* Clear the entry in the JFT */
717 HandleTable
[DosHandle
] = 0xFF;
722 static BOOLEAN
DosDuplicateHandle(WORD OldHandle
, WORD NewHandle
)
728 DPRINT("DosDuplicateHandle: OldHandle 0x%04X, NewHandle 0x%04X\n",
732 /* The system PSP has no handle table */
733 if (CurrentPsp
== SYSTEM_PSP
) return FALSE
;
735 /* Get a pointer to the handle table */
736 PspBlock
= SEGMENT_TO_PSP(CurrentPsp
);
737 HandleTable
= (LPBYTE
)FAR_POINTER(PspBlock
->HandleTablePtr
);
739 /* Make sure the old handle is open */
740 if (HandleTable
[OldHandle
] == 0xFF) return FALSE
;
742 /* Check if the new handle is open */
743 if (HandleTable
[NewHandle
] != 0xFF)
746 DosCloseHandle(NewHandle
);
749 /* Increment the reference count of the SFT entry */
750 SftIndex
= HandleTable
[OldHandle
];
751 DosSystemFileTable
[SftIndex
].RefCount
++;
753 /* Make the new handle point to that SFT entry */
754 HandleTable
[NewHandle
] = SftIndex
;
766 static BOOLEAN
DosChangeDrive(BYTE Drive
)
768 WCHAR DirectoryPath
[DOS_CMDLINE_LENGTH
];
770 /* Make sure the drive exists */
771 if (Drive
> (LastDrive
- 'A')) return FALSE
;
773 /* Find the path to the new current directory */
774 swprintf(DirectoryPath
, L
"%c\\%S", Drive
+ 'A', CurrentDirectories
[Drive
]);
776 /* Change the current directory of the process */
777 if (!SetCurrentDirectory(DirectoryPath
)) return FALSE
;
779 /* Set the current drive */
780 CurrentDrive
= Drive
;
786 static BOOLEAN
DosChangeDirectory(LPSTR Directory
)
792 /* Make sure the directory path is not too long */
793 if (strlen(Directory
) >= DOS_DIR_LENGTH
)
795 DosLastError
= ERROR_PATH_NOT_FOUND
;
799 /* Get the drive number */
800 DriveNumber
= Directory
[0] - 'A';
802 /* Make sure the drive exists */
803 if (DriveNumber
> (LastDrive
- 'A'))
805 DosLastError
= ERROR_PATH_NOT_FOUND
;
809 /* Get the file attributes */
810 Attributes
= GetFileAttributesA(Directory
);
812 /* Make sure the path exists and is a directory */
813 if ((Attributes
== INVALID_FILE_ATTRIBUTES
)
814 || !(Attributes
& FILE_ATTRIBUTE_DIRECTORY
))
816 DosLastError
= ERROR_PATH_NOT_FOUND
;
820 /* Check if this is the current drive */
821 if (DriveNumber
== CurrentDrive
)
823 /* Change the directory */
824 if (!SetCurrentDirectoryA(Directory
))
826 DosLastError
= LOWORD(GetLastError());
831 /* Get the directory part of the path */
832 Path
= strchr(Directory
, '\\');
835 /* Skip the backslash */
839 /* Set the directory for the drive */
842 strncpy(CurrentDirectories
[DriveNumber
], Path
, DOS_DIR_LENGTH
);
846 CurrentDirectories
[DriveNumber
][0] = '\0';
853 /* PUBLIC FUNCTIONS ***********************************************************/
855 VOID
DosInitializePsp(WORD PspSegment
, LPCSTR CommandLine
, WORD ProgramSize
, WORD Environment
)
857 PDOS_PSP PspBlock
= SEGMENT_TO_PSP(PspSegment
);
858 LPDWORD IntVecTable
= (LPDWORD
)((ULONG_PTR
)BaseAddress
);
860 ZeroMemory(PspBlock
, sizeof(DOS_PSP
));
862 /* Set the exit interrupt */
863 PspBlock
->Exit
[0] = 0xCD; // int 0x20
864 PspBlock
->Exit
[1] = 0x20;
866 /* Set the number of the last paragraph */
867 PspBlock
->LastParagraph
= PspSegment
+ ProgramSize
- 1;
869 /* Save the interrupt vectors */
870 PspBlock
->TerminateAddress
= IntVecTable
[0x22];
871 PspBlock
->BreakAddress
= IntVecTable
[0x23];
872 PspBlock
->CriticalAddress
= IntVecTable
[0x24];
874 /* Set the parent PSP */
875 PspBlock
->ParentPsp
= CurrentPsp
;
877 /* Copy the parent handle table */
878 DosCopyHandleTable(PspBlock
->HandleTable
);
880 /* Set the environment block */
881 PspBlock
->EnvBlock
= Environment
;
883 /* Set the handle table pointers to the internal handle table */
884 PspBlock
->HandleTableSize
= 20;
885 PspBlock
->HandleTablePtr
= MAKELONG(0x18, PspSegment
);
887 /* Set the DOS version */
888 PspBlock
->DosVersion
= DOS_VERSION
;
890 /* Set the far call opcodes */
891 PspBlock
->FarCall
[0] = 0xCD; // int 0x21
892 PspBlock
->FarCall
[1] = 0x21;
893 PspBlock
->FarCall
[2] = 0xCB; // retf
895 /* Set the command line */
896 PspBlock
->CommandLineSize
= (BYTE
)min(strlen(CommandLine
), DOS_CMDLINE_LENGTH
- 1);
897 RtlCopyMemory(PspBlock
->CommandLine
, CommandLine
, PspBlock
->CommandLineSize
);
898 PspBlock
->CommandLine
[PspBlock
->CommandLineSize
] = '\r';
901 DWORD
DosLoadExecutable(IN DOS_EXEC_TYPE LoadType
,
902 IN LPCSTR ExecutablePath
,
903 IN LPCSTR CommandLine
,
904 IN PVOID Environment
,
905 OUT PDWORD StackLocation OPTIONAL
,
906 OUT PDWORD EntryPoint OPTIONAL
)
908 DWORD Result
= ERROR_SUCCESS
;
909 HANDLE FileHandle
= INVALID_HANDLE_VALUE
, FileMapping
= NULL
;
910 LPBYTE Address
= NULL
;
914 DWORD i
, FileSize
, ExeSize
;
915 PIMAGE_DOS_HEADER Header
;
916 PDWORD RelocationTable
;
918 LPSTR CmdLinePtr
= (LPSTR
)CommandLine
;
920 DPRINT1("DosLoadExecutable(%d, %s, %s, %s, 0x%08X, 0x%08X)\n",
928 if (LoadType
== DOS_LOAD_OVERLAY
)
930 DPRINT1("Overlay loading is not supported yet.\n");
931 return ERROR_NOT_SUPPORTED
;
934 /* NULL-terminate the command line by removing the return carriage character */
935 while (*CmdLinePtr
&& *CmdLinePtr
!= '\r') CmdLinePtr
++;
938 /* Open a handle to the executable */
939 FileHandle
= CreateFileA(ExecutablePath
,
944 FILE_ATTRIBUTE_NORMAL
,
946 if (FileHandle
== INVALID_HANDLE_VALUE
)
948 Result
= GetLastError();
952 /* Get the file size */
953 FileSize
= GetFileSize(FileHandle
, NULL
);
955 /* Create a mapping object for the file */
956 FileMapping
= CreateFileMapping(FileHandle
,
962 if (FileMapping
== NULL
)
964 Result
= GetLastError();
968 /* Map the file into memory */
969 Address
= (LPBYTE
)MapViewOfFile(FileMapping
, FILE_MAP_READ
, 0, 0, 0);
972 Result
= GetLastError();
976 /* Copy the environment block to DOS memory */
977 EnvBlock
= DosCopyEnvironmentBlock(Environment
, ExecutablePath
);
980 Result
= ERROR_NOT_ENOUGH_MEMORY
;
984 /* Check if this is an EXE file or a COM file */
985 if (Address
[0] == 'M' && Address
[1] == 'Z')
989 /* Get the MZ header */
990 Header
= (PIMAGE_DOS_HEADER
)Address
;
992 /* Get the base size of the file, in paragraphs (rounded up) */
993 ExeSize
= (((Header
->e_cp
- 1) * 512) + Header
->e_cblp
+ 0x0F) >> 4;
995 /* Add the PSP size, in paragraphs */
996 ExeSize
+= sizeof(DOS_PSP
) >> 4;
998 /* Add the maximum size that should be allocated */
999 ExeSize
+= Header
->e_maxalloc
;
1001 /* Make sure it does not pass 0xFFFF */
1002 if (ExeSize
> 0xFFFF) ExeSize
= 0xFFFF;
1004 /* Reduce the size one by one until the allocation is successful */
1005 for (i
= Header
->e_maxalloc
; i
>= Header
->e_minalloc
; i
--, ExeSize
--)
1007 /* Try to allocate that much memory */
1008 Segment
= DosAllocateMemory((WORD
)ExeSize
, NULL
);
1009 if (Segment
!= 0) break;
1012 /* Check if at least the lowest allocation was successful */
1015 Result
= ERROR_NOT_ENOUGH_MEMORY
;
1019 /* Initialize the PSP */
1020 DosInitializePsp(Segment
,
1025 /* The process owns its own memory */
1026 DosChangeMemoryOwner(Segment
, Segment
);
1027 DosChangeMemoryOwner(EnvBlock
, Segment
);
1029 /* Copy the program to Segment:0100 */
1030 RtlCopyMemory(SEG_OFF_TO_PTR(Segment
, 0x100),
1031 Address
+ (Header
->e_cparhdr
<< 4),
1032 min(FileSize
- (Header
->e_cparhdr
<< 4),
1033 (ExeSize
<< 4) - sizeof(DOS_PSP
)));
1035 /* Get the relocation table */
1036 RelocationTable
= (PDWORD
)(Address
+ Header
->e_lfarlc
);
1038 /* Perform relocations */
1039 for (i
= 0; i
< Header
->e_crlc
; i
++)
1041 /* Get a pointer to the word that needs to be patched */
1042 RelocWord
= (PWORD
)SEG_OFF_TO_PTR(Segment
+ HIWORD(RelocationTable
[i
]),
1043 0x100 + LOWORD(RelocationTable
[i
]));
1045 /* Add the number of the EXE segment to it */
1046 *RelocWord
+= Segment
+ (sizeof(DOS_PSP
) >> 4);
1049 if (LoadType
== DOS_LOAD_AND_EXECUTE
)
1051 /* Set the initial segment registers */
1055 /* Set the stack to the location from the header */
1056 EmulatorSetStack(Segment
+ (sizeof(DOS_PSP
) >> 4) + Header
->e_ss
,
1060 CurrentPsp
= Segment
;
1061 DiskTransferArea
= MAKELONG(0x80, Segment
);
1062 EmulatorExecute(Segment
+ Header
->e_cs
+ (sizeof(DOS_PSP
) >> 4),
1070 /* Find the maximum amount of memory that can be allocated */
1071 DosAllocateMemory(0xFFFF, &MaxAllocSize
);
1073 /* Make sure it's enough for the whole program and the PSP */
1074 if (((DWORD
)MaxAllocSize
<< 4) < (FileSize
+ sizeof(DOS_PSP
)))
1076 Result
= ERROR_NOT_ENOUGH_MEMORY
;
1080 /* Allocate all of it */
1081 Segment
= DosAllocateMemory(MaxAllocSize
, NULL
);
1084 Result
= ERROR_ARENA_TRASHED
;
1088 /* The process owns its own memory */
1089 DosChangeMemoryOwner(Segment
, Segment
);
1090 DosChangeMemoryOwner(EnvBlock
, Segment
);
1092 /* Copy the program to Segment:0100 */
1093 RtlCopyMemory(SEG_OFF_TO_PTR(Segment
, 0x100),
1097 /* Initialize the PSP */
1098 DosInitializePsp(Segment
,
1103 if (LoadType
== DOS_LOAD_AND_EXECUTE
)
1105 /* Set the initial segment registers */
1109 /* Set the stack to the last word of the segment */
1110 EmulatorSetStack(Segment
, 0xFFFE);
1113 * Set the value on the stack to 0, so that a near return
1114 * jumps to PSP:0000 which has the exit code.
1116 *((LPWORD
)SEG_OFF_TO_PTR(Segment
, 0xFFFE)) = 0;
1119 CurrentPsp
= Segment
;
1120 DiskTransferArea
= MAKELONG(0x80, Segment
);
1121 EmulatorExecute(Segment
, 0x100);
1126 if (Result
!= ERROR_SUCCESS
)
1128 /* It was not successful, cleanup the DOS memory */
1129 if (EnvBlock
) DosFreeMemory(EnvBlock
);
1130 if (Segment
) DosFreeMemory(Segment
);
1134 if (Address
!= NULL
) UnmapViewOfFile(Address
);
1136 /* Close the file mapping object */
1137 if (FileMapping
!= NULL
) CloseHandle(FileMapping
);
1139 /* Close the file handle */
1140 if (FileHandle
!= INVALID_HANDLE_VALUE
) CloseHandle(FileHandle
);
1145 DWORD
DosStartProcess(IN LPCSTR ExecutablePath
,
1146 IN LPCSTR CommandLine
,
1147 IN PVOID Environment
)
1151 Result
= DosLoadExecutable(DOS_LOAD_AND_EXECUTE
,
1158 if (Result
!= ERROR_SUCCESS
) goto Quit
;
1160 /* Attach to the console */
1161 VidBiosAttachToConsole(); // FIXME: And in fact, attach the full NTVDM UI to the console
1163 /* Start simulation */
1164 SetEvent(VdmTaskEvent
);
1167 /* Detach from the console */
1168 VidBiosDetachFromConsole(); // FIXME: And in fact, detach the full NTVDM UI from the console
1175 WORD
DosCreateProcess(DOS_EXEC_TYPE LoadType
,
1177 PDOS_EXEC_PARAM_BLOCK Parameters
)
1181 LPVOID Environment
= NULL
;
1182 VDM_COMMAND_INFO CommandInfo
;
1183 CHAR CmdLine
[MAX_PATH
];
1184 CHAR AppName
[MAX_PATH
];
1185 CHAR PifFile
[MAX_PATH
];
1186 CHAR Desktop
[MAX_PATH
];
1187 CHAR Title
[MAX_PATH
];
1189 STARTUPINFOA StartupInfo
;
1190 PROCESS_INFORMATION ProcessInfo
;
1192 /* Get the binary type */
1193 if (!GetBinaryTypeA(ProgramName
, &BinaryType
)) return GetLastError();
1195 /* Did the caller specify an environment segment? */
1196 if (Parameters
->Environment
)
1198 /* Yes, use it instead of the parent one */
1199 Environment
= SEG_OFF_TO_PTR(Parameters
->Environment
, 0);
1202 /* Set up the startup info structure */
1203 ZeroMemory(&StartupInfo
, sizeof(STARTUPINFOA
));
1204 StartupInfo
.cb
= sizeof(STARTUPINFOA
);
1206 /* Create the process */
1207 if (!CreateProcessA(ProgramName
,
1208 FAR_POINTER(Parameters
->CommandLine
),
1218 return GetLastError();
1221 /* Check the type of the program */
1224 /* These are handled by NTVDM */
1225 case SCS_DOS_BINARY
:
1226 case SCS_WOW_BINARY
:
1228 /* Clear the structure */
1229 ZeroMemory(&CommandInfo
, sizeof(CommandInfo
));
1231 /* Initialize the structure members */
1232 CommandInfo
.TaskId
= SessionId
;
1233 CommandInfo
.VDMState
= VDM_FLAG_NESTED_TASK
| VDM_FLAG_DONT_WAIT
;
1234 CommandInfo
.CmdLine
= CmdLine
;
1235 CommandInfo
.CmdLen
= sizeof(CmdLine
);
1236 CommandInfo
.AppName
= AppName
;
1237 CommandInfo
.AppLen
= sizeof(AppName
);
1238 CommandInfo
.PifFile
= PifFile
;
1239 CommandInfo
.PifLen
= sizeof(PifFile
);
1240 CommandInfo
.Desktop
= Desktop
;
1241 CommandInfo
.DesktopLen
= sizeof(Desktop
);
1242 CommandInfo
.Title
= Title
;
1243 CommandInfo
.TitleLen
= sizeof(Title
);
1244 CommandInfo
.Env
= Env
;
1245 CommandInfo
.EnvLen
= sizeof(Env
);
1247 /* Get the VDM command information */
1248 if (!GetNextVDMCommand(&CommandInfo
))
1250 /* Shouldn't happen */
1254 /* Increment the re-entry count */
1255 CommandInfo
.VDMState
= VDM_INC_REENTER_COUNT
;
1256 GetNextVDMCommand(&CommandInfo
);
1258 /* Load the executable */
1259 Result
= DosLoadExecutable(LoadType
,
1263 &Parameters
->StackLocation
,
1264 &Parameters
->EntryPoint
);
1265 if (Result
!= ERROR_SUCCESS
)
1267 DisplayMessage(L
"Could not load '%S'. Error: %u", AppName
, Result
);
1268 // FIXME: Decrement the reenter count. Or, instead, just increment
1269 // the VDM reenter count *only* if this call succeeds...
1275 /* Not handled by NTVDM */
1278 /* Wait for the process to finish executing */
1279 WaitForSingleObject(ProcessInfo
.hProcess
, INFINITE
);
1283 /* Close the handles */
1284 CloseHandle(ProcessInfo
.hProcess
);
1285 CloseHandle(ProcessInfo
.hThread
);
1287 return ERROR_SUCCESS
;
1291 VOID
DosTerminateProcess(WORD Psp
, BYTE ReturnCode
)
1294 WORD McbSegment
= FIRST_MCB_SEGMENT
;
1295 PDOS_MCB CurrentMcb
;
1296 LPDWORD IntVecTable
= (LPDWORD
)((ULONG_PTR
)BaseAddress
);
1297 PDOS_PSP PspBlock
= SEGMENT_TO_PSP(Psp
);
1299 DPRINT("DosTerminateProcess: Psp 0x%04X, ReturnCode 0x%02X\n",
1303 /* Check if this PSP is it's own parent */
1304 if (PspBlock
->ParentPsp
== Psp
) goto Done
;
1306 for (i
= 0; i
< PspBlock
->HandleTableSize
; i
++)
1308 /* Close the handle */
1312 /* Free the memory used by the process */
1315 /* Get a pointer to the MCB */
1316 CurrentMcb
= SEGMENT_TO_MCB(McbSegment
);
1318 /* Make sure the MCB is valid */
1319 if (CurrentMcb
->BlockType
!= 'M' && CurrentMcb
->BlockType
!='Z') break;
1321 /* If this block was allocated by the process, free it */
1322 if (CurrentMcb
->OwnerPsp
== Psp
) DosFreeMemory(McbSegment
+ 1);
1324 /* If this was the last block, quit */
1325 if (CurrentMcb
->BlockType
== 'Z') break;
1327 /* Update the segment and continue */
1328 McbSegment
+= CurrentMcb
->Size
+ 1;
1332 /* Restore the interrupt vectors */
1333 IntVecTable
[0x22] = PspBlock
->TerminateAddress
;
1334 IntVecTable
[0x23] = PspBlock
->BreakAddress
;
1335 IntVecTable
[0x24] = PspBlock
->CriticalAddress
;
1337 /* Update the current PSP */
1338 if (Psp
== CurrentPsp
)
1340 CurrentPsp
= PspBlock
->ParentPsp
;
1341 if (CurrentPsp
== SYSTEM_PSP
)
1343 ResetEvent(VdmTaskEvent
);
1344 EmulatorUnsimulate();
1349 // FIXME: This is probably not the best way to do it
1350 /* Check if this was a nested DOS task */
1351 if (CurrentPsp
!= SYSTEM_PSP
)
1353 VDM_COMMAND_INFO CommandInfo
;
1355 /* Decrement the re-entry count */
1356 CommandInfo
.TaskId
= SessionId
;
1357 CommandInfo
.VDMState
= VDM_DEC_REENTER_COUNT
;
1358 GetNextVDMCommand(&CommandInfo
);
1360 /* Clear the structure */
1361 ZeroMemory(&CommandInfo
, sizeof(CommandInfo
));
1363 /* Update the VDM state of the task */
1364 CommandInfo
.TaskId
= SessionId
;
1365 CommandInfo
.VDMState
= VDM_FLAG_DONT_WAIT
;
1366 GetNextVDMCommand(&CommandInfo
);
1370 /* Save the return code - Normal termination */
1371 DosErrorLevel
= MAKEWORD(ReturnCode
, 0x00);
1373 /* Return control to the parent process */
1374 EmulatorExecute(HIWORD(PspBlock
->TerminateAddress
),
1375 LOWORD(PspBlock
->TerminateAddress
));
1378 BOOLEAN
DosHandleIoctl(BYTE ControlCode
, WORD FileHandle
)
1380 HANDLE Handle
= DosGetRealHandle(FileHandle
);
1382 if (Handle
== INVALID_HANDLE_VALUE
)
1385 DosLastError
= ERROR_FILE_NOT_FOUND
;
1389 switch (ControlCode
)
1391 /* Get Device Information */
1397 * See Ralf Brown: http://www.ctyme.com/intr/rb-2820.htm
1398 * for a list of possible flags.
1401 if (Handle
== DosSystemFileTable
[DOS_INPUT_HANDLE
].Handle
)
1406 else if (Handle
== DosSystemFileTable
[DOS_OUTPUT_HANDLE
].Handle
)
1408 /* Console output */
1412 /* It is a device */
1415 /* Return the device information word */
1420 /* Unsupported control code */
1423 DPRINT1("Unsupported IOCTL: 0x%02X\n", ControlCode
);
1425 DosLastError
= ERROR_INVALID_PARAMETER
;
1431 VOID WINAPI
DosInt20h(LPWORD Stack
)
1433 /* This is the exit interrupt */
1434 DosTerminateProcess(Stack
[STACK_CS
], 0);
1437 VOID WINAPI
DosInt21h(LPWORD Stack
)
1440 SYSTEMTIME SystemTime
;
1442 PDOS_INPUT_BUFFER InputBuffer
;
1444 /* Check the value in the AH register */
1447 /* Terminate Program */
1450 DosTerminateProcess(Stack
[STACK_CS
], 0);
1454 /* Read Character from STDIN with Echo */
1457 DPRINT("INT 21h, AH = 01h\n");
1459 // FIXME: Under DOS 2+, input / output handle may be redirected!!!!
1461 Character
= DosReadCharacter(DOS_INPUT_HANDLE
);
1464 // FIXME: Check whether Ctrl-C / Ctrl-Break is pressed, and call INT 23h if so.
1465 // Check also Ctrl-P and set echo-to-printer flag.
1466 // Ctrl-Z is not interpreted.
1472 /* Write Character to STDOUT */
1475 // FIXME: Under DOS 2+, output handle may be redirected!!!!
1476 Character
= getDL();
1477 DosPrintCharacter(DOS_OUTPUT_HANDLE
, Character
);
1480 * We return the output character (DOS 2.1+).
1481 * Also, if we're going to output a TAB, then
1482 * don't return a TAB but a SPACE instead.
1483 * See Ralf Brown: http://www.ctyme.com/intr/rb-2554.htm
1484 * for more information.
1486 setAL(Character
== '\t' ? ' ' : Character
);
1490 /* Read Character from STDAUX */
1493 // FIXME: Really read it from STDAUX!
1494 DPRINT1("INT 16h, 03h: Read character from STDAUX is HALFPLEMENTED\n");
1495 // setAL(DosReadCharacter());
1499 /* Write Character to STDAUX */
1502 // FIXME: Really write it to STDAUX!
1503 DPRINT1("INT 16h, 04h: Write character to STDAUX is HALFPLEMENTED\n");
1504 // DosPrintCharacter(getDL());
1508 /* Write Character to Printer */
1511 // FIXME: Really write it to printer!
1512 DPRINT1("INT 16h, 05h: Write character to printer is HALFPLEMENTED -\n\n");
1513 DPRINT1("0x%p\n", getDL());
1514 DPRINT1("\n\n-----------\n\n");
1518 /* Direct Console I/O */
1521 Character
= getDL();
1523 // FIXME: Under DOS 2+, output handle may be redirected!!!!
1525 if (Character
!= 0xFF)
1528 DosPrintCharacter(DOS_OUTPUT_HANDLE
, Character
);
1531 * We return the output character (DOS 2.1+).
1532 * See Ralf Brown: http://www.ctyme.com/intr/rb-2558.htm
1533 * for more information.
1540 if (DosCheckInput())
1542 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_ZF
;
1543 setAL(DosReadCharacter(DOS_INPUT_HANDLE
));
1547 /* No character available */
1548 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_ZF
;
1556 /* Character Input without Echo */
1560 DPRINT("Char input without echo\n");
1562 // FIXME: Under DOS 2+, input handle may be redirected!!!!
1563 Character
= DosReadCharacter(DOS_INPUT_HANDLE
);
1565 // FIXME: For 0x07, do not check Ctrl-C/Break.
1566 // For 0x08, do check those control sequences and if needed,
1569 // /* Let the BOP repeat if needed */
1570 // if (getCF()) break;
1576 /* Write string to STDOUT */
1579 String
= (PCHAR
)SEG_OFF_TO_PTR(getDS(), getDX());
1581 while (*String
!= '$')
1583 DosPrintCharacter(DOS_OUTPUT_HANDLE
, *String
);
1588 * We return the terminating character (DOS 2.1+).
1589 * See Ralf Brown: http://www.ctyme.com/intr/rb-2562.htm
1590 * for more information.
1592 setAL('$'); // *String
1596 /* Read Buffered Input */
1600 InputBuffer
= (PDOS_INPUT_BUFFER
)SEG_OFF_TO_PTR(getDS(), getDX());
1602 DPRINT("Read Buffered Input\n");
1604 while (Count
< InputBuffer
->MaxLength
)
1606 // FIXME!! This function should interpret backspaces etc...
1608 /* Try to read a character (wait) */
1609 Character
= DosReadCharacter(DOS_INPUT_HANDLE
);
1611 // FIXME: Check whether Ctrl-C / Ctrl-Break is pressed, and call INT 23h if so.
1613 /* Echo the character and append it to the buffer */
1614 DosPrintCharacter(DOS_OUTPUT_HANDLE
, Character
);
1615 InputBuffer
->Buffer
[Count
] = Character
;
1617 Count
++; /* Carriage returns are also counted */
1619 if (Character
== '\r') break;
1622 /* Update the length */
1623 InputBuffer
->Length
= Count
;
1628 /* Get STDIN Status */
1631 setAL(DosCheckInput() ? 0xFF : 0x00);
1635 /* Flush Buffer and Read STDIN */
1638 BYTE InputFunction
= getAL();
1640 /* Flush STDIN buffer */
1641 DosFlushFileBuffers(DOS_INPUT_HANDLE
);
1644 * If the input function number contained in AL is valid, i.e.
1645 * AL == 0x01 or 0x06 or 0x07 or 0x08 or 0x0A, call ourselves
1646 * recursively with AL == AH.
1648 if (InputFunction
== 0x01 || InputFunction
== 0x06 ||
1649 InputFunction
== 0x07 || InputFunction
== 0x08 ||
1650 InputFunction
== 0x0A)
1652 /* Call ourselves recursively */
1653 setAH(InputFunction
);
1662 PDOS_PSP PspBlock
= SEGMENT_TO_PSP(CurrentPsp
);
1664 // TODO: Flush what's needed.
1665 DPRINT1("INT 21h, 0Dh is UNIMPLEMENTED\n");
1667 /* Clear CF in DOS 6 only */
1668 if (PspBlock
->DosVersion
== 0x0006)
1669 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
1674 /* Set Default Drive */
1677 DosChangeDrive(getDL());
1678 setAL(LastDrive
- 'A' + 1);
1682 /* NULL Function for CP/M Compatibility */
1686 * This function corresponds to the CP/M BDOS function
1687 * "get bit map of logged drives", which is meaningless
1690 * For: PTS-DOS 6.51 & S/DOS 1.0 - EXTENDED RENAME FILE USING FCB
1691 * See Ralf Brown: http://www.ctyme.com/intr/rb-2584.htm
1692 * for more information.
1698 /* Get Default Drive */
1701 setAL(CurrentDrive
);
1705 /* Set Disk Transfer Area */
1708 DiskTransferArea
= MAKELONG(getDX(), getDS());
1712 /* NULL Function for CP/M Compatibility */
1717 * Function 0x1D corresponds to the CP/M BDOS function
1718 * "get bit map of read-only drives", which is meaningless
1720 * See Ralf Brown: http://www.ctyme.com/intr/rb-2592.htm
1721 * for more information.
1723 * Function 0x1E corresponds to the CP/M BDOS function
1724 * "set file attributes", which was meaningless under MS-DOS 1.x.
1725 * See Ralf Brown: http://www.ctyme.com/intr/rb-2593.htm
1726 * for more information.
1732 /* NULL Function for CP/M Compatibility */
1736 * This function corresponds to the CP/M BDOS function
1737 * "get/set default user (sublibrary) number", which is meaningless
1740 * For: S/DOS 1.0+ & PTS-DOS 6.51+ - GET OEM REVISION
1741 * See Ralf Brown: http://www.ctyme.com/intr/rb-2596.htm
1742 * for more information.
1748 /* Set Interrupt Vector */
1751 ULONG FarPointer
= MAKELONG(getDX(), getDS());
1752 DPRINT1("Setting interrupt 0x%x ...\n", getAL());
1754 /* Write the new far pointer to the IDT */
1755 ((PULONG
)BaseAddress
)[getAL()] = FarPointer
;
1759 /* Create New PSP */
1762 DPRINT1("INT 21h, AH = 26h - Create New PSP is UNIMPLEMENTED\n");
1766 /* Get System Date */
1769 GetLocalTime(&SystemTime
);
1770 setCX(SystemTime
.wYear
);
1771 setDX(MAKEWORD(SystemTime
.wDay
, SystemTime
.wMonth
));
1772 setAL(SystemTime
.wDayOfWeek
);
1776 /* Set System Date */
1779 GetLocalTime(&SystemTime
);
1780 SystemTime
.wYear
= getCX();
1781 SystemTime
.wMonth
= getDH();
1782 SystemTime
.wDay
= getDL();
1784 /* Return success or failure */
1785 setAL(SetLocalTime(&SystemTime
) ? 0x00 : 0xFF);
1789 /* Get System Time */
1792 GetLocalTime(&SystemTime
);
1793 setCX(MAKEWORD(SystemTime
.wMinute
, SystemTime
.wHour
));
1794 setDX(MAKEWORD(SystemTime
.wMilliseconds
/ 10, SystemTime
.wSecond
));
1798 /* Set System Time */
1801 GetLocalTime(&SystemTime
);
1802 SystemTime
.wHour
= getCH();
1803 SystemTime
.wMinute
= getCL();
1804 SystemTime
.wSecond
= getDH();
1805 SystemTime
.wMilliseconds
= getDL() * 10; // In hundredths of seconds
1807 /* Return success or failure */
1808 setAL(SetLocalTime(&SystemTime
) ? 0x00 : 0xFF);
1812 /* Get Disk Transfer Area */
1815 setES(HIWORD(DiskTransferArea
));
1816 setBX(LOWORD(DiskTransferArea
));
1820 /* Get DOS Version */
1823 PDOS_PSP PspBlock
= SEGMENT_TO_PSP(CurrentPsp
);
1826 * DOS 2+ - GET DOS VERSION
1827 * See Ralf Brown: http://www.ctyme.com/intr/rb-2711.htm
1828 * for more information.
1831 if (LOBYTE(PspBlock
->DosVersion
) < 5 || getAL() == 0x00)
1834 * Return DOS OEM number:
1835 * 0x00 for IBM PC-DOS
1836 * 0x02 for packaged MS-DOS
1841 if (LOBYTE(PspBlock
->DosVersion
) >= 5 && getAL() == 0x01)
1844 * Return version flag:
1845 * 1 << 3 if DOS is in ROM,
1846 * 0 (reserved) if not.
1851 /* Return DOS 24-bit user serial number in BL:CX */
1856 * Return DOS version: Minor:Major in AH:AL
1857 * The Windows NT DOS box returns version 5.00, subject to SETVER.
1859 setAX(PspBlock
->DosVersion
);
1864 /* Extended functionalities */
1867 if (getAL() == 0x06)
1870 * DOS 5+ - GET TRUE VERSION NUMBER
1871 * This function always returns the true version number, unlike
1872 * AH=30h, whose return value may be changed with SETVER.
1873 * See Ralf Brown: http://www.ctyme.com/intr/rb-2730.htm
1874 * for more information.
1878 * Return the true DOS version: Minor:Major in BH:BL
1879 * The Windows NT DOS box returns BX=3205h (version 5.50).
1881 setBX(NTDOS_VERSION
);
1883 /* DOS revision 0 */
1891 // /* Invalid subfunction */
1898 /* Get Interrupt Vector */
1901 DWORD FarPointer
= ((PDWORD
)BaseAddress
)[getAL()];
1903 /* Read the address from the IDT into ES:BX */
1904 setES(HIWORD(FarPointer
));
1905 setBX(LOWORD(FarPointer
));
1909 /* SWITCH character - AVAILDEV */
1912 if (getAL() == 0x00)
1915 * DOS 2+ - "SWITCHAR" - GET SWITCH CHARACTER
1916 * This setting is ignored by MS-DOS 4.0+.
1917 * MS-DOS 5+ always return AL=00h/DL=2Fh.
1918 * See Ralf Brown: http://www.ctyme.com/intr/rb-2752.htm
1919 * for more information.
1924 else if (getAL() == 0x01)
1927 * DOS 2+ - "SWITCHAR" - SET SWITCH CHARACTER
1928 * This setting is ignored by MS-DOS 5+.
1929 * See Ralf Brown: http://www.ctyme.com/intr/rb-2753.htm
1930 * for more information.
1935 else if (getAL() == 0x02)
1938 * DOS 2.x and 3.3+ only - "AVAILDEV" - SPECIFY \DEV\ PREFIX USE
1939 * See Ralf Brown: http://www.ctyme.com/intr/rb-2754.htm
1940 * for more information.
1945 else if (getAL() == 0x03)
1948 * DOS 2.x and 3.3+ only - "AVAILDEV" - SPECIFY \DEV\ PREFIX USE
1949 * See Ralf Brown: http://www.ctyme.com/intr/rb-2754.htm
1950 * for more information.
1957 /* Invalid subfunction */
1964 /* Create Directory */
1967 String
= (PCHAR
)SEG_OFF_TO_PTR(getDS(), getDX());
1969 if (CreateDirectoryA(String
, NULL
))
1971 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
1975 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
1976 setAX(LOWORD(GetLastError()));
1982 /* Remove Directory */
1985 String
= (PCHAR
)SEG_OFF_TO_PTR(getDS(), getDX());
1987 if (RemoveDirectoryA(String
))
1989 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
1993 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
1994 setAX(LOWORD(GetLastError()));
2000 /* Set Current Directory */
2003 String
= (PCHAR
)SEG_OFF_TO_PTR(getDS(), getDX());
2005 if (DosChangeDirectory(String
))
2007 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2011 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2012 setAX(DosLastError
);
2018 /* Create or Truncate File */
2022 WORD ErrorCode
= DosCreateFile(&FileHandle
,
2023 (LPCSTR
)SEG_OFF_TO_PTR(getDS(), getDX()),
2027 if (ErrorCode
== ERROR_SUCCESS
)
2029 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2034 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2045 WORD ErrorCode
= DosOpenFile(&FileHandle
,
2046 (LPCSTR
)SEG_OFF_TO_PTR(getDS(), getDX()),
2049 if (ErrorCode
== ERROR_SUCCESS
)
2051 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2056 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2066 if (DosCloseHandle(getBX()))
2068 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2072 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2073 setAX(ERROR_INVALID_HANDLE
);
2079 /* Read from File or Device */
2085 DPRINT("INT 21h, AH = 3Fh\n");
2088 ErrorCode
= DosReadFile(getBX(),
2089 SEG_OFF_TO_PTR(getDS(), getDX()),
2094 if (ErrorCode
== ERROR_SUCCESS
)
2096 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2099 else if (ErrorCode
!= ERROR_NOT_READY
)
2101 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2108 /* Write to File or Device */
2111 WORD BytesWritten
= 0;
2112 WORD ErrorCode
= DosWriteFile(getBX(),
2113 SEG_OFF_TO_PTR(getDS(), getDX()),
2117 if (ErrorCode
== ERROR_SUCCESS
)
2119 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2120 setAX(BytesWritten
);
2124 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2134 LPSTR FileName
= (LPSTR
)SEG_OFF_TO_PTR(getDS(), getDX());
2136 if (demFileDelete(FileName
) == ERROR_SUCCESS
)
2138 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2140 * See Ralf Brown: http://www.ctyme.com/intr/rb-2797.htm
2141 * "AX destroyed (DOS 3.3) AL seems to be drive of deleted file."
2143 setAL(FileName
[0] - 'A');
2147 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2148 setAX(GetLastError());
2158 WORD ErrorCode
= DosSeekFile(getBX(),
2159 MAKELONG(getDX(), getCX()),
2163 if (ErrorCode
== ERROR_SUCCESS
)
2165 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2167 /* Return the new offset in DX:AX */
2168 setDX(HIWORD(NewLocation
));
2169 setAX(LOWORD(NewLocation
));
2173 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2180 /* Get/Set File Attributes */
2184 LPSTR FileName
= (LPSTR
)SEG_OFF_TO_PTR(getDS(), getDX());
2186 if (getAL() == 0x00)
2188 /* Get the attributes */
2189 Attributes
= GetFileAttributesA(FileName
);
2191 /* Check if it failed */
2192 if (Attributes
== INVALID_FILE_ATTRIBUTES
)
2194 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2195 setAX(GetLastError());
2199 /* Return the attributes that DOS can understand */
2200 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2201 setCX(Attributes
& 0x00FF);
2204 else if (getAL() == 0x01)
2206 /* Try to set the attributes */
2207 if (SetFileAttributesA(FileName
, getCL()))
2209 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2213 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2214 setAX(GetLastError());
2219 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2220 setAX(ERROR_INVALID_FUNCTION
);
2229 if (DosHandleIoctl(getAL(), getBX()))
2231 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2235 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2236 setAX(DosLastError
);
2242 /* Duplicate Handle */
2246 HANDLE Handle
= DosGetRealHandle(getBX());
2248 if (Handle
== INVALID_HANDLE_VALUE
)
2250 /* The handle is invalid */
2251 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2252 setAX(ERROR_INVALID_HANDLE
);
2256 /* Open a new handle to the same entry */
2257 NewHandle
= DosOpenHandle(Handle
);
2259 if (NewHandle
== INVALID_DOS_HANDLE
)
2261 /* Too many files open */
2262 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2263 setAX(ERROR_TOO_MANY_OPEN_FILES
);
2267 /* Return the result */
2268 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2273 /* Force Duplicate Handle */
2276 if (DosDuplicateHandle(getBX(), getCX()))
2278 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2282 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2283 setAX(ERROR_INVALID_HANDLE
);
2289 /* Get Current Directory */
2292 BYTE DriveNumber
= getDL();
2293 String
= (PCHAR
)SEG_OFF_TO_PTR(getDS(), getSI());
2295 /* Get the real drive number */
2296 if (DriveNumber
== 0)
2298 DriveNumber
= CurrentDrive
;
2302 /* Decrement DriveNumber since it was 1-based */
2306 if (DriveNumber
<= LastDrive
- 'A')
2309 * Copy the current directory into the target buffer.
2310 * It doesn't contain the drive letter and the backslash.
2312 strncpy(String
, CurrentDirectories
[DriveNumber
], DOS_DIR_LENGTH
);
2313 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2314 setAX(0x0100); // Undocumented, see Ralf Brown: http://www.ctyme.com/intr/rb-2933.htm
2318 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2319 setAX(ERROR_INVALID_DRIVE
);
2325 /* Allocate Memory */
2328 WORD MaxAvailable
= 0;
2329 WORD Segment
= DosAllocateMemory(getBX(), &MaxAvailable
);
2333 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2338 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2339 setAX(DosLastError
);
2340 setBX(MaxAvailable
);
2349 if (DosFreeMemory(getES()))
2351 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2355 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2356 setAX(ERROR_ARENA_TRASHED
);
2362 /* Resize Memory Block */
2367 if (DosResizeMemory(getES(), getBX(), &Size
))
2369 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2373 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2374 setAX(DosLastError
);
2385 DOS_EXEC_TYPE LoadType
= (DOS_EXEC_TYPE
)getAL();
2386 LPSTR ProgramName
= SEG_OFF_TO_PTR(getDS(), getDX());
2387 PDOS_EXEC_PARAM_BLOCK ParamBlock
= SEG_OFF_TO_PTR(getES(), getBX());
2388 WORD ErrorCode
= DosCreateProcess(LoadType
, ProgramName
, ParamBlock
);
2390 if (ErrorCode
== ERROR_SUCCESS
)
2392 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2396 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2404 /* Terminate With Return Code */
2407 DosTerminateProcess(CurrentPsp
, getAL());
2411 /* Get Return Code (ERRORLEVEL) */
2415 * According to Ralf Brown: http://www.ctyme.com/intr/rb-2976.htm
2416 * DosErrorLevel is cleared after being read by this function.
2418 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2419 setAX(DosErrorLevel
);
2420 DosErrorLevel
= 0x0000; // Clear it
2424 /* Find First File */
2427 WORD Result
= (WORD
)demFileFindFirst(FAR_POINTER(DiskTransferArea
),
2428 SEG_OFF_TO_PTR(getDS(), getDX()),
2433 if (Result
== ERROR_SUCCESS
)
2434 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2436 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2441 /* Find Next File */
2444 WORD Result
= (WORD
)demFileFindNext(FAR_POINTER(DiskTransferArea
));
2448 if (Result
== ERROR_SUCCESS
)
2449 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2451 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2456 /* Internal - Set Current Process ID (Set PSP Address) */
2459 // FIXME: Is it really what it's done ??
2460 CurrentPsp
= getBX();
2464 /* Internal - Get Current Process ID (Get PSP Address) */
2466 /* Get Current PSP Address */
2470 * Undocumented AH=51h is identical to the documented AH=62h.
2471 * See Ralf Brown: http://www.ctyme.com/intr/rb-2982.htm
2472 * and http://www.ctyme.com/intr/rb-3140.htm
2473 * for more information.
2479 /* Internal - Get "List of lists" (SYSVARS) */
2483 * On return, ES points at the DOS data segment (see also INT 2F/AX=1203h).
2484 * See Ralf Brown: http://www.ctyme.com/intr/rb-2983.htm
2485 * for more information.
2488 /* Return the DOS "list of lists" in ES:BX */
2492 DisplayMessage(L
"Required for AARD code, do you remember? :P");
2499 LPSTR ExistingFileName
= (LPSTR
)SEG_OFF_TO_PTR(getDS(), getDX());
2500 LPSTR NewFileName
= (LPSTR
)SEG_OFF_TO_PTR(getES(), getDI());
2503 * See Ralf Brown: http://www.ctyme.com/intr/rb-2990.htm
2504 * for more information.
2507 if (MoveFileA(ExistingFileName
, NewFileName
))
2509 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2513 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2514 setAX(GetLastError());
2520 /* Get/Set Memory Management Options */
2523 if (getAL() == 0x00)
2525 /* Get allocation strategy */
2526 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2527 setAX(DosAllocStrategy
);
2529 else if (getAL() == 0x01)
2531 /* Set allocation strategy */
2533 if ((getBL() & (DOS_ALLOC_HIGH
| DOS_ALLOC_HIGH_LOW
))
2534 == (DOS_ALLOC_HIGH
| DOS_ALLOC_HIGH_LOW
))
2536 /* Can't set both */
2537 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2538 setAX(ERROR_INVALID_PARAMETER
);
2542 if ((getBL() & 0x3F) > DOS_ALLOC_LAST_FIT
)
2544 /* Invalid allocation strategy */
2545 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2546 setAX(ERROR_INVALID_PARAMETER
);
2550 DosAllocStrategy
= getBL();
2551 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2553 else if (getAL() == 0x02)
2555 /* Get UMB link state */
2556 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2557 setAL(DosUmbLinked
? 0x01 : 0x00);
2559 else if (getAL() == 0x03)
2561 /* Set UMB link state */
2562 if (getBX()) DosLinkUmb();
2563 else DosUnlinkUmb();
2564 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2568 /* Invalid or unsupported function */
2569 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2570 setAX(ERROR_INVALID_FUNCTION
);
2576 /* Create Temporary File */
2579 LPSTR PathName
= (LPSTR
)SEG_OFF_TO_PTR(getDS(), getDX());
2580 LPSTR FileName
= PathName
; // The buffer for the path and the full file name is the same.
2586 * See Ralf Brown: http://www.ctyme.com/intr/rb-3014.htm
2587 * for more information.
2590 // FIXME: Check for buffer validity?
2591 // It should be a ASCIZ path ending with a '\' + 13 zero bytes
2592 // to receive the generated filename.
2594 /* First create the temporary file */
2595 uRetVal
= GetTempFileNameA(PathName
, NULL
, 0, FileName
);
2598 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2599 setAX(GetLastError());
2603 /* Now try to open it in read/write access */
2604 ErrorCode
= DosOpenFile(&FileHandle
, FileName
, 2);
2605 if (ErrorCode
== ERROR_SUCCESS
)
2607 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2612 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2619 /* Create New File */
2623 WORD ErrorCode
= DosCreateFile(&FileHandle
,
2624 (LPCSTR
)SEG_OFF_TO_PTR(getDS(), getDX()),
2628 if (ErrorCode
== ERROR_SUCCESS
)
2630 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2635 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2642 /* Lock/Unlock Region of File */
2645 HANDLE Handle
= DosGetRealHandle(getBX());
2647 if (Handle
== INVALID_HANDLE_VALUE
)
2649 /* The handle is invalid */
2650 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2651 setAX(ERROR_INVALID_HANDLE
);
2655 if (getAL() == 0x00)
2657 /* Lock region of file */
2658 if (LockFile(Handle
,
2659 MAKELONG(getCX(), getDX()), 0,
2660 MAKELONG(getSI(), getDI()), 0))
2662 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2666 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2667 setAX(GetLastError());
2670 else if (getAL() == 0x01)
2672 /* Unlock region of file */
2673 if (UnlockFile(Handle
,
2674 MAKELONG(getCX(), getDX()), 0,
2675 MAKELONG(getSI(), getDI()), 0))
2677 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2681 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2682 setAX(GetLastError());
2687 /* Invalid subfunction */
2688 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2689 setAX(ERROR_INVALID_FUNCTION
);
2695 /* Canonicalize File Name or Path */
2699 * See Ralf Brown: http://www.ctyme.com/intr/rb-3137.htm
2700 * for more information.
2704 * We suppose that the DOS app gave to us a valid
2705 * 128-byte long buffer for the canonicalized name.
2707 DWORD dwRetVal
= GetFullPathNameA(SEG_OFF_TO_PTR(getDS(), getSI()),
2709 SEG_OFF_TO_PTR(getES(), getDI()),
2713 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2714 setAX(GetLastError());
2718 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2722 // FIXME: Convert the full path name into short version.
2723 // We cannot reliably use GetShortPathName, because it fails
2724 // if the path name given doesn't exist. However this DOS
2725 // function AH=60h should be able to work even for non-existing
2726 // path and file names.
2731 /* Set Handle Count */
2734 if (!DosResizeHandleTable(getBX()))
2736 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2737 setAX(DosLastError
);
2739 else Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2749 * Function 6Ah is identical to function 68h,
2750 * and sets AH to 68h if success.
2751 * See Ralf Brown: http://www.ctyme.com/intr/rb-3176.htm
2752 * for more information.
2756 if (DosFlushFileBuffers(getBX()))
2758 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
2762 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2763 setAX(GetLastError());
2769 /* Extended Open/Create */
2772 /* Check for AL == 00 */
2773 if (getAL() != 0x00)
2775 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2776 setAX(ERROR_INVALID_FUNCTION
);
2780 // TODO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2781 // FIXME: Extend and merge DosOpenFile and DosCreateFile into
2782 // a single wrapper around CreateFileA, which acts as:
2783 // http://www.ctyme.com/intr/rb-3179.htm
2791 DPRINT1("DOS Function INT 0x21, AH = %xh, AL = %xh NOT IMPLEMENTED!\n",
2794 setAL(0); // Some functions expect AL to be 0 when it's not supported.
2795 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2800 VOID WINAPI
DosBreakInterrupt(LPWORD Stack
)
2802 UNREFERENCED_PARAMETER(Stack
);
2804 /* Stop the VDM task */
2805 ResetEvent(VdmTaskEvent
);
2806 EmulatorUnsimulate();
2809 VOID WINAPI
DosFastConOut(LPWORD Stack
)
2812 * This is the DOS 2+ Fast Console Output Interrupt.
2813 * The default handler under DOS 2.x and 3.x simply calls INT 10h/AH=0Eh.
2815 * See Ralf Brown: http://www.ctyme.com/intr/rb-4124.htm
2816 * for more information.
2819 /* Save AX and BX */
2820 USHORT AX
= getAX();
2821 USHORT BX
= getBX();
2824 * Set the parameters:
2825 * AL contains the character to print (already set),
2826 * BL contains the character attribute,
2827 * BH contains the video page to use.
2829 setBL(DOS_CHAR_ATTRIBUTE
);
2830 setBH(Bda
->VideoPage
);
2832 /* Call the BIOS INT 10h, AH=0Eh "Teletype Output" */
2834 Int32Call(&DosContext
, BIOS_VIDEO_INTERRUPT
);
2836 /* Restore AX and BX */
2841 VOID WINAPI
DosInt2Fh(LPWORD Stack
)
2843 DPRINT1("DOS Internal System Function INT 0x2F, AH = %xh, AL = %xh NOT IMPLEMENTED!\n",
2845 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
2848 BOOLEAN
DosKRNLInitialize(VOID
)
2854 CHAR CurrentDirectory
[MAX_PATH
];
2855 CHAR DosDirectory
[DOS_DIR_LENGTH
];
2861 /* Clear the current directory buffer */
2862 ZeroMemory(CurrentDirectories
, sizeof(CurrentDirectories
));
2864 /* Get the current directory */
2865 if (!GetCurrentDirectoryA(MAX_PATH
, CurrentDirectory
))
2867 // TODO: Use some kind of default path?
2871 /* Convert that to a DOS path */
2872 if (!GetShortPathNameA(CurrentDirectory
, DosDirectory
, DOS_DIR_LENGTH
))
2874 // TODO: Use some kind of default path?
2879 CurrentDrive
= DosDirectory
[0] - 'A';
2881 /* Get the directory part of the path */
2882 Path
= strchr(DosDirectory
, '\\');
2885 /* Skip the backslash */
2889 /* Set the directory */
2892 strncpy(CurrentDirectories
[CurrentDrive
], Path
, DOS_DIR_LENGTH
);
2895 /* Read CONFIG.SYS */
2896 Stream
= _wfopen(DOS_CONFIG_PATH
, L
"r");
2899 while (fgetws(Buffer
, 256, Stream
))
2901 // TODO: Parse the line
2906 /* Initialize the SFT */
2907 for (i
= 0; i
< DOS_SFT_SIZE
; i
++)
2909 DosSystemFileTable
[i
].Handle
= INVALID_HANDLE_VALUE
;
2910 DosSystemFileTable
[i
].RefCount
= 0;
2913 /* Get handles to standard I/O devices */
2914 DosSystemFileTable
[0].Handle
= GetStdHandle(STD_INPUT_HANDLE
);
2915 DosSystemFileTable
[1].Handle
= GetStdHandle(STD_OUTPUT_HANDLE
);
2916 DosSystemFileTable
[2].Handle
= GetStdHandle(STD_ERROR_HANDLE
);
2918 /* Initialize the reference counts */
2919 DosSystemFileTable
[0].RefCount
=
2920 DosSystemFileTable
[1].RefCount
=
2921 DosSystemFileTable
[2].RefCount
= 1;
2925 /* Initialize the callback context */
2926 InitializeContext(&DosContext
, 0x0070, 0x0000);
2928 /* Register the DOS 32-bit Interrupts */
2929 RegisterDosInt32(0x20, DosInt20h
);
2930 RegisterDosInt32(0x21, DosInt21h
);
2931 // RegisterDosInt32(0x22, DosInt22h ); // Termination
2932 RegisterDosInt32(0x23, DosBreakInterrupt
); // Ctrl-C / Ctrl-Break
2933 // RegisterDosInt32(0x24, DosInt24h ); // Critical Error
2934 RegisterDosInt32(0x29, DosFastConOut
); // DOS 2+ Fast Console Output
2935 RegisterDosInt32(0x2F, DosInt2Fh
);