Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
381 | ligi | 1 | /******************************************* |
2 | * |
||
3 | * Handling of MK LCD |
||
4 | * |
||
5 | * Author: Marcus -LiGi- Bueschleb |
||
6 | * see README for further Infos |
||
7 | * |
||
8 | * |
||
9 | *******************************************/ |
||
10 | |||
11 | package org.ligi.ufo; |
||
12 | import java.io.*; |
||
13 | |||
14 | //#ifdef j2me |
||
15 | //# import javax.microedition.io.*; |
||
16 | //#endif |
||
17 | |||
18 | public class MKFirmwareFlasher |
||
19 | implements Runnable |
||
20 | { |
||
21 | |||
22 | public String[] flash_msgs; |
||
23 | public int msg_pos=0; |
||
24 | public int bootloader_init_attempt; |
||
25 | |||
26 | |||
27 | private InputStream reader; |
||
28 | private OutputStream writer; |
||
29 | |||
30 | private StreamConnection connection; |
||
31 | |||
32 | private byte[] bl_magic={(byte)'M',(byte)'K',(byte)'B',(byte)'L'}; |
||
33 | private byte bl_magic_pos=0; |
||
34 | private boolean got_bl_magic=false; |
||
35 | |||
36 | private int attempt=0; |
||
37 | |||
38 | |||
39 | byte intension=0; |
||
40 | |||
41 | |||
42 | public boolean success=false; |
||
43 | public boolean job_done=false; |
||
44 | public boolean started=false; |
||
45 | |||
46 | public MKFirmwareFlasher(String _url,byte _intension) |
||
47 | { |
||
48 | intension=_intension; |
||
49 | url=_url; |
||
50 | flash_msgs=new String[100]; |
||
51 | log("loading BL Handler"); |
||
52 | } |
||
53 | |||
54 | |||
55 | public void start() |
||
56 | { |
||
57 | started=true; |
||
58 | connect(); |
||
59 | new Thread( this ).start(); // fire up main Thread |
||
60 | } |
||
61 | |||
62 | public void sleep(int time) |
||
63 | { |
||
64 | try { Thread.sleep(time); } |
||
65 | catch (Exception e) { } |
||
66 | } |
||
67 | |||
68 | private void log(String msg) |
||
69 | { |
||
70 | flash_msgs[msg_pos++]=msg; |
||
71 | } |
||
72 | |||
73 | private void log_(String msg) |
||
74 | { |
||
75 | flash_msgs[msg_pos]=msg; |
||
76 | } |
||
77 | |||
78 | |||
79 | String url; |
||
80 | boolean sigfail; |
||
81 | // int last_avr_sig; |
||
82 | |||
83 | public int avr_sig=0; |
||
84 | |||
85 | public void connect() |
||
86 | { |
||
87 | |||
88 | try { |
||
89 | connection = (StreamConnection) Connector.open(url); |
||
90 | |||
91 | reader=connection.openInputStream(); |
||
92 | writer=connection.openOutputStream(); |
||
93 | } |
||
94 | catch (Exception e) {} |
||
95 | } |
||
96 | |||
97 | public void close_connections() |
||
98 | { |
||
99 | |||
100 | try { |
||
101 | int foo,leech_cnt=0; |
||
102 | while (( reader.available() != 0)&&(leech_cnt<1000)) |
||
103 | { |
||
104 | leech_cnt++; |
||
105 | foo=reader.read(); |
||
106 | } |
||
107 | } |
||
108 | catch (Exception e) {} |
||
109 | |||
110 | // if ((!force)&&root.canvas.do_vibra) root.vibrate(500); |
||
111 | try{ reader.close(); } |
||
112 | catch (Exception inner_ex) { } |
||
113 | |||
114 | try{ writer.close(); } |
||
115 | catch (Exception inner_ex) { } |
||
116 | |||
117 | //#ifdef j2me |
||
118 | //# try{ connection.close(); } |
||
119 | //# catch (Exception inner_ex) { } |
||
120 | //#endif |
||
121 | |||
122 | } |
||
123 | |||
124 | |||
125 | |||
126 | |||
127 | public InputStream in; |
||
128 | public void run() |
||
129 | { |
||
130 | boolean init_sequence=true; |
||
131 | int send_buff_size=0; |
||
132 | int gbl=0; |
||
133 | log_("attempt:"+attempt); |
||
134 | while (!got_bl_magic) |
||
135 | { |
||
136 | attempt++; |
||
137 | try{ |
||
138 | if (init_sequence) |
||
139 | { |
||
140 | // if (attempt!=0) |
||
141 | // sleep((attempt%2==0)?80:800); //800 |
||
142 | |||
143 | msg_pos=0; |
||
144 | flash_msgs[msg_pos++]=("at:"+attempt+" gbl:"+gbl); |
||
145 | flash_msgs[msg_pos]=null; |
||
146 | |||
147 | if ((attempt%2)==0) // after work |
||
148 | { |
||
149 | writer.write('#'); |
||
150 | writer.write(0); |
||
151 | writer.write('R'); |
||
152 | |||
153 | // CRC |
||
154 | writer.write((char)( (((byte)'#' +0 + (byte)'R')%4096)/64 + '=')); |
||
155 | writer.write((char)( (((byte)'#' +0 + (byte)'R')%4096)%64 + '=')); |
||
156 | |||
157 | writer.write('\r'); |
||
158 | |||
159 | writer.flush(); |
||
160 | //sleep(20); |
||
161 | } |
||
162 | |||
163 | writer.write( 27); |
||
164 | writer.flush(); |
||
165 | |||
166 | sleep(20); |
||
167 | |||
168 | writer.write( 0xAA); |
||
169 | writer.flush(); |
||
170 | sleep(180); |
||
171 | } |
||
172 | // else |
||
173 | // sleep(20); |
||
174 | init_sequence=true; |
||
175 | // sleep(20); |
||
176 | while ( reader.available() > 0 ) |
||
177 | { |
||
178 | if ((byte)reader.read()==bl_magic[bl_magic_pos]) |
||
179 | { |
||
180 | bl_magic_pos++; |
||
181 | if (bl_magic_pos==bl_magic.length) |
||
182 | { |
||
183 | log("got bl magic"); |
||
184 | got_bl_magic=true; |
||
185 | gbl++; |
||
186 | bl_magic_pos=0; |
||
187 | |||
188 | int foo,leech_cnt=0; |
||
189 | while (( reader.available() != 0)&&(leech_cnt<1000)) |
||
190 | { |
||
191 | leech_cnt++; |
||
192 | foo=reader.read(); |
||
193 | } |
||
194 | |||
195 | } |
||
196 | |||
197 | |||
198 | } |
||
199 | else |
||
200 | bl_magic_pos=0; |
||
201 | } |
||
202 | |||
203 | |||
204 | |||
205 | if (got_bl_magic) |
||
206 | { |
||
207 | // sleep(50); |
||
208 | flash_msgs[msg_pos++]="reading avr_sig"; |
||
209 | |||
210 | writer.write( 't'); |
||
211 | writer.flush(); |
||
212 | |||
213 | sleep(120); |
||
214 | |||
215 | // if ( reader.available() == 0 ) |
||
216 | //throw new Exception("cant read avrsig"); |
||
217 | |||
218 | /* |
||
219 | int sig1_timeout=0; |
||
220 | while ( reader.available() == 0) |
||
221 | { |
||
222 | |||
223 | if ((sig1_timeout++)>1000 ) |
||
224 | throw new Exception("cant read avrsig"); |
||
225 | sleep(10); |
||
226 | } |
||
227 | |||
228 | */ |
||
229 | avr_sig=reader.read(); |
||
230 | |||
231 | if (avr_sig==63) |
||
232 | init_sequence=false; |
||
233 | /* while (avr_sig==63) |
||
234 | avr_sig=reader.read(); |
||
235 | */ |
||
236 | flash_msgs[msg_pos++]="got avr sig " + avr_sig; |
||
237 | |||
238 | // last_avr_sig=avr_sig; |
||
239 | // for(int sig2_timeout=0;sig2_timeout<100;sig2_timeout++) |
||
240 | /* |
||
241 | int sig2_timeout=0; |
||
242 | while ( reader.available() == 0) |
||
243 | { |
||
244 | |||
245 | if ((sig2_timeout++)>1000 ) |
||
246 | throw new Exception("cant read avrsig-val2"); |
||
247 | sleep(10); |
||
248 | }*/ |
||
249 | |||
250 | int avrsig_suff=reader.read(); |
||
251 | |||
252 | |||
253 | if (avrsig_suff!=0) |
||
254 | throw new Exception("val after avrsig is" +avrsig_suff +"should b 0"); |
||
255 | |||
256 | if ((avr_sig!=0x74)&&(avr_sig!=224)&&(avr_sig!=120)) |
||
257 | { |
||
258 | sigfail=true; |
||
259 | throw new Exception("avr sig" + avr_sig + " unknown"); |
||
260 | } |
||
261 | |||
262 | |||
263 | } |
||
264 | |||
265 | |||
266 | } |
||
267 | catch (Exception e) { |
||
268 | |||
269 | |||
270 | log("" + e.getMessage()); |
||
271 | sleep(1000); |
||
272 | exit_bootloader(); |
||
273 | connect(); |
||
274 | got_bl_magic=false; |
||
275 | init_sequence=false; // after work |
||
276 | } |
||
277 | |||
278 | } |
||
279 | |||
280 | |||
281 | |||
282 | log("have bl-magic & good avrsig" + avr_sig); |
||
283 | |||
284 | try{ |
||
285 | |||
286 | writer.write('T'); |
||
287 | // writer.flush(); |
||
288 | writer.write(avr_sig); // set devicetyp = 0x74 oder 0x76 |
||
289 | writer.flush(); |
||
290 | |||
291 | if (reader.read()!=0x0d) |
||
292 | throw new Exception("cant get buffer size"); |
||
293 | |||
294 | writer.write('V'); |
||
295 | writer.flush(); |
||
296 | |||
297 | int bl_version_major=reader.read(); |
||
298 | int bl_version_minor=reader.read(); |
||
299 | |||
300 | flash_msgs[msg_pos++]="BL Version " + bl_version_major+"."+bl_version_minor; |
||
301 | |||
302 | |||
303 | writer.write('b'); |
||
304 | writer.flush(); |
||
305 | |||
306 | if (reader.read()!='Y') |
||
307 | throw new Exception("cant get buffer size"); |
||
308 | |||
309 | |||
310 | send_buff_size=reader.read()*0x100; |
||
311 | send_buff_size+=reader.read(); |
||
312 | |||
313 | |||
314 | flash_msgs[msg_pos++]="BUFF Size:" + send_buff_size; |
||
315 | |||
316 | } |
||
317 | catch (Exception e2) { |
||
318 | exit_bootloader(); |
||
319 | return; |
||
320 | } |
||
321 | |||
322 | // if (send_buff_size>128) |
||
323 | // send_buff_size=128; |
||
324 | switch (intension) |
||
325 | { |
||
326 | case MKFirmwareHelper.BOOTLOADER_INTENSION_RESET_PARAMS: |
||
327 | try{ |
||
328 | |||
329 | flash_msgs[msg_pos]="reset params .."; |
||
330 | writer.write( MKFirmwareHelper.cmd_reset_params); |
||
331 | |||
332 | writer.flush(); |
||
333 | flash_msgs[msg_pos++]+=" done"; |
||
334 | success=true; |
||
335 | } |
||
336 | catch (Exception e2) { } |
||
337 | |||
338 | break; |
||
339 | |||
340 | case MKFirmwareHelper.BOOTLOADER_INTENSION_FLASH_FIRMWARE: |
||
341 | try{ |
||
342 | |||
343 | |||
344 | |||
345 | byte[] flash_buff =new byte[send_buff_size]; ///!! |
||
346 | |||
347 | //String firmware_filename=(avr_sig==224)?"/navi.bin":((avr_sig==120)?"/mk3.bin":"/fc.bin"); |
||
348 | //flash_msgs[msg_pos++]="Opening firmware " + firmware_filename + ".."; |
||
349 | |||
350 | |||
351 | //InputStream in; |
||
352 | //try { |
||
353 | //in=this.getClass().getResourceAsStream(firmware_filename); |
||
354 | // } |
||
355 | |||
356 | // catch (Exception e) { throw new Exception(" .. cant open firmware"); } |
||
357 | |||
358 | int firmware_size=-1; |
||
359 | |||
360 | try { |
||
361 | firmware_size= ((int)in.read()<<24) |((int)in.read()<<16) | ((int)in.read()<<8) | ((int)in.read()&0xff) ; |
||
362 | } |
||
363 | catch (Exception e) |
||
364 | { |
||
365 | throw new Exception(" .. cant read size"); |
||
366 | } |
||
367 | |||
368 | |||
369 | // in.mark(firmware_size+10); |
||
370 | int blocks2write=((firmware_size/send_buff_size))+(((firmware_size%send_buff_size)==0)?0:1); |
||
371 | flash_msgs[msg_pos++]=".. open("+blocks2write+" blocks," + firmware_size + "bytes)"; |
||
372 | |||
373 | |||
374 | // if (true) throw new Exception("before erasing"); |
||
375 | |||
376 | // if (true) throw new Exception("before erasing" ); |
||
377 | |||
378 | flash_msgs[msg_pos++]="Erasing Flash .."; |
||
379 | writer.write('e'); |
||
380 | writer.flush(); |
||
381 | |||
382 | if (reader.read()!=0x0d) |
||
383 | throw new Exception("cant erase flash"); |
||
384 | |||
385 | flash_msgs[msg_pos]+="OK"; |
||
386 | |||
387 | |||
388 | writer.write('A'); |
||
389 | writer.write(0); |
||
390 | writer.write(0); |
||
391 | writer.flush(); |
||
392 | |||
393 | if (reader.read()!=0x0d) |
||
394 | throw new Exception("cant set addr"); |
||
395 | |||
396 | flash_msgs[msg_pos++]="addr set"; |
||
397 | |||
398 | |||
399 | // int blocks2write=((firmware_size/send_buff_size)); |
||
400 | |||
401 | |||
402 | for ( int block=0; block<blocks2write; block ++) |
||
403 | { |
||
404 | |||
405 | for (int s=0;s<send_buff_size;s++) |
||
406 | flash_buff[s]=0; |
||
407 | int hex_bytes_read=in.read(flash_buff,0,send_buff_size); |
||
408 | |||
409 | /* |
||
410 | if (avr_sig==224) |
||
411 | { |
||
412 | |||
413 | |||
414 | for (int s=hex_bytes_read;s<send_buff_size;s++) |
||
415 | flash_buff[s]=0; |
||
416 | hex_bytes_read=send_buff_size; // fix |
||
417 | } |
||
418 | |||
419 | */ |
||
420 | |||
421 | |||
422 | hex_bytes_read=send_buff_size; // fix |
||
423 | |||
424 | flash_msgs[msg_pos]="bl:" + (block+1) + "/" + blocks2write + " si:"+hex_bytes_read ; |
||
425 | |||
426 | writer.write('B'); |
||
427 | writer.write((hex_bytes_read>>8)& 0xFF); |
||
428 | writer.write((hex_bytes_read)& 0xFF); |
||
429 | writer.write('F'); |
||
430 | writer.flush(); |
||
431 | |||
432 | |||
433 | writer.write(flash_buff,0,hex_bytes_read); |
||
434 | writer.flush(); |
||
435 | |||
436 | |||
437 | if (avr_sig==224) |
||
438 | { |
||
439 | int crc=0xFFFF; |
||
440 | for (int crc_pos=0;crc_pos<hex_bytes_read;crc_pos++) |
||
441 | crc=MKFirmwareHelper.CRC16(flash_buff[crc_pos],crc); |
||
442 | writer.write((crc>>8)&0xff); |
||
443 | writer.write(crc&0xff); |
||
444 | writer.flush(); |
||
445 | } |
||
446 | // flash_msgs[msg_pos]+="ok"; |
||
447 | // writer.flush(); |
||
448 | |||
449 | |||
450 | |||
451 | if (reader.read()!=0x0d) |
||
452 | throw new Exception("abort write at block"+block); |
||
453 | |||
454 | |||
455 | |||
456 | // sleep(1000); |
||
457 | } // for block |
||
458 | |||
459 | |||
460 | sleep(1000); |
||
461 | |||
462 | |||
463 | |||
464 | success=true; |
||
465 | } |
||
466 | catch (Exception e2) { |
||
467 | msg_pos++;} |
||
468 | |||
469 | break; |
||
470 | } |
||
471 | |||
472 | |||
473 | sleep(50); |
||
474 | exit_bootloader(); |
||
475 | job_done=true; |
||
476 | |||
477 | } |
||
478 | |||
479 | |||
480 | |||
481 | public void exit_bootloader() |
||
482 | { |
||
483 | try{ |
||
484 | writer.write('E'); |
||
485 | writer.flush(); |
||
486 | } |
||
487 | catch (Exception e) { |
||
488 | flash_msgs[msg_pos++]="cant exit bootloader" ; |
||
489 | } |
||
490 | flash_msgs[msg_pos++]="Exit BL done" ; |
||
491 | |||
492 | |||
493 | |||
494 | close_connections(); |
||
495 | } |
||
496 | |||
497 | |||
498 | } |