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)
28 #include "talloc/talloc.h"
29 #include "i18n/i18n.h"
30 #include "nc-boot-editor.h"
31 #include "nc-widgets.h"
37 struct pmenu_item *item;
44 void (*on_exit)(struct cui *cui,
45 struct pmenu_item *item,
46 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_button *ok_b;
67 struct nc_widget_button *help_b;
68 struct nc_widget_button *cancel_b;
71 const char *selected_device;
78 extern const struct help_text boot_editor_help_text;
80 static struct boot_editor *boot_editor_from_scr(struct nc_scr *scr)
82 struct boot_editor *boot_editor;
84 assert(scr->sig == pb_boot_editor_sig);
85 boot_editor = (struct boot_editor *)
86 ((char *)scr - (size_t)&((struct boot_editor *)0)->scr);
87 assert(boot_editor->scr.sig == pb_boot_editor_sig);
91 static void pad_refresh(struct boot_editor *boot_editor)
95 getmaxyx(boot_editor->scr.sub_ncw, rows, cols);
96 getbegyx(boot_editor->scr.sub_ncw, y, x);
98 prefresh(boot_editor->pad, boot_editor->scroll_y, 0,
102 static struct boot_editor *boot_editor_from_arg(void *arg)
104 struct boot_editor *boot_editor = arg;
106 assert(boot_editor->scr.sig == pb_boot_editor_sig);
110 static int boot_editor_post(struct nc_scr *scr)
112 struct boot_editor *boot_editor = boot_editor_from_scr(scr);
113 widgetset_post(boot_editor->widgetset);
114 nc_scr_frame_draw(scr);
115 if (boot_editor->need_redraw) {
116 redrawwin(scr->main_ncw);
117 boot_editor->need_redraw = false;
119 wrefresh(boot_editor->scr.main_ncw);
120 pad_refresh(boot_editor);
124 static int boot_editor_unpost(struct nc_scr *scr)
126 widgetset_unpost(boot_editor_from_scr(scr)->widgetset);
130 struct nc_scr *boot_editor_scr(struct boot_editor *boot_editor)
132 return &boot_editor->scr;
135 static void boot_editor_resize(struct nc_scr *scr)
137 /* FIXME: forms can't be resized, need to recreate here */
138 boot_editor_unpost(scr);
139 boot_editor_post(scr);
142 static char *conditional_prefix(struct pb_boot_data *ctx,
143 const char *prefix, const char *value)
147 if (!value || !*value)
153 else if (prefix[strlen(prefix)] != '/')
156 return talloc_asprintf(ctx, "%s%s%s", prefix, sep, value);
159 static struct pb_boot_data *boot_editor_prepare_data(
160 struct boot_editor *boot_editor)
162 struct pb_boot_data *bd;
166 bd = talloc(boot_editor, struct pb_boot_data);
171 idx = widget_select_get_value(boot_editor->widgets.device_f);
172 if (idx == -1 || (unsigned int)idx >
173 boot_editor->cui->sysinfo->n_blockdevs)
176 prefix = boot_editor->cui->sysinfo->blockdevs[idx]->mountpoint;
178 s = widget_textbox_get_value(boot_editor->widgets.image_f);
179 bd->image = conditional_prefix(bd, prefix, s);
185 s = widget_textbox_get_value(boot_editor->widgets.initrd_f);
186 bd->initrd = conditional_prefix(bd, prefix, s);
188 s = widget_textbox_get_value(boot_editor->widgets.dtb_f);
189 bd->dtb = conditional_prefix(bd, prefix, s);
191 s = widget_textbox_get_value(boot_editor->widgets.args_f);
192 bd->args = *s ? talloc_strdup(bd, s) : NULL;
198 * boot_editor_process_key - Process a user keystroke.
200 * Called from the cui via the scr:process_key method.
203 static void boot_editor_process_key(struct nc_scr *scr, int key)
205 struct boot_editor *boot_editor = boot_editor_from_scr(scr);
206 struct pmenu_item *item;
207 struct pb_boot_data *bd;
210 handled = widgetset_process_key(boot_editor->widgetset, key);
212 pad_refresh(boot_editor);
214 else if (key == 'x' || key == 27)
215 boot_editor->state = STATE_CANCEL;
218 boot_editor->state = STATE_HELP;
223 switch (boot_editor->state) {
225 item = boot_editor->item;
226 bd = boot_editor_prepare_data(boot_editor);
228 /* Incomplete entry */
229 boot_editor->state = STATE_EDIT;
234 boot_editor->on_exit(boot_editor->cui, item, bd);
237 boot_editor->state = STATE_EDIT;
238 boot_editor->need_redraw = true;
239 cui_show_help(boot_editor->cui, _("Boot Option Editor"),
240 &boot_editor_help_text);
248 * boot_editor_destructor - The talloc destructor for a boot_editor.
251 static int boot_editor_destructor(void *arg)
253 struct boot_editor *boot_editor = boot_editor_from_arg(arg);
254 boot_editor->scr.sig = pb_removed_sig;
255 if (boot_editor->pad)
256 delwin(boot_editor->pad);
260 static void ok_click(void *arg)
262 struct boot_editor *boot_editor = arg;
263 boot_editor->state = STATE_SAVE;
266 static void help_click(void *arg)
268 struct boot_editor *boot_editor = arg;
269 boot_editor->state = STATE_HELP;
272 static void cancel_click(void *arg)
274 struct boot_editor *boot_editor = arg;
275 boot_editor->state = STATE_CANCEL;
278 static int layout_pair(struct boot_editor *boot_editor, int y,
279 struct nc_widget_label *label,
280 struct nc_widget_textbox *field)
282 struct nc_widget *label_w = widget_label_base(label);
283 struct nc_widget *field_w = widget_textbox_base(field);
284 widget_move(label_w, y, boot_editor->label_x);
285 widget_move(field_w, y, boot_editor->field_x);
286 return max(widget_height(label_w), widget_height(field_w));
289 static int pad_height(int blockdevs_height)
291 return 10 + blockdevs_height;
294 static void boot_editor_layout_widgets(struct boot_editor *boot_editor)
296 struct nc_widget *wf, *wl;
299 wl = widget_label_base(boot_editor->widgets.device_l);
300 wf = widget_select_base(boot_editor->widgets.device_f);
301 widget_move(wl, y, boot_editor->label_x);
302 widget_move(wf, y, boot_editor->field_x);
304 y += widget_height(wf) + 1;
307 y += layout_pair(boot_editor, y, boot_editor->widgets.image_l,
308 boot_editor->widgets.image_f);
310 y += layout_pair(boot_editor, y, boot_editor->widgets.initrd_l,
311 boot_editor->widgets.initrd_f);
313 y += layout_pair(boot_editor, y, boot_editor->widgets.dtb_l,
314 boot_editor->widgets.dtb_f);
316 y += layout_pair(boot_editor, y, boot_editor->widgets.args_l,
317 boot_editor->widgets.args_f);
321 widget_move(widget_button_base(boot_editor->widgets.ok_b), y, 9);
322 widget_move(widget_button_base(boot_editor->widgets.help_b), y, 19);
323 widget_move(widget_button_base(boot_editor->widgets.cancel_b), y, 33);
326 static void boot_editor_widget_focus(struct nc_widget *widget, void *arg)
328 struct boot_editor *boot_editor = arg;
331 w_y = widget_y(widget) + widget_focus_y(widget);
332 s_max = getmaxy(boot_editor->scr.sub_ncw) - 1;
334 if (w_y < boot_editor->scroll_y)
335 boot_editor->scroll_y = w_y;
337 else if (w_y + boot_editor->scroll_y + 1 > s_max)
338 boot_editor->scroll_y = 1 + w_y - s_max;
343 pad_refresh(boot_editor);
346 static void boot_editor_device_select_change(void *arg, int idx)
348 struct boot_editor *boot_editor = arg;
350 boot_editor->selected_device = NULL;
352 boot_editor->selected_device =
353 boot_editor->cui->sysinfo->blockdevs[idx]->name;
356 static void boot_editor_populate_device_select(struct boot_editor *boot_editor,
357 const struct system_info *sysinfo)
359 struct nc_widget_select *select = boot_editor->widgets.device_f;
363 widget_select_drop_options(select);
365 for (i = 0; sysinfo && i < sysinfo->n_blockdevs; i++) {
366 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
369 name = talloc_asprintf(boot_editor, "%s [%s]",
370 bd_info->name, bd_info->uuid);
371 selected = boot_editor->selected_device &&
372 !strcmp(bd_info->name,
373 boot_editor->selected_device);
375 widget_select_add_option(select, i, name, selected);
378 /* If we're editing an existing option, the paths will be fully-
379 * resolved. In this case, we want the manual device pre-selected.
380 * However, we only do this if the widget hasn't been manually
382 selected = !boot_editor->selected_device;
384 widget_select_add_option(select, -1, _("Specify paths/URLs manually"),
388 static bool path_on_device(struct blockdev_info *bd_info,
393 if (!bd_info->mountpoint)
396 len = strlen(bd_info->mountpoint);
397 if (strncmp(bd_info->mountpoint, path, len))
400 /* if the mountpoint doesn't have a trailing slash, ensure that
401 * the path starts with one (so we don't match a "/mnt/sda1/foo" path
402 * on a "/mnt/sda" mountpoint) */
403 return bd_info->mountpoint[len-1] == '/' || path[len] == '/';
407 static void boot_editor_find_device(struct boot_editor *boot_editor,
408 struct pb_boot_data *bd, const struct system_info *sysinfo)
410 struct blockdev_info *bd_info, *tmp;
413 if (!sysinfo || !sysinfo->n_blockdevs)
416 /* find the device for our boot image, by finding the longest
417 * matching blockdev's mountpoint */
418 for (len = 0, i = 0, bd_info = NULL; i < sysinfo->n_blockdevs; i++) {
419 tmp = sysinfo->blockdevs[i];
420 if (!path_on_device(tmp, bd->image))
422 if (strlen(tmp->mountpoint) <= len)
425 len = strlen(tmp->mountpoint);
431 /* ensure that other paths are on this device */
432 if (bd->initrd && !path_on_device(bd_info, bd->initrd))
435 if (bd->dtb && !path_on_device(bd_info, bd->dtb))
438 /* ok, we match; preselect the device option, and remove the common
440 boot_editor->selected_device = bd_info->name;
441 boot_editor->image += len;
443 if (boot_editor->initrd)
444 boot_editor->initrd += len;
445 if (boot_editor->dtb)
446 boot_editor->dtb += len;
449 static void boot_editor_setup_widgets(struct boot_editor *boot_editor,
450 const struct system_info *sysinfo)
452 struct nc_widgetset *set;
455 field_size = COLS - 1 - boot_editor->field_x;
457 boot_editor->widgetset = set = widgetset_create(boot_editor,
458 boot_editor->scr.main_ncw,
461 widgetset_set_widget_focus(boot_editor->widgetset,
462 boot_editor_widget_focus, boot_editor);
464 boot_editor->widgets.device_l = widget_new_label(set, 0, 0,
466 boot_editor->widgets.device_f = widget_new_select(set, 0, 0,
468 widget_select_on_change(boot_editor->widgets.device_f,
469 boot_editor_device_select_change, boot_editor);
471 boot_editor_populate_device_select(boot_editor, sysinfo);
473 boot_editor->widgets.image_l = widget_new_label(set, 0, 0,
475 boot_editor->widgets.image_f = widget_new_textbox(set, 0, 0,
476 field_size, boot_editor->image);
478 boot_editor->widgets.initrd_l = widget_new_label(set, 0, 0,
480 boot_editor->widgets.initrd_f = widget_new_textbox(set, 0, 0,
482 boot_editor->initrd);
484 boot_editor->widgets.dtb_l = widget_new_label(set, 0, 0,
486 boot_editor->widgets.dtb_f = widget_new_textbox(set, 0, 0,
487 field_size, boot_editor->dtb);
489 boot_editor->widgets.args_l = widget_new_label(set, 0, 0,
490 _("Boot arguments:"));
491 boot_editor->widgets.args_f = widget_new_textbox(set, 0, 0,
492 field_size, boot_editor->args);
494 boot_editor->widgets.ok_b = widget_new_button(set, 0, 0, 6,
495 _("OK"), ok_click, boot_editor);
496 boot_editor->widgets.help_b = widget_new_button(set, 0, 0, 10,
497 _("Help"), help_click, boot_editor);
498 boot_editor->widgets.cancel_b = widget_new_button(set, 0, 0, 10,
499 _("Cancel"), cancel_click, boot_editor);
502 void boot_editor_update(struct boot_editor *boot_editor,
503 const struct system_info *sysinfo)
507 widgetset_unpost(boot_editor->widgetset);
509 height = pad_height(sysinfo ? sysinfo->n_blockdevs : 0);
510 if (getmaxy(boot_editor->pad) < height) {
511 delwin(boot_editor->pad);
512 boot_editor->pad = newpad(height, COLS);
513 widgetset_set_windows(boot_editor->widgetset,
514 boot_editor->scr.main_ncw,
518 boot_editor_populate_device_select(boot_editor, sysinfo);
520 boot_editor_layout_widgets(boot_editor);
522 widgetset_post(boot_editor->widgetset);
524 pad_refresh(boot_editor);
527 /* Return the number of columns required to display a localised string */
528 static int strncols(const char *str)
530 int i, wlen, ncols = 0;
533 wlen = mbstowcs(NULL, str, 0);
537 wstr = malloc(sizeof(wchar_t) * wlen + 1);
541 wlen = mbstowcs(wstr, str, wlen);
547 /* Processing each character individually lets us use the same
548 * check for all languages */
549 for (i = 0; i < wlen; i++) {
550 ncols += wcwidth(wstr[i]);
557 struct boot_editor *boot_editor_init(struct cui *cui,
558 struct pmenu_item *item,
559 const struct system_info *sysinfo,
560 void (*on_exit)(struct cui *cui,
561 struct pmenu_item *item,
562 struct pb_boot_data *bd))
564 struct boot_editor *boot_editor;
566 boot_editor = talloc_zero(cui, struct boot_editor);
571 talloc_set_destructor(boot_editor, boot_editor_destructor);
572 boot_editor->cui = cui;
573 boot_editor->item = item;
574 boot_editor->on_exit = on_exit;
575 boot_editor->state = STATE_EDIT;
576 boot_editor->need_redraw = false;
578 int ncols1 = strncols(_("Device tree:"));
579 int ncols2 = strncols(_("Boot arguments:"));
581 boot_editor->label_x = 1;
582 boot_editor->field_x = 2 + max(ncols1, ncols2);
584 nc_scr_init(&boot_editor->scr, pb_boot_editor_sig, 0,
585 cui, boot_editor_process_key,
586 boot_editor_post, boot_editor_unpost, boot_editor_resize);
588 boot_editor->scr.frame.ltitle = talloc_strdup(boot_editor,
589 _("Petitboot Option Editor"));
590 boot_editor->scr.frame.rtitle = NULL;
591 boot_editor->scr.frame.help = talloc_strdup(boot_editor,
592 _("tab=next, shift+tab=previous, x=exit, h=help"));
593 nc_scr_frame_draw(&boot_editor->scr);
596 struct pb_boot_data *bd = cod_from_item(item)->bd;
597 boot_editor->image = bd->image;
598 boot_editor->initrd = bd->initrd;
599 boot_editor->dtb = bd->dtb;
600 boot_editor->args = bd->args;
601 boot_editor_find_device(boot_editor, bd, sysinfo);
603 boot_editor->image = boot_editor->initrd =
604 boot_editor->dtb = boot_editor->args = "";
607 boot_editor->pad = newpad(
608 pad_height(sysinfo ? sysinfo->n_blockdevs : 0),
611 boot_editor_setup_widgets(boot_editor, sysinfo);
613 boot_editor_layout_widgets(boot_editor);
614 wrefresh(boot_editor->scr.main_ncw);