138465Smsmith/*- 238465Smsmith * Copyright (c) 1998 Michael Smith <msmith@freebsd.org> 338465Smsmith * All rights reserved. 438465Smsmith * 538465Smsmith * Redistribution and use in source and binary forms, with or without 638465Smsmith * modification, are permitted provided that the following conditions 738465Smsmith * are met: 838465Smsmith * 1. Redistributions of source code must retain the above copyright 938465Smsmith * notice, this list of conditions and the following disclaimer. 1038465Smsmith * 2. Redistributions in binary form must reproduce the above copyright 1138465Smsmith * notice, this list of conditions and the following disclaimer in the 1238465Smsmith * documentation and/or other materials provided with the distribution. 1338465Smsmith * 1438465Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1538465Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1638465Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1738465Smsmith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1838465Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1938465Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2038465Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2138465Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2238465Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2338465Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2438465Smsmith * SUCH DAMAGE. 2538465Smsmith */ 2638465Smsmith 27124140Sobrien#include <sys/cdefs.h> 28124140Sobrien__FBSDID("$FreeBSD: stable/11/stand/ofw/libofw/devicename.c 344377 2019-02-20 19:13:09Z kevans $"); 29124140Sobrien 3038465Smsmith#include <stand.h> 31234898Smarius 32234898Smarius#include "bootstrap.h" 3367227Sobrien#include "libofw.h" 34344377Skevans#include "libzfs.h" 3538465Smsmith 36106738Sjakestatic int ofw_parsedev(struct ofw_devdesc **, const char *, const char **); 3738465Smsmith 3838465Smsmith/* 3938465Smsmith * Point (dev) at an allocated device specifier for the device matching the 4038465Smsmith * path in (devspec). If it contains an explicit device specification, 4138465Smsmith * use that. If not, use the default device. 4238465Smsmith */ 4338465Smsmithint 4467227Sobrienofw_getdev(void **vdev, const char *devspec, const char **path) 4538465Smsmith{ 4667227Sobrien struct ofw_devdesc **dev = (struct ofw_devdesc **)vdev; 4738465Smsmith int rv; 48106738Sjake 4938465Smsmith /* 5038465Smsmith * If it looks like this is just a path and no 5138465Smsmith * device, go with the current device. 5238465Smsmith */ 5338465Smsmith if ((devspec == NULL) || 54123703Sgrehan ((strchr(devspec, '@') == NULL) && 55123703Sgrehan (strchr(devspec, ':') == NULL))) { 5638465Smsmith 5767227Sobrien if (((rv = ofw_parsedev(dev, getenv("currdev"), NULL)) == 0) && 5838465Smsmith (path != NULL)) 5938465Smsmith *path = devspec; 6038465Smsmith return(rv); 6138465Smsmith } 6238465Smsmith 6338465Smsmith /* 6438465Smsmith * Try to parse the device name off the beginning of the devspec 6538465Smsmith */ 6667227Sobrien return(ofw_parsedev(dev, devspec, path)); 6738465Smsmith} 6838465Smsmith 6938465Smsmith/* 7038465Smsmith * Point (dev) at an allocated device specifier matching the string version 7138465Smsmith * at the beginning of (devspec). Return a pointer to the remaining 7238465Smsmith * text in (path). 7338465Smsmith */ 7438465Smsmithstatic int 7567227Sobrienofw_parsedev(struct ofw_devdesc **dev, const char *devspec, const char **path) 7638465Smsmith{ 77106738Sjake struct ofw_devdesc *idev; 7838465Smsmith struct devsw *dv; 79106738Sjake phandle_t handle; 80106738Sjake const char *p; 81106738Sjake const char *s; 82234898Smarius char *ep; 83106738Sjake char name[256]; 84106738Sjake char type[64]; 85235364Savg int err; 86106738Sjake int len; 87106738Sjake int i; 8838465Smsmith 89106738Sjake for (p = s = devspec; *s != '\0'; p = s) { 90106738Sjake if ((s = strchr(p + 1, '/')) == NULL) 91106738Sjake s = strchr(p, '\0'); 92106738Sjake len = s - devspec; 93106738Sjake bcopy(devspec, name, len); 94106738Sjake name[len] = '\0'; 95234898Smarius if ((handle = OF_finddevice(name)) == -1) { 96234898Smarius bcopy(name, type, len); 97234898Smarius type[len] = '\0'; 98234898Smarius } else if (OF_getprop(handle, "device_type", type, sizeof(type)) == -1) 99106738Sjake continue; 100106738Sjake for (i = 0; (dv = devsw[i]) != NULL; i++) { 101107483Sjake if (strncmp(dv->dv_name, type, strlen(dv->dv_name)) == 0) 102106738Sjake goto found; 10338465Smsmith } 10438465Smsmith } 105106738Sjake return(ENOENT); 10638465Smsmith 107106738Sjakefound: 108240655Smav if (path != NULL) 109106738Sjake *path = s; 11067227Sobrien idev = malloc(sizeof(struct ofw_devdesc)); 111151649Smarius if (idev == NULL) { 112151649Smarius printf("ofw_parsedev: malloc failed\n"); 113151649Smarius return ENOMEM; 114151649Smarius } 115106738Sjake strcpy(idev->d_path, name); 116332154Skevans idev->dd.d_dev = dv; 117332154Skevans if (dv->dv_type == DEVT_ZFS) { 118235364Savg p = devspec + strlen(dv->dv_name); 119235364Savg err = zfs_parsedev((struct zfs_devdesc *)idev, p, path); 120235364Savg if (err != 0) { 121235364Savg free(idev); 122235364Savg return (err); 123234898Smarius } 124234898Smarius } 125234898Smarius 12638475Sdfr if (dev == NULL) { 12738475Sdfr free(idev); 12838475Sdfr } else { 12938465Smsmith *dev = idev; 13038475Sdfr } 13138465Smsmith return(0); 13238465Smsmith} 13368548Sbenno 13484968Srobertint 135123703Sgrehanofw_setcurrdev(struct env_var *ev, int flags, const void *value) 13668548Sbenno{ 137106738Sjake struct ofw_devdesc *ncurr; 138106738Sjake int rv; 13968548Sbenno 140106738Sjake if ((rv = ofw_parsedev(&ncurr, value, NULL)) != 0) 141106738Sjake return rv; 14268548Sbenno 143106738Sjake free(ncurr); 144106738Sjake env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL); 145106738Sjake return 0; 14668548Sbenno} 147