annotate loadtools/romload.c @ 10:e2e80a09338e

fc-sertool actually works; output from loadagent is visible!
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Wed, 01 May 2013 03:41:42 +0000
parents fea204bc7674
children 24b88c119465
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
1 /*
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
2 * This module implements the communication protocol for pushing our
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
3 * IRAM-loadable code to the Calypso ROM bootloader.
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
4 */
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
5
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
6 #include <sys/types.h>
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
7 #include <sys/ioctl.h>
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
8 #include <sys/time.h>
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
9 #include <sys/errno.h>
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
10 #include <stdint.h>
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
11 #include <stdio.h>
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
12 #include <stdlib.h>
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
13 #include <strings.h>
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
14 #include <termios.h>
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
15 #include <unistd.h>
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
16 #include "srecreader.h"
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
17
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
18 extern int errno;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
19
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
20 extern char *target_ttydev;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
21 extern int target_fd;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
22
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
23 struct srecreader iramimage;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
24
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
25 static int beacon_interval = 13; /* in milliseconds */
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
26
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
27 static u_char beacon_cmd[2] = {'<', 'i'};
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
28
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
29 static u_char param_cmd[11] = {'<', 'p',
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
30 0x00, /* baud rate select code (115200) */
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
31 0x00, /* DPLL setup: leave it off like on power-up, */
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
32 /* OsmocomBB does the same thing */
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
33 0x00, 0x04, /* chip select timing (WS) settings */
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
34 /* our setting matches both OsmocomBB */
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
35 /* and what the ROM runs with */
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
36 /* before receiving this command */
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
37 0x22, /* FFFF:F900 register config, low byte */
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
38 /* OsmocomBB sends 0x00 here, but I've chosen */
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
39 /* 0x22 to match the setting of this register */
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
40 /* used by the boot ROM before this command. */
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
41 0x00, 0x01, 0xD4, 0xC0 /* UART timeout */
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
42 /* I've chosen the same value as what the */
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
43 /* boot ROM runs with before getting this cmd */
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
44 };
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
45
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
46 static u_char write_cmd[10] = {'<', 'w', 0x01, 0x01, 0x00};
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
47 static u_char cksum_cmd[3] = {'<', 'c'};
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
48 static u_char branch_cmd[6] = {'<', 'b'};
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
49
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
50 #define INTERMEDIATE_TIMEOUT 500 /* ms to wait for responses */
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
51 #define SERIAL_FLUSH_DELAY 200 /* also in ms */
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
52
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
53 /*
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
54 * The following function should be called by command line option
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
55 * parsers upon encountering the -i option.
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
56 */
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
57 set_beacon_interval(arg)
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
58 char *arg;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
59 {
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
60 int i;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
61
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
62 i = atoi(arg);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
63 if (i < 2 || i > 500) {
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
64 fprintf(stderr, "invalid -i argument specified\n");
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
65 exit(1);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
66 }
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
67 beacon_interval = i;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
68 }
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
69
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
70 static int
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
71 expect_response(timeout)
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
72 {
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
73 char buf[2];
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
74 fd_set fds;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
75 struct timeval tv;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
76 int pass, cc;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
77
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
78 for (pass = 0; pass < 2; ) {
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
79 FD_ZERO(&fds);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
80 FD_SET(target_fd, &fds);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
81 tv.tv_sec = 0;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
82 tv.tv_usec = timeout * 1000;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
83 cc = select(target_fd+1, &fds, NULL, NULL, &tv);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
84 if (cc < 0) {
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
85 if (errno == EINTR)
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
86 continue;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
87 perror("select");
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
88 exit(1);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
89 }
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
90 if (cc < 1)
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
91 return(-1);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
92 cc = read(target_fd, buf + pass, 2 - pass);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
93 if (cc <= 0) {
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
94 perror("read after successful select");
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
95 exit(1);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
96 }
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
97 if (pass == 0 && buf[0] != '>')
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
98 continue;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
99 pass += cc;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
100 }
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
101 return(buf[1]);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
102 }
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
103
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
104 static
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
105 send_beacons()
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
106 {
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
107 printf("Sending beacons to %s\n", target_ttydev);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
108 do
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
109 write(target_fd, beacon_cmd, sizeof beacon_cmd);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
110 while (expect_response(beacon_interval) != 'i');
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
111 return 0;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
112 }
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
113
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
114 static uint32_t
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
115 compute_block_cksum()
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
116 {
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
117 uint32_t sum;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
118 int i, llen;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
119
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
120 sum = iramimage.datalen + 5;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
121 llen = iramimage.datalen + 4;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
122 for (i = 0; i < llen; i++)
10
e2e80a09338e fc-sertool actually works; output from loadagent is visible!
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 9
diff changeset
123 sum += iramimage.record[i+1];
9
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
124 return sum;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
125 }
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
126
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
127 perform_romload()
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
128 {
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
129 int resp;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
130 uint16_t image_cksum;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
131 unsigned long rec_count;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
132 static int zero = 0;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
133
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
134 if (open_srec_file(&iramimage) < 0)
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
135 exit(1);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
136 ioctl(target_fd, FIONBIO, &zero);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
137 send_beacons();
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
138 printf("Got beacon response, attempting download\n");
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
139
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
140 usleep(SERIAL_FLUSH_DELAY * 1000);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
141 tcflush(target_fd, TCIFLUSH);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
142 write(target_fd, param_cmd, sizeof param_cmd);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
143 resp = expect_response(INTERMEDIATE_TIMEOUT);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
144 if (resp != 'p') {
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
145 if (resp < 0)
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
146 fprintf(stderr, "No response to <p command\n");
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
147 else if (isprint(resp))
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
148 fprintf(stderr,
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
149 "Got >%c in response to <p command; expected >p\n",
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
150 resp);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
151 else
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
152 fprintf(stderr,
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
153 "Got > %02X in response to <p command; expected >p\n",
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
154 resp);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
155 exit(1);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
156 }
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
157 printf("<p command successful, switching to 115200 baud\n");
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
158 switch_baud_rate(B115200);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
159 usleep(SERIAL_FLUSH_DELAY * 1000);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
160 tcflush(target_fd, TCIFLUSH);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
161
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
162 image_cksum = 0;
10
e2e80a09338e fc-sertool actually works; output from loadagent is visible!
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 9
diff changeset
163 for (rec_count = 0; ; ) {
9
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
164 if (read_s_record(&iramimage) < 0)
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
165 exit(1);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
166 switch (iramimage.record_type) {
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
167 case '0':
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
168 if (iramimage.lineno == 1)
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
169 continue;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
170 fprintf(stderr,
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
171 "%s: S0 record found in line %d (expected in line 1 only)\n",
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
172 iramimage.filename, iramimage.lineno);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
173 exit(1);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
174 case '3':
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
175 case '7':
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
176 if (s3s7_get_addr_data(&iramimage) < 0)
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
177 exit(1);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
178 break;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
179 default:
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
180 fprintf(stderr,
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
181 "%s line %d: S%c record type not supported\n",
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
182 iramimage.filename, iramimage.lineno,
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
183 iramimage.record_type);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
184 exit(1);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
185 }
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
186 if (iramimage.record_type == '7')
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
187 break;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
188 /* must be S3 */
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
189 if (iramimage.datalen < 1) {
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
190 fprintf(stderr,
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
191 "%s line %d: S3 record has zero data length\n",
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
192 iramimage.filename, iramimage.lineno);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
193 exit(1);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
194 }
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
195 /* form <w command */
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
196 if (!rec_count)
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
197 printf("Sending image payload\n");
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
198 write_cmd[5] = iramimage.datalen;
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
199 bcopy(iramimage.record + 1, write_cmd + 6, 4);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
200 write(target_fd, write_cmd, sizeof write_cmd);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
201 write(target_fd, iramimage.record + 5, iramimage.datalen);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
202 /* update our checksum accumulator */
10
e2e80a09338e fc-sertool actually works; output from loadagent is visible!
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 9
diff changeset
203 image_cksum += ~compute_block_cksum() & 0xFF;
9
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
204 /* collect response */
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
205 resp = expect_response(INTERMEDIATE_TIMEOUT);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
206 if (resp != 'w') {
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
207 fprintf(stderr, "Block #%lu: ", rec_count);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
208 if (resp < 0)
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
209 fprintf(stderr, "No response to <w command\n");
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
210 else if (isprint(resp))
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
211 fprintf(stderr,
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
212 "Got >%c in response to <w command; expected >w\n",
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
213 resp);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
214 else
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
215 fprintf(stderr,
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
216 "Got > %02X in response to <w command; expected >w\n",
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
217 resp);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
218 exit(1);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
219 }
10
e2e80a09338e fc-sertool actually works; output from loadagent is visible!
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 9
diff changeset
220 putchar('.');
e2e80a09338e fc-sertool actually works; output from loadagent is visible!
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 9
diff changeset
221 fflush(stdout);
e2e80a09338e fc-sertool actually works; output from loadagent is visible!
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 9
diff changeset
222 rec_count++;
9
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
223 }
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
224 /* got S7 */
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
225 fclose(iramimage.openfile);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
226 if (!rec_count) {
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
227 fprintf(stderr,
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
228 "%s line %d: S7 without any preceding S3 data records\n",
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
229 iramimage.filename, iramimage.lineno);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
230 exit(1);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
231 }
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
232
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
233 /* send <c */
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
234 printf("Sending checksum\n");
10
e2e80a09338e fc-sertool actually works; output from loadagent is visible!
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 9
diff changeset
235 cksum_cmd[2] = ~image_cksum & 0xFF;
9
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
236 write(target_fd, cksum_cmd, sizeof cksum_cmd);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
237 resp = expect_response(INTERMEDIATE_TIMEOUT);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
238 if (resp != 'c') {
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
239 if (resp < 0)
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
240 fprintf(stderr, "No response to <c command\n");
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
241 else if (isprint(resp))
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
242 fprintf(stderr,
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
243 "Got >%c in response to <c command; expected >c\n",
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
244 resp);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
245 else
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
246 fprintf(stderr,
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
247 "Got > %02X in response to <c command; expected >c\n",
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
248 resp);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
249 exit(1);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
250 }
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
251 printf("<c command successful, sending <b\n");
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
252
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
253 bcopy(iramimage.record + 1, branch_cmd + 2, 4);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
254 write(target_fd, branch_cmd, sizeof branch_cmd);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
255 resp = expect_response(INTERMEDIATE_TIMEOUT);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
256 if (resp != 'b') {
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
257 if (resp < 0)
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
258 fprintf(stderr, "No response to <b command\n");
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
259 else if (isprint(resp))
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
260 fprintf(stderr,
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
261 "Got >%c in response to <b command; expected >b\n",
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
262 resp);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
263 else
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
264 fprintf(stderr,
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
265 "Got > %02X in response to <b command; expected >b\n",
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
266 resp);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
267 exit(1);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
268 }
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
269 printf("<b command successful: downloaded image should now be running!\n");
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
270 return(0);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
271 }