2 * Petitboot generic ncurses bootloader UI
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
21 #if defined(HAVE_CONFIG_H)
34 #include "talloc/talloc.h"
35 #include "waiter/waiter.h"
36 #include "ui/common/discover-client.h"
39 static void print_version(void)
41 printf("petitboot-nc (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
44 static void print_usage(void)
48 "Usage: petitboot-nc [-d, --dry-run] [-h, --help] [-l, --log log-file]\n"
49 " [-s, --start-daemon] [-V, --version]\n");
53 * enum opt_value - Tri-state options variables.
56 enum opt_value {opt_undef = 0, opt_yes, opt_no};
59 * struct opts - Values from command line options.
63 enum opt_value dry_run;
64 enum opt_value show_help;
66 enum opt_value start_daemon;
67 enum opt_value show_version;
71 * opts_parse - Parse the command line options.
74 static int opts_parse(struct opts *opts, int argc, char *argv[])
76 static const struct option long_options[] = {
77 {"dry-run", no_argument, NULL, 'd'},
78 {"help", no_argument, NULL, 'h'},
79 {"log", required_argument, NULL, 'l'},
80 {"start-daemon", no_argument, NULL, 's'},
81 {"version", no_argument, NULL, 'V'},
84 static const char short_options[] = "dhl:sV";
85 static const struct opts default_values = {
86 .log_file = "/var/log/petitboot/petitboot-nc.log",
89 *opts = default_values;
92 int c = getopt_long(argc, argv, short_options, long_options,
100 opts->dry_run = opt_yes;
103 opts->show_help = opt_yes;
106 opts->log_file = optarg;
109 opts->start_daemon = opt_yes;
112 opts->show_version = opt_yes;
115 opts->show_help = opt_yes;
124 * struct pb_cui - Main cui program instance.
126 * @svm: Set video mode menu.
135 static struct pb_cui *pb_from_cui(struct cui *cui)
139 assert(cui->c_sig == pb_cui_sig);
140 pb = cui->platform_info;
141 assert(pb->cui->c_sig == pb_cui_sig);
146 * pb_kexec_cb - The kexec callback.
149 static int pb_kexec_cb(struct cui *cui, struct cui_opt_data *cod)
151 struct pb_cui *pb = pb_from_cui(cui);
153 pb_log("%s: %s\n", __func__, cod->name);
155 assert(pb->cui->current == &pb->cui->main->scr);
157 return pb_run_kexec(cod->kd, pb->opts.dry_run);
161 * pb_mm_init - Setup the main menu instance.
164 static struct pmenu *pb_mm_init(struct pb_cui *pb_cui)
168 struct pmenu_item *i;
170 m = pmenu_init(pb_cui->cui, 1, cui_on_exit);
173 pb_log("%s: failed\n", __func__);
177 m->on_open = cui_on_open;
179 m->scr.frame.title = talloc_strdup(m, "Petitboot");
180 m->scr.frame.help = talloc_strdup(m,
181 "ESC=exit, Enter=accept, e=edit, o=open");
182 m->scr.frame.status = talloc_strdup(m, "Welcome to Petitboot");
184 i = pmenu_item_init(m, 0, "Exit to Shell");
185 i->on_execute = pmenu_exit_cb;
187 result = pmenu_setup(m);
190 pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
195 menu_opts_off(m->ncm, O_SHOWDESC);
196 set_menu_mark(m->ncm, " *");
197 set_current_item(m->ncm, i->nci);
206 static struct pb_cui pb;
208 static void sig_handler(int signum)
210 DBGS("%d\n", signum);
215 ui_timer_sigalrm(&pb.cui->timer);
222 assert(0 && "unknown sig");
234 * main - cui bootloader main routine.
237 int main(int argc, char *argv[])
239 static struct sigaction sa;
243 result = opts_parse(&pb.opts, argc, argv);
250 if (pb.opts.show_help == opt_yes) {
255 if (pb.opts.show_version == opt_yes) {
260 if (strcmp(pb.opts.log_file, "-")) {
261 FILE *log = fopen(pb.opts.log_file, "a");
264 pb_log_set_stream(log);
266 pb_log_set_stream(stderr);
269 pb_log_always_flush(1);
272 pb_log("--- petitboot-nc ---\n");
274 sa.sa_handler = sig_handler;
275 result = sigaction(SIGALRM, &sa, NULL);
276 result += sigaction(SIGHUP, &sa, NULL);
277 result += sigaction(SIGINT, &sa, NULL);
278 result += sigaction(SIGTERM, &sa, NULL);
279 result += sigaction(SIGWINCH, &sa, NULL);
282 pb_log("%s sigaction failed.\n", __func__);
286 pb.cui = cui_init(&pb, pb_kexec_cb, NULL, pb.opts.start_daemon);
291 pb.mm = pb_mm_init(&pb);
292 ui_timer_disable(&pb.cui->timer);
294 cui_result = cui_run(pb.cui, pb.mm, 0);
300 pb_log("--- end ---\n");
302 return cui_result ? EXIT_FAILURE : EXIT_SUCCESS;