]> git.ozlabs.org Git - petitboot/blobdiff - ui/ncurses/nc-config.c
discover/sysinfo: Add network availability helper
[petitboot] / ui / ncurses / nc-config.c
index 2750bbb7719189de934b9716e5eb952d7d09e06c..834962968ca04328f13112b03d81d5488703649c 100644 (file)
@@ -33,7 +33,7 @@
 #include "nc-config.h"
 #include "nc-widgets.h"
 
-#define N_FIELDS       34
+#define N_FIELDS       48
 
 extern struct help_text config_help_text;
 
@@ -67,8 +67,11 @@ struct config_screen {
 
        bool                    autoboot_enabled;
        bool                    ipmi_override;
+       bool                    net_override;
 
        struct {
+               struct nc_widget_label          *autoboot_l;
+               struct nc_widget_select         *autoboot_f;
                struct nc_widget_label          *boot_order_l;
                struct nc_widget_subset         *boot_order_f;
                struct nc_widget_label          *boot_empty_l;
@@ -96,14 +99,26 @@ struct config_screen {
                struct nc_widget_label          *gateway_l;
                struct nc_widget_textbox        *gateway_f;
                struct nc_widget_label          *gateway_help_l;
+               struct nc_widget_label          *url_l;
+               struct nc_widget_textbox        *url_f;
+               struct nc_widget_label          *url_help_l;
                struct nc_widget_label          *dns_l;
                struct nc_widget_textbox        *dns_f;
                struct nc_widget_label          *dns_dhcp_help_l;
                struct nc_widget_label          *dns_help_l;
+               struct nc_widget_label          *http_proxy_l;
+               struct nc_widget_textbox        *http_proxy_f;
+               struct nc_widget_label          *https_proxy_l;
+               struct nc_widget_textbox        *https_proxy_f;
 
                struct nc_widget_label          *allow_write_l;
                struct nc_widget_select         *allow_write_f;
+               struct nc_widget_label          *boot_console_l;
+               struct nc_widget_select         *boot_console_f;
+               struct nc_widget_label          *manual_console_l;
+               struct nc_widget_label          *current_console_l;
 
+               struct nc_widget_label          *net_override_l;
                struct nc_widget_label          *safe_mode;
                struct nc_widget_button         *ok_b;
                struct nc_widget_button         *help_b;
@@ -188,11 +203,11 @@ static int screen_process_form(struct config_screen *screen)
        const struct system_info *sysinfo = screen->cui->sysinfo;
        enum net_conf_type net_conf_type;
        struct interface_config *iface;
+       bool allow_write, autoboot;
        char *str, *end;
        struct config *config;
-       int i, n_boot_opts, rc, idx;
-       unsigned int *order;
-       bool allow_write;
+       int i, n_boot_opts, rc;
+       unsigned int *order, idx;
        char mac[20];
 
        config = config_copy(screen, screen->cui->config);
@@ -203,7 +218,8 @@ static int screen_process_form(struct config_screen *screen)
        n_boot_opts = widget_subset_get_order(config, &order,
                                              screen->widgets.boot_order_f);
 
-       config->autoboot_enabled = n_boot_opts > 0;
+       autoboot = widget_select_get_value(screen->widgets.autoboot_f);
+       config->autoboot_enabled = autoboot && n_boot_opts;
 
        config->n_autoboot_opts = n_boot_opts;
        config->autoboot_opts = talloc_array(config, struct autoboot_option,
@@ -274,11 +290,12 @@ static int screen_process_form(struct config_screen *screen)
        }
 
        if (net_conf_type == NET_CONF_TYPE_STATIC) {
-               char *ip, *mask, *gateway;
+               char *ip, *mask, *gateway, *url;
 
                ip = widget_textbox_get_value(screen->widgets.ip_addr_f);
                mask = widget_textbox_get_value(screen->widgets.ip_mask_f);
                gateway = widget_textbox_get_value(screen->widgets.gateway_f);
+               url = widget_textbox_get_value(screen->widgets.url_f);
 
                if (!ip || !*ip || !mask || !*mask) {
                        screen->scr.frame.status =
@@ -292,6 +309,7 @@ static int screen_process_form(struct config_screen *screen)
                iface->static_config.address = talloc_asprintf(iface, "%s/%s",
                                ip, mask);
                iface->static_config.gateway = talloc_strdup(iface, gateway);
+               iface->static_config.url = talloc_strdup(iface, url);
        }
 
        str = widget_textbox_get_value(screen->widgets.dns_f);
@@ -321,10 +339,30 @@ static int screen_process_form(struct config_screen *screen)
                }
        }
 
+       talloc_free(config->http_proxy);
+       talloc_free(config->https_proxy);
+       str = widget_textbox_get_value(screen->widgets.http_proxy_f);
+       config->http_proxy = talloc_strdup(config, str);
+       str = widget_textbox_get_value(screen->widgets.https_proxy_f);
+       config->https_proxy = talloc_strdup(config, str);
+
        allow_write = widget_select_get_value(screen->widgets.allow_write_f);
        if (allow_write != config->allow_writes)
                config->allow_writes = allow_write;
 
+       if (config->n_consoles && !config->manual_console) {
+               idx = widget_select_get_value(screen->widgets.boot_console_f);
+               if (!config->boot_console) {
+                       config->boot_console = talloc_strdup(config,
+                                                       config->consoles[idx]);
+               } else if (strncmp(config->boot_console, config->consoles[idx],
+                               strlen(config->boot_console)) != 0) {
+                       talloc_free(config->boot_console);
+                       config->boot_console = talloc_strdup(config,
+                                                       config->consoles[idx]);
+               }
+       }
+
        config->safe_mode = false;
        rc = cui_send_config(screen->cui, config);
        talloc_free(config);
@@ -382,41 +420,60 @@ static void config_screen_layout_widgets(struct config_screen *screen)
        help_x = screen->field_x + 2 +
                widget_width(widget_textbox_base(screen->widgets.dns_f));
 
-       wl = widget_label_base(screen->widgets.boot_order_l);
+       wl = widget_label_base(screen->widgets.autoboot_l);
        widget_set_visible(wl, true);
        widget_move(wl, y, screen->label_x);
 
+       wf = widget_select_base(screen->widgets.autoboot_f);
+       widget_set_visible(wf, true);
+       widget_move(wf, y, screen->field_x);
+       y += widget_height(wf);
+
+       show = screen->autoboot_enabled;
+
+       if (show)
+               y += 1;
+
+       wl = widget_label_base(screen->widgets.boot_order_l);
+       widget_set_visible(wl, show);
+       widget_move(wl, y, screen->label_x);
+
        wf = widget_subset_base(screen->widgets.boot_order_f);
        widget_move(wf, y, screen->field_x);
        wl = widget_label_base(screen->widgets.boot_empty_l);
        widget_move(wl, y, screen->field_x);
 
        if (widget_subset_height(screen->widgets.boot_order_f)) {
-               widget_set_visible(wf, true);
                widget_set_visible(wl, false);
-               y += widget_height(wf);
+               widget_set_visible(wf, show);
+               y += show ? widget_height(wf) : 0;
        } else {
-               widget_set_visible(wl, true);
+               widget_set_visible(wl, show);
                widget_set_visible(wf, false);
-               y += 1;
+               y += show ? 1 : 0;
        }
 
-       y += 1;
-
-       widget_move(widget_button_base(screen->widgets.boot_add_b),
-                       y++, screen->field_x);
-       widget_move(widget_button_base(screen->widgets.boot_any_b),
-                       y++, screen->field_x);
-       widget_move(widget_button_base(screen->widgets.boot_none_b),
-                       y, screen->field_x);
+       if (show) {
+               y += 1;
+               widget_move(widget_button_base(screen->widgets.boot_add_b),
+                               y++, screen->field_x);
+               widget_move(widget_button_base(screen->widgets.boot_any_b),
+                               y++, screen->field_x);
+               widget_move(widget_button_base(screen->widgets.boot_none_b),
+                               y, screen->field_x);
+       }
 
        wf = widget_button_base(screen->widgets.boot_add_b);
-       if (widget_subset_n_inactive(screen->widgets.boot_order_f))
+       if (widget_subset_n_inactive(screen->widgets.boot_order_f) && show)
                widget_set_visible(wf, true);
        else
                widget_set_visible(wf, false);
 
-       y += 2;
+       if (show)
+               y += 2;
+
+       widget_set_visible(widget_button_base(screen->widgets.boot_any_b), show);
+       widget_set_visible(widget_button_base(screen->widgets.boot_none_b), show);
 
        wf = widget_textbox_base(screen->widgets.timeout_f);
        wl = widget_label_base(screen->widgets.timeout_l);
@@ -430,7 +487,8 @@ static void config_screen_layout_widgets(struct config_screen *screen)
                widget_move(wf, y, screen->field_x);
                widget_move(wh, y, screen->field_x + widget_width(wf) + 1);
                y += 2;
-       }
+       } else
+               y += 1;
 
        if (screen->ipmi_override) {
                wl = widget_label_base(screen->widgets.ipmi_type_l);
@@ -510,6 +568,19 @@ static void config_screen_layout_widgets(struct config_screen *screen)
                y++;
        }
 
