1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim:expandtab:autoindent:tabstop=4:shiftwidth=4:filetype=c:cindent:textwidth=0:
*
* Copyright (C) 2005 Dell Inc.
* by Michael Brown <Michael_E_Brown@dell.com>
* Licensed under the Open Software License version 2.1
*
* Alternatively, you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*/
// compat header should always be first header if including system headers
#define LIBSMBIOS_SOURCE
#include "smbios/compat.h"
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "MemoryImpl.h"
// include this file last
#include "smbios/message.h"
using namespace std;
namespace memory
{
MemoryFactory::~MemoryFactory() throw()
{}
MemoryFactory::MemoryFactory()
{}
MemoryFactory *MemoryFactory::getFactory()
{
// reinterpret_cast<...>(0) to ensure template parameter is correct
// this is a workaround for VC6 which cannot use explicit member template
// function initialization.
return MemoryFactoryImpl::getFactory(reinterpret_cast<MemoryFactoryImpl *>(0));
}
IMemory *MemoryFactoryImpl::_mem_instance = 0;
MemoryFactoryImpl::~MemoryFactoryImpl() throw()
{
if( _mem_instance )
{
delete _mem_instance;
}
_mem_instance = 0;
}
//
// MemoryFactoryImpl::MemoryFactoryImpl() // Constructor
// -- Moved to the Memory_OSNAME.cc file
// so that the default parameters can be OS-Specific
//
IMemory *MemoryFactoryImpl::getSingleton()
{
if (! _mem_instance)
_mem_instance = makeNew();
return _mem_instance;
}
IMemory *MemoryFactoryImpl::makeNew()
{
if (mode == UnitTestMode)
{
return new MemoryFile( getParameterString("memFile") );
}
else if (mode == AutoDetectMode)
{
return new MemoryOsSpecific( getParameterString("memFile") );
}
else
{
throw smbios::NotImplementedImpl( _("Unknown Memory mode requested.") );
}
}
//
// IMemory
//
IMemory::~IMemory ()
{}
IMemory::IMemory ()
{}
MemoryFile::MemoryFile( const string initFilename )
: IMemory(), filename(initFilename), fd(0), rw(false), reopenHint(1)
{
// workaround MSVC++ bugs...
if( filename == "" )
{
throw AccessErrorImpl( _("File name passed in was null or zero-length.") );
}
// fopen portable to Windows if "b" is added to mode.
fd = fopen( filename.c_str(), "rb" ); // open for read to start
if (!fd)
{
AccessErrorImpl accessError;
accessError.setMessageString( _("Unable to open memory. File: %(file)s, OS Error: %(err)s") );
accessError.setParameter( "file", filename );
accessError.setParameter( "err", strerror(errno) );
throw accessError;
}
if (reopenHint>0)
{
fclose(fd);
fd=0;
}
}
MemoryFile::~MemoryFile()
{
if (fd)
{
fclose(fd);
fd = 0;
}
}
u8 MemoryFile::getByte( u64 offset ) const
{
u8 value = 0;
fillBuffer( &value, offset, 1 );
return value;
}
void MemoryFile::fillBuffer(u8 *buffer, u64 offset, unsigned int length) const
{
if (!fd)
{
// fopen portable to Windows if "b" is added to mode.
fd = fopen( filename.c_str(), "rb" ); // open for read to start
if (!fd)
{
AccessErrorImpl accessError;
accessError.setMessageString( _("Unable to open memory. File: %(file)s, OS Error: %(err)s") );
accessError.setParameter( "file", filename );
accessError.setParameter( "err", strerror(errno) );
throw accessError;
}
}
// FSEEK is a macro defined in config/ for portability
int ret = FSEEK(fd, offset, 0);
if (ret)
{
OutOfBoundsImpl outOfBounds;
outOfBounds.setMessageString(_("Seek error trying to seek to memory location. OS Error: %(err)s"));
outOfBounds.setParameter("err", strerror(errno) );
fclose(fd);
fd = 0;
throw outOfBounds;
}
size_t bytesRead = fread( buffer, 1, length, fd );
if (reopenHint>0)
{
fclose(fd);
fd=0;
}
// TODO: handle short reads
if ((length != bytesRead))
{
AccessErrorImpl accessError;
accessError.setMessageString(_("Read error trying to read memory. OS Error: %(err)s"));
accessError.setParameter("err", strerror(errno) );
if(fd)
{
fclose(fd);
fd = 0;
}
throw accessError;
}
}
void MemoryFile::putByte( u64 offset, u8 byte ) const
{
if(!rw || !fd)
{
if(fd)
{
fclose(fd);
fd = 0;
}
fd = fopen( filename.c_str(), "r+b" ); // reopen for write
if(!fd)
{
AccessErrorImpl accessError;
accessError.setMessageString( _("Unable to re-open memory file for writing. File: %(file)s, OS Error: %(err)s") );
accessError.setParameter( "file", filename );
accessError.setParameter( "err", strerror(errno) );
throw accessError;
}
}
// FSEEK is a macro defined in config/ for portability
int ret = FSEEK(fd, offset, 0);
if( 0 != ret )
{
OutOfBoundsImpl outOfBounds;
outOfBounds.setMessageString(_("Seek error trying to seek to memory location. OS Error: %(err)s"));
outOfBounds.setParameter("err", strerror(errno) );
fclose(fd);
fd = 0;
throw outOfBounds;
}
size_t bytesRead = fwrite( &byte, 1, 1, fd );
if (reopenHint > 0)
{
fclose(fd);
fd = 0;
}
if( 1 != bytesRead )
{
AccessErrorImpl accessError;
accessError.setMessageString(_("Error trying to write memory. OS Error: %(err)s"));
accessError.setParameter("err", strerror(errno) );
if(fd)
{
fclose(fd);
fd = 0;
}
throw accessError;
}
}
}
|