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

Subversion Repositories amber

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

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

powered by: WebSVN 2.1.0

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