OpenCores
URL https://opencores.org/ocsvn/amber/amber/trunk

Subversion Repositories amber

[/] [amber/] [trunk/] [sw/] [boot-loader-ethmac/] [udp.c] - Blame information for rev 80

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 61 csantifort
/*----------------------------------------------------------------
2
//                                                              //
3
//  boot-loader-ethmac.c                                        //
4
//                                                              //
5
//  This file is part of the Amber project                      //
6
//  http://www.opencores.org/project,amber                      //
7
//                                                              //
8
//  Description                                                 //
9
//  The main functions for the boot loader application. This    //
10 78 csantifort
//  application is embedded in the FPGA's SRAM and is used      //
11 61 csantifort
//  to load larger applications into the DDR3 memory on         //
12
//  the development board.                                      //
13
//                                                              //
14
//  Author(s):                                                  //
15
//      - Conor Santifort, csantifort.amber@gmail.com           //
16
//                                                              //
17
//////////////////////////////////////////////////////////////////
18
//                                                              //
19
// Copyright (C) 2011 Authors and OPENCORES.ORG                 //
20
//                                                              //
21
// This source file may be used and distributed without         //
22
// restriction provided that this copyright statement is not    //
23
// removed from the file and that any derivative work contains  //
24
// the original copyright notice and the associated disclaimer. //
25
//                                                              //
26
// This source file is free software; you can redistribute it   //
27
// and/or modify it under the terms of the GNU Lesser General   //
28
// Public License as published by the Free Software Foundation; //
29
// either version 2.1 of the License, or (at your option) any   //
30
// later version.                                               //
31
//                                                              //
32
// This source is distributed in the hope that it will be       //
33
// useful, but WITHOUT ANY WARRANTY; without even the implied   //
34
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
35
// PURPOSE.  See the GNU Lesser General Public License for more //
36
// details.                                                     //
37
//                                                              //
38
// You should have received a copy of the GNU Lesser General    //
39
// Public License along with this source; if not, download it   //
40
// from http://www.opencores.org/lgpl.shtml                     //
41
//                                                              //
42
----------------------------------------------------------------*/
43
 
44
 
45
#include "address_map.h"
46
#include "line-buffer.h"
47
#include "timer.h"
48
#include "utilities.h"
49
#include "packet.h"
50
#include "udp.h"
51
 
52
 
53
int         udp_checksum_errors_g = 0;
54
 
55
 
56
void parse_udp_packet(char * buf, packet_t* rx_packet)
57
{
58
    unsigned int udp_src_port    = buf[0]<<8|buf[1];
59
    unsigned int udp_dst_port    = buf[2]<<8|buf[3];
60
    unsigned int udp_len         = buf[4]<<8|buf[5];
61 78 csantifort
 
62 61 csantifort
    unsigned short prot_udp=17;
63
    unsigned short word16;
64 78 csantifort
    unsigned long  sum = 0;
65 61 csantifort
    unsigned int   checksum;
66
    int mode_offset;
67
    int binary_mode;
68
    int i;
69 78 csantifort
 
70
 
71 61 csantifort
    for (i=0;i<4;i=i+2){
72
            word16 =((rx_packet->src_ip[i]<<8)&0xFF00)+(rx_packet->src_ip[i+1]&0xFF);
73 78 csantifort
            sum=sum+word16;
74 61 csantifort
    }
75
    for (i=0;i<4;i=i+2){
76
            word16 =((rx_packet->dst_ip[i]<<8)&0xFF00)+(rx_packet->dst_ip[i+1]&0xFF);
77 78 csantifort
            sum=sum+word16;
78 61 csantifort
    }
79
    // the protocol number and the length of the TCP packet
80
    sum = sum + prot_udp + udp_len;
81
 
82
    checksum = header_checksum16(buf, udp_len, sum);
83 78 csantifort
 
84
    if (checksum)
85
        udp_checksum_errors_g++;
86
 
87
 
88
     /* TFTP */
89 80 csantifort
    if (udp_dst_port == 69 && !checksum)
90
        parse_tftp_packet(buf, rx_packet, udp_len-12, udp_src_port, udp_dst_port);
91 78 csantifort
 
92 61 csantifort
}
93
 
