Plan 9 from Bell Labs’s /usr/web/sources/contrib/quanstro/dmi/dmi.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <u.h>
#include <libc.h>

enum {
	Dmistart	= 0x000F0000,
	Dmiend	= 0x000FFFFF,
	Dmisz	= Dmiend+1 - Dmistart,
};

typedef	struct	Enum	Enum;
typedef	struct	Item	Item;
typedef	struct	Itemv	Itemv;
typedef	struct	Smbuf	Smbuf;
typedef	struct	Sm	Sm;
typedef uvlong	uintmem;		/* wierd */

struct Enum {
	uint	v;
	char	*s;
};

struct Smbuf {
	uchar	sig[4];
	uchar	cksum;
	uchar	len;
	uchar	vers[2];
	uchar	maxss[2];		/* maximum structure size */
	uchar	eprev;		/* entry point revision */
	uchar	fma[5];		/* formatted area */
	uchar	dmi[5];		/* _DMI_ */
	uchar	dmicksum;
	uchar	stlen[2];
	uchar	stpa[4];		/* structure table pa */
	uchar	nstrut[2];
	uchar	bcdrev;
};

struct Sm {
	Smbuf	raw;
	uintmem	stpa;
	uint	stlen;
};

struct Item{
	char	*name;
	int	nbytes;
	int	type;
	int	offset;
	char	*fmt;
	Enum	*tab;
};

struct Itemv{
	union	{
		char	*s;
		uvlong	i;
		uchar	uuid[16];
	};
};

enum {
	Biosinfo	= 0,
	Sysinfo	= 1,
	Module	= 2,
	Chassis	= 3,
	Cpu	= 4,
	Memctlr	= 5,		/* obs. */
	Memstk	= 6,		/* obs. */
	Cache	= 7,
	Conn	= 8,
	Slot	= 9,
	Oemstr	= 11,
	Bioslang	= 13,
	Groupa	= 14,
	Syslog	= 15,		/* requires extra poking around */
	Pmem	= 16,
	Mdev	= 17,
	Merror	= 18,
	Mmap	= 19,
	Mmdev	= 20,
	Pointer	= 21,
	Battery	= 22,
	Wdog	= 23,
	Hwsec	= 24,
	Syspwr	= 25,
	Vprobe	= 26,
	Fan	= 27,
	Tprobe	= 28,
	Cprobe	= 29,
	Bootinfo = 32,
	Merror64 = 33,
	Mgmt	= 34,
	Mgmtc	= 35,
	Mgmtdt	= 36,
	Memch	= 37,
	Ipmi	= 38,
	Ps	= 39,
	Obd	= 41,
};

Enum tabletab[] = {
	0,	"biosinfo",
	1,	"sysinfo",
	2,	"module",
	3,	"chassis",
	4,	"cpu",
	5,	"memctlr",
	6,	"memstk",
	7,	"cache",
	8,	"conn",
	9,	"slot",
	11,	"oemstr",
	13,	"bioslang",
	14,	"groupa",
	15,	"syslog",
	16,	"pmem",
	17,	"mdev",
	18,	"merror",
	19,	"mmap",
	20,	"mmdev",
	21,	"pointer",
	22,	"battery",
	23,	"wdog",
	24,	"hwsec",
	25,	"syspwr",
	26,	"vprobe",
	27,	"fan",
	28,	"tprobe",
	29,	"cprobe",
	32,	"bootinfo",
	33,	"merror64",
	34,	"mgmt",
	35,	"mgmtc",
	36,	"mgmtdt",
	37,	"memch",
	38,	"ipmi",
	39,	"ps",
	41,	"obd",
	0,	nil,
};

uchar	*dmianchor[0x20];
int	rfd = -1;
int	prtype = -1;
int	prhand = -1;

void
realmodefd(void)
{
	if(rfd != -1)
		return;
	rfd = open("/dev/realmodemem", OREAD);
	if(rfd == -1)
		sysfatal("open: %r");
}

void*
emalloc(usize sz)
{
	void *v;

	v = malloc(sz);
	if(v == nil)
		sysfatal("malloc: %r");
	memset(v, 0, sz);
	setmalloctag(v, getcallerpc(&sz));
	return v;
}

enum {
	String,
	Integer,
	Uchar,
	Enumf,
	Enumbv,
};

Item	biositems[] = {
	"vendor",	1,	String,	-1,	"%s",		nil,
	"version",	1,	String,	-1,	"%s",		nil,
	"biosstartseg",	2,	Integer,	-1,	"%#.4llux",	nil,
	"biosdate",	1,	String,	-1,	"%s",		nil,
	"biosromsz",	1,	Integer,	-1,	"%#.4llux",	nil,	// _+1<<16
	"biosflags",	8,	Uchar,	-1,	"%.8H",		nil,
	"extflags0",	1,	Integer,	-1,	"%.2llux",	nil,
	"extflags1",	1,	Integer,	-1,	"%.2llux",	nil,
	"release0",	1,	Integer,	-1,	"%.2llux",	nil,
	"release1",	1,	Integer,	-1,	"%.2llux",	nil,
	"embedrelease0",	1,	Integer,	-1,	"%.2llux",	nil,
	"embedrelease1",	1,	Integer,	-1,	"%.2llux",	nil,
};

Enum waketype[] = {
	0,	"reserved",
	1,	"other",
	2,	"unknown",
	3,	"apmtimer",
	4,	"modem",
	5,	"lan",
	6,	"powersw",
	7,	"pcipme",
	8,	"powerrestored",
	0,	nil,
};

Item	sysitems[] = {
	"mfg",		1,	String,	-1,	"%s",		nil,
	"product",	1,	String,	-1,	"%s",		nil,
	"version",	1,	String,	-1,	"%s",		nil,
	"uuid",		16,	Uchar,	-1,	"%.16H",	nil,
	"waketype",	1,	Integer,	-1,	"%lld",		nil,
	"sku",		1,	String,	-1,	"%s",		nil,
	"family",		1,	String,	-1,	"%s",		nil,
};

