FreeCalypso > hg > fc-pcsc-tools
annotate simtool/bfsearch.c @ 148:db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Thu, 25 Feb 2021 07:00:51 +0000 |
parents | 8d55571c8118 |
children | 26d7a8815515 |
rev | line source |
---|---|
145
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This module implements a brute force search of file ID space at a given |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * file system directory level. |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 */ |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <sys/types.h> |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <ctype.h> |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <stdio.h> |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <stdlib.h> |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include "simresp.h" |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 static |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 parse_skip_ids(argv, array, total) |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 char **argv; |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 unsigned *array, total; |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 { |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 unsigned n; |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 for (n = 0; n < total; n++) { |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 if (!isxdigit(argv[n][0]) || !isxdigit(argv[n][1]) || |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 !isxdigit(argv[n][2]) || !isxdigit(argv[n][3]) || |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 argv[n][4]) { |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 fprintf(stderr, "error: argument is not 4-digit hex\n"); |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 return(-1); |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 } |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 array[n] = strtoul(argv[n], 0, 16); |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 } |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 return(0); |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 } |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 |
148
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
31 static void |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
32 report_ef_struct(outf) |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
33 FILE *outf; |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
34 { |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
35 unsigned total_size, record_len; |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
36 |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
37 fputs("EF, ", outf); |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
38 total_size = (sim_resp_data[2] << 8) | sim_resp_data[3]; |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
39 switch (sim_resp_data[13]) { |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
40 case 0x00: |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
41 fprintf(outf, "transparent, length %u\n", total_size); |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
42 return; |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
43 case 0x01: |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
44 fputs("linear fixed, ", outf); |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
45 break; |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
46 case 0x03: |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
47 fputs("cyclic, ", outf); |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
48 break; |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
49 default: |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
50 fprintf(outf, "struct 0x%02X\n", sim_resp_data[13]); |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
51 return; |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
52 } |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
53 if (sim_resp_data_len < 15) { |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
54 fprintf(outf, "response struct cut off\n"); |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
55 return; |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
56 } |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
57 record_len = sim_resp_data[14]; |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
58 fprintf(outf, "record length %u", record_len); |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
59 if (record_len && total_size % record_len == 0) |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
60 fprintf(outf, ", %u records", total_size / record_len); |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
61 putc('\n', outf); |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
62 } |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
63 |
145
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 cmd_bfsearch(argc, argv, outf) |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 char **argv; |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 FILE *outf; |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 { |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 unsigned skip_ids[8], num_skip_ids; |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 unsigned bfs, n; |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 int rc; |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 num_skip_ids = argc - 1; |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 rc = parse_skip_ids(argv + 1, skip_ids, num_skip_ids); |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 if (rc < 0) |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 return(rc); |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 rc = elem_select_op(skip_ids[0]); |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 if (rc < 0) |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 return(rc); |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 if (!rc) { |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 fprintf(stderr, "error: starting file ID 0x%04X not found\n", |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 skip_ids[0]); |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 return(-1); |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 } |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 for (bfs = 0; bfs <= 0xFFFF; bfs++) { |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 for (n = 0; n < num_skip_ids; n++) { |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 if (bfs == skip_ids[n]) |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
87 break; |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 } |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 if (n < num_skip_ids) |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
90 continue; |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
91 rc = elem_select_op(bfs); |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
92 if (rc < 0) |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
93 return(rc); |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
94 if (!rc) |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
95 continue; |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
96 rc = get_response_op(); |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
97 if (rc < 0) |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
98 return(rc); |
148
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
99 fprintf(outf, "%04X: ", bfs); |
145
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
100 if (sim_resp_data_len < 14) |
148
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
101 fprintf(outf, "too-short response struct\n"); |
145
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
102 else { |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
103 switch (sim_resp_data[6]) { |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
104 case 0x01: |
148
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
105 fprintf(outf, "MF\n"); |
145
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
106 break; |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
107 case 0x02: |
148
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
108 fprintf(outf, "DF\n"); |
145
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
109 break; |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
110 case 0x04: |
148
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
111 report_ef_struct(outf); |
145
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
112 break; |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
113 default: |
148
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
114 fprintf(outf, "unknown file type %02X\n", |
db43bc57ecf7
fc-simtool bfsearch: better reporting of EF structure
Mychaela Falconia <falcon@freecalypso.org>
parents:
146
diff
changeset
|
115 sim_resp_data[6]); |
145
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
116 } |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
117 } |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
118 rc = elem_select_op(skip_ids[0]); |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
119 if (rc < 0) |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
120 return(rc); |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
121 if (!rc) { |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
122 fprintf(stderr, |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
123 "reselecting starting file ID 0x%04X not-found error\n", |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
124 skip_ids[0]); |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
125 return(-1); |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
126 } |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
127 } |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
128 return(0); |
c2889812788e
fc-simtool bfsearch implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
129 } |