summaryrefslogtreecommitdiffstats
path: root/discover/kboot-parser.c
blob: df2e7620e13ad8ea40a735aaa04da60881f43de1 (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
287
288
#define _GNU_SOURCE

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "parser.h"
#include "params.h"

#define buf_size 1024

static const char *devpath;

static int param_is_ignored(const char *param)
{
	static const char *ignored_options[] =
		{ "message", "timeout", "default", NULL };
	const char **str;

	for (str = ignored_options; *str; str++)
		if (streq(*str, param))
			return 1;
	return 0;
}

/**
 * Splits a name=value pair, with value terminated by @term (or nul). if there
 * is no '=', then only the value is populated, and *name is set to NULL. The
 * string is modified in place.
 *
 * Returns the next byte to process, or null if we've hit the end of the
 * string.
 *
 * */
static char *get_param_pair(char *str, char **name_out, char **value_out,
		char terminator)
{
	char *sep, *tmp, *name, *value;

	/* terminate the value */
	tmp = strchr(str, terminator);
	if (tmp)
		*tmp = 0;
	else
		tmp = NULL;

	sep = strchr(str, '=');
	if (!sep) {
		*name_out = NULL;
		*value_out = str;
		return tmp ? tmp + 1 : NULL;
	}

	/* terminate the name */
	*sep = 0;

	/* remove leading spaces */
	for (name = str; isspace(*name); name++);
	for (value = sep + 1; isspace(*value); value++);

	/* .. and trailing ones.. */
	for (sep--; isspace(*sep); sep--)
		*sep = 0;
	for (sep = value + strlen(value) - 1; isspace(*sep); sep--)
		*sep = 0;

	*name_out = name;
	*value_out = value;

	return tmp ? tmp + 1 : NULL;
}

struct global_option {
	char *name;
	char *value;
};


static struct global_option global_options[] = {
	{ .name = "root" },
	{ .name = "initrd" },
	{ .name = "video" },
	{ .name = NULL }
};

/*
 * Check if an option (name=value) is a global option. If so, store it in
 * the global options table, and return 1. Otherwise, return 0.
 */
static int check_for_global_option(const char *name, const char *value)
{
	int i;

	for (i = 0; global_options[i].name ;i++) {
		if (!strcmp(name, global_options[i].name)) {
			global_options[i].value = strdup(value);
			return 1;
		}
	}
	return 0;
}

static char *get_global_option(const char *name)
{
	int i;

	for (i = 0; global_options[i].name ;i++)
		if (!strcmp(name, global_options[i].name))
			return global_options[i].value;

	return NULL;
}

static int parse_option(struct boot_option *opt, char *config)
{
	char *pos, *name, *value, *root, *initrd, *cmdline, *tmp;

	root = initrd = cmdline = NULL;

	/* remove quotes around the value */
	while (*config == '"' || *config == '\'')
		config++;

	pos = config + strlen(config) - 1;
	while (*pos == '"' || *pos == '\'')
		*(pos--) = 0;

	if (!strlen(pos))
		return 0;

	pos = strchr(config, ' ');

	/* if there's no space, it's only a kernel image with no params */
	if (!pos) {
		opt->boot_image_file = resolve_path(config, devpath);
		opt->description = strdup(config);
		return 1;
	}

	*pos = 0;
	opt->boot_image_file = resolve_path(config, devpath);

	cmdline = malloc(buf_size);
	*cmdline = 0;

	for (pos++; pos;) {
		pos = get_param_pair(pos, &name, &value, ' ');

		if (!name) {
			strcat(cmdline, " ");
			strcat(cmdline, value);

		} else if (streq(name, "initrd")) {
			initrd = value;

		} else if (streq(name, "root")) {
			root = value;

		} else {
			strcat(cmdline, " ");
			*(value - 1) = '=';
			strcat(cmdline, name);
		}
	}

	if (!root)
		root = get_global_option("root");
	if (!initrd)
		initrd = get_global_option("initrd");

	if (initrd) {
		asprintf(&tmp, "initrd=%s %s", initrd, cmdline);
		free(cmdline);
		cmdline = tmp;

		opt->initrd_file = resolve_path(initrd, devpath);
	}

	if (root) {
		asprintf(&tmp, "root=%s %s", root, cmdline);
		free(cmdline);
		cmdline = tmp;

	} else if (initrd) {
		/* if there's an initrd but no root, fake up /dev/ram0 */
		asprintf(&tmp, "root=/dev/ram0 %s", cmdline);
		free(cmdline);
		cmdline = tmp;
	}

	pb_log("kboot cmdline: %s\n", cmdline);
	opt->boot_args = cmdline;

	asprintf(&opt->description, "%s %s",
			config, opt->boot_args);

	return 1;
}

static void parse_buf(struct device *dev, char *buf)
{
	char *pos, *name, *value;
	int sent_device = 0;

	for (pos = buf; pos;) {
		struct boot_option opt;

		pos = get_param_pair(pos, &name, &value, '\n');

		pb_log("kboot param: '%s' = '%s'\n", name, value);

		if (name == NULL || param_is_ignored(name))
			continue;

		if (*name == '#')
			continue;

		if (check_for_global_option(name, value))
			continue;

		memset(&opt, 0, sizeof(opt));
		opt.name = strdup(name);

		if (parse_option(&opt, value))
			if (!sent_device++)
				add_device(dev);
			add_boot_option(&opt);

		free(opt.name);
	}
}

static int parse(const char *device)
{
	char *filepath, *buf;
	int fd, len, rc = 0;
	struct stat stat;
	struct device *dev;

	devpath = device;

	filepath = resolve_path("/etc/kboot.conf", devpath);

	fd = open(filepath, O_RDONLY);
	if (fd < 0)
		goto out_free_path;

	if (fstat(fd, &stat))
		goto out_close;

	buf = malloc(stat.st_size + 1);
	if (!buf)
		goto out_close;;

	len = read(fd, buf, stat.st_size);
	if (len < 0)
		goto out_free_buf;
	buf[len] = 0;

	dev = malloc(sizeof(*dev));
	memset(dev, 0, sizeof(*dev));
	dev->id = strdup(device);
	dev->icon_file = strdup(generic_icon_file(guess_device_type()));

	parse_buf(dev, buf);

	rc = 1;

out_free_buf:
	free(buf);
out_close:
	close(fd);
out_free_path:
	free(filepath);
	return rc;
}

struct parser kboot_parser = {
	.name = "kboot.conf parser",
	.priority = 98,
	.parse	  = parse
};
OpenPOWER on IntegriCloud