Item	moditems[] = {
	"bbmfg",		1,	String,	-1,	"%s",		nil,
	"product",	1,	String,	-1,	"%s",		nil,
	"bbversion",	1,	String,	-1,	"%s",		nil,
	"bbserial",	1,	String,	-1,	"%s",		nil,
	"assettag",	1,	String,	-1,	"%s",		nil,
	"features",	1,	Integer,	-1,	"%#.2llux",	nil,
	"chassisloc",	1,	String,	-1,	"%s",		nil,
	"chassishand",	2,	Integer,	-1,	"%#.4llux",	nil,
	"bbtype",		1,	Integer,	-1,	"%lld",		nil,
	/* continued object handles; 2 bytes each */
};

Item	chassisitems[] = {
	"chassismfg",	1,	String,	-1,	"%s",		nil,
	"chassistype",	1,	Integer,	-1,	"%lld",		nil,
	"chassisver",	1,	String,	-1,	"%s",		nil,
	"chassisserial",	1,	String,	-1,	"%s",		nil,
	"chassisassettag",	1,	String,	-1,	"%s",		nil,

	"bootstate",	1,	Integer,	-1,	"%lld",		nil,
	"powerst",	1,	Integer,	-1,	"%lld",		nil,
	"thermalst",	1,	Integer,	-1,	"%lld",		nil,
	"securityst",	1,	Integer,	-1,	"%lld",		nil,

	"height",		1,	Integer,	-1,	"%lld",		nil,
	"powercord#",	1,	Integer,	-1,	"%lld",		nil,
	"elementcnt",	1,	Integer,	-1,	"%lld",		nil,

	/* containted elements & sku number */
};

Enum sockettype[] = {
	1,	"other",
	2,	"unknown",
	3,	"daughter board",
	4,	"zif socet",
	5,	"replacable piggy back",
	6,	"none",
	7,	"lif socket",
	8,	"slot 1",
	9,	"slot 2",
	0xa,	"370-pin socket",
	0xb,	"slot a",
	0xc,	"slot m",
	0xd,	"socket 423",
	0xe,	"socket a (socket 462)",
	0xf,	"socket 478",
	0x10,	"socket 754",
	0x11,	"socket 940",
	0x12,	"socket 939",
	0x13,	"socket mPGA604",
	0x14,	"socket lga771",
	0x15,	"socket lga775",
	0x16,	"socket s1",
	0x17,	"socket am2",
	0x18,	"socket f",
	0x19,	"socket lga1366",
	0x1a,	"socket g34",
	0x1b,	"socket am3",
	0x1c,	"socket c32",
	0x1d,	"socket lga1156",
	0x1d,	"socket lga1567",
	0x1f,	"socket pga988a",
	0x20,	"socket bg1288",
	0x21,	"socket rpga988b",
	0x22,	"socket bga1023",
	0x23,	"socket bga1224",
	0x24,	"socket bga1155",
	0x25,	"socket bga1356",
	0x26,	"socket lga2011",
	0x27,	"socket fs1",
	0x28,	"socket fs2",
	0x29,	"socket fm1",
	0x2a,	"socket fm2",
};

Item	cpuitems[] = {
	"socket",		1,	String,	-1,	"%s",		nil,
	"cputype",	1,	Integer,	-1,	"%lld",		nil,
	"cpufamily",	1,	Integer,	-1,	"%lld",		nil,
	"cpumfg",	1,	String,	-1,	"%s",		nil,
	"cpuid",		8,	Integer,	-1,	"%llux",		nil,
	"cpuvers",	1,	String,	-1,	"%s",		nil,
	"cpuvolt",	1,	Integer,	-1,	"%lld",		nil,
	"clockmhz",	2,	Integer,	-1,	"%lld",		nil,
	"cpuspeed",	2,	Integer,	-1,	"%lld",		nil,
	"cpuspeed (now)",	2,	Integer,	-1,	"%lld",		nil,
	"cpustatus",	1,	Integer,	-1,	"%lld",		nil,
	"cpuupgrade",	1,	Enumf,	-1,	"%s",		sockettype,
	"cpul1",		2,	Integer,	-1,	"%.2llux",	nil,
	"cpul2",		2,	Integer,	-1,	"%.2llux",	nil,
	"cpul3",		2,	Integer,	-1,	"%.2llux",	nil,
	"cpuserial",	1,	String,	-1,	"%s",		nil,
	"cpuassettag",	1,	String,	-1,	"%s",		nil,
	"cpupart#",	1,	String,	-1,	"%s",		nil,
	"cpucore#",	1,	Integer,	-1,	"%lld",		nil,
	"cpucoreen",	1,	Integer,	-1,	"%lld",		nil,
	"cputhreadcnt",	1,	Integer,	-1,	"%lld",		nil,
	"cpuenum",	2,	Integer,	-1,	"%#.4llux",	nil,
	"cpufamily2",	2,	Integer,	-1,	"%#.4llux",	nil,
};

Enum ecctypetab[] = {
	1,	"other",
	2,	"unknown",
	3,	"none",
	4,	"8-bit parity",
	5,	"32-bit ecc",
	6,	"32-bit ecc",
	7,	"32-bit ecc",
	8,	"crc",
	0,	nil,
};

/* bitfield */
Enum ecccap[] = {
	1<<0,	"other",
	1<<1,	"unknown",
	1<<2,	"none",
	1<<3,	"single-bit",
	1<<4,	"double-bit",
	1<<5,	"error scrubbing",
	0,	nil,
};

Enum interleave[] = {
	1,	"other",
	2,	"unknown",
	3,	"one-way",
	4,	"two-way",
	5,	"four-way",
	6,	"eight-way",
	7,	"sixteen-way",
	0,	nil,
};

