git-svn-id: https://spexeah.com:8443/svn/Asuro@484 6dbc8c32-bb84-406f-8558-d1cf31a0ab0c

This commit is contained in:
kieron
2018-04-11 14:21:06 +00:00
parent 3bed07c80a
commit c8b3b14893
43 changed files with 340 additions and 108 deletions

View File

@ -3,7 +3,7 @@ unit netutils;
interface
uses
util, nettypes, console, lmemorymanager;
tracer, util, nettypes, console, lmemorymanager;
procedure copyMAC(src : puint8; dst : puint8);
procedure copyIPv4(src : puint8; dst : puint8);
@ -21,13 +21,15 @@ var
i : uint8;
begin
push_trace('netutils.IPEqual');
IPEqual:= true;
for i:=0 to 3 do begin
if ip1[i] <> ip2[i] then begin
IPEqual:= false;
exit;
break;
end;
end;
pop_trace;
end;
function MACEqual(mac1 : puint8; mac2 : puint8) : boolean;
@ -35,13 +37,15 @@ var
i : uint8;
begin
push_trace('netutils.MACEqual');
MACEqual:= true;
for i:=0 to 5 do begin
if mac1[i] <> mac2[i] then begin
MACEqual:= false;
exit;
break;
end;
end;
pop_trace;
end;
procedure writeIPv4Address(ip : puint8);
@ -49,12 +53,14 @@ var
i : integer;
begin
push_trace('netutils.writeIPv4Address');
console.writeint(ip[0]);
for i:=1 to 3 do begin
console.writestring('.');
console.writeint(ip[i]);
end;
console.writestringln(' ');
pop_trace;
end;
procedure writeMACAddress(mac : puint8);
@ -62,23 +68,29 @@ var
i : integer;
begin
push_trace('netutils.writeMACAddress');
console.writehexpair(mac[0]);
for i:=1 to 5 do begin
console.writestring(':');
console.writehexpair(mac[i]);
end;
console.writestringln(' ');
pop_trace;
end;
function newPacketContext : PPacketContext;
begin
push_trace('netutils.newPacketContext');
newPacketContext:= PPacketContext(kalloc(sizeof(TPacketContext)));
memset(uint32(newPacketContext), 0, sizeof(TPacketContext));
pop_trace;
end;
procedure freePacketContext(p_context : PPacketContext);
begin
push_trace('netutils.freePacketContext');
kfree(void(p_context));
pop_trace;
end;
procedure copyMAC(src : puint8; dst : puint8);
@ -86,9 +98,11 @@ var
i : uint8;
begin
push_trace('netutils.copyMAC');
for i:=0 to 5 do begin
dst[i]:= src[i];
end;
pop_trace;
end;
procedure copyIPv4(src : puint8; dst : puint8);
@ -96,9 +110,11 @@ var
i : uint8;
begin
push_trace('netutils.copyIPv4');
for i:=0 to 3 do begin
dst[i]:= src[i];
end;
pop_trace;
end;
end.

View File

@ -3,6 +3,7 @@ unit net;
interface
uses
tracer,
console,
nettypes, netutils;
@ -25,22 +26,28 @@ var
procedure registerNetworkCard(SendCallback : TNetSendCallback; _MAC : puint8);
begin
push_trace('net.registerNetworkCard');
if CBSend = nil then begin
CBSend:= SendCallback;
MAC:= _MAC;
end;
pop_trace;
end;
procedure registerNextLayer(RecvCallback : TRecvCallback);
begin
push_trace('net.registerNextLayer');
if CBNext = nil then begin
CBNext:= RecvCallback;
end;
pop_trace;
end;
procedure send(p_data : void; p_len : uint16);
begin
push_trace('net.send');
if CBSend <> nil then CBSend(p_data, p_len);
pop_trace;
end;
procedure recv(p_data : void; p_len : uint16);
@ -48,22 +55,28 @@ var
context : PPacketContext;
begin
push_trace('net.recv');
//console.outputln('net', 'RECV.');
context:= newPacketContext;
if CBNext <> nil then CBNext(p_data, p_len, context);
freePacketContext(context);
pop_trace;
end;
function getMAC : puint8;
begin
push_trace('net.getMAC');
getMAC:= MAC;
pop_trace;
end;
procedure init;
begin
push_trace('net.init');
eth2.register;
arp.register;
ipv4.register;
pop_trace;
end;
end.

