2 * Copyright (C) 2009 Sony Computer Entertainment Inc.
3 * Copyright 2009 Sony Corp.
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.
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.
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
19 #if defined(HAVE_CONFIG_H)
27 #include "talloc/talloc.h"
28 #include "i18n/i18n.h"
29 #include "nc-boot-editor.h"
30 #include "nc-widgets.h"
36 struct pmenu_item *item;
43 void (*on_exit)(struct cui *cui,
44 struct pmenu_item *item,
45 struct pb_boot_data *bd);
54 struct nc_widgetset *widgetset;
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_label *args_sig_file_l;
67 struct nc_widget_textbox *args_sig_file_f;
68 struct nc_widget_button *ok_b;
69 struct nc_widget_button *help_b;
70 struct nc_widget_button *cancel_b;
73 const char *selected_device;
80 bool use_signature_files;
83 extern const struct help_text boot_editor_help_text;
85 static struct boot_editor *boot_editor_from_scr(struct nc_scr *scr)
87 struct boot_editor *boot_editor;
89 assert(scr->sig == pb_boot_editor_sig);
90 boot_editor = (struct boot_editor *)
91 ((char *)scr - (size_t)&((struct boot_editor *)0)->scr);
92 assert(boot_editor->scr.sig == pb_boot_editor_sig);
96 static void pad_refresh(struct boot_editor *boot_editor)
100 getmaxyx(boot_editor->scr.sub_ncw, rows, cols);
101 getbegyx(boot_editor->scr.sub_ncw, y, x);
103 prefresh(boot_editor->pad, boot_editor->scroll_y, 0,
107 static struct boot_editor *boot_editor_from_arg(void *arg)
109 struct boot_editor *boot_editor = arg;
111 assert(boot_editor->scr.sig == pb_boot_editor_sig);
115 static int boot_editor_post(struct nc_scr *scr)
117 struct boot_editor *boot_editor = boot_editor_from_scr(scr);
119 if (boot_editor->need_update) {
120 boot_editor_update(boot_editor, boot_editor->cui->sysinfo);
121 boot_editor->need_update = false;
123 widgetset_post(boot_editor->widgetset);
126 nc_scr_frame_draw(scr);
127 if (boot_editor->need_redraw) {
128 redrawwin(scr->main_ncw);
129 boot_editor->need_redraw = false;
131 wrefresh(boot_editor->scr.main_ncw);
132 pad_refresh(boot_editor);
136 static int boot_editor_unpost(struct nc_scr *scr)
138 widgetset_unpost(boot_editor_from_scr(scr)->widgetset);
142 struct nc_scr *boot_editor_scr(struct boot_editor *boot_editor)
144 return &boot_editor->scr;
147 static void boot_editor_resize(struct nc_scr *scr)
149 /* FIXME: forms can't be resized, need to recreate here */
150 boot_editor_unpost(scr);
151 boot_editor_post(scr);
154 static char *conditional_prefix(struct pb_boot_data *ctx,
155 const char *prefix, const char *value)
159 if (!value || !*value)
165 else if (prefix[strlen(prefix)] != '/')
168 return talloc_asprintf(ctx, "%s%s%s", prefix, sep, value);
171 static struct pb_boot_data *boot_editor_prepare_data(
172 struct boot_editor *boot_editor)
174 struct pb_boot_data *bd;
178 bd = talloc(boot_editor, struct pb_boot_data);
183 idx = widget_select_get_value(boot_editor->widgets.device_f);
184 if (idx == -1 || (unsigned int)idx >
185 boot_editor->cui->sysinfo->n_blockdevs)
188 prefix = boot_editor->cui->sysinfo->blockdevs[idx]->mountpoint;
190 s = widget_textbox_get_value(boot_editor->widgets.image_f);
191 bd->image = conditional_prefix(bd, prefix, s);
197 s = widget_textbox_get_value(boot_editor->widgets.initrd_f);
198 bd->initrd = conditional_prefix(bd, prefix, s);
200 s = widget_textbox_get_value(boot_editor->widgets.dtb_f);
201 bd->dtb = conditional_prefix(bd, prefix, s);
203 s = widget_textbox_get_value(boot_editor->widgets.args_f);
204 bd->args = *s ? talloc_strdup(bd, s) : NULL;
206 if (boot_editor->use_signature_files) {
207 s = widget_textbox_get_value(
208 boot_editor->widgets.args_sig_file_f);
209 bd->args_sig_file = conditional_prefix(bd, prefix, s);
212 bd->args_sig_file = NULL;
219 * boot_editor_process_key - Process a user keystroke.
221 * Called from the cui via the scr:process_key method.
224 static void boot_editor_process_key(struct nc_scr *scr, int key)
226 struct boot_editor *boot_editor = boot_editor_from_scr(scr);
227 struct pmenu_item *item;
228 struct pb_boot_data *bd;
231 handled = widgetset_process_key(boot_editor->widgetset, key);
233 pad_refresh(boot_editor);
235 else if (key == 'x' || key == 27)
236 boot_editor->state = STATE_CANCEL;
239 boot_editor->state = STATE_HELP;
244 switch (boot_editor->state) {
246 item = boot_editor->item;
247 bd = boot_editor_prepare_data(boot_editor);
249 /* Incomplete entry */
250 boot_editor->state = STATE_EDIT;
255 boot_editor->on_exit(boot_editor->cui, item, bd);
258 boot_editor->state = STATE_EDIT;
259 boot_editor->need_redraw = true;
260 cui_show_help(boot_editor->cui, _("Boot Option Editor"),
261 &boot_editor_help_text);
269 * boot_editor_destructor - The talloc destructor for a boot_editor.
272 static int boot_editor_destructor(void *arg)
274 struct boot_editor *boot_editor = boot_editor_from_arg(arg);
275 boot_editor->scr.sig = pb_removed_sig;
276 if (boot_editor->pad)
277 delwin(boot_editor->pad);
281 static void ok_click(void *arg)
283 struct boot_editor *boot_editor = arg;
284 boot_editor->state = STATE_SAVE;
287 static void help_click(void *arg)
289 struct boot_editor *boot_editor = arg;
290 boot_editor->state = STATE_HELP;
293 static void cancel_click(void *arg)
295 struct boot_editor *boot_editor = arg;
296 boot_editor->state = STATE_CANCEL;
299 static int layout_pair(struct boot_editor *boot_editor, int y,
300 struct nc_widget_label *label,
301 struct nc_widget_textbox *field)
303 struct nc_widget *label_w = widget_label_base(label);
304 struct nc_widget *field_w = widget_textbox_base(field);
305 widget_move(label_w, y, boot_editor->label_x);
306 widget_move(field_w, y, boot_editor->field_x);
307 return max(widget_height(label_w), widget_height(field_w));
310 static int pad_height(int blockdevs_height)
312 return 10 + blockdevs_height;
315 static void boot_editor_layout_widgets(struct boot_editor *boot_editor)
317 struct nc_widget *wf, *wl;
320 wl = widget_label_base(boot_editor->widgets.device_l);
321 wf = widget_select_base(boot_editor->widgets.device_f);
322 widget_move(wl, y, boot_editor->label_x);
323 widget_move(wf, y, boot_editor->field_x);
325 y += widget_height(wf) + 1;
328 y += layout_pair(boot_editor, y, boot_editor->widgets.image_l,
329 boot_editor->widgets.image_f);
331 y += layout_pair(boot_editor, y, boot_editor->widgets.initrd_l,
332 boot_editor->widgets.initrd_f);
334 y += layout_pair(boot_editor, y, boot_editor->widgets.dtb_l,
335 boot_editor->widgets.dtb_f);
337 y += layout_pair(boot_editor, y, boot_editor->widgets.args_l,
338 boot_editor->widgets.args_f);
340 if (boot_editor->use_signature_files) {
341 y += layout_pair(boot_editor, y,
342 boot_editor->widgets.args_sig_file_l,
343 boot_editor->widgets.args_sig_file_f);
348 widget_move(widget_button_base(boot_editor->widgets.ok_b), y,
349 boot_editor->field_x);
350 widget_move(widget_button_base(boot_editor->widgets.help_b), y,
351 boot_editor->field_x + 14);
352 widget_move(widget_button_base(boot_editor->widgets.cancel_b), y,
353 boot_editor->field_x + 28);
356 static void boot_editor_widget_focus(struct nc_widget *widget, void *arg)
358 struct boot_editor *boot_editor = arg;
361 w_y = widget_y(widget) + widget_focus_y(widget);
362 s_max = getmaxy(boot_editor->scr.sub_ncw) - 1;
364 if (w_y < boot_editor->scroll_y)
365 boot_editor->scroll_y = w_y;
367 else if (w_y + boot_editor->scroll_y + 1 > s_max)
368 boot_editor->scroll_y = 1 + w_y - s_max;
373 pad_refresh(boot_editor);
376 static void boot_editor_device_select_change(void *arg, int idx)
378 struct boot_editor *boot_editor = arg;
380 boot_editor->selected_device = NULL;
382 boot_editor->selected_device =
383 boot_editor->cui->sysinfo->blockdevs[idx]->name;
386 static void boot_editor_populate_device_select(struct boot_editor *boot_editor,
387 const struct system_info *sysinfo)
389 struct nc_widget_select *select = boot_editor->widgets.device_f;
393 widget_select_drop_options(select);
395 for (i = 0; sysinfo && i < sysinfo->n_blockdevs; i++) {
396 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
399 name = talloc_asprintf(boot_editor, "%s [%s]",
400 bd_info->name, bd_info->uuid);
401 selected = boot_editor->selected_device &&
402 !strcmp(bd_info->name,
403 boot_editor->selected_device);
405 widget_select_add_option(select, i, name, selected);
408 /* If we're editing an existing option, the paths will be fully-
409 * resolved. In this case, we want the manual device pre-selected.
410 * However, we only do this if the widget hasn't been manually
412 selected = !boot_editor->selected_device;
414 widget_select_add_option(select, -1, _("Specify paths/URLs manually"),
418 static bool path_on_device(struct blockdev_info *bd_info,
423 if (!bd_info->mountpoint)
426 len = strlen(bd_info->mountpoint);
427 if (strncmp(bd_info->mountpoint, path, len))
430 /* if the mountpoint doesn't have a trailing slash, ensure that
431 * the path starts with one (so we don't match a "/mnt/sda1/foo" path
432 * on a "/mnt/sda" mountpoint) */
433 return bd_info->mountpoint[len-1] == '/' || path[len] == '/';
437 static void boot_editor_find_device(struct boot_editor *boot_editor,
438 struct pb_boot_data *bd, const struct system_info *sysinfo)
440 struct blockdev_info *bd_info, *tmp;
443 if (!sysinfo || !sysinfo->n_blockdevs)
446 /* find the device for our boot image, by finding the longest
447 * matching blockdev's mountpoint */
448 for (len = 0, i = 0, bd_info = NULL; i < sysinfo->n_blockdevs; i++) {
449 tmp = sysinfo->blockdevs[i];
450 if (!path_on_device(tmp, bd->image))
452 if (strlen(tmp->mountpoint) <= len)
455 len = strlen(tmp->mountpoint);
461 /* ensure that other paths are on this device */
462 if (bd->initrd && !path_on_device(bd_info, bd->initrd))
465 if (bd->dtb && !path_on_device(bd_info, bd->dtb))
468 if (boot_editor->use_signature_files)
469 if (bd->args_sig_file && !path_on_device(bd_info,
473 /* ok, we match; preselect the device option, and remove the common
475 boot_editor->selected_device = bd_info->name;
476 boot_editor->image += len;
478 if (boot_editor->initrd)
479 boot_editor->initrd += len;
480 if (boot_editor->dtb)
481 boot_editor->dtb += len;
482 if (boot_editor->use_signature_files)
483 if (boot_editor->args_sig_file)
484 boot_editor->args_sig_file += len;
487 static void boot_editor_setup_widgets(struct boot_editor *boot_editor,
488 const struct system_info *sysinfo)
490 struct nc_widgetset *set;
493 field_size = COLS - 1 - boot_editor->field_x;
495 boot_editor->widgetset = set = widgetset_create(boot_editor,
496 boot_editor->scr.main_ncw,
499 widgetset_set_widget_focus(boot_editor->widgetset,
500 boot_editor_widget_focus, boot_editor);
502 boot_editor->widgets.device_l = widget_new_label(set, 0, 0,
504 boot_editor->widgets.device_f = widget_new_select(set, 0, 0,
506 widget_select_on_change(boot_editor->widgets.device_f,
507 boot_editor_device_select_change, boot_editor);
509 boot_editor_populate_device_select(boot_editor, sysinfo);
511 boot_editor->widgets.image_l = widget_new_label(set, 0, 0,
513 boot_editor->widgets.image_f = widget_new_textbox(set, 0, 0,
514 field_size, boot_editor->image);
516 boot_editor->widgets.initrd_l = widget_new_label(set, 0, 0,
518 boot_editor->widgets.initrd_f = widget_new_textbox(set, 0, 0,
520 boot_editor->initrd);
522 boot_editor->widgets.dtb_l = widget_new_label(set, 0, 0,
524 boot_editor->widgets.dtb_f = widget_new_textbox(set, 0, 0,
525 field_size, boot_editor->dtb);
527 boot_editor->widgets.args_l = widget_new_label(set, 0, 0,
528 _("Boot arguments:"));
529 boot_editor->widgets.args_f = widget_new_textbox(set, 0, 0,
530 field_size, boot_editor->args);
532 if (boot_editor->use_signature_files) {
533 boot_editor->widgets.args_sig_file_l = widget_new_label(set,
534 0, 0, _("Argument signature file:"));
535 boot_editor->widgets.args_sig_file_f = widget_new_textbox(set,
536 0, 0, field_size, boot_editor->args_sig_file);
539 boot_editor->widgets.args_sig_file_l = NULL;
540 boot_editor->widgets.args_sig_file_f = NULL;
543 boot_editor->widgets.ok_b = widget_new_button(set, 0, 0, 10,
544 _("OK"), ok_click, boot_editor);
545 boot_editor->widgets.help_b = widget_new_button(set, 0, 0, 10,
546 _("Help"), help_click, boot_editor);
547 boot_editor->widgets.cancel_b = widget_new_button(set, 0, 0, 10,
548 _("Cancel"), cancel_click, boot_editor);
551 void boot_editor_update(struct boot_editor *boot_editor,
552 const struct system_info *sysinfo)
556 if (boot_editor->cui->current != boot_editor_scr(boot_editor)) {
557 boot_editor->need_update = true;
561 widgetset_unpost(boot_editor->widgetset);
563 height = pad_height(sysinfo ? sysinfo->n_blockdevs : 0);
564 if (getmaxy(boot_editor->pad) < height) {
565 delwin(boot_editor->pad);
566 boot_editor->pad = newpad(height, COLS);
567 widgetset_set_windows(boot_editor->widgetset,
568 boot_editor->scr.main_ncw,
572 boot_editor_populate_device_select(boot_editor, sysinfo);
574 boot_editor_layout_widgets(boot_editor);
576 widgetset_post(boot_editor->widgetset);
578 pad_refresh(boot_editor);
581 struct boot_editor *boot_editor_init(struct cui *cui,
582 struct pmenu_item *item,
583 const struct system_info *sysinfo,
584 void (*on_exit)(struct cui *cui,
585 struct pmenu_item *item,
586 struct pb_boot_data *bd))
588 struct boot_editor *boot_editor;
589 int ncols1, ncols2, ncols3;
591 boot_editor = talloc_zero(cui, struct boot_editor);
596 #if defined(HAVE_LIBGPGME)
597 if (access(LOCKDOWN_FILE, F_OK) == -1)
598 boot_editor->use_signature_files = false;
600 boot_editor->use_signature_files = true;
602 boot_editor->use_signature_files = false;
605 talloc_set_destructor(boot_editor, boot_editor_destructor);
606 boot_editor->cui = cui;
607 boot_editor->item = item;
608 boot_editor->on_exit = on_exit;
609 boot_editor->state = STATE_EDIT;
610 boot_editor->need_redraw = false;
611 boot_editor->need_update = false;
613 ncols1 = strncols(_("Device tree:"));
614 ncols2 = strncols(_("Boot arguments:"));
615 if (boot_editor->use_signature_files)
616 ncols3 = strncols(_("Argument signature file:"));
620 boot_editor->label_x = 1;
621 boot_editor->field_x = 2 + max(max(ncols1, ncols2), ncols3);
623 nc_scr_init(&boot_editor->scr, pb_boot_editor_sig, 0,
624 cui, boot_editor_process_key,
625 boot_editor_post, boot_editor_unpost, boot_editor_resize);
627 boot_editor->scr.frame.ltitle = talloc_strdup(boot_editor,
628 _("Petitboot Option Editor"));
629 boot_editor->scr.frame.rtitle = NULL;
630 boot_editor->scr.frame.help = talloc_strdup(boot_editor,
631 _("tab=next, shift+tab=previous, x=exit, h=help"));
632 nc_scr_frame_draw(&boot_editor->scr);
635 struct pb_boot_data *bd = cod_from_item(item)->bd;
636 boot_editor->image = bd->image;
637 boot_editor->initrd = bd->initrd;
638 boot_editor->dtb = bd->dtb;
639 boot_editor->args = bd->args;
640 if (boot_editor->use_signature_files)
641 boot_editor->args_sig_file = bd->args_sig_file;
643 boot_editor->args_sig_file = talloc_strdup(bd, "");
644 boot_editor_find_device(boot_editor, bd, sysinfo);
646 boot_editor->image = boot_editor->initrd =
647 boot_editor->dtb = boot_editor->args =
648 boot_editor->args_sig_file = "";
651 boot_editor->pad = newpad(
652 pad_height(sysinfo ? sysinfo->n_blockdevs : 0),
655 boot_editor_setup_widgets(boot_editor, sysinfo);
657 boot_editor_layout_widgets(boot_editor);
658 wrefresh(boot_editor->scr.main_ncw);