Item	memctlritems[] = {
	"ecctype",	1,	Enumf,	-1,	"%s",		ecctypetab,
	"ecccap",		1,	Enumbv,	-1,	"%s",		ecccap,
	"interleavesupp",	1,	Enumf,	-1,	"%s",		interleave,
	"interleavecurr",	1,	Enumf,	-1,	"%s",		interleave,
	"maxmodsz",	1,	Integer,	-1,	"%ℛ",		nil,
	"speedsupp",	2,	Integer,	-1,	"%#.4llux",	nil,
	"typessupp",	2,	Integer,	-1,	"%#.4llux",	nil,
	"voltages",	1,	Integer,	-1,	"%#.2llux",	nil,
	"memslots",	1,	Integer,	-1,	"%lld",		nil,
/*	"handles"	2*memslots,	Integer,	-1,	"%#.4llux",	nil,	*/
/*	"eccenabled"	1,	Integer,	-1,	"%.2llux",		nil,	*/
};

int
fmtℛ(Fmt *f)
{
	char *prefix;
	uint code;

	prefix = "";
	code = va_arg(f->args, uvlong);
	if(f->flags & FmtSharp){
		switch(code){
		case 0x7d:
			return fmtprint(f, "not determinable");
		case 0x7e:
			return fmtprint(f, "installed but disabled");
		case 0x7f:
			return fmtprint(f, "not installed");
		}
		if(code & 0x80)
			prefix = "dual-port ";
		code &= ~0x80;
	}
	return fmtprint(f, "%s%dMB", prefix, 1<<code);
}

Enum moduletype[] = {
	1<<0,	"other",
	1<<1,	"unknown",
	1<<2,	"standard",
	1<<3,	"fast page mode",
	1<<4,	"edo",
	1<<5,	"parity",
	1<<6,	"ecc",
	1<<7,	"simm",
	1<<8,	"dimm",
	1<<9,	"burst edo",
	1<<10,	"sdram",
};

Item	memstkitems[] = {
	"memsocket",	1,	String,	-1,	"%s",		nil,
	"bankconn",	1,	Integer,	-1,	"%#.2llux",	nil,	/* nybble-encoding */
	"currspeed (ns)",	1,	Integer,	-1,	"%lld",		nil,
	"memtype",	2,	Integer,	-1,	"%#.4llux",	nil,
	"installsz",	1,	Integer,	-1,	"%#ℛ",		nil,
	"enablesz",	1,	Integer,	-1,	"%#ℛ",		nil,
	"errorsts",	1,	Integer,	-1,	"%.2llux",	nil,
};

Enum	sramtype[] = {
	1<<0,	"other",
	1<<1,	"unknown",
	1<<2,	"non-burst",
	1<<3,	"pipeline burst",
	1<<4,	"sync",
	1<<5,	"async",
	0,	nil,
};

Enum	cachetype[] = {
	1,	"other",
	2,	"unknown",
	3,	"instruction",
	4,	"data",
	5,	"unified",
	0,	nil,
};

Enum	assoctype[] = {
	1,	"other",
	2,	"unknown",
	3,	"direct mapped",
	4,	"2-way set-associative",
	5,	"4-way set-associative",
	6,	"fully associative",
	7,	"8-way set-associative",
	8,	"16-way set-associative",
	9,	"12-way set-associative",
	10,	"24-way set-associative",
	11,	"32-way set-associative",
	12,	"48-way set-associative",
	13,	"64-way set-associative",
	14,	"20-way set-associative",
	0,	nil,
};

Item	cacheitems[] = {
	"socket",		1,	String,	-1,	"%s",		nil,
	"config",		2,	Integer,	-1,	"%#.4llux",	nil,
	"maxsize",	2,	Integer,	-1,	"%#.4llux",	nil,
	"instalsz",	2,	Integer,	-1,	"%#.4llux",	nil,
	"sramtype",	2,	Enumf,	-1,	"%s",		sramtype,
	"installtype",	2,	Integer,	-1,	"%#.4llux",	nil,
	"speed (ns)",	1,	Integer,	-1,	"%lld",		nil,
	"ecctype",	1,	Integer,	-1,	"%#.2llux",	nil,
	"logicaltype",	1,	Enumf,	-1,	"%s",		cachetype,
	"assoc",		1,	Enumf,	-1,	"%s",		assoctype,
};

Item	connitems[] = {
	"intrefdes",	1,	String,	-1,	"%s",		nil,
	"intconntype",	1,	Integer,	-1,	"%#.2llux",	nil,
	"extrefdes",	1,	String,	-1,	"%s",		nil,
	"intconntype",	1,	Integer,	-1,	"%#.2llux",	nil,
	"porttype",	1,	Integer,	-1,	"%#.2llux",	nil,
};

Enum slottype[] = {
	1,	"other",
	2,	"unknown",
	3,	"isa",
	4,	"mca",
	5,	"eisa",
	6,	"pci",
	7,	"pcmcia",
	8,	"vl-vesa",
	9,	"propoetart",
	0xa,	"processor card slot",
	0xb,	"propietary memory card slot",
	0xc,	"i/o reiser card slot",
	0xd,	"nubus",
	0xe,	"pci66mhz",
	0xf,	"agp",
	0x10,	"agp2x",
	0x11,	"agp4x",
	0x12,	"pci-x",
	0x13,	"agp8x",
	0xa0,	"pc-98/c20",
	0xa1,	"pc-98/c24",
	0xa2,	"pc-98/e",
	0xa3,	"pc-98/localbus",
	0xa4,	"pc-98/card",
	0xa5,	"pcie",
	0xa6,	"pcie x1",
	0xa7,	"pcie x2",
	0xa8,	"pcie x4",
	0xa9,	"pcie x8",
	0xaa,	"pcie x16",

	0xab,	"pcie gen2",
	0xac,	"pcie gen2 x1",
	0xad,	"pcie gen2 x2",
	0xae,	"pcie gen2 x4",
	0xaf,	"pcie gen2 x8",
	0xb0,	"pcie gen2 x16",

	0xb1,	"pcie gen3",
	0xb2,	"pcie gen3 x1",
	0xb3,	"pcie gen3 x2",
	0xb4,	"pcie gen3 x4",
	0xb5,	"pcie gen3 x8",
	0xb6,	"pcie gen3 x16",
	0,	nil,
};

