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
|
/*
* Petitboot generic ncurses bootloader UI
*
* 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
*/
#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 <sys/time.h>
#include "log/log.h"
#include "talloc/talloc.h"
#include "waiter/waiter.h"
#include "ui/common/discover-client.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 pb_cui - Main cui program instance.
* @mm: Main menu.
* @svm: Set video mode menu.
*/
struct pb_cui {
struct pmenu *mm;
struct cui *cui;
};
static struct pb_cui *pb_from_cui(struct cui *cui)
{
struct pb_cui *pb;
assert(cui->c_sig == pb_cui_sig);
pb = cui->platform_info;
assert(pb->cui->c_sig == pb_cui_sig);
return pb;
}
/**
* pb_kexec_cb - The kexec callback.
*/
static int pb_kexec_cb(struct cui *cui, struct cui_opt_data *cod)
{
struct pb_cui *pb = pb_from_cui(cui);
pb_log("%s: %s\n", __func__, cod->name);
assert(pb->cui->current == &pb->cui->main->scr);
return pb_run_kexec(cod->kd);
}
/**
* pb_mm_init - Setup the main menu instance.
*/
static struct pmenu *pb_mm_init(struct pb_cui *pb_cui)
{
int result;
struct pmenu *m;
struct pmenu_item *i;
m = pmenu_init(pb_cui->cui, 1, cui_on_exit);
if (!m) {
pb_log("%s: failed\n", __func__);
return NULL;
}
m->on_open = cui_on_open;
m->scr.frame.title = talloc_strdup(m, "Petitboot");
m->scr.frame.help = talloc_strdup(m,
"ESC=exit, Enter=accept, e=edit, o=open");
m->scr.frame.status = talloc_strdup(m, "Welcome to Petitboot");
i = pmenu_item_init(m, 0, "Exit to Shell");
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;
}
static struct pb_cui pb;
static void sig_handler(int signum)
{
DBGS("%d\n", signum);
switch (signum) {
case SIGALRM:
if (pb.cui)
ui_timer_sigalrm(&pb.cui->timer);
break;
case SIGWINCH:
if (pb.cui)
cui_resize(pb.cui);
break;
default:
assert(0 && "unknown sig");
/* fall through */
case SIGINT:
case SIGHUP:
case SIGTERM:
if (pb.cui)
cui_abort(pb.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;
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(SIGALRM, &sa, NULL);
result += sigaction(SIGHUP, &sa, NULL);
result += sigaction(SIGINT, &sa, NULL);
result += sigaction(SIGTERM, &sa, NULL);
result += sigaction(SIGWINCH, &sa, NULL);
if (result) {
pb_log("%s sigaction failed.\n", __func__);
return EXIT_FAILURE;
}
pb.cui = cui_init(&pb, pb_kexec_cb, NULL);
if (!pb.cui)
return EXIT_FAILURE;
pb.mm = pb_mm_init(&pb);
ui_timer_disable(&pb.cui->timer);
cui_result = cui_run(pb.cui, pb.mm, 0);
pmenu_delete(pb.mm);
talloc_free(pb.cui);
pb_log("--- end ---\n");
return cui_result ? EXIT_FAILURE : EXIT_SUCCESS;
}
|