SMBIOS Library
CmosRW_WindowsIO.cpp
Go to the documentation of this file.
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * vim:expandtab:autoindent:tabstop=4:shiftwidth=4:filetype=c:cindent:textwidth=0:
3  *
4  * Copyright (C) 2005 Dell Inc.
5  * by Michael Brown <Michael_E_Brown@dell.com>
6  * Licensed under the Open Software License version 2.1
7  *
8  * Alternatively, you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published
10  * by the Free Software Foundation; either version 2 of the License,
11  * or (at your option) any later version.
12 
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  */
18 
19 // compat header should always be first header if including system headers
20 #define LIBSMBIOS_SOURCE
21 #include "smbios/compat.h"
22 
23 #include <stdio.h>
24 #include <conio.h>
25 
26 #include "CmosRWImpl.h"
27 #include "miniddk.h"
28 
29 using namespace std;
30 
31 namespace cmos
32 {
33 
34  // Non Member Functions
35  ZwSystemDebugControlPtr ZwSystemDebugControl;
36 
37  int LoadNtdllFuncs(void)
38  {
39  HMODULE hNtdll;
40 
41  hNtdll = GetModuleHandle(L"ntdll.dll");
42  if (!hNtdll)
43  return FALSE;
44 
45  ZwSystemDebugControl = (ZwSystemDebugControlPtr) GetProcAddress(hNtdll, "ZwSystemDebugControl");
47  return FALSE;
48 
49  return TRUE;
50  }
51 
52  int EnableDebug(void)
53  {
54  HANDLE hToken;
55  TOKEN_PRIVILEGES TP;
56  NTSTATUS status;
57 
58  status = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
59  if (!NT_SUCCESS(status))
60  {
61  return FALSE;
62  }
63 
64  TP.PrivilegeCount = 1;
65  TP.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
66 
67  status = LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &TP.Privileges[0].Luid);
68  if (!NT_SUCCESS(status))
69  {
70  return FALSE;
71  }
72 
73  status = AdjustTokenPrivileges(hToken, FALSE, &TP, sizeof(TP), NULL, NULL);
74  if (!NT_SUCCESS(status))
75  {
76  return FALSE;
77  }
78 
79  return TRUE;
80  }
81  //
82  // CmosRWIo functions
83  //
84 
85  // REGULAR CONSTRUCTOR
86  CmosRWIo::CmosRWIo ()
87  : ICmosRW(), Suppressable()
88  {
89 
90  if (!LoadNtdllFuncs())
91  throw smbios::NotImplementedImpl("Error loading DLLs needed to call functions to read CMOS.");
92 
93  if (!EnableDebug())
94  throw smbios::NotImplementedImpl("Error loading DLLs needed to call functions to read CMOS.");
95  }
96 
97  // COPY CONSTRUCTOR
98  CmosRWIo::CmosRWIo (const CmosRWIo &)
99  : ICmosRW(), Suppressable()
100  {}
101 
102  // OVERLOADED ASSIGNMENT
103  CmosRWIo & CmosRWIo::operator = (const CmosRWIo &)
104  {
105  return *this;
106  }
107 
108  // TODO: need to throw exception on problem with iopl
109  //
110  u8 CmosRWIo::readByte (u32 indexPort, u32 dataPort, u32 offset) const
111  {
112  NTSTATUS status;
113  u8 Buf;
114 
115  IO_STRUCT io;
116 
117  memset(&io, 0, sizeof(io));
118  io.Addr = indexPort;
119  io.pBuf = &offset;
120  io.NumBytes = 1;
121  io.Reserved4 = 1;
122  io.Reserved6 = 1;
123 
124  status = ZwSystemDebugControl(DebugSysWriteIoSpace, &io, sizeof(io), NULL, 0, NULL);
125  if (!NT_SUCCESS(status))
126  throw smbios::NotImplementedImpl();
127 
128  memset(&io, 0, sizeof(io));
129  io.Addr = dataPort;
130  io.pBuf = &Buf;
131  io.NumBytes = 1;
132  io.Reserved4 = 1;
133  io.Reserved6 = 1;
134 
135  status = ZwSystemDebugControl(DebugSysReadIoSpace, &io, sizeof(io), NULL, 0, NULL);
136  if (!NT_SUCCESS(status))
137  throw smbios::NotImplementedImpl();
138 
139  return Buf;
140  }
141 
142  // TODO: need to throw exception on problem with iopl
143  //
144  void CmosRWIo::writeByte (u32 indexPort, u32 dataPort, u32 offset, u8 byte) const
145  {
146  (void) indexPort;
147  (void) dataPort;
148  (void) offset;
149  (void) byte;
150  throw smbios::NotImplementedImpl();
151 
152 // enable this whenever we get write support on windows.
153 #if 0
154  if(! isNotifySuppressed() )
155  {
156  notify();
157  }
158 #endif
159  }
160 
161 }