xref: /netbsd-src/sys/arch/mvme68k/stand/sboot/etherfun.c (revision 5993922d717f1fd012ade8bdea36896f10f96656)
1*5993922dShe /*	$NetBSD: etherfun.c,v 1.12 2011/09/29 09:18:17 he Exp $	*/
2015e898cSperry 
30b036e11Schuck /*
40b036e11Schuck  * Copyright (c) 1995 Charles D. Cranor and Seth Widoff
50b036e11Schuck  * All rights reserved.
60b036e11Schuck  *
70b036e11Schuck  * Redistribution and use in source and binary forms, with or without
80b036e11Schuck  * modification, are permitted provided that the following conditions
90b036e11Schuck  * are met:
100b036e11Schuck  * 1. Redistributions of source code must retain the above copyright
110b036e11Schuck  *    notice, this list of conditions and the following disclaimer.
120b036e11Schuck  * 2. Redistributions in binary form must reproduce the above copyright
130b036e11Schuck  *    notice, this list of conditions and the following disclaimer in the
140b036e11Schuck  *    documentation and/or other materials provided with the distribution.
150b036e11Schuck  *
160b036e11Schuck  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
170b036e11Schuck  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
180b036e11Schuck  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
190b036e11Schuck  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
200b036e11Schuck  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
210b036e11Schuck  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
220b036e11Schuck  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
230b036e11Schuck  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
240b036e11Schuck  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
250b036e11Schuck  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
260b036e11Schuck  */
270b036e11Schuck /* etherfun.c */
280b036e11Schuck 
290b036e11Schuck #include "sboot.h"
300b036e11Schuck #include "etherfun.h"
310b036e11Schuck 
320b036e11Schuck /* Construct and send a rev arp packet */
330b036e11Schuck void
do_rev_arp(void)34a07f7c80Stsutsui do_rev_arp(void)
350b036e11Schuck {
360b036e11Schuck 	int i;
370b036e11Schuck 
380b036e11Schuck 	for (i = 0; i < 6; i++) {
390b036e11Schuck 		eh->ether_dhost[i] = 0xff;
400b036e11Schuck 	}
41c1ab2b54Sscw 	memcpy(eh->ether_shost, myea, 6);
420b036e11Schuck 	eh->ether_type = ETYPE_RARP;
430b036e11Schuck 
440b036e11Schuck 	rarp->ar_hrd = 1;	/* hardware type is 1 */
450b036e11Schuck 	rarp->ar_pro = PTYPE_IP;
460b036e11Schuck 	rarp->ar_hln = 6;	/* length of hardware address is 6 bytes */
470b036e11Schuck 	rarp->ar_pln = 4;	/* length of ip address is 4 byte */
480b036e11Schuck 	rarp->ar_op = OPCODE_RARP;
49c1ab2b54Sscw 	memcpy(rarp->arp_sha, myea, sizeof(myea));
50c1ab2b54Sscw 	memcpy(rarp->arp_tha, myea, sizeof(myea));
510b036e11Schuck 	for (i = 0; i < 4; i++) {
520b036e11Schuck 		rarp->arp_spa[i] = rarp->arp_tpa[i] = 0x00;
530b036e11Schuck 	}
540b036e11Schuck 
550b036e11Schuck 	le_put(buf, 76);
560b036e11Schuck }
570b036e11Schuck 
580a600be8Swiz /* Receive and disassemble the rev_arp reply */
590b036e11Schuck 
600b036e11Schuck int
get_rev_arp(void)61a07f7c80Stsutsui get_rev_arp(void)
620b036e11Schuck {
63a07f7c80Stsutsui 
640b036e11Schuck 	le_get(buf, sizeof(buf), 6);
650b036e11Schuck 	if (eh->ether_type == ETYPE_RARP && rarp->ar_op == OPCODE_REPLY) {
66c1ab2b54Sscw 		memcpy(myip, rarp->arp_tpa, sizeof(rarp->arp_tpa));
67c1ab2b54Sscw 		memcpy(servip, rarp->arp_spa, sizeof(rarp->arp_spa));
68c1ab2b54Sscw 		memcpy(servea, rarp->arp_sha, sizeof(rarp->arp_sha));
690b036e11Schuck 		return 1;
700b036e11Schuck 	}
710b036e11Schuck 	return 0;
720b036e11Schuck }
730b036e11Schuck 
740b036e11Schuck /* Try to get a reply to a rev arp request */
750b036e11Schuck 
760b036e11Schuck int
rev_arp(void)77a07f7c80Stsutsui rev_arp(void)
780b036e11Schuck {
790b036e11Schuck 	int tries = 0;
80a07f7c80Stsutsui 
810b036e11Schuck 	while (tries < 5) {
820b036e11Schuck 		do_rev_arp();
830b036e11Schuck 		if (get_rev_arp()) {
840b036e11Schuck 			return 1;
850b036e11Schuck 		}
860b036e11Schuck 		tries++;
870b036e11Schuck 	}
880b036e11Schuck 	return 0;
890b036e11Schuck }
900b036e11Schuck 
910b036e11Schuck /* Send a tftp read request or acknowledgement
920b036e11Schuck    mesgtype 0 is a read request, 1 is an aknowledgement */
93*5993922dShe const char HEXDIGITS[16] = "0123456789ABCDEF";
940b036e11Schuck 
950b036e11Schuck void
do_send_tftp(int mesgtype)960b036e11Schuck do_send_tftp(int mesgtype)
970b036e11Schuck {
980b036e11Schuck 	u_long res, iptmp, lcv;
992d245c62Smrg 	u_char *tot;
1000b036e11Schuck 
1010b036e11Schuck 	if (mesgtype == 0) {
1020b036e11Schuck 		tot = tftp_r + (sizeof(MSG) - 1);
1030b036e11Schuck 		myport = (u_short)time();
104a07f7c80Stsutsui 		if (myport < 1000)
105a07f7c80Stsutsui 			myport += 1000;
1060b036e11Schuck 		servport = FTP_PORT; /* to start */
1070b036e11Schuck 	} else {
1082d245c62Smrg 		tot = (u_char *)tftp_a + 4;
1090b036e11Schuck 	}
1100b036e11Schuck 
111c1ab2b54Sscw 	memcpy (eh->ether_dhost, servea, sizeof(servea));
112c1ab2b54Sscw 	memcpy (eh->ether_shost, myea, sizeof(myea));
1130b036e11Schuck 	eh->ether_type = ETYPE_IP;
1140b036e11Schuck 
1150b036e11Schuck 	iph->ip_v = IP_VERSION;
1160b036e11Schuck 	iph->ip_hl = IP_HLEN;
1170b036e11Schuck 	iph->ip_tos = 0;	/* type of service is 0 */
1180b036e11Schuck 	iph->ip_id = 0;		/* id field is 0 */
1190b036e11Schuck 	iph->ip_off = IP_DF;
1200b036e11Schuck 	iph->ip_ttl = 3;	/* time to live is 3 seconds/hops */
1210b036e11Schuck 	iph->ip_p = IPP_UDP;
122c1ab2b54Sscw 	memcpy(iph->ip_src, myip, sizeof(myip));
123c1ab2b54Sscw 	memcpy(iph->ip_dst, servip, sizeof(servip));
1240b036e11Schuck 	iph->ip_sum = 0;
1252d245c62Smrg 	iph->ip_len = tot - (u_char *)iph;
1260b036e11Schuck 	res = oc_cksum(iph, sizeof(struct ip), 0);
1270b036e11Schuck 	iph->ip_sum = 0xffff & ~res;
1280b036e11Schuck 	udph->uh_sport = myport;
1290b036e11Schuck 	udph->uh_dport = servport;
1300b036e11Schuck 	udph->uh_sum = 0;
1310b036e11Schuck 
1320b036e11Schuck 	if (mesgtype)  {
1330b036e11Schuck 		tftp_a->op_code = FTPOP_ACKN;
1340b036e11Schuck 		tftp_a->block = (u_short)(mesgtype);
1350b036e11Schuck 	} else {
136c1ab2b54Sscw 		memcpy (&iptmp, myip, sizeof(iptmp));
137c1ab2b54Sscw 		memcpy(tftp_r, MSG, (sizeof(MSG)-1));
1380b036e11Schuck 		for (lcv = 9; lcv >= 2; lcv--) {
139362a4a0bSchristos 			tftp_r[lcv] = HEXDIGITS[iptmp & 0xF];
1400b036e11Schuck 
1410b036e11Schuck 			iptmp = iptmp >> 4;
1420b036e11Schuck 		}
1430b036e11Schuck 	}
1440b036e11Schuck 
1452d245c62Smrg 	udph->uh_ulen = tot - (u_char *)udph;
1460b036e11Schuck 
1470b036e11Schuck 	le_put(buf, tot - buf);
1480b036e11Schuck }
1490b036e11Schuck 
1500b036e11Schuck /* Attempt to tftp a file and read it into memory */
1510b036e11Schuck 
1520b036e11Schuck int
do_get_file(void)153a07f7c80Stsutsui do_get_file(void)
1540b036e11Schuck {
1550b036e11Schuck 	int fail = 0, oldlen;
1560b036e11Schuck 	char *loadat = (char *)LOAD_ADDR;
1570b036e11Schuck 	last_ack = 0;
1580b036e11Schuck 
1590b036e11Schuck 	do_send_tftp(READ);
160a07f7c80Stsutsui 	for (;;) {
161456dff6cSwiz 		if (le_get(buf, sizeof(buf), 5) == 0) { /* timeout occurred */
1620b036e11Schuck 			if (last_ack) {
1630b036e11Schuck 				do_send_tftp(last_ack);
1640b036e11Schuck 			} else {
1650b036e11Schuck 				do_send_tftp(READ);
1660b036e11Schuck 			}
1670b036e11Schuck 			fail++;
1680b036e11Schuck 			if (fail > 5) {
1690b036e11Schuck 			        printf("\n");
1700b036e11Schuck 				return 1;
1710b036e11Schuck 			}
1720b036e11Schuck 		} else {
1730b036e11Schuck 			printf("%x \r", tftp->info.block*512);
174a07f7c80Stsutsui 			if ((eh->ether_type != ETYPE_IP) ||
175a07f7c80Stsutsui 			    (iph->ip_p != IPP_UDP)) {
1760b036e11Schuck 				fail++;
1770b036e11Schuck 				continue;
1780b036e11Schuck 			}
1790b036e11Schuck 			if (servport == FTP_PORT) servport = udph->uh_sport;
1800b036e11Schuck 			if (tftp->info.op_code == FTPOP_ERR) {
1810b036e11Schuck 				printf("TFTP: Download error %d: %s\n",
1820b036e11Schuck 				    tftp->info.block, tftp->data);
1830b036e11Schuck 				return 1;
1840b036e11Schuck 			}
185a07f7c80Stsutsui 			if (tftp->info.block != last_ack + 1) {
186a07f7c80Stsutsui 				/* we received the wrong block */
1870b036e11Schuck 				if (tftp->info.block < last_ack +1) {
188a07f7c80Stsutsui 					/* ackn whatever we received */
189a07f7c80Stsutsui 					do_send_tftp(tftp->info.block);
1900b036e11Schuck 				} else {
191a07f7c80Stsutsui 					/* ackn the last confirmed block */
192a07f7c80Stsutsui 					do_send_tftp(last_ack);
1930b036e11Schuck 				}
1940b036e11Schuck 				fail++;
1950b036e11Schuck 			} else {		/* we got the right block */
1960b036e11Schuck 				fail = 0;
1970b036e11Schuck 				last_ack++;
1980b036e11Schuck 				oldlen = udph->uh_ulen;
1990b036e11Schuck 				do_send_tftp( last_ack );
200a07f7c80Stsutsui #if 0
201a07f7c80Stsutsui 				printf("memcpy %x %x %d\n", loadat,
202a07f7c80Stsutsui 				    &tftp->data, oldlen - 12);
203a07f7c80Stsutsui #endif
204c1ab2b54Sscw 				memcpy(loadat, &tftp->data, oldlen - 12);
2050b036e11Schuck 				loadat += oldlen - 12;
2060b036e11Schuck 				if (oldlen < (8 + 4 + 512)) {
2070b036e11Schuck 					printf("\n");
2080b036e11Schuck 					return 0;
2090b036e11Schuck 				}
2100b036e11Schuck 			}
2110b036e11Schuck 		}
2120b036e11Schuck 	}
2130b036e11Schuck 	printf("\n");
2140b036e11Schuck 	return 0;
2150b036e11Schuck }
216