Permalink
Browse files

Files for Appendix A01, https://www.youtube.com/watch?v=fU0tMBxEXoA

  • Loading branch information...
AlgorithMan-de committed Aug 20, 2016
1 parent 1f72da7 commit cb3517b40b8777bd65a68379c96c5efe2d0cd812
@@ -15,6 +15,21 @@ namespace myos
namespace drivers
{
class amd_am79c973;
class RawDataHandler
{
protected:
amd_am79c973* backend;
public:
RawDataHandler(amd_am79c973* backend);
~RawDataHandler();
bool OnRawDataReceived(common::uint8_t* buffer, common::uint32_t size);
void Send(common::uint8_t* buffer, common::uint32_t size);
};
class amd_am79c973 : public Driver, public hardwarecommunication::InterruptHandler
{
struct InitializationBlock
@@ -62,6 +77,8 @@ namespace myos
common::uint8_t currentRecvBuffer;
RawDataHandler* handler;
public:
amd_am79c973(myos::hardwarecommunication::PeripheralComponentInterconnectDeviceDescriptor *dev,
myos::hardwarecommunication::InterruptManager* interrupts);
@@ -73,6 +90,9 @@ namespace myos
void Send(common::uint8_t* buffer, int count);
void Receive();
void SetHandler(RawDataHandler* handler);
common::uint64_t GetMACAddress();
};
View
@@ -0,0 +1,65 @@
#ifndef __MYOS__NET__ETHERFRAME_H
#define __MYOS__NET__ETHERFRAME_H
#include <common/types.h>
#include <drivers/amd_am79c973.h>
#include <memorymanagement.h>
namespace myos
{
namespace net
{
struct EtherFrameHeader
{
common::uint64_t dstMAC_BE;
common::uint64_t srcMAC_BE;
common::uint16_t etherType_BE;
} __attribute__ ((packed));
typedef common::uint32_t EtherFrameFooter;
class EtherFrameProvider;
class EtherFrameHandler
{
protected:
EtherFrameProvider* backend;
common::uint16_t etherType_BE;
public:
EtherFrameHandler(EtherFrameProvider* backend, common::uint16_t etherType);
~EtherFrameHandler();
bool OnEtherFrameReceived(common::uint8_t* etherframePayload, common::uint32_t size);
void Send(common::uint64_t dstMAC_BE, common::uint8_t* etherframePayload, common::uint32_t size);
};
class EtherFrameProvider : public myos::drivers::RawDataHandler
{
friend class EtherFrameHandler;
protected:
EtherFrameHandler* handlers[65535];
public:
EtherFrameProvider(drivers::amd_am79c973* backend);
~EtherFrameProvider();
bool OnRawDataReceived(common::uint8_t* buffer, common::uint32_t size);
void Send(common::uint64_t dstMAC_BE, common::uint16_t etherType_BE, common::uint8_t* buffer, common::uint32_t size);
};
}
}
#endif
View
@@ -24,6 +24,7 @@ objects = obj/loader.o \
obj/gui/widget.o \
obj/gui/window.o \
obj/gui/desktop.o \
obj/net/etherframe.o \
obj/kernel.o
@@ -8,6 +8,35 @@ using namespace myos::hardwarecommunication;
RawDataHandler::RawDataHandler(amd_am79c973* backend)
{
this->backend = backend;
backend->SetHandler(this);
}
RawDataHandler::~RawDataHandler()
{
backend->SetHandler(0);
}
bool RawDataHandler::OnRawDataReceived(uint8_t* buffer, uint32_t size)
{
return false;
}
void RawDataHandler::Send(uint8_t* buffer, uint32_t size)
{
backend->Send(buffer, size);
}
void printf(char*);
void printfHex(uint8_t);
amd_am79c973::amd_am79c973(PeripheralComponentInterconnectDeviceDescriptor *dev, InterruptManager* interrupts)
: Driver(),
InterruptHandler(interrupts, dev->interrupt + interrupts->HardwareInterruptOffset()),
@@ -19,6 +48,7 @@ amd_am79c973::amd_am79c973(PeripheralComponentInterconnectDeviceDescriptor *dev,
resetPort(dev->portBase + 0x14),
busControlRegisterDataPort(dev->portBase + 0x16)
{
this->handler = 0;
currentSendBuffer = 0;
currentRecvBuffer = 0;
@@ -107,8 +137,6 @@ int amd_am79c973::Reset()
}
void printf(char*);
void printfHex(uint8_t);
uint32_t amd_am79c973::HandleInterrupt(common::uint32_t esp)
{
@@ -172,15 +200,30 @@ void amd_am79c973::Receive()
uint8_t* buffer = (uint8_t*)(recvBufferDescr[currentRecvBuffer].address);
if(handler != 0)
if(handler->OnRawDataReceived(buffer, size))
Send(buffer, size);
/*
for(int i = 0; i < size; i++)
{
printfHex(buffer[i]);
printf(" ");
}
*/
}
recvBufferDescr[currentRecvBuffer].flags2 = 0;
recvBufferDescr[currentRecvBuffer].flags = 0x8000F7FF;
}
}
void amd_am79c973::SetHandler(RawDataHandler* handler)
{
this->handler = handler;
}
uint64_t amd_am79c973::GetMACAddress()
{
return initBlock.physicalAddress;
}
@@ -166,10 +166,12 @@ Driver* PeripheralComponentInterconnectController::GetDriver(PeripheralComponent
switch(dev.device_id)
{
case 0x2000: // am79c973
printf("AMD am79c973 ");
driver = (amd_am79c973*)MemoryManager::activeMemoryManager->malloc(sizeof(amd_am79c973));
if(driver != 0)
new (driver) amd_am79c973(&dev, interrupts);
printf("AMD am79c973 ");
else
printf("instantiation failed");
return driver;
break;
}
View
@@ -15,7 +15,7 @@
#include <multitasking.h>
#include <drivers/amd_am79c973.h>
#include <net/etherframe.h>
// #define GRAPHICSMODE
@@ -25,6 +25,7 @@ using namespace myos::common;
using namespace myos::drivers;
using namespace myos::hardwarecommunication;
using namespace myos::gui;
using namespace myos::net;
@@ -199,11 +200,12 @@ extern "C" void kernelMain(const void* multiboot_structure, uint32_t /*multiboot
printf("\n");
TaskManager taskManager;
/*
Task task1(&gdt, taskA);
Task task2(&gdt, taskB);
taskManager.AddTask(&task1);
taskManager.AddTask(&task2);
*/
InterruptManager interrupts(0x20, &gdt, &taskManager);
SyscallHandler syscalls(&interrupts, 0x80);
@@ -279,7 +281,9 @@ extern "C" void kernelMain(const void* multiboot_structure, uint32_t /*multiboot
amd_am79c973* eth0 = (amd_am79c973*)(drvManager.drivers[2]);
eth0->Send((uint8_t*)"Hello Network", 13);
EtherFrameProvider etherframe(eth0);
etherframe.Send(0xFFFFFFFFFFFF, 0x0608, (uint8_t*)"FOO", 3);
//eth0->Send((uint8_t*)"Hello Network", 13);
interrupts.Activate();
View
@@ -0,0 +1,86 @@
#include <net/etherframe.h>
using namespace myos;
using namespace myos::common;
using namespace myos::net;
using namespace myos::drivers;
EtherFrameHandler::EtherFrameHandler(EtherFrameProvider* backend, uint16_t etherType)
{
this->etherType_BE = ((etherType & 0x00FF) << 8)
| ((etherType & 0xFF00) >> 8);
this->backend = backend;
backend->handlers[etherType_BE] = this;
}
EtherFrameHandler::~EtherFrameHandler()
{
backend->handlers[etherType_BE] = 0;
}
bool EtherFrameHandler::OnEtherFrameReceived(common::uint8_t* etherframePayload, common::uint32_t size)
{
return false;
}
void EtherFrameHandler::Send(common::uint64_t dstMAC_BE, common::uint8_t* data, common::uint32_t size)
{
backend->Send(dstMAC_BE, etherType_BE, data, size);
}
EtherFrameProvider::EtherFrameProvider(amd_am79c973* backend)
: RawDataHandler(backend)
{
for(uint32_t i = 0; i < 65535; i++)
handlers[i] = 0;
}
EtherFrameProvider::~EtherFrameProvider()
{
}
bool EtherFrameProvider::OnRawDataReceived(common::uint8_t* buffer, common::uint32_t size)
{
EtherFrameHeader* frame = (EtherFrameHeader*)buffer;
bool sendBack = false;
if(frame->dstMAC_BE == 0xFFFFFFFFFFFF
|| frame->dstMAC_BE == backend->GetMACAddress())
{
if(handlers[frame->etherType_BE] != 0)
sendBack = handlers[frame->etherType_BE]->OnEtherFrameReceived(
buffer + sizeof(EtherFrameHeader), size - sizeof(EtherFrameHeader));
}
if(sendBack)
{
frame->dstMAC_BE = frame->srcMAC_BE;
frame->srcMAC_BE = backend->GetMACAddress();
}
return sendBack;
}
void EtherFrameProvider::Send(common::uint64_t dstMAC_BE, common::uint16_t etherType_BE, common::uint8_t* buffer, common::uint32_t size)
{
uint8_t* buffer2 = (uint8_t*)MemoryManager::activeMemoryManager->malloc(sizeof(EtherFrameHeader) + size);
EtherFrameHeader* frame = (EtherFrameHeader*)buffer2;
frame->dstMAC_BE = dstMAC_BE;
frame->srcMAC_BE = backend->GetMACAddress();
frame->etherType_BE = etherType_BE;
uint8_t* src = buffer;
uint8_t* dst = buffer2 + sizeof(EtherFrameHeader);
for(uint32_t i = 0; i < size; i++)
dst[i] = src[i];
backend->Send(buffer2, size + sizeof(EtherFrameHeader));
}

0 comments on commit cb3517b

Please sign in to comment.