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) - 1] != '/') &&
169 return talloc_asprintf(ctx, "%s%s%s", prefix, sep, value);
172 static struct pb_boot_data *boot_editor_prepare_data(
173 struct boot_editor *boot_editor)
175 struct pb_boot_data *bd;
179 bd = talloc(boot_editor, struct pb_boot_data);
184 idx = widget_select_get_value(boot_editor->widgets.device_f);
185 if (idx == -1 || (unsigned int)idx >
186 boot_editor->cui->sysinfo->n_blockdevs)
189 prefix = boot_editor->cui->sysinfo->blockdevs[idx]->mountpoint;
191 s = widget_textbox_get_value(boot_editor->widgets.image_f);
192 bd->image = conditional_prefix(bd, prefix, s);
198 s = widget_textbox_get_value(boot_editor->widgets.initrd_f);
199 bd->initrd = conditional_prefix(bd, prefix, s);
201 s = widget_textbox_get_value(boot_editor->widgets.dtb_f);
202 bd->dtb = conditional_prefix(bd, prefix, s);
204 s = widget_textbox_get_value(boot_editor->widgets.args_f);
205 bd->args = *s ? talloc_strdup(bd, s) : NULL;
207 if (boot_editor->use_signature_files) {
208 s = widget_textbox_get_value(
209 boot_editor->widgets.args_sig_file_f);
210 bd->args_sig_file = conditional_prefix(bd, prefix, s);
213 bd->args_sig_file = NULL;
220 * boot_editor_process_key - Process a user keystroke.
222 * Called from the cui via the scr:process_key method.
225 static void boot_editor_process_key(struct nc_scr *scr, int key)
227 struct boot_editor *boot_editor = boot_editor_from_scr(scr);
228 struct pmenu_item *item;
229 struct pb_boot_data *bd;
232 handled = widgetset_process_key(boot_editor->widgetset, key);
234 pad_refresh(boot_editor);
236 else if (key == 'x' || key == 27)
237 boot_editor->state = STATE_CANCEL;
240 boot_editor->state = STATE_HELP;
245 switch (boot_editor->state) {
247 item = boot_editor->item;
248 bd = boot_editor_prepare_data(boot_editor);
250 /* Incomplete entry */
251 boot_editor->state = STATE_EDIT;
256 boot_editor->on_exit(boot_editor->cui, item, bd);
259 boot_editor->state = STATE_EDIT;
260 boot_editor->need_redraw = true;
261 cui_show_help(boot_editor->cui, _("Boot Option Editor"),
262 &boot_editor_help_text);
270 * boot_editor_destructor - The talloc destructor for a boot_editor.
273 static int boot_editor_destructor(void *arg)
275 struct boot_editor *boot_editor = boot_editor_from_arg(arg);
276 boot_editor->scr.sig = pb_removed_sig;
277 if (boot_editor->pad)
278 delwin(boot_editor->pad);
282 static void ok_click(void *arg)
284 struct boot_editor *boot_editor = arg;
285 boot_editor->state = STATE_SAVE;
288 static void help_click(void *arg)
290 struct boot_editor *boot_editor = arg;
291 boot_editor->state = STATE_HELP;
294 static void cancel_click(void *arg)
296 struct boot_editor *boot_editor = arg;
297 boot_editor->state = STATE_CANCEL;
300 static int layout_pair(struct boot_editor *boot_editor, int y,
301 struct nc_widget_label *label,
302 struct nc_widget_textbox *field)
304 struct nc_widget *label_w = widget_label_base(label);
305 struct nc_widget *field_w = widget_textbox_base(field);
306 widget_move(label_w, y, boot_editor->label_x);
307 widget_move(field_w, y, boot_editor->field_x);
308 return max(widget_height(label_w), widget_height(field_w));
311 static int pad_height(int blockdevs_height)
313 return 10 + (2 * blockdevs_height);
316 static void boot_editor_layout_widgets(struct boot_editor *boot_editor)
318 struct nc_widget *wf, *wl;
321 wl = widget_label_base(boot_editor->widgets.device_l);
322 wf = widget_select_base(boot_editor->widgets.device_f);
323 widget_move(wl, y, boot_editor->label_x);
324 widget_move(wf, y, boot_editor->field_x);
326 y += widget_height(wf) + 1;
329 y += layout_pair(boot_editor, y, boot_editor->widgets.image_l,
330 boot_editor->widgets.image_f);
332 y += layout_pair(boot_editor, y, boot_editor->widgets.initrd_l,
333 boot_editor->widgets.initrd_f);
335 y += layout_pair(boot_editor, y, boot_editor->widgets.dtb_l,
336 boot_editor->widgets.dtb_f);
338 y += layout_pair(boot_editor, y, boot_editor->widgets.args_l,
339 boot_editor->widgets.args_f);
341 if (boot_editor->use_signature_files) {
342 y += layout_pair(boot_editor, y,
343 boot_editor->widgets.args_sig_file_l,
344 boot_editor->widgets.args_sig_file_f);
349 widget_move(widget_button_base(boot_editor->widgets.ok_b), y,
350 boot_editor->field_x);
351 widget_move(widget_button_base(boot_editor->widgets.help_b), y,
352 boot_editor->field_x + 14);
353 widget_move(widget_button_base(boot_editor->widgets.cancel_b), y,
354 boot_editor->field_x + 28);
357 static void boot_editor_widget_focus(struct nc_widget *widget, void *arg)
359 struct boot_editor *boot_editor = arg;
362 w_y = widget_y(widget) + widget_focus_y(widget);
363 s_max = getmaxy(boot_editor->scr.sub_ncw) - 1;
365 if (w_y < boot_editor->scroll_y)
366 boot_editor->scroll_y = w_y;
368 else if (w_y + boot_editor->scroll_y + 1 > s_max)
369 boot_editor->scroll_y = 1 + w_y - s_max;
374 pad_refresh(boot_editor);
377 static void boot_editor_device_select_change(void *arg, int idx)
379 struct boot_editor *boot_editor = arg;
381 boot_editor->selected_device = NULL;
383 boot_editor->selected_device =
384 boot_editor->cui->sysinfo->blockdevs[idx]->name;
387 static void boot_editor_populate_device_select(struct boot_editor *boot_editor,
388 const struct system_info *sysinfo)
390 struct nc_widget_select *select = boot_editor->widgets.device_f;
394 widget_select_drop_options(select);
396 for (i = 0; sysinfo && i < sysinfo->n_blockdevs; i++) {
397 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
400 name = talloc_asprintf(boot_editor, "%s [%s]",
401 bd_info->name, bd_info->uuid);
402 selected = boot_editor->selected_device &&
403 !strcmp(bd_info->name,
404 boot_editor->selected_device);
406 widget_select_add_option(select, i, name, selected);
409 /* If we're editing an existing option, the paths will be fully-
410 * resolved. In this case, we want the manual device pre-selected.
411 * However, we only do this if the widget hasn't been manually
413 selected = !boot_editor->selected_device;
415 widget_select_add_option(select, -1, _("Specify paths/URLs manually"),
419 static bool path_on_device(struct blockdev_info *bd_info,
424 if (!bd_info->mountpoint)
427 len = strlen(bd_info->mountpoint);
428 if (strncmp(bd_info->mountpoint, path, len))
431 /* if the mountpoint doesn't have a trailing slash, ensure that
432 * the path starts with one (so we don't match a "/mnt/sda1/foo" path
433 * on a "/mnt/sda" mountpoint) */
434 return bd_info->mountpoint[len-1] == '/' || path[len] == '/';
438 static void boot_editor_find_device(struct boot_editor *boot_editor,
439 struct pb_boot_data *bd, const struct system_info *sysinfo)
441 struct blockdev_info *bd_info, *tmp;
444 if (!sysinfo || !sysinfo->n_blockdevs)
447 /* find the device for our boot image, by finding the longest
448 * matching blockdev's mountpoint */
449 for (len = 0, i = 0, bd_info = NULL; i < sysinfo->n_blockdevs; i++) {
450 tmp = sysinfo->blockdevs[i];
451 if (!path_on_device(tmp, bd->image))
453 if (strlen(tmp->mountpoint) <= len)
456 len = strlen(tmp->mountpoint);
462 /* ensure that other paths are on this device */
463 if (bd->initrd && !path_on_device(bd_info, bd->initrd))
466 if (bd->dtb && !path_on_device(bd_info, bd->dtb))
469 if (boot_editor->use_signature_files)
470 if (bd->args_sig_file && !path_on_device(bd_info,
474 /* ok, we match; preselect the device option, and remove the common
476 boot_editor->selected_device = bd_info->name;
477 boot_editor->image += len;
479 if (boot_editor->initrd)
480 boot_editor->initrd += len;
481 if (boot_editor->dtb)
482 boot_editor->dtb += len;
483 if (boot_editor->use_signature_files)
484 if (boot_editor->args_sig_file)
485 boot_editor->args_sig_file += len;
488 static void boot_editor_setup_widgets(struct boot_editor *boot_editor,
489 const struct system_info *sysinfo)
491 struct nc_widgetset *set = boot_editor->widgetset;
494 field_size = COLS - 1 - boot_editor->field_x;
496 boot_editor->widgets.device_l = widget_new_label(set, 0, 0,
498 boot_editor->widgets.device_f = widget_new_select(set, 0, 0,
500 widget_select_on_change(boot_editor->widgets.device_f,
501 boot_editor_device_select_change, boot_editor);
503 boot_editor_populate_device_select(boot_editor, sysinfo);
505 boot_editor->widgets.image_l = widget_new_label(set, 0, 0,
507 boot_editor->widgets.image_f = widget_new_textbox(set, 0, 0,
508 field_size, boot_editor->image);
510 boot_editor->widgets.initrd_l = widget_new_label(set, 0, 0,
512 boot_editor->widgets.initrd_f = widget_new_textbox(set, 0, 0,
514 boot_editor->initrd);
516 boot_editor->widgets.dtb_l = widget_new_label(set, 0, 0,
518 boot_editor->widgets.dtb_f = widget_new_textbox(set, 0, 0,
519 field_size, boot_editor->dtb);
521 boot_editor->widgets.args_l = widget_new_label(set, 0, 0,
522 _("Boot arguments:"));
523 boot_editor->widgets.args_f = widget_new_textbox(set, 0, 0,
524 field_size, boot_editor->args);
526 if (boot_editor->use_signature_files) {
527 boot_editor->widgets.args_sig_file_l = widget_new_label(set,
528 0, 0, _("Argument signature file:"));
529 boot_editor->widgets.args_sig_file_f = widget_new_textbox(set,
530 0, 0, field_size, boot_editor->args_sig_file);
533 boot_editor->widgets.args_sig_file_l = NULL;
534 boot_editor->widgets.args_sig_file_f = NULL;
537 boot_editor->widgets.ok_b = widget_new_button(set, 0, 0, 10,
538 _("OK"), ok_click, boot_editor);
539 boot_editor->widgets.help_b = widget_new_button(set, 0, 0, 10,
540 _("Help"), help_click, boot_editor);
541 boot_editor->widgets.cancel_b = widget_new_button(set, 0, 0, 10,
542 _("Cancel"), cancel_click, boot_editor);
545 static void boot_editor_draw(struct boot_editor *boot_editor,
546 const struct system_info *sysinfo)
551 height = pad_height(sysinfo ? sysinfo->n_blockdevs : 0);
553 if (!boot_editor->pad || getmaxy(boot_editor->pad) < height) {
554 if (boot_editor->pad)
555 delwin(boot_editor->pad);
556 boot_editor->pad = newpad(height, COLS);
559 if (boot_editor->widgetset) {
560 widgetset_unpost(boot_editor->widgetset);
561 talloc_free(boot_editor->widgetset);
565 boot_editor->widgetset = widgetset_create(boot_editor,
566 boot_editor->scr.main_ncw,
568 widgetset_set_widget_focus(boot_editor->widgetset,
569 boot_editor_widget_focus, boot_editor);
571 boot_editor_setup_widgets(boot_editor, sysinfo);
572 boot_editor_layout_widgets(boot_editor);
575 widgetset_post(boot_editor->widgetset);
578 void boot_editor_update(struct boot_editor *boot_editor,
579 const struct system_info *sysinfo)
583 if (boot_editor->cui->current != boot_editor_scr(boot_editor)) {
584 boot_editor->need_update = true;
588 str = widget_textbox_get_value(boot_editor->widgets.image_f);
590 talloc_free(boot_editor->image);
591 boot_editor->image = talloc_strdup(boot_editor, str);
594 str = widget_textbox_get_value(boot_editor->widgets.initrd_f);
596 talloc_free(boot_editor->initrd);
597 boot_editor->initrd = talloc_strdup(boot_editor, str);
600 str = widget_textbox_get_value(boot_editor->widgets.dtb_f);
602 talloc_free(boot_editor->dtb);
603 boot_editor->dtb = talloc_strdup(boot_editor, str);
606 str = widget_textbox_get_value(boot_editor->widgets.args_f);
608 talloc_free(boot_editor->args);
609 boot_editor->args = talloc_strdup(boot_editor, str);
612 if (boot_editor->use_signature_files) {
613 str = widget_textbox_get_value(boot_editor->widgets.args_sig_file_f);
615 talloc_free(boot_editor->args_sig_file);
616 boot_editor->args_sig_file = talloc_strdup(boot_editor, str);
620 boot_editor_draw(boot_editor, sysinfo);
621 pad_refresh(boot_editor);
624 struct boot_editor *boot_editor_init(struct cui *cui,
625 struct pmenu_item *item,
626 const struct system_info *sysinfo,
627 void (*on_exit)(struct cui *cui,
628 struct pmenu_item *item,
629 struct pb_boot_data *bd))
631 struct boot_editor *boot_editor;
632 int ncols1, ncols2, ncols3;
634 boot_editor = talloc_zero(cui, struct boot_editor);
639 #if defined(HAVE_LIBGPGME)
640 if (access(LOCKDOWN_FILE, F_OK) == -1)
641 boot_editor->use_signature_files = false;
643 boot_editor->use_signature_files = true;
645 boot_editor->use_signature_files = false;
648 talloc_set_destructor(boot_editor, boot_editor_destructor);
649 boot_editor->cui = cui;
650 boot_editor->item = item;
651 boot_editor->on_exit = on_exit;
652 boot_editor->state = STATE_EDIT;
653 boot_editor->need_redraw = false;
654 boot_editor->need_update = false;
656 ncols1 = strncols(_("Device tree:"));
657 ncols2 = strncols(_("Boot arguments:"));
658 if (boot_editor->use_signature_files)
659 ncols3 = strncols(_("Argument signature file:"));
663 boot_editor->label_x = 1;
664 boot_editor->field_x = 2 + max(max(ncols1, ncols2), ncols3);
666 nc_scr_init(&boot_editor->scr, pb_boot_editor_sig, 0,
667 cui, boot_editor_process_key,
668 boot_editor_post, boot_editor_unpost, boot_editor_resize);
670 boot_editor->scr.frame.ltitle = talloc_strdup(boot_editor,
671 _("Petitboot Option Editor"));
672 boot_editor->scr.frame.rtitle = NULL;
673 boot_editor->scr.frame.help = talloc_strdup(boot_editor,
674 _("tab=next, shift+tab=previous, x=exit, h=help"));
675 nc_scr_frame_draw(&boot_editor->scr);
678 struct pb_boot_data *bd = cod_from_item(item)->bd;
679 boot_editor->image = talloc_strdup(boot_editor, bd->image);
680 boot_editor->initrd = talloc_strdup(boot_editor, bd->initrd);
681 boot_editor->dtb = talloc_strdup(boot_editor, bd->dtb);
682 boot_editor->args = talloc_strdup(boot_editor, bd->args);
683 if (boot_editor->use_signature_files)
684 boot_editor->args_sig_file = talloc_strdup(boot_editor,
687 boot_editor->args_sig_file = talloc_strdup(boot_editor,
689 boot_editor_find_device(boot_editor, bd, sysinfo);
692 boot_editor_draw(boot_editor, sysinfo);
693 wrefresh(boot_editor->scr.main_ncw);