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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [redboot/] [v2_0/] [src/] [net/] [tftp_client.c] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      net/tftp_client.c
4
//
5
//      Stand-alone TFTP support for RedBoot
6
//
7
//==========================================================================
8
//####ECOSGPLCOPYRIGHTBEGIN####
9
// -------------------------------------------
10
// This file is part of eCos, the Embedded Configurable Operating System.
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12
// Copyright (C) 2002 Gary Thomas
13
//
14
// eCos is free software; you can redistribute it and/or modify it under
15
// the terms of the GNU General Public License as published by the Free
16
// Software Foundation; either version 2 or (at your option) any later version.
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
21
// for more details.
22
//
23
// You should have received a copy of the GNU General Public License along
24
// with eCos; if not, write to the Free Software Foundation, Inc.,
25
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26
//
27
// As a special exception, if other files instantiate templates or use macros
28
// or inline functions from this file, or you compile this file and link it
29
// with other works to produce a work based on this file, this file does not
30
// by itself cause the resulting work to be covered by the GNU General Public
31
// License. However the source code for this file must still be made available
32
// in accordance with section (3) of the GNU General Public License.
33
//
34
// This exception does not invalidate any other reasons why a work based on
35
// this file might be covered by the GNU General Public License.
36
//
37
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
38
// at http://sources.redhat.com/ecos/ecos-license/
39
// -------------------------------------------
40
//####ECOSGPLCOPYRIGHTEND####
41
//==========================================================================
42
//#####DESCRIPTIONBEGIN####
43
//
44
// Author(s):    gthomas
45
// Contributors: gthomas
46
// Date:         2000-07-14
47
// Purpose:      
48
// Description:  
49
//              
50
// This code is part of RedBoot (tm).
51
//
52
//####DESCRIPTIONEND####
53
//
54
//==========================================================================
55
 
56
// TFTP client support
57
 
58
#include <redboot.h>     // have_net
59
#include <net/net.h>
60
#include <net/tftp.h>
61
#include <net/tftp_support.h>
62
 
63
// So we remember which ports have been used
64
static int get_port = 7700;
65
 
66
static struct {
67
    bool open;
68
    int  total_timeouts;
69
    unsigned short last_good_block;
70
    int  avail, actual_len;
71
    struct sockaddr_in local_addr, from_addr;
72
    char data[SEGSIZE+sizeof(struct tftphdr)];
73
    char *bufp;
74
} tftp_stream;
75
 
76
int
77
tftp_stream_open(connection_info_t *info,
78
                 int *err)
79
{
80
    struct tftphdr *hdr = (struct tftphdr *)tftp_stream.data;
81
    char *cp, *fp;
82
    char test_buf;
83
 
84
    if (!have_net || tftp_stream.open) {
85
        *err = TFTP_INVALID;  // Already open
86
        return -1;
87
    }
88
 
89
    // Create initial request
90
    hdr->th_opcode = htons(RRQ);  // Read file
91
    cp = (char *)&hdr->th_stuff;
92
    fp = info->filename;
93
    while (*fp) *cp++ = *fp++;
94
    *cp++ = '\0';
95
    // Since this is used for downloading data, OCTET (binary) is the
96
    // only mode that makes sense.
97
    fp = "OCTET";
98
    while (*fp) *cp++ = *fp++;
99
    *cp++ = '\0';
100
 
101
    memset((char *)&tftp_stream.local_addr, 0, sizeof(tftp_stream.local_addr));
102
    tftp_stream.local_addr.sin_family = AF_INET;
103
    tftp_stream.local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
104
    tftp_stream.local_addr.sin_port = htons(get_port++);
105
 
106
    if (info->server->sin_port == 0) {
107
        info->server->sin_port = htons(TFTP_PORT);
108
    }
109
 
110
    // Send request - note: RFC 1350 (TFTP rev 2) indicates that this should be
111
    // only as long as required to hold the request, with the nul terminator.
112
    // Some servers silently go to lunch if the request is not the correct size.
113
    if (__udp_sendto(tftp_stream.data, cp-(char *)hdr,
114
                     info->server, &tftp_stream.local_addr) < 0) {
115
        // Problem sending request
116
        *err = TFTP_NETERR;
117
        return -1;
118
    }
119
 
120
    tftp_stream.open = true;
121
    tftp_stream.avail = 0;
122
    tftp_stream.actual_len = -1;
123
    tftp_stream.last_good_block = 0;
124
    tftp_stream.total_timeouts = 0;
125
    tftp_stream.from_addr.sin_port = 0;
126
 
127
    // Try and read the first byte [block] since no errors are
128
    // reported until then.
129
    if (tftp_stream_read(&test_buf, 1, err) == 1) {
130
        // Back up [rewind] over this datum
131
        tftp_stream.bufp--;
132
        tftp_stream.avail++;
133
        return 0;  // Open and first read successful
134
    } else {
135
        tftp_stream.open = false;
136
        return -1; // Couldn't read
137
    }
138
}
139
 
