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);
53 struct nc_widgetset *widgetset;
55 struct nc_widget_label *device_l;
56 struct nc_widget_select *device_f;
57 struct nc_widget_label *image_l;
58 struct nc_widget_textbox *image_f;
59 struct nc_widget_label *initrd_l;
60 struct nc_widget_textbox *initrd_f;
61 struct nc_widget_label *dtb_l;
62 struct nc_widget_textbox *dtb_f;
63 struct nc_widget_label *args_l;
64 struct nc_widget_textbox *args_f;
65 struct nc_widget_button *ok_b;
66 struct nc_widget_button *help_b;
67 struct nc_widget_button *cancel_b;
70 const char *selected_device;
77 extern const struct help_text boot_editor_help_text;
79 static struct boot_editor *boot_editor_from_scr(struct nc_scr *scr)
81 struct boot_editor *boot_editor;
83 assert(scr->sig == pb_boot_editor_sig);
84 boot_editor = (struct boot_editor *)
85 ((char *)scr - (size_t)&((struct boot_editor *)0)->scr);
86 assert(boot_editor->scr.sig == pb_boot_editor_sig);
90 static void pad_refresh(struct boot_editor *boot_editor)
94 getmaxyx(boot_editor->scr.sub_ncw, rows, cols);
95 getbegyx(boot_editor->scr.sub_ncw, y, x);
97 prefresh(boot_editor->pad, boot_editor->scroll_y, 0,
101 static struct boot_editor *boot_editor_from_arg(void *arg)
103 struct boot_editor *boot_editor = arg;
105 assert(boot_editor->scr.sig == pb_boot_editor_sig);
109 static int boot_editor_post(struct nc_scr *scr)
111 struct boot_editor *boot_editor = boot_editor_from_scr(scr);
112 widgetset_post(boot_editor->widgetset);
113 nc_scr_frame_draw(scr);
114 redrawwin(scr->main_ncw);
115 wrefresh(boot_editor->scr.main_ncw);
116 pad_refresh(boot_editor);
120 static int boot_editor_unpost(struct nc_scr *scr)
122 widgetset_unpost(boot_editor_from_scr(scr)->widgetset);
126 struct nc_scr *boot_editor_scr(struct boot_editor *boot_editor)
128 return &boot_editor->scr;
131 static void boot_editor_resize(struct nc_scr *scr)
133 /* FIXME: forms can't be resized, need to recreate here */
134 boot_editor_unpost(scr);
135 boot_editor_post(scr);
138 static char *conditional_prefix(struct pb_boot_data *ctx,
139 const char *prefix, const char *value)
143 if (!value || !*value)
149 else if (prefix[strlen(prefix)] != '/')
152 return talloc_asprintf(ctx, "%s%s%s", prefix, sep, value);
155 static struct pb_boot_data *boot_editor_prepare_data(
156 struct boot_editor *boot_editor)
158 struct pb_boot_data *bd;
162 bd = talloc(boot_editor, struct pb_boot_data);
167 idx = widget_select_get_value(boot_editor->widgets.device_f);
168 if (idx == -1 || (unsigned int)idx >
169 boot_editor->cui->sysinfo->n_blockdevs)
172 prefix = boot_editor->cui->sysinfo->blockdevs[idx]->mountpoint;
174 s = widget_textbox_get_value(boot_editor->widgets.image_f);
175 bd->image = conditional_prefix(bd, prefix, s);
177 s = widget_textbox_get_value(boot_editor->widgets.initrd_f);
178 bd->initrd = conditional_prefix(bd, prefix, s);
180 s = widget_textbox_get_value(boot_editor->widgets.dtb_f);
181 bd->dtb = conditional_prefix(bd, prefix, s);
183 s = widget_textbox_get_value(boot_editor->widgets.args_f);
184 bd->args = *s ? talloc_strdup(bd, s) : NULL;
190 * boot_editor_process_key - Process a user keystroke.
192 * Called from the cui via the scr:process_key method.
195 static void boot_editor_process_key(struct nc_scr *scr, int key)
197 struct boot_editor *boot_editor = boot_editor_from_scr(scr);
198 struct pmenu_item *item;
199 struct pb_boot_data *bd;
202 handled = widgetset_process_key(boot_editor->widgetset, key);
204 pad_refresh(boot_editor);
206 else if (key == 'x' || key == 27)
207 boot_editor->state = STATE_CANCEL;
210 boot_editor->state = STATE_HELP;
215 switch (boot_editor->state) {
217 item = boot_editor->item;
218 bd = boot_editor_prepare_data(boot_editor);
221 boot_editor->on_exit(boot_editor->cui, item, bd);
224 boot_editor->state = STATE_EDIT;
225 cui_show_help(boot_editor->cui, _("Boot Option Editor"),
226 &boot_editor_help_text);
234 * boot_editor_destructor - The talloc destructor for a boot_editor.
237 static int boot_editor_destructor(void *arg)
239 struct boot_editor *boot_editor = boot_editor_from_arg(arg);
240 boot_editor->scr.sig = pb_removed_sig;
241 if (boot_editor->pad)
242 delwin(boot_editor->pad);
246 static void ok_click(void *arg)
248 struct boot_editor *boot_editor = arg;
249 boot_editor->state = STATE_SAVE;
252 static void help_click(void *arg)
254 struct boot_editor *boot_editor = arg;
255 boot_editor->state = STATE_HELP;
258 static void cancel_click(void *arg)
260 struct boot_editor *boot_editor = arg;
261 boot_editor->state = STATE_CANCEL;
264 static int layout_pair(struct boot_editor *boot_editor, int y,
265 struct nc_widget_label *label,
266 struct nc_widget_textbox *field)
268 struct nc_widget *label_w = widget_label_base(label);
269 struct nc_widget *field_w = widget_textbox_base(field);
270 widget_move(label_w, y, boot_editor->label_x);
271 widget_move(field_w, y, boot_editor->field_x);
272 return max(widget_height(label_w), widget_height(field_w));
275 static int pad_height(int blockdevs_height)
277 return 10 + blockdevs_height;
280 static void boot_editor_layout_widgets(struct boot_editor *boot_editor)
282 struct nc_widget *wf, *wl;
285 wl = widget_label_base(boot_editor->widgets.device_l);
286 wf = widget_select_base(boot_editor->widgets.device_f);
287 widget_move(wl, y, boot_editor->label_x);
288 widget_move(wf, y, boot_editor->field_x);
290 y += widget_height(wf) + 1;
293 y += layout_pair(boot_editor, y, boot_editor->widgets.image_l,
294 boot_editor->widgets.image_f);
296 y += layout_pair(boot_editor, y, boot_editor->widgets.initrd_l,
297 boot_editor->widgets.initrd_f);
299 y += layout_pair(boot_editor, y, boot_editor->widgets.dtb_l,
300 boot_editor->widgets.dtb_f);
302 y += layout_pair(boot_editor, y, boot_editor->widgets.args_l,
303 boot_editor->widgets.args_f);
307 widget_move(widget_button_base(boot_editor->widgets.ok_b), y, 9);
308 widget_move(widget_button_base(boot_editor->widgets.help_b), y, 19);
309 widget_move(widget_button_base(boot_editor->widgets.cancel_b), y, 33);
312 static void boot_editor_widget_focus(struct nc_widget *widget, void *arg)
314 struct boot_editor *boot_editor = arg;
317 w_y = widget_y(widget) + widget_focus_y(widget);
318 s_max = getmaxy(boot_editor->scr.sub_ncw) - 1;
320 if (w_y < boot_editor->scroll_y)
321 boot_editor->scroll_y = w_y;
323 else if (w_y + boot_editor->scroll_y + 1 > s_max)
324 boot_editor->scroll_y = 1 + w_y - s_max;
329 pad_refresh(boot_editor);
332 static void boot_editor_device_select_change(void *arg, int idx)
334 struct boot_editor *boot_editor = arg;
336 boot_editor->selected_device = NULL;
338 boot_editor->selected_device =
339 boot_editor->cui->sysinfo->blockdevs[idx]->name;
342 static void boot_editor_populate_device_select(struct boot_editor *boot_editor,
343 const struct system_info *sysinfo)
345 struct nc_widget_select *select = boot_editor->widgets.device_f;
349 widget_select_drop_options(select);
351 for (i = 0; sysinfo && i < sysinfo->n_blockdevs; i++) {
352 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
355 name = talloc_asprintf(boot_editor, "%s [%s]",
356 bd_info->name, bd_info->uuid);
357 selected = boot_editor->selected_device &&
358 !strcmp(bd_info->name,
359 boot_editor->selected_device);
361 widget_select_add_option(select, i, name, selected);
364 /* If we're editing an existing option, the paths will be fully-
365 * resolved. In this case, we want the manual device pre-selected.
366 * However, we only do this if the widget hasn't been manually
368 selected = !boot_editor->selected_device;
370 widget_select_add_option(select, -1, _("Specify paths/URLs manually"),
374 static bool path_on_device(struct blockdev_info *bd_info,
379 if (!bd_info->mountpoint)
382 len = strlen(bd_info->mountpoint);
383 if (strncmp(bd_info->mountpoint, path, len))
386 /* if the mountpoint doesn't have a trailing slash, ensure that
387 * the path starts with one (so we don't match a "/mnt/sda1/foo" path
388 * on a "/mnt/sda" mountpoint) */
389 return bd_info->mountpoint[len-1] == '/' || path[len] == '/';
393 static void boot_editor_find_device(struct boot_editor *boot_editor,
394 struct pb_boot_data *bd, const struct system_info *sysinfo)
396 struct blockdev_info *bd_info, *tmp;
399 if (!sysinfo || !sysinfo->n_blockdevs)
402 /* find the device for our boot image, by finding the longest
403 * matching blockdev's mountpoint */
404 for (len = 0, i = 0, bd_info = NULL; i < sysinfo->n_blockdevs; i++) {
405 tmp = sysinfo->blockdevs[i];
406 if (!path_on_device(tmp, bd->image))
408 if (strlen(tmp->mountpoint) <= len)
411 len = strlen(tmp->mountpoint);
417 /* ensure that other paths are on this device */
418 if (bd->initrd && !path_on_device(bd_info, bd->initrd))
421 if (bd->dtb && !path_on_device(bd_info, bd->dtb))
424 /* ok, we match; preselect the device option, and remove the common
426 boot_editor->selected_device = bd_info->name;
427 boot_editor->image += len;
429 if (boot_editor->initrd)
430 boot_editor->initrd += len;
431 if (boot_editor->dtb)
432 boot_editor->dtb += len;
435 static void boot_editor_setup_widgets(struct boot_editor *boot_editor,
436 const struct system_info *sysinfo)
438 struct nc_widgetset *set;
441 field_size = COLS - 1 - boot_editor->field_x;
443 boot_editor->widgetset = set = widgetset_create(boot_editor,
444 boot_editor->scr.main_ncw,
447 widgetset_set_widget_focus(boot_editor->widgetset,
448 boot_editor_widget_focus, boot_editor);
450 boot_editor->widgets.device_l = widget_new_label(set, 0, 0,
452 boot_editor->widgets.device_f = widget_new_select(set, 0, 0,
454 widget_select_on_change(boot_editor->widgets.device_f,
455 boot_editor_device_select_change, boot_editor);
457 boot_editor_populate_device_select(boot_editor, sysinfo);
459 boot_editor->widgets.image_l = widget_new_label(set, 0, 0,
461 boot_editor->widgets.image_f = widget_new_textbox(set, 0, 0,
462 field_size, boot_editor->image);
464 boot_editor->widgets.initrd_l = widget_new_label(set, 0, 0,
466 boot_editor->widgets.initrd_f = widget_new_textbox(set, 0, 0,
468 boot_editor->initrd);
470 boot_editor->widgets.dtb_l = widget_new_label(set, 0, 0,
472 boot_editor->widgets.dtb_f = widget_new_textbox(set, 0, 0,
473 field_size, boot_editor->dtb);
475 boot_editor->widgets.args_l = widget_new_label(set, 0, 0,
476 _("Boot arguments:"));
477 boot_editor->widgets.args_f = widget_new_textbox(set, 0, 0,
478 field_size, boot_editor->args);
480 boot_editor->widgets.ok_b = widget_new_button(set, 0, 0, 6,
481 _("OK"), ok_click, boot_editor);
482 boot_editor->widgets.help_b = widget_new_button(set, 0, 0, 10,
483 _("Help"), help_click, boot_editor);
484 boot_editor->widgets.cancel_b = widget_new_button(set, 0, 0, 10,
485 _("Cancel"), cancel_click, boot_editor);
488 void boot_editor_update(struct boot_editor *boot_editor,
489 const struct system_info *sysinfo)
493 widgetset_unpost(boot_editor->widgetset);
495 height = pad_height(sysinfo ? sysinfo->n_blockdevs : 0);
496 if (getmaxy(boot_editor->pad) < height) {
497 delwin(boot_editor->pad);
498 boot_editor->pad = newpad(height, COLS);
499 widgetset_set_windows(boot_editor->widgetset,
500 boot_editor->scr.main_ncw,
504 boot_editor_populate_device_select(boot_editor, sysinfo);
506 boot_editor_layout_widgets(boot_editor);
508 widgetset_post(boot_editor->widgetset);
510 pad_refresh(boot_editor);
513 /* Return the number of columns required to display a localised string */
514 static int strncols(const char *str)
516 int i, wlen, ncols = 0;
519 wlen = mbstowcs(NULL, str, 0);
523 wstr = malloc(sizeof(wchar_t) * wlen + 1);
527 wlen = mbstowcs(wstr, str, wlen);
533 /* Processing each character individually lets us use the same
534 * check for all languages */
535 for (i = 0; i < wlen; i++) {
536 ncols += wcwidth(wstr[i]);
543 struct boot_editor *boot_editor_init(struct cui *cui,
544 struct pmenu_item *item,
545 const struct system_info *sysinfo,
546 void (*on_exit)(struct cui *cui,
547 struct pmenu_item *item,
548 struct pb_boot_data *bd))
550 struct boot_editor *boot_editor;
552 boot_editor = talloc_zero(cui, struct boot_editor);
557 talloc_set_destructor(boot_editor, boot_editor_destructor);
558 boot_editor->cui = cui;
559 boot_editor->item = item;
560 boot_editor->on_exit = on_exit;
561 boot_editor->state = STATE_EDIT;
563 int ncols1 = strncols(_("Device tree:"));
564 int ncols2 = strncols(_("Boot arguments:"));
566 boot_editor->label_x = 1;
567 boot_editor->field_x = 2 + max(ncols1, ncols2);
569 nc_scr_init(&boot_editor->scr, pb_boot_editor_sig, 0,
570 cui, boot_editor_process_key,
571 boot_editor_post, boot_editor_unpost, boot_editor_resize);
573 boot_editor->scr.frame.ltitle = talloc_strdup(boot_editor,
574 _("Petitboot Option Editor"));
575 boot_editor->scr.frame.rtitle = NULL;
576 boot_editor->scr.frame.help = talloc_strdup(boot_editor,
577 _("tab=next, shift+tab=previous, x=exit, h=help"));
578 nc_scr_frame_draw(&boot_editor->scr);
581 struct pb_boot_data *bd = cod_from_item(item)->bd;
582 boot_editor->image = bd->image;
583 boot_editor->initrd = bd->initrd;
584 boot_editor->dtb = bd->dtb;
585 boot_editor->args = bd->args;
586 boot_editor_find_device(boot_editor, bd, sysinfo);
588 boot_editor->image = boot_editor->initrd =
589 boot_editor->dtb = boot_editor->args = "";
592 boot_editor->pad = newpad(
593 pad_height(sysinfo ? sysinfo->n_blockdevs : 0),
596 boot_editor_setup_widgets(boot_editor, sysinfo);
598 boot_editor_layout_widgets(boot_editor);
599 wrefresh(boot_editor->scr.main_ncw);