Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
7.5 KiB

  1. /* fixchg.c */
  2. /* Inoperative changelines frequenly cause problems when switching between */
  3. /* 1.44Mb diskettes and 1.68Mb DMF diskettes. FixChangeline() tries to */
  4. /* assure that drives A: and B: will not depend upon proper operation of */
  5. /* the drive's changeline. If these efforts fail, it's no big deal; we */
  6. /* do this without even knowing whether the changeline works or not. */
  7. #include "fixchg.h" /* prototype verification */
  8. /* --- definitions -------------------------------------------------------- */
  9. /* See Microsoft MS-DOS Programmer's Reference V6.0, p.38, 312, 319 */
  10. typedef unsigned char BYTE;
  11. typedef unsigned short WORD;
  12. typedef unsigned long DWORD;
  13. #pragma pack (1)
  14. typedef struct
  15. {
  16. WORD tklSectorNum; /* this physical position's sector number */
  17. WORD tklSectorSize; /* size of this sector in bytes */
  18. } NUMSIZE;
  19. typedef struct
  20. {
  21. WORD tklSectors; /* number of sectors in the layout */
  22. NUMSIZE tklNumSize[1]; /* don't need much of this, not used here */
  23. } TRACKLAYOUT;
  24. typedef struct
  25. {
  26. WORD dpBytesPerSec; /* bytes per sector */
  27. BYTE dpSecPerClust; /* sectors per cluster */
  28. WORD dpResSectors; /* reserved sectors */
  29. BYTE dpFATs; /* number of copies of the FAT */
  30. WORD dpRootDirEnts; /* number of entries in the root directory */
  31. WORD dpSectors; /* total # of sectors, 0->more than 64k */
  32. BYTE dpMedia; /* media descriptor byte */
  33. WORD dpFATsecs; /* sectors per copy of the FAT */
  34. WORD dpSecPerTrack; /* sectors per track */
  35. WORD dpHeads; /* number of heads */
  36. DWORD dpHiddenSecs; /* sectors hidden before boot sector */
  37. DWORD dpHugeSectors; /* number of sectors if > 64k sectors */
  38. WORD reserved[3];
  39. } BPB;
  40. typedef struct
  41. {
  42. BYTE dpSpecFunc; /* special functions */
  43. BYTE dpDevType; /* device type, 7=1.44Mb, 9=2.88Mb, etc. */
  44. WORD dpDevAttr; /* device's attributes */
  45. WORD dpCylinders; /* number of cylinders */
  46. BYTE dpMediaType; /* media type, more like density code */
  47. BPB dpBPB; /* the BPB (default or current) */
  48. TRACKLAYOUT dpTrackLayout; /* track layout field appended for set call */
  49. } DEVICEPARAMS, far *PFDEVICEPARAMS;
  50. #pragma pack()
  51. #define SPECIAL_GET_DEFAULT 0 /* get information for default media */
  52. #define SPECIAL_SET_DEFAULT 4 /* set default media, good track layout */
  53. #define ATTR_NONREMOVABLE 1 /* attr bit for non-removable device */
  54. #define ATTR_CHANGELINE 2 /* attr bit for changeline supported */
  55. /* --- FixChangelines() --------------------------------------------------- */
  56. #pragma warning(disable:4704) /* no in-line balking */
  57. void FixChangelines(void)
  58. {
  59. WORD dosVersion;
  60. DEVICEPARAMS dp;
  61. PFDEVICEPARAMS pfDp;
  62. WORD drive;
  63. WORD owner;
  64. _asm mov ah,30h ; get DOS version
  65. _asm int 21h
  66. _asm xchg ah,al
  67. _asm mov dosVersion,ax
  68. /* these IoCtls were new to MS-DOS 3.2. (But then, 1.44Mb drives */
  69. /* weren't supported until 3.3, so needing this is pretty unlikely.) */
  70. if (dosVersion < (0x300 + 20))
  71. {
  72. return; /* prior versions don't need help */
  73. }
  74. pfDp = &dp; /* make a far pointer to DEVICEPARAMS structure */
  75. for (drive = 1; drive <= 2; drive++) /* do A: and B: */
  76. {
  77. /* get drive owner so we can restore it */
  78. _asm mov owner,0 ; assume not shared
  79. _asm mov ax,440Eh ; Get Logical Drive Map
  80. _asm mov bx,drive ; drive number
  81. _asm int 21h ; execute DOS request
  82. _asm jc no_owner ; if failed
  83. _asm mov owner,ax ; save owner (AL)
  84. /* set drive owner to suppress "Insert diskette for drive..." */
  85. _asm mov ax,440Fh ; Set Logical Drive Map
  86. _asm mov bx,drive ; drive number
  87. _asm int 21h ; execute DOS request
  88. /* MS-DOS 5.0 added query Ioctl, to see if the calls we need are */
  89. /* supported. This is highly unlikely to fail. */
  90. no_owner:
  91. if (dosVersion >= 0x500)
  92. {
  93. _asm mov ax,4411h ; Query Ioctl device
  94. _asm mov bx,drive ; drive number
  95. _asm mov cx,0840h ; check on SET DEVICE PARAMETERS
  96. _asm int 21h ; execute DOS request
  97. _asm jc failed ; if not supported
  98. _asm mov ax,4411h ; Query Ioctl device
  99. _asm mov bx,drive ; drive number
  100. _asm mov cx,0860h ; check on GET DEVICE PARAMETERS
  101. _asm int 21h ; execute DOS request
  102. _asm jc failed ; if not supported
  103. }
  104. /* get information about this physical device */
  105. dp.dpSpecFunc = SPECIAL_GET_DEFAULT;
  106. _asm push ds ; preserve data selector
  107. _asm mov ax,440Dh ; generic IoCtl
  108. _asm mov bx,drive ; drive number 1=A: 2=B:
  109. _asm mov cx,0860h ; DISK / GET DEVICE PARAMETERS
  110. _asm lds dx,pfDp ; pointer to DEVICEPARAMS structure
  111. _asm int 21h ; execute DOS request
  112. _asm pop ds ; restore data selector
  113. _asm jc failed ; if error
  114. /* is this device is removable and claims changeline is supported? */
  115. if ((dp.dpDevAttr & (ATTR_NONREMOVABLE | ATTR_CHANGELINE)) ==
  116. ATTR_CHANGELINE) /* if removable with changeline: */
  117. {
  118. /* modify device to "changeline not supported" */
  119. dp.dpSpecFunc = SPECIAL_SET_DEFAULT;
  120. dp.dpDevAttr &= ~ATTR_CHANGELINE; /* disable changeline */
  121. dp.dpTrackLayout.tklSectors = 0; /* no layout being sent */
  122. dp.dpBPB.reserved[0] = 0;
  123. dp.dpBPB.reserved[1] = 0;
  124. dp.dpBPB.reserved[2] = 0;
  125. _asm push ds ; preserve data selector
  126. _asm mov ax,440Dh ; generic IoCtl
  127. _asm mov bx,drive ; drive number 1=A: 2=B:
  128. _asm mov cx,0840h ; DISK / SET DEVICE PARAMETERS
  129. _asm lds dx,pfDp ; pointer to DEVICEPARAMS structure
  130. _asm int 21h ; execute DOS request
  131. _asm pop ds ; restore data selector
  132. }
  133. failed:
  134. /* restore initial drive owner */
  135. _asm mov ax,440Fh ; Set Logical Drive Map
  136. _asm mov bx,owner ; drive number
  137. _asm or bx,bx ; is it shared?
  138. _asm jz nextdrive ; if not shared
  139. _asm int 21h ; execute DOS request
  140. nextdrive:
  141. continue; /* C labels require some statement */
  142. }
  143. return;
  144. }
  145. /* --- stand-alone test stub ---------------------------------------------- */
  146. #ifdef STANDALONE
  147. void main(void)
  148. {
  149. FixChangelines();
  150. }
  151. #endif
  152. /* ------------------------------------------------------------------------ */