Home | History | Annotate | Line # | Download | only in ofwboot
net.c revision 1.1
      1  1.1  mrg /*	$NetBSD: net.c,v 1.1 2000/08/20 14:58:38 mrg Exp $	*/
      2  1.1  mrg 
      3  1.1  mrg /*
      4  1.1  mrg  * Copyright (C) 1995 Wolfgang Solfrank.
      5  1.1  mrg  * Copyright (C) 1995 TooLs GmbH.
      6  1.1  mrg  * All rights reserved.
      7  1.1  mrg  *
      8  1.1  mrg  * Redistribution and use in source and binary forms, with or without
      9  1.1  mrg  * modification, are permitted provided that the following conditions
     10  1.1  mrg  * are met:
     11  1.1  mrg  * 1. Redistributions of source code must retain the above copyright
     12  1.1  mrg  *    notice, this list of conditions and the following disclaimer.
     13  1.1  mrg  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  mrg  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  mrg  *    documentation and/or other materials provided with the distribution.
     16  1.1  mrg  * 3. All advertising materials mentioning features or use of this software
     17  1.1  mrg  *    must display the following acknowledgement:
     18  1.1  mrg  *	This product includes software developed by TooLs GmbH.
     19  1.1  mrg  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     20  1.1  mrg  *    derived from this software without specific prior written permission.
     21  1.1  mrg  *
     22  1.1  mrg  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     23  1.1  mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  1.1  mrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  1.1  mrg  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     26  1.1  mrg  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     27  1.1  mrg  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     28  1.1  mrg  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29  1.1  mrg  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     30  1.1  mrg  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     31  1.1  mrg  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  1.1  mrg  */
     33  1.1  mrg 
     34  1.1  mrg /*
     35  1.1  mrg  * This module implements a "raw device" interface suitable for
     36  1.1  mrg  * use by the stand-alone I/O library NFS code.  This interface
     37  1.1  mrg  * does not support any "block" access, and exists only for the
     38  1.1  mrg  * purpose of initializing the network interface, getting boot
     39  1.1  mrg  * parameters, and performing the NFS mount.
     40  1.1  mrg  *
     41  1.1  mrg  * At open time, this does:
     42  1.1  mrg  *
     43  1.1  mrg  * find interface	- netif_open()
     44  1.1  mrg  * BOOTP		- bootp()
     45  1.1  mrg  * RPC/mountd		- nfs_mount()
     46  1.1  mrg  *
     47  1.1  mrg  * The root file handle from mountd is saved in a global
     48  1.1  mrg  * for use by the NFS open code (NFS/lookup).
     49  1.1  mrg  *
     50  1.1  mrg  * Note: this is based in part on sys/arch/sparc/stand/net.c
     51  1.1  mrg  */
     52  1.1  mrg 
     53  1.1  mrg #include <sys/param.h>
     54  1.1  mrg #include <sys/socket.h>
     55  1.1  mrg 
     56  1.1  mrg #include <net/if.h>
     57  1.1  mrg #include <netinet/in.h>
     58  1.1  mrg #include <netinet/in_systm.h>
     59  1.1  mrg 
     60  1.1  mrg #include <lib/libsa/stand.h>
     61  1.1  mrg #include <lib/libsa/net.h>
     62  1.1  mrg #include <lib/libsa/netif.h>
     63  1.1  mrg 
     64  1.1  mrg #include <lib/libkern/libkern.h>
     65  1.1  mrg 
     66  1.1  mrg static int net_mountroot_bootparams __P((void));
     67  1.1  mrg static int net_mountroot_bootp __P((void));
     68  1.1  mrg 
     69  1.1  mrg char	rootpath[FNAME_SIZE];
     70  1.1  mrg 
     71  1.1  mrg static	int netdev_sock = -1;
     72  1.1  mrg static	int open_count;
     73  1.1  mrg 
     74  1.1  mrg /*
     75  1.1  mrg  * Called by devopen after it sets f->f_dev to our devsw entry.
     76  1.1  mrg  * This opens the low-level device and sets f->f_devdata.
     77  1.1  mrg  */
     78  1.1  mrg int
     79  1.1  mrg net_open(op)
     80  1.1  mrg 	struct of_dev *op;
     81  1.1  mrg {
     82  1.1  mrg 	int error = 0;
     83  1.1  mrg 
     84  1.1  mrg 	/*
     85  1.1  mrg 	 * On first open, do netif open, mount, etc.
     86  1.1  mrg 	 */
     87  1.1  mrg 	if (open_count == 0) {
     88  1.1  mrg 		/* Find network interface. */
     89  1.1  mrg 		if ((netdev_sock = netif_open(op)) < 0) {
     90  1.1  mrg 			error = errno;
     91  1.1  mrg 			goto bad;
     92  1.1  mrg 		}
     93  1.1  mrg 		if ((error = net_mountroot()) != 0)
     94  1.1  mrg 			goto bad;
     95  1.1  mrg 	}
     96  1.1  mrg 	open_count++;
     97  1.1  mrg bad:
     98  1.1  mrg 	if (netdev_sock >= 0 && open_count == 0) {
     99  1.1  mrg 		netif_close(netdev_sock);
    100  1.1  mrg 		netdev_sock = -1;
    101  1.1  mrg 	}
    102  1.1  mrg 	return error;
    103  1.1  mrg }
    104  1.1  mrg 
    105  1.1  mrg int
    106  1.1  mrg net_close(op)
    107  1.1  mrg 	struct of_dev *op;
    108  1.1  mrg {
    109  1.1  mrg 	/*
    110  1.1  mrg 	 * On last close, do netif close, etc.
    111  1.1  mrg 	 */
    112  1.1  mrg 	if (open_count > 0)
    113  1.1  mrg 		if (--open_count == 0) {
    114  1.1  mrg 			netif_close(netdev_sock);
    115  1.1  mrg 			netdev_sock = -1;
    116  1.1  mrg 		}
    117  1.1  mrg }
    118  1.1  mrg 
    119  1.1  mrg int
    120  1.1  mrg net_mountroot_bootparams()
    121  1.1  mrg {
    122  1.1  mrg 	/* Get our IP address.  (rarp.c) */
    123  1.1  mrg 	if (rarp_getipaddress(netdev_sock) == -1)
    124  1.1  mrg 		return (errno);
    125  1.1  mrg 
    126  1.1  mrg 	printf("Using BOOTPARAMS protocol: ");
    127  1.1  mrg 	printf("ip address: %s", inet_ntoa(myip));
    128  1.1  mrg 
    129  1.1  mrg 	/* Get our hostname, server IP address. */
    130  1.1  mrg 	if (bp_whoami(netdev_sock))
    131  1.1  mrg 		return (errno);
    132  1.1  mrg 
    133  1.1  mrg 	printf(", hostname: %s\n", hostname);
    134  1.1  mrg 
    135  1.1  mrg 	/* Get the root pathname. */
    136  1.1  mrg 	if (bp_getfile(netdev_sock, "root", &rootip, rootpath))
    137  1.1  mrg 		return (errno);
    138  1.1  mrg 
    139  1.1  mrg 	return (0);
    140  1.1  mrg }
    141  1.1  mrg 
    142  1.1  mrg int
    143  1.1  mrg net_mountroot_bootp()
    144  1.1  mrg {
    145  1.1  mrg 	bootp(netdev_sock);
    146  1.1  mrg 
    147  1.1  mrg 	if (myip.s_addr == 0)
    148  1.1  mrg 		return(ENOENT);
    149  1.1  mrg 
    150  1.1  mrg 	printf("Using BOOTP protocol: ");
    151  1.1  mrg 	printf("ip address: %s", inet_ntoa(myip));
    152  1.1  mrg 
    153  1.1  mrg 	if (hostname[0])
    154  1.1  mrg 		printf(", hostname: %s", hostname);
    155  1.1  mrg 	if (netmask)
    156  1.1  mrg 		printf(", netmask: %s", intoa(netmask));
    157  1.1  mrg 	if (gateip.s_addr)
    158  1.1  mrg 		printf(", gateway: %s", inet_ntoa(gateip));
    159  1.1  mrg 	printf("\n");
    160  1.1  mrg 
    161  1.1  mrg 	return (0);
    162  1.1  mrg }
    163  1.1  mrg 
    164  1.1  mrg int
    165  1.1  mrg net_mountroot()
    166  1.1  mrg {
    167  1.1  mrg 	int error;
    168  1.1  mrg 
    169  1.1  mrg #ifdef DEBUG
    170  1.1  mrg 	printf("net_mountroot\n");
    171  1.1  mrg #endif
    172  1.1  mrg 
    173  1.1  mrg 	/*
    174  1.1  mrg 	 * Get info for NFS boot: our IP address, our hostname,
    175  1.1  mrg 	 * server IP address, and our root path on the server.
    176  1.1  mrg 	 * There are two ways to do this:  The old, Sun way,
    177  1.1  mrg 	 * and the more modern, BOOTP way. (RFC951, RFC1048)
    178  1.1  mrg 	 */
    179  1.1  mrg 
    180  1.1  mrg 	/* Historically, we've used BOOTPARAMS, so try that first */
    181  1.1  mrg 	error = net_mountroot_bootparams();
    182  1.1  mrg 	if (error != 0)
    183  1.1  mrg 		/* Next, try BOOTP */
    184  1.1  mrg 		error = net_mountroot_bootp();
    185  1.1  mrg 	if (error != 0)
    186  1.1  mrg 		return (error);
    187  1.1  mrg 
    188  1.1  mrg 	printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);
    189  1.1  mrg 
    190  1.1  mrg 	/* Get the NFS file handle (mount). */
    191  1.1  mrg 	if (nfs_mount(netdev_sock, rootip, rootpath) != 0)
    192  1.1  mrg 		return (errno);
    193  1.1  mrg 
    194  1.1  mrg 	return (0);
    195  1.1  mrg }
    196