Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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) ;
00051 memcpy( const_cast<calling_interface_structure*>(&structure), ptr, sizeof(structure) );
00052 delete [] const_cast<u8 *>(ptr);
00053 }
00054
00055
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
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
00119 if(i==1)
00120 throw;
00121
00122
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 );
00150 ci->setSelect( 0x0 );
00151 ci->setArg( 0, token.location );
00152 ci->execute();
00153
00154
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;
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 );
00178 ci->setSelect( 0x0 );
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
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 }