]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/nc-boot-editor.c
7fa1a42f0eddfbc07794b9f003c4dbfd330dfac6
[petitboot] / ui / ncurses / nc-boot-editor.c
1 /*
2  *  Copyright (C) 2009 Sony Computer Entertainment Inc.
3  *  Copyright 2009 Sony Corp.
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; version 2 of the License.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #if defined(HAVE_CONFIG_H)
20 #include "config.h"
21 #endif
22
23 #include <assert.h>
24 #include <string.h>
25
26 #include "log/log.h"
27 #include "talloc/talloc.h"
28 #include "i18n/i18n.h"
29 #include "nc-boot-editor.h"
30 #include "nc-widgets.h"
31
32 struct boot_editor {
33         struct nc_scr           scr;
34         struct cui              *cui;
35         void                    *data;
36         struct pmenu_item       *item;
37         enum {
38                 STATE_EDIT,
39                 STATE_CANCEL,
40                 STATE_SAVE,
41                 STATE_HELP,
42         }                       state;
43         void                    (*on_exit)(struct cui *cui,
44                                         struct pmenu_item *item,
45                                         struct pb_boot_data *bd);
46         bool                    need_redraw;
47         bool                    need_update;
48
49         int                     label_x;
50         int                     field_x;
51         int                     scroll_y;
52
53         WINDOW                  *pad;
54         struct nc_widgetset     *widgetset;
55         struct {
56                 struct nc_widget_label          *device_l;
57                 struct nc_widget_select         *device_f;
58                 struct nc_widget_label          *image_l;
59                 struct nc_widget_textbox        *image_f;
60                 struct nc_widget_label          *initrd_l;
61                 struct nc_widget_textbox        *initrd_f;
62                 struct nc_widget_label          *dtb_l;
63                 struct nc_widget_textbox        *dtb_f;
64                 struct nc_widget_label          *args_l;
65                 struct nc_widget_textbox        *args_f;
66                 struct nc_widget_label          *args_sig_file_l;
67                 struct nc_widget_textbox        *args_sig_file_f;
68                 struct nc_widget_button         *ok_b;
69                 struct nc_widget_button         *help_b;
70                 struct nc_widget_button         *cancel_b;
71         } widgets;
72
73         const char              *selected_device;
74         char                    *image;
75         char                    *initrd;
76         char                    *dtb;
77         char                    *args;
78         char                    *args_sig_file;
79
80         bool                    use_signature_files;
81 };
82
83 extern const struct help_text boot_editor_help_text;
84
85 static struct boot_editor *boot_editor_from_scr(struct nc_scr *scr)
86 {
87         struct boot_editor *boot_editor;
88
89         assert(scr->sig == pb_boot_editor_sig);
90         boot_editor = (struct boot_editor *)
91                 ((char *)scr - (size_t)&((struct boot_editor *)0)->scr);
92         assert(boot_editor->scr.sig == pb_boot_editor_sig);
93         return boot_editor;
94 }
95
96 static void pad_refresh(struct boot_editor *boot_editor)
97 {
98         int y, x, rows, cols;
99
100         getmaxyx(boot_editor->scr.sub_ncw, rows, cols);
101         getbegyx(boot_editor->scr.sub_ncw, y, x);
102
103         prefresh(boot_editor->pad, boot_editor->scroll_y, 0,
104                         y, x, rows, cols);
105 }
106
107 static struct boot_editor *boot_editor_from_arg(void *arg)
108 {
109         struct boot_editor *boot_editor = arg;
110
111         assert(boot_editor->scr.sig == pb_boot_editor_sig);
112         return boot_editor;
113 }
114
115 static int boot_editor_post(struct nc_scr *scr)
116 {
117         struct boot_editor *boot_editor = boot_editor_from_scr(scr);
118
119         if (boot_editor->need_update) {
120                 boot_editor_update(boot_editor, boot_editor->cui->sysinfo);
121                 boot_editor->need_update = false;
122         } else {
123                 widgetset_post(boot_editor->widgetset);
124         }
125
126         nc_scr_frame_draw(scr);
127         if (boot_editor->need_redraw) {
128                 redrawwin(scr->main_ncw);
129                 boot_editor->need_redraw = false;
130         }
131         wrefresh(boot_editor->scr.main_ncw);
132         pad_refresh(boot_editor);
133         return 0;
134 }
135
136 static int boot_editor_unpost(struct nc_scr *scr)
137 {
138         widgetset_unpost(boot_editor_from_scr(scr)->widgetset);
139         return 0;
140 }
141
142 struct nc_scr *boot_editor_scr(struct boot_editor *boot_editor)
143 {
144         return &boot_editor->scr;
145 }
146
147 static void boot_editor_resize(struct nc_scr *scr)
148 {
149         /* FIXME: forms can't be resized, need to recreate here */
150         boot_editor_unpost(scr);
151         boot_editor_post(scr);
152 }
153
154 static char *conditional_prefix(struct pb_boot_data *ctx,
155                 const char *prefix, const char *value)
156 {
157         const char *sep;
158
159         if (!value || !*value)
160                 return NULL;
161
162         sep = "";
163         if (!prefix)
164                 prefix = "";
165         else if (prefix[strlen(prefix)] != '/')
166                 sep = "/";
167
168         return talloc_asprintf(ctx, "%s%s%s", prefix, sep, value);
169 }
170
171 static struct pb_boot_data *boot_editor_prepare_data(
172                 struct boot_editor *boot_editor)
173 {
174         struct pb_boot_data *bd;
175         char *s, *prefix;
176         int idx;
177
178         bd = talloc(boot_editor, struct pb_boot_data);
179
180         if (!bd)
181                 return NULL;
182
183         idx = widget_select_get_value(boot_editor->widgets.device_f);
184         if (idx == -1 || (unsigned int)idx >
185                         boot_editor->cui->sysinfo->n_blockdevs)
186                 prefix = NULL;
187         else
188                 prefix = boot_editor->cui->sysinfo->blockdevs[idx]->mountpoint;
189
190         s = widget_textbox_get_value(boot_editor->widgets.image_f);
191         bd->image = conditional_prefix(bd, prefix, s);
192         if (!bd->image) {
193                 talloc_free(bd);
194                 return NULL;
195         }
196
197         s = widget_textbox_get_value(boot_editor->widgets.initrd_f);
198         bd->initrd = conditional_prefix(bd, prefix, s);
199
200         s = widget_textbox_get_value(boot_editor->widgets.dtb_f);
201         bd->dtb = conditional_prefix(bd, prefix, s);
202
203         s = widget_textbox_get_value(boot_editor->widgets.args_f);
204         bd->args = *s ? talloc_strdup(bd, s) : NULL;
205
206         if (boot_editor->use_signature_files) {
207                 s = widget_textbox_get_value(
208                         boot_editor->widgets.args_sig_file_f);
209                 bd->args_sig_file = conditional_prefix(bd, prefix, s);
210         }
211         else {
212                 bd->args_sig_file = NULL;
213         }
214
215         return bd;
216 }
217
218 /**
219  * boot_editor_process_key - Process a user keystroke.
220  *
221  * Called from the cui via the scr:process_key method.
222  */
223
224 static void boot_editor_process_key(struct nc_scr *scr, int key)
225 {
226         struct boot_editor *boot_editor = boot_editor_from_scr(scr);
227         struct pmenu_item *item;
228         struct pb_boot_data *bd;
229         bool handled;
230
231         handled = widgetset_process_key(boot_editor->widgetset, key);
232         if (handled)
233                 pad_refresh(boot_editor);
234
235         else if (key == 'x' || key == 27)
236                 boot_editor->state = STATE_CANCEL;
237
238         else if (key == 'h')
239                 boot_editor->state = STATE_HELP;
240
241         item = NULL;
242         bd = NULL;
243
244         switch (boot_editor->state) {
245         case STATE_SAVE:
246                 item = boot_editor->item;
247                 bd = boot_editor_prepare_data(boot_editor);
248                 if (!bd) {
249                         /* Incomplete entry */
250                         boot_editor->state = STATE_EDIT;
251                         break;
252                 }
253                 /* fall through */
254         case STATE_CANCEL:
255                 boot_editor->on_exit(boot_editor->cui, item, bd);
256                 break;
257         case STATE_HELP:
258                 boot_editor->state = STATE_EDIT;
259                 boot_editor->need_redraw = true;
260                 cui_show_help(boot_editor->cui, _("Boot Option Editor"),
261                                 &boot_editor_help_text);
262                 break;
263         default:
264                 break;
265         }
266 }
267
268 /**
269  * boot_editor_destructor - The talloc destructor for a boot_editor.
270  */
271
272 static int boot_editor_destructor(void *arg)
273 {
274         struct boot_editor *boot_editor = boot_editor_from_arg(arg);
275         boot_editor->scr.sig = pb_removed_sig;
276         if (boot_editor->pad)
277                 delwin(boot_editor->pad);
278         return 0;
279 }
280
281 static void ok_click(void *arg)
282 {
283         struct boot_editor *boot_editor = arg;
284         boot_editor->state = STATE_SAVE;
285 }
286
287 static void help_click(void *arg)
288 {
289         struct boot_editor *boot_editor = arg;
290         boot_editor->state = STATE_HELP;
291 }
292
293 static void cancel_click(void *arg)
294 {
295         struct boot_editor *boot_editor = arg;
296         boot_editor->state = STATE_CANCEL;
297 }
298
299 static int layout_pair(struct boot_editor *boot_editor, int y,
300                 struct nc_widget_label *label,
301                 struct nc_widget_textbox *field)
302 {
303         struct nc_widget *label_w = widget_label_base(label);
304         struct nc_widget *field_w = widget_textbox_base(field);
305         widget_move(label_w, y, boot_editor->label_x);
306         widget_move(field_w, y, boot_editor->field_x);
307         return max(widget_height(label_w), widget_height(field_w));
308 }
309
310 static int pad_height(int blockdevs_height)
311 {
312         return 10 + blockdevs_height;
313 }
314
315 static void boot_editor_layout_widgets(struct boot_editor *boot_editor)
316 {
317         struct nc_widget *wf, *wl;
318         int y = 1;
319
320         wl = widget_label_base(boot_editor->widgets.device_l);
321         wf = widget_select_base(boot_editor->widgets.device_f);
322         widget_move(wl, y, boot_editor->label_x);
323         widget_move(wf, y, boot_editor->field_x);
324
325         y += widget_height(wf) + 1;
326
327
328         y += layout_pair(boot_editor, y, boot_editor->widgets.image_l,
329                                          boot_editor->widgets.image_f);
330
331         y += layout_pair(boot_editor, y, boot_editor->widgets.initrd_l,
332                                          boot_editor->widgets.initrd_f);
333
334         y += layout_pair(boot_editor, y, boot_editor->widgets.dtb_l,
335                                          boot_editor->widgets.dtb_f);
336
337         y += layout_pair(boot_editor, y, boot_editor->widgets.args_l,
338                                          boot_editor->widgets.args_f);
339
340         if (boot_editor->use_signature_files) {
341                 y += layout_pair(boot_editor, y,
342                                         boot_editor->widgets.args_sig_file_l,
343                                         boot_editor->widgets.args_sig_file_f);
344         }
345
346
347         y++;
348         widget_move(widget_button_base(boot_editor->widgets.ok_b), y,
349                     boot_editor->field_x);
350         widget_move(widget_button_base(boot_editor->widgets.help_b), y,
351                     boot_editor->field_x + 14);
352         widget_move(widget_button_base(boot_editor->widgets.cancel_b), y,
353                     boot_editor->field_x + 28);
354 }
355
356 static void boot_editor_widget_focus(struct nc_widget *widget, void *arg)
357 {
358         struct boot_editor *boot_editor = arg;
359         int w_y, s_max;
360
361         w_y = widget_y(widget) + widget_focus_y(widget);
362         s_max = getmaxy(boot_editor->scr.sub_ncw) - 1;
363
364         if (w_y < boot_editor->scroll_y)
365                 boot_editor->scroll_y = w_y;
366
367         else if (w_y + boot_editor->scroll_y + 1 > s_max)
368                 boot_editor->scroll_y = 1 + w_y - s_max;
369
370         else
371                 return;
372
373         pad_refresh(boot_editor);
374 }
375
376 static void boot_editor_device_select_change(void *arg, int idx)
377 {
378         struct boot_editor *boot_editor = arg;
379         if (idx == -1)
380                 boot_editor->selected_device = NULL;
381         else
382                 boot_editor->selected_device =
383                         boot_editor->cui->sysinfo->blockdevs[idx]->name;
384 }
385
386 static void boot_editor_populate_device_select(struct boot_editor *boot_editor,
387                 const struct system_info *sysinfo)
388 {
389         struct nc_widget_select *select = boot_editor->widgets.device_f;
390         unsigned int i;
391         bool selected;
392
393         widget_select_drop_options(select);
394
395         for (i = 0; sysinfo && i < sysinfo->n_blockdevs; i++) {
396                 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
397                 const char *name;
398
399                 name = talloc_asprintf(boot_editor, "%s [%s]",
400                                 bd_info->name, bd_info->uuid);
401                 selected = boot_editor->selected_device &&
402                                 !strcmp(bd_info->name,
403                                                 boot_editor->selected_device);
404
405                 widget_select_add_option(select, i, name, selected);
406         }
407
408         /* If we're editing an existing option, the paths will be fully-
409          * resolved. In this case, we want the manual device pre-selected.
410          * However, we only do this if the widget hasn't been manually
411          * changed. */
412         selected = !boot_editor->selected_device;
413
414         widget_select_add_option(select, -1, _("Specify paths/URLs manually"),
415                         selected);
416 }
417
418 static bool path_on_device(struct blockdev_info *bd_info,
419                 const char *path)
420 {
421         int len;
422
423         if (!bd_info->mountpoint)
424                 return false;
425
426         len = strlen(bd_info->mountpoint);
427         if (strncmp(bd_info->mountpoint, path, len))
428                 return false;
429
430         /* if the mountpoint doesn't have a trailing slash, ensure that
431          * the path starts with one (so we don't match a "/mnt/sda1/foo" path
432          * on a "/mnt/sda" mountpoint) */
433         return bd_info->mountpoint[len-1] == '/' || path[len] == '/';
434 }
435
436
437 static void boot_editor_find_device(struct boot_editor *boot_editor,
438                 struct pb_boot_data *bd, const struct system_info *sysinfo)
439 {
440         struct blockdev_info *bd_info, *tmp;
441         unsigned int i, len;
442
443         if (!sysinfo || !sysinfo->n_blockdevs)
444                 return;
445
446         /* find the device for our boot image, by finding the longest
447          * matching blockdev's mountpoint */
448         for (len = 0, i = 0, bd_info = NULL; i < sysinfo->n_blockdevs; i++) {
449                 tmp = sysinfo->blockdevs[i];
450                 if (!path_on_device(tmp, bd->image))
451                         continue;
452                 if (strlen(tmp->mountpoint) <= len)
453                         continue;
454                 bd_info = tmp;
455                 len = strlen(tmp->mountpoint);
456         }
457
458         if (!bd_info)
459                 return;
460
461         /* ensure that other paths are on this device */
462         if (bd->initrd && !path_on_device(bd_info, bd->initrd))
463                 return;
464
465         if (bd->dtb && !path_on_device(bd_info, bd->dtb))
466                 return;
467
468         if (boot_editor->use_signature_files)
469                 if (bd->args_sig_file && !path_on_device(bd_info,
470                         bd->args_sig_file))
471                         return;
472
473         /* ok, we match; preselect the device option, and remove the common
474          * prefix */
475         boot_editor->selected_device = bd_info->name;
476         boot_editor->image += len;
477
478         if (boot_editor->initrd)
479                 boot_editor->initrd += len;
480         if (boot_editor->dtb)
481                 boot_editor->dtb += len;
482         if (boot_editor->use_signature_files)
483                 if (boot_editor->args_sig_file)
484                         boot_editor->args_sig_file += len;
485 }
486
487 static void boot_editor_setup_widgets(struct boot_editor *boot_editor,
488                 const struct system_info *sysinfo)
489 {
490         struct nc_widgetset *set;
491         int field_size;
492
493         field_size = COLS - 1 - boot_editor->field_x;
494
495         boot_editor->widgetset = set = widgetset_create(boot_editor,
496                         boot_editor->scr.main_ncw,
497                         boot_editor->pad);
498
499         widgetset_set_widget_focus(boot_editor->widgetset,
500                         boot_editor_widget_focus, boot_editor);
501
502         boot_editor->widgets.device_l = widget_new_label(set, 0, 0,
503                         _("Device:"));
504         boot_editor->widgets.device_f = widget_new_select(set, 0, 0,
505                                                 field_size);
506         widget_select_on_change(boot_editor->widgets.device_f,
507                         boot_editor_device_select_change, boot_editor);
508
509         boot_editor_populate_device_select(boot_editor, sysinfo);
510
511         boot_editor->widgets.image_l = widget_new_label(set, 0, 0,
512                         _("Kernel:"));
513         boot_editor->widgets.image_f = widget_new_textbox(set, 0, 0,
514                                                 field_size, boot_editor->image);
515
516         boot_editor->widgets.initrd_l = widget_new_label(set, 0, 0,
517                         _("Initrd:"));
518         boot_editor->widgets.initrd_f = widget_new_textbox(set, 0, 0,
519                                                 field_size,
520                                                 boot_editor->initrd);
521
522         boot_editor->widgets.dtb_l = widget_new_label(set, 0, 0,
523                         _("Device tree:"));
524         boot_editor->widgets.dtb_f = widget_new_textbox(set, 0, 0,
525                                                 field_size, boot_editor->dtb);
526
527         boot_editor->widgets.args_l = widget_new_label(set, 0, 0,
528                         _("Boot arguments:"));
529         boot_editor->widgets.args_f = widget_new_textbox(set, 0, 0,
530                                         field_size, boot_editor->args);
531
532         if (boot_editor->use_signature_files) {
533                 boot_editor->widgets.args_sig_file_l = widget_new_label(set,
534                                 0, 0, _("Argument signature file:"));
535                 boot_editor->widgets.args_sig_file_f = widget_new_textbox(set,
536                                 0, 0, field_size, boot_editor->args_sig_file);
537         }
538         else {
539                 boot_editor->widgets.args_sig_file_l = NULL;
540                 boot_editor->widgets.args_sig_file_f = NULL;
541         }
542
543         boot_editor->widgets.ok_b = widget_new_button(set, 0, 0, 10,
544                                         _("OK"), ok_click, boot_editor);
545         boot_editor->widgets.help_b = widget_new_button(set, 0, 0, 10,
546                                         _("Help"), help_click, boot_editor);
547         boot_editor->widgets.cancel_b = widget_new_button(set, 0, 0, 10,
548                                         _("Cancel"), cancel_click, boot_editor);
549 }
550
551 void boot_editor_update(struct boot_editor *boot_editor,
552                 const struct system_info *sysinfo)
553 {
554         int height;
555
556         if (boot_editor->cui->current != boot_editor_scr(boot_editor)) {
557                 boot_editor->need_update = true;
558                 return;
559         }
560
561         widgetset_unpost(boot_editor->widgetset);
562
563         height = pad_height(sysinfo ? sysinfo->n_blockdevs : 0);
564         if (getmaxy(boot_editor->pad) < height) {
565                 delwin(boot_editor->pad);
566                 boot_editor->pad = newpad(height, COLS);
567                 widgetset_set_windows(boot_editor->widgetset,
568                                 boot_editor->scr.main_ncw,
569                                 boot_editor->pad);
570         }
571
572         boot_editor_populate_device_select(boot_editor, sysinfo);
573
574         boot_editor_layout_widgets(boot_editor);
575
576         widgetset_post(boot_editor->widgetset);
577
578         pad_refresh(boot_editor);
579 }
580
581 struct boot_editor *boot_editor_init(struct cui *cui,
582                 struct pmenu_item *item,
583                 const struct system_info *sysinfo,
584                 void (*on_exit)(struct cui *cui,
585                                 struct pmenu_item *item,
586                                 struct pb_boot_data *bd))
587 {
588         struct boot_editor *boot_editor;
589         int ncols1, ncols2, ncols3;
590
591         boot_editor = talloc_zero(cui, struct boot_editor);
592
593         if (!boot_editor)
594                 return NULL;
595
596 #if defined(HAVE_LIBGPGME)
597         if (access(LOCKDOWN_FILE, F_OK) == -1)
598                 boot_editor->use_signature_files = false;
599         else
600                 boot_editor->use_signature_files = true;
601 #else
602         boot_editor->use_signature_files = false;
603 #endif
604
605         talloc_set_destructor(boot_editor, boot_editor_destructor);
606         boot_editor->cui = cui;
607         boot_editor->item = item;
608         boot_editor->on_exit = on_exit;
609         boot_editor->state = STATE_EDIT;
610         boot_editor->need_redraw = false;
611         boot_editor->need_update = false;
612
613         ncols1 = strncols(_("Device tree:"));
614         ncols2 = strncols(_("Boot arguments:"));
615         if (boot_editor->use_signature_files)
616                 ncols3 = strncols(_("Argument signature file:"));
617         else
618                 ncols3 = 0;
619
620         boot_editor->label_x = 1;
621         boot_editor->field_x = 2 + max(max(ncols1, ncols2), ncols3);
622
623         nc_scr_init(&boot_editor->scr, pb_boot_editor_sig, 0,
624                         cui, boot_editor_process_key,
625                 boot_editor_post, boot_editor_unpost, boot_editor_resize);
626
627         boot_editor->scr.frame.ltitle = talloc_strdup(boot_editor,
628                         _("Petitboot Option Editor"));
629         boot_editor->scr.frame.rtitle = NULL;
630         boot_editor->scr.frame.help = talloc_strdup(boot_editor,
631                         _("tab=next, shift+tab=previous, x=exit, h=help"));
632         nc_scr_frame_draw(&boot_editor->scr);
633
634         if (item) {
635                 struct pb_boot_data *bd = cod_from_item(item)->bd;
636                 boot_editor->image = bd->image;
637                 boot_editor->initrd = bd->initrd;
638                 boot_editor->dtb = bd->dtb;
639                 boot_editor->args = bd->args;
640                 if (boot_editor->use_signature_files)
641                         boot_editor->args_sig_file = bd->args_sig_file;
642                 else
643                         boot_editor->args_sig_file = talloc_strdup(bd, "");
644                 boot_editor_find_device(boot_editor, bd, sysinfo);
645         } else {
646                 boot_editor->image = boot_editor->initrd =
647                         boot_editor->dtb = boot_editor->args =
648                         boot_editor->args_sig_file = "";
649         }
650
651         boot_editor->pad = newpad(
652                                 pad_height(sysinfo ? sysinfo->n_blockdevs : 0),
653                                 COLS);
654
655         boot_editor_setup_widgets(boot_editor, sysinfo);
656
657         boot_editor_layout_widgets(boot_editor);
658         wrefresh(boot_editor->scr.main_ncw);
659
660         return boot_editor;
661 }