~ubuntu-core-dev/base-installer/maverick-proposed

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
/* Some code here borrowed from dmidecode, also GPLed and copyrighted by:
 *   (C) 2000-2002 Alan Cox <alan@redhat.com>
 *   (C) 2002-2005 Jean Delvare <khali@linux-fr.org>
 * I (Colin Watson) copied it in reduced form rather than using dmidecode
 * directly because (a) the d-i initrd is tight on space and this is much
 * smaller and (b) parsing the output of dmidecode in shell is unreasonably
 * painful.
 */

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

#define WORD(x) (*(const uint16_t *)(x))
#define DWORD(x) (*(const uint32_t *)(x))

struct dmi_header
{
	uint8_t type;
	uint8_t length;
	uint16_t handle;
};

static int checksum(const uint8_t *buf, size_t len)
{
	uint8_t sum = 0;
	size_t a;

	for (a = 0; a < len; a++)
		sum += buf[a];
	return (sum == 0);
}

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

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

	p = malloc(len);
	if (p == NULL)
	{
		close(fd);
		return NULL;
	}

#ifdef _SC_PAGESIZE
	mmoffset = base % sysconf(_SC_PAGESIZE);
#else
	mmoffset = base % getpagesize();
#endif
	/* 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)
	{
		free(p);
		close(fd);
		return NULL;
	}

	memcpy(p, mmp + mmoffset, len);

	munmap(mmp, mmoffset + len);

	close(fd);

	return p;
}

static uint64_t dmi_table(uint32_t base, uint16_t len, uint16_t num)
{
	uint8_t *buf, *data;
	int i = 0;
	uint64_t available = 0;

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

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

		/* Stop decoding at end of table marker */
		if (h->type == 127)
			break;

		/* Look for the next handle */
		next = data + h->length;
		while (next - buf + 1 < len && (next[0] != 0 || next[1] != 0))
			next++;
		next += 2;
		/* Memory Array Mapped Address */
		if (h->type == 19 && h->length >= 0x0F)
		{
			uint64_t start, end;
			start = DWORD(data + 0x04);
			end = DWORD(data + 0x08);
			if (end >= start)
				/* output in kilobytes */
				available += end - start + 1;
		}

		data = next;
		i++;
	}

	free(buf);
	return available;
}

static uint64_t smbios_decode(uint8_t *buf)
{
	if (checksum(buf, buf[0x05]) &&
	    memcmp(buf + 0x10, "_DMI_", 5) == 0 &&
	    checksum(buf + 0x10, 0x0F))
	{
		return dmi_table(DWORD(buf + 0x18), WORD(buf + 0x16),
				 WORD(buf + 0x1C));
	}

	return 0;
}

static uint64_t legacy_decode(uint8_t *buf)
{
	if (checksum(buf, 0x0F))
	{
		return dmi_table(DWORD(buf + 0x08), WORD(buf + 0x06),
				 WORD(buf + 0x0C));
	}

	return 0;
}

static uint64_t dmi_available_memory(void)
{
	uint8_t *buf;
	size_t fp;
	uint64_t ret = 0;

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

	for (fp = 0; fp <= 0xFFF0; fp += 16)
	{
		if (memcmp(buf + fp, "_SM_", 4) == 0 && fp <= 0xFFE0)
		{
			ret = smbios_decode(buf + fp);
			if (ret)
				break;
			fp += 16;
		}
		else if (memcmp(buf + fp, "_DMI_", 5) == 0)
		{
			ret = legacy_decode(buf + fp);
			if (ret)
				break;
		}
	}

	free(buf);
	return ret;
}

int main(int argc, char **argv)
{
	printf("%llu\n", dmi_available_memory());
	return 0;
}