]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/nc-config.c
ui/ncurses: Expose boot_device configuration through UI
[petitboot] / ui / ncurses / nc-config.c
1 /*
2  *  Copyright (C) 2013 IBM Corporation
3  *
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.
7  *
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.
12  *
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
16  */
17
18 #if defined(HAVE_CONFIG_H)
19 #include "config.h"
20 #endif
21
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <pb-config/pb-config.h>
27 #include <talloc/talloc.h>
28 #include <types/types.h>
29 #include <log/log.h>
30
31 #include "nc-cui.h"
32 #include "nc-config.h"
33 #include "nc-widgets.h"
34
35 #define N_FIELDS        25
36
37 extern const char *config_help_text;
38
39 enum autoboot_type {
40         AUTOBOOT_ANY,
41         AUTOBOOT_ONE,
42         AUTOBOOT_DISABLED,
43 };
44
45 enum net_conf_type {
46         NET_CONF_TYPE_DHCP_ALL,
47         NET_CONF_TYPE_DHCP_ONE,
48         NET_CONF_TYPE_STATIC,
49 };
50
51 struct config_screen {
52         struct nc_scr           scr;
53         struct cui              *cui;
54         struct nc_widgetset     *widgetset;
55         WINDOW                  *pad;
56
57         bool                    exit;
58         bool                    show_help;
59         void                    (*on_exit)(struct cui *);
60
61         int                     scroll_y;
62
63         int                     label_x;
64         int                     field_x;
65         int                     network_config_y;
66
67         enum net_conf_type      net_conf_type;
68         enum autoboot_type      autoboot_type;
69
70         struct {
71                 struct nc_widget_select         *autoboot_f;
72                 struct nc_widget_label          *autoboot_l;
73                 struct nc_widget_select         *boot_device_f;
74                 struct nc_widget_textbox        *timeout_f;
75                 struct nc_widget_label          *timeout_l;
76                 struct nc_widget_label          *timeout_help_l;
77
78                 struct nc_widget_label          *network_l;
79                 struct nc_widget_select         *network_f;
80
81                 struct nc_widget_label          *iface_l;
82                 struct nc_widget_select         *iface_f;
83                 struct nc_widget_label          *ip_addr_l;
84                 struct nc_widget_textbox        *ip_addr_f;
85                 struct nc_widget_label          *ip_mask_l;
86                 struct nc_widget_textbox        *ip_mask_f;
87                 struct nc_widget_label          *ip_addr_mask_help_l;
88                 struct nc_widget_label          *gateway_l;
89                 struct nc_widget_textbox        *gateway_f;
90                 struct nc_widget_label          *gateway_help_l;
91                 struct nc_widget_label          *dns_l;
92                 struct nc_widget_textbox        *dns_f;
93                 struct nc_widget_label          *dns_dhcp_help_l;
94                 struct nc_widget_label          *dns_help_l;
95
96                 struct nc_widget_button         *ok_b;
97                 struct nc_widget_button         *help_b;
98                 struct nc_widget_button         *cancel_b;
99         } widgets;
100 };
101
102 static struct config_screen *config_screen_from_scr(struct nc_scr *scr)
103 {
104         struct config_screen *config_screen;
105
106         assert(scr->sig == pb_config_screen_sig);
107         config_screen = (struct config_screen *)
108                 ((char *)scr - (size_t)&((struct config_screen *)0)->scr);
109         assert(config_screen->scr.sig == pb_config_screen_sig);
110         return config_screen;
111 }
112
113 static void pad_refresh(struct config_screen *screen)
114 {
115         int y, x, rows, cols;
116
117         getmaxyx(screen->scr.sub_ncw, rows, cols);
118         getbegyx(screen->scr.sub_ncw, y, x);
119
120         prefresh(screen->pad, screen->scroll_y, 0, y, x, rows, cols);
121 }
122
123 static void config_screen_process_key(struct nc_scr *scr, int key)
124 {
125         struct config_screen *screen = config_screen_from_scr(scr);
126         bool handled;
127
128         handled = widgetset_process_key(screen->widgetset, key);
129
130         if (!handled) {
131                 switch (key) {
132                 case 'x':
133                 case 27: /* esc */
134                         screen->exit = true;
135                         break;
136                 case 'h':
137                         screen->show_help = true;
138                         break;
139                 }
140         }
141
142         if (screen->exit) {
143                 screen->on_exit(screen->cui);
144
145         } else if (screen->show_help) {
146                 screen->show_help = false;
147                 cui_show_help(screen->cui, "System Configuration",
148                                 config_help_text);
149
150         } else if (handled) {
151                 pad_refresh(screen);
152         }
153 }
154
155 static void config_screen_resize(struct nc_scr *scr)
156 {
157         struct config_screen *screen = config_screen_from_scr(scr);
158         (void)screen;
159 }
160
161 static int config_screen_post(struct nc_scr *scr)
162 {
163         struct config_screen *screen = config_screen_from_scr(scr);
164         widgetset_post(screen->widgetset);
165         nc_scr_frame_draw(scr);
166         redrawwin(scr->main_ncw);
167         wrefresh(screen->scr.main_ncw);
168         pad_refresh(screen);
169         return 0;
170 }
171
172 static int config_screen_unpost(struct nc_scr *scr)
173 {
174         struct config_screen *screen = config_screen_from_scr(scr);
175         widgetset_unpost(screen->widgetset);
176         return 0;
177 }
178
179 struct nc_scr *config_screen_scr(struct config_screen *screen)
180 {
181         return &screen->scr;
182 }
183
184 static int screen_process_form(struct config_screen *screen)
185 {
186         const struct system_info *sysinfo = screen->cui->sysinfo;
187         enum net_conf_type net_conf_type;
188         struct interface_config *iface;
189         struct config *config;
190         char *str, *end;
191         int rc, idx;
192
193         config = config_copy(screen, screen->cui->config);
194
195         screen->autoboot_type =
196                 widget_select_get_value(screen->widgets.autoboot_f);
197
198         config->autoboot_enabled = screen->autoboot_type != AUTOBOOT_DISABLED;
199
200         if (screen->autoboot_type == AUTOBOOT_ONE) {
201                 char mac[20], *uuid = NULL;
202
203                 /* if idx is -1 here, we have an unknown UUID selected.
204                  * Otherwise, it's a blockdev index (idx <= n_blockdevs) or an
205                  * interface index.
206                  */
207                 idx = widget_select_get_value(screen->widgets.boot_device_f);
208                 if (idx >= (int)sysinfo->n_blockdevs) {
209                         struct interface_info *info = sysinfo->
210                                 interfaces[idx - sysinfo->n_blockdevs];
211                         mac_str(info->hwaddr, info->hwaddr_size,
212                                         mac, sizeof(mac));
213                         uuid = mac;
214                 } else if (idx != -1) {
215                         uuid = sysinfo->blockdevs[idx]->uuid;
216                 }
217
218                 if (uuid) {
219                         talloc_free(config->boot_device);
220                         config->boot_device = talloc_strdup(config, uuid);
221                 }
222         }
223
224         str = widget_textbox_get_value(screen->widgets.timeout_f);
225         if (str) {
226                 unsigned long x;
227                 errno = 0;
228                 x = strtoul(str, &end, 10);
229                 if (!errno && end != str)
230                         config->autoboot_timeout_sec = x;
231         }
232
233         net_conf_type = widget_select_get_value(screen->widgets.network_f);
234
235         /* if we don't have any network interfaces, prevent per-interface
236          * configuration */
237         if (sysinfo->n_interfaces == 0)
238                 net_conf_type = NET_CONF_TYPE_DHCP_ALL;
239
240         if (net_conf_type == NET_CONF_TYPE_DHCP_ALL) {
241                 config->network.n_interfaces = 0;
242
243         } else {
244                 iface = talloc_zero(config, struct interface_config);
245                 config->network.n_interfaces = 1;
246                 config->network.interfaces = talloc_array(config,
247                                 struct interface_config *, 1);
248                 config->network.interfaces[0] = iface;
249
250                 /* copy hwaddr (from the sysinfo interface data) to
251                  * the configuration */
252                 idx = widget_select_get_value(screen->widgets.iface_f);
253                 memcpy(iface->hwaddr, sysinfo->interfaces[idx]->hwaddr,
254                                 sizeof(iface->hwaddr));
255         }
256
257         if (net_conf_type == NET_CONF_TYPE_DHCP_ONE) {
258                 iface->method = CONFIG_METHOD_DHCP;
259         }
260
261         if (net_conf_type == NET_CONF_TYPE_STATIC) {
262                 char *ip, *mask, *gateway;
263
264                 ip = widget_textbox_get_value(screen->widgets.ip_addr_f);
265                 mask = widget_textbox_get_value(screen->widgets.ip_mask_f);
266                 gateway = widget_textbox_get_value(screen->widgets.gateway_f);
267
268                 if (!ip || !*ip || !mask || !*mask) {
269                         screen->scr.frame.status =
270                                 "No IP / mask values are set";
271                         nc_scr_frame_draw(&screen->scr);
272                         talloc_free(config);
273                         return -1;
274                 }
275
276                 iface->method = CONFIG_METHOD_STATIC;
277                 iface->static_config.address = talloc_asprintf(iface, "%s/%s",
278                                 ip, mask);
279                 iface->static_config.gateway = talloc_strdup(iface, gateway);
280         }
281
282         str = widget_textbox_get_value(screen->widgets.dns_f);
283         talloc_free(config->network.dns_servers);
284         config->network.dns_servers = NULL;
285         config->network.n_dns_servers = 0;
286
287         if (str && strlen(str)) {
288                 char *dns, *tmp;
289                 int i;
290
291                 for (;;) {
292                         dns = strtok_r(str, " \t", &tmp);
293
294                         if (!dns)
295                                 break;
296
297                         i = config->network.n_dns_servers++;
298                         config->network.dns_servers = talloc_realloc(config,
299                                         config->network.dns_servers,
300                                         const char *,
301                                         config->network.n_dns_servers);
302                         config->network.dns_servers[i] =
303                                 talloc_strdup(config, dns);
304
305                         str = NULL;
306                 }
307         }
308
309         rc = cui_send_config(screen->cui, config);
310         talloc_free(config);
311
312         if (rc)
313                 pb_log("cui_send_config failed!\n");
314         else
315                 pb_debug("config sent!\n");
316
317         return 0;
318 }
319
320 static void ok_click(void *arg)
321 {
322         struct config_screen *screen = arg;
323         if (screen_process_form(screen))
324                 /* errors are written to the status line, so we'll need
325                  * to refresh */
326                 wrefresh(screen->scr.main_ncw);
327         else
328                 screen->exit = true;
329 }
330
331 static void help_click(void *arg)
332 {
333         struct config_screen *screen = arg;
334         screen->show_help = true;
335 }
336
337 static void cancel_click(void *arg)
338 {
339         struct config_screen *screen = arg;
340         screen->exit = true;
341 }
342
343 static int layout_pair(struct config_screen *screen, int y,
344                 struct nc_widget_label *label,
345                 struct nc_widget *field)
346 {
347         struct nc_widget *label_w = widget_label_base(label);
348         widget_move(label_w, y, screen->label_x);
349         widget_move(field, y, screen->field_x);
350         return max(widget_height(label_w), widget_height(field));
351 }
352
353 static void config_screen_layout_widgets(struct config_screen *screen)
354 {
355         struct nc_widget *wl, *wf, *wh;
356         int y, x, help_x;
357         bool show;
358
359         y = 1;
360         help_x = screen->field_x + 2 +
361                 widget_width(widget_textbox_base(screen->widgets.dns_f));
362
363         y += layout_pair(screen, y, screen->widgets.autoboot_l,
364                         widget_select_base(screen->widgets.autoboot_f));
365
366         wf = widget_select_base(screen->widgets.boot_device_f);
367         if (screen->autoboot_type == AUTOBOOT_ONE) {
368                 widget_set_visible(wf, true);
369                 widget_move(wf, y, screen->field_x + 3);
370                 y += widget_height(wf);
371         } else {
372                 widget_set_visible(wf, false);
373         }
374
375         y += 1;
376
377         wf = widget_textbox_base(screen->widgets.timeout_f);
378         wl = widget_label_base(screen->widgets.timeout_l);
379         wh = widget_label_base(screen->widgets.timeout_help_l);
380         if (screen->autoboot_type != AUTOBOOT_DISABLED) {
381                 widget_set_visible(wl, true);
382                 widget_set_visible(wf, true);
383                 widget_set_visible(wh, true);
384                 widget_move(wl, y, screen->label_x);
385                 widget_move(wf, y, screen->field_x);
386                 widget_move(wh, y, screen->field_x + widget_width(wf) + 1);
387                 y += 2;
388         } else {
389                 widget_set_visible(wl, false);
390                 widget_set_visible(wf, false);
391                 widget_set_visible(wh, false);
392         }
393
394         y += layout_pair(screen, y, screen->widgets.network_l,
395                         widget_select_base(screen->widgets.network_f));
396
397         y += 1;
398
399         /* conditionally show iface select */
400         wl = widget_label_base(screen->widgets.iface_l);
401         wf = widget_select_base(screen->widgets.iface_f);
402
403         show = screen->net_conf_type == NET_CONF_TYPE_DHCP_ONE ||
404                 screen->net_conf_type == NET_CONF_TYPE_STATIC;
405
406         widget_set_visible(wl, show);
407         widget_set_visible(wf, show);
408
409         if (show)
410                 y += layout_pair(screen, y, screen->widgets.iface_l, wf) + 1;
411
412         /* conditionally show static IP params */
413         show = screen->net_conf_type == NET_CONF_TYPE_STATIC;
414
415         wl = widget_label_base(screen->widgets.ip_addr_l);
416         wf = widget_textbox_base(screen->widgets.ip_addr_f);
417         widget_set_visible(wl, show);
418         widget_set_visible(wf, show);
419         x = screen->field_x + widget_width(wf) + 1;
420
421         if (show)
422                 layout_pair(screen, y, screen->widgets.ip_addr_l, wf);
423
424         wl = widget_label_base(screen->widgets.ip_mask_l);
425         wf = widget_textbox_base(screen->widgets.ip_mask_f);
426         widget_set_visible(wl, show);
427         widget_set_visible(wf, show);
428
429         if (show) {
430                 widget_move(wl, y, x);
431                 widget_move(wf, y, x + 2);
432         }
433
434         /* help for IP/mask */
435         wh = widget_label_base(screen->widgets.ip_addr_mask_help_l);
436         widget_set_visible(wh, show);
437         if (show) {
438                 widget_move(wh, y, help_x);
439                 y++;
440         }
441
442         wl = widget_label_base(screen->widgets.gateway_l);
443         wf = widget_textbox_base(screen->widgets.gateway_f);
444         wh = widget_label_base(screen->widgets.gateway_help_l);
445         widget_set_visible(wl, show);
446         widget_set_visible(wf, show);
447         widget_set_visible(wh, show);
448
449         if (show) {
450                 layout_pair(screen, y, screen->widgets.gateway_l, wf);
451                 widget_move(wh, y, help_x);
452                 y++;
453         }
454
455         wh = widget_label_base(screen->widgets.dns_help_l);
456         layout_pair(screen, y, screen->widgets.dns_l,
457                         widget_textbox_base(screen->widgets.dns_f));
458         widget_move(wh, y, help_x);
459         y++;
460
461         /* we show the DNS/DHCP help if we're configuring DHCP */
462         show = screen->net_conf_type != NET_CONF_TYPE_STATIC;
463         wl = widget_label_base(screen->widgets.dns_dhcp_help_l);
464         widget_set_visible(wl, show);
465         if (show) {
466                 widget_move(wl, y, screen->field_x);
467                 y += 1;
468         }
469
470         y += 1;
471
472         widget_move(widget_button_base(screen->widgets.ok_b),
473                         y, screen->field_x);
474         widget_move(widget_button_base(screen->widgets.help_b),
475                         y, screen->field_x + 10);
476         widget_move(widget_button_base(screen->widgets.cancel_b),
477                         y, screen->field_x + 20);
478 }
479
480 static void config_screen_network_change(void *arg, int value)
481 {
482         struct config_screen *screen = arg;
483         screen->net_conf_type = value;
484         widgetset_unpost(screen->widgetset);
485         config_screen_layout_widgets(screen);
486         widgetset_post(screen->widgetset);
487 }
488
489 static void config_screen_autoboot_change(void *arg, int value)
490 {
491         struct config_screen *screen = arg;
492         screen->autoboot_type = value;
493         widgetset_unpost(screen->widgetset);
494         config_screen_layout_widgets(screen);
495         widgetset_post(screen->widgetset);
496 }
497
498 static struct interface_config *first_active_interface(
499                 const struct config *config)
500 {
501         unsigned int i;
502
503         for (i = 0; i < config->network.n_interfaces; i++) {
504                 if (config->network.interfaces[i]->ignore)
505                         continue;
506                 return config->network.interfaces[i];
507         }
508         return NULL;
509 }
510
511 static enum net_conf_type find_net_conf_type(const struct config *config)
512 {
513         struct interface_config *ifcfg;
514
515         ifcfg = first_active_interface(config);
516
517         if (!ifcfg)
518                 return NET_CONF_TYPE_DHCP_ALL;
519
520         else if (ifcfg->method == CONFIG_METHOD_DHCP)
521                 return NET_CONF_TYPE_DHCP_ONE;
522
523         else if (ifcfg->method == CONFIG_METHOD_STATIC)
524                 return NET_CONF_TYPE_STATIC;
525
526         assert(0);
527         return NET_CONF_TYPE_DHCP_ALL;
528 }
529
530 static void config_screen_setup_empty(struct config_screen *screen)
531 {
532         widget_new_label(screen->widgetset, 2, screen->field_x,
533                         "Waiting for configuration data...");
534         screen->widgets.cancel_b = widget_new_button(screen->widgetset,
535                         4, screen->field_x, 6, "Cancel", cancel_click, screen);
536 }
537
538
539 static void config_screen_setup_widgets(struct config_screen *screen,
540                 const struct config *config,
541                 const struct system_info *sysinfo)
542 {
543         struct nc_widgetset *set = screen->widgetset;
544         struct interface_config *ifcfg;
545         char *str, *ip, *mask, *gw;
546         enum net_conf_type type;
547         unsigned int i;
548         bool found;
549
550         build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
551                         == N_FIELDS);
552
553         type = screen->net_conf_type;
554         ifcfg = first_active_interface(config);
555
556         screen->widgets.autoboot_l = widget_new_label(set, 0, 0, "Autoboot:");
557         screen->widgets.autoboot_f = widget_new_select(set, 0, 0, 55);
558
559         widget_select_on_change(screen->widgets.autoboot_f,
560                         config_screen_autoboot_change, screen);
561
562         screen->widgets.boot_device_f = widget_new_select(set, 0, 0, 55);
563
564         widget_select_add_option(screen->widgets.autoboot_f,
565                                         AUTOBOOT_DISABLED,
566                                         "Don't autoboot",
567                                         screen->autoboot_type ==
568                                                 AUTOBOOT_DISABLED);
569         widget_select_add_option(screen->widgets.autoboot_f,
570                                         AUTOBOOT_ANY,
571                                         "Autoboot from any disk/network device",
572                                         screen->autoboot_type ==
573                                                 AUTOBOOT_ANY);
574         widget_select_add_option(screen->widgets.autoboot_f,
575                                         AUTOBOOT_ONE,
576                                         "Only autoboot from a specific "
577                                         "disk/network device",
578                                         screen->autoboot_type ==
579                                                 AUTOBOOT_ONE);
580
581         found = false;
582
583         for (i = 0; i < sysinfo->n_blockdevs; i++) {
584                 struct blockdev_info *bd = sysinfo->blockdevs[i];
585                 bool selected;
586                 char *label;
587
588                 selected = config->boot_device &&
589                                 !strcmp(config->boot_device, bd->uuid);
590                 if (selected)
591                         found = true;
592
593                 label = talloc_asprintf(screen, "disk: %s [uuid: %s]",
594                                 bd->name, bd->uuid);
595
596                 widget_select_add_option(screen->widgets.boot_device_f, i,
597                                         label, selected);
598         }
599
600         for (i = 0; i < sysinfo->n_interfaces; i++) {
601                 struct interface_info *info = sysinfo->interfaces[i];
602                 char *label, mac[20];
603                 bool selected;
604
605                 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
606                 selected = config->boot_device &&
607                                 !strcmp(config->boot_device, mac);
608                 if (selected)
609                         found = true;
610
611                 label = talloc_asprintf(screen, "net:  %s [mac: %s]",
612                                 info->name, mac);
613
614                 widget_select_add_option(screen->widgets.boot_device_f,
615                                                 i + sysinfo->n_blockdevs,
616                                                 label, selected);
617         }
618
619         if (screen->autoboot_type == AUTOBOOT_ONE && !found) {
620                 char *label;
621
622                 label = talloc_asprintf(screen, "Unknown UUID: %s",
623                                 config->boot_device);
624
625                 widget_select_add_option(screen->widgets.boot_device_f, -1,
626                                                 label, true);
627         }
628
629         str = talloc_asprintf(screen, "%d", config->autoboot_timeout_sec);
630         screen->widgets.timeout_l = widget_new_label(set, 0, 0, "Timeout:");
631         screen->widgets.timeout_f = widget_new_textbox(set, 0, 0, 5, str);
632         screen->widgets.timeout_help_l = widget_new_label(set, 0, 0, "seconds");
633
634         widget_textbox_set_fixed_size(screen->widgets.timeout_f);
635         widget_textbox_set_validator_integer(screen->widgets.timeout_f, 0, 999);
636
637         screen->widgets.network_l = widget_new_label(set, 0, 0, "Network");
638         screen->widgets.network_f = widget_new_select(set, 0, 0, 50);
639
640         widget_select_add_option(screen->widgets.network_f,
641                                         NET_CONF_TYPE_DHCP_ALL,
642                                         "DHCP on all active interfaces",
643                                         type == NET_CONF_TYPE_DHCP_ALL);
644         widget_select_add_option(screen->widgets.network_f,
645                                         NET_CONF_TYPE_DHCP_ONE,
646                                         "DHCP on a specific interface",
647                                         type == NET_CONF_TYPE_DHCP_ONE);
648         widget_select_add_option(screen->widgets.network_f,
649                                         NET_CONF_TYPE_STATIC,
650                                         "Static IP configuration",
651                                         type == NET_CONF_TYPE_STATIC);
652
653         widget_select_on_change(screen->widgets.network_f,
654                         config_screen_network_change, screen);
655
656         screen->widgets.iface_l = widget_new_label(set, 0, 0, "Device:");
657         screen->widgets.iface_f = widget_new_select(set, 0, 0, 50);
658
659         for (i = 0; i < sysinfo->n_interfaces; i++) {
660                 struct interface_info *info = sysinfo->interfaces[i];
661                 char str[50], mac[20];
662                 bool is_default;
663
664                 is_default = ifcfg && !memcmp(ifcfg->hwaddr, info->hwaddr,
665                                         sizeof(ifcfg->hwaddr));
666
667                 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
668                 snprintf(str, sizeof(str), "%s [%s, %s]", info->name, mac,
669                                 info->link ? "link up" : "link down");
670
671                 widget_select_add_option(screen->widgets.iface_f,
672                                                 i, str, is_default);
673         }
674
675         gw = ip = mask = NULL;
676         if (ifcfg && ifcfg->method == CONFIG_METHOD_STATIC) {
677                 char *sep;
678
679                 str = talloc_strdup(screen, ifcfg->static_config.address);
680                 sep = strchr(str, '/');
681                 ip = str;
682
683                 if (sep) {
684                         *sep = '\0';
685                         mask = sep + 1;
686                 }
687                 gw = ifcfg->static_config.gateway;
688         }
689
690         screen->widgets.ip_addr_l = widget_new_label(set, 0, 0, "IP/mask:");
691         screen->widgets.ip_addr_f = widget_new_textbox(set, 0, 0, 16, ip);
692         screen->widgets.ip_mask_l = widget_new_label(set, 0, 0, "/");
693         screen->widgets.ip_mask_f = widget_new_textbox(set, 0, 0, 3, mask);
694         screen->widgets.ip_addr_mask_help_l =
695                 widget_new_label(set, 0, 0, "(eg. 192.168.0.10 / 24)");
696
697         widget_textbox_set_fixed_size(screen->widgets.ip_addr_f);
698         widget_textbox_set_fixed_size(screen->widgets.ip_mask_f);
699         widget_textbox_set_validator_ipv4(screen->widgets.ip_addr_f);
700         widget_textbox_set_validator_integer(screen->widgets.ip_mask_f, 1, 31);
701
702         screen->widgets.gateway_l = widget_new_label(set, 0, 0, "Gateway:");
703         screen->widgets.gateway_f = widget_new_textbox(set, 0, 0, 16, gw);
704         screen->widgets.gateway_help_l =
705                 widget_new_label(set, 0, 0, "(eg. 192.168.0.1)");
706
707         widget_textbox_set_fixed_size(screen->widgets.gateway_f);
708         widget_textbox_set_validator_ipv4(screen->widgets.gateway_f);
709
710         str = talloc_strdup(screen, "");
711         for (i = 0; i < config->network.n_dns_servers; i++) {
712                 str = talloc_asprintf_append(str, "%s%s",
713                                 (i == 0) ? "" : " ",
714                                 config->network.dns_servers[i]);
715         }
716
717         screen->widgets.dns_l = widget_new_label(set, 0, 0, "DNS Server(s):");
718         screen->widgets.dns_f = widget_new_textbox(set, 0, 0, 32, str);
719         screen->widgets.dns_help_l =
720                 widget_new_label(set, 0, 0, "(eg. 192.168.0.2)");
721
722         widget_textbox_set_validator_ipv4_multi(screen->widgets.dns_f);
723
724         screen->widgets.dns_dhcp_help_l = widget_new_label(set, 0, 0,
725                         "(if not provided by DHCP server)");
726
727         screen->widgets.ok_b = widget_new_button(set, 0, 0, 6, "OK",
728                         ok_click, screen);
729         screen->widgets.help_b = widget_new_button(set, 0, 0, 6, "Help",
730                         help_click, screen);
731         screen->widgets.cancel_b = widget_new_button(set, 0, 0, 6, "Cancel",
732                         cancel_click, screen);
733 }
734
735 static void config_screen_widget_focus(struct nc_widget *widget, void *arg)
736 {
737         struct config_screen *screen = arg;
738         int w_y, s_max;
739
740         w_y = widget_y(widget) + widget_focus_y(widget);
741         s_max = getmaxy(screen->scr.sub_ncw) - 1;
742
743         if (w_y < screen->scroll_y)
744                 screen->scroll_y = w_y;
745
746         else if (w_y + screen->scroll_y + 1 > s_max)
747                 screen->scroll_y = 1 + w_y - s_max;
748
749         else
750                 return;
751
752         pad_refresh(screen);
753 }
754
755 static void config_screen_draw(struct config_screen *screen,
756                 const struct config *config,
757                 const struct system_info *sysinfo)
758 {
759         bool repost = false;
760         int height;
761
762         /* The size of the pad we'll need depends on the number of interfaces.
763          *
764          * We use N_FIELDS (which is quite conservative, as some fields share
765          * a line) as a base, then add 3 (as the network select field is
766          * takes 3 lines), and n_interfaces (as the network interface field
767          * has n_interfaces lines).
768          */
769         height = N_FIELDS + 3;
770         if (sysinfo)
771                 height += sysinfo->n_interfaces;
772         if (!screen->pad || getmaxy(screen->pad) < height) {
773                 if (screen->pad)
774                         delwin(screen->pad);
775                 screen->pad = newpad(height, COLS);
776         }
777
778         if (screen->widgetset) {
779                 widgetset_unpost(screen->widgetset);
780                 talloc_free(screen->widgetset);
781                 repost = true;
782         }
783
784         screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
785                         screen->pad);
786         widgetset_set_widget_focus(screen->widgetset,
787                         config_screen_widget_focus, screen);
788
789         if (!config || !sysinfo) {
790                 config_screen_setup_empty(screen);
791         } else {
792                 screen->net_conf_type = find_net_conf_type(config);
793                 if (!config->autoboot_enabled)
794                         screen->autoboot_type = AUTOBOOT_DISABLED;
795                 else
796                         screen->autoboot_type = config->boot_device ?
797                                         AUTOBOOT_ONE : AUTOBOOT_ANY;
798
799                 config_screen_setup_widgets(screen, config, sysinfo);
800                 config_screen_layout_widgets(screen);
801         }
802
803         if (repost)
804                 widgetset_post(screen->widgetset);
805 }
806
807 void config_screen_update(struct config_screen *screen,
808                 const struct config *config,
809                 const struct system_info *sysinfo)
810 {
811         config_screen_draw(screen, config, sysinfo);
812         pad_refresh(screen);
813 }
814
815 static int config_screen_destroy(void *arg)
816 {
817         struct config_screen *screen = arg;
818         if (screen->pad)
819                 delwin(screen->pad);
820         return 0;
821 }
822
823 struct config_screen *config_screen_init(struct cui *cui,
824                 const struct config *config,
825                 const struct system_info *sysinfo,
826                 void (*on_exit)(struct cui *))
827 {
828         struct config_screen *screen;
829
830         screen = talloc_zero(cui, struct config_screen);
831         talloc_set_destructor(screen, config_screen_destroy);
832         nc_scr_init(&screen->scr, pb_config_screen_sig, 0,
833                         cui, config_screen_process_key,
834                         config_screen_post, config_screen_unpost,
835                         config_screen_resize);
836
837         screen->cui = cui;
838         screen->on_exit = on_exit;
839         screen->label_x = 2;
840         screen->field_x = 17;
841
842         screen->scr.frame.ltitle = talloc_strdup(screen,
843                         "Petitboot System Configuration");
844         screen->scr.frame.rtitle = NULL;
845         screen->scr.frame.help = talloc_strdup(screen,
846                         "tab=next, shift+tab=previous, x=exit, h=help");
847         nc_scr_frame_draw(&screen->scr);
848
849         scrollok(screen->scr.sub_ncw, true);
850
851         config_screen_draw(screen, config, sysinfo);
852
853         return screen;
854 }