2 * Copyright (C) 2012 Lothar Waßmann <LW@KARO-electronics.de>
3 * based on: code from RedBoot (C) Uwe Steinkohl <US@KARO-electronics.de>
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
28 #include <asm/errno.h>
30 DECLARE_GLOBAL_DATA_PTR;
32 #define WINCE_VRAM_BASE 0x80000000
33 #define CE_FIX_ADDRESS(a) ((void *)((a) - WINCE_VRAM_BASE + CONFIG_SYS_SDRAM_BASE))
36 #define INT_MAX ((int)(~0 >> 1))
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
44 #define CE_PS_E_CHKSUM 4
45 #define CE_PS_E_DATA 5
47 #define CE_MIN(a, b) (((a) < (b)) ? (a) : (b))
48 #define CE_MAX(a, b) (((a) > (b)) ? (a) : (b))
51 #define STRMAC(s) _STRMAC(s)
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;
67 static void __attribute__((unused)) ce_dump_block(const void *ptr, int length)
73 for (i = 0; i < length; i++) {
75 printf("\n%p: ", ptr + i);
78 printf("%02x ", p[i]);
81 for (j = i - 15; j <= i; j++){
82 if((p[j] > 0x1f) && (p[j] < 0x7f)) {
93 static inline void ce_dump_block(void *ptr, int length)
98 static void bootme_timeout_handler(void)
100 net_set_state(NETLOOP_SUCCESS);
104 static inline int env_changed(int *id)
106 int env_id = get_env_id();
109 debug("env_id: %d -> %d\n", *id, env_id);
118 static int is_broadcast(IPaddr_t ip)
120 static IPaddr_t netmask;
121 static IPaddr_t our_ip;
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 */
128 static int check_net_config(void)
130 if (env_changed(&env_id)) {
134 bootme_dst_port = EDBG_DOWNLOAD_PORT;
135 if (bootme_ip == 0) {
136 bip = getenv("bootmeip");
138 bootme_ip = getenv_IPaddr("bootmeip");
141 p = strchr(bip, ':');
143 bootme_dst_port = simple_strtoul(p + 1, NULL, 10);
146 memset(&bootme_ip, 0xff, sizeof(bootme_ip));
150 p = getenv("bootme_dst_port");
152 bootme_dst_port = simple_strtoul(p, NULL, 10);
154 p = getenv("bootme_src_port");
156 bootme_src_port = simple_strtoul(p, NULL, 10);
158 bootme_src_port = bootme_dst_port;
160 if (is_broadcast(bootme_ip))
161 memset(bootme_ether, 0xff, sizeof(bootme_ether));
163 memset(bootme_ether, 0, sizeof(bootme_ether));
166 NetServerIP = bootme_ip;
171 static void bootme_wait_arp_handler(uchar *pkt, unsigned dest,
172 IPaddr_t sip, unsigned src,
175 net_set_state(NETLOOP_SUCCESS); /* got arp reply - quit net loop */
178 static inline char next_cursor(char c)
193 static void bootme_handler(uchar *pkt, unsigned dest_port, IPaddr_t src_ip,
194 unsigned src_port, unsigned len)
196 uchar *eth_pkt = pkt;
197 unsigned eth_len = len;
198 static char cursor = '|';
199 enum bootme_state last_state = BOOTME_INIT;
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);
205 if (!bootme_packet_handler) {
206 printf("No packet handler set for BOOTME protocol; dropping packet\n");
209 if (dest_port != bootme_src_port || !len)
210 return; /* not for us */
212 printf("%c\x08", cursor);
213 cursor = next_cursor(cursor);
215 if (is_broadcast(bootme_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 */
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);
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) {
238 case BOOTME_DOWNLOAD:
239 if (last_state != BOOTME_INIT)
240 NetBootFileXferSize += len - 4;
243 if (last_state == BOOTME_INIT) {
244 bootme_timeout = 3 * 1000;
246 NetSetTimeout(bootme_timeout, bootme_timeout_handler);
250 net_set_state(NETLOOP_SUCCESS);
251 bootme_packet_handler = NULL;
255 net_set_state(NETLOOP_FAIL);
256 bootme_packet_handler = NULL;
260 void BootmeStart(void)
262 if (bootme_state != BOOTME_DOWNLOAD)
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);
272 /* send ARP request */
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);
284 int bootme_send_frame(const void *buf, size_t len)
287 struct eth_device *eth;
295 if (bootme_state == BOOTME_INIT)
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);
301 if (memcmp(bootme_ether, NetEtherNullAddr, ETH_ALEN) == 0) {
302 if (eth->state == ETH_STATE_ACTIVE)
303 return 0; /* inside net loop */
306 output_packet_len = len;
307 /* wait for arp reply and send packet */
308 ret = NetLoop(BOOTME);
311 output_packet_len = 0;
314 if (bootme_timed_out)
319 if (eth->state != ETH_STATE_ACTIVE) {
320 if (eth_is_on_demand_init()) {
321 ret = eth_init(gd->bd);
324 eth_set_last_protocol(BOOTME);
326 eth_init_state_only(gd->bd);
331 assert(NetTxPacket != NULL);
332 pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
333 memcpy(pkt, buf, len);
335 ret = NetSendUDPPacket(bootme_ether, bootme_ip, bootme_dst_port,
336 bootme_src_port, len);
338 debug("Stopping network\n");
339 if (eth_is_on_demand_init())
342 eth_halt_state_only();
347 static void bootme_init(IPaddr_t server_ip)
349 bootme_state = BOOTME_INIT;
350 bootme_ip = server_ip;
351 /* force reconfiguration in check_net_config() */
355 int BootMeDownload(bootme_hand_f *handler)
359 bootme_packet_handler = handler;
361 ret = NetLoop(BOOTME);
364 if (bootme_timed_out && bootme_state != BOOTME_INIT)
370 int BootMeDebugStart(bootme_hand_f *handler)
374 bootme_packet_handler = handler;
376 bootme_init(bootme_ip);
377 bootme_state = BOOTME_DEBUG;
378 bootme_timeout = 3 * 1000;
379 NetSetTimeout(bootme_timeout, bootme_timeout_handler);
381 ret = NetLoop(BOOTME);
384 if (bootme_timed_out)
389 int BootMeRequest(IPaddr_t server_ip, const void *buf, size_t len, int timeout)
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);