• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

TokenDA.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 <iomanip>
00024 #include <string.h>
00025 
00026 #include "TokenImpl.h"
00027 
00028 #include "smbios/ISmi.h"
00029 
00030 #define TODO do { throw NotImplementedImpl(); } while(0)
00031 
00032 using namespace std;
00033 
00034 #if defined(DEBUG_TOKEN_DA)
00035 #   define DCOUT(line) do { cout << line; } while(0)
00036 #   define DCERR(line) do { cerr << line; } while(0)
00037 #else
00038 #   define DCOUT(line) do {} while(0)
00039 #   define DCERR(line) do {} while(0)
00040 #endif
00041 
00042 namespace smbios
00043 {
00044     SmiTokenDA::SmiTokenDA( const smbios::ISmbiosItem &initItem, const calling_interface_token *initToken )
00045             : IToken(), ISmiToken(), IProtectedToken(), item(initItem.clone()), password("")
00046     {
00047         memcpy( const_cast<calling_interface_token *>(&token), initToken, sizeof(token) );
00048 
00049         size_t size;
00050         const u8 *ptr =  item->getBufferCopy(size) ; // MUST DELETE[]!
00051         memcpy( const_cast<calling_interface_structure*>(&structure), ptr, sizeof(structure) );
00052         delete [] const_cast<u8 *>(ptr); //const_cast to fix msvc++
00053     }
00054 
00055     // no dynamically allocated memory, yay!
00056     SmiTokenDA::~SmiTokenDA() throw()
00057     {}
00058 
00059     string SmiTokenDA::getTokenClass() const
00060     {
00061         return "TokenDA";
00062     }
00063 
00064     u32 SmiTokenDA::getValueFormat() const
00065     {
00066         return 0xFFFFFFFF;
00067     }
00068 
00069     bool SmiTokenDA::tryPassword(std::string pw) const
00070     {
00071         // can't really validate password without retrying operation
00072         password = pw;
00073         return true;
00074     }
00075 
00076     const ISmbiosItem &SmiTokenDA::getItemRef() const
00077     {
00078         return *item;
00079     }
00080 
00081     void SmiTokenDA::getSmiDetails(  u16 *cmdIOAddress, u8 *cmdIOCode, u8 *location ) const
00082     {
00083         if (cmdIOAddress)
00084             *cmdIOAddress = structure.cmdIOAddress;
00085         if (cmdIOCode)
00086             *cmdIOCode = structure.cmdIOCode;
00087         if (location)
00088             *location = token.location;
00089     }
00090 
00091     u32 SmiTokenDA::getType() const
00092     {
00093         return token.tokenId;
00094     }
00095 
00096     bool SmiTokenDA::isActive() const
00097     {
00098         bool ret = false;
00099 
00100         DCERR("reading token: " << static_cast<u32>(token.location) << "  compare with value: " << static_cast<u32>(token.value) << "  actual: " << smi::readNVStorage(token.location, 0, 0) << endl);
00101         if (token.value == smi::readNVStorage(token.location, 0, 0))
00102             ret = true;
00103 
00104         return ret;
00105     }
00106 
00107     static void executeWithPassword(smi::IDellCallingInterfaceSmi *ci, u8 arg, string password)
00108     {
00109         for(int i=0; i<2; i++)
00110         {
00111             try
00112             {
00113                 ci->execute();
00114                 break;
00115             }
00116             catch(const smi::SmiExecutedWithError &)
00117             {
00118                 // on second time through, just pass exception upwards.
00119                 if(i==1)
00120                     throw;
00121 
00122                 //cout << "Caught error. Might be bad password. Trying password: " << password << endl;
00123                 ci->setArg( arg, smi::getAuthenticationKey(password));
00124             }
00125         }
00126     }
00127 
00128     void SmiTokenDA::activate() const
00129     {
00130         DCERR("trying to activate token: " << static_cast<u32>(token.location) << "  with value: " << static_cast<u32>(token.value) << endl);
00131         smi::writeNVStorage(password, token.location, token.value, 0, 0);
00132     }
00133 
00134     bool SmiTokenDA::isString() const
00135     {
00136         return true;
00137     }
00138 
00139     bool SmiTokenDA::isBool() const
00140     {
00141         return true;
00142     }
00143 
00144     const string SmiTokenDA::getString(u8 *byteArray, unsigned int size ) const
00145     {
00146         std::auto_ptr<smi::IDellCallingInterfaceSmi> smi = smi::SmiFactory::getFactory()->makeNew(smi::SmiFactory::DELL_CALLING_INTERFACE_SMI);
00147 
00148         smi::IDellCallingInterfaceSmi *ci = dynamic_cast<smi::IDellCallingInterfaceSmi *>(smi.get());
00149         ci->setClass( 0x0 );  /* Read Non-Volatile Storage class code */
00150         ci->setSelect( 0x0 );  /* Read Non-Volatile Storage select code */
00151         ci->setArg( 0, token.location );
00152         ci->execute();
00153 
00154         // first word is data. ignore high bits.
00155         u16 word = static_cast<u16>(ci->getRes(1));
00156 
00157         if(byteArray && size >= 2)
00158         {
00159             memset(byteArray, 0, size);
00160             memcpy(byteArray, &word, sizeof(u16));
00161         }
00162 
00163         char ret[3]={0};
00164         memcpy(ret, &word, sizeof(u16));
00165 
00166         return ret; //automatically converted to std::string
00167     }
00168 
00169     void SmiTokenDA::setString( const u8 *byteArray, size_t size ) const
00170     {
00171         if( size < 2 )
00172             return;
00173 
00174         std::auto_ptr<smi::IDellCallingInterfaceSmi> smi = smi::SmiFactory::getFactory()->makeNew(smi::SmiFactory::DELL_CALLING_INTERFACE_SMI);
00175 
00176         smi::IDellCallingInterfaceSmi *ci = dynamic_cast<smi::IDellCallingInterfaceSmi *>(smi.get());
00177         ci->setClass( 0x1 );  /* Read Non-Volatile Storage class code */
00178         ci->setSelect( 0x0 );  /* Read Non-Volatile Storage select code */
00179         ci->setArg( 0, token.location );
00180         ci->setArg( 1, *reinterpret_cast<const u16 *>(byteArray) );
00181         executeWithPassword(ci, 2, password);
00182     }
00183 
00184     unsigned int SmiTokenDA::getStringLength() const
00185     {
00186         // pretend all SMI tokens are one word
00187         return 2;
00188     }
00189 
00190     std::ostream & SmiTokenDA::streamify( std::ostream & cout ) const
00191     {
00192         std::ios::fmtflags old_opts = cout.flags ();
00193 
00194         cout << hex << setfill('0');
00195         cout << "DMI type 0x"       << setw(2) << static_cast<int>(structure.type);
00196         cout << "  Handle 0x"       << setw(4) << static_cast<int>(structure.handle);
00197         cout << "  CmdIO Port 0x"  << setw(4) << static_cast<int>(structure.cmdIOAddress);
00198         cout << "  CmdIO Code 0x"  << setw(2) << static_cast<int>(structure.cmdIOCode);
00199         cout << "  Type 0x"         << setw(4) << static_cast<int>(getType());
00200         cout << "  Location 0x"     << setw(4) << static_cast<int>(token.location);
00201         cout << " value "           << setw(4) << static_cast<int>(token.value);
00202 
00203         cout.flags (old_opts);
00204 
00205         return cout;
00206     }
00207 
00208 }

Generated on Sun Aug 22 2010 14:38:31 for SMBIOS Library by  doxygen 1.7.1