summaryrefslogtreecommitdiffstats
path: root/ui/ncurses/ps3-cui.c
blob: f166c8830cf54a748080e4205e853344c5940df1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
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;
}
OpenPOWER on IntegriCloud