views:

377

answers:

4

Hi, does anyone know how to gain access to devices such as an ethernet port on a mainboard or on a pci card?

Are there special registers? Opcodes? Do I have to make a call to the OS? If so, how?

Thanks in advance.

A: 

You can use calls to the card itself, using inupt and output instructions or something similar. Each ethernet card is different, so you'll need documentation from the manufacturer.

The next level up is the DOS function calls, assuming you're usuing Windows. These will allow you to access limited features of an ethernet card using standard MSDOS calls. You can find these in old MSDOS documentation. Most of them still work on later versions of Windows, if I'm not mistaken.

xpda
I believe direct port access is denied in user mode code under any windows after 9x.
BioBuckyBall
Do you have any examples? Do load the memory address of the device into a register? How do I get said memory address? What keywords in x86 would I use? etc. And unfortunately in this case, developing on Windows will be highly unlikely. If I'm successful in any capacity, I'll likely want it to use with other code, all of which I develop on Unix systems.
Kevin
As yetapb said, there may be security restrictions on direct port access. You can get details on all this from the hardware manufacturer. Each manufacturer and most models are likely to be different. Sorry, I don't have any examples from the past decade.
xpda
A: 

You will need to write to specific port addresses. This Wiki page might get you started.
MemoryMappedIO

BioBuckyBall
+1  A: 

It depends on the particular Ethernet MAC chip you're trying to talk to. Even chips in the same family will often have minor differences in how they operate. This is why modern OSes have the "driver" concept: the hardware manufacturer usually writes the driver because they know the hardware, and the driver provides a translation between the hardware and what the OS wants to see.

You can often get documentation from the MAC chip's manufacturer to write your own driver. Again, you have to know exactly which chip you're trying to talk to in order to get the right specs. Some chips have no public documentation, but that's usually not a problem with Ethernet chips.

Warren Young
Wow, that's great (in the sense that I just learned something new). Thanks! So, if I find the correct spec for my MAC chip, I could essentially write my own driver either for fun or another feature. Is there an easy way to find out about the MAC chip at the application level? And, drivers have obviously got to be assembled to byte-code, but is there an easy way to "disassemble" them back to assembly? Said step happens in the Linux community quite often right?
Kevin
Yes, you're supposed to be able to write a driver just from the chip docs; finding said docs may be a challenge. To detect the chip in use, you can walk the PCI device tables; in Linux, for instance, there's the lspci command, which does this for you. Yes, you can disassemble a driver, but there's little reason to do so if you have the specs for the chip. And no, Linux Ethernet drivers aren't usually reverse-engineered from Windows drivers. That sometimes happens with other types of drivers, but as I said, Ethernet chip specs usually aren't hard to come by.
Warren Young
+1  A: 

The simplest answer, although probably not what you're looking for, would be to write the C code to access the card, compile it, and see the code generated by the compiler.

The C code is likely to go through the NIC driver directly, or use a library like winpcap. Built-in support for raw sockets on Windows, for example, was disabled for security reasons.

But this isn't the best way to learn how NICs work. For that, pick a datasheet of a popular embedded NIC like LAN91C111 and read how to access it. That will teach you a lot about interfacing Eterthet in the raw way.

This still isn't a good enough sandbox to study assembly language in, IMHO. For that, just implement a few small algorithmic programs in assembly - like binary tree search.

Eli Bendersky