Blame view

RIOT/boards/msba2-common/tools/src/download.c 36.8 KB
fb11e647   vrobic   reseau statique a...
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
  /*
   * LPC 2000 Loader, http://www.pjrc.com/arm/lpc2k_pgm
   * Copyright (c) 2004, PJRC.COM, LLC, <paul@pjrc.com>
   *
   * This program is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License as published by the
   * Free Software Foundation; version 2 of the License.
   *
   * This program is distributed in the hope that it will be useful, but
   * WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
   * Public License for more details.
   *
   * You should have received a copy of the GNU General Public License along
   * with this program; if not, write to the Free Software Foundation, Inc.,
   * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   */
  
  /* If this code fails to build, please provide at least the following
   * information when requesting (free) technical support.
   *
   * 1: Complete copy of all messages during the build.
   * 2: Output of "gtk-config --version"
   * 3: Output of "gtk-config --libs"
   * 4: Output of "gtk-config --cflags"
   * 5: Output of "uname -a"
   * 6: Version of GTK installed... eg, type: ls -l /lib/libgtk*
   * 7: Other info... which linux distribution, version, other software
   */
  
  #include <stdio.h>
  #include <stdarg.h>
  #include <stdlib.h>
  #include <string.h>
  #include <ctype.h>
  #include <unistd.h>
  
  #include "lpc2k_pgm.h"
  #include "download.h"
  #include "serial.h"
  #include "ihex.h"
  #include "uuencode.h"
  //#include "gui.h"
  #include "chipinfo.h"
  #include "boot.h"
  #include "control_2xxx.h"
  
  
  // This will cause all bytes send and received to be printed as hex.
  // It's a LOT of extra output, but useful for difficult debugging
  // of what's _really_ being sent and received.
  //#define PRINT_TX_RX_BYTES
  
  
  static void download_main(int event);
  static void xmit_cmd(const char *cmd, int max_time);
  static void mk_valid_code_vector(void);
  static unsigned int sum(unsigned char *data, int num);
  
  
  static int state = 0;
  static int reboot_only = 0;
  static char expected_echo_buf[4096];
  static char *expected_echo_ptr = NULL;
  static char parsed_response_buf[4096];
  static char *parsed_response_ptr = NULL;
  static int response_timer = 0;
  
  extern int programming_done;
  extern int done_program(int);
  
  
  char *port_name = "/dev/ttyUSB1";
  char *file_name = "";
  char *crystal = "16";
  
  /****************************************************************/
  /*                              */
  /*           Main Download Section          */
  /*                              */
  /****************************************************************/
  
  // possible states
  #define SYNC_1              1
  #define SYNC_2              2
  #define SYNC_3              3
  #define CHIP_ID             4
  #define UNLOCK              5
  #define BLANK_CHECK_SECTOR  6
  #define ERASE_PREPARE       7
  #define ERASE_SECTOR        8
  #define DOWNLOAD_CODE       9
  #define XMIT_DATA           10
  #define XMIT_CKSUM          11
  #define WRITE_PREPARE       12
  #define WRITE_SECTOR        13
  #define BOOT_HARD           14
  #define BOOT_SOFT           15
  #define BOOT_XMIT_DATA      16
  #define BOOT_XMIT_CKSUM     17
  #define BOOT_RUN_CODE       18
  
  
  // possible input values for "event"
  #define BEGIN       1
  #define RESPONSE    2
  #define TIMEOUT     3
  #define RETRY       4
  
  
  
  
  int download_begin(char *file)
  {
      int r;
  
      file_name = file;
  
      printf("\r\nEntering Bootloader Mode\r\n");
      hard_reset_to_bootloader();
      printf("Read \"%s\"", file_name);
      r = read_intel_hex(file_name);
  
      if (r < 0) {
          /* abort on ioerror */
          return 0;
      }
  
      printf(": %d bytes\r\n", r);
      mk_valid_code_vector();
      state = SYNC_1;
      reboot_only = 0;
      download_main(BEGIN);
      return 1;
  }
  
  
  void soft_reboot_begin(void)
  {
      printf("\r\nEntering Bootloader Mode\r\n");
      hard_reset_to_bootloader();
      state = SYNC_1;
      reboot_only = 1;
      download_main(BEGIN);
  }
  
  static void mk_valid_code_vector(void)
  {
      unsigned char b[4];
      unsigned int sum = 0;
      int addr;
  
      for (addr = 0; addr < 0x20; addr += 4) {
          if (addr != 0x14) {
              get_ihex_data(addr, 4, b);
              sum += (b[0] | (b[1] << 8) | (b[2] << 16) | (b[3] << 24));
          }
      }
  
      sum ^= 0xFFFFFFFF;
      sum++;
      b[0] = (sum >> 0) & 255;
      b[1] = (sum >> 8) & 255;
      b[2] = (sum >> 16) & 255;
      b[3] = (sum >> 24) & 255;
      put_ihex_data(0x14, 4, b);
  }
  
  
  static unsigned int sum(unsigned char *data, int num)
  {
      unsigned int sum = 0;
  
      while (num > 0) {
          sum += *data++;
          num--;
      }
  
      return sum;
  }
  
  
  static int num_lines(const char *buf)
  {
      const char *p;
      int count = 0;
  
      p = buf;
  
      while (p != NULL) {
          p = strstr(p, "\r\n");
  
          if (p != NULL) {
              count++;
              p += 2;
          }
      }
  
      return count;
  }
  
  void trim_crlf(char *str)
  {
      char *p;
      p = strstr(str, "\r\n");
  
      if (p != NULL) {
          *p = '\0';
      }
  }
  
  void copy_boot_code_to_memory(struct chip_info_struct *chip)
  {
      int i;
      unsigned char c[4];
  
      for (i = 0; i < chip->bootprog[0]; i++) {
          c[3] = (chip->bootprog[i + 1] >> 24) & 255;
          c[2] = (chip->bootprog[i + 1] >> 16) & 255;
          c[1] = (chip->bootprog[i + 1] >> 8) & 255;
          c[0] = (chip->bootprog[i + 1]) & 255;
          put_ihex_data(i * 4, 4, c);
      }
  }
  
  
  #define NO_SYNC_ERR "\r\n\
  ERROR: Unable to sync to baud rate.\r\n\
  This probably means the LPC2xxx chip is not connected\r\n\
  or it is not being reset, or P0.14 is not low after\r\n\
  reset to cause it to enter the bootloader mode.\r\n\r\n\
  Please check the serial port connection, make sure\r\n\
  pin P0.14 is low (or tied to RTS via RS-232 level\r\n\
  translator), and the chip has been reset (or reset\r\n\
  is tied to DTR via RS-232 level translator).\r\n"
  
  #define UNKNOWN_CHIP_ERROR "\r\n\
  Unknown chip ID: \"%s\".\r\n\r\n\
  Perhaps you have a new Philips LPC chip which does not\r\n\
  have its ID string and sector map defined in this program?\r\n\
  Please contact paul@pjrc.com.  Please include an exact copy\r\n\
  of this message and any info about the chip and other\r\n\
  hardware you may be using.  Thanks :-)\r\n"
  
  
  static void download_main(int event)
  {
      char buf[4096];
      unsigned char bytes[256];
      double xtal;
      int n;
      static unsigned int cksum;
      static int retry = 0;
      static int sector;      // current sector we're doing
      static int sector_offset;
      static struct chip_info_struct *chip;   // which chip
      static int current_addr, num_to_xmit, linecount;
  
  
      while (1) {
          switch (state) {
              case SYNC_1:
                  switch (event) {
                      case BEGIN:
                          printf("Attempting baud sync");
                          retry = 0;
  
                      case RETRY:
                          printf(".");
                          fflush(stdout);
                          xmit_cmd("?", 2);
                          return;
  
                      case RESPONSE:
                          if (strcmp(parsed_response_buf, "Synchronized\r\n") == 0) {
                              //printf("response: sync'd\n");
                              state = SYNC_2;
                              event = BEGIN;
                              break;
                          }
  
                          if (strcmp(parsed_response_buf, "?") == 0) {
                              //printf("response: echo only\n");
                              retry++;
  
                              if (retry > 150) {
                                  download_cancel(NO_SYNC_ERR);
                                  return;
                              }
  
                              event = RETRY;
                              usleep(30000);
                              break;
                          }
  
                          snprintf(buf, sizeof(buf), "Unexpected response to sync, \"%s\"",
                                   parsed_response_buf);
                          download_cancel(buf);
                          return;
  
                      case TIMEOUT:
                          if (retry < 100) {
                              retry++;
                              event = RETRY;
                              break;
                          }
  
                          download_cancel(NO_SYNC_ERR);
                          return;
                  }
  
                  break;
  
  
              case SYNC_2:
                  switch (event) {
                      case BEGIN:
                          xmit_cmd("Synchronized\r\n", 3);
                          return;
  
                      case RESPONSE:
                          if (num_lines(parsed_response_buf) != 1) {
                              return;
                          }
  
                          if (strcmp(parsed_response_buf, "OK\r\n") == 0) {
                              state = SYNC_3;
                              event = BEGIN;
                              break;
                          }
                          else {
                              snprintf(buf, sizeof(buf), "Unable to complete baud sync, %s",
                                       parsed_response_buf);
                              download_cancel(buf);
                              return;
                          }
  
                          return;
  
                      case TIMEOUT:
                          download_cancel("No response to complete baud sync");
                          return;
                  }
  
                  break;
  
  
              case SYNC_3:
                  switch (event) {
                      case BEGIN:
                          if (sscanf(crystal, "%20lf", &xtal) != 1) {
                              printf("\r\n");
                              download_cancel("Crystal frequency is required for 3rd step of baud rate sync");
                              return;
                          }
  
                          if (xtal < 10.0 || xtal > 25.0) {
                              printf("\r\n");
                              printf("Warning: crystal frequency out of range (10.0 to 25.0), continuing anyway! (hope you know what you're doing)\r\n");
                          }
  
                          snprintf(buf, sizeof(buf), "%d\r\n", (int)(xtal * 1000.0 + 0.5));
                          xmit_cmd(buf, 3);
                          return;
  
                      case RESPONSE:
                          if (num_lines(parsed_response_buf) != 1) {
                              return;
                          }
  
                          if (strcmp(parsed_response_buf, "OK\r\n") == 0) {
                              printf("Baud sync sucessful\r\n");
                              state = CHIP_ID;
                              event = BEGIN;
                              break;
                          }
                          else {
                              snprintf(buf, sizeof(buf), "wrong response to crystal: %s",
                                       parsed_response_buf);
                              download_cancel(buf);
                              return;
                          }
  
                          return;
  
                      case TIMEOUT:
                          download_cancel("No response to crystal speed");
                          return;
                  }
  
                  break;
  
  
              case CHIP_ID:
                  switch (event) {
                      case BEGIN:
                          xmit_cmd("J\r\n", 3);
                          return;
  
                      case RESPONSE:
                          if (num_lines(parsed_response_buf) < 2) {
                              return;
                          }
  
                          if (strncmp(parsed_response_buf, "0\r\n", 3) == 0) {
                              trim_crlf(parsed_response_buf + 3);
  
                              for (chip = chip_info; chip->part_number != NULL; chip++) {
                                  if (strcmp(parsed_response_buf + 3, chip->id_string) == 0) {
                                      break;
                                  }
                              }
  
                              if (chip->part_number == NULL) {
                                  snprintf(buf, sizeof(buf), UNKNOWN_CHIP_ERROR,
                                           parsed_response_buf + 3);
                                  download_cancel(buf);
                                  break;
                              }
  
                              printf("Found chip: \"%s\"\r\n", chip->part_number);
                              //download_cancel("stop here, remove this later");
                              state = UNLOCK;
                              event = BEGIN;
                              break;
                          }
                          else {
                              snprintf(buf, sizeof(buf), "wrong response to ID: %s",
                                       parsed_response_buf);
                              download_cancel(buf);
                              return;
                          }
  
                          return;
  
                      case TIMEOUT:
                          download_cancel("No response to unlock command");
                          return;
                  }
  
                  break;
  
  
              case UNLOCK:
                  switch (event) {
                      case BEGIN:
                          xmit_cmd("U 23130\r\n", 3);
                          return;
  
                      case RESPONSE:
                          if (num_lines(parsed_response_buf) != 1) {
                              return;
                          }
  
                          if (strcmp(parsed_response_buf, "0\r\n") == 0) {
                              printf("Device Unlocked\r\n");
  
                              if (reboot_only) {
                                  state = BOOT_SOFT;
                              }
                              else {
                                  state = BLANK_CHECK_SECTOR;
                                  printf("Erasing....\r\n");
                                  sector = 0;
                              }
  
                              event = BEGIN;
                              break;
                          }
                          else {
                              snprintf(buf, sizeof(buf), "wrong response unlock: %s",
                                       parsed_response_buf);
                              download_cancel(buf);
                              return;
                          }
  
                          return;
  
                      case TIMEOUT:
                          download_cancel("No response to unlock command");
                          return;
                  }
  
                  break;
  
  
              case BLANK_CHECK_SECTOR:
                  switch (event) {
                      case BEGIN:
                          if (sector >= chip->num_sector) {
                              printf("Programming....\r\n");
                              state = DOWNLOAD_CODE;
                              sector = sector_offset = 0;
                              event = BEGIN;
                              break;
                          }
  
                          printf("  Sector %2d: ", sector);
                          fflush(stdout);
  
                          if (!bytes_within_range(chip->layout[sector].address,
                                                  chip->layout[sector].address + chip->layout[sector].size - 1)) {
                              printf("not used\r\n");
                              sector++;
                              break;
                          }
  
                          if (sector == 0) {
                              // can't blank check sector 0, so always erase it
                              state = ERASE_PREPARE;
                              break;
                          }
  
                          snprintf(buf, sizeof(buf), "I %d %d\r\n", sector, sector);
                          xmit_cmd(buf, 5);
                          return;
  
                      case RESPONSE:
                          if (num_lines(parsed_response_buf) == 1 &&
                              strcmp(parsed_response_buf, "0\r\n") == 0) {
                              printf("already blank\r\n");
                              sector++;
                              event = BEGIN;
                              break;
                          }
                          else {
                              if (num_lines(parsed_response_buf) < 3) {
                                  return;
                              }
  
                              state = ERASE_PREPARE;
                              event = BEGIN;
                              break;
                          }
  
                      case TIMEOUT:
                          download_cancel("No response to blank check");
                          return;
                  }
  
                  break;
  
  
  
              case ERASE_PREPARE:
                  switch (event) {
                      case BEGIN:
                          printf("prep, ");
                          fflush(stdout);
                          snprintf(buf, sizeof(buf), "P %d %d\r\n", sector, sector);
                          xmit_cmd(buf, 8);
                          return;
  
                      case RESPONSE:
                          if (num_lines(parsed_response_buf) != 1) {
                              return;
                          }
  
                          if (strcmp(parsed_response_buf, "0\r\n") == 0) {
                              state = ERASE_SECTOR;
                              event = BEGIN;
                              break;
                          }
                          else {
                              download_cancel("Unable to prep for write");
                              return;
                          }
  
                      case TIMEOUT:
                          download_cancel("No response");
                          return;
                  }
  
                  break;
  
  
  
              case ERASE_SECTOR:
                  switch (event) {
                      case BEGIN:
                          printf("erase... ");
                          fflush(stdout);
                          snprintf(buf, sizeof(buf), "E %d %d\r\n", sector, sector);
                          xmit_cmd(buf, 25);
                          return;
  
                      case RESPONSE:
                          if (num_lines(parsed_response_buf) < 1) {
                              return;
                          }
  
                          if (strcmp(parsed_response_buf, "0\r\n") == 0) {
                              printf("Ok\r\n");
                              sector++;
                              state = BLANK_CHECK_SECTOR;
                              event = BEGIN;
                              break;
                          }
                          else {
                              printf("Error\r\n");
                              download_cancel("Unable to erase flash");
                              return;
                          }
  
                      case TIMEOUT:
                          download_cancel("No response");
                          return;
                  }
  
                  break;
  
  
  
              case DOWNLOAD_CODE:
                  switch (event) {
                      case BEGIN:
                          if (sector >= chip->num_sector) {
                              state = BOOT_HARD;
                              sector = 0;
                              event = BEGIN;
                              break;
                          }
  
                          printf("  Sector %2d (0x%08X-0x%08X): ", sector,
                                 chip->layout[sector].address + sector_offset,
                                 chip->layout[sector].address + sector_offset + chip->chunk_size - 1);
                          fflush(stdout);
  
                          if (!bytes_within_range(chip->layout[sector].address + sector_offset,
                                                  chip->layout[sector].address + sector_offset + chip->chunk_size - 1)) {
                              printf("not used\r\n");
                              sector_offset += chip->chunk_size;
  
                              if (sector_offset >= chip->layout[sector].size) {
                                  sector_offset = 0;
                                  sector++;
                              }
  
                              break;
                          }
  
                          snprintf(buf, sizeof(buf), "W %u %d\r\n", chip->ram_addr, chip->chunk_size);
                          xmit_cmd(buf, 4);
                          return;
  
                      case RESPONSE:
                          if (num_lines(parsed_response_buf) != 1) {
                              return;
                          }
  
                          if (strcmp(parsed_response_buf, "0\r\n") == 0) {
                              state = XMIT_DATA;
                              printf("xmit");
                              current_addr = chip->layout[sector].address + sector_offset;
                              num_to_xmit = chip->chunk_size;
                              linecount = 0;
                              cksum = 0;
                              event = BEGIN;
                              break;
                          }
                          else {
                              download_cancel("can't xmit to ram");
                              return;
                          }
  
                      case TIMEOUT:
                          download_cancel("No response");
                          return;
                  }
  
                  break;
  
  
              case XMIT_DATA:
                  switch (event) {
                      case BEGIN:
                          n = num_to_xmit;
  
                          if (n > 45) {
                              n = 45;
                          }
  
                          get_ihex_data(current_addr, n, bytes);
                          cksum += sum(bytes, n);
                          uuencode(buf, bytes, n);
                          current_addr += n;
                          num_to_xmit -= n;
                          linecount++;
                          xmit_cmd(buf, 5);
                          write_serial_port("\r\n", 2);
                          return;
  
                      case RESPONSE:
                          if (num_lines(parsed_response_buf) != 1) {
                              return;
                          }
  
                          if (strcmp(parsed_response_buf, "\r\n") == 0) {
                              if (linecount >= 20 || num_to_xmit <= 0) {
                                  state = XMIT_CKSUM;
                              }
  
                              event = BEGIN;
                              break;
                          }
                          else {
                              download_cancel("data xmit did not echo");
                              return;
                          }
  
                      case TIMEOUT:
                          download_cancel("No response");
                          return;
                  }
  
                  break;
  
  
              case XMIT_CKSUM:
                  switch (event) {
                      case BEGIN:
                          snprintf(buf, sizeof(buf), "%u\r\n", cksum);
                          xmit_cmd(buf, 3);
                          return;
  
                      case RESPONSE:
                          if (num_lines(parsed_response_buf) != 1) {
                              return;
                          }
  
                          if (strcmp(parsed_response_buf, "OK\r\n") == 0) {
                              if (num_to_xmit > 0) {
                                  printf(".");
                                  fflush(stdout);
                                  state = XMIT_DATA;
                                  event = BEGIN;
                                  linecount = 0;
                                  cksum = 0;
                                  break;
                              }
  
                              state = WRITE_PREPARE;
                              event = BEGIN;
                              break;
                          }
                          else {
                              download_cancel("bad checksum");
                              return;
                          }
  
                      case TIMEOUT:
                          download_cancel("No response");
                          return;
                  }
  
                  break;
  
  
              case WRITE_PREPARE:
                  switch (event) {
                      case BEGIN:
                          printf("prep, ");
                          fflush(stdout);
                          snprintf(buf, sizeof(buf), "P %d %d\r\n", sector, sector);
                          xmit_cmd(buf, 5);
                          return;
  
                      case RESPONSE:
                          if (num_lines(parsed_response_buf) != 1) {
                              return;
                          }
  
                          if (strcmp(parsed_response_buf, "0\r\n") == 0) {
                              state = WRITE_SECTOR;
                              event = BEGIN;
                              break;
                          }
                          else {
                              download_cancel("Unable to prep for write");
                              return;
                          }
  
                      case TIMEOUT:
                          download_cancel("No response");
                          return;
                  }
  
                  break;
  
  
              case WRITE_SECTOR:
                  switch (event) {
                      case BEGIN:
                          printf("write, ");
                          fflush(stdout);
                          snprintf(buf, sizeof(buf), "C %d %u %d\r\n",
                                   chip->layout[sector].address + sector_offset,
                                   chip->ram_addr, chip->chunk_size);
                          xmit_cmd(buf, 5);
                          return;
  
                      case RESPONSE:
                          if (num_lines(parsed_response_buf) != 1) {
                              return;
                          }
  
                          if (strcmp(parsed_response_buf, "0\r\n") == 0) {
                              printf("Ok\r\n");
                              sector_offset += chip->chunk_size;
  
                              if (sector_offset >= chip->layout[sector].size) {
                                  sector_offset = 0;
                                  sector++;
                              }
  
                              state = DOWNLOAD_CODE;
                              event = BEGIN;
                          }
                          else {
                              download_cancel("Unable to prep for write");
                              return;
                          }
  
                          break;
  
                      case TIMEOUT:
                          download_cancel("No response");
                          return;
                  }
  
                  break;
  
  
              case BOOT_HARD:
                  //  if (chip->bootprog) {
                  //      state = BOOT_SOFT;
                  //      break;
                  //  }
                  //  else {
                  printf("Booting (hardware reset)...\r\n\r\n");
                  hard_reset_to_user_code();
                  done_program(0);
                  return;
                  //  }
  
              case BOOT_SOFT:
                  switch (event) {
                      case BEGIN:
                          printf("Booting (soft jump)...\r\n");
                          printf("loading jump code\r\n");
                          // would be nice if we could simply jump to the user's code, but
                          // Philips didn't think of that.  The interrupt vector table stays
                          // mapped to the bootloader, so jumping to zero only runs the
                          // bootloader again.  Intead, we need to download a tiny ARM
                          // program that reconfigures the hardware and then jumps to zero.
                          //snprintf(buf, sizeof(buf), "G %d A\r\n", 0);
                          snprintf(buf, sizeof(buf), "W %u %d\r\n", chip->ram_addr, chip->bootprog[0] * 4);
                          xmit_cmd(buf, 4);
                          return;
  
                      case RESPONSE:
                          if (num_lines(parsed_response_buf) < 1) {
                              return;
                          }
  
                          if (strcmp(parsed_response_buf, "0\r\n") == 0) {
                              current_addr = 0;
                              num_to_xmit = chip->bootprog[0] * 4;
                              copy_boot_code_to_memory(chip);
                              linecount = 0;
                              cksum = 0;
                              state = BOOT_XMIT_DATA;
                              event = BEGIN;
                          }
                          else {
                              download_cancel("can't xmit to ram");
                              return;
                          }
  
                          break;
  
                      case TIMEOUT:
                          download_cancel("No response");
                          return;
                  }
  
                  break;
  
  
              case BOOT_XMIT_DATA:
                  switch (event) {
                      case BEGIN:
                          n = num_to_xmit;
  
                          if (n > 45) {
                              n = 45;
                          }
  
                          get_ihex_data(current_addr, n, bytes);
                          cksum += sum(bytes, n);
                          uuencode(buf, bytes, n);
                          current_addr += n;
                          num_to_xmit -= n;
                          linecount++;
                          //printf("send: %s\r\n", buf);
                          xmit_cmd(buf, 5);
                          write_serial_port("\r\n", 2);
                          return;
  
                      case RESPONSE:
                          if (num_lines(parsed_response_buf) != 1) {
                              return;
                          }
  
                          if (strcmp(parsed_response_buf, "\r\n") == 0) {
                              if (linecount >= 20 || num_to_xmit <= 0) {
                                  state = BOOT_XMIT_CKSUM;
                              }
  
                              event = BEGIN;
                              break;
                          }
                          else {
                              download_cancel("data xmit did not echo");
                              return;
                          }
  
                      case TIMEOUT:
                          download_cancel("No response");
                          return;
                  }
  
                  break;
  
  
              case BOOT_XMIT_CKSUM:
                  switch (event) {
                      case BEGIN:
                          snprintf(buf, sizeof(buf), "%u\r\n", cksum);
                          //printf("send: %s", buf);
                          xmit_cmd(buf, 3);
                          return;
  
                      case RESPONSE:
                          if (num_lines(parsed_response_buf) != 1) {
                              return;
                          }
  
                          if (strcmp(parsed_response_buf, "OK\r\n") == 0) {
                              if (num_to_xmit > 0) {
                                  printf(".");
                                  fflush(stdout);
                                  state = BOOT_XMIT_DATA;
                                  event = BEGIN;
                                  linecount = 0;
                                  cksum = 0;
                                  break;
                              }
  
                              state = BOOT_RUN_CODE;
                              event = BEGIN;
                              break;
                          }
                          else {
                              download_cancel("bad checksum");
                              return;
                          }
  
                      case TIMEOUT:
                          download_cancel("No response");
                          return;
                  }
  
                  break;
  
  
              case BOOT_RUN_CODE:
                  switch (event) {
                      case BEGIN:
                          printf("jumping now!\r\n");
                          snprintf(buf, sizeof(buf), "G %u A\r\n", chip->ram_addr);
                          xmit_cmd(buf, 4);
                          return;
  
                      case RESPONSE:
                          if (num_lines(parsed_response_buf) < 1) {
                              return;
                          }
  
                          if (strcmp(parsed_response_buf, "0\r\n") == 0) {
                              done_program(0);
                              return;
                          }
                          else {
                              printf("response = %s", parsed_response_buf);
                              download_cancel("couldn't run program");
                              return;
                          }
  
                          break;
  
                      case TIMEOUT:
                          done_program(0);
                          return;
                          // Philips user name says it responds, but it does not.
                          // It seems to just immediately jump to the code without
                          // any "0" response.
                          //download_cancel("No response"); return;
                  }
  
                  break;
  
  
  
              default:
                  snprintf(buf, sizeof(buf), "unknown state %d\r\n", state);
                  download_cancel(buf);
                  return;
          }
      }
  }
  
  
  void download_cancel(const char *mesg)
  {
      printf("\r\nDownload Canceled");
  
      if (mesg && *mesg) {
          printf(": %s", mesg);
      }
  
      printf("\r\n");
      // need to do some cleanup for various states???
      done_program(1);
  }
  
  
  /****************************************************************/
  /*                              */
  /*      Transmit Commands to Bootloader         */
  /*                              */
  /****************************************************************/
  
  
  
  static void xmit_cmd(const char *cmd, int max_time)
  {
      int len;
  
      if (cmd == NULL || *cmd == '\0') {
          return;
      }
  
      len = strlen(cmd);
  
  #ifdef PRINT_TX_RX_BYTES
      printf("tx %d bytes: %s\n", len, cmd);
  #endif
  
      input_flush_serial_port();
  
      write_serial_port(cmd, len);
  
      snprintf(expected_echo_buf, sizeof(expected_echo_buf), "%s", cmd);
  
      if (state == SYNC_1) {
          // special case, baud sync doesn't echo
          expected_echo_buf[0] = '\0';
      }
  
      expected_echo_ptr = expected_echo_buf;
      parsed_response_ptr = parsed_response_buf;
  
      response_timer = max_time;
  }
  
  
  
  /****************************************************************/
  /*                              */
  /*      Handlers that respond to input          */
  /*                              */
  /****************************************************************/
  
  
  /*
  Whenever the main gtk event loop detects more input has arrived from the
  serial port, and we're in the process of a download, it calls here to
  hand off the data.  We're supposed to match it up to the echo buffer,
  and then store it into the parsed response buffer and if it looks like
  this might be a complete response, call download_main with a response
  event.
  */
  void download_rx_port(const unsigned char *buf, int num)
  {
      int i = 0;
  
      if (num <= 0) {
          return;
      }
  
      // echo the data
      //write(term_fd, buf, num);
  
  #ifdef PRINT_TX_RX_BYTES
      printf("rx %d bytes:", num);
  
      for (i = 0; i < num; i++) {
          printf(" %02X", *(buf + i));
      }
  
      printf("\r\n");
  #endif
  
      // ignore extra incoming garbage we didn't expect
      if (expected_echo_ptr == NULL) {
          return;
      }
  
      // special case, echo of '?' during unsuccessful sync
      if (state == SYNC_1 && num == 1 && buf[0] == '?') {
          *parsed_response_ptr++ = '?';
          *parsed_response_ptr = '\0';
          response_timer = 0;
          download_main(RESPONSE);
          return;
      }
  
      // parse it
      for (i = 0; i < num; i++) {
          // if we're still expecting the echo, gobble it up
          if (*expected_echo_ptr) {
              if (buf[i] != *expected_echo_ptr) {
  #ifdef PRINT_TX_RX_BYTES
                  printf("  <echo_err>  ");
  #endif
                  // ignore incorrect echo (will timeout)
                  expected_echo_ptr = NULL;
                  return;
              }
  
              expected_echo_ptr++;
              continue;
          }
  
          // store this into a parsed response buffer
          *parsed_response_ptr++ = buf[i];
      }
  
      // if the last two characters of the response are "\r\n",
      // then it's likely we've got a complete response.
      *parsed_response_ptr = '\0';
  
      if (parsed_response_ptr > parsed_response_buf + 1
          && *(parsed_response_ptr - 2) == '\r'
          && *(parsed_response_ptr - 1) == '\n') {
          //response_timer = 0;
          download_main(RESPONSE);
      }
  }
  
  
  
  /*
  During a download, this is supposed to get called at 100 Hz.  Whenever
  something is transmitted and we expect a response, the response_timer
  is initialized to the maximum time we will wait.
  */
  void download_timer(void)
  {
      if (response_timer > 0) {
          response_timer--;
  
          if (response_timer == 0) {
              expected_echo_ptr = NULL;
              download_main(TIMEOUT);
          }
      }
  }
  
  /*
  During a download, all input the user types into the terminal is sent
  to this function, instead of passing it to xterm for display
  */
  void download_rx_term(const unsigned char *buf, int num)
  {
      // discard anything the user types into the terminal
      // while we are in the middle of downloading.  Maybe
      // we should look for CTRL-C and abort??
  }