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.

366 lines
8.2 KiB

  1. #include <nt.h>
  2. #include <ntddtx.h>
  3. #include <malloc.h>
  4. #include "sim32.h"
  5. UCHAR TransmitPkt[MAXSIZE];
  6. UCHAR ReceivePkt[MAXSIZE];
  7. HANDLE DeviceHandle;
  8. IO_STATUS_BLOCK IoStatusBlock;
  9. NTSTATUS Status;
  10. /*****************************************************************************
  11. *
  12. * Sim32GetVDMMemory
  13. *
  14. * This routine gets 'Size' bytes from WOW VDM starting at address
  15. * specified by 'Address'. These bytes are returned to the caller in
  16. * the Buffer (which is owned by the caller).
  17. *
  18. *****************************************************************************/
  19. USHORT Sim32GetVDMMemory (IN ULONG Address,
  20. IN USHORT Size,
  21. IN OUT PVOID Buffer)
  22. {
  23. if (Size < MAXSIZE-11) {
  24. TransmitPkt[0] = SOH;
  25. TransmitPkt[1] = GETMEM;
  26. TransmitPkt[2] = 11;
  27. TransmitPkt[3] = 0;
  28. TransmitPkt[4] = (UCHAR) FIRSTBYTE(Address);
  29. TransmitPkt[5] = (UCHAR) SECONDBYTE(Address);
  30. TransmitPkt[6] = (UCHAR) THIRDBYTE(Address);
  31. TransmitPkt[7] = (UCHAR) FOURTHBYTE(Address);
  32. TransmitPkt[8] = (UCHAR) FIRSTBYTE(Size);
  33. TransmitPkt[9] = (UCHAR) SECONDBYTE(Size);
  34. TransmitPkt[10] = EOT;
  35. if (!Xceive((USHORT)(Size+5), 11)) {
  36. DbgPrint ("Sim32GetVDMMemory.....BAD Memory \a\n");
  37. return (BAD);
  38. }
  39. RtlMoveMemory(Buffer, &ReceivePkt[4], Size);
  40. return(GOOD);
  41. }
  42. else {
  43. DbgPrint ("Bad Packet Size %d\n", Size);
  44. return (BADSIZE);
  45. }
  46. }
  47. /*****************************************************************************
  48. *
  49. * Sim32SetVDMMemory
  50. *
  51. * This routine sets 'Size' bytes in WOW VDM starting at address
  52. * specified by 'Address' to the values in Buffer.
  53. *
  54. *****************************************************************************/
  55. USHORT Sim32SetVDMMemory (IN ULONG Address,
  56. IN USHORT Size,
  57. IN OUT PVOID Buffer)
  58. {
  59. if (Size < MAXSIZE-11) {
  60. TransmitPkt[0] = SOH;
  61. TransmitPkt[1] = SETMEM;
  62. TransmitPkt[2] = (UCHAR) (Size+11);
  63. TransmitPkt[3] = 0;
  64. TransmitPkt[4] = (UCHAR) FIRSTBYTE(Address);
  65. TransmitPkt[5] = (UCHAR) SECONDBYTE(Address);
  66. TransmitPkt[6] = (UCHAR) THIRDBYTE(Address);
  67. TransmitPkt[7] = (UCHAR) FOURTHBYTE(Address);
  68. TransmitPkt[8] = (UCHAR) FIRSTBYTE(Size);
  69. TransmitPkt[9] = (UCHAR) SECONDBYTE(Size);
  70. TransmitPkt[10+Size] = EOT;
  71. RtlMoveMemory(&TransmitPkt[10], Buffer, Size);
  72. if (!Xceive(7, (USHORT)(Size+11))) {
  73. DbgPrint ("Sim32SetVDMMemory... could not set : \a\n");
  74. return (BAD);
  75. }
  76. return(GOOD);
  77. }
  78. else {
  79. DbgPrint ("Bad Packet Size %d\n", Size);
  80. return (BADSIZE);
  81. }
  82. }
  83. /*****************************************************************************
  84. *
  85. * Sim32GetVDMPSZPointer
  86. *
  87. * This routine returns a pointer to a null terminated string in the WOW
  88. * VDM at the specified address.
  89. *
  90. * This routine does the following,
  91. * allocates a sufficient size buffer,
  92. * gets the string from SIM16,
  93. * copies the string into buffer,
  94. * returns a pointer to the buffer.
  95. *
  96. *****************************************************************************/
  97. PSZ Sim32GetVDMPSZPointer (IN ULONG Address)
  98. {
  99. USHORT Size;
  100. PSZ Ptr;
  101. TransmitPkt[0] = SOH;
  102. TransmitPkt[1] = PSZLEN;
  103. TransmitPkt[2] = 9;
  104. TransmitPkt[3] = 0;
  105. TransmitPkt[4] = (UCHAR) FIRSTBYTE(Address);
  106. TransmitPkt[5] = (UCHAR) SECONDBYTE(Address);
  107. TransmitPkt[6] = (UCHAR) THIRDBYTE(Address);
  108. TransmitPkt[7] = (UCHAR) FOURTHBYTE(Address);
  109. TransmitPkt[8] = EOT;
  110. if (!Xceive(7, 9)) {
  111. DbgPrint ("Sim32GetVDMPSZPointer.....Attempt to get PSZ length failed \a\a\n");
  112. return NULL;
  113. }
  114. Size = *(PUSHORT)(ReceivePkt+4);
  115. //
  116. // allocate buffer to copy string into
  117. //
  118. Ptr = (PSZ) malloc(Size);
  119. if (!Ptr) {
  120. DbgPrint ("Sim32GetVDMPSZPointer..., malloc failed \a\a\n");
  121. }
  122. //
  123. // get null terminated string
  124. //
  125. if (Size < MAXSIZE-11) {
  126. TransmitPkt[1] = GETMEM;
  127. TransmitPkt[2] = 11;
  128. TransmitPkt[3] = 0;
  129. TransmitPkt[8] = (UCHAR) FIRSTBYTE(Size);
  130. TransmitPkt[9] = (UCHAR) SECONDBYTE(Size);
  131. TransmitPkt[10] = EOT;
  132. if (!Xceive((USHORT)(Size+5), 11)) {
  133. DbgPrint ("Sim32GetVDMPSZPointer.....Unsuccessful \a\a\n");
  134. return NULL;
  135. }
  136. RtlMoveMemory(Ptr, &ReceivePkt[4], Size);
  137. } else {
  138. DbgPrint ("Sim32GetVDMPSZPointer.....Size of the string too big Size = %d\a\a\n", Size);
  139. return NULL;
  140. }
  141. return Ptr;
  142. }
  143. /*****************************************************************************
  144. *
  145. * Sim32FreeVDMPointer
  146. *
  147. * This routine frees the buffer which was allocated earlier.
  148. *
  149. *****************************************************************************/
  150. VOID Sim32FreeVDMPointer (PVOID Ptr)
  151. {
  152. free (Ptr);
  153. }
  154. /*****************************************************************************
  155. *
  156. * Sim32SendSim16
  157. *
  158. * This routine specifies the stack of the WOW VDM task and asks the
  159. * WOW 16 to make that task run.
  160. *
  161. *****************************************************************************/
  162. USHORT Sim32SendSim16 (IN OUT ULONG *WOWStack)
  163. {
  164. static USHORT fInit = 0;
  165. if (fInit) {
  166. TransmitPkt[0] = SOH;
  167. TransmitPkt[1] = WAKEUP;
  168. TransmitPkt[2] = 9;
  169. TransmitPkt[3] = 0;
  170. TransmitPkt[4] = (UCHAR) FIRSTBYTE(*WOWStack);
  171. TransmitPkt[5] = (UCHAR) SECONDBYTE(*WOWStack);
  172. TransmitPkt[6] = (UCHAR) THIRDBYTE(*WOWStack);
  173. TransmitPkt[7] = (UCHAR) FOURTHBYTE(*WOWStack);
  174. TransmitPkt[8] = EOT;
  175. if (!Xceive(9, 9)) {
  176. return (BAD);
  177. }
  178. *WOWStack = *(PULONG)(ReceivePkt+4);
  179. return(GOOD);
  180. }
  181. else {
  182. Initialize(9);
  183. *WOWStack = *(PULONG)(ReceivePkt+4);
  184. fInit = 1;
  185. return (GOOD);
  186. }
  187. }
  188. /*****************************************************************************
  189. *
  190. * Xceive
  191. *
  192. * This routine transmits a packet and waits for the data from the remote
  193. * side to come. When this routine returns, the ReceivePkt has the data
  194. * sent by the remote machine.
  195. *
  196. *****************************************************************************/
  197. USHORT Xceive(IN USHORT Length_In, IN USHORT Length_Out)
  198. {
  199. BOOLEAN Done = FALSE;
  200. USHORT i = 0;
  201. while ((i < MAXTRY) && (!Done)) {
  202. Status = NtDeviceIoControlFile(
  203. DeviceHandle,
  204. NULL,
  205. NULL,
  206. NULL,
  207. &IoStatusBlock,
  208. IOCTL_TRNXT_XCEIVE,
  209. TransmitPkt,
  210. (ULONG) Length_Out,
  211. ReceivePkt,
  212. (ULONG) Length_In
  213. );
  214. //
  215. // check error condition
  216. // if no error, then
  217. //
  218. if (ReceivePkt[0] == SOH) {
  219. if (ReceivePkt[1] != NAK) {
  220. i = *(PUSHORT)(ReceivePkt+2);
  221. if (ReceivePkt[(--i)] == EOT) {
  222. Done = TRUE;
  223. }
  224. else {
  225. DbgPrint ("EOT is missing from the packet, *ERROR*, Do Not Proceed Further !\a\a\n");
  226. }
  227. }
  228. else {
  229. DbgPrint ("It is a NAK packet, *ERROR*, Do Not Proceed Further !\a\a\n");
  230. }
  231. }
  232. else {
  233. DbgPrint ("SOH is missing from the packet, *ERROR*, Do Not Proceed Further !\a\a\n");
  234. }
  235. if (!Done) {
  236. i++;
  237. DbgPrint ("\nSTOP STOP STOP !!!\a\a\a\a\a\n");
  238. }
  239. }
  240. if (Done) {
  241. return (GOOD);
  242. }
  243. else {
  244. return (BAD);
  245. }
  246. }
  247. void Initialize (IN USHORT Length_In)
  248. {
  249. OBJECT_ATTRIBUTES ObjectAttributes;
  250. STRING DeviceName;
  251. USHORT j;
  252. char TestPkt[] = "WOW 32 Simulator on NT\n\r";
  253. RtlInitString(&DeviceName, "\\Device\\Serial1");
  254. //
  255. // set attributes
  256. //
  257. ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
  258. ObjectAttributes.RootDirectory = NULL;
  259. ObjectAttributes.ObjectName = &DeviceName;
  260. ObjectAttributes.Attributes = OBJ_INHERIT;
  261. ObjectAttributes.SecuriAR) SECONDBYTE(Size);
  262. TransmitPkt[10] = EOT;
  263. if (!Xceive((USHORT)(Size+5), 11)) {
  264. DbgPrint ("Sim32GetVDMPSZPointer.....Unsuccessful \a\a\n");
  265. return NULL;
  266. }
  267. RtlMoveMemory(Ptr, &ReceivePkt[4], Size);
  268. } else {
  269. DbgPrint ("Sim32GetVDMPSZPointer.....Size of the string too big Size = %d\a\a\n", Size);
  270. return NULL;
  271. }
  272. return Ptr;
  273. }
  274. /*****************************************************************************
  275. *
  276. * Sim32FreeVDMPointer
  277. *
  278. * This routine frees the buffer which was allocated earlier.
  279. *
  280. *****************************************************************************/
  281. VOID Sim32FreeVDMPointer (PVOID Ptr)
  282. {
  283. free (Ptr);
  284. }
  285. /******************************