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

Subversion Repositories amber

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

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
//  application is embedded in the FPGA's SRAM and is used      //
11
//  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
block_t*    udp_file_g = NULL;
55
block_t*    udp_current_block_g = NULL;
56
 
57
 
58
void parse_udp_packet(char * buf, packet_t* rx_packet)
59
{
60
    unsigned int udp_src_port    = buf[0]<<8|buf[1];
61
    unsigned int udp_dst_port    = buf[2]<<8|buf[3];
62
    unsigned int udp_len         = buf[4]<<8|buf[5];
63
 
64
    unsigned short prot_udp=17;
65
    unsigned short word16;
66
    unsigned long  sum = 0;
67
    unsigned int   checksum;
68
    int mode_offset;
69
    int binary_mode;
70
    int i;
71
 
72
 
73
    for (i=0;i<4;i=i+2){
74
            word16 =((rx_packet->src_ip[i]<<8)&0xFF00)+(rx_packet->src_ip[i+1]&0xFF);
75
            sum=sum+word16;
76
    }
77
    for (i=0;i<4;i=i+2){
78
            word16 =((rx_packet->dst_ip[i]<<8)&0xFF00)+(rx_packet->dst_ip[i+1]&0xFF);
79
            sum=sum+word16;
80
    }
81
    // the protocol number and the length of the TCP packet
82
    sum = sum + prot_udp + udp_len;
83
 
84
    checksum = header_checksum16(buf, udp_len, sum);
85
 
86
    if (checksum)
87
        udp_checksum_errors_g++;
88
 
89
 
90
     /* TFTP */
91
    if (udp_dst_port == 69 && !checksum) {
92
        unsigned int opcode = buf[8]<<8|buf[9];
93
        unsigned int block  = buf[10]<<8|buf[11];
94
        int tftp_len = udp_len - 12;
95
 
96
        mode_offset = next_string(&buf[10]);
97
        binary_mode = strcmp("octet", &buf[10+mode_offset]);
98
 
99
        switch (opcode) {
100
 
101
            case  UDP_READ:
102
                udp_reply(rx_packet, udp_dst_port, udp_src_port, 0, UDP_ERROR);
103
                break;
104
 
105
            case  UDP_WRITE:
106
                udp_file_g = init_buffer_512();
107
                udp_file_g->filename = malloc(256);
108
                strcpy(udp_file_g->filename, &buf[10]);
109
 
110
                if (strncmp(&buf[10], "vmlinux") == 0)
111
                    udp_file_g->linux_boot == 1;
112
 
113
                udp_current_block_g = udp_file_g;
114
 
115
 
116
                if (binary_mode)
117
                    udp_reply(rx_packet, udp_dst_port, udp_src_port, 0, UDP_ACK);
118
                else
119
                    udp_reply(rx_packet, udp_dst_port, udp_src_port, 0, UDP_ERROR);
120
                break;
121
 
122
 
123
            case  UDP_DATA:
124
                udp_reply(rx_packet, udp_dst_port, udp_src_port, block, UDP_ACK);
125
 
126
                if (block > udp_file_g->last_block) {
127
                    // Have not already received this block
128
                    udp_file_g->last_block = block;
129
 
130
                    /* receive and save a block */
131
                    udp_current_block_g->bytes = tftp_len;
132
                    udp_file_g->total_bytes += tftp_len;
133
                    udp_file_g->total_blocks++;
134
 
135
                    memcpy(udp_current_block_g->buf512, &buf[12], tftp_len);
136
 
137
                    /* Prepare the next block */
138
                    if (tftp_len == 512) {
139
                        udp_current_block_g->next = init_buffer_512();
140
                        udp_current_block_g = udp_current_block_g->next;
141
                        }
142
                    else { /* Last block */
143
                        udp_file_g->ready = 1;
144
                        }
145
                    }
146
                break;
147
 
148
 
149
            default: break;
150
            }
151
        }
152
}
153
 
154
 
155
 