140
void
141
tftp_stream_close(int *err)
142
{
143
    tftp_stream.open = false;
144
}
145
 
146
int
147
tftp_stream_read(char *buf,
148
                 int len,
149
                 int *err)
150
{
151
    int total_bytes = 0;
152
    int size, recv_len, data_len;
153
    struct timeval timeout;
154
    struct tftphdr *hdr = (struct tftphdr *)tftp_stream.data;
155
 
156
    while (total_bytes < len) {
157
        // Move any bytes which we've already read/buffered
158
        if (tftp_stream.avail > 0) {
159
            size = tftp_stream.avail;
160
            if (size > (len - total_bytes)) size = len - total_bytes;
161
            memcpy(buf, tftp_stream.bufp, size);
162
            buf += size;
163
            tftp_stream.bufp += size;
164
            tftp_stream.avail -= size;
165
            total_bytes += size;
166
        } else {
167
            if (tftp_stream.last_good_block != 0) {
168
                // Send out the ACK
169
                hdr->th_opcode = htons(ACK);
170
                hdr->th_block = htons(tftp_stream.last_good_block);
171
                if (__udp_sendto(tftp_stream.data, 4 /* FIXME */,
172
                                 &tftp_stream.from_addr, &tftp_stream.local_addr) < 0) {
173
                    // Problem sending ACK
174
                    *err = TFTP_NETERR;
175
                    return -1;
176
                }
177
            }
178
            if ((tftp_stream.actual_len >= 0) && (tftp_stream.actual_len < SEGSIZE)) {
179
                // Out of data
180
                break;
181
            }
182
            timeout.tv_sec = TFTP_TIMEOUT_PERIOD;
183
            timeout.tv_usec = 0;
184
            recv_len = sizeof(tftp_stream.data);
185
            if ((data_len = __udp_recvfrom(&tftp_stream.data[0], recv_len, &tftp_stream.from_addr,
186
                                           &tftp_stream.local_addr,  &timeout)) < 0) {
187
                // No data, try again
188
                if ((++tftp_stream.total_timeouts > TFTP_TIMEOUT_MAX) ||
189
                    (tftp_stream.last_good_block == 0)) {
190
                    // Timeout - no data received
191
                    *err = TFTP_TIMEOUT;
192
                    return -1;
193
                }
194
            } else {
195
                if (ntohs(hdr->th_opcode) == DATA) {
196
                    if (ntohs(hdr->th_block) == (tftp_stream.last_good_block+1)) {
197
                        // Consume this data
198
                        data_len -= 4;  /* Sizeof TFTP header */
199
                        tftp_stream.avail = tftp_stream.actual_len = data_len;
200
                        tftp_stream.bufp = hdr->th_data;
201
                        tftp_stream.last_good_block++;
202
                    }
203
                } else {
204
                    if (ntohs(hdr->th_opcode) == ERROR) {
205
                        *err = ntohs(hdr->th_code);
206
                        return -1;
207
                    } else {
208
                        // What kind of packet is this?
209
                        *err = TFTP_PROTOCOL;
210
                        return -1;
211
                    }
212
                }
213
            }
214
        }
215
    }
216
    return total_bytes;
217
}
218
 
219
char *
220
tftp_error(int err)
221
{
222
    char *errmsg = "Unknown error";
223
 
224
    switch (err) {
225
    case TFTP_ENOTFOUND:
226
        return "file not found";
227
    case TFTP_EACCESS:
228
        return "access violation";
229
    case TFTP_ENOSPACE:
230
        return "disk full or allocation exceeded";
231
    case TFTP_EBADOP:
232
        return "illegal TFTP operation";
233
    case TFTP_EBADID:
234
        return "unknown transfer ID";
235
    case TFTP_EEXISTS:
236
        return "file already exists";
237
    case TFTP_ENOUSER:
238
        return "no such user";
239
    case TFTP_TIMEOUT:
240
        return "operation timed out";
241
    case TFTP_NETERR:
242
        return "some sort of network error";
243
    case TFTP_INVALID:
244
        return "invalid parameter";
245
    case TFTP_PROTOCOL:
246
        return "protocol violation";
247
    case TFTP_TOOLARGE:
248
        return "file is larger than buffer";
249
    }
250
    return errmsg;
251
}
252
 
253
//
254
// RedBoot interface
255
//
256
GETC_IO_FUNCS(tftp_io, tftp_stream_open, tftp_stream_close,
257
              0, tftp_stream_read, tftp_error);
258
RedBoot_load(tftp, tftp_io, true, true, 0);
259
 

powered by: WebSVN 2.1.0

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