View File

@ -3,6 +3,7 @@ unit eth2;
interface
uses
tracer,
nettypes, netutils,
net,
console;
@ -19,8 +20,10 @@ var
procedure registerType(eType : uint16; RecvCB : TRecvCallback);
begin
push_trace('eth2.registerType');
register;
if EthTypes[eType] = nil then EthTypes[eType]:= RecvCB;
pop_trace;
end;
procedure recv(p_data : void; p_len : uint16; p_context : PPacketContext);
@ -30,6 +33,7 @@ var
buf : puint8;
begin
push_trace('eth2.recv');
//console.outputln('net.eth2', 'RECV.');
buf:= puint8(p_data);
@ -56,6 +60,7 @@ begin
EthTypes[proto_type](void(buf), p_len - 14, p_context);
end;
end;
pop_trace;
end;
procedure register;
@ -63,6 +68,7 @@ var
i : uint16;
begin
push_trace('eth2.register');
if not Registered then begin
for i:=0 to 65535 do begin
EthTypes[i]:= nil;
@ -71,6 +77,7 @@ begin
MAC:= net.getMAC;
Registered:= true;
end;
pop_trace;
end;
end.

View File

@ -3,6 +3,7 @@ unit arp;
interface
uses
tracer,
util, lists, console,
nettypes, netutils,
eth2;
@ -30,14 +31,16 @@ var
r : PARPCacheRecord;
begin
push_trace('arp.findCacheRecordByMAC');
findCacheRecordByMAC:= nil;
for i:=0 to LL_Size(Cache)-1 do begin
r:= PARPCacheRecord(LL_Get(Cache, i));
if MACEqual(mac, @r^.MAC[0]) then begin
findCacheRecordByMAC:= r;
exit;
break;
end;
end;
pop_trace;
end;
function findCacheRecordByIP(ip : puint8) : PARPCacheRecord;
@ -46,14 +49,16 @@ var
r : PARPCacheRecord;
begin
push_trace('arp.findCacheRecordByIP');
findCacheRecordByIP:= nil;
for i:=0 to LL_Size(Cache)-1 do begin
r:= PARPCacheRecord(LL_Get(Cache, i));
if IPEqual(ip, @r^.IP[0]) then begin
findCacheRecordByIP:= r;
exit;
break;
end;
end;
pop_trace;
end;
procedure recv(p_data : void; p_len : uint16; p_context : PPacketContext);
@ -63,6 +68,7 @@ var
CacheElement : PARPCacheRecord;
begin
push_trace('arp.recv');
{ Get our converted Header }
Header:= PARPHeader(p_data);
AHeader.Hardware_Type:= (Header^.Hardware_Type_Hi SHL 8) + Header^.Hardware_Type_Lo;
@ -104,15 +110,18 @@ begin
end;
end;
pop_trace;
end;
procedure register;
begin
push_trace('arp.register');
if not Registered then begin
Cache:= LL_New(sizeof(TARPCacheRecord));
eth2.registerType($0806, @recv);
Registered:= true;
end;
pop_trace;
end;
function IPv4ToMAC(ip : puint8) : puint8;
@ -120,12 +129,14 @@ var
r : PARPCacheRecord;
begin
push_trace('arp.IPv4ToMAC');
register;
IPv4ToMAC:= nil;
r:= findCacheRecordByIP(ip);
if r <> nil then begin
IPv4ToMAC:= @r^.MAC[0];
end;
pop_trace;
end;
function MACToIIPv4(mac : puint8) : puint8;
@ -133,12 +144,14 @@ var
r : PARPCacheRecord;
begin
push_trace('arp.MACToIPv4');
register;
MACToIIPv4:= nil;
r:= findCacheRecordByMAC(mac);
if r <> nil then begin
MACToIIPv4:= @r^.IP[0];
end;
pop_trace;
end;
end.

