Code: Select all
//net.c
#include "bootpack.h"
#include <stdio.h>
#define PCI_BUS_CONFIG(bus,dev,func,reg) 0x80000000|(bus<<16)|(dev&0x1f<<11)|(func&0x07<<8)|(reg&0x3f<<2)|0x00
char *ns;
int nic_flag;
int ioaddr;
char *nic_data;
char *nic_rev_data;
char nstatus;
char nstatus2;
void inthandler2b(int *esp)
/* */
{
io_out8(PIC1_OCW2, 0x63);//nic interrupt
io_out8(PIC0_OCW2, 0x62);
nic_flag = 1;
return;
}
void make_arp_pac(){
nic_data[0] = 0xff;
nic_data[1] = 0xff;
nic_data[2] = 0xff;
nic_data[3] = 0xff;
nic_data[4] = 0xff;
nic_data[5] = 0xff;
nic_data[6] = 0xb0;
nic_data[7] = 0xc4;
nic_data[8] = 0x20;
nic_data[9] = 0x00;
nic_data[10] = 0x00;
nic_data[11] = 0x01;
nic_data[12] = 0x00;
nic_data[13] = 0x01;//frame type
nic_data[14] = 0x00;
nic_data[15] = 0x01;//hardware type
nic_data[16] = 0x08;
nic_data[17] = 0x06;//arp type
nic_data[18] = 0x06;
nic_data[19] = 0x04;
nic_data[20] = 0x00;
nic_data[21] = 0x01;//op code
nic_data[22] = 0xb0;
nic_data[23] = 0xc4;
nic_data[24] = 0x20;
nic_data[25] = 0x00;
nic_data[26] = 0x00;
nic_data[27] = 0x01;//source mac address
nic_data[28] = 0xC0;
nic_data[29] = 0xA8;
nic_data[30] = 0x00;
nic_data[31] = 0x65;//source ip address
nic_data[32] = 0x00;
nic_data[33] = 0x00;
nic_data[34] = 0x00;
nic_data[35] = 0x00;
nic_data[36] = 0x00;
nic_data[37] = 0x00;
nic_data[38] = 0xC0;
nic_data[39] = 0xA8;
nic_data[40] = 0x00;
nic_data[41] = 0x01;//destnation ip address
}
void ne_send_packets(struct BOOTINFO *binfo,int lba_num,short count){
int addr,data;
char bus,dev,func,reg;
reg = 0x10;
int i;
int net_mem = 0x40;
ioaddr = 0xc040;
addr = PCI_BUS_CONFIG(0,3,0,reg);
io_out32(0xCF8,addr);
io_out32(0xCFC,ioaddr);
make_arp_pac();
io_out8(ioaddr+0x00,0x22);//COMMAND register set to "start" and "nodma"
io_out8(ioaddr+0x04,0x40);//TPSR tx_start_page
io_out8(ioaddr+0x05,count&0xff);// TBCR0 tx_bytes low
io_out8(ioaddr+0x06,(count>>8)&0xff);// TBCR1 tx_bytes high
io_out8(ioaddr+0x07,(1<<6));//Remote DMA complete? ISR
io_out8(ioaddr+0x08,0x00);//loaded with 0x00 (low)
io_out8(ioaddr+0x09,0x40);//target page number (high)
io_out8(ioaddr+0x0a,count&0xff);//low byte count
io_out8(ioaddr+0x0b,(count>>8)&0xff);//high byte count
//io_out8(ioaddr+0x0f,0x02);//set IMR receive irq allowed
for(i=0;i<42;i++){
io_out8(ioaddr+0x10,nic_data[i]);
}
do{
nstatus = io_in8(ioaddr+0x07);
}while((nstatus&0x40)==0);
io_out8(ioaddr+0x00,0x14);//COMMAND register set to "start" and "remote write DMA" (0x12)
do{
nstatus = io_in8(ioaddr+0x07);
}while((nstatus&0x02)==0);
//sprintf(ns,"nic int %x",nstatus);
//putfonts8_asc(binfo->vram, binfo->scrnx, 0, 32, COL8_FFFFFF, ns);
}
void init_net(struct BOOTINFO *binfo){
sprintf(ns,"init net");
putfonts8_asc(binfo->vram, binfo->scrnx, 0, 16, COL8_FFFFFF, ns);
short count = 42;
//ne_send_packets(binfo,0,count);
int addr,data;
char bus,dev,func,reg;
reg = 0x10;
int i;
int net_mem = 0x40;
ioaddr = 0xc040;
addr = PCI_BUS_CONFIG(0,3,0,reg);
io_out32(0xCF8,addr);
io_out32(0xCFC,ioaddr);
ne_send_packets(binfo,0,count);
io_out8(ioaddr+0x00,0x0A);//start the controller and set read bit
io_out8(ioaddr+0x01,0x40);//set start page address
io_out8(ioaddr+0x02,0x43);//set stop page address
io_out8(ioaddr+0x0C,0x02|0x10);//set runts_ok bit and promise bit
io_out8(ioaddr+0x0D,0x02);//set TCR loop mode 01
io_out8(ioaddr+0x0E,0x08);//set loop bit of DCR
//io_out8(ioaddr+0x0F,0x01);//set IMR receive irq allowed
io_out8(ioaddr+0x00,0x04);//set command register
do{
nstatus2 = io_in8(ioaddr+0x07);
}while((nstatus2&0x01)==0);
//sprintf(ns,"nic status %x",nstatus2);
//putfonts8_asc(binfo->vram, binfo->scrnx, 0, 32, COL8_FFFFFF, ns);
for(i=0;i<50;i++){
nic_rev_data[i] = io_in8(ioaddr+0x10);
}
do{
nstatus2 = io_in8(ioaddr+0x07);
}while((nstatus2&0x40)==0);
sprintf(ns,"nic status %x",nstatus2);
putfonts8_asc(binfo->vram, binfo->scrnx, 0, 32, COL8_FFFFFF, ns);
sprintf(ns,"nic data %x,%x,%x,%x,%x,%x,%x,%x",nic_rev_data[0],nic_rev_data[1],nic_rev_data[2],nic_rev_data[3],nic_rev_data[4],nic_rev_data[5],nic_rev_data[6],nic_rev_data[7]);
putfonts8_asc(binfo->vram, binfo->scrnx, 0, 48, COL8_FFFFFF, ns);
sprintf(ns,"nic data %x,%x,%x,%x,%x,%x,%x,%x",nic_rev_data[8],nic_rev_data[9],nic_rev_data[10],nic_rev_data[11],nic_rev_data[12],nic_rev_data[13],nic_rev_data[14],nic_rev_data[15]);
putfonts8_asc(binfo->vram, binfo->scrnx, 0, 64, COL8_FFFFFF, ns);
sprintf(ns,"nic data %x,%x,%x,%x,%x,%x,%x,%x",nic_rev_data[16],nic_rev_data[17],nic_rev_data[18],nic_rev_data[19],nic_rev_data[20],nic_rev_data[21],nic_rev_data[22],nic_rev_data[23]);
putfonts8_asc(binfo->vram, binfo->scrnx, 0, 80, COL8_FFFFFF, ns);
sprintf(ns,"nic data %x,%x,%x,%x,%x,%x,%x,%x",nic_rev_data[24],nic_rev_data[25],nic_rev_data[26],nic_rev_data[27],nic_rev_data[28],nic_rev_data[29],nic_rev_data[30],nic_rev_data[31]);
putfonts8_asc(binfo->vram, binfo->scrnx, 0, 96, COL8_FFFFFF, ns);
sprintf(ns,"nic data %x,%x,%x,%x,%x,%x,%x,%x",nic_rev_data[32],nic_rev_data[33],nic_rev_data[34],nic_rev_data[35],nic_rev_data[36],nic_rev_data[37],nic_rev_data[38],nic_rev_data[39]);
putfonts8_asc(binfo->vram, binfo->scrnx, 0, 112, COL8_FFFFFF, ns);
}