94
 
95
 
96
void udp_reply(packet_t* rx_packet, int udp_src_port, int udp_dst_port, int block, int reply_type)
97
{
98
    char* buf = (char*)ETHMAC_TX_BUFFER;
99
    unsigned short checksum;
100
    unsigned short prot_udp=17;
101
    unsigned short udp_len;
102
    unsigned short word16;
103 78 csantifort
    unsigned long  sum = 0;
104 61 csantifort
    int i;
105
    mac_ip_t target;
106 78 csantifort
 
107 61 csantifort
    target.mac[0] = rx_packet->src_mac[0];
108
    target.mac[1] = rx_packet->src_mac[1];
109
    target.mac[2] = rx_packet->src_mac[2];
110
    target.mac[3] = rx_packet->src_mac[3];
111
    target.mac[4] = rx_packet->src_mac[4];
112
    target.mac[5] = rx_packet->src_mac[5];
113
    target.ip[0]  = rx_packet->src_ip[0];
114
    target.ip[1]  = rx_packet->src_ip[1];
115
    target.ip[2]  = rx_packet->src_ip[2];
116
    target.ip[3]  = rx_packet->src_ip[3];
117
 
118
    /* udp header */
119
    buf[34] = (udp_src_port & 0xff00)>>8;
120
    buf[35] =  udp_src_port & 0xff;
121
    buf[36] = (udp_dst_port & 0xff00)>>8;
122
    buf[37] =  udp_dst_port & 0xff;
123 78 csantifort
 
124
    if (reply_type == UDP_ACK)
125 61 csantifort
        udp_len = 8+4;
126
    else /* error */
127
        udp_len = 8+2+2+14;
128
 
129
 
130
    buf[38] = (udp_len      & 0xff00)>>8;
131
    buf[39] =  udp_len      & 0xff;
132 78 csantifort
 
133 61 csantifort
    buf[40] = 0;  // checksum
134
    buf[41] = 0;  // checksum
135 78 csantifort
 
136 61 csantifort
    // -------------------------------------
137 78 csantifort
    // tftf payload
138 61 csantifort
    // -------------------------------------
139
    // Opcode
140 78 csantifort
    buf[42] = 0;
141
    buf[43] = reply_type & 0xff;  // Acknowledgment
142
 
143 61 csantifort
    if (reply_type == UDP_ACK) {
144
        // block
145
        buf[44] = (block & 0xff00)>>8;
146
        buf[45] =  block & 0xff;
147
        }
148
    else {/* error */
149
        // Error Code
150
        buf[44] = 0;
151
        buf[45] = 0;
152
        // 46 to 59
153
        strncpy(&buf[46], "Not supported", 14);
154
        }
155 78 csantifort
 
156
 
157 61 csantifort
    // -------------------------------------
158
    // UDP Checksum calculation
159
    // -------------------------------------
160
    for (i=0;i<4;i=i+2){
161
            word16 =((rx_packet->src_ip[i]<<8)&0xFF00)+(rx_packet->src_ip[i+1]&0xFF);
162 78 csantifort
            sum=sum+word16;
163 61 csantifort
    }
164
    for (i=0;i<4;i=i+2){
165
            word16 =((rx_packet->dst_ip[i]<<8)&0xFF00)+(rx_packet->dst_ip[i+1]&0xFF);
166 78 csantifort
            sum=sum+word16;
167 61 csantifort
    }
168
    // the protocol number and the length of the TCP packet
169
    sum = sum + prot_udp + udp_len;
170
 
171
    checksum = header_checksum16(&buf[34], udp_len, sum);
172
    buf[40] = (checksum & 0xff00)>>8;  // checksum
173
    buf[41] =  checksum & 0xff;        // checksum
174 78 csantifort
 
175 61 csantifort
    ip_header(&buf[14], &target, 20+udp_len, 17); /* 20 byes of tcp  options, bytes 14 to 33, ip_proto = 17, UDP */
176
    ethernet_header(buf, &target, 0x0800);  /*bytes 0 to 13*/
177
    tx_packet(34+udp_len);  // packet length in bytes
178
}
179
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.