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