Home | History | Annotate | Line # | Download | only in efiboot
      1 /*	$NetBSD: dev_net.c,v 1.3 2024/08/15 05:58:34 skrll 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 #include <lib/libsa/nfs.h>
     41 
     42 #include "dev_net.h"
     43 
     44 static int	net_socket = -1;
     45 
     46 int
     47 net_open(struct open_file *f, ...)
     48 {
     49 	va_list ap;
     50 	char *devname;
     51 	int error;
     52 
     53 	va_start(ap, f);
     54 	devname = va_arg(ap, char *);
     55 	va_end(ap);
     56 
     57 	if (net_socket < 0)
     58 		net_socket = netif_open(devname);
     59 	if (net_socket < 0)
     60 		return ENXIO;
     61 
     62 	if (myip.s_addr == INADDR_ANY) {
     63 		bootp(net_socket);
     64 
     65 		if (myip.s_addr == INADDR_ANY) {
     66 			error = EIO;
     67 			goto fail;
     68 		}
     69 
     70 		printf("boot: client ip: %s\n", inet_ntoa(myip));
     71 		printf("boot: server ip: %s\n", inet_ntoa(rootip));
     72 		if (rootpath[0] != '\0')
     73 			printf("boot: server path: %s\n", rootpath);
     74 		if (bootfile[0] != '\0')
     75 			printf("boot: file name: %s\n", bootfile);
     76 	}
     77 
     78 	if (rootpath[0] != '\0') {
     79 		error = nfs_mount(net_socket, rootip, rootpath);
     80 		if (error) {
     81 			printf("NFS mount error=%d\n", errno);
     82 			goto fail;
     83 		}
     84 	}
     85 
     86 	f->f_devdata = &net_socket;
     87 
     88 	return 0;
     89 
     90 fail:
     91 	printf("net_open failed: %d\n", error);
     92 	netif_close(net_socket);
     93 	net_socket = -1;
     94 	return error;
     95 }
     96 
     97 int
     98 net_close(struct open_file *f)
     99 {
    100 	f->f_devdata = NULL;
    101 
    102 	return 0;
    103 }
    104 
    105 int
    106 net_strategy(void *devdata, int rw, daddr_t dblk, size_t size, void *buf, size_t *rsize)
    107 {
    108 	return EIO;
    109 }
    110