]> git.ozlabs.org Git - petitboot/blob - lib/pb-config/storage-powerpc-nvram.c
fa2437c586a289408c829fbaea6ce730a735eb3c
[petitboot] / lib / pb-config / storage-powerpc-nvram.c
1
2 #include <string.h>
3 #include <stdlib.h>
4 #include <limits.h>
5 #include <sys/types.h>
6 #include <sys/wait.h>
7
8 #include <talloc/talloc.h>
9 #include <list/list.h>
10 #include <log/log.h>
11 #include <process/process.h>
12
13 #include "pb-config.h"
14 #include "storage.h"
15
16 static const char *partition = "common";
17
18 struct param {
19         char                    *name;
20         char                    *value;
21         bool                    modified;
22         struct list_item        list;
23 };
24
25 struct powerpc_nvram_storage {
26         struct config_storage   storage;
27         struct list             params;
28 };
29
30 static const char *known_params[] = {
31         "auto-boot?",
32         "petitboot,network",
33         "petitboot,timeout",
34         NULL,
35 };
36
37 #define to_powerpc_nvram_storage(s) \
38         container_of(s, struct powerpc_nvram_storage, storage)
39
40 /* a partition max a max size of 64k * 16bytes = 1M */
41 static const int max_partition_size = 64 * 1024 * 16;
42
43 static bool param_is_known(const char *param, unsigned int len)
44 {
45         const char *known_param;
46         unsigned int i;
47
48         for (i = 0; known_params[i]; i++) {
49                 known_param = known_params[i];
50                 if (len == strlen(known_param) &&
51                                 !strncmp(param, known_param, len))
52                         return true;
53         }
54
55         return false;
56 }
57
58 static int parse_nvram_params(struct powerpc_nvram_storage *nv,
59                 char *buf, int len)
60 {
61         char *pos, *name, *value;
62         unsigned int paramlen;
63         int i, count;
64
65         /* discard 2 header lines:
66          * "common" partiton"
67          * ------------------
68          */
69         pos = buf;
70         count = 0;
71
72         for (i = 0; i < len; i++) {
73                 if (pos[i] == '\n')
74                         count++;
75                 if (count == 2)
76                         break;
77         }
78
79         if (i == len) {
80                 fprintf(stderr, "failure parsing nvram output\n");
81                 return -1;
82         }
83
84         for (pos = buf + i; pos < buf + len; pos += paramlen + 1) {
85                 unsigned int namelen;
86                 struct param *param;
87                 char *newline;
88
89                 newline = strchr(pos, '\n');
90                 if (!newline)
91                         break;
92
93                 *newline = '\0';
94
95                 paramlen = strlen(pos);
96
97                 name = pos;
98                 value = strchr(pos, '=');
99                 if (!value)
100                         continue;
101
102                 namelen = value - name;
103                 if (namelen == 0)
104                         continue;
105
106                 if (!param_is_known(name, namelen))
107                         continue;
108
109                 value++;
110
111                 param = talloc(nv, struct param);
112                 param->modified = false;
113                 param->name = talloc_strndup(nv, name, namelen);
114                 param->value = talloc_strdup(nv, value);
115                 list_add(&nv->params, &param->list);
116         }
117
118         return 0;
119 }
120
121 static int parse_nvram(struct powerpc_nvram_storage *nv)
122 {
123         struct process *process;
124         const char *argv[5];
125         int rc;
126
127         argv[0] = "nvram";
128         argv[1] = "--print-config";
129         argv[2] = "--partition";
130         argv[3] = partition;
131         argv[4] = NULL;
132
133         process = process_create(nv);
134         process->path = "nvram";
135         process->argv = argv;
136         process->keep_stdout = true;
137
138         rc = process_run_sync(process);
139
140         if (rc || !WIFEXITED(process->exit_status)
141                         || WEXITSTATUS(process->exit_status)) {
142                 fprintf(stderr, "nvram process returned "
143                                 "non-zero exit status\n");
144                 rc = -1;
145         } else {
146                 rc = parse_nvram_params(nv, process->stdout_buf,
147                                             process->stdout_len);
148         }
149
150         process_release(process);
151         return rc;
152 }
153
154 static const char *get_param(struct powerpc_nvram_storage *nv,
155                 const char *name)
156 {
157         struct param *param;
158
159         list_for_each_entry(&nv->params, param, list)
160                 if (!strcmp(param->name, name))
161                         return param->value;
162         return NULL;
163 }
164
165 static int parse_hwaddr(struct interface_config *ifconf, char *str)
166 {
167         int i;
168
169         if (strlen(str) != strlen("00:00:00:00:00:00"))
170                 return -1;
171
172         for (i = 0; i < HWADDR_SIZE; i++) {
173                 char byte[3], *endp;
174                 unsigned long x;
175
176                 byte[0] = str[i * 3 + 0];
177                 byte[1] = str[i * 3 + 1];
178                 byte[2] = '\0';
179
180                 x = strtoul(byte, &endp, 16);
181                 if (endp != byte + 2)
182                         return -1;
183
184                 ifconf->hwaddr[i] = x & 0xff;
185         }
186
187         return 0;
188 }
189
190 static int parse_one_interface_config(struct config *config,
191                 char *confstr)
192 {
193         struct interface_config *ifconf;
194         char *tok, *saveptr;
195
196         ifconf = talloc(config, struct interface_config);
197
198         if (!confstr || !strlen(confstr))
199                 goto out_err;
200
201         /* first token should be the mac address */
202         tok = strtok_r(confstr, ",", &saveptr);
203         if (!tok)
204                 goto out_err;
205
206         if (parse_hwaddr(ifconf, tok))
207                 goto out_err;
208
209         /* second token is the method */
210         tok = strtok_r(NULL, ",", &saveptr);
211         if (!tok || !strlen(tok) || !strcmp(tok, "ignore")) {
212                 ifconf->ignore = true;
213
214         } else if (!strcmp(tok, "dhcp")) {
215                 ifconf->method = CONFIG_METHOD_DHCP;
216
217         } else if (!strcmp(tok, "static")) {
218                 ifconf->method = CONFIG_METHOD_STATIC;
219
220                 /* ip/mask, [optional] gateway */
221                 tok = strtok_r(NULL, ",", &saveptr);
222                 if (!tok)
223                         goto out_err;
224                 ifconf->static_config.address =
225                         talloc_strdup(ifconf, tok);
226
227                 tok = strtok_r(NULL, ",", &saveptr);
228                 if (tok) {
229                         ifconf->static_config.gateway =
230                                 talloc_strdup(ifconf, tok);
231                 }
232
233         } else {
234                 pb_log("Unknown network configuration method %s\n", tok);
235                 goto out_err;
236         }
237
238         config->network.interfaces = talloc_realloc(config,
239                         config->network.interfaces,
240                         struct interface_config *,
241                         ++config->network.n_interfaces);
242
243         config->network.interfaces[config->network.n_interfaces - 1] = ifconf;
244
245         return 0;
246 out_err:
247         talloc_free(ifconf);
248         return -1;
249 }
250
251 static int parse_one_dns_config(struct config *config,
252                 char *confstr)
253 {
254         char *tok, *saveptr;
255
256         for (tok = strtok_r(confstr, ",", &saveptr); tok;
257                         tok = strtok_r(NULL, ",", &saveptr)) {
258
259                 char *server = talloc_strdup(config, tok);
260
261                 config->network.dns_servers = talloc_realloc(config,
262                                 config->network.dns_servers, const char *,
263                                 ++config->network.n_dns_servers);
264
265                 config->network.dns_servers[config->network.n_dns_servers - 1]
266                                 = server;
267         }
268
269         return 0;
270 }
271
272 static void populate_network_config(struct powerpc_nvram_storage *nv,
273                 struct config *config)
274 {
275         const char *cval;
276         char *val;
277         int i;
278
279         cval = get_param(nv, "petitboot,network");
280         if (!cval || !strlen(cval))
281                 return;
282
283         val = talloc_strdup(config, cval);
284
285         for (i = 0; ; i++) {
286                 char *tok, *saveptr;
287
288                 tok = strtok_r(i == 0 ? val : NULL, " ", &saveptr);
289                 if (!tok)
290                         break;
291
292                 if (!strncasecmp(tok, "dns,", strlen("dns,")))
293                         parse_one_dns_config(config, tok + strlen("dns,"));
294                 else
295                         parse_one_interface_config(config, tok);
296
297         }
298
299         talloc_free(val);
300 }
301
302 static void populate_config(struct powerpc_nvram_storage *nv,
303                 struct config *config)
304 {
305         const char *val;
306         char *end;
307         unsigned long timeout;
308
309         /* if the "auto-boot?' property is present and "false", disable auto
310          * boot */
311         val = get_param(nv, "auto-boot?");
312         config->autoboot_enabled = !val || strcmp(val, "false");
313
314         val = get_param(nv, "petitboot,timeout");
315         if (val) {
316                 timeout = strtoul(val, &end, 10);
317                 if (end != val) {
318                         if (timeout >= INT_MAX)
319                                 timeout = INT_MAX;
320                         config->autoboot_timeout_sec = (int)timeout;
321                 }
322         }
323
324         populate_network_config(nv, config);
325 }
326
327 static int load(struct config_storage *st, struct config *config)
328 {
329         struct powerpc_nvram_storage *nv = to_powerpc_nvram_storage(st);
330         int rc;
331
332         rc = parse_nvram(nv);
333         if (rc)
334                 return rc;
335
336         populate_config(nv, config);
337
338         return 0;
339 }
340
341 struct config_storage *create_powerpc_nvram_storage(void *ctx)
342 {
343         struct powerpc_nvram_storage *nv;
344
345         nv = talloc(ctx, struct powerpc_nvram_storage);
346         nv->storage.load = load;
347         list_init(&nv->params);
348
349         return &nv->storage;
350 }