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
25 #include <sys/ioctl.h>
28 #include "talloc/talloc.h"
29 #include "waiter/waiter.h"
30 #include "ui/common/discover-client.h"
34 static struct cui_opt_data *cod_from_item(struct pmenu_item *item)
40 * cui_abort - Signal the main cui program loop to exit.
42 * Sets cui.abort, which causes the cui_run() routine to return.
45 void cui_abort(struct cui *cui)
47 pb_log("%s: exiting\n", __func__);
52 * cui_resize - Signal the main cui program loop to resize
57 void cui_resize(struct cui *cui)
59 pb_log("%s: resizing\n", __func__);
64 * cui_on_exit - A generic main menu ESC callback.
67 void cui_on_exit(struct pmenu *menu)
69 cui_abort(cui_from_pmenu(menu));
73 * cui_run_cmd - A generic cb to run the supplied command.
76 int cui_run_cmd(struct pmenu_item *item)
79 struct cui *cui = cui_from_item(item);
80 const char *const *cmd_argv = item->data;
82 nc_scr_status_printf(cui->current, "Running %s...", cmd_argv[0]);
86 result = pb_run_cmd(cmd_argv);
89 redrawwin(cui->current->main_ncw);
92 pb_log("%s: failed: '%s'\n", __func__, cmd_argv[0]);
93 nc_scr_status_printf(cui->current, "Failed: %s", cmd_argv[0]);
100 * cui_run_kexec - A generic cb to run kexec.
103 static int cui_run_kexec(struct pmenu_item *item)
106 struct cui *cui = cui_from_item(item);
107 struct cui_opt_data *cod = cod_from_item(item);
109 assert(cui->current == &cui->main->scr);
110 assert(cui->on_kexec);
112 pb_log("%s: %s\n", __func__, cod->dev->name, cod->opt->name);
113 nc_scr_status_printf(cui->current, "Booting %s...", cod->opt->name);
117 result = cui->on_kexec(cui, cod);
120 redrawwin(cui->current->main_ncw);
124 mvaddstr(1, 0, "system is going down now...");
129 pb_log("%s: failed: %s\n", __func__, cod->kd->image);
130 nc_scr_status_printf(cui->current, "Failed: kexec %s", cod->kd->image);
136 * cui_ked_on_exit - The ked on_exit callback.
139 static void cui_ked_on_exit(struct ked *ked, enum ked_result ked_result,
140 struct pb_kexec_data *kd)
142 struct cui *cui = cui_from_arg(ked->scr.ui_ctx);
144 if (ked_result == ked_update || ked_result == ked_boot) {
145 struct pmenu_item *i = pmenu_find_selected(cui->main);
146 struct cui_opt_data *cod = cod_from_item(i);
151 talloc_free(cod->kd);
154 pb_log("%s: updating opt '%s'\n", __func__, cod->opt->name);
155 pb_log(" image '%s'\n", cod->kd->image);
156 pb_log(" initrd '%s'\n", cod->kd->initrd);
157 pb_log(" args '%s'\n", cod->kd->args);
160 cui_set_current(cui, &cui->main->scr);
164 if (ked_result == ked_boot) {
165 struct pmenu_item *i = pmenu_find_selected(cui->main);
170 int cui_ked_run(struct pmenu_item *item)
172 struct cui *cui = cui_from_item(item);
175 ked = ked_init(cui, cod_from_item(item)->kd, cui_ked_on_exit);
177 cui_set_current(cui, &ked->scr);
183 * cui_set_current - Set the currently active screen and redraw it.
186 struct nc_scr *cui_set_current(struct cui *cui, struct nc_scr *scr)
190 DBGS("%p -> %p\n", cui->current, scr);
192 assert(cui->current != scr);
198 cui->current->post(cui->current);
203 static int cui_process_key(void *arg)
205 struct cui *cui = cui_from_arg(arg);
207 assert(cui->current);
208 cui->current->process_key(cui->current);
212 static int cui_client_process_socket(void *arg)
214 struct discover_client *client = arg;
216 discover_client_process(client);
221 * cui_handle_resize - Handle the term resize.
224 static void cui_handle_resize(struct cui *cui)
228 if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
229 pb_log("%s: ioctl failed: %s\n", __func__, strerror(errno));
233 pb_log("%s: {%u,%u}\n", __func__, ws.ws_row, ws.ws_col);
235 wclear(cui->current->main_ncw);
236 resize_term(ws.ws_row, ws.ws_col);
237 cui->current->resize(cui->current);
239 /* For some reason this makes ncurses redraw the screen */
241 redrawwin(cui->current->main_ncw);
242 wrefresh(cui->current->main_ncw);
246 * cui_device_add - Client device_add callback.
248 * Creates menu_items for all the device boot_options and inserts those
249 * menu_items into the main menu. Redraws the main menu if it is active.
252 static int cui_device_add(struct device *dev, void *arg)
254 struct cui *cui = cui_from_arg(arg);
256 struct boot_option *opt;
257 unsigned int o_count; /* device opts */
258 unsigned int insert_pt;
261 pb_log("%s: %p %s\n", __func__, dev, dev->id);
263 selected = current_item(cui->main->ncm);
265 if (cui->current == &cui->main->scr)
266 cui->current->unpost(cui->current);
268 /* This disconnects items array from menu. */
270 result = set_menu_items(cui->main->ncm, NULL);
273 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
276 list_for_each_entry(&dev->boot_options, opt, list)
279 /* Insert new items at insert_pt. */
281 insert_pt = pmenu_grow(cui->main, o_count);
283 list_for_each_entry(&dev->boot_options, opt, list) {
284 struct pmenu_item *i;
285 struct cui_opt_data *cod;
289 /* Save the item in opt->ui_info for cui_device_remove() */
291 opt->ui_info = i = pmenu_item_alloc(cui->main);
293 name = talloc_asprintf(i, "%s: %s", opt->name,
296 description = talloc_asprintf(i,
297 " kexec: image='%s' initrd='%s' args='%s'",
298 opt->boot_image_file,
302 pmenu_item_setup(cui->main, i, insert_pt, name, description);
304 i->on_edit = cui_ked_run;
305 i->on_execute = cui_run_kexec;
306 i->data = cod = talloc(i, struct cui_opt_data);
310 cod->opt_hash = pb_opt_hash(dev, opt);
311 cod->kd = talloc(i, struct pb_kexec_data);
313 cod->kd->image = talloc_strdup(cod->kd, opt->boot_image_file);
314 cod->kd->initrd = talloc_strdup(cod->kd, opt->initrd_file);
315 cod->kd->args = talloc_strdup(cod->kd, opt->boot_args);
319 /* If this is the default_item select it. */
321 if (cod->opt_hash == cui->default_item)
324 pb_log("%s: adding opt '%s'\n", __func__, cod->opt->name);
325 pb_log(" image '%s'\n", cod->kd->image);
326 pb_log(" initrd '%s'\n", cod->kd->initrd);
327 pb_log(" args '%s'\n", cod->kd->args);
330 /* Re-attach the items array. */
332 result = set_menu_items(cui->main->ncm, cui->main->items);
335 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
338 pb_log("%s\n", __func__);
339 pmenu_dump_items(cui->main->items,
340 item_count(cui->main->ncm) + 1);
343 set_current_item(cui->main->ncm, selected);
345 if (cui->current == &cui->main->scr)
346 cui->current->post(cui->current);
352 * cui_device_remove - Client device remove callback.
354 * Removes all the menu_items for the device from the main menu and redraws the
355 * main menu if it is active.
358 static void cui_device_remove(struct device *dev, void *arg)
360 struct cui *cui = cui_from_arg(arg);
362 struct boot_option *opt;
364 pb_log("%s: %p %s\n", __func__, dev, dev->id);
366 if (cui->current == &cui->main->scr)
367 cui->current->unpost(cui->current);
369 /* This disconnects items array from menu. */
371 result = set_menu_items(cui->main->ncm, NULL);
374 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
376 list_for_each_entry(&dev->boot_options, opt, list) {
377 struct pmenu_item *i = pmenu_item_from_arg(opt->ui_info);
379 assert(pb_protocol_device_cmp(dev, cod_from_item(i)->dev));
380 pmenu_remove(cui->main, i);
383 /* Re-attach the items array. */
385 result = set_menu_items(cui->main->ncm, cui->main->items);
388 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
391 pb_log("%s\n", __func__);
392 pmenu_dump_items(cui->main->items,
393 item_count(cui->main->ncm) + 1);
396 if (cui->current == &cui->main->scr)
397 cui->current->post(cui->current);
401 * cui_client_process_socket - Process a socket event from the discover server.
404 static struct discover_client_ops cui_client_ops = {
405 .device_add = cui_device_add,
406 .device_remove = cui_device_remove,
410 * cui_init - Setup the cui instance.
411 * @platform_info: A value for the struct cui platform_info member.
413 * Returns a pointer to a struct cui on success, or NULL on error.
415 * Allocates the cui instance, sets up the client and stdin waiters, and
416 * sets up the ncurses menu screen.
419 struct cui *cui_init(void* platform_info,
420 int (*on_kexec)(struct cui *, struct cui_opt_data *))
423 struct discover_client *client;
426 cui = talloc_zero(NULL, struct cui);
429 pb_log("%s: alloc cui failed.\n", __func__);
430 fprintf(stderr, "%s: alloc cui failed.\n", __func__);
434 cui->c_sig = pb_cui_sig;
435 cui->platform_info = platform_info;
436 cui->on_kexec = on_kexec;
438 /* Loop here for scripts that just started the server. */
440 for (i = 10; i; i--) {
441 client = discover_client_init(&cui_client_ops, cui);
444 pb_log("%s: waiting for server %d\n", __func__, i);
449 pb_log("%s: discover_client_init failed.\n", __func__);
450 fprintf(stderr, "%s: error: discover_client_init failed.\n",
452 fprintf(stderr, "check that pb-discover, "
453 "the petitboot daemon is running.\n");
454 goto fail_client_init;
460 waiter_register(discover_client_get_fd(client), WAIT_IN,
461 cui_client_process_socket, client);
463 waiter_register(STDIN_FILENO, WAIT_IN, cui_process_key, cui);
474 * cui_run - The main cui program loop.
475 * @cui: The cui instance.
476 * @main: The menu to use as the main menu.
478 * Runs the cui engine. Does not return until indicated to do so by some
479 * user action, or an error occurs. Frees the cui object on return.
480 * Returns 0 on success (return to shell), -1 on error (should restart).
483 int cui_run(struct cui *cui, struct pmenu *main, unsigned int default_item)
488 cui->current = &cui->main->scr;
489 cui->default_item = default_item;
491 cui->current->post(cui->current);
494 int result = waiter_poll();
496 if (result < 0 && errno != EINTR) {
497 pb_log("%s: poll: %s\n", __func__, strerror(errno));
504 while (cui->resize) {
506 cui_handle_resize(cui);
512 return cui->abort ? 0 : -1;