From 6fe83028cc8de0954e81bede58115c18ec29b040 Mon Sep 17 00:00:00 2001 From: Ge Song Date: Thu, 2 Aug 2018 17:29:39 +0000 Subject: [PATCH] discover: Move generic params routines to platform Move the generic params routines from platform-powerpc to platform. Also, for clarity, add a params prefix to the names. Signed-off-by: Ge Song [Split from a larger patch and cleaned up] Signed-off-by: Geoff Levand Signed-off-by: Samuel Mendoza-Jonas --- discover/platform-powerpc.c | 130 ++---------------------------------- discover/platform.c | 117 ++++++++++++++++++++++++++++++++ discover/platform.h | 5 ++ 3 files changed, 129 insertions(+), 123 deletions(-) diff --git a/discover/platform-powerpc.c b/discover/platform-powerpc.c index 3c1ffb9..84e18cc 100644 --- a/discover/platform-powerpc.c +++ b/discover/platform-powerpc.c @@ -171,124 +171,8 @@ static int write_nvram(struct platform_powerpc *platform) return rc; } -static char *interface_config_str(void *ctx, struct interface_config *config) -{ - char *str; - - /* todo: HWADDR size is hardcoded as 6, but we may need to handle - * different hardware address formats */ - str = talloc_asprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x,", - config->hwaddr[0], config->hwaddr[1], - config->hwaddr[2], config->hwaddr[3], - config->hwaddr[4], config->hwaddr[5]); - - if (config->ignore) { - str = talloc_asprintf_append(str, "ignore"); - - } else if (config->method == CONFIG_METHOD_DHCP) { - str = talloc_asprintf_append(str, "dhcp"); - - } else if (config->method == CONFIG_METHOD_STATIC) { - str = talloc_asprintf_append(str, "static,%s%s%s%s%s", - config->static_config.address, - config->static_config.gateway ? "," : "", - config->static_config.gateway ?: "", - config->static_config.url ? "," : "", - config->static_config.url ?: ""); - } - return str; -} - -static char *dns_config_str(void *ctx, const char **dns_servers, int n) -{ - char *str; - int i; - - str = talloc_strdup(ctx, "dns,"); - for (i = 0; i < n; i++) { - str = talloc_asprintf_append(str, "%s%s", - i == 0 ? "" : ",", - dns_servers[i]); - } - - return str; -} - -static void update_network_config(struct param_list *pl, const char *param_name, - const struct config *config) -{ - unsigned int i; - char *val; - - /* - * Don't store IPMI overrides to NVRAM. If this was a persistent - * override it was already stored in NVRAM by - * get_ipmi_network_override() - */ - if (config->network.n_interfaces && - config->network.interfaces[0]->override) - return; - - val = talloc_strdup(pl, ""); - - for (i = 0; i < config->network.n_interfaces; i++) { - char *iface_str = interface_config_str(pl, - config->network.interfaces[i]); - val = talloc_asprintf_append(val, "%s%s", - *val == '\0' ? "" : " ", iface_str); - talloc_free(iface_str); - } - - if (config->network.n_dns_servers) { - char *dns_str = dns_config_str(pl, - config->network.dns_servers, - config->network.n_dns_servers); - val = talloc_asprintf_append(val, "%s%s", - *val == '\0' ? "" : " ", dns_str); - talloc_free(dns_str); - } - - param_list_set_non_empty(pl, param_name, val, true); - - talloc_free(val); -} - -static void update_bootdev_config(struct param_list *pl, const char *param_name, - const struct config *config) -{ - char *val = NULL, *boot_str = NULL, *tmp = NULL; - struct autoboot_option *opt; - const char delim = ' '; - unsigned int i; - - if (!config->n_autoboot_opts) - val = ""; - - for (i = 0; i < config->n_autoboot_opts; i++) { - opt = &config->autoboot_opts[i]; - switch (opt->boot_type) { - case BOOT_DEVICE_TYPE: - boot_str = talloc_asprintf(config, "%s%c", - device_type_name(opt->type), - delim); - break; - case BOOT_DEVICE_UUID: - boot_str = talloc_asprintf(config, "uuid:%s%c", - opt->uuid, delim); - break; - } - tmp = val = talloc_asprintf_append(val, "%s", boot_str); - } - - param_list_set_non_empty(pl, param_name, val, true); - - talloc_free(tmp); - if (boot_str) - talloc_free(boot_str); -} - -static void update_config(struct param_list *pl, struct config *config, - const struct config *defaults) +static void params_update_all(struct param_list *pl, + const struct config *config, const struct config *defaults) { char *tmp = NULL; const char *val; @@ -329,8 +213,8 @@ static void update_config(struct param_list *pl, struct config *config, val = config->https_proxy ?: ""; param_list_set_non_empty(pl, "petitboot,https_proxy", val, true); - update_network_config(pl, "petitboot,network", config); - update_bootdev_config(pl, "petitboot,bootdevs", config); + params_update_network_values(pl, "petitboot,network", config); + params_update_bootdev_values(pl, "petitboot,bootdevs", config); } static void config_set_ipmi_bootdev(struct config *config, enum ipmi_bootdev bootdev, @@ -678,8 +562,8 @@ static void get_ipmi_network_override(struct platform_powerpc *platform, if (!rc && persistent) { /* Write this new config to NVRAM */ - update_network_config(&platform->params, "petitboot,network", - config); + params_update_network_values(&platform->params, + "petitboot,network", config); rc = write_nvram(platform); if (rc) pb_log("platform: Failed to save persistent interface override\n"); @@ -765,7 +649,7 @@ static int save_config(struct platform *p, struct config *config) defaults = talloc_zero(platform, struct config); config_set_defaults(defaults); - update_config(&platform->params, config, defaults); + params_update_all(&platform->params, config, defaults); talloc_free(defaults); return write_nvram(platform); diff --git a/discover/platform.c b/discover/platform.c index 2ac2de2..767f66e 100644 --- a/discover/platform.c +++ b/discover/platform.c @@ -560,3 +560,120 @@ void config_populate_all(struct config *config, const struct param_list *pl) if (val) config->https_proxy = talloc_strdup(config, val); } + +static char *interface_config_str(void *ctx, struct interface_config *config) +{ + char *str; + + /* todo: HWADDR size is hardcoded as 6, but we may need to handle + * different hardware address formats */ + str = talloc_asprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x,", + config->hwaddr[0], config->hwaddr[1], + config->hwaddr[2], config->hwaddr[3], + config->hwaddr[4], config->hwaddr[5]); + + if (config->ignore) { + str = talloc_asprintf_append(str, "ignore"); + + } else if (config->method == CONFIG_METHOD_DHCP) { + str = talloc_asprintf_append(str, "dhcp"); + + } else if (config->method == CONFIG_METHOD_STATIC) { + str = talloc_asprintf_append(str, "static,%s%s%s%s%s", + config->static_config.address, + config->static_config.gateway ? "," : "", + config->static_config.gateway ?: "", + config->static_config.url ? "," : "", + config->static_config.url ?: ""); + } + return str; +} + +static char *dns_config_str(void *ctx, const char **dns_servers, int n) +{ + char *str; + int i; + + str = talloc_strdup(ctx, "dns,"); + for (i = 0; i < n; i++) { + str = talloc_asprintf_append(str, "%s%s", + i == 0 ? "" : ",", + dns_servers[i]); + } + + return str; +} + +void params_update_network_values(struct param_list *pl, + const char *param_name, const struct config *config) +{ + unsigned int i; + char *val; + + /* + * Don't store IPMI overrides to NVRAM. If this was a persistent + * override it was already stored in NVRAM by + * get_ipmi_network_override() + */ + if (config->network.n_interfaces && + config->network.interfaces[0]->override) + return; + + val = talloc_strdup(pl, ""); + + for (i = 0; i < config->network.n_interfaces; i++) { + char *iface_str = interface_config_str(pl, + config->network.interfaces[i]); + val = talloc_asprintf_append(val, "%s%s", + *val == '\0' ? "" : " ", iface_str); + talloc_free(iface_str); + } + + if (config->network.n_dns_servers) { + char *dns_str = dns_config_str(pl, + config->network.dns_servers, + config->network.n_dns_servers); + val = talloc_asprintf_append(val, "%s%s", + *val == '\0' ? "" : " ", dns_str); + talloc_free(dns_str); + } + + param_list_set_non_empty(pl, param_name, val, true); + + talloc_free(val); +} + +void params_update_bootdev_values(struct param_list *pl, + const char *param_name, const struct config *config) +{ + char *val = NULL, *boot_str = NULL, *tmp = NULL; + struct autoboot_option *opt; + const char delim = ' '; + unsigned int i; + + if (!config->n_autoboot_opts) + val = ""; + + for (i = 0; i < config->n_autoboot_opts; i++) { + opt = &config->autoboot_opts[i]; + switch (opt->boot_type) { + case BOOT_DEVICE_TYPE: + boot_str = talloc_asprintf(config, "%s%c", + device_type_name(opt->type), + delim); + break; + case BOOT_DEVICE_UUID: + boot_str = talloc_asprintf(config, "uuid:%s%c", + opt->uuid, delim); + break; + } + tmp = val = talloc_asprintf_append(val, "%s", boot_str); + } + + param_list_set_non_empty(pl, param_name, val, true); + + talloc_free(tmp); + if (boot_str) + talloc_free(boot_str); +} + diff --git a/discover/platform.h b/discover/platform.h index a9ea79b..2940562 100644 --- a/discover/platform.h +++ b/discover/platform.h @@ -29,6 +29,11 @@ void config_set_defaults(struct config *config); void config_set_autoboot(bool autoboot_enabled); void config_populate_all(struct config *config, const struct param_list *pl); +void params_update_network_values(struct param_list *pl, + const char *param_name, const struct config *config); +void params_update_bootdev_values(struct param_list *pl, + const char *param_name, const struct config *config); + #define __platform_ptrname(_n) __platform_ ## _n #define _platform_ptrname(_n) __platform_ptrname(_n) -- 2.39.2