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