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