Enum slotwidth[] = {
	1,	"other",
	2,	"unknown",
	3,	"8bit",
	4,	"16bit",
	5,	"32bit",
	6,	"64bit",
	7,	"128bit",
	8,	"1x",
	9,	"2x",
	0xa,	"4x",
	0xb,	"8x",
	0xc,	"12x",
	0xd,	"16x",
	0xe,	"32x",
	0,	nil,
};

Enum slotuse[] = {
	1,	"other",
	2,	"unknown",
	3,	"available",
	4,	"inuse",
	0,	nil,
};

Enum slotlength[] = {
	1,	"other",
	2,	"unknown",
	3,	"short",
	4,	"long",
	0,	nil,
};

Item	slotitems[] = {
	"slotname",	1,	String,	-1,	"%s",		nil,
	"slottype",	1,	Enumf,	-1,	"%s",		slottype,
	"datawidth",	1,	Enumf,	-1,	"%s",		slotwidth,
	"use",		1,	Enumf,	-1,	"%s",		slotuse,
	"slotlen",		1,	Enumf,	-1,	"%s",		slotlength,
	"slotid",		2,	Integer,	-1,	"%lld",		nil,
	"slotbits0",	1,	Integer,	-1,	"%.2llux",	nil,
	"slotbits1",	1,	Integer,	-1,	"%.2llux",	nil,
	"segment",	1,	Integer,	-1,	"%.2llux",	nil,
	"bus",		1,	Integer,	-1,	"%.2llux",	nil,
	"devfn",		1,	Integer,	-1,	"%.2llux",	nil,
};

Item	bioslangitems[] = {
	"installable",	1,	String,	-1,	"%lld",		nil,
	"flags",		1,	Integer,	-1,	"%lld",		nil,
	"reserved",	15,	Uchar,	-1,	"%.15H",	nil,
	"currlang",	1,	String,	-1,	"%s",		nil,
};

Enum accesstab[] = {
	0,	"i/o indexed 1x 8-bit index/1x 8-bit data",
	1,	"i/o indexed 2x 8-bit index/2x 8-bit data",
	2,	"i/o indexed 1x 16-bit index/1x 8-bit data",
	3,	"32-bit mmio",
	4,	"general purpose nv data access method",
};

Item	syslogitems[] = {
	"loglength",	2,	Integer,	-1,	"%#.4llux",	nil,
	"hdroffset",	2,	Integer,	-1,	"%#.4llux",	nil,
	"dataoffset",	2,	Integer,	-1,	"%#.4llux",	nil,
	"access",		1,	Enumf,	-1,	"%s",		accesstab,
	"status",		1,	Integer,	-1,	"%#.2llux",	nil,
	"token",		4,	Integer,	-1,	"%#.8llux",	nil,
	"pa",		4,	Integer,	-1,	"%#.8llux",	nil,
	"format",		1,	Integer,	-1,	"%lld",		nil,
	"ndesc",		1,	Integer,	-1,	"%lld",		nil,
	"desclen",	1,	Integer,	-1,	"%lld",		nil,
	/* list goes here */
};

Enum	memuse[] = {
	1,	"other",
	2,	"unknown",
	3,	"system memory",
	4,	"video memory",
	5,	"flash memory",
	6,	"nvram",
	7,	"cache",
	0,	nil,
};

Item	pmemitems[] = {
	"loc",		1,	Integer,	-1,	"%lld",		nil,
	"use",		1,	Enumf,	-1,	"%s",		memuse,
	"ecc",		1,	Integer,	-1,	"%lld",		nil,
	"capacity",	4,	Integer,	-1,	"%lld kb",	nil,
	"errhandle",	2,	Integer,	-1,	"%.4llux",	nil,
	"#devices",	2,	Integer,	-1,	"%lld",		nil,
	"ecapacity",	8,	Integer,	-1,	"%lld bytes",	nil,
};

Enum	formfactor[] = {
	1,	"other",
	2,	"unknown",
	3,	"simm",
	4,	"sip",
	5,	"chip",
	6,	"dip",
	7,	"zip",
	8,	"proprietary card",
	9,	"dimm",
	0xa,	"tsop",
	0xb,	"row of chips",
	0xc,	"rimm",
	0xd,	"sodimm",
	0xe,	"srimm",
	0xf,	"fb-dimm",
	0,	nil,
};

Enum	mdevmemtype[] = {
	1,	"other",
	2,	"unknown",
	3,	"dram",
	4,	"edram",
	5,	"vram",
	6,	"sram",
	7,	"ram",
	8,	"rom",
	9,	"flash",
	0xa,	"eeprom",
	0xb,	"feprom",
	0xc,	"eprom",
	0xd,	"cdram",
	0xe,	"3dram",
	0xf,	"sdram",
	0x10,	"sgram",
	0x11,	"rdram",
	0x12,	"ddr",
	0x13,	"ddr2",
	0x14,	"ddr2 fb-dimm",
	0x18,	"ddr3",
	0x19,	"fbd2",
	0,	nil,
};

Enum	typedetail[] = {
	1<<1,	"other",
	1<<2,	"unknown",
	1<<3,	"fast-paged",
	1<<4,	"static- column",
	1<<5,	"pseudo-static",
	1<<6,	"rambus",
	1<<7,	"synchronous",
	1<<8,	"cmos",
	1<<9,	"edo",
	1<<10,	"window dram",
	1<<11,	"cache dram",
	1<<12,	"non-volatile",
	1<<13,	"registered (buffered)",
	1<<14,	"unregistered (unbuffered)",
	0,	nil,
};

int
fmt𝒮(Fmt *f)
{
	uint u;

	u = va_arg(f->args, uvlong);
	if(u & 0x8000)
		return fmtprint(f, "%d kb", u&~0x8000);
	return fmtprint(f, "%d mb", u);
}

