annotate loadtools/compalload.c @ 768:461aad16a816

ss_csf.c compiles
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sun, 12 Oct 2014 00:41:46 +0000
parents 7b3fd0e2a352
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
379
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
1 /*
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
2 * This module implements Compal's serial code loading protocol to load
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
3 * what we call compalstage, a piece of code that re-enables the Calypso
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
4 * boot ROM and allows us to use the same loadagent which we use on
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
5 * freedom-enabled target devices.
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
6 */
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
7
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
8 #include <sys/param.h>
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
9 #include <sys/file.h>
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
10 #include <sys/stat.h>
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
11 #include <sys/ioctl.h>
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
12 #include <stdio.h>
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
13 #include <stdlib.h>
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
14 #include <string.h>
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
15 #include <strings.h>
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
16 #include <termios.h>
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
17 #include <unistd.h>
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
18
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
19 extern char default_helpers_dir[];
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
20 extern char *target_ttydev;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
21 extern int target_fd;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
22 extern struct termios target_termios;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
23
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
24 static char compalstage_pathname[MAXPATHLEN];
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
25 static u_char *compalstage_imgbuf;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
26 static size_t compalstage_len, compalstage_totlen;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
27 static int rx_state;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
28 static u_char rx_msg[3];
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
29
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
30 void
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
31 set_compalstage_short(arg)
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
32 char *arg;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
33 {
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
34 if (strcmp(arg, "none"))
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
35 sprintf(compalstage_pathname, "%s/compalstage-%s.bin",
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
36 default_helpers_dir, arg);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
37 else
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
38 compalstage_pathname[0] = 0;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
39 }
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
40
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
41 void
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
42 set_compalstage_fullpath(arg)
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
43 char *arg;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
44 {
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
45 strcpy(compalstage_pathname, arg);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
46 }
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
47
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
48 static void
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
49 compute_checksum()
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
50 {
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
51 size_t i, l;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
52 u_char ck;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
53
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
54 ck = 0x02;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
55 l = compalstage_len + 3;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
56 for (i = 1; i < l; i++)
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
57 ck ^= compalstage_imgbuf[i];
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
58 compalstage_imgbuf[l] = ck;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
59 }
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
60
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
61 static
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
62 handle_rx_msg()
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
63 {
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
64 static u_char download_cmd[7] = {0x1B, 0xF6, 0x02, 0x00,
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
65 0x52, 0x01, 0x53};
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
66
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
67 if (rx_msg[0] == 0x41 && rx_msg[1] == 0x01 && rx_msg[2] == 0x40) {
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
68 printf("Received PROMPT1, sending download command\n");
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
69 write(target_fd, download_cmd, 7);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
70 return(0);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
71 } else if (rx_msg[0] == 0x41 && rx_msg[1] == 0x02 && rx_msg[2] == 0x43){
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
72 printf("Received PROMPT2, sending download image\n");
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
73 write(target_fd, compalstage_imgbuf, compalstage_totlen);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
74 return(0);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
75 } else if (rx_msg[0] == 0x41 && rx_msg[1] == 0x03 && rx_msg[2] == 0x42){
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
76 printf("Received ACK; downloaded image should now be running!\n");
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
77 return(1);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
78 } else if (rx_msg[0] == 0x45 && rx_msg[1] == 0x53 && rx_msg[2] == 0x16){
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
79 printf("Bootloader indicates bad checksum :-(\n");
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
80 return(0);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
81 } else if (rx_msg[0] == 0x41 && rx_msg[1] == 0x03 && rx_msg[2] == 0x57){
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
82 printf("Bootloader indicates bad magic :-(\n");
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
83 return(0);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
84 } else {
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
85 printf("Unknown msg from bootloader: 1B F6 02 00 %02X %02X %02X\n",
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
86 rx_msg[0], rx_msg[1], rx_msg[2]);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
87 return(0);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
88 }
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
89 }
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
90
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
91 static
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
92 handle_rx_byte(rxb)
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
93 {
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
94 switch (rx_state) {
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
95 case 0:
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
96 if (rxb == 0x1B)
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
97 rx_state = 1;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
98 return(0);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
99 case 1:
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
100 if (rxb == 0xF6)
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
101 rx_state = 2;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
102 else if (rxb == 0x1B)
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
103 rx_state = 1;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
104 else
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
105 rx_state = 0;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
106 return(0);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
107 case 2:
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
108 if (rxb == 0x02)
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
109 rx_state = 3;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
110 else if (rxb == 0x1B)
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
111 rx_state = 1;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
112 else
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
113 rx_state = 0;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
114 return(0);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
115 case 3:
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
116 if (rxb == 0x00)
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
117 rx_state = 4;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
118 else if (rxb == 0x1B)
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
119 rx_state = 1;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
120 else
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
121 rx_state = 0;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
122 return(0);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
123 case 4:
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
124 rx_msg[0] = rxb;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
125 rx_state = 5;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
126 return(0);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
127 case 5:
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
128 rx_msg[1] = rxb;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
129 rx_state = 6;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
130 return(0);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
131 case 6:
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
132 rx_msg[2] = rxb;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
133 rx_state = 0;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
134 return handle_rx_msg();
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
135 }
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
136 }
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
137
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
138 static void
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
139 read_loop()
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
140 {
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
141 u_char rdbuf[16];
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
142 int cc, i;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
143
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
144 for (;;) {
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
145 cc = read(target_fd, rdbuf, sizeof rdbuf);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
146 if (cc <= 0) {
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
147 fprintf(stderr, "EOF/error on target tty\n");
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
148 exit(1);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
149 }
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
150 for (i = 0; i < cc; i++)
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
151 if (handle_rx_byte(rdbuf[i]))
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
152 return;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
153 }
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
154 }
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
155
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
156 perform_compal_stage(for_boot_rom)
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
157 {
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
158 int fd;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
159 struct stat st;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
160 static int zero = 0;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
161
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
162 if (!compalstage_pathname[0])
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
163 return(0);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
164 fd = open(compalstage_pathname, O_RDONLY);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
165 if (fd < 0) {
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
166 perror(compalstage_pathname);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
167 exit(1);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
168 }
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
169 fstat(fd, &st);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
170 if (!S_ISREG(st.st_mode)) {
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
171 fprintf(stderr, "error: %s is not a regular file\n",
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
172 compalstage_pathname);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
173 exit(1);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
174 }
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
175 if (st.st_size > 65535) {
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
176 fprintf(stderr,
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
177 "error: %s exceed Compal download limit of 65535 bytes\n",
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
178 compalstage_pathname);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
179 exit(1);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
180 }
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
181 compalstage_len = st.st_size;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
182 compalstage_totlen = compalstage_len + 4;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
183 compalstage_imgbuf = malloc(compalstage_totlen);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
184 if (!compalstage_imgbuf) {
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
185 perror("malloc");
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
186 exit(1);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
187 }
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
188 compalstage_imgbuf[0] = 0x02;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
189 compalstage_imgbuf[1] = compalstage_len >> 8;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
190 compalstage_imgbuf[2] = compalstage_len;
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
191 if (read(fd, compalstage_imgbuf+3, compalstage_len) != compalstage_len){
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
192 fprintf(stderr, "%s: read error or short read\n",
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
193 compalstage_pathname);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
194 exit(1);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
195 }
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
196 close(fd);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
197 compute_checksum();
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
198
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
199 printf("Using Compal stage image %s\n", compalstage_pathname);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
200 cfsetispeed(&target_termios, B115200);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
201 cfsetospeed(&target_termios, B115200);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
202 if (tcsetattr(target_fd, TCSAFLUSH, &target_termios) < 0) {
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
203 perror("tcsetattr to switch baud rate");
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
204 exit(1);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
205 }
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
206 ioctl(target_fd, FIONBIO, &zero);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
207 printf("Waiting for PROMPT1 from target (%s) at 115200 baud\n",
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
208 target_ttydev);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
209 read_loop();
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
210 free(compalstage_imgbuf);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
211
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
212 if (for_boot_rom) {
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
213 cfsetispeed(&target_termios, B19200);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
214 cfsetospeed(&target_termios, B19200);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
215 if (tcsetattr(target_fd, TCSAFLUSH, &target_termios) < 0) {
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
216 perror("tcsetattr to switch baud rate");
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
217 exit(1);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
218 }
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
219 }
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
220
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
221 return(1);
7b3fd0e2a352 loadtools: compalstage support implemented in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
222 }