Changeset View
Changeset View
Standalone View
Standalone View
sys/boot/efi/loader/devicename.c
Show All 25 Lines | |||||
*/ | */ | ||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
#include <stand.h> | #include <stand.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include <sys/disklabel.h> | #include <sys/disklabel.h> | ||||
#include <sys/param.h> | |||||
#include "bootstrap.h" | #include "bootstrap.h" | ||||
#ifdef EFI_ZFS_BOOT | |||||
#include "libzfs.h" | |||||
#endif | |||||
#include <efi.h> | #include <efi.h> | ||||
#include <efilib.h> | #include <efilib.h> | ||||
static int efi_parsedev(struct devdesc **, const char *, const char **); | static int efi_parsedev(struct devdesc **, const char *, const char **); | ||||
/* | /* | ||||
* Point (dev) at an allocated device specifier for the device matching the | * Point (dev) at an allocated device specifier for the device matching the | ||||
* path in (devspec). If it contains an explicit device specification, | * path in (devspec). If it contains an explicit device specification, | ||||
* use that. If not, use the default device. | * use that. If not, use the default device. | ||||
*/ | */ | ||||
int | int | ||||
efi_getdev(void **vdev, const char *devspec, const char **path) | efi_getdev(void **vdev, const char *devspec, const char **path) | ||||
{ | { | ||||
struct devdesc **dev = (struct devdesc **)vdev; | struct devdesc **dev = (struct devdesc **)vdev; | ||||
Show All 29 Lines | |||||
*/ | */ | ||||
static int | static int | ||||
efi_parsedev(struct devdesc **dev, const char *devspec, const char **path) | efi_parsedev(struct devdesc **dev, const char *devspec, const char **path) | ||||
{ | { | ||||
struct devdesc *idev; | struct devdesc *idev; | ||||
struct devsw *dv; | struct devsw *dv; | ||||
char *cp; | char *cp; | ||||
const char *np; | const char *np; | ||||
int i, err; | int i; | ||||
/* minimum length check */ | /* minimum length check */ | ||||
if (strlen(devspec) < 2) | if (strlen(devspec) < 2) | ||||
return (EINVAL); | return (EINVAL); | ||||
/* look for a device that matches */ | /* look for a device that matches */ | ||||
for (i = 0; devsw[i] != NULL; i++) { | for (i = 0; devsw[i] != NULL; i++) { | ||||
dv = devsw[i]; | dv = devsw[i]; | ||||
if (!strncmp(devspec, dv->dv_name, strlen(dv->dv_name))) | if (!strncmp(devspec, dv->dv_name, strlen(dv->dv_name))) | ||||
break; | break; | ||||
} | } | ||||
if (devsw[i] == NULL) | if (devsw[i] == NULL) | ||||
return (ENOENT); | return (ENOENT); | ||||
np = devspec + strlen(dv->dv_name); | |||||
#ifdef EFI_ZFS_BOOT | |||||
if (dv->dv_type == DEVT_ZFS) { | |||||
int err; | |||||
idev = malloc(sizeof(struct zfs_devdesc)); | |||||
if (idev == NULL) | |||||
return (ENOMEM); | |||||
err = zfs_parsedev((struct zfs_devdesc*)idev, np, path); | |||||
if (err != 0) { | |||||
free(idev); | |||||
return (err); | |||||
} | |||||
*dev = idev; | |||||
cp = strchr(np + 1, ':'); | |||||
} else { | |||||
#endif | |||||
idev = malloc(sizeof(struct devdesc)); | idev = malloc(sizeof(struct devdesc)); | ||||
if (idev == NULL) | if (idev == NULL) | ||||
return (ENOMEM); | return (ENOMEM); | ||||
idev->d_dev = dv; | idev->d_dev = dv; | ||||
idev->d_type = dv->dv_type; | idev->d_type = dv->dv_type; | ||||
idev->d_unit = -1; | idev->d_unit = -1; | ||||
err = 0; | |||||
np = devspec + strlen(dv->dv_name); | |||||
if (*np != '\0' && *np != ':') { | if (*np != '\0' && *np != ':') { | ||||
idev->d_unit = strtol(np, &cp, 0); | idev->d_unit = strtol(np, &cp, 0); | ||||
if (cp == np) { | if (cp == np) { | ||||
idev->d_unit = -1; | idev->d_unit = -1; | ||||
free(idev); | free(idev); | ||||
return (EUNIT); | return (EUNIT); | ||||
} | } | ||||
} | } | ||||
#ifdef EFI_ZFS_BOOT | |||||
} | |||||
#endif | |||||
if (*cp != '\0' && *cp != ':') { | if (*cp != '\0' && *cp != ':') { | ||||
free(idev); | free(idev); | ||||
return (EINVAL); | return (EINVAL); | ||||
} | } | ||||
if (path != NULL) | if (path != NULL) | ||||
*path = (*cp == 0) ? cp : cp + 1; | *path = (*cp == 0) ? cp : cp + 1; | ||||
if (dev != NULL) | if (dev != NULL) | ||||
*dev = idev; | *dev = idev; | ||||
else | else | ||||
free(idev); | free(idev); | ||||
return (0); | return (0); | ||||
} | } | ||||
char * | char * | ||||
efi_fmtdev(void *vdev) | efi_fmtdev(void *vdev) | ||||
{ | { | ||||
struct devdesc *dev = (struct devdesc *)vdev; | struct devdesc *dev = (struct devdesc *)vdev; | ||||
static char buf[32]; /* XXX device length constant? */ | static char buf[SPECNAMELEN + 1]; | ||||
switch(dev->d_type) { | switch(dev->d_type) { | ||||
#ifdef EFI_ZFS_BOOT | |||||
case DEVT_ZFS: | |||||
return (zfs_fmtdev(dev)); | |||||
#endif | |||||
case DEVT_NONE: | case DEVT_NONE: | ||||
strcpy(buf, "(no device)"); | strcpy(buf, "(no device)"); | ||||
break; | break; | ||||
default: | default: | ||||
sprintf(buf, "%s%d:", dev->d_dev->dv_name, dev->d_unit); | sprintf(buf, "%s%d:", dev->d_dev->dv_name, dev->d_unit); | ||||
break; | break; | ||||
} | } | ||||
return(buf); | return (buf); | ||||
} | } | ||||
/* | /* | ||||
* Set currdev to suit the value being supplied in (value) | * Set currdev to suit the value being supplied in (value) | ||||
*/ | */ | ||||
int | int | ||||
efi_setcurrdev(struct env_var *ev, int flags, const void *value) | efi_setcurrdev(struct env_var *ev, int flags, const void *value) | ||||
{ | { | ||||
struct devdesc *ncurr; | struct devdesc *ncurr; | ||||
int rv; | int rv; | ||||
rv = efi_parsedev(&ncurr, value, NULL); | rv = efi_parsedev(&ncurr, value, NULL); | ||||
if (rv != 0) | if (rv != 0) | ||||
return(rv); | return (rv); | ||||
free(ncurr); | free(ncurr); | ||||
env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL); | env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL); | ||||
return (0); | return (0); | ||||
} | } |