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