SMBIOS Library
Memory_Linux.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 <sys/mman.h> /* mmap */
25 #include <stdio.h>
26 #include <unistd.h> /* getpagesize */
27 #include <string.h> /* memcpy etc. */
28 
29 #include "MemoryImpl.h"
30 
31 // include this last
32 #include "smbios/message.h"
33 
34 using namespace std;
35 using namespace factory;
36 
37 namespace memory
38 {
39  struct LinuxData
40  {
41  FILE *fd;
42  void *lastMapping;
43  unsigned long lastMappedOffset;
44  unsigned long mappingSize;
46  string filename;
47  };
48 
49  static void condOpenFd(struct LinuxData *data)
50  {
51  if(!data->fd)
52  {
53  data->lastMapping = 0;
54  data->lastMappedOffset = 0;
55  data->fd = fopen( data->filename.c_str(), "rb" );
56  if(!data->fd)
57  {
58  AccessErrorImpl accessError;
59  accessError.setMessageString( _("Unable to open memory. File: %(file)s, OS Error: %(err)s") );
60  accessError.setParameter( "file", data->filename );
61  accessError.setParameter( "err", strerror(errno) );
62  throw accessError;
63  }
64  }
65  }
66 
67  static void closeFd(struct LinuxData *data)
68  {
69  if(data->lastMapping)
70  {
71  munmap(data->lastMapping, data->mappingSize);
72  data->lastMapping = 0;
73  }
74  if (data->fd)
75  {
76  fclose(data->fd);
77  data->fd = 0;
78  }
79  data->lastMappedOffset = 0;
80  }
81 
82  MemoryFactoryImpl::MemoryFactoryImpl()
83  {
84  setParameter("memFile", "/dev/mem");
85  }
86 
87  MemoryOsSpecific::MemoryOsSpecific( const string filename )
88  : IMemory()
89  {
90  LinuxData *data = new LinuxData();
91  data->fd = 0;
92  data->filename = filename;
93  data->mappingSize = getpagesize() * 16;
94  data->reopenHint = 1;
95  condOpenFd(data);
96  closeFd(data);
97  osData = static_cast<void *>(data);
98  }
99 
101  {
102  LinuxData *data = static_cast<LinuxData *>(osData);
103  closeFd(data);
104  delete data;
105  osData = 0;
106  }
107 
109  {
110  LinuxData *data = static_cast<LinuxData *>(osData);
111  return ++(data->reopenHint);
112  }
114  {
115  LinuxData *data = static_cast<LinuxData *>(osData);
116  return --(data->reopenHint);
117  }
118 
119  void MemoryOsSpecific::fillBuffer( u8 *buffer, u64 offset, unsigned int length) const
120  {
121  LinuxData *data = static_cast<LinuxData *>(osData);
122  unsigned int bytesCopied = 0;
123 
124  condOpenFd(data);
125 
126  while( bytesCopied < length )
127  {
128  off_t mmoff = offset % data->mappingSize;
129 
130  if ((offset-mmoff) != data->lastMappedOffset)
131  {
132  data->lastMappedOffset = offset-mmoff;
133  if (data->lastMapping)
134  munmap(data->lastMapping, data->mappingSize);
135  data->lastMapping = mmap( 0, data->mappingSize, PROT_READ, MAP_PRIVATE, fileno(data->fd), offset-mmoff);
136  if ((data->lastMapping) == reinterpret_cast<void *>(-1))
137  throw AccessErrorImpl(_("mmap failed."));
138  }
139 
140  unsigned long toCopy = length - bytesCopied;
141  if( toCopy + mmoff > (data->mappingSize) )
142  toCopy = (data->mappingSize) - mmoff;
143 
144  memcpy(buffer + bytesCopied, (reinterpret_cast<const u8 *>(data->lastMapping) + mmoff), toCopy);
145  offset += toCopy;
146  bytesCopied += toCopy;
147  }
148 
149  if(data->reopenHint)
150  closeFd(data);
151 
152  }
153 
154  u8 MemoryOsSpecific::getByte( u64 offset ) const
155  {
156  u8 value=0;
157  fillBuffer( &value, offset, 1 );
158  return value;
159  }
160 
161  void MemoryOsSpecific::putByte( u64 offset, u8 value ) const
162  {
163  LinuxData *data = static_cast<LinuxData *>(osData);
164  condOpenFd(data);
165  int ret = fseek( data->fd, offset, 0 );
166  if( 0 != ret )
167  {
168  OutOfBoundsImpl outOfBounds;
169  outOfBounds.setMessageString(_("Seek error trying to seek to memory location. OS Error: %(err)s"));
170  outOfBounds.setParameter("err", strerror(errno) );
171  closeFd(data);
172  throw outOfBounds;
173  }
174  ret = fwrite( &value, 1, 1, data->fd );
175  if( 1 != ret )
176  {
177  AccessErrorImpl accessError;
178  accessError.setMessageString(_("Error trying to write memory. OS Error: %(err)s"));
179  accessError.setParameter("err", strerror(errno) );
180  closeFd(data);
181  throw accessError;
182  }
183  if(data->reopenHint)
184  closeFd(data);
185  }
186 
187 }