#include "sd.h"
#include "mpath.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/buf.h>
#include <sys/disklabel.h>
#include <sys/conf.h>
#include <sys/reboot.h>
#include <sys/device.h>
#include <dev/cons.h>
#include <uvm/uvm_extern.h>
#include <machine/autoconf.h>
#include <sys/disk.h>
#include <scsi/scsi_all.h>
#include <scsi/scsiconf.h>
#include <dev/ata/atavar.h>
#if NMPATH > 0
#include <scsi/mpathvar.h>
#endif
void dumpconf(void);
static struct devmap *findtype(char **);
void parseofwbp(char *);
int getpno(char **);
int cold = 1;
char bootdev[16];
struct device *bootdv = NULL;
enum devclass bootdev_class = DV_DULL;
int bootdev_type = 0;
int bootdev_unit = 0;
void
cpu_configure(void)
{
(void)splhigh();
softintr_init();
if (config_rootfound("mainbus", "mainbus") == 0)
panic("no mainbus found");
(void)spl0();
cold = 0;
}
struct devmap {
char *att;
char *dev;
int type;
};
#define T_IFACE 0x10
#define T_BUS 0x00
#define T_SCSI 0x11
#define T_IDE 0x12
#define T_DISK 0x21
static struct devmap *
findtype(char **s)
{
static struct devmap devmap[] = {
{ "/ht", NULL, T_BUS },
{ "/ht@", NULL, T_BUS },
{ "/pci@", NULL, T_BUS },
{ "/pci", NULL, T_BUS },
{ "/AppleKiwi@", NULL, T_BUS },
{ "/AppleKiwi", NULL, T_BUS },
{ "/mac-io@", NULL, T_BUS },
{ "/mac-io", NULL, T_BUS },
{ "/@", NULL, T_BUS },
{ "/LSILogic,sas@", "sd", T_SCSI },
{ "/scsi@", "sd", T_SCSI },
{ "/ide", "wd", T_IDE },
{ "/ata", "wd", T_IDE },
{ "/k2-sata-root", NULL, T_BUS },
{ "/k2-sata", "wd", T_IDE },
{ "/disk@", "sd", T_DISK },
{ "/disk", "wd", T_DISK },
{ "/usb@", "sd", T_SCSI },
{ "/ADPT,2940U2B@", "sd", T_SCSI },
{ "/bcom5704@4", "bge0", T_IFACE },
{ "/bcom5704@4,1", "bge1", T_IFACE },
{ "/ethernet", "gem0", T_IFACE },
{ "/enet", "mc0", T_IFACE },
{ NULL, NULL }
};
struct devmap *dp = &devmap[0];
while (dp->att) {
if (strncmp(*s, dp->att, strlen(dp->att)) == 0) {
*s += strlen(dp->att);
break;
}
dp++;
}
if (dp->att == NULL)
printf("string [%s] not found\n", *s);
return(dp);
}
void
parseofwbp(char *bp)
{
int ptype;
char *dev, *cp;
struct devmap *dp;
cp = bp;
do {
while(*cp && *cp != '/')
cp++;
dp = findtype(&cp);
if (!dp->att) {
printf("Warning: bootpath unrecognized: %s\n", bp);
return;
}
} while((dp->type & T_IFACE) == 0);
if (dp->att && dp->type == T_IFACE) {
bootdev_class = DV_IFNET;
bootdev_type = dp->type;
strlcpy(bootdev, dp->dev, sizeof bootdev);
return;
}
dev = dp->dev;
while(*cp && *cp != '/')
cp++;
ptype = dp->type;
dp = findtype(&cp);
if (dp->att && dp->type == T_DISK) {
bootdev_class = DV_DISK;
bootdev_type = ptype;
bootdev_unit = getpno(&cp);
return;
}
printf("Warning: boot device unrecognized: %s\n", bp);
}
int
getpno(char **cp)
{
int val = 0, digit;
char *cx = *cp;
while (*cx) {
if (*cx >= '0' && *cx <= '9')
digit = *cx - '0';
else if (*cx >= 'a' && *cx <= 'f')
digit = *cx - 'a' + 0x0a;
else
break;
val = val * 16 + digit;
cx++;
}
*cp = cx;
return (val);
}
void
device_register(struct device *dev, void *aux)
{
#if NSD > 0
extern struct cfdriver scsibus_cd;
#endif
const char *drvrname = dev->dv_cfdata->cf_driver->cd_name;
const char *name = dev->dv_xname;
if (bootdv != NULL || dev->dv_class != bootdev_class)
return;
switch (bootdev_type) {
#if NSD > 0
case T_SCSI:
if (dev->dv_parent->dv_cfdata->cf_driver == &scsibus_cd) {
struct scsi_attach_args *sa = aux;
if (sa->sa_sc_link->target == bootdev_unit)
bootdv = dev;
}
#endif
case T_IDE:
if (strcmp(drvrname, "wd") == 0) {
struct ata_atapi_attach *aa = aux;
if (aa->aa_drv_data->drive == bootdev_unit)
bootdv = dev;
}
break;
case T_IFACE:
if (strcmp(name, bootdev) == 0)
bootdv = dev;
break;
default:
break;
}
}
void
diskconf(void)
{
printf("bootpath: %s\n", bootpath);
#if NMPATH > 0
if (bootdv != NULL)
bootdv = mpath_bootdv(bootdv);
#endif
setroot(bootdv, 0, RB_USERREQ);
dumpconf();
}
const struct nam2blk nam2blk[] = {
{ "wd", 0 },
{ "sd", 2 },
{ "cd", 3 },
{ "vnd", 14 },
{ "rd", 17 },
{ NULL, -1 }
};