]> git.ozlabs.org Git - petitboot/commitdiff
discover: Move generic params routines to platform
authorGe Song <ge.song@hxt-semitech.com>
Thu, 2 Aug 2018 17:29:39 +0000 (17:29 +0000)
committerSamuel Mendoza-Jonas <sam@mendozajonas.com>
Tue, 7 Aug 2018 01:30:36 +0000 (11:30 +1000)
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 <ge.song@hxt-semitech.com>
[Split from a larger patch and cleaned up]
Signed-off-by: Geoff Levand <geoff@infradead.org>
Signed-off-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
discover/platform-powerpc.c
discover/platform.c
discover/platform.h

index 3c1ffb98d4d724613e55351180911dd559eae2d7..84e18ccca5c51e73edc93585cbefe1a30b822598 100644 (file)
@@ -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);
index 2ac2de21d43b3d8414ee8392c3d1a57e5b094276..767f66e6fa41c4b25135287dacdff288a73e0677 100644 (file)
@@ -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);
+}
+
index a9ea79b622e459e9c7b82f00d0996d1d62db105c..294056263782e7f72020a52b4d4aeaa4cfc9da41 100644 (file)
@@ -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)