Home | History | Annotate | Line # | Download | only in boot2440
      1  1.1  nisimura /*-
      2  1.1  nisimura  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      3  1.1  nisimura  * All rights reserved.
      4  1.1  nisimura  *
      5  1.1  nisimura  * This code is derived from software contributed to The NetBSD Foundation
      6  1.1  nisimura  * by Paul Fleischer <paul (at) xpg.dk>
      7  1.1  nisimura  *
      8  1.1  nisimura  * Redistribution and use in source and binary forms, with or without
      9  1.1  nisimura  * modification, are permitted provided that the following conditions
     10  1.1  nisimura  * are met:
     11  1.1  nisimura  * 1. Redistributions of source code must retain the above copyright
     12  1.1  nisimura  *    notice, this list of conditions and the following disclaimer.
     13  1.1  nisimura  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  nisimura  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  nisimura  *    documentation and/or other materials provided with the distribution.
     16  1.1  nisimura  *
     17  1.1  nisimura  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  1.1  nisimura  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  1.1  nisimura  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  1.1  nisimura  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  1.1  nisimura  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  1.1  nisimura  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  1.1  nisimura  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  1.1  nisimura  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  1.1  nisimura  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  1.1  nisimura  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  1.1  nisimura  * POSSIBILITY OF SUCH DAMAGE.
     28  1.1  nisimura  */
     29  1.1  nisimura #include <sys/param.h>
     30  1.1  nisimura 
     31  1.1  nisimura #include <netinet/in.h>
     32  1.1  nisimura #include <netinet/in_systm.h>
     33  1.1  nisimura 
     34  1.1  nisimura #include <lib/libsa/stand.h>
     35  1.1  nisimura #include <lib/libsa/net.h>
     36  1.1  nisimura #include <lib/libsa/bootp.h>
     37  1.1  nisimura #include <lib/libsa/nfs.h>
     38  1.1  nisimura 
     39  1.1  nisimura #include <lib/libkern/libkern.h>
     40  1.1  nisimura 
     41  1.1  nisimura 
     42  1.1  nisimura extern int netif_open(void*);
     43  1.1  nisimura extern int netif_init(unsigned int tag);
     44  1.1  nisimura 
     45  1.2  nisimura extern struct in_addr servip;
     46  1.2  nisimura 
     47  1.2  nisimura static int netdev_sock = -1;
     48  1.2  nisimura 
     49  1.1  nisimura int
     50  1.1  nisimura net_open(struct open_file *f, ...)
     51  1.1  nisimura {
     52  1.1  nisimura 	va_list ap;
     53  1.2  nisimura 	char *path, *proto;
     54  1.2  nisimura 	int error = 0;
     55  1.1  nisimura 
     56  1.1  nisimura 	if( netif_init(0) == 0 ) {
     57  1.1  nisimura 		error = ENODEV;
     58  1.1  nisimura 		goto out;
     59  1.1  nisimura 	}
     60  1.1  nisimura 
     61  1.1  nisimura 	va_start(ap, f);
     62  1.1  nisimura 	path = va_arg(ap, char *);
     63  1.2  nisimura 	proto = va_arg(ap, char *);
     64  1.2  nisimura 	va_end(ap);
     65  1.2  nisimura 	if ((netdev_sock = netif_open(path)) < 0) {
     66  1.1  nisimura 		error = errno;
     67  1.1  nisimura 		goto out;
     68  1.1  nisimura 	}
     69  1.1  nisimura 
     70  1.1  nisimura 	/* send DHCP request */
     71  1.2  nisimura 	bootp(netdev_sock);
     72  1.1  nisimura 
     73  1.1  nisimura 	/* IP address was not found */
     74  1.1  nisimura 	if (myip.s_addr == 0) {
     75  1.1  nisimura 		error = ENOENT;
     76  1.1  nisimura 		goto out;
     77  1.1  nisimura 	}
     78  1.1  nisimura 
     79  1.1  nisimura 	if (path[0] != '\0') {
     80  1.1  nisimura 		char *c;
     81  1.1  nisimura 		char ip_addr[200];
     82  1.1  nisimura 		/* Parse path specification as IP-ADDR:PATH and overwrite
     83  1.1  nisimura 		   rootip and rootpath with those
     84  1.1  nisimura 		 */
     85  1.1  nisimura 
     86  1.1  nisimura 		for(c=path; *c != '\0' && *c != ':'; c++);
     87  1.1  nisimura 
     88  1.1  nisimura 		if (*c == ':') {
     89  1.1  nisimura 			char *filename;
     90  1.1  nisimura 			strncpy(ip_addr, path, (c-path));
     91  1.1  nisimura 			ip_addr[(c-path)] = '\0';
     92  1.1  nisimura 			printf("IP addr: %s\n", ip_addr);
     93  1.1  nisimura 			rootip.s_addr = inet_addr(ip_addr);
     94  1.1  nisimura 
     95  1.1  nisimura 			c++;
     96  1.1  nisimura 			/* Finally, strip away file-component and copy it to
     97  1.1  nisimura 			   bootfile */
     98  1.1  nisimura 			for(filename = c+strlen(c); *filename != '/' && filename > c; filename--);
     99  1.1  nisimura 			strncpy(rootpath, c, (filename-c));
    100  1.1  nisimura 			rootpath[(filename-c)] = '\0';
    101  1.1  nisimura 			printf("Root path: %s\n", rootpath);
    102  1.1  nisimura 			strcpy(bootfile, ++filename);
    103  1.1  nisimura 			printf("Bootfile: %s\n", bootfile);
    104  1.2  nisimura 		} else {
    105  1.2  nisimura 			/* No ':' found, assume it's just a filename */
    106  1.2  nisimura 			strcpy(bootfile, path);
    107  1.1  nisimura 		}
    108  1.1  nisimura 	}
    109  1.1  nisimura 
    110  1.1  nisimura 	/* boot filename was not found */
    111  1.1  nisimura 	if (bootfile[0] == '\0')
    112  1.1  nisimura 		strcpy(bootfile, "netbsd");
    113  1.1  nisimura 
    114  1.2  nisimura 	if (strcmp(proto, "nfs") == 0
    115  1.2  nisimura 	    && (nfs_mount(netdev_sock, rootip, rootpath) != 0)) {
    116  1.1  nisimura 		error = errno;
    117  1.1  nisimura 		goto out;
    118  1.1  nisimura 	}
    119  1.2  nisimura 
    120  1.2  nisimura 	f->f_devdata = &netdev_sock;
    121  1.1  nisimura out:
    122  1.1  nisimura 	return (error);
    123  1.1  nisimura }
    124  1.1  nisimura 
    125  1.1  nisimura int
    126  1.1  nisimura net_close(struct open_file *f)
    127  1.1  nisimura 
    128  1.1  nisimura {
    129  1.1  nisimura 	return (0);
    130  1.1  nisimura }
    131  1.1  nisimura 
    132  1.1  nisimura int
    133  1.1  nisimura net_strategy(void *d, int f, daddr_t b, size_t s, void *buf, size_t *r)
    134  1.1  nisimura {
    135  1.1  nisimura 
    136  1.1  nisimura 	return (EIO);
    137  1.1  nisimura }
    138