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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
/* Copyright (C) 2006 Andi Kleen, SuSE Labs.
Use SMBIOS/DMI to map address to DIMM description.
For reference see the SMBIOS specification 2.4
dmi is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; version
2.
dmi 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.
You should find a copy of v2 of the GNU General Public License somewhere
on your Linux system; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
/* Notebook
could check first if all dimms cover all memory and not dump then
(aka the Sun/HP disease)
add an option to dump existing errors in SMBIOS?
implement code to look up PCI resources too.
*/
#define _GNU_SOURCE 1
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stddef.h>
#include <sys/mman.h>
#ifdef STANDALONE
#define NELE(x) (sizeof(x)/sizeof(*(x)))
#define Wprintf printf
#else
#include "mcelog.h"
#include "dmi.h"
#endif
int verbose = 0;
struct anchor {
char str[4]; /* _SM_ */
char csum;
char entry_length;
char major;
char minor;
short maxlength;
char rev;
char fmt[5];
char str2[5]; /* _DMI_ */
char csum2;
short length;
unsigned table;
unsigned short numentries;
char bcdrev;
} __attribute__((packed));
struct dmi_entry {
unsigned char type;
unsigned char length;
unsigned short handle;
};
enum {
DMI_MEMORY_MAPPED_ADDR = 20
};
struct dmi_memdev_addr {
struct dmi_entry header;
unsigned start_addr;
unsigned end_addr;
unsigned short dev_handle;
unsigned short memarray_handle;
unsigned char row;
unsigned char interleave_pos;
unsigned char interleave_depth;
} __attribute__((packed));
struct dmi_memdev {
struct dmi_entry header;
unsigned short array_handle;
unsigned short memerr_handle;
unsigned short total_width;
unsigned short data_width;
unsigned short size;
unsigned char form_factor;
unsigned char device_set;
unsigned char device_locator;
unsigned char bank_locator;
unsigned char memory_type;
unsigned short type_details;
unsigned short speed;
unsigned char manufacturer;
unsigned char serial_number;
unsigned char asset_tag;
unsigned char part_number;
} __attribute__((packed));
static struct dmi_entry *entries;
static int numentries;
static int dmi_length;
static struct dmi_entry *handle_to_entry[0xffff];
static unsigned checksum(unsigned char *s, int len)
{
unsigned char csum = 0;
int i;
for (i = 0; i < len; i++)
csum += s[i];
return csum;
}
/* Check if entry is valid */
static int check_entry(struct dmi_entry *e, struct dmi_entry **next)
{
char *end = (char *)entries + dmi_length;
char *s = (char *)e + e->length;
if (!e)
return 0;
if (verbose >= 2)
printf("entry %lx length %d handle %x\n", (char*)e - (char*)entries,
e->length, e->handle);
do {
if (verbose >= 2)
printf("string %s\n", s);
while (s < end-1 && *s)
s++;
if (s >= end-1)
return 0;
s++;
} while (*s);
if (s >= end)
*next = NULL;
else
*next = (struct dmi_entry *)(s + 1);
return 1;
}
/* Relies on sanity checks in check_entry */
static char *getstring(struct dmi_entry *e, unsigned number)
{
char *s = (char *)e + e->length;
if (number == 0)
return "<unknown>";
do {
if (--number == 0)
return s;
while (*s)
s++;
s++;
} while (*s);
return NULL;
}
static void fill_handles(void)
{
int i;
struct dmi_entry *e, *next;
e = entries;
for (i = 0; i < numentries; i++, e = next) {
if (!check_entry(e, &next))
break;
handle_to_entry[e->handle] = e;
}
}
#define round_up(x,y) (((x) + (y) - 1) & ~((y)-1))
#define round_down(x,y) ((x) & ~((y)-1))
int opendmi(void)
{
int pagesize = getpagesize();
int memfd;
memfd = open("/dev/mem", O_RDONLY);
if (memfd < 0) {
fprintf(stderr, "Cannot open /dev/mem for DMI decoding: %s\n",
strerror(errno));
return -1;
}
struct anchor *a, *abase;
abase = mmap(NULL, 0xffff, PROT_READ, MAP_SHARED, memfd, 0xf0000);
if (abase == (struct anchor *)-1) {
fprintf(stderr, "Cannot mmap 0xf0000: %s\n", strerror(errno));
return -1;
}
a = abase;
for (a = abase;;a += 4) {
a = memmem(a, 0xffff, "_SM_", 4);
if (!a) {
fprintf(stderr, "Cannot find SMBIOS DMI tables\n");
return -1;
}
if (checksum((unsigned char *)a, a->entry_length) == 0)
break;
}
if (verbose)
printf("DMI tables at %x, %u bytes, %u entries\n",
a->table, a->length, a->numentries);
unsigned corr = a->table - round_down(a->table, pagesize);
entries = mmap(NULL, round_up(a->length + pagesize, pagesize),
PROT_READ, MAP_SHARED, memfd,
round_down(a->table, pagesize));
if (entries == (struct dmi_entry *)-1) {
fprintf(stderr, "Cannot mmap SMBIOS tables at %x\n", a->table);
return -1;
}
entries = (struct dmi_entry *)(((char *)entries) + corr);
numentries = a->numentries;
dmi_length = a->length;
fill_handles();
return 0;
}
static unsigned dimm_size(unsigned short size, char *unit)
{
unsigned mbflag = !(size & (1<<15));
size &= ~(1<<15);
strcpy(unit, "KB");
if (mbflag) {
unit[0] = 'M';
if (size >= 1024) {
unit[0] = 'G';
size /= 1024;
}
}
return size;
}
static char *form_factors[] = {
"?",
"Other", "Unknown", "SIMM", "SIP", "Chip", "DIP", "ZIP",
"Proprietary Card", "DIMM", "TSOP", "Row of chips", "RIMM",
"SODIMM", "SRIMM"
};
static char *memory_types[] = {
"?",
"Other", "Unknown", "DRAM", "EDRAM", "VRAM", "SRAM", "RAM",
"ROM", "FLASH", "EEPROM", "FEPROM", "EPROM", "CDRAM", "3DRAM",
"SDRAM", "SGRAM", "RDRAM", "DDR", "DDR2"
};
#define LOOKUP(array, val, buf) ((val) >= NELE(array) ? (sprintf(buf,"<%u>",(val)), (buf)) : (array)[val])
static char *type_details[16] = {
"Reserved", "Other", "Unknown", "Fast-paged", "Static Column",
"Pseudo static", "RAMBUS", "Synchronous", "CMOS", "EDO",
"Window DRAM", "Cache DRAM", "Non-volatile", "Res13", "Res14", "Res15"
};
static void dump_type_details(unsigned short td)
{
int i;
if (!td)
return;
for (i = 0; i < 16; i++)
if (td & (1<<i))
Wprintf("%s ", type_details[i]);
}
void dump_memdev(unsigned handle, unsigned long addr)
{
char tmp[20];
struct dmi_memdev *md = (struct dmi_memdev *)handle_to_entry[handle];
if (!md) {
Wprintf("Cannot resolve memory device %x for %lx\n",
handle, addr);
return;
}
if (md->header.length <
offsetof(struct dmi_memdev, manufacturer)) {
Wprintf("Memory device %x for address %lx too short %hu expected %lu\n",
handle, addr, md->header.length,
sizeof(struct dmi_memdev));
return;
}
char unit[10];
Wprintf("%s ", LOOKUP(memory_types, md->memory_type, tmp));
if (md->form_factor >= 3)
Wprintf("%s ", LOOKUP(form_factors, md->form_factor, tmp));
if (md->speed != 0)
Wprintf("%hu Mhz ", md->speed);
dump_type_details(md->type_details);
Wprintf("Width %hu Data Width %hu Size %u %s\n",
md->total_width, md->data_width,
dimm_size(md->size, unit), unit);
char *s;
#define DUMPSTR(n,x) \
if (md->x) { \
s = getstring(&md->header, md->x); \
if (s && strcmp(s,"None")) \
Wprintf(n ": %s\n", s); \
}
DUMPSTR("Device Locator", device_locator);
DUMPSTR("Bank Locator", bank_locator);
if (md->header.length < offsetof(struct dmi_memdev, manufacturer))
return;
DUMPSTR("Manufacturer", manufacturer);
DUMPSTR("Serial Number", serial_number);
DUMPSTR("Asset Tag", asset_tag);
DUMPSTR("Part Number", part_number);
}
static void warnuser(void)
{
static int warned;
if (warned)
return;
warned = 1;
Wprintf("WARNING: SMBIOS data is often unreliable. Take with a grain of salt!\n");
}
int dmi_decodeaddr(unsigned long addr)
{
int i, found, matching;
struct dmi_entry *e, *next;
e = entries;
found = 0;
matching = 0;
Wprintf("Resolving address %lx using SMBIOS\n", addr);
for (i = 0; i < numentries; i++, e = next) {
if (!check_entry(e, &next))
break;
if (verbose)
printf("handle %x type %u length %u\n",
e->handle, e->type, e->length);
if (e->type != DMI_MEMORY_MAPPED_ADDR)
continue;
if (e->length < sizeof(struct dmi_memdev_addr)) {
printf("too short %hd vs %lu\n", e->length,
sizeof(struct dmi_memdev_addr));
continue;
}
found++;
struct dmi_memdev_addr *da = (struct dmi_memdev_addr *)e;
if (verbose)
printf("--- %Lx-%Lx\n",
((long long)da->start_addr)*1024,
((long long)da->end_addr) * 1024);
if (addr < ((unsigned long long)da->start_addr)*1024 ||
addr >= ((unsigned long long)da->end_addr)*1024)
continue;
warnuser();
matching++;
dump_memdev(da->dev_handle, addr);
if (0)
Wprintf("Row %u Interleave Position %u Interleave Depth %u\n",
da->row, da->interleave_pos, da->interleave_depth);
}
if (!found)
Wprintf("No DIMMs found in SMBIOS tables\n");
else if (!matching)
Wprintf(
"No matching memory address found for %lx in SMBIOS\n",
addr);
return matching > 0;
}
#ifdef STANDALONE
int main(int ac, char **av)
{
if (!av[1]) {
printf("%s physaddr [verboselevel]\n", av[0]);
exit(1);
}
if (av[2])
verbose = atoi(av[2]);
if (opendmi() < 0)
exit(1);
dmi_decodeaddr(strtoul(av[1], NULL, 0));
return 0;
}
#endif
|