eth.c

来自「ADS下的bios工程」· C语言 代码 · 共 162 行

C
162
字号
#include <bios/s3c2410x.h>#include <bios/config_param.h>#include <bios/stdio.h>#include <bios/netdev.h>#include <bios/string.h>#include <bios/config.h>#undef DEBUG_S3C4_ETH#ifdef DEBUG_S3C4_ETH	#define DEBUG_ETH(fmt, args...) printf("%s-%s()[%d]: " fmt, __FILE__, __FUNCTION__, __LINE__, args)#else	#define DEBUG_ETH(fmt, args...)#endif/* extern functions */extern int arp_send(struct netdev *nd, u32 ip_addr);extern int arp_reply(struct netdev *nd, u32 ip_addr);extern struct IIC_MEM_MAP sys_params;/* Ethernet packet buffer */static u8 ether_buffer[1600];/* Function proto types */static struct buflist *eth_header(struct netdev *nd, u8 *to, u16 proto, struct buflist *blp);static int             eth_send(struct netdev *nd, struct buflist *data);static int             eth_recv(struct netdev *nd, u16 proto, u8 *buffer);int                    eth_setup(struct netdev *nd);int eth_setup(struct netdev *nd){	int i;	//unsigned char cmdline[128];       /* Fill the dev fields */       nd->hw_addr_len = 6;       /* setup broadcast address */       for (i = 0; i < nd->hw_addr_len; i++)		nd->hw_broadcast[i] = 0xff;       DEBUG_ETH("%s\n", "System params are in NAND Flash");       /* Get haredware MAC address */       for(i = 0; i < nd->hw_addr_len; i++)	     nd->hw_addr[i] = MAC_ADDR[i];        /* network configurations */       nd->ip_addr      = IP_ADDRESS;       nd->serv_ip_addr = SERVER_IP;       nd->default_gw   = DEFAULT_GW;       sprintf(nd->boot_file,"%s",BOOT_FILE);               /* setup ethernet transfer functions */	       nd->eth_header = &eth_header;        nd->eth_send = &eth_send;        nd->eth_recv = &eth_recv;          return 0;}static struct buflist *eth_header(struct netdev *nd, u8 *to, u16 proto, struct buflist *blp){	static struct buflist bl;	static struct ethhdr  eth;	bl.data = &eth;	bl.size = 14;	bl.next = blp;	memcpy(eth.eth_source, nd->hw_addr, nd->hw_addr_len);	memcpy(eth.eth_dest, to, nd->hw_addr_len);	eth.eth_proto = htons(proto);	return &bl;}static int eth_send(struct netdev *nd, struct buflist *data){	struct buflist *blp;	int size = 0;	for (blp = data; blp; blp = blp->next) {		memcpy(ether_buffer + size, blp->data, blp->size);		size += blp->size;	}        DEBUG_ETH("pkt size(%d)\n", size);	return nd->send(nd, ether_buffer, size);}static int eth_recv(struct netdev *nd, u16 proto, u8 *buffer){	struct ethhdr *eth = (struct ethhdr *)ether_buffer;	int bytes;	unsigned long tmp;	do {                DEBUG_ETH("%x\n", eth->eth_proto);		bytes = nd->recv(nd, ether_buffer);		if (bytes < 14)  			continue;		if (!memeq(eth->eth_dest, nd->hw_addr, nd->hw_addr_len))                {                      DEBUG_ETH("(%c)\n", '$');                      if (eth->eth_proto == htons(ETH_P_ARP)) 		      {                             DEBUG_ETH("(%c)\n", '?');			     tmp= ether_buffer[21]|ether_buffer[20]<<8;			     if (tmp == 2) return 0;			     tmp=0;			     memcpy(nd->tha_addr,eth->eth_source,nd->hw_addr_len);			     tmp= ether_buffer[31]<<24|ether_buffer[30]<<16|			     	ether_buffer[29]<<8|ether_buffer[28];                             arp_reply(nd,tmp);		             //continue; /* 2001-2-24 */		      }	      	      return 0;   /* add 2001-2-24 */                }		if (eth->eth_proto == htons(proto)) 		{                        DEBUG_ETH("(%c)Proto(%x)\n", '+',eth->eth_proto);			bytes -= 14;			memcpy(buffer, ether_buffer + 14, bytes);			break;		}		else 		{                        DEBUG_ETH("(%c)Protocol(%x)\n", '#',eth->eth_proto);                        if (eth->eth_proto == htons(ETH_P_ARP)) 			{		             if (!memeq(eth->eth_source, nd->hw_addr, nd->hw_addr_len)) {                                   DEBUG_ETH("(%c)ARP recvd\n", '-');			     	   tmp= ether_buffer[21]|ether_buffer[20]<<8;			     	   if (tmp == 2) return 0;			           tmp=0;			           memcpy(nd->tha_addr,eth->eth_source,nd->hw_addr_len);			           tmp= ether_buffer[31]<<24|ether_buffer[30]<<16|			     	      ether_buffer[29]<<8|ether_buffer[28];                                   arp_reply(nd,tmp);                             }		             //continue;		        }			return 0;		}	} while (bytes);	return bytes;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?