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 "../memory/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("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         {
00092             //printf("Could not find functions in dll!\n");
00093             //TODO change exception
00094             throw smbios::NotImplementedImpl("Error loading DLLs needed to call functions to read CMOS.");
00095         }
00096         if (!EnableDebug())
00097         {
00098             //printf("Could not enable debug privileges!\n");
00099             //TODO change exception
00100             throw smbios::NotImplementedImpl("Error loading DLLs needed to call functions to read CMOS.");
00101         }
00102     }
00103 
00104     // COPY CONSTRUCTOR
00105     CmosRWIo::CmosRWIo (const CmosRWIo &)
00106             : ICmosRW(), Suppressable()
00107     {}
00108 
00109     // OVERLOADED ASSIGNMENT
00110     CmosRWIo & CmosRWIo::operator = (const CmosRWIo &)
00111     {
00112         return *this;
00113     }
00114 
00115     // TODO: need to throw exception on problem with iopl
00116     //
00117     u8 CmosRWIo::readByte (u32 indexPort, u32 dataPort, u32 offset) const
00118     {
00119         NTSTATUS status;
00120         u8 Buf;
00121 
00122         IO_STRUCT io;
00123 
00124         memset(&io, 0, sizeof(io));
00125         io.Addr = indexPort;
00126         io.pBuf = &offset;
00127         io.NumBytes = 1;
00128         io.Reserved4 = 1;
00129         io.Reserved6 = 1;
00130 
00131         status = ZwSystemDebugControl(DebugSysWriteIoSpace, &io, sizeof(io), NULL, 0, NULL);
00132         //if (!NT_SUCCESS(status))
00133         //  throw smbios::NotImplementedImpl();  // TODO Change exception
00134 
00135         memset(&io, 0, sizeof(io));
00136         io.Addr = dataPort;
00137         io.pBuf = &Buf;
00138         io.NumBytes = 1;
00139         io.Reserved4 = 1;
00140         io.Reserved6 = 1;
00141 
00142         status = ZwSystemDebugControl(DebugSysReadIoSpace, &io, sizeof(io), NULL, 0, NULL);
00143         //if (!NT_SUCCESS(status))
00144         //  throw smbios::NotImplementedImpl();  // TODO Change exception
00145 
00146         return Buf;
00147     }
00148 
00149     // TODO: need to throw exception on problem with iopl
00150     //
00151     void CmosRWIo::writeByte (u32 indexPort, u32 dataPort, u32 offset, u8 byte) const
00152     {
00153         (void) indexPort;
00154         (void) dataPort;
00155         (void) offset;
00156         (void) byte;
00157         throw smbios::NotImplementedImpl();
00158 
00159 // enable this whenever we get write support on windows.
00160 #if 0
00161         if(! isNotifySuppressed() )
00162         {
00163             notify();
00164         }
00165 #endif
00166     }
00167 
00168 }

Generated on Tue Jan 17 02:59:07 2006 for SMBIOS Library by  doxygen 1.4.6