summaryrefslogtreecommitdiffstats
path: root/perl-install/c/smp-dmi.c
blob: c3eb9ead1dd480de5899f37c087176d21bddc730 (plain)
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
/*
 *	CPU detetion based on DMI decode rev 1.2
 *
 *	(C) 2003 Nicolas Planel <nplanel@mandrakesoft.com>
 *      
 *	Licensed under the GNU Public license. If you want to use it in with
 *	another license just ask.
 */

#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;


#define DEFAULT_MEM_DEV "/dev/mem"

void *mem_chunk(u32 base, u32 len, const char *devmem)
{
    void *p;
    int fd;
    off_t mmoffset;
    void *mmp;

    if ((fd = open(devmem, O_RDONLY)) < 0)
	return NULL;

    if ((p = malloc(len)) == NULL)
	return NULL;

    mmoffset = base % getpagesize();
    mmp = mmap(0, mmoffset + len, PROT_READ, MAP_SHARED, fd, base - mmoffset);
    if (mmp == MAP_FAILED) {
	free(p);
	return NULL;
    }

    memcpy(p, (u8 *)mmp + mmoffset, len);
    munmap(mmp, mmoffset + len);
    close(fd);
    return p;
}


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

typedef int (*dmi_decode)(u8 * data);

static int decode_handle(u32 base, int len, int num, dmi_decode decode)
{
    u8 *buf;
    u8 *data;
    int i = 0;
    int ret = 0;

    if ((buf = mem_chunk(base, len, DEFAULT_MEM_DEV)) == NULL)
	return 0;

    data = buf;
    while(i<num && data+sizeof(struct dmi_header)<=buf+len)
    {
	u8 *next;
	struct dmi_header *dm = (struct dmi_header *)data;

	/* look for the next handle */
	next=data+dm->length;
	while(next-buf+1<len && (next[0]!=0 || next[1]!=0))
	    next++;
	next+=2;
	if(next-buf<=len)
	    ret += decode(data);
	else {
	    ret = 0; /* TRUNCATED */
	    break;
	}
	data=next;
	i++;
    }

    free(buf);
    return ret;
}

static int dmi_detect(dmi_decode decode) {
    u8 *buf;
    long fp;
    int ret;

    if ((buf = mem_chunk(0xf0000, 0x10000, DEFAULT_MEM_DEV)) == NULL) {
	perror("dmi_detect");
	exit(1);
    }

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

	    ret = decode_handle(base, len, num, decode);
	    break;
	}
    }

    free(buf);
    return ret;
}

static int processor(u8 *data) {
	struct dmi_header *dm = (struct dmi_header *)data;

	if((dm->type == 4) && /*"Central Processor"*/(data[5] == 3)) {
		if(/*Processor Manufacturer*/data[7] != 0
		   && /*Populated*/(data[0x18] & (1 << 6))
		   && !/*Disabled {User:2h, BIOS:3}*/(data[0x18] & (1 << 1)))
			return 1;
	}
	return 0;
}

int intelDetectSMP(void) {
	return dmi_detect(processor) > 1;
}