xref: /netbsd-src/sys/arch/sandpoint/stand/altboot/dev_net.c (revision c30561b5e9d6aef2b1378175332f7de0060799e1)
1 /* $NetBSD: dev_net.c,v 1.3 2014/08/05 17:55:20 joerg Exp $ */
2 
3 /*-
4  * Copyright (c) 2007 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Tohru Nishimura.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/param.h>
33 
34 #include <netinet/in.h>
35 #include <netinet/in_systm.h>
36 
37 #include <lib/libsa/stand.h>
38 #include <lib/libsa/net.h>
39 #include <lib/libsa/bootp.h>
40 #include <lib/libsa/nfs.h>
41 #include <lib/libkern/libkern.h>
42 
43 #include <machine/bootinfo.h>
44 
45 #include "globals.h"
46 
47 static int netdev_sock = -1;
48 static int netdev_opens;
49 
50 int
net_open(struct open_file * f,...)51 net_open(struct open_file *f, ...)
52 
53 {
54 	va_list ap;
55 	char *file, *proto;
56 	int error;
57 	extern struct btinfo_bootpath bi_path;
58 
59 	va_start(ap, f);
60 	file = va_arg(ap, char *);
61 	proto = va_arg(ap, char *);
62 	va_end(ap);
63 
64 	if (netdev_opens > 0)
65 		return 0;
66 
67 	if ((netdev_sock = netif_open(NULL)) < 0)
68 		return ENXIO;	/* never fails indeed */
69 
70 	error = 0;
71 	bootp(netdev_sock);	/* send DHCP request */
72 	if (myip.s_addr == 0) {
73 		error = ENOENT;	/* IP address was not found */
74 		goto bad;
75 	}
76 
77 	if (file[0] != '\0')
78 		snprintf(bootfile, sizeof(bootfile), "%s", file);
79 	else if (bootfile[0] == '\0')
80 		snprintf(bootfile, sizeof(bootfile), "netbsd");
81 
82 	if (strcmp(proto, "nfs") == 0
83 	    && (error = nfs_mount(netdev_sock, rootip, rootpath)) != 0)
84 		goto bad;
85 
86 	snprintf(bi_path.bootpath, sizeof(bi_path.bootpath), "%s", bootfile);
87 	f->f_devdata = &netdev_sock;
88 	netdev_opens++;
89 	return 0;
90  bad:
91 	netif_close(netdev_sock);
92 	netdev_sock = -1;
93 	return error;
94 }
95 
96 int
net_close(struct open_file * f)97 net_close(struct open_file *f)
98 
99 {
100 	f->f_devdata = NULL;
101 	if (--netdev_opens > 0)
102 		return 0;
103 	netif_close(netdev_sock);
104 	netdev_sock = -1;
105 	return 0;
106 }
107 
108 int
net_strategy(void * devdata,int rw,daddr_t dblk,size_t size,void * p,size_t * rsize)109 net_strategy(void *devdata, int rw, daddr_t dblk, size_t size,
110 	void *p, size_t *rsize)
111 {
112 
113 	return EIO;
114 }
115