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

IdByte.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 #define LIBSMBIOS_SOURCE
00020 #include "smbios/ISmbios.h"
00021 #include "smbios/IToken.h"
00022 
00023 #include "smbios/SystemInfo.h"
00024 #include "smbios/IMemory.h"
00025 #include "smbios/SmbiosDefs.h"
00026 #include "ExceptionImpl.h"
00027 
00028 #include "SystemDetect.h"
00029 #include "DellMagic.h"
00030 
00031 // should always be included last.
00032 #include "smbios/message.h"
00033 
00034 using namespace smbios;
00035 using namespace cmos;
00036 using namespace std;
00037 
00038 extern smbios::Exception<smbios::IException> SysInfoException;
00039 
00040 //
00041 //
00042 // ID Byte functions
00043 //
00044 //
00045 static u16 getIdByteFromMem ()
00046 {
00047     u16 tempWord = 0;
00048     u16 idWord = 0;
00049     memory::IMemory *mem = 0;
00050 
00051     struct two_byte_structure tbs;
00052     struct two_byte_structure *ptbs = &tbs;
00053     struct one_byte_structure *pobs =
00054                     reinterpret_cast<one_byte_structure*>(&(tbs.bios_version));
00055 
00056     mem = memory::MemoryFactory::getFactory()->getSingleton();
00057 
00058     if( 0 == mem )
00059         throw InternalErrorImpl();
00060 
00061     // Step 1: Check that "Dell System" is present at the proper offset
00062     u8 strBuf[DELL_SYSTEM_STRING_LEN] = { 0, };
00063     mem->fillBuffer( strBuf, DELL_SYSTEM_STRING_LOC, DELL_SYSTEM_STRING_LEN - 1 );
00064     if( strncmp( reinterpret_cast<char*>(strBuf), DELL_SYSTEM_STRING, DELL_SYSTEM_STRING_LEN ) != 0 )
00065         goto out;
00066 
00067     // Step 2: fill the id structs
00068     mem->fillBuffer( reinterpret_cast<u8 *>(ptbs), TWO_BYTE_STRUCT_LOC, sizeof(two_byte_structure) );
00069 
00070     // Step 3: check the checksum of one-byte struct
00071     //    update: checksum is not reliable, so don't use it...
00072 
00073     // Step 4: Check one byte ID
00074     tempWord = pobs->system_id;
00075 
00076     // Step 5: if 0xFE, then it is a double byte (word) ID.
00077     // *  -- byte at 0xFE845 is 0xFE
00078     if (0xFE == tempWord)
00079 {
00080         // Step 6: check two byte struct checksum
00081         // *  -- three bytes at 0xFE845 sum to 0x00 but are not all 0x00.
00082         //*  -- extension checksum is 0
00083 
00084         // Step 7: get ID.
00085         tempWord = ptbs->two_byte_id;
00086     }
00087 
00088     idWord = tempWord;
00089 
00090 out:
00091     return idWord;
00092 }
00093 
00094 static u16 getIdByteFromMem_Diamond ()
00095 {
00096     u16 idWord = 0;
00097     memory::IMemory *mem = 0;
00098     u8 strBuf[DELL_SYSTEM_STRING_LEN] = { 0, };
00099 
00100     mem = memory::MemoryFactory::getFactory()->getSingleton();
00101 
00102     if( 0 == mem )
00103         throw InternalErrorImpl();
00104 
00105     // Step 1: Check that "Dell System" is present at the proper offset
00106     mem->fillBuffer( strBuf, DELL_SYSTEM_STRING_LOC_DIAMOND_1, DELL_SYSTEM_STRING_LEN - 1 );
00107     if( strncmp( reinterpret_cast<char*>(strBuf), DELL_SYSTEM_STRING, DELL_SYSTEM_STRING_LEN ) == 0 )
00108         if( SYSTEM_ID_DIAMOND == mem->getByte( ID_BYTE_LOC_DIAMOND_1 ) )
00109             idWord = SYSTEM_ID_DIAMOND;
00110 
00111     mem->fillBuffer( strBuf, DELL_SYSTEM_STRING_LOC_DIAMOND_2, DELL_SYSTEM_STRING_LEN - 1 );
00112     if( strncmp( reinterpret_cast<char*>(strBuf), DELL_SYSTEM_STRING, DELL_SYSTEM_STRING_LEN ) == 0 )
00113         if( SYSTEM_ID_DIAMOND == mem->getByte( ID_BYTE_LOC_DIAMOND_2 ) )
00114             idWord = SYSTEM_ID_DIAMOND;
00115 
00116     return idWord;
00117 }
00118 
00119 static u16 getIdByteFromOEMItem ()
00120 {
00121     //functionEnter( "%s", "" );
00122     u16 idWord = 0;
00123     smbios::ISmbiosTable *table = 0;
00124     smbios::ISmbiosTable::iterator item;
00125     if (!couldBeBayonet())
00126         goto out;
00127 
00128     table = smbios::SmbiosFactory::getFactory()->getSingleton();
00129 
00130     if (0 == table)
00131         throw InternalErrorImpl();
00132 
00133     // search through 0x0B (OEM_Strings) items
00134     for( item = (*table)[OEM_Strings] ; item != table->end(); ++item)
00135     {
00136         const char *str = item->getStringByStringNumber (2);
00137         //isBayonet = true;
00138         //  Id byte is in second string in table 0x0B
00139         //  the format is "n[NN]", where NN is the idbyte;
00140         //  note the &str[2] below to skip the 'n['
00141         if( 0 != str )
00142             // quiet vc.net warning using cast
00143             idWord = static_cast<u16>(strtol( &str[2], NULL, 16 ));
00144     }
00145 
00146 out:
00147     //functionLeave( "\t\tretval = %i\n", (int)idWord );
00148     return idWord;
00149 }
00150 
00151 static u16 getIdByteFromRevItem ()
00152 {
00153     //functionEnter( "%s", "" );
00154     u16 idWord = 0;
00155     smbios::ISmbiosTable *table = 0;
00156     smbios::ISmbiosTable::iterator item;
00157 
00158     table = smbios::SmbiosFactory::getFactory()->getSingleton();
00159 
00160     if (0 == table)
00161         throw InternalErrorImpl();
00162 
00163     // search through 0x0B (Revisions_and_IDs_Structure)
00164     for( item = (*table)[Dell_Revisions_and_IDs]; item != table->end(); ++item)
00165     {
00166         //If byte field is 0xFE, we need to look in the extension field
00167         idWord = getU8_FromItem(*item, 0x06);
00168         if( 0xFE == idWord )
00169         {
00170             idWord = getU16_FromItem(*item, 0x08);
00171         }
00172     }
00173     //functionLeave( "\t\tretval = %i\n", (int)idWord );
00174     return idWord;
00175 }
00176 
00177 //The code for detecting ID byte in case of Diamond is left out.
00178 //  need to write a function for it.
00179 struct DellIdByteFunctions
00180 {
00181     u16 (*f_ptr)();
00182 }
00183 DellIdByteFunctions[] = {
00184                             {&getIdByteFromMem,},       // normal system -- try this last always.
00185 
00186                             {&getIdByteFromOEMItem,},   // bayonet
00187                             {&getIdByteFromMem_Diamond,}, // diamond
00188 
00189                             // do this last because this may contain an OEM id
00190                             // do this as a last resort because it is
00191                             // unreliable.
00192                             {&getIdByteFromRevItem,},   // Dell Smbios Revisions and ID's struct
00193                         };
00194 
00195 
00196 
00197 
00198 int SMBIOSGetDellSystemId()
00199 {
00200     //functionEnter( "%s", "" );
00201     int systemId = 0;
00202     int numEntries =
00203         sizeof (DellIdByteFunctions) / sizeof (DellIdByteFunctions[0]);
00204 
00205     for (int i = 0; i < numEntries; ++i)
00206     {
00207         // eat exceptions from lowlevel functions and keep going.
00208         try
00209         {
00210             // first function to return non-zero id wins.
00211             systemId = DellIdByteFunctions[i].f_ptr ();
00212         }
00213         catch(const smbios::IException &e)
00214         {
00215             SysInfoException.setMessageString(e.what());
00216         }
00217         catch(...)
00218         {
00219             SysInfoException.setMessageString( _("Unknown internal error occurred") );
00220         }
00221         if (0 != systemId)
00222         {
00223             break;
00224         }
00225     }
00226 
00227     return systemId;
00228 }
00229 

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