+       wl = widget_label_base(screen->widgets.url_l);
+       wf = widget_textbox_base(screen->widgets.url_f);
+       wh = widget_label_base(screen->widgets.url_help_l);
+       widget_set_visible(wl, show);
+       widget_set_visible(wf, show);
+       widget_set_visible(wh, show);
+
+       if (show) {
+               layout_pair(screen, y, screen->widgets.url_l, wf);
+               widget_move(wh, y, help_x);
+               y++;
+       }
+
        wh = widget_label_base(screen->widgets.dns_help_l);
        layout_pair(screen, y, screen->widgets.dns_l,
                        widget_textbox_base(screen->widgets.dns_f));
@@ -525,14 +596,14 @@ static void config_screen_layout_widgets(struct config_screen *screen)
                y += 1;
        }
 
-       y += 1;
+       wf = widget_textbox_base(screen->widgets.http_proxy_f);
+       layout_pair(screen, y, screen->widgets.http_proxy_l, wf);
+       y++;
+       wf = widget_textbox_base(screen->widgets.https_proxy_f);
+       layout_pair(screen, y, screen->widgets.https_proxy_l, wf);
+       y++;
 
-       show = screen->cui->config->safe_mode;
-       if (show) {
-               widget_move(widget_label_base(screen->widgets.safe_mode),
-                       y, screen->field_x);
-               y += 1;
-       }
+       y += 1;
 
        layout_pair(screen, y, screen->widgets.allow_write_l,
                    widget_select_base(screen->widgets.allow_write_f));
