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