#include <sys/types.h>
#include <sys/reboot.h>
#include <sys/bootblock.h>
#include <sys/boot_flag.h>
#include <lib/libsa/stand.h>
#include <lib/libsa/loadfile.h>
#include <lib/libsa/ufs.h>
#include <lib/libkern/libkern.h>
#include "biosdisk.h"
#include "boot.h"
#include "bootinfo.h"
#include "cons.h"
int errno;
extern struct landisk_boot_params boot_params;
static const char * const names[][2] = {
{ "netbsd", "netbsd.gz" },
{ "netbsd.old", "netbsd.old.gz", },
{ "onetbsd", "onetbsd.gz" },
};
#define NUMNAMES (sizeof(names) / sizeof(names[0]))
#define DEFFILENAME names[0][0]
#define MAXDEVNAME 16
static char *default_devname;
static uint default_unit, default_partition;
static const char *default_filename;
char *sprint_bootsel(const char *filename);
void bootit(const char *filename, int howto, int tell);
void print_banner(void);
void boot2(uint32_t boot_biossector);
int exec_netbsd(const char *file, int howto);
static char *gettrailer(char *arg);
static int parseopts(const char *opts, int *howto);
static int parseboot(char *arg, char **filename, int *howto);
void bootmenu(void);
static void bootcmd_help(char *);
static void bootcmd_ls(char *);
static void bootcmd_quit(char *);
static void bootcmd_halt(char *);
static void bootcmd_boot(char *);
static void bootcmd_monitor(char *);
static const struct bootblk_command {
const char *c_name;
void (*c_fn)(char *arg);
} bootcmds[] = {
{ "help", bootcmd_help },
{ "?", bootcmd_help },
{ "ls", bootcmd_ls },
{ "quit", bootcmd_quit },
{ "halt", bootcmd_halt },
{ "boot", bootcmd_boot },
{ "!", bootcmd_monitor },
{ NULL, NULL },
};
int
parsebootfile(const char *fname, char **devname,
uint *unit, uint *partition, const char **file)
{
const char *col;
*devname = default_devname;
*unit = default_unit;
*partition = default_partition;
*file = default_filename;
if (fname == NULL)
return (0);
if((col = strchr(fname, ':'))) {
static char savedevname[MAXDEVNAME+1];
int devlen;
unsigned int u = 0, p = 0;
int i = 0;
devlen = col - fname;
if (devlen > MAXDEVNAME)
return (EINVAL);
#define isvalidname(c) ((c) >= 'a' && (c) <= 'z')
if (!isvalidname(fname[i]))
return (EINVAL);
do {
savedevname[i] = fname[i];
i++;
} while (isvalidname(fname[i]));
savedevname[i] = '\0';
#define isnum(c) ((c) >= '0' && (c) <= '9')
if (i < devlen) {
if (!isnum(fname[i]))
return (EUNIT);
do {
u *= 10;
u += fname[i++] - '0';
} while (isnum(fname[i]));
}
#define isvalidpart(c) ((c) >= 'a' && (c) <= 'p')
if (i < devlen) {
if (!isvalidpart(fname[i]))
return (EPART);
p = fname[i++] - 'a';
}
if (i != devlen)
return (ENXIO);
*devname = savedevname;
*unit = u;
*partition = p;
fname = col + 1;
}
if (*fname)
*file = fname;
return (0);
}
char *
sprint_bootsel(const char *filename)
{
static char buf[80];
char *devname;
uint unit, partition;
const char *file;
if (parsebootfile(filename, &devname, &unit, &partition, &file) == 0) {
snprintf(buf, sizeof(buf), "%s%d%c:%s", devname, unit,
'a' + partition, file);
return (buf);
}
return ("(invalid)");
}
void
bootit(const char *filename, int howto, int tell)
{
if (tell) {
printf("booting %s", sprint_bootsel(filename));
if (howto)
printf(" (howto 0x%x)", howto);
printf("\n");
}
if (exec_netbsd(filename, howto) < 0) {
printf("boot: %s: %s\n", sprint_bootsel(filename),
strerror(errno));
} else {
printf("boot returned\n");
}
}
void
print_banner(void)
{
extern const char bootprog_name[];
extern const char bootprog_rev[];
printf("\n");
printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
}
void
boot2(uint32_t boot_biossector)
{
int currname;
int c;
tick_init();
cninit(boot_params.bp_consdev);
print_banner();
bios2dev(0x40, &default_devname, &default_unit,
boot_biossector, &default_partition);
default_filename = DEFFILENAME;
printf("Press return to boot now, any other key for boot menu\n");
currname = 0;
for (;;) {
printf("booting %s - starting in ",
sprint_bootsel(names[currname][0]));
c = awaitkey(boot_params.bp_timeout, 1);
if ((c != '\r') && (c != '\n') && (c != '\0')) {
printf("type \"?\" or \"help\" for help.\n");
bootmenu();
}
bootit(names[currname][0], 0, 0);
bootit(names[currname][1], 0, 1);
currname = (currname + 1) % NUMNAMES;
}
}
int
exec_netbsd(const char *file, int howto)
{
static char bibuf[BOOTINFO_MAXSIZE];
u_long marks[MARK_MAX];
int fd;
BI_ALLOC(6);
marks[MARK_START] = 0;
if ((fd = loadfile(file, marks, LOAD_KERNEL)) == -1)
goto out;
printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
{
struct btinfo_common *help;
char *p;
int i;
p = bibuf;
memcpy(p, &bootinfo->nentries, sizeof(bootinfo->nentries));
p += sizeof(bootinfo->nentries);
for (i = 0; i < bootinfo->nentries; i++) {
help = (struct btinfo_common *)(bootinfo->entry[i]);
memcpy(p, help, help->len);
p += help->len;
}
}
cache_flush();
cache_disable();
(*(void (*)(int, void *))marks[MARK_ENTRY])(howto, bibuf);
panic("exec returned");
out:
BI_FREE();
bootinfo = 0;
return (-1);
}
static void
bootcmd_help(char *arg)
{
printf("commands are:\n"
"boot [xdNx:][filename] [-acdqsv]\n"
" (ex. \"hd0a:netbsd.old -s\"\n"
"ls [path]\n"
"help|?\n"
"halt\n"
"quit\n");
}
static void
bootcmd_ls(char *arg)
{
const char *save = default_filename;
default_filename = "/";
ls(arg);
default_filename = save;
}
static void
bootcmd_quit(char *arg)
{
printf("Exiting...\n");
delay(1000);
reboot();
panic("Could not reboot!");
exit(0);
}
static void
bootcmd_halt(char *arg)
{
printf("Exiting...\n");
delay(1000);
halt();
panic("Could not halt!");
exit(0);
}
static void
bootcmd_boot(char *arg)
{
char *filename;
int howto;
if (parseboot(arg, &filename, &howto)) {
bootit(filename, howto, 1);
}
}
static void
bootcmd_monitor(char *arg)
{
db_monitor();
printf("\n");
}
static void
docommand(const struct bootblk_command * const cmds, char *arg)
{
char *options;
int i;
options = gettrailer(arg);
for (i = 0; cmds[i].c_name != NULL; i++) {
if (strcmp(arg, cmds[i].c_name) == 0) {
(*cmds[i].c_fn)(options);
return;
}
}
printf("unknown command\n");
bootcmd_help(NULL);
}
void
bootmenu(void)
{
char input[256];
char *c;
for (;;) {
c = input;
input[0] = '\0';
printf("> ");
kgets(input, sizeof(input));
while (*c == ' ') {
c++;
}
if (*c != '\0') {
docommand(bootcmds, c);
}
}
}
static char *
gettrailer(char *arg)
{
char *options;
if ((options = strchr(arg, ' ')) == NULL)
return ("");
else
*options++ = '\0';
while (*options == ' ')
options++;
return (options);
}
static int
parseopts(const char *opts, int *howto)
{
int r, tmpopt = 0;
opts++;
while (*opts && *opts != ' ') {
r = 0;
BOOT_FLAG(*opts, r);
if (r == 0) {
printf("-%c: unknown flag\n", *opts);
bootcmd_help(NULL);
return (0);
}
tmpopt |= r;
opts++;
}
*howto = tmpopt;
return (1);
}
static int
parseboot(char *arg, char **filename, int *howto)
{
char *opts = NULL;
*filename = 0;
*howto = 0;
if (*arg == '\0')
return (1);
if (arg[0] == '-') {
opts = arg;
} else {
*filename = arg;
opts = gettrailer(arg);
if (*opts == '\0') {
opts = NULL;
} else if (*opts != '-') {
printf("invalid arguments\n");
bootcmd_help(NULL);
return (0);
}
}
if (opts) {
if (parseopts(opts, howto) == 0) {
return (0);
}
}
return (1);
}
int raise(int sig);
int
raise(int sig)
{
return 0;
}