1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
// SPDX-License-Identifier: GPL-2.0
/*
* WGET/HTTP support driver based on U-BOOT's nfs.c
* Copyright Duncan Hare <dh@synoia.com> 2017
*/
#include <asm/global_data.h>
#include <command.h>
#include <display_options.h>
#include <env.h>
#include <efi_loader.h>
#include <image.h>
#include <lmb.h>
#include <mapmem.h>
#include <net.h>
#include <net/tcp.h>
#include <net/wget.h>
#include <stdlib.h>
DECLARE_GLOBAL_DATA_PTR;
/* The default, change with environment variable 'httpdstp' */
#define SERVER_PORT 80
#define HASHES_PER_LINE 65
#define HTTP_MAX_HDR_LEN 2048
static const char bootfile1[] = "GET ";
static const char bootfile3[] = " HTTP/1.0\r\n\r\n";
static const char http_eom[] = "\r\n\r\n";
static const char content_len[] = "Content-Length:";
static const char http_ok[] = " 200 ";
static const char linefeed[] = "\r\n";
static struct in_addr web_server_ip;
static unsigned int server_port;
static unsigned long content_length;
static u32 http_hdr_size;
static int wget_tsize_num_hash;
static char *image_url;
static enum net_loop_state wget_loop_state;
/**
* store_block() - store block in memory
* @src: source of data
* @offset: offset
* @len: length
*/
static inline int store_block(uchar *src, unsigned int offset, unsigned int len)
{
ulong store_addr = image_load_addr + offset;
uchar *ptr;
if (CONFIG_IS_ENABLED(LMB) && wget_info->set_bootdev) {
if (store_addr < image_load_addr ||
lmb_read_check(store_addr, len)) {
printf("\nwget error: ");
printf("trying to overwrite reserved memory...\n");
return -1;
}
}
ptr = map_sysmem(store_addr, len);
memcpy(ptr, src, len);
unmap_sysmem(ptr);
return 0;
}
static void show_block_marker(u32 packets)
{
int cnt;
if (content_length != -1) {
if (net_boot_file_size > content_length)
content_length = net_boot_file_size;
cnt = net_boot_file_size * 50 / content_length;
while (wget_tsize_num_hash < cnt) {
putc('#');
wget_tsize_num_hash++;
}
} else {
if ((packets % 10) == 0)
putc('#');
else if (((packets + 1) % (10 * HASHES_PER_LINE)) == 0)
puts("\n");
}
}
static void tcp_stream_on_closed(struct tcp_stream *tcp)
{
if (tcp->status != TCP_ERR_OK)
wget_loop_state = NETLOOP_FAIL;
net_set_state(wget_loop_state);
if (wget_loop_state != NETLOOP_SUCCESS) {
printf("\nwget: Transfer Fail, TCP status - %d\n", tcp->status);
return;
}
printf("\nPackets received %d, Transfer Successful\n", tcp->rx_packets);
efi_set_bootdev("Http", "", image_url,
map_sysmem(image_load_addr, 0),
net_boot_file_size);
env_set_hex("filesize", net_boot_file_size);
}
static void tcp_stream_on_rcv_nxt_update(struct tcp_stream *tcp, u32 rx_bytes)
{
char *pos, *tail;
uchar saved, *ptr;
int i;
if (http_hdr_size) {
net_boot_file_size = rx_bytes - http_hdr_size;
show_block_marker(tcp->rx_packets);
return;
}
ptr = map_sysmem(image_load_addr, rx_bytes + 1);
saved = ptr[rx_bytes];
ptr[rx_bytes] = '\0';
pos = strstr((char *)ptr, http_eom);
ptr[rx_bytes] = saved;
if (!pos) {
if (rx_bytes < HTTP_MAX_HDR_LEN &&
tcp->state == TCP_ESTABLISHED)
goto end;
printf("ERROR: misssed HTTP header\n");
tcp_stream_close(tcp);
wget_loop_state = NETLOOP_FAIL;
goto end;
}
http_hdr_size = pos - (char *)ptr + strlen(http_eom);
*pos = '\0';
pos = strstr((char *)ptr, linefeed);
if (pos)
i = pos - (char *)ptr;
else
i = http_hdr_size - strlen(http_eom);
printf("%.*s\n", i, ptr);
if (!strstr((char *)ptr, http_ok)) {
debug_cond(DEBUG_WGET, "wget: Connected Bad Xfer\n");
tcp_stream_close(tcp);
wget_loop_state = NETLOOP_FAIL;
goto end;
}
debug_cond(DEBUG_WGET, "wget: Connctd pkt %p hlen %x\n",
ptr, http_hdr_size);
pos = strstr((char *)ptr, content_len);
if (!pos) {
content_length = -1;
} else {
pos += strlen(content_len) + 1;
content_length = simple_strtoul(pos, &tail, 10);
if (*tail != '\r' && *tail != '\n' && *tail != '\0')
content_length = -1;
printf("%s %d\n", content_len, (int)content_length);
}
net_boot_file_size = rx_bytes - http_hdr_size;
memmove(ptr, ptr + http_hdr_size, net_boot_file_size);
end:
unmap_sysmem(ptr);
wget_loop_state = NETLOOP_SUCCESS;
}
static int tcp_stream_rx(struct tcp_stream *tcp, u32 rx_offs, void *buf, int len)
{
store_block(buf, rx_offs - http_hdr_size, len);
return len;
}
static int tcp_stream_tx(struct tcp_stream *tcp, u32 tx_offs, void *buf, int maxlen)
{
int ret;
if (tx_offs)
return 0;
ret = snprintf(buf, maxlen, "%s%s%s", bootfile1, image_url, bootfile3);
return ret;
}
static int tcp_stream_on_create(struct tcp_stream *tcp)
{
if (tcp->rhost.s_addr != web_server_ip.s_addr ||
tcp->rport != server_port)
return 0;
tcp->max_retry_count = WGET_RETRY_COUNT;
tcp->initial_timeout = WGET_TIMEOUT;
tcp->on_closed = tcp_stream_on_closed;
tcp->on_rcv_nxt_update = tcp_stream_on_rcv_nxt_update;
tcp->rx = tcp_stream_rx;
tcp->tx = tcp_stream_tx;
return 1;
}
#define BLOCKSIZE 512
void wget_start(void)
{
struct tcp_stream *tcp;
image_url = strchr(net_boot_file_name, ':');
if (image_url > 0) {
web_server_ip = string_to_ip(net_boot_file_name);
++image_url;
net_server_ip = web_server_ip;
} else {
web_server_ip = net_server_ip;
image_url = net_boot_file_name;
}
debug_cond(DEBUG_WGET,
"wget: Transfer HTTP Server %pI4; our IP %pI4\n",
&web_server_ip, &net_ip);
/* Check if we need to send across this subnet */
if (net_gateway.s_addr && net_netmask.s_addr) {
struct in_addr our_net;
struct in_addr server_net;
our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
server_net.s_addr = net_server_ip.s_addr & net_netmask.s_addr;
if (our_net.s_addr != server_net.s_addr)
debug_cond(DEBUG_WGET,
"wget: sending through gateway %pI4",
&net_gateway);
}
debug_cond(DEBUG_WGET, "URL '%s'\n", image_url);
if (net_boot_file_expected_size_in_blocks) {
debug_cond(DEBUG_WGET, "wget: Size is 0x%x Bytes = ",
net_boot_file_expected_size_in_blocks * BLOCKSIZE);
print_size(net_boot_file_expected_size_in_blocks * BLOCKSIZE,
"");
}
debug_cond(DEBUG_WGET,
"\nwget:Load address: 0x%lx\nLoading: *\b", image_load_addr);
/*
* Zero out server ether to force arp resolution in case
* the server ip for the previous u-boot command, for example dns
* is not the same as the web server ip.
*/
memset(net_server_ethaddr, 0, 6);
net_boot_file_size = 0;
http_hdr_size = 0;
wget_tsize_num_hash = 0;
wget_loop_state = NETLOOP_FAIL;
server_port = env_get_ulong("httpdstp", 10, SERVER_PORT) & 0xffff;
tcp_stream_set_on_create_handler(tcp_stream_on_create);
tcp = tcp_stream_connect(web_server_ip, server_port);
if (!tcp) {
printf("No free tcp streams\n");
net_set_state(NETLOOP_FAIL);
return;
}
tcp_stream_put(tcp);
}
int wget_do_request(ulong dst_addr, char *uri)
{
int ret;
char *s, *host_name, *file_name, *str_copy;
/*
* Download file using wget.
*
* U-Boot wget takes the target uri in this format.
* "<http server ip>:<file path>" e.g.) 192.168.1.1:/sample/test.iso
* Need to resolve the http server ip address before starting wget.
*/
str_copy = strdup(uri);
if (!str_copy)
return -ENOMEM;
s = str_copy + strlen("http://");
host_name = strsep(&s, "/");
if (!s) {
ret = -EINVAL;
goto out;
}
file_name = s;
host_name = strsep(&host_name, ":");
if (string_to_ip(host_name).s_addr) {
s = host_name;
} else {
#if IS_ENABLED(CONFIG_CMD_DNS)
net_dns_resolve = host_name;
net_dns_env_var = "httpserverip";
if (net_loop(DNS) < 0) {
ret = -EINVAL;
goto out;
}
s = env_get("httpserverip");
if (!s) {
ret = -EINVAL;
goto out;
}
#else
ret = -EINVAL;
goto out;
#endif
}
strlcpy(net_boot_file_name, s, sizeof(net_boot_file_name));
strlcat(net_boot_file_name, ":/", sizeof(net_boot_file_name)); /* append '/' which is removed by strsep() */
strlcat(net_boot_file_name, file_name, sizeof(net_boot_file_name));
image_load_addr = dst_addr;
ret = net_loop(WGET);
out:
free(str_copy);
return ret < 0 ? ret : 0;
}
/**
* wget_validate_uri() - validate the uri for wget
*
* @uri: uri string
*
* This function follows the current U-Boot wget implementation.
* scheme: only "http:" is supported
* authority:
* - user information: not supported
* - host: supported
* - port: not supported(always use the default port)
*
* Uri is expected to be correctly percent encoded.
* This is the minimum check, control codes(0x1-0x19, 0x7F, except '\0')
* and space character(0x20) are not allowed.
*
* TODO: stricter uri conformance check
*
* Return: true on success, false on failure
*/
bool wget_validate_uri(char *uri)
{
char c;
bool ret = true;
char *str_copy, *s, *authority;
for (c = 0x1; c < 0x21; c++) {
if (strchr(uri, c)) {
log_err("invalid character is used\n");
return false;
}
}
if (strchr(uri, 0x7f)) {
log_err("invalid character is used\n");
return false;
}
if (strncmp(uri, "http://", 7)) {
log_err("only http:// is supported\n");
return false;
}
str_copy = strdup(uri);
if (!str_copy)
return false;
s = str_copy + strlen("http://");
authority = strsep(&s, "/");
if (!s) {
log_err("invalid uri, no file path\n");
ret = false;
goto out;
}
s = strchr(authority, '@');
if (s) {
log_err("user information is not supported\n");
ret = false;
goto out;
}
s = strchr(authority, ':');
if (s) {
log_err("user defined port is not supported\n");
ret = false;
goto out;
}
out:
free(str_copy);
return ret;
}
|