dev_net.c revision 1.1 1 /* $NetBSD: dev_net.c,v 1.1 2018/09/03 00:04:02 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include "efiboot.h"
30
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <net/if.h>
34 #include <netinet/in.h>
35
36 #include <lib/libsa/net.h>
37 #include <lib/libsa/netif.h>
38 #include <lib/libsa/bootparam.h>
39 #include <lib/libsa/bootp.h>
40
41 #include "dev_net.h"
42
43 static int net_socket = -1;
44
45 int
46 net_open(struct open_file *f, ...)
47 {
48 va_list ap;
49 char *devname;
50 int error;
51
52 va_start(ap, f);
53 devname = va_arg(ap, char *);
54 va_end(ap);
55
56 if (net_socket < 0)
57 net_socket = netif_open(devname);
58 if (net_socket < 0)
59 return ENXIO;
60
61 if (myip.s_addr == INADDR_ANY) {
62 bootp(net_socket);
63
64 if (myip.s_addr == INADDR_ANY) {
65 error = EIO;
66 goto fail;
67 }
68
69 printf("boot: client ip: %s\n", inet_ntoa(myip));
70 printf("boot: server ip: %s\n", inet_ntoa(rootip));
71 }
72
73 f->f_devdata = &net_socket;
74
75 return 0;
76
77 fail:
78 printf("net_open failed: %d\n", error);
79 netif_close(net_socket);
80 net_socket = -1;
81 return error;
82 }
83
84 int
85 net_close(struct open_file *f)
86 {
87 f->f_devdata = NULL;
88
89 return 0;
90 }
91
92 int
93 net_strategy(void *devdata, int rw, daddr_t dblk, size_t size, void *buf, size_t *rsize)
94 {
95 return EIO;
96 }
97