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/";
24 static const int ipmi_timeout = 500; /* milliseconds */
30 struct list_item list;
33 struct platform_powerpc {
36 bool ipmi_bootdev_persistent;
37 int (*get_ipmi_bootdev)(
38 struct platform_powerpc *platform,
39 uint8_t *bootdev, bool *persistent);
40 int (*clear_ipmi_bootdev)(
41 struct platform_powerpc *platform);
44 static const char *known_params[] = {
54 #define to_platform_powerpc(p) \
55 (struct platform_powerpc *)(p->platform_data)
57 /* a partition max a max size of 64k * 16bytes = 1M */
58 static const int max_partition_size = 64 * 1024 * 16;
60 static bool param_is_known(const char *param, unsigned int len)
62 const char *known_param;
65 for (i = 0; known_params[i]; i++) {
66 known_param = known_params[i];
67 if (len == strlen(known_param) &&
68 !strncmp(param, known_param, len))
75 static int parse_nvram_params(struct platform_powerpc *platform,
78 char *pos, *name, *value;
79 unsigned int paramlen;
82 /* discard 2 header lines:
89 for (i = 0; i < len; i++) {
97 fprintf(stderr, "failure parsing nvram output\n");
101 for (pos = buf + i; pos < buf + len; pos += paramlen + 1) {
102 unsigned int namelen;
106 newline = strchr(pos, '\n');
112 paramlen = strlen(pos);
115 value = strchr(pos, '=');
119 namelen = value - name;
123 if (!param_is_known(name, namelen))
128 param = talloc(platform, struct param);
129 param->modified = false;
130 param->name = talloc_strndup(platform, name, namelen);
131 param->value = talloc_strdup(platform, value);
132 list_add(&platform->params, ¶m->list);
138 static int parse_nvram(struct platform_powerpc *platform)
140 struct process *process;
145 argv[1] = "--print-config";
146 argv[2] = "--partition";
150 process = process_create(platform);
151 process->path = "nvram";
152 process->argv = argv;
153 process->keep_stdout = true;
155 rc = process_run_sync(process);
157 if (rc || !process_exit_ok(process)) {
158 fprintf(stderr, "nvram process returned "
159 "non-zero exit status\n");
162 rc = parse_nvram_params(platform, process->stdout_buf,
163 process->stdout_len);
166 process_release(process);
170 static int write_nvram(struct platform_powerpc *platform)
172 struct process *process;
178 argv[1] = "--update-config";
180 argv[3] = "--partition";
184 process = process_create(platform);
185 process->path = "nvram";
186 process->argv = argv;
188 list_for_each_entry(&platform->params, param, list) {
191 if (!param->modified)
194 paramstr = talloc_asprintf(platform, "%s=%s",
195 param->name, param->value);
198 rc = process_run_sync(process);
200 talloc_free(paramstr);
202 if (rc || !process_exit_ok(process)) {
204 pb_log("nvram update process returned "
205 "non-zero exit status\n");
210 process_release(process);
214 static const char *get_param(struct platform_powerpc *platform,
219 list_for_each_entry(&platform->params, param, list)
220 if (!strcmp(param->name, name))
225 static void set_param(struct platform_powerpc *platform, const char *name,
230 list_for_each_entry(&platform->params, param, list) {
231 if (strcmp(param->name, name))
234 if (!strcmp(param->value, value))
237 talloc_free(param->value);
238 param->value = talloc_strdup(param, value);
239 param->modified = true;
244 param = talloc(platform, struct param);
245 param->modified = true;
246 param->name = talloc_strdup(platform, name);
247 param->value = talloc_strdup(platform, value);
248 list_add(&platform->params, ¶m->list);
251 static int parse_hwaddr(struct interface_config *ifconf, char *str)
255 if (strlen(str) != strlen("00:00:00:00:00:00"))
258 for (i = 0; i < HWADDR_SIZE; i++) {
262 byte[0] = str[i * 3 + 0];
263 byte[1] = str[i * 3 + 1];
266 x = strtoul(byte, &endp, 16);
267 if (endp != byte + 2)
270 ifconf->hwaddr[i] = x & 0xff;
276 static int parse_one_interface_config(struct config *config,
279 struct interface_config *ifconf;
282 ifconf = talloc_zero(config, struct interface_config);
284 if (!confstr || !strlen(confstr))
287 /* first token should be the mac address */
288 tok = strtok_r(confstr, ",", &saveptr);
292 if (parse_hwaddr(ifconf, tok))
295 /* second token is the method */
296 tok = strtok_r(NULL, ",", &saveptr);
297 if (!tok || !strlen(tok) || !strcmp(tok, "ignore")) {
298 ifconf->ignore = true;
300 } else if (!strcmp(tok, "dhcp")) {
301 ifconf->method = CONFIG_METHOD_DHCP;
303 } else if (!strcmp(tok, "static")) {
304 ifconf->method = CONFIG_METHOD_STATIC;
306 /* ip/mask, [optional] gateway */
307 tok = strtok_r(NULL, ",", &saveptr);
310 ifconf->static_config.address =
311 talloc_strdup(ifconf, tok);
313 tok = strtok_r(NULL, ",", &saveptr);
315 ifconf->static_config.gateway =
316 talloc_strdup(ifconf, tok);
320 pb_log("Unknown network configuration method %s\n", tok);
324 config->network.interfaces = talloc_realloc(config,
325 config->network.interfaces,
326 struct interface_config *,
327 ++config->network.n_interfaces);
329 config->network.interfaces[config->network.n_interfaces - 1] = ifconf;
337 static int parse_one_dns_config(struct config *config,
342 for (tok = strtok_r(confstr, ",", &saveptr); tok;
343 tok = strtok_r(NULL, ",", &saveptr)) {
345 char *server = talloc_strdup(config, tok);
347 config->network.dns_servers = talloc_realloc(config,
348 config->network.dns_servers, const char *,
349 ++config->network.n_dns_servers);
351 config->network.dns_servers[config->network.n_dns_servers - 1]
358 static void populate_network_config(struct platform_powerpc *platform,
359 struct config *config)
361 char *val, *saveptr = NULL;
365 cval = get_param(platform, "petitboot,network");
366 if (!cval || !strlen(cval))
369 val = talloc_strdup(config, cval);
374 tok = strtok_r(i == 0 ? val : NULL, " ", &saveptr);
378 if (!strncasecmp(tok, "dns,", strlen("dns,")))
379 parse_one_dns_config(config, tok + strlen("dns,"));
381 parse_one_interface_config(config, tok);
388 static void populate_bootdev_config(struct platform_powerpc *platform,
389 struct config *config)
394 config->boot_device = NULL;
396 val = get_param(platform, "petitboot,bootdev");
397 if (!val || !strlen(val))
400 if (!strncmp(val, "uuid:", strlen("uuid:"))) {
401 config->boot_device = talloc_strdup(config,
402 val + strlen("uuid:"));
404 } else if (!strncmp(val, "mac:", strlen("mac:"))) {
405 config->boot_device = talloc_strdup(config,
406 val + strlen("mac:"));
409 pb_log("bootdev config is in an unknown format "
410 "(expected uuid:... or mac:...)");
414 static void populate_config(struct platform_powerpc *platform,
415 struct config *config)
419 unsigned long timeout;
421 /* if the "auto-boot?' property is present and "false", disable auto
423 val = get_param(platform, "auto-boot?");
424 config->autoboot_enabled = !val || strcmp(val, "false");
426 val = get_param(platform, "petitboot,timeout");
428 timeout = strtoul(val, &end, 10);
430 if (timeout >= INT_MAX)
432 config->autoboot_timeout_sec = (int)timeout;
436 val = get_param(platform, "petitboot,language");
437 config->lang = val ? talloc_strdup(config, val) : NULL;
439 populate_network_config(platform, config);
441 populate_bootdev_config(platform, config);
443 if (!config->debug) {
444 val = get_param(platform, "petitboot,debug?");
445 config->debug = val && !strcmp(val, "true");
449 static char *iface_config_str(void *ctx, struct interface_config *config)
453 /* todo: HWADDR size is hardcoded as 6, but we may need to handle
454 * different hardware address formats */
455 str = talloc_asprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x,",
456 config->hwaddr[0], config->hwaddr[1],
457 config->hwaddr[2], config->hwaddr[3],
458 config->hwaddr[4], config->hwaddr[5]);
460 if (config->ignore) {
461 str = talloc_asprintf_append(str, "ignore");
463 } else if (config->method == CONFIG_METHOD_DHCP) {
464 str = talloc_asprintf_append(str, "dhcp");
466 } else if (config->method == CONFIG_METHOD_STATIC) {
467 str = talloc_asprintf_append(str, "static,%s%s%s",
468 config->static_config.address,
469 config->static_config.gateway ? "," : "",
470 config->static_config.gateway ?: "");
475 static char *dns_config_str(void *ctx, const char **dns_servers, int n)
480 str = talloc_strdup(ctx, "dns,");
481 for (i = 0; i < n; i++) {
482 str = talloc_asprintf_append(str, "%s%s",
490 static void update_string_config(struct platform_powerpc *platform,
491 const char *name, const char *value)
495 cur = get_param(platform, name);
497 /* don't set an empty parameter if it doesn't already exist */
498 if (!cur && !strlen(value))
501 set_param(platform, name, value);
504 static void update_network_config(struct platform_powerpc *platform,
505 struct config *config)
510 val = talloc_strdup(platform, "");
512 for (i = 0; i < config->network.n_interfaces; i++) {
513 char *iface_str = iface_config_str(platform,
514 config->network.interfaces[i]);
515 val = talloc_asprintf_append(val, "%s%s",
516 *val == '\0' ? "" : " ", iface_str);
517 talloc_free(iface_str);
520 if (config->network.n_dns_servers) {
521 char *dns_str = dns_config_str(platform,
522 config->network.dns_servers,
523 config->network.n_dns_servers);
524 val = talloc_asprintf_append(val, "%s%s",
525 *val == '\0' ? "" : " ", dns_str);
526 talloc_free(dns_str);
529 update_string_config(platform, "petitboot,network", val);
534 static void update_bootdev_config(struct platform_powerpc *platform,
535 struct config *config)
537 char *val, *tmp = NULL;
539 if (!config->boot_device)
542 tmp = val = talloc_asprintf(platform,
543 "uuid:%s", config->boot_device);
545 update_string_config(platform, "petitboot,bootdev", val);
549 static int update_config(struct platform_powerpc *platform,
550 struct config *config, struct config *defaults)
555 if (config->autoboot_enabled == defaults->autoboot_enabled)
558 val = config->autoboot_enabled ? "true" : "false";
559 update_string_config(platform, "auto-boot?", val);
561 if (config->autoboot_timeout_sec == defaults->autoboot_timeout_sec)
564 val = tmp = talloc_asprintf(platform, "%d",
565 config->autoboot_timeout_sec);
567 update_string_config(platform, "petitboot,timeout", val);
571 val = config->lang ?: "";
572 update_string_config(platform, "petitboot,language", val);
574 update_network_config(platform, config);
576 update_bootdev_config(platform, config);
578 return write_nvram(platform);
581 static void set_ipmi_bootdev(struct config *config, enum ipmi_bootdev bootdev,
584 config->ipmi_bootdev = bootdev;
585 config->ipmi_bootdev_persistent = persistent;
588 case IPMI_BOOTDEV_NONE:
589 case IPMI_BOOTDEV_DISK:
590 case IPMI_BOOTDEV_NETWORK:
591 case IPMI_BOOTDEV_CDROM:
593 case IPMI_BOOTDEV_SETUP:
594 config->autoboot_enabled = false;
596 case IPMI_BOOTDEV_SAFE:
597 config->autoboot_enabled = false;
598 config->safe_mode = true;
603 static int read_bootdev_sysparam(const char *name, uint8_t *val)
609 strcpy(path, sysparams_dir);
610 assert(strlen(name) < sizeof(path) - strlen(path));
613 fd = open(path, O_RDONLY);
615 pb_debug("powerpc: can't access sysparam %s\n",
620 rc = read(fd, buf, sizeof(buf));
624 /* bootdev definitions should only be one byte in size */
626 pb_debug("powerpc: sysparam %s read returned %d\n",
631 pb_debug("powerpc: sysparam %s: 0x%02x\n", name, buf[0]);
633 if (!ipmi_bootdev_is_valid(buf[0]))
640 static int write_bootdev_sysparam(const char *name, uint8_t val)
645 strcpy(path, sysparams_dir);
646 assert(strlen(name) < sizeof(path) - strlen(path));
649 fd = open(path, O_WRONLY);
651 pb_debug("powerpc: can't access sysparam %s for writing\n",
658 rc = write(fd, &val, sizeof(val));
659 if (rc == sizeof(val)) {
664 if (rc <= 0 && errno != EINTR) {
665 pb_log("powerpc: error updating sysparam %s: %s",
666 name, strerror(errno));
675 pb_debug("powerpc: set sysparam %s: 0x%02x\n", name, val);
680 static int clear_ipmi_bootdev_sysparams(
681 struct platform_powerpc *platform __attribute__((unused)))
683 /* invalidate next-boot-device setting */
684 write_bootdev_sysparam("next-boot-device", 0xff);
688 static int get_ipmi_bootdev_sysparams(
689 struct platform_powerpc *platform __attribute__((unused)),
690 uint8_t *bootdev, bool *persistent)
692 uint8_t next_bootdev, default_bootdev;
693 bool next_valid, default_valid;
696 rc = read_bootdev_sysparam("next-boot-device", &next_bootdev);
697 next_valid = rc == 0;
699 rc = read_bootdev_sysparam("default-boot-device", &default_bootdev);
700 default_valid = rc == 0;
702 /* nothing valid? no need to change the config */
703 if (!next_valid && !default_valid)
706 *persistent = !next_valid;
707 *bootdev = next_valid ? next_bootdev : default_bootdev;
711 static int clear_ipmi_bootdev_ipmi(struct platform_powerpc *platform)
716 0x05, /* parameter selector: boot flags */
717 0x80, /* data 1: valid */
718 0x00, /* data 2: bootdev: no override */
719 0x00, /* data 3: system defaults */
720 0x00, /* data 4: no request for shared mode, mux defaults */
721 0x00, /* data 5: no instance request */
724 resp_len = sizeof(resp);
726 ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
727 IPMI_CMD_CHASSIS_SET_SYSTEM_BOOT_OPTIONS,
734 static int get_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
735 uint8_t *bootdev, bool *persistent)
741 0x05, /* parameter selector: boot flags */
742 0x00, /* no set selector */
743 0x00, /* no block selector */
746 resp_len = sizeof(resp);
747 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
748 IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
753 pb_log("platform: error reading IPMI boot options\n");
757 if (resp_len != sizeof(resp)) {
758 pb_log("platform: unexpected length (%d) in "
759 "boot options response\n", resp_len);
764 pb_log("platform: non-zero completion code %d from IPMI req\n",
769 /* check for correct parameter version */
770 if ((resp[1] & 0xf) != 0x1) {
771 pb_log("platform: unexpected version (0x%x) in "
772 "boot options response\n", resp[0]);
776 /* check for valid paramters */
777 if (resp[2] & 0x80) {
778 pb_debug("platform: boot options are invalid/locked\n");
784 /* check for valid flags */
785 if (!(resp[3] & 0x80)) {
786 pb_debug("platform: boot flags are invalid, ignoring\n");
790 *persistent = resp[3] & 0x40;
791 *bootdev = (resp[4] >> 2) & 0x0f;
796 static int load_config(struct platform *p, struct config *config)
798 struct platform_powerpc *platform = to_platform_powerpc(p);
801 rc = parse_nvram(platform);
805 populate_config(platform, config);
807 if (platform->get_ipmi_bootdev) {
808 bool bootdev_persistent;
810 rc = platform->get_ipmi_bootdev(platform, &bootdev,
811 &bootdev_persistent);
812 if (!rc && ipmi_bootdev_is_valid(bootdev)) {
813 set_ipmi_bootdev(config, bootdev, bootdev_persistent);
820 static int save_config(struct platform *p, struct config *config)
822 struct platform_powerpc *platform = to_platform_powerpc(p);
823 struct config *defaults;
826 defaults = talloc_zero(platform, struct config);
827 config_set_defaults(defaults);
829 rc = update_config(platform, config, defaults);
831 talloc_free(defaults);
835 static void finalise_config(struct platform *p, const struct config *config)
837 struct platform_powerpc *platform = to_platform_powerpc(p);
839 if (config->ipmi_bootdev_persistent && platform->clear_ipmi_bootdev)
840 platform->clear_ipmi_bootdev(platform);
843 static int get_sysinfo(struct platform *p, struct system_info *sysinfo)
845 struct platform_powerpc *platform = p->platform_data;
846 char *buf, *filename;
849 filename = talloc_asprintf(platform, "%smodel", devtree_dir);
850 rc = read_file(platform, filename, &buf, &len);
852 sysinfo->type = talloc_steal(sysinfo, buf);
853 talloc_free(filename);
855 filename = talloc_asprintf(platform, "%ssystem-id", devtree_dir);
856 rc = read_file(platform, filename, &buf, &len);
858 sysinfo->identifier = talloc_steal(sysinfo, buf);
859 talloc_free(filename);
864 static bool probe(struct platform *p, void *ctx)
866 struct platform_powerpc *platform;
870 /* we need a device tree */
871 rc = stat("/proc/device-tree", &statbuf);
875 if (!S_ISDIR(statbuf.st_mode))
878 platform = talloc(ctx, struct platform_powerpc);
879 list_init(&platform->params);
881 p->platform_data = platform;
883 if (ipmi_present()) {
884 pb_debug("platform: using direct IPMI for IPMI paramters\n");
885 platform->ipmi = ipmi_open(platform);
886 platform->get_ipmi_bootdev = get_ipmi_bootdev_ipmi;
887 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_ipmi;
889 } else if (!stat(sysparams_dir, &statbuf)) {
890 pb_debug("platform: using sysparams for IPMI paramters\n");
891 platform->get_ipmi_bootdev = get_ipmi_bootdev_sysparams;
892 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_sysparams;
895 pb_log("platform: no IPMI parameter support\n");
902 static struct platform platform_powerpc = {
904 .dhcp_arch_id = 0x000e,
906 .load_config = load_config,
907 .save_config = save_config,
908 .finalise_config = finalise_config,
909 .get_sysinfo = get_sysinfo,
912 register_platform(platform_powerpc);