@@ -540,6 +611,46 @@ static void config_screen_layout_widgets(struct config_screen *screen)
 
        y += 1;
 
+       if (screen->widgets.manual_console_l) {
+               layout_pair(screen, y++, screen->widgets.boot_console_l,
+                       widget_label_base(screen->widgets.manual_console_l));
+               widget_move(widget_label_base(screen->widgets.current_console_l),
+                       y, screen->field_x);
+               widget_set_visible(widget_select_base(
+                       screen->widgets.boot_console_f), false);
+               y += 2;
+       } else if (widget_height(widget_select_base(screen->widgets.boot_console_f))) {
+               layout_pair(screen, y, screen->widgets.boot_console_l,
+                           widget_select_base(screen->widgets.boot_console_f));
+               y += widget_height(widget_select_base(screen->widgets.boot_console_f));
+               widget_move(widget_label_base(screen->widgets.current_console_l),
+                       y, screen->field_x);
+               y += 2;
+       } else {
+               widget_set_visible(widget_label_base(
+                                       screen->widgets.boot_console_l), false);
+               widget_set_visible(widget_select_base(
+                                       screen->widgets.boot_console_f), false);
+               widget_set_visible(widget_label_base(
+                                       screen->widgets.current_console_l), false);
+       }
+
+       if (screen->net_override) {
+               widget_move(widget_label_base(screen->widgets.net_override_l),
+                               y, screen->label_x);
+               widget_set_visible(widget_label_base(screen->widgets.net_override_l),
+                                       true);
+               y += 1;
+       }
+
+       if (screen->cui->config->safe_mode) {
+               widget_move(widget_label_base(screen->widgets.safe_mode),
+                       y, screen->label_x);
+               widget_set_visible(widget_label_base(screen->widgets.safe_mode),
+                                       true);
+               y += 1;
+       }
+
        widget_move(widget_button_base(screen->widgets.ok_b),
                        y, screen->field_x);
        widget_move(widget_button_base(screen->widgets.help_b),
@@ -566,6 +677,15 @@ static void config_screen_boot_order_change(void *arg, int value)
        widgetset_post(screen->widgetset);
 }
 
