2 * DOS interrupt 21h handler
4 * Copyright 1993, 1994 Erik Bos
5 * Copyright 1996 Alexandre Julliard
6 * Copyright 1997 Andreas Mohr
7 * Copyright 1998 Uwe Bonnes
8 * Copyright 1998, 1999 Ove Kaaven
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "wine/winbase16.h"
36 #include "wine/unicode.h"
37 #include "wine/debug.h"
40 * FIXME: Delete this reference when all int21 code has been moved to winedos.
42 extern void WINAPI INT_Int21Handler( CONTEXT86 *context );
44 WINE_DEFAULT_DEBUG_CHANNEL(int21);
47 /***********************************************************************
50 * Handler for functions 0x51 and 0x62.
52 static void INT21_GetPSP( CONTEXT86 *context )
54 TRACE( "GET CURRENT PSP ADDRESS (%02x)\n", AH_reg(context) );
57 * FIXME: should we return the original DOS PSP upon
60 if (!ISV86(context) && DOSVM_IsWin16())
61 SET_BX( context, LOWORD(GetCurrentPDB16()) );
63 SET_BX( context, DOSVM_psp );
67 /***********************************************************************
70 * Handler for function 0x44.
72 static void INT21_Ioctl( CONTEXT86 *context )
74 static const WCHAR emmxxxx0W[] = {'E','M','M','X','X','X','X','0',0};
75 const DOS_DEVICE *dev = DOSFS_GetDeviceByHandle(
76 DosFileHandleToWin32Handle(BX_reg(context)) );
78 if (dev && !strcmpiW( dev->name, emmxxxx0W )) {
79 EMS_Ioctl_Handler(context);
83 switch (AL_reg(context))
85 case 0x0b: /* SET SHARING RETRY COUNT */
86 TRACE("IOCTL - SET SHARING RETRY COUNT pause %d retries %d\n",
87 CX_reg(context), DX_reg(context));
94 DOSMEM_LOL()->sharing_retry_delay = CX_reg(context);
96 DOSMEM_LOL()->sharing_retry_count = DX_reg(context);
100 INT_Int21Handler( context );
105 /***********************************************************************
106 * INT21_GetExtendedError
108 static void INT21_GetExtendedError( CONTEXT86 *context )
110 BYTE class, action, locus;
111 WORD error = GetLastError();
116 class = action = locus = 0;
118 case ERROR_DIR_NOT_EMPTY:
123 case ERROR_ACCESS_DENIED:
124 class = EC_AccessDenied;
128 case ERROR_CANNOT_MAKE:
129 class = EC_AccessDenied;
133 case ERROR_DISK_FULL:
134 case ERROR_HANDLE_DISK_FULL:
135 class = EC_MediaError;
139 case ERROR_FILE_EXISTS:
140 case ERROR_ALREADY_EXISTS:
145 case ERROR_FILE_NOT_FOUND:
150 case ER_GeneralFailure:
151 class = EC_SystemFailure;
155 case ERROR_INVALID_DRIVE:
156 class = EC_MediaError;
160 case ERROR_INVALID_HANDLE:
161 class = EC_ProgramError;
165 case ERROR_LOCK_VIOLATION:
166 class = EC_AccessDenied;
170 case ERROR_NO_MORE_FILES:
171 class = EC_MediaError;
180 case ERROR_NOT_ENOUGH_MEMORY:
181 class = EC_OutOfResource;
185 case ERROR_PATH_NOT_FOUND:
195 case ERROR_SHARING_VIOLATION:
196 class = EC_Temporary;
200 case ERROR_TOO_MANY_OPEN_FILES:
201 class = EC_ProgramError;
206 FIXME("Unknown error %d\n", error );
207 class = EC_SystemFailure;
212 TRACE("GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
213 error, class, action, locus );
214 SET_AX( context, error );
215 SET_BH( context, class );
216 SET_BL( context, action );
217 SET_CH( context, locus );
221 /***********************************************************************
222 * DOSVM_Int21Handler (WINEDOS16.133)
224 * Interrupt 0x21 handler.
226 void WINAPI DOSVM_Int21Handler( CONTEXT86 *context )
228 BOOL bSetDOSExtendedError = FALSE;
230 TRACE( "AX=%04x BX=%04x CX=%04x DX=%04x "
231 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
232 AX_reg(context), BX_reg(context),
233 CX_reg(context), DX_reg(context),
234 SI_reg(context), DI_reg(context),
235 (WORD)context->SegDs, (WORD)context->SegEs,
239 * Extended error is used by (at least) functions 0x2f to 0x62.
240 * Function 0x59 returns extended error and error should not
241 * be cleared before handling the function.
243 if (AH_reg(context) >= 0x2f && AH_reg(context) != 0x59)
246 RESET_CFLAG(context); /* Not sure if this is a good idea. */
248 switch(AH_reg(context))
250 case 0x00: /* TERMINATE PROGRAM */
251 TRACE("TERMINATE PROGRAM\n");
255 MZ_Exit( context, FALSE, 0 );
258 case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
261 TRACE("DIRECT CHARACTER INPUT WITH ECHO\n");
262 DOSVM_Int16ReadChar(&ascii, NULL, FALSE);
263 SET_AL( context, ascii );
264 DOSVM_PutChar(AL_reg(context));
268 case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
269 TRACE("Write Character to Standard Output\n");
270 DOSVM_PutChar(DL_reg(context));
273 case 0x03: /* READ CHARACTER FROM STDAUX */
274 case 0x04: /* WRITE CHARACTER TO STDAUX */
275 case 0x05: /* WRITE CHARACTER TO PRINTER */
276 INT_BARF( context, 0x21 );
279 case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
280 /* FIXME: Use DOSDEV_Peek/Read/Write(DOSDEV_Console(),...) !! */
281 if (DL_reg(context) == 0xff) {
282 static char scan = 0;
283 TRACE("Direct Console Input\n");
285 /* return pending scancode */
286 SET_AL( context, scan );
287 RESET_ZFLAG(context);
291 if (DOSVM_Int16ReadChar(&ascii,&scan,TRUE)) {
292 DOSVM_Int16ReadChar(&ascii,&scan,FALSE);
293 /* return ASCII code */
294 SET_AL( context, ascii );
295 RESET_ZFLAG(context);
296 /* return scan code on next call only if ascii==0 */
299 /* nothing pending, clear everything */
300 SET_AL( context, 0 );
302 scan = 0; /* just in case */
306 TRACE("Direct Console Output\n");
307 DOSVM_PutChar(DL_reg(context));
311 case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
314 /* FIXME: Use DOSDEV_Peek/Read(DOSDEV_Console(),...) !! */
315 TRACE("DIRECT CHARACTER INPUT WITHOUT ECHO\n");
316 DOSVM_Int16ReadChar(&ascii, NULL, FALSE);
317 SET_AL( context, ascii );
321 case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
324 /* FIXME: Use DOSDEV_Peek/Read(DOSDEV_Console(),...) !! */
325 TRACE("CHARACTER INPUT WITHOUT ECHO\n");
326 DOSVM_Int16ReadChar(&ascii, NULL, FALSE);
327 SET_AL( context, ascii );
331 case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
332 INT_Int21Handler( context );
335 case 0x0a: /* BUFFERED INPUT */
336 INT_Int21Handler( context );
339 case 0x0b: /* GET STDIN STATUS */
340 TRACE( "GET STDIN STATUS\n" );
342 BIOSDATA *data = BIOS_DATA;
343 if(data->FirstKbdCharPtr == data->NextKbdCharPtr)
344 SET_AL( context, 0 );
346 SET_AL( context, 0xff );
350 case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
352 BYTE al = AL_reg(context); /* Input function to execute after flush. */
354 TRACE( "FLUSH BUFFER AND READ STANDARD INPUT - 0x%02x\n", al );
356 /* FIXME: buffers are not flushed */
359 * If AL is one of 0x01, 0x06, 0x07, 0x08, or 0x0a,
360 * int21 function identified by AL will be called.
362 if(al == 0x01 || al == 0x06 || al == 0x07 || al == 0x08 || al == 0x0a)
364 SET_AH( context, al );
365 DOSVM_Int21Handler( context );
370 case 0x0d: /* DISK BUFFER FLUSH */
371 TRACE("DISK BUFFER FLUSH ignored\n");
374 case 0x0e: /* SELECT DEFAULT DRIVE */
375 INT_Int21Handler( context );
378 case 0x0f: /* OPEN FILE USING FCB */
379 case 0x10: /* CLOSE FILE USING FCB */
380 INT_BARF( context, 0x21 );
383 case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
384 case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
385 case 0x13: /* DELETE FILE USING FCB */
386 INT_Int21Handler( context );
389 case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
390 case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
391 case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
392 INT_BARF( context, 0x21 );
395 case 0x17: /* RENAME FILE USING FCB */
396 INT_Int21Handler( context );
399 case 0x18: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
400 SET_AL( context, 0 );
403 case 0x19: /* GET CURRENT DEFAULT DRIVE */
404 case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
405 case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */
406 case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
407 INT_Int21Handler( context );
410 case 0x1d: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
411 case 0x1e: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
412 SET_AL( context, 0 );
415 case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
416 INT_Int21Handler( context );
419 case 0x20: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
420 SET_AL( context, 0 );
423 case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
424 case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
425 case 0x23: /* GET FILE SIZE FOR FCB */
426 case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
427 INT_BARF( context, 0x21 );
430 case 0x25: /* SET INTERRUPT VECTOR */
431 TRACE("SET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
433 FARPROC16 ptr = (FARPROC16)MAKESEGPTR( context->SegDs, DX_reg(context) );
434 if (!ISV86(context) && DOSVM_IsWin16())
435 DOSVM_SetPMHandler16( AL_reg(context), ptr );
437 DOSVM_SetRMHandler( AL_reg(context), ptr );
441 case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
442 case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
443 case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
444 INT_BARF( context, 0x21 );
447 case 0x29: /* PARSE FILENAME INTO FCB */
448 INT_Int21Handler( context );
451 case 0x2a: /* GET SYSTEM DATE */
452 TRACE( "GET SYSTEM DATE\n" );
455 GetLocalTime( &systime );
456 SET_CX( context, systime.wYear );
457 SET_DH( context, systime.wMonth );
458 SET_DL( context, systime.wDay );
459 SET_AL( context, systime.wDayOfWeek );
463 case 0x2b: /* SET SYSTEM DATE */
464 FIXME("SetSystemDate(%02d/%02d/%04d): not allowed\n",
465 DL_reg(context), DH_reg(context), CX_reg(context) );
466 SET_AL( context, 0 ); /* Let's pretend we succeeded */
469 case 0x2c: /* GET SYSTEM TIME */
470 TRACE( "GET SYSTEM TIME\n" );
473 GetLocalTime( &systime );
474 SET_CL( context, systime.wHour );
475 SET_CH( context, systime.wMinute );
476 SET_DH( context, systime.wSecond );
477 SET_DL( context, systime.wMilliseconds / 10 );
481 case 0x2d: /* SET SYSTEM TIME */
482 FIXME("SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
483 CH_reg(context), CL_reg(context),
484 DH_reg(context), DL_reg(context) );
485 SET_AL( context, 0 ); /* Let's pretend we succeeded */
488 case 0x2e: /* SET VERIFY FLAG */
489 TRACE("SET VERIFY FLAG ignored\n");
490 /* we cannot change the behaviour anyway, so just ignore it */
493 case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
494 case 0x30: /* GET DOS VERSION */
495 INT_Int21Handler( context );
498 case 0x31: /* TERMINATE AND STAY RESIDENT */
499 FIXME("TERMINATE AND STAY RESIDENT stub\n");
502 case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
503 case 0x33: /* MULTIPLEXED */
504 case 0x34: /* GET ADDRESS OF INDOS FLAG */
505 INT_Int21Handler( context );
508 case 0x35: /* GET INTERRUPT VECTOR */
509 TRACE("GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
512 if (!ISV86(context) && DOSVM_IsWin16())
513 addr = DOSVM_GetPMHandler16( AL_reg(context) );
515 addr = DOSVM_GetRMHandler( AL_reg(context) );
516 context->SegEs = SELECTOROF(addr);
517 SET_BX( context, OFFSETOF(addr) );
521 case 0x36: /* GET FREE DISK SPACE */
522 case 0x37: /* SWITCHAR */
523 INT_Int21Handler( context );
526 case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
527 TRACE( "GET COUNTRY-SPECIFIC INFORMATION for country 0x%02x\n",
529 SET_AX( context, 0x02 ); /* no country support available */
530 SET_CFLAG( context );
533 case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
534 case 0x3a: /* "RMDIR" - REMOVE SUBDIRECTORY */
535 case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
536 case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
537 case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
538 case 0x3e: /* "CLOSE" - CLOSE FILE */
539 case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
540 INT_Int21Handler( context );
543 case 0x40: /* "WRITE" - WRITE TO FILE OR DEVICE */
544 /* Writes to stdout are handled here. */
545 if (!DOSVM_IsWin16() && BX_reg(context) == 1) {
546 BYTE *ptr = CTX_SEG_OFF_TO_LIN(context,
551 for(i=0; i<CX_reg(context); i++)
552 DOSVM_PutChar(ptr[i]);
554 INT_Int21Handler( context );
557 case 0x41: /* "UNLINK" - DELETE FILE */
558 case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
559 case 0x43: /* FILE ATTRIBUTES */
560 INT_Int21Handler( context );
563 case 0x44: /* IOCTL */
564 INT21_Ioctl( context );
567 case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
568 case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
569 case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
570 INT_Int21Handler( context );
573 case 0x48: /* ALLOCATE MEMORY */
574 TRACE( "ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context) );
577 DWORD bytes = (DWORD)BX_reg(context) << 4;
579 if (!ISV86(context) && DOSVM_IsWin16())
581 DWORD rv = GlobalDOSAlloc16( bytes );
582 selector = LOWORD( rv );
585 DOSMEM_GetBlock( bytes, &selector );
588 SET_AX( context, selector );
592 SET_AX( context, 0x0008 ); /* insufficient memory */
593 SET_BX( context, DOSMEM_Available() >> 4 );
598 case 0x49: /* FREE MEMORY */
599 TRACE( "FREE MEMORY segment %04lX\n", context->SegEs );
603 if (!ISV86(context) && DOSVM_IsWin16())
605 ok = !GlobalDOSFree16( context->SegEs );
607 /* If we don't reset ES_reg, we will fail in the relay code */
612 ok = DOSMEM_FreeBlock( (void*)((DWORD)context->SegEs << 4) );
616 TRACE("FREE MEMORY failed\n");
618 SET_AX( context, 0x0009 ); /* memory block address invalid */
623 case 0x4a: /* RESIZE MEMORY BLOCK */
624 INT_Int21Handler( context );
627 case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
628 if(DOSVM_IsWin16()) {
629 INT_Int21Handler( context );
633 TRACE("EXEC %s\n", (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx ));
634 if (!MZ_Exec( context, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx),
635 AL_reg(context), CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Ebx) ))
637 SET_AX( context, GetLastError() );
642 case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
643 TRACE( "EXIT with return code %d\n", AL_reg(context) );
645 ExitThread( AL_reg(context) );
647 MZ_Exit( context, FALSE, AL_reg(context) );
650 case 0x4d: /* GET RETURN CODE */
651 TRACE("GET RETURN CODE (ERRORLEVEL)\n");
652 SET_AX( context, DOSVM_retval );
656 case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
657 case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
658 INT_Int21Handler( context );
661 case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
662 TRACE("SET CURRENT PROCESS ID (SET PSP ADDRESS)\n");
663 DOSVM_psp = BX_reg(context);
666 case 0x51: /* GET PSP ADDRESS */
667 INT21_GetPSP( context );
670 case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
671 if (!ISV86(context) && DOSVM_IsWin16())
673 SEGPTR ptr = DOSMEM_LOL()->wine_pm_lol;
674 context->SegEs = SELECTOROF(ptr);
675 SET_BX( context, OFFSETOF(ptr) );
679 SEGPTR ptr = DOSMEM_LOL()->wine_rm_lol;
680 context->SegEs = SELECTOROF(ptr);
681 SET_BX( context, OFFSETOF(ptr) );
685 case 0x54: /* Get Verify Flag */
686 TRACE("Get Verify Flag - Not Supported\n");
687 SET_AL( context, 0x00 ); /* pretend we can tell. 00h = off 01h = on */
690 case 0x56: /* "RENAME" - RENAME FILE */
691 case 0x57: /* FILE DATE AND TIME */
692 INT_Int21Handler( context );
695 case 0x58: /* GET OR SET MEMORY ALLOCATION STRATEGY */
696 TRACE( "GET OR SET MEMORY ALLOCATION STRATEGY, subfunction %d\n",
698 switch (AL_reg(context))
700 case 0x00: /* GET ALLOCATION STRATEGY */
701 SET_AX( context, 1 ); /* low memory best fit */
704 case 0x01: /* SET ALLOCATION STRATEGY */
705 TRACE( "Set allocation strategy to %d - ignored\n",
710 INT_BARF( context, 0x21 );
715 case 0x59: /* GET EXTENDED ERROR INFO */
716 INT21_GetExtendedError( context );
719 case 0x5a: /* CREATE TEMPORARY FILE */
720 case 0x5b: /* CREATE NEW FILE */
721 case 0x5c: /* "FLOCK" - RECORD LOCKING */
722 case 0x5d: /* NETWORK 5D */
723 case 0x5e: /* NETWORK 5E */
724 case 0x5f: /* NETWORK 5F */
725 case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
726 case 0x61: /* UNUSED */
727 INT_Int21Handler( context );
730 case 0x62: /* GET PSP ADDRESS */
731 INT21_GetPSP( context );
734 case 0x63: /* MISC. LANGUAGE SUPPORT */
735 INT_Int21Handler( context );
738 case 0x64: /* OS/2 DOS BOX */
739 INT_BARF( context, 0x21 );
743 case 0x65: /* GET EXTENDED COUNTRY INFORMATION */
744 case 0x66: /* GLOBAL CODE PAGE TABLE */
745 INT_Int21Handler( context );
748 case 0x67: /* SET HANDLE COUNT */
749 TRACE( "SET HANDLE COUNT to %d\n", BX_reg(context) );
750 if (SetHandleCount( BX_reg(context) ) < BX_reg(context) )
751 bSetDOSExtendedError = TRUE;
754 case 0x68: /* "FFLUSH" - COMMIT FILE */
755 case 0x69: /* DISK SERIAL NUMBER */
756 case 0x6a: /* COMMIT FILE */
757 INT_Int21Handler( context );
760 case 0x6b: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
761 SET_AL( context, 0 );
764 case 0x6c: /* EXTENDED OPEN/CREATE */
765 case 0x70: /* MS-DOS 7 (Windows95) - ??? (country-specific?)*/
766 case 0x71: /* MS-DOS 7 (Windows95) - LONG FILENAME FUNCTIONS */
767 case 0x72: /* MS-DOS 7 (Windows95) - ??? */
768 case 0x73: /* MULTIPLEXED: Win95 OSR2/Win98 FAT32 calls */
769 case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
770 case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
771 INT_Int21Handler( context );
775 INT_BARF( context, 0x21 );
778 } /* END OF SWITCH */
780 /* Set general error condition. */
781 if (bSetDOSExtendedError)
783 SET_AX( context, GetLastError() );
784 SET_CFLAG( context );
787 /* Print error code if carry flag is set. */
788 if (context->EFlags & 0x0001)
789 TRACE("failed, error %ld\n", GetLastError() );
791 TRACE( "returning: AX=%04x BX=%04x CX=%04x DX=%04x "
792 "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
793 AX_reg(context), BX_reg(context),
794 CX_reg(context), DX_reg(context),
795 SI_reg(context), DI_reg(context),
796 (WORD)context->SegDs, (WORD)context->SegEs,