2 * Petitboot cui bootloader for the PS3 game console
4 * Copyright (C) 2009 Sony Computer Entertainment Inc.
5 * Copyright 2009 Sony Corp.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * removable media event
24 * ncurses mouse support
27 #if defined(HAVE_CONFIG_H)
40 #include "talloc/talloc.h"
41 #include "waiter/waiter.h"
42 #include "ui/common/discover-client.h"
43 #include "ui/common/ps3.h"
46 static void print_version(void)
48 printf("pb-cui (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
51 static void print_usage(void)
55 "Usage: pb-cui [-h, --help] [-l, --log log-file] [-r, --reset-defaults]\n"
56 " [-t, --timeout] [-V, --version]\n");
60 * enum opt_value - Tri-state options variables.
63 enum opt_value {opt_undef = 0, opt_yes, opt_no};
66 * struct opts - Values from command line options.
70 enum opt_value show_help;
72 enum opt_value reset_defaults;
73 enum opt_value use_timeout;
74 enum opt_value show_version;
78 * opts_parse - Parse the command line options.
81 static int opts_parse(struct opts *opts, int argc, char *argv[])
83 static const struct option long_options[] = {
84 {"help", no_argument, NULL, 'h'},
85 {"log", required_argument, NULL, 'l'},
86 {"reset-defaults", no_argument, NULL, 'r'},
87 {"timeout", no_argument, NULL, 't'},
88 {"version", no_argument, NULL, 'V'},
91 static const char short_options[] = "hl:trV";
92 static const struct opts default_values = {
93 .log_file = "pb-cui.log",
96 *opts = default_values;
99 int c = getopt_long(argc, argv, short_options, long_options,
107 opts->show_help = opt_yes;
110 opts->log_file = optarg;
113 opts->use_timeout = opt_yes;
116 opts->reset_defaults = opt_yes;
119 opts->show_version = opt_yes;
122 opts->show_help = opt_yes;
127 return optind != argc;
131 * struct ps3_cui - Main cui program instance.
133 * @svm: Set video mode menu.
140 struct ps3_flash_values values;
144 static struct ps3_cui *ps3_from_cui(struct cui *cui)
148 assert(cui->c_sig == pb_cui_sig);
149 ps3 = cui->platform_info;
150 assert(ps3->cui->c_sig == pb_cui_sig);
154 static struct ps3_cui *ps3_from_item(struct pmenu_item *item)
156 return ps3_from_cui(cui_from_item(item));
160 * ps3_sixaxis_map - Map a Linux joystick event to an ncurses key code.
164 static int ps3_sixaxis_map(const struct js_event *e)
167 static const int axis_map[] = {
168 0, /* 0 Left thumb X */
169 0, /* 1 Left thumb Y */
170 0, /* 2 Right thumb X */
171 0, /* 3 Right thumb Y */
177 0, /* 9 Dpad Right */
178 0, /* 10 Dpad Down */
179 0, /* 11 Dpad Left */
198 static const int button_map[] = {
203 KEY_UP, /* 4 Dpad Up */
204 0, /* 5 Dpad Right */
205 KEY_DOWN, /* 6 Dpad Down */
209 KEY_HOME, /* 10 L1 */
215 0, /* 16 PS Button */
223 if (e->type == JS_EVENT_BUTTON
224 && e->number < sizeof(button_map) / sizeof(button_map[0]))
225 return button_map[e->number];
228 if (e->type == JS_EVENT_AXIS
229 && e->number < sizeof(axis_map) / sizeof(axis_map[0]))
230 return axis_map[e->number];
237 * ps3_set_mode - Set video mode helper.
239 * Runs ps3_set_video_mode().
242 static void ps3_set_mode(struct ps3_cui *ps3, unsigned int mode)
246 if (ps3->values.video_mode == (uint16_t)mode)
249 ps3->values.video_mode = (uint16_t)mode;
250 ps3->dirty_values = 1;
252 result = ps3_set_video_mode(mode);
255 nc_scr_status_printf(ps3->cui->current,
256 "Failed: set_video_mode(%u)", mode);
260 * ps3_svm_cb - The set video mode callback.
263 static int ps3_svm_cb(struct pmenu_item *item)
265 ps3_set_mode(ps3_from_item(item), (unsigned int)item->data);
270 * ps3_kexec_cb - The kexec callback.
272 * Writes config data to PS3 flash then calls pb_run_kexec().
275 static int ps3_kexec_cb(struct cui *cui, struct cui_opt_data *cod)
277 struct ps3_cui *ps3 = ps3_from_cui(cui);
279 pb_log("%s: %s\n", __func__, cod->name);
281 assert(ps3->cui->current == &ps3->cui->main->scr);
283 if ((cod->opt_hash && cod->opt_hash != cui->default_item)
284 || ps3->dirty_values) {
285 ps3->values.default_item = cod->opt_hash;
286 ps3_flash_set_values(&ps3->values);
289 return pb_run_kexec(cod->kd);
293 * ps3_mm_to_svm_cb - Callback to switch to the set video mode menu.
296 static int ps3_mm_to_svm_cb(struct pmenu_item *item)
298 struct ps3_cui *ps3 = ps3_from_item(item);
301 old = cui_set_current(ps3->cui, &ps3->svm->scr);
302 assert(old == &ps3->mm->scr);
308 * ps3_svm_to_mm_cb - Callback to switch back to the main menu.
311 static int ps3_svm_to_mm_cb(struct pmenu_item *item)
313 struct ps3_cui *ps3 = ps3_from_item(item);
316 old = cui_set_current(ps3->cui, &ps3->mm->scr);
317 assert(old == &ps3->svm->scr);
323 * ps3_svm_to_mm_helper - The svm ESC callback.
326 static void ps3_svm_to_mm_helper(struct pmenu *menu)
328 ps3_svm_to_mm_cb(pmenu_find_selected(menu));
332 * ps3_hot_key - PS3 specific hot keys.
334 * '@' = Set video mode to auto (mode 0)
335 * '$' = Set video mode to safe (480i)
336 * '+' = Cycles through a set of common video modes.
337 * '-' = Cycles through a set of common video modes in reverse.
340 static int ps3_hot_key(struct pmenu __attribute__((unused)) *menu,
341 struct pmenu_item *item, int c)
343 static const unsigned int modes[] = {0, 1, 6, 3, 11, 12};
344 static const unsigned int *const end = modes
345 + sizeof(modes) / sizeof(modes[0]) - 1;
346 static const unsigned int *p = modes;
350 /* DBGS("%d (%o)\n", c, c); */
354 ps3_set_mode(ps3_from_item(item), *p);
358 ps3_set_mode(ps3_from_item(item), *p);
361 p = (p < end) ? p + 1 : modes;
362 ps3_set_mode(ps3_from_item(item), *p);
365 p = (p > modes) ? p - 1 : end;
366 ps3_set_mode(ps3_from_item(item), *p);
374 * ps3_timer_update - Timer callback.
377 static void ps3_timer_update(struct ui_timer *timer, unsigned int timeout)
379 struct ps3_cui *ps3 = ps3_from_cui(cui_from_timer(timer));
381 //FIXME: make scr:timer.
382 // nc_scr_timer_update(&ps3.mm->scr, timeout);
384 nc_scr_status_printf(&ps3->mm->scr,
385 "Welcome to Petitboot (timeout %u sec)", timeout);
389 * ps3_mm_init - Setup the main menu instance.
392 static struct pmenu *ps3_mm_init(struct ps3_cui *ps3_cui)
396 struct pmenu_item *i;
397 static const char *const bgo[] = {"/usr/sbin/ps3-boot-game-os", NULL};
399 m = pmenu_init(ps3_cui->cui, 3, cui_on_exit);
402 pb_log("%s: failed\n", __func__);
406 m->hot_key = ps3_hot_key;
407 m->on_open = cui_on_open;
410 m->scr.frame.title = talloc_strdup(m,
411 "Petitboot PS3 (ver " PACKAGE_VERSION ")");
413 m->scr.frame.title = talloc_strdup(m, "Petitboot PS3");
415 m->scr.frame.help = talloc_strdup(m,
416 "ESC=exit, Enter=accept, e=edit, o=open");
417 m->scr.frame.status = talloc_strdup(m, "Welcome to Petitboot");
419 i = pmenu_item_init(m, 0, "Boot GameOS");
420 i->on_execute = cui_run_cmd;
421 i->data = (void *)bgo;
423 i = pmenu_item_init(m, 1, "Set Video Mode");
424 i->on_execute = ps3_mm_to_svm_cb;
426 i = pmenu_item_init(m, 2, "Exit to Shell");
427 i->on_execute = pmenu_exit_cb;
429 result = pmenu_setup(m);
432 pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
437 menu_opts_off(m->ncm, O_SHOWDESC);
438 set_menu_mark(m->ncm, " *");
439 set_current_item(m->ncm, i->nci);
449 * ps3_svm_init - Setup the set video mode menu instance.
452 static struct pmenu *ps3_svm_init(struct ps3_cui *ps3_cui)
456 struct pmenu_item *i;
458 m = pmenu_init(ps3_cui->cui, 12, ps3_svm_to_mm_helper);
461 pb_log("%s: failed\n", __func__);
465 m->hot_key = ps3_hot_key;
466 m->scr.frame.title = talloc_strdup(m, "Select PS3 Video Mode");
467 m->scr.frame.help = talloc_strdup(m, "ESC=exit, Enter=accept");
469 i = pmenu_item_init(m, 0, "auto detect");
470 i->on_execute = ps3_svm_cb;
473 i = pmenu_item_init(m, 1, "480i (576 x 384)");
474 i->on_execute = ps3_svm_cb;
477 i = pmenu_item_init(m, 2, "480p (576 x 384)");
478 i->on_execute = ps3_svm_cb;
481 i = pmenu_item_init(m, 3, "576i (576 x 460)");
482 i->on_execute = ps3_svm_cb;
485 i = pmenu_item_init(m, 4, "576p (576 x 460)");
486 i->on_execute = ps3_svm_cb;
489 i = pmenu_item_init(m, 5, "720p (1124 x 644)");
490 i->on_execute = ps3_svm_cb;
493 i = pmenu_item_init(m, 6, "1080i (1688 x 964)");
494 i->on_execute = ps3_svm_cb;
497 i = pmenu_item_init(m, 7, "1080p (1688 x 964)");
498 i->on_execute = ps3_svm_cb;
501 i = pmenu_item_init(m, 8, "wxga (1280 x 768)");
502 i->on_execute = ps3_svm_cb;
503 i->data = (void *)11;
505 i = pmenu_item_init(m, 9, "sxga (1280 x 1024)");
506 i->on_execute = ps3_svm_cb;
507 i->data = (void *)12;
509 i = pmenu_item_init(m, 10, "wuxga (1920 x 1200)");
510 i->on_execute = ps3_svm_cb;
511 i->data = (void *)13;
513 i = pmenu_item_init(m, 11, "Return");
514 i->on_execute = ps3_svm_to_mm_cb;
516 result = pmenu_setup(m);
519 pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
524 menu_opts_off(m->ncm, O_SHOWDESC);
525 set_menu_mark(m->ncm, " *");
534 static struct ps3_cui ps3;
536 static void sig_handler(int signum)
538 DBGS("%d\n", signum);
543 ui_timer_sigalrm(&ps3.cui->timer);
550 assert(0 && "unknown sig");
562 * main - cui bootloader main routine.
565 int main(int argc, char *argv[])
567 static struct sigaction sa;
568 static struct opts opts;
574 result = opts_parse(&opts, argc, argv);
581 if (opts.show_help == opt_yes) {
586 if (opts.show_version == opt_yes) {
591 log = fopen(opts.log_file, "a");
593 pb_log_set_stream(log);
596 pb_log_always_flush(1);
599 pb_log("--- pb-cui ---\n");
601 sa.sa_handler = sig_handler;
602 result = sigaction(SIGALRM, &sa, NULL);
603 result += sigaction(SIGHUP, &sa, NULL);
604 result += sigaction(SIGINT, &sa, NULL);
605 result += sigaction(SIGTERM, &sa, NULL);
606 result += sigaction(SIGWINCH, &sa, NULL);
609 pb_log("%s sigaction failed.\n", __func__);
613 ps3.values = ps3_flash_defaults;
615 if (opts.reset_defaults != opt_yes)
616 ps3.dirty_values = ps3_flash_get_values(&ps3.values);
618 result = ps3_get_video_mode(&mode);
620 /* Current becomes default if ps3_flash_get_values() failed. */
622 if (ps3.dirty_values && !result)
623 ps3.values.video_mode = mode;
625 /* Set mode if not at default. */
627 if (!result && (ps3.values.video_mode != (uint16_t)mode))
628 ps3_set_video_mode(ps3.values.video_mode);
630 ps3.cui = cui_init(&ps3, ps3_kexec_cb, ps3_sixaxis_map);
635 ps3.mm = ps3_mm_init(&ps3);
636 ps3.svm = ps3_svm_init(&ps3);
638 if (opts.use_timeout != opt_yes
639 || ps3.values.timeout == ps3_timeout_forever)
640 ui_timer_disable(&ps3.cui->timer);
642 ps3.cui->timer.update_display = ps3_timer_update;
643 ui_timer_init(&ps3.cui->timer, ps3.values.timeout);
646 cui_result = cui_run(ps3.cui, ps3.mm, ps3.values.default_item);
648 pmenu_delete(ps3.mm);
649 pmenu_delete(ps3.svm);
651 if (ps3.dirty_values)
652 ps3_flash_set_values(&ps3.values);
654 talloc_free(ps3.cui);
656 pb_log("--- end ---\n");
658 return cui_result ? EXIT_FAILURE : EXIT_SUCCESS;