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 || !WIFEXITED(process->exit_status)
141 || WEXITSTATUS(process->exit_status)) {
142 fprintf(stderr, "nvram process returned "
143 "non-zero exit status\n");
146 rc = parse_nvram_params(nv, process->stdout_buf,
147 process->stdout_len);
150 process_release(process);
154 static const char *get_param(struct powerpc_nvram_storage *nv,
159 list_for_each_entry(&nv->params, param, list)
160 if (!strcmp(param->name, name))
165 static int parse_hwaddr(struct interface_config *ifconf, char *str)
169 if (strlen(str) != strlen("00:00:00:00:00:00"))
172 for (i = 0; i < HWADDR_SIZE; i++) {
176 byte[0] = str[i * 3 + 0];
177 byte[1] = str[i * 3 + 1];
180 x = strtoul(byte, &endp, 16);
181 if (endp != byte + 2)
184 ifconf->hwaddr[i] = x & 0xff;
190 static int parse_one_interface_config(struct config *config,
193 struct interface_config *ifconf;
196 ifconf = talloc(config, struct interface_config);
198 if (!confstr || !strlen(confstr))
201 /* first token should be the mac address */
202 tok = strtok_r(confstr, ",", &saveptr);
206 if (parse_hwaddr(ifconf, tok))
209 /* second token is the method */
210 tok = strtok_r(NULL, ",", &saveptr);
211 if (!tok || !strlen(tok) || !strcmp(tok, "ignore")) {
212 ifconf->ignore = true;
214 } else if (!strcmp(tok, "dhcp")) {
215 ifconf->method = CONFIG_METHOD_DHCP;
217 } else if (!strcmp(tok, "static")) {
218 ifconf->method = CONFIG_METHOD_STATIC;
220 /* ip/mask, [optional] gateway */
221 tok = strtok_r(NULL, ",", &saveptr);
224 ifconf->static_config.address =
225 talloc_strdup(ifconf, tok);
227 tok = strtok_r(NULL, ",", &saveptr);
229 ifconf->static_config.gateway =
230 talloc_strdup(ifconf, tok);
234 pb_log("Unknown network configuration method %s\n", tok);
238 config->network.interfaces = talloc_realloc(config,
239 config->network.interfaces,
240 struct interface_config *,
241 ++config->network.n_interfaces);
243 config->network.interfaces[config->network.n_interfaces - 1] = ifconf;
251 static int parse_one_dns_config(struct config *config,
256 for (tok = strtok_r(confstr, ",", &saveptr); tok;
257 tok = strtok_r(NULL, ",", &saveptr)) {
259 char *server = talloc_strdup(config, tok);
261 config->network.dns_servers = talloc_realloc(config,
262 config->network.dns_servers, const char *,
263 ++config->network.n_dns_servers);
265 config->network.dns_servers[config->network.n_dns_servers - 1]
272 static void populate_network_config(struct powerpc_nvram_storage *nv,
273 struct config *config)
279 cval = get_param(nv, "petitboot,network");
280 if (!cval || !strlen(cval))
283 val = talloc_strdup(config, cval);
288 tok = strtok_r(i == 0 ? val : NULL, " ", &saveptr);
292 if (!strncasecmp(tok, "dns,", strlen("dns,")))
293 parse_one_dns_config(config, tok + strlen("dns,"));
295 parse_one_interface_config(config, tok);
302 static void populate_config(struct powerpc_nvram_storage *nv,
303 struct config *config)
307 unsigned long timeout;
309 /* if the "auto-boot?' property is present and "false", disable auto
311 val = get_param(nv, "auto-boot?");
312 config->autoboot_enabled = !val || strcmp(val, "false");
314 val = get_param(nv, "petitboot,timeout");
316 timeout = strtoul(val, &end, 10);
318 if (timeout >= INT_MAX)
320 config->autoboot_timeout_sec = (int)timeout;
324 populate_network_config(nv, config);
327 static int load(struct config_storage *st, struct config *config)
329 struct powerpc_nvram_storage *nv = to_powerpc_nvram_storage(st);
332 rc = parse_nvram(nv);
336 populate_config(nv, config);
341 struct config_storage *create_powerpc_nvram_storage(void *ctx)
343 struct powerpc_nvram_storage *nv;
345 nv = talloc(ctx, struct powerpc_nvram_storage);
346 nv->storage.load = load;
347 list_init(&nv->params);