discover: Maintain a backlog of status updates
[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        48
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         bool                    need_update;
57
58         void                    (*on_exit)(struct cui *);
59
60         int                     scroll_y;
61
62         int                     label_x;
63         int                     field_x;
64         int                     network_config_y;
65
66         enum net_conf_type      net_conf_type;
67
68         bool                    autoboot_enabled;
69         bool                    ipmi_override;
70         bool                    net_override;
71
72         struct {
73                 struct nc_widget_label          *autoboot_l;
74                 struct nc_widget_select         *autoboot_f;
75                 struct nc_widget_label          *boot_order_l;
76                 struct nc_widget_subset         *boot_order_f;
77                 struct nc_widget_label          *boot_empty_l;
78                 struct nc_widget_button         *boot_add_b;
79                 struct nc_widget_button         *boot_none_b;
80                 struct nc_widget_button         *boot_any_b;
81                 struct nc_widget_textbox        *timeout_f;
82                 struct nc_widget_label          *timeout_l;
83                 struct nc_widget_label          *timeout_help_l;
84
85                 struct nc_widget_label          *ipmi_type_l;
86                 struct nc_widget_label          *ipmi_clear_l;
87                 struct nc_widget_checkbox       *ipmi_clear_cb;
88
89                 struct nc_widget_label          *network_l;
90                 struct nc_widget_select         *network_f;
91
92                 struct nc_widget_label          *iface_l;
93                 struct nc_widget_select         *iface_f;
94                 struct nc_widget_label          *ip_addr_l;
95                 struct nc_widget_textbox        *ip_addr_f;
96                 struct nc_widget_label          *ip_mask_l;
97                 struct nc_widget_textbox        *ip_mask_f;
98                 struct nc_widget_label          *ip_addr_mask_help_l;
99                 struct nc_widget_label          *gateway_l;
100                 struct nc_widget_textbox        *gateway_f;
101                 struct nc_widget_label          *gateway_help_l;
102                 struct nc_widget_label          *url_l;
103                 struct nc_widget_textbox        *url_f;
104                 struct nc_widget_label          *url_help_l;
105                 struct nc_widget_label          *dns_l;
106                 struct nc_widget_textbox        *dns_f;
107                 struct nc_widget_label          *dns_dhcp_help_l;
108                 struct nc_widget_label          *dns_help_l;
109                 struct nc_widget_label          *http_proxy_l;
110                 struct nc_widget_textbox        *http_proxy_f;
111                 struct nc_widget_label          *https_proxy_l;
112                 struct nc_widget_textbox        *https_proxy_f;
113
114                 struct nc_widget_label          *allow_write_l;
115                 struct nc_widget_select         *allow_write_f;
116                 struct nc_widget_label          *boot_console_l;
117                 struct nc_widget_select         *boot_console_f;
118                 struct nc_widget_label          *manual_console_l;
119                 struct nc_widget_label          *current_console_l;
120
121                 struct nc_widget_label          *net_override_l;
122                 struct nc_widget_label          *safe_mode;
123                 struct nc_widget_button         *ok_b;
124                 struct nc_widget_button         *help_b;
125                 struct nc_widget_button         *cancel_b;
126         } widgets;
127 };
128
129 static struct config_screen *config_screen_from_scr(struct nc_scr *scr)
130 {
131         struct config_screen *config_screen;
132
133         assert(scr->sig == pb_config_screen_sig);
134         config_screen = (struct config_screen *)
135                 ((char *)scr - (size_t)&((struct config_screen *)0)->scr);
136         assert(config_screen->scr.sig == pb_config_screen_sig);
137         return config_screen;
138 }
139
140 static void pad_refresh(struct config_screen *screen)
141 {
142         int y, x, rows, cols;
143
144         getmaxyx(screen->scr.sub_ncw, rows, cols);
145         getbegyx(screen->scr.sub_ncw, y, x);
146
147         prefresh(screen->pad, screen->scroll_y, 0, y, x, rows, cols);
148 }
149
150 static void config_screen_process_key(struct nc_scr *scr, int key)
151 {
152         struct config_screen *screen = config_screen_from_scr(scr);
153         bool handled;
154
155         handled = widgetset_process_key(screen->widgetset, key);
156
157         if (!handled) {
158                 switch (key) {
159                 case 'x':
160                 case 27: /* esc */
161                         screen->exit = true;
162                         break;
163                 case 'h':
164                         screen->show_help = true;
165                         break;
166                 }
167         }
168
169         if (screen->exit) {
170                 screen->on_exit(screen->cui);
171
172         } else if (screen->show_help) {
173                 screen->show_help = false;
174                 screen->need_redraw = true;
175                 cui_show_help(screen->cui, _("System Configuration"),
176                                 &config_help_text);
177
178         } else if (handled && !screen->show_subset) {
179                 pad_refresh(screen);
180         }
181 }
182
183 static void config_screen_resize(struct nc_scr *scr)
184 {
185         struct config_screen *screen = config_screen_from_scr(scr);
186         (void)screen;
187 }
188
189 static int config_screen_unpost(struct nc_scr *scr)
190 {
191         struct config_screen *screen = config_screen_from_scr(scr);
192         widgetset_unpost(screen->widgetset);
193         return 0;
194 }
195
196 struct nc_scr *config_screen_scr(struct config_screen *screen)
197 {
198         return &screen->scr;
199 }
200
201 static int screen_process_form(struct config_screen *screen)
202 {
203         const struct system_info *sysinfo = screen->cui->sysinfo;
204         enum net_conf_type net_conf_type;
205         struct interface_config *iface;
206         bool allow_write, autoboot;
207         char *str, *end;
208         struct config *config;
209         int i, n_boot_opts, rc;
210         unsigned int *order, idx;
211         char mac[20];
212
213         config = config_copy(screen, screen->cui->config);
214
215         talloc_free(config->autoboot_opts);
216         config->n_autoboot_opts = 0;
217
218         n_boot_opts = widget_subset_get_order(config, &order,
219                                               screen->widgets.boot_order_f);
220
221         autoboot = widget_select_get_value(screen->widgets.autoboot_f);
222         config->autoboot_enabled = autoboot && n_boot_opts;
223
224         config->n_autoboot_opts = n_boot_opts;
225         config->autoboot_opts = talloc_array(config, struct autoboot_option,
226                                              n_boot_opts);
227
228         for (i = 0; i < n_boot_opts; i++) {
229                 if (order[i] < sysinfo->n_blockdevs) {
230                         /* disk uuid */
231                         config->autoboot_opts[i].boot_type = BOOT_DEVICE_UUID;
232                         config->autoboot_opts[i].uuid = talloc_strdup(config,
233                                                         sysinfo->blockdevs[order[i]]->uuid);
234                 } else if(order[i] < (sysinfo->n_blockdevs + sysinfo->n_interfaces)) {
235                         /* net uuid */
236                         order[i] -= sysinfo->n_blockdevs;
237                         config->autoboot_opts[i].boot_type = BOOT_DEVICE_UUID;
238                         mac_str(sysinfo->interfaces[order[i]]->hwaddr,
239                                 sysinfo->interfaces[order[i]]->hwaddr_size,
240                                 mac, sizeof(mac));
241                         config->autoboot_opts[i].uuid = talloc_strdup(config, mac);
242                 } else {
243                         /* device type */
244                         order[i] -= (sysinfo->n_blockdevs + sysinfo->n_interfaces);
245                         config->autoboot_opts[i].boot_type = BOOT_DEVICE_TYPE;
246                         config->autoboot_opts[i].type = order[i];
247                 }
248         }
249
250         str = widget_textbox_get_value(screen->widgets.timeout_f);
251         if (str) {
252                 unsigned long x;
253                 errno = 0;
254                 x = strtoul(str, &end, 10);
255                 if (!errno && end != str)
256                         config->autoboot_timeout_sec = x;
257         }
258
259         if (screen->ipmi_override)
260                 if (widget_checkbox_get_value(screen->widgets.ipmi_clear_cb))
261                         config->ipmi_bootdev = IPMI_BOOTDEV_INVALID;
262
263
264         net_conf_type = widget_select_get_value(screen->widgets.network_f);
265
266         /* if we don't have any network interfaces, prevent per-interface
267          * configuration */
268         if (sysinfo->n_interfaces == 0)
269                 net_conf_type = NET_CONF_TYPE_DHCP_ALL;
270
271         if (net_conf_type == NET_CONF_TYPE_DHCP_ALL) {
272                 config->network.n_interfaces = 0;
273
274         } else {
275                 iface = talloc_zero(config, struct interface_config);
276                 config->network.n_interfaces = 1;
277                 config->network.interfaces = talloc_array(config,
278                                 struct interface_config *, 1);
279                 config->network.interfaces[0] = iface;
280
281                 /* copy hwaddr (from the sysinfo interface data) to
282                  * the configuration */
283                 idx = widget_select_get_value(screen->widgets.iface_f);
284                 memcpy(iface->hwaddr, sysinfo->interfaces[idx]->hwaddr,
285                                 sizeof(iface->hwaddr));
286         }
287
288         if (net_conf_type == NET_CONF_TYPE_DHCP_ONE) {
289                 iface->method = CONFIG_METHOD_DHCP;
290         }
291
292         if (net_conf_type == NET_CONF_TYPE_STATIC) {
293                 char *ip, *mask, *gateway, *url;
294
295                 ip = widget_textbox_get_value(screen->widgets.ip_addr_f);
296                 mask = widget_textbox_get_value(screen->widgets.ip_mask_f);
297                 gateway = widget_textbox_get_value(screen->widgets.gateway_f);
298                 url = widget_textbox_get_value(screen->widgets.url_f);
299
300                 if (!ip || !*ip || !mask || !*mask) {
301                         screen->scr.frame.status =
302                                 _("No IP / mask values are set");
303                         nc_scr_frame_draw(&screen->scr);
304                         talloc_free(config);
305                         return -1;
306                 }
307
308                 iface->method = CONFIG_METHOD_STATIC;
309                 iface->static_config.address = talloc_asprintf(iface, "%s/%s",
310                                 ip, mask);
311                 iface->static_config.gateway = talloc_strdup(iface, gateway);
312                 iface->static_config.url = talloc_strdup(iface, url);
313         }
314
315         str = widget_textbox_get_value(screen->widgets.dns_f);
316         talloc_free(config->network.dns_servers);
317         config->network.dns_servers = NULL;
318         config->network.n_dns_servers = 0;
319
320         if (str && strlen(str)) {
321                 char *dns, *tmp;
322                 int i;
323
324                 for (;;) {
325                         dns = strtok_r(str, " \t", &tmp);
326
327                         if (!dns)
328                                 break;
329
330                         i = config->network.n_dns_servers++;
331                         config->network.dns_servers = talloc_realloc(config,
332                                         config->network.dns_servers,
333                                         const char *,
334                                         config->network.n_dns_servers);
335                         config->network.dns_servers[i] =
336                                 talloc_strdup(config, dns);
337
338                         str = NULL;
339                 }
340         }
341
342         talloc_free(config->http_proxy);
343         talloc_free(config->https_proxy);
344         str = widget_textbox_get_value(screen->widgets.http_proxy_f);
345         config->http_proxy = talloc_strdup(config, str);
346         str = widget_textbox_get_value(screen->widgets.https_proxy_f);
347         config->https_proxy = talloc_strdup(config, str);
348
349         allow_write = widget_select_get_value(screen->widgets.allow_write_f);
350         if (allow_write != config->allow_writes)
351                 config->allow_writes = allow_write;
352
353         if (config->n_consoles && !config->manual_console) {
354                 idx = widget_select_get_value(screen->widgets.boot_console_f);
355                 if (!config->boot_console) {
356                         config->boot_console = talloc_strdup(config,
357                                                         config->consoles[idx]);
358                 } else if (strncmp(config->boot_console, config->consoles[idx],
359                                 strlen(config->boot_console)) != 0) {
360                         talloc_free(config->boot_console);
361                         config->boot_console = talloc_strdup(config,
362                                                         config->consoles[idx]);
363                 }
364         }
365
366         config->safe_mode = false;
367         rc = cui_send_config(screen->cui, config);
368         talloc_free(config);
369
370         if (rc)
371                 pb_log("cui_send_config failed!\n");
372         else
373                 pb_debug("config sent!\n");
374
375         return 0;
376 }
377
378 static void ok_click(void *arg)
379 {
380         struct config_screen *screen = arg;
381         if (screen_process_form(screen))
382                 /* errors are written to the status line, so we'll need
383                  * to refresh */
384                 wrefresh(screen->scr.main_ncw);
385         else
386                 screen->exit = true;
387 }
388
389 static void help_click(void *arg)
390 {
391         struct config_screen *screen = arg;
392         screen->show_help = true;
393 }
394
395 static void cancel_click(void *arg)
396 {
397         struct config_screen *screen = arg;
398         screen->exit = true;
399 }
400
401 static int layout_pair(struct config_screen *screen, int y,
402                 struct nc_widget_label *label,
403                 struct nc_widget *field)
404 {
405         struct nc_widget *label_w = widget_label_base(label);
406         widget_move(label_w, y, screen->label_x);
407         widget_move(field, y, screen->field_x);
408         return max(widget_height(label_w), widget_height(field));
409 }
410
411 static void config_screen_layout_widgets(struct config_screen *screen)
412 {
413         struct nc_widget *wl, *wf, *wh;
414         int y, x, help_x;
415         bool show;
416
417         y = 1;
418         /* currently, the longest label we have is the DNS-servers
419          * widget, so layout our screen based on that */
420         help_x = screen->field_x + 2 +
421                 widget_width(widget_textbox_base(screen->widgets.dns_f));
422
423         wl = widget_label_base(screen->widgets.autoboot_l);
424         widget_set_visible(wl, true);
425         widget_move(wl, y, screen->label_x);
426
427         wf = widget_select_base(screen->widgets.autoboot_f);
428         widget_set_visible(wf, true);
429         widget_move(wf, y, screen->field_x);
430         y += widget_height(wf);
431
432         show = screen->autoboot_enabled;
433
434         if (show)
435                 y += 1;
436
437         wl = widget_label_base(screen->widgets.boot_order_l);
438         widget_set_visible(wl, show);
439         widget_move(wl, y, screen->label_x);
440
441         wf = widget_subset_base(screen->widgets.boot_order_f);
442         widget_move(wf, y, screen->field_x);
443         wl = widget_label_base(screen->widgets.boot_empty_l);
444         widget_move(wl, y, screen->field_x);
445
446         if (widget_subset_height(screen->widgets.boot_order_f)) {
447                 widget_set_visible(wl, false);
448                 widget_set_visible(wf, show);
449                 y += show ? widget_height(wf) : 0;
450         } else {
451                 widget_set_visible(wl, show);
452                 widget_set_visible(wf, false);
453                 y += show ? 1 : 0;
454         }
455
456         if (show) {
457                 y += 1;
458                 widget_move(widget_button_base(screen->widgets.boot_add_b),
459                                 y++, screen->field_x);
460                 widget_move(widget_button_base(screen->widgets.boot_any_b),
461                                 y++, screen->field_x);
462                 widget_move(widget_button_base(screen->widgets.boot_none_b),
463                                 y, screen->field_x);
464         }
465
466         wf = widget_button_base(screen->widgets.boot_add_b);
467         if (widget_subset_n_inactive(screen->widgets.boot_order_f) && show)
468                 widget_set_visible(wf, true);
469         else
470                 widget_set_visible(wf, false);
471
472         if (show)
473                 y += 2;
474
475         widget_set_visible(widget_button_base(screen->widgets.boot_any_b), show);
476         widget_set_visible(widget_button_base(screen->widgets.boot_none_b), show);
477
478         wf = widget_textbox_base(screen->widgets.timeout_f);
479         wl = widget_label_base(screen->widgets.timeout_l);
480         wh = widget_label_base(screen->widgets.timeout_help_l);
481         widget_set_visible(wl, screen->autoboot_enabled);
482         widget_set_visible(wf, screen->autoboot_enabled);
483         widget_set_visible(wh, screen->autoboot_enabled);
484         if (screen->autoboot_enabled) {
485                 widget_set_visible(wh, screen->autoboot_enabled);
486                 widget_move(wl, y, screen->label_x);
487                 widget_move(wf, y, screen->field_x);
488                 widget_move(wh, y, screen->field_x + widget_width(wf) + 1);
489                 y += 2;
490         } else
491                 y += 1;
492
493         if (screen->ipmi_override) {
494                 wl = widget_label_base(screen->widgets.ipmi_type_l);
495                 widget_set_visible(wl, true);
496                 widget_move(wl, y, screen->label_x);
497                 y += 1;
498
499                 wl = widget_label_base(screen->widgets.ipmi_clear_l);
500                 wf = widget_checkbox_base(screen->widgets.ipmi_clear_cb);
501                 widget_set_visible(wl, true);
502                 widget_set_visible(wf, true);
503                 widget_move(wl, y, screen->label_x);
504                 widget_move(wf, y, screen->field_x);
505                 y += 1;
506         }
507
508         y += 1;
509
510         y += layout_pair(screen, y, screen->widgets.network_l,
511                         widget_select_base(screen->widgets.network_f));
512
513         y += 1;
514
515         /* conditionally show iface select */
516         wl = widget_label_base(screen->widgets.iface_l);
517         wf = widget_select_base(screen->widgets.iface_f);
518
519         show = screen->net_conf_type == NET_CONF_TYPE_DHCP_ONE ||
520                 screen->net_conf_type == NET_CONF_TYPE_STATIC;
521
522         widget_set_visible(wl, show);
523         widget_set_visible(wf, show);
524
525         if (show)
526                 y += layout_pair(screen, y, screen->widgets.iface_l, wf) + 1;
527
528         /* conditionally show static IP params */
529         show = screen->net_conf_type == NET_CONF_TYPE_STATIC;
530
531         wl = widget_label_base(screen->widgets.ip_addr_l);
532         wf = widget_textbox_base(screen->widgets.ip_addr_f);
533         widget_set_visible(wl, show);
534         widget_set_visible(wf, show);
535         x = screen->field_x + widget_width(wf) + 1;
536
537         if (show)
538                 layout_pair(screen, y, screen->widgets.ip_addr_l, wf);
539
540         wl = widget_label_base(screen->widgets.ip_mask_l);
541         wf = widget_textbox_base(screen->widgets.ip_mask_f);
542         widget_set_visible(wl, show);
543         widget_set_visible(wf, show);
544
545         if (show) {
546                 widget_move(wl, y, x);
547                 widget_move(wf, y, x + 2);
548         }
549
550         /* help for IP/mask */
551         wh = widget_label_base(screen->widgets.ip_addr_mask_help_l);
552         widget_set_visible(wh, show);
553         if (show) {
554                 widget_move(wh, y, help_x);
555                 y++;
556         }
557
558         wl = widget_label_base(screen->widgets.gateway_l);
559         wf = widget_textbox_base(screen->widgets.gateway_f);
560         wh = widget_label_base(screen->widgets.gateway_help_l);
561         widget_set_visible(wl, show);
562         widget_set_visible(wf, show);
563         widget_set_visible(wh, show);
564
565         if (show) {
566                 layout_pair(screen, y, screen->widgets.gateway_l, wf);
567                 widget_move(wh, y, help_x);
568                 y++;
569         }
570
571         wl = widget_label_base(screen->widgets.url_l);
572         wf = widget_textbox_base(screen->widgets.url_f);
573         wh = widget_label_base(screen->widgets.url_help_l);
574         widget_set_visible(wl, show);
575         widget_set_visible(wf, show);
576         widget_set_visible(wh, show);
577
578         if (show) {
579                 layout_pair(screen, y, screen->widgets.url_l, wf);
580                 widget_move(wh, y, help_x);
581                 y++;
582         }
583
584         wh = widget_label_base(screen->widgets.dns_help_l);
585         layout_pair(screen, y, screen->widgets.dns_l,
586                         widget_textbox_base(screen->widgets.dns_f));
587         widget_move(wh, y, help_x);
588         y++;
589
590         /* we show the DNS/DHCP help if we're configuring DHCP */
591         show = screen->net_conf_type != NET_CONF_TYPE_STATIC;
592         wl = widget_label_base(screen->widgets.dns_dhcp_help_l);
593         widget_set_visible(wl, show);
594         if (show) {
595                 widget_move(wl, y, screen->field_x);
596                 y += 1;
597         }
598
599         wf = widget_textbox_base(screen->widgets.http_proxy_f);
600         layout_pair(screen, y, screen->widgets.http_proxy_l, wf);
601         y++;
602         wf = widget_textbox_base(screen->widgets.https_proxy_f);
603         layout_pair(screen, y, screen->widgets.https_proxy_l, wf);
604         y++;
605
606         y += 1;
607
608         layout_pair(screen, y, screen->widgets.allow_write_l,
609                     widget_select_base(screen->widgets.allow_write_f));
610         y += widget_height(widget_select_base(screen->widgets.allow_write_f));
611
612         y += 1;
613
614         if (screen->widgets.manual_console_l) {
615                 layout_pair(screen, y++, screen->widgets.boot_console_l,
616                         widget_label_base(screen->widgets.manual_console_l));
617                 widget_move(widget_label_base(screen->widgets.current_console_l),
618                         y, screen->field_x);
619                 widget_set_visible(widget_select_base(
620                         screen->widgets.boot_console_f), false);
621                 y += 2;
622         } else if (widget_height(widget_select_base(screen->widgets.boot_console_f))) {
623                 layout_pair(screen, y, screen->widgets.boot_console_l,
624                             widget_select_base(screen->widgets.boot_console_f));
625                 y += widget_height(widget_select_base(screen->widgets.boot_console_f));
626                 widget_move(widget_label_base(screen->widgets.current_console_l),
627                         y, screen->field_x);
628                 y += 2;
629         } else {
630                 widget_set_visible(widget_label_base(
631                                         screen->widgets.boot_console_l), false);
632                 widget_set_visible(widget_select_base(
633                                         screen->widgets.boot_console_f), false);
634                 widget_set_visible(widget_label_base(
635                                         screen->widgets.current_console_l), false);
636         }
637
638         if (screen->net_override) {
639                 widget_move(widget_label_base(screen->widgets.net_override_l),
640                                 y, screen->label_x);
641                 widget_set_visible(widget_label_base(screen->widgets.net_override_l),
642                                         true);
643                 y += 1;
644         }
645
646         if (screen->cui->config->safe_mode) {
647                 widget_move(widget_label_base(screen->widgets.safe_mode),
648                         y, screen->label_x);
649                 widget_set_visible(widget_label_base(screen->widgets.safe_mode),
650                                         true);
651                 y += 1;
652         }
653
654         widget_move(widget_button_base(screen->widgets.ok_b),
655                         y, screen->field_x);
656         widget_move(widget_button_base(screen->widgets.help_b),
657                         y, screen->field_x + 14);
658         widget_move(widget_button_base(screen->widgets.cancel_b),
659                         y, screen->field_x + 28);
660 }
661
662 static void config_screen_network_change(void *arg, int value)
663 {
664         struct config_screen *screen = arg;
665         screen->net_conf_type = value;
666         widgetset_unpost(screen->widgetset);
667         config_screen_layout_widgets(screen);
668         widgetset_post(screen->widgetset);
669 }
670
671 static void config_screen_boot_order_change(void *arg, int value)
672 {
673         (void)value;
674         struct config_screen *screen = arg;
675         widgetset_unpost(screen->widgetset);
676         config_screen_layout_widgets(screen);
677         widgetset_post(screen->widgetset);
678 }
679
680 static void config_screen_autoboot_change(void *arg, int value)
681 {
682         struct config_screen *screen = arg;
683         screen->autoboot_enabled = !!value;
684         widgetset_unpost(screen->widgetset);
685         config_screen_layout_widgets(screen);
686         widgetset_post(screen->widgetset);
687 }
688
689 static void config_screen_add_device(void *arg)
690 {
691         struct config_screen *screen = arg;
692
693         screen->show_subset = true;
694         cui_show_subset(screen->cui, _("Select a boot device to add"),
695                         screen->widgets.boot_order_f);
696 }
697
698 static void config_screen_autoboot_none(void *arg)
699 {
700         struct config_screen *screen = arg;
701         struct nc_widget_subset *subset = screen->widgets.boot_order_f;
702
703         widget_subset_clear_active(subset);
704
705         widgetset_unpost(screen->widgetset);
706         config_screen_layout_widgets(screen);
707         widgetset_post(screen->widgetset);
708 }
709
710 static void config_screen_autoboot_any(void *arg)
711 {
712         struct config_screen *screen = arg;
713         const struct system_info *sysinfo = screen->cui->sysinfo;
714         struct nc_widget_subset *subset = screen->widgets.boot_order_f;
715         int idx;
716
717         widget_subset_clear_active(subset);
718
719         idx = sysinfo->n_blockdevs + sysinfo->n_interfaces + DEVICE_TYPE_ANY;
720
721         widget_subset_make_active(screen->widgets.boot_order_f, idx);
722
723         screen->autoboot_enabled = true;
724
725         widgetset_unpost(screen->widgetset);
726         config_screen_layout_widgets(screen);
727         widgetset_post(screen->widgetset);
728 }
729
730 static void config_screen_update_subset(void *arg,
731                         struct nc_widget_subset *subset, int idx)
732 {
733         struct config_screen *screen = arg;
734
735         if (idx >= 0)
736                 widget_subset_make_active(subset, idx);
737         if (!screen->autoboot_enabled)
738                 screen->autoboot_enabled = true;
739         config_screen_layout_widgets(screen);
740 }
741
742 static struct interface_config *first_active_interface(
743                 const struct config *config)
744 {
745         unsigned int i;
746
747         for (i = 0; i < config->network.n_interfaces; i++) {
748                 if (config->network.interfaces[i]->ignore)
749                         continue;
750                 return config->network.interfaces[i];
751         }
752         return NULL;
753 }
754
755 static enum net_conf_type find_net_conf_type(const struct config *config)
756 {
757         struct interface_config *ifcfg;
758
759         ifcfg = first_active_interface(config);
760
761         if (!ifcfg)
762                 return NET_CONF_TYPE_DHCP_ALL;
763
764         else if (ifcfg->method == CONFIG_METHOD_DHCP)
765                 return NET_CONF_TYPE_DHCP_ONE;
766
767         else if (ifcfg->method == CONFIG_METHOD_STATIC)
768                 return NET_CONF_TYPE_STATIC;
769
770         assert(0);
771         return NET_CONF_TYPE_DHCP_ALL;
772 }
773
774 static void config_screen_setup_empty(struct config_screen *screen)
775 {
776         widget_new_label(screen->widgetset, 2, screen->field_x,
777                         _("Waiting for configuration data..."));
778         screen->widgets.cancel_b = widget_new_button(screen->widgetset,
779                         4, screen->field_x, 9, _("Cancel"),
780                         cancel_click, screen);
781 }
782
783 static int find_autoboot_idx(const struct system_info *sysinfo,
784                 struct autoboot_option *opt)
785 {
786         unsigned int i;
787
788         if (opt->boot_type == BOOT_DEVICE_TYPE)
789                 return sysinfo->n_blockdevs + sysinfo->n_interfaces + opt->type;
790
791         for (i = 0; i < sysinfo->n_blockdevs; i++) {
792                 if (!strcmp(sysinfo->blockdevs[i]->uuid, opt->uuid))
793                         return i;
794         }
795
796         for (i = 0; i < sysinfo->n_interfaces; i++) {
797                 struct interface_info *info = sysinfo->interfaces[i];
798                 char mac[20];
799
800                 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
801
802                 if (!strcmp(mac, opt->uuid))
803                         return sysinfo->n_blockdevs + i;
804         }
805
806         return -1;
807 }
808
809 static void config_screen_setup_widgets(struct config_screen *screen,
810                 const struct config *config,
811                 const struct system_info *sysinfo)
812 {
813         struct nc_widgetset *set = screen->widgetset;
814         struct interface_config *ifcfg;
815         char *str, *ip, *mask, *gw, *url, *tty, *label;
816         enum net_conf_type type;
817         unsigned int i;
818         int add_len, clear_len, any_len, min_len = 20;
819         bool found;
820
821         build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
822                         == N_FIELDS);
823
824         type = screen->net_conf_type;
825         ifcfg = first_active_interface(config);
826
827         screen->autoboot_enabled = config->autoboot_enabled;
828
829         screen->widgets.autoboot_l = widget_new_label(set, 0, 0,
830                                         _("Autoboot:"));
831         screen->widgets.autoboot_f = widget_new_select(set, 0, 0,
832                                         COLS - screen->field_x - 1);
833
834         widget_select_add_option(screen->widgets.autoboot_f, 0, _("Disabled"),
835                                  !screen->autoboot_enabled);
836         widget_select_add_option(screen->widgets.autoboot_f, 1, _("Enabled"),
837                                  screen->autoboot_enabled);
838
839         widget_select_on_change(screen->widgets.autoboot_f,
840                         config_screen_autoboot_change, screen);
841
842         add_len = max(min_len, strncols(_("Add Device")));
843         clear_len = max(min_len, strncols(_("Clear")));
844         any_len = max(min_len, strncols(_("Clear & Boot Any")));
845
846         screen->widgets.boot_add_b = widget_new_button(set, 0, 0, add_len,
847                                         _("Add Device"),
848                                         config_screen_add_device, screen);
849
850         screen->widgets.boot_none_b = widget_new_button(set, 0, 0, clear_len,
851                                         _("Clear"),
852                                         config_screen_autoboot_none, screen);
853
854         screen->widgets.boot_any_b = widget_new_button(set, 0, 0, any_len,
855                                         _("Clear & Boot Any"),
856                                         config_screen_autoboot_any, screen);
857
858         screen->widgets.boot_order_l = widget_new_label(set, 0, 0,
859                                         _("Boot Order:"));
860         screen->widgets.boot_order_f = widget_new_subset(set, 0, 0,
861                                         COLS - screen->field_x,
862                                         config_screen_update_subset);
863         screen->widgets.boot_empty_l = widget_new_label(set, 0, 0,
864                                         _("(None)"));
865
866         widget_subset_on_change(screen->widgets.boot_order_f,
867                         config_screen_boot_order_change, screen);
868
869         for (i = 0; i < sysinfo->n_blockdevs; i++) {
870                 struct blockdev_info *bd = sysinfo->blockdevs[i];
871
872                 label = talloc_asprintf(screen, _("disk: %s [uuid: %s]"),
873                                 bd->name, bd->uuid);
874
875                 widget_subset_add_option(screen->widgets.boot_order_f, label);
876         }
877
878         for (i = 0; i < sysinfo->n_interfaces; i++) {
879                 struct interface_info *info = sysinfo->interfaces[i];
880                 char mac[20];
881
882                 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
883
884                 label = talloc_asprintf(screen, _("net:  %s [mac: %s]"),
885                                 info->name, mac);
886
887                 widget_subset_add_option(screen->widgets.boot_order_f, label);
888         }
889
890         for (i = DEVICE_TYPE_NETWORK; i < DEVICE_TYPE_UNKNOWN; i++) {
891
892                 if (i == DEVICE_TYPE_ANY)
893                         label = talloc_asprintf(screen, _("Any Device"));
894                 else
895                         label = talloc_asprintf(screen, _("Any %s device"),
896                                                 device_type_display_name(i));
897
898                 widget_subset_add_option(screen->widgets.boot_order_f, label);
899         }
900
901         for (i = 0; i < config->n_autoboot_opts; i++) {
902                 struct autoboot_option *opt = &config->autoboot_opts[i];
903                 int idx;
904
905                 idx = find_autoboot_idx(sysinfo, opt);
906
907                 if (idx >= 0) {
908                         widget_subset_make_active(screen->widgets.boot_order_f,
909                                                   idx);
910                 } else {
911                         if (opt->boot_type == BOOT_DEVICE_TYPE)
912                                 pb_log("%s: Unknown autoboot option: %d\n",
913                                        __func__, opt->type);
914                         else
915                                 pb_log("%s: Unknown autoboot UUID: %s\n",
916                                        __func__, opt->uuid);
917                 }
918         }
919
920
921         str = talloc_asprintf(screen, "%d", config->autoboot_timeout_sec);
922         screen->widgets.timeout_l = widget_new_label(set, 0, 0, _("Timeout:"));
923         screen->widgets.timeout_f = widget_new_textbox(set, 0, 0, 5, str);
924         screen->widgets.timeout_help_l = widget_new_label(set, 0, 0,
925                                         _("seconds"));
926
927         widget_textbox_set_fixed_size(screen->widgets.timeout_f);
928         widget_textbox_set_validator_integer(screen->widgets.timeout_f, 0, 999);
929
930         if (config->ipmi_bootdev) {
931                 label = talloc_asprintf(screen,
932                                 _("%s IPMI boot option: %s"),
933                                 config->ipmi_bootdev_persistent ?
934                                 "Persistent" : "Temporary",
935                                 ipmi_bootdev_display_name(config->ipmi_bootdev));
936                 screen->widgets.ipmi_type_l = widget_new_label(set, 0, 0,
937                                                         label);
938                 screen->widgets.ipmi_clear_l = widget_new_label(set, 0, 0,
939                                                         _("Clear option:"));
940                 screen->widgets.ipmi_clear_cb = widget_new_checkbox(set, 0, 0,
941                                                         false);
942                 screen->ipmi_override = true;
943         }
944
945         screen->widgets.network_l = widget_new_label(set, 0, 0, _("Network:"));
946         screen->widgets.network_f = widget_new_select(set, 0, 0,
947                                                 COLS - screen->field_x - 1);
948
949         widget_select_add_option(screen->widgets.network_f,
950                                         NET_CONF_TYPE_DHCP_ALL,
951                                         _("DHCP on all active interfaces"),
952                                         type == NET_CONF_TYPE_DHCP_ALL);
953         widget_select_add_option(screen->widgets.network_f,
954                                         NET_CONF_TYPE_DHCP_ONE,
955                                         _("DHCP on a specific interface"),
956                                         type == NET_CONF_TYPE_DHCP_ONE);
957         widget_select_add_option(screen->widgets.network_f,
958                                         NET_CONF_TYPE_STATIC,
959                                         _("Static IP configuration"),
960                                         type == NET_CONF_TYPE_STATIC);
961
962         widget_select_on_change(screen->widgets.network_f,
963                         config_screen_network_change, screen);
964
965         screen->widgets.iface_l = widget_new_label(set, 0, 0, _("Device:"));
966         screen->widgets.iface_f = widget_new_select(set, 0, 0, 50);
967
968         for (i = 0; i < sysinfo->n_interfaces; i++) {
969                 struct interface_info *info = sysinfo->interfaces[i];
970                 char str[50], mac[20];
971                 bool is_default;
972
973                 is_default = ifcfg && !memcmp(ifcfg->hwaddr, info->hwaddr,
974                                         sizeof(ifcfg->hwaddr));
975
976                 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
977                 snprintf(str, sizeof(str), "%s [%s, %s]", info->name, mac,
978                                 info->link ? _("link up") : _("link down"));
979
980                 widget_select_add_option(screen->widgets.iface_f,
981                                                 i, str, is_default);
982         }
983
984         url = gw = ip = mask = NULL;
985         if (ifcfg && ifcfg->method == CONFIG_METHOD_STATIC) {
986                 char *sep;
987
988                 str = talloc_strdup(screen, ifcfg->static_config.address);
989                 sep = strchr(str, '/');
990                 ip = str;
991
992                 if (sep) {
993                         *sep = '\0';
994                         mask = sep + 1;
995                 }
996                 gw = ifcfg->static_config.gateway;
997                 url = ifcfg->static_config.url;
998         }
999
1000         screen->net_override = ifcfg && ifcfg->override;
1001         if (screen->net_override) {
1002                 screen->widgets.net_override_l = widget_new_label(set, 0, 0,
1003                         _("Network Override Active! 'OK' will overwrite interface config"));
1004         }
1005
1006         screen->widgets.ip_addr_l = widget_new_label(set, 0, 0, _("IP/mask:"));
1007         screen->widgets.ip_addr_f = widget_new_textbox(set, 0, 0, 16, ip);
1008         screen->widgets.ip_mask_l = widget_new_label(set, 0, 0, "/");
1009         screen->widgets.ip_mask_f = widget_new_textbox(set, 0, 0, 3, mask);
1010         screen->widgets.ip_addr_mask_help_l =
1011                 widget_new_label(set, 0, 0, _("(eg. 192.168.0.10 / 24)"));
1012
1013         widget_textbox_set_fixed_size(screen->widgets.ip_addr_f);
1014         widget_textbox_set_fixed_size(screen->widgets.ip_mask_f);
1015         widget_textbox_set_validator_ipv4(screen->widgets.ip_addr_f);
1016         widget_textbox_set_validator_integer(screen->widgets.ip_mask_f, 1, 31);
1017
1018         screen->widgets.gateway_l = widget_new_label(set, 0, 0, _("Gateway:"));
1019         screen->widgets.gateway_f = widget_new_textbox(set, 0, 0, 16, gw);
1020         screen->widgets.gateway_help_l =
1021                 widget_new_label(set, 0, 0, _("(eg. 192.168.0.1)"));
1022
1023         widget_textbox_set_fixed_size(screen->widgets.gateway_f);
1024         widget_textbox_set_validator_ipv4(screen->widgets.gateway_f);
1025
1026         screen->widgets.url_l = widget_new_label(set, 0, 0, _("URL:"));
1027         screen->widgets.url_f = widget_new_textbox(set, 0, 0, 32, url);
1028         screen->widgets.url_help_l =
1029                 widget_new_label(set, 0, 0, _("(eg. tftp://)"));
1030
1031         str = talloc_strdup(screen, "");
1032         for (i = 0; i < config->network.n_dns_servers; i++) {
1033                 str = talloc_asprintf_append(str, "%s%s",
1034                                 (i == 0) ? "" : " ",
1035                                 config->network.dns_servers[i]);
1036         }
1037
1038         screen->widgets.dns_l = widget_new_label(set, 0, 0,
1039                                         _("DNS Server(s):"));
1040         screen->widgets.dns_f = widget_new_textbox(set, 0, 0, 32, str);
1041         screen->widgets.dns_help_l =
1042                 widget_new_label(set, 0, 0, _("(eg. 192.168.0.2)"));
1043
1044         widget_textbox_set_validator_ipv4_multi(screen->widgets.dns_f);
1045
1046         screen->widgets.dns_dhcp_help_l = widget_new_label(set, 0, 0,
1047                         _("(if not provided by DHCP server)"));
1048
1049         screen->widgets.http_proxy_l = widget_new_label(set, 0, 0,
1050                                         _("HTTP Proxy:"));
1051         screen->widgets.http_proxy_f = widget_new_textbox(set, 0, 0, 32,
1052                                                 config->http_proxy);
1053         screen->widgets.https_proxy_l = widget_new_label(set, 0, 0,
1054                                         _("HTTPS Proxy:"));
1055         screen->widgets.https_proxy_f = widget_new_textbox(set, 0, 0, 32,
1056                                                 config->https_proxy);
1057
1058         if (config->safe_mode)
1059                 screen->widgets.safe_mode = widget_new_label(set, 0, 0,
1060                          _("Selecting 'OK' will exit safe mode"));
1061
1062         screen->widgets.allow_write_l = widget_new_label(set, 0, 0,
1063                         _("Disk R/W:"));
1064         screen->widgets.allow_write_f = widget_new_select(set, 0, 0,
1065                                                 COLS - screen->field_x - 1);
1066
1067         widget_select_add_option(screen->widgets.allow_write_f, 0,
1068                                 _("Prevent all writes to disk"),
1069                                 !config->allow_writes);
1070
1071         widget_select_add_option(screen->widgets.allow_write_f, 1,
1072                                 _("Allow bootloader scripts to modify disks"),
1073                                 config->allow_writes);
1074
1075         screen->widgets.boot_console_l = widget_new_label(set, 0, 0,
1076                         _("Boot console:"));
1077         screen->widgets.boot_console_f = widget_new_select(set, 0, 0,
1078                                                 COLS - screen->field_x - 1);
1079
1080         for (i = 0; i < config->n_consoles; i++){
1081                 found = config->boot_console &&
1082                         strncmp(config->boot_console, config->consoles[i],
1083                                 strlen(config->boot_console)) == 0;
1084                 widget_select_add_option(screen->widgets.boot_console_f, i,
1085                                         config->consoles[i], found);
1086         }
1087
1088         if (config->manual_console) {
1089                 label = talloc_asprintf(screen, _("Manually set: '%s'"),
1090                                         config->boot_console);
1091                 screen->widgets.manual_console_l = widget_new_label(set, 0, 0, label);
1092         }
1093
1094         tty = talloc_asprintf(screen, _("Current interface: %s"),
1095                                 ttyname(STDIN_FILENO));
1096         screen->widgets.current_console_l = widget_new_label(set, 0 , 0, tty);
1097
1098         screen->widgets.ok_b = widget_new_button(set, 0, 0, 10, _("OK"),
1099                         ok_click, screen);
1100         screen->widgets.help_b = widget_new_button(set, 0, 0, 10, _("Help"),
1101                         help_click, screen);
1102         screen->widgets.cancel_b = widget_new_button(set, 0, 0, 10, _("Cancel"),
1103                         cancel_click, screen);
1104 }
1105
1106 static void config_screen_widget_focus(struct nc_widget *widget, void *arg)
1107 {
1108         struct config_screen *screen = arg;
1109         int w_y, w_height, w_focus, s_max, adjust;
1110
1111         w_height = widget_height(widget);
1112         w_focus = widget_focus_y(widget);
1113         w_y = widget_y(widget) + w_focus;
1114         s_max = getmaxy(screen->scr.sub_ncw) - 1;
1115
1116         if (w_y < screen->scroll_y)
1117                 screen->scroll_y = w_y;
1118
1119         else if (w_y + screen->scroll_y + 1 > s_max) {
1120                 /* Fit as much of the widget into the screen as possible */
1121                 adjust = min(s_max - 1, w_height - w_focus);
1122                 if (w_y + adjust >= screen->scroll_y + s_max)
1123                         screen->scroll_y = max(0, 1 + w_y + adjust - s_max);
1124         } else
1125                 return;
1126
1127         pad_refresh(screen);
1128 }
1129
1130 static void config_screen_draw(struct config_screen *screen,
1131                 const struct config *config,
1132                 const struct system_info *sysinfo)
1133 {
1134         bool repost = false;
1135         int height;
1136
1137         /* The size of the pad we'll need depends on the number of interfaces.
1138          *
1139          * We use N_FIELDS (which is quite conservative, as some fields share
1140          * a line) as a base, then:
1141          * - add 6 (as the network select & boot device select fields take 3
1142          *   lines each),
1143          * - add n_interfaces for every field in the network select field, and
1144          * - add (n_blockdevs + n_interfaces) for every field in the boot device
1145          *   select field
1146          */
1147         height = N_FIELDS + 6;
1148         if (sysinfo) {
1149                 height += sysinfo->n_interfaces;
1150                 height += (sysinfo->n_blockdevs + sysinfo->n_interfaces);
1151         }
1152         if (!screen->pad || getmaxy(screen->pad) < height) {
1153                 if (screen->pad)
1154                         delwin(screen->pad);
1155                 screen->pad = newpad(height, COLS + 10);
1156         }
1157
1158         if (screen->widgetset) {
1159                 widgetset_unpost(screen->widgetset);
1160                 talloc_free(screen->widgetset);
1161                 repost = true;
1162         }
1163
1164         screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
1165                         screen->pad);
1166         widgetset_set_widget_focus(screen->widgetset,
1167                         config_screen_widget_focus, screen);
1168
1169         if (!config || !sysinfo) {
1170                 config_screen_setup_empty(screen);
1171         } else {
1172                 screen->net_conf_type = find_net_conf_type(config);
1173                 config_screen_setup_widgets(screen, config, sysinfo);
1174                 config_screen_layout_widgets(screen);
1175         }
1176
1177         if (repost)
1178                 widgetset_post(screen->widgetset);
1179 }
1180
1181 void config_screen_update(struct config_screen *screen,
1182                 const struct config *config,
1183                 const struct system_info *sysinfo)
1184 {
1185         if (screen->cui->current != config_screen_scr(screen)) {
1186                 screen->need_update = true;
1187                 return;
1188         }
1189
1190         config_screen_draw(screen, config, sysinfo);
1191         pad_refresh(screen);
1192 }
1193
1194 static int config_screen_post(struct nc_scr *scr)
1195 {
1196         struct config_screen *screen = config_screen_from_scr(scr);
1197         screen->show_subset = false;
1198
1199         if (screen->need_update) {
1200                 config_screen_draw(screen, screen->cui->config,
1201                                    screen->cui->sysinfo);
1202                 screen->need_update = false;
1203         } else {
1204                 widgetset_post(screen->widgetset);
1205         }
1206
1207         nc_scr_frame_draw(scr);
1208         if (screen->need_redraw) {
1209                 redrawwin(scr->main_ncw);
1210                 screen->need_redraw = false;
1211         }
1212         wrefresh(screen->scr.main_ncw);
1213         pad_refresh(screen);
1214         return 0;
1215 }
1216
1217 static int config_screen_destroy(void *arg)
1218 {
1219         struct config_screen *screen = arg;
1220         if (screen->pad)
1221                 delwin(screen->pad);
1222         return 0;
1223 }
1224
1225 struct config_screen *config_screen_init(struct cui *cui,
1226                 const struct config *config,
1227                 const struct system_info *sysinfo,
1228                 void (*on_exit)(struct cui *))
1229 {
1230         struct config_screen *screen;
1231
1232         screen = talloc_zero(cui, struct config_screen);
1233         talloc_set_destructor(screen, config_screen_destroy);
1234         nc_scr_init(&screen->scr, pb_config_screen_sig, 0,
1235                         cui, config_screen_process_key,
1236                         config_screen_post, config_screen_unpost,
1237                         config_screen_resize);
1238
1239         screen->cui = cui;
1240         screen->on_exit = on_exit;
1241         screen->need_redraw = false;
1242         screen->need_update = false;
1243         screen->label_x = 2;
1244         screen->field_x = 17;
1245
1246         screen->ipmi_override = false;
1247         screen->show_subset = false;
1248
1249         screen->scr.frame.ltitle = talloc_strdup(screen,
1250                         _("Petitboot System Configuration"));
1251         screen->scr.frame.rtitle = NULL;
1252         screen->scr.frame.help = talloc_strdup(screen,
1253                         _("tab=next, shift+tab=previous, x=exit, h=help"));
1254         nc_scr_frame_draw(&screen->scr);
1255
1256         scrollok(screen->scr.sub_ncw, true);
1257
1258         config_screen_draw(screen, config, sysinfo);
1259
1260         return screen;
1261 }