annotate calypso/main.c @ 63:f4eb486aab40

doc/Admin-write-commands: fix verify-hex command name
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 23 Mar 2021 23:29:31 +0000
parents 2260fbd28b2a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 #include <ctype.h>
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 #include <stdio.h>
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 #include <stdlib.h>
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 #include <string.h>
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <strings.h>
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 static
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 is_string_all_hex(str)
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 char *str;
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 {
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 char *cp;
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 for (cp = str; *cp; cp++)
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 if (!isxdigit(*cp))
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 return(0);
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 return(1);
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 }
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 cmd_exchange(input)
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 char *input;
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 {
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 char *targv[3];
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 int rc;
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 targv[0] = "X";
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 targv[1] = input;
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 targv[2] = 0;
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 tpinterf_make_cmd(targv);
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 rc = tpinterf_send_cmd();
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 if (rc < 0)
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 return(rc);
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 return tpinterf_pass_output(20);
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 }
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 cmd_atr()
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 {
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 static char *atr_argv[2] = {"atr", 0};
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 int rc;
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 tpinterf_make_cmd(atr_argv);
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 rc = tpinterf_send_cmd();
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 if (rc < 0)
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 return(rc);
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 return tpinterf_pass_output(1);
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 }
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 cmd_poweroff()
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 {
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 static char *poweroff_argv[2] = {"poweroff", 0};
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 tpinterf_make_cmd(poweroff_argv);
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 tpinterf_send_cmd();
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 }
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 main(argc, argv)
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 char **argv;
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 {
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 char inbuf[576], *cp;
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 unsigned len;
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 parse_target_fd_opt(argc, argv);
5
f6b03af63bf7 fcsim-calypso-be: add FIONBIO setting to 0
Mychaela Falconia <falcon@freecalypso.org>
parents: 4
diff changeset
63 set_serial_nonblock(0);
4
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 putchar('\n');
12
2260fbd28b2a fcsim-calypso-be: flush stdout
Mychaela Falconia <falcon@freecalypso.org>
parents: 5
diff changeset
65 fflush(stdout);
4
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66
12
2260fbd28b2a fcsim-calypso-be: flush stdout
Mychaela Falconia <falcon@freecalypso.org>
parents: 5
diff changeset
67 for (; fgets(inbuf, sizeof inbuf, stdin); fflush(stdout)) {
4
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 cp = index(inbuf, '\n');
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 if (!cp) {
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 printf("back end error: missing newline on input\n");
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 continue;
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 }
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 *cp = '\0';
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 if (!strcmp(inbuf, "atr")) {
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 cmd_atr();
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 continue;
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 }
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 if (!strcmp(inbuf, "poweroff")) {
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 cmd_poweroff();
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 exit(0);
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 }
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 if (!is_string_all_hex(inbuf)) {
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 printf("back end error: input is not all hex\n");
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 continue;
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 }
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 len = strlen(inbuf);
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 if (len & 1) {
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 printf(
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 "back end error: input has odd number of hex digits\n");
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 continue;
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 }
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 if (len < 10) {
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 printf(
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 "back end error: input is too short for command APDU\n");
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 continue;
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 }
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 if (len > 520) {
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 printf(
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 "back end error: input is too long for command APDU\n");
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 continue;
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 }
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 cmd_exchange(inbuf);
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 }
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 exit(0);
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 }