From patchwork Thu Aug 2 17:29:39 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geoff Levand X-Patchwork-Id: 952925 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41hHMM0N4vz9s2g for ; Fri, 3 Aug 2018 03:33:19 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=infradead.org header.i=@infradead.org header.b="e130YGGX"; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 41hHML5vnJzF2DY for ; Fri, 3 Aug 2018 03:33:18 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=infradead.org header.i=@infradead.org header.b="e130YGGX"; dkim-atps=neutral X-Original-To: Petitboot@lists.ozlabs.org Delivered-To: Petitboot@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=none (mailfrom) smtp.mailfrom=infradead.org (client-ip=2001:8b0:10b:1231::1; helo=merlin.infradead.org; envelope-from=geoff@infradead.org; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=infradead.org header.i=@infradead.org header.b="e130YGGX"; dkim-atps=neutral Received: from merlin.infradead.org (merlin.infradead.org [IPv6:2001:8b0:10b:1231::1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 41hHHC2pg3zF27G for ; Fri, 3 Aug 2018 03:29:43 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=Date:Cc:To:Subject:From:References: In-Reply-To:Message-Id:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=1bjjJnfCxuen2ADbRmY0SCf0ElSeh1wlZT2iXz+HS64=; b=e130YGGXBKq9shc+q2OIFdpD6 N/1JPET3NSW/8wc00ttHTere+FSCA0WPL6c0JmxIPKJ0NbJeGPgT3Utf/138Ow0/Ymm1z+9keQmdF L4hVQjjD9HbbpedVj05Qug0C2UOrzlzDfUNhfiOO60/ufaZ0316JLHG2yKGaoR5q/exol/JE9HoPk KdQozqdbFOhDwcjqdtG/zo0EaegN6UpNDPAa/yaFQc6ybkaCm9hVlF2QEhkNAElLTahr4l+CGR2GI J7NHjfdV9EvXbgGyNkR6J3UHB1QbnIinU97BwF0YXF2xPvzcCd/ZDct9BBngLIsFSk2lJmQEsdSAI L8+wqnGMg==; Received: from geoff by merlin.infradead.org with local (Exim 4.90_1 #2 (Red Hat Linux)) id 1flHPv-0007Zh-KH; Thu, 02 Aug 2018 17:29:39 +0000 Message-Id: In-Reply-To: References: From: Geoff Levand Patch-Date: Thu, 2 Aug 2018 10:19:54 -0700 Subject: [PATCH v2 25/30] discover: Move generic params routines to platform To: Samuel Mendoza-Jonas Date: Thu, 02 Aug 2018 17:29:39 +0000 X-BeenThere: petitboot@lists.ozlabs.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: Petitboot bootloader development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Ge Song , Petitboot@lists.ozlabs.org MIME-Version: 1.0 Errors-To: petitboot-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Petitboot" From: Ge Song 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 --- 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 a96f5d5..b2f60e4 100644 --- a/discover/platform-powerpc.c +++ b/discover/platform-powerpc.c @@ -170,124 +170,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; @@ -328,8 +212,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, @@ -677,8 +561,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"); @@ -764,7 +648,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)