2 * Copyright (C) 2013 IBM Corporation
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #if defined(HAVE_CONFIG_H)
26 #include <pb-config/pb-config.h>
27 #include <talloc/talloc.h>
28 #include <types/types.h>
30 #include <i18n/i18n.h>
33 #include "nc-config.h"
34 #include "nc-widgets.h"
38 extern struct help_text config_help_text;
41 NET_CONF_TYPE_DHCP_ALL,
42 NET_CONF_TYPE_DHCP_ONE,
46 struct config_screen {
49 struct nc_widgetset *widgetset;
57 void (*on_exit)(struct cui *);
65 enum net_conf_type net_conf_type;
67 bool autoboot_enabled;
71 struct nc_widget_label *boot_order_l;
72 struct nc_widget_subset *boot_order_f;
73 struct nc_widget_label *boot_empty_l;
74 struct nc_widget_button *boot_add_b;
75 struct nc_widget_button *boot_none_b;
76 struct nc_widget_button *boot_any_b;
77 struct nc_widget_textbox *timeout_f;
78 struct nc_widget_label *timeout_l;
79 struct nc_widget_label *timeout_help_l;
81 struct nc_widget_label *ipmi_type_l;
82 struct nc_widget_label *ipmi_clear_l;
83 struct nc_widget_checkbox *ipmi_clear_cb;
85 struct nc_widget_label *network_l;
86 struct nc_widget_select *network_f;
88 struct nc_widget_label *iface_l;
89 struct nc_widget_select *iface_f;
90 struct nc_widget_label *ip_addr_l;
91 struct nc_widget_textbox *ip_addr_f;
92 struct nc_widget_label *ip_mask_l;
93 struct nc_widget_textbox *ip_mask_f;
94 struct nc_widget_label *ip_addr_mask_help_l;
95 struct nc_widget_label *gateway_l;
96 struct nc_widget_textbox *gateway_f;
97 struct nc_widget_label *gateway_help_l;
98 struct nc_widget_label *dns_l;
99 struct nc_widget_textbox *dns_f;
100 struct nc_widget_label *dns_dhcp_help_l;
101 struct nc_widget_label *dns_help_l;
103 struct nc_widget_label *allow_write_l;
104 struct nc_widget_select *allow_write_f;
106 struct nc_widget_label *safe_mode;
107 struct nc_widget_button *ok_b;
108 struct nc_widget_button *help_b;
109 struct nc_widget_button *cancel_b;
113 static struct config_screen *config_screen_from_scr(struct nc_scr *scr)
115 struct config_screen *config_screen;
117 assert(scr->sig == pb_config_screen_sig);
118 config_screen = (struct config_screen *)
119 ((char *)scr - (size_t)&((struct config_screen *)0)->scr);
120 assert(config_screen->scr.sig == pb_config_screen_sig);
121 return config_screen;
124 static void pad_refresh(struct config_screen *screen)
126 int y, x, rows, cols;
128 getmaxyx(screen->scr.sub_ncw, rows, cols);
129 getbegyx(screen->scr.sub_ncw, y, x);
131 prefresh(screen->pad, screen->scroll_y, 0, y, x, rows, cols);
134 static void config_screen_process_key(struct nc_scr *scr, int key)
136 struct config_screen *screen = config_screen_from_scr(scr);
139 handled = widgetset_process_key(screen->widgetset, key);
148 screen->show_help = true;
154 screen->on_exit(screen->cui);
156 } else if (screen->show_help) {
157 screen->show_help = false;
158 screen->need_redraw = true;
159 cui_show_help(screen->cui, _("System Configuration"),
162 } else if (handled && !screen->show_subset) {
167 static void config_screen_resize(struct nc_scr *scr)
169 struct config_screen *screen = config_screen_from_scr(scr);
173 static int config_screen_post(struct nc_scr *scr)
175 struct config_screen *screen = config_screen_from_scr(scr);
176 screen->show_subset = false;
177 widgetset_post(screen->widgetset);
178 nc_scr_frame_draw(scr);
179 if (screen->need_redraw) {
180 redrawwin(scr->main_ncw);
181 screen->need_redraw = false;
183 wrefresh(screen->scr.main_ncw);
188 static int config_screen_unpost(struct nc_scr *scr)
190 struct config_screen *screen = config_screen_from_scr(scr);
191 widgetset_unpost(screen->widgetset);
195 struct nc_scr *config_screen_scr(struct config_screen *screen)
200 static int screen_process_form(struct config_screen *screen)
202 const struct system_info *sysinfo = screen->cui->sysinfo;
203 enum net_conf_type net_conf_type;
204 struct interface_config *iface;
206 struct config *config;
207 int i, n_boot_opts, rc, idx;
212 config = config_copy(screen, screen->cui->config);
214 talloc_free(config->autoboot_opts);
215 config->n_autoboot_opts = 0;
217 n_boot_opts = widget_subset_get_order(config, &order,
218 screen->widgets.boot_order_f);
220 config->autoboot_enabled = n_boot_opts > 0;
222 config->n_autoboot_opts = n_boot_opts;
223 config->autoboot_opts = talloc_array(config, struct autoboot_option,
226 for (i = 0; i < n_boot_opts; i++) {
227 if (order[i] < sysinfo->n_blockdevs) {
229 config->autoboot_opts[i].boot_type = BOOT_DEVICE_UUID;
230 config->autoboot_opts[i].uuid = talloc_strdup(config,
231 sysinfo->blockdevs[order[i]]->uuid);
232 } else if(order[i] < (sysinfo->n_blockdevs + sysinfo->n_interfaces)) {
234 order[i] -= sysinfo->n_blockdevs;
235 config->autoboot_opts[i].boot_type = BOOT_DEVICE_UUID;
236 mac_str(sysinfo->interfaces[order[i]]->hwaddr,
237 sysinfo->interfaces[order[i]]->hwaddr_size,
239 config->autoboot_opts[i].uuid = talloc_strdup(config, mac);
242 order[i] -= (sysinfo->n_blockdevs + sysinfo->n_interfaces);
243 config->autoboot_opts[i].boot_type = BOOT_DEVICE_TYPE;
244 config->autoboot_opts[i].type = order[i];
248 str = widget_textbox_get_value(screen->widgets.timeout_f);
252 x = strtoul(str, &end, 10);
253 if (!errno && end != str)
254 config->autoboot_timeout_sec = x;
257 if (screen->ipmi_override)
258 if (widget_checkbox_get_value(screen->widgets.ipmi_clear_cb))
259 config->ipmi_bootdev = IPMI_BOOTDEV_INVALID;
262 net_conf_type = widget_select_get_value(screen->widgets.network_f);
264 /* if we don't have any network interfaces, prevent per-interface
266 if (sysinfo->n_interfaces == 0)
267 net_conf_type = NET_CONF_TYPE_DHCP_ALL;
269 if (net_conf_type == NET_CONF_TYPE_DHCP_ALL) {
270 config->network.n_interfaces = 0;
273 iface = talloc_zero(config, struct interface_config);
274 config->network.n_interfaces = 1;
275 config->network.interfaces = talloc_array(config,
276 struct interface_config *, 1);
277 config->network.interfaces[0] = iface;
279 /* copy hwaddr (from the sysinfo interface data) to
280 * the configuration */
281 idx = widget_select_get_value(screen->widgets.iface_f);
282 memcpy(iface->hwaddr, sysinfo->interfaces[idx]->hwaddr,
283 sizeof(iface->hwaddr));
286 if (net_conf_type == NET_CONF_TYPE_DHCP_ONE) {
287 iface->method = CONFIG_METHOD_DHCP;
290 if (net_conf_type == NET_CONF_TYPE_STATIC) {
291 char *ip, *mask, *gateway;
293 ip = widget_textbox_get_value(screen->widgets.ip_addr_f);
294 mask = widget_textbox_get_value(screen->widgets.ip_mask_f);
295 gateway = widget_textbox_get_value(screen->widgets.gateway_f);
297 if (!ip || !*ip || !mask || !*mask) {
298 screen->scr.frame.status =
299 _("No IP / mask values are set");
300 nc_scr_frame_draw(&screen->scr);
305 iface->method = CONFIG_METHOD_STATIC;
306 iface->static_config.address = talloc_asprintf(iface, "%s/%s",
308 iface->static_config.gateway = talloc_strdup(iface, gateway);
311 str = widget_textbox_get_value(screen->widgets.dns_f);
312 talloc_free(config->network.dns_servers);
313 config->network.dns_servers = NULL;
314 config->network.n_dns_servers = 0;
316 if (str && strlen(str)) {
321 dns = strtok_r(str, " \t", &tmp);
326 i = config->network.n_dns_servers++;
327 config->network.dns_servers = talloc_realloc(config,
328 config->network.dns_servers,
330 config->network.n_dns_servers);
331 config->network.dns_servers[i] =
332 talloc_strdup(config, dns);
338 allow_write = widget_select_get_value(screen->widgets.allow_write_f);
339 if (allow_write != config->allow_writes)
340 config->allow_writes = allow_write;
342 config->safe_mode = false;
343 rc = cui_send_config(screen->cui, config);
347 pb_log("cui_send_config failed!\n");
349 pb_debug("config sent!\n");
354 static void ok_click(void *arg)
356 struct config_screen *screen = arg;
357 if (screen_process_form(screen))
358 /* errors are written to the status line, so we'll need
360 wrefresh(screen->scr.main_ncw);
365 static void help_click(void *arg)
367 struct config_screen *screen = arg;
368 screen->show_help = true;
371 static void cancel_click(void *arg)
373 struct config_screen *screen = arg;
377 static int layout_pair(struct config_screen *screen, int y,
378 struct nc_widget_label *label,
379 struct nc_widget *field)
381 struct nc_widget *label_w = widget_label_base(label);
382 widget_move(label_w, y, screen->label_x);
383 widget_move(field, y, screen->field_x);
384 return max(widget_height(label_w), widget_height(field));
387 static void config_screen_layout_widgets(struct config_screen *screen)
389 struct nc_widget *wl, *wf, *wh;
394 /* currently, the longest label we have is the DNS-servers
395 * widget, so layout our screen based on that */
396 help_x = screen->field_x + 2 +
397 widget_width(widget_textbox_base(screen->widgets.dns_f));
399 wl = widget_label_base(screen->widgets.boot_order_l);
400 widget_set_visible(wl, true);
401 widget_move(wl, y, screen->label_x);
403 wf = widget_subset_base(screen->widgets.boot_order_f);
404 widget_move(wf, y, screen->field_x);
405 wl = widget_label_base(screen->widgets.boot_empty_l);
406 widget_move(wl, y, screen->field_x);
408 if (widget_subset_height(screen->widgets.boot_order_f)) {
409 widget_set_visible(wf, true);
410 widget_set_visible(wl, false);
411 y += widget_height(wf);
413 widget_set_visible(wl, true);
414 widget_set_visible(wf, false);
420 widget_move(widget_button_base(screen->widgets.boot_add_b),
422 widget_move(widget_button_base(screen->widgets.boot_any_b),
423 y, screen->field_x + 14);
424 widget_move(widget_button_base(screen->widgets.boot_none_b),
425 y, screen->field_x + 34);
427 wf = widget_button_base(screen->widgets.boot_add_b);
428 if (widget_subset_n_inactive(screen->widgets.boot_order_f))
429 widget_set_visible(wf, true);
431 widget_set_visible(wf, false);
435 wf = widget_textbox_base(screen->widgets.timeout_f);
436 wl = widget_label_base(screen->widgets.timeout_l);
437 wh = widget_label_base(screen->widgets.timeout_help_l);
438 widget_set_visible(wl, screen->autoboot_enabled);
439 widget_set_visible(wf, screen->autoboot_enabled);
440 widget_set_visible(wh, screen->autoboot_enabled);
441 if (screen->autoboot_enabled) {
442 widget_set_visible(wh, screen->autoboot_enabled);
443 widget_move(wl, y, screen->label_x);
444 widget_move(wf, y, screen->field_x);
445 widget_move(wh, y, screen->field_x + widget_width(wf) + 1);
449 if (screen->ipmi_override) {
450 wl = widget_label_base(screen->widgets.ipmi_type_l);
451 widget_set_visible(wl, true);
452 widget_move(wl, y, screen->label_x);
455 wl = widget_label_base(screen->widgets.ipmi_clear_l);
456 wf = widget_checkbox_base(screen->widgets.ipmi_clear_cb);
457 widget_set_visible(wl, true);
458 widget_set_visible(wf, true);
459 widget_move(wl, y, screen->label_x);
460 widget_move(wf, y, screen->field_x);
466 y += layout_pair(screen, y, screen->widgets.network_l,
467 widget_select_base(screen->widgets.network_f));
471 /* conditionally show iface select */
472 wl = widget_label_base(screen->widgets.iface_l);
473 wf = widget_select_base(screen->widgets.iface_f);
475 show = screen->net_conf_type == NET_CONF_TYPE_DHCP_ONE ||
476 screen->net_conf_type == NET_CONF_TYPE_STATIC;
478 widget_set_visible(wl, show);
479 widget_set_visible(wf, show);
482 y += layout_pair(screen, y, screen->widgets.iface_l, wf) + 1;
484 /* conditionally show static IP params */
485 show = screen->net_conf_type == NET_CONF_TYPE_STATIC;
487 wl = widget_label_base(screen->widgets.ip_addr_l);
488 wf = widget_textbox_base(screen->widgets.ip_addr_f);
489 widget_set_visible(wl, show);
490 widget_set_visible(wf, show);
491 x = screen->field_x + widget_width(wf) + 1;
494 layout_pair(screen, y, screen->widgets.ip_addr_l, wf);
496 wl = widget_label_base(screen->widgets.ip_mask_l);
497 wf = widget_textbox_base(screen->widgets.ip_mask_f);
498 widget_set_visible(wl, show);
499 widget_set_visible(wf, show);
502 widget_move(wl, y, x);
503 widget_move(wf, y, x + 2);
506 /* help for IP/mask */
507 wh = widget_label_base(screen->widgets.ip_addr_mask_help_l);
508 widget_set_visible(wh, show);
510 widget_move(wh, y, help_x);
514 wl = widget_label_base(screen->widgets.gateway_l);
515 wf = widget_textbox_base(screen->widgets.gateway_f);
516 wh = widget_label_base(screen->widgets.gateway_help_l);
517 widget_set_visible(wl, show);
518 widget_set_visible(wf, show);
519 widget_set_visible(wh, show);
522 layout_pair(screen, y, screen->widgets.gateway_l, wf);
523 widget_move(wh, y, help_x);
527 wh = widget_label_base(screen->widgets.dns_help_l);
528 layout_pair(screen, y, screen->widgets.dns_l,
529 widget_textbox_base(screen->widgets.dns_f));
530 widget_move(wh, y, help_x);
533 /* we show the DNS/DHCP help if we're configuring DHCP */
534 show = screen->net_conf_type != NET_CONF_TYPE_STATIC;
535 wl = widget_label_base(screen->widgets.dns_dhcp_help_l);
536 widget_set_visible(wl, show);
538 widget_move(wl, y, screen->field_x);
544 show = screen->cui->config->safe_mode;
546 widget_move(widget_label_base(screen->widgets.safe_mode),
551 layout_pair(screen, y, screen->widgets.allow_write_l,
552 widget_select_base(screen->widgets.allow_write_f));
553 y += widget_height(widget_select_base(screen->widgets.allow_write_f));
557 widget_move(widget_button_base(screen->widgets.ok_b),
559 widget_move(widget_button_base(screen->widgets.help_b),
560 y, screen->field_x + 10);
561 widget_move(widget_button_base(screen->widgets.cancel_b),
562 y, screen->field_x + 24);
565 static void config_screen_network_change(void *arg, int value)
567 struct config_screen *screen = arg;
568 screen->net_conf_type = value;
569 widgetset_unpost(screen->widgetset);
570 config_screen_layout_widgets(screen);
571 widgetset_post(screen->widgetset);
574 static void config_screen_boot_order_change(void *arg, int value)
577 struct config_screen *screen = arg;
578 widgetset_unpost(screen->widgetset);
579 config_screen_layout_widgets(screen);
580 widgetset_post(screen->widgetset);
583 static void config_screen_add_device(void *arg)
585 struct config_screen *screen = arg;
587 screen->show_subset = true;
588 cui_show_subset(screen->cui, _("Select a boot device to add"),
589 screen->widgets.boot_order_f);
592 static void config_screen_autoboot_none(void *arg)
594 struct config_screen *screen = arg;
595 struct nc_widget_subset *subset = screen->widgets.boot_order_f;
597 widget_subset_clear_active(subset);
598 screen->autoboot_enabled = false;
600 widgetset_unpost(screen->widgetset);
601 config_screen_layout_widgets(screen);
602 widgetset_post(screen->widgetset);
605 static void config_screen_autoboot_any(void *arg)
607 struct config_screen *screen = arg;
608 const struct system_info *sysinfo = screen->cui->sysinfo;
609 struct nc_widget_subset *subset = screen->widgets.boot_order_f;
612 widget_subset_clear_active(subset);
614 idx = sysinfo->n_blockdevs + sysinfo->n_interfaces + DEVICE_TYPE_ANY;
616 widget_subset_make_active(screen->widgets.boot_order_f, idx);
618 screen->autoboot_enabled = true;
620 widgetset_unpost(screen->widgetset);
621 config_screen_layout_widgets(screen);
622 widgetset_post(screen->widgetset);
625 static void config_screen_update_subset(void *arg,
626 struct nc_widget_subset *subset, int idx)
628 struct config_screen *screen = arg;
631 widget_subset_make_active(subset, idx);
632 if (!screen->autoboot_enabled)
633 screen->autoboot_enabled = true;
634 config_screen_layout_widgets(screen);
637 static struct interface_config *first_active_interface(
638 const struct config *config)
642 for (i = 0; i < config->network.n_interfaces; i++) {
643 if (config->network.interfaces[i]->ignore)
645 return config->network.interfaces[i];
650 static enum net_conf_type find_net_conf_type(const struct config *config)
652 struct interface_config *ifcfg;
654 ifcfg = first_active_interface(config);
657 return NET_CONF_TYPE_DHCP_ALL;
659 else if (ifcfg->method == CONFIG_METHOD_DHCP)
660 return NET_CONF_TYPE_DHCP_ONE;
662 else if (ifcfg->method == CONFIG_METHOD_STATIC)
663 return NET_CONF_TYPE_STATIC;
666 return NET_CONF_TYPE_DHCP_ALL;
669 static void config_screen_setup_empty(struct config_screen *screen)
671 widget_new_label(screen->widgetset, 2, screen->field_x,
672 _("Waiting for configuration data..."));
673 screen->widgets.cancel_b = widget_new_button(screen->widgetset,
674 4, screen->field_x, 9, _("Cancel"),
675 cancel_click, screen);
678 static int find_autoboot_idx(const struct system_info *sysinfo,
679 struct autoboot_option *opt)
683 if (opt->boot_type == BOOT_DEVICE_TYPE)
684 return sysinfo->n_blockdevs + sysinfo->n_interfaces + opt->type;
686 for (i = 0; i < sysinfo->n_blockdevs; i++) {
687 if (!strcmp(sysinfo->blockdevs[i]->uuid, opt->uuid))
691 for (i = 0; i < sysinfo->n_interfaces; i++) {
692 struct interface_info *info = sysinfo->interfaces[i];
695 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
697 if (!strcmp(mac, opt->uuid))
698 return sysinfo->n_blockdevs + i;
704 static void config_screen_setup_widgets(struct config_screen *screen,
705 const struct config *config,
706 const struct system_info *sysinfo)
708 struct nc_widgetset *set = screen->widgetset;
709 struct interface_config *ifcfg;
710 char *str, *ip, *mask, *gw;
711 enum net_conf_type type;
714 build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
717 type = screen->net_conf_type;
718 ifcfg = first_active_interface(config);
720 screen->widgets.boot_add_b = widget_new_button(set, 0, 0, 10,
722 config_screen_add_device, screen);
724 screen->widgets.boot_none_b = widget_new_button(set, 0, 0, 10,
726 config_screen_autoboot_none, screen);
728 screen->widgets.boot_any_b = widget_new_button(set, 0, 0, 16,
729 _("Clear & Boot Any"),
730 config_screen_autoboot_any, screen);
732 screen->widgets.boot_order_l = widget_new_label(set, 0, 0,
734 screen->widgets.boot_order_f = widget_new_subset(set, 0, 0,
735 COLS - screen->field_x,
736 config_screen_update_subset);
737 screen->widgets.boot_empty_l = widget_new_label(set, 0, 0,
740 widget_subset_on_change(screen->widgets.boot_order_f,
741 config_screen_boot_order_change, screen);
743 for (i = 0; i < sysinfo->n_blockdevs; i++) {
744 struct blockdev_info *bd = sysinfo->blockdevs[i];
747 label = talloc_asprintf(screen, _("disk: %s [uuid: %s]"),
750 widget_subset_add_option(screen->widgets.boot_order_f, label);
753 for (i = 0; i < sysinfo->n_interfaces; i++) {
754 struct interface_info *info = sysinfo->interfaces[i];
755 char *label, mac[20];
757 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
759 label = talloc_asprintf(screen, _("net: %s [mac: %s]"),
762 widget_subset_add_option(screen->widgets.boot_order_f, label);
765 for (i = DEVICE_TYPE_NETWORK; i < DEVICE_TYPE_NETWORK + 4; i++) {
768 if (i == DEVICE_TYPE_ANY)
769 label = talloc_asprintf(screen, _("Any Device"));
771 label = talloc_asprintf(screen, _("Any %s device"),
772 device_type_display_name(i));
774 widget_subset_add_option(screen->widgets.boot_order_f, label);
777 screen->autoboot_enabled = config->n_autoboot_opts;
778 for (i = 0; i < config->n_autoboot_opts; i++) {
779 struct autoboot_option *opt = &config->autoboot_opts[i];
782 idx = find_autoboot_idx(sysinfo, opt);
785 widget_subset_make_active(screen->widgets.boot_order_f,
788 if (opt->boot_type == BOOT_DEVICE_TYPE)
789 pb_log("%s: Unknown autoboot option: %d\n",
790 __func__, opt->type);
792 pb_log("%s: Unknown autoboot UUID: %s\n",
793 __func__, opt->uuid);
798 str = talloc_asprintf(screen, "%d", config->autoboot_timeout_sec);
799 screen->widgets.timeout_l = widget_new_label(set, 0, 0, _("Timeout:"));
800 screen->widgets.timeout_f = widget_new_textbox(set, 0, 0, 5, str);
801 screen->widgets.timeout_help_l = widget_new_label(set, 0, 0,
804 widget_textbox_set_fixed_size(screen->widgets.timeout_f);
805 widget_textbox_set_validator_integer(screen->widgets.timeout_f, 0, 999);
807 if (config->ipmi_bootdev) {
808 char *label = talloc_asprintf(screen,
809 _("%s IPMI boot option: %s"),
810 config->ipmi_bootdev_persistent ?
811 "Persistent" : "Temporary",
812 ipmi_bootdev_display_name(config->ipmi_bootdev));
813 screen->widgets.ipmi_type_l = widget_new_label(set, 0, 0,
815 screen->widgets.ipmi_clear_l = widget_new_label(set, 0, 0,
817 screen->widgets.ipmi_clear_cb = widget_new_checkbox(set, 0, 0,
819 screen->ipmi_override = true;
822 screen->widgets.network_l = widget_new_label(set, 0, 0, _("Network:"));
823 screen->widgets.network_f = widget_new_select(set, 0, 0, 50);
825 widget_select_add_option(screen->widgets.network_f,
826 NET_CONF_TYPE_DHCP_ALL,
827 _("DHCP on all active interfaces"),
828 type == NET_CONF_TYPE_DHCP_ALL);
829 widget_select_add_option(screen->widgets.network_f,
830 NET_CONF_TYPE_DHCP_ONE,
831 _("DHCP on a specific interface"),
832 type == NET_CONF_TYPE_DHCP_ONE);
833 widget_select_add_option(screen->widgets.network_f,
834 NET_CONF_TYPE_STATIC,
835 _("Static IP configuration"),
836 type == NET_CONF_TYPE_STATIC);
838 widget_select_on_change(screen->widgets.network_f,
839 config_screen_network_change, screen);
841 screen->widgets.iface_l = widget_new_label(set, 0, 0, _("Device:"));
842 screen->widgets.iface_f = widget_new_select(set, 0, 0, 50);
844 for (i = 0; i < sysinfo->n_interfaces; i++) {
845 struct interface_info *info = sysinfo->interfaces[i];
846 char str[50], mac[20];
849 is_default = ifcfg && !memcmp(ifcfg->hwaddr, info->hwaddr,
850 sizeof(ifcfg->hwaddr));
852 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
853 snprintf(str, sizeof(str), "%s [%s, %s]", info->name, mac,
854 info->link ? _("link up") : _("link down"));
856 widget_select_add_option(screen->widgets.iface_f,
860 gw = ip = mask = NULL;
861 if (ifcfg && ifcfg->method == CONFIG_METHOD_STATIC) {
864 str = talloc_strdup(screen, ifcfg->static_config.address);
865 sep = strchr(str, '/');
872 gw = ifcfg->static_config.gateway;
875 screen->widgets.ip_addr_l = widget_new_label(set, 0, 0, _("IP/mask:"));
876 screen->widgets.ip_addr_f = widget_new_textbox(set, 0, 0, 16, ip);
877 screen->widgets.ip_mask_l = widget_new_label(set, 0, 0, "/");
878 screen->widgets.ip_mask_f = widget_new_textbox(set, 0, 0, 3, mask);
879 screen->widgets.ip_addr_mask_help_l =
880 widget_new_label(set, 0, 0, _("(eg. 192.168.0.10 / 24)"));
882 widget_textbox_set_fixed_size(screen->widgets.ip_addr_f);
883 widget_textbox_set_fixed_size(screen->widgets.ip_mask_f);
884 widget_textbox_set_validator_ipv4(screen->widgets.ip_addr_f);
885 widget_textbox_set_validator_integer(screen->widgets.ip_mask_f, 1, 31);
887 screen->widgets.gateway_l = widget_new_label(set, 0, 0, _("Gateway:"));
888 screen->widgets.gateway_f = widget_new_textbox(set, 0, 0, 16, gw);
889 screen->widgets.gateway_help_l =
890 widget_new_label(set, 0, 0, _("(eg. 192.168.0.1)"));
892 widget_textbox_set_fixed_size(screen->widgets.gateway_f);
893 widget_textbox_set_validator_ipv4(screen->widgets.gateway_f);
895 str = talloc_strdup(screen, "");
896 for (i = 0; i < config->network.n_dns_servers; i++) {
897 str = talloc_asprintf_append(str, "%s%s",
899 config->network.dns_servers[i]);
902 screen->widgets.dns_l = widget_new_label(set, 0, 0,
903 _("DNS Server(s):"));
904 screen->widgets.dns_f = widget_new_textbox(set, 0, 0, 32, str);
905 screen->widgets.dns_help_l =
906 widget_new_label(set, 0, 0, _("(eg. 192.168.0.2)"));
908 widget_textbox_set_validator_ipv4_multi(screen->widgets.dns_f);
910 screen->widgets.dns_dhcp_help_l = widget_new_label(set, 0, 0,
911 _("(if not provided by DHCP server)"));
913 if (config->safe_mode)
914 screen->widgets.safe_mode = widget_new_label(set, 0, 0,
915 _("Selecting 'OK' will exit safe mode"));
917 screen->widgets.allow_write_l = widget_new_label(set, 0, 0,
919 screen->widgets.allow_write_f = widget_new_select(set, 0, 0,
920 COLS - screen->field_x);
922 widget_select_add_option(screen->widgets.allow_write_f, 0,
923 _("Prevent all writes to disk"),
924 !config->allow_writes);
925 widget_select_add_option(screen->widgets.allow_write_f, 1,
926 _("Allow bootloader scripts to modify disks"),
927 config->allow_writes);
929 screen->widgets.ok_b = widget_new_button(set, 0, 0, 6, _("OK"),
931 screen->widgets.help_b = widget_new_button(set, 0, 0, 10, _("Help"),
933 screen->widgets.cancel_b = widget_new_button(set, 0, 0, 10, _("Cancel"),
934 cancel_click, screen);
937 static void config_screen_widget_focus(struct nc_widget *widget, void *arg)
939 struct config_screen *screen = arg;
942 w_y = widget_y(widget) + widget_focus_y(widget);
943 s_max = getmaxy(screen->scr.sub_ncw) - 1;
945 if (w_y < screen->scroll_y)
946 screen->scroll_y = w_y;
948 else if (w_y + screen->scroll_y + 1 > s_max)
949 screen->scroll_y = 1 + w_y - s_max;
957 static void config_screen_draw(struct config_screen *screen,
958 const struct config *config,
959 const struct system_info *sysinfo)
964 /* The size of the pad we'll need depends on the number of interfaces.
966 * We use N_FIELDS (which is quite conservative, as some fields share
967 * a line) as a base, then:
968 * - add 6 (as the network select & boot device select fields take 3
970 * - add n_interfaces for every field in the network select field, and
971 * - add (n_blockdevs + n_interfaces) for every field in the boot device
974 height = N_FIELDS + 6;
976 height += sysinfo->n_interfaces;
977 height += (sysinfo->n_blockdevs + sysinfo->n_interfaces);
979 if (!screen->pad || getmaxy(screen->pad) < height) {
982 screen->pad = newpad(height, COLS + 10);
985 if (screen->widgetset) {
986 widgetset_unpost(screen->widgetset);
987 talloc_free(screen->widgetset);
991 screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
993 widgetset_set_widget_focus(screen->widgetset,
994 config_screen_widget_focus, screen);
996 if (!config || !sysinfo) {
997 config_screen_setup_empty(screen);
999 screen->net_conf_type = find_net_conf_type(config);
1000 config_screen_setup_widgets(screen, config, sysinfo);
1001 config_screen_layout_widgets(screen);
1005 widgetset_post(screen->widgetset);
1008 void config_screen_update(struct config_screen *screen,
1009 const struct config *config,
1010 const struct system_info *sysinfo)
1012 config_screen_draw(screen, config, sysinfo);
1013 pad_refresh(screen);
1016 static int config_screen_destroy(void *arg)
1018 struct config_screen *screen = arg;
1020 delwin(screen->pad);
1024 struct config_screen *config_screen_init(struct cui *cui,
1025 const struct config *config,
1026 const struct system_info *sysinfo,
1027 void (*on_exit)(struct cui *))
1029 struct config_screen *screen;
1031 screen = talloc_zero(cui, struct config_screen);
1032 talloc_set_destructor(screen, config_screen_destroy);
1033 nc_scr_init(&screen->scr, pb_config_screen_sig, 0,
1034 cui, config_screen_process_key,
1035 config_screen_post, config_screen_unpost,
1036 config_screen_resize);
1039 screen->on_exit = on_exit;
1040 screen->need_redraw = false;
1041 screen->label_x = 2;
1042 screen->field_x = 17;
1044 screen->ipmi_override = false;
1045 screen->show_subset = false;
1047 screen->scr.frame.ltitle = talloc_strdup(screen,
1048 _("Petitboot System Configuration"));
1049 screen->scr.frame.rtitle = NULL;
1050 screen->scr.frame.help = talloc_strdup(screen,
1051 _("tab=next, shift+tab=previous, x=exit, h=help"));
1052 nc_scr_frame_draw(&screen->scr);
1054 scrollok(screen->scr.sub_ncw, true);
1056 config_screen_draw(screen, config, sysinfo);