annotate frbl/test/frbl2.c @ 327:b5b8df53aa51

frbl/reconst/command.c: missing break in switch case
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 06 Mar 2020 04:10:21 +0000
parents 7df08926a4e7
children 10d62a3bfeec
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
323
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 #include <sys/types.h>
324
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
2 #include <sys/ioctl.h>
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
3 #include <sys/time.h>
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
4 #include <sys/errno.h>
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
5 #include <ctype.h>
323
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <stdint.h>
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdio.h>
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdlib.h>
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <string.h>
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <strings.h>
324
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
11 #include <termios.h>
323
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <unistd.h>
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include "srecreader.h"
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14
324
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
15 extern int errno;
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
16
323
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 extern char *target_ttydev;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 extern int target_fd;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 extern struct srecreader srimage;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 #define MAX_IMAGE_LEN 32768
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 static u_char codeimage[MAX_IMAGE_LEN];
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 static unsigned codeimage_len;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 static uint32_t loadaddr;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26
324
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
27 static u_char beacon_cmd[3] = {0xAA, 0x01, 0xDD};
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
28 static u_char fluid_baudrate_cmd[2] = {0x00, 0x07};
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
29 static u_char fluid_version_cmd[1] = {'V'};
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
30 static u_char fluid_chipquery_cmd[2] = {'Q', 'C'};
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
31 static u_char fluid_download_cmd[7] = {'L'};
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
32
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
33 #define BEACON_INTERVAL 13 /* ms between beacons */
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
34 #define INTERMEDIATE_TIMEOUT 500 /* ms to wait for responses */
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
35 #define SERIAL_FLUSH_DELAY 200 /* also in ms */
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
36
323
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 read_srec_image()
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 u_char *writep;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 uint32_t endaddr;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 int i;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 if (open_srec_file(&srimage) < 0)
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 exit(1);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 for (;;) {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 if (read_s_record(&srimage) < 0)
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 exit(1);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 switch (srimage.record_type) {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 case '0':
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 if (srimage.lineno == 1)
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 continue;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 fprintf(stderr,
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 "%s: S0 record found in line %d (expected in line 1 only)\n",
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 srimage.filename, srimage.lineno);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 exit(1);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 case '3':
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 case '7':
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 if (s3s7_get_addr_data(&srimage) < 0)
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 exit(1);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 break;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 default:
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 fprintf(stderr,
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 "%s line %d: S%c record type not supported\n",
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 srimage.filename, srimage.lineno,
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 srimage.record_type);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 exit(1);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 if (srimage.record_type == '7')
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 break;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 /* must be S3 */
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 if (srimage.datalen < 1) {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 fprintf(stderr,
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 "%s line %d: S3 record has zero data length\n",
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 srimage.filename, srimage.lineno);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 exit(1);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 if (srimage.datalen & 1) {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 fprintf(stderr,
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 "%s line %d: S3 record has odd data length\n",
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 srimage.filename, srimage.lineno);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 exit(1);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 if (srimage.addr & 1) {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 fprintf(stderr,
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 "%s line %d: S3 record has odd address\n",
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 srimage.filename, srimage.lineno);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 exit(1);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 /* handle first record */
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 if (!codeimage_len) {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 endaddr = loadaddr = srimage.addr;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 writep = codeimage;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 if (srimage.addr != endaddr) {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 fprintf(stderr, "%s line %d: address discontinuity\n",
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 srimage.filename, srimage.lineno);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 exit(1);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 if (codeimage_len + srimage.datalen > MAX_IMAGE_LEN) {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 fprintf(stderr,
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 "%s line %d: max image length exceeded\n",
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 srimage.filename, srimage.lineno);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 exit(1);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 /* reverse byte order */
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 for (i = 0; i < srimage.datalen; i += 2) {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 *writep++ = srimage.record[i+6];
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 *writep++ = srimage.record[i+5];
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 endaddr += srimage.datalen;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 codeimage_len += srimage.datalen;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 /* got S7 */
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 fclose(srimage.openfile);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 if (!codeimage_len) {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 fprintf(stderr,
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 "%s line %d: S7 without any preceding S3 data records\n",
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 srimage.filename, srimage.lineno);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 exit(1);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 if (srimage.addr != loadaddr) {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
122 fprintf(stderr,
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 "%s line %d: S7 address differs from image load address\n",
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124 srimage.filename, srimage.lineno);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
125 exit(1);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
126 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
127 /* all good */
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
128 return(0);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
129 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
130
324
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
131 static void
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
132 fill_download_cmd()
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
133 {
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
134 unsigned nwords;
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
135
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
136 nwords = codeimage_len >> 1;
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
137 fluid_download_cmd[1] = loadaddr >> 24;
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
138 fluid_download_cmd[2] = loadaddr >> 16;
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
139 fluid_download_cmd[3] = loadaddr >> 8;
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
140 fluid_download_cmd[4] = loadaddr;
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
141 fluid_download_cmd[5] = nwords >> 8;
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
142 fluid_download_cmd[6] = nwords;
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
143 }
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
144
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
145 static int
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
146 expect_beacon_response(timeout)
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
147 {
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
148 u_char buf;
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
149 fd_set fds;
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
150 struct timeval tv;
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
151 int cc;
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
152
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
153 for (;;) {
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
154 FD_ZERO(&fds);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
155 FD_SET(target_fd, &fds);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
156 tv.tv_sec = 0;
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
157 tv.tv_usec = timeout * 1000;
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
158 cc = select(target_fd+1, &fds, NULL, NULL, &tv);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
159 if (cc < 0) {
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
160 if (errno == EINTR)
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
161 continue;
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
162 perror("select");
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
163 exit(1);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
164 }
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
165 if (cc < 1)
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
166 return(0);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
167 cc = read(target_fd, &buf, 1);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
168 if (cc <= 0) {
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
169 perror("read after successful select");
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
170 exit(1);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
171 }
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
172 if (buf == 'H')
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
173 return(1);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
174 }
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
175 }
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
176
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
177 static
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
178 send_beacons()
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
179 {
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
180 printf("Sending beacons to %s\n", target_ttydev);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
181 do
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
182 write(target_fd, beacon_cmd, sizeof beacon_cmd);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
183 while (!expect_beacon_response(BEACON_INTERVAL));
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
184 return 0;
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
185 }
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
186
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
187 static int
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
188 expect_add_response(buf, expect_bytes, timeout)
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
189 u_char *buf;
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
190 {
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
191 fd_set fds;
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
192 struct timeval tv;
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
193 int pass, cc;
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
194
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
195 for (pass = 0; pass < expect_bytes; ) {
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
196 FD_ZERO(&fds);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
197 FD_SET(target_fd, &fds);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
198 tv.tv_sec = 0;
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
199 tv.tv_usec = timeout * 1000;
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
200 cc = select(target_fd+1, &fds, NULL, NULL, &tv);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
201 if (cc < 0) {
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
202 if (errno == EINTR)
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
203 continue;
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
204 perror("select");
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
205 exit(1);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
206 }
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
207 if (cc < 1)
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
208 return(-1);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
209 cc = read(target_fd, buf + pass, expect_bytes - pass);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
210 if (cc <= 0) {
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
211 perror("read after successful select");
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
212 exit(1);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
213 }
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
214 pass += cc;
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
215 }
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
216 return(0);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
217 }
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
218
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
219 perform_frbl2_protocol()
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
220 {
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
221 u_char respbuf[4];
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
222 static int zero = 0;
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
223
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
224 ioctl(target_fd, FIONBIO, &zero);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
225 send_beacons();
325
7df08926a4e7 frbl2test: better message for Hello response
Mychaela Falconia <falcon@freecalypso.org>
parents: 324
diff changeset
226 printf("Got Hello response, attempting FLUID protocol\n");
324
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
227 write(target_fd, fluid_baudrate_cmd, sizeof fluid_baudrate_cmd);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
228 usleep(SERIAL_FLUSH_DELAY * 1000);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
229 tcflush(target_fd, TCIFLUSH);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
230 write(target_fd, fluid_version_cmd, sizeof fluid_version_cmd);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
231 if (expect_add_response(respbuf, 1, INTERMEDIATE_TIMEOUT) < 0) {
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
232 fprintf(stderr, "no response to version query\n");
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
233 exit(1);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
234 }
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
235 if (!isdigit(respbuf[0])) {
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
236 fprintf(stderr, "bad response to version query: 0x%02X\n",
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
237 respbuf[0]);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
238 exit(1);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
239 }
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
240 printf("Version response: %d\n", respbuf[0] - '0');
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
241 write(target_fd, fluid_chipquery_cmd, sizeof fluid_chipquery_cmd);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
242 if (expect_add_response(respbuf, 4, INTERMEDIATE_TIMEOUT) < 0) {
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
243 fprintf(stderr, "no response to chip ID query\n");
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
244 exit(1);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
245 }
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
246 if (respbuf[2] || respbuf[3]) {
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
247 fprintf(stderr,
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
248 "bad response to chip ID query: %02X %02X %02X %02X\n",
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
249 respbuf[0], respbuf[1], respbuf[2], respbuf[3]);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
250 exit(1);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
251 }
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
252 printf("Chip ID response: %04X\n", (respbuf[1] << 8) | respbuf[0]);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
253 printf("Sending download command\n");
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
254 write(target_fd, fluid_download_cmd, sizeof fluid_download_cmd);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
255 if (expect_add_response(respbuf, 1, INTERMEDIATE_TIMEOUT) < 0) {
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
256 fprintf(stderr, "no response to download command\n");
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
257 exit(1);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
258 }
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
259 if (respbuf[0] != 'R') {
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
260 fprintf(stderr, "bad response to download command: 0x%02X\n",
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
261 respbuf[0]);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
262 exit(1);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
263 }
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
264 if (write(target_fd, codeimage, codeimage_len) != codeimage_len) {
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
265 perror("write of download image bulk");
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
266 exit(1);
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
267 }
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
268 printf("Code image sent!\n");
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
269 }
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
270
323
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
271 frbl_test_main()
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
272 {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
273 read_srec_image();
324
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
274 fill_download_cmd();
43c92df87ac6 frbl2test: meat filled in
Mychaela Falconia <falcon@freecalypso.org>
parents: 323
diff changeset
275 perform_frbl2_protocol();
323
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
276 }