Item	mdevitems[] = {
	"paryhnd",	2,	Integer,	-1,	"%#.4llux",	nil,
	"errhnd",		2,	Integer,	-1,	"%#.4llux",	nil,
	"twidth",		2,	Integer,	-1,	"%llud",		nil,
	"width",		2,	Integer,	-1,	"%llud",		nil,
	"size",		2,	Integer,	-1,	"%𝒮",		nil,
	"formfactor",	1,	Enumf,	-1,	"%s",		formfactor,
	"deviceset",	1,	Integer,	-1,	"%llud",		nil,
	"locator",	1,	String,	-1,	"%s",		nil,
	"bankloc",	1,	String,	-1,	"%s",		nil,
	"memtype",	1,	Enumf,	-1,	"%s",		mdevmemtype,
	"typedetail",	2,	Enumf,	-1,	"%s",		typedetail,
	"speed",		2,	Integer,	-1,	"%llud Mhz",	nil,
	"mfgr",		1,	String,	-1,	"%s",		nil,
	"serial",		1,	String,	-1,	"%s",		nil,
	"assettag",	1,	String,	-1,	"%s",		nil,
	"part#",		1,	String,	-1,	"%s",		nil,
	"attr",		1,	Integer,	-1,	"%.2llux",	nil,
	"xsize",		4,	Integer,	-1,	"%#.4llud MB",	nil,
	"cfgspeed",	2,	Integer,	-1,	"%llud Mhz",	nil,
};

Enum errortype[] = {
	1,	"other",
	2,	"unknown",
	3,	"ok",
	4,	"bad read",
	5,	"parity error",
	6,	"single-bit error",
	7,	"double-bit error",
	8,	"muti-bit error",
	9,	"nybble error",
	0xa,	"cksum error",
	0xb,	"crc error",
	0xc,	"corrected single-bit error",
	0xd,	"corrected error",
	0xe,	"uncorrectable error",
	0,	nil,
};

Enum errorgran[] = {
	1,	"other",
	2,	"unknown",
	3,	"device",
	4,	"memory partition",
	0,	nil,
};

Enum memoryop[] = {
	1,	"other",
	2,	"unknown",
	3,	"read",
	4,	"write",
	0,	nil,
};

Item	merroritems[] = {
	"errortype",	1,	Enumf,	-1,	"%s",		errortype,
	"errorgran",	1,	Enumf,	-1,	"%s",		errorgran,
	"errorop",	1,	Enumf,	-1,	"%s",		memoryop,
	"syndrome",	4,	Integer,	-1,	"%#.8llux",	nil,
	"physaddr",	4,	Integer,	-1,	"%#.8llux",	nil,
	"devaddr",	4,	Integer,	-1,	"%#.8llux",	nil,
	"resolution",	4,	Integer,	-1,	"%#.8llux",	nil,
};

Item	mmapitems[] = {
	"start",		4,	Integer,	-1,	"%#.8llux kb",	nil,
	"end",		4,	Integer,	-1,	"%#.8llux kb",	nil,
	"ahand",		2,	Integer,	-1,	"%#.4llux",	nil,
	"partwidth",	1,	Integer,	-1,	"%lld",		nil,
	"xstart",		8,	Integer,	-1,	"%#.16llux",	nil,
	"xend",		8,	Integer,	-1,	"%#.16llux",	nil,
};

/* prec→multiplier, width→prec */
int
fmt×(Fmt *f)
{
	uvlong kb, m;

	kb = va_arg(f->args, uvlong);
	m = 1024;
	if(f->flags & FmtPrec){
		f->flags &= ~FmtPrec;
		m = f->prec;
	}
	if(f->flags & FmtWidth)
		return fmtprint(f, "%#.*llux", f->width, kb*m);
	return fmtprint(f, "%#.8llux", kb*m);
}

Item	mmdevitems[] = {
	"start",		4,	Integer,	-1,	"%8.1024×",	nil,
	"end",		4,	Integer,	-1,	"%8.1024×",	nil,
	"dhand",		2,	Integer,	-1,	"%#.4llux",	nil,
	"ahand",		2,	Integer,	-1,	"%#.4llux",	nil,
	"row",		1,	Integer,	-1,	"%lld",		nil,
	"interleave",	1,	Integer,	-1,	"%lld",		nil,
	"idepth",		1,	Integer,	-1,	"%lld",		nil,
	"xstart",		8,	Integer,	-1,	"%#.16llux",	nil,
	"xend",		8,	Integer,	-1,	"%#.16llux",	nil,
};

Item	ptritems[] = {
	"type",		1,	Integer,	-1,	"%lld",		nil,
	"interface",	1,	Integer,	-1,	"%lld",		nil,
	"buttons",	1,	Integer,	-1,	"%lld",		nil,
};

Enum batterychemistry[] = {
	1,	"other",
	2,	"unknown",
	3,	"lead acid",
	4,	"nicad",
	5,	"nimh",
	6,	"lion",
	7,	"zincair",
	8,	"li polymer",
	0,	nil,
};

Item	batteryitems[] = {
	"location",	1,	String,	-1,	"%s",		nil,
	"mfgr",		1,	String,	-1,	"%s",		nil,
	"mfgr date",	1,	String,	-1,	"%s",		nil,
	"serial",		1,	String,	-1,	"%s",		nil,
	"devname",	1,	String,	-1,	"%s",		nil,

	"chemistry",	1,	Enumf,	-1,	"%s",		batterychemistry,
	"capacity",	2,	Integer,	-1,	"%lld mWh",	nil,
	"voltage",	2,	Integer,	-1,	"%lld mV",	nil,
	"sbds vers",	1,	Integer,	-1,	"%lld",		nil,
	"maxerror%",	1,	Integer,	-1,	"%lld",		nil,
	"sbds serial",	2,	Integer,	-1,	"%.4llux",	nil,
	"sbds mfgdate",	2,	Integer,	-1,	"%.4llux",	nil,
	"sbds chem",	1,	String,	-1,	"%s",		nil,
	"capacity ×",	1,	Integer,	-1,	"%lld",		nil,
	"oemword",	2,	Integer,	-1,	"%llux",		nil,
};

Item	wdogitems[] = {
	"cap",		1,	Integer,	-1,	"%.2llux",	nil,
  	"rstcount",	2,	Integer,	-1,	"%lld",		nil,
  	"rstlim",		2,	Integer,	-1,	"%lld",		nil,
  	"interval",	2,	Integer,	-1,	"%lld min",	nil,
  	"timeout",	2,	Integer,	-1,	"%lld min",	nil,
};

