#include <lib/libsa/stand.h>
#include <lib/libsa/loadfile.h>
#include <lib/libsa/dev_net.h>
#include <lib/libkern/libkern.h>
#include <sys/param.h>
#include <sys/boot_flag.h>
#include <sys/exec.h>
#include <sys/exec_elf.h>
#include <machine/cpu.h>
#include <cobalt/dev/gtreg.h>
#include "boot.h"
#include "cons.h"
#include "common.h"
#include "bootinfo.h"
char *kernelnames[] = {
"netbsd",
"netbsd.gz",
"onetbsd",
"onetbsd.gz",
"netbsd.bak",
"netbsd.bak.gz",
"netbsd.old",
"netbsd.old.gz",
"netbsd.cobalt",
"netbsd.cobalt.gz",
"netbsd.elf",
"netbsd.elf.gz",
NULL
};
u_int cobalt_id;
static const char * const cobalt_model[] =
{
[0] = "Unknown Cobalt",
[COBALT_ID_QUBE2700] = "Cobalt Qube 2700",
[COBALT_ID_RAQ] = "Cobalt RaQ",
[COBALT_ID_QUBE2] = "Cobalt Qube 2",
[COBALT_ID_RAQ2] = "Cobalt RaQ 2"
};
#define COBALT_MODELS __arraycount(cobalt_model)
extern u_long end;
void start(void);
static char *bootstring;
static int patch_bootstring(char *bootspec);
static int get_bsdbootname(char **, char **, int *);
static int parse_bootname(char *, int, char **, char **);
static void prominit(unsigned int memsize);
static void print_banner(unsigned int memsize);
static u_int read_board_id(void);
void cpu_reboot(void);
int main(unsigned int memsize);
void
cpu_reboot(void)
{
printf("rebooting...\n\n");
*(volatile uint8_t *)MIPS_PHYS_TO_KSEG1(LED_ADDR) = LED_RESET;
printf("WARNING: reboot failed!\n");
for (;;)
;
}
int
patch_bootstring(char *bootspec)
{
char *sp = bootstring;
uint8_t unit, part;
int dev;
char *file;
DPRINTF(("patch_bootstring: %s\n", bootspec));
if (devparse(bootspec, &dev, &unit, &part, (const char **)&file) != 0)
unit = part = 0;
DPRINTF(("patch_bootstring: unit = %d, part = %d\n", unit, part));
if ((sp = strstr(bootstring, "root=")) != NULL) {
const char *end;
end = strchr(sp, ' ');
for (--sp; (sp > bootstring) && (*sp == ' '); --sp)
;
if (end != NULL)
strcpy(++sp, end);
else
*++sp = '\0';
}
DPRINTF(("patch_bootstring: [%s]\n", bootstring));
#define DEVNAMESIZE (MAXDEVNAME + sizeof(" root=/dev/hd") + sizeof("0a"))
if (strcmp(devsw[dev].dv_name, "wd") == 0 &&
strlen(bootstring) <= (511 - DEVNAMESIZE)) {
int len;
if ((sp = strstr(bootstring, "nfsroot=")) != NULL) {
const char *end;
end = strchr(sp, ' ');
for (--sp; (sp > bootstring) && (*sp == ' '); --sp)
;
if (end != NULL)
strcpy(++sp, end);
else
*++sp = '\0';
}
strcat(bootstring, " root=/dev/hd");
len = strlen(bootstring);
bootstring[len++] = unit + 'a';
bootstring[len++] = part + '1';
bootstring[len++] = '\0';
}
DPRINTF(("patch_bootstring: -> %s\n", bootstring));
return 0;
}
static int
get_bsdbootname(char **dev, char **kname, int *howtop)
{
int len;
int bootunit, bootpart;
char *bootstr_dev, *bootstr_kname;
char *prompt_dev, *prompt_kname;
char *ptr, *spec;
char c, namebuf[PATH_MAX];
static char bootdev[] = "wd0a";
static char nfsbootdev[] = "nfs";
bootstr_dev = prompt_dev = NULL;
bootstr_kname = prompt_kname = NULL;
spec = bootstring;
while ((spec = strstr(spec, "root=")) != NULL) {
spec += 5;
ptr = strchr(spec, ' ');
len = (ptr == NULL) ? strlen(spec) : ptr - spec;
if (len == 9 && memcmp("/dev/hd", spec, 7) == 0) {
bootunit = spec[7] - 'a';
bootpart = spec[8] - '1';
if (bootunit >= 0 && bootunit < 2 &&
bootpart >= 0 && bootpart < 4) {
bootdev[sizeof(bootdev) - 3] = '0' + bootunit;
#if 0
bootdev[sizeof(bootdev) - 2] = 'a' + bootpart;
#endif
bootstr_dev = bootdev;
}
}
spec += len;
}
if ((spec = strstr(bootstring, "nbsd=")) != NULL) {
ptr = strchr(spec, ' ');
spec += 5;
len = (ptr == NULL) ? strlen(spec) : ptr - spec;
if (len > 0) {
if (parse_bootname(spec, len,
&bootstr_dev, &bootstr_kname))
return 1;
}
}
if (strstr(bootstring, "nfsroot=") != NULL) {
bootstr_dev = nfsbootdev;
}
DPRINTF(("bootstr_dev = %s, bootstr_kname = %s\n",
bootstr_dev ? bootstr_dev : "<NULL>",
bootstr_kname ? bootstr_kname : "<NULL>"));
spec = NULL;
len = 0;
memset(namebuf, 0, sizeof namebuf);
printf("Boot [%s:%s]: ",
bootstr_dev ? bootstr_dev : DEFBOOTDEV,
bootstr_kname ? bootstr_kname : DEFKERNELNAME);
if (tgets(namebuf) == -1)
printf("\n");
ptr = namebuf;
while ((c = *ptr) != '\0') {
while (c == ' ')
c = *++ptr;
if (c == '\0')
break;
if (c == '-') {
while ((c = *++ptr) && c != ' ')
BOOT_FLAG(c, *howtop);
} else {
spec = ptr;
while ((c = *++ptr) && c != ' ')
;
if (c)
*ptr++ = '\0';
len = strlen(spec);
}
}
if (len > 0) {
if (parse_bootname(spec, len, &prompt_dev, &prompt_kname))
return 1;
}
DPRINTF(("prompt_dev = %s, prompt_kname = %s\n",
prompt_dev ? prompt_dev : "<NULL>",
prompt_kname ? prompt_kname : "<NULL>"));
if (prompt_dev)
*dev = prompt_dev;
else
*dev = bootstr_dev;
if (prompt_kname)
*kname = prompt_kname;
else
*kname = bootstr_kname;
DPRINTF(("dev = %s, kname = %s\n",
*dev ? *dev : "<NULL>",
*kname ? *kname : "<NULL>"));
return 0;
}
static int
parse_bootname(char *spec, int len, char **dev, char **kname)
{
char *bootname, *ptr;
bootname = alloc(len + 1);
if (bootname == NULL)
return 1;
memcpy(bootname, spec, len);
bootname[len] = '\0';
if ((ptr = memchr(bootname, ':', len)) != NULL) {
*ptr = '\0';
*dev = bootname;
if (*++ptr)
*kname = ptr;
} else
*kname = bootname;
return 0;
}
void
prominit(unsigned int memsize)
{
bootstring = (char *)(memsize - 512);
bootstring[511] = '\0';
}
void
print_banner(unsigned int memsize)
{
lcd_banner();
printf("\n");
printf(">> %s " NETBSD_VERS " Bootloader, Revision %s [@%p]\n",
bootprog_name, bootprog_rev, (void*)&start);
printf(">> Model:\t\t%s\n", cobalt_model[cobalt_id]);
printf(">> Memory:\t\t%lu k\n", (memsize - MIPS_KSEG0_START) / 1024);
printf(">> PROM boot string:\t%s\n", bootstring);
}
u_int
read_board_id(void)
{
uint32_t tag, reg;
#define PCIB_PCI_BUS 0
#define PCIB_PCI_DEV 9
#define PCIB_PCI_FUNC 0
#define PCIB_BOARD_ID_REG 0x94
#define COBALT_BOARD_ID(reg) ((reg & 0x000000f0) >> 4)
tag = (PCIB_PCI_BUS << 16) | (PCIB_PCI_DEV << 11) |
(PCIB_PCI_FUNC << 8);
reg = pcicfgread(tag, PCIB_BOARD_ID_REG);
return COBALT_BOARD_ID(reg);
}
int
main(unsigned int memsize)
{
char **namep, *dev, *kernel, *bi_addr;
char bootpath[PATH_MAX];
int win;
u_long marks[MARK_MAX];
void (*entry)(unsigned int, u_int, char *);
struct btinfo_flags bi_flags;
struct btinfo_symtab bi_syms;
struct btinfo_bootpath bi_bpath;
struct btinfo_howto bi_howto;
int addr, speed, howto;
try_bootp = 1;
dev = NULL;
kernel = NULL;
howto = 0x0;
bi_flags.bi_flags = 0x0;
bi_addr = bi_init();
lcd_init();
cobalt_id = read_board_id();
prominit(memsize);
if (cninit(&addr, &speed) != NULL)
bi_flags.bi_flags |= BI_SERIAL_CONSOLE;
print_banner(memsize);
memset(marks, 0, sizeof marks);
get_bsdbootname(&dev, &kernel, &howto);
if (kernel != NULL) {
DPRINTF(("kernel: %s\n", kernel));
kernelnames[0] = kernel;
kernelnames[1] = NULL;
} else {
DPRINTF(("kernel: NULL\n"));
}
win = 0;
DPRINTF(("Kernel names: %p\n", kernelnames));
for (namep = kernelnames, win = 0; (*namep != NULL) && !win; namep++) {
kernel = *namep;
bootpath[0] = '\0';
strcpy(bootpath, dev ? dev : DEFBOOTDEV);
strcat(bootpath, ":");
strcat(bootpath, kernel);
lcd_loadfile(bootpath);
printf("Loading: %s", bootpath);
if (howto)
printf(" (howto 0x%x)", howto);
printf("\n");
patch_bootstring(bootpath);
win = (loadfile(bootpath, marks, LOAD_ALL) != -1);
}
if (win) {
strncpy(bi_bpath.bootpath, kernel, BTINFO_BOOTPATH_LEN);
bi_add(&bi_bpath, BTINFO_BOOTPATH, sizeof(bi_bpath));
entry = (void *)marks[MARK_ENTRY];
bi_syms.nsym = marks[MARK_NSYM];
bi_syms.ssym = marks[MARK_SYM];
bi_syms.esym = marks[MARK_END];
bi_add(&bi_syms, BTINFO_SYMTAB, sizeof(bi_syms));
bi_add(&bi_flags, BTINFO_FLAGS, sizeof(bi_flags));
bi_howto.bi_howto = howto;
bi_add(&bi_howto, BTINFO_HOWTO, sizeof(bi_howto));
entry = (void *)marks[MARK_ENTRY];
DPRINTF(("Bootinfo @ 0x%lx\n", (u_long)bi_addr));
printf("Starting at 0x%lx\n\n", (u_long)entry);
(*entry)(memsize, BOOTINFO_MAGIC, bi_addr);
}
delay(20000);
lcd_failed();
(void)printf("Boot failed! Rebooting...\n");
return 0;
}