]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/nc-cui.c
discover/powerpc: Fix sysparams base path
[petitboot] / ui / ncurses / nc-cui.c
1 /*
2  *  Copyright (C) 2009 Sony Computer Entertainment Inc.
3  *  Copyright 2009 Sony Corp.
4  *
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.
8  *
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.
13  *
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
17  */
18
19 #if defined(HAVE_CONFIG_H)
20 #include "config.h"
21 #endif
22
23 #include <assert.h>
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <locale.h>
28 #include <sys/ioctl.h>
29
30 #include "log/log.h"
31 #include "pb-protocol/pb-protocol.h"
32 #include "talloc/talloc.h"
33 #include "waiter/waiter.h"
34 #include "process/process.h"
35 #include "ui/common/discover-client.h"
36 #include "ui/common/ui-system.h"
37 #include "nc-cui.h"
38 #include "nc-boot-editor.h"
39 #include "nc-config.h"
40 #include "nc-sysinfo.h"
41 #include "nc-helpscreen.h"
42
43 static void cui_start(void)
44 {
45         initscr();                      /* Initialize ncurses. */
46         cbreak();                       /* Disable line buffering. */
47         noecho();                       /* Disable getch() echo. */
48         keypad(stdscr, TRUE);           /* Enable num keypad keys. */
49         nonl();                         /* Disable new-line translation. */
50         intrflush(stdscr, FALSE);       /* Disable interrupt flush. */
51         curs_set(0);                    /* Make cursor invisible */
52         nodelay(stdscr, TRUE);          /* Enable non-blocking getch() */
53
54         /* We may be operating with an incorrect $TERM type; in this case
55          * the keymappings will be slightly broken. We want at least
56          * backspace to work though, so we'll define both DEL and ^H to
57          * map to backspace */
58         define_key("\x7f", KEY_BACKSPACE);
59         define_key("\x08", KEY_BACKSPACE);
60
61         while (getch() != ERR)          /* flush stdin */
62                 (void)0;
63 }
64
65 static void cui_atexit(void)
66 {
67         clear();
68         refresh();
69         endwin();
70 }
71
72 /**
73  * cui_abort - Signal the main cui program loop to exit.
74  *
75  * Sets cui.abort, which causes the cui_run() routine to return.
76  */
77
78 void cui_abort(struct cui *cui)
79 {
80         pb_log("%s: exiting\n", __func__);
81         cui->abort = 1;
82 }
83
84 /**
85  * cui_resize - Signal the main cui program loop to resize
86  *
87  * Called at SIGWINCH.
88  */
89
90 void cui_resize(struct cui *cui)
91 {
92         pb_debug("%s: resizing\n", __func__);
93         cui->resize = 1;
94 }
95
96 /**
97  * cui_on_exit - A generic main menu exit callback.
98  */
99
100 void cui_on_exit(struct pmenu *menu)
101 {
102         cui_abort(cui_from_pmenu(menu));
103 }
104
105 /**
106  * cui_run_cmd - A generic cb to run the supplied command.
107  */
108
109 int cui_run_cmd(struct pmenu_item *item)
110 {
111         int result;
112         struct cui *cui = cui_from_item(item);
113         const char **cmd_argv = item->data;
114
115         nc_scr_status_printf(cui->current, "Running %s...", cmd_argv[0]);
116
117         def_prog_mode();
118
119         result = process_run_simple_argv(item, cmd_argv);
120
121         reset_prog_mode();
122         redrawwin(cui->current->main_ncw);
123
124         if (result) {
125                 pb_log("%s: failed: '%s'\n", __func__, cmd_argv[0]);
126                 nc_scr_status_printf(cui->current, "Failed: %s", cmd_argv[0]);
127         }
128
129         return result;
130 }
131
132 /**
133  * cui_boot - A generic cb to run kexec.
134  */
135
136 static int cui_boot(struct pmenu_item *item)
137 {
138         int result;
139         struct cui *cui = cui_from_item(item);
140         struct cui_opt_data *cod = cod_from_item(item);
141
142         assert(cui->current == &cui->main->scr);
143
144         pb_debug("%s: %s\n", __func__, cod->name);
145
146         nc_scr_status_printf(cui->current, "Booting %s...", cod->name);
147
148         result = discover_client_boot(cui->client, NULL, cod->opt, cod->bd);
149
150         if (result) {
151                 nc_scr_status_printf(cui->current,
152                                 "Failed: boot %s", cod->bd->image);
153         }
154
155         return 0;
156 }
157
158 static void cui_boot_editor_on_exit(struct cui *cui,
159                 struct pmenu_item *item,
160                 struct pb_boot_data *bd)
161 {
162         struct pmenu *menu = cui->main;
163         struct cui_opt_data *cod;
164
165         /* Was the edit cancelled? */
166         if (!bd) {
167                 cui_set_current(cui, &cui->main->scr);
168                 talloc_free(cui->boot_editor);
169                 cui->boot_editor = NULL;
170                 return;
171         }
172
173         /* Is this was a new item, we'll need to update the menu */
174         if (!item) {
175                 int insert_pt;
176
177                 /* Detach the items array. */
178                 set_menu_items(menu->ncm, NULL);
179
180                 /* Insert new item at insert_pt. */
181                 insert_pt = pmenu_grow(menu, 1);
182                 item = pmenu_item_alloc(menu);
183                 item->on_edit = cui_item_edit;
184                 item->on_execute = cui_boot;
185                 item->data = cod = talloc_zero(item, struct cui_opt_data);
186
187                 cod->name = talloc_asprintf(cod, "User item %u:", insert_pt);
188                 pmenu_item_setup(menu, item, insert_pt,
189                                 talloc_strdup(item, cod->name));
190
191                 /* Re-attach the items array. */
192                 set_menu_items(menu->ncm, menu->items);
193                 nc_scr_post(&menu->scr);
194         } else {
195                 cod = item->data;
196         }
197
198         cod->bd = talloc_steal(cod, bd);
199
200         set_current_item(item->pmenu->ncm, item->nci);
201         cui_set_current(cui, &cui->main->scr);
202         talloc_free(cui->boot_editor);
203         cui->boot_editor = NULL;
204 }
205
206 void cui_item_edit(struct pmenu_item *item)
207 {
208         struct cui *cui = cui_from_item(item);
209         cui->boot_editor = boot_editor_init(cui, item, cui->sysinfo,
210                                         cui_boot_editor_on_exit);
211         cui_set_current(cui, boot_editor_scr(cui->boot_editor));
212 }
213
214 void cui_item_new(struct pmenu *menu)
215 {
216         struct cui *cui = cui_from_pmenu(menu);
217         cui->boot_editor = boot_editor_init(cui, NULL, cui->sysinfo,
218                                         cui_boot_editor_on_exit);
219         cui_set_current(cui, boot_editor_scr(cui->boot_editor));
220 }
221
222 static void cui_sysinfo_exit(struct cui *cui)
223 {
224         cui_set_current(cui, &cui->main->scr);
225         talloc_free(cui->sysinfo_screen);
226         cui->sysinfo_screen = NULL;
227 }
228
229 void cui_show_sysinfo(struct cui *cui)
230 {
231         cui->sysinfo_screen = sysinfo_screen_init(cui, cui->sysinfo,
232                         cui_sysinfo_exit);
233         cui_set_current(cui, sysinfo_screen_scr(cui->sysinfo_screen));
234 }
235
236 static void cui_config_exit(struct cui *cui)
237 {
238         cui_set_current(cui, &cui->main->scr);
239         talloc_free(cui->config_screen);
240         cui->config_screen = NULL;
241 }
242
243 void cui_show_config(struct cui *cui)
244 {
245         cui->config_screen = config_screen_init(cui, cui->config,
246                         cui->sysinfo, cui_config_exit);
247         cui_set_current(cui, config_screen_scr(cui->config_screen));
248 }
249
250 static void cui_help_exit(struct cui *cui)
251 {
252         cui_set_current(cui, help_screen_return_scr(cui->help_screen));
253         talloc_free(cui->help_screen);
254         cui->help_screen = NULL;
255 }
256
257 void cui_show_help(struct cui *cui, const char *title, const char *text)
258 {
259         if (!cui->current)
260                 return;
261
262         if (cui->help_screen)
263                 return;
264
265         cui->help_screen = help_screen_init(cui, cui->current,
266                         title, text, cui_help_exit);
267
268         if (cui->help_screen)
269                 cui_set_current(cui, help_screen_scr(cui->help_screen));
270 }
271
272 /**
273  * cui_set_current - Set the currently active screen and redraw it.
274  */
275
276 struct nc_scr *cui_set_current(struct cui *cui, struct nc_scr *scr)
277 {
278         struct nc_scr *old;
279
280         DBGS("%p -> %p\n", cui->current, scr);
281
282         assert(cui->current != scr);
283
284         old = cui->current;
285         nc_scr_unpost(old);
286
287         cui->current = scr;
288
289         nc_scr_post(cui->current);
290
291         return old;
292 }
293
294 static bool process_global_keys(struct cui *cui, int key)
295 {
296         switch (key) {
297         case 0xc:
298                 if (cui->current && cui->current->main_ncw)
299                         wrefresh(curscr);
300                 return true;
301         }
302         return false;
303 }
304
305 /**
306  * cui_process_key - Process input on stdin.
307  */
308
309 static int cui_process_key(void *arg)
310 {
311         struct cui *cui = cui_from_arg(arg);
312
313         assert(cui->current);
314
315         if (!cui->has_input)
316                 discover_client_cancel_default(cui->client);
317         cui->has_input = true;
318
319         for (;;) {
320                 int c = getch();
321
322                 pb_debug("%s: got key %d\n", __func__, c);
323
324                 if (c == ERR)
325                         break;
326
327                 if (process_global_keys(cui, c))
328                         continue;
329
330                 cui->current->process_key(cui->current, c);
331         }
332
333         return 0;
334 }
335
336 /**
337  * cui_process_js - Process joystick events.
338  */
339
340 static int cui_process_js(void *arg)
341 {
342         struct cui *cui = cui_from_arg(arg);
343         int c;
344
345         c = pjs_process_event(cui->pjs);
346
347         if (c) {
348                 ungetch(c);
349                 cui_process_key(arg);
350         }
351
352         return 0;
353 }
354
355 /**
356  * cui_handle_resize - Handle the term resize.
357  */
358
359 static void cui_handle_resize(struct cui *cui)
360 {
361         struct winsize ws;
362
363         if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
364                 pb_log("%s: ioctl failed: %s\n", __func__, strerror(errno));
365                 return;
366         }
367
368         pb_debug("%s: {%u,%u}\n", __func__, ws.ws_row, ws.ws_col);
369
370         wclear(cui->current->main_ncw);
371         resize_term(ws.ws_row, ws.ws_col);
372         cui->current->resize(cui->current);
373
374         /* For some reason this makes ncurses redraw the screen */
375         getch();
376         redrawwin(cui->current->main_ncw);
377         wrefresh(cui->current->main_ncw);
378 }
379
380 /**
381  * cui_device_add - Client device_add callback.
382  *
383  * Creates menu_items for all the device boot_options and inserts those
384  * menu_items into the main menu.  Redraws the main menu if it is active.
385  */
386
387 static int cui_boot_option_add(struct device *dev, struct boot_option *opt,
388                 void *arg)
389 {
390         struct cui *cui = cui_from_arg(arg);
391         struct cui_opt_data *cod;
392         unsigned int insert_pt;
393         int result, rows, cols;
394         struct pmenu_item *i;
395         ITEM *selected;
396
397         pb_debug("%s: %p %s\n", __func__, opt, opt->id);
398
399         selected = current_item(cui->main->ncm);
400         menu_format(cui->main->ncm, &rows, &cols);
401
402         if (cui->current == &cui->main->scr)
403                 nc_scr_unpost(cui->current);
404
405         /* This disconnects items array from menu. */
406
407         result = set_menu_items(cui->main->ncm, NULL);
408
409         if (result)
410                 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
411
412         /* Insert new items at insert_pt. */
413         insert_pt = pmenu_grow(cui->main, 1);
414
415         /* Save the item in opt->ui_info for cui_device_remove() */
416
417         opt->ui_info = i = pmenu_item_alloc(cui->main);
418
419         i->on_edit = cui_item_edit;
420         i->on_execute = cui_boot;
421         i->data = cod = talloc(i, struct cui_opt_data);
422
423         cod->opt = opt;
424         cod->dev = dev;
425         cod->opt_hash = pb_opt_hash(dev, opt);
426         cod->name = opt->name;
427         cod->bd = talloc(i, struct pb_boot_data);
428
429         cod->bd->image = talloc_strdup(cod->bd, opt->boot_image_file);
430         cod->bd->initrd = talloc_strdup(cod->bd, opt->initrd_file);
431         cod->bd->dtb = talloc_strdup(cod->bd, opt->dtb_file);
432         cod->bd->args = talloc_strdup(cod->bd, opt->boot_args);
433
434         pmenu_item_setup(cui->main, i, insert_pt, cod->name);
435
436         pb_log("%s: adding opt '%s'\n", __func__, cod->name);
437         pb_log("   image  '%s'\n", cod->bd->image);
438         pb_log("   initrd '%s'\n", cod->bd->initrd);
439         pb_log("   args   '%s'\n", cod->bd->args);
440
441         /* Re-attach the items array. */
442         result = set_menu_items(cui->main->ncm, cui->main->items);
443
444         if (result)
445                 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
446
447         if (0) {
448                 pb_log("%s\n", __func__);
449                 pmenu_dump_items(cui->main->items,
450                         item_count(cui->main->ncm) + 1);
451         }
452
453         if (!item_visible(selected)) {
454                 int idx, top;
455
456                 top = top_row(cui->main->ncm);
457                 idx = item_index(selected);
458
459                 /* If our index is above the current top row, align
460                  * us to the new top. Otherwise, align us to the new
461                  * bottom */
462                 top = top < idx ? idx - rows : idx;
463
464                 set_top_row(cui->main->ncm, top);
465                 set_current_item(cui->main->ncm, selected);
466         }
467
468         if (cui->current == &cui->main->scr)
469                 nc_scr_post(cui->current);
470
471         return 0;
472 }
473
474 /**
475  * cui_device_remove - Client device remove callback.
476  *
477  * Removes all the menu_items for the device from the main menu and redraws the
478  * main menu if it is active.
479  */
480
481 static void cui_device_remove(struct device *dev, void *arg)
482 {
483         struct cui *cui = cui_from_arg(arg);
484         int result;
485         struct boot_option *opt;
486
487         pb_log("%s: %p %s\n", __func__, dev, dev->id);
488
489         if (cui->current == &cui->main->scr)
490                 nc_scr_unpost(cui->current);
491
492         /* This disconnects items array from menu. */
493
494         result = set_menu_items(cui->main->ncm, NULL);
495
496         if (result)
497                 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
498
499         list_for_each_entry(&dev->boot_options, opt, list) {
500                 struct pmenu_item *i = pmenu_item_from_arg(opt->ui_info);
501
502                 assert(pb_protocol_device_cmp(dev, cod_from_item(i)->dev));
503                 pmenu_remove(cui->main, i);
504         }
505
506         /* Re-attach the items array. */
507
508         result = set_menu_items(cui->main->ncm, cui->main->items);
509
510         if (result)
511                 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
512
513         if (0) {
514                 pb_log("%s\n", __func__);
515                 pmenu_dump_items(cui->main->items,
516                         item_count(cui->main->ncm) + 1);
517         }
518
519         if (cui->current == &cui->main->scr)
520                 nc_scr_post(cui->current);
521 }
522
523 static void cui_update_status(struct boot_status *status, void *arg)
524 {
525         struct cui *cui = cui_from_arg(arg);
526
527         nc_scr_status_printf(cui->current,
528                         "%s: %s",
529                         status->type == BOOT_STATUS_ERROR ? "Error" : "Info",
530                         status->message);
531
532 }
533
534 static void cui_update_mm_title(struct cui *cui)
535 {
536         struct nc_frame *frame = &cui->main->scr.frame;
537
538         talloc_free(frame->rtitle);
539
540         frame->rtitle = talloc_strdup(cui->main, cui->sysinfo->type);
541         if (cui->sysinfo->identifier)
542                 frame->rtitle = talloc_asprintf_append(frame->rtitle,
543                                 " %s", cui->sysinfo->identifier);
544
545         if (cui->current == &cui->main->scr)
546                 nc_scr_post(cui->current);
547 }
548
549 static void cui_update_sysinfo(struct system_info *sysinfo, void *arg)
550 {
551         struct cui *cui = cui_from_arg(arg);
552         cui->sysinfo = talloc_steal(cui, sysinfo);
553
554         /* if we're currently displaying the system info screen, inform it
555          * of the updated information. */
556         if (cui->sysinfo_screen)
557                 sysinfo_screen_update(cui->sysinfo_screen, sysinfo);
558
559         /* ... and do the same with the config screen... */
560         if (cui->config_screen)
561                 config_screen_update(cui->config_screen, cui->config, sysinfo);
562
563         /* ... and the boot editor. */
564         if (cui->boot_editor)
565                 boot_editor_update(cui->boot_editor, sysinfo);
566
567         cui_update_mm_title(cui);
568 }
569
570 static void cui_update_config(struct config *config, void *arg)
571 {
572         struct cui *cui = cui_from_arg(arg);
573         cui->config = talloc_steal(cui, config);
574
575         if (cui->config_screen)
576                 config_screen_update(cui->config_screen, config, cui->sysinfo);
577 }
578
579 int cui_send_config(struct cui *cui, struct config *config)
580 {
581         return discover_client_send_config(cui->client, config);
582 }
583
584 static struct discover_client_ops cui_client_ops = {
585         .device_add = NULL,
586         .boot_option_add = cui_boot_option_add,
587         .device_remove = cui_device_remove,
588         .update_status = cui_update_status,
589         .update_sysinfo = cui_update_sysinfo,
590         .update_config = cui_update_config,
591 };
592
593 /**
594  * cui_init - Setup the cui instance.
595  * @platform_info: A value for the struct cui platform_info member.
596  *
597  * Returns a pointer to a struct cui on success, or NULL on error.
598  *
599  * Allocates the cui instance, sets up the client and stdin waiters, and
600  * sets up the ncurses menu screen.
601  */
602
603 struct cui *cui_init(void* platform_info,
604         int (*js_map)(const struct js_event *e), int start_deamon)
605 {
606         struct cui *cui;
607         unsigned int i;
608
609         cui = talloc_zero(NULL, struct cui);
610
611         if (!cui) {
612                 pb_log("%s: alloc cui failed.\n", __func__);
613                 fprintf(stderr, "%s: alloc cui failed.\n", __func__);
614                 goto fail_alloc;
615         }
616
617         cui->c_sig = pb_cui_sig;
618         cui->platform_info = platform_info;
619         cui->waitset = waitset_create(cui);
620
621         process_init(cui, cui->waitset, false);
622
623         setlocale(LC_ALL, "");
624
625         /* Loop here for scripts that just started the server. */
626
627 retry_start:
628         for (i = start_deamon ? 2 : 10; i; i--) {
629                 cui->client = discover_client_init(cui->waitset,
630                                 &cui_client_ops, cui);
631                 if (cui->client || !i)
632                         break;
633                 pb_log("%s: waiting for server %d\n", __func__, i);
634                 sleep(1);
635         }
636
637         if (!cui->client && start_deamon) {
638                 int result;
639
640                 start_deamon = 0;
641
642                 result = pb_start_daemon(cui);
643
644                 if (!result)
645                         goto retry_start;
646
647                 pb_log("%s: discover_client_init failed.\n", __func__);
648                 fprintf(stderr, "%s: error: discover_client_init failed.\n",
649                         __func__);
650                 fprintf(stderr, "could not start pb-discover, the petitboot "
651                         "daemon.\n");
652                 goto fail_client_init;
653         }
654
655         if (!cui->client) {
656                 pb_log("%s: discover_client_init failed.\n", __func__);
657                 fprintf(stderr, "%s: error: discover_client_init failed.\n",
658                         __func__);
659                 fprintf(stderr, "check that pb-discover, "
660                         "the petitboot daemon is running.\n");
661                 goto fail_client_init;
662         }
663
664         atexit(cui_atexit);
665         cui_start();
666
667         waiter_register_io(cui->waitset, STDIN_FILENO, WAIT_IN,
668                         cui_process_key, cui);
669
670         if (js_map) {
671
672                 cui->pjs = pjs_init(cui, js_map);
673
674                 if (cui->pjs)
675                         waiter_register_io(cui->waitset, pjs_get_fd(cui->pjs),
676                                         WAIT_IN, cui_process_js, cui);
677         }
678
679         return cui;
680
681 fail_client_init:
682         talloc_free(cui);
683 fail_alloc:
684         return NULL;
685 }
686
687 /**
688  * cui_run - The main cui program loop.
689  * @cui: The cui instance.
690  * @main: The menu to use as the main menu.
691  *
692  * Runs the cui engine.  Does not return until indicated to do so by some
693  * user action, or an error occurs.  Frees the cui object on return.
694  * Returns 0 on success (return to shell), -1 on error (should restart).
695  */
696
697 int cui_run(struct cui *cui, struct pmenu *main, unsigned int default_item)
698 {
699         assert(main);
700
701         cui->main = main;
702         cui->current = &cui->main->scr;
703         cui->default_item = default_item;
704
705         nc_scr_post(cui->current);
706
707         while (1) {
708                 int result = waiter_poll(cui->waitset);
709
710                 if (result < 0) {
711                         pb_log("%s: poll: %s\n", __func__, strerror(errno));
712                         break;
713                 }
714
715                 if (cui->abort)
716                         break;
717
718                 while (cui->resize) {
719                         cui->resize = 0;
720                         cui_handle_resize(cui);
721                 }
722         }
723
724         cui_atexit();
725
726         return cui->abort ? 0 : -1;
727 }