156
void udp_reply(packet_t* rx_packet, int udp_src_port, int udp_dst_port, int block, int reply_type)
157
{
158
    char* buf = (char*)ETHMAC_TX_BUFFER;
159
    unsigned short checksum;
160
    unsigned short prot_udp=17;
161
    unsigned short udp_len;
162
    unsigned short word16;
163
    unsigned long  sum = 0;
164
    int i;
165
    mac_ip_t target;
166
 
167
    target.mac[0] = rx_packet->src_mac[0];
168
    target.mac[1] = rx_packet->src_mac[1];
169
    target.mac[2] = rx_packet->src_mac[2];
170
    target.mac[3] = rx_packet->src_mac[3];
171
    target.mac[4] = rx_packet->src_mac[4];
172
    target.mac[5] = rx_packet->src_mac[5];
173
    target.ip[0]  = rx_packet->src_ip[0];
174
    target.ip[1]  = rx_packet->src_ip[1];
175
    target.ip[2]  = rx_packet->src_ip[2];
176
    target.ip[3]  = rx_packet->src_ip[3];
177
 
178
    /* udp header */
179
    buf[34] = (udp_src_port & 0xff00)>>8;
180
    buf[35] =  udp_src_port & 0xff;
181
    buf[36] = (udp_dst_port & 0xff00)>>8;
182
    buf[37] =  udp_dst_port & 0xff;
183
 
184
    if (reply_type == UDP_ACK)
185
        udp_len = 8+4;
186
    else /* error */
187
        udp_len = 8+2+2+14;
188
 
189
 
190
    buf[38] = (udp_len      & 0xff00)>>8;
191
    buf[39] =  udp_len      & 0xff;
192
 
193
    buf[40] = 0;  // checksum
194
    buf[41] = 0;  // checksum
195
 
196
    // -------------------------------------
197
    // tftf payload 
198
    // -------------------------------------
199
    // Opcode
200
    buf[42] = 0;
201
    buf[43] = reply_type & 0xff;  // Acknowledgment    
202
 
203
    if (reply_type == UDP_ACK) {
204
        // block
205
        buf[44] = (block & 0xff00)>>8;
206
        buf[45] =  block & 0xff;
207
        }
208
    else {/* error */
209
        // Error Code
210
        buf[44] = 0;
211
        buf[45] = 0;
212
        // 46 to 59
213
        strncpy(&buf[46], "Not supported", 14);
214
        }
215
 
216
 
217
    // -------------------------------------
218
    // UDP Checksum calculation
219
    // -------------------------------------
220
    for (i=0;i<4;i=i+2){
221
            word16 =((rx_packet->src_ip[i]<<8)&0xFF00)+(rx_packet->src_ip[i+1]&0xFF);
222
            sum=sum+word16;
223
    }
224
    for (i=0;i<4;i=i+2){
225
            word16 =((rx_packet->dst_ip[i]<<8)&0xFF00)+(rx_packet->dst_ip[i+1]&0xFF);
226
            sum=sum+word16;
227
    }
228
    // the protocol number and the length of the TCP packet
229
    sum = sum + prot_udp + udp_len;
230
 
231
    checksum = header_checksum16(&buf[34], udp_len, sum);
232
    buf[40] = (checksum & 0xff00)>>8;  // checksum
233
    buf[41] =  checksum & 0xff;        // checksum
234
 
235
    ip_header(&buf[14], &target, 20+udp_len, 17); /* 20 byes of tcp  options, bytes 14 to 33, ip_proto = 17, UDP */
236
    ethernet_header(buf, &target, 0x0800);  /*bytes 0 to 13*/
237
    tx_packet(34+udp_len);  // packet length in bytes
238
}
239
 
240
 
241
block_t* init_buffer_512()
242
{
243
    block_t* block = malloc(sizeof(block_t));
244
    block->buf512 = malloc(512);
245
    block->next   = NULL;
246
    block->bytes  = 0;
247
    block->last_block  = 0;
248
    block->total_bytes  = 0;
249
    block->total_blocks  = 0;
250
    block->ready  = 0;
251
    block->filename  = NULL;
252
    return block;
253
}
254
 

powered by: WebSVN 2.1.0

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