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