File: dmidecode.c

package info (click to toggle)
uswsusp 0.7-1.2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,000 kB
  • ctags: 962
  • sloc: ansic: 6,060; xml: 755; sh: 425; makefile: 176
file content (207 lines) | stat: -rw-r--r-- 4,387 bytes parent folder | download | duplicates (3)
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
/*
 *	DMI decoder for s2ram
 *
 *	(C) 2000,2001 Alan Cox <alan@redhat.com>
 *      
 *      2-July-2001 Matt Domsch <Matt_Domsch@dell.com>
 *      Additional structures displayed per SMBIOS 2.3.1 spec
 *
 *	(C) 2006 Pavel Machek <pavel@suse.cz>
 *
 *	Licensed under the GNU General Public license v2.
 */

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/mman.h>

typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;

struct dmi_header
{
	u8	type;
	u8	length;
	u16	handle;
};

#ifdef S2RAM
extern char bios_version[1024], sys_vendor[1024], sys_product[1024], sys_version[1024];
#define PRINTF(a...)
#else
char bios_version[1024], sys_vendor[1024], sys_product[1024], sys_version[1024];
#define PRINTF printf
#endif

static char *dmi_string(struct dmi_header *dm, u8 s)
{
	char *bp=(char *)dm;
	if (!s) return "";
	
	bp+=dm->length;
	while (s>1) {
		bp+=strlen(bp);
		bp++;
		s--;
	}
	return bp;
}

/*
 * Copy a physical memory chunk into a memory buffer.
 * This function allocates memory.
 */
void *mem_chunk(size_t base, size_t len)
{
	void *p;
	int fd;
	size_t mmoffset;
	void *mmp;

	if((fd=open("/dev/mem", O_RDONLY))==-1){
		perror("/dev/mem");
		return NULL;
        }

	if((p=malloc(len))==NULL) {
		perror("/dev/mem malloc");
		return NULL;
	}

	mmoffset=base%getpagesize();
	/*
	 * Please note that we don't use mmap() for performance reasons here,
	 * but to workaround problems many people encountered when trying
	 * to read from /dev/mem using regular read() calls.
	 */
	mmp=mmap(0, mmoffset+len, PROT_READ, MAP_SHARED, fd, base-mmoffset);
	if(mmp==MAP_FAILED) {
		perror("mmap");
		free(p);
		return NULL;
	}

	memcpy(p, (u8 *)mmp+mmoffset, len);

	if(munmap(mmp, mmoffset+len)==-1)
		perror("/dev/mem munmap");

	if(close(fd)==-1)
		perror("/dev/mem");

	return p;
}

static void dmi_table(u32 base, int len, int num)
{
	char *buf;
	struct dmi_header *dm;
	char *data;
	int i=0;
	
	if ((buf=mem_chunk(base, len))==0) {
		printf("DMI Table is unreachable\n");
		return;
	}
	
	data = buf;
	while (i<num) {
		u32 u;
		u32 u2;
		dm=(struct dmi_header *)data;
		switch(dm->type)
		{
		case  0:
			PRINTF("\tBIOS Information Block\n");
			PRINTF("\t\tVendor: %s\n", 
			       dmi_string(dm, data[4]));
			strcpy(bios_version, dmi_string(dm, data[5]));
			PRINTF("\t\tVersion: %s\n", bios_version);
			PRINTF("\t\tRelease: %s\n",
			       dmi_string(dm, data[8]));
			PRINTF("\t\tBIOS base: 0x%04X0\n",
			       data[7]<<8|data[6]);
			PRINTF("\t\tROM size: %dK\n",
			       64*data[9]);
			PRINTF("\t\tCapabilities:\n");
			u=data[13]<<24|data[12]<<16|data[11]<<8|data[10];		
			u2=data[17]<<24|data[16]<<16|data[15]<<8|data[14];
			PRINTF("\t\t\tFlags: 0x%08X%08X\n",
			       u2,u);
			break;
			
		case 1:
			PRINTF("\tSystem Information Block\n");
			strcpy(sys_vendor, dmi_string(dm, data[4]));
			strcpy(sys_product, dmi_string(dm, data[5]));
			strcpy(sys_version, dmi_string(dm, data[6]));
			PRINTF("\t\tVendor: %s\n", sys_vendor);
			PRINTF("\t\tProduct: %s\n", sys_product);
			PRINTF("\t\tVersion: %s\n", sys_version);
			PRINTF("\t\tSerial Number: %s\n",
			       dmi_string(dm, data[7]));
			break;
			
		case 2:
			PRINTF("\tBoard Information Block\n");
			PRINTF("\t\tVendor: %s\n",
			       dmi_string(dm, data[4]));
			PRINTF("\t\tProduct: %s\n",
			       dmi_string(dm, data[5]));
			PRINTF("\t\tVersion: %s\n",
			       dmi_string(dm, data[6]));
			PRINTF("\t\tSerial Number: %s\n",
			       dmi_string(dm, data[7]));
			break;
			
		default:
			break;
		}
		data+=dm->length;
		while(*data || data[1])
			data++;
		data+=2;
		i++;
	}
	free(buf);
}

void dmi_scan(void)
{
	size_t fp;
	u8 *buf;

	if((buf=mem_chunk(0xF0000, 0x10000))==NULL)
		return;

	for(fp=0; fp<=0xFFF0; fp+=16) {
		if(memcmp(buf+fp, "_DMI_", 5)==0) {
			u8 *cur = buf+fp;
			u16 num=cur[13]<<8|cur[12];
			u16 len=cur[7]<<8|cur[6];
			u32 base=cur[11]<<24|cur[10]<<16|cur[9]<<8|cur[8];

			PRINTF("DMI %d.%d present.\n",
			       cur[14]>>4, cur[14]&0x0F);
			PRINTF("%d structures occupying %d bytes.\n",
			       cur[13]<<8|cur[12],
			       cur[7]<<8|cur[6]);
			PRINTF("DMI table at 0x%08X.\n",
			       cur[11]<<24|cur[10]<<16|cur[9]<<8|cur[8]);
			dmi_table(base,len,num);
		}
	}
	free(buf);
	return;
}

#ifndef S2RAM
int main(int argc, char *argv[])
{
	dmi_scan();
}
#endif