summaryrefslogtreecommitdiffstats
path: root/discover/yaboot-parser.c
blob: 430649266aa77bcf6f67f5a1ed89466e441cd129 (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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#define _GNU_SOURCE

#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include "log/log.h"
#include "talloc/talloc.h"
#include "pb-protocol/pb-protocol.h"
#include "parser-conf.h"
#include "parser-utils.h"
#include "paths.h"

struct yaboot_state {
	struct boot_option *opt;
	const char *desc_image;
	char *desc_initrd;
	int globals_done;
	const char *const *known_names;
};

static void yaboot_finish(struct conf_context *conf)
{
	struct yaboot_state *state = conf->parser_info;

	if (!state->desc_image) {
		pb_log("%s: %s: no image found\n", __func__,
			conf->dc->device->id);
		return;
	}

	assert(state->opt);
	assert(state->opt->name);
	assert(state->opt->boot_args);

	state->opt->description = talloc_asprintf(state->opt, "%s %s %s",
		state->desc_image,
		(state->desc_initrd ? state->desc_initrd : ""),
		state->opt->boot_args);

	talloc_free(state->desc_initrd);
	state->desc_initrd = NULL;

	conf_strip_str(state->opt->boot_args);
	conf_strip_str(state->opt->description);

	/* opt is persistent, so must be associated with device */

	device_add_boot_option(conf->dc->device, state->opt);
	state->opt = talloc_zero(conf->dc->device, struct boot_option);
	state->opt->boot_args = talloc_strdup(state->opt, "");
}

static void yaboot_process_pair(struct conf_context *conf, const char *name,
		char *value)
{
	struct yaboot_state *state = conf->parser_info;
	struct fixed_pair {
		const char *image;
		const char *initrd;
	};
	static const struct fixed_pair suse_fp32 = {
		.image = "/suseboot/vmlinux32",
		.initrd = "/suseboot/initrd32",
	};
	static const struct fixed_pair suse_fp64 = {
		.image = "/suseboot/vmlinux64",
		.initrd = "/suseboot/initrd64",
	};
	const struct fixed_pair *suse_fp;

	/* fixup for bare values */

	if (!name)
		name = value;

	if (!state->globals_done && conf_set_global_option(conf, name, value))
		return;

	if (!conf_param_in_list(state->known_names, name))
		return;

	state->globals_done = 1;

	/* image */

	if (streq(name, "image")) {
		const char *g_boot = conf_get_global_option(conf, "boot");
		const char *g_part = conf_get_global_option(conf, "partition");

		/* First finish any previous image. */

		if (state->opt->boot_image_file)
			yaboot_finish(conf);

		/* Then start the new image. */

		if (g_boot && g_part) {
			char* dev = talloc_asprintf(NULL, "%s%s", g_boot,
				g_part);

			state->opt->boot_image_file = resolve_path(state->opt,
				value, dev);
			state->desc_image = talloc_asprintf(state->opt,
				"%s%s", dev, value);
			talloc_free(dev);
		} else if (g_boot) {
			state->opt->boot_image_file = resolve_path(state->opt,
				value, g_boot);
			state->desc_image = talloc_asprintf(state->opt,
				"%s%s", g_boot, value);
		} else {
			state->opt->boot_image_file = resolve_path(state->opt,
				value, conf->dc->device_path);
			state->desc_image = talloc_strdup(state->opt, value);
		}

		return;
	}

	/* Special processing for SUSE install CD. */

	if (streq(name, "image[32bit]"))
		suse_fp = &suse_fp32;
	else if (streq(name, "image[64bit]"))
		suse_fp = &suse_fp64;
	else
		suse_fp = NULL;

	if (suse_fp) {
		/* First finish any previous image. */

		if (state->opt->boot_image_file)
			yaboot_finish(conf);

		/* Then start the new image. */

		if (*value == '/') {
			state->opt->boot_image_file = resolve_path(state->opt,
				value, conf->dc->device_path);
			state->desc_image = talloc_strdup(state->opt, value);
		} else {
			state->opt->boot_image_file = resolve_path(state->opt,
				suse_fp->image, conf->dc->device_path);
			state->desc_image = talloc_strdup(state->opt,
				suse_fp->image);

			state->opt->initrd_file = resolve_path(state->opt,
				suse_fp->initrd, conf->dc->device_path);
			state->desc_initrd = talloc_asprintf(state, "initrd=%s",
				suse_fp->initrd);
		}

		return;
	}

	if (!state->opt->boot_image_file) {
		pb_log("%s: unknown name: %s\n", __func__, name);
		return;
	}

	/* initrd */

	if (streq(name, "initrd")) {
		const char *g_boot = conf_get_global_option(conf, "boot");
		const char *g_part = conf_get_global_option(conf, "partition");

		if (g_boot && g_part) {
			char* dev = talloc_asprintf(NULL, "%s%s", g_boot,
				g_part);

			state->opt->initrd_file = resolve_path(state->opt,
				value, dev);
			state->desc_initrd = talloc_asprintf(state,
				"initrd=%s%s", dev, value);
			talloc_free(dev);
		} else if (g_boot) {
			state->opt->initrd_file = resolve_path(state->opt,
				value, g_boot);
			state->desc_initrd = talloc_asprintf(state,
				"initrd=%s%s", g_boot, value);
		} else {
			state->opt->initrd_file = resolve_path(state->opt,
				value, conf->dc->device_path);
			state->desc_initrd = talloc_asprintf(state, "initrd=%s",
				value);
		}
		return;
	}

	/* label */

	if (streq(name, "label")) {
		state->opt->id = talloc_asprintf(state->opt, "%s#%s",
			conf->dc->device->id, value);
		state->opt->name = talloc_strdup(state->opt, value);
		return;
	}

	/* args */

	if (streq(name, "append")) {
		state->opt->boot_args = talloc_asprintf_append(
			state->opt->boot_args, "%s ", value);
		return;
	}

	if (streq(name, "initrd-size")) {
		state->opt->boot_args = talloc_asprintf_append(
			state->opt->boot_args, "ramdisk_size=%s ", value);
		return;
	}

	if (streq(name, "literal")) {
		if (*state->opt->boot_args) {
			pb_log("%s: literal over writes '%s'\n", __func__,
				state->opt->boot_args);
			talloc_free(state->opt->boot_args);
		}
		talloc_asprintf(state->opt, "%s ", value);
		return;
	}

	if (streq(name, "ramdisk")) {
		state->opt->boot_args = talloc_asprintf_append(
			state->opt->boot_args, "ramdisk=%s ", value);
		return;
	}

	if (streq(name, "read-only")) {
		state->opt->boot_args = talloc_asprintf_append(
			state->opt->boot_args, "ro ");
		return;
	}

	if (streq(name, "read-write")) {
		state->opt->boot_args = talloc_asprintf_append(
			state->opt->boot_args, "rw ");
		return;
	}

	if (streq(name, "root")) {
		state->opt->boot_args = talloc_asprintf_append(
			state->opt->boot_args, "root=%s ", value);
		return;
	}

	pb_log("%s: unknown name: %s\n", __func__, name);
}

static struct conf_global_option yaboot_global_options[] = {
	{ .name = "boot" },
	{ .name = "initrd" },
	{ .name = "partition" },
	{ .name = "video" },
	{ .name = NULL },
};

static const char *const yaboot_conf_files[] = {
	"/yaboot.conf",
	"/yaboot.cnf",
	"/etc/yaboot.conf",
	"/etc/yaboot.cnf",
	"/suseboot/yaboot.cnf",
	"/YABOOT.CONF",
	"/YABOOT.CNF",
	"/ETC/YABOOT.CONF",
	"/ETC/YABOOT.CNF",
	"/SUSEBOOT/YABOOT.CNF",
	NULL
};

static const char *yaboot_known_names[] = {
	"append",
	"image",
	"image[64bit]", /* SUSE extension */
	"image[32bit]", /* SUSE extension */
	"initrd",
	"initrd-size",
	"label",
	"literal",
	"ramdisk",
	"read-only",
	"read-write",
	"root",
	NULL
};

static int yaboot_parse(struct discover_context *dc)
{
	struct conf_context *conf;
	struct yaboot_state *state;
	int rc;

	conf = talloc_zero(dc, struct conf_context);

	if (!conf)
		return 0;

	conf->dc = dc;
	conf->global_options = yaboot_global_options,
	conf_init_global_options(conf);
	conf->conf_files = yaboot_conf_files,
	conf->get_pair = conf_get_pair_equal;
	conf->process_pair = yaboot_process_pair;
	conf->finish = yaboot_finish;
	conf->parser_info = state = talloc_zero(conf, struct yaboot_state);

	state->known_names = yaboot_known_names;

	/* opt is persistent, so must be associated with device */

	state->opt = talloc_zero(conf->dc->device, struct boot_option);
	state->opt->boot_args = talloc_strdup(state->opt, "");

	rc = conf_parse(conf);

	talloc_free(conf);
	return rc;
}

define_parser(yaboot, yaboot_parse);
OpenPOWER on IntegriCloud