2 #include "udev-helper.h"
4 #include "yaboot-cfg.h"
14 #include <sys/param.h>
16 static struct device *dev;
17 static const char *mountpoint;
18 static char partition_mntpoint[PATH_MAX];
19 static char *defimage;
22 make_params(char *label, char *params)
25 static char buffer[2048];
30 p = cfg_get_strg(label, "literal");
42 p = cfg_get_strg(label, "root");
49 if (cfg_get_flag(label, "read-only")) {
53 if (cfg_get_flag(label, "read-write")) {
57 p = cfg_get_strg(label, "ramdisk");
59 strcpy (q, "ramdisk=");
64 p = cfg_get_strg(label, "initrd-size");
66 strcpy (q, "ramdisk_size=");
71 if (cfg_get_flag(label, "novideo")) {
72 strcpy (q, "video=ofonly");
76 p = cfg_get_strg (label, "append");
89 static char *prepend_mountpoint(const char *path)
93 full_path = malloc(strlen(path) + strlen(mountpoint) + 2);
95 strcpy(full_path, mountpoint);
97 strcat(full_path, "/");
98 strcat(full_path, path);
103 static int check_and_add_device(struct device *dev)
106 dev->icon_file = strdup(generic_icon_file(guess_device_type()));
108 return !add_device(dev);
111 void process_image(char *label)
113 struct boot_option opt;
116 memset(&opt, 0, sizeof(opt));
119 cfgopt = cfg_get_strg(label, "image");
120 opt.boot_image_file = prepend_mountpoint(cfgopt);
121 if (cfgopt == defimage)
122 printf("This one is default. What do we do about it?\n");
124 cfgopt = cfg_get_strg(label, "initrd");
126 opt.initrd_file = prepend_mountpoint(cfgopt);
128 opt.boot_args = make_params(label, NULL);
130 add_boot_option(&opt);
133 free(opt.initrd_file);
136 static int yaboot_parse(const char *devicepath, const char *_mountpoint)
146 mountpoint = _mountpoint;
148 filepath = prepend_mountpoint("/etc/yaboot.conf");
150 fd = open(filepath, O_RDONLY);
153 filepath = prepend_mountpoint("/yaboot.conf");
154 fd = open(filepath, O_RDONLY);
160 if (fstat(fd, &st)) {
165 conf_file = malloc(st.st_size+1);
171 conf_len = read(fd, conf_file, st.st_size);
176 conf_file[conf_len] = 0;
180 if (cfg_parse(filepath, conf_file, conf_len)) {
181 printf("Error parsing yaboot.conf\n");
187 dev = malloc(sizeof(*dev));
188 memset(dev, 0, sizeof(*dev));
189 dev->id = strdup(devicepath);
190 if (cfg_get_strg(0, "init-message")) {
192 dev->description = strdup(cfg_get_strg(0, "init-message"));
193 newline = strchr(dev->description, '\n');
197 dev->icon_file = strdup(generic_icon_file(guess_device_type()));
199 /* Mount the 'partition' which is what all the image filenames
201 tmpstr = cfg_get_strg(0, "partition");
204 int partnr = strtol(tmpstr, &endp, 10);
205 if (endp != tmpstr && !*endp) {
206 char *new_dev = malloc(strlen(devicepath) + strlen(tmpstr) + 1);
210 strcpy(new_dev, devicepath);
212 /* Strip digits (partition number) from string */
213 endp = &new_dev[strlen(devicepath) - 1];
214 while (isdigit(*endp))
217 /* and add our own... */
218 sprintf(endp+1, "%d", partnr);
220 /* FIXME: udev may not have created the device node
221 yet. And on removal, unmount_device() only unmounts
222 it once, while in fact it may be mounted twice. */
223 if (mount_device(new_dev, partition_mntpoint)) {
224 printf("Error mounting image partition\n");
227 mountpoint = partition_mntpoint;
232 defimage = cfg_get_default();
235 defimage = cfg_get_strg(defimage, "image");
237 label = cfg_next_image(NULL);
238 if (!label || !check_and_add_device(dev))
242 process_image(label);
243 } while ((label = cfg_next_image(label)));
248 struct parser yaboot_parser = {
249 .name = "yaboot.conf parser",
251 .parse = yaboot_parse