b257076d03b2f50be229b2a51a718abe8a8822e7
[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 #include <i18n/i18n.h>
31
32 #include "nc-cui.h"
33 #include "nc-config.h"
34 #include "nc-widgets.h"
35
36 #define N_FIELDS        34
37
38 extern struct help_text config_help_text;
39
40 enum net_conf_type {
41         NET_CONF_TYPE_DHCP_ALL,
42         NET_CONF_TYPE_DHCP_ONE,
43         NET_CONF_TYPE_STATIC,
44 };
45
46 struct config_screen {
47         struct nc_scr           scr;
48         struct cui              *cui;
49         struct nc_widgetset     *widgetset;
50         WINDOW                  *pad;
51
52         bool                    exit;
53         bool                    show_help;
54         bool                    show_subset;
55         bool                    need_redraw;
56
57         void                    (*on_exit)(struct cui *);
58
59         int                     scroll_y;
60
61         int                     label_x;
62         int                     field_x;
63         int                     network_config_y;
64
65         enum net_conf_type      net_conf_type;
66
67         bool                    autoboot_enabled;
68         bool                    ipmi_override;
69
70         struct {
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;
80
81                 struct nc_widget_label          *ipmi_type_l;
82                 struct nc_widget_label          *ipmi_clear_l;
83                 struct nc_widget_checkbox       *ipmi_clear_cb;
84
85                 struct nc_widget_label          *network_l;
86                 struct nc_widget_select         *network_f;
87
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;
102
103                 struct nc_widget_label          *allow_write_l;
104                 struct nc_widget_select         *allow_write_f;
105
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;
110         } widgets;
111 };
112
113 static struct config_screen *config_screen_from_scr(struct nc_scr *scr)
114 {
115         struct config_screen *config_screen;
116
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;
122 }
123
124 static void pad_refresh(struct config_screen *screen)
125 {
126         int y, x, rows, cols;
127
128         getmaxyx(screen->scr.sub_ncw, rows, cols);
129         getbegyx(screen->scr.sub_ncw, y, x);
130
131         prefresh(screen->pad, screen->scroll_y, 0, y, x, rows, cols);
132 }
133
134 static void config_screen_process_key(struct nc_scr *scr, int key)
135 {
136         struct config_screen *screen = config_screen_from_scr(scr);
137         bool handled;
138
139         handled = widgetset_process_key(screen->widgetset, key);
140
141         if (!handled) {
142                 switch (key) {
143                 case 'x':
144                 case 27: /* esc */
145                         screen->exit = true;
146                         break;
147                 case 'h':
148                         screen->show_help = true;
149                         break;
150                 }
151         }
152
153         if (screen->exit) {
154                 screen->on_exit(screen->cui);
155
156         } else if (screen->show_help) {
157                 screen->show_help = false;
158                 screen->need_redraw = true;
159                 cui_show_help(screen->cui, _("System Configuration"),
160                                 &config_help_text);
161
162         } else if (handled && !screen->show_subset) {
163                 pad_refresh(screen);
164         }
165 }
166
167 static void config_screen_resize(struct nc_scr *scr)
168 {
169         struct config_screen *screen = config_screen_from_scr(scr);
170         (void)screen;
171 }
172
173 static int config_screen_post(struct nc_scr *scr)
174 {
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;
182         }
183         wrefresh(screen->scr.main_ncw);
184         pad_refresh(screen);
185         return 0;
186 }
187
188 static int config_screen_unpost(struct nc_scr *scr)
189 {
190         struct config_screen *screen = config_screen_from_scr(scr);
191         widgetset_unpost(screen->widgetset);
192         return 0;
193 }
194
195 struct nc_scr *config_screen_scr(struct config_screen *screen)
196 {
197         return &screen->scr;
198 }
199
200 static int screen_process_form(struct config_screen *screen)
201 {
202         const struct system_info *sysinfo = screen->cui->sysinfo;
203         enum net_conf_type net_conf_type;
204         struct interface_config *iface;
205         char *str, *end;
206         struct config *config;
207         int i, n_boot_opts, rc, idx;
208         unsigned int *order;
209         bool allow_write;
210         char mac[20];
211
212         config = config_copy(screen, screen->cui->config);
213
214         talloc_free(config->autoboot_opts);
215         config->n_autoboot_opts = 0;
216
217         n_boot_opts = widget_subset_get_order(config, &order,
218                                               screen->widgets.boot_order_f);
219
220         config->autoboot_enabled = n_boot_opts > 0;
221
222         config->n_autoboot_opts = n_boot_opts;
223         config->autoboot_opts = talloc_array(config, struct autoboot_option,
224                                              n_boot_opts);
225
226         for (i = 0; i < n_boot_opts; i++) {
227                 if (order[i] < sysinfo->n_blockdevs) {
228                         /* disk uuid */
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)) {
233                         /* net uuid */
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,
238                                 mac, sizeof(mac));
239                         config->autoboot_opts[i].uuid = talloc_strdup(config, mac);
240                 } else {
241                         /* device type */
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];
245                 }
246         }
247
248         str = widget_textbox_get_value(screen->widgets.timeout_f);
249         if (str) {
250                 unsigned long x;
251                 errno = 0;
252                 x = strtoul(str, &end, 10);
253                 if (!errno && end != str)
254                         config->autoboot_timeout_sec = x;
255         }
256
257         if (screen->ipmi_override)
258                 if (widget_checkbox_get_value(screen->widgets.ipmi_clear_cb))
259                         config->ipmi_bootdev = IPMI_BOOTDEV_INVALID;
260
261
262         net_conf_type = widget_select_get_value(screen->widgets.network_f);
263
264         /* if we don't have any network interfaces, prevent per-interface
265          * configuration */
266         if (sysinfo->n_interfaces == 0)
267                 net_conf_type = NET_CONF_TYPE_DHCP_ALL;
268
269         if (net_conf_type == NET_CONF_TYPE_DHCP_ALL) {
270                 config->network.n_interfaces = 0;
271
272         } else {
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;
278
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));
284         }
285
286         if (net_conf_type == NET_CONF_TYPE_DHCP_ONE) {
287                 iface->method = CONFIG_METHOD_DHCP;
288         }
289
290         if (net_conf_type == NET_CONF_TYPE_STATIC) {
291                 char *ip, *mask, *gateway;
292
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);
296
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);
301                         talloc_free(config);
302                         return -1;
303                 }
304
305                 iface->method = CONFIG_METHOD_STATIC;
306                 iface->static_config.address = talloc_asprintf(iface, "%s/%s",
307                                 ip, mask);
308                 iface->static_config.gateway = talloc_strdup(iface, gateway);
309         }
310
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;
315
316         if (str && strlen(str)) {
317                 char *dns, *tmp;
318                 int i;
319
320                 for (;;) {
321                         dns = strtok_r(str, " \t", &tmp);
322
323                         if (!dns)
324                                 break;
325
326                         i = config->network.n_dns_servers++;
327                         config->network.dns_servers = talloc_realloc(config,
328                                         config->network.dns_servers,
329                                         const char *,
330                                         config->network.n_dns_servers);
331                         config->network.dns_servers[i] =
332                                 talloc_strdup(config, dns);
333
334                         str = NULL;
335                 }
336         }
337
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;
341
342         config->safe_mode = false;
343         rc = cui_send_config(screen->cui, config);
344         talloc_free(config);
345
346         if (rc)
347                 pb_log("cui_send_config failed!\n");
348         else
349                 pb_debug("config sent!\n");
350
351         return 0;
352 }
353
354 static void ok_click(void *arg)
355 {
356         struct config_screen *screen = arg;
357         if (screen_process_form(screen))
358                 /* errors are written to the status line, so we'll need
359                  * to refresh */
360                 wrefresh(screen->scr.main_ncw);
361         else
362                 screen->exit = true;
363 }
364
365 static void help_click(void *arg)
366 {
367         struct config_screen *screen = arg;
368         screen->show_help = true;
369 }
370
371 static void cancel_click(void *arg)
372 {
373         struct config_screen *screen = arg;
374         screen->exit = true;
375 }
376
377 static int layout_pair(struct config_screen *screen, int y,
378                 struct nc_widget_label *label,
379                 struct nc_widget *field)
380 {
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));
385 }
386
387 static void config_screen_layout_widgets(struct config_screen *screen)
388 {
389         struct nc_widget *wl, *wf, *wh;
390         int y, x, help_x;
391         bool show;
392
393         y = 1;
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));
398
399         wl = widget_label_base(screen->widgets.boot_order_l);
400         widget_set_visible(wl, true);
401         widget_move(wl, y, screen->label_x);
402
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);
407
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);
412         } else {
413                 widget_set_visible(wl, true);
414                 widget_set_visible(wf, false);
415                 y += 1;
416         }
417
418         y += 1;
419
420         widget_move(widget_button_base(screen->widgets.boot_add_b),
421                         y++, screen->field_x);
422         widget_move(widget_button_base(screen->widgets.boot_any_b),
423                         y++, screen->field_x);
424         widget_move(widget_button_base(screen->widgets.boot_none_b),
425                         y, screen->field_x);
426
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);
430         else
431                 widget_set_visible(wf, false);
432
433         y += 2;
434
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);
446                 y += 2;
447         }
448
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);
453                 y += 1;
454
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);
461                 y += 1;
462         }
463
464         y += 1;
465
466         y += layout_pair(screen, y, screen->widgets.network_l,
467                         widget_select_base(screen->widgets.network_f));
468
469         y += 1;
470
471         /* conditionally show iface select */
472         wl = widget_label_base(screen->widgets.iface_l);
473         wf = widget_select_base(screen->widgets.iface_f);
474
475         show = screen->net_conf_type == NET_CONF_TYPE_DHCP_ONE ||
476                 screen->net_conf_type == NET_CONF_TYPE_STATIC;
477
478         widget_set_visible(wl, show);
479         widget_set_visible(wf, show);
480
481         if (show)
482                 y += layout_pair(screen, y, screen->widgets.iface_l, wf) + 1;
483
484         /* conditionally show static IP params */
485         show = screen->net_conf_type == NET_CONF_TYPE_STATIC;
486
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;
492
493         if (show)
494                 layout_pair(screen, y, screen->widgets.ip_addr_l, wf);
495
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);
500
501         if (show) {
502                 widget_move(wl, y, x);
503                 widget_move(wf, y, x + 2);
504         }
505
506         /* help for IP/mask */
507         wh = widget_label_base(screen->widgets.ip_addr_mask_help_l);
508         widget_set_visible(wh, show);
509         if (show) {
510                 widget_move(wh, y, help_x);
511                 y++;
512         }
513
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);
520
521         if (show) {
522                 layout_pair(screen, y, screen->widgets.gateway_l, wf);
523                 widget_move(wh, y, help_x);
524                 y++;
525         }
526
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);
531         y++;
532
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);
537         if (show) {
538                 widget_move(wl, y, screen->field_x);
539                 y += 1;
540         }
541
542         y += 1;
543
544         show = screen->cui->config->safe_mode;
545         if (show) {
546                 widget_move(widget_label_base(screen->widgets.safe_mode),
547                         y, screen->field_x);
548                 y += 1;
549         }
550
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));
554
555         y += 1;
556
557         widget_move(widget_button_base(screen->widgets.ok_b),
558                         y, screen->field_x);
559         widget_move(widget_button_base(screen->widgets.help_b),
560                         y, screen->field_x + 14);
561         widget_move(widget_button_base(screen->widgets.cancel_b),
562                         y, screen->field_x + 28);
563 }
564
565 static void config_screen_network_change(void *arg, int value)
566 {
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);
572 }
573
574 static void config_screen_boot_order_change(void *arg, int value)
575 {
576         (void)value;
577         struct config_screen *screen = arg;
578         widgetset_unpost(screen->widgetset);
579         config_screen_layout_widgets(screen);
580         widgetset_post(screen->widgetset);
581 }
582
583 static void config_screen_add_device(void *arg)
584 {
585         struct config_screen *screen = arg;
586
587         screen->show_subset = true;
588         cui_show_subset(screen->cui, _("Select a boot device to add"),
589                         screen->widgets.boot_order_f);
590 }
591
592 static void config_screen_autoboot_none(void *arg)
593 {
594         struct config_screen *screen = arg;
595         struct nc_widget_subset *subset = screen->widgets.boot_order_f;
596
597         widget_subset_clear_active(subset);
598         screen->autoboot_enabled = false;
599
600         widgetset_unpost(screen->widgetset);
601         config_screen_layout_widgets(screen);
602         widgetset_post(screen->widgetset);
603 }
604
605 static void config_screen_autoboot_any(void *arg)
606 {
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;
610         int idx;
611
612         widget_subset_clear_active(subset);
613
614         idx = sysinfo->n_blockdevs + sysinfo->n_interfaces + DEVICE_TYPE_ANY;
615
616         widget_subset_make_active(screen->widgets.boot_order_f, idx);
617
618         screen->autoboot_enabled = true;
619
620         widgetset_unpost(screen->widgetset);
621         config_screen_layout_widgets(screen);
622         widgetset_post(screen->widgetset);
623 }
624
625 static void config_screen_update_subset(void *arg,
626                         struct nc_widget_subset *subset, int idx)
627 {
628         struct config_screen *screen = arg;
629
630         if (idx >= 0)
631                 widget_subset_make_active(subset, idx);
632         if (!screen->autoboot_enabled)
633                 screen->autoboot_enabled = true;
634         config_screen_layout_widgets(screen);
635 }
636
637 static struct interface_config *first_active_interface(
638                 const struct config *config)
639 {
640         unsigned int i;
641
642         for (i = 0; i < config->network.n_interfaces; i++) {
643                 if (config->network.interfaces[i]->ignore)
644                         continue;
645                 return config->network.interfaces[i];
646         }
647         return NULL;
648 }
649
650 static enum net_conf_type find_net_conf_type(const struct config *config)
651 {
652         struct interface_config *ifcfg;
653
654         ifcfg = first_active_interface(config);
655
656         if (!ifcfg)
657                 return NET_CONF_TYPE_DHCP_ALL;
658
659         else if (ifcfg->method == CONFIG_METHOD_DHCP)
660                 return NET_CONF_TYPE_DHCP_ONE;
661
662         else if (ifcfg->method == CONFIG_METHOD_STATIC)
663                 return NET_CONF_TYPE_STATIC;
664
665         assert(0);
666         return NET_CONF_TYPE_DHCP_ALL;
667 }
668
669 static void config_screen_setup_empty(struct config_screen *screen)
670 {
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);
676 }
677
678 static int find_autoboot_idx(const struct system_info *sysinfo,
679                 struct autoboot_option *opt)
680 {
681         unsigned int i;
682
683         if (opt->boot_type == BOOT_DEVICE_TYPE)
684                 return sysinfo->n_blockdevs + sysinfo->n_interfaces + opt->type;
685
686         for (i = 0; i < sysinfo->n_blockdevs; i++) {
687                 if (!strcmp(sysinfo->blockdevs[i]->uuid, opt->uuid))
688                         return i;
689         }
690
691         for (i = 0; i < sysinfo->n_interfaces; i++) {
692                 struct interface_info *info = sysinfo->interfaces[i];
693                 char mac[20];
694
695                 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
696
697                 if (!strcmp(mac, opt->uuid))
698                         return sysinfo->n_blockdevs + i;
699         }
700
701         return -1;
702 }
703
704 static void config_screen_setup_widgets(struct config_screen *screen,
705                 const struct config *config,
706                 const struct system_info *sysinfo)
707 {
708         struct nc_widgetset *set = screen->widgetset;
709         struct interface_config *ifcfg;
710         char *str, *ip, *mask, *gw;
711         enum net_conf_type type;
712         unsigned int i;
713         int add_len, clear_len, any_len, min_len = 20;
714
715         build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
716                         == N_FIELDS);
717
718         type = screen->net_conf_type;
719         ifcfg = first_active_interface(config);
720
721         add_len = max(min_len, strncols(_("Add Device")));
722         clear_len = max(min_len, strncols(_("Clear")));
723         any_len = max(min_len, strncols(_("Clear & Boot Any")));
724
725         screen->widgets.boot_add_b = widget_new_button(set, 0, 0, add_len,
726                                         _("Add Device"),
727                                         config_screen_add_device, screen);
728
729         screen->widgets.boot_none_b = widget_new_button(set, 0, 0, clear_len,
730                                         _("Clear"),
731                                         config_screen_autoboot_none, screen);
732
733         screen->widgets.boot_any_b = widget_new_button(set, 0, 0, any_len,
734                                         _("Clear & Boot Any"),
735                                         config_screen_autoboot_any, screen);
736
737         screen->widgets.boot_order_l = widget_new_label(set, 0, 0,
738                                         _("Boot Order:"));
739         screen->widgets.boot_order_f = widget_new_subset(set, 0, 0,
740                                         COLS - screen->field_x,
741                                         config_screen_update_subset);
742         screen->widgets.boot_empty_l = widget_new_label(set, 0, 0,
743                                         _("(None)"));
744
745         widget_subset_on_change(screen->widgets.boot_order_f,
746                         config_screen_boot_order_change, screen);
747
748         for (i = 0; i < sysinfo->n_blockdevs; i++) {
749                 struct blockdev_info *bd = sysinfo->blockdevs[i];
750                 char *label;
751
752                 label = talloc_asprintf(screen, _("disk: %s [uuid: %s]"),
753                                 bd->name, bd->uuid);
754
755                 widget_subset_add_option(screen->widgets.boot_order_f, label);
756         }
757
758         for (i = 0; i < sysinfo->n_interfaces; i++) {
759                 struct interface_info *info = sysinfo->interfaces[i];
760                 char *label, mac[20];
761
762                 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
763
764                 label = talloc_asprintf(screen, _("net:  %s [mac: %s]"),
765                                 info->name, mac);
766
767                 widget_subset_add_option(screen->widgets.boot_order_f, label);
768         }
769
770         for (i = DEVICE_TYPE_NETWORK; i < DEVICE_TYPE_UNKNOWN; i++) {
771                 char *label;
772
773                 if (i == DEVICE_TYPE_ANY)
774                         label = talloc_asprintf(screen, _("Any Device"));
775                 else
776                         label = talloc_asprintf(screen, _("Any %s device"),
777                                                 device_type_display_name(i));
778
779                 widget_subset_add_option(screen->widgets.boot_order_f, label);
780         }
781
782         screen->autoboot_enabled = config->n_autoboot_opts;
783         for (i = 0; i < config->n_autoboot_opts; i++) {
784                 struct autoboot_option *opt = &config->autoboot_opts[i];
785                 int idx;
786
787                 idx = find_autoboot_idx(sysinfo, opt);
788
789                 if (idx >= 0) {
790                         widget_subset_make_active(screen->widgets.boot_order_f,
791                                                   idx);
792                 } else {
793                         if (opt->boot_type == BOOT_DEVICE_TYPE)
794                                 pb_log("%s: Unknown autoboot option: %d\n",
795                                        __func__, opt->type);
796                         else
797                                 pb_log("%s: Unknown autoboot UUID: %s\n",
798                                        __func__, opt->uuid);
799                 }
800         }
801
802
803         str = talloc_asprintf(screen, "%d", config->autoboot_timeout_sec);
804         screen->widgets.timeout_l = widget_new_label(set, 0, 0, _("Timeout:"));
805         screen->widgets.timeout_f = widget_new_textbox(set, 0, 0, 5, str);
806         screen->widgets.timeout_help_l = widget_new_label(set, 0, 0,
807                                         _("seconds"));
808
809         widget_textbox_set_fixed_size(screen->widgets.timeout_f);
810         widget_textbox_set_validator_integer(screen->widgets.timeout_f, 0, 999);
811
812         if (config->ipmi_bootdev) {
813                 char *label = talloc_asprintf(screen,
814                                 _("%s IPMI boot option: %s"),
815                                 config->ipmi_bootdev_persistent ?
816                                 "Persistent" : "Temporary",
817                                 ipmi_bootdev_display_name(config->ipmi_bootdev));
818                 screen->widgets.ipmi_type_l = widget_new_label(set, 0, 0,
819                                                         label);
820                 screen->widgets.ipmi_clear_l = widget_new_label(set, 0, 0,
821                                                         _("Clear option:"));
822                 screen->widgets.ipmi_clear_cb = widget_new_checkbox(set, 0, 0,
823                                                         false);
824                 screen->ipmi_override = true;
825         }
826
827         screen->widgets.network_l = widget_new_label(set, 0, 0, _("Network:"));
828         screen->widgets.network_f = widget_new_select(set, 0, 0, 50);
829
830         widget_select_add_option(screen->widgets.network_f,
831                                         NET_CONF_TYPE_DHCP_ALL,
832                                         _("DHCP on all active interfaces"),
833                                         type == NET_CONF_TYPE_DHCP_ALL);
834         widget_select_add_option(screen->widgets.network_f,
835                                         NET_CONF_TYPE_DHCP_ONE,
836                                         _("DHCP on a specific interface"),
837                                         type == NET_CONF_TYPE_DHCP_ONE);
838         widget_select_add_option(screen->widgets.network_f,
839                                         NET_CONF_TYPE_STATIC,
840                                         _("Static IP configuration"),
841                                         type == NET_CONF_TYPE_STATIC);
842
843         widget_select_on_change(screen->widgets.network_f,
844                         config_screen_network_change, screen);
845
846         screen->widgets.iface_l = widget_new_label(set, 0, 0, _("Device:"));
847         screen->widgets.iface_f = widget_new_select(set, 0, 0, 50);
848
849         for (i = 0; i < sysinfo->n_interfaces; i++) {
850                 struct interface_info *info = sysinfo->interfaces[i];
851                 char str[50], mac[20];
852                 bool is_default;
853
854                 is_default = ifcfg && !memcmp(ifcfg->hwaddr, info->hwaddr,
855                                         sizeof(ifcfg->hwaddr));
856
857                 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
858                 snprintf(str, sizeof(str), "%s [%s, %s]", info->name, mac,
859                                 info->link ? _("link up") : _("link down"));
860
861                 widget_select_add_option(screen->widgets.iface_f,
862                                                 i, str, is_default);
863         }
864
865         gw = ip = mask = NULL;
866         if (ifcfg && ifcfg->method == CONFIG_METHOD_STATIC) {
867                 char *sep;
868
869                 str = talloc_strdup(screen, ifcfg->static_config.address);
870                 sep = strchr(str, '/');
871                 ip = str;
872
873                 if (sep) {
874                         *sep = '\0';
875                         mask = sep + 1;
876                 }
877                 gw = ifcfg->static_config.gateway;
878         }
879
880         screen->widgets.ip_addr_l = widget_new_label(set, 0, 0, _("IP/mask:"));
881         screen->widgets.ip_addr_f = widget_new_textbox(set, 0, 0, 16, ip);
882         screen->widgets.ip_mask_l = widget_new_label(set, 0, 0, "/");
883         screen->widgets.ip_mask_f = widget_new_textbox(set, 0, 0, 3, mask);
884         screen->widgets.ip_addr_mask_help_l =
885                 widget_new_label(set, 0, 0, _("(eg. 192.168.0.10 / 24)"));
886
887         widget_textbox_set_fixed_size(screen->widgets.ip_addr_f);
888         widget_textbox_set_fixed_size(screen->widgets.ip_mask_f);
889         widget_textbox_set_validator_ipv4(screen->widgets.ip_addr_f);
890         widget_textbox_set_validator_integer(screen->widgets.ip_mask_f, 1, 31);
891
892         screen->widgets.gateway_l = widget_new_label(set, 0, 0, _("Gateway:"));
893         screen->widgets.gateway_f = widget_new_textbox(set, 0, 0, 16, gw);
894         screen->widgets.gateway_help_l =
895                 widget_new_label(set, 0, 0, _("(eg. 192.168.0.1)"));
896
897         widget_textbox_set_fixed_size(screen->widgets.gateway_f);
898         widget_textbox_set_validator_ipv4(screen->widgets.gateway_f);
899
900         str = talloc_strdup(screen, "");
901         for (i = 0; i < config->network.n_dns_servers; i++) {
902                 str = talloc_asprintf_append(str, "%s%s",
903                                 (i == 0) ? "" : " ",
904                                 config->network.dns_servers[i]);
905         }
906
907         screen->widgets.dns_l = widget_new_label(set, 0, 0,
908                                         _("DNS Server(s):"));
909         screen->widgets.dns_f = widget_new_textbox(set, 0, 0, 32, str);
910         screen->widgets.dns_help_l =
911                 widget_new_label(set, 0, 0, _("(eg. 192.168.0.2)"));
912
913         widget_textbox_set_validator_ipv4_multi(screen->widgets.dns_f);
914
915         screen->widgets.dns_dhcp_help_l = widget_new_label(set, 0, 0,
916                         _("(if not provided by DHCP server)"));
917
918         if (config->safe_mode)
919                 screen->widgets.safe_mode = widget_new_label(set, 0, 0,
920                          _("Selecting 'OK' will exit safe mode"));
921
922         screen->widgets.allow_write_l = widget_new_label(set, 0, 0,
923                         _("Disk R/W:"));
924         screen->widgets.allow_write_f = widget_new_select(set, 0, 0,
925                                                 COLS - screen->field_x);
926
927         widget_select_add_option(screen->widgets.allow_write_f, 0,
928                                 _("Prevent all writes to disk"),
929                                 !config->allow_writes);
930         widget_select_add_option(screen->widgets.allow_write_f, 1,
931                                 _("Allow bootloader scripts to modify disks"),
932                                 config->allow_writes);
933
934         screen->widgets.ok_b = widget_new_button(set, 0, 0, 10, _("OK"),
935                         ok_click, screen);
936         screen->widgets.help_b = widget_new_button(set, 0, 0, 10, _("Help"),
937                         help_click, screen);
938         screen->widgets.cancel_b = widget_new_button(set, 0, 0, 10, _("Cancel"),
939                         cancel_click, screen);
940 }
941
942 static void config_screen_widget_focus(struct nc_widget *widget, void *arg)
943 {
944         struct config_screen *screen = arg;
945         int w_y, s_max;
946
947         w_y = widget_y(widget) + widget_focus_y(widget);
948         s_max = getmaxy(screen->scr.sub_ncw) - 1;
949
950         if (w_y < screen->scroll_y)
951                 screen->scroll_y = w_y;
952
953         else if (w_y + screen->scroll_y + 1 > s_max)
954                 screen->scroll_y = 1 + w_y - s_max;
955
956         else
957                 return;
958
959         pad_refresh(screen);
960 }
961
962 static void config_screen_draw(struct config_screen *screen,
963                 const struct config *config,
964                 const struct system_info *sysinfo)
965 {
966         bool repost = false;
967         int height;
968
969         /* The size of the pad we'll need depends on the number of interfaces.
970          *
971          * We use N_FIELDS (which is quite conservative, as some fields share
972          * a line) as a base, then:
973          * - add 6 (as the network select & boot device select fields take 3
974          *   lines each),
975          * - add n_interfaces for every field in the network select field, and
976          * - add (n_blockdevs + n_interfaces) for every field in the boot device
977          *   select field
978          */
979         height = N_FIELDS + 6;
980         if (sysinfo) {
981                 height += sysinfo->n_interfaces;
982                 height += (sysinfo->n_blockdevs + sysinfo->n_interfaces);
983         }
984         if (!screen->pad || getmaxy(screen->pad) < height) {
985                 if (screen->pad)
986                         delwin(screen->pad);
987                 screen->pad = newpad(height, COLS + 10);
988         }
989
990         if (screen->widgetset) {
991                 widgetset_unpost(screen->widgetset);
992                 talloc_free(screen->widgetset);
993                 repost = true;
994         }
995
996         screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
997                         screen->pad);
998         widgetset_set_widget_focus(screen->widgetset,
999                         config_screen_widget_focus, screen);
1000
1001         if (!config || !sysinfo) {
1002                 config_screen_setup_empty(screen);
1003         } else {
1004                 screen->net_conf_type = find_net_conf_type(config);
1005                 config_screen_setup_widgets(screen, config, sysinfo);
1006                 config_screen_layout_widgets(screen);
1007         }
1008
1009         if (repost)
1010                 widgetset_post(screen->widgetset);
1011 }
1012
1013 void config_screen_update(struct config_screen *screen,
1014                 const struct config *config,
1015                 const struct system_info *sysinfo)
1016 {
1017         config_screen_draw(screen, config, sysinfo);
1018         pad_refresh(screen);
1019 }
1020
1021 static int config_screen_destroy(void *arg)
1022 {
1023         struct config_screen *screen = arg;
1024         if (screen->pad)
1025                 delwin(screen->pad);
1026         return 0;
1027 }
1028
1029 struct config_screen *config_screen_init(struct cui *cui,
1030                 const struct config *config,
1031                 const struct system_info *sysinfo,
1032                 void (*on_exit)(struct cui *))
1033 {
1034         struct config_screen *screen;
1035
1036         screen = talloc_zero(cui, struct config_screen);
1037         talloc_set_destructor(screen, config_screen_destroy);
1038         nc_scr_init(&screen->scr, pb_config_screen_sig, 0,
1039                         cui, config_screen_process_key,
1040                         config_screen_post, config_screen_unpost,
1041                         config_screen_resize);
1042
1043         screen->cui = cui;
1044         screen->on_exit = on_exit;
1045         screen->need_redraw = false;
1046         screen->label_x = 2;
1047         screen->field_x = 17;
1048
1049         screen->ipmi_override = false;
1050         screen->show_subset = false;
1051
1052         screen->scr.frame.ltitle = talloc_strdup(screen,
1053                         _("Petitboot System Configuration"));
1054         screen->scr.frame.rtitle = NULL;
1055         screen->scr.frame.help = talloc_strdup(screen,
1056                         _("tab=next, shift+tab=previous, x=exit, h=help"));
1057         nc_scr_frame_draw(&screen->scr);
1058
1059         scrollok(screen->scr.sub_ncw, true);
1060
1061         config_screen_draw(screen, config, sysinfo);
1062
1063         return screen;
1064 }