SMBIOS Library
Smi_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 <sys/file.h>
24 #include <string.h>
25 
26 #include "SmiImpl.h"
27 
28 using namespace std;
29 
30 #define SMI_DATA_FILE "/sys/devices/platform/dcdbas/smi_data"
31 #define SMI_PHYS_ADDR_FILE "/sys/devices/platform/dcdbas/smi_data_buf_phys_addr"
32 #define SMI_DO_REQUEST_FILE "/sys/devices/platform/dcdbas/smi_request"
33 #define SMI_BUF_SIZE_FILE "/sys/devices/platform/dcdbas/smi_data_buf_size"
34 
35 //#define SMI_DATA_FILE "/tmp/smi_data"
36 //#define SMI_PHYS_ADDR_FILE "/tmp/smi_data_buf_phys_addr"
37 //#define SMI_DO_REQUEST_FILE "/tmp/smi_request"
38 
40 {
41  FILE *fh_data;
42  FILE *fh_doReq;
43 };
44 
45 namespace smi
46 {
47  SmiArchStrategy::SmiArchStrategy()
48  {
49  privateData = new smiLinuxPrivateData;
50  memset(privateData, 0, sizeof(smiLinuxPrivateData));
51  }
52 
53  SmiArchStrategy::~SmiArchStrategy()
54  {
55  smiLinuxPrivateData *tmpPrivPtr = reinterpret_cast<smiLinuxPrivateData *>(privateData);
56 
57  if(tmpPrivPtr->fh_data)
58  fclose(tmpPrivPtr->fh_data);
59 
60  if(tmpPrivPtr->fh_doReq)
61  fclose(tmpPrivPtr->fh_doReq);
62 
63  delete tmpPrivPtr;
64  privateData = 0;
65  }
66 
67  void SmiArchStrategy::lock()
68  {
69  smiLinuxPrivateData *tmpPrivPtr = reinterpret_cast<smiLinuxPrivateData *>(privateData);
70 
71 
72  tmpPrivPtr->fh_data = fopen(SMI_DATA_FILE, "r+b");
73  if( ! tmpPrivPtr->fh_data )
74  throw smbios::InternalErrorImpl("Could not open file " SMI_DATA_FILE ". Check that dcdbas driver is properly loaded.");
75 
76  tmpPrivPtr->fh_doReq = fopen(SMI_DO_REQUEST_FILE, "wb");
77  if( ! tmpPrivPtr->fh_doReq)
78  throw smbios::InternalErrorImpl("Could not open file " SMI_DO_REQUEST_FILE ". Check that dcdbas driver is properly loaded.");
79 
80  flock( fileno(tmpPrivPtr->fh_data), LOCK_EX );
81 
82  fseek(tmpPrivPtr->fh_doReq, 0L, 0);
83  fwrite("0", 1, 1, tmpPrivPtr->fh_doReq);
84  fseek(tmpPrivPtr->fh_doReq, 0L, 0);
85  }
86 
87  size_t SmiArchStrategy::getPhysicalBufferBaseAddress()
88  {
89  const int bufSize=63;
90  char tmpBuf[bufSize+1] = {0,};
91  size_t retval = 0;
92 
93  fflush(NULL);
94 
95  FILE *fh = fopen(SMI_PHYS_ADDR_FILE, "rb");
96  if( ! fh )
97  throw smbios::InternalErrorImpl("Could not open file " SMI_PHYS_ADDR_FILE ". Check that dcdbas driver is properly loaded.");
98 
99  fseek(fh, 0L, 0);
100  size_t numBytes = fread(tmpBuf, 1, bufSize, fh);
101  fclose(fh);
102  fh=0;
103  if (!numBytes) // dont care how many bytes as long as we get at least 1.
104  throw smbios::InternalErrorImpl("Short read from physical address file. Driver problem?");
105 
106  retval = strtoll(tmpBuf, NULL, 16);
107 
108  return retval;
109  }
110 
111  void SmiArchStrategy::setSize(int newSize)
112  {
113  const int bufSize=63;
114  char tmpBuf[bufSize+1] = {0,};
115 
116  fflush(NULL);
117 
118  FILE *fh = fopen(SMI_BUF_SIZE_FILE, "w+b");
119  if( ! fh )
120  throw smbios::InternalErrorImpl("Could not open file " SMI_BUF_SIZE_FILE ". Check that dcdbas driver is properly loaded.");
121 
122  snprintf(tmpBuf, bufSize, "%d", newSize);
123  fwrite(tmpBuf, 1, bufSize, fh);
124  fclose(fh);
125 
126  fflush(NULL);
127  fh=0;
128  }
129 
130  void SmiArchStrategy::addInputBuffer(u8 *buffer, size_t size)
131  {
132  smiLinuxPrivateData *tmpPrivPtr = reinterpret_cast<smiLinuxPrivateData *>(privateData);
133  fwrite(buffer, 1, size, tmpPrivPtr->fh_data);
134  }
135 
136  void SmiArchStrategy::getResultBuffer(u8 *buffer, size_t size)
137  {
138  smiLinuxPrivateData *tmpPrivPtr = reinterpret_cast<smiLinuxPrivateData *>(privateData);
139  fflush(NULL);
140  int numbytes = fread(buffer, 1, size, tmpPrivPtr->fh_data);
141  if (!numbytes)
142  throw smbios::InternalErrorImpl("Short read from file handle");
143  }
144 
145 
146  void SmiArchStrategy::execute()
147  {
148  smiLinuxPrivateData *tmpPrivPtr = reinterpret_cast<smiLinuxPrivateData *>(privateData);
149  fflush(NULL);
150  fwrite("1", 1, 1, tmpPrivPtr->fh_doReq);
151  fflush(NULL);
152  fseek(tmpPrivPtr->fh_data, 0L, 0);
153  }
154 
155  void SmiArchStrategy::finish()
156  {
157  smiLinuxPrivateData *tmpPrivPtr = reinterpret_cast<smiLinuxPrivateData *>(privateData);
158  flock( fileno(tmpPrivPtr->fh_data), LOCK_UN );
159  fclose(tmpPrivPtr->fh_doReq);
160  fclose(tmpPrivPtr->fh_data);
161 
162  tmpPrivPtr->fh_doReq=0;
163  tmpPrivPtr->fh_data=0;
164  }
165 }
166