net.c revision 1.1
11.1Stsubai/*	$NetBSD: net.c,v 1.1 1998/05/15 10:15:59 tsubai Exp $	*/
21.1Stsubai
31.1Stsubai/*
41.1Stsubai * Copyright (C) 1995 Wolfgang Solfrank.
51.1Stsubai * Copyright (C) 1995 TooLs GmbH.
61.1Stsubai * All rights reserved.
71.1Stsubai *
81.1Stsubai * Redistribution and use in source and binary forms, with or without
91.1Stsubai * modification, are permitted provided that the following conditions
101.1Stsubai * are met:
111.1Stsubai * 1. Redistributions of source code must retain the above copyright
121.1Stsubai *    notice, this list of conditions and the following disclaimer.
131.1Stsubai * 2. Redistributions in binary form must reproduce the above copyright
141.1Stsubai *    notice, this list of conditions and the following disclaimer in the
151.1Stsubai *    documentation and/or other materials provided with the distribution.
161.1Stsubai * 3. All advertising materials mentioning features or use of this software
171.1Stsubai *    must display the following acknowledgement:
181.1Stsubai *	This product includes software developed by TooLs GmbH.
191.1Stsubai * 4. The name of TooLs GmbH may not be used to endorse or promote products
201.1Stsubai *    derived from this software without specific prior written permission.
211.1Stsubai *
221.1Stsubai * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
231.1Stsubai * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
241.1Stsubai * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
251.1Stsubai * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
261.1Stsubai * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
271.1Stsubai * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
281.1Stsubai * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
291.1Stsubai * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
301.1Stsubai * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
311.1Stsubai * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
321.1Stsubai */
331.1Stsubai
341.1Stsubai/*
351.1Stsubai * This module implements a "raw device" interface suitable for
361.1Stsubai * use by the stand-alone I/O library NFS code.  This interface
371.1Stsubai * does not support any "block" access, and exists only for the
381.1Stsubai * purpose of initializing the network interface, getting boot
391.1Stsubai * parameters, and performing the NFS mount.
401.1Stsubai *
411.1Stsubai * At open time, this does:
421.1Stsubai *
431.1Stsubai * find interface	- netif_open()
441.1Stsubai * BOOTP		- bootp()
451.1Stsubai * RPC/mountd		- nfs_mount()
461.1Stsubai *
471.1Stsubai * The root file handle from mountd is saved in a global
481.1Stsubai * for use by the NFS open code (NFS/lookup).
491.1Stsubai *
501.1Stsubai * Note: this is based in part on sys/arch/sparc/stand/net.c
511.1Stsubai */
521.1Stsubai
531.1Stsubai#include <sys/param.h>
541.1Stsubai#include <sys/socket.h>
551.1Stsubai
561.1Stsubai#include <net/if.h>
571.1Stsubai#include <netinet/in.h>
581.1Stsubai#include <netinet/in_systm.h>
591.1Stsubai
601.1Stsubai#include <lib/libsa/stand.h>
611.1Stsubai#include <lib/libsa/net.h>
621.1Stsubai#include <lib/libsa/netif.h>
631.1Stsubai
641.1Stsubaichar	rootpath[FNAME_SIZE];
651.1Stsubai
661.1Stsubaistatic	int netdev_sock = -1;
671.1Stsubaistatic	int open_count;
681.1Stsubai
691.1Stsubai/*
701.1Stsubai * Called by devopen after it sets f->f_dev to our devsw entry.
711.1Stsubai * This opens the low-level device and sets f->f_devdata.
721.1Stsubai */
731.1Stsubaiint
741.1Stsubainet_open(op)
751.1Stsubai	struct of_dev *op;
761.1Stsubai{
771.1Stsubai	int error = 0;
781.1Stsubai
791.1Stsubai	/*
801.1Stsubai	 * On first open, do netif open, mount, etc.
811.1Stsubai	 */
821.1Stsubai	if (open_count == 0) {
831.1Stsubai		/* Find network interface. */
841.1Stsubai		if ((netdev_sock = netif_open(op)) < 0) {
851.1Stsubai			error = errno;
861.1Stsubai			goto bad;
871.1Stsubai		}
881.1Stsubai		if ((error = net_mountroot()) != 0)
891.1Stsubai			goto bad;
901.1Stsubai	}
911.1Stsubai	open_count++;
921.1Stsubaibad:
931.1Stsubai	if (netdev_sock >= 0 && open_count == 0) {
941.1Stsubai		netif_close(netdev_sock);
951.1Stsubai		netdev_sock = -1;
961.1Stsubai	}
971.1Stsubai	return error;
981.1Stsubai}
991.1Stsubai
1001.1Stsubaiint
1011.1Stsubainet_close(op)
1021.1Stsubai	struct of_dev *op;
1031.1Stsubai{
1041.1Stsubai	/*
1051.1Stsubai	 * On last close, do netif close, etc.
1061.1Stsubai	 */
1071.1Stsubai	if (open_count > 0)
1081.1Stsubai		if (--open_count == 0) {
1091.1Stsubai			netif_close(netdev_sock);
1101.1Stsubai			netdev_sock = -1;
1111.1Stsubai		}
1121.1Stsubai}
1131.1Stsubai
1141.1Stsubaiint
1151.1Stsubainet_mountroot()
1161.1Stsubai{
1171.1Stsubai
1181.1Stsubai#ifdef	DEBUG
1191.1Stsubai	printf("net_mountroot\n");
1201.1Stsubai#endif
1211.1Stsubai
1221.1Stsubai	/*
1231.1Stsubai	 * Get info for NFS boot: our IP address, out hostname,
1241.1Stsubai	 * server IP address, and our root path on the server.
1251.1Stsubai	 * We use BOOTP (RFC951, RFC1532) exclusively as mandated
1261.1Stsubai	 * by PowerPC Reference Platform Specification I.4.2
1271.1Stsubai	 */
1281.1Stsubai
1291.1Stsubai	bootp(netdev_sock);
1301.1Stsubai
1311.1Stsubai	if (myip.s_addr == 0)
1321.1Stsubai		return ETIMEDOUT;
1331.1Stsubai
1341.1Stsubai	printf("Using IP address: %s\n", inet_ntoa(myip));
1351.1Stsubai
1361.1Stsubai#ifdef	DEBUG
1371.1Stsubai	printf("myip: %s (%s)", hostname, inet_ntoa(myip));
1381.1Stsubai	if (gateip.s_addr)
1391.1Stsubai		printf(", gateip: %s", inet_ntoa(gateip));
1401.1Stsubai	if (netmask)
1411.1Stsubai		printf(", netmask: %s", intoa(netmask));
1421.1Stsubai	printf("\n");
1431.1Stsubai#endif
1441.1Stsubai	printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);
1451.1Stsubai
1461.1Stsubai	/*
1471.1Stsubai	 * Get the NFS file handle (mount).
1481.1Stsubai	 */
1491.1Stsubai	if (nfs_mount(netdev_sock, rootip, rootpath) < 0)
1501.1Stsubai		return errno;
1511.1Stsubai	return 0;
1521.1Stsubai}
153