+static void config_screen_autoboot_change(void *arg, int value)
+{
+       struct config_screen *screen = arg;
+       screen->autoboot_enabled = !!value;
+       widgetset_unpost(screen->widgetset);
+       config_screen_layout_widgets(screen);
+       widgetset_post(screen->widgetset);
+}
+
 static void config_screen_add_device(void *arg)
 {
        struct config_screen *screen = arg;
@@ -581,7 +701,6 @@ static void config_screen_autoboot_none(void *arg)
        struct nc_widget_subset *subset = screen->widgets.boot_order_f;
 
        widget_subset_clear_active(subset);
-       screen->autoboot_enabled = false;
 
        widgetset_unpost(screen->widgetset);
        config_screen_layout_widgets(screen);
@@ -693,10 +812,11 @@ static void config_screen_setup_widgets(struct config_screen *screen,
 {
        struct nc_widgetset *set = screen->widgetset;
        struct interface_config *ifcfg;
-       char *str, *ip, *mask, *gw;
+       char *str, *ip, *mask, *gw, *url, *tty, *label;
        enum net_conf_type type;
        unsigned int i;
        int add_len, clear_len, any_len, min_len = 20;
+       bool found;
 
        build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
                        == N_FIELDS);
@@ -704,6 +824,21 @@ static void config_screen_setup_widgets(struct config_screen *screen,
        type = screen->net_conf_type;
        ifcfg = first_active_interface(config);
 
+       screen->autoboot_enabled = config->autoboot_enabled;
+
+       screen->widgets.autoboot_l = widget_new_label(set, 0, 0,
+                                       _("Autoboot:"));
+       screen->widgets.autoboot_f = widget_new_select(set, 0, 0,
+                                       COLS - screen->field_x - 1);
+
+       widget_select_add_option(screen->widgets.autoboot_f, 0, _("Disabled"),
+                                !screen->autoboot_enabled);
+       widget_select_add_option(screen->widgets.autoboot_f, 1, _("Enabled"),
+                                screen->autoboot_enabled);
+
+       widget_select_on_change(screen->widgets.autoboot_f,
+                       config_screen_autoboot_change, screen);
+
        add_len = max(min_len, strncols(_("Add Device")));
        clear_len = max(min_len, strncols(_("Clear")));
        any_len = max(min_len, strncols(_("Clear & Boot Any")));
@@ -733,7 +868,6 @@ static void config_screen_setup_widgets(struct config_screen *screen,
 
        for (i = 0; i < sysinfo->n_blockdevs; i++) {
                struct blockdev_info *bd = sysinfo->blockdevs[i];
-               char *label;
 
                label = talloc_asprintf(screen, _("disk: %s [uuid: %s]"),
                                bd->name, bd->uuid);
@@ -743,7 +877,7 @@ static void config_screen_setup_widgets(struct config_screen *screen,
 
        for (i = 0; i < sysinfo->n_interfaces; i++) {
                struct interface_info *info = sysinfo->interfaces[i];
-               char *label, mac[20];
+               char mac[20];
 
                mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
 
@@ -754,7 +888,6 @@ static void config_screen_setup_widgets(struct config_screen *screen,
        }
 
        for (i = DEVICE_TYPE_NETWORK; i < DEVICE_TYPE_UNKNOWN; i++) {
-               char *label;
 
                if (i == DEVICE_TYPE_ANY)
                        label = talloc_asprintf(screen, _("Any Device"));
@@ -765,7 +898,6 @@ static void config_screen_setup_widgets(struct config_screen *screen,
                widget_subset_add_option(screen->widgets.boot_order_f, label);
        }
 
-       screen->autoboot_enabled = config->n_autoboot_opts;
        for (i = 0; i < config->n_autoboot_opts; i++) {
                struct autoboot_option *opt = &config->autoboot_opts[i];
                int idx;
@@ -796,7 +928,7 @@ static void config_screen_setup_widgets(struct config_screen *screen,
        widget_textbox_set_validator_integer(screen->widgets.timeout_f, 0, 999);
 
        if (config->ipmi_bootdev) {
-               char *label = talloc_asprintf(screen,
+               label = talloc_asprintf(screen,
                                _("%s IPMI boot option: %s"),
                                config->ipmi_bootdev_persistent ?
                                "Persistent" : "Temporary",
@@ -849,7 +981,7 @@ static void config_screen_setup_widgets(struct config_screen *screen,
                                                i, str, is_default);
        }
 
-       gw = ip = mask = NULL;
+       url = gw = ip = mask = NULL;
        if (ifcfg && ifcfg->method == CONFIG_METHOD_STATIC) {
                char *sep;
 
@@ -862,6 +994,13 @@ static void config_screen_setup_widgets(struct config_screen *screen,
                        mask = sep + 1;
                }
                gw = ifcfg->static_config.gateway;
+               url = ifcfg->static_config.url;
+       }
+
+       screen->net_override = ifcfg && ifcfg->override;
+       if (screen->net_override) {
+               screen->widgets.net_override_l = widget_new_label(set, 0, 0,
+                       _("Network Override Active! 'OK' will overwrite interface config"));
        }
 
        screen->widgets.ip_addr_l = widget_new_label(set, 0, 0, _("IP/mask:"));
@@ -884,6 +1023,11 @@ static void config_screen_setup_widgets(struct config_screen *screen,
        widget_textbox_set_fixed_size(screen->widgets.gateway_f);
        widget_textbox_set_validator_ipv4(screen->widgets.gateway_f);
 
+       screen->widgets.url_l = widget_new_label(set, 0, 0, _("URL:"));
+       screen->widgets.url_f = widget_new_textbox(set, 0, 0, 32, url);
+       screen->widgets.url_help_l =
+               widget_new_label(set, 0, 0, _("(eg. tftp://)"));
+
        str = talloc_strdup(screen, "");
        for (i = 0; i < config->network.n_dns_servers; i++) {
                str = talloc_asprintf_append(str, "%s%s",
@@ -902,6 +1046,15 @@ static void config_screen_setup_widgets(struct config_screen *screen,
        screen->widgets.dns_dhcp_help_l = widget_new_label(set, 0, 0,
                        _("(if not provided by DHCP server)"));
 
+       screen->widgets.http_proxy_l = widget_new_label(set, 0, 0,
+                                       _("HTTP Proxy:"));
+       screen->widgets.http_proxy_f = widget_new_textbox(set, 0, 0, 32,
+                                               config->http_proxy);
+       screen->widgets.https_proxy_l = widget_new_label(set, 0, 0,
+                                       _("HTTPS Proxy:"));
+       screen->widgets.https_proxy_f = widget_new_textbox(set, 0, 0, 32,
+                                               config->https_proxy);
+
        if (config->safe_mode)
                screen->widgets.safe_mode = widget_new_label(set, 0, 0,
                         _("Selecting 'OK' will exit safe mode"));
@@ -919,6 +1072,29 @@ static void config_screen_setup_widgets(struct config_screen *screen,
                                _("Allow bootloader scripts to modify disks"),
                                config->allow_writes);
 
+       screen->widgets.boot_console_l = widget_new_label(set, 0, 0,
+                       _("Boot console:"));
+       screen->widgets.boot_console_f = widget_new_select(set, 0, 0,
+                                               COLS - screen->field_x - 1);
+
+       for (i = 0; i < config->n_consoles; i++){
+               found = config->boot_console &&
+                       strncmp(config->boot_console, config->consoles[i],
+                               strlen(config->boot_console)) == 0;
+               widget_select_add_option(screen->widgets.boot_console_f, i,
+                                       config->consoles[i], found);
+       }
+
+       if (config->manual_console) {
+               label = talloc_asprintf(screen, _("Manually set: '%s'"),
+                                       config->boot_console);
+               screen->widgets.manual_console_l = widget_new_label(set, 0, 0, label);
+       }
+
+       tty = talloc_asprintf(screen, _("Current interface: %s"),
+                               ttyname(STDIN_FILENO));
+       screen->widgets.current_console_l = widget_new_label(set, 0 , 0, tty);
+
        screen->widgets.ok_b = widget_new_button(set, 0, 0, 10, _("OK"),
                        ok_click, screen);
        screen->widgets.help_b = widget_new_button(set, 0, 0, 10, _("Help"),
@@ -930,18 +1106,22 @@ static void config_screen_setup_widgets(struct config_screen *screen,
 static void config_screen_widget_focus(struct nc_widget *widget, void *arg)
 {
        struct config_screen *screen = arg;
-       int w_y, s_max;
+       int w_y, w_height, w_focus, s_max, adjust;
 
-       w_y = widget_y(widget) + widget_focus_y(widget);
+       w_height = widget_height(widget);
+       w_focus = widget_focus_y(widget);
+       w_y = widget_y(widget) + w_focus;
        s_max = getmaxy(screen->scr.sub_ncw) - 1;
 
        if (w_y < screen->scroll_y)
                screen->scroll_y = w_y;
 
-       else if (w_y + screen->scroll_y + 1 > s_max)
-               screen->scroll_y = 1 + w_y - s_max;
-
-       else
+       else if (w_y + screen->scroll_y + 1 > s_max) {
+               /* Fit as much of the widget into the screen as possible */
+               adjust = min(s_max - 1, w_height - w_focus);
+               if (w_y + adjust >= screen->scroll_y + s_max)
+                       screen->scroll_y = max(0, 1 + w_y + adjust - s_max);
+       } else
                return;
 
        pad_refresh(screen);