View File

@ -3,6 +3,7 @@ unit ipv4;
interface
uses
tracer,
util, console, terminal,
net, nettypes, netutils,
eth2;
@ -26,6 +27,7 @@ var
len : uint16;
begin
push_trace('ipv4.recv');
//console.outputln('net.ipv4', 'RECV.');
Header:= PIPV4Header(p_data);
AHeader.version:= Header^.version;
@ -61,12 +63,13 @@ begin
if (IPEqual(@Config.Address[0], @AHeader.Dst[0])) OR (AHeader.Dst[3] = 255) then begin
if Protocols[AHeader.Protocol] <> nil then Protocols[AHeader.Protocol](void(buf), len, p_context);
end;
pop_trace;
end;
procedure terminal_command_ifconfig(params : PParamList);
begin
push_trace('ipv4.terminal_command_ifconfig');
if paramCount(params) > 2 then begin
end else begin
writestring(' MAC: ');
writeMACAddress(net.GetMAC);
@ -81,6 +84,7 @@ begin
else
writestringln(' NetUP: false');
end;
pop_trace;
end;
procedure register;
@ -88,6 +92,7 @@ var
i : uint8;
begin
push_trace('ipv4.register');
if not Registered then begin
for i:=0 to 255 do begin
Protocols[i]:= nil;
@ -102,12 +107,15 @@ begin
terminal.registerCommand('IFCONFIG', @terminal_command_ifconfig, 'Configure Network Settings.');
Registered:= true;
end;
pop_trace;
end;
procedure registerProtocol(Protocol_ID : uint8; recv_callback : TRecvCallback);
begin
push_trace('ipv4.registerProtocol');
register;
if Protocols[Protocol_ID] = nil then Protocols[Protocol_ID]:= recv_callback;
pop_trace;
end;
end.

View File