Item	hwsecitems[] = {
	"settings",	1,	Integer,	-1,	"%.2llux",	nil,
};

Item	syspwritems[] = {
  	"month",		1,	Integer,	-1,	"%.2llux",	nil,
 	"mday",		1,	Integer,	-1,	"%.2llux",	nil,
	"day",		1,	Integer,	-1,	"%.2llux",	nil,
    	"min",		1,	Integer,	-1,	"%.2llux",	nil,
    	"osec",		1,	Integer,	-1,	"%.2llux",	nil,
};

Item	vprobeitems[] = {
	"desc",		1,	String,	-1,	"%s",		nil,
  	"locstat",		1,	Integer,	-1,	"%.2llux",	nil,

  	"max",		2,	Integer,	-1,	"%.2llud mV",	nil,
  	"min",		2,	Integer,	-1,	"%.2llud mV",	nil,
  	"res",		2,	Integer,	-1,	"%.2llud mV",	nil,
  	"tolerance",	2,	Integer,	-1,	"%.2llud mV",	nil,
  	"accuracy",	2,	Integer,	-1,	"%.2llud mV",	nil,
  	"oem",		4,	Integer,	-1,	"%.8llux",	nil,
  	"nominal",	2,	Integer,	-1,	"%.2llud mV",	nil,
};

Enum fantypestat[] = {
	1<<5,	"other",
	2<<5,	"unknown",
	3<<5,	"ok",
	4<<5,	"non-critical",
	5<<5,	"critical",
	6<<5,	"non-recoverable",

	1,	"other",
	2,	"unknown",
	3,	"fan",
	4,	"centrifugal blower",
	5,	"chip fan",
	6,	"cabinet fan",
	7,	"powersupply fan",
	8,	"heat pipe",
	9,	"integrated refrig",
	0xa,	"active cooling",
	0xb,	"passive cooling",
	0,	nil,
};

Item	fanitems[] = {
	"tprobhnd",	2,	Integer,	-1,	"%.4llux",	nil,
  	"typestat",	1,	Integer,	-1,	"%.2llux",	nil,
	"coolgroup",	1,	Integer,	-1,	"%.2llux",	nil,
	"oem",		4,	Integer,	-1,	"%.8llux",	nil,
	"nominalspd",	2,	Integer,	-1,	"%.4lld",		nil,
	"description",	1,	String,	-1,	"%s",		nil,
};

Item	tprobeitems[] = {
	"desc",		1,	String,	-1,	"%s",			nil,
  	"locstat",		1,	Integer,	-1,	"%.2llux",		nil,
  	"max",		2,	Integer,	-1,	"%.llud  1/10th °C",	nil,
  	"min",		2,	Integer,	-1,	"%.llud  1/10th °C",	nil,
  	"res",		2,	Integer,	-1,	"%.llud  1/10th °C",	nil,
	"tolerance",	2,	Integer,	-1,	"%.llud  1/10th °C",	nil,
  	"accuracy",	2,	Integer,	-1,	"%.llud  1/10th °C",	nil,
  	"oem",		4,	Integer,	-1,	"%.8llux",		nil,
  	"nominal",	2,	Integer,	-1,	"%.llud  1/10th °C",	nil,
};

Item	cprobeitems[] = {
	"desc",		1,	String,	-1,	"%s",		nil,
  	"locstat",		1,	Integer,	-1,	"%.2llux",	nil,
  	"max",		2,	Integer,	-1,	"%.llud  mA",	nil,
  	"min",		2,	Integer,	-1,	"%.llud  mA",	nil,
  	"res",		2,	Integer,	-1,	"%.llud  mA",	nil,
	"tolerance",	2,	Integer,	-1,	"%.llud  mA",	nil,
  	"accuracy",	2,	Integer,	-1,	"%.llud  mA",	nil,
  	"oem",		4,	Integer,	-1,	"%.8llux",	nil,
  	"nominal",	2,	Integer,	-1,	"%.llud  mA",	nil,
};

Item	bootinfoitems[] = {
	"undefined",	6,	Integer,	-1,	"%llux",		nil,
	"status",		10,	Uchar,	-1,	"%.10H",	nil,
};

Item	memerr64items[] = {
	"type",		1,	Integer,	-1,	"%llud",		nil,
	"gran",		1,	Integer,	-1,	"%llud",		nil,
	"operation",	1,	Integer,	-1,	"%llud",		nil,
	"synrdome",	4,	Integer,	-1,	"%.8llux",	nil,

	"addr",		8,	Integer,	-1,	"%.16llux",	nil,
	"devaddr",	8,	Integer,	-1,	"%.16llux",	nil,
	"resolution",	4,	Integer,	-1,	"%.8llux",	nil,
};

Enum mgmtaddrtype[] = {
	1,	"other",
	2,	"unknown",
	3,	"i/o port",
	4,	"memory",
	5,	"smbus",
	0,	nil,
};

Item	mgmtitems[] = {
	"desc",		1,	String,	-1,	"%s",		nil,
	"type",		1,	Integer,	-1,	"%lld",		nil,
	"addr",		4,	Integer,	-1,	"%#.8llx",	nil,
	"addrtype",	1,	Enumf,	-1,	"%s",		mgmtaddrtype,
};

Item	mgmtcitems[] = {
	"desc",		1,	String,	-1,	"%s",		nil,
	"devhand",	2,	Integer,	-1,	"%.4llux",	nil,
	"cmphand",	2,	Integer,	-1,	"%.4llux",	nil,
	"thand",		2,	Integer,	-1,	"%.4llux",	nil,
};

Item	mgmtdtitems[] = {
	"low thresh",	2,	Integer,	-1,	"%.4llud",	nil,
	"hi thresh",	2,	Integer,	-1,	"%.4llud",	nil,
	"low crit thresh",	2,	Integer,	-1,	"%.4llud",	nil,
	"hi crit thresh",	2,	Integer,	-1,	"%.4llud",	nil,
	"low nr thresh",	2,	Integer,	-1,	"%.4llud",	nil,
	"hi nr thresh",	2,	Integer,	-1,	"%.4llud",	nil,
};

