Add PS3 ncurses CUI program
authorGeoff Levand <geoffrey.levand@am.sony.com>
Sun, 12 Apr 2009 15:12:00 +0000 (15:12 +0000)
committerJeremy Kerr <jk@ozlabs.org>
Tue, 30 Jun 2009 07:29:27 +0000 (15:29 +0800)
Add a PS3 ncurses CUI program, and the configure option --enable-ps3
to control its build.  The default is --enable-ps3=yes.

Signed-off-by: Geoff Levand <geoffrey.levand@am.sony.com>
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
Makefile.in
configure.ac
rules.mk
ui/ncurses/ps3-cui.c [new file with mode: 0644]

index e68f80e370c26f5ce0a7bc33b7f246724d8899e2..01771dbf5396c5c7bb24fc652253aa6ce6fc8e5d 100644 (file)
@@ -16,6 +16,9 @@ LIBTWIN = @LIBTWIN@
 twin_CFLAGS = @twin_CFLAGS@
 twin_LDFLAGS = @twin_LIBS@
 
+# build target
+ENABLE_PS3 = @ENABLE_PS3@
+
 # other programs
 INSTALL = @INSTALL@
 SHELL = @SHELL@
index 524cbc574adaf87769d283bbad8b5b4dc3a8d43f..cb43f8c804308b00e290e184ab877e7a73ca5743 100644 (file)
@@ -29,6 +29,16 @@ fi
 AC_PROG_CC
 AC_PROG_INSTALL
 
