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)
35 #include "talloc/talloc.h"
36 #include "waiter/waiter.h"
37 #include "ui/common/discover-client.h"
40 extern const char *main_menu_help_text;
42 static void print_version(void)
44 printf("petitboot-nc (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
47 static void print_usage(void)
51 "Usage: petitboot-nc [-h, --help] [-l, --log log-file]\n"
52 " [-s, --start-daemon] [-v, --verbose] [-V, --version]\n");
56 * enum opt_value - Tri-state options variables.
59 enum opt_value {opt_undef = 0, opt_yes, opt_no};
62 * struct opts - Values from command line options.
66 enum opt_value show_help;
68 enum opt_value start_daemon;
69 enum opt_value verbose;
70 enum opt_value show_version;
74 * opts_parse - Parse the command line options.
77 static int opts_parse(struct opts *opts, int argc, char *argv[])
79 static const struct option long_options[] = {
80 {"help", no_argument, NULL, 'h'},
81 {"log", required_argument, NULL, 'l'},
82 {"start-daemon", no_argument, NULL, 's'},
83 {"verbose", no_argument, NULL, 'v'},
84 {"version", no_argument, NULL, 'V'},
87 static const char short_options[] = "dhl:svV";
88 static const struct opts default_values = { 0 };
90 *opts = default_values;
93 int c = getopt_long(argc, argv, short_options, long_options,
101 opts->show_help = opt_yes;
104 opts->log_file = optarg;
107 opts->start_daemon = opt_yes;
110 opts->verbose = opt_yes;
113 opts->show_version = opt_yes;
116 opts->show_help = opt_yes;
124 static char *default_log_filename(void)
126 const char *base = "/var/log/petitboot/petitboot-nc";
127 static char name[PATH_MAX];
131 tty = ttyname(STDIN_FILENO);
134 if (tty && !strncmp(tty, "/dev/", 5))
137 /* change slashes to hyphens */
138 for (i = 0; tty && tty[i]; i++)
145 snprintf(name, sizeof(name), "%s.%s.log", base, tty);
150 * struct pb_cui - Main cui program instance.
152 * @svm: Set video mode menu.
160 static int pmenu_sysinfo(struct pmenu_item *item)
162 cui_show_sysinfo(cui_from_item(item));
166 static int pmenu_config(struct pmenu_item *item)
168 cui_show_config(cui_from_item(item));
172 static int pmenu_reinit(struct pmenu_item *item)
174 cui_send_reinit(cui_from_item(item));
179 * pb_mm_init - Setup the main menu instance.
182 static struct pmenu *pb_mm_init(struct pb_cui *pb_cui)
186 struct pmenu_item *i;
188 m = pmenu_init(pb_cui->cui, 5, cui_on_exit);
191 pb_log("%s: failed\n", __func__);
195 m->on_new = cui_item_new;
197 m->scr.frame.ltitle = talloc_asprintf(m,
198 "Petitboot (" PACKAGE_VERSION ")");
199 m->scr.frame.rtitle = NULL;
200 m->scr.frame.help = talloc_strdup(m,
201 "Enter=accept, e=edit, n=new, x=exit, h=help");
202 m->scr.frame.status = talloc_strdup(m, "Welcome to Petitboot");
204 i = pmenu_item_init(m, 0, " ");
205 item_opts_off(i->nci, O_SELECTABLE);
206 i = pmenu_item_init(m, 1, "System information");
207 i->on_execute = pmenu_sysinfo;
208 i = pmenu_item_init(m, 2, "System configuration");
209 i->on_execute = pmenu_config;
210 i = pmenu_item_init(m, 3, "Rescan devices");
211 i->on_execute = pmenu_reinit;
212 i = pmenu_item_init(m, 4, "Exit to shell");
213 i->on_execute = pmenu_exit_cb;
215 result = pmenu_setup(m);
218 pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
223 m->help_title = "main menu";
224 m->help_text = main_menu_help_text;
226 menu_opts_off(m->ncm, O_SHOWDESC);
227 set_menu_mark(m->ncm, " *");
228 set_current_item(m->ncm, i->nci);
237 static struct pb_cui pb;
239 static void sig_handler(int signum)
241 DBGS("%d\n", signum);
249 assert(0 && "unknown sig");
261 * main - cui bootloader main routine.
264 int main(int argc, char *argv[])
266 static struct sigaction sa;
267 const char *log_filename;
273 result = opts_parse(&opts, argc, argv);
280 if (opts.show_help == opt_yes) {
285 if (opts.show_version == opt_yes) {
291 log_filename = opts.log_file;
293 log_filename = default_log_filename();
296 if (strcmp(log_filename, "-")) {
297 log = fopen(log_filename, "a");
300 log = fopen("/dev/null", "a");
306 pb_log_set_debug(true);
308 pb_log("--- petitboot-nc ---\n");
310 sa.sa_handler = sig_handler;
311 result = sigaction(SIGALRM, &sa, NULL);
312 result += sigaction(SIGHUP, &sa, NULL);
313 result += sigaction(SIGINT, &sa, NULL);
314 result += sigaction(SIGTERM, &sa, NULL);
315 result += sigaction(SIGWINCH, &sa, NULL);
318 pb_log("%s sigaction failed.\n", __func__);
322 pb.cui = cui_init(&pb, NULL, opts.start_daemon);
327 pb.mm = pb_mm_init(&pb);
329 cui_result = cui_run(pb.cui, pb.mm, 0);
335 pb_log("--- end ---\n");
337 return cui_result ? EXIT_FAILURE : EXIT_SUCCESS;