FreeCalypso > hg > freecalypso-tools
comparison target-utils/simagent/simup.c @ 774:130c46b83760
simagent: sim-up command fully implemented
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Fri, 12 Mar 2021 21:52:09 +0000 |
parents | 81f9e4b4f55c |
children | 6ec781e61e68 |
comparison
equal
deleted
inserted
replaced
773:6d1b22d4926f | 774:130c46b83760 |
---|---|
1 #include <sys/types.h> | 1 #include <sys/types.h> |
2 #include <strings.h> | 2 #include <strings.h> |
3 #include "types.h" | 3 #include "types.h" |
4 #include "abbdefs.h" | 4 #include "abbdefs.h" |
5 #include "simregs.h" | 5 #include "simregs.h" |
6 #include "timeout.h" | |
7 | |
8 #define MAX_ATR_BYTES 33 | |
6 | 9 |
7 #define WAIT_ONE_TDMA 60000 | 10 #define WAIT_ONE_TDMA 60000 |
8 | 11 |
9 extern u16 abb_reg_read(); | 12 extern u16 abb_reg_read(); |
10 extern void abb_reg_write(); | 13 extern void abb_reg_write(); |
11 | 14 |
15 extern const u8 inverse_coding_table[256]; | |
16 | |
12 int sim_if_state; | 17 int sim_if_state; |
13 u16 conf1_reg; | 18 u16 conf1_reg; |
19 u8 atr_buf[MAX_ATR_BYTES]; | |
20 unsigned atr_length; | |
21 int inverse_coding; | |
22 | |
23 void | |
24 print_atr() | |
25 { | |
26 unsigned n; | |
27 | |
28 printf("ATR:"); | |
29 for (n = 0; n < atr_length; n++) | |
30 printf(" %02X", atr_buf[n]); | |
31 putchar('\n'); | |
32 } | |
33 | |
34 static | |
35 rx_atr_byte() | |
36 { | |
37 int rc; | |
38 | |
39 rc = rx_sim_byte(SIM_WAIT_TIMEOUT); | |
40 if (rc < 0) { | |
41 printf("ERROR: timeout waiting for subsequent byte of ATR\n"); | |
42 return(-1); | |
43 } | |
44 rc &= 0xFF; | |
45 if (inverse_coding) | |
46 rc = inverse_coding_table[rc]; | |
47 atr_buf[atr_length++] = rc; | |
48 return rc; | |
49 } | |
14 | 50 |
15 void | 51 void |
16 cmd_sim_up(argbulk) | 52 cmd_sim_up(argbulk) |
17 char *argbulk; | 53 char *argbulk; |
18 { | 54 { |
19 char *argv[2]; | 55 char *argv[2]; |
20 u16 abb_sim_reg; | 56 u16 abb_sim_reg; |
21 unsigned count; | 57 unsigned count, y, nhist, have_tck; |
58 int rc; | |
22 | 59 |
23 if (sim_if_state) { | 60 if (sim_if_state) { |
24 printf("ERROR: SIM interface is already up\n"); | 61 printf("ERROR: SIM interface is already up\n"); |
25 return; | 62 return; |
26 } | 63 } |
87 if (++count >= 32) { | 124 if (++count >= 32) { |
88 printf("ERROR: Rx FIFO flush does not end\n"); | 125 printf("ERROR: Rx FIFO flush does not end\n"); |
89 return; | 126 return; |
90 } | 127 } |
91 } | 128 } |
92 | |
93 #if 0 | |
94 /* lift the card out of reset! */ | 129 /* lift the card out of reset! */ |
95 SIMREGS.conf1 = conf1_reg |= SIM_CONF1_SRSTLEV; | 130 SIMREGS.conf1 = conf1_reg |= SIM_CONF1_SRSTLEV; |
96 #endif | 131 |
132 /* first byte of ATR */ | |
133 rc = rx_sim_byte(SIM_WAIT_TIMEOUT); | |
134 if (rc < 0) { | |
135 printf("ERROR: timeout waiting for first byte of ATR\n"); | |
136 return; | |
137 } | |
138 rc &= 0xFF; | |
139 if (rc == 0x3B) { | |
140 /* direct convention */ | |
141 inverse_coding = 0; | |
142 atr_buf[0] = 0x3B; | |
143 } else if (rc == 0x03) { | |
144 /* inverse convention */ | |
145 inverse_coding = 1; | |
146 atr_buf[0] = 0x3F; | |
147 } else { | |
148 printf( | |
149 "ERROR: received TS=0x%02X, matches neither convention\n", | |
150 rc); | |
151 return; | |
152 } | |
153 atr_length = 1; | |
154 | |
155 /* remainder of ATR, starting with T0 */ | |
156 rc = rx_atr_byte(); | |
157 if (rc < 0) | |
158 return; | |
159 nhist = rc & 0xF; | |
160 y = rc & 0xF0; | |
161 have_tck = 0; | |
162 while (y) { | |
163 if (y & 0x10) { | |
164 if (atr_length >= MAX_ATR_BYTES) { | |
165 atr_too_long: printf("ERROR: ATR exceeds 33 byte limit\n"); | |
166 return; | |
167 } | |
168 rc = rx_atr_byte(); | |
169 if (rc < 0) | |
170 return; | |
171 } | |
172 if (y & 0x20) { | |
173 if (atr_length >= MAX_ATR_BYTES) | |
174 goto atr_too_long; | |
175 rc = rx_atr_byte(); | |
176 if (rc < 0) | |
177 return; | |
178 } | |
179 if (y & 0x40) { | |
180 if (atr_length >= MAX_ATR_BYTES) | |
181 goto atr_too_long; | |
182 rc = rx_atr_byte(); | |
183 if (rc < 0) | |
184 return; | |
185 } | |
186 if (y & 0x80) { | |
187 if (atr_length >= MAX_ATR_BYTES) | |
188 goto atr_too_long; | |
189 rc = rx_atr_byte(); | |
190 if (rc < 0) | |
191 return; | |
192 y = rc & 0xF0; | |
193 if (rc & 0x0F) | |
194 have_tck = 1; | |
195 } else | |
196 y = 0; | |
197 } | |
198 for (count = 0; count < nhist + have_tck; count++) { | |
199 if (atr_length >= MAX_ATR_BYTES) | |
200 goto atr_too_long; | |
201 rc = rx_atr_byte(); | |
202 if (rc < 0) | |
203 return; | |
204 } | |
205 | |
206 /* all good! */ | |
207 sim_if_state = 2; | |
208 print_atr(); | |
97 } | 209 } |