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 #include <errno.h>
00023 #include <string.h>
00024 #include "CmosRWImpl.h"
00025
00026 using namespace std;
00027
00028 namespace cmos
00029 {
00030
00031
00032
00033
00034 void readByteArray ( const ICmosRW &cmos, u32 indexPort, u32 dataPort, u32 offset, u8 * target, u32 count)
00035 {
00036 for (u32 i = 0; i < count; ++i)
00037 {
00038 target[i] = cmos.readByte (indexPort, dataPort, offset + i);
00039 }
00040 }
00041
00042 void writeByteArray ( const ICmosRW &cmos, u32 indexPort, u32 dataPort, u32 offset, const u8 * source, u32 count)
00043 {
00044 const Suppressable *s = dynamic_cast<const Suppressable *>(&cmos);
00045 if(s)
00046 s->suppressNotification();
00047 for (u32 i = 0; i < count; ++i)
00048 {
00049 cmos.writeByte (indexPort, dataPort, offset + i, source[i]);
00050 }
00051 if(s)
00052 s->resumeNotification();
00053 }
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 Suppressable::Suppressable()
00066 : suppressNotify(false)
00067 {}
00068
00069 Suppressable::~Suppressable()
00070 {}
00071
00072 void Suppressable::suppressNotification(bool sup) const
00073 {
00074 suppressNotify = sup;
00075 }
00076
00077 void Suppressable::resumeNotification(bool doNotify) const
00078 {
00079 const observer::IObservable *o = dynamic_cast<const observer::IObservable *>(this);
00080 if(o && doNotify)
00081 o->notify();
00082
00083 suppressNotify = false;
00084 }
00085
00086 bool Suppressable::isNotifySuppressed() const
00087 {
00088 return suppressNotify;
00089 }
00090
00091
00092
00093
00094 ICmosRW::ICmosRW()
00095 {}
00096
00097 ICmosRW::~ICmosRW()
00098 {}
00099
00100
00101
00102
00103
00104
00105 CmosRWFile::CmosRWFile ( const string &File )
00106 :ICmosRW(), Suppressable(), fileName (File)
00107 {}
00108
00109
00110 CmosRWFile::~CmosRWFile()
00111 {}
00112
00113 CmosRWIo::~CmosRWIo()
00114 {}
00115
00116
00117
00118 u8 CmosRWFile::readByte (u32 indexPort, u32 dataPort, u32 offset) const
00119 {
00120 u8 retval = 0xFF;
00121 u32 realOffset = indexPort * 256 + offset;
00122 (void) dataPort;
00123 string errMessage("Could not open CMOS file(" + fileName + ") for reading: ");
00124
00125 FILE *fh = fopen (fileName.c_str (), "rb");
00126 if( !fh )
00127 throw smbios::InternalErrorImpl(errMessage + strerror(errno));
00128
00129 fseek (fh, static_cast<long>(realOffset), SEEK_SET);
00130 size_t numBytes = fread (&retval, 1, sizeof (retval), fh);
00131 fclose (fh);
00132 if (numBytes != sizeof(retval))
00133 throw std::exception();
00134
00135 return retval;
00136 }
00137
00138
00139
00140 void CmosRWFile::writeByte (u32 indexPort, u32 dataPort, u32 offset, u8 byte) const
00141 {
00142
00143 u32 realOffset = indexPort * 256 + offset;
00144 (void) dataPort;
00145 string errMessage("Could not open CMOS file(" + fileName + ") for writing: ");
00146
00147 FILE *fh = fopen (fileName.c_str (), "r+b");
00148 if( !fh )
00149 throw smbios::InternalErrorImpl(errMessage + strerror(errno));
00150
00151 fseek (fh, static_cast<long>(realOffset), SEEK_SET);
00152 fwrite (&byte, 1, sizeof (byte), fh);
00153 fclose (fh);
00154 fflush(NULL);
00155
00156 if(! isNotifySuppressed() )
00157 {
00158
00159
00160
00161 notify();
00162 }
00163 return;
00164 }
00165
00166
00167 }