]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - net/bootme.c
merged tx6dl-devel into denx master branch
[karo-tx-uboot.git] / net / bootme.c
1 /*
2  * Copyright (C) 2012 Lothar Waßmann <LW@KARO-electronics.de>
3  * based on: code from RedBoot (C) Uwe Steinkohl <US@KARO-electronics.de>
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 #include <common.h>
25 #include <command.h>
26 #include <net.h>
27 #include <wince.h>
28 #include <asm/errno.h>
29
30 DECLARE_GLOBAL_DATA_PTR;
31
32 #define WINCE_VRAM_BASE         0x80000000
33 #define CE_FIX_ADDRESS(a)       ((void *)((a) - WINCE_VRAM_BASE + CONFIG_SYS_SDRAM_BASE))
34
35 #ifndef INT_MAX
36 #define INT_MAX                 ((int)(~0 >> 1))
37 #endif
38
39 /* Bin image parse states */
40 #define CE_PS_RTI_ADDR          0
41 #define CE_PS_RTI_LEN           1
42 #define CE_PS_E_ADDR            2
43 #define CE_PS_E_LEN             3
44 #define CE_PS_E_CHKSUM          4
45 #define CE_PS_E_DATA            5
46
47 #define CE_MIN(a, b)            (((a) < (b)) ? (a) : (b))
48 #define CE_MAX(a, b)            (((a) > (b)) ? (a) : (b))
49
50 #define _STRMAC(s)              #s
51 #define STRMAC(s)               _STRMAC(s)
52
53 static enum bootme_state bootme_state;
54 static int bootme_src_port = 0xdeadface;
55 static int bootme_dst_port = 0xdeadbeef;
56 static uchar bootme_ether[ETH_ALEN];
57 static IPaddr_t bootme_ip;
58 static int bootme_timed_out;
59 //static size_t input_len, input_size;
60 //static void *input_packet;
61 static const char *output_packet; /* used by first send udp */
62 static int output_packet_len;
63 static unsigned long bootme_timeout;
64 static bootme_hand_f *bootme_packet_handler;
65
66 #ifdef DEBUG
67 static void __attribute__((unused)) ce_dump_block(const void *ptr, int length)
68 {
69         const char *p = ptr;
70         int i;
71         int j;
72
73         for (i = 0; i < length; i++) {
74                 if (!(i % 16)) {
75                         printf("\n%p: ", ptr + i);
76                 }
77
78                 printf("%02x ", p[i]);
79                 if (!((i + 1) % 16)){
80                         printf("      ");
81                         for (j = i - 15; j <= i; j++){
82                                 if((p[j] > 0x1f) && (p[j] < 0x7f)) {
83                                         printf("%c", p[j]);
84                                 } else {
85                                         printf(".");
86                                 }
87                         }
88                 }
89         }
90         printf("\n");
91 }
92 #else
93 static inline void ce_dump_block(void *ptr, int length)
94 {
95 }
96 #endif
97
98 static void bootme_timeout_handler(void)
99 {
100         net_set_state(NETLOOP_SUCCESS);
101         bootme_timed_out++;
102 }
103
104 static inline int env_changed(int *id)
105 {
106         int env_id = get_env_id();
107
108         if (*id != env_id) {
109                 debug("env_id: %d -> %d\n", *id, env_id);
110                 *id = env_id;
111                 return 1;
112         }
113         return 0;
114 }
115
116 static int env_id;
117
118 static int is_broadcast(IPaddr_t ip)
119 {
120         static IPaddr_t netmask;
121         static IPaddr_t our_ip;
122
123         return (ip == ~0 ||                             /* 255.255.255.255 */
124             ((netmask & our_ip) == (netmask & ip) &&    /* on the same net */
125             (netmask | ip) == ~0));             /* broadcast to our net */
126 }
127
128 static int check_net_config(void)
129 {
130         if (env_changed(&env_id)) {
131                 char *p;
132                 char *bip;
133
134                 bootme_dst_port = EDBG_DOWNLOAD_PORT;
135                 if (bootme_ip == 0) {
136                         bip = getenv("bootmeip");
137                         if (bip) {
138                                 bootme_ip = getenv_IPaddr("bootmeip");
139                                 if (!bootme_ip)
140                                         return -EINVAL;
141                                 p = strchr(bip, ':');
142                                 if (p) {
143                                         bootme_dst_port = simple_strtoul(p + 1, NULL, 10);
144                                 }
145                         } else {
146                                 memset(&bootme_ip, 0xff, sizeof(bootme_ip));
147                         }
148                 }
149
150                 p = getenv("bootme_dst_port");
151                 if (p)
152                         bootme_dst_port = simple_strtoul(p, NULL, 10);
153
154                 p = getenv("bootme_src_port");
155                 if (p)
156                         bootme_src_port = simple_strtoul(p, NULL, 10);
157                 else
158                         bootme_src_port = bootme_dst_port;
159
160                 if (is_broadcast(bootme_ip))
161                         memset(bootme_ether, 0xff, sizeof(bootme_ether));
162                 else
163                         memset(bootme_ether, 0, sizeof(bootme_ether));
164
165                 net_init();
166                 NetServerIP = bootme_ip;
167         }
168         return 0;
169 }
170
171 static void bootme_wait_arp_handler(uchar *pkt, unsigned dest,
172                                 IPaddr_t sip, unsigned src,
173                                 unsigned len)
174 {
175         net_set_state(NETLOOP_SUCCESS); /* got arp reply - quit net loop */
176 }
177
178 static inline char next_cursor(char c)
179 {
180         switch(c) {
181         case '-':
182                 return '\\';
183         case '\\':
184                 return '|';
185         case '|':
186                 return '/';
187         case '/':
188                 return '-';
189         }
190         return 0;
191 }
192
193 static void bootme_handler(uchar *pkt, unsigned dest_port, IPaddr_t src_ip,
194                         unsigned src_port, unsigned len)
195 {
196         uchar *eth_pkt = pkt;
197         unsigned eth_len = len;
198         static char cursor = '|';
199         enum bootme_state last_state = BOOTME_INIT;
200 #if 1
201         debug("received packet of len %d from %pI4:%d to port %d\n",
202                 len, &src_ip, src_port, dest_port);
203         ce_dump_block(pkt, len);
204 #endif
205         if (!bootme_packet_handler) {
206                 printf("No packet handler set for BOOTME protocol; dropping packet\n");
207                 return;
208         }
209         if (dest_port != bootme_src_port || !len)
210                 return; /* not for us */
211
212         printf("%c\x08", cursor);
213         cursor = next_cursor(cursor);
214
215         if (is_broadcast(bootme_ip)) {
216                 bootme_ip = src_ip;
217         } else if (src_ip != bootme_ip) {
218                 debug("src_ip %pI4 does not match destination IP %pI4\n",
219                         &src_ip, &bootme_ip);
220                 return; /* not from our server */
221         }
222
223         last_state = bootme_state;
224         bootme_dst_port = src_port;
225         debug("bootme_dst_port set to %d\n", bootme_dst_port);
226         if (bootme_state == BOOTME_INIT) {
227                 bootme_src_port = EDBG_SVC_PORT;
228                 debug("%s: bootme_src_port set to %d\n", __func__, bootme_src_port);
229         }
230         bootme_state = bootme_packet_handler(eth_pkt, eth_len);
231         debug("bootme_packet_handler() returned %d\n", bootme_state);
232         if (bootme_state != last_state)
233                 debug("bootme_state: %d -> %d\n", last_state, bootme_state);
234         switch (bootme_state) {
235         case BOOTME_INIT:
236                 break;
237
238         case BOOTME_DOWNLOAD:
239                 if (last_state != BOOTME_INIT)
240                         NetBootFileXferSize += len - 4;
241                 /* fallthru */
242         case BOOTME_DEBUG:
243                 if (last_state == BOOTME_INIT) {
244                         bootme_timeout = 3 * 1000;
245                 }
246                 NetSetTimeout(bootme_timeout, bootme_timeout_handler);
247                 break;
248
249         case BOOTME_DONE:
250                 net_set_state(NETLOOP_SUCCESS);
251                 bootme_packet_handler = NULL;
252                 break;
253
254         case BOOTME_ERROR:
255                 net_set_state(NETLOOP_FAIL);
256                 bootme_packet_handler = NULL;
257         }
258 }
259
260 void BootmeStart(void)
261 {
262         if (bootme_state != BOOTME_DOWNLOAD)
263                 check_net_config();
264
265         if (output_packet_len == 0 ||
266                 memcmp(bootme_ether, NetEtherNullAddr, ETH_ALEN) != 0) {
267                 /* wait for incoming packet */
268                 net_set_udp_handler(bootme_handler);
269                 bootme_timed_out = 0;
270                 NetSetTimeout(bootme_timeout, bootme_timeout_handler);
271         } else {
272                 /* send ARP request */
273                 uchar *pkt;
274
275                 net_set_arp_handler(bootme_wait_arp_handler);
276                 assert(NetTxPacket != NULL);
277                 pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
278                 memcpy(pkt, output_packet, output_packet_len);
279                 NetSendUDPPacket(bootme_ether, bootme_ip, bootme_dst_port,
280                                 bootme_src_port, output_packet_len);
281         }
282 }
283
284 int bootme_send_frame(const void *buf, size_t len)
285 {
286         int ret;
287         struct eth_device *eth;
288         int inited = 0;
289         uchar *pkt;
290
291         eth = eth_get_dev();
292         if (eth == NULL)
293                 return -EINVAL;
294
295         if (bootme_state == BOOTME_INIT)
296                 check_net_config();
297
298         debug("%s: buf: %p len: %u from %pI4:%d to %pI4:%d\n",
299                 __func__, buf, len, &NetOurIP, bootme_src_port, &bootme_ip, bootme_dst_port);
300
301         if (memcmp(bootme_ether, NetEtherNullAddr, ETH_ALEN) == 0) {
302                 if (eth->state == ETH_STATE_ACTIVE)
303                         return 0;       /* inside net loop */
304
305                 output_packet = buf;
306                 output_packet_len = len;
307                 /* wait for arp reply and send packet */
308                 ret = NetLoop(BOOTME);
309                 if (ret < 0) {
310                         /* drop packet */
311                         output_packet_len = 0;
312                         return ret;
313                 }
314                 if (bootme_timed_out)
315                         return -ETIMEDOUT;
316                 return 0;
317         }
318
319         if (eth->state != ETH_STATE_ACTIVE) {
320                 if (eth_is_on_demand_init()) {
321                         ret = eth_init(gd->bd);
322                         if (ret < 0)
323                                 return ret;
324                         eth_set_last_protocol(BOOTME);
325                 } else {
326                         eth_init_state_only(gd->bd);
327                 }
328                 inited = 1;
329         }
330
331         assert(NetTxPacket != NULL);
332         pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
333         memcpy(pkt, buf, len);
334
335         ret = NetSendUDPPacket(bootme_ether, bootme_ip, bootme_dst_port,
336                         bootme_src_port, len);
337         if (inited) {
338                 debug("Stopping network\n");
339                 if (eth_is_on_demand_init())
340                         eth_halt();
341                 else
342                         eth_halt_state_only();
343         }
344         return ret;
345 }
346
347 static void bootme_init(IPaddr_t server_ip)
348 {
349         bootme_state = BOOTME_INIT;
350         bootme_ip = server_ip;
351         /* force reconfiguration in check_net_config() */
352         env_id = 0;
353 }
354
355 int BootMeDownload(bootme_hand_f *handler)
356 {
357         int ret;
358
359         bootme_packet_handler = handler;
360
361         ret = NetLoop(BOOTME);
362         if (ret < 0)
363                 return BOOTME_ERROR;
364         if (bootme_timed_out && bootme_state != BOOTME_INIT)
365                 return BOOTME_ERROR;
366
367         return bootme_state;
368 }
369
370 int BootMeDebugStart(bootme_hand_f *handler)
371 {
372         int ret;
373
374         bootme_packet_handler = handler;
375
376         bootme_init(bootme_ip);
377         bootme_state = BOOTME_DEBUG;
378         bootme_timeout = 3 * 1000;
379         NetSetTimeout(bootme_timeout, bootme_timeout_handler);
380
381         ret = NetLoop(BOOTME);
382         if (ret < 0)
383                 return BOOTME_ERROR;
384         if (bootme_timed_out)
385                 return BOOTME_DONE;
386         return bootme_state;
387 }
388
389 int BootMeRequest(IPaddr_t server_ip, const void *buf, size_t len, int timeout)
390 {
391         bootme_init(server_ip);
392         bootme_timeout = timeout * 1000;
393         bootme_timed_out = 0;
394         NetSetTimeout(bootme_timeout, bootme_timeout_handler);
395         return bootme_send_frame(buf, len);
396 }