annotate calypso/main.c @ 4:deeeef558279

fcsim-calypso-be put together
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 14 Mar 2021 05:07:34 +0000
parents
children f6b03af63bf7
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);
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 putchar('\n');
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 while (fgets(inbuf, sizeof inbuf, stdin)) {
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 cp = index(inbuf, '\n');
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 if (!cp) {
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 printf("back end error: missing newline on input\n");
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 continue;
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 }
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 *cp = '\0';
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 if (!strcmp(inbuf, "atr")) {
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 cmd_atr();
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 continue;
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 }
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 if (!strcmp(inbuf, "poweroff")) {
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 cmd_poweroff();
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 exit(0);
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 }
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 if (!is_string_all_hex(inbuf)) {
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 printf("back end error: input is not all hex\n");
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 continue;
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 }
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 len = strlen(inbuf);
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 if (len & 1) {
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 printf(
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 "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
88 continue;
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 }
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 if (len < 10) {
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 printf(
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 "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
93 continue;
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 }
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 if (len > 520) {
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 printf(
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 "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
98 continue;
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 }
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 cmd_exchange(inbuf);
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
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 exit(0);
deeeef558279 fcsim-calypso-be put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 }