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);
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 if (boot_editor->need_redraw) {
115 redrawwin(scr->main_ncw);
116 boot_editor->need_redraw = false;
118 wrefresh(boot_editor->scr.main_ncw);
119 pad_refresh(boot_editor);
123 static int boot_editor_unpost(struct nc_scr *scr)
125 widgetset_unpost(boot_editor_from_scr(scr)->widgetset);
129 struct nc_scr *boot_editor_scr(struct boot_editor *boot_editor)
131 return &boot_editor->scr;
134 static void boot_editor_resize(struct nc_scr *scr)
136 /* FIXME: forms can't be resized, need to recreate here */
137 boot_editor_unpost(scr);
138 boot_editor_post(scr);
141 static char *conditional_prefix(struct pb_boot_data *ctx,
142 const char *prefix, const char *value)
146 if (!value || !*value)
152 else if (prefix[strlen(prefix)] != '/')
155 return talloc_asprintf(ctx, "%s%s%s", prefix, sep, value);
158 static struct pb_boot_data *boot_editor_prepare_data(
159 struct boot_editor *boot_editor)
161 struct pb_boot_data *bd;
165 bd = talloc(boot_editor, struct pb_boot_data);
170 idx = widget_select_get_value(boot_editor->widgets.device_f);
171 if (idx == -1 || (unsigned int)idx >
172 boot_editor->cui->sysinfo->n_blockdevs)
175 prefix = boot_editor->cui->sysinfo->blockdevs[idx]->mountpoint;
177 s = widget_textbox_get_value(boot_editor->widgets.image_f);
178 bd->image = conditional_prefix(bd, prefix, s);
184 s = widget_textbox_get_value(boot_editor->widgets.initrd_f);
185 bd->initrd = conditional_prefix(bd, prefix, s);
187 s = widget_textbox_get_value(boot_editor->widgets.dtb_f);
188 bd->dtb = conditional_prefix(bd, prefix, s);
190 s = widget_textbox_get_value(boot_editor->widgets.args_f);
191 bd->args = *s ? talloc_strdup(bd, s) : NULL;
197 * boot_editor_process_key - Process a user keystroke.
199 * Called from the cui via the scr:process_key method.
202 static void boot_editor_process_key(struct nc_scr *scr, int key)
204 struct boot_editor *boot_editor = boot_editor_from_scr(scr);
205 struct pmenu_item *item;
206 struct pb_boot_data *bd;
209 handled = widgetset_process_key(boot_editor->widgetset, key);
211 pad_refresh(boot_editor);
213 else if (key == 'x' || key == 27)
214 boot_editor->state = STATE_CANCEL;
217 boot_editor->state = STATE_HELP;
222 switch (boot_editor->state) {
224 item = boot_editor->item;
225 bd = boot_editor_prepare_data(boot_editor);
227 /* Incomplete entry */
228 boot_editor->state = STATE_EDIT;
233 boot_editor->on_exit(boot_editor->cui, item, bd);
236 boot_editor->state = STATE_EDIT;
237 boot_editor->need_redraw = true;
238 cui_show_help(boot_editor->cui, _("Boot Option Editor"),
239 &boot_editor_help_text);
247 * boot_editor_destructor - The talloc destructor for a boot_editor.
250 static int boot_editor_destructor(void *arg)
252 struct boot_editor *boot_editor = boot_editor_from_arg(arg);
253 boot_editor->scr.sig = pb_removed_sig;
254 if (boot_editor->pad)
255 delwin(boot_editor->pad);
259 static void ok_click(void *arg)
261 struct boot_editor *boot_editor = arg;
262 boot_editor->state = STATE_SAVE;
265 static void help_click(void *arg)
267 struct boot_editor *boot_editor = arg;
268 boot_editor->state = STATE_HELP;
271 static void cancel_click(void *arg)
273 struct boot_editor *boot_editor = arg;
274 boot_editor->state = STATE_CANCEL;
277 static int layout_pair(struct boot_editor *boot_editor, int y,
278 struct nc_widget_label *label,
279 struct nc_widget_textbox *field)
281 struct nc_widget *label_w = widget_label_base(label);
282 struct nc_widget *field_w = widget_textbox_base(field);
283 widget_move(label_w, y, boot_editor->label_x);
284 widget_move(field_w, y, boot_editor->field_x);
285 return max(widget_height(label_w), widget_height(field_w));
288 static int pad_height(int blockdevs_height)
290 return 10 + blockdevs_height;
293 static void boot_editor_layout_widgets(struct boot_editor *boot_editor)
295 struct nc_widget *wf, *wl;
298 wl = widget_label_base(boot_editor->widgets.device_l);
299 wf = widget_select_base(boot_editor->widgets.device_f);
300 widget_move(wl, y, boot_editor->label_x);
301 widget_move(wf, y, boot_editor->field_x);
303 y += widget_height(wf) + 1;
306 y += layout_pair(boot_editor, y, boot_editor->widgets.image_l,
307 boot_editor->widgets.image_f);
309 y += layout_pair(boot_editor, y, boot_editor->widgets.initrd_l,
310 boot_editor->widgets.initrd_f);
312 y += layout_pair(boot_editor, y, boot_editor->widgets.dtb_l,
313 boot_editor->widgets.dtb_f);
315 y += layout_pair(boot_editor, y, boot_editor->widgets.args_l,
316 boot_editor->widgets.args_f);
320 widget_move(widget_button_base(boot_editor->widgets.ok_b), y,
321 boot_editor->field_x);
322 widget_move(widget_button_base(boot_editor->widgets.help_b), y,
323 boot_editor->field_x + 14);
324 widget_move(widget_button_base(boot_editor->widgets.cancel_b), y,
325 boot_editor->field_x + 28);
328 static void boot_editor_widget_focus(struct nc_widget *widget, void *arg)
330 struct boot_editor *boot_editor = arg;
333 w_y = widget_y(widget) + widget_focus_y(widget);
334 s_max = getmaxy(boot_editor->scr.sub_ncw) - 1;
336 if (w_y < boot_editor->scroll_y)
337 boot_editor->scroll_y = w_y;
339 else if (w_y + boot_editor->scroll_y + 1 > s_max)
340 boot_editor->scroll_y = 1 + w_y - s_max;
345 pad_refresh(boot_editor);
348 static void boot_editor_device_select_change(void *arg, int idx)
350 struct boot_editor *boot_editor = arg;
352 boot_editor->selected_device = NULL;
354 boot_editor->selected_device =
355 boot_editor->cui->sysinfo->blockdevs[idx]->name;
358 static void boot_editor_populate_device_select(struct boot_editor *boot_editor,
359 const struct system_info *sysinfo)
361 struct nc_widget_select *select = boot_editor->widgets.device_f;
365 widget_select_drop_options(select);
367 for (i = 0; sysinfo && i < sysinfo->n_blockdevs; i++) {
368 struct blockdev_info *bd_info = sysinfo->blockdevs[i];
371 name = talloc_asprintf(boot_editor, "%s [%s]",
372 bd_info->name, bd_info->uuid);
373 selected = boot_editor->selected_device &&
374 !strcmp(bd_info->name,
375 boot_editor->selected_device);
377 widget_select_add_option(select, i, name, selected);
380 /* If we're editing an existing option, the paths will be fully-
381 * resolved. In this case, we want the manual device pre-selected.
382 * However, we only do this if the widget hasn't been manually
384 selected = !boot_editor->selected_device;
386 widget_select_add_option(select, -1, _("Specify paths/URLs manually"),
390 static bool path_on_device(struct blockdev_info *bd_info,
395 if (!bd_info->mountpoint)
398 len = strlen(bd_info->mountpoint);
399 if (strncmp(bd_info->mountpoint, path, len))
402 /* if the mountpoint doesn't have a trailing slash, ensure that
403 * the path starts with one (so we don't match a "/mnt/sda1/foo" path
404 * on a "/mnt/sda" mountpoint) */
405 return bd_info->mountpoint[len-1] == '/' || path[len] == '/';
409 static void boot_editor_find_device(struct boot_editor *boot_editor,
410 struct pb_boot_data *bd, const struct system_info *sysinfo)
412 struct blockdev_info *bd_info, *tmp;
415 if (!sysinfo || !sysinfo->n_blockdevs)
418 /* find the device for our boot image, by finding the longest
419 * matching blockdev's mountpoint */
420 for (len = 0, i = 0, bd_info = NULL; i < sysinfo->n_blockdevs; i++) {
421 tmp = sysinfo->blockdevs[i];
422 if (!path_on_device(tmp, bd->image))
424 if (strlen(tmp->mountpoint) <= len)
427 len = strlen(tmp->mountpoint);
433 /* ensure that other paths are on this device */
434 if (bd->initrd && !path_on_device(bd_info, bd->initrd))
437 if (bd->dtb && !path_on_device(bd_info, bd->dtb))
440 /* ok, we match; preselect the device option, and remove the common
442 boot_editor->selected_device = bd_info->name;
443 boot_editor->image += len;
445 if (boot_editor->initrd)
446 boot_editor->initrd += len;
447 if (boot_editor->dtb)
448 boot_editor->dtb += len;
451 static void boot_editor_setup_widgets(struct boot_editor *boot_editor,
452 const struct system_info *sysinfo)
454 struct nc_widgetset *set;
457 field_size = COLS - 1 - boot_editor->field_x;
459 boot_editor->widgetset = set = widgetset_create(boot_editor,
460 boot_editor->scr.main_ncw,
463 widgetset_set_widget_focus(boot_editor->widgetset,
464 boot_editor_widget_focus, boot_editor);
466 boot_editor->widgets.device_l = widget_new_label(set, 0, 0,
468 boot_editor->widgets.device_f = widget_new_select(set, 0, 0,
470 widget_select_on_change(boot_editor->widgets.device_f,
471 boot_editor_device_select_change, boot_editor);
473 boot_editor_populate_device_select(boot_editor, sysinfo);
475 boot_editor->widgets.image_l = widget_new_label(set, 0, 0,
477 boot_editor->widgets.image_f = widget_new_textbox(set, 0, 0,
478 field_size, boot_editor->image);
480 boot_editor->widgets.initrd_l = widget_new_label(set, 0, 0,
482 boot_editor->widgets.initrd_f = widget_new_textbox(set, 0, 0,
484 boot_editor->initrd);
486 boot_editor->widgets.dtb_l = widget_new_label(set, 0, 0,
488 boot_editor->widgets.dtb_f = widget_new_textbox(set, 0, 0,
489 field_size, boot_editor->dtb);
491 boot_editor->widgets.args_l = widget_new_label(set, 0, 0,
492 _("Boot arguments:"));
493 boot_editor->widgets.args_f = widget_new_textbox(set, 0, 0,
494 field_size, boot_editor->args);
496 boot_editor->widgets.ok_b = widget_new_button(set, 0, 0, 10,
497 _("OK"), ok_click, boot_editor);
498 boot_editor->widgets.help_b = widget_new_button(set, 0, 0, 10,
499 _("Help"), help_click, boot_editor);
500 boot_editor->widgets.cancel_b = widget_new_button(set, 0, 0, 10,
501 _("Cancel"), cancel_click, boot_editor);
504 void boot_editor_update(struct boot_editor *boot_editor,
505 const struct system_info *sysinfo)
509 widgetset_unpost(boot_editor->widgetset);
511 height = pad_height(sysinfo ? sysinfo->n_blockdevs : 0);
512 if (getmaxy(boot_editor->pad) < height) {
513 delwin(boot_editor->pad);
514 boot_editor->pad = newpad(height, COLS);
515 widgetset_set_windows(boot_editor->widgetset,
516 boot_editor->scr.main_ncw,
520 boot_editor_populate_device_select(boot_editor, sysinfo);
522 boot_editor_layout_widgets(boot_editor);
524 widgetset_post(boot_editor->widgetset);
526 pad_refresh(boot_editor);
529 struct boot_editor *boot_editor_init(struct cui *cui,
530 struct pmenu_item *item,
531 const struct system_info *sysinfo,
532 void (*on_exit)(struct cui *cui,
533 struct pmenu_item *item,
534 struct pb_boot_data *bd))
536 struct boot_editor *boot_editor;
538 boot_editor = talloc_zero(cui, struct boot_editor);
543 talloc_set_destructor(boot_editor, boot_editor_destructor);
544 boot_editor->cui = cui;
545 boot_editor->item = item;
546 boot_editor->on_exit = on_exit;
547 boot_editor->state = STATE_EDIT;
548 boot_editor->need_redraw = false;
550 int ncols1 = strncols(_("Device tree:"));
551 int ncols2 = strncols(_("Boot arguments:"));
553 boot_editor->label_x = 1;
554 boot_editor->field_x = 2 + max(ncols1, ncols2);
556 nc_scr_init(&boot_editor->scr, pb_boot_editor_sig, 0,
557 cui, boot_editor_process_key,
558 boot_editor_post, boot_editor_unpost, boot_editor_resize);
560 boot_editor->scr.frame.ltitle = talloc_strdup(boot_editor,
561 _("Petitboot Option Editor"));
562 boot_editor->scr.frame.rtitle = NULL;
563 boot_editor->scr.frame.help = talloc_strdup(boot_editor,
564 _("tab=next, shift+tab=previous, x=exit, h=help"));
565 nc_scr_frame_draw(&boot_editor->scr);
568 struct pb_boot_data *bd = cod_from_item(item)->bd;
569 boot_editor->image = bd->image;
570 boot_editor->initrd = bd->initrd;
571 boot_editor->dtb = bd->dtb;
572 boot_editor->args = bd->args;
573 boot_editor_find_device(boot_editor, bd, sysinfo);
575 boot_editor->image = boot_editor->initrd =
576 boot_editor->dtb = boot_editor->args = "";
579 boot_editor->pad = newpad(
580 pad_height(sysinfo ? sysinfo->n_blockdevs : 0),
583 boot_editor_setup_widgets(boot_editor, sysinfo);
585 boot_editor_layout_widgets(boot_editor);
586 wrefresh(boot_editor->scr.main_ncw);