7 #include <talloc/talloc.h>
11 #include "pb-config.h"
14 static const char *partition = "common";
20 struct list_item list;
23 struct powerpc_nvram_storage {
24 struct config_storage storage;
28 static const char *known_params[] = {
34 #define to_powerpc_nvram_storage(s) \
35 container_of(s, struct powerpc_nvram_storage, storage)
37 /* a partition max a max size of 64k * 16bytes = 1M */
38 static const int max_partition_size = 64 * 1024 * 16;
40 static bool param_is_known(const char *param, unsigned int len)
42 const char *known_param;
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))
55 static int parse_nvram_params(struct powerpc_nvram_storage *nv,
58 char *pos, *name, *value;
59 unsigned int paramlen;
62 /* discard 2 header lines:
69 for (i = 0; i < len; i++) {
77 fprintf(stderr, "failure parsing nvram output\n");
81 for (pos = buf + i; pos < buf + len; pos += paramlen) {
85 paramlen = strlen(pos);
88 value = strchr(pos, '=');
92 namelen = name - value;
94 if (!param_is_known(name, namelen))
99 param = talloc(nv, struct param);
100 param->modified = false;
101 param->name = talloc_strndup(nv, name, namelen);
102 param->value = talloc_strdup(nv, value);
103 list_add(&nv->params, ¶m->list);
109 static int parse_nvram(struct powerpc_nvram_storage *nv)
111 int rc, len, buf_len;
112 int pipefds[2], status;
132 dup2(pipefds[1], STDOUT_FILENO);
133 execlp("nvram", "nvram", "--print-config",
134 "--partition", partition, NULL);
141 buf_len = max_partition_size;
142 buf = talloc_array(nv, char, buf_len);
145 rc = read(pipefds[0], buf + len, buf_len - len);
158 waitpid(pid, &status, 0);
159 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
160 fprintf(stderr, "nvram process returned "
161 "non-zero exit status\n");
168 return parse_nvram_params(nv, buf, len);
171 static const char *get_param(struct powerpc_nvram_storage *nv,
176 list_for_each_entry(&nv->params, param, list)
177 if (!strcmp(param->name, name))
182 static int parse_hwaddr(struct network_config *config, char *str)
186 if (strlen(str) != strlen("00:00:00:00:00:00"))
189 for (i = 0; i < HWADDR_SIZE; i++) {
193 byte[0] = str[i * 3 + 0];
194 byte[1] = str[i * 3 + 1];
197 x = strtoul(byte, &endp, 16);
198 if (endp != byte + 2)
201 config->hwaddr[i] = x & 0xff;
207 static int parse_one_network_config(struct network_config *config,
212 if (!confstr || !strlen(confstr))
215 /* first token should be the mac address */
216 tok = strtok_r(confstr, ",", &saveptr);
220 if (parse_hwaddr(config, tok))
223 /* second token is the method */
224 tok = strtok_r(NULL, ",", &saveptr);
225 if (!tok || !strlen(tok) || !strcmp(tok, "ignore")) {
226 config->ignore = true;
230 if (!strcmp(tok, "dhcp")) {
231 config->method = CONFIG_METHOD_DHCP;
233 } else if (!strcmp(tok, "static")) {
234 config->method = CONFIG_METHOD_STATIC;
236 /* ip/mask, [optional] gateway, [optional] dns */
237 tok = strtok_r(NULL, ",", &saveptr);
240 config->static_config.address =
241 talloc_strdup(config, tok);
243 tok = strtok_r(NULL, ",", &saveptr);
245 config->static_config.gateway =
246 talloc_strdup(config, tok);
247 tok = strtok_r(NULL, ",", &saveptr);
251 config->static_config.dns =
252 talloc_strdup(config, tok);
255 pb_log("Unknown network configuration method %s\n", tok);
262 static void populate_network_config(struct powerpc_nvram_storage *nv,
263 struct config *config)
269 cval = get_param(nv, "petitboot,network");
270 if (!cval || !strlen(cval))
273 val = talloc_strdup(config, cval);
276 struct network_config *netconf;
280 tok = strtok_r(i == 0 ? val : NULL, " ", &saveptr);
284 netconf = talloc(nv, struct network_config);
286 rc = parse_one_network_config(netconf, tok);
288 talloc_free(netconf);
292 config->network_configs = talloc_realloc(nv,
293 config->network_configs,
294 struct network_config *,
295 ++config->n_network_configs);
297 config->network_configs[config->n_network_configs - 1] =
304 static void populate_config(struct powerpc_nvram_storage *nv,
305 struct config *config)
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 populate_network_config(nv, config);
317 static int load(struct config_storage *st, struct config *config)
319 struct powerpc_nvram_storage *nv = to_powerpc_nvram_storage(st);
322 rc = parse_nvram(nv);
326 populate_config(nv, config);
331 struct config_storage *create_powerpc_nvram_storage(void *ctx)
333 struct powerpc_nvram_storage *nv;
335 nv = talloc(ctx, struct powerpc_nvram_storage);
336 nv->storage.load = load;
337 list_init(&nv->params);