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