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;
58 void (*on_exit)(struct cui *);
66 enum net_conf_type net_conf_type;
68 bool autoboot_enabled;
72 struct nc_widget_label *autoboot_l;
73 struct nc_widget_select *autoboot_f;
74 struct nc_widget_label *boot_order_l;
75 struct nc_widget_subset *boot_order_f;
76 struct nc_widget_label *boot_empty_l;
77 struct nc_widget_button *boot_add_b;
78 struct nc_widget_button *boot_none_b;
79 struct nc_widget_button *boot_any_b;
80 struct nc_widget_textbox *timeout_f;
81 struct nc_widget_label *timeout_l;
82 struct nc_widget_label *timeout_help_l;
84 struct nc_widget_label *ipmi_type_l;
85 struct nc_widget_label *ipmi_clear_l;
86 struct nc_widget_checkbox *ipmi_clear_cb;
88 struct nc_widget_label *network_l;
89 struct nc_widget_select *network_f;
91 struct nc_widget_label *iface_l;
92 struct nc_widget_select *iface_f;
93 struct nc_widget_label *ip_addr_l;
94 struct nc_widget_textbox *ip_addr_f;
95 struct nc_widget_label *ip_mask_l;
96 struct nc_widget_textbox *ip_mask_f;
97 struct nc_widget_label *ip_addr_mask_help_l;
98 struct nc_widget_label *gateway_l;
99 struct nc_widget_textbox *gateway_f;
100 struct nc_widget_label *gateway_help_l;
101 struct nc_widget_label *url_l;
102 struct nc_widget_textbox *url_f;
103 struct nc_widget_label *url_help_l;
104 struct nc_widget_label *dns_l;
105 struct nc_widget_textbox *dns_f;
106 struct nc_widget_label *dns_dhcp_help_l;
107 struct nc_widget_label *dns_help_l;
109 struct nc_widget_label *allow_write_l;
110 struct nc_widget_select *allow_write_f;
112 struct nc_widget_label *safe_mode;
113 struct nc_widget_button *ok_b;
114 struct nc_widget_button *help_b;
115 struct nc_widget_button *cancel_b;
119 static struct config_screen *config_screen_from_scr(struct nc_scr *scr)
121 struct config_screen *config_screen;
123 assert(scr->sig == pb_config_screen_sig);
124 config_screen = (struct config_screen *)
125 ((char *)scr - (size_t)&((struct config_screen *)0)->scr);
126 assert(config_screen->scr.sig == pb_config_screen_sig);
127 return config_screen;
130 static void pad_refresh(struct config_screen *screen)
132 int y, x, rows, cols;
134 getmaxyx(screen->scr.sub_ncw, rows, cols);
135 getbegyx(screen->scr.sub_ncw, y, x);
137 prefresh(screen->pad, screen->scroll_y, 0, y, x, rows, cols);
140 static void config_screen_process_key(struct nc_scr *scr, int key)
142 struct config_screen *screen = config_screen_from_scr(scr);
145 handled = widgetset_process_key(screen->widgetset, key);
154 screen->show_help = true;
160 screen->on_exit(screen->cui);
162 } else if (screen->show_help) {
163 screen->show_help = false;
164 screen->need_redraw = true;
165 cui_show_help(screen->cui, _("System Configuration"),
168 } else if (handled && !screen->show_subset) {
173 static void config_screen_resize(struct nc_scr *scr)
175 struct config_screen *screen = config_screen_from_scr(scr);
179 static int config_screen_unpost(struct nc_scr *scr)
181 struct config_screen *screen = config_screen_from_scr(scr);
182 widgetset_unpost(screen->widgetset);
186 struct nc_scr *config_screen_scr(struct config_screen *screen)
191 static int screen_process_form(struct config_screen *screen)
193 const struct system_info *sysinfo = screen->cui->sysinfo;
194 enum net_conf_type net_conf_type;
195 struct interface_config *iface;
196 bool allow_write, autoboot;
198 struct config *config;
199 int i, n_boot_opts, rc, idx;
203 config = config_copy(screen, screen->cui->config);
205 talloc_free(config->autoboot_opts);
206 config->n_autoboot_opts = 0;
208 n_boot_opts = widget_subset_get_order(config, &order,
209 screen->widgets.boot_order_f);
211 autoboot = widget_select_get_value(screen->widgets.autoboot_f);
212 config->autoboot_enabled = autoboot || (autoboot && n_boot_opts);
214 config->n_autoboot_opts = n_boot_opts;
215 config->autoboot_opts = talloc_array(config, struct autoboot_option,
218 for (i = 0; i < n_boot_opts; i++) {
219 if (order[i] < sysinfo->n_blockdevs) {
221 config->autoboot_opts[i].boot_type = BOOT_DEVICE_UUID;
222 config->autoboot_opts[i].uuid = talloc_strdup(config,
223 sysinfo->blockdevs[order[i]]->uuid);
224 } else if(order[i] < (sysinfo->n_blockdevs + sysinfo->n_interfaces)) {
226 order[i] -= sysinfo->n_blockdevs;
227 config->autoboot_opts[i].boot_type = BOOT_DEVICE_UUID;
228 mac_str(sysinfo->interfaces[order[i]]->hwaddr,
229 sysinfo->interfaces[order[i]]->hwaddr_size,
231 config->autoboot_opts[i].uuid = talloc_strdup(config, mac);
234 order[i] -= (sysinfo->n_blockdevs + sysinfo->n_interfaces);
235 config->autoboot_opts[i].boot_type = BOOT_DEVICE_TYPE;
236 config->autoboot_opts[i].type = order[i];
240 str = widget_textbox_get_value(screen->widgets.timeout_f);
244 x = strtoul(str, &end, 10);
245 if (!errno && end != str)
246 config->autoboot_timeout_sec = x;
249 if (screen->ipmi_override)
250 if (widget_checkbox_get_value(screen->widgets.ipmi_clear_cb))
251 config->ipmi_bootdev = IPMI_BOOTDEV_INVALID;
254 net_conf_type = widget_select_get_value(screen->widgets.network_f);
256 /* if we don't have any network interfaces, prevent per-interface
258 if (sysinfo->n_interfaces == 0)
259 net_conf_type = NET_CONF_TYPE_DHCP_ALL;
261 if (net_conf_type == NET_CONF_TYPE_DHCP_ALL) {
262 config->network.n_interfaces = 0;
265 iface = talloc_zero(config, struct interface_config);
266 config->network.n_interfaces = 1;
267 config->network.interfaces = talloc_array(config,
268 struct interface_config *, 1);
269 config->network.interfaces[0] = iface;
271 /* copy hwaddr (from the sysinfo interface data) to
272 * the configuration */
273 idx = widget_select_get_value(screen->widgets.iface_f);
274 memcpy(iface->hwaddr, sysinfo->interfaces[idx]->hwaddr,
275 sizeof(iface->hwaddr));
278 if (net_conf_type == NET_CONF_TYPE_DHCP_ONE) {
279 iface->method = CONFIG_METHOD_DHCP;
282 if (net_conf_type == NET_CONF_TYPE_STATIC) {
283 char *ip, *mask, *gateway, *url;
285 ip = widget_textbox_get_value(screen->widgets.ip_addr_f);
286 mask = widget_textbox_get_value(screen->widgets.ip_mask_f);
287 gateway = widget_textbox_get_value(screen->widgets.gateway_f);
288 url = widget_textbox_get_value(screen->widgets.url_f);
290 if (!ip || !*ip || !mask || !*mask) {
291 screen->scr.frame.status =
292 _("No IP / mask values are set");
293 nc_scr_frame_draw(&screen->scr);
298 iface->method = CONFIG_METHOD_STATIC;
299 iface->static_config.address = talloc_asprintf(iface, "%s/%s",
301 iface->static_config.gateway = talloc_strdup(iface, gateway);
302 iface->static_config.url = talloc_strdup(iface, url);
305 str = widget_textbox_get_value(screen->widgets.dns_f);
306 talloc_free(config->network.dns_servers);
307 config->network.dns_servers = NULL;
308 config->network.n_dns_servers = 0;
310 if (str && strlen(str)) {
315 dns = strtok_r(str, " \t", &tmp);
320 i = config->network.n_dns_servers++;
321 config->network.dns_servers = talloc_realloc(config,
322 config->network.dns_servers,
324 config->network.n_dns_servers);
325 config->network.dns_servers[i] =
326 talloc_strdup(config, dns);
332 allow_write = widget_select_get_value(screen->widgets.allow_write_f);
333 if (allow_write != config->allow_writes)
334 config->allow_writes = allow_write;
336 config->safe_mode = false;
337 rc = cui_send_config(screen->cui, config);
341 pb_log("cui_send_config failed!\n");
343 pb_debug("config sent!\n");
348 static void ok_click(void *arg)
350 struct config_screen *screen = arg;
351 if (screen_process_form(screen))
352 /* errors are written to the status line, so we'll need
354 wrefresh(screen->scr.main_ncw);
359 static void help_click(void *arg)
361 struct config_screen *screen = arg;
362 screen->show_help = true;
365 static void cancel_click(void *arg)
367 struct config_screen *screen = arg;
371 static int layout_pair(struct config_screen *screen, int y,
372 struct nc_widget_label *label,
373 struct nc_widget *field)
375 struct nc_widget *label_w = widget_label_base(label);
376 widget_move(label_w, y, screen->label_x);
377 widget_move(field, y, screen->field_x);
378 return max(widget_height(label_w), widget_height(field));
381 static void config_screen_layout_widgets(struct config_screen *screen)
383 struct nc_widget *wl, *wf, *wh;
388 /* currently, the longest label we have is the DNS-servers
389 * widget, so layout our screen based on that */
390 help_x = screen->field_x + 2 +
391 widget_width(widget_textbox_base(screen->widgets.dns_f));
393 wl = widget_label_base(screen->widgets.autoboot_l);
394 widget_set_visible(wl, true);
395 widget_move(wl, y, screen->label_x);
397 wf = widget_select_base(screen->widgets.autoboot_f);
398 widget_move(wf, y, screen->field_x);
399 y += widget_height(wf);
401 show = screen->autoboot_enabled;
406 wl = widget_label_base(screen->widgets.boot_order_l);
407 widget_set_visible(wl, show);
408 widget_move(wl, y, screen->label_x);
410 wf = widget_subset_base(screen->widgets.boot_order_f);
411 widget_move(wf, y, screen->field_x);
412 wl = widget_label_base(screen->widgets.boot_empty_l);
413 widget_move(wl, y, screen->field_x);
415 if (widget_subset_height(screen->widgets.boot_order_f)) {
416 widget_set_visible(wl, false);
417 widget_set_visible(wf, show);
418 y += show ? widget_height(wf) : 0;
420 widget_set_visible(wl, show);
421 widget_set_visible(wf, false);
427 widget_move(widget_button_base(screen->widgets.boot_add_b),
428 y++, screen->field_x);
429 widget_move(widget_button_base(screen->widgets.boot_any_b),
430 y++, screen->field_x);
431 widget_move(widget_button_base(screen->widgets.boot_none_b),
435 wf = widget_button_base(screen->widgets.boot_add_b);
436 if (widget_subset_n_inactive(screen->widgets.boot_order_f) && show)
437 widget_set_visible(wf, true);
439 widget_set_visible(wf, false);
444 widget_set_visible(widget_button_base(screen->widgets.boot_any_b), show);
445 widget_set_visible(widget_button_base(screen->widgets.boot_none_b), show);
447 wf = widget_textbox_base(screen->widgets.timeout_f);
448 wl = widget_label_base(screen->widgets.timeout_l);
449 wh = widget_label_base(screen->widgets.timeout_help_l);
450 widget_set_visible(wl, screen->autoboot_enabled);
451 widget_set_visible(wf, screen->autoboot_enabled);
452 widget_set_visible(wh, screen->autoboot_enabled);
453 if (screen->autoboot_enabled) {
454 widget_set_visible(wh, screen->autoboot_enabled);
455 widget_move(wl, y, screen->label_x);
456 widget_move(wf, y, screen->field_x);
457 widget_move(wh, y, screen->field_x + widget_width(wf) + 1);
461 if (screen->ipmi_override) {
462 wl = widget_label_base(screen->widgets.ipmi_type_l);
463 widget_set_visible(wl, true);
464 widget_move(wl, y, screen->label_x);
467 wl = widget_label_base(screen->widgets.ipmi_clear_l);
468 wf = widget_checkbox_base(screen->widgets.ipmi_clear_cb);
469 widget_set_visible(wl, true);
470 widget_set_visible(wf, true);
471 widget_move(wl, y, screen->label_x);
472 widget_move(wf, y, screen->field_x);
478 y += layout_pair(screen, y, screen->widgets.network_l,
479 widget_select_base(screen->widgets.network_f));
483 /* conditionally show iface select */
484 wl = widget_label_base(screen->widgets.iface_l);
485 wf = widget_select_base(screen->widgets.iface_f);
487 show = screen->net_conf_type == NET_CONF_TYPE_DHCP_ONE ||
488 screen->net_conf_type == NET_CONF_TYPE_STATIC;
490 widget_set_visible(wl, show);
491 widget_set_visible(wf, show);
494 y += layout_pair(screen, y, screen->widgets.iface_l, wf) + 1;
496 /* conditionally show static IP params */
497 show = screen->net_conf_type == NET_CONF_TYPE_STATIC;
499 wl = widget_label_base(screen->widgets.ip_addr_l);
500 wf = widget_textbox_base(screen->widgets.ip_addr_f);
501 widget_set_visible(wl, show);
502 widget_set_visible(wf, show);
503 x = screen->field_x + widget_width(wf) + 1;
506 layout_pair(screen, y, screen->widgets.ip_addr_l, wf);
508 wl = widget_label_base(screen->widgets.ip_mask_l);
509 wf = widget_textbox_base(screen->widgets.ip_mask_f);
510 widget_set_visible(wl, show);
511 widget_set_visible(wf, show);
514 widget_move(wl, y, x);
515 widget_move(wf, y, x + 2);
518 /* help for IP/mask */
519 wh = widget_label_base(screen->widgets.ip_addr_mask_help_l);
520 widget_set_visible(wh, show);
522 widget_move(wh, y, help_x);
526 wl = widget_label_base(screen->widgets.gateway_l);
527 wf = widget_textbox_base(screen->widgets.gateway_f);
528 wh = widget_label_base(screen->widgets.gateway_help_l);
529 widget_set_visible(wl, show);
530 widget_set_visible(wf, show);
531 widget_set_visible(wh, show);
534 layout_pair(screen, y, screen->widgets.gateway_l, wf);
535 widget_move(wh, y, help_x);
539 wl = widget_label_base(screen->widgets.url_l);
540 wf = widget_textbox_base(screen->widgets.url_f);
541 wh = widget_label_base(screen->widgets.url_help_l);
542 widget_set_visible(wl, show);
543 widget_set_visible(wf, show);
544 widget_set_visible(wh, show);
547 layout_pair(screen, y, screen->widgets.url_l, wf);
548 widget_move(wh, y, help_x);
552 wh = widget_label_base(screen->widgets.dns_help_l);
553 layout_pair(screen, y, screen->widgets.dns_l,
554 widget_textbox_base(screen->widgets.dns_f));
555 widget_move(wh, y, help_x);
558 /* we show the DNS/DHCP help if we're configuring DHCP */
559 show = screen->net_conf_type != NET_CONF_TYPE_STATIC;
560 wl = widget_label_base(screen->widgets.dns_dhcp_help_l);
561 widget_set_visible(wl, show);
563 widget_move(wl, y, screen->field_x);
569 show = screen->cui->config->safe_mode;
571 widget_move(widget_label_base(screen->widgets.safe_mode),
576 layout_pair(screen, y, screen->widgets.allow_write_l,
577 widget_select_base(screen->widgets.allow_write_f));
578 y += widget_height(widget_select_base(screen->widgets.allow_write_f));
582 widget_move(widget_button_base(screen->widgets.ok_b),
584 widget_move(widget_button_base(screen->widgets.help_b),
585 y, screen->field_x + 14);
586 widget_move(widget_button_base(screen->widgets.cancel_b),
587 y, screen->field_x + 28);
590 static void config_screen_network_change(void *arg, int value)
592 struct config_screen *screen = arg;
593 screen->net_conf_type = value;
594 widgetset_unpost(screen->widgetset);
595 config_screen_layout_widgets(screen);
596 widgetset_post(screen->widgetset);
599 static void config_screen_boot_order_change(void *arg, int value)
602 struct config_screen *screen = arg;
603 widgetset_unpost(screen->widgetset);
604 config_screen_layout_widgets(screen);
605 widgetset_post(screen->widgetset);
608 static void config_screen_autoboot_change(void *arg, int value)
610 struct config_screen *screen = arg;
611 screen->autoboot_enabled = !!value;
612 widgetset_unpost(screen->widgetset);
613 config_screen_layout_widgets(screen);
614 widgetset_post(screen->widgetset);
617 static void config_screen_add_device(void *arg)
619 struct config_screen *screen = arg;
621 screen->show_subset = true;
622 cui_show_subset(screen->cui, _("Select a boot device to add"),
623 screen->widgets.boot_order_f);
626 static void config_screen_autoboot_none(void *arg)
628 struct config_screen *screen = arg;
629 struct nc_widget_subset *subset = screen->widgets.boot_order_f;
631 widget_subset_clear_active(subset);
632 screen->autoboot_enabled = false;
634 widgetset_unpost(screen->widgetset);
635 config_screen_layout_widgets(screen);
636 widgetset_post(screen->widgetset);
639 static void config_screen_autoboot_any(void *arg)
641 struct config_screen *screen = arg;
642 const struct system_info *sysinfo = screen->cui->sysinfo;
643 struct nc_widget_subset *subset = screen->widgets.boot_order_f;
646 widget_subset_clear_active(subset);
648 idx = sysinfo->n_blockdevs + sysinfo->n_interfaces + DEVICE_TYPE_ANY;
650 widget_subset_make_active(screen->widgets.boot_order_f, idx);
652 screen->autoboot_enabled = true;
654 widgetset_unpost(screen->widgetset);
655 config_screen_layout_widgets(screen);
656 widgetset_post(screen->widgetset);
659 static void config_screen_update_subset(void *arg,
660 struct nc_widget_subset *subset, int idx)
662 struct config_screen *screen = arg;
665 widget_subset_make_active(subset, idx);
666 if (!screen->autoboot_enabled)
667 screen->autoboot_enabled = true;
668 config_screen_layout_widgets(screen);
671 static struct interface_config *first_active_interface(
672 const struct config *config)
676 for (i = 0; i < config->network.n_interfaces; i++) {
677 if (config->network.interfaces[i]->ignore)
679 return config->network.interfaces[i];
684 static enum net_conf_type find_net_conf_type(const struct config *config)
686 struct interface_config *ifcfg;
688 ifcfg = first_active_interface(config);
691 return NET_CONF_TYPE_DHCP_ALL;
693 else if (ifcfg->method == CONFIG_METHOD_DHCP)
694 return NET_CONF_TYPE_DHCP_ONE;
696 else if (ifcfg->method == CONFIG_METHOD_STATIC)
697 return NET_CONF_TYPE_STATIC;
700 return NET_CONF_TYPE_DHCP_ALL;
703 static void config_screen_setup_empty(struct config_screen *screen)
705 widget_new_label(screen->widgetset, 2, screen->field_x,
706 _("Waiting for configuration data..."));
707 screen->widgets.cancel_b = widget_new_button(screen->widgetset,
708 4, screen->field_x, 9, _("Cancel"),
709 cancel_click, screen);
712 static int find_autoboot_idx(const struct system_info *sysinfo,
713 struct autoboot_option *opt)
717 if (opt->boot_type == BOOT_DEVICE_TYPE)
718 return sysinfo->n_blockdevs + sysinfo->n_interfaces + opt->type;
720 for (i = 0; i < sysinfo->n_blockdevs; i++) {
721 if (!strcmp(sysinfo->blockdevs[i]->uuid, opt->uuid))
725 for (i = 0; i < sysinfo->n_interfaces; i++) {
726 struct interface_info *info = sysinfo->interfaces[i];
729 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
731 if (!strcmp(mac, opt->uuid))
732 return sysinfo->n_blockdevs + i;
738 static void config_screen_setup_widgets(struct config_screen *screen,
739 const struct config *config,
740 const struct system_info *sysinfo)
742 struct nc_widgetset *set = screen->widgetset;
743 struct interface_config *ifcfg;
744 char *str, *ip, *mask, *gw, *url;
745 enum net_conf_type type;
747 int add_len, clear_len, any_len, min_len = 20;
749 build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
752 type = screen->net_conf_type;
753 ifcfg = first_active_interface(config);
755 screen->autoboot_enabled = config->autoboot_enabled;
757 screen->widgets.autoboot_l = widget_new_label(set, 0, 0,
759 screen->widgets.autoboot_f = widget_new_select(set, 0, 0,
760 COLS - screen->field_x - 1);
762 widget_select_add_option(screen->widgets.autoboot_f, 0, _("Disabled"),
763 !screen->autoboot_enabled);
764 widget_select_add_option(screen->widgets.autoboot_f, 1, _("Enabled"),
765 screen->autoboot_enabled);
767 widget_select_on_change(screen->widgets.autoboot_f,
768 config_screen_autoboot_change, screen);
770 add_len = max(min_len, strncols(_("Add Device")));
771 clear_len = max(min_len, strncols(_("Clear")));
772 any_len = max(min_len, strncols(_("Clear & Boot Any")));
774 screen->widgets.boot_add_b = widget_new_button(set, 0, 0, add_len,
776 config_screen_add_device, screen);
778 screen->widgets.boot_none_b = widget_new_button(set, 0, 0, clear_len,
780 config_screen_autoboot_none, screen);
782 screen->widgets.boot_any_b = widget_new_button(set, 0, 0, any_len,
783 _("Clear & Boot Any"),
784 config_screen_autoboot_any, screen);
786 screen->widgets.boot_order_l = widget_new_label(set, 0, 0,
788 screen->widgets.boot_order_f = widget_new_subset(set, 0, 0,
789 COLS - screen->field_x,
790 config_screen_update_subset);
791 screen->widgets.boot_empty_l = widget_new_label(set, 0, 0,
794 widget_subset_on_change(screen->widgets.boot_order_f,
795 config_screen_boot_order_change, screen);
797 for (i = 0; i < sysinfo->n_blockdevs; i++) {
798 struct blockdev_info *bd = sysinfo->blockdevs[i];
801 label = talloc_asprintf(screen, _("disk: %s [uuid: %s]"),
804 widget_subset_add_option(screen->widgets.boot_order_f, label);
807 for (i = 0; i < sysinfo->n_interfaces; i++) {
808 struct interface_info *info = sysinfo->interfaces[i];
809 char *label, mac[20];
811 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
813 label = talloc_asprintf(screen, _("net: %s [mac: %s]"),
816 widget_subset_add_option(screen->widgets.boot_order_f, label);
819 for (i = DEVICE_TYPE_NETWORK; i < DEVICE_TYPE_UNKNOWN; i++) {
822 if (i == DEVICE_TYPE_ANY)
823 label = talloc_asprintf(screen, _("Any Device"));
825 label = talloc_asprintf(screen, _("Any %s device"),
826 device_type_display_name(i));
828 widget_subset_add_option(screen->widgets.boot_order_f, label);
831 for (i = 0; i < config->n_autoboot_opts; i++) {
832 struct autoboot_option *opt = &config->autoboot_opts[i];
835 idx = find_autoboot_idx(sysinfo, opt);
838 widget_subset_make_active(screen->widgets.boot_order_f,
841 if (opt->boot_type == BOOT_DEVICE_TYPE)
842 pb_log("%s: Unknown autoboot option: %d\n",
843 __func__, opt->type);
845 pb_log("%s: Unknown autoboot UUID: %s\n",
846 __func__, opt->uuid);
851 str = talloc_asprintf(screen, "%d", config->autoboot_timeout_sec);
852 screen->widgets.timeout_l = widget_new_label(set, 0, 0, _("Timeout:"));
853 screen->widgets.timeout_f = widget_new_textbox(set, 0, 0, 5, str);
854 screen->widgets.timeout_help_l = widget_new_label(set, 0, 0,
857 widget_textbox_set_fixed_size(screen->widgets.timeout_f);
858 widget_textbox_set_validator_integer(screen->widgets.timeout_f, 0, 999);
860 if (config->ipmi_bootdev) {
861 char *label = talloc_asprintf(screen,
862 _("%s IPMI boot option: %s"),
863 config->ipmi_bootdev_persistent ?
864 "Persistent" : "Temporary",
865 ipmi_bootdev_display_name(config->ipmi_bootdev));
866 screen->widgets.ipmi_type_l = widget_new_label(set, 0, 0,
868 screen->widgets.ipmi_clear_l = widget_new_label(set, 0, 0,
870 screen->widgets.ipmi_clear_cb = widget_new_checkbox(set, 0, 0,
872 screen->ipmi_override = true;
875 screen->widgets.network_l = widget_new_label(set, 0, 0, _("Network:"));
876 screen->widgets.network_f = widget_new_select(set, 0, 0,
877 COLS - screen->field_x - 1);
879 widget_select_add_option(screen->widgets.network_f,
880 NET_CONF_TYPE_DHCP_ALL,
881 _("DHCP on all active interfaces"),
882 type == NET_CONF_TYPE_DHCP_ALL);
883 widget_select_add_option(screen->widgets.network_f,
884 NET_CONF_TYPE_DHCP_ONE,
885 _("DHCP on a specific interface"),
886 type == NET_CONF_TYPE_DHCP_ONE);
887 widget_select_add_option(screen->widgets.network_f,
888 NET_CONF_TYPE_STATIC,
889 _("Static IP configuration"),
890 type == NET_CONF_TYPE_STATIC);
892 widget_select_on_change(screen->widgets.network_f,
893 config_screen_network_change, screen);
895 screen->widgets.iface_l = widget_new_label(set, 0, 0, _("Device:"));
896 screen->widgets.iface_f = widget_new_select(set, 0, 0, 50);
898 for (i = 0; i < sysinfo->n_interfaces; i++) {
899 struct interface_info *info = sysinfo->interfaces[i];
900 char str[50], mac[20];
903 is_default = ifcfg && !memcmp(ifcfg->hwaddr, info->hwaddr,
904 sizeof(ifcfg->hwaddr));
906 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
907 snprintf(str, sizeof(str), "%s [%s, %s]", info->name, mac,
908 info->link ? _("link up") : _("link down"));
910 widget_select_add_option(screen->widgets.iface_f,
914 url = gw = ip = mask = NULL;
915 if (ifcfg && ifcfg->method == CONFIG_METHOD_STATIC) {
918 str = talloc_strdup(screen, ifcfg->static_config.address);
919 sep = strchr(str, '/');
926 gw = ifcfg->static_config.gateway;
927 url = ifcfg->static_config.url;
930 screen->widgets.ip_addr_l = widget_new_label(set, 0, 0, _("IP/mask:"));
931 screen->widgets.ip_addr_f = widget_new_textbox(set, 0, 0, 16, ip);
932 screen->widgets.ip_mask_l = widget_new_label(set, 0, 0, "/");
933 screen->widgets.ip_mask_f = widget_new_textbox(set, 0, 0, 3, mask);
934 screen->widgets.ip_addr_mask_help_l =
935 widget_new_label(set, 0, 0, _("(eg. 192.168.0.10 / 24)"));
937 widget_textbox_set_fixed_size(screen->widgets.ip_addr_f);
938 widget_textbox_set_fixed_size(screen->widgets.ip_mask_f);
939 widget_textbox_set_validator_ipv4(screen->widgets.ip_addr_f);
940 widget_textbox_set_validator_integer(screen->widgets.ip_mask_f, 1, 31);
942 screen->widgets.gateway_l = widget_new_label(set, 0, 0, _("Gateway:"));
943 screen->widgets.gateway_f = widget_new_textbox(set, 0, 0, 16, gw);
944 screen->widgets.gateway_help_l =
945 widget_new_label(set, 0, 0, _("(eg. 192.168.0.1)"));
947 widget_textbox_set_fixed_size(screen->widgets.gateway_f);
948 widget_textbox_set_validator_ipv4(screen->widgets.gateway_f);
950 screen->widgets.url_l = widget_new_label(set, 0, 0, _("URL:"));
951 screen->widgets.url_f = widget_new_textbox(set, 0, 0, 32, url);
952 screen->widgets.url_help_l =
953 widget_new_label(set, 0, 0, _("(eg. tftp://)"));
955 str = talloc_strdup(screen, "");
956 for (i = 0; i < config->network.n_dns_servers; i++) {
957 str = talloc_asprintf_append(str, "%s%s",
959 config->network.dns_servers[i]);
962 screen->widgets.dns_l = widget_new_label(set, 0, 0,
963 _("DNS Server(s):"));
964 screen->widgets.dns_f = widget_new_textbox(set, 0, 0, 32, str);
965 screen->widgets.dns_help_l =
966 widget_new_label(set, 0, 0, _("(eg. 192.168.0.2)"));
968 widget_textbox_set_validator_ipv4_multi(screen->widgets.dns_f);
970 screen->widgets.dns_dhcp_help_l = widget_new_label(set, 0, 0,
971 _("(if not provided by DHCP server)"));
973 if (config->safe_mode)
974 screen->widgets.safe_mode = widget_new_label(set, 0, 0,
975 _("Selecting 'OK' will exit safe mode"));
977 screen->widgets.allow_write_l = widget_new_label(set, 0, 0,
979 screen->widgets.allow_write_f = widget_new_select(set, 0, 0,
980 COLS - screen->field_x - 1);
982 widget_select_add_option(screen->widgets.allow_write_f, 0,
983 _("Prevent all writes to disk"),
984 !config->allow_writes);
986 widget_select_add_option(screen->widgets.allow_write_f, 1,
987 _("Allow bootloader scripts to modify disks"),
988 config->allow_writes);
990 screen->widgets.ok_b = widget_new_button(set, 0, 0, 10, _("OK"),
992 screen->widgets.help_b = widget_new_button(set, 0, 0, 10, _("Help"),
994 screen->widgets.cancel_b = widget_new_button(set, 0, 0, 10, _("Cancel"),
995 cancel_click, screen);
998 static void config_screen_widget_focus(struct nc_widget *widget, void *arg)
1000 struct config_screen *screen = arg;
1001 int w_y, w_height, w_focus, s_max, adjust;
1003 w_height = widget_height(widget);
1004 w_focus = widget_focus_y(widget);
1005 w_y = widget_y(widget) + w_focus;
1006 s_max = getmaxy(screen->scr.sub_ncw) - 1;
1008 if (w_y < screen->scroll_y)
1009 screen->scroll_y = w_y;
1011 else if (w_y + screen->scroll_y + 1 > s_max) {
1012 /* Fit as much of the widget into the screen as possible */
1013 adjust = min(s_max - 1, w_height - w_focus);
1014 if (w_y + adjust >= screen->scroll_y + s_max)
1015 screen->scroll_y = max(0, 1 + w_y + adjust - s_max);
1019 pad_refresh(screen);
1022 static void config_screen_draw(struct config_screen *screen,
1023 const struct config *config,
1024 const struct system_info *sysinfo)
1026 bool repost = false;
1029 /* The size of the pad we'll need depends on the number of interfaces.
1031 * We use N_FIELDS (which is quite conservative, as some fields share
1032 * a line) as a base, then:
1033 * - add 6 (as the network select & boot device select fields take 3
1035 * - add n_interfaces for every field in the network select field, and
1036 * - add (n_blockdevs + n_interfaces) for every field in the boot device
1039 height = N_FIELDS + 6;
1041 height += sysinfo->n_interfaces;
1042 height += (sysinfo->n_blockdevs + sysinfo->n_interfaces);
1044 if (!screen->pad || getmaxy(screen->pad) < height) {
1046 delwin(screen->pad);
1047 screen->pad = newpad(height, COLS + 10);
1050 if (screen->widgetset) {
1051 widgetset_unpost(screen->widgetset);
1052 talloc_free(screen->widgetset);
1056 screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
1058 widgetset_set_widget_focus(screen->widgetset,
1059 config_screen_widget_focus, screen);
1061 if (!config || !sysinfo) {
1062 config_screen_setup_empty(screen);
1064 screen->net_conf_type = find_net_conf_type(config);
1065 config_screen_setup_widgets(screen, config, sysinfo);
1066 config_screen_layout_widgets(screen);
1070 widgetset_post(screen->widgetset);
1073 void config_screen_update(struct config_screen *screen,
1074 const struct config *config,
1075 const struct system_info *sysinfo)
1077 if (screen->cui->current != config_screen_scr(screen)) {
1078 screen->need_update = true;
1082 config_screen_draw(screen, config, sysinfo);
1083 pad_refresh(screen);
1086 static int config_screen_post(struct nc_scr *scr)
1088 struct config_screen *screen = config_screen_from_scr(scr);
1089 screen->show_subset = false;
1091 if (screen->need_update) {
1092 config_screen_draw(screen, screen->cui->config,
1093 screen->cui->sysinfo);
1094 screen->need_update = false;
1096 widgetset_post(screen->widgetset);
1099 nc_scr_frame_draw(scr);
1100 if (screen->need_redraw) {
1101 redrawwin(scr->main_ncw);
1102 screen->need_redraw = false;
1104 wrefresh(screen->scr.main_ncw);
1105 pad_refresh(screen);
1109 static int config_screen_destroy(void *arg)
1111 struct config_screen *screen = arg;
1113 delwin(screen->pad);
1117 struct config_screen *config_screen_init(struct cui *cui,
1118 const struct config *config,
1119 const struct system_info *sysinfo,
1120 void (*on_exit)(struct cui *))
1122 struct config_screen *screen;
1124 screen = talloc_zero(cui, struct config_screen);
1125 talloc_set_destructor(screen, config_screen_destroy);
1126 nc_scr_init(&screen->scr, pb_config_screen_sig, 0,
1127 cui, config_screen_process_key,
1128 config_screen_post, config_screen_unpost,
1129 config_screen_resize);
1132 screen->on_exit = on_exit;
1133 screen->need_redraw = false;
1134 screen->need_update = false;
1135 screen->label_x = 2;
1136 screen->field_x = 17;
1138 screen->ipmi_override = false;
1139 screen->show_subset = false;
1141 screen->scr.frame.ltitle = talloc_strdup(screen,
1142 _("Petitboot System Configuration"));
1143 screen->scr.frame.rtitle = NULL;
1144 screen->scr.frame.help = talloc_strdup(screen,
1145 _("tab=next, shift+tab=previous, x=exit, h=help"));
1146 nc_scr_frame_draw(&screen->scr);
1148 scrollok(screen->scr.sub_ncw, true);
1150 config_screen_draw(screen, config, sysinfo);