SHARE
TWEET

Winbond W83627DHG watchdog - windows

a guest Apr 19th, 2016 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "stdafx.h"
  2. #include <windows.h>
  3. #include "WinIoCtl.h"
  4.  
  5. // WinRing0
  6. #define IOCTL_READ_IO_PORT_BYTE          CTL_CODE(40000, 0x833, METHOD_BUFFERED, FILE_READ_ACCESS)
  7. #define IOCTL_WRITE_IO_PORT_BYTE         CTL_CODE(40000, 0x836, METHOD_BUFFERED, FILE_WRITE_ACCESS)
  8.  
  9. #define SERVICE_PERMISS (SC_MANAGER_CREATE_SERVICE | SERVICE_START | SERVICE_STOP | DELETE | SERVICE_CHANGE_CONFIG)
  10. // for WinRing0:
  11. struct WRITE_IO_PORT_INPUT
  12. {
  13.         ULONG PortNumber;
  14.         UCHAR CharData;
  15. };
  16. HANDLE HDriver = 0;
  17.  
  18. const WORD AddrReg_6180 = 0x2E;
  19. const WORD DataReg_6180 = 0x2F;
  20.  
  21. DWORD WriteIoPortByte(WORD u16_Port, BYTE u8_Value)
  22. {
  23.     // Write a Byte to an I/O Port via Driver
  24.     DWORD u32_RetLen;
  25.     WRITE_IO_PORT_INPUT k_In;
  26.     k_In.CharData = u8_Value;
  27.     k_In.PortNumber = u16_Port;
  28.  
  29.     if (!DeviceIoControl(HDriver, IOCTL_WRITE_IO_PORT_BYTE, &k_In, sizeof(k_In),
  30.         NULL, 0, &u32_RetLen, NULL))
  31.         return GetLastError();
  32.  
  33.     return 0;
  34. }
  35.  
  36. DWORD ReadIoPortByte(WORD u16_Port, BYTE* pu8_Value)
  37. {
  38.     // Read a Byte from an I/O Port via Driver
  39.     DWORD u32_RetLen;
  40.     WORD  u16_Value;
  41.     if (!DeviceIoControl(HDriver, IOCTL_READ_IO_PORT_BYTE, &u16_Port, sizeof(u16_Port),
  42.         &u16_Value, sizeof(u16_Value), &u32_RetLen, NULL))
  43.         return GetLastError();
  44.  
  45.     *pu8_Value = (BYTE) u16_Value;
  46.     return 0;
  47. }
  48.  
  49. BYTE ReadReg(DWORD Reg)
  50. {
  51.     BYTE Tmp;
  52.     WriteIoPortByte(AddrReg_6180, Reg);
  53.     ReadIoPortByte(DataReg_6180, &Tmp);
  54.     return Tmp;
  55. }
  56.  
  57. void WriteReg(DWORD Reg, BYTE Data)
  58. {
  59.     WriteIoPortByte(AddrReg_6180, Reg);
  60.     WriteIoPortByte(DataReg_6180, Data);
  61. }
  62.  
  63. int main(int argc, char**argv)
  64. {
  65.     LPTSTR DriverName= L"WinRing0";
  66.     LPTSTR DriverFile = L"%SystemRoot%\\system32\\drivers\\WinRing0.sys";
  67.     LPTSTR DriverID = L"\\\\.\\WinRing0_1_2_0";
  68.  
  69.     if (argc != 2)
  70.     {
  71.         puts("You should specify WD timer in seconds");
  72.         return 1;
  73.     }
  74.     int val = atoi(argv[1]);
  75.     if (val<0 || val>255)
  76.     {
  77.         puts("WD timer should be between 0 (off) and 255");
  78.         return 1;
  79.     }
  80.  
  81.     HDriver = CreateFile(DriverID, GENERIC_READ | GENERIC_WRITE, 0, NULL,
  82.         OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  83.     if (HDriver == INVALID_HANDLE_VALUE)
  84.     {
  85.         SC_HANDLE h_SCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  86.         if (!h_SCManager)
  87.         {
  88.             puts("Not an admin");
  89.             return GetLastError();
  90.         }
  91.         DWORD u32_Err = 0;
  92.         SC_HANDLE h_Service = CreateService(h_SCManager, DriverName, DriverName,
  93.             SERVICE_PERMISS, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
  94.             DriverFile, NULL, NULL, NULL, NULL, NULL);
  95.  
  96.         if (!h_Service)
  97.             h_Service = OpenService(h_SCManager, DriverName, SERVICE_PERMISS);
  98.  
  99.         if (!h_Service)
  100.             puts("Unable to install service. You need to copy WinRing0.sys to system32\drivers.");
  101.  
  102.         if (!StartService(h_Service, 0, NULL))
  103.             u32_Err = GetLastError();
  104.  
  105.         if (u32_Err == ERROR_SERVICE_ALREADY_RUNNING) // 1056
  106.             u32_Err = 0;
  107.  
  108.         CloseServiceHandle(h_Service);
  109.         CloseServiceHandle(h_SCManager);
  110.  
  111.         HDriver = CreateFile(DriverID, GENERIC_READ | GENERIC_WRITE, 0, NULL,
  112.             OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  113.  
  114.         if (HDriver == INVALID_HANDLE_VALUE)
  115.         {
  116.             printf("Unable to open driver %d\n",GetLastError());
  117.             return -1;
  118.         }
  119.     }
  120.  
  121.     WriteIoPortByte(AddrReg_6180, 0x87);
  122.     WriteIoPortByte(AddrReg_6180, 0x87);
  123.  
  124.     WriteReg(0x7, 0x00);
  125.     printf("Chip ID = %02X%02X\n", ReadReg(0x20), ReadReg(0x21));
  126.     printf("Watchdog was enabled = %s\n", ReadReg(0x2D)&1?"no":"yes");
  127.  
  128.     WriteReg(0x2d, ReadReg(0x2D)&~1);       // enable WD
  129.  
  130.     WriteReg(0x7, 0x08);
  131.     WriteReg(0x30, 1);                      // enable WDTO#
  132.  
  133.     WriteReg(0xF5, 2);  // WD in 1 second units via KBRST
  134.  
  135.     printf("Watchdog ticks were = %d sec\n", ReadReg(0xF6));
  136.  
  137.     WriteReg(0xF6,val);     // set timer
  138.  
  139.     WriteIoPortByte(AddrReg_6180, 0xAA);
  140.  
  141.     return 0;
  142. }
RAW Paste Data
Top