annotate simtool/readcmd.c @ 90:53e2c00566af

fc-simtool: readbin command implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 24 Jan 2021 18:18:04 +0000
parents
children 226b231d00f3
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 }