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