+AC_ARG_ENABLE([ps3],
+       [AS_HELP_STRING([--enable-ps3],
+               [build support for the PS3 game console])],
+       [],
+       [enable_ps3=check])
+
+AS_IF([test "x$enable_ps3" != xno],
+       [AC_SUBST([ENABLE_PS3], ["y"])],
+       [AC_SUBST([ENABLE_PS3], ["n"])])
+
 AC_ARG_WITH([twin],
        [AS_HELP_STRING([--with-twin],
                [build GUI versions using the twin window system])],
index aff8858a7065ba1d330f8f87ef78a08efcc344aa..e7433389b0214e6b90ca106c7bf3742ffbba2356 100644 (file)
--- a/rules.mk
+++ b/rules.mk
@@ -19,7 +19,7 @@ parser_test = test/parser-test
 # install targets and components
 daemons = $(pb_discover)
 parsers = event kboot yaboot
-uis = $(pb_test)
+uis = $(pb_cui) $(pb_test)
 tests = $(parser_test)
 utils = $(pb_event)
 
@@ -68,11 +68,12 @@ client_objs = $(lib_objs) $(ui_common_objs)
 all: $(uis) $(daemons) $(utils)
 
 # ncurses cui
-pb_cui_objs = $(client_objs) $(ncurses_objs) ui/ncurses/ps3-cui.o \
-       ui/common/ps3.o
-$(pb_cui_objs): $(makefiles)
+pb_cui_objs-$(ENABLE_PS3) += ui/ncurses/ps3-cui.o ui/common/ps3.o
+pb_cui_ldflags-$(ENABLE_PS3) += -lps3-utils
 
-$(pb_cui): LDFLAGS += -lps3-utils -lmenu -lform -lncurses
+pb_cui_objs = $(client_objs) $(ncurses_objs) $(pb_cui_objs-y)
+$(pb_cui_objs): $(makefiles)
+$(pb_cui): LDFLAGS += $(pb_cui_ldflags-y) -lmenu -lform -lncurses
 
 $(pb_cui): $(pb_cui_objs)
        $(LINK.o) -o $@ $^
diff --git a/ui/ncurses/ps3-cui.c b/ui/ncurses/ps3-cui.c
new file mode 100644 (file)
index 0000000..f166c88
--- /dev/null
@@ -0,0 +1,539 @@
+/*
+ * Petitboot cui bootloader for the PS3 game console
+ *
+ *  Copyright (C) 2009 Sony Computer Entertainment Inc.
+ *  Copyright 2009 Sony Corp.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; version 2 of the License.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+/*
+ * TODO
+ * removable media event
+ * resize after video mode change
+ * ncurses mouse support
+ * timeout
+ */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <getopt.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "log/log.h"
+#include "talloc/talloc.h"
+#include "waiter/waiter.h"
+#include "ui/common/discover-client.h"
+#include "ui/common/ps3.h"
+#include "nc-cui.h"
+
+static void print_version(void)
+{
+       printf("pb-cui (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
+}
+
+static void print_usage(void)
+{
+       print_version();
+       printf(
+"Usage: pb-cui [-h, --help] [-l, --log log-file] [-V, --version]\n");
+}
+
+/**
+ * enum opt_value - Tri-state options variables.
+ */
+
+enum opt_value {opt_undef = 0, opt_yes, opt_no};
+
+/**
+ * struct opts - Values from command line options.
+ */
+
+struct opts {
+       enum opt_value show_help;
+       const char *log_file;
+       enum opt_value show_version;
+};
+
+/**
+ * opts_parse - Parse the command line options.
+ */
+
+static int opts_parse(struct opts *opts, int argc, char *argv[])
+{
+       static const struct option long_options[] = {
+               {"help",    no_argument,       NULL, 'h'},
+               {"log",     required_argument, NULL, 'l'},
+               {"version", no_argument,       NULL, 'V'},
+               { NULL,     0,                 NULL, 0},
+       };
+       static const char short_options[] = "hl:V";
+       static const struct opts default_values = {
+               .log_file = "pb-cui.log",
+       };
+
+       *opts = default_values;
+
+       while (1) {
+               int c = getopt_long(argc, argv, short_options, long_options,
+                       NULL);
+
+               if (c == EOF)
+                       break;
+
+               switch (c) {
+               case 'h':
+                       opts->show_help = opt_yes;
+                       break;
+               case 'l':
+                       opts->log_file = optarg;
+                       break;
+               case 'V':
+                       opts->show_version = opt_yes;
+                       break;
+               default:
+                       opts->show_help = opt_yes;
+                       return -1;
+               }
+       }
+
+       return 0;
+}
+
+/**
+ * struct ps3_cui - Main cui program instance.
+ * @mm: Main menu.
+ * @svm: Set video mode menu.
+ */
+
+struct ps3_cui {
+       struct pmenu *mm;
+       struct pmenu *svm;
+       struct cui *cui;
+       struct ps3_flash_values values;
+       int dirty_values;
+};
+
+static struct ps3_cui *ps3_from_cui(struct cui *cui)
+{
+       struct ps3_cui *ps3;
+
+       assert(cui->c_sig == pb_cui_sig);
+       ps3 = cui->platform_info;
+       assert(ps3->cui->c_sig == pb_cui_sig);
+       return ps3;
+}
+
+static struct ps3_cui *ps3_from_item(struct pmenu_item *item)
+{
+       return ps3_from_cui(cui_from_item(item));
+}
+
+/**
+ * ps3_set_mode - Set video mode helper.
+ *
+ * Runs ps3_set_video_mode().
+ */
+
+static void ps3_set_mode(struct ps3_cui *ps3, unsigned int mode)
+{
+       int result;
+
+       if (ps3->values.video_mode == (uint16_t)mode)
+               return;
+
+       ps3->values.video_mode = (uint16_t)mode;
+       ps3->dirty_values = 1;
+
+       result = ps3_set_video_mode(mode);
+
+       if (result)
+               nc_scr_status_printf(ps3->cui->current,
+                       "Failed: set_video_mode(%u)", mode);
+}
+
+/**
+ * ps3_svm_cb - The set video mode callback.
+ */
+
+static int ps3_svm_cb(struct pmenu_item *item)
+{
+       ps3_set_mode(ps3_from_item(item), (unsigned int)item->data);
+       return 0;
+}
+
+/**
+ * ps3_kexec_cb - The kexec callback.
+ *
+ * Writes config data to PS3 flash then calls pb_run_kexec().
+ */
+
+static int ps3_kexec_cb(struct cui *cui, struct cui_opt_data *cod)
+{
+       struct ps3_cui *ps3 = ps3_from_cui(cui);
+
+       pb_log("%s: %s:%s\n", __func__, cod->dev->name, cod->opt->name);
+
+       assert(ps3->cui->current == &ps3->cui->main->scr);
+
+       if (cui->default_item != cod->opt_hash || ps3->dirty_values) {
+               ps3->values.default_item = cod->opt_hash;
+               ps3_flash_set_values(&ps3->values);
+       }
+
+       return pb_run_kexec(cod->kd);
+}
+
+/**
+ * ps3_mm_to_svm_cb - Callback to switch to the set video mode menu.
+ */
+
+static int ps3_mm_to_svm_cb(struct pmenu_item *item)
+{
+       struct ps3_cui *ps3 = ps3_from_item(item);
+       struct nc_scr *old;
+
+       old = cui_set_current(ps3->cui, &ps3->svm->scr);
+       assert(old == &ps3->mm->scr);
+
+       return 0;
+}
+
+/**
+ * ps3_svm_to_mm_cb - Callback to switch back to the main menu.
+ */
+
+static int ps3_svm_to_mm_cb(struct pmenu_item *item)
+{
+       struct ps3_cui *ps3 = ps3_from_item(item);
+       struct nc_scr *old;
+
+       old = cui_set_current(ps3->cui, &ps3->mm->scr);
+       assert(old == &ps3->svm->scr);
+
+       return 0;
+}
+
+/**
+ * ps3_svm_to_mm_helper - The svm ESC callback.
+ */
+
+static void ps3_svm_to_mm_helper(struct pmenu *menu)
+{
+       ps3_svm_to_mm_cb(pmenu_find_selected(menu));
+}
+
+/**
+ * ps3_hot_key - PS3 specific hot keys.
+ *
+ * '@' = Set video mode to auto (mode 0)
+ * '$' = Set video mode to safe (480i)
+ * '+' = Cycles through a set of common video modes.
+ * '-' = Cycles through a set of common video modes in reverse.
+ */
+
+static int ps3_hot_key(struct pmenu __attribute__((unused)) *menu,
+       struct pmenu_item *item, int c)
+{
+       static const unsigned int modes[] = {0, 1, 6, 3, 11, 12};
+       static const unsigned int *const end = modes
+               + sizeof(modes) / sizeof(modes[0]) - 1;
+       static const unsigned int *p = modes;
+
+       switch (c) {
+       default:
+               /* DBGS("%d (%o)\n", c, c); */
+               break;
+       case '@':
+               p = modes + 0;
+               ps3_set_mode(ps3_from_item(item), *p);
+               break;
+       case '$':
+               p = modes + 1;
+               ps3_set_mode(ps3_from_item(item), *p);
+               break;
+       case '+':
+               p = (p < end) ? p + 1 : modes;
+               ps3_set_mode(ps3_from_item(item), *p);
+               break;
+       case '-':
+               p = (p > modes) ? p - 1 : end;
+               ps3_set_mode(ps3_from_item(item), *p);
+               break;
+       }
+
+       return c;
+}
+
+/**
+ * ps3_mm_init - Setup the main menu instance.
+ */
+
+static struct pmenu *ps3_mm_init(struct ps3_cui *ps3_cui)
+{
+       int result;
+       struct pmenu *m;
+       struct pmenu_item *i;
+       static const char *const bgo[] =
+               {"/usr/sbin/ps3-boot-game-os-NOT", NULL};
+
+       m = pmenu_init(ps3_cui->cui, 3, cui_on_exit);
+
+       if (!m) {
+               pb_log("%s: failed\n", __func__);
+               return NULL;
+       }
+
+       m->hot_key = ps3_hot_key;
+       m->scr.frame.title = talloc_strdup(m, "Petitboot PS3");
+       m->scr.frame.help = talloc_strdup(m,
+               "ESC=exit, Enter=accept, E,e=edit");
+       m->scr.frame.status = talloc_strdup(m, "Welcome to Petitboot");
+
+       i = pmenu_item_init(m, 0, "Boot GameOS",
+               "Reboot the PS3 into the GameOS");
+       i->on_execute = cui_run_cmd;
+       i->data = (void *)bgo;
+
+       i = pmenu_item_init(m, 1, "Set Video Mode",
+               "Display a video mode selection menu");
+       i->on_execute = ps3_mm_to_svm_cb;
+
+       i = pmenu_item_init(m, 2, "Exit to Shell",
+               "Exit petitboot and return to a shell prompt");
+       i->on_execute = pmenu_exit_cb;
+
+       result = pmenu_setup(m);
+
+       if (result) {
+               pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
+                       strerror(errno));
+               goto fail_setup;
+       }
+
+       menu_opts_off(m->ncm, O_SHOWDESC);
+       set_menu_mark(m->ncm, " *");
+       set_current_item(m->ncm, i->nci);
+
+       return m;
+
+fail_setup:
+       talloc_free(m);
+       return NULL;
+}
+
+/**
+ * ps3_svm_init - Setup the set video mode menu instance.
+ */
+
+static struct pmenu *ps3_svm_init(struct ps3_cui *ps3_cui)
+{
+       int result;
+       struct pmenu *m;
+       struct pmenu_item *i;
+
+       m = pmenu_init(ps3_cui->cui, 12, ps3_svm_to_mm_helper);
+
+       if (!m) {
+               pb_log("%s: failed\n", __func__);
+               return NULL;
+       }
+
+       m->hot_key = ps3_hot_key;
+       m->scr.frame.title = talloc_strdup(m, "Select PS3 Video Mode");
+       m->scr.frame.help = talloc_strdup(m, "ESC=exit, Enter=accept");
+
+       i = pmenu_item_init(m, 0, "auto detect",
+               "Auto detect the best HDMI video mode");
+       i->on_execute = ps3_svm_cb;
+       i->data = (void *)0;
+
+       i = pmenu_item_init(m, 1, "480i    (576 x 384)", NULL);
+       i->on_execute = ps3_svm_cb;
+       i->data = (void *)1;
+
+       i = pmenu_item_init(m, 2, "480p    (576 x 384)", NULL);
+       i->on_execute = ps3_svm_cb;
+       i->data = (void *)2;
+
+       i = pmenu_item_init(m, 3, "576i    (576 x 460)", NULL);
+       i->on_execute = ps3_svm_cb;
+       i->data = (void *)6;
+
+       i = pmenu_item_init(m, 4, "576p    (576 x 460)", NULL);
+       i->on_execute = ps3_svm_cb;
+       i->data = (void *)7;
+
+       i = pmenu_item_init(m, 5, "720p   (1124 x 644)", NULL);
+       i->on_execute = ps3_svm_cb;
+       i->data = (void *)3;
+
+       i = pmenu_item_init(m, 6, "1080i  (1688 x 964)", NULL);
+       i->on_execute = ps3_svm_cb;
+       i->data = (void *)4;
+
+       i = pmenu_item_init(m, 7, "1080p  (1688 x 964)", NULL);
+       i->on_execute = ps3_svm_cb;
+       i->data = (void *)5;
+
+       i = pmenu_item_init(m, 8, "wxga   (1280 x 768)", NULL);
+       i->on_execute = ps3_svm_cb;
+       i->data = (void *)11;
+
+       i = pmenu_item_init(m, 9, "sxga   (1280 x 1024)", NULL);
+       i->on_execute = ps3_svm_cb;
+       i->data = (void *)12;
+
+       i = pmenu_item_init(m, 10, "wuxga  (1920 x 1200)", NULL);
+       i->on_execute = ps3_svm_cb;
+       i->data = (void *)13;
+
+       i = pmenu_item_init(m, 11, "Return",
+               "Return to the main menu");
+       i->on_execute = ps3_svm_to_mm_cb;
+
+       result = pmenu_setup(m);
+
+       if (result) {
+               pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
+                       strerror(errno));
+               goto fail_setup;
+       }
+
+       menu_opts_off(m->ncm, O_SHOWDESC);
+       set_menu_mark(m->ncm, " *");
+
+       return m;
+
+fail_setup:
+       talloc_free(m);
+       return NULL;
+}
+
+static struct ps3_cui ps3;
+
+static void sig_handler(int signum)
+{
+       DBGS("%d\n", signum);
+
+       switch (signum) {
+       case SIGWINCH:
+               if (ps3.cui)
+                       cui_resize(ps3.cui);
+               break;
+       default:
+               assert(0 && "unknown sig");
+               /* fall through */
+       case SIGINT:
+       case SIGHUP:
+       case SIGTERM:
+               if (ps3.cui)
+                       cui_abort(ps3.cui);
+               break;
+       }
+}
+
+/**
+ * main - cui bootloader main routine.
+ */
+
+int main(int argc, char *argv[])
+{
+       static struct sigaction sa;
+       static struct opts opts;
+       int result;
+       int cui_result;
+       unsigned int mode;
+       FILE *log;
+
+       result = opts_parse(&opts, argc, argv);
+
+       if (result) {
+               print_usage();
+               return EXIT_FAILURE;
+       }
+
+       if (opts.show_help == opt_yes) {
+               print_usage();
+               return EXIT_SUCCESS;
+       }
+
+       if (opts.show_version == opt_yes) {
+               print_version();
+               return EXIT_SUCCESS;
+       }
+
+       log = fopen(opts.log_file, "a");
+       assert(log);
+       pb_log_set_stream(log);
+
+#if defined(DEBUG)
+       pb_log_always_flush(1);
+#endif
+
+       pb_log("--- pb-cui ---\n");
+
+       sa.sa_handler = sig_handler;
+       result = sigaction(SIGINT, &sa, NULL);
+       result += sigaction(SIGHUP, &sa, NULL);
+       result += sigaction(SIGTERM, &sa, NULL);
+       result += sigaction(SIGWINCH, &sa, NULL);
+
+       if (result) {
+               pb_log("%s sigaction failed.\n", __func__);
+               return EXIT_FAILURE;
+       }
+
+       ps3.dirty_values = ps3_flash_get_values(&ps3.values);
+
+       result = ps3_get_video_mode(&mode);
+
+       /* Current becomes default if ps3_flash_get_values() failed. */
+
+       if (ps3.dirty_values && !result)
+               ps3.values.video_mode = mode;
+
+       /* Set mode if not at default. */
+
+       if (!result && (ps3.values.video_mode != (uint16_t)mode))
+               ps3_set_video_mode(ps3.values.video_mode);
+
+       ps3.cui = cui_init(&ps3, ps3_kexec_cb);
+
+       if (!ps3.cui)
+               return EXIT_FAILURE;
+
+       ps3.mm = ps3_mm_init(&ps3);
+       ps3.svm = ps3_svm_init(&ps3);
+
+       cui_result = cui_run(ps3.cui, ps3.mm, ps3.values.default_item);
+
+       pmenu_delete(ps3.mm);
+       pmenu_delete(ps3.svm);
+
+       if (ps3.dirty_values)
+               ps3_flash_set_values(&ps3.values);
+
+       talloc_free(ps3.cui);
+
+       pb_log("--- end ---\n");
+
+       return cui_result ? EXIT_FAILURE : EXIT_SUCCESS;
+}