SMBIOS Library
SmbiosStrategy_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 <cstdio>
24 #include <sstream>
25 #include <string.h>
26 
27 #include "smbios/IMemory.h"
28 #include "SmbiosImpl.h"
29 
30 // message.h should be included last.
31 #include "smbios/message.h"
32 
33 using namespace smbiosLowlevel;
34 using namespace std;
35 
36 #if defined(DEBUG_SMBIOS_STRATEGY)
37 # define DCOUT(line) do { cout << line; } while(0)
38 # define DCERR(line) do { cerr << line; } while(0)
39 #else
40 # define DCOUT(line) do {} while(0)
41 # define DCERR(line) do {} while(0)
42 #endif
43 
44 #if 1
45 #define EFIVARS_FILE_le266 "/proc/efi/systab"
46 #define EFIVARS_FILE_gt266 "/sys/firmware/efi/systab"
47 #else
48 // for debugging
49 #define EFIVARS_FILE_le266 "/home/michael_e_brown/cc/libsmbios_proj/libsmbios/foo.txt"
50 #define EFIVARS_FILE_gt266 "/home/michael_e_brown/cc/libsmbios_proj/libsmbios/foo.txt"
51 #endif
52 
53 namespace smbios
54 {
55  // allocates no memory, constructs no objects.
56  // can raise an exception
57  void SmbiosLinuxEFIStrategy::getSmbiosTableHeader(smbiosLowlevel::smbios_table_entry_point *table_header, bool strict)
58  {
59  ParseExceptionImpl parseException;
60  parseException.setMessageString(_("EFI support not found"));
61 
62  FILE *fh = NULL;
63  if ( (fh=fopen(EFIVARS_FILE_le266, "r")) == NULL &&
64  (fh=fopen(EFIVARS_FILE_gt266, "r")) == NULL)
65  throw(parseException);
66 
67  DCERR("Found EFI systab. Reading offset..." << endl);
68 
69  // read lines
70  char line[256] = {0,};
71  while(NULL != fgets(line, sizeof(line)-1, fh))
72  {
73  char *varName=line;
74  char *varValue=line;
75  varValue = strchr(line, '=');
76  if(!varValue)
77  continue;
78 
79  *(varValue++) = '\0';
80  if (0 == strcmp(varName, "SMBIOS"))
81  {
82  // offset is in parent class and locks down header
83  // to only search at specified offset
84  offset = strtol(varValue, NULL, 0);
85  DCERR("Found SMBIOS address: " << hex << offset << "." << endl);
86  }
87  }
88  fclose(fh);
89 
90  if(offset)
91  SmbiosMemoryStrategy::getSmbiosTableHeader(table_header, strict);
92  else
93  throw(parseException);
94 
95  DCERR("Parsed SMBIOS table." << endl);
96  }
97 }