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