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