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_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);
114 if (boot_editor->need_update) {
115 boot_editor_update(boot_editor, boot_editor->cui->sysinfo);
116 boot_editor->need_update = false;
118 widgetset_post(boot_editor->widgetset);
121 nc_scr_frame_draw(scr);
122 if (boot_editor->need_redraw) {
123 redrawwin(scr->main_ncw);
124 boot_editor->need_redraw = false;
126 wrefresh(boot_editor->scr.main_ncw);
127 pad_refresh(boot_editor);
131 static int boot_editor_unpost(struct nc_scr *scr)
133 widgetset_unpost(boot_editor_from_scr(scr)->widgetset);
137 struct nc_scr *boot_editor_scr(struct boot_editor *boot_editor)
139 return &boot_editor->scr;
142 static void boot_editor_resize(struct nc_scr *scr)
144 /* FIXME: forms can't be resized, need to recreate here */
145 boot_editor_unpost(scr);
146 boot_editor_post(scr);
149 static char *conditional_prefix(struct pb_boot_data *ctx,
150 const char *prefix, const char *value)
154 if (!value || !*value)
160 else if (prefix[strlen(prefix)] != '/')
163 return talloc_asprintf(ctx, "%s%s%s", prefix, sep, value);
166 static struct pb_boot_data *boot_editor_prepare_data(
167 struct boot_editor *boot_editor)
169 struct pb_boot_data *bd;
173 bd = talloc(boot_editor, struct pb_boot_data);
178 idx = widget_select_get_value(boot_editor->widgets.device_f);
179 if (idx == -1 || (unsigned int)idx >
180 boot_editor->cui->sysinfo->n_blockdevs)
183 prefix = boot_editor->cui->sysinfo->blockdevs[idx]->mountpoint;
185 s = widget_textbox_get_value(boot_editor->widgets.image_f);
186 bd->image = conditional_prefix(bd, prefix, s);
192 s = widget_textbox_get_value(boot_editor->widgets.initrd_f);
193 bd->initrd = conditional_prefix(bd, prefix, s);
195 s = widget_textbox_get_value(boot_editor->widgets.dtb_f);
196 bd->dtb = conditional_prefix(bd, prefix, s);
198 s = widget_textbox_get_value(boot_editor->widgets.args_f);
199 bd->args = *s ? talloc_strdup(bd, s) : NULL;
205 * boot_editor_process_key - Process a user keystroke.
207 * Called from the cui via the scr:process_key method.
210 static void boot_editor_process_key(struct nc_scr *scr, int key)
212 struct boot_editor *boot_editor = boot_editor_from_scr(scr);
213 struct pmenu_item *item;
214 struct pb_boot_data *bd;
217 handled = widgetset_process_key(boot_editor->widgetset, key);
219 pad_refresh(boot_editor);
221 else if (key == 'x' || key == 27)
222 boot_editor->state = STATE_CANCEL;
225 boot_editor->state = STATE_HELP;
230 switch (boot_editor->state) {
232 item = boot_editor->item;
233 bd = boot_editor_prepare_data(boot_editor);
235 /* Incomplete entry */
236 boot_editor->state = STATE_EDIT;
241 boot_editor->on_exit(boot_editor->cui, item, bd);
244 boot_editor->state = STATE_EDIT;
245 boot_editor->need_redraw = true;
246 cui_show_help(boot_editor->cui, _("Boot Option Editor"),
247 &boot_editor_help_text);
255 * boot_editor_destructor - The talloc destructor for a boot_editor.
258 static int boot_editor_destructor(void *arg)
260 struct boot_editor *boot_editor = boot_editor_from_arg(arg);
261 boot_editor->scr.sig = pb_removed_sig;
262 if (boot_editor->pad)
263 delwin(boot_editor->pad);
267 static void ok_click(void *arg)
269 struct boot_editor *boot_editor = arg;
270 boot_editor->state = STATE_SAVE;
273 static void help_click(void *arg)
275 struct boot_editor *boot_editor = arg;
276 boot_editor->state = STATE_HELP;
279 static void cancel_click(void *arg)
281 struct boot_editor *boot_editor = arg;
282 boot_editor->state = STATE_CANCEL;
285 static int layout_pair(struct boot_editor *boot_editor, int y,
286 struct nc_widget_label *label,
287 struct nc_widget_textbox *field)
289 struct nc_widget *label_w = widget_label_base(label);
290 struct nc_widget *field_w = widget_textbox_base(field);
291 widget_move(label_w, y, boot_editor->label_x);
292 widget_move(field_w, y, boot_editor->field_x);
293 return max(widget_height(label_w), widget_height(field_w));
296 static int pad_height(int blockdevs_height)
298 return 10 + blockdevs_height;
301 static void boot_editor_layout_widgets(struct boot_editor *boot_editor)
303 struct nc_widget *wf, *wl;
306 wl = widget_label_base(boot_editor->widgets.device_l);
307 wf = widget_select_base(boot_editor->widgets.device_f);
308 widget_move(wl, y, boot_editor->label_x);
309 widget_move(wf, y, boot_editor->field_x);
311 y += widget_height(wf) + 1;
314 y += layout_pair(boot_editor, y, boot_editor->widgets.image_l,
315 boot_editor->widgets.image_f);
317 y += layout_pair(boot_editor, y, boot_editor->widgets.initrd_l,
318 boot_editor->widgets.initrd_f);
320 y += layout_pair(boot_editor, y, boot_editor->widgets.dtb_l,
321 boot_editor->widgets.dtb_f);
323 y += layout_pair(boot_editor, y, boot_editor->widgets.args_l,
324 boot_editor->widgets.args_f);
328 widget_move(widget_button_base(boot_editor->widgets.ok_b), y,
329 boot_editor->field_x);
330 widget_move(widget_button_base(boot_editor->widgets.help_b), y,
331 boot_editor->field_x + 14);
332 widget_move(widget_button_base(boot_editor->widgets.cancel_b), y,
333 boot_editor->field_x + 28);
336 static void boot_editor_widget_focus(struct nc_widget *widget, void *arg)
338 struct boot_editor *boot_editor = arg;
341 w_y = widget_y(widget) + widget_focus_y(widget);
342 s_max = getmaxy(boot_editor->scr.sub_ncw) - 1;
344 if (w_y < boot_editor->scroll_y)
345 boot_editor->scroll_y = w_y;
347 else if (w_y + boot_editor->scroll_y + 1 > s_max)
348 boot_editor->scroll_y = 1 + w_y - s_max;
353 pad_refresh(boot_editor);
356 static void boot_editor_device_select_change(void *arg, int idx)
358 struct boot_editor *boot_editor = arg;
360 boot_editor->selected_device = NULL;
362 boot_editor->selected_device =
363 boot_editor->cui->sysinfo->blockdevs[idx]->name;
366 static void boot_editor_populate_device_select(struct boot_editor *boot_editor,
367 const struct system_info *sysinfo)
369 struct nc_widget_select *select = boot_editor->widgets.device_f;
373 widget_select_drop_options(select);
375 for (i = 0; sysinfo && i < sysinfo->n_blockdevs; i++) {
376 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
379 name = talloc_asprintf(boot_editor, "%s [%s]",
380 bd_info->name, bd_info->uuid);
381 selected = boot_editor->selected_device &&
382 !strcmp(bd_info->name,
383 boot_editor->selected_device);
385 widget_select_add_option(select, i, name, selected);
388 /* If we're editing an existing option, the paths will be fully-
389 * resolved. In this case, we want the manual device pre-selected.
390 * However, we only do this if the widget hasn't been manually
392 selected = !boot_editor->selected_device;
394 widget_select_add_option(select, -1, _("Specify paths/URLs manually"),
398 static bool path_on_device(struct blockdev_info *bd_info,
403 if (!bd_info->mountpoint)
406 len = strlen(bd_info->mountpoint);
407 if (strncmp(bd_info->mountpoint, path, len))
410 /* if the mountpoint doesn't have a trailing slash, ensure that
411 * the path starts with one (so we don't match a "/mnt/sda1/foo" path
412 * on a "/mnt/sda" mountpoint) */
413 return bd_info->mountpoint[len-1] == '/' || path[len] == '/';
417 static void boot_editor_find_device(struct boot_editor *boot_editor,
418 struct pb_boot_data *bd, const struct system_info *sysinfo)
420 struct blockdev_info *bd_info, *tmp;
423 if (!sysinfo || !sysinfo->n_blockdevs)
426 /* find the device for our boot image, by finding the longest
427 * matching blockdev's mountpoint */
428 for (len = 0, i = 0, bd_info = NULL; i < sysinfo->n_blockdevs; i++) {
429 tmp = sysinfo->blockdevs[i];
430 if (!path_on_device(tmp, bd->image))
432 if (strlen(tmp->mountpoint) <= len)
435 len = strlen(tmp->mountpoint);
441 /* ensure that other paths are on this device */
442 if (bd->initrd && !path_on_device(bd_info, bd->initrd))
445 if (bd->dtb && !path_on_device(bd_info, bd->dtb))
448 /* ok, we match; preselect the device option, and remove the common
450 boot_editor->selected_device = bd_info->name;
451 boot_editor->image += len;
453 if (boot_editor->initrd)
454 boot_editor->initrd += len;
455 if (boot_editor->dtb)
456 boot_editor->dtb += len;
459 static void boot_editor_setup_widgets(struct boot_editor *boot_editor,
460 const struct system_info *sysinfo)
462 struct nc_widgetset *set;
465 field_size = COLS - 1 - boot_editor->field_x;
467 boot_editor->widgetset = set = widgetset_create(boot_editor,
468 boot_editor->scr.main_ncw,
471 widgetset_set_widget_focus(boot_editor->widgetset,
472 boot_editor_widget_focus, boot_editor);
474 boot_editor->widgets.device_l = widget_new_label(set, 0, 0,
476 boot_editor->widgets.device_f = widget_new_select(set, 0, 0,
478 widget_select_on_change(boot_editor->widgets.device_f,
479 boot_editor_device_select_change, boot_editor);
481 boot_editor_populate_device_select(boot_editor, sysinfo);
483 boot_editor->widgets.image_l = widget_new_label(set, 0, 0,
485 boot_editor->widgets.image_f = widget_new_textbox(set, 0, 0,
486 field_size, boot_editor->image);
488 boot_editor->widgets.initrd_l = widget_new_label(set, 0, 0,
490 boot_editor->widgets.initrd_f = widget_new_textbox(set, 0, 0,
492 boot_editor->initrd);
494 boot_editor->widgets.dtb_l = widget_new_label(set, 0, 0,
496 boot_editor->widgets.dtb_f = widget_new_textbox(set, 0, 0,
497 field_size, boot_editor->dtb);
499 boot_editor->widgets.args_l = widget_new_label(set, 0, 0,
500 _("Boot arguments:"));
501 boot_editor->widgets.args_f = widget_new_textbox(set, 0, 0,
502 field_size, boot_editor->args);
504 boot_editor->widgets.ok_b = widget_new_button(set, 0, 0, 10,
505 _("OK"), ok_click, boot_editor);
506 boot_editor->widgets.help_b = widget_new_button(set, 0, 0, 10,
507 _("Help"), help_click, boot_editor);
508 boot_editor->widgets.cancel_b = widget_new_button(set, 0, 0, 10,
509 _("Cancel"), cancel_click, boot_editor);
512 void boot_editor_update(struct boot_editor *boot_editor,
513 const struct system_info *sysinfo)
517 if (boot_editor->cui->current != boot_editor_scr(boot_editor)) {
518 boot_editor->need_update = true;
522 widgetset_unpost(boot_editor->widgetset);
524 height = pad_height(sysinfo ? sysinfo->n_blockdevs : 0);
525 if (getmaxy(boot_editor->pad) < height) {
526 delwin(boot_editor->pad);
527 boot_editor->pad = newpad(height, COLS);
528 widgetset_set_windows(boot_editor->widgetset,
529 boot_editor->scr.main_ncw,
533 boot_editor_populate_device_select(boot_editor, sysinfo);
535 boot_editor_layout_widgets(boot_editor);
537 widgetset_post(boot_editor->widgetset);
539 pad_refresh(boot_editor);
542 struct boot_editor *boot_editor_init(struct cui *cui,
543 struct pmenu_item *item,
544 const struct system_info *sysinfo,
545 void (*on_exit)(struct cui *cui,
546 struct pmenu_item *item,
547 struct pb_boot_data *bd))
549 struct boot_editor *boot_editor;
551 boot_editor = talloc_zero(cui, struct boot_editor);
556 talloc_set_destructor(boot_editor, boot_editor_destructor);
557 boot_editor->cui = cui;
558 boot_editor->item = item;
559 boot_editor->on_exit = on_exit;
560 boot_editor->state = STATE_EDIT;
561 boot_editor->need_redraw = false;
562 boot_editor->need_update = false;
564 int ncols1 = strncols(_("Device tree:"));
565 int ncols2 = strncols(_("Boot arguments:"));
567 boot_editor->label_x = 1;
568 boot_editor->field_x = 2 + max(ncols1, ncols2);
570 nc_scr_init(&boot_editor->scr, pb_boot_editor_sig, 0,
571 cui, boot_editor_process_key,
572 boot_editor_post, boot_editor_unpost, boot_editor_resize);
574 boot_editor->scr.frame.ltitle = talloc_strdup(boot_editor,
575 _("Petitboot Option Editor"));
576 boot_editor->scr.frame.rtitle = NULL;
577 boot_editor->scr.frame.help = talloc_strdup(boot_editor,
578 _("tab=next, shift+tab=previous, x=exit, h=help"));
579 nc_scr_frame_draw(&boot_editor->scr);
582 struct pb_boot_data *bd = cod_from_item(item)->bd;
583 boot_editor->image = bd->image;
584 boot_editor->initrd = bd->initrd;
585 boot_editor->dtb = bd->dtb;
586 boot_editor->args = bd->args;
587 boot_editor_find_device(boot_editor, bd, sysinfo);
589 boot_editor->image = boot_editor->initrd =
590 boot_editor->dtb = boot_editor->args = "";
593 boot_editor->pad = newpad(
594 pad_height(sysinfo ? sysinfo->n_blockdevs : 0),
597 boot_editor_setup_widgets(boot_editor, sysinfo);
599 boot_editor_layout_widgets(boot_editor);
600 wrefresh(boot_editor->scr.main_ncw);