]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/ps3-cui.c
62ca7fa7b5d5becb42dd986916818836bcc0f8db
[petitboot] / ui / ncurses / ps3-cui.c
1 /*
2  * Petitboot cui bootloader for the PS3 game console
3  *
4  *  Copyright (C) 2009 Sony Computer Entertainment Inc.
5  *  Copyright 2009 Sony Corp.
6  *
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.
10  *
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.
15  *
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
19  */
20
21 /*
22  * TODO
23  * removable media event
24  * ncurses mouse support
25  */
26
27 #if defined(HAVE_CONFIG_H)
28 #include "config.h"
29 #endif
30
31 #define _GNU_SOURCE
32 #include <errno.h>
33 #include <getopt.h>
34 #include <signal.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/time.h>
38
39 #include "log/log.h"
40 #include "talloc/talloc.h"
41 #include "waiter/waiter.h"
42 #include "ui/common/discover-client.h"
43 #include "ui/common/ps3.h"
44 #include "nc-cui.h"
45
46 static void print_version(void)
47 {
48         printf("pb-cui (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
49 }
50
51 static void print_usage(void)
52 {
53         print_version();
54         printf(
55 "Usage: pb-cui [-h, --help] [-l, --log log-file] [-r, --reset-defaults]\n"
56 "              [-t, --timeout] [-V, --version]\n");
57 }
58
59 /**
60  * enum opt_value - Tri-state options variables.
61  */
62
63 enum opt_value {opt_undef = 0, opt_yes, opt_no};
64
65 /**
66  * struct opts - Values from command line options.
67  */
68
69 struct opts {
70         enum opt_value show_help;
71         const char *log_file;
72         enum opt_value reset_defaults;
73         enum opt_value use_timeout;
74         enum opt_value show_version;
75 };
76
77 /**
78  * opts_parse - Parse the command line options.
79  */
80
81 static int opts_parse(struct opts *opts, int argc, char *argv[])
82 {
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'},
89                 { NULL, 0, NULL, 0},
90         };
91         static const char short_options[] = "hl:trV";
92         static const struct opts default_values = {
93                 .log_file = "pb-cui.log",
94         };
95
96         *opts = default_values;
97
98         while (1) {
99                 int c = getopt_long(argc, argv, short_options, long_options,
100                         NULL);
101
102                 if (c == EOF)
103                         break;
104
105                 switch (c) {
106                 case 'h':
107                         opts->show_help = opt_yes;
108                         break;
109                 case 'l':
110                         opts->log_file = optarg;
111                         break;
112                 case 't':
113                         opts->use_timeout = opt_yes;
114                         break;
115                 case 'r':
116                         opts->reset_defaults = opt_yes;
117                         break;
118                 case 'V':
119                         opts->show_version = opt_yes;
120                         break;
121                 default:
122                         opts->show_help = opt_yes;
123                         return -1;
124                 }
125         }
126
127         return optind != argc;
128 }
129
130 /**
131  * struct ps3_cui - Main cui program instance.
132  * @mm: Main menu.
133  * @svm: Set video mode menu.
134  */
135
136 struct ps3_cui {
137         struct pmenu *mm;
138         struct pmenu *svm;
139         struct cui *cui;
140         struct ps3_flash_values values;
141         int dirty_values;
142 };
143
144 static struct ps3_cui *ps3_from_cui(struct cui *cui)
145 {
146         struct ps3_cui *ps3;
147
148         assert(cui->c_sig == pb_cui_sig);
149         ps3 = cui->platform_info;
150         assert(ps3->cui->c_sig == pb_cui_sig);
151         return ps3;
152 }
153
154 static struct ps3_cui *ps3_from_item(struct pmenu_item *item)
155 {
156         return ps3_from_cui(cui_from_item(item));
157 }
158
159 /**
160  * ps3_set_mode - Set video mode helper.
161  *
162  * Runs ps3_set_video_mode().
163  */
164
165 static void ps3_set_mode(struct ps3_cui *ps3, unsigned int mode)
166 {
167         int result;
168
169         if (ps3->values.video_mode == (uint16_t)mode)
170                 return;
171
172         ps3->values.video_mode = (uint16_t)mode;
173         ps3->dirty_values = 1;
174
175         result = ps3_set_video_mode(mode);
176
177         if (result)
178                 nc_scr_status_printf(ps3->cui->current,
179                         "Failed: set_video_mode(%u)", mode);
180 }
181
182 /**
183  * ps3_svm_cb - The set video mode callback.
184  */
185
186 static int ps3_svm_cb(struct pmenu_item *item)
187 {
188         ps3_set_mode(ps3_from_item(item), (unsigned int)item->data);
189         return 0;
190 }
191
192 /**
193  * ps3_kexec_cb - The kexec callback.
194  *
195  * Writes config data to PS3 flash then calls pb_run_kexec().
196  */
197
198 static int ps3_kexec_cb(struct cui *cui, struct cui_opt_data *cod)
199 {
200         struct ps3_cui *ps3 = ps3_from_cui(cui);
201
202         pb_log("%s: %s:%s\n", __func__, cod->dev->name, cod->opt->name);
203
204         assert(ps3->cui->current == &ps3->cui->main->scr);
205
206         if (cui->default_item != cod->opt_hash || ps3->dirty_values) {
207                 ps3->values.default_item = cod->opt_hash;
208                 ps3_flash_set_values(&ps3->values);
209         }
210
211         return pb_run_kexec(cod->kd);
212 }
213
214 /**
215  * ps3_mm_to_svm_cb - Callback to switch to the set video mode menu.
216  */
217
218 static int ps3_mm_to_svm_cb(struct pmenu_item *item)
219 {
220         struct ps3_cui *ps3 = ps3_from_item(item);
221         struct nc_scr *old;
222
223         old = cui_set_current(ps3->cui, &ps3->svm->scr);
224         assert(old == &ps3->mm->scr);
225
226         return 0;
227 }
228
229 /**
230  * ps3_svm_to_mm_cb - Callback to switch back to the main menu.
231  */
232
233 static int ps3_svm_to_mm_cb(struct pmenu_item *item)
234 {
235         struct ps3_cui *ps3 = ps3_from_item(item);
236         struct nc_scr *old;
237
238         old = cui_set_current(ps3->cui, &ps3->mm->scr);
239         assert(old == &ps3->svm->scr);
240
241         return 0;
242 }
243
244 /**
245  * ps3_svm_to_mm_helper - The svm ESC callback.
246  */
247
248 static void ps3_svm_to_mm_helper(struct pmenu *menu)
249 {
250         ps3_svm_to_mm_cb(pmenu_find_selected(menu));
251 }
252
253 /**
254  * ps3_hot_key - PS3 specific hot keys.
255  *
256  * '@' = Set video mode to auto (mode 0)
257  * '$' = Set video mode to safe (480i)
258  * '+' = Cycles through a set of common video modes.
259  * '-' = Cycles through a set of common video modes in reverse.
260  */
261
262 static int ps3_hot_key(struct pmenu __attribute__((unused)) *menu,
263         struct pmenu_item *item, int c)
264 {
265         static const unsigned int modes[] = {0, 1, 6, 3, 11, 12};
266         static const unsigned int *const end = modes
267                 + sizeof(modes) / sizeof(modes[0]) - 1;
268         static const unsigned int *p = modes;
269
270         switch (c) {
271         default:
272                 /* DBGS("%d (%o)\n", c, c); */
273                 break;
274         case '@':
275                 p = modes + 0;
276                 ps3_set_mode(ps3_from_item(item), *p);
277                 break;
278         case '$':
279                 p = modes + 1;
280                 ps3_set_mode(ps3_from_item(item), *p);
281                 break;
282         case '+':
283                 p = (p < end) ? p + 1 : modes;
284                 ps3_set_mode(ps3_from_item(item), *p);
285                 break;
286         case '-':
287                 p = (p > modes) ? p - 1 : end;
288                 ps3_set_mode(ps3_from_item(item), *p);
289                 break;
290         }
291
292         return c;
293 }
294
295 /**
296  * ps3_timer_update - Timer callback.
297  */
298
299 static void ps3_timer_update(struct ui_timer *timer, unsigned int timeout)
300 {
301         struct ps3_cui *ps3 = ps3_from_cui(cui_from_timer(timer));
302
303         //FIXME: make scr:timer.
304         // nc_scr_timer_update(&ps3.mm->scr, timeout);
305
306         nc_scr_status_printf(&ps3->mm->scr,
307                 "Welcome to Petitboot (timeout %u sec)", timeout);
308 }
309
310 /**
311  * ps3_mm_init - Setup the main menu instance.
312  */
313
314 static struct pmenu *ps3_mm_init(struct ps3_cui *ps3_cui)
315 {
316         int result;
317         struct pmenu *m;
318         struct pmenu_item *i;
319         static const char *const bgo[] =
320                 {"/usr/sbin/ps3-boot-game-os-NOT", NULL};
321
322         m = pmenu_init(ps3_cui->cui, 3, cui_on_exit);
323
324         if (!m) {
325                 pb_log("%s: failed\n", __func__);
326                 return NULL;
327         }
328
329         m->hot_key = ps3_hot_key;
330 #if defined(DEBUG)
331         m->scr.frame.title = talloc_strdup(m,
332                 "Petitboot PS3 (ver " PACKAGE_VERSION ")");
333 #else
334         m->scr.frame.title = talloc_strdup(m, "Petitboot PS3");
335 #endif
336         m->scr.frame.help = talloc_strdup(m,
337                 "ESC=exit, Enter=accept, E,e=edit");
338         m->scr.frame.status = talloc_strdup(m, "Welcome to Petitboot");
339
340         i = pmenu_item_init(m, 0, "Boot GameOS",
341                 "Reboot the PS3 into the GameOS");
342         i->on_execute = cui_run_cmd;
343         i->data = (void *)bgo;
344
345         i = pmenu_item_init(m, 1, "Set Video Mode",
346                 "Display a video mode selection menu");
347         i->on_execute = ps3_mm_to_svm_cb;
348
349         i = pmenu_item_init(m, 2, "Exit to Shell",
350                 "Exit petitboot and return to a shell prompt");
351         i->on_execute = pmenu_exit_cb;
352
353         result = pmenu_setup(m);
354
355         if (result) {
356                 pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
357                         strerror(errno));
358                 goto fail_setup;
359         }
360
361         menu_opts_off(m->ncm, O_SHOWDESC);
362         set_menu_mark(m->ncm, " *");
363         set_current_item(m->ncm, i->nci);
364
365         return m;
366
367 fail_setup:
368         talloc_free(m);
369         return NULL;
370 }
371
372 /**
373  * ps3_svm_init - Setup the set video mode menu instance.
374  */
375
376 static struct pmenu *ps3_svm_init(struct ps3_cui *ps3_cui)
377 {
378         int result;
379         struct pmenu *m;
380         struct pmenu_item *i;
381
382         m = pmenu_init(ps3_cui->cui, 12, ps3_svm_to_mm_helper);
383
384         if (!m) {
385                 pb_log("%s: failed\n", __func__);
386                 return NULL;
387         }
388
389         m->hot_key = ps3_hot_key;
390         m->scr.frame.title = talloc_strdup(m, "Select PS3 Video Mode");
391         m->scr.frame.help = talloc_strdup(m, "ESC=exit, Enter=accept");
392
393         i = pmenu_item_init(m, 0, "auto detect",
394                 "Auto detect the best HDMI video mode");
395         i->on_execute = ps3_svm_cb;
396         i->data = (void *)0;
397
398         i = pmenu_item_init(m, 1, "480i    (576 x 384)", NULL);
399         i->on_execute = ps3_svm_cb;
400         i->data = (void *)1;
401
402         i = pmenu_item_init(m, 2, "480p    (576 x 384)", NULL);
403         i->on_execute = ps3_svm_cb;
404         i->data = (void *)2;
405
406         i = pmenu_item_init(m, 3, "576i    (576 x 460)", NULL);
407         i->on_execute = ps3_svm_cb;
408         i->data = (void *)6;
409
410         i = pmenu_item_init(m, 4, "576p    (576 x 460)", NULL);
411         i->on_execute = ps3_svm_cb;
412         i->data = (void *)7;
413
414         i = pmenu_item_init(m, 5, "720p   (1124 x 644)", NULL);
415         i->on_execute = ps3_svm_cb;
416         i->data = (void *)3;
417
418         i = pmenu_item_init(m, 6, "1080i  (1688 x 964)", NULL);
419         i->on_execute = ps3_svm_cb;
420         i->data = (void *)4;
421
422         i = pmenu_item_init(m, 7, "1080p  (1688 x 964)", NULL);
423         i->on_execute = ps3_svm_cb;
424         i->data = (void *)5;
425
426         i = pmenu_item_init(m, 8, "wxga   (1280 x 768)", NULL);
427         i->on_execute = ps3_svm_cb;
428         i->data = (void *)11;
429
430         i = pmenu_item_init(m, 9, "sxga   (1280 x 1024)", NULL);
431         i->on_execute = ps3_svm_cb;
432         i->data = (void *)12;
433
434         i = pmenu_item_init(m, 10, "wuxga  (1920 x 1200)", NULL);
435         i->on_execute = ps3_svm_cb;
436         i->data = (void *)13;
437
438         i = pmenu_item_init(m, 11, "Return",
439                 "Return to the main menu");
440         i->on_execute = ps3_svm_to_mm_cb;
441
442         result = pmenu_setup(m);
443
444         if (result) {
445                 pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
446                         strerror(errno));
447                 goto fail_setup;
448         }
449
450         menu_opts_off(m->ncm, O_SHOWDESC);
451         set_menu_mark(m->ncm, " *");
452
453         return m;
454
455 fail_setup:
456         talloc_free(m);
457         return NULL;
458 }
459
460 static struct ps3_cui ps3;
461
462 static void sig_handler(int signum)
463 {
464         DBGS("%d\n", signum);
465
466         switch (signum) {
467         case SIGALRM:
468                 if (ps3.cui)
469                         ui_timer_sigalrm(&ps3.cui->timer);
470                 break;
471         case SIGWINCH:
472                 if (ps3.cui)
473                         cui_resize(ps3.cui);
474                 break;
475         default:
476                 assert(0 && "unknown sig");
477                 /* fall through */
478         case SIGINT:
479         case SIGHUP:
480         case SIGTERM:
481                 if (ps3.cui)
482                         cui_abort(ps3.cui);
483                 break;
484         }
485 }
486
487 /**
488  * main - cui bootloader main routine.
489  */
490
491 int main(int argc, char *argv[])
492 {
493         static struct sigaction sa;
494         static struct opts opts;
495         int result;
496         int cui_result;
497         unsigned int mode;
498         FILE *log;
499
500         result = opts_parse(&opts, argc, argv);
501
502         if (result) {
503                 print_usage();
504                 return EXIT_FAILURE;
505         }
506
507         if (opts.show_help == opt_yes) {
508                 print_usage();
509                 return EXIT_SUCCESS;
510         }
511
512         if (opts.show_version == opt_yes) {
513                 print_version();
514                 return EXIT_SUCCESS;
515         }
516
517         log = fopen(opts.log_file, "a");
518         assert(log);
519         pb_log_set_stream(log);
520
521 #if defined(DEBUG)
522         pb_log_always_flush(1);
523 #endif
524
525         pb_log("--- pb-cui ---\n");
526
527         sa.sa_handler = sig_handler;
528         result = sigaction(SIGALRM, &sa, NULL);
529         result += sigaction(SIGHUP, &sa, NULL);
530         result += sigaction(SIGINT, &sa, NULL);
531         result += sigaction(SIGTERM, &sa, NULL);
532         result += sigaction(SIGWINCH, &sa, NULL);
533
534         if (result) {
535                 pb_log("%s sigaction failed.\n", __func__);
536                 return EXIT_FAILURE;
537         }
538
539         ps3.values = ps3_flash_defaults;
540
541         if (opts.reset_defaults != opt_yes)
542                 ps3.dirty_values = ps3_flash_get_values(&ps3.values);
543
544         result = ps3_get_video_mode(&mode);
545
546         /* Current becomes default if ps3_flash_get_values() failed. */
547
548         if (ps3.dirty_values && !result)
549                 ps3.values.video_mode = mode;
550
551         /* Set mode if not at default. */
552
553         if (!result && (ps3.values.video_mode != (uint16_t)mode))
554                 ps3_set_video_mode(ps3.values.video_mode);
555
556         ps3.cui = cui_init(&ps3, ps3_kexec_cb);
557
558         if (!ps3.cui)
559                 return EXIT_FAILURE;
560
561         ps3.mm = ps3_mm_init(&ps3);
562         ps3.svm = ps3_svm_init(&ps3);
563
564         if (opts.use_timeout != opt_yes
565                 || ps3.values.timeout == ps3_timeout_forever)
566                 ui_timer_disable(&ps3.cui->timer);
567         else {
568                 ps3.cui->timer.update_display = ps3_timer_update;
569                 ui_timer_init(&ps3.cui->timer, ps3.values.timeout);
570         }
571
572         cui_result = cui_run(ps3.cui, ps3.mm, ps3.values.default_item);
573
574         pmenu_delete(ps3.mm);
575         pmenu_delete(ps3.svm);
576
577         if (ps3.dirty_values)
578                 ps3_flash_set_values(&ps3.values);
579
580         talloc_free(ps3.cui);
581
582         pb_log("--- end ---\n");
583
584         return cui_result ? EXIT_FAILURE : EXIT_SUCCESS;
585 }