12 #include <file/file.h>
13 #include <talloc/talloc.h>
14 #include <list/list.h>
16 #include <process/process.h>
21 static const char *partition = "common";
22 static const char *sysparams_dir = "/sys/firmware/opal/sysparams/";
23 static const char *devtree_dir = "/proc/device-tree/";
29 struct list_item list;
32 struct platform_powerpc {
36 static const char *known_params[] = {
46 #define to_platform_powerpc(p) \
47 (struct platform_powerpc *)(p->platform_data)
49 /* a partition max a max size of 64k * 16bytes = 1M */
50 static const int max_partition_size = 64 * 1024 * 16;
52 static bool param_is_known(const char *param, unsigned int len)
54 const char *known_param;
57 for (i = 0; known_params[i]; i++) {
58 known_param = known_params[i];
59 if (len == strlen(known_param) &&
60 !strncmp(param, known_param, len))
67 static int parse_nvram_params(struct platform_powerpc *platform,
70 char *pos, *name, *value;
71 unsigned int paramlen;
74 /* discard 2 header lines:
81 for (i = 0; i < len; i++) {
89 fprintf(stderr, "failure parsing nvram output\n");
93 for (pos = buf + i; pos < buf + len; pos += paramlen + 1) {
98 newline = strchr(pos, '\n');
104 paramlen = strlen(pos);
107 value = strchr(pos, '=');
111 namelen = value - name;
115 if (!param_is_known(name, namelen))
120 param = talloc(platform, struct param);
121 param->modified = false;
122 param->name = talloc_strndup(platform, name, namelen);
123 param->value = talloc_strdup(platform, value);
124 list_add(&platform->params, ¶m->list);
130 static int parse_nvram(struct platform_powerpc *platform)
132 struct process *process;
137 argv[1] = "--print-config";
138 argv[2] = "--partition";
142 process = process_create(platform);
143 process->path = "nvram";
144 process->argv = argv;
145 process->keep_stdout = true;
147 rc = process_run_sync(process);
149 if (rc || !process_exit_ok(process)) {
150 fprintf(stderr, "nvram process returned "
151 "non-zero exit status\n");
154 rc = parse_nvram_params(platform, process->stdout_buf,
155 process->stdout_len);
158 process_release(process);
162 static int write_nvram(struct platform_powerpc *platform)
164 struct process *process;
170 argv[1] = "--update-config";
172 argv[3] = "--partition";
176 process = process_create(platform);
177 process->path = "nvram";
178 process->argv = argv;
180 list_for_each_entry(&platform->params, param, list) {
183 if (!param->modified)
186 paramstr = talloc_asprintf(platform, "%s=%s",
187 param->name, param->value);
190 rc = process_run_sync(process);
192 talloc_free(paramstr);
194 if (rc || !process_exit_ok(process)) {
196 pb_log("nvram update process returned "
197 "non-zero exit status\n");
202 process_release(process);
206 static const char *get_param(struct platform_powerpc *platform,
211 list_for_each_entry(&platform->params, param, list)
212 if (!strcmp(param->name, name))
217 static void set_param(struct platform_powerpc *platform, const char *name,
222 list_for_each_entry(&platform->params, param, list) {
223 if (strcmp(param->name, name))
226 if (!strcmp(param->value, value))
229 talloc_free(param->value);
230 param->value = talloc_strdup(param, value);
231 param->modified = true;
236 param = talloc(platform, struct param);
237 param->modified = true;
238 param->name = talloc_strdup(platform, name);
239 param->value = talloc_strdup(platform, value);
240 list_add(&platform->params, ¶m->list);
243 static int parse_hwaddr(struct interface_config *ifconf, char *str)
247 if (strlen(str) != strlen("00:00:00:00:00:00"))
250 for (i = 0; i < HWADDR_SIZE; i++) {
254 byte[0] = str[i * 3 + 0];
255 byte[1] = str[i * 3 + 1];
258 x = strtoul(byte, &endp, 16);
259 if (endp != byte + 2)
262 ifconf->hwaddr[i] = x & 0xff;
268 static int parse_one_interface_config(struct config *config,
271 struct interface_config *ifconf;
274 ifconf = talloc_zero(config, struct interface_config);
276 if (!confstr || !strlen(confstr))
279 /* first token should be the mac address */
280 tok = strtok_r(confstr, ",", &saveptr);
284 if (parse_hwaddr(ifconf, tok))
287 /* second token is the method */
288 tok = strtok_r(NULL, ",", &saveptr);
289 if (!tok || !strlen(tok) || !strcmp(tok, "ignore")) {
290 ifconf->ignore = true;
292 } else if (!strcmp(tok, "dhcp")) {
293 ifconf->method = CONFIG_METHOD_DHCP;
295 } else if (!strcmp(tok, "static")) {
296 ifconf->method = CONFIG_METHOD_STATIC;
298 /* ip/mask, [optional] gateway */
299 tok = strtok_r(NULL, ",", &saveptr);
302 ifconf->static_config.address =
303 talloc_strdup(ifconf, tok);
305 tok = strtok_r(NULL, ",", &saveptr);
307 ifconf->static_config.gateway =
308 talloc_strdup(ifconf, tok);
312 pb_log("Unknown network configuration method %s\n", tok);
316 config->network.interfaces = talloc_realloc(config,
317 config->network.interfaces,
318 struct interface_config *,
319 ++config->network.n_interfaces);
321 config->network.interfaces[config->network.n_interfaces - 1] = ifconf;
329 static int parse_one_dns_config(struct config *config,
334 for (tok = strtok_r(confstr, ",", &saveptr); tok;
335 tok = strtok_r(NULL, ",", &saveptr)) {
337 char *server = talloc_strdup(config, tok);
339 config->network.dns_servers = talloc_realloc(config,
340 config->network.dns_servers, const char *,
341 ++config->network.n_dns_servers);
343 config->network.dns_servers[config->network.n_dns_servers - 1]
350 static void populate_network_config(struct platform_powerpc *platform,
351 struct config *config)
353 char *val, *saveptr = NULL;
357 cval = get_param(platform, "petitboot,network");
358 if (!cval || !strlen(cval))
361 val = talloc_strdup(config, cval);
366 tok = strtok_r(i == 0 ? val : NULL, " ", &saveptr);
370 if (!strncasecmp(tok, "dns,", strlen("dns,")))
371 parse_one_dns_config(config, tok + strlen("dns,"));
373 parse_one_interface_config(config, tok);
380 static void populate_bootdev_config(struct platform_powerpc *platform,
381 struct config *config)
386 config->boot_device = NULL;
388 val = get_param(platform, "petitboot,bootdev");
389 if (!val || !strlen(val))
392 if (!strncmp(val, "uuid:", strlen("uuid:"))) {
393 config->boot_device = talloc_strdup(config,
394 val + strlen("uuid:"));
396 } else if (!strncmp(val, "mac:", strlen("mac:"))) {
397 config->boot_device = talloc_strdup(config,
398 val + strlen("mac:"));
401 pb_log("bootdev config is in an unknown format "
402 "(expected uuid:... or mac:...)");
406 static void populate_config(struct platform_powerpc *platform,
407 struct config *config)
411 unsigned long timeout;
413 /* if the "auto-boot?' property is present and "false", disable auto
415 val = get_param(platform, "auto-boot?");
416 config->autoboot_enabled = !val || strcmp(val, "false");
418 val = get_param(platform, "petitboot,timeout");
420 timeout = strtoul(val, &end, 10);
422 if (timeout >= INT_MAX)
424 config->autoboot_timeout_sec = (int)timeout;
428 val = get_param(platform, "petitboot,language");
429 config->lang = val ? talloc_strdup(config, val) : NULL;
431 populate_network_config(platform, config);
433 populate_bootdev_config(platform, config);
435 if (!config->debug) {
436 val = get_param(platform, "petitboot,debug?");
437 config->debug = val && !strcmp(val, "true");
441 static char *iface_config_str(void *ctx, struct interface_config *config)
445 /* todo: HWADDR size is hardcoded as 6, but we may need to handle
446 * different hardware address formats */
447 str = talloc_asprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x,",
448 config->hwaddr[0], config->hwaddr[1],
449 config->hwaddr[2], config->hwaddr[3],
450 config->hwaddr[4], config->hwaddr[5]);
452 if (config->ignore) {
453 str = talloc_asprintf_append(str, "ignore");
455 } else if (config->method == CONFIG_METHOD_DHCP) {
456 str = talloc_asprintf_append(str, "dhcp");
458 } else if (config->method == CONFIG_METHOD_STATIC) {
459 str = talloc_asprintf_append(str, "static,%s%s%s",
460 config->static_config.address,
461 config->static_config.gateway ? "," : "",
462 config->static_config.gateway ?: "");
467 static char *dns_config_str(void *ctx, const char **dns_servers, int n)
472 str = talloc_strdup(ctx, "dns,");
473 for (i = 0; i < n; i++) {
474 str = talloc_asprintf_append(str, "%s%s",
482 static void update_string_config(struct platform_powerpc *platform,
483 const char *name, const char *value)
487 cur = get_param(platform, name);
489 /* don't set an empty parameter if it doesn't already exist */
490 if (!cur && !strlen(value))
493 set_param(platform, name, value);
496 static void update_network_config(struct platform_powerpc *platform,
497 struct config *config)
502 val = talloc_strdup(platform, "");
504 for (i = 0; i < config->network.n_interfaces; i++) {
505 char *iface_str = iface_config_str(platform,
506 config->network.interfaces[i]);
507 val = talloc_asprintf_append(val, "%s%s",
508 *val == '\0' ? "" : " ", iface_str);
509 talloc_free(iface_str);
512 if (config->network.n_dns_servers) {
513 char *dns_str = dns_config_str(platform,
514 config->network.dns_servers,
515 config->network.n_dns_servers);
516 val = talloc_asprintf_append(val, "%s%s",
517 *val == '\0' ? "" : " ", dns_str);
518 talloc_free(dns_str);
521 update_string_config(platform, "petitboot,network", val);
526 static void update_bootdev_config(struct platform_powerpc *platform,
527 struct config *config)
529 char *val, *tmp = NULL;
531 if (!config->boot_device)
534 tmp = val = talloc_asprintf(platform,
535 "uuid:%s", config->boot_device);
537 update_string_config(platform, "petitboot,bootdev", val);
541 static int update_config(struct platform_powerpc *platform,
542 struct config *config, struct config *defaults)
547 if (config->autoboot_enabled == defaults->autoboot_enabled)
550 val = config->autoboot_enabled ? "true" : "false";
551 update_string_config(platform, "auto-boot?", val);
553 if (config->autoboot_timeout_sec == defaults->autoboot_timeout_sec)
556 val = tmp = talloc_asprintf(platform, "%d",
557 config->autoboot_timeout_sec);
559 update_string_config(platform, "petitboot,timeout", val);
563 val = config->lang ?: "";
564 update_string_config(platform, "petitboot,language", val);
566 update_network_config(platform, config);
568 update_bootdev_config(platform, config);
570 return write_nvram(platform);
573 static void set_exclusive_devtype(struct config *config,
574 enum device_type devtype)
576 config->n_boot_priorities = 2;
577 config->boot_priorities = talloc_realloc(config,
578 config->boot_priorities, struct boot_priority,
579 config->n_boot_priorities);
580 config->boot_priorities[0].type = devtype;
581 config->boot_priorities[0].priority = 0;
582 config->boot_priorities[1].type = DEVICE_TYPE_ANY;
583 config->boot_priorities[1].priority = -1;
586 static int read_bootdev_sysparam(const char *name, uint8_t *val)
592 strcpy(path, sysparams_dir);
593 assert(strlen(name) < sizeof(path) - strlen(path));
596 fd = open(path, O_RDONLY);
598 pb_debug("powerpc: can't access sysparam %s\n",
603 rc = read(fd, buf, sizeof(buf));
607 /* bootdev definitions should only be one byte in size */
609 pb_debug("powerpc: sysparam %s read returned %d\n",
614 pb_debug("powerpc: sysparam %s: 0x%02x\n", name, buf[0]);
619 case IPMI_BOOTDEV_NONE:
620 case IPMI_BOOTDEV_NETWORK:
621 case IPMI_BOOTDEV_DISK:
622 case IPMI_BOOTDEV_SAFE:
623 case IPMI_BOOTDEV_CDROM:
624 case IPMI_BOOTDEV_SETUP:
631 static int write_bootdev_sysparam(const char *name, uint8_t val)
636 strcpy(path, sysparams_dir);
637 assert(strlen(name) < sizeof(path) - strlen(path));
640 fd = open(path, O_WRONLY);
642 pb_debug("powerpc: can't access sysparam %s for writing\n",
649 rc = write(fd, &val, sizeof(val));
650 if (rc == sizeof(val)) {
655 if (rc <= 0 && errno != EINTR) {
656 pb_log("powerpc: error updating sysparam %s: %s",
657 name, strerror(errno));
666 pb_debug("powerpc: set sysparam %s: 0x%02x\n", name, val);
671 static void parse_opal_sysparams(struct config *config)
673 uint8_t next_bootdev, default_bootdev;
674 bool next_valid, default_valid;
677 rc = read_bootdev_sysparam("next-boot-device", &next_bootdev);
678 next_valid = rc == 0;
680 rc = read_bootdev_sysparam("default-boot-device", &default_bootdev);
681 default_valid = rc == 0;
683 /* nothing valid? no need to change the config */
684 if (!next_valid && !default_valid)
688 next_bootdev = default_bootdev;
690 switch (next_bootdev) {
691 case IPMI_BOOTDEV_NONE:
693 case IPMI_BOOTDEV_DISK:
694 set_exclusive_devtype(config, DEVICE_TYPE_DISK);
696 case IPMI_BOOTDEV_NETWORK:
697 set_exclusive_devtype(config, DEVICE_TYPE_NETWORK);
699 case IPMI_BOOTDEV_CDROM:
700 set_exclusive_devtype(config, DEVICE_TYPE_OPTICAL);
702 case IPMI_BOOTDEV_SETUP:
703 config->autoboot_enabled = false;
705 case IPMI_BOOTDEV_SAFE:
706 config->autoboot_enabled = false;
707 config->safe_mode = true;
712 static int load_config(struct platform *p, struct config *config)
714 struct platform_powerpc *platform = to_platform_powerpc(p);
717 rc = parse_nvram(platform);
721 populate_config(platform, config);
723 parse_opal_sysparams(config);
728 static int save_config(struct platform *p, struct config *config)
730 struct platform_powerpc *platform = to_platform_powerpc(p);
731 struct config *defaults;
734 defaults = talloc_zero(platform, struct config);
735 config_set_defaults(defaults);
737 rc = update_config(platform, config, defaults);
739 talloc_free(defaults);
743 static void finalise_config(struct platform *platform __attribute__((unused)))
745 /* invalidate next-boot-device setting */
746 write_bootdev_sysparam("next-boot-device", 0xff);
749 static int get_sysinfo(struct platform *p, struct system_info *sysinfo)
751 struct platform_powerpc *platform = p->platform_data;
752 char *buf, *filename;
755 filename = talloc_asprintf(platform, "%smodel", devtree_dir);
756 rc = read_file(platform, filename, &buf, &len);
758 sysinfo->type = talloc_steal(sysinfo, buf);
759 talloc_free(filename);
761 filename = talloc_asprintf(platform, "%ssystem-id", devtree_dir);
762 rc = read_file(platform, filename, &buf, &len);
764 sysinfo->identifier = talloc_steal(sysinfo, buf);
765 talloc_free(filename);
770 static bool probe(struct platform *p, void *ctx)
772 struct platform_powerpc *platform;
776 /* we need a device tree */
777 rc = stat("/proc/device-tree", &statbuf);
781 if (!S_ISDIR(statbuf.st_mode))
784 platform = talloc(ctx, struct platform_powerpc);
785 list_init(&platform->params);
787 p->platform_data = platform;
792 static struct platform platform_powerpc = {
794 .dhcp_arch_id = 0x000e,
796 .load_config = load_config,
797 .save_config = save_config,
798 .finalise_config = finalise_config,
799 .get_sysinfo = get_sysinfo,
802 register_platform(platform_powerpc);