@ -3,6 +3,7 @@ unit E1000;
interface
uses
tracer,
console,
strings,
vmemorymanager,
@ -157,6 +158,7 @@ var
mem : puint32;
begin
push_trace('E1000.writeCommand');
if (bar_type = 0) then begin
mem:= puint32(mem_base + p_address);
mem^:= p_value;
@ -164,6 +166,7 @@ begin
outl(io_base + 0, p_address);
outl(io_base + 4, p_address)
end;
pop_trace;
end;
function readCommand(p_address : uint16) : uint32;
@ -171,13 +174,15 @@ var
mem : puint32;
begin
push_trace('E1000.readCommand');
if (bar_type = 0) then begin
mem:= puint32(mem_base + p_address);
readCommand:= mem^;
end else begin
outl(io_base, p_address);
readCommand:= inl(io_base + 4);
end;
end;
pop_trace;
end;
function detectEEPROM() : boolean;
@ -185,6 +190,7 @@ var
val, i : uint32;
begin
push_trace('E1000.detectEEPROM');
val:= 0;
writeCommand(REG_EEPROM, $1);
for i:=0 to 1000 do begin
@ -193,6 +199,7 @@ begin
if (val and $10) > 0 then eeprom_exists:= true else eeprom_exists:= false;
end;
detectEEPROM:= eeprom_exists;
pop_trace;
end;
function EEPROMRead( address : uint8 ) : uint32;
@ -201,6 +208,7 @@ var
tmp : uint32;
begin
push_trace('E1000.EEPROMRead');
tmp:= 0;
if (eeprom_exists) then begin
writeCommand( REG_EEPROM, 1 OR (uint32(address) SHL 8) );
@ -215,6 +223,7 @@ begin
end;
data:= uint16( (tmp SHR 16) AND ($FFFF) );
EEPROMRead:= data;
pop_trace;
end;
function readMACAddress() : boolean;
@ -226,6 +235,7 @@ var
i : uint32;
begin
push_trace('E1000.readMACAddress');
res:= true;
if (eeprom_exists) then begin
temp:= EEPROMRead(0);
@ -249,6 +259,7 @@ begin
end;
end;
readMACAddress:= res;
pop_trace;
end;
procedure startLink();
@ -256,8 +267,10 @@ var
val : uint32;
begin
push_trace('E1000.startLink');
val:= readCommand(REG_CTRL);
writeCommand(REG_CTRL, val OR ECTRL_SLU);
pop_trace;
end;
procedure rxinit();
@ -268,6 +281,7 @@ var
i : uint32;
begin
push_trace('E1000.rxinit');
ptr:= puint8(kalloc(sizeof(TE1000_rx_desc) * E1000_NUM_RX_DESC + 16));
descs:= PE1000_rx_desc(ptr);
for i:=0 to E1000_NUM_RX_DESC do begin
@ -297,6 +311,7 @@ begin
rx_curr:= 0;
writeCommand(REG_RCTRL, RCTL_EN OR RCTL_SBP OR RCTL_UPE OR RCTL_MPE OR RCTL_LBM_NONE OR RTCL_RDMTS_HALF OR RCTL_BAM OR RCTL_SECRC OR RCTL_BSIZE_2048);
pop_trace;
end;
procedure txinit();
@ -307,6 +322,7 @@ var
i : uint32;
begin
push_trace('E1000.txinit');
ptr:= puint8(kalloc(sizeof(TE1000_tx_desc) * (E1000_NUM_TX_DESC + 16)));
descs:= PE1000_tx_desc(ptr);
for i:=0 to E1000_NUM_TX_DESC do begin
@ -338,13 +354,16 @@ begin
writeCommand(REG_TCTRL, $3003F0FA);
writeCommand(REG_TIPG, $0060200A);
end;
pop_trace;
end;
procedure enableInturrupt();
begin
push_trace('E1000.enableInterrupt');
writeCommand(REG_IMASK, $1F6DC);
writeCommand(REG_IMASK, $FF AND NOT(4));
readCommand($C0);
pop_trace;
end;
procedure handleReceive();
@ -356,6 +375,7 @@ var
i : uint16;
begin
push_trace('E1000.handleReceive');
while (rx_descs[rx_curr]^.status AND $1) > 0 do begin
got_packet:= true;
buf:= rx_buffs[rx_curr];
@ -369,6 +389,7 @@ begin
rx_curr:= (rx_curr + 1) mod E1000_NUM_RX_DESC;
writeCommand(REG_RXDESCTAIL, old_cur);
end;
pop_trace;
end;
procedure writeCardType();
@ -388,6 +409,7 @@ var
data : uint32;
begin
push_trace('E1000.fire');
//console.outputln('E1000 Driver', 'FIRED.');
status:= readCommand($C0);
@ -412,11 +434,14 @@ begin
//CLI (Not 100% Nessisary as this is done on IRET)
CLI;
pop_trace;
end;
procedure console_command_mac(params : PParamList);
begin
push_trace('E1000.console_command_mac');
writeMACAddress(@mac[0]);
pop_trace;
end;
procedure console_command_sendtest(params : PParamList);
@ -436,6 +461,7 @@ var
);
begin
push_trace('E1000.console_command_sendtest');
TestPacket[6]:= mac[0];
TestPacket[7]:= mac[1];
TestPacket[8]:= mac[2];
@ -450,6 +476,7 @@ begin
TestPacket[26]:= mac[4];
TestPacket[27]:= mac[5];
sendPacket(void(@TestPacket[0]), 42);
pop_trace;
end;
function load(ptr : void) : boolean;
@ -460,6 +487,8 @@ var
iline : uint8;
begin
push_trace('E1000.load');
console.outputln('E1000 Driver', 'Load Start.');
writeCardType();
@ -484,58 +513,66 @@ begin
if not readMACAddress() then begin
console.outputln('E1000 Driver', 'MAC Read Failed.');
load:= false;
exit;
end else begin
console.output('E1000 Driver', 'MAC Address: ');
writeMACAddress(@mac[0]);
startLink();
for i:=0 to $80 do begin
writeCommand($5200 + i*4, 0);
end;
net.registerNetworkCard(@sendPacket, getMACAddress());
IDT.set_gate(32 + PCI_Info^.interrupt_line, uint32(@fire), $08, ISR_RING_0);
enableInturrupt();
rxinit();
txinit();
load:= true;
if load then registercommand('E1000', @console_command_sendtest, 'Test sending a ARP Request.');
if load then registercommand('MAC', @console_command_mac, 'Print MAC Address.');
end;
console.output('E1000 Driver', 'MAC Address: ');
writeMACAddress(@mac[0]);
startLink();
for i:=0 to $80 do begin
writeCommand($5200 + i*4, 0);
end;
net.registerNetworkCard(@sendPacket, getMACAddress());
IDT.set_gate(32 + PCI_Info^.interrupt_line, uint32(@fire), $08, ISR_RING_0);
enableInturrupt();
rxinit();
txinit();
load:= true;
if load then registercommand('E1000', @console_command_sendtest, 'Test sending a ARP Request.');
if load then registercommand('MAC', @console_command_mac, 'Print MAC Address.');
console.outputln('E1000 Driver', 'Load Finish.');
pop_trace;
end;
function loadE1000(ptr : void) : boolean;
begin
push_trace('E1000.loadE1000');
loadE1000:= false;
if not Loaded then begin
card_type:= ctE1000;
loadE1000:= load(ptr);
end;
pop_trace;
end;
function load82577LM(ptr : void) : boolean;
begin
push_trace('E1000.load82577LM');
load82577LM:= false;
if not Loaded then begin
card_type:= ct82577LM;
load82577LM:= load(ptr);
end;
pop_trace;
end;
function loadI217(ptr : void) : boolean;
begin
push_trace('E1000.loadI217');
loadI217:= false;
if not Loaded then begin
card_type:= ctI217;
loadI217:= load(ptr);
end;
pop_trace;
end;
procedure init();
@ -543,6 +580,7 @@ var
dev : TDeviceIdentifier;
begin
push_trace('E1000.init');
card_type:= ctUnknown;
dev.Bus:= biPCI;
dev.id0:= INTEL_VEND;
@ -556,6 +594,7 @@ begin
drivermanagement.register_driver('I217 Ethernet Driver', @dev, @loadI217);
dev.id4:= LM82577_DEV;
drivermanagement.register_driver('82577LM Ethernet Driver', @dev, @load82577LM);
pop_trace;
end;
function getMACAddress : puint8;
@ -568,6 +607,7 @@ var
old_cur : uint8;
begin
push_trace('E1000.sendPacket');
tx_descs[tx_curr]^.address:= uint32(vtop(uint32(p_data)));
tx_descs[tx_curr]^.length:= p_len;
tx_descs[tx_curr]^.cmd:= CMD_EOP OR CMD_IFCS OR CMD_RS OR CMD_RPS;
@ -578,6 +618,7 @@ begin
while (tx_descs[old_cur]^.status AND $FF) = 0 do begin
end;
sendPacket:= 0;
pop_trace;
end;
end.

View File

@ -31,7 +31,7 @@ type
PPCreateHook = procedure(disk : PStorage_Device; sectors : uint32; start : uint32);
PPDetectHook = procedure(disk : PStorage_Device);
PPHIOHook = procedure(device : PStorage_device; LBA : uint32; sectorCount : uint32; buffer : Puint32);
PPHIOHook_ = procedure(device : PStorage_device; LBA : uint32; sectorCount : uint32; buffer : Puint32);
TFilesystem = record
sName : pchar;
@ -58,7 +58,7 @@ type
maxSectorCount : uint32;
sectorSize : uint32;
writable : boolean;
volumes : array[0..7] of TStorage_Volume
volumes : array[0..7] of TStorage_Volume;
writeCallback : PPHIOHook;
readCallback : PPHIOHook;
end;