/*-1* Copyright (c) 1998 Michael Smith <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <stand.h>27#include <string.h>2829#include "bootstrap.h"30#include "disk.h"31#include "libuboot.h"3233static int uboot_parsedev(struct uboot_devdesc **dev, const char *devspec,34const char **path);3536/*37* Point (dev) at an allocated device specifier for the device matching the38* path in (devspec). If it contains an explicit device specification,39* use that. If not, use the default device.40*/41int42uboot_getdev(void **vdev, const char *devspec, const char **path)43{44struct uboot_devdesc **dev = (struct uboot_devdesc **)vdev;45int rv;4647/*48* If it looks like this is just a path and no49* device, go with the current device.50*/51if ((devspec == NULL) || (devspec[0] == '/') ||52(strchr(devspec, ':') == NULL)) {5354if (((rv = uboot_parsedev(dev, getenv("currdev"), NULL)) == 0)55&& (path != NULL))56*path = devspec;57return(rv);58}5960/*61* Try to parse the device name off the beginning of the devspec.62*/63return (uboot_parsedev(dev, devspec, path));64}6566/*67* Point (dev) at an allocated device specifier matching the string version68* at the beginning of (devspec). Return a pointer to the remaining69* text in (path).70*71* In all cases, the beginning of (devspec) is compared to the names72* of known devices in the device switch, and then any following text73* is parsed according to the rules applied to the device type.74*75* For disk-type devices, the syntax is:76*77* disk<unit>[<partition>]:78*79*/80static int81uboot_parsedev(struct uboot_devdesc **dev, const char *devspec,82const char **path)83{84struct uboot_devdesc *idev;85struct devsw *dv;86char *cp;87const char *np;88int i, unit, err;8990/* minimum length check */91if (strlen(devspec) < 2)92return(EINVAL);9394/* look for a device that matches */95for (i = 0, dv = NULL; devsw[i] != NULL; i++) {96if (!strncmp(devspec, devsw[i]->dv_name,97strlen(devsw[i]->dv_name))) {98dv = devsw[i];99break;100}101}102if (dv == NULL)103return(ENOENT);104idev = malloc(sizeof(struct uboot_devdesc));105err = 0;106np = (devspec + strlen(dv->dv_name));107108switch(dv->dv_type) {109case DEVT_NONE:110break;111112#ifdef LOADER_DISK_SUPPORT113case DEVT_DISK:114free(idev);115err = disk_parsedev((struct devdesc **)&idev, devspec, path);116if (err != 0)117goto fail;118break;119#endif120121case DEVT_NET:122unit = 0;123124if (*np && (*np != ':')) {125/* get unit number if present */126unit = strtol(np, &cp, 0);127if (cp == np) {128err = EUNIT;129goto fail;130}131}132if (*cp && (*cp != ':')) {133err = EINVAL;134goto fail;135}136idev->dd.d_unit = unit;137138if (path != NULL)139*path = (*cp == 0) ? cp : cp + 1;140break;141142default:143err = EINVAL;144goto fail;145}146idev->dd.d_dev = dv;147/*148* dev can be NULL, since uboot_getdev calls us directly, rather than via149* dv_parsedev in devparse() which otherwise ensures that it can't be NULL.150*/151if (dev == NULL) {152free(idev);153} else {154*dev = idev;155}156return (0);157158fail:159free(idev);160return (err);161}162163164/*165* Set currdev to suit the value being supplied in (value).166*/167int168uboot_setcurrdev(struct env_var *ev, int flags, const void *value)169{170struct uboot_devdesc *ncurr;171int rv;172173if ((rv = uboot_parsedev(&ncurr, value, NULL)) != 0)174return (rv);175free(ncurr);176177return (mount_currdev(ev, flags, value));178}179180181