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

Subversion Repositories amber

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

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 81 csantifort
    char buf[96];
99 61 csantifort
    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 78 csantifort
 
106 61 csantifort
    /* udp header */
107
    buf[34] = (udp_src_port & 0xff00)>>8;
108
    buf[35] =  udp_src_port & 0xff;
109
    buf[36] = (udp_dst_port & 0xff00)>>8;
110
    buf[37] =  udp_dst_port & 0xff;
111 78 csantifort
 
112
    if (reply_type == UDP_ACK)
113 61 csantifort
        udp_len = 8+4;
114
    else /* error */
115
        udp_len = 8+2+2+14;
116
 
117
 
118
    buf[38] = (udp_len      & 0xff00)>>8;
119
    buf[39] =  udp_len      & 0xff;
120 78 csantifort
 
121 61 csantifort
    buf[40] = 0;  // checksum
122
    buf[41] = 0;  // checksum
123 78 csantifort
 
124 61 csantifort
    // -------------------------------------
125 78 csantifort
    // tftf payload
126 61 csantifort
    // -------------------------------------
127
    // Opcode
128 78 csantifort
    buf[42] = 0;
129
    buf[43] = reply_type & 0xff;  // Acknowledgment
130
 
131 61 csantifort
    if (reply_type == UDP_ACK) {
132
        // block
133
        buf[44] = (block & 0xff00)>>8;
134
        buf[45] =  block & 0xff;
135
        }
136
    else {/* error */
137
        // Error Code
138
        buf[44] = 0;
139
        buf[45] = 0;
140
        // 46 to 59
141
        strncpy(&buf[46], "Not supported", 14);
142
        }
143 78 csantifort
 
144
 
145 61 csantifort
    // -------------------------------------
146
    // UDP Checksum calculation
147
    // -------------------------------------
148
    for (i=0;i<4;i=i+2){
149
            word16 =((rx_packet->src_ip[i]<<8)&0xFF00)+(rx_packet->src_ip[i+1]&0xFF);
150 78 csantifort
            sum=sum+word16;
151 61 csantifort
    }
152
    for (i=0;i<4;i=i+2){
153
            word16 =((rx_packet->dst_ip[i]<<8)&0xFF00)+(rx_packet->dst_ip[i+1]&0xFF);
154 78 csantifort
            sum=sum+word16;
155 61 csantifort
    }
156
    // the protocol number and the length of the TCP packet
157
    sum = sum + prot_udp + udp_len;
158
 
159
    checksum = header_checksum16(&buf[34], udp_len, sum);
160
    buf[40] = (checksum & 0xff00)>>8;  // checksum
161
    buf[41] =  checksum & 0xff;        // checksum
162 78 csantifort
 
163 81 csantifort
    ip_header(&buf[14], (ip_t*) rx_packet->src_ip, 20+udp_len, 17); /* 20 byes of tcp  options, bytes 14 to 33, ip_proto = 17, UDP */
164
    ethernet_header(buf, (mac_t*) rx_packet->src_mac, 0x0800);  /*bytes 0 to 13*/
165
 
166
    ethmac_tx_packet(buf, 34+udp_len);  // packet length in bytes
167 61 csantifort
}
168
 

powered by: WebSVN 2.1.0

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