Item	memchanitems[] = {
	"type",		1,	Integer,	-1,	"%llud",		nil,
	"maxload",	1,	Integer,	-1,	"%llud",		nil,
	"devcnt",		1,	Integer,	-1,	"%llud",		nil,
	"load1",		1,	Integer,	-1,	"%llud",		nil,
	"hndl1",		2,	Integer,	-1,	"%.4llux",	nil,
};

Item	ipmiitems[] = {
	"type",		1,	Integer,	-1,	"%llud",		nil,
	"version",	1,	Integer,	-1,	"%#.2llux",	nil,
	"i²c slaveaddr",	1,	Integer,	-1,	"%#.2llux",	nil,
	"nvstoraddr",	1,	Integer,	-1,	"%#.2llux",	nil,
	"pa",		8,	Integer,	-1,	"%#.16llux",	nil,
	"pa modify",	1,	Integer,	-1,	"%#.2llux",	nil,
	"irq#",		1,	Integer,	-1,	"%#.2llux",	nil,
};

Item	psitems[] = {
	"pwrgrp",	1,	Integer,	-1,	"%llud",		nil,

	"loc",		1,	String,	-1,	"%s",		nil,
	"name",		1,	String,	-1,	"%s",		nil,
	"mfg",		1,	String,	-1,	"%s",		nil,
	"serial",		1,	String,	-1,	"%s",		nil,
	"assetag",	1,	String,	-1,	"%s",		nil,
	"part#",		1,	String,	-1,	"%s",		nil,
	"rev",		1,	String,	-1,	"%s",		nil,
	"pwrecap",	2,	Integer,	-1,	"%llud mW",	nil,
	"flags",		2,	Integer,	-1,	"%.4llux",	nil,
	"input v hdl",	2,	Integer,	-1,	"%.4llux",	nil,
	"fan hdl",	2,	Integer,	-1,	"%.4llux",	nil,
	"current hdl",	2,	Integer,	-1,	"%.4llux",	nil,
};

Item	obditems[] = {
	"name",		1,	String,	-1,	"%s",		nil,
	"type",		1,	Integer,	-1,	"%.2llux",	nil,
	"inst",		1,	Integer,	-1,	"%.2llux",	nil,
	"segment",	1,	Integer,	-1,	"%.2llux",	nil,
	"bus",		1,	Integer,	-1,	"%.2llux",	nil,
	"devfn",		1,	Integer,	-1,	"%.2llux",	nil,
};

typedef struct Itemtab Itemtab;
struct Itemtab {
	Item	*tab;
	int	n;
};

Itemtab	itemtab[] = {
[Biosinfo]	biositems,	nelem(biositems),
[Sysinfo]		sysitems,		nelem(sysitems),
[Module]	moditems,	nelem(moditems),
[Chassis]		chassisitems,	nelem(chassisitems),
[Cpu]		cpuitems,	nelem(cpuitems),
[Memctlr]	memctlritems,	nelem(memctlritems),
[Memstk]	memstkitems,	nelem(memstkitems),
[Cache]		cacheitems,	nelem(cacheitems),
[Conn]		connitems,	nelem(connitems),
[Slot]		slotitems,	nelem(slotitems),
[Bioslang]	bioslangitems,	nelem(bioslangitems),
[Syslog]		syslogitems,	nelem(syslogitems),
[Pmem]		pmemitems,	nelem(pmemitems),
[Mdev]		mdevitems,	nelem(mdevitems),
[Merror]		merroritems,	nelem(merroritems),
[Mmap]		mmapitems,	nelem(mmapitems),
[Mmdev]		mmdevitems,	nelem(mmdevitems),
[Pointer]		ptritems,		nelem(ptritems),
[Battery]		batteryitems,	nelem(batteryitems),
[Wdog]		wdogitems,	nelem(wdogitems),
[Hwsec]		hwsecitems,	nelem(hwsecitems),
[Syspwr]		syspwritems,	nelem(syspwritems),
[Vprobe]	vprobeitems,	nelem(vprobeitems),
[Fan]		fanitems,	nelem(fanitems),
[Tprobe]		tprobeitems,	nelem(tprobeitems),
[Cprobe]		cprobeitems,	nelem(cprobeitems),
[Bootinfo]	bootinfoitems,	nelem(bootinfoitems),
[Merror64]	memerr64items,	nelem(memerr64items),
[Mgmt]		mgmtitems,	nelem(mgmtitems),
[Mgmtc]		mgmtcitems,	nelem(mgmtcitems),
[Mgmtdt]	mgmtdtitems,	nelem(mgmtdtitems),
[Memch]		memchanitems,	nelem(memchanitems),
[Ipmi]		ipmiitems,	nelem(ipmiitems),
[Ps]		psitems,		nelem(psitems),
[Obd]		obditems,	nelem(obditems),
};

void
setoffsets(Item *tab, int n)
{
	int i, o;

	o = 4;
	for(i = 0; i < n; i++){
		tab[i].offset = o;
		o += tab[i].nbytes;
	}
}

void
tabinit(void)
{
	int i;

	for(i = 0; i < nelem(itemtab); i++)
		setoffsets(itemtab[i].tab, itemtab[i].n);
}

char*
getstring(uchar *p, int i, uchar *e)
{
	uchar *q;

	if(i == 0)
		return nil;
	for(;; i--){
		if(i == 1)
			break;
		p = memchr(p, 0, e - p - 1);
		if(p == nil)
			sysfatal("bad strings");	// return nil;
		p++;
	}
	q = memchr(p, 0, e - p - 1);
	if(q == nil || q-p == 0)
		return nil;
	return strdup((char*)p);
}

