summaryrefslogtreecommitdiffstats
path: root/kexec/firmware_memmap.c
blob: 1ee214aa9316a472ed767a10612b7f0a2150480b (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
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
/*
 * firmware_memmap.c: Read /sys/firmware/memmap
 *
 * Created by: Bernhard Walle (bernhard.walle@gmx.de)
 * Copyright (C) SUSE LINUX Products GmbH, 2008. All rights reserved
 *
 * This program 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 of the License).
 *
 * This program 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 have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#define _GNU_SOURCE /* for ULLONG_MAX without C99 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "firmware_memmap.h"
#include "kexec.h"

/*
 * If the system is too old for ULLONG_MAX or LLONG_MAX, define it here.
 */
#ifndef ULLONG_MAX
#    define ULLONG_MAX (~0ULL)
#endif /* ULLONG_MAX */

#ifndef LLONG_MAX
#    define LLONG_MAX (~0ULL >> 1)
#endif /* LLONG_MAX */


/**
 * The full path to the sysfs interface that provides the memory map.
 */
#define FIRMWARE_MEMMAP_DIR  "/sys/firmware/memmap"

/**
 * Parses a file that only contains one number. Typical for sysfs files.
 *
 * @param[in] filename the name of the file that should be parsed
 * @return the value that has been read or ULLONG_MAX on error.
 */
static unsigned long long parse_numeric_sysfs(const char *filename)
{
	FILE *fp;
	char linebuffer[BUFSIZ];
	unsigned long long retval = ULLONG_MAX;

	fp = fopen(filename, "r");
	if (!fp) {
		fprintf(stderr, "Opening \"%s\" failed: %s\n",
			filename, strerror(errno));
		return ULLONG_MAX;
	}

	if (!fgets(linebuffer, BUFSIZ, fp))
		goto err;

	linebuffer[BUFSIZ-1] = 0;

	/* let strtoll() detect the base */
	retval = strtoll(linebuffer, NULL, 0);

err:
	fclose(fp);

	return retval;
}

/**
 * Reads the contents of a one-line sysfs file to buffer. (This function is
 * not threadsafe.)
 *
 * @param[in] filename the name of the file that should be read
 *
 * @return NULL on failure, a pointer to a static buffer (that should be copied
 *         with strdup() if the caller plans to use it after next function call)
 */
static char *parse_string_sysfs(const char *filename)
{
	FILE *fp;
	static char linebuffer[BUFSIZ];
	char *end;

	fp = fopen(filename, "r");
	if (!fp) {
		fprintf(stderr, "Opening \"%s\" failed: %s\n",
			filename, strerror(errno));
		return NULL;
	}

	if (!fgets(linebuffer, BUFSIZ, fp)) {
		fclose(fp);
		return NULL;
	}

	linebuffer[BUFSIZ-1] = 0;

	/* truncate trailing newline(s) */
	end = linebuffer + strlen(linebuffer) - 1;
	while (*end == '\n')
		*end-- = 0;

	fclose(fp);

	return linebuffer;

}

static int parse_memmap_entry(const char *entry, struct memory_range *range)
{
	char filename[PATH_MAX];
	char *type;

	/*
	 * entry/start
	 */
	snprintf(filename, PATH_MAX, "%s/%s", entry, "start");
	filename[PATH_MAX-1] = 0;

	range->start = parse_numeric_sysfs(filename);
	if (range->start == ULLONG_MAX)
		return -1;

	/*
	 * entry/end
	 */
	snprintf(filename, PATH_MAX, "%s/%s", entry, "end");
	filename[PATH_MAX-1] = 0;

	range->end = parse_numeric_sysfs(filename);
	if (range->end == ULLONG_MAX)
		return -1;

	/*
	 * entry/type
	 */
	snprintf(filename, PATH_MAX, "%s/%s", entry, "type");
	filename[PATH_MAX-1] = 0;

	type = parse_string_sysfs(filename);
	if (!type)
		return -1;

	if (strcmp(type, "System RAM") == 0)
		range->type = RANGE_RAM;
	else if (strcmp(type, "ACPI Tables") == 0)
		range->type = RANGE_ACPI;
	else if (strcmp(type, "Unusable memory") == 0)
		range->type = RANGE_RESERVED;
	else if (strcmp(type, "reserved") == 0)
		range->type = RANGE_RESERVED;
	else if (strcmp(type, "Reserved") == 0)
		range->type = RANGE_RESERVED;
	else if (strcmp(type, "Unknown E820 type") == 0)
		range->type = RANGE_RESERVED;
	else if (strcmp(type, "ACPI Non-volatile Storage") == 0)
		range->type = RANGE_ACPI_NVS;
	else if (strcmp(type, "Uncached RAM") == 0)
		range->type = RANGE_UNCACHED;
	else if (strcmp(type, "Persistent Memory (legacy)") == 0)
		range->type = RANGE_PRAM;
	else if (strcmp(type, "Persistent Memory") == 0)
		range->type = RANGE_PMEM;
	else {
		fprintf(stderr, "Unknown type (%s) while parsing %s. Please "
			"report this as bug. Using RANGE_RESERVED now.\n",
			type, filename);
		range->type = RANGE_RESERVED;
	}

	return 0;
}

/* documentation: firmware_memmap.h */
int compare_ranges(const void *first, const void *second)
{
	const struct memory_range *first_range = first;
	const struct memory_range *second_range = second;

	/*
	 * don't use the "first_range->start - second_range->start"
	 * notation because unsigned long long might overflow
	 */
	if (first_range->start > second_range->start)
		return 1;
	else if (first_range->start < second_range->start)
		return -1;
	else /* first_range->start == second_range->start */
		return 0;
}

/* documentation: firmware_memmap.h */
int have_sys_firmware_memmap(void)
{
	int ret;
	struct stat mystat;

	ret = stat(FIRMWARE_MEMMAP_DIR, &mystat);
	if (ret != 0)
		return 0;

	return S_ISDIR(mystat.st_mode);
}

/* documentation: firmware_memmap.h */
int get_firmware_memmap_ranges(struct memory_range *range, size_t *ranges)
{
	DIR *firmware_memmap_dir = NULL;
	struct dirent *dirent;
	int i = 0;

	/* argument checking */
	if (!range || !ranges) {
		fprintf(stderr, "%s: Invalid arguments.\n", __FUNCTION__);
		return -1;
	}

	/* open the directory */
	firmware_memmap_dir = opendir(FIRMWARE_MEMMAP_DIR);
	if (!firmware_memmap_dir) {
		perror("Could not open \"" FIRMWARE_MEMMAP_DIR "\"");
		goto error;
	}

	/* parse the entries */
	while ((dirent = readdir(firmware_memmap_dir)) != NULL) {
		int ret;
		char full_path[PATH_MAX];

		/* array overflow check */
		if ((size_t)i >= *ranges) {
			fprintf(stderr, "The firmware provides more entries "
				"allowed (%zd). Please report that as bug.\n",
				*ranges);
			goto error;
		}

		/* exclude '.' and '..' */
		if (dirent->d_name[0] && dirent->d_name[0] == '.') {
			continue;
		}

		snprintf(full_path, PATH_MAX, "%s/%s", FIRMWARE_MEMMAP_DIR,
			dirent->d_name);
		full_path[PATH_MAX-1] = 0;
		ret = parse_memmap_entry(full_path, &range[i]);
		if (ret < 0) {
			goto error;
		}

		i++;
	}

	/* close the dir as we don't need it any more */
	closedir(firmware_memmap_dir);

	/* update the number of ranges for the caller */
	*ranges = i;

	/* and finally sort the entries with qsort */
	qsort(range, *ranges, sizeof(struct memory_range), compare_ranges);

	return 0;

error:
	if (firmware_memmap_dir) {
		closedir(firmware_memmap_dir);
	}
	return -1;
}