2009-09-17 15:55:38 +04:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2010-07-12 03:13:12 +04:00
|
|
|
;; Copyright (C) KolibriOS team 2004-2010. All rights reserved. ;;
|
2009-09-17 15:55:38 +04:00
|
|
|
;; Distributed under terms of the GNU General Public License ;;
|
|
|
|
;; ;;
|
|
|
|
;; ETHERNET.INC ;;
|
|
|
|
;; ;;
|
|
|
|
;; Ethernet network layer for KolibriOS ;;
|
|
|
|
;; ;;
|
|
|
|
;; Written by hidnplayr@kolibrios.org ;;
|
|
|
|
;; ;;
|
|
|
|
;; GNU GENERAL PUBLIC LICENSE ;;
|
|
|
|
;; Version 2, June 1991 ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2009-10-12 18:39:59 +04:00
|
|
|
$Revision$
|
2009-09-17 15:55:38 +04:00
|
|
|
|
|
|
|
ETH_QUEUE_SIZE equ 16
|
|
|
|
|
|
|
|
struct ETH_FRAME
|
2010-07-12 03:13:12 +04:00
|
|
|
.DstMAC dp ? ; destination MAC-address
|
|
|
|
.SrcMAC dp ? ; source MAC-address
|
|
|
|
.Type dw ? ; type of the upper-layer protocol
|
|
|
|
.Data: ; data (46-1500 bytes for a normal packet)
|
2009-09-17 15:55:38 +04:00
|
|
|
ends
|
|
|
|
|
2010-07-12 03:13:12 +04:00
|
|
|
virtual at NET_DEVICE.end
|
|
|
|
|
|
|
|
ETH_DEVICE:
|
2009-09-17 15:55:38 +04:00
|
|
|
.unload dd ?
|
|
|
|
.reset dd ?
|
|
|
|
.transmit dd ?
|
|
|
|
.set_MAC dd ?
|
|
|
|
.get_MAC dd ?
|
|
|
|
.set_mode dd ?
|
|
|
|
.get_mode dd ?
|
|
|
|
|
|
|
|
.bytes_tx dq ?
|
|
|
|
.bytes_rx dq ?
|
|
|
|
.packets_tx dd ?
|
|
|
|
.packets_rx dd ?
|
2010-07-12 03:13:12 +04:00
|
|
|
.mode dd ?
|
2009-09-17 15:55:38 +04:00
|
|
|
.name dd ?
|
|
|
|
.mac dp ?
|
2009-11-16 00:44:17 +03:00
|
|
|
|
2010-07-12 03:13:12 +04:00
|
|
|
end virtual
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-10-06 00:47:27 +04:00
|
|
|
align 4
|
|
|
|
iglobal
|
|
|
|
|
|
|
|
ETH_BROADCAST dp 0xffffffffffff
|
|
|
|
endg
|
|
|
|
|
2009-09-17 15:55:38 +04:00
|
|
|
align 4
|
|
|
|
uglobal
|
|
|
|
ETH_RUNNING dd ?
|
|
|
|
endg
|
|
|
|
|
|
|
|
|
2009-11-09 16:59:46 +03:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 15:55:38 +04:00
|
|
|
;
|
|
|
|
; ETH_init
|
|
|
|
;
|
|
|
|
; This function resets all ethernet variables
|
|
|
|
;
|
|
|
|
; IN: /
|
|
|
|
; OUT: /
|
|
|
|
;
|
2009-11-09 16:59:46 +03:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 15:55:38 +04:00
|
|
|
align 4
|
|
|
|
ETH_init:
|
|
|
|
|
2010-07-12 03:13:12 +04:00
|
|
|
mov [ETH_RUNNING], 0
|
2009-09-17 15:55:38 +04:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-11-09 16:59:46 +03:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 15:55:38 +04:00
|
|
|
;
|
|
|
|
; ETH_Receiver:
|
|
|
|
;
|
|
|
|
; This function is called by ethernet drivers,
|
|
|
|
; It pushes the received ethernet packets onto the eth_in_queue
|
|
|
|
;
|
2010-07-12 03:13:12 +04:00
|
|
|
; IN: [esp] = Pointer to buffer
|
2009-11-09 16:59:46 +03:00
|
|
|
; [esp-4] = size of buffer
|
2010-07-12 03:13:12 +04:00
|
|
|
; ebx = pointer to eth_device
|
2009-09-17 15:55:38 +04:00
|
|
|
; OUT: /
|
|
|
|
;
|
2009-11-09 16:59:46 +03:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 15:55:38 +04:00
|
|
|
align 4
|
2009-11-06 23:45:08 +03:00
|
|
|
ETH_receiver:
|
2010-05-29 00:47:32 +04:00
|
|
|
mov eax, [esp]
|
|
|
|
mov ecx, [esp+4]
|
2009-09-17 15:55:38 +04:00
|
|
|
|
|
|
|
DEBUGF 1,"ETH_Handler - size: %u\n", ecx
|
|
|
|
cmp ecx, 60 ; check packet length
|
|
|
|
jl .dump
|
|
|
|
sub ecx, ETH_FRAME.Data
|
|
|
|
|
|
|
|
lea edx, [eax + ETH_FRAME.Data]
|
|
|
|
mov ax , [eax + ETH_FRAME.Type]
|
|
|
|
|
|
|
|
cmp ax, ETHER_IPv4
|
2009-10-06 00:47:27 +04:00
|
|
|
je IPv4_handler
|
2009-09-17 15:55:38 +04:00
|
|
|
|
|
|
|
cmp ax, ETHER_ARP
|
2009-10-06 00:47:27 +04:00
|
|
|
je ARP_handler
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2010-05-29 00:47:32 +04:00
|
|
|
DEBUGF 2,"Unknown ethernet packet type %x\n", ax
|
2009-09-17 15:55:38 +04:00
|
|
|
|
|
|
|
.dump:
|
2010-05-29 00:47:32 +04:00
|
|
|
DEBUGF 2,"ETH_Handler - dumping\n"
|
2009-09-17 15:55:38 +04:00
|
|
|
call kernel_free
|
|
|
|
add esp, 4
|
|
|
|
ret
|
|
|
|
|
2009-11-09 16:59:46 +03:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 15:55:38 +04:00
|
|
|
;
|
2009-11-09 16:59:46 +03:00
|
|
|
; ETH_create_packet
|
2009-09-17 15:55:38 +04:00
|
|
|
;
|
2010-07-12 03:13:12 +04:00
|
|
|
; IN: eax = pointer to source mac
|
|
|
|
; ebx = pointer to destination mac
|
|
|
|
; ecx = packet size
|
|
|
|
; edx = device number
|
|
|
|
; di = protocol
|
2009-09-17 15:55:38 +04:00
|
|
|
;
|
2010-07-12 03:13:12 +04:00
|
|
|
; OUT: edi = 0 on error, pointer to buffer otherwise
|
|
|
|
; eax = buffer start
|
|
|
|
; ebx = to device structure
|
|
|
|
; ecx = unchanged (packet size of embedded data)
|
|
|
|
; edx = size of complete buffer
|
2009-09-17 15:55:38 +04:00
|
|
|
;
|
2009-11-09 16:59:46 +03:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 15:55:38 +04:00
|
|
|
align 4
|
2009-11-06 23:45:08 +03:00
|
|
|
ETH_create_packet:
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2010-05-29 00:47:32 +04:00
|
|
|
DEBUGF 1,"Creating Ethernet Packet (size=%u): \n", ecx
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2010-07-12 03:13:12 +04:00
|
|
|
cmp ecx, 1500 ;;;
|
2009-09-17 15:55:38 +04:00
|
|
|
jg .exit
|
|
|
|
|
|
|
|
push ecx di eax ebx edx
|
|
|
|
|
|
|
|
add ecx, ETH_FRAME.Data
|
|
|
|
push ecx
|
|
|
|
push ecx
|
|
|
|
call kernel_alloc
|
2010-07-12 03:13:12 +04:00
|
|
|
mov edi, eax
|
|
|
|
test edi, edi
|
2009-09-17 15:55:38 +04:00
|
|
|
jz .pop_exit
|
|
|
|
|
|
|
|
pop ecx
|
|
|
|
pop edx
|
|
|
|
|
|
|
|
pop esi
|
|
|
|
movsd
|
|
|
|
movsw
|
|
|
|
pop esi
|
|
|
|
movsd
|
|
|
|
movsw
|
|
|
|
pop ax
|
|
|
|
stosw
|
|
|
|
|
|
|
|
lea eax, [edi - ETH_FRAME.Data] ; Set eax to buffer start
|
2009-10-12 18:39:59 +04:00
|
|
|
mov edx, ecx ; Set ebx to complete buffer size
|
2009-09-17 15:55:38 +04:00
|
|
|
pop ecx
|
|
|
|
|
2009-10-12 18:39:59 +04:00
|
|
|
xor ebx, ebx ;;;; TODO: Fixme
|
2010-07-12 03:13:12 +04:00
|
|
|
mov ebx, [NET_DRV_LIST + ebx]
|
2009-09-17 15:55:38 +04:00
|
|
|
|
2009-10-12 18:39:59 +04:00
|
|
|
cmp edx, 46 + ETH_FRAME.Data ; If data size is less then 46, add padding bytes
|
|
|
|
jg .continue
|
|
|
|
mov edx, 46 + ETH_FRAME.Data
|
|
|
|
.continue:
|
|
|
|
|
2010-05-29 00:47:32 +04:00
|
|
|
DEBUGF 1,"done: %x size:%u device:%x\n", eax, edx, ebx
|
2009-09-17 15:55:38 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
.pop_exit:
|
2010-05-29 00:47:32 +04:00
|
|
|
DEBUGF 2,"Out of ram space!!\n"
|
2009-09-17 15:55:38 +04:00
|
|
|
add esp, 18
|
2010-07-12 03:13:12 +04:00
|
|
|
and edi, 0
|
2009-10-12 18:39:59 +04:00
|
|
|
ret
|
|
|
|
|
2009-09-17 15:55:38 +04:00
|
|
|
.exit:
|
2010-05-29 00:47:32 +04:00
|
|
|
DEBUGF 2,"Packet too large!\n"
|
2010-07-12 03:13:12 +04:00
|
|
|
and edi, 0
|
2009-09-17 15:55:38 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-11-09 16:59:46 +03:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 15:55:38 +04:00
|
|
|
;
|
|
|
|
; ETH_API
|
|
|
|
;
|
|
|
|
; This function is called by system function 75
|
|
|
|
;
|
|
|
|
; IN: subfunction number in bl
|
|
|
|
; device number in bh
|
|
|
|
; ecx, edx, .. depends on subfunction
|
|
|
|
;
|
|
|
|
; OUT:
|
|
|
|
;
|
2009-11-09 16:59:46 +03:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 15:55:38 +04:00
|
|
|
align 4
|
|
|
|
ETH_API:
|
|
|
|
|
2010-07-12 03:13:12 +04:00
|
|
|
cmp bh, MAX_NET_DEVICES
|
|
|
|
jg .error
|
2009-09-17 15:55:38 +04:00
|
|
|
movzx eax, bh
|
|
|
|
shl eax, 2
|
|
|
|
|
2010-07-12 03:13:12 +04:00
|
|
|
cmp bl, 7
|
|
|
|
jz .out_queue
|
|
|
|
cmp bl, 6
|
|
|
|
jz .in_queue
|
|
|
|
|
|
|
|
mov eax, dword [NET_DRV_LIST + eax]
|
|
|
|
cmp [eax + NET_DEVICE.type], NET_TYPE_ETH
|
|
|
|
jne .error
|
|
|
|
|
2009-09-17 15:55:38 +04:00
|
|
|
test bl, bl
|
|
|
|
jz .packets_tx ; 0
|
|
|
|
dec bl
|
|
|
|
jz .packets_rx ; 1
|
|
|
|
dec bl
|
|
|
|
jz .bytes_tx ; 2
|
|
|
|
dec bl
|
|
|
|
jz .bytes_rx ; 3
|
|
|
|
dec bl
|
|
|
|
jz .read_mac ; 4
|
|
|
|
dec bl
|
|
|
|
jz .write_mac ; 5
|
|
|
|
|
2010-07-12 03:13:12 +04:00
|
|
|
.error:
|
|
|
|
DEBUGF 2,"Device is not ethernet type\n"
|
|
|
|
or eax, -1
|
2009-09-17 15:55:38 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
.packets_tx:
|
2009-09-22 20:42:54 +04:00
|
|
|
mov eax, dword [eax + ETH_DEVICE.packets_tx]
|
|
|
|
|
2009-09-17 15:55:38 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
.packets_rx:
|
2009-09-22 20:42:54 +04:00
|
|
|
mov eax, dword [eax + ETH_DEVICE.packets_rx]
|
2009-09-17 15:55:38 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
.bytes_tx:
|
2009-10-28 16:09:28 +03:00
|
|
|
mov ebx, dword [eax + ETH_DEVICE.bytes_tx + 4]
|
2009-09-24 22:32:03 +04:00
|
|
|
mov eax, dword [eax + ETH_DEVICE.bytes_tx]
|
|
|
|
mov [esp+20+4], ebx ; TODO: fix this ugly code
|
2009-09-17 15:55:38 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
.bytes_rx:
|
2009-09-24 22:32:03 +04:00
|
|
|
mov ebx, dword [eax + ETH_DEVICE.bytes_rx + 4]
|
|
|
|
mov eax, dword [eax + ETH_DEVICE.bytes_rx]
|
|
|
|
mov [esp+20+4], ebx ; TODO: fix this ugly code
|
2009-09-17 15:55:38 +04:00
|
|
|
ret
|
|
|
|
|
2009-09-24 22:32:03 +04:00
|
|
|
|
2009-09-17 15:55:38 +04:00
|
|
|
.read_mac:
|
|
|
|
movzx ebx, word [eax + ETH_DEVICE.mac]
|
|
|
|
mov eax, dword [eax + ETH_DEVICE.mac + 2]
|
2009-09-22 20:42:54 +04:00
|
|
|
mov [esp+20+4], ebx ; TODO: fix this ugly code
|
2009-09-17 15:55:38 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
.write_mac:
|
|
|
|
push ecx
|
|
|
|
push dx
|
2010-07-12 03:13:12 +04:00
|
|
|
mov eax, [eax + ETH_DEVICE.set_MAC]
|
2009-09-17 15:55:38 +04:00
|
|
|
call eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
.in_queue:
|
2010-07-12 03:13:12 +04:00
|
|
|
if ETH_QUEUE
|
2009-09-17 15:55:38 +04:00
|
|
|
add eax, ETH_IN_QUEUE
|
|
|
|
mov eax, [eax + queue.size]
|
2010-07-12 03:13:12 +04:00
|
|
|
else
|
|
|
|
or eax, -1
|
|
|
|
end if
|
2009-09-17 15:55:38 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
.out_queue:
|
2010-07-12 03:13:12 +04:00
|
|
|
if ETH_QUEUE
|
2009-09-17 15:55:38 +04:00
|
|
|
add eax, ETH_OUT_QUEUE
|
|
|
|
mov eax, [eax + queue.size]
|
2010-07-12 03:13:12 +04:00
|
|
|
else
|
|
|
|
or eax, -1
|
|
|
|
end if
|
2009-09-17 15:55:38 +04:00
|
|
|
ret
|