1*88b1d6a6Spgoyette /* $NetBSD: nfs_diskless.c,v 1.2 2016/12/13 22:41:46 pgoyette Exp $ */
26ca35587Sdholland /*-
36ca35587Sdholland * Copyright (c) 1990 The Regents of the University of California.
46ca35587Sdholland * All rights reserved.
56ca35587Sdholland *
66ca35587Sdholland * This code is derived from software contributed to Berkeley by
76ca35587Sdholland * William Jolitz.
86ca35587Sdholland *
96ca35587Sdholland * Redistribution and use in source and binary forms, with or without
106ca35587Sdholland * modification, are permitted provided that the following conditions
116ca35587Sdholland * are met:
126ca35587Sdholland * 1. Redistributions of source code must retain the above copyright
136ca35587Sdholland * notice, this list of conditions and the following disclaimer.
146ca35587Sdholland * 2. Redistributions in binary form must reproduce the above copyright
156ca35587Sdholland * notice, this list of conditions and the following disclaimer in the
166ca35587Sdholland * documentation and/or other materials provided with the distribution.
176ca35587Sdholland * 4. Neither the name of the University nor the names of its contributors
186ca35587Sdholland * may be used to endorse or promote products derived from this software
196ca35587Sdholland * without specific prior written permission.
206ca35587Sdholland *
216ca35587Sdholland * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
226ca35587Sdholland * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
236ca35587Sdholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
246ca35587Sdholland * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
256ca35587Sdholland * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
266ca35587Sdholland * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
276ca35587Sdholland * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
286ca35587Sdholland * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
296ca35587Sdholland * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
306ca35587Sdholland * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
316ca35587Sdholland * SUCH DAMAGE.
326ca35587Sdholland *
336ca35587Sdholland * from: @(#)autoconf.c 7.1 (Berkeley) 5/9/91
346ca35587Sdholland */
356ca35587Sdholland
366ca35587Sdholland #include <sys/cdefs.h>
37e81f0ea2Spgoyette /* __FBSDID("FreeBSD: head/sys/nfs/nfs_diskless.c 297086 2016-03-20 21:48:26Z ian "); */
38*88b1d6a6Spgoyette __RCSID("$NetBSD: nfs_diskless.c,v 1.2 2016/12/13 22:41:46 pgoyette Exp $");
396ca35587Sdholland
40*88b1d6a6Spgoyette #ifdef _KERNEL_OPT
41*88b1d6a6Spgoyette #include "opt_newnfs.h"
42*88b1d6a6Spgoyette #endif
436ca35587Sdholland
446ca35587Sdholland #include <sys/param.h>
456ca35587Sdholland #include <sys/systm.h>
466ca35587Sdholland #include <sys/jail.h>
476ca35587Sdholland #include <sys/kernel.h>
486ca35587Sdholland #include <sys/malloc.h>
496ca35587Sdholland #include <sys/mount.h>
506ca35587Sdholland #include <sys/socket.h>
516ca35587Sdholland
526ca35587Sdholland #include <net/if.h>
536ca35587Sdholland #include <net/if_dl.h>
546ca35587Sdholland #include <net/if_types.h>
55*88b1d6a6Spgoyette
566ca35587Sdholland #include <net/if_var.h>
576ca35587Sdholland #include <net/ethernet.h>
586ca35587Sdholland #include <net/vnet.h>
596ca35587Sdholland
606ca35587Sdholland #include <netinet/in.h>
61*88b1d6a6Spgoyette
62*88b1d6a6Spgoyette #include <fs/nfs/common/nfsproto.h>
63*88b1d6a6Spgoyette #include <fs/nfs/client/nfs.h>
64*88b1d6a6Spgoyette #include <fs/nfs/common/nfsdiskless.h>
656ca35587Sdholland
66e81f0ea2Spgoyette #define NFS_IFACE_TIMEOUT_SECS 10 /* Timeout for interface to appear. */
67e81f0ea2Spgoyette
686ca35587Sdholland static int inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa);
696ca35587Sdholland static int hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa);
706ca35587Sdholland static int decode_nfshandle(char *ev, u_char *fh, int maxfh);
716ca35587Sdholland
726ca35587Sdholland /*
736ca35587Sdholland * This structure must be filled in by a primary bootstrap or bootstrap
746ca35587Sdholland * server for a diskless/dataless machine. It is initialized below just
756ca35587Sdholland * to ensure that it is allocated to initialized data (.data not .bss).
766ca35587Sdholland */
776ca35587Sdholland struct nfs_diskless nfs_diskless = { { { 0 } } };
786ca35587Sdholland struct nfsv3_diskless nfsv3_diskless = { { { 0 } } };
796ca35587Sdholland int nfs_diskless_valid = 0;
806ca35587Sdholland
816ca35587Sdholland /*
826ca35587Sdholland * Validate/sanity check a rsize/wsize parameter.
836ca35587Sdholland */
846ca35587Sdholland static int
checkrwsize(unsigned long v,const char * name)856ca35587Sdholland checkrwsize(unsigned long v, const char *name)
866ca35587Sdholland {
876ca35587Sdholland /*
886ca35587Sdholland * 32K is used as an upper bound because most servers
896ca35587Sdholland * limit block size to satisfy IPv4's limit of
906ca35587Sdholland * 64K/reassembled packet. The lower bound is pretty
916ca35587Sdholland * much arbitrary.
926ca35587Sdholland */
936ca35587Sdholland if (!(4 <= v && v <= 32*1024)) {
946ca35587Sdholland printf("nfs_parse_options: invalid %s %lu ignored\n", name, v);
956ca35587Sdholland return 0;
966ca35587Sdholland } else
976ca35587Sdholland return 1;
986ca35587Sdholland }
996ca35587Sdholland
1006ca35587Sdholland /*
1016ca35587Sdholland * Parse mount options and apply them to the supplied
1026ca35587Sdholland * nfs_diskless state. Used also by bootp/dhcp support.
1036ca35587Sdholland */
1046ca35587Sdholland void
nfs_parse_options(const char * envopts,struct nfs_args * nd)1056ca35587Sdholland nfs_parse_options(const char *envopts, struct nfs_args *nd)
1066ca35587Sdholland {
1076ca35587Sdholland char *opts, *o, *otmp;
1086ca35587Sdholland unsigned long v;
1096ca35587Sdholland
1106ca35587Sdholland opts = strdup(envopts, M_TEMP);
1116ca35587Sdholland otmp = opts;
1126ca35587Sdholland while ((o = strsep(&otmp, ":;, ")) != NULL) {
1136ca35587Sdholland if (*o == '\0')
1146ca35587Sdholland ; /* Skip empty options. */
1156ca35587Sdholland else if (strcmp(o, "soft") == 0)
1166ca35587Sdholland nd->flags |= NFSMNT_SOFT;
1176ca35587Sdholland else if (strcmp(o, "intr") == 0)
1186ca35587Sdholland nd->flags |= NFSMNT_INT;
1196ca35587Sdholland else if (strcmp(o, "conn") == 0)
1206ca35587Sdholland nd->flags |= NFSMNT_NOCONN;
1216ca35587Sdholland else if (strcmp(o, "nolockd") == 0)
1226ca35587Sdholland nd->flags |= NFSMNT_NOLOCKD;
1236ca35587Sdholland else if (strcmp(o, "nocto") == 0)
1246ca35587Sdholland nd->flags |= NFSMNT_NOCTO;
1256ca35587Sdholland else if (strcmp(o, "nfsv2") == 0)
1266ca35587Sdholland nd->flags &= ~(NFSMNT_NFSV3 | NFSMNT_NFSV4);
1276ca35587Sdholland else if (strcmp(o, "nfsv3") == 0) {
1286ca35587Sdholland nd->flags &= ~NFSMNT_NFSV4;
1296ca35587Sdholland nd->flags |= NFSMNT_NFSV3;
1306ca35587Sdholland } else if (strcmp(o, "tcp") == 0)
1316ca35587Sdholland nd->sotype = SOCK_STREAM;
1326ca35587Sdholland else if (strcmp(o, "udp") == 0)
1336ca35587Sdholland nd->sotype = SOCK_DGRAM;
1346ca35587Sdholland else if (strncmp(o, "rsize=", 6) == 0) {
1356ca35587Sdholland v = strtoul(o+6, NULL, 10);
1366ca35587Sdholland if (checkrwsize(v, "rsize")) {
1376ca35587Sdholland nd->rsize = (int) v;
1386ca35587Sdholland nd->flags |= NFSMNT_RSIZE;
1396ca35587Sdholland }
1406ca35587Sdholland } else if (strncmp(o, "wsize=", 6) == 0) {
1416ca35587Sdholland v = strtoul(o+6, NULL, 10);
1426ca35587Sdholland if (checkrwsize(v, "wsize")) {
1436ca35587Sdholland nd->wsize = (int) v;
1446ca35587Sdholland nd->flags |= NFSMNT_WSIZE;
1456ca35587Sdholland }
1466ca35587Sdholland } else
1476ca35587Sdholland printf("%s: skipping unknown option \"%s\"\n",
1486ca35587Sdholland __func__, o);
1496ca35587Sdholland }
1506ca35587Sdholland free(opts, M_TEMP);
1516ca35587Sdholland }
1526ca35587Sdholland
1536ca35587Sdholland /*
1546ca35587Sdholland * Populate the essential fields in the nfsv3_diskless structure.
1556ca35587Sdholland *
1566ca35587Sdholland * The loader is expected to export the following environment variables:
1576ca35587Sdholland *
1586ca35587Sdholland * boot.netif.name name of boot interface
1596ca35587Sdholland * boot.netif.ip IP address on boot interface
1606ca35587Sdholland * boot.netif.netmask netmask on boot interface
1616ca35587Sdholland * boot.netif.gateway default gateway (optional)
1626ca35587Sdholland * boot.netif.hwaddr hardware address of boot interface
163e81f0ea2Spgoyette * boot.netif.mtu interface mtu from bootp/dhcp (optional)
1646ca35587Sdholland * boot.nfsroot.server IP address of root filesystem server
1656ca35587Sdholland * boot.nfsroot.path path of the root filesystem on server
1666ca35587Sdholland * boot.nfsroot.nfshandle NFS handle for root filesystem on server
1676ca35587Sdholland * boot.nfsroot.nfshandlelen and length of this handle (for NFSv3 only)
1686ca35587Sdholland * boot.nfsroot.options NFS options for the root filesystem
1696ca35587Sdholland */
1706ca35587Sdholland void
nfs_setup_diskless(void)1716ca35587Sdholland nfs_setup_diskless(void)
1726ca35587Sdholland {
1736ca35587Sdholland struct nfs_diskless *nd = &nfs_diskless;
1746ca35587Sdholland struct nfsv3_diskless *nd3 = &nfsv3_diskless;
1756ca35587Sdholland struct ifnet *ifp;
1766ca35587Sdholland struct ifaddr *ifa;
1776ca35587Sdholland struct sockaddr_dl *sdl, ourdl;
1786ca35587Sdholland struct sockaddr_in myaddr, netmask;
1796ca35587Sdholland char *cp;
1806ca35587Sdholland int cnt, fhlen, is_nfsv3;
1816ca35587Sdholland uint32_t len;
182e81f0ea2Spgoyette time_t timeout_at;
1836ca35587Sdholland
1846ca35587Sdholland if (nfs_diskless_valid != 0)
1856ca35587Sdholland return;
1866ca35587Sdholland
1876ca35587Sdholland /* get handle size. If this succeeds, it's an NFSv3 setup. */
188e81f0ea2Spgoyette if ((cp = kern_getenv("boot.nfsroot.nfshandlelen")) != NULL) {
1896ca35587Sdholland cnt = sscanf(cp, "%d", &len);
1906ca35587Sdholland freeenv(cp);
1916ca35587Sdholland if (cnt != 1 || len == 0 || len > NFSX_V3FHMAX) {
1926ca35587Sdholland printf("nfs_diskless: bad NFS handle len\n");
1936ca35587Sdholland return;
1946ca35587Sdholland }
1956ca35587Sdholland nd3->root_fhsize = len;
1966ca35587Sdholland is_nfsv3 = 1;
1976ca35587Sdholland } else
1986ca35587Sdholland is_nfsv3 = 0;
1996ca35587Sdholland /* set up interface */
2006ca35587Sdholland if (inaddr_to_sockaddr("boot.netif.ip", &myaddr))
2016ca35587Sdholland return;
2026ca35587Sdholland if (inaddr_to_sockaddr("boot.netif.netmask", &netmask)) {
2036ca35587Sdholland printf("nfs_diskless: no netmask\n");
2046ca35587Sdholland return;
2056ca35587Sdholland }
2066ca35587Sdholland if (is_nfsv3 != 0) {
2076ca35587Sdholland bcopy(&myaddr, &nd3->myif.ifra_addr, sizeof(myaddr));
2086ca35587Sdholland bcopy(&myaddr, &nd3->myif.ifra_broadaddr, sizeof(myaddr));
2096ca35587Sdholland ((struct sockaddr_in *)
2106ca35587Sdholland &nd3->myif.ifra_broadaddr)->sin_addr.s_addr =
2116ca35587Sdholland myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr;
2126ca35587Sdholland bcopy(&netmask, &nd3->myif.ifra_mask, sizeof(netmask));
2136ca35587Sdholland } else {
2146ca35587Sdholland bcopy(&myaddr, &nd->myif.ifra_addr, sizeof(myaddr));
2156ca35587Sdholland bcopy(&myaddr, &nd->myif.ifra_broadaddr, sizeof(myaddr));
2166ca35587Sdholland ((struct sockaddr_in *)
2176ca35587Sdholland &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
2186ca35587Sdholland myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr;
2196ca35587Sdholland bcopy(&netmask, &nd->myif.ifra_mask, sizeof(netmask));
2206ca35587Sdholland }
2216ca35587Sdholland
2226ca35587Sdholland if (hwaddr_to_sockaddr("boot.netif.hwaddr", &ourdl)) {
2236ca35587Sdholland printf("nfs_diskless: no hardware address\n");
2246ca35587Sdholland return;
2256ca35587Sdholland }
2266ca35587Sdholland ifa = NULL;
227e81f0ea2Spgoyette timeout_at = time_uptime + NFS_IFACE_TIMEOUT_SECS;
228e81f0ea2Spgoyette retry:
2296ca35587Sdholland CURVNET_SET(TD_TO_VNET(curthread));
2306ca35587Sdholland IFNET_RLOCK();
2316ca35587Sdholland TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2326ca35587Sdholland TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2336ca35587Sdholland if (ifa->ifa_addr->sa_family == AF_LINK) {
2346ca35587Sdholland sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2356ca35587Sdholland if ((sdl->sdl_type == ourdl.sdl_type) &&
2366ca35587Sdholland (sdl->sdl_alen == ourdl.sdl_alen) &&
2376ca35587Sdholland !bcmp(LLADDR(sdl),
2386ca35587Sdholland LLADDR(&ourdl),
2396ca35587Sdholland sdl->sdl_alen)) {
2406ca35587Sdholland IFNET_RUNLOCK();
2416ca35587Sdholland CURVNET_RESTORE();
2426ca35587Sdholland goto match_done;
2436ca35587Sdholland }
2446ca35587Sdholland }
2456ca35587Sdholland }
2466ca35587Sdholland }
2476ca35587Sdholland IFNET_RUNLOCK();
2486ca35587Sdholland CURVNET_RESTORE();
249e81f0ea2Spgoyette if (time_uptime < timeout_at) {
250e81f0ea2Spgoyette pause("nfssdl", hz / 5);
251e81f0ea2Spgoyette goto retry;
252e81f0ea2Spgoyette }
2536ca35587Sdholland printf("nfs_diskless: no interface\n");
2546ca35587Sdholland return; /* no matching interface */
2556ca35587Sdholland match_done:
256e81f0ea2Spgoyette kern_setenv("boot.netif.name", ifp->if_xname);
2576ca35587Sdholland if (is_nfsv3 != 0) {
2586ca35587Sdholland strlcpy(nd3->myif.ifra_name, ifp->if_xname,
2596ca35587Sdholland sizeof(nd3->myif.ifra_name));
2606ca35587Sdholland
2616ca35587Sdholland /* set up gateway */
2626ca35587Sdholland inaddr_to_sockaddr("boot.netif.gateway", &nd3->mygateway);
2636ca35587Sdholland
2646ca35587Sdholland /* set up root mount */
2656ca35587Sdholland nd3->root_args.rsize = 32768; /* XXX tunable? */
2666ca35587Sdholland nd3->root_args.wsize = 32768;
2676ca35587Sdholland nd3->root_args.sotype = SOCK_STREAM;
2686ca35587Sdholland nd3->root_args.flags = (NFSMNT_NFSV3 | NFSMNT_WSIZE |
2696ca35587Sdholland NFSMNT_RSIZE | NFSMNT_RESVPORT);
2706ca35587Sdholland if (inaddr_to_sockaddr("boot.nfsroot.server",
2716ca35587Sdholland &nd3->root_saddr)) {
2726ca35587Sdholland printf("nfs_diskless: no server\n");
2736ca35587Sdholland return;
2746ca35587Sdholland }
2756ca35587Sdholland nd3->root_saddr.sin_port = htons(NFS_PORT);
2766ca35587Sdholland fhlen = decode_nfshandle("boot.nfsroot.nfshandle",
2776ca35587Sdholland &nd3->root_fh[0], NFSX_V3FHMAX);
2786ca35587Sdholland if (fhlen == 0) {
2796ca35587Sdholland printf("nfs_diskless: no NFS handle\n");
2806ca35587Sdholland return;
2816ca35587Sdholland }
2826ca35587Sdholland if (fhlen != nd3->root_fhsize) {
2836ca35587Sdholland printf("nfs_diskless: bad NFS handle len=%d\n", fhlen);
2846ca35587Sdholland return;
2856ca35587Sdholland }
286e81f0ea2Spgoyette if ((cp = kern_getenv("boot.nfsroot.path")) != NULL) {
2876ca35587Sdholland strncpy(nd3->root_hostnam, cp, MNAMELEN - 1);
2886ca35587Sdholland freeenv(cp);
2896ca35587Sdholland }
290e81f0ea2Spgoyette if ((cp = kern_getenv("boot.nfsroot.options")) != NULL) {
2916ca35587Sdholland nfs_parse_options(cp, &nd3->root_args);
2926ca35587Sdholland freeenv(cp);
2936ca35587Sdholland }
2946ca35587Sdholland
2956ca35587Sdholland nfs_diskless_valid = 3;
2966ca35587Sdholland } else {
2976ca35587Sdholland strlcpy(nd->myif.ifra_name, ifp->if_xname,
2986ca35587Sdholland sizeof(nd->myif.ifra_name));
2996ca35587Sdholland
3006ca35587Sdholland /* set up gateway */
3016ca35587Sdholland inaddr_to_sockaddr("boot.netif.gateway", &nd->mygateway);
3026ca35587Sdholland
3036ca35587Sdholland /* set up root mount */
3046ca35587Sdholland nd->root_args.rsize = 8192; /* XXX tunable? */
3056ca35587Sdholland nd->root_args.wsize = 8192;
3066ca35587Sdholland nd->root_args.sotype = SOCK_STREAM;
3076ca35587Sdholland nd->root_args.flags = (NFSMNT_WSIZE |
3086ca35587Sdholland NFSMNT_RSIZE | NFSMNT_RESVPORT);
3096ca35587Sdholland if (inaddr_to_sockaddr("boot.nfsroot.server",
3106ca35587Sdholland &nd->root_saddr)) {
3116ca35587Sdholland printf("nfs_diskless: no server\n");
3126ca35587Sdholland return;
3136ca35587Sdholland }
3146ca35587Sdholland nd->root_saddr.sin_port = htons(NFS_PORT);
3156ca35587Sdholland if (decode_nfshandle("boot.nfsroot.nfshandle",
3166ca35587Sdholland &nd->root_fh[0], NFSX_V2FH) == 0) {
3176ca35587Sdholland printf("nfs_diskless: no NFS handle\n");
3186ca35587Sdholland return;
3196ca35587Sdholland }
320e81f0ea2Spgoyette if ((cp = kern_getenv("boot.nfsroot.path")) != NULL) {
3216ca35587Sdholland strncpy(nd->root_hostnam, cp, MNAMELEN - 1);
3226ca35587Sdholland freeenv(cp);
3236ca35587Sdholland }
324e81f0ea2Spgoyette if ((cp = kern_getenv("boot.nfsroot.options")) != NULL) {
3256ca35587Sdholland struct nfs_args args;
3266ca35587Sdholland
3276ca35587Sdholland /*
3286ca35587Sdholland * XXX yech, convert between old and current
3296ca35587Sdholland * arg format
3306ca35587Sdholland */
3316ca35587Sdholland args.flags = nd->root_args.flags;
3326ca35587Sdholland args.sotype = nd->root_args.sotype;
3336ca35587Sdholland args.rsize = nd->root_args.rsize;
3346ca35587Sdholland args.wsize = nd->root_args.wsize;
3356ca35587Sdholland nfs_parse_options(cp, &args);
3366ca35587Sdholland nd->root_args.flags = args.flags;
3376ca35587Sdholland nd->root_args.sotype = args.sotype;
3386ca35587Sdholland nd->root_args.rsize = args.rsize;
3396ca35587Sdholland nd->root_args.wsize = args.wsize;
3406ca35587Sdholland freeenv(cp);
3416ca35587Sdholland }
3426ca35587Sdholland
3436ca35587Sdholland nfs_diskless_valid = 1;
3446ca35587Sdholland }
3456ca35587Sdholland }
3466ca35587Sdholland
3476ca35587Sdholland static int
inaddr_to_sockaddr(char * ev,struct sockaddr_in * sa)3486ca35587Sdholland inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa)
3496ca35587Sdholland {
3506ca35587Sdholland u_int32_t a[4];
3516ca35587Sdholland char *cp;
3526ca35587Sdholland int count;
3536ca35587Sdholland
3546ca35587Sdholland bzero(sa, sizeof(*sa));
3556ca35587Sdholland sa->sin_len = sizeof(*sa);
3566ca35587Sdholland sa->sin_family = AF_INET;
3576ca35587Sdholland
358e81f0ea2Spgoyette if ((cp = kern_getenv(ev)) == NULL)
3596ca35587Sdholland return (1);
3606ca35587Sdholland count = sscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
3616ca35587Sdholland freeenv(cp);
3626ca35587Sdholland if (count != 4)
3636ca35587Sdholland return (1);
3646ca35587Sdholland sa->sin_addr.s_addr =
3656ca35587Sdholland htonl((a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3]);
3666ca35587Sdholland return (0);
3676ca35587Sdholland }
3686ca35587Sdholland
3696ca35587Sdholland static int
hwaddr_to_sockaddr(char * ev,struct sockaddr_dl * sa)3706ca35587Sdholland hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa)
3716ca35587Sdholland {
3726ca35587Sdholland char *cp;
3736ca35587Sdholland u_int32_t a[6];
3746ca35587Sdholland int count;
3756ca35587Sdholland
3766ca35587Sdholland bzero(sa, sizeof(*sa));
3776ca35587Sdholland sa->sdl_len = sizeof(*sa);
3786ca35587Sdholland sa->sdl_family = AF_LINK;
3796ca35587Sdholland sa->sdl_type = IFT_ETHER;
3806ca35587Sdholland sa->sdl_alen = ETHER_ADDR_LEN;
381e81f0ea2Spgoyette if ((cp = kern_getenv(ev)) == NULL)
3826ca35587Sdholland return (1);
3836ca35587Sdholland count = sscanf(cp, "%x:%x:%x:%x:%x:%x",
3846ca35587Sdholland &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);
3856ca35587Sdholland freeenv(cp);
3866ca35587Sdholland if (count != 6)
3876ca35587Sdholland return (1);
3886ca35587Sdholland sa->sdl_data[0] = a[0];
3896ca35587Sdholland sa->sdl_data[1] = a[1];
3906ca35587Sdholland sa->sdl_data[2] = a[2];
3916ca35587Sdholland sa->sdl_data[3] = a[3];
3926ca35587Sdholland sa->sdl_data[4] = a[4];
3936ca35587Sdholland sa->sdl_data[5] = a[5];
3946ca35587Sdholland return (0);
3956ca35587Sdholland }
3966ca35587Sdholland
3976ca35587Sdholland static int
decode_nfshandle(char * ev,u_char * fh,int maxfh)3986ca35587Sdholland decode_nfshandle(char *ev, u_char *fh, int maxfh)
3996ca35587Sdholland {
4006ca35587Sdholland u_char *cp, *ep;
4016ca35587Sdholland int len, val;
4026ca35587Sdholland
403e81f0ea2Spgoyette ep = cp = kern_getenv(ev);
4046ca35587Sdholland if (cp == NULL)
4056ca35587Sdholland return (0);
4066ca35587Sdholland if ((strlen(cp) < 2) || (*cp != 'X')) {
4076ca35587Sdholland freeenv(ep);
4086ca35587Sdholland return (0);
4096ca35587Sdholland }
4106ca35587Sdholland len = 0;
4116ca35587Sdholland cp++;
4126ca35587Sdholland for (;;) {
4136ca35587Sdholland if (*cp == 'X') {
4146ca35587Sdholland freeenv(ep);
4156ca35587Sdholland return (len);
4166ca35587Sdholland }
4176ca35587Sdholland if ((sscanf(cp, "%2x", &val) != 1) || (val > 0xff)) {
4186ca35587Sdholland freeenv(ep);
4196ca35587Sdholland return (0);
4206ca35587Sdholland }
4216ca35587Sdholland *(fh++) = val;
4226ca35587Sdholland len++;
4236ca35587Sdholland cp += 2;
4246ca35587Sdholland if (len > maxfh) {
4256ca35587Sdholland freeenv(ep);
4266ca35587Sdholland return (0);
4276ca35587Sdholland }
4286ca35587Sdholland }
4296ca35587Sdholland }
4306ca35587Sdholland
431*88b1d6a6Spgoyette #if !defined(NEW_NFS_BOOT_BOOTP)
4326ca35587Sdholland static void
nfs_rootconf(void)4336ca35587Sdholland nfs_rootconf(void)
4346ca35587Sdholland {
4356ca35587Sdholland
4366ca35587Sdholland nfs_setup_diskless();
4376ca35587Sdholland if (nfs_diskless_valid)
4386ca35587Sdholland rootdevnames[0] = "nfs:";
4396ca35587Sdholland }
4406ca35587Sdholland
4416ca35587Sdholland SYSINIT(cpu_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, nfs_rootconf, NULL);
4426ca35587Sdholland #endif
4436ca35587Sdholland
444