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