net.c revision 1.4
11.4Sdrochner/*	$NetBSD: net.c,v 1.4 2003/03/13 11:35:54 drochner 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.4Sdrochner * find interface	- netif_of_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.2Sdrochner
631.2Sdrochner#include <lib/libkern/libkern.h>
641.1Stsubai
651.4Sdrochner#include "ofdev.h"
661.4Sdrochner#include "netif_of.h"
671.4Sdrochner
681.1Stsubaichar	rootpath[FNAME_SIZE];
691.1Stsubai
701.1Stsubaistatic	int netdev_sock = -1;
711.1Stsubaistatic	int open_count;
721.1Stsubai
731.1Stsubai/*
741.1Stsubai * Called by devopen after it sets f->f_dev to our devsw entry.
751.1Stsubai * This opens the low-level device and sets f->f_devdata.
761.1Stsubai */
771.1Stsubaiint
781.1Stsubainet_open(op)
791.1Stsubai	struct of_dev *op;
801.1Stsubai{
811.1Stsubai	int error = 0;
821.3Stsutsui
831.1Stsubai	/*
841.1Stsubai	 * On first open, do netif open, mount, etc.
851.1Stsubai	 */
861.1Stsubai	if (open_count == 0) {
871.1Stsubai		/* Find network interface. */
881.4Sdrochner		if ((netdev_sock = netif_of_open(op)) < 0) {
891.1Stsubai			error = errno;
901.1Stsubai			goto bad;
911.1Stsubai		}
921.1Stsubai		if ((error = net_mountroot()) != 0)
931.1Stsubai			goto bad;
941.1Stsubai	}
951.1Stsubai	open_count++;
961.1Stsubaibad:
971.1Stsubai	if (netdev_sock >= 0 && open_count == 0) {
981.4Sdrochner		netif_of_close(netdev_sock);
991.1Stsubai		netdev_sock = -1;
1001.1Stsubai	}
1011.1Stsubai	return error;
1021.1Stsubai}
1031.1Stsubai
1041.1Stsubaiint
1051.1Stsubainet_close(op)
1061.1Stsubai	struct of_dev *op;
1071.1Stsubai{
1081.1Stsubai	/*
1091.1Stsubai	 * On last close, do netif close, etc.
1101.1Stsubai	 */
1111.1Stsubai	if (open_count > 0)
1121.1Stsubai		if (--open_count == 0) {
1131.4Sdrochner			netif_of_close(netdev_sock);
1141.1Stsubai			netdev_sock = -1;
1151.1Stsubai		}
1161.1Stsubai}
1171.1Stsubai
1181.1Stsubaiint
1191.1Stsubainet_mountroot()
1201.1Stsubai{
1211.1Stsubai
1221.1Stsubai#ifdef	DEBUG
1231.1Stsubai	printf("net_mountroot\n");
1241.1Stsubai#endif
1251.3Stsutsui
1261.1Stsubai	/*
1271.1Stsubai	 * Get info for NFS boot: our IP address, out hostname,
1281.1Stsubai	 * server IP address, and our root path on the server.
1291.1Stsubai	 * We use BOOTP (RFC951, RFC1532) exclusively as mandated
1301.1Stsubai	 * by PowerPC Reference Platform Specification I.4.2
1311.1Stsubai	 */
1321.3Stsutsui
1331.1Stsubai	bootp(netdev_sock);
1341.3Stsutsui
1351.1Stsubai	if (myip.s_addr == 0)
1361.1Stsubai		return ETIMEDOUT;
1371.3Stsutsui
1381.1Stsubai	printf("Using IP address: %s\n", inet_ntoa(myip));
1391.3Stsutsui
1401.1Stsubai#ifdef	DEBUG
1411.1Stsubai	printf("myip: %s (%s)", hostname, inet_ntoa(myip));
1421.1Stsubai	if (gateip.s_addr)
1431.1Stsubai		printf(", gateip: %s", inet_ntoa(gateip));
1441.1Stsubai	if (netmask)
1451.1Stsubai		printf(", netmask: %s", intoa(netmask));
1461.1Stsubai	printf("\n");
1471.1Stsubai#endif
1481.1Stsubai	printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);
1491.3Stsutsui
1501.1Stsubai	/*
1511.1Stsubai	 * Get the NFS file handle (mount).
1521.1Stsubai	 */
1531.1Stsubai	if (nfs_mount(netdev_sock, rootip, rootpath) < 0)
1541.1Stsubai		return errno;
1551.1Stsubai	return 0;
1561.1Stsubai}
157