8 #include <talloc/talloc.h>
11 #include <process/process.h>
13 #include "pb-config.h"
16 static const char *partition = "common";
22 struct list_item list;
25 struct powerpc_nvram_storage {
26 struct config_storage storage;
30 static const char *known_params[] = {
37 #define to_powerpc_nvram_storage(s) \
38 container_of(s, struct powerpc_nvram_storage, storage)
40 /* a partition max a max size of 64k * 16bytes = 1M */
41 static const int max_partition_size = 64 * 1024 * 16;
43 static bool param_is_known(const char *param, unsigned int len)
45 const char *known_param;
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))
58 static int parse_nvram_params(struct powerpc_nvram_storage *nv,
61 char *pos, *name, *value;
62 unsigned int paramlen;
65 /* discard 2 header lines:
72 for (i = 0; i < len; i++) {
80 fprintf(stderr, "failure parsing nvram output\n");
84 for (pos = buf + i; pos < buf + len; pos += paramlen + 1) {
89 newline = strchr(pos, '\n');
95 paramlen = strlen(pos);
98 value = strchr(pos, '=');
102 namelen = value - name;
106 if (!param_is_known(name, namelen))
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, ¶m->list);
121 static int parse_nvram(struct powerpc_nvram_storage *nv)
123 struct process *process;
128 argv[1] = "--print-config";
129 argv[2] = "--partition";
133 process = process_create(nv);
134 process->path = "nvram";
135 process->argv = argv;
136 process->keep_stdout = true;
138 rc = process_run_sync(process);
140 if (rc || !process_exit_ok(process)) {
141 fprintf(stderr, "nvram process returned "
142 "non-zero exit status\n");
145 rc = parse_nvram_params(nv, process->stdout_buf,
146 process->stdout_len);
149 process_release(process);
153 static int write_nvram(struct powerpc_nvram_storage *nv)
155 struct process *process;
161 argv[1] = "--update-config";
163 argv[3] = "--partition";
167 process = process_create(nv);
168 process->path = "nvram";
169 process->argv = argv;
171 list_for_each_entry(&nv->params, param, list) {
174 if (!param->modified)
177 paramstr = talloc_asprintf(nv, "%s=%s",
178 param->name, param->value);
181 rc = process_run_sync(process);
183 talloc_free(paramstr);
185 if (rc || !process_exit_ok(process)) {
187 pb_log("nvram update process returned "
188 "non-zero exit status\n");
193 process_release(process);
197 static const char *get_param(struct powerpc_nvram_storage *nv,
202 list_for_each_entry(&nv->params, param, list)
203 if (!strcmp(param->name, name))
208 static void set_param(struct powerpc_nvram_storage *nv, const char *name,
213 list_for_each_entry(&nv->params, param, list) {
214 if (strcmp(param->name, name))
217 if (!strcmp(param->value, value))
220 talloc_free(param->value);
221 param->value = talloc_strdup(param, value);
222 param->modified = true;
227 param = talloc(nv, struct param);
228 param->modified = true;
229 param->name = talloc_strdup(nv, name);
230 param->value = talloc_strdup(nv, value);
231 list_add(&nv->params, ¶m->list);
234 static int parse_hwaddr(struct interface_config *ifconf, char *str)
238 if (strlen(str) != strlen("00:00:00:00:00:00"))
241 for (i = 0; i < HWADDR_SIZE; i++) {
245 byte[0] = str[i * 3 + 0];
246 byte[1] = str[i * 3 + 1];
249 x = strtoul(byte, &endp, 16);
250 if (endp != byte + 2)
253 ifconf->hwaddr[i] = x & 0xff;
259 static int parse_one_interface_config(struct config *config,
262 struct interface_config *ifconf;
265 ifconf = talloc_zero(config, struct interface_config);
267 if (!confstr || !strlen(confstr))
270 /* first token should be the mac address */
271 tok = strtok_r(confstr, ",", &saveptr);
275 if (parse_hwaddr(ifconf, tok))
278 /* second token is the method */
279 tok = strtok_r(NULL, ",", &saveptr);
280 if (!tok || !strlen(tok) || !strcmp(tok, "ignore")) {
281 ifconf->ignore = true;
283 } else if (!strcmp(tok, "dhcp")) {
284 ifconf->method = CONFIG_METHOD_DHCP;
286 } else if (!strcmp(tok, "static")) {
287 ifconf->method = CONFIG_METHOD_STATIC;
289 /* ip/mask, [optional] gateway */
290 tok = strtok_r(NULL, ",", &saveptr);
293 ifconf->static_config.address =
294 talloc_strdup(ifconf, tok);
296 tok = strtok_r(NULL, ",", &saveptr);
298 ifconf->static_config.gateway =
299 talloc_strdup(ifconf, tok);
303 pb_log("Unknown network configuration method %s\n", tok);
307 config->network.interfaces = talloc_realloc(config,
308 config->network.interfaces,
309 struct interface_config *,
310 ++config->network.n_interfaces);
312 config->network.interfaces[config->network.n_interfaces - 1] = ifconf;
320 static int parse_one_dns_config(struct config *config,
325 for (tok = strtok_r(confstr, ",", &saveptr); tok;
326 tok = strtok_r(NULL, ",", &saveptr)) {
328 char *server = talloc_strdup(config, tok);
330 config->network.dns_servers = talloc_realloc(config,
331 config->network.dns_servers, const char *,
332 ++config->network.n_dns_servers);
334 config->network.dns_servers[config->network.n_dns_servers - 1]
341 static void populate_network_config(struct powerpc_nvram_storage *nv,
342 struct config *config)
348 cval = get_param(nv, "petitboot,network");
349 if (!cval || !strlen(cval))
352 val = talloc_strdup(config, cval);
357 tok = strtok_r(i == 0 ? val : NULL, " ", &saveptr);
361 if (!strncasecmp(tok, "dns,", strlen("dns,")))
362 parse_one_dns_config(config, tok + strlen("dns,"));
364 parse_one_interface_config(config, tok);
371 static void populate_config(struct powerpc_nvram_storage *nv,
372 struct config *config)
376 unsigned long timeout;
378 /* if the "auto-boot?' property is present and "false", disable auto
380 val = get_param(nv, "auto-boot?");
381 config->autoboot_enabled = !val || strcmp(val, "false");
383 val = get_param(nv, "petitboot,timeout");
385 timeout = strtoul(val, &end, 10);
387 if (timeout >= INT_MAX)
389 config->autoboot_timeout_sec = (int)timeout;
393 populate_network_config(nv, config);
396 static char *iface_config_str(void *ctx, struct interface_config *config)
400 /* todo: HWADDR size is hardcoded as 6, but we may need to handle
401 * different hardware address formats */
402 str = talloc_asprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x,",
403 config->hwaddr[0], config->hwaddr[1],
404 config->hwaddr[2], config->hwaddr[3],
405 config->hwaddr[4], config->hwaddr[5]);
407 if (config->ignore) {
408 str = talloc_asprintf_append(str, "ignore");
410 } else if (config->method == CONFIG_METHOD_DHCP) {
411 str = talloc_asprintf_append(str, "dhcp");
413 } else if (config->method == CONFIG_METHOD_STATIC) {
414 str = talloc_asprintf_append(str, "static,%s%s%s",
415 config->static_config.address,
416 config->static_config.gateway ? "," : "",
417 config->static_config.gateway ?: "");
422 static char *dns_config_str(void *ctx, const char **dns_servers, int n)
427 str = talloc_strdup(ctx, "dns,");
428 for (i = 0; i < n; i++) {
429 str = talloc_asprintf_append(str, "%s%s",
437 static void update_network_config(struct powerpc_nvram_storage *nv,
438 struct config *config)
443 val = talloc_strdup(nv, "");
445 for (i = 0; i < config->network.n_interfaces; i++) {
446 char *iface_str = iface_config_str(nv,
447 config->network.interfaces[i]);
448 val = talloc_asprintf_append(val, "%s%s",
449 *val == '\0' ? "" : " ", iface_str);
450 talloc_free(iface_str);
453 if (config->network.n_dns_servers) {
454 char *dns_str = dns_config_str(nv, config->network.dns_servers,
455 config->network.n_dns_servers);
456 val = talloc_asprintf_append(val, "%s%s",
457 *val == '\0' ? "" : " ", dns_str);
458 talloc_free(dns_str);
461 set_param(nv, "petitboot,network", val);
466 static int update_config(struct powerpc_nvram_storage *nv,
467 struct config *config, struct config *defaults)
471 if (config->autoboot_enabled != defaults->autoboot_enabled) {
472 val = config->autoboot_enabled ? "true" : "false";
473 set_param(nv, "auto-boot?", val);
476 if (config->autoboot_timeout_sec != defaults->autoboot_timeout_sec) {
477 val = talloc_asprintf(nv, "%d", config->autoboot_timeout_sec);
478 set_param(nv, "petitboot,timeout", val);
482 update_network_config(nv, config);
484 return write_nvram(nv);
487 static int load(struct config_storage *st, struct config *config)
489 struct powerpc_nvram_storage *nv = to_powerpc_nvram_storage(st);
492 rc = parse_nvram(nv);
496 populate_config(nv, config);
501 static int save(struct config_storage *st, struct config *config)
503 struct powerpc_nvram_storage *nv = to_powerpc_nvram_storage(st);
504 struct config *defaults;
507 defaults = talloc_zero(nv, struct config);
508 config_set_defaults(defaults);
510 rc = update_config(nv, config, defaults);
512 talloc_free(defaults);
516 struct config_storage *create_powerpc_nvram_storage(void *ctx)
518 struct powerpc_nvram_storage *nv;
520 nv = talloc(ctx, struct powerpc_nvram_storage);
521 nv->storage.load = load;
522 nv->storage.save = save;
523 list_init(&nv->params);