lib/efi: Cleanup read/write routines
[petitboot] / discover / native-parser.c
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; version 2 of the License.
5  *
6  *  This program is distributed in the hope that it will be useful,
7  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
8  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9  *  GNU General Public License for more details.
10  *
11  *  You should have received a copy of the GNU General Public License
12  *  along with this program; if not, write to the Free Software
13  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
14  */
15
16 #include "parser.h"
17 #include "params.h"
18 #include "paths.h"
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23
24 static const char *conf_filename = "/boot/petitboot.conf";
25
26 static struct boot_option *cur_opt;
27 static struct device *dev;
28 static const char *devpath;
29 static int device_added;
30
31 static int check_and_add_device(struct device *dev)
32 {
33         if (!dev->icon_file)
34                 dev->icon_file = strdup(generic_icon_file(guess_device_type()));
35
36         return !add_device(dev);
37 }
38
39 static int section(char *section_name)
40 {
41         if (!device_added++ && !check_and_add_device(dev))
42                 return 0;
43
44         if (cur_opt) {
45                 add_boot_option(cur_opt);
46                 free_boot_option(cur_opt);
47         }
48
49         cur_opt = malloc(sizeof(*cur_opt));
50         memset(cur_opt, 0, sizeof(*cur_opt));
51         return 1;
52 }
53
54
55 static void set_boot_option_parameter(struct boot_option *opt,
56                 const char *name, const char *value)
57 {
58         if (streq(name, "name"))
59                 opt->name = strdup(value);
60
61         else if (streq(name, "description"))
62                 opt->description = strdup(value);
63
64         else if (streq(name, "image"))
65                 opt->boot_image_file = resolve_path(value, devpath);
66
67         else if (streq(name, "icon"))
68                 opt->icon_file = resolve_path(value, devpath);
69
70         else if (streq(name, "initrd"))
71                 opt->initrd_file =resolve_path(value, devpath);
72
73         else if (streq(name, "args"))
74                 opt->boot_args = strdup(value);
75
76         else
77                 fprintf(stderr, "Unknown parameter %s\n", name);
78 }
79
80 static void set_device_parameter(struct device *dev,
81                 const char *name, const char *value)
82 {
83         if (streq(name, "name"))
84                 dev->name = strdup(value);
85
86         else if (streq(name, "description"))
87                 dev->description = strdup(value);
88
89         else if (streq(name, "icon"))
90                 dev->icon_file = resolve_path(value, devpath);
91 }
92
93 static int parameter(char *param_name, char *param_value)
94 {
95         if (cur_opt)
96                 set_boot_option_parameter(cur_opt, param_name, param_value);
97         else
98                 set_device_parameter(dev, param_name, param_value);
99         return 1;
100 }
101
102
103 static int native_parse(const char *device)
104 {
105         char *filepath;
106         int rc;
107
108         filepath = resolve_path(conf_filename, device);
109
110         cur_opt = NULL;
111         dev = malloc(sizeof(*dev));
112         memset(dev, 0, sizeof(*dev));
113         dev->id = strdup(device);
114
115         rc = pm_process(filepath, section, parameter);
116         if (!rc)
117                 return 0;
118
119         if (cur_opt) {
120                 add_boot_option(cur_opt);
121                 free_boot_option(cur_opt);
122         }
123
124         cur_opt = NULL;
125
126         free(filepath);
127
128         return 1;
129 }
130
131 define_parser(native, native_parse);