summaryrefslogtreecommitdiffstats
path: root/discover/yaboot-parser.c
blob: d9f2aff5924f6e250074722c97dff0deaa3a66de (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
#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 found_suse;
	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;

	/* 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")) {
		if (state->opt->boot_image_file)
			yaboot_finish(conf);

		state->opt->boot_image_file = resolve_path(state->opt, value,
			conf->dc->device_path);
		state->desc_image = talloc_strdup(state->opt, value);
		return;
	}

	if (streq(name, "image[32bit]") || streq(name, "image[64bit]")) {
		state->found_suse = 1;

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

		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 {
			char *s;
			asprintf(&s, "/suseboot/%s", value);
			state->opt->boot_image_file = resolve_path(state->opt,
				s, conf->dc->device_path);
			state->desc_image = talloc_strdup(state->opt, s);
			free(s);
		}

		return;
	}

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

	/* initrd */

	if (streq(name, "initrd")) {
		if (!state->found_suse || (*value == '/')) {
			state->opt->initrd_file = resolve_path(state->opt,
				value, conf->dc->device_path);
			state->desc_initrd = talloc_asprintf(state, "initrd=%s",
				value);
		} else {
			char *s;
			asprintf(&s, "/suseboot/%s", value);
			state->opt->initrd_file = resolve_path(state->opt,
				s, conf->dc->device_path);
			state->desc_initrd = talloc_asprintf(state, "initrd=%s",
				s);
			free(s);
		}
		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 -1;

	conf->dc = dc;
	conf->global_options = yaboot_global_options,
	conf->conf_files = yaboot_conf_files,
	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, 99, yaboot_parse);
OpenPOWER on IntegriCloud