summaryrefslogtreecommitdiffstats
path: root/discover/yaboot-parser.c
blob: 27b4b782306594c05cbaec0fd3b01bc826a409de (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

#include "parser.h"
#include "params.h"
#include "paths.h"
#include "yaboot-cfg.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/param.h>

static struct device *dev;
static char *devpath;
static char *defimage;

char *
make_params(char *label, char *params)
{
     char *p, *q;
     static char buffer[2048];

     q = buffer;
     *q = 0;

     p = cfg_get_strg(label, "literal");
     if (p) {
          strcpy(q, p);
          q = strchr(q, 0);
          if (params) {
               if (*p)
                    *q++ = ' ';
               strcpy(q, params);
          }
          return buffer;
     }

     p = cfg_get_strg(label, "root");
     if (p) {
          strcpy (q, "root=");
          strcpy (q + 5, p);
          q = strchr (q, 0);
          *q++ = ' ';
     }
     if (cfg_get_flag(label, "read-only")) {
          strcpy (q, "ro ");
          q += 3;
     }
     if (cfg_get_flag(label, "read-write")) {
          strcpy (q, "rw ");
          q += 3;
     }
     p = cfg_get_strg(label, "ramdisk");
     if (p) {
          strcpy (q, "ramdisk=");
          strcpy (q + 8, p);
          q = strchr (q, 0);
          *q++ = ' ';
     }
     p = cfg_get_strg(label, "initrd-size");
     if (p) {
          strcpy (q, "ramdisk_size=");
          strcpy (q + 13, p);
          q = strchr (q, 0);
          *q++ = ' ';
     }
     if (cfg_get_flag(label, "novideo")) {
          strcpy (q, "video=ofonly");
          q = strchr (q, 0);
          *q++ = ' ';
     }
     p = cfg_get_strg (label, "append");
     if (p) {
          strcpy (q, p);
          q = strchr (q, 0);
          *q++ = ' ';
     }
     *q = 0;
     if (params)
          strcpy(q, params);

     return buffer;
}

static int check_and_add_device(struct device *dev)
{
	if (!dev->icon_file)
		dev->icon_file = strdup(generic_icon_file(guess_device_type()));

	return !add_device(dev);
}

void process_image(char *label)
{
	struct boot_option opt;
	char *cfgopt;

	memset(&opt, 0, sizeof(opt));

	opt.name = label;
	cfgopt = cfg_get_strg(label, "image");
	opt.boot_image_file = resolve_path(cfgopt, devpath);
	if (cfgopt == defimage)
		pb_log("This one is default. What do we do about it?\n");

	cfgopt = cfg_get_strg(label, "initrd");
	if (cfgopt)
		opt.initrd_file = resolve_path(cfgopt, devpath);

	opt.boot_args = make_params(label, NULL);

	add_boot_option(&opt);

	if (opt.initrd_file)
		free(opt.initrd_file);
}

static int yaboot_parse(const char *device)
{
	char *filepath;
	char *conf_file;
	char *tmpstr;
	ssize_t conf_len;
	int fd;
	struct stat st;
	char *label;

	devpath = strdup(device);

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

	fd = open(filepath, O_RDONLY);
	if (fd < 0) {
		free(filepath);
		filepath = resolve_path("/yaboot.conf", devpath);
		fd = open(filepath, O_RDONLY);
		
		if (fd < 0)
			return 0;
	}

	if (fstat(fd, &st)) {
		close(fd);
		return 0;
	}

	conf_file = malloc(st.st_size+1);
	if (!conf_file) {
		close(fd);
		return 0;
	}
	
	conf_len = read(fd, conf_file, st.st_size);
	if (conf_len < 0) {
		close(fd);
		return 0;
	}
	conf_file[conf_len] = 0;

	close(fd);
	
	if (cfg_parse(filepath, conf_file, conf_len)) {
		pb_log("Error parsing yaboot.conf\n");
		return 0;
	}

	free(filepath);

	dev = malloc(sizeof(*dev));
	memset(dev, 0, sizeof(*dev));
	dev->id = strdup(devpath);
	if (cfg_get_strg(0, "init-message")) {
		char *newline;
		dev->description = strdup(cfg_get_strg(0, "init-message"));
		newline = strchr(dev->description, '\n');
		if (newline)
			*newline = 0;
	}
	dev->icon_file = strdup(generic_icon_file(guess_device_type()));

	/* If we have a 'partiton=' directive, update the default devpath
	 * to use that instead of the current device */
	tmpstr = cfg_get_strg(0, "partition");
	if (tmpstr) {
		char *endp;
		int partnr = strtol(tmpstr, &endp, 10);
		if (endp != tmpstr && !*endp) {
			char *new_dev, *tmp;

			new_dev = malloc(strlen(devpath) + strlen(tmpstr) + 1);
			if (!new_dev)
				return 0;

			strcpy(new_dev, devpath);

			/* Strip digits (partition number) from string */
			endp = new_dev + strlen(devpath) - 1;
			while (isdigit(*endp))
				*(endp--) = 0;

			/* and add our own... */
			sprintf(endp + 1, "%d", partnr);

			tmp = devpath;
			devpath = parse_device_path(new_dev, devpath);
			free(tmp);
			free(new_dev);
		}
	}

	defimage = cfg_get_default();
	if (!defimage)
		return 0;
	defimage = cfg_get_strg(defimage, "image");

	label = cfg_next_image(NULL);
	if (!label || !check_and_add_device(dev))
		return 0;

	do {
		process_image(label);
	} while ((label = cfg_next_image(label)));

	return 1;
}

struct parser yaboot_parser = {
	.name = "yaboot.conf parser",
	.priority = 99,
	.parse	  = yaboot_parse
};
OpenPOWER on IntegriCloud