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