12 #include <file/file.h>
13 #include <talloc/talloc.h>
14 #include <list/list.h>
16 #include <process/process.h>
22 static const char *partition = "common";
23 static const char *sysparams_dir = "/sys/firmware/opal/sysparams/";
24 static const char *devtree_dir = "/proc/device-tree/";
25 static const int ipmi_timeout = 500; /* milliseconds */
31 struct list_item list;
34 struct platform_powerpc {
37 bool ipmi_bootdev_persistent;
38 int (*get_ipmi_bootdev)(
39 struct platform_powerpc *platform,
40 uint8_t *bootdev, bool *persistent);
41 int (*clear_ipmi_bootdev)(
42 struct platform_powerpc *platform);
43 int (*set_os_boot_sensor)(
44 struct platform_powerpc *platform);
47 static const char *known_params[] = {
57 #define to_platform_powerpc(p) \
58 (struct platform_powerpc *)(p->platform_data)
60 /* a partition max a max size of 64k * 16bytes = 1M */
61 static const int max_partition_size = 64 * 1024 * 16;
63 static bool param_is_known(const char *param, unsigned int len)
65 const char *known_param;
68 for (i = 0; known_params[i]; i++) {
69 known_param = known_params[i];
70 if (len == strlen(known_param) &&
71 !strncmp(param, known_param, len))
78 static int parse_nvram_params(struct platform_powerpc *platform,
81 char *pos, *name, *value;
82 unsigned int paramlen;
85 /* discard 2 header lines:
92 for (i = 0; i < len; i++) {
100 fprintf(stderr, "failure parsing nvram output\n");
104 for (pos = buf + i; pos < buf + len; pos += paramlen + 1) {
105 unsigned int namelen;
109 newline = strchr(pos, '\n');
115 paramlen = strlen(pos);
118 value = strchr(pos, '=');
122 namelen = value - name;
126 if (!param_is_known(name, namelen))
131 param = talloc(platform, struct param);
132 param->modified = false;
133 param->name = talloc_strndup(platform, name, namelen);
134 param->value = talloc_strdup(platform, value);
135 list_add(&platform->params, ¶m->list);
141 static int parse_nvram(struct platform_powerpc *platform)
143 struct process *process;
148 argv[1] = "--print-config";
149 argv[2] = "--partition";
153 process = process_create(platform);
154 process->path = "nvram";
155 process->argv = argv;
156 process->keep_stdout = true;
158 rc = process_run_sync(process);
160 if (rc || !process_exit_ok(process)) {
161 fprintf(stderr, "nvram process returned "
162 "non-zero exit status\n");
165 rc = parse_nvram_params(platform, process->stdout_buf,
166 process->stdout_len);
169 process_release(process);
173 static int write_nvram(struct platform_powerpc *platform)
175 struct process *process;
181 argv[1] = "--update-config";
183 argv[3] = "--partition";
187 process = process_create(platform);
188 process->path = "nvram";
189 process->argv = argv;
191 list_for_each_entry(&platform->params, param, list) {
194 if (!param->modified)
197 paramstr = talloc_asprintf(platform, "%s=%s",
198 param->name, param->value);
201 rc = process_run_sync(process);
203 talloc_free(paramstr);
205 if (rc || !process_exit_ok(process)) {
207 pb_log("nvram update process returned "
208 "non-zero exit status\n");
213 process_release(process);
217 static const char *get_param(struct platform_powerpc *platform,
222 list_for_each_entry(&platform->params, param, list)
223 if (!strcmp(param->name, name))
228 static void set_param(struct platform_powerpc *platform, const char *name,
233 list_for_each_entry(&platform->params, param, list) {
234 if (strcmp(param->name, name))
237 if (!strcmp(param->value, value))
240 talloc_free(param->value);
241 param->value = talloc_strdup(param, value);
242 param->modified = true;
247 param = talloc(platform, struct param);
248 param->modified = true;
249 param->name = talloc_strdup(platform, name);
250 param->value = talloc_strdup(platform, value);
251 list_add(&platform->params, ¶m->list);
254 static int parse_hwaddr(struct interface_config *ifconf, char *str)
258 if (strlen(str) != strlen("00:00:00:00:00:00"))
261 for (i = 0; i < HWADDR_SIZE; i++) {
265 byte[0] = str[i * 3 + 0];
266 byte[1] = str[i * 3 + 1];
269 x = strtoul(byte, &endp, 16);
270 if (endp != byte + 2)
273 ifconf->hwaddr[i] = x & 0xff;
279 static int parse_one_interface_config(struct config *config,
282 struct interface_config *ifconf;
285 ifconf = talloc_zero(config, struct interface_config);
287 if (!confstr || !strlen(confstr))
290 /* first token should be the mac address */
291 tok = strtok_r(confstr, ",", &saveptr);
295 if (parse_hwaddr(ifconf, tok))
298 /* second token is the method */
299 tok = strtok_r(NULL, ",", &saveptr);
300 if (!tok || !strlen(tok) || !strcmp(tok, "ignore")) {
301 ifconf->ignore = true;
303 } else if (!strcmp(tok, "dhcp")) {
304 ifconf->method = CONFIG_METHOD_DHCP;
306 } else if (!strcmp(tok, "static")) {
307 ifconf->method = CONFIG_METHOD_STATIC;
309 /* ip/mask, [optional] gateway */
310 tok = strtok_r(NULL, ",", &saveptr);
313 ifconf->static_config.address =
314 talloc_strdup(ifconf, tok);
316 tok = strtok_r(NULL, ",", &saveptr);
318 ifconf->static_config.gateway =
319 talloc_strdup(ifconf, tok);
323 pb_log("Unknown network configuration method %s\n", tok);
327 config->network.interfaces = talloc_realloc(config,
328 config->network.interfaces,
329 struct interface_config *,
330 ++config->network.n_interfaces);
332 config->network.interfaces[config->network.n_interfaces - 1] = ifconf;
340 static int parse_one_dns_config(struct config *config,
343 char *tok, *saveptr = NULL;
345 for (tok = strtok_r(confstr, ",", &saveptr); tok;
346 tok = strtok_r(NULL, ",", &saveptr)) {
348 char *server = talloc_strdup(config, tok);
350 config->network.dns_servers = talloc_realloc(config,
351 config->network.dns_servers, const char *,
352 ++config->network.n_dns_servers);
354 config->network.dns_servers[config->network.n_dns_servers - 1]
361 static void populate_network_config(struct platform_powerpc *platform,
362 struct config *config)
364 char *val, *saveptr = NULL;
368 cval = get_param(platform, "petitboot,network");
369 if (!cval || !strlen(cval))
372 val = talloc_strdup(config, cval);
377 tok = strtok_r(i == 0 ? val : NULL, " ", &saveptr);
381 if (!strncasecmp(tok, "dns,", strlen("dns,")))
382 parse_one_dns_config(config, tok + strlen("dns,"));
384 parse_one_interface_config(config, tok);
391 static void populate_bootdev_config(struct platform_powerpc *platform,
392 struct config *config)
397 config->boot_device = NULL;
399 val = get_param(platform, "petitboot,bootdev");
400 if (!val || !strlen(val))
403 if (!strncmp(val, "uuid:", strlen("uuid:"))) {
404 config->boot_device = talloc_strdup(config,
405 val + strlen("uuid:"));
407 } else if (!strncmp(val, "mac:", strlen("mac:"))) {
408 config->boot_device = talloc_strdup(config,
409 val + strlen("mac:"));
412 pb_log("bootdev config is in an unknown format "
413 "(expected uuid:... or mac:...)");
417 static void populate_config(struct platform_powerpc *platform,
418 struct config *config)
422 unsigned long timeout;
424 /* if the "auto-boot?' property is present and "false", disable auto
426 val = get_param(platform, "auto-boot?");
427 config->autoboot_enabled = !val || strcmp(val, "false");
429 val = get_param(platform, "petitboot,timeout");
431 timeout = strtoul(val, &end, 10);
433 if (timeout >= INT_MAX)
435 config->autoboot_timeout_sec = (int)timeout;
439 val = get_param(platform, "petitboot,language");
440 config->lang = val ? talloc_strdup(config, val) : NULL;
442 populate_network_config(platform, config);
444 populate_bootdev_config(platform, config);
446 if (!config->debug) {
447 val = get_param(platform, "petitboot,debug?");
448 config->debug = val && !strcmp(val, "true");
452 static char *iface_config_str(void *ctx, struct interface_config *config)
456 /* todo: HWADDR size is hardcoded as 6, but we may need to handle
457 * different hardware address formats */
458 str = talloc_asprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x,",
459 config->hwaddr[0], config->hwaddr[1],
460 config->hwaddr[2], config->hwaddr[3],
461 config->hwaddr[4], config->hwaddr[5]);
463 if (config->ignore) {
464 str = talloc_asprintf_append(str, "ignore");
466 } else if (config->method == CONFIG_METHOD_DHCP) {
467 str = talloc_asprintf_append(str, "dhcp");
469 } else if (config->method == CONFIG_METHOD_STATIC) {
470 str = talloc_asprintf_append(str, "static,%s%s%s",
471 config->static_config.address,
472 config->static_config.gateway ? "," : "",
473 config->static_config.gateway ?: "");
478 static char *dns_config_str(void *ctx, const char **dns_servers, int n)
483 str = talloc_strdup(ctx, "dns,");
484 for (i = 0; i < n; i++) {
485 str = talloc_asprintf_append(str, "%s%s",
493 static void update_string_config(struct platform_powerpc *platform,
494 const char *name, const char *value)
498 cur = get_param(platform, name);
500 /* don't set an empty parameter if it doesn't already exist */
501 if (!cur && !strlen(value))
504 set_param(platform, name, value);
507 static void update_network_config(struct platform_powerpc *platform,
508 struct config *config)
513 val = talloc_strdup(platform, "");
515 for (i = 0; i < config->network.n_interfaces; i++) {
516 char *iface_str = iface_config_str(platform,
517 config->network.interfaces[i]);
518 val = talloc_asprintf_append(val, "%s%s",
519 *val == '\0' ? "" : " ", iface_str);
520 talloc_free(iface_str);
523 if (config->network.n_dns_servers) {
524 char *dns_str = dns_config_str(platform,
525 config->network.dns_servers,
526 config->network.n_dns_servers);
527 val = talloc_asprintf_append(val, "%s%s",
528 *val == '\0' ? "" : " ", dns_str);
529 talloc_free(dns_str);
532 update_string_config(platform, "petitboot,network", val);
537 static void update_bootdev_config(struct platform_powerpc *platform,
538 struct config *config)
540 char *val, *tmp = NULL;
542 if (!config->boot_device)
545 tmp = val = talloc_asprintf(platform,
546 "uuid:%s", config->boot_device);
548 update_string_config(platform, "petitboot,bootdev", val);
552 static int update_config(struct platform_powerpc *platform,
553 struct config *config, struct config *defaults)
558 if (config->autoboot_enabled == defaults->autoboot_enabled)
561 val = config->autoboot_enabled ? "true" : "false";
562 update_string_config(platform, "auto-boot?", val);
564 if (config->autoboot_timeout_sec == defaults->autoboot_timeout_sec)
567 val = tmp = talloc_asprintf(platform, "%d",
568 config->autoboot_timeout_sec);
570 update_string_config(platform, "petitboot,timeout", val);
574 val = config->lang ?: "";
575 update_string_config(platform, "petitboot,language", val);
577 update_network_config(platform, config);
579 update_bootdev_config(platform, config);
581 return write_nvram(platform);
584 static void set_ipmi_bootdev(struct config *config, enum ipmi_bootdev bootdev,
587 config->ipmi_bootdev = bootdev;
588 config->ipmi_bootdev_persistent = persistent;
591 case IPMI_BOOTDEV_NONE:
592 case IPMI_BOOTDEV_DISK:
593 case IPMI_BOOTDEV_NETWORK:
594 case IPMI_BOOTDEV_CDROM:
596 case IPMI_BOOTDEV_SETUP:
597 config->autoboot_enabled = false;
599 case IPMI_BOOTDEV_SAFE:
600 config->autoboot_enabled = false;
601 config->safe_mode = true;
606 static int read_bootdev_sysparam(const char *name, uint8_t *val)
612 strcpy(path, sysparams_dir);
613 assert(strlen(name) < sizeof(path) - strlen(path));
616 fd = open(path, O_RDONLY);
618 pb_debug("powerpc: can't access sysparam %s\n",
623 rc = read(fd, buf, sizeof(buf));
627 /* bootdev definitions should only be one byte in size */
629 pb_debug("powerpc: sysparam %s read returned %d\n",
634 pb_debug("powerpc: sysparam %s: 0x%02x\n", name, buf[0]);
636 if (!ipmi_bootdev_is_valid(buf[0]))
643 static int write_bootdev_sysparam(const char *name, uint8_t val)
648 strcpy(path, sysparams_dir);
649 assert(strlen(name) < sizeof(path) - strlen(path));
652 fd = open(path, O_WRONLY);
654 pb_debug("powerpc: can't access sysparam %s for writing\n",
661 rc = write(fd, &val, sizeof(val));
662 if (rc == sizeof(val)) {
667 if (rc <= 0 && errno != EINTR) {
668 pb_log("powerpc: error updating sysparam %s: %s",
669 name, strerror(errno));
678 pb_debug("powerpc: set sysparam %s: 0x%02x\n", name, val);
683 static int clear_ipmi_bootdev_sysparams(
684 struct platform_powerpc *platform __attribute__((unused)))
686 /* invalidate next-boot-device setting */
687 write_bootdev_sysparam("next-boot-device", 0xff);
691 static int get_ipmi_bootdev_sysparams(
692 struct platform_powerpc *platform __attribute__((unused)),
693 uint8_t *bootdev, bool *persistent)
695 uint8_t next_bootdev, default_bootdev;
696 bool next_valid, default_valid;
699 rc = read_bootdev_sysparam("next-boot-device", &next_bootdev);
700 next_valid = rc == 0;
702 rc = read_bootdev_sysparam("default-boot-device", &default_bootdev);
703 default_valid = rc == 0;
705 /* nothing valid? no need to change the config */
706 if (!next_valid && !default_valid)
709 *persistent = !next_valid;
710 *bootdev = next_valid ? next_bootdev : default_bootdev;
714 static int clear_ipmi_bootdev_ipmi(struct platform_powerpc *platform)
719 0x05, /* parameter selector: boot flags */
720 0x80, /* data 1: valid */
721 0x00, /* data 2: bootdev: no override */
722 0x00, /* data 3: system defaults */
723 0x00, /* data 4: no request for shared mode, mux defaults */
724 0x00, /* data 5: no instance request */
727 resp_len = sizeof(resp);
729 ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
730 IPMI_CMD_CHASSIS_SET_SYSTEM_BOOT_OPTIONS,
737 static int get_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
738 uint8_t *bootdev, bool *persistent)
744 0x05, /* parameter selector: boot flags */
745 0x00, /* no set selector */
746 0x00, /* no block selector */
749 resp_len = sizeof(resp);
750 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
751 IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
756 pb_log("platform: error reading IPMI boot options\n");
760 if (resp_len != sizeof(resp)) {
761 pb_log("platform: unexpected length (%d) in "
762 "boot options response\n", resp_len);
767 pb_log("platform: non-zero completion code %d from IPMI req\n",
772 /* check for correct parameter version */
773 if ((resp[1] & 0xf) != 0x1) {
774 pb_log("platform: unexpected version (0x%x) in "
775 "boot options response\n", resp[0]);
779 /* check for valid paramters */
780 if (resp[2] & 0x80) {
781 pb_debug("platform: boot options are invalid/locked\n");
787 /* check for valid flags */
788 if (!(resp[3] & 0x80)) {
789 pb_debug("platform: boot flags are invalid, ignoring\n");
793 *persistent = resp[3] & 0x40;
794 *bootdev = (resp[4] >> 2) & 0x0f;
798 static int set_ipmi_os_boot_sensor(struct platform_powerpc *platform)
804 0x00, /* sensor number: os boot */
805 0x10, /* operation: set assertion bits */
806 0x00, /* sensor reading: none */
807 0x40, /* assertion mask lsb: set state 6 */
808 0x00, /* assertion mask msb: none */
809 0x00, /* deassertion mask lsb: none */
810 0x00, /* deassertion mask msb: none */
811 0x00, /* event data 1: none */
812 0x00, /* event data 2: none */
813 0x00, /* event data 3: none */
816 sensor_number = get_ipmi_sensor(platform, IPMI_SENSOR_ID_OS_BOOT);
817 if (sensor_number < 0) {
818 pb_log("Couldn't find OS boot sensor in device tree\n");
822 req[0] = sensor_number;
824 resp_len = sizeof(resp);
826 ipmi_transaction(platform->ipmi, IPMI_NETFN_SE,
830 ipmi_timeout); return 0;
835 static int load_config(struct platform *p, struct config *config)
837 struct platform_powerpc *platform = to_platform_powerpc(p);
840 rc = parse_nvram(platform);
844 populate_config(platform, config);
846 if (platform->get_ipmi_bootdev) {
847 bool bootdev_persistent;
849 rc = platform->get_ipmi_bootdev(platform, &bootdev,
850 &bootdev_persistent);
851 if (!rc && ipmi_bootdev_is_valid(bootdev)) {
852 set_ipmi_bootdev(config, bootdev, bootdev_persistent);
859 static int save_config(struct platform *p, struct config *config)
861 struct platform_powerpc *platform = to_platform_powerpc(p);
862 struct config *defaults;
865 defaults = talloc_zero(platform, struct config);
866 config_set_defaults(defaults);
868 rc = update_config(platform, config, defaults);
870 talloc_free(defaults);
874 static void pre_boot(struct platform *p, const struct config *config)
876 struct platform_powerpc *platform = to_platform_powerpc(p);
878 if (!config->ipmi_bootdev_persistent && platform->clear_ipmi_bootdev)
879 platform->clear_ipmi_bootdev(platform);
881 if (platform->set_os_boot_sensor)
882 platform->set_os_boot_sensor(platform);
885 static int get_sysinfo(struct platform *p, struct system_info *sysinfo)
887 struct platform_powerpc *platform = p->platform_data;
888 char *buf, *filename;
891 filename = talloc_asprintf(platform, "%smodel", devtree_dir);
892 rc = read_file(platform, filename, &buf, &len);
894 sysinfo->type = talloc_steal(sysinfo, buf);
895 talloc_free(filename);
897 filename = talloc_asprintf(platform, "%ssystem-id", devtree_dir);
898 rc = read_file(platform, filename, &buf, &len);
900 sysinfo->identifier = talloc_steal(sysinfo, buf);
901 talloc_free(filename);
906 static bool probe(struct platform *p, void *ctx)
908 struct platform_powerpc *platform;
912 /* we need a device tree */
913 rc = stat("/proc/device-tree", &statbuf);
917 if (!S_ISDIR(statbuf.st_mode))
920 platform = talloc_zero(ctx, struct platform_powerpc);
921 list_init(&platform->params);
923 p->platform_data = platform;
925 if (ipmi_present()) {
926 pb_debug("platform: using direct IPMI for IPMI paramters\n");
927 platform->ipmi = ipmi_open(platform);
928 platform->get_ipmi_bootdev = get_ipmi_bootdev_ipmi;
929 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_ipmi;
930 platform->set_os_boot_sensor = set_ipmi_os_boot_sensor;
932 } else if (!stat(sysparams_dir, &statbuf)) {
933 pb_debug("platform: using sysparams for IPMI paramters\n");
934 platform->get_ipmi_bootdev = get_ipmi_bootdev_sysparams;
935 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_sysparams;
938 pb_log("platform: no IPMI parameter support\n");
945 static struct platform platform_powerpc = {
947 .dhcp_arch_id = 0x000e,
949 .load_config = load_config,
950 .save_config = save_config,
951 .pre_boot = pre_boot,
952 .get_sysinfo = get_sysinfo,
955 register_platform(platform_powerpc);