]> git.ozlabs.org Git - petitboot/blob - devices/native-parser.c
Stop on first valid config found
[petitboot] / devices / native-parser.c
1
2 #include "parser.h"
3 #include "params.h"
4
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8
9 const char *conf_filename = "/boot/petitboot.conf";
10
11 static struct boot_option *cur_opt;
12 static struct device *dev;
13 static const char *mountpoint;
14 int device_added;
15
16 int check_and_add_device(struct device *dev)
17 {
18         if (!dev->icon_file)
19                 dev->icon_file = strdup(generic_icon_file(guess_device_type()));
20
21         return !add_device(dev);
22 }
23
24 static int section(char *section_name)
25 {
26         if (!device_added++ && !check_and_add_device(dev))
27                 return 0;
28
29         if (cur_opt) {
30                 add_boot_option(cur_opt);
31                 free_boot_option(cur_opt);
32         }
33
34         cur_opt = malloc(sizeof(*cur_opt));
35         memset(cur_opt, 0, sizeof(*cur_opt));
36         return 1;
37 }
38
39
40 static void set_boot_option_parameter(struct boot_option *opt,
41                 const char *name, const char *value)
42 {
43         if (streq(name, "name"))
44                 opt->name = strdup(value);
45
46         else if (streq(name, "description"))
47                 opt->description = strdup(value);
48
49         else if (streq(name, "image"))
50                 opt->boot_image_file = join_paths(mountpoint, value);
51
52         else if (streq(name, "icon"))
53                 opt->icon_file = join_paths(mountpoint, value);
54
55         else if (streq(name, "initrd"))
56                 opt->initrd_file = join_paths(mountpoint, value);
57
58         else if (streq(name, "args"))
59                 opt->boot_args = strdup(value);
60
61         else
62                 fprintf(stderr, "Unknown parameter %s\n", name);
63 }
64
65 static void set_device_parameter(struct device *dev,
66                 const char *name, const char *value)
67 {
68         if (streq(name, "name"))
69                 dev->name = strdup(value);
70
71         else if (streq(name, "description"))
72                 dev->description = strdup(value);
73
74         else if (streq(name, "icon"))
75                 dev->icon_file = join_paths(mountpoint, value);
76 }
77
78 static int parameter(char *param_name, char *param_value)
79 {
80         if (cur_opt)
81                 set_boot_option_parameter(cur_opt, param_name, param_value);
82         else
83                 set_device_parameter(dev, param_name, param_value);
84         return 1;
85 }
86
87
88 int parse(const char *devicepath, const char *_mountpoint)
89 {
90         char *filepath;
91         int rc;
92
93         mountpoint = _mountpoint;
94
95         filepath = join_paths(mountpoint, conf_filename);
96
97         cur_opt = NULL;
98         dev = malloc(sizeof(*dev));
99         memset(dev, 0, sizeof(*dev));
100         dev->id = strdup(devicepath);
101
102         rc = pm_process(filepath, section, parameter);
103         if (!rc)
104                 return 0;
105
106         if (cur_opt) {
107                 add_boot_option(cur_opt);
108                 free_boot_option(cur_opt);
109         }
110
111         cur_opt = NULL;
112
113         free(filepath);
114
115         return 1;
116 }
117
118 struct parser native_parser = {
119         .name = "native petitboot parser",
120         .priority = 100,
121         .parse    = parse
122 };
123
124
125