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 <errno.h>
00024 #include <sys/mman.h>
00025 #include <unistd.h>
00026
00027 #include "MemoryImpl.h"
00028
00029
00030 #include "smbios/message.h"
00031
00032 using namespace std;
00033 using namespace factory;
00034
00035 namespace memory
00036 {
00037 struct LinuxData
00038 {
00039 FILE *fd;
00040 void *lastMapping;
00041 unsigned long lastMappedOffset;
00042 unsigned long mappingSize;
00043 int reopenHint;
00044 string filename;
00045 };
00046
00047 static void condOpenFd(struct LinuxData *data)
00048 {
00049 if(!data->fd)
00050 {
00051 data->lastMapping = 0;
00052 data->lastMappedOffset = 0;
00053 data->fd = fopen( data->filename.c_str(), "rb" );
00054 if(!data->fd)
00055 {
00056 AccessErrorImpl accessError;
00057 accessError.setMessageString( _("Unable to open memory. File: %(file)s, OS Error: %(err)s") );
00058 accessError.setParameter( "file", data->filename );
00059 accessError.setParameter( "err", strerror(errno) );
00060 throw accessError;
00061 }
00062 }
00063 }
00064
00065 static void closeFd(struct LinuxData *data)
00066 {
00067 if(data->lastMapping)
00068 {
00069 munmap(data->lastMapping, data->mappingSize);
00070 data->lastMapping = 0;
00071 }
00072 if (data->fd)
00073 {
00074 fclose(data->fd);
00075 data->fd = 0;
00076 }
00077 data->lastMappedOffset = 0;
00078 }
00079
00080 MemoryFactoryImpl::MemoryFactoryImpl()
00081 {
00082 setParameter("memFile", "/dev/mem");
00083 }
00084
00085 MemoryOsSpecific::MemoryOsSpecific( const string filename )
00086 : IMemory()
00087 {
00088 LinuxData *data = new LinuxData();
00089 data->fd = 0;
00090 data->filename = filename;
00091 data->mappingSize = getpagesize() * 16;
00092 data->reopenHint = 1;
00093 condOpenFd(data);
00094 closeFd(data);
00095 osData = static_cast<void *>(data);
00096 }
00097
00098 MemoryOsSpecific::~MemoryOsSpecific()
00099 {
00100 LinuxData *data = static_cast<LinuxData *>(osData);
00101 closeFd(data);
00102 delete data;
00103 osData = 0;
00104 }
00105
00106 int MemoryOsSpecific::incReopenHint()
00107 {
00108 LinuxData *data = static_cast<LinuxData *>(osData);
00109 return ++(data->reopenHint);
00110 }
00111 int MemoryOsSpecific::decReopenHint()
00112 {
00113 LinuxData *data = static_cast<LinuxData *>(osData);
00114 return --(data->reopenHint);
00115 }
00116
00117 void MemoryOsSpecific::fillBuffer( u8 *buffer, u64 offset, unsigned int length) const
00118 {
00119 LinuxData *data = static_cast<LinuxData *>(osData);
00120 unsigned int bytesCopied = 0;
00121
00122 condOpenFd(data);
00123
00124 while( bytesCopied < length )
00125 {
00126 off_t mmoff = offset % data->mappingSize;
00127
00128 if ((offset-mmoff) != data->lastMappedOffset)
00129 {
00130 data->lastMappedOffset = offset-mmoff;
00131 if (data->lastMapping)
00132 munmap(data->lastMapping, data->mappingSize);
00133 data->lastMapping = mmap( 0, data->mappingSize, PROT_READ, MAP_PRIVATE, fileno(data->fd), offset-mmoff);
00134 if ((data->lastMapping) == reinterpret_cast<void *>(-1))
00135 throw AccessErrorImpl(_("mmap failed."));
00136 }
00137
00138 unsigned long toCopy = length - bytesCopied;
00139 if( toCopy + mmoff > (data->mappingSize) )
00140 toCopy = (data->mappingSize) - mmoff;
00141
00142 memcpy(buffer + bytesCopied, (reinterpret_cast<const u8 *>(data->lastMapping) + mmoff), toCopy);
00143 offset += toCopy;
00144 bytesCopied += toCopy;
00145 }
00146
00147 if(data->reopenHint)
00148 closeFd(data);
00149
00150 }
00151
00152 u8 MemoryOsSpecific::getByte( u64 offset ) const
00153 {
00154 u8 value=0;
00155 fillBuffer( &value, offset, 1 );
00156 return value;
00157 }
00158
00159 void MemoryOsSpecific::putByte( u64 offset, u8 value ) const
00160 {
00161 LinuxData *data = static_cast<LinuxData *>(osData);
00162 condOpenFd(data);
00163 int ret = fseek( data->fd, offset, 0 );
00164 if( 0 != ret )
00165 {
00166 OutOfBoundsImpl outOfBounds;
00167 outOfBounds.setMessageString(_("Seek error trying to seek to memory location. OS Error: %(err)s"));
00168 outOfBounds.setParameter("err", strerror(errno) );
00169 closeFd(data);
00170 throw outOfBounds;
00171 }
00172 ret = fwrite( &value, 1, 1, data->fd );
00173 if( 1 != ret )
00174 {
00175 AccessErrorImpl accessError;
00176 accessError.setMessageString(_("Error trying to write memory. OS Error: %(err)s"));
00177 accessError.setParameter("err", strerror(errno) );
00178 closeFd(data);
00179 throw accessError;
00180 }
00181 if(data->reopenHint)
00182 closeFd(data);
00183 }
00184
00185 }