FreeCalypso > hg > freecalypso-hwlab
annotate simtool/select.c @ 85:b57cf64ece29
fc-simtool project started
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 24 Jan 2021 00:32:10 +0000 |
parents | |
children | 54c444eb084b |
rev | line source |
---|---|
85
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 #include <sys/types.h> |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 #include <ctype.h> |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 #include <string.h> |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 #include <strings.h> |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 #include <stdio.h> |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <stdlib.h> |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <pcsclite.h> |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <winscard.h> |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include "globals.h" |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 select_op(file_id) |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 unsigned file_id; |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 { |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 u_char cmd[7]; |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 int rc; |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 unsigned expect_resp_len; |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 /* SELECT command APDU */ |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 cmd[0] = 0xA0; |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 cmd[1] = 0xA4; |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 cmd[2] = 0; |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 cmd[3] = 0; |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 cmd[4] = 2; |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 cmd[5] = file_id >> 8; |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 cmd[6] = file_id; |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 rc = apdu_exchange(cmd, 7); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 if (rc < 0) |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 return(rc); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 if ((sim_resp_sw & 0xFF00) != 0x9F00) { |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 fprintf(stderr, |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 "error or unexpected SW response to SELECT of 0x%04X: %04X\n", |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 file_id, sim_resp_sw); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 return(-1); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 } |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 expect_resp_len = sim_resp_sw & 0xFF; |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 /* GET RESPONSE follow-up */ |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 cmd[1] = 0xC0; |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 cmd[4] = expect_resp_len; |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 rc = apdu_exchange(cmd, 5); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 if (rc < 0) |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 return(rc); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 if (sim_resp_sw != 0x9000) { |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 fprintf(stderr, |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 "bad SW resp to GET RESPONSE after SELECT: %04X\n", |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 sim_resp_sw); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 return(-1); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 } |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 if (sim_resp_data_len != expect_resp_len) { |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 fprintf(stderr, |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 "error: GET RESPONSE after SELECT returned %u bytes, expected %u\n", |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 sim_resp_data_len, expect_resp_len); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 return(-1); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 } |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 return(0); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 } |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 cmd_select(argc, argv) |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 char **argv; |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 { |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 unsigned file_id; |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 if (isxdigit(argv[1][0]) && isxdigit(argv[1][1]) && |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 isxdigit(argv[1][2]) && isxdigit(argv[1][3]) && !argv[1][4]) |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 file_id = strtoul(argv[1], 0, 16); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 else { |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 fprintf(stderr, |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 "select: only hex file IDs are currently supported\n"); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 return(-1); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 } |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 return select_op(file_id); |
b57cf64ece29
fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 } |