Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

CmosRW_WindowsIO.cpp

Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  * vim:expandtab:autoindent:tabstop=4:shiftwidth=4:filetype=c:cindent:textwidth=0:
00003  *
00004  * Copyright (C) 2005 Dell Inc.
00005  *  by Michael Brown <Michael_E_Brown@dell.com>
00006  * Licensed under the Open Software License version 2.1
00007  *
00008  * Alternatively, you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published
00010  * by the Free Software Foundation; either version 2 of the License,
00011  * or (at your option) any later version.
00012 
00013  * This program is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016  * See the GNU General Public License for more details.
00017  */
00018 
00019 // compat header should always be first header if including system headers
00020 #define LIBSMBIOS_SOURCE
00021 #include "smbios/compat.h"
00022 
00023 #include <stdio.h>
00024 #include <conio.h>
00025 
00026 #include "CmosRWImpl.h"
00027 #include "miniddk.h"
00028 
00029 using namespace std;
00030 
00031 namespace cmos
00032 {
00033 
00034     // Non Member Functions
00035     ZwSystemDebugControlPtr ZwSystemDebugControl;
00036 
00037     int LoadNtdllFuncs(void)
00038     {
00039         HMODULE hNtdll;
00040 
00041         hNtdll = GetModuleHandle(L"ntdll.dll");
00042         if (!hNtdll)
00043             return FALSE;
00044 
00045         ZwSystemDebugControl = (ZwSystemDebugControlPtr) GetProcAddress(hNtdll, "ZwSystemDebugControl");
00046         if (!ZwSystemDebugControl)
00047             return FALSE;
00048 
00049         return TRUE;
00050     }
00051 
00052     int EnableDebug(void)
00053     {
00054         HANDLE hToken;
00055         TOKEN_PRIVILEGES TP;
00056         NTSTATUS status;
00057 
00058         status = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
00059         if (!NT_SUCCESS(status))
00060         {
00061             return FALSE;
00062         }
00063 
00064         TP.PrivilegeCount = 1;
00065         TP.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
00066 
00067         status = LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &TP.Privileges[0].Luid);
00068         if (!NT_SUCCESS(status))
00069         {
00070             return FALSE;
00071         }
00072 
00073         status = AdjustTokenPrivileges(hToken, FALSE, &TP, sizeof(TP), NULL, NULL);
00074         if (!NT_SUCCESS(status))
00075         {
00076             return FALSE;
00077         }
00078 
00079         return TRUE;
00080     }
00081     //
00082     // CmosRWIo functions
00083     //
00084 
00085     // REGULAR CONSTRUCTOR
00086     CmosRWIo::CmosRWIo ()
00087             : ICmosRW(), Suppressable()
00088     {
00089 
00090         if (!LoadNtdllFuncs())
00091             throw smbios::NotImplementedImpl("Error loading DLLs needed to call functions to read CMOS.");
00092 
00093         if (!EnableDebug())
00094             throw smbios::NotImplementedImpl("Error loading DLLs needed to call functions to read CMOS.");
00095     }
00096 
00097     // COPY CONSTRUCTOR
00098     CmosRWIo::CmosRWIo (const CmosRWIo &)
00099             : ICmosRW(), Suppressable()
00100     {}
00101 
00102     // OVERLOADED ASSIGNMENT
00103     CmosRWIo & CmosRWIo::operator = (const CmosRWIo &)
00104     {
00105         return *this;
00106     }
00107 
00108     // TODO: need to throw exception on problem with iopl
00109     //
00110     u8 CmosRWIo::readByte (u32 indexPort, u32 dataPort, u32 offset) const
00111     {
00112         NTSTATUS status;
00113         u8 Buf;
00114 
00115         IO_STRUCT io;
00116 
00117         memset(&io, 0, sizeof(io));
00118         io.Addr = indexPort;
00119         io.pBuf = &offset;
00120         io.NumBytes = 1;
00121         io.Reserved4 = 1;
00122         io.Reserved6 = 1;
00123 
00124         status = ZwSystemDebugControl(DebugSysWriteIoSpace, &io, sizeof(io), NULL, 0, NULL);
00125         if (!NT_SUCCESS(status))
00126           throw smbios::NotImplementedImpl();
00127 
00128         memset(&io, 0, sizeof(io));
00129         io.Addr = dataPort;
00130         io.pBuf = &Buf;
00131         io.NumBytes = 1;
00132         io.Reserved4 = 1;
00133         io.Reserved6 = 1;
00134 
00135         status = ZwSystemDebugControl(DebugSysReadIoSpace, &io, sizeof(io), NULL, 0, NULL);
00136         if (!NT_SUCCESS(status))
00137           throw smbios::NotImplementedImpl();
00138 
00139         return Buf;
00140     }
00141 
00142     // TODO: need to throw exception on problem with iopl
00143     //
00144     void CmosRWIo::writeByte (u32 indexPort, u32 dataPort, u32 offset, u8 byte) const
00145     {
00146         (void) indexPort;
00147         (void) dataPort;
00148         (void) offset;
00149         (void) byte;
00150         throw smbios::NotImplementedImpl();
00151 
00152 // enable this whenever we get write support on windows.
00153 #if 0
00154         if(! isNotifySuppressed() )
00155         {
00156             notify();
00157         }
00158 #endif
00159     }
00160 
00161 }

Generated on Wed Apr 11 16:25:10 2007 for SMBIOS Library by doxygen 1.3.5