void
crack(uchar *p, uchar *e, Item *tabi, Itemv *tabv, int n)
{
	int i, len;
	Item *t;
	Itemv *v;

	len = p[1];
	for(i = 0; i < n; i++){
		t = tabi + i;
		v = tabv + i;

		if(p + t->offset + t->nbytes >= p+len){
			/* cheater way to protect against new std. versions */
			continue;
		}

		switch(t->type){
		default:
			sysfatal("bad type");
		case String:
			v->s = getstring(p + len, p[t->offset], e);
			break;
		case Enumf:
		case Enumbv:
		case Integer:
			if(p + t->offset + t->nbytes >= e)
				sysfatal("range");
			v->i = getle(p + t->offset, t->nbytes);
			break;
		case Uchar:
			if(p + t->offset + t->nbytes >= e)
				sysfatal("range");
			memcpy(v->uuid, p + t->offset, t->nbytes);
			break;
		}
	}
}

void
dumpenum(Enum *p)
{
	for(; p->s != nil; p++)
		print("%s	%d\n", p->s, p->v);
}

char*
enumtostr(Enum *p, uvlong i)
{
	for(; p->s != nil; p++)
		if(i == p->v)
			return p->s;
	return nil;
}

int
strtoenum(Enum *p, char *s)
{
	for(; p->s != nil; p++)
		if(cistrcmp(s, p->s) == 0)
			return p->v;
	return -1;
}

char*
enumbvtostr(Enum *p, uint i, char *s0, char *e)
{
	char *s;

	s = s0;
	for(; p->s != nil; p++)
		if(i & p->v){
			if(s != s0)
				s = seprint(s, e, " ");
			s = seprint(s, e, "%s", p->s);
		}
	if(s != s0)
		return s0;
	return nil;
}

void
printitems(int type, Item *tabi, Itemv *tabv, int n)
{
	char *s, buf[128];
	int i;
	Item *t;
	Itemv *v;

//	print("type %d\n", type);
	USED(type);
	for(i = 0; i < n; i++){
		t = tabi + i;
		v = tabv + i;
		print("	%-12s	", t->name);
		switch(t->type){
		default:
			sysfatal("bad type");
		case String:
			print(t->fmt, v->s);
			break;
		case Integer:
			print(t->fmt, v->i);
			break;
		case Uchar:
			print(t->fmt, v->uuid);
			break;
		case Enumf:
			s = enumtostr(t->tab, v->i);
			if(s != nil){
				print(t->fmt, s);
				print(" (%#llux)", v->i);
			}
			else
				print("%lld", v->i);
			break;
		case Enumbv:
			s = enumbvtostr(t->tab, v->i, buf, buf+sizeof buf);
			if(s != nil){
				print(t->fmt, s);
				print(" (%#llux)", v->i);
			}
			else
				print("%lld", v->i);
			break;
		}
		print("\n");
	}
}

uchar*
next(uchar *p, uchar *e)
{
	int ns;

	p += p[1];
	if(p >= e)
		sysfatal("structures out-of-bounds");

	for(ns = 0;; ns++){
		p = memchr(p, 0, e - p - 1);
		if(p == nil)
			sysfatal("bad strings");
		p++;
		if(p[0] == 0){
			p++;
			break;
		}
	}
	USED(ns);
	return p;
}

Sm*
finddmi(void)
{
	uchar *buf, *p, *e;
	int type, hand, len, i, n;
	Itemv *v;
	Sm *sm;

	realmodefd();
	buf = emalloc(Dmisz);
	if(pread(rfd, buf, Dmisz, Dmistart) != Dmisz)
		sysfatal("read: %r");

	e = buf + Dmisz;
	for(p = buf; p < e; p += 16)
		if(memcmp(p, "_SM_", 4) == 0)
			goto found;
	sysfatal("no dmi structure");
found:
	print("found at %#lux\n", p-buf+Dmistart);
	len = p[5];
	/* do checksums */

	sm = emalloc(len);
	sm->raw = *(Smbuf*)p;
	print("dmi %ud.%ud\n", sm->raw.vers[0], sm->raw.vers[1]);

	sm->stpa = getle(sm->raw.stpa, 4);
	sm->stlen = getle(sm->raw.stlen, 2);
	print("structure pa %llux len %d count %d\n", sm->stpa, sm->stlen, (uint)getle(sm->raw.nstrut, 2));

	n = getle(sm->raw.nstrut, 2);

	free(buf);
	buf = emalloc(sm->stlen);
	if(pread(rfd, buf, sm->stlen, sm->stpa) != sm->stlen)
		sysfatal("read: %r");
	p = buf;
	e = buf + sm->stlen;
	for(i = 0; i < n; i++){
		type = p[0];
		hand = getle(p+2, 2);
		if(prtype != -1 && type != prtype)
			goto n;
		if(prhand != -1 && hand != prhand)
			goto n;
		print("%d: type %s %ud len %ud handle %ud\n",
			i, enumtostr(tabletab, p[0]), p[0], p[1], hand);
		if(type < nelem(itemtab)){
			v = emalloc(itemtab[type].n*sizeof *v);
			crack(p, e, itemtab[type].tab, v, itemtab[type].n);
			printitems(type, itemtab[type].tab, v, itemtab[type].n);
			free(v);
		}
	n:
		p = next(p, e);
	}
	return sm;
}

void
usage(void)
{
	fprint(2, "usage: dmi [-t type] [-h hand]\n");
	exits("usage");
}

void
main(int argc, char **argv)
{
	char *s, *p;

	fmtinstall(L'ℛ', fmtℛ);
	fmtinstall(L'𝒮', fmt𝒮);
	fmtinstall(L'×', fmt×);
	fmtinstall('H', encodefmt);
	tabinit();

	ARGBEGIN{
	case 't':
		s = EARGF(usage());
		prtype = strtoul(s, &p, 0);
		if(p == s)
			prtype = strtoenum(tabletab, s);
		if(prtype == -1)
			sysfatal("bad type");
		break;
	case 'h':
		prhand = strtoul(EARGF(usage()), &p, 0);
		break;
	case 'l':
		dumpenum(tabletab);
		exits("");
	default:
		usage();
	}ARGEND
	if(argc != 0)
		usage();

	finddmi();
//	printdmi();
	exits("");
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.