annotate simtool/readcmd.c @ 91:226b231d00f3

fc-simtool: readrec command implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 24 Jan 2021 18:46:11 +0000
parents 53e2c00566af
children 0ead9444a698
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
90
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 #include <sys/types.h>
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 #include <stdio.h>
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 #include <stdlib.h>
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 #include <pcsclite.h>
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <winscard.h>
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include "globals.h"
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 cmd_readbin(argc, argv)
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 char **argv;
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 {
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 unsigned offset, len;
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 u_char cmd[5];
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 int rc;
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 offset = strtoul(argv[1], 0, 0);
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 if (offset > 0xFFFF) {
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 fprintf(stderr, "error: offset argument is out of range\n");
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 return(-1);
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 }
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 len = strtoul(argv[2], 0, 0);
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 if (len < 1 || len > 256) {
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 fprintf(stderr, "error: length argument is out of range\n");
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 return(-1);
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 }
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 /* READ BINARY command APDU */
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 cmd[0] = 0xA0;
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 cmd[1] = 0xB0;
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 cmd[2] = offset >> 8;
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 cmd[3] = offset;
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 cmd[4] = len;
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 rc = apdu_exchange(cmd, 5);
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 if (rc < 0)
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 return(rc);
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 if (sim_resp_sw != 0x9000) {
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 fprintf(stderr, "bad SW response to READ BINARY: %04X\n",
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 sim_resp_sw);
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 return(-1);
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 }
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 if (sim_resp_data_len != len) {
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 fprintf(stderr,
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 "error: READ BINARY returned %u bytes, expected %u\n",
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 sim_resp_data_len, len);
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 return(-1);
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 }
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 display_sim_resp_in_hex();
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 return(0);
53e2c00566af fc-simtool: readbin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 }
91
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
48
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
49 cmd_readrec(argc, argv)
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
50 char **argv;
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
51 {
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
52 unsigned recno, len;
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
53 u_char cmd[5];
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
54 int rc;
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
55
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
56 recno = strtoul(argv[1], 0, 0);
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
57 if (recno < 1 || recno > 255) {
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
58 fprintf(stderr,
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
59 "error: record number argument is out of range\n");
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
60 return(-1);
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
61 }
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
62 if (argv[2]) {
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
63 len = strtoul(argv[2], 0, 0);
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
64 if (len < 1 || len > 255) {
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
65 fprintf(stderr,
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
66 "error: length argument is out of range\n");
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
67 return(-1);
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
68 }
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
69 } else {
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
70 if (!curfile_record_len) {
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
71 fprintf(stderr,
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
72 "error: no current file record length is available\n");
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
73 return(-1);
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
74 }
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
75 len = curfile_record_len;
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
76 }
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
77 /* READ RECORD command APDU */
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
78 cmd[0] = 0xA0;
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
79 cmd[1] = 0xB2;
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
80 cmd[2] = recno;
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
81 cmd[3] = 0x04;
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
82 cmd[4] = len;
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
83 rc = apdu_exchange(cmd, 5);
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
84 if (rc < 0)
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
85 return(rc);
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
86 if (sim_resp_sw != 0x9000) {
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
87 fprintf(stderr, "bad SW response to READ RECORD: %04X\n",
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
88 sim_resp_sw);
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
89 return(-1);
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
90 }
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
91 if (sim_resp_data_len != len) {
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
92 fprintf(stderr,
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
93 "error: READ RECORD returned %u bytes, expected %u\n",
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
94 sim_resp_data_len, len);
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
95 return(-1);
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
96 }
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
97 display_sim_resp_in_hex();
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
98 return(0);
226b231d00f3 fc-simtool: readrec command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 90
diff changeset
99 }