annotate loadtools/simatr.c @ 921:74d284add54d

fc-fsio: guard against bogus readdir results from the target If the FFS being operated on contains SE K2x0 extended filenames, readdir will return strings that are bad for printing. We need to guard against this possibility, and also against possible other bogosity that could be sent by other alien firmwares.
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 31 Dec 2022 22:55:23 +0000
parents a31ae776de6e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
790
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module implements the stage in fc-simint where ATR bytes
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * received from simagent are validated.
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/types.h>
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdio.h>
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdlib.h>
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 extern int sim_allow_spenh;
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 extern u_char sim_atr[];
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 extern unsigned sim_atr_len;
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 void
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 sim_atr_validate()
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 {
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 unsigned b, p, y, nhist, have_tck;
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 if (sim_atr_len < 2) {
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 too_short: fprintf(stderr, "error: ATR from simagent is too short\n");
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 exit(1);
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 }
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 b = sim_atr[1];
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 p = 2;
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 nhist = b & 0xF;
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 y = b & 0xF0;
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 have_tck = 0;
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 while (y) {
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 if (y & 0x10) {
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 if (p >= sim_atr_len)
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 goto too_short;
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 p++;
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 }
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 if (y & 0x20) {
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 if (p >= sim_atr_len)
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 goto too_short;
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 p++;
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 }
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 if (y & 0x40) {
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 if (p >= sim_atr_len)
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 goto too_short;
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 p++;
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 }
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 if (y & 0x80) {
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 if (p >= sim_atr_len)
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 goto too_short;
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 b = sim_atr[p++];
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 y = b & 0xF0;
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 if (b & 0x0F)
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 have_tck = 1;
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 } else
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 y = 0;
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 }
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 p += nhist + have_tck;
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 if (p > sim_atr_len)
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 goto too_short;
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 if (p < sim_atr_len) {
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 fprintf(stderr, "error: ATR from simagent is too long\n");
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 exit(1);
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 }
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 if (!have_tck)
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 return;
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 /* validate TCK */
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 b = 0;
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 for (p = 1; p < sim_atr_len; p++)
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 b ^= sim_atr[p];
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 if (b) {
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 fprintf(stderr, "error: ATR checksum is bad\n");
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 exit(1);
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 }
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 }
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 void
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 sim_spenh_logic()
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 {
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 static char *targv[2] = {"spenh", 0};
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 if (sim_atr_len < 3 || !(sim_atr[1] & 0x10) || (sim_atr[2] < 0x94)) {
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 printf("ATR indicates no support for speed enhancement\n");
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 return;
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 }
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 if (!sim_allow_spenh) {
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 printf("Speed enhancement disabled with -n option\n");
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 return;
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 }
791
a31ae776de6e fc-simint: tell the user when we are requesting spenh
Mychaela Falconia <falcon@freecalypso.org>
parents: 790
diff changeset
87 printf("Requesting SIM speed enhancement\n");
790
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 tpinterf_make_cmd(targv);
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 if (tpinterf_send_cmd() < 0)
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 exit(1);
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 if (tpinterf_pass_output(10))
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 exit(1);
0bbe0213812d fc-simint put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 }