#include "efiboot.h"
#include <lib/libsa/dev_net.h>
#include <lib/libsa/net.h>
#include <biosdisk.h>
#include "devopen.h"
#include <bootinfo.h>
#include "efidisk.h"
static int
dev2bios(char *devname, int unit, int *biosdev)
{
if (strcmp(devname, "hd") == 0)
*biosdev = 0x80 + unit;
else if (strcmp(devname, "cd") == 0)
*biosdev = 0x80 + get_harddrives() + unit;
else
return ENXIO;
return 0;
}
void
bios2dev(int biosdev, daddr_t sector, char **devname, int *unit,
int *partition, const char **part_name)
{
static char savedevname[MAXDEVNAME+1];
*unit = biosdev & 0x7f;
if (efi_bootdp_type == BOOT_DEVICE_TYPE_NET) {
*devname = "net";
*unit = efi_net_get_booted_interface_unit();
if (*unit < 0)
*unit = 0;
*partition = 0;
return;
} else if (biosdev >= 0x80 + get_harddrives()) {
*devname = "cd";
*unit -= get_harddrives();
} else
*devname = "hd";
(void)biosdisk_findpartition(biosdev, sector, partition, part_name);
if (part_name != NULL && *part_name != NULL) {
snprintf(savedevname, sizeof(savedevname),
"NAME=%s", *part_name);
*devname = savedevname;
}
}
#if defined(SUPPORT_NFS) || defined(SUPPORT_TFTP)
const struct netboot_fstab *
netboot_fstab_find(const char *name)
{
int i;
if (strcmp(name, "net") == 0)
return &netboot_fstab[0];
for (i = 0; i < nnetboot_fstab; i++) {
if (strcmp(name, netboot_fstab[i].name) == 0)
return &netboot_fstab[i];
}
return NULL;
}
static const struct netboot_fstab *
netboot_fstab_findn(const char *name, size_t len)
{
int i;
if (strncmp(name, "net", len) == 0)
return &netboot_fstab[0];
for (i = 0; i < nnetboot_fstab; i++) {
if (strncmp(name, netboot_fstab[i].name, len) == 0)
return &netboot_fstab[i];
}
return NULL;
}
#endif
struct btinfo_bootpath bibp;
extern bool kernel_loaded;
int
devopen(struct open_file *f, const char *fname, char **file)
{
char *fsname, *devname;
const char *xname = NULL;
int unit, partition;
int biosdev;
int error;
#if defined(SUPPORT_NFS) || defined(SUPPORT_TFTP)
struct devdesc desc;
const struct netboot_fstab *nf;
char *filename;
size_t fsnamelen;
int i, n;
#endif
error = parsebootfile(fname, &fsname, &devname, &unit, &partition,
(const char **) file);
if (error)
return error;
memcpy(file_system, file_system_disk,
sizeof(struct fs_ops) * nfsys_disk);
nfsys = nfsys_disk;
if (strstr(devname, "NAME=") == devname)
xname = devname;
if (strstr(devname, "raid") == devname)
xname = fname;
if (xname != NULL) {
f->f_dev = &devsw[0];
if (!kernel_loaded) {
strncpy(bibp.bootpath, *file, sizeof(bibp.bootpath));
BI_ADD(&bibp, BTINFO_BOOTPATH, sizeof(bibp));
}
error = biosdisk_open_name(f, xname);
return error;
}
#if defined(SUPPORT_NFS) || defined(SUPPORT_TFTP)
nf = netboot_fstab_find(devname);
if (nf != NULL) {
n = 0;
if (strcmp(devname, "net") == 0) {
for (i = 0; i < nnetboot_fstab; i++) {
memcpy(&file_system[n++], netboot_fstab[i].ops,
sizeof(struct fs_ops));
}
} else {
memcpy(&file_system[n++], nf->ops,
sizeof(struct fs_ops));
}
nfsys = n;
#ifdef SUPPORT_BOOTP
try_bootp = 1;
#endif
if (fname) {
filename = strchr(fname, ':');
if (filename != NULL)
filename++;
else
filename = (char *)fname;
strlcpy(bootfile, filename, sizeof(bootfile));
}
memset(&desc, 0, sizeof(desc));
strlcpy(desc.d_name, "net", sizeof(desc.d_name));
desc.d_unit = unit;
f->f_dev = &devsw[1];
if (!kernel_loaded) {
strncpy(bibp.bootpath, *file, sizeof(bibp.bootpath));
BI_ADD(&bibp, BTINFO_BOOTPATH, sizeof(bibp));
}
error = DEV_OPEN(f->f_dev)(f, &desc);
if (error)
return error;
filename = strchr(bootfile, ':');
if (filename != NULL) {
fname = bootfile;
fsnamelen = filename - fname;
nf = netboot_fstab_findn(fname, fsnamelen);
if (nf == NULL ||
strncmp(fname, "net", fsnamelen) == 0) {
printf("Invalid file system type specified in "
"%s\n", fname);
error = EINVAL;
goto neterr;
}
memcpy(file_system, nf->ops, sizeof(struct fs_ops));
nfsys = 1;
}
filename = fname ? strchr(fname, ':') : NULL;
if (filename != NULL) {
filename++;
if (*filename == '\0') {
printf("No file specified in %s\n", fname);
error = EINVAL;
goto neterr;
}
} else
filename = (char *)fname;
*file = filename;
return 0;
neterr:
DEV_CLOSE(f->f_dev)(f);
f->f_dev = NULL;
return error;
}
#endif
if (strcmp(devname, "esp") == 0) {
bios2dev(boot_biosdev, boot_biossector, &devname, &unit,
&partition, NULL);
if (efidisk_get_efi_system_partition(boot_biosdev, &partition))
return ENXIO;
}
error = dev2bios(devname, unit, &biosdev);
if (error)
return error;
f->f_dev = &devsw[0];
if (!kernel_loaded) {
strncpy(bibp.bootpath, *file, sizeof(bibp.bootpath));
BI_ADD(&bibp, BTINFO_BOOTPATH, sizeof(bibp));
}
return biosdisk_open(f, biosdev, partition);
}